off by one bug in string length; CR 1159
[Samba.git] / source / nmbd / nmbd_synclists.c
blobb9952fb446ca63a183499109683909acf0d5f3b9
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
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* this file handles asynchronous browse synchronisation requests. The
25 requests are done by forking and putting the result in a file in the
26 locks directory. We do it this way because we don't want nmbd to be
27 blocked waiting for some server to respond on a TCP connection. This
28 also allows us to have more than 1 sync going at once (tridge) */
30 #include "includes.h"
32 struct sync_record {
33 struct sync_record *next, *prev;
34 fstring workgroup;
35 fstring server;
36 pstring fname;
37 struct in_addr ip;
38 pid_t pid;
41 /* a linked list of current sync connections */
42 static struct sync_record *syncs;
44 static XFILE *fp;
46 /*******************************************************************
47 This is the NetServerEnum callback.
48 Note sname and comment are in UNIX codepage format.
49 ******************************************************************/
50 static void callback(const char *sname, uint32 stype,
51 const char *comment, void *state)
53 x_fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
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 static void sync_child(char *name, int nm_type,
62 char *workgroup,
63 struct in_addr ip, BOOL local, BOOL servers,
64 char *fname)
66 extern fstring local_machine;
67 fstring unix_workgroup;
68 static struct cli_state cli;
69 uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
70 struct nmb_name called, calling;
72 /* W2K DMB's return empty browse lists on port 445. Use 139.
73 * Patch from Andy Levine andyl@epicrealm.com.
76 if (!cli_initialise(&cli) || !cli_set_port(&cli, 139) || !cli_connect(&cli, name, &ip)) {
77 return;
80 make_nmb_name(&calling, local_machine, 0x0);
81 make_nmb_name(&called , name , nm_type);
83 if (!cli_session_request(&cli, &calling, &called))
85 cli_shutdown(&cli);
86 return;
89 if (!cli_negprot(&cli)) {
90 cli_shutdown(&cli);
91 return;
94 if (!cli_session_setup(&cli, "", "", 1, "", 0, workgroup)) {
95 cli_shutdown(&cli);
96 return;
99 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
100 cli_shutdown(&cli);
101 return;
104 /* All the cli_XX functions take UNIX character set. */
105 fstrcpy(unix_workgroup, cli.server_domain?cli.server_domain:workgroup);
107 /* Fetch a workgroup list. */
108 cli_NetServerEnum(&cli, unix_workgroup,
109 local_type|SV_TYPE_DOMAIN_ENUM,
110 callback, NULL);
112 /* Now fetch a server list. */
113 if (servers) {
114 fstrcpy(unix_workgroup, workgroup);
115 cli_NetServerEnum(&cli, unix_workgroup,
116 local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
117 callback, NULL);
120 cli_shutdown(&cli);
124 /*******************************************************************
125 initialise a browse sync with another browse server. Log in on the
126 remote server's SMB port to their IPC$ service, do a NetServerEnum
127 and record the results
128 ******************************************************************/
129 void sync_browse_lists(struct work_record *work,
130 char *name, int nm_type,
131 struct in_addr ip, BOOL local, BOOL servers)
133 struct sync_record *s;
134 static int counter;
136 START_PROFILE(sync_browse_lists);
137 /* Check we're not trying to sync with ourselves. This can
138 happen if we are a domain *and* a local master browser. */
139 if (ismyip(ip)) {
140 done:
141 END_PROFILE(sync_browse_lists);
142 return;
145 s = (struct sync_record *)malloc(sizeof(*s));
146 if (!s) goto done;
148 ZERO_STRUCTP(s);
150 fstrcpy(s->workgroup, work->work_group);
151 fstrcpy(s->server, name);
152 s->ip = ip;
154 slprintf(s->fname, sizeof(pstring)-1,
155 "%s/sync.%d", lp_lockdir(), counter++);
156 all_string_sub(s->fname,"//", "/", 0);
158 DLIST_ADD(syncs, s);
160 /* the parent forks and returns, leaving the child to do the
161 actual sync and call END_PROFILE*/
162 CatchChild();
163 if ((s->pid = sys_fork())) return;
165 BlockSignals( False, SIGTERM );
167 DEBUG(2,("Initiating browse sync for %s to %s(%s)\n",
168 work->work_group, name, inet_ntoa(ip)));
170 fp = x_fopen(s->fname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
171 if (!fp) {
172 END_PROFILE(sync_browse_lists);
173 _exit(1);
176 sync_child(name, nm_type, work->work_group, ip, local, servers,
177 s->fname);
179 x_fclose(fp);
180 END_PROFILE(sync_browse_lists);
181 _exit(0);
184 /**********************************************************************
185 handle one line from a completed sync file
186 **********************************************************************/
187 static void complete_one(struct sync_record *s,
188 char *sname, uint32 stype, char *comment)
190 struct work_record *work;
191 struct server_record *servrec;
193 stype &= ~SV_TYPE_LOCAL_LIST_ONLY;
195 if (stype & SV_TYPE_DOMAIN_ENUM) {
196 /* See if we can find the workgroup on this subnet. */
197 if((work=find_workgroup_on_subnet(unicast_subnet, sname))) {
198 /* We already know about this workgroup -
199 update the ttl. */
200 update_workgroup_ttl(work,lp_max_ttl());
201 } else {
202 /* Create the workgroup on the subnet. */
203 work = create_workgroup_on_subnet(unicast_subnet,
204 sname, lp_max_ttl());
205 if (work) {
206 /* remember who the master is */
207 fstrcpy(work->local_master_browser_name,
208 comment);
211 return;
214 work = find_workgroup_on_subnet(unicast_subnet, s->workgroup);
215 if (!work) {
216 DEBUG(3,("workgroup %s doesn't exist on unicast subnet?\n",
217 s->workgroup));
218 return;
221 if ((servrec = find_server_in_workgroup( work, sname))) {
222 /* Check that this is not a locally known
223 server - if so ignore the entry. */
224 if(!(servrec->serv.type & SV_TYPE_LOCAL_LIST_ONLY)) {
225 /* We already know about this server - update
226 the ttl. */
227 update_server_ttl(servrec, lp_max_ttl());
228 /* Update the type. */
229 servrec->serv.type = stype;
231 return;
234 /* Create the server in the workgroup. */
235 create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
239 /**********************************************************************
240 read the completed sync info
241 **********************************************************************/
242 static void complete_sync(struct sync_record *s)
244 XFILE *f;
245 fstring server, type_str;
246 unsigned type;
247 pstring comment;
248 pstring line;
249 const char *ptr;
250 int count=0;
252 f = x_fopen(s->fname,O_RDONLY, 0);
254 if (!f) return;
256 while (!x_feof(f)) {
258 if (!fgets_slash(line,sizeof(pstring),f)) continue;
260 ptr = line;
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;
268 sscanf(type_str, "%X", &type);
270 complete_one(s, server, type, comment);
272 count++;
275 x_fclose(f);
277 unlink(s->fname);
279 DEBUG(2,("sync with %s(%s) for workgroup %s completed (%d records)\n",
280 s->server, inet_ntoa(s->ip), s->workgroup, count));
283 /**********************************************************************
284 check for completion of any of the child processes
285 **********************************************************************/
286 void sync_check_completion(void)
288 struct sync_record *s, *next;
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 SAFE_FREE(s);