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