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