This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[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 2 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, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24    
25 */
26
27 #include "includes.h"
28
29 extern int  updatecount;
30 extern BOOL found_lm_clients;
31
32 /****************************************************************************
33  Send a browser reset packet.
34 **************************************************************************/
35
36 void send_browser_reset(int reset_type, const char *to_name, int to_type, struct in_addr to_ip)
37 {
38   pstring outbuf;
39   char *p;
40
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) ));
43
44   memset(outbuf,'\0',sizeof(outbuf));
45   p = outbuf;
46   SCVAL(p,0,ANN_ResetBrowserState);
47   p++;
48   SCVAL(p,0,reset_type);
49   p++;
50
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);
54 }
55
56 /****************************************************************************
57   Broadcast a packet to the local net requesting that all servers in this
58   workgroup announce themselves to us.
59   **************************************************************************/
60
61 void broadcast_announce_request(struct subnet_record *subrec, struct work_record *work)
62 {
63   pstring outbuf;
64   char *p;
65
66   work->needannounce = True;
67
68   DEBUG(3,("broadcast_announce_request: sending announce request for workgroup %s \
69 to subnet %s\n", work->work_group, subrec->subnet_name));
70
71   memset(outbuf,'\0',sizeof(outbuf));
72   p = outbuf;
73   SCVAL(p,0,ANN_AnnouncementRequest);
74   p++;
75
76   SCVAL(p,0,work->token); /* (local) Unique workgroup token id. */
77   p++;
78   p +=  push_string(NULL, p+1, global_myname(), 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
79   
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);
83 }
84
85 /****************************************************************************
86   Broadcast an announcement.
87   **************************************************************************/
88
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)
93 {
94   pstring outbuf;
95   char *p;
96
97   memset(outbuf,'\0',sizeof(outbuf));
98   p = outbuf+1;
99
100   SCVAL(outbuf,0,announce_type);
101
102   /* Announcement parameters. */
103   SCVAL(p,0,updatecount);
104   SIVAL(p,1,announce_interval*1000); /* Milliseconds - despite the spec. */
105
106   push_string(NULL, p+5, server_name, 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
107
108   SCVAL(p,21,lp_major_announce_version()); /* Major version. */
109   SCVAL(p,22,lp_minor_announce_version()); /* Minor version. */
110
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. */
115
116   p += 31 + push_string(NULL, p+31, server_comment, -1, STR_ASCII|STR_TERMINATE);
117
118   send_mailslot(False,BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
119                 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
120                 DGRAM_PORT);
121 }
122
123 /****************************************************************************
124   Broadcast a LanMan announcement.
125 **************************************************************************/
126
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)
131 {
132   pstring outbuf;
133   char *p=outbuf;
134
135   memset(outbuf,'\0',sizeof(outbuf));
136
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. */
142
143   p += 10;
144   /*StrnCpy(p,server_name,15);
145   strupper(p);
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);
151
152   send_mailslot(False,LANMAN_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
153                 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
154                 DGRAM_PORT);
155 }
156
157 /****************************************************************************
158  We are a local master browser. Announce this to WORKGROUP<1e>.
159 ****************************************************************************/
160
161 static void send_local_master_announcement(struct subnet_record *subrec, struct work_record *work,
162                                            struct server_record *servrec)
163 {
164   /* Ensure we don't have the prohibited bit set. */
165   uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
166
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));
169
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);
178 }
179
180 /****************************************************************************
181  Announce the workgroup WORKGROUP to MSBROWSE<01>.
182 ****************************************************************************/
183
184 static void send_workgroup_announcement(struct subnet_record *subrec, struct work_record *work)
185 {
186   DEBUG(3,("send_workgroup_announcement: on subnet %s for workgroup %s\n",
187             subrec->subnet_name, work->work_group));
188
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. */
197 }
198
199 /****************************************************************************
200  Announce the given host to WORKGROUP<1d>.
201 ****************************************************************************/
202
203 static void send_host_announcement(struct subnet_record *subrec, struct work_record *work,
204                                    struct server_record *servrec)
205 {
206   /* Ensure we don't have the prohibited bits set. */
207   uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
208
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));
211
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);
220 }
221
222 /****************************************************************************
223  Announce the given LanMan host
224 ****************************************************************************/
225
226 static void send_lm_host_announcement(struct subnet_record *subrec, struct work_record *work,
227                                    struct server_record *servrec, int lm_interval)
228 {
229   /* Ensure we don't have the prohibited bits set. */
230   uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
231
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));
234
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);
243 }
244
245 /****************************************************************************
246   Announce a server record.
247   ****************************************************************************/
248
249 static void announce_server(struct subnet_record *subrec, struct work_record *work,
250                      struct server_record *servrec)
251 {
252   /* Only do domain announcements if we are a master and it's
253      our primary name we're being asked to announce. */
254
255   if (AM_LOCAL_MASTER_BROWSER(work) && strequal(global_myname(),servrec->serv.name))
256   {
257     send_local_master_announcement(subrec, work, servrec);
258     send_workgroup_announcement(subrec, work);
259   }
260   else
261   {
262     send_host_announcement(subrec, work, servrec);
263   }
264 }
265
266 /****************************************************************************
267   Go through all my registered names on all broadcast subnets and announce
268   them if the timeout requires it.
269   **************************************************************************/
270
271 void announce_my_server_names(time_t t)
272 {
273   struct subnet_record *subrec;
274
275   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
276   {
277     struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
278
279     if(work)
280     {
281       struct server_record *servrec;
282
283       if (work->needannounce)
284       {
285         /* Drop back to a max 3 minute announce. This is to prevent a
286            single lost packet from breaking things for too long. */
287
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;
292       }
293
294       /* Announce every minute at first then progress to every 12 mins */
295       if ((t - work->lastannounce_time) < work->announce_interval)
296         continue;
297
298       if (work->announce_interval < (CHECK_TIME_MAX_HOST_ANNCE * 60))
299         work->announce_interval += 60;
300
301       work->lastannounce_time = t;
302
303       for (servrec = work->serverlist; servrec; servrec = servrec->next)
304       {
305         if (is_myname(servrec->serv.name))
306           announce_server(subrec, work, servrec);
307       }
308     } /* if work */
309   } /* for subrec */
310 }
311
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 **************************************************************************/
316
317 void announce_my_lm_server_names(time_t t)
318 {
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();
323
324   if ((announce_interval <= 0) || (lm_announce <= 0))
325   {
326     /* user absolutely does not want LM announcements to be sent. */
327     return;
328   }
329
330   if ((lm_announce >= 2) && (!found_lm_clients))
331   {
332     /* has been set to 2 (Auto) but no LM clients detected (yet). */
333     return;
334   }
335
336   /* Otherwise: must have been set to 1 (Yes), or LM clients *have*
337      been detected. */
338
339   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
340   {
341     struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
342
343     if(work)
344     {
345       struct server_record *servrec;
346
347       if (last_lm_announce_time && ((t - last_lm_announce_time) < announce_interval ))
348         continue;
349
350       last_lm_announce_time = t;
351
352       for (servrec = work->serverlist; servrec; servrec = servrec->next)
353       {
354         if (is_myname(servrec->serv.name))
355           /* skipping equivalent of announce_server() */
356           send_lm_host_announcement(subrec, work, servrec, announce_interval);
357       }
358     } /* if work */
359   } /* for subrec */
360 }
361
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;
365
366 /****************************************************************************
367  Reset the announce_timer so that a local master browser announce will be done
368  immediately.
369  ****************************************************************************/
370
371 void reset_announce_timer(void)
372 {
373   announce_timer_last = time(NULL) - (CHECK_TIME_MST_ANNOUNCE * 60);
374 }
375
376 /****************************************************************************
377   Announce myself as a local master browser to a domain master browser.
378   **************************************************************************/
379
380 void announce_myself_to_domain_master_browser(time_t t)
381 {
382   struct subnet_record *subrec;
383   struct work_record *work;
384
385   if(!we_are_a_wins_client())
386   {
387     DEBUG(10,("announce_myself_to_domain_master_browser: no unicast subnet, ignoring.\n"));
388     return;
389   }
390
391   if (!announce_timer_last)
392     announce_timer_last = t;
393
394   if ((t-announce_timer_last) < (CHECK_TIME_MST_ANNOUNCE * 60))
395   {
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 ));
399     return;
400   }
401
402   announce_timer_last = t;
403
404   /* Look over all our broadcast subnets to see if any of them
405      has the state set as local master browser. */
406
407   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
408   {
409     for (work = subrec->workgrouplist; work; work = work->next)
410     {
411       if (AM_LOCAL_MASTER_BROWSER(work))
412       {
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));
415
416         /* Look in nmbd_browsersync.c for the rest of this code. */
417         announce_and_sync_with_domain_master_browser(subrec, work);
418       }
419     }
420   }
421 }
422
423 /****************************************************************************
424 Announce all samba's server entries as 'gone'.
425 This must *only* be called on shutdown.
426 ****************************************************************************/
427
428 void announce_my_servers_removed(void)
429 {
430   int announce_interval = lp_lm_interval();
431   int lm_announce = lp_lm_announce();
432   struct subnet_record *subrec; 
433
434   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
435   {
436     struct work_record *work;
437     for (work = subrec->workgrouplist; work; work = work->next)
438     {
439       struct server_record *servrec;
440
441       work->announce_interval = 0;
442       for (servrec = work->serverlist; servrec; servrec = servrec->next)
443       {
444         if (!is_myname(servrec->serv.name))
445           continue;
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);
450
451
452         if ((announce_interval <= 0) || (lm_announce <= 0))
453         {
454           /* user absolutely does not want LM announcements to be sent. */
455           continue;
456         }
457
458         if ((lm_announce >= 2) && (!found_lm_clients))
459         {
460           /* has been set to 2 (Auto) but no LM clients detected (yet). */
461           continue;
462         }
463
464         /* 
465          * lm announce was set or we have seen lm announcements, so do
466          * a lm announcement of host removed.
467          */
468
469         send_lm_host_announcement(subrec, work, servrec, 0);
470       }
471     }
472   }
473 }
474
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   **************************************************************************/
480
481 void announce_remote(time_t t)
482 {
483   char *s;
484   const char *ptr;
485   static time_t last_time = 0;
486   pstring s2;
487   struct in_addr addr;
488   char *comment;
489   int stype = lp_default_server_announce();
490
491   if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
492     return;
493
494   last_time = t;
495
496   s = lp_remote_announce();
497   if (!*s)
498     return;
499
500   comment = string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH);
501
502   for (ptr=s; next_token(&ptr,s2,NULL,sizeof(s2)); ) 
503   {
504     /* The entries are of the form a.b.c.d/WORKGROUP with 
505        WORKGROUP being optional */
506     const char *wgroup;
507     char *pwgroup;
508     int i;
509
510     pwgroup = strchr_m(s2,'/');
511     if (pwgroup)
512       *pwgroup++ = 0;
513     if (!pwgroup || !*pwgroup)
514       wgroup = lp_workgroup();
515     else
516       wgroup = pwgroup;
517
518     addr = *interpret_addr2(s2);
519     
520     /* Announce all our names including aliases */
521     /* Give the ip address as the address of our first
522        broadcast subnet. */
523
524     for(i=0; my_netbios_names(i); i++) 
525     {
526       const char *name = my_netbios_names(i);
527
528       DEBUG(5,("announce_remote: Doing remote announce for server %s to IP %s.\n",
529                  name, inet_ntoa(addr) ));
530
531       send_announcement(FIRST_SUBNET, ANN_HostAnnouncement,
532                     name,                      /* From nbt name. */
533                     wgroup, 0x1d,              /* To nbt name. */
534                     addr,                      /* To ip. */
535                     REMOTE_ANNOUNCE_INTERVAL,  /* Time until next announce. */
536                     name,                      /* Name to announce. */
537                     stype,                     /* Type field. */
538                     comment);
539     }
540   }
541 }
542
543 /****************************************************************************
544   Implement the 'remote browse sync' feature Andrew added.
545   These are used to put our browse lists into remote browse lists.
546   **************************************************************************/
547
548 void browse_sync_remote(time_t t)
549 {  
550   char *s;
551   const char *ptr;
552   static time_t last_time = 0; 
553   pstring s2;
554   struct in_addr addr;
555   struct work_record *work;
556   pstring outbuf;
557   char *p;
558   fstring myname;
559  
560   if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
561     return;
562    
563   last_time = t;
564
565   s = lp_remote_browse_sync();
566   if (!*s)
567     return;
568
569   /*
570    * We only do this if we are the local master browser
571    * for our workgroup on the firsst subnet.
572    */
573
574   if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL)
575   {   
576     DEBUG(0,("browse_sync_remote: Cannot find workgroup %s on subnet %s\n",
577            lp_workgroup(), FIRST_SUBNET->subnet_name ));
578     return;
579   }   
580          
581   if(!AM_LOCAL_MASTER_BROWSER(work))
582   {
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 ));
585     return;
586   } 
587
588   memset(outbuf,'\0',sizeof(outbuf));
589   p = outbuf;
590   SCVAL(p,0,ANN_MasterAnnouncement);
591   p++;
592
593   fstrcpy(myname, global_myname());
594   strupper(myname);
595   myname[15]='\0';
596   push_pstring_base(p, myname, outbuf);
597
598   p = skip_string(p,1);
599
600   for (ptr=s; next_token(&ptr,s2,NULL,sizeof(s2)); ) 
601   {
602     /* The entries are of the form a.b.c.d */
603     addr = *interpret_addr2(s2);
604
605     DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
606                  global_myname(), inet_ntoa(addr) ));
607
608     send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
609           global_myname(), 0x0, "*", 0x0, addr, FIRST_SUBNET->myip, DGRAM_PORT);
610   }
611 }