bc731a9ceb783d58159477ec9fd58ffcfff169c1
[samba.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         pid_t 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   Note sname and comment are in UNIX codepage format.
53   ******************************************************************/
54 static void callback(const char *sname, uint32 stype, 
55                      const char *comment, void *state)
56 {
57         fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
58 }
59
60 /*******************************************************************
61   Synchronise browse lists with another browse server.
62   Log in on the remote server's SMB port to their IPC$ service,
63   do a NetServerEnum and record the results in fname
64 ******************************************************************/
65 static void sync_child(char *name, int nm_type, 
66                        char *workgroup,
67                        struct in_addr ip, BOOL local, BOOL servers,
68                        char *fname)
69 {
70         extern fstring local_machine;
71         fstring unix_workgroup;
72         static struct cli_state cli;
73         uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
74         struct nmb_name called, calling;
75
76         if (!cli_initialise(&cli) || !cli_connect(&cli, name, &ip)) {
77                 fclose(fp);
78                 return;
79         }
80
81         make_nmb_name(&calling, local_machine, 0x0);
82         make_nmb_name(&called , name         , nm_type);
83
84         if (!cli_session_request(&cli, &calling, &called))
85         {
86                 cli_shutdown(&cli);
87                 fclose(fp);
88                 return;
89         }
90
91         if (!cli_negprot(&cli)) {
92                 cli_shutdown(&cli);
93                 return;
94         }
95
96         if (!cli_session_setup(&cli, "", "", 1, "", 0, workgroup)) {
97                 cli_shutdown(&cli);
98                 return;
99         }
100
101         if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
102                 cli_shutdown(&cli);
103                 return;
104         }
105
106         /* All the cli_XX functions take UNIX character set. */
107         fstrcpy(unix_workgroup, cli.server_domain?cli.server_domain:workgroup);
108         dos_to_unix(unix_workgroup, True);
109
110         /* Fetch a workgroup list. */
111         cli_NetServerEnum(&cli, unix_workgroup,
112                           local_type|SV_TYPE_DOMAIN_ENUM, 
113                           callback, NULL);
114         
115         /* Now fetch a server list. */
116         if (servers) {
117                 fstrcpy(unix_workgroup, workgroup);
118                 dos_to_unix(unix_workgroup, True);
119                 cli_NetServerEnum(&cli, unix_workgroup, 
120                                   local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
121                                   callback, NULL);
122         }
123         
124         cli_shutdown(&cli);
125 }
126
127
128 /*******************************************************************
129   initialise a browse sync with another browse server.  Log in on the
130   remote server's SMB port to their IPC$ service, do a NetServerEnum
131   and record the results
132 ******************************************************************/
133 void sync_browse_lists(struct work_record *work,
134                        char *name, int nm_type, 
135                        struct in_addr ip, BOOL local, BOOL servers)
136 {
137         struct sync_record *s;
138         static int counter;
139
140         /* Check we're not trying to sync with ourselves. This can
141            happen if we are a domain *and* a local master browser. */
142         if (ismyip(ip)) {
143                 return;
144         }
145
146         s = (struct sync_record *)malloc(sizeof(*s));
147         if (!s) return;
148
149         ZERO_STRUCTP(s);
150         
151         fstrcpy(s->workgroup, work->work_group);
152         fstrcpy(s->server, name);
153         s->ip = ip;
154
155         slprintf(s->fname, sizeof(pstring)-1,
156                  "%s/sync.%d", lp_lockdir(), counter++);
157         all_string_sub(s->fname,"//", "/", 0);
158         
159         DLIST_ADD(syncs, s);
160
161         /* the parent forks and returns, leaving the child to do the
162            actual sync */
163         CatchChild();
164         if ((s->pid = sys_fork())) return;
165
166         BlockSignals( False, SIGTERM );
167
168         DEBUG(2,("Initiating browse sync for %s to %s(%s)\n",
169                  work->work_group, name, inet_ntoa(ip)));
170
171         fp = sys_fopen(s->fname,"w");
172         if (!fp) _exit(1);      
173
174         sync_child(name, nm_type, work->work_group, ip, local, servers,
175                    s->fname);
176
177         fclose(fp);
178         _exit(0);
179 }
180
181 /**********************************************************************
182 handle one line from a completed sync file
183  **********************************************************************/
184 static void complete_one(struct sync_record *s, 
185                          char *sname, uint32 stype, char *comment)
186 {
187         struct work_record *work;
188         struct server_record *servrec;
189
190         stype &= ~SV_TYPE_LOCAL_LIST_ONLY;
191
192         if (stype & SV_TYPE_DOMAIN_ENUM) {
193                 /* See if we can find the workgroup on this subnet. */
194                 if((work=find_workgroup_on_subnet(unicast_subnet, sname))) {
195                         /* We already know about this workgroup -
196                            update the ttl. */
197                         update_workgroup_ttl(work,lp_max_ttl());
198                 } else {
199                         /* Create the workgroup on the subnet. */
200                         work = create_workgroup_on_subnet(unicast_subnet, 
201                                                           sname, lp_max_ttl());
202                         if (work) {
203                                 /* remember who the master is */
204                                 fstrcpy(work->local_master_browser_name, 
205                                         comment);
206                         }
207                 }
208                 return;
209         } 
210
211         work = find_workgroup_on_subnet(unicast_subnet, s->workgroup);
212         if (!work) {
213                 DEBUG(3,("workgroup %s doesn't exist on unicast subnet?\n",
214                          s->workgroup));
215                 return;
216         }
217
218         if ((servrec = find_server_in_workgroup( work, sname))) {
219                 /* Check that this is not a locally known
220                    server - if so ignore the entry. */
221                 if(!(servrec->serv.type & SV_TYPE_LOCAL_LIST_ONLY)) {
222                         /* We already know about this server - update
223                            the ttl. */
224                         update_server_ttl(servrec, lp_max_ttl());
225                         /* Update the type. */
226                         servrec->serv.type = stype;
227                 }
228                 return;
229         } 
230
231         /* Create the server in the workgroup. */ 
232         create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
233 }
234                 
235
236 /**********************************************************************
237 read the completed sync info
238  **********************************************************************/
239 static void complete_sync(struct sync_record *s)
240 {
241         FILE *f;
242         fstring server, type_str;
243         unsigned type;
244         pstring comment;
245         pstring line;
246         char *ptr;
247         int count=0;
248
249         f = sys_fopen(s->fname,"r");
250
251         if (!f) return;
252         
253         while (!feof(f)) {
254                 
255                 if (!fgets_slash(line,sizeof(pstring),f)) continue;
256                 
257                 ptr = line;
258
259                 /* The line is written in UNIX character set. Convert to DOS codepage. */
260                 unix_to_dos(line,True);
261
262                 if (!next_token(&ptr,server,NULL,sizeof(server)) ||
263                     !next_token(&ptr,type_str,NULL, sizeof(type_str)) ||
264                     !next_token(&ptr,comment,NULL, sizeof(comment))) {
265                         continue;
266                 }
267
268                 sscanf(type_str, "%X", &type);
269
270                 complete_one(s, server, type, comment);
271
272                 count++;
273         }
274
275         fclose(f);
276
277         unlink(s->fname);
278
279         DEBUG(2,("sync with %s(%s) for workgroup %s completed (%d records)\n",
280                  s->server, inet_ntoa(s->ip), s->workgroup, count));
281 }
282
283 /**********************************************************************
284 check for completion of any of the child processes
285  **********************************************************************/
286 void sync_check_completion(void)
287 {
288         struct sync_record *s, *next;
289
290         for (s=syncs;s;s=next) {
291                 next = s->next;
292                 if (!process_exists(s->pid)) {
293                         /* it has completed - grab the info */
294                         complete_sync(s);
295                         DLIST_REMOVE(syncs, s);
296                         ZERO_STRUCTP(s);
297                         free(s);
298                 }
299         }
300 }