fixed another potential fork bomb where the wins file becomes
[Samba/gbeck.git] / source / nmbd / nmbd_synclists.c
blobdee64d501b2de70e2b1c5e0929143b2386f15d72
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* this file handles asynchronous browse synchronisation requests. The
26 requests are done by forking and putting the result in a file in the
27 locks directory. We do it this way because we don't want nmbd to be
28 blocked waiting for some server to respond on a TCP connection. This
29 also allows us to have more than 1 sync going at once (tridge) */
31 #include "includes.h"
32 #include "smb.h"
34 extern int DEBUGLEVEL;
36 struct sync_record {
37 struct sync_record *next, *prev;
38 fstring workgroup;
39 fstring server;
40 pstring fname;
41 struct in_addr ip;
42 int pid;
45 /* a linked list of current sync connections */
46 static struct sync_record *syncs;
48 static FILE *fp;
50 /*******************************************************************
51 This is the NetServerEnum callback.
52 ******************************************************************/
53 static void callback(char *sname, uint32 stype, char *comment)
55 fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
59 /*******************************************************************
60 Synchronise browse lists with another browse server.
61 Log in on the remote server's SMB port to their IPC$ service,
62 do a NetServerEnum and record the results in fname
63 ******************************************************************/
64 static void sync_child(char *name, int nm_type,
65 char *workgroup,
66 struct in_addr ip, BOOL local, BOOL servers,
67 char *fname)
69 extern fstring local_machine;
70 static struct cli_state cli;
71 uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
73 if (!cli_initialise(&cli) || !cli_connect(&cli, name, &ip)) {
74 fclose(fp);
75 return;
78 if (!cli_session_request(&cli, name, nm_type, local_machine)) {
79 cli_shutdown(&cli);
80 fclose(fp);
81 return;
84 if (!cli_negprot(&cli)) {
85 cli_shutdown(&cli);
86 return;
89 if (!cli_session_setup(&cli, "", "", 1, "", 0, workgroup)) {
90 cli_shutdown(&cli);
91 return;
94 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
95 cli_shutdown(&cli);
96 return;
99 /* Fetch a workgroup list. */
100 cli_NetServerEnum(&cli, workgroup,
101 local_type|SV_TYPE_DOMAIN_ENUM,
102 callback);
104 /* Now fetch a server list. */
105 if (servers) {
106 cli_NetServerEnum(&cli, workgroup,
107 local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
108 callback);
111 cli_shutdown(&cli);
115 /*******************************************************************
116 initialise a browse sync with another browse server. Log in on the
117 remote server's SMB port to their IPC$ service, do a NetServerEnum
118 and record the results
119 ******************************************************************/
120 void sync_browse_lists(struct work_record *work,
121 char *name, int nm_type,
122 struct in_addr ip, BOOL local, BOOL servers)
124 struct sync_record *s;
125 static int counter;
127 /* Check we're not trying to sync with ourselves. This can
128 happen if we are a domain *and* a local master browser. */
129 if (ismyip(ip)) {
130 return;
133 s = (struct sync_record *)malloc(sizeof(*s));
134 if (!s) return;
136 ZERO_STRUCTP(s);
138 fstrcpy(s->workgroup, work->work_group);
139 fstrcpy(s->server, name);
140 s->ip = ip;
142 slprintf(s->fname, sizeof(pstring)-1,
143 "%s/sync.%d", lp_lockdir(), counter++);
144 string_sub(s->fname,"//", "/");
146 DLIST_ADD(syncs, s);
148 /* the parent forks and returns, leaving the child to do the
149 actual sync */
150 CatchChild();
151 if ((s->pid = fork())) return;
153 BlockSignals( False, SIGTERM );
155 DEBUG(2,("Initiating browse sync for %s to %s(%s)\n",
156 work->work_group, name, inet_ntoa(ip)));
158 fp = fopen(s->fname,"w");
159 if (!fp) _exit(1);
161 sync_child(name, nm_type, work->work_group, ip, local, servers,
162 s->fname);
164 fclose(fp);
165 _exit(0);
168 /**********************************************************************
169 handle one line from a completed sync file
170 **********************************************************************/
171 static void complete_one(struct sync_record *s,
172 char *sname, uint32 stype, char *comment)
174 struct work_record *work;
175 struct server_record *servrec;
177 stype &= ~SV_TYPE_LOCAL_LIST_ONLY;
179 if (stype & SV_TYPE_DOMAIN_ENUM) {
180 /* See if we can find the workgroup on this subnet. */
181 if((work=find_workgroup_on_subnet(unicast_subnet, sname))) {
182 /* We already know about this workgroup -
183 update the ttl. */
184 update_workgroup_ttl(work,lp_max_ttl());
185 } else {
186 /* Create the workgroup on the subnet. */
187 work = create_workgroup_on_subnet(unicast_subnet,
188 sname, lp_max_ttl());
189 if (work) {
190 /* remember who the master is */
191 fstrcpy(work->local_master_browser_name,
192 comment);
195 return;
198 work = find_workgroup_on_subnet(unicast_subnet, s->workgroup);
199 if (!work) {
200 DEBUG(3,("workgroup %s doesn't exist on unicast subnet?\n",
201 s->workgroup));
202 return;
205 if ((servrec = find_server_in_workgroup( work, sname))) {
206 /* Check that this is not a locally known
207 server - if so ignore the entry. */
208 if(!(servrec->serv.type & SV_TYPE_LOCAL_LIST_ONLY)) {
209 /* We already know about this server - update
210 the ttl. */
211 update_server_ttl(servrec, lp_max_ttl());
212 /* Update the type. */
213 servrec->serv.type = stype;
215 return;
218 /* Create the server in the workgroup. */
219 create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
223 /**********************************************************************
224 read the completed sync info
225 **********************************************************************/
226 static void complete_sync(struct sync_record *s)
228 FILE *f;
229 fstring server, type_str;
230 unsigned type;
231 pstring comment;
232 pstring line;
233 char *ptr;
234 int count=0;
236 f = fopen(s->fname,"r");
238 if (!f) return;
240 while (!feof(f)) {
242 if (!fgets_slash(line,sizeof(pstring),f)) continue;
244 ptr = line;
246 if (!next_token(&ptr,server,NULL,sizeof(server)) ||
247 !next_token(&ptr,type_str,NULL, sizeof(type_str)) ||
248 !next_token(&ptr,comment,NULL, sizeof(comment))) {
249 continue;
252 sscanf(type_str, "%X", &type);
254 complete_one(s, server, type, comment);
256 count++;
259 fclose(f);
261 unlink(s->fname);
263 DEBUG(2,("sync with %s(%s) for workgroup %s completed (%d records)\n",
264 s->server, inet_ntoa(s->ip), s->workgroup, count));
267 /**********************************************************************
268 check for completion of any of the child processes
269 **********************************************************************/
270 void sync_check_completion(void)
272 struct sync_record *s, *next;
274 for (s=syncs;s;s=next) {
275 next = s->next;
276 if (!process_exists(s->pid)) {
277 /* it has completed - grab the info */
278 complete_sync(s);
279 DLIST_REMOVE(syncs, s);
280 ZERO_STRUCTP(s);
281 free(s);