charcnv.c client.c clitar.c kanji.c kanji.h loadparm.c
[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 char **my_netbios_names;
42 extern struct in_addr ipzero;
43 extern struct in_addr wins_ip;
44
45 extern struct subnet_record *subnetlist;
46
47 extern uint16 nb_type; /* samba's NetBIOS type */
48
49 /****************************************************************************
50   remove an entry from the name list
51
52   note: the name will _always_ be removed
53   XXXX at present, the name is removed _even_ if a WINS server says keep it.
54
55   ****************************************************************************/
56 void remove_name_entry(struct subnet_record *d, char *name,int type)
57 {
58   /* XXXX BUG: if samba is offering WINS support, it should still broadcast
59       a de-registration packet to the local subnet before removing the
60       name from its local-subnet name database. */
61
62   int search = FIND_SELF;
63   struct name_record n;
64   struct name_record *n2=NULL;
65       
66   make_nmb_name(&n.name,name,type,scope);
67
68   if(d == wins_subnet)
69     search |= FIND_WINS;
70   else
71     search |= FIND_LOCAL;
72
73   if ((n2 = find_name_search(&d, &n.name, search, ipzero)))
74   {
75     /* check name isn't already being de-registered */
76     if (NAME_DEREG(n2->ip_flgs[0].nb_flags))
77       return;
78
79     /* mark the name as in the process of deletion. */
80     n2->ip_flgs[0].nb_flags &= NB_DEREG;
81   }
82
83   if (!n2) return;
84
85   /* Only remove names with non-zero death times. */
86   if(n2->death_time == 0)
87   {
88     DEBUG(5,("remove_name_entry: Name %s(%d) has zero ttl - not removing.\n",
89              name, type));
90     return;
91   }
92
93   /* remove the name immediately. even if the spec says we should
94      first try to release them, this is too dangerous with our current
95      name structures as otherwise we will end up replying to names we
96      don't really own */  
97   remove_netbios_name(d,name,type,SELF,n2->ip_flgs[0].ip);
98
99   if (ip_equal(d->bcast_ip, wins_ip))
100   {
101     if (!lp_wins_support())
102     {
103       /* not a WINS server: we have to release them on the network */
104       queue_netbios_pkt_wins(ClientNMB,NMB_REL,NAME_RELEASE,
105                              name, type, 0, 0,0,NULL,NULL,
106                              ipzero, ipzero);
107     }
108   }
109   else
110   {
111       /* local interface: release them on the network */
112       queue_netbios_packet(d,ClientNMB,NMB_REL,NAME_RELEASE,
113                          name, type, 0, 0,0,NULL,NULL,
114                          True, False, d->bcast_ip, d->bcast_ip);
115   }
116 }
117
118
119 /****************************************************************************
120   add an entry to the name list
121  
122   big note: our name will _always_ be added (if there are no objections).
123   it's just a matter of when this will be done (e.g after a time-out).
124
125   ****************************************************************************/
126 void add_my_name_entry(struct subnet_record *d,char *name,int type,int nb_flags)
127 {
128   BOOL re_reg = False;
129   struct nmb_name n;
130
131   if (!d) return;
132
133   /* not that it particularly matters, but if the SELF name already exists,
134      it must be re-registered, rather than just registered */
135
136   make_nmb_name(&n, name, type, scope);
137   if (find_name(d->namelist, &n, SELF))
138         re_reg = True;
139
140   /* XXXX BUG: if samba is offering WINS support, it should still add the
141      name entry to a local-subnet name database. see rfc1001.txt 15.1.1 p28
142      regarding the point about M-nodes. */
143
144   if (ip_equal(d->bcast_ip, wins_ip))
145   {
146     if (lp_wins_support())
147     {
148       /* we are a WINS server. */
149       if(lp_wins_support())
150       {
151         DEBUG(4,("add_my_name_entry: samba as WINS server adding: "));
152       }
153         
154       /* this will call add_netbios_entry() */
155       name_register_work(d, name, type, nb_flags,0, ipzero, False);
156     }
157     else
158     {
159       DEBUG(4,("add_my_name_entry registering name %s with WINS server.\n",
160                 name));
161       
162       /* a time-to-live allows us to refresh this name with the WINS server. */
163           queue_netbios_pkt_wins(ClientNMB,
164                                  re_reg ? NMB_REG_REFRESH : NMB_REG, NAME_REGISTER,
165                              name, type, nb_flags, GET_TTL(0),0,NULL,NULL,
166                              ipzero, ipzero);
167     }
168   }
169   else
170   {
171     /* broadcast the packet, but it comes from ipzero */
172     queue_netbios_packet(d,ClientNMB,
173          re_reg ? NMB_REG_REFRESH : NMB_REG, NAME_REGISTER,
174          name, type, nb_flags, GET_TTL(0),0,NULL,NULL,
175          True, False, d->bcast_ip, ipzero);
176   }
177 }
178
179
180 /****************************************************************************
181   add the internet group <1c> domain logon names by wins unicast and broadcast.
182   ****************************************************************************/
183 void add_domain_logon_names(void)
184 {
185         struct subnet_record *d;
186
187         if (!lp_domain_logons()) return;
188
189         for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
190         {
191                 struct work_record *work = find_workgroupstruct(d, myworkgroup, True);
192
193                 if (work && work->log_state == LOGON_NONE)
194                 {
195                         struct nmb_name n;
196                         make_nmb_name(&n,myworkgroup,0x1c,scope);
197
198                         if (!find_name(d->namelist, &n, FIND_SELF))
199                         {
200                                 /* logon servers are group names. don't expect failure */
201
202                                 DEBUG(0,("%s attempting to become logon server for %s %s\n",
203                                                 timestring(), myworkgroup, inet_ntoa(d->bcast_ip)));
204
205                                 become_logon_server(d, work);
206                         }
207                 }
208         }
209 }
210
211
212 /****************************************************************************
213   add the <1b> domain master names by broadcast.
214   ****************************************************************************/
215 void add_domain_master_bcast(void)
216 {
217         struct subnet_record *d;
218
219         if (!lp_domain_master()) return;
220
221         for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_EXCLUDING_WINS(d))
222         { 
223                 struct work_record *work = find_workgroupstruct(d, myworkgroup, True);
224
225                 if (work && work->dom_state == DOMAIN_NONE)
226                 {
227                         struct nmb_name n;
228                         make_nmb_name(&n,myworkgroup,0x1b,scope);
229
230                         if (!find_name(d->namelist, &n, FIND_SELF))
231                         {
232                                 DEBUG(0,("%s add_domain_names: attempting to become domain \
233 master browser on workgroup %s %s\n",
234                                 timestring(), myworkgroup, inet_ntoa(d->bcast_ip)));
235
236                                 /* send out a query to establish whether there's a 
237                                    domain controller on the local subnet.  if not,
238                                    we can become a domain controller.  it's only
239                                    polite that we check, before claiming the
240                                    NetBIOS name 0x1b.
241                                 */
242
243                                 DEBUG(0,("add_domain_names:querying subnet %s \
244 for domain master on workgroup %s\n", 
245                                                   inet_ntoa(d->bcast_ip), myworkgroup));
246
247                                 queue_netbios_packet(d,ClientNMB,NMB_QUERY,
248                                                                          NAME_QUERY_DOMAIN,
249                                                                          myworkgroup, 0x1b,
250                                                                          0, 0,0,NULL,NULL,
251                                                                          True, False,
252                                                                          d->bcast_ip, d->bcast_ip);
253                         }
254                 }
255         }
256 }
257
258
259 /****************************************************************************
260   add the <1b> domain master name by wins unicast.
261   ****************************************************************************/
262 void add_domain_master_wins(void)
263 {
264         struct work_record *work;
265
266         if (!lp_domain_master() || wins_subnet == NULL) return;
267
268         work = find_workgroupstruct(wins_subnet, myworkgroup, True);
269
270         if (work && work->dom_state == DOMAIN_NONE)
271         {
272                 struct nmb_name n;
273                 make_nmb_name(&n,myworkgroup,0x1b,scope);
274
275                 if (!find_name(wins_subnet->namelist, &n, FIND_SELF))
276                 {
277                         DEBUG(0,("%s add_domain_names: attempting to become domain \
278 master browser on workgroup %s %s\n",
279                         timestring(), myworkgroup, inet_ntoa(wins_subnet->bcast_ip)));
280
281                         if (lp_wins_support())
282                         {
283                                 /* use the wins server's capabilities (indirectly).  if
284                                    someone has already registered the domain<1b>
285                                    name with the WINS server, then the WINS
286                                    server's job is to _check_ that the owner still
287                                    wants it, before giving it away.
288                                 */
289
290                                 DEBUG(1,("%s initiate become domain master for %s\n",
291                                                   timestring(), myworkgroup));
292
293                                 become_domain_master(wins_subnet, work);
294                         }
295                         else
296                         {
297                                 /* send out a query to establish whether there's a 
298                                    domain controller on the WINS subnet.  if not,
299                                    we can become a domain controller.  it's only
300                                    polite that we check, before claiming the
301                                    NetBIOS name 0x1b.
302                                 */
303
304                                 DEBUG(0,("add_domain_names:querying WINS \
305 for domain master on workgroup %s\n", myworkgroup));
306
307                                 queue_netbios_pkt_wins(ClientNMB,NMB_QUERY,
308                                                                            NAME_QUERY_DOMAIN,
309                                                                            myworkgroup, 0x1b,
310                                                                            0, 0,0,NULL,NULL,
311                                                                            ipzero, ipzero);
312                         }
313                 }
314         }
315 }
316
317
318 /****************************************************************************
319   add the domain logon server and domain master browser names 
320
321   this code was written so that several samba servers can co-operate in
322   sharing the task of (one server) being a domain master, and of being
323   domain logon servers.
324
325   **************************************************************************/
326 void add_domain_names(time_t t)
327 {
328         static time_t lastrun = 0;
329
330         if (lastrun != 0 && t < lastrun + CHECK_TIME_ADD_DOM_NAMES * 60) return;
331         lastrun = t;
332
333         /* do the "internet group" - <1c> names */
334         add_domain_logon_names();
335
336         /* do the domain master names */
337         if (wins_subnet != NULL)
338         {
339                 /* if the registration of the <1b> name is successful, then
340                    add_domain_master_bcast() will be called.  this will
341                    result in domain logon services being gracefully provided,
342                    as opposed to the aggressive nature of 1.9.16p2 to 1.9.16p11.
343
344                    which, due to a bug in namelogon.c from 1.9.16p2 to 1.9.16p11
345                    cannot _provide_ domain master / domain logon services!!!
346
347                  */
348                 add_domain_master_wins();
349         }
350         else
351         {
352                 add_domain_master_bcast();
353         }
354 }
355
356 /****************************************************************************
357   add the magic samba names, useful for finding samba servers
358   **************************************************************************/
359 void add_my_names(void)
360 {
361   struct subnet_record *d;
362   /* each subnet entry, including WINS pseudo-subnet, has SELF names */
363
364   /* XXXX if there was a transport layer added to samba (ipx/spx etc) then
365      there would be yet _another_ for-loop, this time on the transport type
366    */
367
368   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
369   {
370     int n;
371     BOOL wins = (lp_wins_support() && (d == wins_subnet));
372
373     /* Add all our names including aliases. */
374     for (n=0; my_netbios_names[n]; n++) 
375     {
376       add_my_name_entry(d, my_netbios_names[n],0x20,nb_type|NB_ACTIVE);
377       add_my_name_entry(d, my_netbios_names[n],0x03,nb_type|NB_ACTIVE);
378       add_my_name_entry(d, my_netbios_names[n],0x00,nb_type|NB_ACTIVE);
379     }
380     
381     /* these names are added permanently (ttl of zero) and will NOT be
382        refreshed with the WINS server  */
383     add_netbios_entry(d,"*",0x0,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
384     add_netbios_entry(d,"*",0x20,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
385     add_netbios_entry(d,"__SAMBA__",0x20,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
386     add_netbios_entry(d,"__SAMBA__",0x00,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
387   }
388 }
389
390
391 /****************************************************************************
392   remove all the samba names... from a WINS server if necessary.
393   **************************************************************************/
394 void remove_my_names()
395 {
396         struct subnet_record *d;
397
398         for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
399         {
400                 struct name_record *n, *next;
401
402                 for (n = d->namelist; n; n = next)
403                 {
404                         next = n->next;
405                         if (n->source == SELF)
406                         {
407                                 /* get all SELF names removed from the WINS server's database */
408                                 /* XXXX note: problem occurs if this removes the wrong one! */
409
410                                 remove_name_entry(d,n->name.name, n->name.name_type);
411                         }
412                 }
413         }
414 }
415
416
417 /*******************************************************************
418   refresh my own names
419   ******************************************************************/
420 void refresh_my_names(time_t t)
421 {
422   struct subnet_record *d;
423
424   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
425   {
426     struct name_record *n;
427           
428     for (n = d->namelist; n; n = n->next)
429     {
430       /* each SELF name has an individual time to be refreshed */
431       if (n->source == SELF && n->refresh_time < t && 
432           n->death_time != 0)
433       {
434         add_my_name_entry(d,n->name.name,n->name.name_type,
435                           n->ip_flgs[0].nb_flags);
436         /* they get a new lease on life :-) */
437         n->death_time += GET_TTL(0);
438         n->refresh_time += GET_TTL(0);
439       }
440     }
441   }
442 }
443
444
445 /*******************************************************************
446   queries names occasionally. an over-cautious, non-trusting WINS server!
447
448   this function has been added because nmbd could be restarted. it
449   is generally a good idea to check all the names that have been
450   reloaded from file.
451
452   XXXX which names to poll and which not can be refined at a later date.
453   ******************************************************************/
454 void query_refresh_names(time_t t)
455 {
456         struct name_record *n;
457         struct subnet_record *d = wins_subnet;
458
459         static time_t lasttime = 0;
460
461         int count = 0;
462         int name_refresh_time = NAME_POLL_REFRESH_TIME;
463         int max_count = name_refresh_time * 2 / NAME_POLL_INTERVAL;
464         if (max_count > 10) max_count = 10;
465
466         name_refresh_time = NAME_POLL_INTERVAL * max_count / 2;
467
468         /* if (!lp_poll_wins()) return; polling of registered names allowed */
469
470         if (!d) return;
471
472     if (!lasttime) lasttime = t;
473         if (t - lasttime < NAME_POLL_INTERVAL) return;
474
475     lasttime = time(NULL);
476
477         for (n = d->namelist; n; n = n->next)
478         {
479                 /* only do unique, registered names */
480
481                 if (n->source != REGISTER) continue;
482                 if (!NAME_GROUP(n->ip_flgs[0].nb_flags)) continue;
483
484                 if (n->refresh_time < t)
485                 {
486                   DEBUG(3,("Polling name %s\n", namestr(&n->name)));
487                   
488           queue_netbios_packet(d,ClientNMB,NMB_QUERY,NAME_QUERY_CONFIRM,
489                                 n->name.name, n->name.name_type,
490                                 0,0,0,NULL,NULL,
491                                 False,False,n->ip_flgs[0].ip,n->ip_flgs[0].ip);
492                   count++;
493                 }
494
495                 if (count >= max_count)
496                 {
497                         /* don't do too many of these at once, but do enough to
498                            cover everyone in the list */
499                         return;
500                 }
501
502                 /* this name will be checked on again, if it's not removed */
503                 n->refresh_time += name_refresh_time;
504         }
505 }
506