Fixes for manpage from Bill Hawes <whawes@star.net>.
[Samba/gbeck.git] / source / nameservreply.c
blobe1738007df1b6caeea3fcdfe7be257861a966ea2
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: nameservreply.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 nameservreply containing NetBIOS reply functions
33 #include "includes.h"
35 extern int ClientNMB;
37 extern int DEBUGLEVEL;
39 extern struct in_addr wins_ip;
42 /****************************************************************************
43 add a netbios entry. respond to the (possibly new) owner.
44 **************************************************************************/
45 void add_name_respond(struct subnet_record *d, int fd, struct in_addr from_ip,
46 uint16 response_id,
47 struct nmb_name *name,
48 int nb_flags, int ttl, struct in_addr register_ip,
49 BOOL new_owner, struct in_addr reply_to_ip)
51 /* register the old or the new owners' ip */
52 add_netbios_entry(d,name->name,name->name_type,
53 nb_flags,ttl,REGISTER,register_ip,False,True);
55 /* reply yes or no to the host that requested the name */
56 send_name_response(fd,from_ip, response_id, NMB_REG,
57 new_owner, True,
58 name, nb_flags, ttl, reply_to_ip);
61 /****************************************************************************
62 send a registration / release response: pos/neg
63 **************************************************************************/
64 void send_name_response(int fd, struct in_addr from_ip,
65 int name_trn_id, int opcode, BOOL success, BOOL recurse,
66 struct nmb_name *reply_name, int nb_flags, int ttl,
67 struct in_addr ip)
69 char rdata[6];
70 struct packet_struct p;
72 int rcode = 0;
74 if (success == False)
76 /* NEGATIVE RESPONSE */
77 rcode = 6;
79 else if (opcode == NMB_REG && recurse == False)
81 /* END-NODE CHALLENGE REGISTRATION RESPONSE */
82 rcode = 0;
85 rdata[0] = nb_flags;
86 rdata[1] = 0;
87 putip(&rdata[2],(char *)&ip);
89 p.ip = from_ip;
90 p.port = NMB_PORT;
91 p.fd = fd;
92 p.timestamp = time(NULL);
93 p.packet_type = NMB_PACKET;
95 reply_netbios_packet(&p,name_trn_id,
96 rcode,opcode,opcode,recurse,
97 reply_name, 0x20, 0x1,
98 ttl,
99 rdata, 6);
103 /****************************************************************************
104 reply to a name release
105 ****************************************************************************/
106 void reply_name_release(struct packet_struct *p)
108 struct nmb_packet *nmb = &p->packet.nmb;
109 struct in_addr ip;
110 int nb_flags = nmb->additional->rdata[0];
111 BOOL bcast = nmb->header.nm_flags.bcast;
112 struct name_record *n;
113 struct subnet_record *d = NULL;
114 int search = 0;
115 BOOL success = False;
117 putip((char *)&ip,&nmb->additional->rdata[2]);
119 DEBUG(3,("Name release on name %s\n",
120 namestr(&nmb->question.question_name)));
122 if (!(d = find_req_subnet(p->ip, bcast)))
124 DEBUG(3,("response packet: bcast %s not known\n",
125 inet_ntoa(p->ip)));
126 return;
129 if (bcast)
130 search |= FIND_LOCAL;
131 else
132 search |= FIND_WINS;
134 n = find_name_search(&d, &nmb->question.question_name,
135 search, ip);
137 /* XXXX under what conditions should we reject the removal?? */
138 if (n && n->ip_flgs[0].nb_flags == nb_flags)
140 success = True;
142 remove_name(d,n);
143 n = NULL;
146 if (bcast) return;
148 /* Send a NAME RELEASE RESPONSE (pos/neg) see rfc1002.txt 4.2.10-11 */
149 send_name_response(p->fd,p->ip, nmb->header.name_trn_id, NMB_REL,
150 success, False,
151 &nmb->question.question_name, nb_flags, 0, ip);
155 /****************************************************************************
156 reply to a reg request
157 **************************************************************************/
158 void reply_name_reg(struct packet_struct *p)
160 struct nmb_packet *nmb = &p->packet.nmb;
161 struct nmb_name *question = &nmb->question.question_name;
163 struct nmb_name *reply_name = question;
165 char *qname = question->name;
166 int qname_type = question->name_type;
168 BOOL bcast = nmb->header.nm_flags.bcast;
170 int ttl = GET_TTL(nmb->additional->ttl);
171 int nb_flags = nmb->additional->rdata[0];
172 BOOL group = NAME_GROUP(nb_flags);
174 struct subnet_record *d = NULL;
175 struct name_record *n = NULL;
177 BOOL success = True;
178 BOOL secured_redirect = False;
180 struct in_addr ip, from_ip;
181 int search = 0;
183 putip((char *)&from_ip,&nmb->additional->rdata[2]);
184 ip = from_ip;
186 DEBUG(3,("Name registration for name %s at %s - ",
187 namestr(question),inet_ntoa(ip)));
189 if (group)
191 /* apparently we should return 255.255.255.255 for group queries
192 (email from MS) */
193 ip = wins_ip;
196 if (!(d = find_req_subnet(p->ip, bcast)))
198 DEBUG(3,("response packet: bcast %s not known\n",
199 inet_ntoa(p->ip)));
200 return;
203 if (bcast)
204 search |= FIND_LOCAL;
205 else
206 search |= FIND_WINS;
208 /* see if the name already exists */
209 n = find_name_search(&d, question, search, from_ip);
211 if (n)
213 DEBUG(3,("found\n"));
214 if (!group) /* unique names */
216 if (n->source == SELF || NAME_GROUP(n->ip_flgs[0].nb_flags))
218 /* no-one can register one of samba's names, nor can they
219 register a name that's a group name as a unique name */
221 success = False;
223 else if(!ip_equal(ip, n->ip_flgs[0].ip))
225 /* XXXX rfc1001.txt says:
226 * if we are doing secured WINS, we must send a Wait-Acknowledge
227 * packet (WACK) to the person who wants the name, then do a
228 * name query on the person who currently owns the unique name.
229 * if the current owner still says they own it, the person who wants
230 * the name can't have it. if they do not, or are not alive, they can.
233 secured_redirect = True;
235 reply_name = &n->name;
237 else
239 n->ip_flgs[0].ip = ip;
240 n->death_time = ttl?p->timestamp+ttl*3:0;
241 DEBUG(3,("%s owner: %s\n",namestr(&n->name),inet_ntoa(n->ip_flgs[0].ip)));
244 else
246 /* refresh the name */
247 if (n->source != SELF)
249 n->death_time = ttl?p->timestamp + ttl*3:0;
253 /* XXXX bug reported by terryt@ren.pc.athabascau.ca */
254 /* names that people have checked for and not found get DNSFAILed.
255 we need to update the name record if someone then registers */
257 if (n->source == DNSFAIL)
258 n->source = REGISTER;
261 else
263 DEBUG(3,("not found\n"));
264 /* add the name to our name/subnet, or WINS, database */
265 n = add_netbios_entry(d,qname,qname_type,nb_flags,ttl,REGISTER,ip,
266 True,!bcast);
269 /* if samba owns a unique name on a subnet, then it must respond and
270 disallow the attempted registration. if the registration is
271 successful by broadcast, only then is there no need to respond
272 (implicit registration: see rfc1001.txt 15.2.1).
275 if (bcast && success) return;
277 if (secured_redirect)
279 char rdata[2];
281 /* XXXX i am confused. RSVAL or SSVAL? assume NMB byte ordering */
282 RSSVAL(rdata,0,(nmb->header.opcode&0xf) + ((nb_flags&0xff) << 4));
284 /* XXXX mistake in rfc1002.txt? 4.2.16: NULL is 0xa see 4.2.1.3
285 type = 0x0a; see rfc1002.txt 4.2.1.3
286 class = 0x01; see rfc1002.txt 4.2.16
289 /* send WAIT ACKNOWLEDGEMENT see rfc1002.txt 4.2.16 */
290 reply_netbios_packet(p,nmb->header.name_trn_id,
291 0,NMB_WAIT_ACK,NMB_WAIT_ACK,False,
292 reply_name, 0x0a, 0x01,
293 15*1000, /* 15 seconds long enough to wait? */
294 rdata, 2);
296 /* initiate some enquiries to the current owner. */
297 queue_netbios_packet(d,ClientNMB,NMB_QUERY,
298 NAME_REGISTER_CHALLENGE,
299 reply_name->name,reply_name->name_type,
300 nb_flags,0,0,NULL,NULL,
301 False, False, n->ip_flgs[0].ip, p->ip);
303 else
305 /* Send a NAME REGISTRATION RESPONSE (pos/neg) see rfc1002.txt 4.2.13-14
306 or an END-NODE CHALLENGE REGISTRATION RESPONSE see rfc1002.txt 4.2.7
309 send_name_response(p->fd,p->ip, nmb->header.name_trn_id, NMB_REG,
310 success, True,
311 reply_name, nb_flags, ttl, ip);
315 /* this is used to sort names for a name status into a sensible order
316 we put our own names first, then in alphabetical order */
317 static int status_compare(char *n1,char *n2)
319 extern pstring myname;
320 int l1,l2,l3;
322 /* its a bit tricky because the names are space padded */
323 for (l1=0;l1<15 && n1[l1] && n1[l1] != ' ';l1++) ;
324 for (l2=0;l2<15 && n2[l2] && n2[l2] != ' ';l2++) ;
325 l3 = strlen(myname);
327 if ((l1==l3) && strncmp(n1,myname,l3) == 0 &&
328 (l2!=l3 || strncmp(n2,myname,l3) != 0))
329 return -1;
331 if ((l2==l3) && strncmp(n2,myname,l3) == 0 &&
332 (l1!=l3 || strncmp(n1,myname,l3) != 0))
333 return 1;
335 return memcmp(n1,n2,18);
339 /****************************************************************************
340 reply to a name status query
342 combine the list of the local interface on which the query was made with
343 the names registered via wins.
344 ****************************************************************************/
345 void reply_name_status(struct packet_struct *p)
347 struct nmb_packet *nmb = &p->packet.nmb;
348 char *qname = nmb->question.question_name.name;
349 int ques_type = nmb->question.question_name.name_type;
350 char rdata[MAX_DGRAM_SIZE];
351 char *countptr, *buf, *bufend, *buf0;
352 int names_added,i;
353 struct name_record *n;
354 struct subnet_record *d = NULL;
355 int search = FIND_SELF | FIND_WINS | FIND_LOCAL;
357 /* NOTE: we always treat a name status lookup as a bcast */
358 if (!(d = find_req_subnet(p->ip, True)))
360 DEBUG(3,("Name status req: bcast %s not known\n",
361 inet_ntoa(p->ip)));
362 return;
365 DEBUG(3,("Name status for name %s %s\n",
366 namestr(&nmb->question.question_name),
367 inet_ntoa(p->ip)));
369 n = find_name_search(&d, &nmb->question.question_name,
370 search, p->ip);
372 if (!n) return;
374 /* XXXX hack, we should calculate exactly how many will fit */
375 bufend = &rdata[MAX_DGRAM_SIZE] - 18;
376 countptr = buf = rdata;
377 buf += 1;
378 buf0 = buf;
380 names_added = 0;
382 n = d->namelist;
384 while (buf < bufend)
386 if (n->source == SELF)
388 int name_type = n->name.name_type;
390 /* check if we want to exclude other workgroup names
391 from the response. if we don't exclude them, windows clients
392 get confused and will respond with an error for NET VIEW */
394 if (!strequal(n->name.name,"*") &&
395 !strequal(n->name.name,"__SAMBA__") &&
396 (name_type < 0x1b || name_type >= 0x20 ||
397 ques_type < 0x1b || ques_type >= 0x20 ||
398 strequal(qname, n->name.name)))
400 /* start with first bit of putting info in buffer: the name */
401 bzero(buf,18);
402 sprintf(buf,"%-15.15s",n->name.name);
403 strupper(buf);
405 /* put name type and netbios flags in buffer */
406 buf[15] = name_type;
407 buf[16] = n->ip_flgs[0].nb_flags;
409 buf += 18;
411 names_added++;
415 /* remove duplicate names */
416 qsort(buf0,names_added,18,QSORT_CAST status_compare);
418 for (i=1;i<names_added;i++) {
419 if (memcmp(buf0 + 18*i,buf0 + 18*(i-1),16) == 0) {
420 names_added--;
421 if (names_added == i) break;
422 memmove(buf0 + 18*i,buf0 + 18*(i+1),18*(names_added-i));
423 i--;
427 buf = buf0 + 18*names_added;
429 n = n->next;
431 if (!n)
433 /* end of this name list: add wins names too? */
434 struct subnet_record *w_d;
436 if (!(w_d = wins_subnet)) break;
438 if (w_d != d)
440 d = w_d;
441 n = d->namelist; /* start on the wins name list */
444 if (!n) break;
447 SCVAL(countptr,0,names_added);
449 /* XXXXXXX we should fill in more fields of the statistics structure */
450 bzero(buf,64);
452 extern int num_good_sends,num_good_receives;
453 SIVAL(buf,20,num_good_sends);
454 SIVAL(buf,24,num_good_receives);
457 buf += 46;
459 /* Send a POSITIVE NAME STATUS RESPONSE */
460 reply_netbios_packet(p,nmb->header.name_trn_id,
461 0,NMB_STATUS,0,True,
462 &nmb->question.question_name,
463 0x21, 0x01,
464 0, rdata,PTR_DIFF(buf,rdata));
468 /***************************************************************************
469 reply to a name query.
471 with broadcast name queries:
473 - only reply if the query is for one of YOUR names. all other machines on
474 the network will be doing the same thing (that is, only replying to a
475 broadcast query if they own it)
476 NOTE: broadcast name queries should only be sent out by a machine
477 if they HAVEN'T been configured to use WINS. this is generally bad news
478 in a wide area tcp/ip network and should be rectified by the systems
479 administrator. USE WINS! :-)
480 - the exception to this is if the query is for a Primary Domain Controller
481 type name (0x1b), in which case, a reply is sent.
483 - NEVER send a negative response to a broadcast query. no-one else will!
485 with directed name queries:
487 - if you are the WINS server, you are expected to respond with either
488 a negative response, a positive response, or a wait-for-acknowledgement
489 packet, and then later on a pos/neg response.
491 ****************************************************************************/
492 void reply_name_query(struct packet_struct *p)
494 struct nmb_packet *nmb = &p->packet.nmb;
495 struct nmb_name *question = &nmb->question.question_name;
496 int name_type = question->name_type;
497 BOOL bcast = nmb->header.nm_flags.bcast;
498 int ttl=0;
499 int rcode = 0;
500 int nb_flags = 0;
501 struct in_addr retip;
502 char rdata[6];
503 struct subnet_record *d = NULL;
504 BOOL success = True;
505 struct name_record *n = NULL;
507 /* directed queries are for WINS server: broadcasts are local SELF queries.
508 the exception is Domain Master names. */
510 int search = bcast ? FIND_LOCAL | FIND_WINS: FIND_WINS;
512 if (search & FIND_LOCAL)
514 if (!(d = find_req_subnet(p->ip, bcast)))
516 DEBUG(3,("name query: bcast %s not known\n",
517 inet_ntoa(p->ip)));
518 success = False;
521 else
523 if (!(d = wins_subnet))
525 DEBUG(3,("name query: wins search %s not known\n",
526 inet_ntoa(p->ip)));
527 success = False;
531 DEBUG(3,("Name query "));
533 if (search == 0)
535 /* eh? no criterion for searching database. help! */
536 success = False;
539 if (!bcast && name_type == 0x1d)
541 /* see WINS manager HELP - 'How WINS Handles Special Names' */
542 /* a WINS query (unicasted) for a 0x1d name must always return False */
543 success = False;
546 if (success)
548 /* look up the name in the cache */
549 n = find_name_search(&d, question, search, p->ip);
551 /* it is a name that already failed DNS lookup or it's expired */
552 if (n && (n->source == DNSFAIL ||
553 (n->death_time && n->death_time < p->timestamp)))
555 success = False;
558 /* do we want to do dns lookups? */
559 /* XXXX this DELAYS nmbd while it does a search. not a good idea
560 but there's no pleasant alternative. phil@hands.com suggested
561 making the name a full DNS name, which would succeed / fail
562 much quicker.
564 if (success && !n && (lp_wins_proxy() || !bcast))
566 n = dns_name_search(question, p->timestamp);
570 if (!n) success = False;
572 if (success)
574 if (bcast && n->source != SELF && name_type != 0x1b)
576 /* don't respond to broadcast queries unless the query is for
577 a name we own or it is for a Primary Domain Controller name */
579 if (!lp_wins_proxy() ||
580 same_net(p->ip,n->ip_flgs[0].ip,*iface_nmask(p->ip)))
582 /* never reply with a negative response to broadcast queries */
583 return;
587 /* name is directed query, or it's self, or it's a Domain Master type
588 name, or we're replying on behalf of a caller because they are on a
589 different subnet and cannot hear the broadcast. XXXX lp_wins_proxy
590 should be switched off in environments where broadcasts are forwarded
593 /* XXXX note: for proxy servers, we should forward the query on to
594 another WINS server if the name is not in our database, or we are
595 not a WINS server ourselves
597 ttl = n->death_time ? n->death_time - p->timestamp : GET_TTL(0);
598 retip = n->ip_flgs[0].ip;
599 nb_flags = n->ip_flgs[0].nb_flags;
602 if (!success && bcast) return; /* never reply negative response to bcasts */
604 /* if the IP is 0 then substitute my IP */
605 if (zero_ip(retip)) retip = *iface_ip(p->ip);
607 if (success)
609 rcode = 0;
610 DEBUG(3,("OK %s\n",inet_ntoa(retip)));
612 else
614 rcode = 3;
615 DEBUG(3,("UNKNOWN\n"));
618 if (success)
620 rdata[0] = nb_flags;
621 rdata[1] = 0;
622 putip(&rdata[2],(char *)&retip);
625 reply_netbios_packet(p,nmb->header.name_trn_id,
626 rcode,NMB_QUERY,0,True,
627 &nmb->question.question_name,
628 0x20, 0x01,
629 ttl,
630 rdata, success ? 6 : 0);