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: namedbname.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 created module namedbname containing name database functions
34 extern int DEBUGLEVEL
;
37 extern struct in_addr ipzero
;
38 extern struct in_addr wins_ip
;
39 extern BOOL updatedlists
;
41 extern struct subnet_record
*subnetlist
;
43 #define WINS_LIST "wins.dat"
45 uint16 nb_type
= 0; /* samba's NetBIOS name type */
48 /****************************************************************************
49 samba's NetBIOS name type
51 XXXX maybe functionality could be set: B, M, P or H name registration
52 and resolution could be set through nb_type. just a thought.
53 ****************************************************************************/
54 void set_samba_nb_type(void)
56 if (lp_wins_support() || (*lp_wins_server()))
58 nb_type
= NB_MFLAG
; /* samba is a 'hybrid' node type */
62 nb_type
= NB_BFLAG
; /* samba is broadcast-only node type */
67 /****************************************************************************
68 true if two netbios names are equal
69 ****************************************************************************/
70 BOOL
name_equal(struct nmb_name
*n1
,struct nmb_name
*n2
)
72 return n1
->name_type
== n2
->name_type
&&
73 strequal(n1
->name
,n2
->name
) &&
74 strequal(n1
->scope
,n2
->scope
);
78 /****************************************************************************
79 true if the netbios name is ^1^2__MSBROWSE__^2^1
81 note: this name is registered if as a master browser or backup browser
82 you are responsible for a workgroup (when you announce a domain by
83 broadcasting on your local subnet, you announce it as coming from this
84 name: see announce_host()).
86 **************************************************************************/
87 BOOL
ms_browser_name(char *name
, int type
)
89 return strequal(name
,MSBROWSE
) && type
== 0x01;
93 /****************************************************************************
94 add a netbios name into the namelist
95 **************************************************************************/
96 static void add_name(struct subnet_record
*d
, struct name_record
*n
)
98 struct name_record
*n2
;
110 for (n2
= d
->namelist
; n2
->next
; n2
= n2
->next
) ;
116 if((d
== wins_subnet
) && lp_wins_support())
121 /****************************************************************************
122 remove a name from the namelist. The pointer must be an element just
124 **************************************************************************/
125 void remove_name(struct subnet_record
*d
, struct name_record
*n
)
127 struct name_record
*nlist
;
132 while (nlist
&& nlist
!= n
) nlist
= nlist
->next
;
136 if (nlist
->next
) nlist
->next
->prev
= nlist
->prev
;
137 if (nlist
->prev
) nlist
->prev
->next
= nlist
->next
;
139 if(nlist
== d
->namelist
)
140 d
->namelist
= nlist
->next
;
142 if(nlist
->ip_flgs
!= NULL
)
143 free(nlist
->ip_flgs
);
147 if((d
== wins_subnet
) && lp_wins_support())
152 /****************************************************************************
153 find a name in a namelist.
154 **************************************************************************/
155 struct name_record
*find_name(struct name_record
*n
,
156 struct nmb_name
*name
, int search
)
158 struct name_record
*ret
;
160 for (ret
= n
; ret
; ret
= ret
->next
)
162 if (name_equal(&ret
->name
,name
))
164 /* self search: self names only */
165 if ((search
&FIND_SELF
) == FIND_SELF
&& ret
->source
!= SELF
)
169 DEBUG(9,("find_name: found name %s(%02x)\n",
170 name
->name
, name
->name_type
));
174 DEBUG(9,("find_name: name %s(%02x) NOT FOUND\n", name
->name
,
180 /****************************************************************************
181 find a name in the domain database namelist
182 search can be any of:
183 FIND_SELF - look exclusively for names the samba server has added for itself
184 FIND_LOCAL - look for names in the local subnet record.
185 FIND_WINS - look for names in the WINS record
186 **************************************************************************/
187 struct name_record
*find_name_search(struct subnet_record
**d
,
188 struct nmb_name
*name
,
189 int search
, struct in_addr ip
)
191 if (d
== NULL
) return NULL
; /* bad error! */
193 if (search
& FIND_LOCAL
) {
195 struct name_record
*n
= find_name((*d
)->namelist
, name
, search
);
196 DEBUG(4,("find_name on local: %s %s search %x\n",
197 namestr(name
),inet_ntoa(ip
), search
));
202 if (!(search
& FIND_WINS
)) return NULL
;
204 /* find WINS subnet record. */
207 if (*d
== NULL
) return NULL
;
209 DEBUG(4,("find_name on WINS: %s %s search %x\n",
210 namestr(name
),inet_ntoa(ip
), search
));
211 return find_name((*d
)->namelist
, name
, search
);
215 /****************************************************************************
216 dump a copy of the name table
217 **************************************************************************/
218 void dump_names(void)
220 struct name_record
*n
;
221 fstring fname
, fnamenew
;
222 time_t t
= time(NULL
);
226 if(lp_wins_support() == False
|| wins_subnet
== 0)
229 strcpy(fname
,lp_lockdir());
230 trim_string(fname
,NULL
,"/");
232 strcat(fname
,WINS_LIST
);
233 strcpy(fnamenew
,fname
);
234 strcat(fnamenew
,".");
236 f
= fopen(fnamenew
,"w");
240 DEBUG(3,("Can't open %s - %s\n",fnamenew
,strerror(errno
)));
244 DEBUG(4,("Dump of WINS name table:\n"));
246 for (n
= wins_subnet
->namelist
; n
; n
= n
->next
)
250 DEBUG(4,("%15s ", inet_ntoa(wins_subnet
->bcast_ip
)));
251 DEBUG(4,("%15s ", inet_ntoa(wins_subnet
->mask_ip
)));
252 DEBUG(4,("%-19s TTL=%ld ",
254 n
->death_time
?n
->death_time
-t
:0));
256 for (i
= 0; i
< n
->num_ips
; i
++)
258 DEBUG(4,("%15s NB=%2x source=%d",
259 inet_ntoa(n
->ip_flgs
[i
].ip
),
260 n
->ip_flgs
[i
].nb_flags
,n
->source
));
265 if (f
&& ((n
->source
== REGISTER
) || (n
->source
== SELF
)))
267 /* XXXX i have little imagination as to how to output nb_flags as
268 anything other than as a hexadecimal number :-) */
270 fprintf(f
, "%s#%02x %ld ",
271 n
->name
.name
,n
->name
.name_type
, /* XXXX ignore scope for now */
274 for (i
= 0; i
< n
->num_ips
; i
++)
276 fprintf(f
, "%s %2x%c ",
277 inet_ntoa(n
->ip_flgs
[i
].ip
),
278 n
->ip_flgs
[i
].nb_flags
, (n
->source
== REGISTER
? 'R' : 'S'));
287 chmod(fnamenew
,0644);
288 rename(fnamenew
,fname
);
290 DEBUG(3,("Wrote wins database %s\n",fname
));
294 /****************************************************************************
295 load a netbios name database file
297 XXXX we cannot cope with loading Internet Group names, yet
298 ****************************************************************************/
299 void load_netbios_names(void)
301 struct subnet_record
*d
= wins_subnet
;
309 strcpy(fname
,lp_lockdir());
310 trim_string(fname
,NULL
,"/");
312 strcat(fname
,WINS_LIST
);
314 f
= fopen(fname
,"r");
317 DEBUG(2,("Can't open wins database file %s\n",fname
));
323 pstring name_str
, ip_str
, ttd_str
, nb_flags_str
;
327 unsigned int nb_flags
;
329 struct in_addr ipaddr
;
331 enum name_source source
;
338 if (!fgets_slash(line
,sizeof(pstring
),f
)) continue;
340 if (*line
== '#') continue;
344 if (next_token(&ptr
,name_str
,NULL
)) ++count
;
345 if (next_token(&ptr
,ttd_str
,NULL
)) ++count
;
346 if (next_token(&ptr
,ip_str
,NULL
)) ++count
;
347 if (next_token(&ptr
,nb_flags_str
,NULL
)) ++count
;
349 if (count
<= 0) continue;
352 DEBUG(0,("Ill formed wins line"));
353 DEBUG(0,("[%s]: name#type abs_time ip nb_flags\n",line
));
357 /* Deal with SELF or REGISTER name encoding. Default is REGISTER
358 for compatibility with old nmbds. */
359 if(nb_flags_str
[strlen(nb_flags_str
)-1] == 'S')
361 DEBUG(5,("Ignoring SELF name %s\n", line
));
365 if(nb_flags_str
[strlen(nb_flags_str
)-1] == 'R')
366 nb_flags_str
[strlen(nb_flags_str
)-1] = '\0';
368 /* netbios name. # divides the name from the type (hex): netbios#xx */
369 strcpy(name
,name_str
);
371 p
= strchr(name
,'#');
375 sscanf(p
+1,"%x",&type
);
378 /* decode the netbios flags (hex) and the time-to-die (seconds) */
379 sscanf(nb_flags_str
,"%x",&nb_flags
);
380 sscanf(ttd_str
,"%ld",&ttd
);
382 ipaddr
= *interpret_addr2(ip_str
);
384 if (ip_equal(ipaddr
,ipzero
)) {
392 DEBUG(4, ("add WINS line: %s#%02x %ld %s %2x\n",
393 name
,type
, ttd
, inet_ntoa(ipaddr
), nb_flags
));
395 /* add all entries that have 60 seconds or more to live */
396 if (ttd
- 60 > time(NULL
) || ttd
== 0)
398 time_t t
= (ttd
?ttd
-time(NULL
):0) / 3;
400 /* add netbios entry read from the wins.dat file. IF it's ok */
401 add_netbios_entry(d
,name
,type
,nb_flags
,t
,source
,ipaddr
,True
,True
);
409 /****************************************************************************
410 remove an entry from the name list
411 ****************************************************************************/
412 void remove_netbios_name(struct subnet_record
*d
,
413 char *name
,int type
, enum name_source source
,
417 struct name_record
*n
;
419 make_nmb_name(&nn
, name
, type
, scope
);
420 n
= find_name_search(&d
, &nn
, FIND_LOCAL
, ip
);
422 if (n
&& n
->source
== source
) remove_name(d
,n
);
426 /****************************************************************************
427 add an entry to the name list.
429 this is a multi-purpose function.
431 it adds samba's own names in to its records on each interface, keeping a
432 record of whether it is a master browser, domain master, or WINS server.
434 it also keeps a record of WINS entries.
436 ****************************************************************************/
437 struct name_record
*add_netbios_entry(struct subnet_record
*d
,
438 char *name
, int type
, int nb_flags
,
439 int ttl
, enum name_source source
, struct in_addr ip
,
440 BOOL new_only
,BOOL wins
)
442 struct name_record
*n
;
443 struct name_record
*n2
=NULL
;
444 struct subnet_record
*found_subnet
= 0;
446 BOOL self
= (source
== SELF
);
448 /* add the name to the WINS list if the name comes from a directed query */
449 search
|= wins
? FIND_WINS
: FIND_LOCAL
;
451 /* If it's a local search then we need to set the subnet
452 we are looking at. */
453 if(search
& FIND_LOCAL
)
456 /* search for SELF names only */
457 search
|= self
? FIND_SELF
: 0;
461 if (!wins
&& (type
!= 0x1b))
463 /* the only broadcast (non-WINS) names we are adding are ours
464 (SELF) and Domain Master type names */
467 if(wins
&& (type
== 0x1d))
469 /* Do not allow any 0x1d names to be registered in a WINS,
470 database although we return success for them.
476 n
= (struct name_record
*)malloc(sizeof(*n
));
477 if (!n
) return(NULL
);
479 bzero((char *)n
,sizeof(*n
));
481 n
->num_ips
= 1; /* XXXX ONLY USE THIS FUNCTION FOR ONE ENTRY */
482 n
->ip_flgs
= (struct nmb_ip
*)malloc(sizeof(*n
->ip_flgs
) * n
->num_ips
);
489 make_nmb_name(&n
->name
,name
,type
,scope
);
491 if ((n2
= find_name_search(&found_subnet
, &n
->name
, search
, new_only
?ipzero
:ip
)))
495 if (new_only
|| (n2
->source
==SELF
&& source
!=SELF
)) return n2
;
501 n
->death_time
= time(NULL
)+ttl
*3;
502 n
->refresh_time
= time(NULL
)+GET_TTL(ttl
);
504 /* XXXX only one entry expected with this function */
505 n
->ip_flgs
[0].ip
= ip
;
506 n
->ip_flgs
[0].nb_flags
= nb_flags
;
510 if (!n2
) add_name(d
,n
);
512 DEBUG(3,("Added netbios name %s at %s ttl=%d nb_flags=%2x to interface %s\n",
513 namestr(&n
->name
),inet_ntoa(ip
),ttl
,nb_flags
,
514 ip_equal(d
->bcast_ip
, wins_ip
) ? "WINS" : (char *)inet_ntoa(d
->bcast_ip
)));
520 /*******************************************************************
521 expires old names in the namelist
522 ******************************************************************/
523 void expire_names(time_t t
)
525 struct name_record
*n
;
526 struct name_record
*next
;
527 struct subnet_record
*d
;
529 /* expire old names */
530 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_INCLUDING_WINS(d
))
532 for (n
= d
->namelist
; n
; n
= next
)
535 if (n
->death_time
&& n
->death_time
< t
)
537 if (n
->source
== SELF
)
539 DEBUG(3,("not expiring SELF name %s\n", namestr(&n
->name
)));
540 n
->death_time
+= 300;
543 DEBUG(3,("Removing dead name %s\n", namestr(&n
->name
)));
545 if (n
->prev
) n
->prev
->next
= n
->next
;
546 if (n
->next
) n
->next
->prev
= n
->prev
;
548 if (d
->namelist
== n
) d
->namelist
= n
->next
;
550 if(n
->ip_flgs
!= NULL
)
559 /***************************************************************************
560 assume a WINS name is a dns name, and do a gethostbyname() on it.
561 ****************************************************************************/
562 struct name_record
*dns_name_search(struct nmb_name
*question
, int Time
)
564 int name_type
= question
->name_type
;
565 char *qname
= question
->name
;
566 BOOL dns_type
= (name_type
== 0x20 || name_type
== 0);
567 struct in_addr dns_ip
;
569 if (wins_subnet
== NULL
)
572 DEBUG(3,("Search for %s - ", namestr(question
)));
574 /* only do DNS lookups if the query is for type 0x20 or type 0x0 */
577 DEBUG(3,("types 0x20 0x0 only: name not found\n"));
581 /* look it up with DNS */
582 dns_ip
.s_addr
= interpret_addr(qname
);
586 /* no luck with DNS. We could possibly recurse here XXXX */
587 DEBUG(3,("not found. no recursion.\n"));
588 /* add the fail to WINS cache of names. give it 1 hour in the cache */
589 add_netbios_entry(wins_subnet
,qname
,name_type
,NB_ACTIVE
,60*60,DNSFAIL
,dns_ip
,
594 DEBUG(3,("found with DNS: %s\n", inet_ntoa(dns_ip
)));
596 /* add it to our WINS cache of names. give it 2 hours in the cache */
597 return add_netbios_entry(wins_subnet
,qname
,name_type
,NB_ACTIVE
,2*60*60,DNS
,dns_ip
,