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