Merge branch 'master' of ssh://git.samba.org/data/git/samba into pyregistry
[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 struct sync_record {
32         struct sync_record *next, *prev;
33         unstring workgroup;
34         unstring server;
35         char *fname;
36         struct in_addr ip;
37         pid_t pid;
38 };
39
40 /* a linked list of current sync connections */
41 static struct sync_record *syncs;
42
43 static XFILE *fp;
44
45 /*******************************************************************
46   This is the NetServerEnum callback.
47   Note sname and comment are in UNIX codepage format.
48   ******************************************************************/
49
50 static void callback(const char *sname, uint32 stype, 
51                      const char *comment, void *state)
52 {
53         x_fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
54 }
55
56 /*******************************************************************
57   Synchronise browse lists with another browse server.
58   Log in on the remote server's SMB port to their IPC$ service,
59   do a NetServerEnum and record the results in fname
60 ******************************************************************/
61
62 static void sync_child(char *name, int nm_type, 
63                        char *workgroup,
64                        struct in_addr ip, bool local, bool servers,
65                        char *fname)
66 {
67         fstring unix_workgroup;
68         struct cli_state *cli;
69         uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
70         struct nmb_name called, calling;
71         struct sockaddr_storage ss;
72         NTSTATUS status;
73
74         /* W2K DMB's return empty browse lists on port 445. Use 139.
75          * Patch from Andy Levine andyl@epicrealm.com.
76          */
77
78         cli = cli_initialise();
79         if (!cli) {
80                 return;
81         }
82
83         if (!cli_set_port(cli, 139)) {
84                 cli_shutdown(cli);
85                 return;
86         }
87
88         in_addr_to_sockaddr_storage(&ss, ip);
89         status = cli_connect(cli, name, &ss);
90         if (!NT_STATUS_IS_OK(status)) {
91                 cli_shutdown(cli);
92                 return;
93         }
94
95         make_nmb_name(&calling, get_local_machine_name(), 0x0);
96         make_nmb_name(&called , name, nm_type);
97
98         if (!cli_session_request(cli, &calling, &called)) {
99                 cli_shutdown(cli);
100                 return;
101         }
102
103         status = cli_negprot(cli);
104         if (!NT_STATUS_IS_OK(status)) {
105                 cli_shutdown(cli);
106                 return;
107         }
108
109         if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 1, "", 0,
110                                                workgroup))) {
111                 cli_shutdown(cli);
112                 return;
113         }
114
115         if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
116                 cli_shutdown(cli);
117                 return;
118         }
119
120         /* All the cli_XX functions take UNIX character set. */
121         fstrcpy(unix_workgroup, cli->server_domain ? cli->server_domain : workgroup);
122
123         /* Fetch a workgroup list. */
124         cli_NetServerEnum(cli, unix_workgroup,
125                           local_type|SV_TYPE_DOMAIN_ENUM, 
126                           callback, NULL);
127         
128         /* Now fetch a server list. */
129         if (servers) {
130                 fstrcpy(unix_workgroup, workgroup);
131                 cli_NetServerEnum(cli, unix_workgroup, 
132                                   local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
133                                   callback, NULL);
134         }
135         
136         cli_shutdown(cli);
137 }
138
139 /*******************************************************************
140   initialise a browse sync with another browse server.  Log in on the
141   remote server's SMB port to their IPC$ service, do a NetServerEnum
142   and record the results
143 ******************************************************************/
144
145 void sync_browse_lists(struct work_record *work,
146                        char *name, int nm_type, 
147                        struct in_addr ip, bool local, bool servers)
148 {
149         struct sync_record *s;
150         static int counter;
151
152         START_PROFILE(sync_browse_lists);
153         /* Check we're not trying to sync with ourselves. This can
154            happen if we are a domain *and* a local master browser. */
155         if (ismyip_v4(ip)) {
156 done:
157                 END_PROFILE(sync_browse_lists);
158                 return;
159         }
160
161         s = SMB_MALLOC_P(struct sync_record);
162         if (!s) goto done;
163
164         ZERO_STRUCTP(s);
165
166         unstrcpy(s->workgroup, work->work_group);
167         unstrcpy(s->server, name);
168         s->ip = ip;
169
170         if (asprintf(&s->fname, "%s/sync.%d", lp_lockdir(), counter++) < 0) {
171                 SAFE_FREE(s);
172                 goto done;
173         }
174         /* Safe to use as 0 means no size change. */
175         all_string_sub(s->fname,"//", "/", 0);
176
177         DLIST_ADD(syncs, s);
178
179         /* the parent forks and returns, leaving the child to do the
180            actual sync and call END_PROFILE*/
181         CatchChild();
182         if ((s->pid = sys_fork())) return;
183
184         BlockSignals( False, SIGTERM );
185
186         DEBUG(2,("Initiating browse sync for %s to %s(%s)\n",
187                  work->work_group, name, inet_ntoa(ip)));
188
189         fp = x_fopen(s->fname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
190         if (!fp) {
191                 END_PROFILE(sync_browse_lists);
192                 _exit(1);
193         }
194
195         sync_child(name, nm_type, work->work_group, ip, local, servers,
196                    s->fname);
197
198         x_fclose(fp);
199         END_PROFILE(sync_browse_lists);
200         _exit(0);
201 }
202
203 /**********************************************************************
204  Handle one line from a completed sync file.
205  **********************************************************************/
206
207 static void complete_one(struct sync_record *s,
208                          char *sname, uint32 stype, char *comment)
209 {
210         struct work_record *work;
211         struct server_record *servrec;
212
213         stype &= ~SV_TYPE_LOCAL_LIST_ONLY;
214
215         if (stype & SV_TYPE_DOMAIN_ENUM) {
216                 /* See if we can find the workgroup on this subnet. */
217                 if((work=find_workgroup_on_subnet(unicast_subnet, sname))) {
218                         /* We already know about this workgroup -
219                            update the ttl. */
220                         update_workgroup_ttl(work,lp_max_ttl());
221                 } else {
222                         /* Create the workgroup on the subnet. */
223                         work = create_workgroup_on_subnet(unicast_subnet, 
224                                                           sname, lp_max_ttl());
225                         if (work) {
226                                 /* remember who the master is */
227                                 unstrcpy(work->local_master_browser_name, comment);
228                         }
229                 }
230                 return;
231         } 
232
233         work = find_workgroup_on_subnet(unicast_subnet, s->workgroup);
234         if (!work) {
235                 DEBUG(3,("workgroup %s doesn't exist on unicast subnet?\n",
236                          s->workgroup));
237                 return;
238         }
239
240         if ((servrec = find_server_in_workgroup( work, sname))) {
241                 /* Check that this is not a locally known
242                    server - if so ignore the entry. */
243                 if(!(servrec->serv.type & SV_TYPE_LOCAL_LIST_ONLY)) {
244                         /* We already know about this server - update
245                            the ttl. */
246                         update_server_ttl(servrec, lp_max_ttl());
247                         /* Update the type. */
248                         servrec->serv.type = stype;
249                 }
250                 return;
251         } 
252
253         /* Create the server in the workgroup. */ 
254         create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
255 }
256
257 /**********************************************************************
258  Read the completed sync info.
259 **********************************************************************/
260
261 static void complete_sync(struct sync_record *s)
262 {
263         XFILE *f;
264         char *server;
265         char *type_str;
266         unsigned type;
267         char *comment;
268         char line[1024];
269         const char *ptr;
270         int count=0;
271
272         f = x_fopen(s->fname,O_RDONLY, 0);
273
274         if (!f)
275                 return;
276
277         while (!x_feof(f)) {
278                 TALLOC_CTX *frame = NULL;
279
280                 if (!fgets_slash(line,sizeof(line),f))
281                         continue;
282
283                 ptr = line;
284
285                 frame = talloc_stackframe();
286                 if (!next_token_talloc(frame,&ptr,&server,NULL) ||
287                     !next_token_talloc(frame,&ptr,&type_str,NULL) ||
288                     !next_token_talloc(frame,&ptr,&comment,NULL)) {
289                         TALLOC_FREE(frame);
290                         continue;
291                 }
292
293                 sscanf(type_str, "%X", &type);
294
295                 complete_one(s, server, type, comment);
296
297                 count++;
298                 TALLOC_FREE(frame);
299         }
300         x_fclose(f);
301
302         unlink(s->fname);
303
304         DEBUG(2,("sync with %s(%s) for workgroup %s completed (%d records)\n",
305                  s->server, inet_ntoa(s->ip), s->workgroup, count));
306 }
307
308 /**********************************************************************
309  Check for completion of any of the child processes.
310 **********************************************************************/
311
312 void sync_check_completion(void)
313 {
314         struct sync_record *s, *next;
315
316         for (s=syncs;s;s=next) {
317                 next = s->next;
318                 if (!process_exists_by_pid(s->pid)) {
319                         /* it has completed - grab the info */
320                         complete_sync(s);
321                         DLIST_REMOVE(syncs, s);
322                         SAFE_FREE(s->fname);
323                         SAFE_FREE(s);
324                 }
325         }
326 }