samba-tool group list: add more info to samba-tool group list
[Samba/gbeck.git] / source3 / nmbd / nmbd_sendannounce.c
blob86012b834b29934fe6c74e7b0d62ffbd9632a419
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 SMB Version handling
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 3 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, see <http://www.gnu.org/licenses/>.
26 #include "includes.h"
27 #include "../librpc/gen_ndr/svcctl.h"
28 #include "nmbd/nmbd.h"
30 extern int updatecount;
31 extern bool found_lm_clients;
33 /****************************************************************************
34 Send a browser reset packet.
35 **************************************************************************/
37 void send_browser_reset(int reset_type, const char *to_name, int to_type, struct in_addr to_ip)
39 char outbuf[1024];
40 char *p;
42 DEBUG(3,("send_browser_reset: sending reset request type %d to %s<%02x> IP %s.\n",
43 reset_type, to_name, to_type, inet_ntoa(to_ip) ));
45 memset(outbuf,'\0',sizeof(outbuf));
46 p = outbuf;
47 SCVAL(p,0,ANN_ResetBrowserState);
48 p++;
49 SCVAL(p,0,reset_type);
50 p++;
52 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
53 lp_netbios_name(), 0x0, to_name, to_type, to_ip,
54 FIRST_SUBNET->myip, DGRAM_PORT);
57 /****************************************************************************
58 Broadcast a packet to the local net requesting that all servers in this
59 workgroup announce themselves to us.
60 **************************************************************************/
62 void broadcast_announce_request(struct subnet_record *subrec, struct work_record *work)
64 char outbuf[1024];
65 char *p;
67 work->needannounce = True;
69 DEBUG(3,("broadcast_announce_request: sending announce request for workgroup %s \
70 to subnet %s\n", work->work_group, subrec->subnet_name));
72 memset(outbuf,'\0',sizeof(outbuf));
73 p = outbuf;
74 SCVAL(p,0,ANN_AnnouncementRequest);
75 p++;
77 SCVAL(p,0,work->token); /* (local) Unique workgroup token id. */
78 p++;
79 p += push_string_check(p+1, lp_netbios_name(), 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
81 send_mailslot(False, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
82 lp_netbios_name(), 0x0, work->work_group,0x1e, subrec->bcast_ip,
83 subrec->myip, DGRAM_PORT);
86 /****************************************************************************
87 Broadcast an announcement.
88 **************************************************************************/
90 static void send_announcement(struct subnet_record *subrec, int announce_type,
91 const char *from_name, const char *to_name, int to_type, struct in_addr to_ip,
92 time_t announce_interval,
93 const char *server_name, int server_type, const char *server_comment)
95 char outbuf[1024];
96 unstring upper_server_name;
97 char *p;
99 memset(outbuf,'\0',sizeof(outbuf));
100 p = outbuf+1;
102 SCVAL(outbuf,0,announce_type);
104 /* Announcement parameters. */
105 SCVAL(p,0,updatecount);
106 SIVAL(p,1,announce_interval*1000); /* Milliseconds - despite the spec. */
108 strlcpy(upper_server_name, server_name ? server_name : "", sizeof(upper_server_name));
109 if (!strupper_m(upper_server_name)) {
110 DEBUG(2,("strupper_m %s failed\n", upper_server_name));
111 return;
113 push_string_check(p+5, upper_server_name, 16, STR_ASCII|STR_TERMINATE);
115 SCVAL(p,21,SAMBA_MAJOR_NBT_ANNOUNCE_VERSION); /* Major version. */
116 SCVAL(p,22,SAMBA_MINOR_NBT_ANNOUNCE_VERSION); /* Minor version. */
118 SIVAL(p,23,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
119 /* Browse version: got from NT/AS 4.00 - Value defined in smb.h (JHT). */
120 SSVAL(p,27,BROWSER_ELECTION_VERSION);
121 SSVAL(p,29,BROWSER_CONSTANT); /* Browse signature. */
123 p += 31 + push_string_check(p+31, server_comment, sizeof(outbuf) - (p + 31 - outbuf), STR_ASCII|STR_TERMINATE);
125 send_mailslot(False,BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
126 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
127 DGRAM_PORT);
130 /****************************************************************************
131 Broadcast a LanMan announcement.
132 **************************************************************************/
134 static void send_lm_announcement(struct subnet_record *subrec, int announce_type,
135 char *from_name, char *to_name, int to_type, struct in_addr to_ip,
136 time_t announce_interval,
137 char *server_name, int server_type, char *server_comment)
139 char outbuf[1024];
140 char *p=outbuf;
142 memset(outbuf,'\0',sizeof(outbuf));
144 SSVAL(p,0,announce_type);
145 SIVAL(p,2,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
146 SCVAL(p,6,SAMBA_MAJOR_NBT_ANNOUNCE_VERSION); /* Major version. */
147 SCVAL(p,7,SAMBA_MINOR_NBT_ANNOUNCE_VERSION); /* Minor version. */
148 SSVAL(p,8,announce_interval); /* In seconds - according to spec. */
150 p += 10;
151 p += push_string_check(p, server_name, 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
152 p += push_string_check(p, server_comment, sizeof(outbuf)- (p - outbuf), STR_ASCII|STR_UPPER|STR_TERMINATE);
154 send_mailslot(False,LANMAN_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
155 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
156 DGRAM_PORT);
159 /****************************************************************************
160 We are a local master browser. Announce this to WORKGROUP<1e>.
161 ****************************************************************************/
163 static void send_local_master_announcement(struct subnet_record *subrec, struct work_record *work,
164 struct server_record *servrec)
166 /* Ensure we don't have the prohibited bit set. */
167 uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
169 DEBUG(3,("send_local_master_announcement: type %x for name %s on subnet %s for workgroup %s\n",
170 type, lp_netbios_name(), subrec->subnet_name, work->work_group));
172 send_announcement(subrec, ANN_LocalMasterAnnouncement,
173 lp_netbios_name(), /* From nbt name. */
174 work->work_group, 0x1e, /* To nbt name. */
175 subrec->bcast_ip, /* To ip. */
176 work->announce_interval, /* Time until next announce. */
177 lp_netbios_name(), /* Name to announce. */
178 type, /* Type field. */
179 servrec->serv.comment);
182 /****************************************************************************
183 Announce the workgroup WORKGROUP to MSBROWSE<01>.
184 ****************************************************************************/
186 static void send_workgroup_announcement(struct subnet_record *subrec, struct work_record *work)
188 DEBUG(3,("send_workgroup_announcement: on subnet %s for workgroup %s\n",
189 subrec->subnet_name, work->work_group));
191 send_announcement(subrec, ANN_DomainAnnouncement,
192 lp_netbios_name(), /* From nbt name. */
193 MSBROWSE, 0x1, /* To nbt name. */
194 subrec->bcast_ip, /* To ip. */
195 work->announce_interval, /* Time until next announce. */
196 work->work_group, /* Name to announce. */
197 SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT, /* workgroup announce flags. */
198 lp_netbios_name()); /* From name as comment. */
201 /****************************************************************************
202 Announce the given host to WORKGROUP<1d>.
203 ****************************************************************************/
205 static void send_host_announcement(struct subnet_record *subrec, struct work_record *work,
206 struct server_record *servrec)
208 /* Ensure we don't have the prohibited bits set. */
209 uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
211 DEBUG(3,("send_host_announcement: type %x for host %s on subnet %s for workgroup %s\n",
212 type, servrec->serv.name, subrec->subnet_name, work->work_group));
214 send_announcement(subrec, ANN_HostAnnouncement,
215 servrec->serv.name, /* From nbt name. */
216 work->work_group, 0x1d, /* To nbt name. */
217 subrec->bcast_ip, /* To ip. */
218 work->announce_interval, /* Time until next announce. */
219 servrec->serv.name, /* Name to announce. */
220 type, /* Type field. */
221 servrec->serv.comment);
224 /****************************************************************************
225 Announce the given LanMan host
226 ****************************************************************************/
228 static void send_lm_host_announcement(struct subnet_record *subrec, struct work_record *work,
229 struct server_record *servrec, int lm_interval)
231 /* Ensure we don't have the prohibited bits set. */
232 uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
234 DEBUG(3,("send_lm_host_announcement: type %x for host %s on subnet %s for workgroup %s, ttl: %d\n",
235 type, servrec->serv.name, subrec->subnet_name, work->work_group, lm_interval));
237 send_lm_announcement(subrec, ANN_HostAnnouncement,
238 servrec->serv.name, /* From nbt name. */
239 work->work_group, 0x00, /* To nbt name. */
240 subrec->bcast_ip, /* To ip. */
241 lm_interval, /* Time until next announce. */
242 servrec->serv.name, /* Name to announce (fstring not netbios name struct). */
243 type, /* Type field. */
244 servrec->serv.comment);
247 /****************************************************************************
248 Announce a server record.
249 ****************************************************************************/
251 static void announce_server(struct subnet_record *subrec, struct work_record *work,
252 struct server_record *servrec)
254 /* Only do domain announcements if we are a master and it's
255 our primary name we're being asked to announce. */
257 if (AM_LOCAL_MASTER_BROWSER(work) && strequal(lp_netbios_name(),servrec->serv.name)) {
258 send_local_master_announcement(subrec, work, servrec);
259 send_workgroup_announcement(subrec, work);
260 } else {
261 send_host_announcement(subrec, work, servrec);
265 /****************************************************************************
266 Go through all my registered names on all broadcast subnets and announce
267 them if the timeout requires it.
268 **************************************************************************/
270 void announce_my_server_names(time_t t)
272 struct subnet_record *subrec;
274 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
275 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
277 if(work) {
278 struct server_record *servrec;
280 if (work->needannounce) {
281 /* Drop back to a max 3 minute announce. This is to prevent a
282 single lost packet from breaking things for too long. */
284 work->announce_interval = MIN(work->announce_interval,
285 CHECK_TIME_MIN_HOST_ANNCE*60);
286 work->lastannounce_time = t - (work->announce_interval+1);
287 work->needannounce = False;
290 /* Announce every minute at first then progress to every 12 mins */
291 if ((t - work->lastannounce_time) < work->announce_interval)
292 continue;
294 if (work->announce_interval < (CHECK_TIME_MAX_HOST_ANNCE * 60))
295 work->announce_interval += 60;
297 work->lastannounce_time = t;
299 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
300 if (is_myname(servrec->serv.name))
301 announce_server(subrec, work, servrec);
303 } /* if work */
304 } /* for subrec */
307 /****************************************************************************
308 Go through all my registered names on all broadcast subnets and announce
309 them as a LanMan server if the timeout requires it.
310 **************************************************************************/
312 void announce_my_lm_server_names(time_t t)
314 struct subnet_record *subrec;
315 static time_t last_lm_announce_time=0;
316 int announce_interval = lp_lm_interval();
317 int lm_announce = lp_lm_announce();
319 if ((announce_interval <= 0) || (lm_announce <= 0)) {
320 /* user absolutely does not want LM announcements to be sent. */
321 return;
324 if ((lm_announce >= 2) && (!found_lm_clients)) {
325 /* has been set to 2 (Auto) but no LM clients detected (yet). */
326 return;
329 /* Otherwise: must have been set to 1 (Yes), or LM clients *have*
330 been detected. */
332 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
333 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
335 if(work) {
336 struct server_record *servrec;
338 if (last_lm_announce_time && ((t - last_lm_announce_time) < announce_interval ))
339 continue;
341 last_lm_announce_time = t;
343 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
344 if (is_myname(servrec->serv.name))
345 /* skipping equivalent of announce_server() */
346 send_lm_host_announcement(subrec, work, servrec, announce_interval);
348 } /* if work */
349 } /* for subrec */
352 /* Announce timer. Moved into global static so it can be reset
353 when a machine becomes a local master browser. */
354 static time_t announce_timer_last=0;
356 /****************************************************************************
357 Reset the announce_timer so that a local master browser announce will be done
358 immediately.
359 ****************************************************************************/
361 void reset_announce_timer(void)
363 announce_timer_last = time(NULL) - (CHECK_TIME_MST_ANNOUNCE * 60);
366 /****************************************************************************
367 Announce myself as a local master browser to a domain master browser.
368 **************************************************************************/
370 void announce_myself_to_domain_master_browser(time_t t)
372 struct subnet_record *subrec;
373 struct work_record *work;
375 if(!we_are_a_wins_client()) {
376 DEBUG(10,("announce_myself_to_domain_master_browser: no unicast subnet, ignoring.\n"));
377 return;
380 if (!announce_timer_last)
381 announce_timer_last = t;
383 if ((t-announce_timer_last) < (CHECK_TIME_MST_ANNOUNCE * 60)) {
384 DEBUG(10,("announce_myself_to_domain_master_browser: t (%d) - last(%d) < %d\n",
385 (int)t, (int)announce_timer_last,
386 CHECK_TIME_MST_ANNOUNCE * 60 ));
387 return;
390 announce_timer_last = t;
392 /* Look over all our broadcast subnets to see if any of them
393 has the state set as local master browser. */
395 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
396 for (work = subrec->workgrouplist; work; work = work->next) {
397 if (AM_LOCAL_MASTER_BROWSER(work)) {
398 DEBUG(4,( "announce_myself_to_domain_master_browser: I am a local master browser for \
399 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
401 /* Look in nmbd_browsersync.c for the rest of this code. */
402 announce_and_sync_with_domain_master_browser(subrec, work);
408 /****************************************************************************
409 Announce all samba's server entries as 'gone'.
410 This must *only* be called on shutdown.
411 ****************************************************************************/
413 void announce_my_servers_removed(void)
415 int announce_interval = lp_lm_interval();
416 int lm_announce = lp_lm_announce();
417 struct subnet_record *subrec;
419 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
420 struct work_record *work;
421 for (work = subrec->workgrouplist; work; work = work->next) {
422 struct server_record *servrec;
424 work->announce_interval = 0;
425 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
426 if (!is_myname(servrec->serv.name))
427 continue;
428 servrec->serv.type = 0;
429 if(AM_LOCAL_MASTER_BROWSER(work))
430 send_local_master_announcement(subrec, work, servrec);
431 send_host_announcement(subrec, work, servrec);
433 if ((announce_interval <= 0) || (lm_announce <= 0)) {
434 /* user absolutely does not want LM announcements to be sent. */
435 continue;
438 if ((lm_announce >= 2) && (!found_lm_clients)) {
439 /* has been set to 2 (Auto) but no LM clients detected (yet). */
440 continue;
444 * lm announce was set or we have seen lm announcements, so do
445 * a lm announcement of host removed.
448 send_lm_host_announcement(subrec, work, servrec, 0);
454 /****************************************************************************
455 Do all the "remote" announcements. These are used to put ourselves
456 on a remote browse list. They are done blind, no checking is done to
457 see if there is actually a local master browser at the other end.
458 **************************************************************************/
460 void announce_remote(time_t t)
462 char *s;
463 const char *ptr;
464 static time_t last_time = 0;
465 char *s2;
466 struct in_addr addr;
467 char *comment;
468 int stype = lp_default_server_announce();
469 TALLOC_CTX *frame = NULL;
471 if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
472 return;
474 last_time = t;
476 s = lp_remote_announce(talloc_tos());
477 if (!*s)
478 return;
480 comment = string_truncate(lp_serverstring(talloc_tos()),
481 MAX_SERVER_STRING_LENGTH);
483 frame = talloc_stackframe();
484 for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) {
485 /* The entries are of the form a.b.c.d/WORKGROUP with
486 WORKGROUP being optional */
487 const char *wgroup;
488 char *pwgroup;
489 int i;
491 pwgroup = strchr_m(s2,'/');
492 if (pwgroup)
493 *pwgroup++ = 0;
494 if (!pwgroup || !*pwgroup)
495 wgroup = lp_workgroup();
496 else
497 wgroup = pwgroup;
499 addr = interpret_addr2(s2);
501 /* Announce all our names including aliases */
502 /* Give the ip address as the address of our first
503 broadcast subnet. */
505 for(i=0; my_netbios_names(i); i++) {
506 const char *name = my_netbios_names(i);
508 DEBUG(5,("announce_remote: Doing remote announce for server %s to IP %s.\n",
509 name, inet_ntoa(addr) ));
511 send_announcement(FIRST_SUBNET, ANN_HostAnnouncement,
512 name, /* From nbt name. */
513 wgroup, 0x1d, /* To nbt name. */
514 addr, /* To ip. */
515 REMOTE_ANNOUNCE_INTERVAL, /* Time until next announce. */
516 name, /* Name to announce. */
517 stype, /* Type field. */
518 comment);
521 TALLOC_FREE(frame);
524 /****************************************************************************
525 Implement the 'remote browse sync' feature Andrew added.
526 These are used to put our browse lists into remote browse lists.
527 **************************************************************************/
529 void browse_sync_remote(time_t t)
531 char *s;
532 const char *ptr;
533 static time_t last_time = 0;
534 char *s2;
535 struct in_addr addr;
536 struct work_record *work;
537 char outbuf[1024];
538 char *p;
539 unstring myname;
540 TALLOC_CTX *frame = NULL;
542 if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
543 return;
545 last_time = t;
547 s = lp_remote_browse_sync(talloc_tos());
548 if (!*s)
549 return;
552 * We only do this if we are the local master browser
553 * for our workgroup on the firsst subnet.
556 if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL) {
557 DEBUG(0,("browse_sync_remote: Cannot find workgroup %s on subnet %s\n",
558 lp_workgroup(), FIRST_SUBNET->subnet_name ));
559 return;
562 if(!AM_LOCAL_MASTER_BROWSER(work)) {
563 DEBUG(5,("browse_sync_remote: We can only do this if we are a local master browser \
564 for workgroup %s on subnet %s.\n", lp_workgroup(), FIRST_SUBNET->subnet_name ));
565 return;
568 memset(outbuf,'\0',sizeof(outbuf));
569 p = outbuf;
570 SCVAL(p,0,ANN_MasterAnnouncement);
571 p++;
573 unstrcpy(myname, lp_netbios_name());
574 if (!strupper_m(myname)) {
575 DEBUG(2,("strupper_m %s failed\n", myname));
576 return;
578 myname[15]='\0';
579 push_ascii(p, myname, sizeof(outbuf)-PTR_DIFF(p,outbuf)-1, STR_TERMINATE);
581 p = skip_string(outbuf,sizeof(outbuf),p);
583 frame = talloc_stackframe();
584 for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) {
585 /* The entries are of the form a.b.c.d */
586 addr = interpret_addr2(s2);
588 DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
589 lp_netbios_name(), inet_ntoa(addr) ));
591 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
592 lp_netbios_name(), 0x0, "*", 0x0, addr, FIRST_SUBNET->myip, DGRAM_PORT);
594 TALLOC_FREE(frame);