RIP BOOL. Convert BOOL -> bool. I found a few interesting
[kai/samba.git] / source3 / nmbd / nmbd_synclists.c
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    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20    
21 */
22
23 /* this file handles asynchronous browse synchronisation requests. The
24    requests are done by forking and putting the result in a file in the
25    locks directory. We do it this way because we don't want nmbd to be
26    blocked waiting for some server to respond on a TCP connection. This
27    also allows us to have more than 1 sync going at once (tridge) */
28
29 #include "includes.h"
30
31 extern fstring local_machine;
32
33 struct sync_record {
34         struct sync_record *next, *prev;
35         unstring workgroup;
36         unstring server;
37         pstring fname;
38         struct in_addr ip;
39         pid_t pid;
40 };
41
42 /* a linked list of current sync connections */
43 static struct sync_record *syncs;
44
45 static XFILE *fp;
46
47 /*******************************************************************
48   This is the NetServerEnum callback.
49   Note sname and comment are in UNIX codepage format.
50   ******************************************************************/
51
52 static void callback(const char *sname, uint32 stype, 
53                      const char *comment, void *state)
54 {
55         x_fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
56 }
57
58 /*******************************************************************
59   Synchronise browse lists with another browse server.
60   Log in on the remote server's SMB port to their IPC$ service,
61   do a NetServerEnum and record the results in fname
62 ******************************************************************/
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         fstring unix_workgroup;
70         struct cli_state *cli;
71         uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
72         struct nmb_name called, calling;
73         NTSTATUS status;
74
75         /* W2K DMB's return empty browse lists on port 445. Use 139.
76          * Patch from Andy Levine andyl@epicrealm.com.
77          */
78
79         cli = cli_initialise();
80         if (!cli) {
81                 return;
82         }
83
84         if (!cli_set_port(cli, 139)) {
85                 return;
86         }
87
88         status = cli_connect(cli, name, &ip);
89         if (!NT_STATUS_IS_OK(status)) {
90                 return;
91         }
92
93         make_nmb_name(&calling, local_machine, 0x0);
94         make_nmb_name(&called , name, nm_type);
95
96         if (!cli_session_request(cli, &calling, &called)) {
97                 cli_shutdown(cli);
98                 return;
99         }
100
101         if (!cli_negprot(cli)) {
102                 cli_shutdown(cli);
103                 return;
104         }
105
106         if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 1, "", 0,
107                                                workgroup))) {
108                 cli_shutdown(cli);
109                 return;
110         }
111
112         if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
113                 cli_shutdown(cli);
114                 return;
115         }
116
117         /* All the cli_XX functions take UNIX character set. */
118         fstrcpy(unix_workgroup, cli->server_domain ? cli->server_domain : workgroup);
119
120         /* Fetch a workgroup list. */
121         cli_NetServerEnum(cli, unix_workgroup,
122                           local_type|SV_TYPE_DOMAIN_ENUM, 
123                           callback, NULL);
124         
125         /* Now fetch a server list. */
126         if (servers) {
127                 fstrcpy(unix_workgroup, workgroup);
128                 cli_NetServerEnum(cli, unix_workgroup, 
129                                   local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
130                                   callback, NULL);
131         }
132         
133         cli_shutdown(cli);
134 }
135
136 /*******************************************************************
137   initialise a browse sync with another browse server.  Log in on the
138   remote server's SMB port to their IPC$ service, do a NetServerEnum
139   and record the results
140 ******************************************************************/
141
142 void sync_browse_lists(struct work_record *work,
143                        char *name, int nm_type, 
144                        struct in_addr ip, bool local, bool servers)
145 {
146         struct sync_record *s;
147         static int counter;
148
149         START_PROFILE(sync_browse_lists);
150         /* Check we're not trying to sync with ourselves. This can
151            happen if we are a domain *and* a local master browser. */
152         if (ismyip_v4(ip)) {
153 done:
154                 END_PROFILE(sync_browse_lists);
155                 return;
156         }
157
158         s = SMB_MALLOC_P(struct sync_record);
159         if (!s) goto done;
160
161         ZERO_STRUCTP(s);
162         
163         unstrcpy(s->workgroup, work->work_group);
164         unstrcpy(s->server, name);
165         s->ip = ip;
166
167         slprintf(s->fname, sizeof(pstring)-1,
168                  "%s/sync.%d", lp_lockdir(), counter++);
169         all_string_sub(s->fname,"//", "/", 0);
170         
171         DLIST_ADD(syncs, s);
172
173         /* the parent forks and returns, leaving the child to do the
174            actual sync and call END_PROFILE*/
175         CatchChild();
176         if ((s->pid = sys_fork())) return;
177
178         BlockSignals( False, SIGTERM );
179
180         DEBUG(2,("Initiating browse sync for %s to %s(%s)\n",
181                  work->work_group, name, inet_ntoa(ip)));
182
183         fp = x_fopen(s->fname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
184         if (!fp) {
185                 END_PROFILE(sync_browse_lists);
186                 _exit(1);       
187         }
188
189         sync_child(name, nm_type, work->work_group, ip, local, servers,
190                    s->fname);
191
192         x_fclose(fp);
193         END_PROFILE(sync_browse_lists);
194         _exit(0);
195 }
196
197 /**********************************************************************
198  Handle one line from a completed sync file.
199  **********************************************************************/
200
201 static void complete_one(struct sync_record *s, 
202                          char *sname, uint32 stype, char *comment)
203 {
204         struct work_record *work;
205         struct server_record *servrec;
206
207         stype &= ~SV_TYPE_LOCAL_LIST_ONLY;
208
209         if (stype & SV_TYPE_DOMAIN_ENUM) {
210                 /* See if we can find the workgroup on this subnet. */
211                 if((work=find_workgroup_on_subnet(unicast_subnet, sname))) {
212                         /* We already know about this workgroup -
213                            update the ttl. */
214                         update_workgroup_ttl(work,lp_max_ttl());
215                 } else {
216                         /* Create the workgroup on the subnet. */
217                         work = create_workgroup_on_subnet(unicast_subnet, 
218                                                           sname, lp_max_ttl());
219                         if (work) {
220                                 /* remember who the master is */
221                                 unstrcpy(work->local_master_browser_name, comment);
222                         }
223                 }
224                 return;
225         } 
226
227         work = find_workgroup_on_subnet(unicast_subnet, s->workgroup);
228         if (!work) {
229                 DEBUG(3,("workgroup %s doesn't exist on unicast subnet?\n",
230                          s->workgroup));
231                 return;
232         }
233
234         if ((servrec = find_server_in_workgroup( work, sname))) {
235                 /* Check that this is not a locally known
236                    server - if so ignore the entry. */
237                 if(!(servrec->serv.type & SV_TYPE_LOCAL_LIST_ONLY)) {
238                         /* We already know about this server - update
239                            the ttl. */
240                         update_server_ttl(servrec, lp_max_ttl());
241                         /* Update the type. */
242                         servrec->serv.type = stype;
243                 }
244                 return;
245         } 
246
247         /* Create the server in the workgroup. */ 
248         create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
249 }
250                 
251 /**********************************************************************
252  Read the completed sync info.
253 **********************************************************************/
254
255 static void complete_sync(struct sync_record *s)
256 {
257         XFILE *f;
258         unstring server, type_str;
259         unsigned type;
260         pstring comment;
261         pstring line;
262         const char *ptr;
263         int count=0;
264
265         f = x_fopen(s->fname,O_RDONLY, 0);
266
267         if (!f)
268                 return;
269         
270         while (!x_feof(f)) {
271                 
272                 if (!fgets_slash(line,sizeof(pstring),f))
273                         continue;
274                 
275                 ptr = line;
276
277                 if (!next_token(&ptr,server,NULL,sizeof(server)) ||
278                     !next_token(&ptr,type_str,NULL, sizeof(type_str)) ||
279                     !next_token(&ptr,comment,NULL, sizeof(comment))) {
280                         continue;
281                 }
282
283                 sscanf(type_str, "%X", &type);
284
285                 complete_one(s, server, type, comment);
286
287                 count++;
288         }
289
290         x_fclose(f);
291
292         unlink(s->fname);
293
294         DEBUG(2,("sync with %s(%s) for workgroup %s completed (%d records)\n",
295                  s->server, inet_ntoa(s->ip), s->workgroup, count));
296 }
297
298 /**********************************************************************
299  Check for completion of any of the child processes.
300 **********************************************************************/
301
302 void sync_check_completion(void)
303 {
304         struct sync_record *s, *next;
305
306         for (s=syncs;s;s=next) {
307                 next = s->next;
308                 if (!process_exists_by_pid(s->pid)) {
309                         /* it has completed - grab the info */
310                         complete_sync(s);
311                         DLIST_REMOVE(syncs, s);
312                         ZERO_STRUCTP(s);
313                         SAFE_FREE(s);
314                 }
315         }
316 }