2 Unix SMB/Netbios implementation.
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
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
36 extern int DEBUGLEVEL
;
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 int search
= FIND_SELF
;
63 struct name_record
*n2
=NULL
;
65 make_nmb_name(&n
.name
,name
,type
,scope
);
72 if ((n2
= find_name_search(&d
, &n
.name
, search
, ipzero
)))
74 /* check name isn't already being de-registered */
75 if (NAME_DEREG(n2
->ip_flgs
[0].nb_flags
))
78 /* mark the name as in the process of deletion. */
79 n2
->ip_flgs
[0].nb_flags
&= NB_DEREG
;
84 /* Only remove names with non-zero death times. */
85 if(n2
->death_time
== 0)
87 DEBUG(5,("remove_name_entry: Name %s(%d) has zero ttl - not removing.\n",
92 /* remove the name immediately. even if the spec says we should
93 first try to release them, this is too dangerous with our current
94 name structures as otherwise we will end up replying to names we
96 remove_netbios_name(d
,name
,type
,SELF
,n2
->ip_flgs
[0].ip
);
98 if (ip_equal(d
->bcast_ip
, wins_ip
))
100 if (!lp_wins_support())
102 /* not a WINS server: we have to release them on the network */
103 queue_netbios_pkt_wins(ClientNMB
,NMB_REL
,NAME_RELEASE
,
104 name
, type
, 0, 0,0,NULL
,NULL
,
105 False
, True
, ipzero
, ipzero
);
110 /* local interface: release them on the network */
111 queue_netbios_packet(d
,ClientNMB
,NMB_REL
,NAME_RELEASE
,
112 name
, type
, 0, 0,0,NULL
,NULL
,
113 True
, False
, d
->bcast_ip
, d
->bcast_ip
);
118 /****************************************************************************
119 add an entry to the name list
121 big note: our name will _always_ be added (if there are no objections).
122 it's just a matter of when this will be done (e.g after a time-out).
124 ****************************************************************************/
125 void add_my_name_entry(struct subnet_record
*d
,char *name
,int type
,int nb_flags
)
132 /* not that it particularly matters, but if the SELF name already exists,
133 it must be re-registered, rather than just registered */
135 make_nmb_name(&n
, name
, type
, scope
);
136 if (find_name(d
->namelist
, &n
, SELF
))
139 /* XXXX BUG: if samba is offering WINS support, it should still add the
140 name entry to a local-subnet name database. see rfc1001.txt 15.1.1 p28
141 regarding the point about M-nodes. */
143 if (ip_equal(d
->bcast_ip
, wins_ip
))
145 if (lp_wins_support())
147 /* we are a WINS server. */
148 if(lp_wins_support())
150 DEBUG(4,("add_my_name_entry: samba as WINS server adding: "));
153 /* this will call add_netbios_entry() */
154 name_register_work(d
, name
, type
, nb_flags
,0, ipzero
, False
);
158 DEBUG(4,("add_my_name_entry registering name %s with WINS server.\n",
161 /* a time-to-live allows us to refresh this name with the WINS server. */
162 queue_netbios_pkt_wins(ClientNMB
,
163 re_reg
? NMB_REG_REFRESH
: NMB_REG
, NAME_REGISTER
,
164 name
, type
, nb_flags
, GET_TTL(0),0,NULL
,NULL
,
165 False
, True
, ipzero
, ipzero
);
170 /* broadcast the packet, but it comes from ipzero */
171 queue_netbios_packet(d
,ClientNMB
,
172 re_reg
? NMB_REG_REFRESH
: NMB_REG
, NAME_REGISTER
,
173 name
, type
, nb_flags
, GET_TTL(0),0,NULL
,NULL
,
174 True
, False
, d
->bcast_ip
, ipzero
);
179 /****************************************************************************
180 add the internet group <1c> domain logon names by wins unicast and broadcast.
181 ****************************************************************************/
182 void add_domain_logon_names(void)
184 struct subnet_record
*d
;
186 if (!lp_domain_logons()) return;
188 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_INCLUDING_WINS(d
))
190 struct work_record
*work
= find_workgroupstruct(d
, myworkgroup
, True
);
192 if (work
&& work
->log_state
== LOGON_NONE
)
195 make_nmb_name(&n
,myworkgroup
,0x1c,scope
);
197 if (!find_name(d
->namelist
, &n
, FIND_SELF
))
199 /* logon servers are group names. don't expect failure */
201 DEBUG(0,("%s attempting to become logon server for %s %s\n",
202 timestring(), myworkgroup
, inet_ntoa(d
->bcast_ip
)));
204 become_logon_server(d
, work
);
211 /****************************************************************************
212 add the <1b> domain master names by broadcast.
213 ****************************************************************************/
214 void add_domain_master_bcast(void)
216 struct subnet_record
*d
;
218 if (!lp_domain_master()) return;
220 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_EXCLUDING_WINS(d
))
222 struct work_record
*work
= find_workgroupstruct(d
, myworkgroup
, True
);
224 if (work
&& work
->dom_state
== DOMAIN_NONE
)
227 make_nmb_name(&n
,myworkgroup
,0x1b,scope
);
229 if (!find_name(d
->namelist
, &n
, FIND_SELF
))
231 DEBUG(0,("%s add_domain_names: attempting to become domain \
232 master browser on workgroup %s %s\n",
233 timestring(), myworkgroup
, inet_ntoa(d
->bcast_ip
)));
235 /* send out a query to establish whether there's a
236 domain controller on the local subnet. if not,
237 we can become a domain controller. it's only
238 polite that we check, before claiming the
242 DEBUG(0,("add_domain_names:querying subnet %s \
243 for domain master on workgroup %s\n",
244 inet_ntoa(d
->bcast_ip
), myworkgroup
));
246 queue_netbios_packet(d
,ClientNMB
,NMB_QUERY
,
251 d
->bcast_ip
, d
->bcast_ip
);
258 /****************************************************************************
259 add the <1b> domain master name by wins unicast.
260 ****************************************************************************/
261 void add_domain_master_wins(void)
263 struct work_record
*work
;
265 if (!lp_domain_master() || wins_subnet
== NULL
) return;
267 work
= find_workgroupstruct(wins_subnet
, myworkgroup
, True
);
269 if (work
&& work
->dom_state
== DOMAIN_NONE
)
272 make_nmb_name(&n
,myworkgroup
,0x1b,scope
);
274 if (!find_name(wins_subnet
->namelist
, &n
, FIND_SELF
))
276 DEBUG(0,("%s add_domain_names: attempting to become domain \
277 master browser on workgroup %s %s\n",
278 timestring(), myworkgroup
, inet_ntoa(wins_subnet
->bcast_ip
)));
280 if (lp_wins_support())
282 /* use the wins server's capabilities (indirectly). if
283 someone has already registered the domain<1b>
284 name with the WINS server, then the WINS
285 server's job is to _check_ that the owner still
286 wants it, before giving it away.
289 DEBUG(1,("%s initiate become domain master for %s\n",
290 timestring(), myworkgroup
));
292 become_domain_master(wins_subnet
, work
);
296 /* send out a query to establish whether there's a
297 domain controller on the WINS subnet. if not,
298 we can become a domain controller. it's only
299 polite that we check, before claiming the
303 DEBUG(0,("add_domain_names:querying WINS \
304 for domain master on workgroup %s\n", myworkgroup
));
306 queue_netbios_pkt_wins(ClientNMB
,NMB_QUERY
,
310 False
, True
, ipzero
, ipzero
);
317 /****************************************************************************
318 add the domain logon server and domain master browser names
320 this code was written so that several samba servers can co-operate in
321 sharing the task of (one server) being a domain master, and of being
322 domain logon servers.
324 **************************************************************************/
325 void add_domain_names(time_t t
)
327 static time_t lastrun
= 0;
329 if (lastrun
!= 0 && t
< lastrun
+ CHECK_TIME_ADD_DOM_NAMES
* 60) return;
332 /* do the "internet group" - <1c> names */
333 add_domain_logon_names();
335 /* do the domain master names */
336 if (wins_subnet
!= NULL
)
338 /* if the registration of the <1b> name is successful, then
339 add_domain_master_bcast() will be called. this will
340 result in domain logon services being gracefully provided,
341 as opposed to the aggressive nature of 1.9.16p2 to 1.9.16p11.
343 which, due to a bug in namelogon.c from 1.9.16p2 to 1.9.16p11
344 cannot _provide_ domain master / domain logon services!!!
347 add_domain_master_wins();
351 add_domain_master_bcast();
355 /****************************************************************************
356 add the magic samba names, useful for finding samba servers
357 **************************************************************************/
358 void add_my_names(void)
360 struct subnet_record
*d
;
361 /* each subnet entry, including WINS pseudo-subnet, has SELF names */
363 /* XXXX if there was a transport layer added to samba (ipx/spx etc) then
364 there would be yet _another_ for-loop, this time on the transport type
367 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_INCLUDING_WINS(d
))
369 BOOL wins
= (lp_wins_support() && (d
== wins_subnet
));
371 add_my_name_entry(d
, myname
,0x20,nb_type
|NB_ACTIVE
);
372 add_my_name_entry(d
, myname
,0x03,nb_type
|NB_ACTIVE
);
373 add_my_name_entry(d
, myname
,0x00,nb_type
|NB_ACTIVE
);
375 /* these names are added permanently (ttl of zero) and will NOT be
376 refreshed with the WINS server */
377 add_netbios_entry(d
,"*",0x0,nb_type
|NB_ACTIVE
,0,SELF
,d
->myip
,False
,wins
);
378 add_netbios_entry(d
,"*",0x20,nb_type
|NB_ACTIVE
,0,SELF
,d
->myip
,False
,wins
);
379 add_netbios_entry(d
,"__SAMBA__",0x20,nb_type
|NB_ACTIVE
,0,SELF
,d
->myip
,False
,wins
);
380 add_netbios_entry(d
,"__SAMBA__",0x00,nb_type
|NB_ACTIVE
,0,SELF
,d
->myip
,False
,wins
);
385 /****************************************************************************
386 remove all the samba names... from a WINS server if necessary.
387 **************************************************************************/
388 void remove_my_names()
390 struct subnet_record
*d
;
392 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_INCLUDING_WINS(d
))
394 struct name_record
*n
, *next
;
396 for (n
= d
->namelist
; n
; n
= next
)
399 if (n
->source
== SELF
)
401 /* get all SELF names removed from the WINS server's database */
402 /* XXXX note: problem occurs if this removes the wrong one! */
404 remove_name_entry(d
,n
->name
.name
, n
->name
.name_type
);
411 /*******************************************************************
413 ******************************************************************/
414 void refresh_my_names(time_t t
)
416 struct subnet_record
*d
;
418 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_INCLUDING_WINS(d
))
420 struct name_record
*n
;
422 for (n
= d
->namelist
; n
; n
= n
->next
)
424 /* each SELF name has an individual time to be refreshed */
425 if (n
->source
== SELF
&& n
->refresh_time
< t
&&
428 add_my_name_entry(d
,n
->name
.name
,n
->name
.name_type
,
429 n
->ip_flgs
[0].nb_flags
);
430 /* they get a new lease on life :-) */
431 n
->death_time
+= GET_TTL(0);
432 n
->refresh_time
+= GET_TTL(0);
439 /*******************************************************************
440 queries names occasionally. an over-cautious, non-trusting WINS server!
442 this function has been added because nmbd could be restarted. it
443 is generally a good idea to check all the names that have been
446 XXXX which names to poll and which not can be refined at a later date.
447 ******************************************************************/
448 void query_refresh_names(time_t t
)
450 struct name_record
*n
;
451 struct subnet_record
*d
= wins_subnet
;
453 static time_t lasttime
= 0;
456 int name_refresh_time
= NAME_POLL_REFRESH_TIME
;
457 int max_count
= name_refresh_time
* 2 / NAME_POLL_INTERVAL
;
458 if (max_count
> 10) max_count
= 10;
460 name_refresh_time
= NAME_POLL_INTERVAL
* max_count
/ 2;
462 /* if (!lp_poll_wins()) return; polling of registered names allowed */
466 if (!lasttime
) lasttime
= t
;
467 if (t
- lasttime
< NAME_POLL_INTERVAL
) return;
469 lasttime
= time(NULL
);
471 for (n
= d
->namelist
; n
; n
= n
->next
)
473 /* only do unique, registered names */
475 if (n
->source
!= REGISTER
) continue;
476 if (!NAME_GROUP(n
->ip_flgs
[0].nb_flags
)) continue;
478 if (n
->refresh_time
< t
)
480 DEBUG(3,("Polling name %s\n", namestr(&n
->name
)));
482 queue_netbios_packet(d
,ClientNMB
,NMB_QUERY
,NAME_QUERY_CONFIRM
,
483 n
->name
.name
, n
->name
.name_type
,
485 False
,False
,n
->ip_flgs
[0].ip
,n
->ip_flgs
[0].ip
);
489 if (count
>= max_count
)
491 /* don't do too many of these at once, but do enough to
492 cover everyone in the list */
496 /* this name will be checked on again, if it's not removed */
497 n
->refresh_time
+= name_refresh_time
;