b389c02278df613abcf4a927248b3e5e44b42c08
[kai/samba.git] / source3 / nmbd / nmbd_sendannounce.c
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
7
8    SMB Version handling
9    Copyright (C) John H Terpstra 1995-1998
10    
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.
15    
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.
20    
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/>.
23    
24 */
25
26 #include "includes.h"
27 #include "../librpc/gen_ndr/svcctl.h"
28 #include "nmbd/nmbd.h"
29
30 extern int  updatecount;
31 extern bool found_lm_clients;
32
33 /****************************************************************************
34  Send a browser reset packet.
35 **************************************************************************/
36
37 void send_browser_reset(int reset_type, const char *to_name, int to_type, struct in_addr to_ip)
38 {
39         char outbuf[1024];
40         char *p;
41
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) ));
44
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++;
51
52         send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
53                 global_myname(), 0x0, to_name, to_type, to_ip, 
54                 FIRST_SUBNET->myip, DGRAM_PORT);
55 }
56
57 /****************************************************************************
58   Broadcast a packet to the local net requesting that all servers in this
59   workgroup announce themselves to us.
60   **************************************************************************/
61
62 void broadcast_announce_request(struct subnet_record *subrec, struct work_record *work)
63 {
64         char outbuf[1024];
65         char *p;
66
67         work->needannounce = True;
68
69         DEBUG(3,("broadcast_announce_request: sending announce request for workgroup %s \
70 to subnet %s\n", work->work_group, subrec->subnet_name));
71
72         memset(outbuf,'\0',sizeof(outbuf));
73         p = outbuf;
74         SCVAL(p,0,ANN_AnnouncementRequest);
75         p++;
76
77         SCVAL(p,0,work->token); /* (local) Unique workgroup token id. */
78         p++;
79         p +=  push_string_check(p+1, global_myname(), 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
80   
81         send_mailslot(False, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
82                 global_myname(), 0x0, work->work_group,0x1e, subrec->bcast_ip, 
83                 subrec->myip, DGRAM_PORT);
84 }
85
86 /****************************************************************************
87   Broadcast an announcement.
88   **************************************************************************/
89
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)
94 {
95         char outbuf[1024];
96         unstring upper_server_name;
97         char *p;
98
99         memset(outbuf,'\0',sizeof(outbuf));
100         p = outbuf+1;
101
102         SCVAL(outbuf,0,announce_type);
103
104         /* Announcement parameters. */
105         SCVAL(p,0,updatecount);
106         SIVAL(p,1,announce_interval*1000); /* Milliseconds - despite the spec. */
107
108         strlcpy(upper_server_name, server_name ? server_name : "", sizeof(upper_server_name));
109         strupper_m(upper_server_name);
110         push_string_check(p+5, upper_server_name, 16, STR_ASCII|STR_TERMINATE);
111
112         SCVAL(p,21,lp_major_announce_version()); /* Major version. */
113         SCVAL(p,22,lp_minor_announce_version()); /* Minor version. */
114
115         SIVAL(p,23,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
116         /* Browse version: got from NT/AS 4.00  - Value defined in smb.h (JHT). */
117         SSVAL(p,27,BROWSER_ELECTION_VERSION);
118         SSVAL(p,29,BROWSER_CONSTANT); /* Browse signature. */
119
120         p += 31 + push_string_check(p+31, server_comment, sizeof(outbuf) - (p + 31 - outbuf), STR_ASCII|STR_TERMINATE);
121
122         send_mailslot(False,BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
123                         from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
124                         DGRAM_PORT);
125 }
126
127 /****************************************************************************
128   Broadcast a LanMan announcement.
129 **************************************************************************/
130
131 static void send_lm_announcement(struct subnet_record *subrec, int announce_type,
132                               char *from_name, char *to_name, int to_type, struct in_addr to_ip,
133                               time_t announce_interval,
134                               char *server_name, int server_type, char *server_comment)
135 {
136         char outbuf[1024];
137         char *p=outbuf;
138
139         memset(outbuf,'\0',sizeof(outbuf));
140
141         SSVAL(p,0,announce_type);
142         SIVAL(p,2,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
143         SCVAL(p,6,lp_major_announce_version()); /* Major version. */
144         SCVAL(p,7,lp_minor_announce_version()); /* Minor version. */
145         SSVAL(p,8,announce_interval);            /* In seconds - according to spec. */
146
147         p += 10;
148         p += push_string_check(p, server_name, 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
149         p += push_string_check(p, server_comment, sizeof(outbuf)- (p - outbuf), STR_ASCII|STR_UPPER|STR_TERMINATE);
150
151         send_mailslot(False,LANMAN_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
152                 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
153                 DGRAM_PORT);
154 }
155
156 /****************************************************************************
157  We are a local master browser. Announce this to WORKGROUP<1e>.
158 ****************************************************************************/
159
160 static void send_local_master_announcement(struct subnet_record *subrec, struct work_record *work,
161                                            struct server_record *servrec)
162 {
163         /* Ensure we don't have the prohibited bit set. */
164         uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
165
166         DEBUG(3,("send_local_master_announcement: type %x for name %s on subnet %s for workgroup %s\n",
167                 type, global_myname(), subrec->subnet_name, work->work_group));
168
169         send_announcement(subrec, ANN_LocalMasterAnnouncement,
170                         global_myname(),                 /* From nbt name. */
171                         work->work_group, 0x1e,          /* To nbt name. */
172                         subrec->bcast_ip,                /* To ip. */
173                         work->announce_interval,         /* Time until next announce. */
174                         global_myname(),                 /* Name to announce. */
175                         type,                            /* Type field. */
176                         servrec->serv.comment);
177 }
178
179 /****************************************************************************
180  Announce the workgroup WORKGROUP to MSBROWSE<01>.
181 ****************************************************************************/
182
183 static void send_workgroup_announcement(struct subnet_record *subrec, struct work_record *work)
184 {
185         DEBUG(3,("send_workgroup_announcement: on subnet %s for workgroup %s\n",
186                 subrec->subnet_name, work->work_group));
187
188         send_announcement(subrec, ANN_DomainAnnouncement,
189                         global_myname(),                 /* From nbt name. */
190                         MSBROWSE, 0x1,                   /* To nbt name. */
191                         subrec->bcast_ip,                /* To ip. */
192                         work->announce_interval,         /* Time until next announce. */
193                         work->work_group,                /* Name to announce. */
194                         SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT,  /* workgroup announce flags. */
195                         global_myname());                /* From name as comment. */
196 }
197
198 /****************************************************************************
199  Announce the given host to WORKGROUP<1d>.
200 ****************************************************************************/
201
202 static void send_host_announcement(struct subnet_record *subrec, struct work_record *work,
203                                    struct server_record *servrec)
204 {
205         /* Ensure we don't have the prohibited bits set. */
206         uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
207
208         DEBUG(3,("send_host_announcement: type %x for host %s on subnet %s for workgroup %s\n",
209                 type, servrec->serv.name, subrec->subnet_name, work->work_group));
210
211         send_announcement(subrec, ANN_HostAnnouncement,
212                         servrec->serv.name,              /* From nbt name. */
213                         work->work_group, 0x1d,          /* To nbt name. */
214                         subrec->bcast_ip,                /* To ip. */
215                         work->announce_interval,         /* Time until next announce. */
216                         servrec->serv.name,              /* Name to announce. */
217                         type,                            /* Type field. */
218                         servrec->serv.comment);
219 }
220
221 /****************************************************************************
222  Announce the given LanMan host
223 ****************************************************************************/
224
225 static void send_lm_host_announcement(struct subnet_record *subrec, struct work_record *work,
226                                    struct server_record *servrec, int lm_interval)
227 {
228         /* Ensure we don't have the prohibited bits set. */
229         uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
230
231         DEBUG(3,("send_lm_host_announcement: type %x for host %s on subnet %s for workgroup %s, ttl: %d\n",
232                 type, servrec->serv.name, subrec->subnet_name, work->work_group, lm_interval));
233
234         send_lm_announcement(subrec, ANN_HostAnnouncement,
235                         servrec->serv.name,              /* From nbt name. */
236                         work->work_group, 0x00,          /* To nbt name. */
237                         subrec->bcast_ip,                /* To ip. */
238                         lm_interval,                     /* Time until next announce. */
239                         servrec->serv.name,              /* Name to announce (fstring not netbios name struct). */
240                         type,                            /* Type field. */
241                         servrec->serv.comment);
242 }
243
244 /****************************************************************************
245   Announce a server record.
246   ****************************************************************************/
247
248 static void announce_server(struct subnet_record *subrec, struct work_record *work,
249                      struct server_record *servrec)
250 {
251         /* Only do domain announcements if we are a master and it's
252                 our primary name we're being asked to announce. */
253
254         if (AM_LOCAL_MASTER_BROWSER(work) && strequal(global_myname(),servrec->serv.name)) {
255                 send_local_master_announcement(subrec, work, servrec);
256                 send_workgroup_announcement(subrec, work);
257         } else {
258                 send_host_announcement(subrec, work, servrec);
259         }
260 }
261
262 /****************************************************************************
263   Go through all my registered names on all broadcast subnets and announce
264   them if the timeout requires it.
265   **************************************************************************/
266
267 void announce_my_server_names(time_t t)
268 {
269         struct subnet_record *subrec;
270
271         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
272                 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
273
274                 if(work) {
275                         struct server_record *servrec;
276
277                         if (work->needannounce) {
278                                 /* Drop back to a max 3 minute announce. This is to prevent a
279                                         single lost packet from breaking things for too long. */
280
281                                 work->announce_interval = MIN(work->announce_interval,
282                                                         CHECK_TIME_MIN_HOST_ANNCE*60);
283                                 work->lastannounce_time = t - (work->announce_interval+1);
284                                 work->needannounce = False;
285                         }
286
287                         /* Announce every minute at first then progress to every 12 mins */
288                         if ((t - work->lastannounce_time) < work->announce_interval)
289                                 continue;
290
291                         if (work->announce_interval < (CHECK_TIME_MAX_HOST_ANNCE * 60))
292                                 work->announce_interval += 60;
293
294                         work->lastannounce_time = t;
295
296                         for (servrec = work->serverlist; servrec; servrec = servrec->next) {
297                                 if (is_myname(servrec->serv.name))
298                                         announce_server(subrec, work, servrec);
299                         }
300                 } /* if work */
301         } /* for subrec */
302 }
303
304 /****************************************************************************
305   Go through all my registered names on all broadcast subnets and announce
306   them as a LanMan server if the timeout requires it.
307 **************************************************************************/
308
309 void announce_my_lm_server_names(time_t t)
310 {
311         struct subnet_record *subrec;
312         static time_t last_lm_announce_time=0;
313         int announce_interval = lp_lm_interval();
314         int lm_announce = lp_lm_announce();
315
316         if ((announce_interval <= 0) || (lm_announce <= 0)) {
317                 /* user absolutely does not want LM announcements to be sent. */
318                 return;
319         }
320
321         if ((lm_announce >= 2) && (!found_lm_clients)) {
322                 /* has been set to 2 (Auto) but no LM clients detected (yet). */
323                 return;
324         }
325
326         /* Otherwise: must have been set to 1 (Yes), or LM clients *have*
327                 been detected. */
328
329         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
330                 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
331
332                 if(work) {
333                         struct server_record *servrec;
334
335                         if (last_lm_announce_time && ((t - last_lm_announce_time) < announce_interval ))
336                                 continue;
337
338                         last_lm_announce_time = t;
339
340                         for (servrec = work->serverlist; servrec; servrec = servrec->next) {
341                                 if (is_myname(servrec->serv.name))
342                                         /* skipping equivalent of announce_server() */
343                                         send_lm_host_announcement(subrec, work, servrec, announce_interval);
344                         }
345                 } /* if work */
346         } /* for subrec */
347 }
348
349 /* Announce timer. Moved into global static so it can be reset
350    when a machine becomes a local master browser. */
351 static time_t announce_timer_last=0;
352
353 /****************************************************************************
354  Reset the announce_timer so that a local master browser announce will be done
355  immediately.
356  ****************************************************************************/
357
358 void reset_announce_timer(void)
359 {
360         announce_timer_last = time(NULL) - (CHECK_TIME_MST_ANNOUNCE * 60);
361 }
362
363 /****************************************************************************
364   Announce myself as a local master browser to a domain master browser.
365   **************************************************************************/
366
367 void announce_myself_to_domain_master_browser(time_t t)
368 {
369         struct subnet_record *subrec;
370         struct work_record *work;
371
372         if(!we_are_a_wins_client()) {
373                 DEBUG(10,("announce_myself_to_domain_master_browser: no unicast subnet, ignoring.\n"));
374                 return;
375         }
376
377         if (!announce_timer_last)
378                 announce_timer_last = t;
379
380         if ((t-announce_timer_last) < (CHECK_TIME_MST_ANNOUNCE * 60)) {
381                 DEBUG(10,("announce_myself_to_domain_master_browser: t (%d) - last(%d) < %d\n",
382                         (int)t, (int)announce_timer_last, 
383                         CHECK_TIME_MST_ANNOUNCE * 60 ));
384                 return;
385         }
386
387         announce_timer_last = t;
388
389         /* Look over all our broadcast subnets to see if any of them
390                 has the state set as local master browser. */
391
392         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
393                 for (work = subrec->workgrouplist; work; work = work->next) {
394                         if (AM_LOCAL_MASTER_BROWSER(work)) {
395                                 DEBUG(4,( "announce_myself_to_domain_master_browser: I am a local master browser for \
396 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
397
398                                 /* Look in nmbd_browsersync.c for the rest of this code. */
399                                 announce_and_sync_with_domain_master_browser(subrec, work);
400                         }
401                 }
402         }
403 }
404
405 /****************************************************************************
406 Announce all samba's server entries as 'gone'.
407 This must *only* be called on shutdown.
408 ****************************************************************************/
409
410 void announce_my_servers_removed(void)
411 {
412         int announce_interval = lp_lm_interval();
413         int lm_announce = lp_lm_announce();
414         struct subnet_record *subrec; 
415
416         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
417                 struct work_record *work;
418                 for (work = subrec->workgrouplist; work; work = work->next) {
419                         struct server_record *servrec;
420
421                         work->announce_interval = 0;
422                         for (servrec = work->serverlist; servrec; servrec = servrec->next) {
423                                 if (!is_myname(servrec->serv.name))
424                                         continue;
425                                 servrec->serv.type = 0;
426                                 if(AM_LOCAL_MASTER_BROWSER(work))
427                                         send_local_master_announcement(subrec, work, servrec);
428                                 send_host_announcement(subrec, work, servrec);
429
430                                 if ((announce_interval <= 0) || (lm_announce <= 0)) {
431                                         /* user absolutely does not want LM announcements to be sent. */
432                                         continue;
433                                 }
434
435                                 if ((lm_announce >= 2) && (!found_lm_clients)) {
436                                         /* has been set to 2 (Auto) but no LM clients detected (yet). */
437                                         continue;
438                                 }
439
440                                 /* 
441                                  * lm announce was set or we have seen lm announcements, so do
442                                  * a lm announcement of host removed.
443                                  */
444
445                                 send_lm_host_announcement(subrec, work, servrec, 0);
446                         }
447                 }
448         }
449 }
450
451 /****************************************************************************
452   Do all the "remote" announcements. These are used to put ourselves
453   on a remote browse list. They are done blind, no checking is done to
454   see if there is actually a local master browser at the other end.
455   **************************************************************************/
456
457 void announce_remote(time_t t)
458 {
459         char *s;
460         const char *ptr;
461         static time_t last_time = 0;
462         char *s2;
463         struct in_addr addr;
464         char *comment;
465         int stype = lp_default_server_announce();
466         TALLOC_CTX *frame = NULL;
467
468         if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
469                 return;
470
471         last_time = t;
472
473         s = lp_remote_announce();
474         if (!*s)
475                 return;
476
477         comment = string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH);
478
479         frame = talloc_stackframe();
480         for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) {
481                 /* The entries are of the form a.b.c.d/WORKGROUP with
482                                 WORKGROUP being optional */
483                 const char *wgroup;
484                 char *pwgroup;
485                 int i;
486
487                 pwgroup = strchr_m(s2,'/');
488                 if (pwgroup)
489                         *pwgroup++ = 0;
490                 if (!pwgroup || !*pwgroup)
491                         wgroup = lp_workgroup();
492                 else
493                         wgroup = pwgroup;
494
495                 addr = interpret_addr2(s2);
496
497                 /* Announce all our names including aliases */
498                 /* Give the ip address as the address of our first
499                                 broadcast subnet. */
500
501                 for(i=0; my_netbios_names(i); i++) {
502                         const char *name = my_netbios_names(i);
503
504                         DEBUG(5,("announce_remote: Doing remote announce for server %s to IP %s.\n",
505                                 name, inet_ntoa(addr) ));
506
507                         send_announcement(FIRST_SUBNET, ANN_HostAnnouncement,
508                                                 name,                      /* From nbt name. */
509                                                 wgroup, 0x1d,              /* To nbt name. */
510                                                 addr,                      /* To ip. */
511                                                 REMOTE_ANNOUNCE_INTERVAL,  /* Time until next announce. */
512                                                 name,                      /* Name to announce. */
513                                                 stype,                     /* Type field. */
514                                                 comment);
515                 }
516         }
517         TALLOC_FREE(frame);
518 }
519
520 /****************************************************************************
521   Implement the 'remote browse sync' feature Andrew added.
522   These are used to put our browse lists into remote browse lists.
523 **************************************************************************/
524
525 void browse_sync_remote(time_t t)
526 {
527         char *s;
528         const char *ptr;
529         static time_t last_time = 0;
530         char *s2;
531         struct in_addr addr;
532         struct work_record *work;
533         char outbuf[1024];
534         char *p;
535         unstring myname;
536         TALLOC_CTX *frame = NULL;
537
538         if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
539                 return;
540
541         last_time = t;
542
543         s = lp_remote_browse_sync();
544         if (!*s)
545                 return;
546
547         /*
548          * We only do this if we are the local master browser
549          * for our workgroup on the firsst subnet.
550          */
551
552         if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL) {   
553                 DEBUG(0,("browse_sync_remote: Cannot find workgroup %s on subnet %s\n",
554                         lp_workgroup(), FIRST_SUBNET->subnet_name ));
555                 return;
556         }
557
558         if(!AM_LOCAL_MASTER_BROWSER(work)) {
559                 DEBUG(5,("browse_sync_remote: We can only do this if we are a local master browser \
560 for workgroup %s on subnet %s.\n", lp_workgroup(), FIRST_SUBNET->subnet_name ));
561                 return;
562         }
563
564         memset(outbuf,'\0',sizeof(outbuf));
565         p = outbuf;
566         SCVAL(p,0,ANN_MasterAnnouncement);
567         p++;
568
569         unstrcpy(myname, global_myname());
570         strupper_m(myname);
571         myname[15]='\0';
572         push_ascii(p, myname, sizeof(outbuf)-PTR_DIFF(p,outbuf)-1, STR_TERMINATE);
573
574         p = skip_string(outbuf,sizeof(outbuf),p);
575
576         frame = talloc_stackframe();
577         for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) {
578                 /* The entries are of the form a.b.c.d */
579                 addr = interpret_addr2(s2);
580
581                 DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
582                         global_myname(), inet_ntoa(addr) ));
583
584                 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
585                         global_myname(), 0x0, "*", 0x0, addr, FIRST_SUBNET->myip, DGRAM_PORT);
586         }
587         TALLOC_FREE(frame);
588 }