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
9 Copyright (C) John H Terpstra 1995-1998
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 extern int updatecount;
30 extern BOOL found_lm_clients;
32 /****************************************************************************
33 Send a browser reset packet.
34 **************************************************************************/
36 void send_browser_reset(int reset_type, const char *to_name, int to_type, struct in_addr to_ip)
41 DEBUG(3,("send_browser_reset: sending reset request type %d to %s<%02x> IP %s.\n",
42 reset_type, to_name, to_type, inet_ntoa(to_ip) ));
44 memset(outbuf,'\0',sizeof(outbuf));
46 SCVAL(p,0,ANN_ResetBrowserState);
48 SCVAL(p,0,reset_type);
51 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
52 global_myname(), 0x0, to_name, to_type, to_ip,
53 FIRST_SUBNET->myip, DGRAM_PORT);
56 /****************************************************************************
57 Broadcast a packet to the local net requesting that all servers in this
58 workgroup announce themselves to us.
59 **************************************************************************/
61 void broadcast_announce_request(struct subnet_record *subrec, struct work_record *work)
66 work->needannounce = True;
68 DEBUG(3,("broadcast_announce_request: sending announce request for workgroup %s \
69 to subnet %s\n", work->work_group, subrec->subnet_name));
71 memset(outbuf,'\0',sizeof(outbuf));
73 SCVAL(p,0,ANN_AnnouncementRequest);
76 SCVAL(p,0,work->token); /* (local) Unique workgroup token id. */
78 p += push_string(NULL, p+1, global_myname(), 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
80 send_mailslot(False, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
81 global_myname(), 0x0, work->work_group,0x1e, subrec->bcast_ip,
82 subrec->myip, DGRAM_PORT);
85 /****************************************************************************
86 Broadcast an announcement.
87 **************************************************************************/
89 static void send_announcement(struct subnet_record *subrec, int announce_type,
90 const char *from_name, const char *to_name, int to_type, struct in_addr to_ip,
91 time_t announce_interval,
92 const char *server_name, int server_type, const char *server_comment)
97 memset(outbuf,'\0',sizeof(outbuf));
100 SCVAL(outbuf,0,announce_type);
102 /* Announcement parameters. */
103 SCVAL(p,0,updatecount);
104 SIVAL(p,1,announce_interval*1000); /* Milliseconds - despite the spec. */
106 push_string(NULL, p+5, server_name, 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
108 SCVAL(p,21,lp_major_announce_version()); /* Major version. */
109 SCVAL(p,22,lp_minor_announce_version()); /* Minor version. */
111 SIVAL(p,23,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
112 /* Browse version: got from NT/AS 4.00 - Value defined in smb.h (JHT). */
113 SSVAL(p,27,BROWSER_ELECTION_VERSION);
114 SSVAL(p,29,BROWSER_CONSTANT); /* Browse signature. */
116 p += 31 + push_string(NULL, p+31, server_comment, -1, STR_ASCII|STR_TERMINATE);
118 send_mailslot(False,BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
119 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
123 /****************************************************************************
124 Broadcast a LanMan announcement.
125 **************************************************************************/
127 static void send_lm_announcement(struct subnet_record *subrec, int announce_type,
128 char *from_name, char *to_name, int to_type, struct in_addr to_ip,
129 time_t announce_interval,
130 char *server_name, int server_type, char *server_comment)
135 memset(outbuf,'\0',sizeof(outbuf));
137 SSVAL(p,0,announce_type);
138 SIVAL(p,2,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
139 SCVAL(p,6,lp_major_announce_version()); /* Major version. */
140 SCVAL(p,7,lp_minor_announce_version()); /* Minor version. */
141 SSVAL(p,8,announce_interval); /* In seconds - according to spec. */
144 /*StrnCpy(p,server_name,15);
146 p = skip_string(p,1);
147 pstrcpy(p,server_comment);
148 p = skip_string(p,1);*/
149 p += push_string(NULL, p, server_name, 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
150 p += push_string(NULL, p, server_comment, sizeof(pstring)-15, STR_ASCII|STR_UPPER|STR_TERMINATE);
152 send_mailslot(False,LANMAN_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
153 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
157 /****************************************************************************
158 We are a local master browser. Announce this to WORKGROUP<1e>.
159 ****************************************************************************/
161 static void send_local_master_announcement(struct subnet_record *subrec, struct work_record *work,
162 struct server_record *servrec)
164 /* Ensure we don't have the prohibited bit set. */
165 uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
167 DEBUG(3,("send_local_master_announcement: type %x for name %s on subnet %s for workgroup %s\n",
168 type, global_myname(), subrec->subnet_name, work->work_group));
170 send_announcement(subrec, ANN_LocalMasterAnnouncement,
171 global_myname(), /* From nbt name. */
172 work->work_group, 0x1e, /* To nbt name. */
173 subrec->bcast_ip, /* To ip. */
174 work->announce_interval, /* Time until next announce. */
175 global_myname(), /* Name to announce. */
176 type, /* Type field. */
177 servrec->serv.comment);
180 /****************************************************************************
181 Announce the workgroup WORKGROUP to MSBROWSE<01>.
182 ****************************************************************************/
184 static void send_workgroup_announcement(struct subnet_record *subrec, struct work_record *work)
186 DEBUG(3,("send_workgroup_announcement: on subnet %s for workgroup %s\n",
187 subrec->subnet_name, work->work_group));
189 send_announcement(subrec, ANN_DomainAnnouncement,
190 global_myname(), /* From nbt name. */
191 MSBROWSE, 0x1, /* To nbt name. */
192 subrec->bcast_ip, /* To ip. */
193 work->announce_interval, /* Time until next announce. */
194 work->work_group, /* Name to announce. */
195 SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT, /* workgroup announce flags. */
196 global_myname()); /* From name as comment. */
199 /****************************************************************************
200 Announce the given host to WORKGROUP<1d>.
201 ****************************************************************************/
203 static void send_host_announcement(struct subnet_record *subrec, struct work_record *work,
204 struct server_record *servrec)
206 /* Ensure we don't have the prohibited bits set. */
207 uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
209 DEBUG(3,("send_host_announcement: type %x for host %s on subnet %s for workgroup %s\n",
210 type, servrec->serv.name, subrec->subnet_name, work->work_group));
212 send_announcement(subrec, ANN_HostAnnouncement,
213 servrec->serv.name, /* From nbt name. */
214 work->work_group, 0x1d, /* To nbt name. */
215 subrec->bcast_ip, /* To ip. */
216 work->announce_interval, /* Time until next announce. */
217 servrec->serv.name, /* Name to announce. */
218 type, /* Type field. */
219 servrec->serv.comment);
222 /****************************************************************************
223 Announce the given LanMan host
224 ****************************************************************************/
226 static void send_lm_host_announcement(struct subnet_record *subrec, struct work_record *work,
227 struct server_record *servrec, int lm_interval)
229 /* Ensure we don't have the prohibited bits set. */
230 uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
232 DEBUG(3,("send_lm_host_announcement: type %x for host %s on subnet %s for workgroup %s, ttl: %d\n",
233 type, servrec->serv.name, subrec->subnet_name, work->work_group, lm_interval));
235 send_lm_announcement(subrec, ANN_HostAnnouncement,
236 servrec->serv.name, /* From nbt name. */
237 work->work_group, 0x00, /* To nbt name. */
238 subrec->bcast_ip, /* To ip. */
239 lm_interval, /* Time until next announce. */
240 servrec->serv.name, /* Name to announce. */
241 type, /* Type field. */
242 servrec->serv.comment);
245 /****************************************************************************
246 Announce a server record.
247 ****************************************************************************/
249 static void announce_server(struct subnet_record *subrec, struct work_record *work,
250 struct server_record *servrec)
252 /* Only do domain announcements if we are a master and it's
253 our primary name we're being asked to announce. */
255 if (AM_LOCAL_MASTER_BROWSER(work) && strequal(global_myname(),servrec->serv.name))
257 send_local_master_announcement(subrec, work, servrec);
258 send_workgroup_announcement(subrec, work);
262 send_host_announcement(subrec, work, servrec);
266 /****************************************************************************
267 Go through all my registered names on all broadcast subnets and announce
268 them if the timeout requires it.
269 **************************************************************************/
271 void announce_my_server_names(time_t t)
273 struct subnet_record *subrec;
275 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
277 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
281 struct server_record *servrec;
283 if (work->needannounce)
285 /* Drop back to a max 3 minute announce. This is to prevent a
286 single lost packet from breaking things for too long. */
288 work->announce_interval = MIN(work->announce_interval,
289 CHECK_TIME_MIN_HOST_ANNCE*60);
290 work->lastannounce_time = t - (work->announce_interval+1);
291 work->needannounce = False;
294 /* Announce every minute at first then progress to every 12 mins */
295 if ((t - work->lastannounce_time) < work->announce_interval)
298 if (work->announce_interval < (CHECK_TIME_MAX_HOST_ANNCE * 60))
299 work->announce_interval += 60;
301 work->lastannounce_time = t;
303 for (servrec = work->serverlist; servrec; servrec = servrec->next)
305 if (is_myname(servrec->serv.name))
306 announce_server(subrec, work, servrec);
312 /****************************************************************************
313 Go through all my registered names on all broadcast subnets and announce
314 them as a LanMan server if the timeout requires it.
315 **************************************************************************/
317 void announce_my_lm_server_names(time_t t)
319 struct subnet_record *subrec;
320 static time_t last_lm_announce_time=0;
321 int announce_interval = lp_lm_interval();
322 int lm_announce = lp_lm_announce();
324 if ((announce_interval <= 0) || (lm_announce <= 0))
326 /* user absolutely does not want LM announcements to be sent. */
330 if ((lm_announce >= 2) && (!found_lm_clients))
332 /* has been set to 2 (Auto) but no LM clients detected (yet). */
336 /* Otherwise: must have been set to 1 (Yes), or LM clients *have*
339 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
341 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
345 struct server_record *servrec;
347 if (last_lm_announce_time && ((t - last_lm_announce_time) < announce_interval ))
350 last_lm_announce_time = t;
352 for (servrec = work->serverlist; servrec; servrec = servrec->next)
354 if (is_myname(servrec->serv.name))
355 /* skipping equivalent of announce_server() */
356 send_lm_host_announcement(subrec, work, servrec, announce_interval);
362 /* Announce timer. Moved into global static so it can be reset
363 when a machine becomes a local master browser. */
364 static time_t announce_timer_last=0;
366 /****************************************************************************
367 Reset the announce_timer so that a local master browser announce will be done
369 ****************************************************************************/
371 void reset_announce_timer(void)
373 announce_timer_last = time(NULL) - (CHECK_TIME_MST_ANNOUNCE * 60);
376 /****************************************************************************
377 Announce myself as a local master browser to a domain master browser.
378 **************************************************************************/
380 void announce_myself_to_domain_master_browser(time_t t)
382 struct subnet_record *subrec;
383 struct work_record *work;
385 if(!we_are_a_wins_client())
387 DEBUG(10,("announce_myself_to_domain_master_browser: no unicast subnet, ignoring.\n"));
391 if (!announce_timer_last)
392 announce_timer_last = t;
394 if ((t-announce_timer_last) < (CHECK_TIME_MST_ANNOUNCE * 60))
396 DEBUG(10,("announce_myself_to_domain_master_browser: t (%d) - last(%d) < %d\n",
397 (int)t, (int)announce_timer_last,
398 CHECK_TIME_MST_ANNOUNCE * 60 ));
402 announce_timer_last = t;
404 /* Look over all our broadcast subnets to see if any of them
405 has the state set as local master browser. */
407 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
409 for (work = subrec->workgrouplist; work; work = work->next)
411 if (AM_LOCAL_MASTER_BROWSER(work))
413 DEBUG(4,( "announce_myself_to_domain_master_browser: I am a local master browser for \
414 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
416 /* Look in nmbd_browsersync.c for the rest of this code. */
417 announce_and_sync_with_domain_master_browser(subrec, work);
423 /****************************************************************************
424 Announce all samba's server entries as 'gone'.
425 This must *only* be called on shutdown.
426 ****************************************************************************/
428 void announce_my_servers_removed(void)
430 int announce_interval = lp_lm_interval();
431 int lm_announce = lp_lm_announce();
432 struct subnet_record *subrec;
434 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
436 struct work_record *work;
437 for (work = subrec->workgrouplist; work; work = work->next)
439 struct server_record *servrec;
441 work->announce_interval = 0;
442 for (servrec = work->serverlist; servrec; servrec = servrec->next)
444 if (!is_myname(servrec->serv.name))
446 servrec->serv.type = 0;
447 if(AM_LOCAL_MASTER_BROWSER(work))
448 send_local_master_announcement(subrec, work, servrec);
449 send_host_announcement(subrec, work, servrec);
452 if ((announce_interval <= 0) || (lm_announce <= 0))
454 /* user absolutely does not want LM announcements to be sent. */
458 if ((lm_announce >= 2) && (!found_lm_clients))
460 /* has been set to 2 (Auto) but no LM clients detected (yet). */
465 * lm announce was set or we have seen lm announcements, so do
466 * a lm announcement of host removed.
469 send_lm_host_announcement(subrec, work, servrec, 0);
475 /****************************************************************************
476 Do all the "remote" announcements. These are used to put ourselves
477 on a remote browse list. They are done blind, no checking is done to
478 see if there is actually a local master browser at the other end.
479 **************************************************************************/
481 void announce_remote(time_t t)
485 static time_t last_time = 0;
489 int stype = lp_default_server_announce();
491 if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
496 s = lp_remote_announce();
500 comment = string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH);
502 for (ptr=s; next_token(&ptr,s2,NULL,sizeof(s2)); )
504 /* The entries are of the form a.b.c.d/WORKGROUP with
505 WORKGROUP being optional */
510 pwgroup = strchr_m(s2,'/');
513 if (!pwgroup || !*pwgroup)
514 wgroup = lp_workgroup();
518 addr = *interpret_addr2(s2);
520 /* Announce all our names including aliases */
521 /* Give the ip address as the address of our first
524 for(i=0; my_netbios_names(i); i++)
526 const char *name = my_netbios_names(i);
528 DEBUG(5,("announce_remote: Doing remote announce for server %s to IP %s.\n",
529 name, inet_ntoa(addr) ));
531 send_announcement(FIRST_SUBNET, ANN_HostAnnouncement,
532 name, /* From nbt name. */
533 wgroup, 0x1d, /* To nbt name. */
535 REMOTE_ANNOUNCE_INTERVAL, /* Time until next announce. */
536 name, /* Name to announce. */
537 stype, /* Type field. */
543 /****************************************************************************
544 Implement the 'remote browse sync' feature Andrew added.
545 These are used to put our browse lists into remote browse lists.
546 **************************************************************************/
548 void browse_sync_remote(time_t t)
552 static time_t last_time = 0;
555 struct work_record *work;
560 if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
565 s = lp_remote_browse_sync();
570 * We only do this if we are the local master browser
571 * for our workgroup on the firsst subnet.
574 if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL)
576 DEBUG(0,("browse_sync_remote: Cannot find workgroup %s on subnet %s\n",
577 lp_workgroup(), FIRST_SUBNET->subnet_name ));
581 if(!AM_LOCAL_MASTER_BROWSER(work))
583 DEBUG(5,("browse_sync_remote: We can only do this if we are a local master browser \
584 for workgroup %s on subnet %s.\n", lp_workgroup(), FIRST_SUBNET->subnet_name ));
588 memset(outbuf,'\0',sizeof(outbuf));
590 SCVAL(p,0,ANN_MasterAnnouncement);
593 fstrcpy(myname, global_myname());
596 push_pstring_base(p, myname, outbuf);
598 p = skip_string(p,1);
600 for (ptr=s; next_token(&ptr,s2,NULL,sizeof(s2)); )
602 /* The entries are of the form a.b.c.d */
603 addr = *interpret_addr2(s2);
605 DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
606 global_myname(), inet_ntoa(addr) ));
608 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
609 global_myname(), 0x0, "*", 0x0, addr, FIRST_SUBNET->myip, DGRAM_PORT);