PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/source3/nmbd/nmbd_serverlistdb.c

https://repo.or.cz/Samba/ita.git
C | 397 lines | 227 code | 77 blank | 93 comment | 54 complexity | ce39571094aae3cea9efdd6b861da865 MD5 | raw file
Possible License(s): GPL-3.0
  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. int updatecount = 0;
  22. /*******************************************************************
  23. Remove all the servers in a work group.
  24. ******************************************************************/
  25. void remove_all_servers(struct work_record *work)
  26. {
  27. struct server_record *servrec;
  28. struct server_record *nexts;
  29. for (servrec = work->serverlist; servrec; servrec = nexts) {
  30. DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name));
  31. nexts = servrec->next;
  32. DLIST_REMOVE(work->serverlist, servrec);
  33. ZERO_STRUCTP(servrec);
  34. SAFE_FREE(servrec);
  35. }
  36. work->subnet->work_changed = True;
  37. }
  38. /***************************************************************************
  39. Add a server into the a workgroup serverlist.
  40. **************************************************************************/
  41. static void add_server_to_workgroup(struct work_record *work,
  42. struct server_record *servrec)
  43. {
  44. DLIST_ADD_END(work->serverlist, servrec, struct server_record *);
  45. work->subnet->work_changed = True;
  46. }
  47. /****************************************************************************
  48. Find a server in a server list.
  49. **************************************************************************/
  50. struct server_record *find_server_in_workgroup(struct work_record *work, const char *name)
  51. {
  52. struct server_record *ret;
  53. for (ret = work->serverlist; ret; ret = ret->next) {
  54. if (strequal(ret->serv.name,name))
  55. return ret;
  56. }
  57. return NULL;
  58. }
  59. /****************************************************************************
  60. Remove a server entry from this workgroup.
  61. ****************************************************************************/
  62. void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec)
  63. {
  64. DLIST_REMOVE(work->serverlist, servrec);
  65. ZERO_STRUCTP(servrec);
  66. SAFE_FREE(servrec);
  67. work->subnet->work_changed = True;
  68. }
  69. /****************************************************************************
  70. Create a server entry on this workgroup.
  71. ****************************************************************************/
  72. struct server_record *create_server_on_workgroup(struct work_record *work,
  73. const char *name,int servertype,
  74. int ttl, const char *comment)
  75. {
  76. struct server_record *servrec;
  77. if (name[0] == '*') {
  78. DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
  79. name));
  80. return (NULL);
  81. }
  82. if(find_server_in_workgroup(work, name) != NULL) {
  83. DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
  84. workgroup %s. This is a bug.\n", name, work->work_group));
  85. return NULL;
  86. }
  87. if((servrec = SMB_MALLOC_P(struct server_record)) == NULL) {
  88. DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
  89. return NULL;
  90. }
  91. memset((char *)servrec,'\0',sizeof(*servrec));
  92. servrec->subnet = work->subnet;
  93. fstrcpy(servrec->serv.name,name);
  94. fstrcpy(servrec->serv.comment,comment);
  95. strupper_m(servrec->serv.name);
  96. servrec->serv.type = servertype;
  97. update_server_ttl(servrec, ttl);
  98. add_server_to_workgroup(work, servrec);
  99. DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \
  100. workgroup %s.\n", name,servertype,comment, work->work_group));
  101. work->subnet->work_changed = True;
  102. return(servrec);
  103. }
  104. /*******************************************************************
  105. Update the ttl field of a server record.
  106. *******************************************************************/
  107. void update_server_ttl(struct server_record *servrec, int ttl)
  108. {
  109. if(ttl > lp_max_ttl())
  110. ttl = lp_max_ttl();
  111. if(is_myname(servrec->serv.name))
  112. servrec->death_time = PERMANENT_TTL;
  113. else
  114. servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
  115. servrec->subnet->work_changed = True;
  116. }
  117. /*******************************************************************
  118. Expire old servers in the serverlist. A time of -1 indicates
  119. everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
  120. This should only be called from expire_workgroups_and_servers().
  121. ******************************************************************/
  122. void expire_servers(struct work_record *work, time_t t)
  123. {
  124. struct server_record *servrec;
  125. struct server_record *nexts;
  126. for (servrec = work->serverlist; servrec; servrec = nexts) {
  127. nexts = servrec->next;
  128. if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t))) {
  129. DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name));
  130. remove_server_from_workgroup(work, servrec);
  131. work->subnet->work_changed = True;
  132. }
  133. }
  134. }
  135. /*******************************************************************
  136. Decide if we should write out a server record for this server.
  137. We return zero if we should not. Check if we've already written
  138. out this server record from an earlier subnet.
  139. ******************************************************************/
  140. static uint32 write_this_server_name( struct subnet_record *subrec,
  141. struct work_record *work,
  142. struct server_record *servrec)
  143. {
  144. struct subnet_record *ssub;
  145. struct work_record *iwork;
  146. /* Go through all the subnets we have already seen. */
  147. for (ssub = FIRST_SUBNET; ssub && (ssub != subrec); ssub = NEXT_SUBNET_INCLUDING_UNICAST(ssub)) {
  148. for(iwork = ssub->workgrouplist; iwork; iwork = iwork->next) {
  149. if(find_server_in_workgroup( iwork, servrec->serv.name) != NULL) {
  150. /*
  151. * We have already written out this server record, don't
  152. * do it again. This gives precedence to servers we have seen
  153. * on the broadcast subnets over servers that may have been
  154. * added via a sync on the unicast_subet.
  155. *
  156. * The correct way to do this is to have a serverlist file
  157. * per subnet - this means changes to smbd as well. I may
  158. * add this at a later date (JRA).
  159. */
  160. return 0;
  161. }
  162. }
  163. }
  164. return servrec->serv.type;
  165. }
  166. /*******************************************************************
  167. Decide if we should write out a workgroup record for this workgroup.
  168. We return zero if we should not. Don't write out lp_workgroup() (we've
  169. already done it) and also don't write out a second workgroup record
  170. on the unicast subnet that we've already written out on one of the
  171. broadcast subnets.
  172. ******************************************************************/
  173. static uint32 write_this_workgroup_name( struct subnet_record *subrec,
  174. struct work_record *work)
  175. {
  176. struct subnet_record *ssub;
  177. if(strequal(lp_workgroup(), work->work_group))
  178. return 0;
  179. /* This is a workgroup we have seen on a broadcast subnet. All
  180. these have the same type. */
  181. if(subrec != unicast_subnet)
  182. return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
  183. for(ssub = FIRST_SUBNET; ssub; ssub = NEXT_SUBNET_EXCLUDING_UNICAST(ssub)) {
  184. /* This is the unicast subnet so check if we've already written out
  185. this subnet when we passed over the broadcast subnets. */
  186. if(find_workgroup_on_subnet( ssub, work->work_group) != NULL)
  187. return 0;
  188. }
  189. /* All workgroups on the unicast subnet (except our own, which we
  190. have already written out) cannot be local. */
  191. return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT);
  192. }
  193. /*******************************************************************
  194. Write out the browse.dat file.
  195. ******************************************************************/
  196. void write_browse_list_entry(XFILE *fp, const char *name, uint32 rec_type,
  197. const char *local_master_browser_name, const char *description)
  198. {
  199. fstring tmp;
  200. slprintf(tmp,sizeof(tmp)-1, "\"%s\"", name);
  201. x_fprintf(fp, "%-25s ", tmp);
  202. x_fprintf(fp, "%08x ", rec_type);
  203. slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", local_master_browser_name);
  204. x_fprintf(fp, "%-30s", tmp);
  205. x_fprintf(fp, "\"%s\"\n", description);
  206. }
  207. void write_browse_list(time_t t, bool force_write)
  208. {
  209. struct subnet_record *subrec;
  210. struct work_record *work;
  211. struct server_record *servrec;
  212. char *fname;
  213. char *fnamenew;
  214. uint32 stype;
  215. int i;
  216. XFILE *fp;
  217. bool list_changed = force_write;
  218. static time_t lasttime = 0;
  219. TALLOC_CTX *ctx = talloc_tos();
  220. /* Always dump if we're being told to by a signal. */
  221. if(force_write == False) {
  222. if (!lasttime)
  223. lasttime = t;
  224. if (t - lasttime < 5)
  225. return;
  226. }
  227. lasttime = t;
  228. dump_workgroups(force_write);
  229. for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
  230. if(subrec->work_changed) {
  231. list_changed = True;
  232. break;
  233. }
  234. }
  235. if(!list_changed)
  236. return;
  237. updatecount++;
  238. fname = cache_path(SERVER_LIST);
  239. if (!fname) {
  240. return;
  241. }
  242. fnamenew = talloc_asprintf(ctx, "%s.",
  243. fname);
  244. if (!fnamenew) {
  245. return;
  246. }
  247. fp = x_fopen(fnamenew,O_WRONLY|O_CREAT|O_TRUNC, 0644);
  248. if (!fp) {
  249. DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
  250. fnamenew,strerror(errno)));
  251. return;
  252. }
  253. /*
  254. * Write out a record for our workgroup. Use the record from the first
  255. * subnet.
  256. */
  257. if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL) {
  258. DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
  259. lp_workgroup()));
  260. x_fclose(fp);
  261. return;
  262. }
  263. write_browse_list_entry(fp, work->work_group,
  264. SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY,
  265. work->local_master_browser_name, work->work_group);
  266. /*
  267. * We need to do something special for our own names.
  268. * This is due to the fact that we may be a local master browser on
  269. * one of our broadcast subnets, and a domain master on the unicast
  270. * subnet. We iterate over the subnets and only write out the name
  271. * once.
  272. */
  273. for (i=0; my_netbios_names(i); i++) {
  274. stype = 0;
  275. for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
  276. if((work = find_workgroup_on_subnet( subrec, lp_workgroup() )) == NULL)
  277. continue;
  278. if((servrec = find_server_in_workgroup( work, my_netbios_names(i))) == NULL)
  279. continue;
  280. stype |= servrec->serv.type;
  281. }
  282. /* Output server details, plus what workgroup they're in. */
  283. write_browse_list_entry(fp, my_netbios_names(i), stype,
  284. string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH), lp_workgroup());
  285. }
  286. for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
  287. subrec->work_changed = False;
  288. for (work = subrec->workgrouplist; work ; work = work->next) {
  289. /* Write out a workgroup record for a workgroup. */
  290. uint32 wg_type = write_this_workgroup_name( subrec, work);
  291. if(wg_type) {
  292. write_browse_list_entry(fp, work->work_group, wg_type,
  293. work->local_master_browser_name,
  294. work->work_group);
  295. }
  296. /* Now write out any server records a workgroup may have. */
  297. for (servrec = work->serverlist; servrec ; servrec = servrec->next) {
  298. uint32 serv_type;
  299. /* We have already written our names here. */
  300. if(is_myname(servrec->serv.name))
  301. continue;
  302. serv_type = write_this_server_name(subrec, work, servrec);
  303. if(serv_type) {
  304. /* Output server details, plus what workgroup they're in. */
  305. write_browse_list_entry(fp, servrec->serv.name, serv_type,
  306. servrec->serv.comment, work->work_group);
  307. }
  308. }
  309. }
  310. }
  311. x_fclose(fp);
  312. unlink(fname);
  313. chmod(fnamenew,0644);
  314. rename(fnamenew,fname);
  315. DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname));
  316. }