fixed error check which caused domain logons to fail
[Samba.git] / source / nmbd / nmbd_sendannounce.c
blob72becf58617ad94bd39a482fd787b31d6c99cb74
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
9 SMB Version handling
10 Copyright (C) John H Terpstra 1995-1998
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "includes.h"
30 extern pstring global_myname;
31 extern fstring global_myworkgroup;
32 extern char **my_netbios_names;
33 extern int updatecount;
34 extern BOOL found_lm_clients;
36 /****************************************************************************
37 Send a browser reset packet.
38 **************************************************************************/
40 void send_browser_reset(int reset_type, char *to_name, int to_type, struct in_addr to_ip)
42 pstring outbuf;
43 char *p;
45 DEBUG(3,("send_browser_reset: sending reset request type %d to %s<%02x> IP %s.\n",
46 reset_type, to_name, to_type, inet_ntoa(to_ip) ));
48 memset(outbuf,'\0',sizeof(outbuf));
49 p = outbuf;
50 CVAL(p,0) = ANN_ResetBrowserState;
51 p++;
52 CVAL(p,0) = reset_type;
53 p++;
55 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
56 global_myname, 0x0, to_name, to_type, to_ip,
57 FIRST_SUBNET->myip, DGRAM_PORT);
60 /****************************************************************************
61 Broadcast a packet to the local net requesting that all servers in this
62 workgroup announce themselves to us.
63 **************************************************************************/
65 void broadcast_announce_request(struct subnet_record *subrec, struct work_record *work)
67 pstring outbuf;
68 char *p;
70 work->needannounce = True;
72 DEBUG(3,("broadcast_announce_request: sending announce request for workgroup %s \
73 to subnet %s\n", work->work_group, subrec->subnet_name));
75 memset(outbuf,'\0',sizeof(outbuf));
76 p = outbuf;
77 CVAL(p,0) = ANN_AnnouncementRequest;
78 p++;
80 CVAL(p,0) = work->token; /* (local) Unique workgroup token id. */
81 p++;
82 StrnCpy(p,global_myname,15);
83 strupper(p);
84 p = skip_string(p,1);
86 send_mailslot(False, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
87 global_myname, 0x0, work->work_group,0x1e, subrec->bcast_ip,
88 subrec->myip, DGRAM_PORT);
91 /****************************************************************************
92 Broadcast an announcement.
93 **************************************************************************/
95 static void send_announcement(struct subnet_record *subrec, int announce_type,
96 char *from_name, char *to_name, int to_type, struct in_addr to_ip,
97 time_t announce_interval,
98 char *server_name, int server_type, char *server_comment)
100 pstring outbuf;
101 char *p;
103 memset(outbuf,'\0',sizeof(outbuf));
104 p = outbuf+1;
106 CVAL(outbuf,0) = announce_type;
108 /* Announcement parameters. */
109 CVAL(p,0) = updatecount;
110 SIVAL(p,1,announce_interval*1000); /* Milliseconds - despite the spec. */
112 StrnCpy(p+5,server_name,15);
113 strupper(p+5);
115 CVAL(p,21) = lp_major_announce_version(); /* Major version. */
116 CVAL(p,22) = lp_minor_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 pstrcpy(p+31,server_comment);
124 p += 31;
125 p = skip_string(p,1);
127 send_mailslot(False,BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
128 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
129 DGRAM_PORT);
132 /****************************************************************************
133 Broadcast a LanMan announcement.
134 **************************************************************************/
136 static void send_lm_announcement(struct subnet_record *subrec, int announce_type,
137 char *from_name, char *to_name, int to_type, struct in_addr to_ip,
138 time_t announce_interval,
139 char *server_name, int server_type, char *server_comment)
141 pstring outbuf;
142 char *p=outbuf;
144 memset(outbuf,'\0',sizeof(outbuf));
146 SSVAL(p,0,announce_type);
147 SIVAL(p,2,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
148 CVAL(p,6) = lp_major_announce_version(); /* Major version. */
149 CVAL(p,7) = lp_minor_announce_version(); /* Minor version. */
150 SSVAL(p,8,announce_interval); /* In seconds - according to spec. */
152 p += 10;
153 StrnCpy(p,server_name,15);
154 strupper(p);
155 p = skip_string(p,1);
156 pstrcpy(p,server_comment);
157 p = skip_string(p,1);
159 send_mailslot(False,LANMAN_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
160 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
161 DGRAM_PORT);
164 /****************************************************************************
165 We are a local master browser. Announce this to WORKGROUP<1e>.
166 ****************************************************************************/
168 static void send_local_master_announcement(struct subnet_record *subrec, struct work_record *work,
169 struct server_record *servrec)
171 /* Ensure we don't have the prohibited bit set. */
172 uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
174 DEBUG(3,("send_local_master_announcement: type %x for name %s on subnet %s for workgroup %s\n",
175 type, global_myname, subrec->subnet_name, work->work_group));
177 send_announcement(subrec, ANN_LocalMasterAnnouncement,
178 global_myname, /* From nbt name. */
179 work->work_group, 0x1e, /* To nbt name. */
180 subrec->bcast_ip, /* To ip. */
181 work->announce_interval, /* Time until next announce. */
182 global_myname, /* Name to announce. */
183 type, /* Type field. */
184 servrec->serv.comment);
187 /****************************************************************************
188 Announce the workgroup WORKGROUP to MSBROWSE<01>.
189 ****************************************************************************/
191 static void send_workgroup_announcement(struct subnet_record *subrec, struct work_record *work)
193 DEBUG(3,("send_workgroup_announcement: on subnet %s for workgroup %s\n",
194 subrec->subnet_name, work->work_group));
196 send_announcement(subrec, ANN_DomainAnnouncement,
197 global_myname, /* From nbt name. */
198 MSBROWSE, 0x1, /* To nbt name. */
199 subrec->bcast_ip, /* To ip. */
200 work->announce_interval, /* Time until next announce. */
201 work->work_group, /* Name to announce. */
202 SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT, /* workgroup announce flags. */
203 global_myname); /* From name as comment. */
206 /****************************************************************************
207 Announce the given host to WORKGROUP<1d>.
208 ****************************************************************************/
210 static void send_host_announcement(struct subnet_record *subrec, struct work_record *work,
211 struct server_record *servrec)
213 /* Ensure we don't have the prohibited bits set. */
214 uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
216 DEBUG(3,("send_host_announcement: type %x for host %s on subnet %s for workgroup %s\n",
217 type, servrec->serv.name, subrec->subnet_name, work->work_group));
219 send_announcement(subrec, ANN_HostAnnouncement,
220 servrec->serv.name, /* From nbt name. */
221 work->work_group, 0x1d, /* To nbt name. */
222 subrec->bcast_ip, /* To ip. */
223 work->announce_interval, /* Time until next announce. */
224 servrec->serv.name, /* Name to announce. */
225 type, /* Type field. */
226 servrec->serv.comment);
229 /****************************************************************************
230 Announce the given LanMan host
231 ****************************************************************************/
233 static void send_lm_host_announcement(struct subnet_record *subrec, struct work_record *work,
234 struct server_record *servrec, int lm_interval)
236 /* Ensure we don't have the prohibited bits set. */
237 uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
239 DEBUG(3,("send_lm_host_announcement: type %x for host %s on subnet %s for workgroup %s, ttl: %d\n",
240 type, servrec->serv.name, subrec->subnet_name, work->work_group, lm_interval));
242 send_lm_announcement(subrec, ANN_HostAnnouncement,
243 servrec->serv.name, /* From nbt name. */
244 work->work_group, 0x00, /* To nbt name. */
245 subrec->bcast_ip, /* To ip. */
246 lm_interval, /* Time until next announce. */
247 servrec->serv.name, /* Name to announce. */
248 type, /* Type field. */
249 servrec->serv.comment);
252 /****************************************************************************
253 Announce a server record.
254 ****************************************************************************/
256 static void announce_server(struct subnet_record *subrec, struct work_record *work,
257 struct server_record *servrec)
259 /* Only do domain announcements if we are a master and it's
260 our primary name we're being asked to announce. */
262 if (AM_LOCAL_MASTER_BROWSER(work) && strequal(global_myname,servrec->serv.name))
264 send_local_master_announcement(subrec, work, servrec);
265 send_workgroup_announcement(subrec, work);
267 else
269 send_host_announcement(subrec, work, servrec);
273 /****************************************************************************
274 Go through all my registered names on all broadcast subnets and announce
275 them if the timeout requires it.
276 **************************************************************************/
278 void announce_my_server_names(time_t t)
280 struct subnet_record *subrec;
282 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
284 struct work_record *work = find_workgroup_on_subnet(subrec, global_myworkgroup);
286 if(work)
288 struct server_record *servrec;
290 if (work->needannounce)
292 /* Drop back to a max 3 minute announce. This is to prevent a
293 single lost packet from breaking things for too long. */
295 work->announce_interval = MIN(work->announce_interval,
296 CHECK_TIME_MIN_HOST_ANNCE*60);
297 work->lastannounce_time = t - (work->announce_interval+1);
298 work->needannounce = False;
301 /* Announce every minute at first then progress to every 12 mins */
302 if ((t - work->lastannounce_time) < work->announce_interval)
303 continue;
305 if (work->announce_interval < (CHECK_TIME_MAX_HOST_ANNCE * 60))
306 work->announce_interval += 60;
308 work->lastannounce_time = t;
310 for (servrec = work->serverlist; servrec; servrec = servrec->next)
312 if (is_myname(servrec->serv.name))
313 announce_server(subrec, work, servrec);
315 } /* if work */
316 } /* for subrec */
319 /****************************************************************************
320 Go through all my registered names on all broadcast subnets and announce
321 them as a LanMan server if the timeout requires it.
322 **************************************************************************/
324 void announce_my_lm_server_names(time_t t)
326 struct subnet_record *subrec;
327 static time_t last_lm_announce_time=0;
328 int announce_interval = lp_lm_interval();
329 int lm_announce = lp_lm_announce();
331 if ((announce_interval <= 0) || (lm_announce <= 0))
333 /* user absolutely does not want LM announcements to be sent. */
334 return;
337 if ((lm_announce >= 2) && (!found_lm_clients))
339 /* has been set to 2 (Auto) but no LM clients detected (yet). */
340 return;
343 /* Otherwise: must have been set to 1 (Yes), or LM clients *have*
344 been detected. */
346 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
348 struct work_record *work = find_workgroup_on_subnet(subrec, global_myworkgroup);
350 if(work)
352 struct server_record *servrec;
354 if (last_lm_announce_time && ((t - last_lm_announce_time) < announce_interval ))
355 continue;
357 last_lm_announce_time = t;
359 for (servrec = work->serverlist; servrec; servrec = servrec->next)
361 if (is_myname(servrec->serv.name))
362 /* skipping equivalent of announce_server() */
363 send_lm_host_announcement(subrec, work, servrec, announce_interval);
365 } /* if work */
366 } /* for subrec */
369 /* Announce timer. Moved into global static so it can be reset
370 when a machine becomes a local master browser. */
371 static time_t announce_timer_last=0;
373 /****************************************************************************
374 Reset the announce_timer so that a local master browser announce will be done
375 immediately.
376 ****************************************************************************/
378 void reset_announce_timer(void)
380 announce_timer_last = time(NULL) - (CHECK_TIME_MST_ANNOUNCE * 60);
383 /****************************************************************************
384 Announce myself as a local master browser to a domain master browser.
385 **************************************************************************/
387 void announce_myself_to_domain_master_browser(time_t t)
389 struct subnet_record *subrec;
390 struct work_record *work;
392 if(!we_are_a_wins_client())
394 DEBUG(10,("announce_myself_to_domain_master_browser: no unicast subnet, ignoring.\n"));
395 return;
398 if (!announce_timer_last)
399 announce_timer_last = t;
401 if ((t-announce_timer_last) < (CHECK_TIME_MST_ANNOUNCE * 60))
403 DEBUG(10,("announce_myself_to_domain_master_browser: t (%d) - last(%d) < %d\n",
404 (int)t, (int)announce_timer_last,
405 CHECK_TIME_MST_ANNOUNCE * 60 ));
406 return;
409 announce_timer_last = t;
411 /* Look over all our broadcast subnets to see if any of them
412 has the state set as local master browser. */
414 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
416 for (work = subrec->workgrouplist; work; work = work->next)
418 if (AM_LOCAL_MASTER_BROWSER(work))
420 DEBUG(4,( "announce_myself_to_domain_master_browser: I am a local master browser for \
421 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
423 /* Look in nmbd_browsersync.c for the rest of this code. */
424 announce_and_sync_with_domain_master_browser(subrec, work);
430 /****************************************************************************
431 Announce all samba's server entries as 'gone'.
432 This must *only* be called on shutdown.
433 ****************************************************************************/
435 void announce_my_servers_removed(void)
437 int announce_interval = lp_lm_interval();
438 int lm_announce = lp_lm_announce();
439 struct subnet_record *subrec;
441 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
443 struct work_record *work;
444 for (work = subrec->workgrouplist; work; work = work->next)
446 struct server_record *servrec;
448 work->announce_interval = 0;
449 for (servrec = work->serverlist; servrec; servrec = servrec->next)
451 if (!is_myname(servrec->serv.name))
452 continue;
453 servrec->serv.type = 0;
454 if(AM_LOCAL_MASTER_BROWSER(work))
455 send_local_master_announcement(subrec, work, servrec);
456 send_host_announcement(subrec, work, servrec);
459 if ((announce_interval <= 0) || (lm_announce <= 0))
461 /* user absolutely does not want LM announcements to be sent. */
462 continue;
465 if ((lm_announce >= 2) && (!found_lm_clients))
467 /* has been set to 2 (Auto) but no LM clients detected (yet). */
468 continue;
472 * lm announce was set or we have seen lm announcements, so do
473 * a lm announcement of host removed.
476 send_lm_host_announcement(subrec, work, servrec, 0);
482 /****************************************************************************
483 Do all the "remote" announcements. These are used to put ourselves
484 on a remote browse list. They are done blind, no checking is done to
485 see if there is actually a local master browser at the other end.
486 **************************************************************************/
488 void announce_remote(time_t t)
490 char *s,*ptr;
491 static time_t last_time = 0;
492 pstring s2;
493 struct in_addr addr;
494 char *comment;
495 int stype = lp_default_server_announce();
497 if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
498 return;
500 last_time = t;
502 s = lp_remote_announce();
503 if (!*s)
504 return;
506 comment = string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH);
508 for (ptr=s; next_token(&ptr,s2,NULL,sizeof(s2)); )
510 /* The entries are of the form a.b.c.d/WORKGROUP with
511 WORKGROUP being optional */
512 char *wgroup;
513 int i;
515 wgroup = strchr(s2,'/');
516 if (wgroup)
517 *wgroup++ = 0;
518 if (!wgroup || !*wgroup)
519 wgroup = global_myworkgroup;
521 addr = *interpret_addr2(s2);
523 /* Announce all our names including aliases */
524 /* Give the ip address as the address of our first
525 broadcast subnet. */
527 for(i=0; my_netbios_names[i]; i++)
529 char *name = my_netbios_names[i];
531 DEBUG(5,("announce_remote: Doing remote announce for server %s to IP %s.\n",
532 name, inet_ntoa(addr) ));
534 send_announcement(FIRST_SUBNET, ANN_HostAnnouncement,
535 name, /* From nbt name. */
536 wgroup, 0x1d, /* To nbt name. */
537 addr, /* To ip. */
538 REMOTE_ANNOUNCE_INTERVAL, /* Time until next announce. */
539 name, /* Name to announce. */
540 stype, /* Type field. */
541 comment);
546 /****************************************************************************
547 Implement the 'remote browse sync' feature Andrew added.
548 These are used to put our browse lists into remote browse lists.
549 **************************************************************************/
551 void browse_sync_remote(time_t t)
553 char *s,*ptr;
554 static time_t last_time = 0;
555 pstring s2;
556 struct in_addr addr;
557 struct work_record *work;
558 pstring outbuf;
559 char *p;
561 if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
562 return;
564 last_time = t;
566 s = lp_remote_browse_sync();
567 if (!*s)
568 return;
571 * We only do this if we are the local master browser
572 * for our workgroup on the firsst subnet.
575 if((work = find_workgroup_on_subnet(FIRST_SUBNET, global_myworkgroup)) == NULL)
577 DEBUG(0,("browse_sync_remote: Cannot find workgroup %s on subnet %s\n",
578 global_myworkgroup, FIRST_SUBNET->subnet_name ));
579 return;
582 if(!AM_LOCAL_MASTER_BROWSER(work))
584 DEBUG(5,("browse_sync_remote: We can only do this if we are a local master browser \
585 for workgroup %s on subnet %s.\n", global_myworkgroup, FIRST_SUBNET->subnet_name ));
586 return;
589 memset(outbuf,'\0',sizeof(outbuf));
590 p = outbuf;
591 CVAL(p,0) = ANN_MasterAnnouncement;
592 p++;
594 StrnCpy(p,global_myname,15);
595 strupper(p);
596 p = skip_string(p,1);
598 for (ptr=s; next_token(&ptr,s2,NULL,sizeof(s2)); )
600 /* The entries are of the form a.b.c.d */
601 addr = *interpret_addr2(s2);
603 DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
604 global_myname, inet_ntoa(addr) ));
606 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
607 global_myname, 0x0, "*", 0x0, addr, FIRST_SUBNET->myip, DGRAM_PORT);