2 Unix SMB/Netbios implementation.
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
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.
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.
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.
27 extern int DEBUGLEVEL;
29 extern pstring myname;
30 extern fstring myworkgroup;
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.
40 /****************************************************************************
41 Tell a server to become a backup browser
42 **************************************************************************/
44 void tell_become_backup(void)
46 struct subnet_record *subrec;
47 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
49 struct work_record *work;
50 for (work = subrec->workgrouplist; work; work = work->next)
52 struct server_record *servrec;
56 for (servrec = work->serverlist; servrec; servrec = servrec->next)
60 if (is_myname(servrec->serv.name))
63 if (servrec->serv.type & SV_TYPE_BACKUP_BROWSER)
69 if (servrec->serv.type & SV_TYPE_MASTER_BROWSER)
72 if (!(servrec->serv.type & SV_TYPE_POTENTIAL_BROWSER))
75 DEBUG(3,("num servers: %d num backups: %d\n",
76 num_servers, num_backups));
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)
83 DEBUG(2,("sending become backup to %s %s for %s\n",
84 servrec->serv.name, inet_ntoa(subrec->bcast_ip),
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);
96 /*******************************************************************
97 Process an incoming host announcement packet.
98 *******************************************************************/
100 void process_host_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
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;
110 char *source_name = dgram->source_name.name;
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));
118 DEBUG(5,("process_host_announce: ttl=%d server type=%08x comment=%s\n",
119 ttl, servertype,comment));
121 /* Filter servertype to remove impossible bits. */
122 servertype &= ~(SV_TYPE_LOCAL_LIST_ONLY|SV_TYPE_DOMAIN_ENUM);
124 /* A host announcement must be sent to the name WORKGROUP<1d>. */
125 if(dgram->dest_name.name_type != 0x1d)
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;
134 /* For a host announce the workgroup name is the destination name. */
135 work_name = dgram->dest_name.name;
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.
145 if(strequal(work_name, myname))
146 work_name = myworkgroup;
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.
155 if ((work = find_workgroup_on_subnet(subrec, work_name))==NULL)
157 /* We have no record of this workgroup. Add it. */
158 if((work = create_workgroup_on_subnet(subrec, work_name, ttl))==NULL)
162 if((servrec = find_server_in_workgroup( work, announce_name))==NULL)
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,
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);
177 subrec->work_changed = True;
180 /*******************************************************************
181 Process an incoming WORKGROUP announcement packet.
182 *******************************************************************/
184 void process_workgroup_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
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;
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));
200 DEBUG(5,("process_workgroup_announce: ttl=%d server type=%08x master browser=%s\n",
201 ttl, servertype, master_name));
203 /* Workgroup announcements must only go to the MSBROWSE name. */
204 if (!strequal(dgram->dest_name.name, MSBROWSE) || (dgram->dest_name.name_type != 0x1))
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)));
211 if ((work = find_workgroup_on_subnet(subrec, workgroup_announce_name))==NULL)
213 /* We have no record of this workgroup. Add it. */
214 if((work = create_workgroup_on_subnet(subrec, workgroup_announce_name, ttl))==NULL)
219 /* Update the workgroup death_time. */
220 update_workgroup_ttl(work, ttl);
223 if(*work->local_master_browser_name == '\0')
225 /* Set the master browser name. */
226 StrnCpy(work->local_master_browser_name, master_name,
227 sizeof(work->local_master_browser_name)-1);
231 subrec->work_changed = True;
234 /*******************************************************************
235 Process an incoming local master browser announcement packet.
236 *******************************************************************/
238 void process_local_master_announce(struct subnet_record *subrec, struct packet_struct *p, char *buf)
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;
246 struct work_record *work;
247 struct server_record *servrec;
248 char *source_name = dgram->source_name.name;
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));
256 DEBUG(5,("process_local_master_announce: ttl=%d server type=%08x comment=%s\n",
257 ttl, servertype, comment));
259 /* A local master announcement must be sent to the name WORKGROUP<1e>. */
260 if(dgram->dest_name.name_type != 0x1e)
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));
268 /* Filter servertype to remove impossible bits. */
269 servertype &= ~(SV_TYPE_LOCAL_LIST_ONLY|SV_TYPE_DOMAIN_ENUM);
271 /* For a local master announce the workgroup name is the destination name. */
272 work_name = dgram->dest_name.name;
274 if ((work = find_workgroup_on_subnet(subrec, work_name))==NULL)
276 /* We have no record of this workgroup. Add it. */
277 if((work = create_workgroup_on_subnet(subrec, work_name, ttl))==NULL)
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
285 if(AM_LOCAL_MASTER_BROWSER(work))
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));
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. */
297 send_browser_reset( 0x2, "__SAMBA__" , 0x20, p->ip);
299 /* We should demote ourself and force an election. */
301 unbecome_local_master_browser( subrec, work);
303 /* The actual election requests are handled in
306 work->needelection = True;
310 /* Find the server record on this workgroup. If it doesn't exist, add it. */
312 if((servrec = find_server_in_workgroup( work, server_name))==NULL)
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,
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);
327 /* Set the master browser name. */
328 StrnCpy(work->local_master_browser_name, server_name,
329 sizeof(work->local_master_browser_name)-1);
331 subrec->work_changed = True;
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 ******************************************************************/
341 void process_master_browser_announce(struct subnet_record *subrec,
342 struct packet_struct *p,char *buf)
344 char *local_master_name = buf;
345 struct work_record *work;
346 struct browse_cache_record *browrec;
348 local_master_name[15] = 0;
350 DEBUG(3,("process_master_browser_announce: Local master announce from %s IP %s.\n",
351 local_master_name, inet_ntoa(p->ip)));
353 if (!lp_domain_master())
355 DEBUG(0,("process_master_browser_announce: Not configured as domain \
356 master - ignoring master announce.\n"));
360 if((work = find_workgroup_on_subnet(subrec, myworkgroup)) == NULL)
362 DEBUG(0,("process_master_browser_announce: Cannot find workgroup %s on subnet %s\n",
363 myworkgroup, subrec->subnet_name));
367 if(!AM_DOMAIN_MASTER_BROWSER(work))
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)));
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.
378 if((browrec = find_browser_in_lmb_cache( local_master_name )) == NULL)
381 create_browser_in_lmb_cache( work->work_group, local_master_name, p->ip);
384 update_browser_death_time(browrec);
387 /****************************************************************************
388 Send a backup list response.
389 *****************************************************************************/
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)
398 char *p, *countptr, *nameptr;
401 struct server_record *servrec;
403 bzero(outbuf,sizeof(outbuf));
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)));
410 SCVAL(p,0,ANN_GetBackupListResp); /* Backup list response opcode. */
416 SIVAL(p,0,token); /* The sender's unique info. */
421 /* We always return at least one name - our own. */
423 StrnCpy(p,myname,15);
425 p = skip_string(p,1);
427 /* Look for backup browsers in this workgroup. */
428 for (servrec = work->serverlist; servrec; servrec = servrec->next)
430 len = PTR_DIFF(p, outbuf);
431 if((sizeof(outbuf) - len) < 16)
434 if(count >= max_number_requested)
437 if(strnequal(servrec->serv.name, myname,15))
440 if(!(servrec->serv.type & SV_TYPE_BACKUP_BROWSER))
443 StrnCpy(p, servrec->serv.name, 15);
447 DEBUG(5,("send_backup_list_response: Adding server %s number %d\n",
450 p = skip_string(p,1);
453 SCVAL(countptr, 0, count);
455 len = PTR_DIFF(p, outbuf);
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));
460 send_mailslot(True, BROWSE_MAILSLOT,
461 outbuf,PTR_DIFF(p,outbuf),
463 send_to_name->name,0,
464 sendto_ip, subrec->myip);
467 /*******************************************************************
468 Process a send backup list request packet.
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
475 ********************************************************************/
477 void process_get_backup_list_request(struct subnet_record *subrec,
478 struct packet_struct *p,char *buf)
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;
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)));
491 /* We have to be a master browser, or a domain master browser
492 for the requested workgroup. That means it must be our
495 if(strequal(workgroup_name, myworkgroup) == False)
497 DEBUG(7,("process_get_backup_list_request: Ignoring announce request for workgroup %s.\n",
502 if((work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL)
504 DEBUG(0,("process_get_backup_list_request: Cannot find workgroup %s on \
505 subnet %s.\n", workgroup_name, subrec->subnet_name));
509 if(name_type == 0x1b)
511 /* We must be a domain master browser in order to
512 process this packet. */
514 if(!AM_DOMAIN_MASTER_BROWSER(work))
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));
521 else if (name_type == 0x1d)
523 /* We must be a local master browser in order to
524 process this packet. */
526 if(!AM_LOCAL_MASTER_BROWSER(work))
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));
535 DEBUG(0,("process_get_backup_list_request: Invalid name type %x - should be 0x1b or 0x1d.\n",
540 send_backup_list_response(subrec, work, &dgram->source_name,
541 max_number_requested, token, p->ip);
544 /*******************************************************************
545 Process a reset browser state 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.
552 ******************************************************************/
554 void process_reset_browser(struct subnet_record *subrec,
555 struct packet_struct *p,char *buf)
557 struct dgram_packet *dgram = &p->packet.dgram;
558 int state = CVAL(buf,0);
559 struct subnet_record *sr;
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));
565 /* Stop being a local master browser on all our broadcast subnets. */
568 for (sr = FIRST_SUBNET; sr; sr = NEXT_SUBNET_EXCLUDING_UNICAST(sr))
570 struct work_record *work;
571 for (work = sr->workgrouplist; work; work = work->next)
573 if (AM_LOCAL_MASTER_BROWSER(work))
575 unbecome_local_master_browser(sr, work);
576 work->needelection = True;
582 /* Discard our browse lists. */
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.
594 expire_workgroups_and_servers(-1);
597 /* Request to stop browsing altogether. */
599 DEBUG(1,("process_reset_browser: ignoring request to stop being a browser.\n"));
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 ******************************************************************/
610 void process_announce_request(struct subnet_record *subrec, struct packet_struct *p, char *buf)
612 struct dgram_packet *dgram = &p->packet.dgram;
613 struct work_record *work;
614 char *workgroup_name = dgram->dest_name.name;
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)));
620 /* We only send announcement requests on our workgroup. */
621 if(strequal(workgroup_name, myworkgroup) == False)
623 DEBUG(7,("process_announce_request: Ignoring announce request for workgroup %s.\n",
628 if((work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL)
630 DEBUG(0,("process_announce_request: Unable to find workgroup %s on subnet !\n",
635 work->needannounce = True;