Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[metze/samba/wip.git] / source3 / nmbd / nmbd_incomingdgrams.c
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
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23 */
24
25 #include "includes.h"
26
27 extern pstring global_myname;
28 extern fstring global_myworkgroup;
29 extern BOOL found_lm_clients;
30
31 #if 0
32
33 /* XXXX note: This function is currently unsuitable for use, as it
34    does not properly check that a server is in a fit state to become
35    a backup browser before asking it to be one.
36    The code is left here to be worked on at a later date.
37 */
38
39 /****************************************************************************
40 Tell a server to become a backup browser
41 **************************************************************************/
42
43 void tell_become_backup(void)
44 {
45   struct subnet_record *subrec;
46   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
47   {
48     struct work_record *work;
49     for (work = subrec->workgrouplist; work; work = work->next)
50     {
51       struct server_record *servrec;
52       int num_servers = 0;
53       int num_backups = 0;
54           
55       for (servrec = work->serverlist; servrec; servrec = servrec->next)
56       {
57         num_servers++;
58               
59         if (is_myname(servrec->serv.name))
60           continue;
61               
62         if (servrec->serv.type & SV_TYPE_BACKUP_BROWSER) 
63         {
64           num_backups++;
65           continue;
66         }
67               
68         if (servrec->serv.type & SV_TYPE_MASTER_BROWSER)
69           continue;
70               
71         if (!(servrec->serv.type & SV_TYPE_POTENTIAL_BROWSER))
72           continue;
73               
74         DEBUG(3,("num servers: %d num backups: %d\n", 
75               num_servers, num_backups));
76               
77         /* make first server a backup server. thereafter make every
78            tenth server a backup server */
79         if (num_backups != 0 && (num_servers+9) / num_backups > 10)
80           continue;
81               
82         DEBUG(2,("sending become backup to %s %s for %s\n",
83              servrec->serv.name, inet_ntoa(subrec->bcast_ip),
84              work->work_group));
85               
86         /* type 11 request from MYNAME(20) to WG(1e) for SERVER */
87         do_announce_request(servrec->serv.name, work->work_group,
88               ANN_BecomeBackup, 0x20, 0x1e, subrec->bcast_ip);
89       }
90     }
91   }
92 }
93 #endif
94
95 /*******************************************************************
96   Process an incoming host announcement packet.
97 *******************************************************************/
98
99 void process_host_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
100 {
101   struct dgram_packet *dgram = &p->packet.dgram;
102   int ttl = IVAL(buf,1)/1000;
103   char *announce_name = buf+5;
104   uint32 servertype = IVAL(buf,23);
105   char *comment = buf+31;
106   struct work_record *work;
107   struct server_record *servrec;
108   char *work_name;
109   char *source_name = dgram->source_name.name;
110
111   START_PROFILE(host_announce);
112   comment[43] = 0;
113   
114   DEBUG(3,("process_host_announce: from %s<%02x> IP %s to \
115 %s for server %s.\n", source_name, source_name[15], inet_ntoa(p->ip),
116               nmb_namestr(&dgram->dest_name),announce_name));
117
118   DEBUG(5,("process_host_announce: ttl=%d server type=%08x comment=%s\n",
119            ttl, servertype,comment));
120
121   /* Filter servertype to remove impossible bits. */
122   servertype &= ~(SV_TYPE_LOCAL_LIST_ONLY|SV_TYPE_DOMAIN_ENUM);
123
124   /* A host announcement must be sent to the name WORKGROUP<1d>. */
125   if(dgram->dest_name.name_type != 0x1d)
126   {
127     DEBUG(2,("process_host_announce: incorrect name type for destination from IP %s \
128 (was %02x) should be 0x1d. Allowing packet anyway.\n",
129               inet_ntoa(p->ip), dgram->dest_name.name_type));
130     /* Change it so it was. */
131     dgram->dest_name.name_type = 0x1d;
132   }
133
134   /* For a host announce the workgroup name is the destination name. */
135   work_name = dgram->dest_name.name;
136
137   /*
138    * Syntax servers version 5.1 send HostAnnounce packets to
139    * *THE WRONG NAME*. They send to LOCAL_MASTER_BROWSER_NAME<00>
140    * instead of WORKGROUP<1d> name. So to fix this we check if
141    * the workgroup name is our own name, and if so change it
142    * to be our primary workgroup name.
143    */
144
145   if(strequal(work_name, global_myname))
146     work_name = global_myworkgroup;
147
148   /*
149    * We are being very agressive here in adding a workgroup
150    * name on the basis of a host announcing itself as being
151    * in that workgroup. Maybe we should wait for the workgroup
152    * announce instead ? JRA.
153    */
154
155   work = find_workgroup_on_subnet(subrec, work_name);
156
157   if(servertype != 0)
158   {
159     if (work ==NULL )
160     {
161       /* We have no record of this workgroup. Add it. */
162       if((work = create_workgroup_on_subnet(subrec, work_name, ttl))==NULL)
163         goto done;
164     }
165   
166     if((servrec = find_server_in_workgroup( work, announce_name))==NULL)
167     {
168       /* If this server is not already in the workgroup, add it. */
169       create_server_on_workgroup(work, announce_name, 
170                                  servertype|SV_TYPE_LOCAL_LIST_ONLY, 
171                                  ttl, comment);
172     }
173     else
174     {
175       /* Update the record. */
176       servrec->serv.type = servertype|SV_TYPE_LOCAL_LIST_ONLY;
177       update_server_ttl( servrec, ttl);
178       StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
179     }
180   }
181   else
182   {
183     /*
184      * This server is announcing it is going down. Remove it from the 
185      * workgroup.
186      */
187     if(!is_myname(announce_name) && (work != NULL) &&
188        ((servrec = find_server_in_workgroup( work, announce_name))!=NULL)
189       )
190     {
191       remove_server_from_workgroup( work, servrec);
192     }
193   }
194   subrec->work_changed = True;
195 done:
196   END_PROFILE(host_announce);
197 }
198
199 /*******************************************************************
200   Process an incoming WORKGROUP announcement packet.
201 *******************************************************************/
202
203 void process_workgroup_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
204 {
205   struct dgram_packet *dgram = &p->packet.dgram;
206   int ttl = IVAL(buf,1)/1000;
207   char *workgroup_announce_name = buf+5;
208   uint32 servertype = IVAL(buf,23);
209   char *master_name = buf+31;
210   struct work_record *work;
211   char *source_name = dgram->source_name.name;
212
213   START_PROFILE(workgroup_announce);
214   master_name[43] = 0;
215
216   DEBUG(3,("process_workgroup_announce: from %s<%02x> IP %s to \
217 %s for workgroup %s.\n", source_name, source_name[15], inet_ntoa(p->ip),
218               nmb_namestr(&dgram->dest_name),workgroup_announce_name));
219
220   DEBUG(5,("process_workgroup_announce: ttl=%d server type=%08x master browser=%s\n",
221            ttl, servertype, master_name));
222
223   /* Workgroup announcements must only go to the MSBROWSE name. */
224   if (!strequal(dgram->dest_name.name, MSBROWSE) || (dgram->dest_name.name_type != 0x1))
225   {
226     DEBUG(0,("process_workgroup_announce: from IP %s should be to __MSBROWSE__<0x01> not %s\n",
227               inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
228     goto done;
229   }
230
231   if ((work = find_workgroup_on_subnet(subrec, workgroup_announce_name))==NULL)
232   {
233     /* We have no record of this workgroup. Add it. */
234     if((work = create_workgroup_on_subnet(subrec, workgroup_announce_name, ttl))==NULL)
235       goto done;
236   }
237   else
238   {
239     /* Update the workgroup death_time. */
240     update_workgroup_ttl(work, ttl);
241   }
242
243   if(*work->local_master_browser_name == '\0')
244   {
245     /* Set the master browser name. */
246     set_workgroup_local_master_browser_name( work, master_name );
247   }
248
249   subrec->work_changed = True;
250 done:
251   END_PROFILE(workgroup_announce);
252 }
253
254 /*******************************************************************
255   Process an incoming local master browser announcement packet.
256 *******************************************************************/
257
258 void process_local_master_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
259 {
260   struct dgram_packet *dgram = &p->packet.dgram;
261   int ttl = IVAL(buf,1)/1000;
262   char *server_name = buf+5;
263   uint32 servertype = IVAL(buf,23);
264   char *comment = buf+31;
265   char *work_name;
266   struct work_record *work;
267   struct server_record *servrec;
268   char *source_name = dgram->source_name.name;
269
270   START_PROFILE(local_master_announce);
271   comment[43] = 0;
272
273   DEBUG(3,("process_local_master_announce: from %s<%02x> IP %s to \
274 %s for server %s.\n", source_name, source_name[15], inet_ntoa(p->ip),
275               nmb_namestr(&dgram->dest_name),server_name));
276
277   DEBUG(5,("process_local_master_announce: ttl=%d server type=%08x comment=%s\n",
278            ttl, servertype, comment));
279
280   /* A local master announcement must be sent to the name WORKGROUP<1e>. */
281   if(dgram->dest_name.name_type != 0x1e)
282   {
283     DEBUG(0,("process_local_master_announce: incorrect name type for destination from IP %s \
284 (was %02x) should be 0x1e. Ignoring packet.\n",
285               inet_ntoa(p->ip), dgram->dest_name.name_type));
286     goto done;
287   }
288
289   /* Filter servertype to remove impossible bits. */
290   servertype &= ~(SV_TYPE_LOCAL_LIST_ONLY|SV_TYPE_DOMAIN_ENUM);
291
292   /* For a local master announce the workgroup name is the destination name. */
293   work_name = dgram->dest_name.name;
294
295   if ((work = find_workgroup_on_subnet(subrec, work_name))==NULL)
296   {
297     /* Don't bother adding if it's a local master release announce. */
298     if(servertype == 0)
299       goto done;
300
301     /* We have no record of this workgroup. Add it. */
302     if((work = create_workgroup_on_subnet(subrec, work_name, ttl))==NULL)
303       goto done;
304   }
305
306   /* If we think we're the local master browser for this workgroup,
307      we should never have got this packet. We don't see our own
308      packets.
309    */
310   if(AM_LOCAL_MASTER_BROWSER(work))
311   {
312     DEBUG(0,("process_local_master_announce: Server %s at IP %s is announcing itself as \
313 a local master browser for workgroup %s and we think we are master. Forcing election.\n",
314       server_name, inet_ntoa(p->ip), work_name));
315
316     /* Samba nmbd versions 1.9.17 to 1.9.17p4 have a bug in that when
317        they have become a local master browser once, they will never
318        stop sending local master announcements. To fix this we send
319        them a reset browser packet, with level 0x2 on the __SAMBA__
320        name that only they should be listening to. */
321    
322     send_browser_reset( 0x2, "__SAMBA__" , 0x20, p->ip);
323
324     /* We should demote ourself and force an election. */
325
326     unbecome_local_master_browser( subrec, work, True);
327
328     /* The actual election requests are handled in
329        nmbd_election.c */
330     goto done;
331   }  
332
333   /* Find the server record on this workgroup. If it doesn't exist, add it. */
334
335   if(servertype != 0)
336   {
337     if((servrec = find_server_in_workgroup( work, server_name))==NULL)
338     {
339       /* If this server is not already in the workgroup, add it. */
340       create_server_on_workgroup(work, server_name, 
341                                  servertype|SV_TYPE_LOCAL_LIST_ONLY, 
342                                  ttl, comment);
343     }
344     else
345     {
346       /* Update the record. */
347       servrec->serv.type = servertype|SV_TYPE_LOCAL_LIST_ONLY;
348       update_server_ttl(servrec, ttl);
349       StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
350     }
351
352     set_workgroup_local_master_browser_name( work, server_name );
353   }
354   else
355   {
356     /*
357      * This server is announcing it is going down. Remove it from the
358      * workgroup.
359      */
360     if(!is_myname(server_name) && (work != NULL) &&
361        ((servrec = find_server_in_workgroup( work, server_name))!=NULL)
362       )
363     {
364       remove_server_from_workgroup( work, servrec);
365     }
366   }
367
368   subrec->work_changed = True;
369 done:
370   END_PROFILE(local_master_announce);
371 }
372
373 /*******************************************************************
374   Process a domain master announcement frame.
375   Domain master browsers receive these from local masters. The Domain
376   master should then issue a sync with the local master, asking for
377   that machines local server list.
378 ******************************************************************/
379
380 void process_master_browser_announce(struct subnet_record *subrec, 
381                                      struct packet_struct *p,char *buf)
382 {
383   char *local_master_name = buf;
384   struct work_record *work;
385   struct browse_cache_record *browrec;
386
387   START_PROFILE(master_browser_announce);
388   local_master_name[15] = 0;
389   
390   DEBUG(3,("process_master_browser_announce: Local master announce from %s IP %s.\n",
391            local_master_name, inet_ntoa(p->ip)));
392   
393   if (!lp_domain_master()) 
394   {
395     DEBUG(0,("process_master_browser_announce: Not configured as domain \
396 master - ignoring master announce.\n"));
397     goto done;
398   }
399   
400   if((work = find_workgroup_on_subnet(subrec, global_myworkgroup)) == NULL)
401   {
402     DEBUG(0,("process_master_browser_announce: Cannot find workgroup %s on subnet %s\n",
403            global_myworkgroup, subrec->subnet_name));
404     goto done;
405   }
406
407   if(!AM_DOMAIN_MASTER_BROWSER(work))
408   {
409     DEBUG(0,("process_master_browser_announce: Local master announce made to us from \
410 %s IP %s and we are not a domain master browser.\n", local_master_name, inet_ntoa(p->ip)));
411     goto done;
412   }
413
414   /* Add this host as a local master browser entry on the browse lists.
415      This causes a sync request to be made to it at a later date.
416    */
417
418   if((browrec = find_browser_in_lmb_cache( local_master_name )) == NULL)
419   {
420     /* Add it. */
421     create_browser_in_lmb_cache( work->work_group, local_master_name, p->ip);
422   }
423   else
424     update_browser_death_time(browrec);
425 done:
426   END_PROFILE(master_browser_announce);
427 }
428
429 /*******************************************************************
430   Process an incoming LanMan host announcement packet.
431 *******************************************************************/
432
433 void process_lm_host_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
434 {
435   struct dgram_packet *dgram = &p->packet.dgram;
436   uint32 servertype = IVAL(buf,1);
437   int osmajor=CVAL(buf,5);           /* major version of node software */
438   int osminor=CVAL(buf,6);           /* minor version of node software */
439   int ttl = SVAL(buf,7);
440   char *announce_name = buf+9;
441   struct work_record *work;
442   struct server_record *servrec;
443   char *work_name;
444   char *source_name = dgram->source_name.name;
445   pstring comment;
446   char *s = buf+9;
447
448   START_PROFILE(lm_host_announce);
449   s = skip_string(s,1);
450   StrnCpy(comment, s, 43);
451
452   DEBUG(3,("process_lm_host_announce: LM Announcement from %s<%02x> IP %s to \
453 %s for server %s.\n", source_name, source_name[15], inet_ntoa(p->ip),
454               nmb_namestr(&dgram->dest_name),announce_name));
455
456   DEBUG(5,("process_lm_host_announce: os=(%d,%d) ttl=%d server type=%08x comment=%s\n",
457           osmajor, osminor, ttl, servertype,comment));
458
459   if ((osmajor < 36) || (osmajor > 38) || (osminor !=0))
460   {
461     DEBUG(5,("process_lm_host_announce: LM Announcement packet does not \
462 originate from OS/2 Warp client. Ignoring packet.\n"));
463     /* Could have been from a Windows machine (with its LM Announce enabled),
464        or a Samba server. Then don't disrupt the current browse list. */
465     goto done;
466   }
467
468   /* Filter servertype to remove impossible bits. */
469   servertype &= ~(SV_TYPE_LOCAL_LIST_ONLY|SV_TYPE_DOMAIN_ENUM);
470
471   /* A LanMan host announcement must be sent to the name WORKGROUP<00>. */
472   if(dgram->dest_name.name_type != 0x00)
473   {
474     DEBUG(2,("process_lm_host_announce: incorrect name type for destination from IP %s \
475 (was %02x) should be 0x00. Allowing packet anyway.\n",
476               inet_ntoa(p->ip), dgram->dest_name.name_type));
477     /* Change it so it was. */
478     dgram->dest_name.name_type = 0x00;
479   }
480
481   /* For a LanMan host announce the workgroup name is the destination name. */
482   work_name = dgram->dest_name.name;
483
484   /*
485    * Syntax servers version 5.1 send HostAnnounce packets to
486    * *THE WRONG NAME*. They send to LOCAL_MASTER_BROWSER_NAME<00>
487    * instead of WORKGROUP<1d> name. So to fix this we check if
488    * the workgroup name is our own name, and if so change it
489    * to be our primary workgroup name. This code is probably
490    * not needed in the LanMan announce code, but it won't hurt.
491    */
492
493   if(strequal(work_name, global_myname))
494     work_name = global_myworkgroup;
495
496   /*
497    * We are being very agressive here in adding a workgroup
498    * name on the basis of a host announcing itself as being
499    * in that workgroup. Maybe we should wait for the workgroup
500    * announce instead ? JRA.
501    */
502
503   work = find_workgroup_on_subnet(subrec, work_name);
504
505   if(servertype != 0)
506   {
507     if (work == NULL)
508     {
509       /* We have no record of this workgroup. Add it. */
510       if((work = create_workgroup_on_subnet(subrec, work_name, ttl))==NULL)
511         goto done;
512     }
513
514     if((servrec = find_server_in_workgroup( work, announce_name))==NULL)
515     {
516       /* If this server is not already in the workgroup, add it. */
517       create_server_on_workgroup(work, announce_name,
518                                  servertype|SV_TYPE_LOCAL_LIST_ONLY,
519                                  ttl, comment);
520     }
521     else
522     {
523       /* Update the record. */
524       servrec->serv.type = servertype|SV_TYPE_LOCAL_LIST_ONLY;
525       update_server_ttl( servrec, ttl);
526       StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
527     }
528   }
529   else
530   {
531     /*
532      * This server is announcing it is going down. Remove it from the
533      * workgroup.
534      */
535     if(!is_myname(announce_name) && (work != NULL) &&
536        ((servrec = find_server_in_workgroup( work, announce_name))!=NULL)
537       )
538     {
539       remove_server_from_workgroup( work, servrec);
540     }
541   }
542
543   subrec->work_changed = True;
544   found_lm_clients = True;
545 done:
546   END_PROFILE(lm_host_announce);
547 }
548
549 /****************************************************************************
550   Send a backup list response.
551 *****************************************************************************/
552 static void send_backup_list_response(struct subnet_record *subrec, 
553                                       struct work_record *work,
554                                       struct nmb_name *send_to_name,
555                                       unsigned char max_number_requested,
556                                       uint32 token, struct in_addr sendto_ip,
557                                       int port)
558 {                     
559   char outbuf[1024];
560   char *p, *countptr;
561   unsigned int count = 0;
562 #if 0
563   struct server_record *servrec;
564 #endif
565
566   memset(outbuf,'\0',sizeof(outbuf));
567
568   DEBUG(3,("send_backup_list_response: sending backup list for workgroup %s to %s IP %s\n",
569            work->work_group, nmb_namestr(send_to_name), inet_ntoa(sendto_ip)));
570   
571   p = outbuf;
572   
573   SCVAL(p,0,ANN_GetBackupListResp); /* Backup list response opcode. */
574   p++;
575
576   countptr = p;
577   p++;
578
579   SIVAL(p,0,token); /* The sender's unique info. */
580   p += 4;
581   
582   /* We always return at least one name - our own. */
583   count = 1;
584   StrnCpy(p,global_myname,15);
585   strupper(p);
586   p = skip_string(p,1);
587
588   /* Look for backup browsers in this workgroup. */
589
590 #if 0
591   /* we don't currently send become_backup requests so we should never
592      send any other servers names out as backups for our
593      workgroup. That's why this is commented out (tridge) */
594
595   /*
596    * NB. Note that the struct work_record here is not neccessarily
597    * attached to the subnet *subrec.
598    */
599
600   for (servrec = work->serverlist; servrec; servrec = servrec->next)
601   { 
602     int len = PTR_DIFF(p, outbuf);
603     if((sizeof(outbuf) - len) < 16)
604       break;
605
606     if(count >= (unsigned int)max_number_requested)
607       break;
608
609     if(strnequal(servrec->serv.name, global_myname,15))
610       continue;
611
612     if(!(servrec->serv.type & SV_TYPE_BACKUP_BROWSER))
613       continue;
614
615     StrnCpy(p, servrec->serv.name, 15);
616     strupper(p);
617     count++;
618
619     DEBUG(5,("send_backup_list_response: Adding server %s number %d\n",
620               p, count));
621
622     p = skip_string(p,1);
623   }
624 #endif
625
626   SCVAL(countptr, 0, count);
627
628   DEBUG(4,("send_backup_list_response: sending response to %s<00> IP %s with %d servers.\n",
629           send_to_name->name, inet_ntoa(sendto_ip), count));
630
631   send_mailslot(True, BROWSE_MAILSLOT,
632                 outbuf,PTR_DIFF(p,outbuf),
633                 global_myname, 0, 
634                 send_to_name->name,0,
635                 sendto_ip, subrec->myip, port);
636 }
637
638 /*******************************************************************
639   Process a send backup list request packet.
640
641   A client sends a backup list request to ask for a list of servers on
642   the net that maintain server lists for a domain. A server is then
643   chosen from this list to send NetServerEnum commands to to list
644   available servers.
645
646 ********************************************************************/
647
648 void process_get_backup_list_request(struct subnet_record *subrec,
649                                      struct packet_struct *p,char *buf)
650 {
651   struct dgram_packet *dgram = &p->packet.dgram;
652   struct work_record *work;
653   unsigned char max_number_requested = CVAL(buf,0);
654   uint32 token = IVAL(buf,1); /* Sender's key index for the workgroup. */
655   int name_type = dgram->dest_name.name_type;
656   char *workgroup_name = dgram->dest_name.name;
657   struct subnet_record *search_subrec = subrec;
658
659   START_PROFILE(get_backup_list);
660   DEBUG(3,("process_get_backup_list_request: request from %s IP %s to %s.\n",
661            nmb_namestr(&dgram->source_name), inet_ntoa(p->ip),
662            nmb_namestr(&dgram->dest_name)));
663   
664   /* We have to be a master browser, or a domain master browser
665      for the requested workgroup. That means it must be our
666      workgroup. */
667
668   if(strequal(workgroup_name, global_myworkgroup) == False)
669   {
670     DEBUG(7,("process_get_backup_list_request: Ignoring announce request for workgroup %s.\n",
671            workgroup_name));
672     goto done;
673   }
674
675   if((work = find_workgroup_on_subnet(search_subrec, workgroup_name)) == NULL)
676   {
677     DEBUG(0,("process_get_backup_list_request: Cannot find workgroup %s on \
678 subnet %s.\n", workgroup_name, search_subrec->subnet_name));
679     goto done;
680   }
681
682   /* 
683    * If the packet was sent to WORKGROUP<1b> instead
684    * of WORKGROUP<1d> then it was unicast to us a domain master
685    * browser. Change search subrec to unicast.
686    */
687
688   if(name_type == 0x1b)
689   {
690     /* We must be a domain master browser in order to
691        process this packet. */
692
693     if(!AM_DOMAIN_MASTER_BROWSER(work))
694     {
695       DEBUG(0,("process_get_backup_list_request: domain list requested for workgroup %s \
696 and I am not a domain master browser.\n", workgroup_name));
697       goto done;
698     }
699
700     search_subrec = unicast_subnet;
701   }
702   else if (name_type == 0x1d)
703   {
704     /* We must be a local master browser in order to
705        process this packet. */
706
707     if(!AM_LOCAL_MASTER_BROWSER(work))
708     {
709       DEBUG(0,("process_get_backup_list_request: domain list requested for workgroup %s \
710 and I am not a local master browser.\n", workgroup_name));
711       goto done;
712     }
713   }
714   else
715   {
716     DEBUG(0,("process_get_backup_list_request: Invalid name type %x - should be 0x1b or 0x1d.\n",
717             name_type));
718     goto done;
719   }
720
721   send_backup_list_response(subrec, work, &dgram->source_name, 
722                             max_number_requested, token, p->ip, p->port);
723 done:
724   END_PROFILE(get_backup_list);
725 }
726
727 /*******************************************************************
728   Process a reset browser state packet.
729
730   Diagnostic packet:
731   0x1 - Stop being a master browser and become a backup browser.
732   0x2 - Discard browse lists, stop being a master browser, try again.
733   0x4 - Stop being a master browser forever.
734          
735 ******************************************************************/
736
737 void process_reset_browser(struct subnet_record *subrec,
738                                   struct packet_struct *p,char *buf)
739 {
740   struct dgram_packet *dgram = &p->packet.dgram;
741   int state = CVAL(buf,0);
742   struct subnet_record *sr;
743
744   START_PROFILE(reset_browser);
745   DEBUG(1,("process_reset_browser: received diagnostic browser reset \
746 request from %s IP %s state=0x%X\n",
747              nmb_namestr(&dgram->source_name), inet_ntoa(p->ip), state));
748
749   /* Stop being a local master browser on all our broadcast subnets. */
750   if (state & 0x1)
751   {
752     for (sr = FIRST_SUBNET; sr; sr = NEXT_SUBNET_EXCLUDING_UNICAST(sr))
753     {
754       struct work_record *work;
755       for (work = sr->workgrouplist; work; work = work->next)
756       {
757         if (AM_LOCAL_MASTER_BROWSER(work))
758           unbecome_local_master_browser(sr, work, True);
759       }
760     }
761   }
762   
763   /* Discard our browse lists. */
764   if (state & 0x2)
765   {
766     /*
767      * Calling expire_workgroups_and_servers with a -1
768      * time causes all servers not marked with a PERMANENT_TTL
769      * on the workgroup lists to be discarded, and all 
770      * workgroups with empty server lists to be discarded.
771      * This means we keep our own server names and workgroup
772      * as these have a PERMANENT_TTL.
773      */
774
775     expire_workgroups_and_servers(-1);
776   }
777   
778   /* Request to stop browsing altogether. */
779   if (state & 0x4)
780     DEBUG(1,("process_reset_browser: ignoring request to stop being a browser.\n"));
781
782   END_PROFILE(reset_browser);
783 }
784
785 /*******************************************************************
786   Process an announcement request packet.
787   We don't respond immediately, we just check it's a request for
788   our workgroup and then set the flag telling the announce code
789   in nmbd_sendannounce.c:announce_my_server_names that an 
790   announcement is needed soon.
791 ******************************************************************/
792
793 void process_announce_request(struct subnet_record *subrec, struct packet_struct *p, char *buf)
794 {
795   struct dgram_packet *dgram = &p->packet.dgram;
796   struct work_record *work;
797   char *workgroup_name = dgram->dest_name.name;
798  
799   START_PROFILE(announce_request);
800   DEBUG(3,("process_announce_request: Announce request from %s IP %s to %s.\n",
801            nmb_namestr(&dgram->source_name), inet_ntoa(p->ip),
802            nmb_namestr(&dgram->dest_name)));
803   
804   /* We only send announcement requests on our workgroup. */
805   if(strequal(workgroup_name, global_myworkgroup) == False)
806   {
807     DEBUG(7,("process_announce_request: Ignoring announce request for workgroup %s.\n",
808            workgroup_name));
809     goto done;
810   }
811
812   if((work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL)
813   {
814     DEBUG(0,("process_announce_request: Unable to find workgroup %s on subnet !\n",
815             workgroup_name));
816     goto done;
817   }
818
819   work->needannounce = True;
820 done:
821   END_PROFILE(lm_host_announce);
822 }
823
824 /*******************************************************************
825   Process a LanMan announcement request packet.
826   We don't respond immediately, we just check it's a request for
827   our workgroup and then set the flag telling that we have found
828   a LanMan client (DOS or OS/2) and that we will have to start
829   sending LanMan announcements (unless specifically disabled
830   through the "lm announce" parameter in smb.conf)
831 ******************************************************************/
832
833 void process_lm_announce_request(struct subnet_record *subrec, struct packet_struct *p, char *buf)
834 {
835   struct dgram_packet *dgram = &p->packet.dgram;
836   char *workgroup_name = dgram->dest_name.name;
837
838   START_PROFILE(lm_announce_request);
839   DEBUG(3,("process_lm_announce_request: Announce request from %s IP %s to %s.\n",
840            nmb_namestr(&dgram->source_name), inet_ntoa(p->ip),
841            nmb_namestr(&dgram->dest_name)));
842
843   /* We only send announcement requests on our workgroup. */
844   if(strequal(workgroup_name, global_myworkgroup) == False)
845   {
846     DEBUG(7,("process_lm_announce_request: Ignoring announce request for workgroup %s.\n",
847            workgroup_name));
848     goto done;
849   }
850
851   if(find_workgroup_on_subnet(subrec, workgroup_name) == NULL)
852   {
853     DEBUG(0,("process_announce_request: Unable to find workgroup %s on subnet !\n",
854             workgroup_name));
855     goto done;
856   }
857
858   found_lm_clients = True;
859 done:
860   END_PROFILE(lm_host_announce);
861 }