bounds check next_token() to prevent possible buffer overflows
[vlendec/samba-autobuild/.git] / source3 / nmbd / nmbd_synclists.c
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
8    
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.
13    
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.
18    
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.
22    
23 */
24
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) */
30
31 #include "includes.h"
32 #include "smb.h"
33
34 extern int DEBUGLEVEL;
35
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;
43 };
44
45 /* a linked list of current sync connections */
46 static struct sync_record *syncs;
47
48 static FILE *fp;
49
50 /*******************************************************************
51   This is the NetServerEnum callback.
52   ******************************************************************/
53 static void callback(char *sname, uint32 stype, char *comment)
54 {
55         fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
56 }
57
58
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)
68 {
69         extern fstring local_machine;
70         static struct cli_state cli;
71         uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
72
73         if (!cli_initialise(&cli) || !cli_connect(&cli, name, &ip)) {
74                 fclose(fp);
75                 return;
76         }
77
78         if (!cli_session_request(&cli, name, nm_type, local_machine)) {
79                 cli_shutdown(&cli);
80                 fclose(fp);
81                 return;
82         }
83
84         if (!cli_negprot(&cli)) {
85                 cli_shutdown(&cli);
86                 return;
87         }
88
89         if (!cli_session_setup(&cli, "", "", 1, "", 0, workgroup)) {
90                 cli_shutdown(&cli);
91                 return;
92         }
93
94         if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
95                 cli_shutdown(&cli);
96                 return;
97         }
98
99         /* Fetch a workgroup list. */
100         cli_NetServerEnum(&cli, workgroup, 
101                           local_type|SV_TYPE_DOMAIN_ENUM,
102                           callback);
103         
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);
109         }
110         
111         cli_shutdown(&cli);
112 }
113
114
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)
123 {
124         struct sync_record *s;
125         static int counter;
126
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;
131         }
132
133         s = (struct sync_record *)malloc(sizeof(*s));
134         if (!s) return;
135
136         ZERO_STRUCTP(s);
137         
138         fstrcpy(s->workgroup, work->work_group);
139         fstrcpy(s->server, name);
140         s->ip = ip;
141
142         slprintf(s->fname, sizeof(pstring)-1,
143                  "%s/sync.%d", lp_lockdir(), counter++);
144         string_sub(s->fname,"//", "/");
145         
146         DLIST_ADD(syncs, s);
147
148         /* the parent forks and returns, leaving the child to do the
149            actual sync */
150         CatchChild();
151         if ((s->pid = fork())) return;
152
153         BlockSignals( False, SIGTERM );
154
155         DEBUG(2,("Initiating browse sync for %s to %s(%s)\n",
156                  work->work_group, name, inet_ntoa(ip)));
157
158         fp = fopen(s->fname,"w");
159         if (!fp) _exit(1);      
160
161         sync_child(name, nm_type, work->work_group, ip, local, servers,
162                    s->fname);
163
164         fclose(fp);
165         _exit(0);
166 }
167
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)
173 {
174         struct work_record *work;
175         struct server_record *servrec;
176
177         stype &= ~SV_TYPE_LOCAL_LIST_ONLY;
178
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);
193                         }
194                 }
195                 return;
196         } 
197
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;
203         }
204
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;
214                 }
215                 return;
216         } 
217
218         /* Create the server in the workgroup. */ 
219         create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
220 }
221                 
222
223 /**********************************************************************
224 read the completed sync info
225  **********************************************************************/
226 static void complete_sync(struct sync_record *s)
227 {
228         FILE *f;
229         fstring server, type_str;
230         unsigned type;
231         pstring comment;
232         pstring line;
233         char *ptr;
234         int count=0;
235
236         f = fopen(s->fname,"r");
237         
238         while (!feof(f)) {
239                 
240                 if (!fgets_slash(line,sizeof(pstring),f)) continue;
241                 
242                 ptr = line;
243
244                 if (!next_token(&ptr,server,NULL,sizeof(server)) ||
245                     !next_token(&ptr,type_str,NULL, sizeof(type_str)) ||
246                     !next_token(&ptr,comment,NULL, sizeof(comment))) {
247                         continue;
248                 }
249
250                 sscanf(type_str, "%X", &type);
251
252                 complete_one(s, server, type, comment);
253
254                 count++;
255         }
256
257         fclose(f);
258
259         unlink(s->fname);
260
261         DEBUG(2,("sync with %s(%s) for workgroup %s completed (%d records)\n",
262                  s->server, inet_ntoa(s->ip), s->workgroup, count));
263 }
264
265 /**********************************************************************
266 check for completion of any of the child processes
267  **********************************************************************/
268 void sync_check_completion(void)
269 {
270         struct sync_record *s, *next;
271
272         for (s=syncs;s;s=next) {
273                 next = s->next;
274                 if (!process_exists(s->pid)) {
275                         /* it has completed - grab the info */
276                         complete_sync(s);
277                         DLIST_REMOVE(syncs, s);
278                         ZERO_STRUCTP(s);
279                         free(s);
280                 }
281         }
282 }