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