s3:libads/ldap.c: store the dc name in the saf cache as in all other places
[Samba.git] / source / nmbd / nmbd_elections.c
blobb50d215b91d57c37b5287ceb9f076782c932561a
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-2003
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
25 /* Election parameters. */
26 extern time_t StartupTime;
28 /****************************************************************************
29 Send an election datagram packet.
30 **************************************************************************/
32 static void send_election_dgram(struct subnet_record *subrec, const char *workgroup_name,
33 uint32 criterion, int timeup,const char *server_name)
35 char outbuf[1024];
36 unstring srv_name;
37 char *p;
39 DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
40 workgroup_name, subrec->subnet_name ));
42 memset(outbuf,'\0',sizeof(outbuf));
43 p = outbuf;
44 SCVAL(p,0,ANN_Election); /* Election opcode. */
45 p++;
47 SCVAL(p,0,((criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION));
48 SIVAL(p,1,criterion);
49 SIVAL(p,5,timeup*1000); /* ms - Despite what the spec says. */
50 p += 13;
51 unstrcpy(srv_name, server_name);
52 strupper_m(srv_name);
53 /* The following call does UNIX -> DOS charset conversion. */
54 push_ascii(p, srv_name, sizeof(outbuf)-PTR_DIFF(p,outbuf)-1, STR_TERMINATE);
55 p = skip_string(outbuf,sizeof(outbuf),p);
57 send_mailslot(False, BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
58 global_myname(), 0,
59 workgroup_name, 0x1e,
60 subrec->bcast_ip, subrec->myip, DGRAM_PORT);
63 /*******************************************************************
64 We found a current master browser on one of our broadcast interfaces.
65 ******************************************************************/
67 static void check_for_master_browser_success(struct subnet_record *subrec,
68 struct userdata_struct *userdata,
69 struct nmb_name *answer_name,
70 struct in_addr answer_ip, struct res_rec *rrec)
72 unstring aname;
73 pull_ascii_nstring(aname, sizeof(aname), answer_name->name);
74 DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
75 IP %s (just checking).\n", aname, inet_ntoa(answer_ip) ));
78 /*******************************************************************
79 We failed to find a current master browser on one of our broadcast interfaces.
80 ******************************************************************/
82 static void check_for_master_browser_fail( struct subnet_record *subrec,
83 struct response_record *rrec,
84 struct nmb_name *question_name,
85 int fail_code)
87 unstring workgroup_name;
88 struct work_record *work;
90 pull_ascii_nstring(workgroup_name,sizeof(workgroup_name),question_name->name);
92 work = find_workgroup_on_subnet(subrec, workgroup_name);
93 if(work == NULL) {
94 DEBUG(0,("check_for_master_browser_fail: Unable to find workgroup %s on subnet %s.=\n",
95 workgroup_name, subrec->subnet_name ));
96 return;
99 if (strequal(work->work_group, lp_workgroup())) {
101 if (lp_local_master()) {
102 /* We have discovered that there is no local master
103 browser, and we are configured to initiate
104 an election that we will participate in.
106 DEBUG(2,("check_for_master_browser_fail: Forcing election on workgroup %s subnet %s\n",
107 work->work_group, subrec->subnet_name ));
109 /* Setting this means we will participate when the
110 election is run in run_elections(). */
111 work->needelection = True;
112 } else {
113 /* We need to force an election, because we are configured
114 not to become the local master, but we still need one,
115 having detected that one doesn't exist.
117 send_election_dgram(subrec, work->work_group, 0, 0, "");
122 /*******************************************************************
123 Ensure there is a local master browser for a workgroup on our
124 broadcast interfaces.
125 ******************************************************************/
127 void check_master_browser_exists(time_t t)
129 static time_t lastrun=0;
130 struct subnet_record *subrec;
131 const char *workgroup_name = lp_workgroup();
133 if (!lastrun)
134 lastrun = t;
136 if (t < (lastrun + (CHECK_TIME_MST_BROWSE * 60)))
137 return;
139 lastrun = t;
141 dump_workgroups(False);
143 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
144 struct work_record *work;
146 for (work = subrec->workgrouplist; work; work = work->next) {
147 if (strequal(work->work_group, workgroup_name) && !AM_LOCAL_MASTER_BROWSER(work)) {
148 /* Do a name query for the local master browser on this net. */
149 query_name( subrec, work->work_group, 0x1d,
150 check_for_master_browser_success,
151 check_for_master_browser_fail,
152 NULL);
158 /*******************************************************************
159 Run an election.
160 ******************************************************************/
162 void run_elections(time_t t)
164 static time_t lastime = 0;
166 struct subnet_record *subrec;
168 START_PROFILE(run_elections);
170 /* Send election packets once every 2 seconds - note */
171 if (lastime && (t - lastime < 2)) {
172 END_PROFILE(run_elections);
173 return;
176 lastime = t;
178 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
179 struct work_record *work;
181 for (work = subrec->workgrouplist; work; work = work->next) {
182 if (work->RunningElection) {
184 * We can only run an election for a workgroup if we have
185 * registered the WORKGROUP<1e> name, as that's the name
186 * we must listen to.
188 struct nmb_name nmbname;
190 make_nmb_name(&nmbname, work->work_group, 0x1e);
191 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
192 DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
193 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
194 continue;
197 send_election_dgram(subrec, work->work_group, work->ElectionCriterion,
198 t - StartupTime, global_myname());
200 if (work->ElectionCount++ >= 4) {
201 /* Won election (4 packets were sent out uncontested. */
202 DEBUG(2,("run_elections: >>> Won election for workgroup %s on subnet %s <<<\n",
203 work->work_group, subrec->subnet_name ));
205 work->RunningElection = False;
207 become_local_master_browser(subrec, work);
212 END_PROFILE(run_elections);
215 /*******************************************************************
216 Determine if I win an election.
217 ******************************************************************/
219 static bool win_election(struct work_record *work, int version,
220 uint32 criterion, int timeup, const char *server_name)
222 int mytimeup = time(NULL) - StartupTime;
223 uint32 mycriterion = work->ElectionCriterion;
225 /* If local master is false then never win in election broadcasts. */
226 if(!lp_local_master()) {
227 DEBUG(3,("win_election: Losing election as local master == False\n"));
228 return False;
231 DEBUG(4,("win_election: election comparison: %x:%x %x:%x %d:%d %s:%s\n",
232 version, ELECTION_VERSION,
233 criterion, mycriterion,
234 timeup, mytimeup,
235 server_name, global_myname()));
237 if (version > ELECTION_VERSION)
238 return(False);
239 if (version < ELECTION_VERSION)
240 return(True);
242 if (criterion > mycriterion)
243 return(False);
244 if (criterion < mycriterion)
245 return(True);
247 if (timeup > mytimeup)
248 return(False);
249 if (timeup < mytimeup)
250 return(True);
252 if (StrCaseCmp(global_myname(), server_name) > 0)
253 return(False);
255 return(True);
258 /*******************************************************************
259 Process an incoming election datagram packet.
260 ******************************************************************/
262 void process_election(struct subnet_record *subrec, struct packet_struct *p, char *buf)
264 struct dgram_packet *dgram = &p->packet.dgram;
265 int version = CVAL(buf,0);
266 uint32 criterion = IVAL(buf,1);
267 int timeup = IVAL(buf,5)/1000;
268 unstring server_name;
269 struct work_record *work;
270 unstring workgroup_name;
272 START_PROFILE(election);
274 pull_ascii_nstring(server_name, sizeof(server_name), buf+13);
275 pull_ascii_nstring(workgroup_name, sizeof(workgroup_name), dgram->dest_name.name);
277 server_name[15] = 0;
279 DEBUG(3,("process_election: Election request from %s at IP %s on subnet %s for workgroup %s.\n",
280 server_name,inet_ntoa(p->ip), subrec->subnet_name, workgroup_name ));
282 DEBUG(5,("process_election: vers=%d criterion=%08x timeup=%d\n", version,criterion,timeup));
284 if(( work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL) {
285 DEBUG(0,("process_election: Cannot find workgroup %s on subnet %s.\n",
286 workgroup_name, subrec->subnet_name ));
287 goto done;
290 if (!strequal(work->work_group, lp_workgroup())) {
291 DEBUG(3,("process_election: ignoring election request for workgroup %s on subnet %s as this \
292 is not my workgroup.\n", work->work_group, subrec->subnet_name ));
293 goto done;
296 if (win_election(work, version,criterion,timeup,server_name)) {
297 /* We take precedence over the requesting server. */
298 if (!work->RunningElection) {
299 /* We weren't running an election - start running one. */
301 work->needelection = True;
302 work->ElectionCount=0;
305 /* Note that if we were running an election for this workgroup on this
306 subnet already, we just ignore the server we take precedence over. */
307 } else {
308 /* We lost. Stop participating. */
309 work->needelection = False;
311 if (work->RunningElection || AM_LOCAL_MASTER_BROWSER(work)) {
312 work->RunningElection = False;
313 DEBUG(3,("process_election: >>> Lost election for workgroup %s on subnet %s <<<\n",
314 work->work_group, subrec->subnet_name ));
315 if (AM_LOCAL_MASTER_BROWSER(work))
316 unbecome_local_master_browser(subrec, work, False);
319 done:
321 END_PROFILE(election);
324 /****************************************************************************
325 This function looks over all the workgroups known on all the broadcast
326 subnets and decides if a browser election is to be run on that workgroup.
327 It returns True if any election packets need to be sent (this will then
328 be done by run_elections().
329 ***************************************************************************/
331 bool check_elections(void)
333 struct subnet_record *subrec;
334 bool run_any_election = False;
336 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
337 struct work_record *work;
338 for (work = subrec->workgrouplist; work; work = work->next) {
339 if (work->RunningElection) {
340 run_any_election = work->RunningElection;
344 * Start an election if we have any chance of winning.
345 * Note this is a change to the previous code, that would
346 * only run an election if nmbd was in the potential browser
347 * state. We need to run elections in any state if we're told
348 * to. JRA.
351 if (work->needelection && !work->RunningElection && lp_local_master()) {
353 * We can only run an election for a workgroup if we have
354 * registered the WORKGROUP<1e> name, as that's the name
355 * we must listen to.
357 struct nmb_name nmbname;
359 make_nmb_name(&nmbname, work->work_group, 0x1e);
360 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
361 DEBUG(8,("check_elections: Cannot send election packet yet as name %s not \
362 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
363 continue;
366 DEBUG(3,("check_elections: >>> Starting election for workgroup %s on subnet %s <<<\n",
367 work->work_group, subrec->subnet_name ));
369 work->ElectionCount = 0;
370 work->RunningElection = True;
371 work->needelection = False;
375 return run_any_election;
378 /****************************************************************************
379 Process a internal Samba message forcing an election.
380 ***************************************************************************/
382 void nmbd_message_election(struct messaging_context *msg,
383 void *private_data,
384 uint32_t msg_type,
385 struct server_id server_id,
386 DATA_BLOB *data)
388 struct subnet_record *subrec;
390 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
391 struct work_record *work;
392 for (work = subrec->workgrouplist; work; work = work->next) {
393 if (strequal(work->work_group, lp_workgroup())) {
394 work->needelection = True;
395 work->ElectionCount=0;
396 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;