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