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