2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1996
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 Module name: nameelect.c
25 14 jan 96: lkcl@pires.co.uk
26 added multiple workgroup domain master support
28 04 jul 96: lkcl@pires.co.uk
29 added system to become a master browser by stages.
37 extern int ClientDGRAM;
39 extern int DEBUGLEVEL;
42 extern pstring myname;
43 extern struct in_addr ipzero;
44 extern struct in_addr ipgrp;
46 /* here are my election parameters */
48 extern time_t StartupTime;
50 extern struct subnet_record *subnetlist;
52 extern uint16 nb_type; /* samba's NetBIOS name type */
55 /*******************************************************************
56 occasionally check to see if the master browser is around
57 ******************************************************************/
58 void check_master_browser(time_t t)
60 static time_t lastrun=0;
61 struct subnet_record *d;
63 if (!lastrun) lastrun = t;
64 if (t < lastrun + CHECK_TIME_MST_BROWSE * 60) return;
70 for (d = subnetlist; d; d = d->next)
72 struct work_record *work;
74 for (work = d->workgrouplist; work; work = work->next)
78 if (lp_preferred_master())
80 /* preferred master - not a master browser. force
81 becoming a master browser, hence the log message.
84 DEBUG(0,("%s preferred master for %s %s - force election\n",
85 timestring(), work->work_group,
86 inet_ntoa(d->bcast_ip)));
88 browser_gone(work->work_group, d->bcast_ip);
92 /* if we are not the browse master of a workgroup,
93 and we can't find a browser on the subnet, do
97 queue_netbios_packet(d,ClientNMB,NMB_QUERY,NAME_QUERY_MST_CHK,
98 work->work_group,0x1d,0,0,0,NULL,NULL,
99 True,False,d->bcast_ip,d->bcast_ip);
107 /*******************************************************************
108 what to do if a master browser DOESN't exist
109 ******************************************************************/
110 void browser_gone(char *work_name, struct in_addr ip)
112 struct subnet_record *d = find_subnet(ip);
113 struct work_record *work = find_workgroupstruct(d, work_name, False);
115 /* i don't know about this workgroup, therefore i don't care */
116 if (!work || !d) return;
118 /* don't do election stuff on the WINS subnet */
119 if (ip_equal(d->bcast_ip,ipgrp))
122 if (strequal(work->work_group, lp_workgroup()))
125 DEBUG(2,("Forcing election on %s %s\n",
126 work->work_group,inet_ntoa(d->bcast_ip)));
128 /* we can attempt to become master browser */
129 work->needelection = True;
133 /* local interfaces: force an election */
134 send_election(d, work->work_group, 0, 0, myname);
136 /* only removes workgroup completely on a local interface
137 persistent lmhosts entries on a local interface _will_ be removed).
139 remove_workgroup(d, work,True);
144 /****************************************************************************
145 send an election packet
146 **************************************************************************/
147 void send_election(struct subnet_record *d, char *group,uint32 criterion,
148 int timeup,char *name)
155 DEBUG(2,("Sending election to %s for workgroup %s\n",
156 inet_ntoa(d->bcast_ip),group));
158 bzero(outbuf,sizeof(outbuf));
160 CVAL(p,0) = ANN_Election; /* election */
163 CVAL(p,0) = (criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION;
164 SIVAL(p,1,criterion);
165 SIVAL(p,5,timeup*1000); /* ms - despite the spec */
169 p = skip_string(p,1);
171 send_mailslot_reply(False,BROWSE_MAILSLOT,ClientDGRAM,
172 outbuf,PTR_DIFF(p,outbuf),
173 name,group,0,0x1e,d->bcast_ip,*iface_ip(d->bcast_ip));
177 /****************************************************************************
178 un-register a SELF name that got rejected.
180 if this name happens to be rejected when samba is in the process
181 of becoming a master browser (registering __MSBROWSE__, WORKGROUP(1d)
182 or WORKGROUP(1b)) then we must stop being a master browser. sad.
184 **************************************************************************/
185 void name_unregister_work(struct subnet_record *d, char *name, int name_type)
187 struct work_record *work;
188 int remove_type_local = 0;
189 int remove_type_domain = 0;
190 int remove_type_logon = 0;
192 remove_netbios_name(d,name,name_type,SELF,ipzero);
194 if (!(work = find_workgroupstruct(d, name, False))) return;
196 /* work out what to unbecome, from the name type being removed */
198 if (ms_browser_name(name, name_type))
200 remove_type_local |= SV_TYPE_MASTER_BROWSER;
202 if (AM_MASTER(work) && strequal(name, lp_workgroup()) && name_type == 0x1d)
204 remove_type_local |= SV_TYPE_MASTER_BROWSER;
206 if (AM_DOMMST(work) && strequal(name, lp_workgroup()) && name_type == 0x1b)
208 remove_type_domain |= SV_TYPE_DOMAIN_MASTER;
210 if (AM_DOMMEM(work) && strequal(name, lp_workgroup()) && name_type == 0x1c)
212 remove_type_logon|= SV_TYPE_DOMAIN_MEMBER;
215 if (remove_type_local ) unbecome_local_master (d, work, remove_type_local );
216 if (remove_type_domain) unbecome_domain_master(d, work, remove_type_domain);
217 if (remove_type_logon ) unbecome_logon_server (d, work, remove_type_logon );
221 /****************************************************************************
224 if the name being added is a SELF name, we must additionally check
225 whether to proceed to the next stage in samba becoming a master browser.
227 **************************************************************************/
228 void name_register_work(struct subnet_record *d, char *name, int name_type,
229 int nb_flags, time_t ttl, struct in_addr ip, BOOL bcast)
231 enum name_source source = (ismyip(ip) || ip_equal(ip, ipzero)) ?
236 struct work_record *work = find_workgroupstruct(d, lp_workgroup(), False);
238 add_netbios_entry(d,name,name_type,nb_flags,ttl,source,ip,True,!bcast);
242 int add_type_local = False;
243 int add_type_domain = False;
244 int add_type_logon = False;
246 DEBUG(4,("checking next stage: name_register_work %s\n", name));
248 /* work out what to become, from the name type being added */
250 if (ms_browser_name(name, name_type))
252 add_type_local = True;
254 if (strequal(name, lp_workgroup()) && name_type == 0x1d)
256 add_type_local = True;
258 if (strequal(name, lp_workgroup()) && name_type == 0x1b)
260 add_type_domain = True;
262 if (strequal(name, lp_workgroup()) && name_type == 0x1c)
264 add_type_logon = True;
267 if (add_type_local ) become_local_master (d, work);
268 if (add_type_domain) become_domain_master(d, work);
269 if (add_type_logon ) become_logon_server (d, work);
275 /*******************************************************************
276 become the local master browser.
278 this is done in stages. note that this could take a while,
279 particularly on a broadcast subnet, as we have to wait for
280 the implicit registration of each name to be accepted.
282 as each name is successfully registered, become_local_master() is
283 called again, in order to initiate the next stage. see
284 dead_netbios_entry() - deals with implicit name registration
285 and response_name_reg() - deals with explicit registration
288 stage 1: was MST_POTENTIAL - go to MST_POTENTIAL and register ^1^2__MSBROWSE__^2^1.
289 stage 2: was MST_BACK - go to MST_MSB and register WORKGROUP(0x1d)
290 stage 3: was MST_MSB - go to MST_BROWSER and stay there
292 XXXX note: this code still does not cope with the distinction
293 between different types of nodes, particularly between M and P
294 nodes. that comes later.
296 ******************************************************************/
297 void become_local_master(struct subnet_record *d, struct work_record *work)
299 /* domain type must be limited to domain enum + server type. it must
300 not have SV_TYPE_SERVER or anything else with SERVER in it, else
301 clients get confused and start thinking this entry is a server
304 uint32 domain_type = SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT;
306 if (!work || !d) return;
308 DEBUG(2,("Becoming master for %s %s (currently at stage %d)\n",
309 work->work_group,inet_ntoa(d->bcast_ip),work->mst_state));
311 switch (work->mst_state)
313 case MST_POTENTIAL: /* while we were nothing but a server... */
315 DEBUG(3,("go to first stage: register ^1^2__MSBROWSE__^2^1\n"));
316 work->mst_state = MST_BACK; /* ... an election win was successful */
318 work->ElectionCriterion |= 0x5;
320 /* update our server status */
321 work->ServerType &= ~SV_TYPE_POTENTIAL_BROWSER;
322 add_server_entry(d,work,myname,work->ServerType,0,lp_serverstring(),True);
324 /* add special browser name */
325 add_my_name_entry(d,MSBROWSE ,0x01,nb_type|NB_ACTIVE|NB_GROUP);
327 /* DON'T do anything else after calling add_my_name_entry() */
331 case MST_BACK: /* while nothing had happened except we won an election... */
333 DEBUG(3,("go to second stage: register as master browser\n"));
334 work->mst_state = MST_MSB; /* ... registering MSBROWSE was successful */
336 /* add server entry on successful registration of MSBROWSE */
337 add_server_entry(d,work,work->work_group,domain_type,0,myname,True);
339 /* add master name */
340 add_my_name_entry(d,work->work_group,0x1d,nb_type|NB_ACTIVE);
342 /* DON'T do anything else after calling add_my_name_entry() */
346 case MST_MSB: /* while we were still only registered MSBROWSE state... */
348 DEBUG(3,("2nd stage complete: registered as master browser\n"));
349 work->mst_state = MST_BROWSER; /* ... registering WORKGROUP(1d) succeeded */
351 /* update our server status */
352 work->ServerType |= SV_TYPE_MASTER_BROWSER;
353 add_server_entry(d,work,myname,work->ServerType,0,lp_serverstring(),True);
355 if (work->serverlist == NULL) /* no servers! */
357 /* ask all servers on our local net to announce to us */
358 /* XXXX OOPS! add_server_entry will always add one entry - our own. */
359 announce_request(work, d->bcast_ip);
366 /* don't have to do anything: just report success */
367 DEBUG(3,("3rd stage: become master browser!\n"));
375 /*******************************************************************
376 become the domain master browser.
378 this is done in stages. note that this could take a while,
379 particularly on a broadcast subnet, as we have to wait for
380 the implicit registration of each name to be accepted.
382 as each name is successfully registered, become_domain_master() is
383 called again, in order to initiate the next stage. see
384 dead_netbios_entry() - deals with implicit name registration
385 and response_name_reg() - deals with explicit registration
388 stage 1: was DOMAIN_NONE - go to DOMAIN_MST
390 XXXX note: this code still does not cope with the distinction
391 between different types of nodes, particularly between M and P
392 nodes. that comes later.
394 ******************************************************************/
395 void become_domain_master(struct subnet_record *d, struct work_record *work)
397 /* domain type must be limited to domain enum + server type. it must
398 not have SV_TYPE_SERVER or anything else with SERVER in it, else
399 clients get confused and start thinking this entry is a server
403 if ((!work) || (!d)) return;
405 DEBUG(2,("Becoming domain master for %s %s (currently at stage %d)\n",
406 work->work_group,inet_ntoa(d->bcast_ip),work->dom_state));
408 switch (work->dom_state)
410 case DOMAIN_NONE: /* while we were nothing but a server... */
412 if (lp_domain_master())
414 DEBUG(3,("go to first stage: register <1b> name\n"));
415 work->dom_state = DOMAIN_WAIT;
417 /* XXXX the 0x1b is domain master browser name */
418 add_my_name_entry(d, lp_workgroup(),0x1b,nb_type|NB_ACTIVE|NB_GROUP);
420 /* DON'T do anything else after calling add_my_name_entry() */
425 DEBUG(4,("samba not configured as a domain master.\n"));
433 if (lp_domain_master())
435 work->dom_state = DOMAIN_MST; /* ... become domain master */
436 DEBUG(3,("domain first stage: register as domain member\n"));
438 /* update our server status */
439 work->ServerType |= SV_TYPE_NT|SV_TYPE_DOMAIN_MASTER;
440 add_server_entry(d,work,myname,work->ServerType,0,
441 lp_serverstring(),True);
443 DEBUG(4,("samba is now a domain master\n"));
449 DEBUG(4,("samba not configured as a domain master.\n"));
457 /* don't have to do anything: just report success */
458 DEBUG(3,("domain second stage: there isn't one!\n"));
465 /*******************************************************************
466 become a logon server.
467 ******************************************************************/
468 void become_logon_server(struct subnet_record *d, struct work_record *work)
470 if (!work || !d) return;
472 DEBUG(2,("Becoming logon server for %s %s (currently at stage %d)\n",
473 work->work_group,inet_ntoa(d->bcast_ip),work->log_state));
475 switch (work->log_state)
477 case LOGON_NONE: /* while we were nothing but a server... */
479 if (lp_domain_logons())
481 DEBUG(3,("go to first stage: register <1c> name\n"));
482 work->log_state = LOGON_WAIT;
484 /* XXXX the 0x1c is apparently something to do with domain logons */
485 add_my_name_entry(d, lp_workgroup(),0x1c,nb_type|NB_ACTIVE|NB_GROUP);
487 /* DON'T do anything else after calling add_my_name_entry() */
491 DEBUG(4,("samba not configured as a logon master.\n"));
499 if (lp_domain_logons())
501 work->log_state = LOGON_SRV; /* ... become logon server */
502 DEBUG(3,("logon second stage: register \n"));
504 /* update our server status */
505 work->ServerType |= SV_TYPE_NT|SV_TYPE_DOMAIN_MEMBER;
506 add_server_entry(d,work,myname,work->ServerType,0,
507 lp_serverstring(),True);
509 /* DON'T do anything else after calling add_my_name_entry() */
514 DEBUG(4,("samba not configured as a logon server.\n"));
522 DEBUG(3,("logon third stage: there isn't one!\n"));
530 /*******************************************************************
531 unbecome the local master browser. initates removal of necessary netbios
532 names, and tells the world that we are no longer a master browser.
534 XXXX this _should_ be used to demote to a backup master browser, without
535 going straight to non-master browser. another time.
537 ******************************************************************/
538 void unbecome_local_master(struct subnet_record *d, struct work_record *work,
541 int new_server_type = work->ServerType;
543 /* can only remove master types with this function */
544 remove_type &= SV_TYPE_MASTER_BROWSER;
546 new_server_type &= ~remove_type;
550 DEBUG(2,("Becoming local non-master for %s\n",work->work_group));
552 /* no longer a master browser of any sort */
554 work->ServerType |= SV_TYPE_POTENTIAL_BROWSER;
555 work->ElectionCriterion &= ~0x4;
556 work->mst_state = MST_POTENTIAL;
558 /* announce ourselves as no longer active as a master browser. */
559 announce_server(d, work, work->work_group, myname, 0, 0);
560 remove_name_entry(d,MSBROWSE ,0x01);
561 remove_name_entry(d,work->work_group,0x1d);
566 /*******************************************************************
567 unbecome the domain master browser. initates removal of necessary netbios
568 names, and tells the world that we are no longer a domain browser.
569 ******************************************************************/
570 void unbecome_domain_master(struct subnet_record *d, struct work_record *work,
573 int new_server_type = work->ServerType;
575 DEBUG(2,("Becoming domain non-master for %s\n",work->work_group));
577 /* can only remove master or domain types with this function */
578 remove_type &= SV_TYPE_DOMAIN_MASTER;
580 new_server_type &= ~remove_type;
584 /* no longer a domain master browser of any sort */
586 work->dom_state = DOMAIN_NONE;
588 /* announce ourselves as no longer active as a master browser. */
589 announce_server(d, work, work->work_group, myname, 0, 0);
590 remove_name_entry(d,work->work_group,0x1b);
595 /*******************************************************************
596 unbecome the logon server. initates removal of necessary netbios
597 names, and tells the world that we are no longer a logon server.
598 ******************************************************************/
599 void unbecome_logon_server(struct subnet_record *d, struct work_record *work,
602 int new_server_type = work->ServerType;
604 DEBUG(2,("Becoming logon non-server for %s\n",work->work_group));
606 /* can only remove master or domain types with this function */
607 remove_type &= SV_TYPE_DOMAIN_MEMBER;
609 new_server_type &= ~remove_type;
613 /* no longer a master browser of any sort */
615 work->log_state = LOGON_NONE;
617 /* announce ourselves as no longer active as a master browser. */
618 announce_server(d, work, work->work_group, myname, 0, 0);
619 remove_name_entry(d,work->work_group,0x1c);
624 /*******************************************************************
626 ******************************************************************/
627 void run_elections(time_t t)
629 static time_t lastime = 0;
631 struct subnet_record *d;
633 /* send election packets once a second */
634 if (lastime && t-lastime <= 0) return;
638 for (d = subnetlist; d; d = d->next)
640 struct work_record *work;
641 for (work = d->workgrouplist; work; work = work->next)
643 if (work->RunningElection)
645 send_election(d,work->work_group, work->ElectionCriterion,
646 t-StartupTime,myname);
648 if (work->ElectionCount++ >= 4)
650 /* I won! now what :-) */
651 DEBUG(2,(">>> Won election on %s %s <<<\n",
652 work->work_group,inet_ntoa(d->bcast_ip)));
654 work->RunningElection = False;
655 work->mst_state = MST_POTENTIAL;
657 become_local_master(d, work);
665 /*******************************************************************
666 work out if I win an election
667 ******************************************************************/
668 static BOOL win_election(struct work_record *work,int version,uint32 criterion,
669 int timeup,char *name)
671 int mytimeup = time(NULL) - StartupTime;
672 uint32 mycriterion = work->ElectionCriterion;
674 DEBUG(4,("election comparison: %x:%x %x:%x %d:%d %s:%s\n",
675 version,ELECTION_VERSION,
676 criterion,mycriterion,
680 if (version > ELECTION_VERSION) return(False);
681 if (version < ELECTION_VERSION) return(True);
683 if (criterion > mycriterion) return(False);
684 if (criterion < mycriterion) return(True);
686 if (timeup > mytimeup) return(False);
687 if (timeup < mytimeup) return(True);
689 if (strcasecmp(myname,name) > 0) return(False);
695 /*******************************************************************
696 process a election packet
698 An election dynamically decides who will be the master.
699 ******************************************************************/
700 void process_election(struct packet_struct *p,char *buf)
702 struct dgram_packet *dgram = &p->packet.dgram;
703 struct in_addr ip = dgram->header.source_ip;
704 struct subnet_record *d = find_subnet(ip);
705 int version = CVAL(buf,0);
706 uint32 criterion = IVAL(buf,1);
707 int timeup = IVAL(buf,5)/1000;
709 struct work_record *work;
713 if (ip_equal(d->bcast_ip,ipgrp)) {
714 DEBUG(3,("Unexpected election request from %s %s on WINS net\n",
715 name, inet_ntoa(p->ip)));
721 DEBUG(3,("Election request from %s %s vers=%d criterion=%08x timeup=%d\n",
722 name,inet_ntoa(p->ip),version,criterion,timeup));
724 if (same_context(dgram)) return;
726 for (work = d->workgrouplist; work; work = work->next)
728 if (!strequal(work->work_group, lp_workgroup()))
731 if (win_election(work, version,criterion,timeup,name)) {
732 if (!work->RunningElection) {
733 work->needelection = True;
734 work->ElectionCount=0;
735 work->mst_state = MST_POTENTIAL;
738 work->needelection = False;
740 if (work->RunningElection || AM_MASTER(work)) {
741 work->RunningElection = False;
742 DEBUG(3,(">>> Lost election on %s %s <<<\n",
743 work->work_group,inet_ntoa(d->bcast_ip)));
745 unbecome_local_master(d, work, SV_TYPE_MASTER_BROWSER);
752 /****************************************************************************
753 checks whether a browser election is to be run on any workgroup
755 this function really ought to return the time between election
756 packets (which depends on whether samba intends to be a domain
757 master or a master browser) in milliseconds.
759 ***************************************************************************/
760 BOOL check_elections(void)
762 struct subnet_record *d;
763 BOOL run_any_election = False;
765 for (d = subnetlist; d; d = d->next)
767 struct work_record *work;
768 for (work = d->workgrouplist; work; work = work->next)
770 run_any_election |= work->RunningElection;
772 if (work->needelection && !work->RunningElection)
774 DEBUG(3,(">>> Starting election on %s %s <<<\n",
775 work->work_group,inet_ntoa(d->bcast_ip)));
776 work->ElectionCount = 0;
777 work->RunningElection = True;
778 work->needelection = False;
782 return run_any_election;