client.c: New print queue query code from Jeff C. Foster " <jfoste@wgc.woodward...
[Samba/gbeck.git] / source / nameserv.c
blobf1b34482c85dd8fe178a82e4e5b3fe7480a5424a
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
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.
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.
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.
21 Module name: nameserv.c
23 Revision History:
25 14 jan 96: lkcl@pires.co.uk
26 added multiple workgroup domain master support
28 04 jul 96: lkcl@pires.co.uk
29 module nameserv contains name server management functions
32 #include "includes.h"
34 extern int ClientNMB;
36 extern int DEBUGLEVEL;
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;
44 extern struct subnet_record *subnetlist;
46 extern uint16 nb_type; /* samba's NetBIOS type */
48 /****************************************************************************
49 remove an entry from the name list
51 note: the name will _always_ be removed
52 XXXX at present, the name is removed _even_ if a WINS server says keep it.
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.
58 ****************************************************************************/
59 void remove_name_entry(struct subnet_record *d, char *name,int type, BOOL direct)
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. */
65 int search = FIND_SELF;
66 struct name_record n;
67 struct name_record *n2=NULL;
69 make_nmb_name(&n.name,name,type,scope);
71 if(d == wins_subnet)
72 search |= FIND_WINS;
73 else
74 search |= FIND_LOCAL;
76 if ((n2 = find_name_search(&d, &n.name, search, ipzero)))
78 /* check name isn't already being de-registered */
79 if (NAME_DEREG(n2->ip_flgs[0].nb_flags))
80 return;
82 /* mark the name as in the process of deletion. */
83 n2->ip_flgs[0].nb_flags &= NB_DEREG;
86 if (!n2) return;
88 /* Only remove names with non-zero death times. */
89 if(n2->death_time == 0)
91 DEBUG(5,("remove_name_entry: Name %s(%d) has zero ttl - not removing.\n",
92 name, type));
93 return;
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);
102 if (ip_equal(d->bcast_ip, wins_ip))
104 if (!lp_wins_support() && !direct)
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);
112 else
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, False, d->bcast_ip, d->bcast_ip);
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>.
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).
133 ****************************************************************************/
134 void add_my_name_entry(struct subnet_record *d,char *name,int type,int nb_flags, BOOL direct)
136 BOOL re_reg = False;
137 struct nmb_name n;
139 if (!d) return;
141 /* not that it particularly matters, but if the SELF name already exists,
142 it must be re-registered, rather than just registered */
144 make_nmb_name(&n, name, type, scope);
145 if (find_name(d->namelist, &n, SELF))
146 re_reg = True;
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. */
152 if (ip_equal(d->bcast_ip, wins_ip))
154 if (lp_wins_support() || direct)
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: "));
162 /* this will call add_netbios_entry() */
163 name_register_work(d, name, type, nb_flags,0, ipzero, False);
165 else
167 DEBUG(4,("add_my_name_entry registering name %s with WINS server.\n",
168 name));
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);
177 else
179 if(direct)
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.
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);
188 else
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, False, d->bcast_ip, ipzero);
200 /****************************************************************************
201 add the domain logon server and domain master browser names
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.
207 **************************************************************************/
208 void add_domain_names(time_t t)
210 static time_t lastrun = 0;
211 struct subnet_record *d;
212 struct work_record *work;
213 struct nmb_name n;
215 if (lastrun != 0 && t < lastrun + CHECK_TIME_ADD_DOM_NAMES * 60) return;
216 lastrun = t;
218 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
220 work = find_workgroupstruct(d, myworkgroup, False);
221 if (lp_domain_logons() && work && work->log_state == LOGON_NONE)
223 make_nmb_name(&n,myworkgroup,0x1c,scope);
224 if (!find_name(d->namelist, &n, FIND_SELF))
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);
234 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
236 work = find_workgroupstruct(d, myworkgroup, True);
238 if (lp_domain_master() && work && work->dom_state == DOMAIN_NONE)
240 make_nmb_name(&n,myworkgroup,0x1b,scope);
241 if (!find_name(d->namelist, &n, FIND_SELF))
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)));
247 if(d == wins_subnet)
249 if (lp_wins_support())
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.
257 DEBUG(1,("%s initiating becoming domain master for %s\n",
258 timestring(), myworkgroup));
259 become_domain_master(d, work);
261 else
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.
270 DEBUG(0,("add_domain_names:querying WINS for domain master \
271 on workgroup %s\n", myworkgroup));
273 queue_netbios_pkt_wins(ClientNMB,NMB_QUERY,NAME_QUERY_DOMAIN,
274 myworkgroup, 0x1b,
275 0, 0,0,NULL,NULL,
276 False, True, ipzero, ipzero);
279 else
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);
295 /****************************************************************************
296 add the magic samba names, useful for finding samba servers
297 **************************************************************************/
298 void add_my_names(void)
300 struct subnet_record *d;
301 /* each subnet entry, including WINS pseudo-subnet, has SELF names */
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
307 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
309 BOOL wins = (lp_wins_support() && (d == wins_subnet));
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);
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);
325 /****************************************************************************
326 remove all the samba names... from a WINS server if necessary.
327 **************************************************************************/
328 void remove_my_names()
330 struct subnet_record *d;
332 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
334 struct name_record *n, *next;
336 for (n = d->namelist; n; n = next)
338 next = n->next;
339 if (n->source == SELF)
341 /* get all SELF names removed from the WINS server's database */
342 /* XXXX note: problem occurs if this removes the wrong one! */
344 remove_name_entry(d,n->name.name, n->name.name_type,False);
351 /*******************************************************************
352 refresh my own names
353 ******************************************************************/
354 void refresh_my_names(time_t t)
356 struct subnet_record *d;
358 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
360 struct name_record *n;
362 for (n = d->namelist; n; n = n->next)
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)
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);
379 /*******************************************************************
380 queries names occasionally. an over-cautious, non-trusting WINS server!
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.
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)
390 struct name_record *n;
391 struct subnet_record *d = wins_subnet;
393 static time_t lasttime = 0;
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;
400 name_refresh_time = NAME_POLL_INTERVAL * max_count / 2;
402 /* if (!lp_poll_wins()) return; polling of registered names allowed */
404 if (!d) return;
406 if (!lasttime) lasttime = t;
407 if (t - lasttime < NAME_POLL_INTERVAL) return;
409 lasttime = time(NULL);
411 for (n = d->namelist; n; n = n->next)
413 /* only do unique, registered names */
415 if (n->source != REGISTER) continue;
416 if (!NAME_GROUP(n->ip_flgs[0].nb_flags)) continue;
418 if (n->refresh_time < t)
420 DEBUG(3,("Polling name %s\n", namestr(&n->name)));
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++;
429 if (count >= max_count)
431 /* don't do too many of these at once, but do enough to
432 cover everyone in the list */
433 return;
436 /* this name will be checked on again, if it's not removed */
437 n->refresh_time += name_refresh_time;