dir.c: Fixed double slash issue.
[Samba.git] / source / nameserv.c
blob91636428961327b884d0afafa74e62f5a5bce1fc
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 ****************************************************************************/
55 void remove_name_entry(struct subnet_record *d, char *name,int type)
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. */
61 struct name_record n;
62 struct name_record *n2=NULL;
64 make_nmb_name(&n.name,name,type,scope);
66 if ((n2 = find_name_search(&d, &n.name, FIND_SELF, ipzero)))
68 /* check name isn't already being de-registered */
69 if (NAME_DEREG(n2->ip_flgs[0].nb_flags))
70 return;
72 /* mark the name as in the process of deletion. */
73 n2->ip_flgs[0].nb_flags &= NB_DEREG;
76 if (!n2) return;
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);
84 if (ip_equal(d->bcast_ip, wins_ip))
86 if (!lp_wins_support())
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);
94 else
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);
104 /****************************************************************************
105 add an entry to the name list
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).
110 ****************************************************************************/
111 void add_my_name_entry(struct subnet_record *d,char *name,int type,int nb_flags)
113 BOOL re_reg = False;
114 struct nmb_name n;
116 if (!d) return;
118 /* not that it particularly matters, but if the SELF name already exists,
119 it must be re-registered, rather than just registered */
121 make_nmb_name(&n, name, type, scope);
122 if (find_name(d->namelist, &n, SELF))
123 re_reg = True;
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. */
129 if (ip_equal(d->bcast_ip, wins_ip))
131 if (lp_wins_support())
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
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);
143 else
145 DEBUG(4,("add_my_name_entry registering name %s with WINS server.\n",
146 name));
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);
155 else
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);
166 /****************************************************************************
167 add the domain logon server and domain master browser names
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.
173 **************************************************************************/
174 void add_domain_names(time_t t)
176 static time_t lastrun = 0;
177 struct subnet_record *d;
178 struct work_record *work;
179 struct nmb_name n;
181 if (lastrun != 0 && t < lastrun + CHECK_TIME_ADD_DOM_NAMES * 60) return;
182 lastrun = t;
184 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
186 work = find_workgroupstruct(d, myworkgroup, False);
187 if (lp_domain_logons() && work && work->log_state == LOGON_NONE)
189 make_nmb_name(&n,myworkgroup,0x1c,scope);
190 if (!find_name(d->namelist, &n, FIND_SELF))
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);
200 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
202 work = find_workgroupstruct(d, myworkgroup, True);
204 if (lp_domain_master() && work && work->dom_state == DOMAIN_NONE)
206 make_nmb_name(&n,myworkgroup,0x1b,scope);
207 if (!find_name(d->namelist, &n, FIND_SELF))
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)));
213 if (lp_wins_support())
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.
221 DEBUG(1,("%s initiating becoming domain master for %s\n",
222 timestring(), myworkgroup));
223 become_domain_master(d, work);
225 else
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.
234 DEBUG(0,("add_domain_names:querying WINS for domain master \
235 on workgroup %s\n", myworkgroup));
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);
248 /****************************************************************************
249 add the magic samba names, useful for finding samba servers
250 **************************************************************************/
251 void add_my_names(void)
253 struct subnet_record *d;
254 /* each subnet entry, including WINS pseudo-subnet, has SELF names */
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
260 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
262 BOOL wins = (lp_wins_support() && (d == wins_subnet));
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);
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);
279 /****************************************************************************
280 remove all the samba names... from a WINS server if necessary.
281 **************************************************************************/
282 void remove_my_names()
284 struct subnet_record *d;
286 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
288 struct name_record *n, *next;
290 for (n = d->namelist; n; n = next)
292 next = n->next;
293 if (n->source == SELF)
295 /* get all SELF names removed from the WINS server's database */
296 /* XXXX note: problem occurs if this removes the wrong one! */
298 remove_name_entry(d,n->name.name, n->name.name_type);
305 /*******************************************************************
306 refresh my own names
307 ******************************************************************/
308 void refresh_my_names(time_t t)
310 struct subnet_record *d;
312 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
314 struct name_record *n;
316 for (n = d->namelist; n; n = n->next)
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)
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);
333 /*******************************************************************
334 queries names occasionally. an over-cautious, non-trusting WINS server!
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.
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)
344 struct name_record *n;
345 struct subnet_record *d = wins_subnet;
347 static time_t lasttime = 0;
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;
354 name_refresh_time = NAME_POLL_INTERVAL * max_count / 2;
356 /* if (!lp_poll_wins()) return; polling of registered names allowed */
358 if (!d) return;
360 if (!lasttime) lasttime = t;
361 if (t - lasttime < NAME_POLL_INTERVAL) return;
363 lasttime = time(NULL);
365 for (n = d->namelist; n; n = n->next)
367 /* only do unique, registered names */
369 if (n->source != REGISTER) continue;
370 if (!NAME_GROUP(n->ip_flgs[0].nb_flags)) continue;
372 if (n->refresh_time < t)
374 DEBUG(3,("Polling name %s\n", namestr(&n->name)));
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++;
383 if (count >= max_count)
385 /* don't do too many of these at once, but do enough to
386 cover everyone in the list */
387 return;
390 /* this name will be checked on again, if it's not removed */
391 n->refresh_time += name_refresh_time;