nmbd_incomingdgrams.c: Fix bug with Syntax 5.1 servers reported by SGI where
[gd/samba/.git] / source / 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-1997
6    Copyright (C) Luke Kenneth Casson Leighton 1994-1997 
7    Copyright (C) Jeremy Allison 1994-1997
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 int DEBUGLEVEL;
28
29 extern pstring myname;
30 extern fstring myworkgroup;
31
32 #if 0
33
34 /* XXXX note: This function is currently unsuitable for use, as it
35    does not properly check that a server is in a fit state to become
36    a backup browser before asking it to be one.
37    The code is left here to be worked on at a later date.
38 */
39
40 /****************************************************************************
41 Tell a server to become a backup browser
42 **************************************************************************/
43
44 void tell_become_backup(void)
45 {
46   struct subnet_record *subrec;
47   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
48   {
49     struct work_record *work;
50     for (work = subrec->workgrouplist; work; work = work->next)
51     {
52       struct server_record *servrec;
53       int num_servers = 0;
54       int num_backups = 0;
55           
56       for (servrec = work->serverlist; servrec; servrec = servrec->next)
57       {
58         num_servers++;
59               
60         if (is_myname(servrec->serv.name))
61           continue;
62               
63         if (servrec->serv.type & SV_TYPE_BACKUP_BROWSER) 
64         {
65           num_backups++;
66           continue;
67         }
68               
69         if (servrec->serv.type & SV_TYPE_MASTER_BROWSER)
70           continue;
71               
72         if (!(servrec->serv.type & SV_TYPE_POTENTIAL_BROWSER))
73           continue;
74               
75         DEBUG(3,("num servers: %d num backups: %d\n", 
76               num_servers, num_backups));
77               
78         /* make first server a backup server. thereafter make every
79            tenth server a backup server */
80         if (num_backups != 0 && (num_servers+9) / num_backups > 10)
81           continue;
82               
83         DEBUG(2,("sending become backup to %s %s for %s\n",
84              servrec->serv.name, inet_ntoa(subrec->bcast_ip),
85              work->work_group));
86               
87         /* type 11 request from MYNAME(20) to WG(1e) for SERVER */
88         do_announce_request(servrec->serv.name, work->work_group,
89               ANN_BecomeBackup, 0x20, 0x1e, subrec->bcast_ip);
90       }
91     }
92   }
93 }
94 #endif
95
96 /*******************************************************************
97   Process an incoming host announcement packet.
98 *******************************************************************/
99
100 void process_host_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
101 {
102   struct dgram_packet *dgram = &p->packet.dgram;
103   int ttl = IVAL(buf,1)/1000;
104   char *announce_name = buf+5;
105   uint32 servertype = IVAL(buf,23);
106   char *comment = buf+31;
107   struct work_record *work;
108   struct server_record *servrec;
109   char *work_name;
110   char *source_name = dgram->source_name.name;
111
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               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, myname))
146     work_name = 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   if ((work = find_workgroup_on_subnet(subrec, work_name))==NULL)
156   {
157     /* We have no record of this workgroup. Add it. */
158     if((work = create_workgroup_on_subnet(subrec, work_name, ttl))==NULL)
159       return;
160   }
161   
162   if((servrec = find_server_in_workgroup( work, announce_name))==NULL)
163   {
164     /* If this server is not already in the workgroup, add it. */
165     create_server_on_workgroup(work, announce_name, 
166                                servertype|SV_TYPE_LOCAL_LIST_ONLY, 
167                                ttl, comment);
168   }
169   else
170   {
171     /* Update the record. */
172     servrec->serv.type = servertype|SV_TYPE_LOCAL_LIST_ONLY;
173     update_server_ttl( servrec, ttl);
174     StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
175   }
176
177   subrec->work_changed = True;
178 }
179
180 /*******************************************************************
181   Process an incoming WORKGROUP announcement packet.
182 *******************************************************************/
183
184 void process_workgroup_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
185 {
186   struct dgram_packet *dgram = &p->packet.dgram;
187   int ttl = IVAL(buf,1)/1000;
188   char *workgroup_announce_name = buf+5;
189   uint32 servertype = IVAL(buf,23);
190   char *master_name = buf+31;
191   struct work_record *work;
192   char *source_name = dgram->source_name.name;
193
194   master_name[43] = 0;
195
196   DEBUG(3,("process_workgroup_announce: from %s<%02x> IP %s to \
197 %s for workgroup %s.\n", source_name, source_name[15], inet_ntoa(p->ip),
198               namestr(&dgram->dest_name),workgroup_announce_name));
199
200   DEBUG(5,("process_workgroup_announce: ttl=%d server type=%08x master browser=%s\n",
201            ttl, servertype, master_name));
202
203   /* Workgroup announcements must only go to the MSBROWSE name. */
204   if (!strequal(dgram->dest_name.name, MSBROWSE) || (dgram->dest_name.name_type != 0x1))
205   {
206     DEBUG(0,("process_workgroup_announce: from IP %s should be to __MSBROWSE__<0x01> not %s\n",
207               inet_ntoa(p->ip), namestr(&dgram->dest_name)));
208     return;
209   }
210
211   if ((work = find_workgroup_on_subnet(subrec, workgroup_announce_name))==NULL)
212   {
213     /* We have no record of this workgroup. Add it. */
214     if((work = create_workgroup_on_subnet(subrec, workgroup_announce_name, ttl))==NULL)
215       return;
216   }
217   else
218   {
219     /* Update the workgroup death_time. */
220     update_workgroup_ttl(work, ttl);
221   }
222
223   if(*work->local_master_browser_name == '\0')
224   {
225     /* Set the master browser name. */
226     StrnCpy(work->local_master_browser_name, master_name,
227             sizeof(work->local_master_browser_name)-1);
228
229   }
230
231   subrec->work_changed = True;
232 }
233
234 /*******************************************************************
235   Process an incoming local master browser announcement packet.
236 *******************************************************************/
237
238 void process_local_master_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
239 {
240   struct dgram_packet *dgram = &p->packet.dgram;
241   int ttl = IVAL(buf,1)/1000;
242   char *server_name = buf+5;
243   uint32 servertype = IVAL(buf,23);
244   char *comment = buf+31;
245   char *work_name;
246   struct work_record *work;
247   struct server_record *servrec;
248   char *source_name = dgram->source_name.name;
249
250   comment[43] = 0;
251
252   DEBUG(3,("process_local_master_announce: from %s<%02x> IP %s to \
253 %s for server %s.\n", source_name, source_name[15], inet_ntoa(p->ip),
254               namestr(&dgram->dest_name),server_name));
255
256   DEBUG(5,("process_local_master_announce: ttl=%d server type=%08x comment=%s\n",
257            ttl, servertype, comment));
258
259   /* A local master announcement must be sent to the name WORKGROUP<1e>. */
260   if(dgram->dest_name.name_type != 0x1e)
261   {
262     DEBUG(0,("process_local_master_announce: incorrect name type for destination from IP %s \
263 (was %02x) should be 0x1e. Ignoring packet.\n",
264               inet_ntoa(p->ip), dgram->dest_name.name_type));
265     return;
266   }
267
268   /* Filter servertype to remove impossible bits. */
269   servertype &= ~(SV_TYPE_LOCAL_LIST_ONLY|SV_TYPE_DOMAIN_ENUM);
270
271   /* For a local master announce the workgroup name is the destination name. */
272   work_name = dgram->dest_name.name;
273
274   if ((work = find_workgroup_on_subnet(subrec, work_name))==NULL)
275   {
276     /* We have no record of this workgroup. Add it. */
277     if((work = create_workgroup_on_subnet(subrec, work_name, ttl))==NULL)
278       return;
279   }
280
281   /* If we think we're the local master browser for this workgroup,
282      we should never have got this packet. We don't see our own
283      packets.
284    */
285   if(AM_LOCAL_MASTER_BROWSER(work))
286   {
287     DEBUG(0,("process_local_master_announce: Server %s at IP %s is announcing itself as \
288 a local master browser for workgroup %s and we think we are master. Forcing election.\n",
289       server_name, inet_ntoa(p->ip), work_name));
290
291     /* Samba nmbd versions 1.9.17 to 1.9.17p4 have a bug in that when
292        they have become a local master browser once, they will never
293        stop sending local master announcements. To fix this we send
294        them a reset browser packet, with level 0x2 on the __SAMBA__
295        name that only they should be listening to. */
296    
297     send_browser_reset( 0x2, "__SAMBA__" , 0x20, p->ip);
298
299     /* We should demote ourself and force an election. */
300
301     unbecome_local_master_browser( subrec, work);
302
303     /* The actual election requests are handled in
304        nmbd_election.c */
305
306     work->needelection = True;
307     return;
308   }  
309
310   /* Find the server record on this workgroup. If it doesn't exist, add it. */
311
312   if((servrec = find_server_in_workgroup( work, server_name))==NULL)
313   {
314     /* If this server is not already in the workgroup, add it. */
315     create_server_on_workgroup(work, server_name, 
316                                servertype|SV_TYPE_LOCAL_LIST_ONLY, 
317                                ttl, comment);
318   }
319   else
320   {
321     /* Update the record. */
322     servrec->serv.type = servertype|SV_TYPE_LOCAL_LIST_ONLY;
323     update_server_ttl(servrec, ttl);
324     StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
325   }
326
327   /* Set the master browser name. */
328   StrnCpy(work->local_master_browser_name, server_name,
329             sizeof(work->local_master_browser_name)-1);
330
331   subrec->work_changed = True;
332 }
333
334 /*******************************************************************
335   Process a domain master announcement frame.
336   Domain master browsers receive these from local masters. The Domain
337   master should then issue a sync with the local master, asking for
338   that machines local server list.
339 ******************************************************************/
340
341 void process_master_browser_announce(struct subnet_record *subrec, 
342                                      struct packet_struct *p,char *buf)
343 {
344   char *local_master_name = buf;
345   struct work_record *work;
346   struct browse_cache_record *browrec;
347
348   local_master_name[15] = 0;
349   
350   DEBUG(3,("process_master_browser_announce: Local master announce from %s IP %s.\n",
351            local_master_name, inet_ntoa(p->ip)));
352   
353   if (!lp_domain_master()) 
354   {
355     DEBUG(0,("process_master_browser_announce: Not configured as domain \
356 master - ignoring master announce.\n"));
357     return;
358   }
359   
360   if((work = find_workgroup_on_subnet(subrec, myworkgroup)) == NULL)
361   {
362     DEBUG(0,("process_master_browser_announce: Cannot find workgroup %s on subnet %s\n",
363            myworkgroup, subrec->subnet_name));
364     return;
365   }
366
367   if(!AM_DOMAIN_MASTER_BROWSER(work))
368   {
369     DEBUG(0,("process_master_browser_announce: Local master announce made to us from \
370 %s IP %s and we are not a domain master browser.\n", local_master_name, inet_ntoa(p->ip)));
371     return;
372   }
373
374   /* Add this host as a local master browser entry on the browse lists.
375      This causes a sync request to be made to it at a later date.
376    */
377
378   if((browrec = find_browser_in_lmb_cache( local_master_name )) == NULL)
379   {
380     /* Add it. */
381     create_browser_in_lmb_cache( work->work_group, local_master_name, p->ip);
382   }
383   else
384     update_browser_death_time(browrec);
385 }
386
387 /****************************************************************************
388   Send a backup list response.
389 *****************************************************************************/
390
391 static void send_backup_list_response(struct subnet_record *subrec, 
392                              struct work_record *work,
393                              struct nmb_name *send_to_name,
394                              unsigned char max_number_requested,
395                              uint32 token, struct in_addr sendto_ip)
396 {                     
397   char outbuf[1024];
398   char *p, *countptr, *nameptr;
399   int count = 0;
400   int len;
401   struct server_record *servrec;
402
403   bzero(outbuf,sizeof(outbuf));
404
405   DEBUG(3,("send_backup_list_response: sending backup list for workgroup %s to %s IP %s\n",
406            work->work_group, namestr(send_to_name), inet_ntoa(sendto_ip)));
407   
408   p = outbuf;
409   
410   SCVAL(p,0,ANN_GetBackupListResp); /* Backup list response opcode. */
411   p++;
412
413   countptr = p;
414   p++;
415
416   SIVAL(p,0,token); /* The sender's unique info. */
417   p += 4;
418   
419   nameptr = p;
420
421   /* We always return at least one name - our own. */
422   count = 1;
423   StrnCpy(p,myname,15);
424   strupper(p);
425   p = skip_string(p,1);
426
427   /* Look for backup browsers in this workgroup. */
428   for (servrec = work->serverlist; servrec; servrec = servrec->next)
429   { 
430     len = PTR_DIFF(p, outbuf);
431     if((sizeof(outbuf) - len) < 16)
432       break;
433
434     if(count >= max_number_requested)
435       break;
436
437     if(strnequal(servrec->serv.name, myname,15))
438       continue;
439
440     if(!(servrec->serv.type & SV_TYPE_BACKUP_BROWSER))
441       continue;
442
443     StrnCpy(p, servrec->serv.name, 15);
444     strupper(p);
445     count++;
446
447     DEBUG(5,("send_backup_list_response: Adding server %s number %d\n",
448               p, count));
449
450     p = skip_string(p,1);
451   }
452
453   SCVAL(countptr, 0, count);
454
455   len = PTR_DIFF(p, outbuf);
456
457   DEBUG(4,("send_backup_list_response: sending response to %s<00> IP %s with %d servers.\n",
458           send_to_name->name, inet_ntoa(sendto_ip), count));
459
460   send_mailslot(True, BROWSE_MAILSLOT,
461                 outbuf,PTR_DIFF(p,outbuf),
462                 myname, 0, 
463                 send_to_name->name,0,
464                 sendto_ip, subrec->myip);
465 }
466
467 /*******************************************************************
468   Process a send backup list request packet.
469
470   A client sends a backup list request to ask for a list of servers on
471   the net that maintain server lists for a domain. A server is then
472   chosen from this list to send NetServerEnum commands to to list
473   available servers.
474
475 ********************************************************************/
476
477 void process_get_backup_list_request(struct subnet_record *subrec,
478                                      struct packet_struct *p,char *buf)
479 {
480   struct dgram_packet *dgram = &p->packet.dgram;
481   struct work_record *work;
482   unsigned char max_number_requested = CVAL(buf,0);
483   uint32 token = IVAL(buf,1); /* Sender's key index for the workgroup. */
484   int name_type = dgram->dest_name.name_type;
485   char *workgroup_name = dgram->dest_name.name;
486
487   DEBUG(3,("process_get_backup_list_request: request from %s IP %s to %s.\n",
488            namestr(&dgram->source_name), inet_ntoa(p->ip),
489            namestr(&dgram->dest_name)));
490   
491   /* We have to be a master browser, or a domain master browser
492      for the requested workgroup. That means it must be our
493      workgroup. */
494
495   if(strequal(workgroup_name, myworkgroup) == False)
496   {
497     DEBUG(7,("process_get_backup_list_request: Ignoring announce request for workgroup %s.\n",
498            workgroup_name));
499     return;
500   }
501
502   if((work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL)
503   {
504     DEBUG(0,("process_get_backup_list_request: Cannot find workgroup %s on \
505 subnet %s.\n", workgroup_name, subrec->subnet_name));
506     return;
507   }
508
509   if(name_type == 0x1b)
510   {
511     /* We must be a domain master browser in order to
512        process this packet. */
513
514     if(!AM_DOMAIN_MASTER_BROWSER(work))
515     {
516       DEBUG(0,("process_get_backup_list_request: domain list requested for workgroup %s \
517 and I am not a domain master browser.\n", workgroup_name));
518       return;
519     }
520   }
521   else if (name_type == 0x1d)
522   {
523     /* We must be a local master browser in order to
524        process this packet. */
525
526     if(!AM_LOCAL_MASTER_BROWSER(work))
527     {
528       DEBUG(0,("process_get_backup_list_request: domain list requested for workgroup %s \
529 and I am not a local master browser.\n", workgroup_name));
530       return;
531     }
532   }
533   else
534   {
535     DEBUG(0,("process_get_backup_list_request: Invalid name type %x - should be 0x1b or 0x1d.\n",
536             name_type));
537     return;
538   }
539
540   send_backup_list_response(subrec, work, &dgram->source_name, 
541                             max_number_requested, token, p->ip);
542 }
543
544 /*******************************************************************
545   Process a reset browser state packet.
546
547   Diagnostic packet:
548   0x1 - Stop being a master browser and become a backup browser.
549   0x2 - Discard browse lists, stop being a master browser, try again.
550   0x4 - Stop being a master browser forever.
551          
552 ******************************************************************/
553
554 void process_reset_browser(struct subnet_record *subrec,
555                                   struct packet_struct *p,char *buf)
556 {
557   struct dgram_packet *dgram = &p->packet.dgram;
558   int state = CVAL(buf,0);
559   struct subnet_record *sr;
560
561   DEBUG(1,("process_reset_browser: received diagnostic browser reset \
562 request from %s IP %s state=0x%X\n",
563              namestr(&dgram->source_name), inet_ntoa(p->ip), state));
564
565   /* Stop being a local master browser on all our broadcast subnets. */
566   if (state & 0x1)
567   {
568     for (sr = FIRST_SUBNET; sr; sr = NEXT_SUBNET_EXCLUDING_UNICAST(sr))
569     {
570       struct work_record *work;
571       for (work = sr->workgrouplist; work; work = work->next)
572       {
573         if (AM_LOCAL_MASTER_BROWSER(work))
574         {
575           unbecome_local_master_browser(sr, work);
576           work->needelection = True;
577         }
578       }
579     }
580   }
581   
582   /* Discard our browse lists. */
583   if (state & 0x2)
584   {
585     /*
586      * Calling expire_workgroups_and_servers with a -1
587      * time causes all servers not marked with a PERMANENT_TTL
588      * on the workgroup lists to be discarded, and all 
589      * workgroups with empty server lists to be discarded.
590      * This means we keep our own server names and workgroup
591      * as these have a PERMANENT_TTL.
592      */
593
594     expire_workgroups_and_servers(-1);
595   }
596   
597   /* Request to stop browsing altogether. */
598   if (state & 0x4)
599     DEBUG(1,("process_reset_browser: ignoring request to stop being a browser.\n"));
600 }
601
602 /*******************************************************************
603   Process a announcement request packet.
604   We don't respond immediately, we just check it's a request for
605   out workgroup and then set the flag telling the announce code
606   in nmbd_sendannounce.c:announce_my_server_names that an 
607   announcement is needed soon.
608   ******************************************************************/
609
610 void process_announce_request(struct subnet_record *subrec, struct packet_struct *p, char *buf)
611 {
612   struct dgram_packet *dgram = &p->packet.dgram;
613   struct work_record *work;
614   char *workgroup_name = dgram->dest_name.name;
615  
616   DEBUG(3,("process_announce_request: Announce request from %s IP %s to %s.\n",
617            namestr(&dgram->source_name), inet_ntoa(p->ip),
618            namestr(&dgram->dest_name)));
619   
620   /* We only send announcement requests on our workgroup. */
621   if(strequal(workgroup_name, myworkgroup) == False)
622   {
623     DEBUG(7,("process_announce_request: Ignoring announce request for workgroup %s.\n",
624            workgroup_name));
625     return;
626   }
627
628   if((work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL)
629   {
630     DEBUG(0,("process_announce_request: Unable to find workgroup %s on subnet !\n",
631             workgroup_name));
632     return;
633   }
634
635   work->needannounce = True;
636 }