namepacket.c: Block SIGTERM correctly - we can only take them at defined points.
[samba.git] / source3 / nameserv.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios routines and daemon - version 2
5    Copyright (C) Andrew Tridgell 1994-1997
6    
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.
11    
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.
16    
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.
20    
21    Module name: nameserv.c
22
23    Revision History:
24
25    14 jan 96: lkcl@pires.co.uk
26    added multiple workgroup domain master support
27
28    04 jul 96: lkcl@pires.co.uk
29    module nameserv contains name server management functions
30 */
31
32 #include "includes.h"
33
34 extern int ClientNMB;
35
36 extern int DEBUGLEVEL;
37
38 extern pstring scope;
39 extern pstring myname;
40 extern fstring myworkgroup;
41 extern struct in_addr ipzero;
42 extern struct in_addr wins_ip;
43
44 extern struct subnet_record *subnetlist;
45
46 extern uint16 nb_type; /* samba's NetBIOS type */
47
48 /****************************************************************************
49   remove an entry from the name list
50
51   note: the name will _always_ be removed
52   XXXX at present, the name is removed _even_ if a WINS server says keep it.
53
54   If direct is True then the name being removed must have been a direct name
55   add. This is done for special names such as DOMAIN<1b>. Just delete it
56   without any network release traffic.
57
58   ****************************************************************************/
59 void remove_name_entry(struct subnet_record *d, char *name,int type, BOOL direct)
60 {
61   /* XXXX BUG: if samba is offering WINS support, it should still broadcast
62       a de-registration packet to the local subnet before removing the
63       name from its local-subnet name database. */
64
65   int search = FIND_SELF;
66   struct name_record n;
67   struct name_record *n2=NULL;
68       
69   make_nmb_name(&n.name,name,type,scope);
70
71   if(d == wins_subnet)
72     search |= FIND_WINS;
73   else
74     search |= FIND_LOCAL;
75
76   if ((n2 = find_name_search(&d, &n.name, search, ipzero)))
77   {
78     /* check name isn't already being de-registered */
79     if (NAME_DEREG(n2->ip_flgs[0].nb_flags))
80       return;
81
82     /* mark the name as in the process of deletion. */
83     n2->ip_flgs[0].nb_flags &= NB_DEREG;
84   }
85
86   if (!n2) return;
87
88   /* Only remove names with non-zero death times. */
89   if(n2->death_time == 0)
90   {
91     DEBUG(5,("remove_name_entry: Name %s(%d) has zero ttl - not removing.\n",
92              name, type));
93     return;
94   }
95
96   /* remove the name immediately. even if the spec says we should
97      first try to release them, this is too dangerous with our current
98      name structures as otherwise we will end up replying to names we
99      don't really own */  
100   remove_netbios_name(d,name,type,SELF,n2->ip_flgs[0].ip);
101
102   if (ip_equal(d->bcast_ip, wins_ip))
103   {
104     if (!lp_wins_support() && !direct)
105     {
106       /* not a WINS server: we have to release them on the network */
107       queue_netbios_pkt_wins(ClientNMB,NMB_REL,NAME_RELEASE,
108                              name, type, 0, 0,0,NULL,NULL,
109                              False, True, ipzero, ipzero);
110     }
111   }
112   else
113   {
114     if(!direct)
115       /* local interface: release them on the network */
116       queue_netbios_packet(d,ClientNMB,NMB_REL,NAME_RELEASE,
117                          name, type, 0, 0,0,NULL,NULL,
118                          True, True, d->bcast_ip, d->bcast_ip);
119   }
120 }
121
122
123 /****************************************************************************
124   add an entry to the name list
125   If the direct BOOL is set then no network traffic is done for the add - it
126   is just blasted into the subnet entry with a zero TTL - it will not
127   expire and has not been legitimately claimed. This is *only* done if
128   we are a WINS server or for a special name such as DOMAIN<1b>.
129  
130   big note: our name will _always_ be added (if there are no objections).
131   it's just a matter of when this will be done (e.g after a time-out).
132
133   ****************************************************************************/
134 void add_my_name_entry(struct subnet_record *d,char *name,int type,int nb_flags, BOOL direct)
135 {
136   BOOL re_reg = False;
137   struct nmb_name n;
138
139   if (!d) return;
140
141   /* not that it particularly matters, but if the SELF name already exists,
142      it must be re-registered, rather than just registered */
143
144   make_nmb_name(&n, name, type, scope);
145   if (find_name(d->namelist, &n, SELF))
146         re_reg = True;
147
148   /* XXXX BUG: if samba is offering WINS support, it should still add the
149      name entry to a local-subnet name database. see rfc1001.txt 15.1.1 p28
150      regarding the point about M-nodes. */
151
152   if (ip_equal(d->bcast_ip, wins_ip))
153   {
154     if (lp_wins_support() || direct)
155     {
156       /* we are a WINS server. */
157       if(lp_wins_support())
158         DEBUG(4,("add_my_name_entry: samba as WINS server adding: "));
159       else
160         DEBUG(4,("add_my_name_entry: direct name entry : adding: "));
161         
162       /* this will call add_netbios_entry() */
163       name_register_work(d, name, type, nb_flags,0, ipzero, False);
164     }
165     else
166     {
167       DEBUG(4,("add_my_name_entry registering name %s with WINS server.\n",
168                 name));
169       
170       /* a time-to-live allows us to refresh this name with the WINS server. */
171           queue_netbios_pkt_wins(ClientNMB,
172                                  re_reg ? NMB_REG_REFRESH : NMB_REG, NAME_REGISTER,
173                              name, type, nb_flags, GET_TTL(0),0,NULL,NULL,
174                              False, True, ipzero, ipzero);
175     }
176   }
177   else
178   {
179     if(direct)
180     {
181       /* Just enter the name to be the ip address of the subnet
182          via name_register_work to ensure all side effects are done.
183        */
184       DEBUG(4,("add_my_name_entry: direct name entry : adding: "));
185       /* this will call add_netbios_entry() */
186       name_register_work(d, name, type, nb_flags,0, d->myip, False);
187     }
188     else
189     {
190       /* broadcast the packet, but it comes from ipzero */
191       queue_netbios_packet(d,ClientNMB,
192          re_reg ? NMB_REG_REFRESH : NMB_REG, NAME_REGISTER,
193          name, type, nb_flags, GET_TTL(0),0,NULL,NULL,
194          True, True, d->bcast_ip, ipzero);
195     }
196   }
197 }
198
199
200 /****************************************************************************
201   add the domain logon server and domain master browser names 
202
203   this code was written so that several samba servers can co-operate in
204   sharing the task of (one server) being a domain master, and of being
205   domain logon servers.
206
207   **************************************************************************/
208 void add_domain_names(time_t t)
209 {
210   static time_t lastrun = 0;
211   struct subnet_record *d;
212   struct work_record *work;
213   struct nmb_name n;
214
215   if (lastrun != 0 && t < lastrun + CHECK_TIME_ADD_DOM_NAMES * 60) return;
216   lastrun = t;
217
218   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
219   {
220     work = find_workgroupstruct(d, myworkgroup, False);
221     if (lp_domain_logons() && work && work->log_state == LOGON_NONE)
222     {
223       make_nmb_name(&n,myworkgroup,0x1c,scope);
224       if (!find_name(d->namelist, &n, FIND_SELF))
225       {
226         /* logon servers are group names - we don't expect this to fail. */
227         DEBUG(0,("%s attempting to become logon server for %s %s\n",
228              timestring(), myworkgroup, inet_ntoa(d->bcast_ip)));
229         become_logon_server(d, work);
230       }
231     }
232   }
233
234  for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
235  { 
236    work = find_workgroupstruct(d, myworkgroup, True);
237
238    if (lp_domain_master() && work && work->dom_state == DOMAIN_NONE)
239      {
240       make_nmb_name(&n,myworkgroup,0x1b,scope);
241       if (!find_name(d->namelist, &n, FIND_SELF))
242       {
243         DEBUG(0,("%s add_domain_names: attempting to become domain master \
244 browser on workgroup %s %s\n",
245                   timestring(), myworkgroup, inet_ntoa(d->bcast_ip)));
246
247         if(d == wins_subnet)
248         {
249           if (lp_wins_support())
250           {
251             /* use the wins server's capabilities (indirectly).  if
252                someone has already registered the domain<1b> name with 
253                the WINS server, then the WINS server's job is to _check_
254                that the owner still wants it, before giving it away.
255              */
256                
257             DEBUG(1,("%s initiating becoming domain master for %s\n",
258                         timestring(), myworkgroup));
259             become_domain_master(d, work);
260           }
261           else
262           {
263             /* send out a query to establish whether there's a 
264                domain controller on the WINS subnet.  if not,
265                we can become a domain controller.
266                it's only polite that we check, before claiming the
267                NetBIOS name 0x1b.
268              */
269
270             DEBUG(0,("add_domain_names:querying WINS for domain master \
271 on workgroup %s\n", myworkgroup));
272
273             queue_netbios_pkt_wins(ClientNMB,NMB_QUERY,NAME_QUERY_DOMAIN,
274                                 myworkgroup, 0x1b,
275                                 0, 0,0,NULL,NULL,
276                                 False, False, ipzero, ipzero);
277           }
278         }
279         else
280         {
281           DEBUG(0,("add_domain_names:querying subnet %s for domain master \
282 on workgroup %s\n", inet_ntoa(d->bcast_ip), myworkgroup));
283           queue_netbios_packet(d,ClientNMB,NMB_QUERY,NAME_QUERY_DOMAIN,
284                               myworkgroup, 0x1b,
285                               0, 0,0,NULL,NULL,
286                               True, False,
287                               d->bcast_ip, d->bcast_ip);
288         }
289       }
290     }
291   }
292 }
293
294
295 /****************************************************************************
296   add the magic samba names, useful for finding samba servers
297   **************************************************************************/
298 void add_my_names(void)
299 {
300   struct subnet_record *d;
301   /* each subnet entry, including WINS pseudo-subnet, has SELF names */
302
303   /* XXXX if there was a transport layer added to samba (ipx/spx etc) then
304      there would be yet _another_ for-loop, this time on the transport type
305    */
306
307   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
308   {
309     BOOL wins = (lp_wins_support() && (d == wins_subnet));
310
311     add_my_name_entry(d, myname,0x20,nb_type|NB_ACTIVE,False);
312     add_my_name_entry(d, myname,0x03,nb_type|NB_ACTIVE,False);
313     add_my_name_entry(d, myname,0x00,nb_type|NB_ACTIVE,False);
314     
315     /* these names are added permanently (ttl of zero) and will NOT be
316        refreshed with the WINS server  */
317     add_netbios_entry(d,"*",0x0,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
318     add_netbios_entry(d,"*",0x20,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
319     add_netbios_entry(d,"__SAMBA__",0x20,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
320     add_netbios_entry(d,"__SAMBA__",0x00,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
321   }
322 }
323
324
325 /****************************************************************************
326   remove all the samba names... from a WINS server if necessary.
327   **************************************************************************/
328 void remove_my_names()
329 {
330         struct subnet_record *d;
331
332         for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
333         {
334                 struct name_record *n, *next;
335
336                 for (n = d->namelist; n; n = next)
337                 {
338                         next = n->next;
339                         if (n->source == SELF)
340                         {
341                                 /* get all SELF names removed from the WINS server's database */
342                                 /* XXXX note: problem occurs if this removes the wrong one! */
343
344                                 remove_name_entry(d,n->name.name, n->name.name_type,False);
345                         }
346                 }
347         }
348 }
349
350
351 /*******************************************************************
352   refresh my own names
353   ******************************************************************/
354 void refresh_my_names(time_t t)
355 {
356   struct subnet_record *d;
357
358   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
359   {
360     struct name_record *n;
361           
362     for (n = d->namelist; n; n = n->next)
363     {
364       /* each SELF name has an individual time to be refreshed */
365       if (n->source == SELF && n->refresh_time < t && 
366           n->death_time != 0)
367       {
368         add_my_name_entry(d,n->name.name,n->name.name_type,
369                           n->ip_flgs[0].nb_flags,False);
370         /* they get a new lease on life :-) */
371         n->death_time += GET_TTL(0);
372         n->refresh_time += GET_TTL(0);
373       }
374     }
375   }
376 }
377
378
379 /*******************************************************************
380   queries names occasionally. an over-cautious, non-trusting WINS server!
381
382   this function has been added because nmbd could be restarted. it
383   is generally a good idea to check all the names that have been
384   reloaded from file.
385
386   XXXX which names to poll and which not can be refined at a later date.
387   ******************************************************************/
388 void query_refresh_names(time_t t)
389 {
390         struct name_record *n;
391         struct subnet_record *d = wins_subnet;
392
393         static time_t lasttime = 0;
394
395         int count = 0;
396         int name_refresh_time = NAME_POLL_REFRESH_TIME;
397         int max_count = name_refresh_time * 2 / NAME_POLL_INTERVAL;
398         if (max_count > 10) max_count = 10;
399
400         name_refresh_time = NAME_POLL_INTERVAL * max_count / 2;
401
402         /* if (!lp_poll_wins()) return; polling of registered names allowed */
403
404         if (!d) return;
405
406     if (!lasttime) lasttime = t;
407         if (t - lasttime < NAME_POLL_INTERVAL) return;
408
409     lasttime = time(NULL);
410
411         for (n = d->namelist; n; n = n->next)
412         {
413                 /* only do unique, registered names */
414
415                 if (n->source != REGISTER) continue;
416                 if (!NAME_GROUP(n->ip_flgs[0].nb_flags)) continue;
417
418                 if (n->refresh_time < t)
419                 {
420                   DEBUG(3,("Polling name %s\n", namestr(&n->name)));
421                   
422           queue_netbios_packet(d,ClientNMB,NMB_QUERY,NAME_QUERY_CONFIRM,
423                                 n->name.name, n->name.name_type,
424                                 0,0,0,NULL,NULL,
425                                 False,False,n->ip_flgs[0].ip,n->ip_flgs[0].ip);
426                   count++;
427                 }
428
429                 if (count >= max_count)
430                 {
431                         /* don't do too many of these at once, but do enough to
432                            cover everyone in the list */
433                         return;
434                 }
435
436                 /* this name will be checked on again, if it's not removed */
437                 n->refresh_time += name_refresh_time;
438         }
439 }
440