wrong answer response resource record type expected, so the response packet
[Samba.git] / source / namedbname.c
blob0ec8cdbbbe581a2a20c504c4406f5c8faeeb2c7a
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1996
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
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 created module namedbname containing name database functions
32 #include "includes.h"
34 extern int DEBUGLEVEL;
36 extern pstring scope;
37 extern struct in_addr ipzero;
38 extern struct in_addr ipgrp;
40 extern struct subnet_record *subnetlist;
42 #define WINS_LIST "wins.dat"
44 uint16 nb_type = 0; /* samba's NetBIOS name type */
47 /****************************************************************************
48 samba's NetBIOS name type
50 XXXX maybe functionality could be set: B, M, P or H name registration
51 and resolution could be set through nb_type. just a thought.
52 ****************************************************************************/
53 void set_samba_nb_type(void)
55 if (lp_wins_support() || (*lp_wins_server()))
57 nb_type = NB_MFLAG; /* samba is a 'hybrid' node type */
59 else
61 nb_type = NB_BFLAG; /* samba is broadcast-only node type */
66 /****************************************************************************
67 true if two netbios names are equal
68 ****************************************************************************/
69 BOOL name_equal(struct nmb_name *n1,struct nmb_name *n2)
71 return n1->name_type == n2->name_type &&
72 strequal(n1->name ,n2->name ) &&
73 strequal(n1->scope,n2->scope);
77 /****************************************************************************
78 true if the netbios name is ^1^2__MSBROWSE__^2^1
80 note: this name is registered if as a master browser or backup browser
81 you are responsible for a workgroup (when you announce a domain by
82 broadcasting on your local subnet, you announce it as coming from this
83 name: see announce_host()).
85 **************************************************************************/
86 BOOL ms_browser_name(char *name, int type)
88 return strequal(name,MSBROWSE) && type == 0x01;
92 /****************************************************************************
93 add a netbios name into the namelist
94 **************************************************************************/
95 static void add_name(struct subnet_record *d, struct name_record *n)
97 struct name_record *n2;
99 if (!d) return;
101 if (!d->namelist)
103 d->namelist = n;
104 n->prev = NULL;
105 n->next = NULL;
106 return;
109 for (n2 = d->namelist; n2->next; n2 = n2->next) ;
111 n2->next = n;
112 n->next = NULL;
113 n->prev = n2;
117 /****************************************************************************
118 remove a name from the namelist. The pointer must be an element just
119 retrieved
120 **************************************************************************/
121 void remove_name(struct subnet_record *d, struct name_record *n)
123 struct name_record *nlist;
124 if (!d) return;
126 nlist = d->namelist;
128 while (nlist && nlist != n) nlist = nlist->next;
130 if (nlist)
132 if (nlist->next) nlist->next->prev = nlist->prev;
133 if (nlist->prev) nlist->prev->next = nlist->next;
134 free(nlist);
139 /****************************************************************************
140 find a name in a namelist.
141 **************************************************************************/
142 struct name_record *find_name(struct name_record *n,
143 struct nmb_name *name,
144 int search)
146 struct name_record *ret;
148 for (ret = n; ret; ret = ret->next)
150 if (name_equal(&ret->name,name))
152 /* self search: self names only */
153 if ((search&FIND_SELF) == FIND_SELF &&
154 ret->source != SELF)
155 continue;
157 return ret;
160 return NULL;
164 /****************************************************************************
165 find a name in the domain database namelist
166 search can be any of:
167 FIND_SELF - look exclusively for names the samba server has added for itself
168 FIND_LOCAL - look for names in the local subnet record.
169 FIND_WINS - look for names in the WINS record
170 **************************************************************************/
171 struct name_record *find_name_search(struct subnet_record **d,
172 struct nmb_name *name,
173 int search, struct in_addr ip)
175 if (d == NULL) return NULL; /* bad error! */
177 if (search & FIND_LOCAL) {
178 if (*d != NULL) {
179 struct name_record *n = find_name((*d)->namelist, name, search);
180 DEBUG(4,("find_name on local: %s %s search %x\n",
181 namestr(name),inet_ntoa(ip), search));
182 if (n) return n;
186 if (!(search & FIND_WINS)) return NULL;
188 /* find WINS subnet record. */
189 *d = find_subnet(ipgrp);
191 if (*d == NULL) return NULL;
193 DEBUG(4,("find_name on WINS: %s %s search %x\n",
194 namestr(name),inet_ntoa(ip), search));
195 return find_name((*d)->namelist, name, search);
199 /****************************************************************************
200 dump a copy of the name table
201 **************************************************************************/
202 void dump_names(void)
204 struct name_record *n;
205 struct subnet_record *d;
206 fstring fname, fnamenew;
207 time_t t = time(NULL);
209 FILE *f;
211 strcpy(fname,lp_lockdir());
212 trim_string(fname,NULL,"/");
213 strcat(fname,"/");
214 strcat(fname,WINS_LIST);
215 strcpy(fnamenew,fname);
216 strcat(fnamenew,".");
218 f = fopen(fnamenew,"w");
220 if (!f)
222 DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
225 DEBUG(3,("Dump of local name table:\n"));
227 for (d = subnetlist; d; d = d->next)
228 for (n = d->namelist; n; n = n->next)
230 int i;
232 DEBUG(3,("%15s ", inet_ntoa(d->bcast_ip)));
233 DEBUG(3,("%15s ", inet_ntoa(d->mask_ip)));
234 DEBUG(3,("%-19s TTL=%ld ",
235 namestr(&n->name),
236 n->death_time?n->death_time-t:0));
238 for (i = 0; i < n->num_ips; i++)
240 DEBUG(3,("%15s NB=%2x source=%d",
241 inet_ntoa(n->ip_flgs[i].ip),
242 n->ip_flgs[i].nb_flags,n->source));
245 DEBUG(3,("\n"));
247 if (f && ip_equal(d->bcast_ip, ipgrp) && n->source == REGISTER)
249 /* XXXX i have little imagination as to how to output nb_flags as
250 anything other than as a hexadecimal number :-) */
252 fprintf(f, "%s#%02x %ld ",
253 n->name.name,n->name.name_type, /* XXXX ignore scope for now */
254 n->death_time);
256 for (i = 0; i < n->num_ips; i++)
258 fprintf(f, "%s %2x ",
259 inet_ntoa(n->ip_flgs[i].ip),
260 n->ip_flgs[i].nb_flags);
262 fprintf(f, "\n");
267 fclose(f);
268 unlink(fname);
269 chmod(fnamenew,0644);
270 rename(fnamenew,fname);
272 DEBUG(3,("Wrote wins database %s\n",fname));
276 /****************************************************************************
277 load a netbios name database file
279 XXXX we cannot cope with loading Internet Group names, yet
280 ****************************************************************************/
281 void load_netbios_names(void)
283 struct subnet_record *d = find_subnet(ipgrp);
284 fstring fname;
286 FILE *f;
287 pstring line;
289 if (!d) return;
291 strcpy(fname,lp_lockdir());
292 trim_string(fname,NULL,"/");
293 strcat(fname,"/");
294 strcat(fname,WINS_LIST);
296 f = fopen(fname,"r");
298 if (!f) {
299 DEBUG(2,("Can't open wins database file %s\n",fname));
300 return;
303 while (!feof(f))
305 pstring name_str, ip_str, ttd_str, nb_flags_str;
307 pstring name;
308 int type = 0;
309 unsigned int nb_flags;
310 time_t ttd;
311 struct in_addr ipaddr;
313 enum name_source source;
315 char *ptr;
316 int count = 0;
318 char *p;
320 if (!fgets_slash(line,sizeof(pstring),f)) continue;
322 if (*line == '#') continue;
324 ptr = line;
326 if (next_token(&ptr,name_str ,NULL)) ++count;
327 if (next_token(&ptr,ttd_str ,NULL)) ++count;
328 if (next_token(&ptr,ip_str ,NULL)) ++count;
329 if (next_token(&ptr,nb_flags_str,NULL)) ++count;
331 if (count <= 0) continue;
333 if (count != 4) {
334 DEBUG(0,("Ill formed wins line"));
335 DEBUG(0,("[%s]: name#type abs_time ip nb_flags\n",line));
336 continue;
339 /* netbios name. # divides the name from the type (hex): netbios#xx */
340 strcpy(name,name_str);
342 p = strchr(name,'#');
344 if (p) {
345 *p = 0;
346 sscanf(p+1,"%x",&type);
349 /* decode the netbios flags (hex) and the time-to-die (seconds) */
350 sscanf(nb_flags_str,"%x",&nb_flags);
351 sscanf(ttd_str,"%ld",&ttd);
353 ipaddr = *interpret_addr2(ip_str);
355 if (ip_equal(ipaddr,ipzero)) {
356 source = SELF;
358 else
360 source = REGISTER;
363 DEBUG(4, ("add WINS line: %s#%02x %ld %s %2x\n",
364 name,type, ttd, inet_ntoa(ipaddr), nb_flags));
366 /* add all entries that have 60 seconds or more to live */
367 if (ttd - 60 > time(NULL) || ttd == 0)
369 time_t t = (ttd?ttd-time(NULL):0) / 3;
371 /* add netbios entry read from the wins.dat file. IF it's ok */
372 add_netbios_entry(d,name,type,nb_flags,t,source,ipaddr,True,True);
376 fclose(f);
380 /****************************************************************************
381 remove an entry from the name list
382 ****************************************************************************/
383 void remove_netbios_name(struct subnet_record *d,
384 char *name,int type, enum name_source source,
385 struct in_addr ip)
387 struct nmb_name nn;
388 struct name_record *n;
390 make_nmb_name(&nn, name, type, scope);
391 n = find_name_search(&d, &nn, FIND_LOCAL, ip);
393 if (n && n->source == source) remove_name(d,n);
397 /****************************************************************************
398 add an entry to the name list.
400 this is a multi-purpose function.
402 it adds samba's own names in to its records on each interface, keeping a
403 record of whether it is a master browser, domain master, or WINS server.
405 it also keeps a record of WINS entries.
407 ****************************************************************************/
408 struct name_record *add_netbios_entry(struct subnet_record *d,
409 char *name, int type, int nb_flags,
410 int ttl, enum name_source source, struct in_addr ip,
411 BOOL new_only,BOOL wins)
413 struct name_record *n;
414 struct name_record *n2=NULL;
415 int search = 0;
416 BOOL self = source == SELF;
418 /* add the name to the WINS list if the name comes from a directed query */
419 search |= wins ? FIND_WINS : FIND_LOCAL;
420 /* search for SELF names only */
421 search |= self ? FIND_SELF : 0;
423 if (!self)
425 if (!wins && type != 0x1b)
427 /* the only broadcast (non-WINS) names we are adding are ours
428 (SELF) and Domain Master type names */
429 return NULL;
433 n = (struct name_record *)malloc(sizeof(*n));
434 if (!n) return(NULL);
436 bzero((char *)n,sizeof(*n));
438 n->num_ips = 1; /* XXXX ONLY USE THIS FUNCTION FOR ONE ENTRY */
439 n->ip_flgs = (struct nmb_ip*)malloc(sizeof(*n->ip_flgs) * n->num_ips);
440 if (!n->ip_flgs)
442 free(n);
443 return NULL;
446 make_nmb_name(&n->name,name,type,scope);
448 if ((n2 = find_name_search(&d, &n->name, search, new_only?ipzero:ip)))
450 free(n->ip_flgs);
451 free(n);
452 if (new_only || (n2->source==SELF && source!=SELF)) return n2;
453 n = n2;
456 if (ttl)
457 n->death_time = time(NULL)+ttl*3;
458 n->refresh_time = time(NULL)+GET_TTL(ttl);
460 /* XXXX only one entry expected with this function */
461 n->ip_flgs[0].ip = ip;
462 n->ip_flgs[0].nb_flags = nb_flags;
464 n->source = source;
466 if (!n2) add_name(d,n);
468 DEBUG(3,("Added netbios name %s at %s ttl=%d nb_flags=%2x\n",
469 namestr(&n->name),inet_ntoa(ip),ttl,nb_flags));
471 return(n);
475 /*******************************************************************
476 expires old names in the namelist
477 ******************************************************************/
478 void expire_names(time_t t)
480 struct name_record *n;
481 struct name_record *next;
482 struct subnet_record *d;
484 /* expire old names */
485 for (d = subnetlist; d; d = d->next)
487 for (n = d->namelist; n; n = next)
489 next = n->next;
490 if (n->death_time && n->death_time < t)
492 if (n->source == SELF) {
493 DEBUG(3,("not expiring SELF name %s\n", namestr(&n->name)));
494 n->death_time += 300;
495 continue;
497 DEBUG(3,("Removing dead name %s\n", namestr(&n->name)));
499 if (n->prev) n->prev->next = n->next;
500 if (n->next) n->next->prev = n->prev;
502 if (d->namelist == n) d->namelist = n->next;
504 free(n->ip_flgs);
505 free(n);
512 /***************************************************************************
513 reply to a name query
514 ****************************************************************************/
515 struct name_record *search_for_name(struct subnet_record **d,
516 struct nmb_name *question,
517 struct in_addr ip, int Time, int search)
519 int name_type = question->name_type;
520 char *qname = question->name;
521 BOOL dns_type = (name_type == 0x20 || name_type == 0);
523 struct name_record *n;
525 DEBUG(3,("Search for %s from %s - ", namestr(question), inet_ntoa(ip)));
527 /* first look up name in cache */
528 n = find_name_search(d,question,search,ip);
530 if (*d == NULL) return NULL;
532 /* now try DNS lookup. */
533 if (!n)
535 struct in_addr dns_ip;
536 uint32 a;
538 /* only do DNS lookups if the query is for type 0x20 or type 0x0 */
539 if (!dns_type && name_type != 0x1b)
541 DEBUG(3,("types 0x20 0x1b 0x0 only: name not found\n"));
542 return NULL;
545 /* look it up with DNS */
546 a = interpret_addr(qname);
548 putip((char *)&dns_ip,(char *)&a);
550 if (!a)
552 /* no luck with DNS. We could possibly recurse here XXXX */
553 DEBUG(3,("no recursion.\n"));
554 /* add the fail to our WINS cache of names. give it 1 hour in the cache */
555 add_netbios_entry(*d,qname,name_type,NB_ACTIVE,60*60,DNSFAIL,dns_ip,
556 True, True);
557 return NULL;
560 /* add it to our WINS cache of names. give it 2 hours in the cache */
561 n = add_netbios_entry(*d,qname,name_type,NB_ACTIVE,2*60*60,DNS,dns_ip,
562 True,True);
564 /* failed to add it? yikes! */
565 if (!n) return NULL;
568 /* is our entry already dead? */
569 if (n->death_time)
571 if (n->death_time < Time) return False;
574 /* it may have been an earlier failure */
575 if (n->source == DNSFAIL)
577 DEBUG(3,("DNSFAIL\n"));
578 return NULL;
581 DEBUG(3,("OK %s\n",inet_ntoa(n->ip_flgs[0].ip)));
583 return n;