PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/source3/nmbd/nmbd_workgroupdb.c

https://gitlab.com/miztake/samba
C | 327 lines | 199 code | 64 blank | 64 comment | 47 complexity | 896616199076135ec4abef8e8f11afa4 MD5 | raw file
  1. /*
  2. Unix SMB/CIFS implementation.
  3. NBT netbios routines and daemon - version 2
  4. Copyright (C) Andrew Tridgell 1994-1998
  5. Copyright (C) Luke Kenneth Casson Leighton 1994-1998
  6. Copyright (C) Jeremy Allison 1994-1998
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "includes.h"
  19. #include "../librpc/gen_ndr/svcctl.h"
  20. #include "nmbd/nmbd.h"
  21. #include "lib/util/string_wrappers.h"
  22. extern uint16_t samba_nb_type;
  23. int workgroup_count = 0; /* unique index key: one for each workgroup */
  24. /****************************************************************************
  25. Add a workgroup into the list.
  26. **************************************************************************/
  27. static void add_workgroup(struct subnet_record *subrec, struct work_record *work)
  28. {
  29. work->subnet = subrec;
  30. DLIST_ADD(subrec->workgrouplist, work);
  31. subrec->work_changed = True;
  32. }
  33. /****************************************************************************
  34. Copy name to unstring. Used by create_workgroup() and find_workgroup_on_subnet().
  35. **************************************************************************/
  36. static void name_to_unstring(unstring unname, const char *name)
  37. {
  38. nstring nname;
  39. errno = 0;
  40. push_ascii_nstring(nname, name);
  41. if (errno == E2BIG) {
  42. unstring tname;
  43. pull_ascii_nstring(tname, sizeof(tname), nname);
  44. strlcpy(unname, tname, sizeof(nname));
  45. DEBUG(0,("name_to_nstring: workgroup name %s is too long. Truncating to %s\n",
  46. name, tname));
  47. } else {
  48. unstrcpy(unname, name);
  49. }
  50. }
  51. /****************************************************************************
  52. Create an empty workgroup.
  53. **************************************************************************/
  54. static struct work_record *create_workgroup(const char *name, int ttl)
  55. {
  56. struct work_record *work;
  57. struct subnet_record *subrec;
  58. int t = -1;
  59. if((work = SMB_MALLOC_P(struct work_record)) == NULL) {
  60. DEBUG(0,("create_workgroup: malloc fail !\n"));
  61. return NULL;
  62. }
  63. memset((char *)work, '\0', sizeof(*work));
  64. name_to_unstring(work->work_group, name);
  65. work->serverlist = NULL;
  66. work->RunningElection = False;
  67. work->ElectionCount = 0;
  68. work->announce_interval = 0;
  69. work->needelection = False;
  70. work->needannounce = True;
  71. work->lastannounce_time = time(NULL);
  72. work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
  73. work->dom_state = DOMAIN_NONE;
  74. work->log_state = LOGON_NONE;
  75. work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
  76. /* Make sure all token representations of workgroups are unique. */
  77. for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
  78. struct work_record *w;
  79. for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
  80. if (strequal(w->work_group, work->work_group))
  81. t = w->token;
  82. }
  83. }
  84. if (t == -1)
  85. work->token = ++workgroup_count;
  86. else
  87. work->token = t;
  88. /* No known local master browser as yet. */
  89. *work->local_master_browser_name = '\0';
  90. /* No known domain master browser as yet. */
  91. *work->dmb_name.name = '\0';
  92. zero_ip_v4(&work->dmb_addr);
  93. /* WfWg uses 01040b01 */
  94. /* Win95 uses 01041501 */
  95. /* NTAS uses ???????? */
  96. work->ElectionCriterion = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8);
  97. work->ElectionCriterion |= (lp_os_level() << 24);
  98. if (lp_domain_master())
  99. work->ElectionCriterion |= 0x80;
  100. return work;
  101. }
  102. /*******************************************************************
  103. Remove a workgroup.
  104. ******************************************************************/
  105. static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec,
  106. struct work_record *work)
  107. {
  108. struct work_record *ret_work = NULL;
  109. DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
  110. ret_work = work->next;
  111. remove_all_servers(work);
  112. if (!work->serverlist) {
  113. DLIST_REMOVE(subrec->workgrouplist, work);
  114. ZERO_STRUCTP(work);
  115. SAFE_FREE(work);
  116. }
  117. subrec->work_changed = True;
  118. return ret_work;
  119. }
  120. /****************************************************************************
  121. Find a workgroup in the workgroup list of a subnet.
  122. **************************************************************************/
  123. struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec,
  124. const char *name)
  125. {
  126. struct work_record *ret;
  127. unstring un_name;
  128. DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
  129. name, subrec->subnet_name));
  130. name_to_unstring(un_name, name);
  131. for (ret = subrec->workgrouplist; ret; ret = ret->next) {
  132. if (strequal(ret->work_group,un_name)) {
  133. DEBUGADD(4, ("found.\n"));
  134. return(ret);
  135. }
  136. }
  137. DEBUGADD(4, ("not found.\n"));
  138. return NULL;
  139. }
  140. /****************************************************************************
  141. Create a workgroup in the workgroup list of the subnet.
  142. **************************************************************************/
  143. struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
  144. const char *name, int ttl)
  145. {
  146. struct work_record *work = NULL;
  147. DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
  148. name, subrec->subnet_name));
  149. if ((work = create_workgroup(name, ttl))) {
  150. add_workgroup(subrec, work);
  151. subrec->work_changed = True;
  152. return(work);
  153. }
  154. return NULL;
  155. }
  156. /****************************************************************************
  157. Update a workgroup ttl.
  158. **************************************************************************/
  159. void update_workgroup_ttl(struct work_record *work, int ttl)
  160. {
  161. if(work->death_time != PERMANENT_TTL)
  162. work->death_time = time(NULL)+(ttl*3);
  163. work->subnet->work_changed = True;
  164. }
  165. /****************************************************************************
  166. Fail function called if we cannot register the WORKGROUP<0> and
  167. WORKGROUP<1e> names on the net.
  168. **************************************************************************/
  169. static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
  170. struct nmb_name *nmbname)
  171. {
  172. DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
  173. nmb_namestr(nmbname), subrec->subnet_name));
  174. }
  175. /****************************************************************************
  176. If the workgroup is our primary workgroup, add the required names to it.
  177. **************************************************************************/
  178. void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
  179. {
  180. const struct loadparm_substitution *lp_sub =
  181. loadparm_s3_global_substitution();
  182. int i;
  183. if(!strequal(lp_workgroup(), work->work_group))
  184. return;
  185. /* If this is a broadcast subnet then start elections on it if we are so configured. */
  186. if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
  187. (subrec != wins_server_subnet) && lp_preferred_master() && lp_local_master()) {
  188. DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
  189. workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
  190. work->needelection = True;
  191. work->ElectionCriterion |= (1<<3);
  192. }
  193. /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
  194. register_name(subrec,lp_workgroup(),0x0,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
  195. register_name(subrec,lp_workgroup(),0x1e,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
  196. for( i = 0; my_netbios_names(i); i++) {
  197. const char *name = my_netbios_names(i);
  198. int stype = lp_default_server_announce() | (lp_local_master() ? SV_TYPE_POTENTIAL_BROWSER : 0 );
  199. if(!strequal(lp_netbios_name(), name))
  200. stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
  201. create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY, PERMANENT_TTL,
  202. string_truncate(lp_server_string(talloc_tos(), lp_sub), MAX_SERVER_STRING_LENGTH));
  203. DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
  204. on subnet %s\n", name, subrec->subnet_name));
  205. }
  206. }
  207. /****************************************************************************
  208. Dump a copy of the workgroup database into the log file.
  209. **************************************************************************/
  210. void dump_workgroups(bool force_write)
  211. {
  212. struct subnet_record *subrec;
  213. int debuglevel = force_write ? 0 : 4;
  214. for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
  215. if (subrec->workgrouplist) {
  216. struct work_record *work;
  217. if( DEBUGLVL( debuglevel ) ) {
  218. dbgtext( "dump_workgroups()\n " );
  219. dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
  220. dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
  221. }
  222. for (work = subrec->workgrouplist; work; work = work->next) {
  223. DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n", work->work_group,
  224. work->token, *work->local_master_browser_name ? work->local_master_browser_name : "UNKNOWN" ) );
  225. if (work->serverlist) {
  226. struct server_record *servrec;
  227. for (servrec = work->serverlist; servrec; servrec = servrec->next) {
  228. DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
  229. servrec->serv.name,
  230. servrec->serv.type,
  231. servrec->serv.comment ) );
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. /****************************************************************************
  239. Expire any dead servers on all workgroups. If the workgroup has expired
  240. remove it.
  241. **************************************************************************/
  242. void expire_workgroups_and_servers(time_t t)
  243. {
  244. struct subnet_record *subrec;
  245. for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
  246. struct work_record *work;
  247. struct work_record *nextwork;
  248. for (work = subrec->workgrouplist; work; work = nextwork) {
  249. nextwork = work->next;
  250. expire_servers(work, t);
  251. if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) &&
  252. ((t == (time_t)-1) || (work->death_time < t))) {
  253. DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
  254. work->work_group));
  255. remove_workgroup_from_subnet(subrec, work);
  256. }
  257. }
  258. }
  259. }