vfstest: Convert cmd_lstat to synthetic_smb_fname
[Samba/id10ts.git] / source3 / nmbd / nmbd_elections.c
blob1947e37e3afeb0bbac0272408eaace56d35504c8
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"
24 #include "nmbd/nmbd.h"
25 #include "smbprofile.h"
27 /* Election parameters. */
28 extern time_t StartupTime;
30 /****************************************************************************
31 Send an election datagram packet.
32 **************************************************************************/
34 static void send_election_dgram(struct subnet_record *subrec, const char *workgroup_name,
35 uint32 criterion, int timeup,const char *server_name)
37 char outbuf[1024];
38 unstring srv_name;
39 char *p;
41 DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
42 workgroup_name, subrec->subnet_name ));
44 memset(outbuf,'\0',sizeof(outbuf));
45 p = outbuf;
46 SCVAL(p,0,ANN_Election); /* Election opcode. */
47 p++;
49 SCVAL(p,0,((criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION));
50 SIVAL(p,1,criterion);
51 SIVAL(p,5,timeup*1000); /* ms - Despite what the spec says. */
52 p += 13;
53 unstrcpy(srv_name, server_name);
54 if (!strupper_m(srv_name)) {
55 DEBUG(2,("strupper_m failed for %s\n", srv_name));
56 return;
58 /* The following call does UNIX -> DOS charset conversion. */
59 push_ascii(p, srv_name, sizeof(outbuf)-PTR_DIFF(p,outbuf)-1, STR_TERMINATE);
60 p = skip_string(outbuf,sizeof(outbuf),p);
62 send_mailslot(False, BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
63 lp_netbios_name(), 0,
64 workgroup_name, 0x1e,
65 subrec->bcast_ip, subrec->myip, DGRAM_PORT);
68 /*******************************************************************
69 We found a current master browser on one of our broadcast interfaces.
70 ******************************************************************/
72 static void check_for_master_browser_success(struct subnet_record *subrec,
73 struct userdata_struct *userdata,
74 struct nmb_name *answer_name,
75 struct in_addr answer_ip, struct res_rec *rrec)
77 unstring aname;
78 pull_ascii_nstring(aname, sizeof(aname), answer_name->name);
79 DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
80 IP %s (just checking).\n", aname, inet_ntoa(answer_ip) ));
83 /*******************************************************************
84 We failed to find a current master browser on one of our broadcast interfaces.
85 ******************************************************************/
87 static void check_for_master_browser_fail( struct subnet_record *subrec,
88 struct response_record *rrec,
89 struct nmb_name *question_name,
90 int fail_code)
92 unstring workgroup_name;
93 struct work_record *work;
95 pull_ascii_nstring(workgroup_name,sizeof(workgroup_name),question_name->name);
97 work = find_workgroup_on_subnet(subrec, workgroup_name);
98 if(work == NULL) {
99 DEBUG(0,("check_for_master_browser_fail: Unable to find workgroup %s on subnet %s.=\n",
100 workgroup_name, subrec->subnet_name ));
101 return;
104 if (strequal(work->work_group, lp_workgroup())) {
106 if (lp_local_master()) {
107 /* We have discovered that there is no local master
108 browser, and we are configured to initiate
109 an election that we will participate in.
111 DEBUG(2,("check_for_master_browser_fail: Forcing election on workgroup %s subnet %s\n",
112 work->work_group, subrec->subnet_name ));
114 /* Setting this means we will participate when the
115 election is run in run_elections(). */
116 work->needelection = True;
117 } else {
118 /* We need to force an election, because we are configured
119 not to become the local master, but we still need one,
120 having detected that one doesn't exist.
122 send_election_dgram(subrec, work->work_group, 0, 0, "");
127 /*******************************************************************
128 Ensure there is a local master browser for a workgroup on our
129 broadcast interfaces.
130 ******************************************************************/
132 void check_master_browser_exists(time_t t)
134 static time_t lastrun=0;
135 struct subnet_record *subrec;
136 const char *workgroup_name = lp_workgroup();
138 if (t < (lastrun + (CHECK_TIME_MST_BROWSE * 60)))
139 return;
141 lastrun = t;
143 dump_workgroups(False);
145 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
146 struct work_record *work;
148 for (work = subrec->workgrouplist; work; work = work->next) {
149 if (strequal(work->work_group, workgroup_name) && !AM_LOCAL_MASTER_BROWSER(work)) {
150 /* Do a name query for the local master browser on this net. */
151 query_name( subrec, work->work_group, 0x1d,
152 check_for_master_browser_success,
153 check_for_master_browser_fail,
154 NULL);
160 /*******************************************************************
161 Run an election.
162 ******************************************************************/
164 void run_elections(time_t t)
166 static time_t lastime = 0;
168 struct subnet_record *subrec;
170 START_PROFILE(run_elections);
172 /* Send election packets once every 2 seconds - note */
173 if (lastime && (t - lastime < 2)) {
174 END_PROFILE(run_elections);
175 return;
178 lastime = t;
180 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
181 struct work_record *work;
183 for (work = subrec->workgrouplist; work; work = work->next) {
184 if (work->RunningElection) {
186 * We can only run an election for a workgroup if we have
187 * registered the WORKGROUP<1e> name, as that's the name
188 * we must listen to.
190 struct nmb_name nmbname;
192 make_nmb_name(&nmbname, work->work_group, 0x1e);
193 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
194 DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
195 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
196 continue;
199 send_election_dgram(subrec, work->work_group, work->ElectionCriterion,
200 t - StartupTime, lp_netbios_name());
202 if (work->ElectionCount++ >= 4) {
203 /* Won election (4 packets were sent out uncontested. */
204 DEBUG(2,("run_elections: >>> Won election for workgroup %s on subnet %s <<<\n",
205 work->work_group, subrec->subnet_name ));
207 work->RunningElection = False;
209 become_local_master_browser(subrec, work);
214 END_PROFILE(run_elections);
217 /*******************************************************************
218 Determine if I win an election.
219 ******************************************************************/
221 static bool win_election(struct work_record *work, int version,
222 uint32 criterion, int timeup, const char *server_name)
224 int mytimeup = time(NULL) - StartupTime;
225 uint32 mycriterion = work->ElectionCriterion;
227 /* If local master is false then never win in election broadcasts. */
228 if(!lp_local_master()) {
229 DEBUG(3,("win_election: Losing election as local master == False\n"));
230 return False;
233 DEBUG(4,("win_election: election comparison: %x:%x %x:%x %d:%d %s:%s\n",
234 version, ELECTION_VERSION,
235 criterion, mycriterion,
236 timeup, mytimeup,
237 server_name, lp_netbios_name()));
239 if (version > ELECTION_VERSION)
240 return(False);
241 if (version < ELECTION_VERSION)
242 return(True);
244 if (criterion > mycriterion)
245 return(False);
246 if (criterion < mycriterion)
247 return(True);
249 if (timeup > mytimeup)
250 return(False);
251 if (timeup < mytimeup)
252 return(True);
254 if (strcasecmp_m(lp_netbios_name(), server_name) > 0)
255 return(False);
257 return(True);
260 /*******************************************************************
261 Process an incoming election datagram packet.
262 ******************************************************************/
264 void process_election(struct subnet_record *subrec, struct packet_struct *p, const char *buf)
266 struct dgram_packet *dgram = &p->packet.dgram;
267 int version = CVAL(buf,0);
268 uint32 criterion = IVAL(buf,1);
269 int timeup = IVAL(buf,5)/1000;
270 unstring server_name;
271 struct work_record *work;
272 unstring workgroup_name;
274 START_PROFILE(election);
276 pull_ascii_nstring(server_name, sizeof(server_name), buf+13);
277 pull_ascii_nstring(workgroup_name, sizeof(workgroup_name), dgram->dest_name.name);
279 server_name[15] = 0;
281 DEBUG(3,("process_election: Election request from %s at IP %s on subnet %s for workgroup %s.\n",
282 server_name,inet_ntoa(p->ip), subrec->subnet_name, workgroup_name ));
284 DEBUG(5,("process_election: vers=%d criterion=%08x timeup=%d\n", version,criterion,timeup));
286 if(( work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL) {
287 DEBUG(0,("process_election: Cannot find workgroup %s on subnet %s.\n",
288 workgroup_name, subrec->subnet_name ));
289 goto done;
292 if (!strequal(work->work_group, lp_workgroup())) {
293 DEBUG(3,("process_election: ignoring election request for workgroup %s on subnet %s as this \
294 is not my workgroup.\n", work->work_group, subrec->subnet_name ));
295 goto done;
298 if (win_election(work, version,criterion,timeup,server_name)) {
299 /* We take precedence over the requesting server. */
300 if (!work->RunningElection) {
301 /* We weren't running an election - start running one. */
303 work->needelection = True;
304 work->ElectionCount=0;
307 /* Note that if we were running an election for this workgroup on this
308 subnet already, we just ignore the server we take precedence over. */
309 } else {
310 /* We lost. Stop participating. */
311 work->needelection = False;
313 if (work->RunningElection || AM_LOCAL_MASTER_BROWSER(work)) {
314 work->RunningElection = False;
315 DEBUG(3,("process_election: >>> Lost election for workgroup %s on subnet %s <<<\n",
316 work->work_group, subrec->subnet_name ));
317 if (AM_LOCAL_MASTER_BROWSER(work))
318 unbecome_local_master_browser(subrec, work, False);
321 done:
323 END_PROFILE(election);
326 /****************************************************************************
327 This function looks over all the workgroups known on all the broadcast
328 subnets and decides if a browser election is to be run on that workgroup.
329 It returns True if any election packets need to be sent (this will then
330 be done by run_elections().
331 ***************************************************************************/
333 bool check_elections(void)
335 struct subnet_record *subrec;
336 bool run_any_election = False;
338 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
339 struct work_record *work;
340 for (work = subrec->workgrouplist; work; work = work->next) {
341 if (work->RunningElection) {
342 run_any_election = work->RunningElection;
346 * Start an election if we have any chance of winning.
347 * Note this is a change to the previous code, that would
348 * only run an election if nmbd was in the potential browser
349 * state. We need to run elections in any state if we're told
350 * to. JRA.
353 if (work->needelection && !work->RunningElection && lp_local_master()) {
355 * We can only run an election for a workgroup if we have
356 * registered the WORKGROUP<1e> name, as that's the name
357 * we must listen to.
359 struct nmb_name nmbname;
361 make_nmb_name(&nmbname, work->work_group, 0x1e);
362 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
363 DEBUG(8,("check_elections: Cannot send election packet yet as name %s not \
364 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
365 continue;
368 DEBUG(3,("check_elections: >>> Starting election for workgroup %s on subnet %s <<<\n",
369 work->work_group, subrec->subnet_name ));
371 work->ElectionCount = 0;
372 work->RunningElection = True;
373 work->needelection = False;
377 return run_any_election;
380 /****************************************************************************
381 Process a internal Samba message forcing an election.
382 ***************************************************************************/
384 void nmbd_message_election(struct messaging_context *msg,
385 void *private_data,
386 uint32_t msg_type,
387 struct server_id server_id,
388 DATA_BLOB *data)
390 struct subnet_record *subrec;
392 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
393 struct work_record *work;
394 for (work = subrec->workgrouplist; work; work = work->next) {
395 if (strequal(work->work_group, lp_workgroup())) {
396 work->needelection = True;
397 work->ElectionCount=0;
398 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;