91636428961327b884d0afafa74e62f5a5bce1fc
[kai/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   ****************************************************************************/
55 void remove_name_entry(struct subnet_record *d, char *name,int type)
56 {
57   /* XXXX BUG: if samba is offering WINS support, it should still broadcast
58       a de-registration packet to the local subnet before removing the
59       name from its local-subnet name database. */
60
61   struct name_record n;
62   struct name_record *n2=NULL;
63       
64   make_nmb_name(&n.name,name,type,scope);
65
66   if ((n2 = find_name_search(&d, &n.name, FIND_SELF, ipzero)))
67   {
68     /* check name isn't already being de-registered */
69     if (NAME_DEREG(n2->ip_flgs[0].nb_flags))
70       return;
71
72     /* mark the name as in the process of deletion. */
73     n2->ip_flgs[0].nb_flags &= NB_DEREG;
74   }
75
76   if (!n2) return;
77
78   /* remove the name immediately. even if the spec says we should
79      first try to release them, this is too dangerous with our current
80      name structures as otherwise we will end up replying to names we
81      don't really own */  
82   remove_netbios_name(d,name,type,SELF,n2->ip_flgs[0].ip);
83
84   if (ip_equal(d->bcast_ip, wins_ip))
85   {
86     if (!lp_wins_support())
87     {
88       /* not a WINS server: we have to release them on the network */
89       queue_netbios_pkt_wins(ClientNMB,NMB_REL,NAME_RELEASE,
90                              name, type, 0, 0,0,NULL,NULL,
91                              False, True, ipzero, ipzero);
92     }
93   }
94   else
95   {
96     /* local interface: release them on the network */
97     queue_netbios_packet(d,ClientNMB,NMB_REL,NAME_RELEASE,
98                          name, type, 0, 0,0,NULL,NULL,
99                          True, True, d->bcast_ip, d->bcast_ip);
100   }
101 }
102
103
104 /****************************************************************************
105   add an entry to the name list
106   
107   big note: our name will _always_ be added (if there are no objections).
108   it's just a matter of when this will be done (e.g after a time-out).
109
110   ****************************************************************************/
111 void add_my_name_entry(struct subnet_record *d,char *name,int type,int nb_flags)
112 {
113   BOOL re_reg = False;
114   struct nmb_name n;
115
116   if (!d) return;
117
118   /* not that it particularly matters, but if the SELF name already exists,
119      it must be re-registered, rather than just registered */
120
121   make_nmb_name(&n, name, type, scope);
122   if (find_name(d->namelist, &n, SELF))
123         re_reg = True;
124
125   /* XXXX BUG: if samba is offering WINS support, it should still add the
126      name entry to a local-subnet name database. see rfc1001.txt 15.1.1 p28
127      regarding the point about M-nodes. */
128
129   if (ip_equal(d->bcast_ip, wins_ip))
130   {
131     if (lp_wins_support())
132     {
133       /* we are a WINS server. */
134       /* XXXX assume that if we are a WINS server that we are therefore
135          not pointing to another WINS server as well. this may later NOT
136          actually be true
137        */
138
139       DEBUG(4,("samba as WINS server adding: "));
140       /* this will call add_netbios_entry() */
141       name_register_work(d, name, type, nb_flags,0, ipzero, False);
142     }
143     else
144     {
145       DEBUG(4,("add_my_name_entry registering name %s with WINS server.\n",
146                 name));
147       
148       /* a time-to-live allows us to refresh this name with the WINS server. */
149           queue_netbios_pkt_wins(ClientNMB,
150                                  re_reg ? NMB_REG_REFRESH : NMB_REG, NAME_REGISTER,
151                              name, type, nb_flags, GET_TTL(0),0,NULL,NULL,
152                              False, True, ipzero, ipzero);
153     }
154   }
155   else
156   {
157     /* broadcast the packet, but it comes from ipzero */
158         queue_netbios_packet(d,ClientNMB,
159                                  re_reg ? NMB_REG_REFRESH : NMB_REG, NAME_REGISTER,
160                              name, type, nb_flags, GET_TTL(0),0,NULL,NULL,
161                              True, True, d->bcast_ip, ipzero);
162   }
163 }
164
165
166 /****************************************************************************
167   add the domain logon server and domain master browser names 
168
169   this code was written so that several samba servers can co-operate in
170   sharing the task of (one server) being a domain master, and of being
171   domain logon servers.
172
173   **************************************************************************/
174 void add_domain_names(time_t t)
175 {
176   static time_t lastrun = 0;
177   struct subnet_record *d;
178   struct work_record *work;
179   struct nmb_name n;
180
181   if (lastrun != 0 && t < lastrun + CHECK_TIME_ADD_DOM_NAMES * 60) return;
182   lastrun = t;
183
184   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
185   {
186     work = find_workgroupstruct(d, myworkgroup, False);
187     if (lp_domain_logons() && work && work->log_state == LOGON_NONE)
188     {
189       make_nmb_name(&n,myworkgroup,0x1c,scope);
190       if (!find_name(d->namelist, &n, FIND_SELF))
191       {
192         /* logon servers are group names - we don't expect this to fail. */
193         DEBUG(0,("%s attempting to become logon server for %s %s\n",
194              timestring(), myworkgroup, inet_ntoa(d->bcast_ip)));
195         become_logon_server(d, work);
196       }
197     }
198   }
199
200  for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
201  { 
202    work = find_workgroupstruct(d, myworkgroup, True);
203
204    if (lp_domain_master() && work && work->dom_state == DOMAIN_NONE)
205      {
206       make_nmb_name(&n,myworkgroup,0x1b,scope);
207       if (!find_name(d->namelist, &n, FIND_SELF))
208       {
209         DEBUG(0,("%s add_domain_names: attempting to become domain master \
210 browser on workgroup %s %s\n",
211                   timestring(), myworkgroup, inet_ntoa(d->bcast_ip)));
212
213         if (lp_wins_support())
214         {
215           /* use the wins server's capabilities (indirectly).  if
216              someone has already registered the domain<1b> name with 
217              the WINS server, then the WINS server's job is to _check_
218              that the owner still wants it, before giving it away.
219            */
220                
221           DEBUG(1,("%s initiating becoming domain master for %s\n",
222                         timestring(), myworkgroup));
223           become_domain_master(d, work);
224         }
225         else
226         {
227           /* send out a query to establish whether there's a 
228              domain controller on the WINS subnet.  if not,
229              we can become a domain controller.
230              it's only polite that we check, before claiming the
231              NetBIOS name 0x1b.
232            */
233
234           DEBUG(0,("add_domain_names:querying WINS for domain master \
235 on workgroup %s\n", myworkgroup));
236
237           queue_netbios_pkt_wins(ClientNMB,NMB_QUERY,NAME_QUERY_DOMAIN,
238                                 myworkgroup, 0x1b,
239                                 0, 0,0,NULL,NULL,
240                                 False, False, ipzero, ipzero);
241         }
242       }
243     }
244   }
245 }
246
247
248 /****************************************************************************
249   add the magic samba names, useful for finding samba servers
250   **************************************************************************/
251 void add_my_names(void)
252 {
253   struct subnet_record *d;
254   /* each subnet entry, including WINS pseudo-subnet, has SELF names */
255
256   /* XXXX if there was a transport layer added to samba (ipx/spx etc) then
257      there would be yet _another_ for-loop, this time on the transport type
258    */
259
260   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
261   {
262     BOOL wins = (lp_wins_support() && (d == wins_subnet));
263
264     add_my_name_entry(d, myname,0x20,nb_type|NB_ACTIVE);
265     add_my_name_entry(d, myname,0x03,nb_type|NB_ACTIVE);
266     add_my_name_entry(d, myname,0x00,nb_type|NB_ACTIVE);
267     add_my_name_entry(d, myname,0x1f,nb_type|NB_ACTIVE);
268     
269     /* these names are added permanently (ttl of zero) and will NOT be
270        refreshed with the WINS server  */
271     add_netbios_entry(d,"*",0x0,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
272     add_netbios_entry(d,"*",0x20,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
273     add_netbios_entry(d,"__SAMBA__",0x20,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
274     add_netbios_entry(d,"__SAMBA__",0x00,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
275   }
276 }
277
278
279 /****************************************************************************
280   remove all the samba names... from a WINS server if necessary.
281   **************************************************************************/
282 void remove_my_names()
283 {
284         struct subnet_record *d;
285
286         for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
287         {
288                 struct name_record *n, *next;
289
290                 for (n = d->namelist; n; n = next)
291                 {
292                         next = n->next;
293                         if (n->source == SELF)
294                         {
295                                 /* get all SELF names removed from the WINS server's database */
296                                 /* XXXX note: problem occurs if this removes the wrong one! */
297
298                                 remove_name_entry(d,n->name.name, n->name.name_type);
299                         }
300                 }
301         }
302 }
303
304
305 /*******************************************************************
306   refresh my own names
307   ******************************************************************/
308 void refresh_my_names(time_t t)
309 {
310   struct subnet_record *d;
311
312   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
313   {
314     struct name_record *n;
315           
316         for (n = d->namelist; n; n = n->next)
317     {
318       /* each SELF name has an individual time to be refreshed */
319       if (n->source == SELF && n->refresh_time < t && 
320           n->death_time != 0)
321       {
322         add_my_name_entry(d,n->name.name,n->name.name_type,
323                           n->ip_flgs[0].nb_flags);
324         /* they get a new lease on life :-) */
325         n->death_time += GET_TTL(0);
326         n->refresh_time += GET_TTL(0);
327       }
328     }
329   }
330 }
331
332
333 /*******************************************************************
334   queries names occasionally. an over-cautious, non-trusting WINS server!
335
336   this function has been added because nmbd could be restarted. it
337   is generally a good idea to check all the names that have been
338   reloaded from file.
339
340   XXXX which names to poll and which not can be refined at a later date.
341   ******************************************************************/
342 void query_refresh_names(time_t t)
343 {
344         struct name_record *n;
345         struct subnet_record *d = wins_subnet;
346
347         static time_t lasttime = 0;
348
349         int count = 0;
350         int name_refresh_time = NAME_POLL_REFRESH_TIME;
351         int max_count = name_refresh_time * 2 / NAME_POLL_INTERVAL;
352         if (max_count > 10) max_count = 10;
353
354         name_refresh_time = NAME_POLL_INTERVAL * max_count / 2;
355
356         /* if (!lp_poll_wins()) return; polling of registered names allowed */
357
358         if (!d) return;
359
360     if (!lasttime) lasttime = t;
361         if (t - lasttime < NAME_POLL_INTERVAL) return;
362
363     lasttime = time(NULL);
364
365         for (n = d->namelist; n; n = n->next)
366         {
367                 /* only do unique, registered names */
368
369                 if (n->source != REGISTER) continue;
370                 if (!NAME_GROUP(n->ip_flgs[0].nb_flags)) continue;
371
372                 if (n->refresh_time < t)
373                 {
374                   DEBUG(3,("Polling name %s\n", namestr(&n->name)));
375                   
376           queue_netbios_packet(d,ClientNMB,NMB_QUERY,NAME_QUERY_CONFIRM,
377                                 n->name.name, n->name.name_type,
378                                 0,0,0,NULL,NULL,
379                                 False,False,n->ip_flgs[0].ip,n->ip_flgs[0].ip);
380                   count++;
381                 }
382
383                 if (count >= max_count)
384                 {
385                         /* don't do too many of these at once, but do enough to
386                            cover everyone in the list */
387                         return;
388                 }
389
390                 /* this name will be checked on again, if it's not removed */
391                 n->refresh_time += name_refresh_time;
392         }
393 }
394