s3:winbindd: Remove unused set_routing_domain()
[samba.git] / source3 / nmbd / nmbd_sendannounce.c
blob4e8be04f3b58b0e655b4f38c9734be7c13a475a8
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"
29 #include "lib/util/string_wrappers.h"
31 extern int updatecount;
32 extern bool found_lm_clients;
34 /****************************************************************************
35 Send a browser reset packet.
36 **************************************************************************/
38 void send_browser_reset(int reset_type, const char *to_name, int to_type, struct in_addr to_ip)
40 char outbuf[1024];
41 char *p;
43 DBG_NOTICE("send_browser_reset: sending reset request type %d to %s<%02x> IP %s.\n",
44 reset_type, to_name, to_type, inet_ntoa(to_ip) );
46 memset(outbuf,'\0',sizeof(outbuf));
47 p = outbuf;
48 SCVAL(p,0,ANN_ResetBrowserState);
49 p++;
50 SCVAL(p,0,reset_type);
51 p++;
53 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
54 lp_netbios_name(), 0x0, to_name, to_type, to_ip,
55 FIRST_SUBNET->myip, DGRAM_PORT);
58 /****************************************************************************
59 Broadcast a packet to the local net requesting that all servers in this
60 workgroup announce themselves to us.
61 **************************************************************************/
63 void broadcast_announce_request(struct subnet_record *subrec, struct work_record *work)
65 char outbuf[1024];
66 char *p;
68 work->needannounce = True;
70 DBG_NOTICE("broadcast_announce_request: sending announce request for workgroup %s \
71 to subnet %s\n", work->work_group, subrec->subnet_name);
73 memset(outbuf,'\0',sizeof(outbuf));
74 p = outbuf;
75 SCVAL(p,0,ANN_AnnouncementRequest);
76 p++;
78 SCVAL(p,0,work->token); /* (local) Unique workgroup token id. */
79 p++;
80 p += push_string_check(p+1, lp_netbios_name(), 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
82 send_mailslot(False, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
83 lp_netbios_name(), 0x0, work->work_group,0x1e, subrec->bcast_ip,
84 subrec->myip, DGRAM_PORT);
87 /****************************************************************************
88 Broadcast an announcement.
89 **************************************************************************/
91 static void send_announcement(struct subnet_record *subrec, int announce_type,
92 const char *from_name, const char *to_name, int to_type, struct in_addr to_ip,
93 time_t announce_interval,
94 const char *server_name, int server_type, const char *server_comment)
96 char outbuf[1024];
97 unstring upper_server_name;
98 char *p;
100 memset(outbuf,'\0',sizeof(outbuf));
101 p = outbuf+1;
103 SCVAL(outbuf,0,announce_type);
105 /* Announcement parameters. */
106 SCVAL(p,0,updatecount);
107 SIVAL(p,1,announce_interval*1000); /* Milliseconds - despite the spec. */
109 strlcpy(upper_server_name, server_name ? server_name : "", sizeof(upper_server_name));
110 if (!strupper_m(upper_server_name)) {
111 DBG_WARNING("strupper_m %s failed\n", upper_server_name);
112 return;
114 push_string_check(p+5, upper_server_name, 16, STR_ASCII|STR_TERMINATE);
116 SCVAL(p,21,SAMBA_MAJOR_NBT_ANNOUNCE_VERSION); /* Major version. */
117 SCVAL(p,22,SAMBA_MINOR_NBT_ANNOUNCE_VERSION); /* Minor version. */
119 SIVAL(p,23,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
120 /* Browse version: got from NT/AS 4.00 - Value defined in smb.h (JHT). */
121 SSVAL(p,27,BROWSER_ELECTION_VERSION);
122 SSVAL(p,29,BROWSER_CONSTANT); /* Browse signature. */
124 p += 31 + push_string_check(p+31, server_comment, sizeof(outbuf) - (p + 31 - outbuf), STR_ASCII|STR_TERMINATE);
126 send_mailslot(False,BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
127 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
128 DGRAM_PORT);
131 /****************************************************************************
132 Broadcast a LanMan announcement.
133 **************************************************************************/
135 static void send_lm_announcement(struct subnet_record *subrec, int announce_type,
136 char *from_name, char *to_name, int to_type, struct in_addr to_ip,
137 time_t announce_interval,
138 char *server_name, int server_type, char *server_comment)
140 char outbuf[1024];
141 char *p=outbuf;
143 memset(outbuf,'\0',sizeof(outbuf));
145 SSVAL(p,0,announce_type);
146 SIVAL(p,2,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
147 SCVAL(p,6,SAMBA_MAJOR_NBT_ANNOUNCE_VERSION); /* Major version. */
148 SCVAL(p,7,SAMBA_MINOR_NBT_ANNOUNCE_VERSION); /* Minor version. */
149 SSVAL(p,8,announce_interval); /* In seconds - according to spec. */
151 p += 10;
152 p += push_string_check(p, server_name, 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
153 p += push_string_check(p, server_comment, sizeof(outbuf)- (p - outbuf), STR_ASCII|STR_UPPER|STR_TERMINATE);
155 send_mailslot(False,LANMAN_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
156 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
157 DGRAM_PORT);
160 /****************************************************************************
161 We are a local master browser. Announce this to WORKGROUP<1e>.
162 ****************************************************************************/
164 static void send_local_master_announcement(struct subnet_record *subrec, struct work_record *work,
165 struct server_record *servrec)
167 /* Ensure we don't have the prohibited bit set. */
168 uint32_t type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
170 DBG_NOTICE("send_local_master_announcement: type %x for name %s on subnet %s for workgroup %s\n",
171 type, lp_netbios_name(), subrec->subnet_name, work->work_group);
173 send_announcement(subrec, ANN_LocalMasterAnnouncement,
174 lp_netbios_name(), /* From nbt name. */
175 work->work_group, 0x1e, /* To nbt name. */
176 subrec->bcast_ip, /* To ip. */
177 work->announce_interval, /* Time until next announce. */
178 lp_netbios_name(), /* Name to announce. */
179 type, /* Type field. */
180 servrec->serv.comment);
183 /****************************************************************************
184 Announce the workgroup WORKGROUP to MSBROWSE<01>.
185 ****************************************************************************/
187 static void send_workgroup_announcement(struct subnet_record *subrec, struct work_record *work)
189 DBG_NOTICE("send_workgroup_announcement: on subnet %s for workgroup %s\n",
190 subrec->subnet_name, work->work_group);
192 send_announcement(subrec, ANN_DomainAnnouncement,
193 lp_netbios_name(), /* From nbt name. */
194 MSBROWSE, 0x1, /* To nbt name. */
195 subrec->bcast_ip, /* To ip. */
196 work->announce_interval, /* Time until next announce. */
197 work->work_group, /* Name to announce. */
198 SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT, /* workgroup announce flags. */
199 lp_netbios_name()); /* From name as comment. */
202 /****************************************************************************
203 Announce the given host to WORKGROUP<1d>.
204 ****************************************************************************/
206 static void send_host_announcement(struct subnet_record *subrec, struct work_record *work,
207 struct server_record *servrec)
209 /* Ensure we don't have the prohibited bits set. */
210 uint32_t type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
212 DBG_NOTICE("send_host_announcement: type %x for host %s on subnet %s for workgroup %s\n",
213 type, servrec->serv.name, subrec->subnet_name, work->work_group);
215 send_announcement(subrec, ANN_HostAnnouncement,
216 servrec->serv.name, /* From nbt name. */
217 work->work_group, 0x1d, /* To nbt name. */
218 subrec->bcast_ip, /* To ip. */
219 work->announce_interval, /* Time until next announce. */
220 servrec->serv.name, /* Name to announce. */
221 type, /* Type field. */
222 servrec->serv.comment);
225 /****************************************************************************
226 Announce the given LanMan host
227 ****************************************************************************/
229 static void send_lm_host_announcement(struct subnet_record *subrec, struct work_record *work,
230 struct server_record *servrec, int lm_interval)
232 /* Ensure we don't have the prohibited bits set. */
233 uint32_t type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
235 DBG_NOTICE("send_lm_host_announcement: type %x for host %s on subnet %s for workgroup %s, ttl: %d\n",
236 type, servrec->serv.name, subrec->subnet_name, work->work_group, lm_interval);
238 send_lm_announcement(subrec, ANN_HostAnnouncement,
239 servrec->serv.name, /* From nbt name. */
240 work->work_group, 0x00, /* To nbt name. */
241 subrec->bcast_ip, /* To ip. */
242 lm_interval, /* Time until next announce. */
243 servrec->serv.name, /* Name to announce (fstring not netbios name struct). */
244 type, /* Type field. */
245 servrec->serv.comment);
248 /****************************************************************************
249 Announce a server record.
250 ****************************************************************************/
252 static void announce_server(struct subnet_record *subrec, struct work_record *work,
253 struct server_record *servrec)
255 /* Only do domain announcements if we are a master and it's
256 our primary name we're being asked to announce. */
258 if (AM_LOCAL_MASTER_BROWSER(work) && strequal(lp_netbios_name(),servrec->serv.name)) {
259 send_local_master_announcement(subrec, work, servrec);
260 send_workgroup_announcement(subrec, work);
261 } else {
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)) {
276 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
278 if(work) {
279 struct server_record *servrec;
281 if (work->needannounce) {
282 /* Drop back to a max 3 minute announce. This is to prevent a
283 single lost packet from breaking things for too long. */
285 work->announce_interval = MIN(work->announce_interval,
286 CHECK_TIME_MIN_HOST_ANNCE*60);
287 work->lastannounce_time = t - (work->announce_interval+1);
288 work->needannounce = False;
291 /* Announce every minute at first then progress to every 12 mins */
292 if (t >= work->lastannounce_time &&
293 (t - work->lastannounce_time) < work->announce_interval) {
294 continue;
297 if (work->announce_interval < (CHECK_TIME_MAX_HOST_ANNCE * 60))
298 work->announce_interval += 60;
300 work->lastannounce_time = t;
302 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
303 if (is_myname(servrec->serv.name))
304 announce_server(subrec, work, servrec);
306 } /* if work */
307 } /* for subrec */
310 /****************************************************************************
311 Go through all my registered names on all broadcast subnets and announce
312 them as a LanMan server if the timeout requires it.
313 **************************************************************************/
315 void announce_my_lm_server_names(time_t t)
317 struct subnet_record *subrec;
318 static time_t last_lm_announce_time=0;
319 int announce_interval = lp_lm_interval();
320 int lm_announce = lp_lm_announce();
322 if ((announce_interval <= 0) || (lm_announce <= 0)) {
323 /* user absolutely does not want LM announcements to be sent. */
324 return;
327 if ((lm_announce >= 2) && (!found_lm_clients)) {
328 /* has been set to 2 (Auto) but no LM clients detected (yet). */
329 return;
332 /* Otherwise: must have been set to 1 (Yes), or LM clients *have*
333 been detected. */
335 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
336 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
338 if(work) {
339 struct server_record *servrec;
341 if (last_lm_announce_time && ((t - last_lm_announce_time) < announce_interval ))
342 continue;
344 last_lm_announce_time = t;
346 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
347 if (is_myname(servrec->serv.name))
348 /* skipping equivalent of announce_server() */
349 send_lm_host_announcement(subrec, work, servrec, announce_interval);
351 } /* if work */
352 } /* for subrec */
355 /* Announce timer. Moved into global static so it can be reset
356 when a machine becomes a local master browser. */
357 static time_t announce_timer_last=0;
359 /****************************************************************************
360 Reset the announce_timer so that a local master browser announce will be done
361 immediately.
362 ****************************************************************************/
364 void reset_announce_timer(void)
366 announce_timer_last = time(NULL) - (CHECK_TIME_MST_ANNOUNCE * 60);
369 /****************************************************************************
370 Announce myself as a local master browser to a domain master browser.
371 **************************************************************************/
373 void announce_myself_to_domain_master_browser(time_t t)
375 struct subnet_record *subrec;
376 struct work_record *work;
378 if(!we_are_a_wins_client()) {
379 DBG_DEBUG("announce_myself_to_domain_master_browser: no unicast subnet, ignoring.\n");
380 return;
383 if (!announce_timer_last)
384 announce_timer_last = t;
386 if ((t-announce_timer_last) < (CHECK_TIME_MST_ANNOUNCE * 60)) {
387 DBG_DEBUG("announce_myself_to_domain_master_browser: t (%d) - last(%d) < %d\n",
388 (int)t, (int)announce_timer_last,
389 CHECK_TIME_MST_ANNOUNCE * 60 );
390 return;
393 announce_timer_last = t;
395 /* Look over all our broadcast subnets to see if any of them
396 has the state set as local master browser. */
398 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
399 for (work = subrec->workgrouplist; work; work = work->next) {
400 if (AM_LOCAL_MASTER_BROWSER(work)) {
401 DBG_NOTICE( "announce_myself_to_domain_master_browser: I am a local master browser for \
402 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name);
404 /* Look in nmbd_browsersync.c for the rest of this code. */
405 announce_and_sync_with_domain_master_browser(subrec, work);
411 /****************************************************************************
412 Announce all samba's server entries as 'gone'.
413 This must *only* be called on shutdown.
414 ****************************************************************************/
416 void announce_my_servers_removed(void)
418 int announce_interval = lp_lm_interval();
419 int lm_announce = lp_lm_announce();
420 struct subnet_record *subrec;
422 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
423 struct work_record *work;
424 for (work = subrec->workgrouplist; work; work = work->next) {
425 struct server_record *servrec;
427 work->announce_interval = 0;
428 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
429 if (!is_myname(servrec->serv.name))
430 continue;
431 servrec->serv.type = 0;
432 if(AM_LOCAL_MASTER_BROWSER(work))
433 send_local_master_announcement(subrec, work, servrec);
434 send_host_announcement(subrec, work, servrec);
436 if ((announce_interval <= 0) || (lm_announce <= 0)) {
437 /* user absolutely does not want LM announcements to be sent. */
438 continue;
441 if ((lm_announce >= 2) && (!found_lm_clients)) {
442 /* has been set to 2 (Auto) but no LM clients detected (yet). */
443 continue;
447 * lm announce was set or we have seen lm announcements, so do
448 * a lm announcement of host removed.
451 send_lm_host_announcement(subrec, work, servrec, 0);
457 /****************************************************************************
458 Do all the "remote" announcements. These are used to put ourselves
459 on a remote browse list. They are done blind, no checking is done to
460 see if there is actually a local master browser at the other end.
461 **************************************************************************/
463 void announce_remote(time_t t)
465 char *s = NULL;
466 const char *ptr;
467 static time_t last_time = 0;
468 char *s2;
469 struct in_addr addr;
470 char *comment;
471 int stype = lp_default_server_announce();
472 TALLOC_CTX *frame = NULL;
473 const struct loadparm_substitution *lp_sub =
474 loadparm_s3_global_substitution();
476 if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
477 return;
479 last_time = t;
481 s = lp_remote_announce(talloc_tos(), lp_sub);
482 if (!*s)
483 return;
485 comment = string_truncate(lp_server_string(talloc_tos(), lp_sub),
486 MAX_SERVER_STRING_LENGTH);
488 frame = talloc_stackframe();
489 for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) {
490 /* The entries are of the form a.b.c.d/WORKGROUP with
491 WORKGROUP being optional */
492 const char *wgroup;
493 char *pwgroup;
494 int i;
496 pwgroup = strchr_m(s2,'/');
497 if (pwgroup)
498 *pwgroup++ = 0;
499 if (!pwgroup || !*pwgroup)
500 wgroup = lp_workgroup();
501 else
502 wgroup = pwgroup;
504 addr = interpret_addr2(s2);
506 /* Announce all our names including aliases */
507 /* Give the ip address as the address of our first
508 broadcast subnet. */
510 for(i=0; my_netbios_names(i); i++) {
511 const char *name = my_netbios_names(i);
513 DBG_INFO("announce_remote: Doing remote announce for server %s to IP %s.\n",
514 name, inet_ntoa(addr) );
516 send_announcement(FIRST_SUBNET, ANN_HostAnnouncement,
517 name, /* From nbt name. */
518 wgroup, 0x1d, /* To nbt name. */
519 addr, /* To ip. */
520 REMOTE_ANNOUNCE_INTERVAL, /* Time until next announce. */
521 name, /* Name to announce. */
522 stype, /* Type field. */
523 comment);
526 TALLOC_FREE(frame);
529 /****************************************************************************
530 Implement the 'remote browse sync' feature Andrew added.
531 These are used to put our browse lists into remote browse lists.
532 **************************************************************************/
534 void browse_sync_remote(time_t t)
536 char *s;
537 const char *ptr;
538 static time_t last_time = 0;
539 char *s2;
540 struct in_addr addr;
541 struct work_record *work;
542 char outbuf[1024];
543 char *p;
544 unstring myname;
545 TALLOC_CTX *frame = NULL;
546 const struct loadparm_substitution *lp_sub =
547 loadparm_s3_global_substitution();
549 if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
550 return;
552 last_time = t;
554 s = lp_remote_browse_sync(talloc_tos(), lp_sub);
555 if (!*s)
556 return;
559 * We only do this if we are the local master browser
560 * for our workgroup on the firsst subnet.
563 if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL) {
564 DBG_WARNING("browse_sync_remote: Cannot find workgroup %s on subnet %s\n",
565 lp_workgroup(), FIRST_SUBNET->subnet_name );
566 return;
569 if(!AM_LOCAL_MASTER_BROWSER(work)) {
570 DBG_NOTICE("browse_sync_remote: We can only do this if we are a local master browser \
571 for workgroup %s on subnet %s.\n", lp_workgroup(), FIRST_SUBNET->subnet_name );
572 return;
575 memset(outbuf,'\0',sizeof(outbuf));
576 p = outbuf;
577 SCVAL(p,0,ANN_MasterAnnouncement);
578 p++;
580 unstrcpy(myname, lp_netbios_name());
581 if (!strupper_m(myname)) {
582 DBG_WARNING("strupper_m %s failed\n", myname);
583 return;
585 myname[15]='\0';
586 push_ascii(p, myname, sizeof(outbuf)-PTR_DIFF(p,outbuf)-1, STR_TERMINATE);
588 p = skip_string(outbuf,sizeof(outbuf),p);
590 frame = talloc_stackframe();
591 for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) {
592 /* The entries are of the form a.b.c.d */
593 addr = interpret_addr2(s2);
595 DBG_INFO("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
596 lp_netbios_name(), inet_ntoa(addr) );
598 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
599 lp_netbios_name(), 0x0, "*", 0x0, addr, FIRST_SUBNET->myip, DGRAM_PORT);
601 TALLOC_FREE(frame);