PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/source3/nmbd/nmbd_serverlistdb.c

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