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: nameservreply.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 nameservreply containing NetBIOS reply functions
37 extern int DEBUGLEVEL
;
39 extern struct in_addr wins_ip
;
41 /****************************************************************************
42 send a registration / release response: pos/neg
43 **************************************************************************/
44 static void send_name_response(int fd
, struct in_addr from_ip
,
45 int name_trn_id
, int opcode
, BOOL success
,
46 BOOL recursion_available
, BOOL recursion_desired
,
47 struct nmb_name
*reply_name
, int nb_flags
, int ttl
,
51 struct packet_struct p
;
57 /* NEGATIVE RESPONSE */
60 else if (opcode
== NMB_REG
&& !recursion_available
)
62 /* END-NODE CHALLENGE REGISTRATION RESPONSE */
68 putip(&rdata
[2],(char *)&ip
);
73 p
.timestamp
= time(NULL
);
74 p
.packet_type
= NMB_PACKET
;
76 reply_netbios_packet(&p
,name_trn_id
,
78 recursion_available
, recursion_desired
,
79 reply_name
, 0x20, 0x1,
84 /****************************************************************************
85 add a netbios entry. respond to the (possibly new) owner.
86 **************************************************************************/
87 void add_name_respond(struct subnet_record
*d
, int fd
, struct in_addr from_ip
,
89 struct nmb_name
*name
,
90 int nb_flags
, int ttl
, struct in_addr register_ip
,
91 BOOL new_owner
, struct in_addr reply_to_ip
)
93 /* register the old or the new owners' ip */
94 add_netbios_entry(d
,name
->name
,name
->name_type
,
95 nb_flags
,ttl
,REGISTER
,register_ip
,False
,True
);
97 /* reply yes or no to the host that requested the name */
98 /* see rfc1002.txt - 4.2.10 and 4.2.11 */
100 send_name_response(fd
, reply_to_ip
, response_id
, NMB_REG
,
103 name
, nb_flags
, ttl
, reply_to_ip
);
107 /****************************************************************************
108 reply to a name release
109 ****************************************************************************/
110 void reply_name_release(struct packet_struct
*p
)
112 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
114 int nb_flags
= nmb
->additional
->rdata
[0];
115 BOOL bcast
= nmb
->header
.nm_flags
.bcast
;
116 struct name_record
*n
;
117 struct subnet_record
*d
= NULL
;
119 BOOL success
= False
;
121 putip((char *)&ip
,&nmb
->additional
->rdata
[2]);
123 DEBUG(3,("Name release on name %s\n",
124 namestr(&nmb
->question
.question_name
)));
126 if (!(d
= find_req_subnet(p
->ip
, bcast
)))
128 DEBUG(3,("response packet: bcast %s not known\n",
134 search
|= FIND_LOCAL
;
138 n
= find_name_search(&d
, &nmb
->question
.question_name
,
141 /* XXXX under what conditions should we reject the removal?? */
142 /* For now - remove if the names match and the group bit matches. */
143 if (n
&& (n
->source
!= SELF
) && (NAME_GROUP(n
->ip_flgs
[0].nb_flags
) == NAME_GROUP(nb_flags
)))
147 /* If it's a group name not ending in 1c (not an internet name)
148 then just allow it to fade out of existance by timing out. */
149 if(NAME_GROUP(nb_flags
) && (n
->name
.name_type
!= 0x1c))
151 DEBUG(5, ("reply_name_release: Allow group name %s(%d) to fade out on \
152 subnet %s\n", namestr(&nmb
->question
.question_name
), n
->name
.name_type
,
153 inet_ntoa(d
->bcast_ip
)));
157 DEBUG(5, ("reply_name_release: Removing name %s on subnet %s\n",
158 namestr(&nmb
->question
.question_name
), inet_ntoa(d
->bcast_ip
)));
166 /* Send a NAME RELEASE RESPONSE (pos/neg) see rfc1002.txt 4.2.10-11 */
167 send_name_response(p
->fd
,p
->ip
, nmb
->header
.name_trn_id
, NMB_REL
,
168 success
, False
, False
,
169 &nmb
->question
.question_name
, nb_flags
, 0, ip
);
173 /****************************************************************************
174 reply to a reg request
175 **************************************************************************/
176 void reply_name_reg(struct packet_struct
*p
)
178 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
179 struct nmb_name
*question
= &nmb
->question
.question_name
;
181 struct nmb_name
*reply_name
= question
;
183 char *qname
= question
->name
;
184 int qname_type
= question
->name_type
;
186 BOOL bcast
= nmb
->header
.nm_flags
.bcast
;
188 int ttl
= GET_TTL(nmb
->additional
->ttl
);
189 int nb_flags
= nmb
->additional
->rdata
[0];
190 BOOL group
= NAME_GROUP(nb_flags
);
192 struct subnet_record
*d
= NULL
;
193 struct name_record
*n
= NULL
;
196 BOOL secured_redirect
= False
;
198 struct in_addr ip
, from_ip
;
201 putip((char *)&from_ip
,&nmb
->additional
->rdata
[2]);
204 DEBUG(3,("Name registration for name %s at %s - ",
205 namestr(question
),inet_ntoa(ip
)));
209 /* apparently we should return 255.255.255.255 for group queries
211 ip
= *interpret_addr2("255.255.255.255");
214 if (!(d
= find_req_subnet(p
->ip
, bcast
)))
216 DEBUG(3,("reply_name_reg: subnet %s not known\n",
222 search
|= FIND_LOCAL
;
226 /* see if the name already exists */
227 n
= find_name_search(&d
, question
, search
, from_ip
);
231 DEBUG(3,("found\n"));
232 if (!group
) /* unique names */
234 if (n
->source
== SELF
|| NAME_GROUP(n
->ip_flgs
[0].nb_flags
))
236 /* no-one can register one of samba's names, nor can they
237 register a name that's a group name as a unique name */
241 else if(!ip_equal(ip
, n
->ip_flgs
[0].ip
))
243 /* XXXX rfc1001.txt says:
244 * if we are doing secured WINS, we must send a Wait-Acknowledge
245 * packet (WACK) to the person who wants the name, then do a
246 * name query on the person who currently owns the unique name.
247 * if the current owner still says they own it, the person who wants
248 * the name can't have it. if they do not, or are not alive, they can.
251 secured_redirect
= True
;
253 reply_name
= &n
->name
;
257 n
->ip_flgs
[0].ip
= ip
;
258 n
->death_time
= ttl
?p
->timestamp
+ttl
*3:0;
259 DEBUG(3,("%s owner: %s\n",namestr(&n
->name
),inet_ntoa(n
->ip_flgs
[0].ip
)));
264 /* refresh the name */
265 if (n
->source
!= SELF
)
267 n
->death_time
= ttl
?p
->timestamp
+ ttl
*3:0;
271 /* XXXX bug reported by terryt@ren.pc.athabascau.ca */
272 /* names that people have checked for and not found get DNSFAILed.
273 we need to update the name record if someone then registers */
275 if (n
->source
== DNSFAIL
)
276 n
->source
= REGISTER
;
281 DEBUG(3,("not found\n"));
282 /* add the name to our name/subnet, or WINS, database */
283 n
= add_netbios_entry(d
,qname
,qname_type
,nb_flags
,ttl
,REGISTER
,ip
,
287 /* if samba owns a unique name on a subnet, then it must respond and
288 disallow the attempted registration. if the registration is
289 successful by broadcast, only then is there no need to respond
290 (implicit registration: see rfc1001.txt 15.2.1).
293 if (bcast
&& success
) return;
295 if (secured_redirect
)
299 /* XXXX i am confused. RSVAL or SSVAL? assume NMB byte ordering */
300 RSSVAL(rdata
,0,(nmb
->header
.opcode
&0xf) + ((nb_flags
&0xff) << 4));
302 /* XXXX mistake in rfc1002.txt? 4.2.16: NULL is 0xa see 4.2.1.3
303 type = 0x0a; see rfc1002.txt 4.2.1.3
304 class = 0x01; see rfc1002.txt 4.2.16
307 /* send WAIT ACKNOWLEDGEMENT see rfc1002.txt 4.2.16 */
308 reply_netbios_packet(p
,nmb
->header
.name_trn_id
,
309 0,NMB_WAIT_ACK
,NMB_WAIT_ACK
,
311 reply_name
, 0x0a, 0x01,
312 15*1000, /* 15 seconds long enough to wait? */
315 /* initiate some enquiries to the current owner. */
316 queue_netbios_packet(d
,ClientNMB
,NMB_QUERY
,
317 NAME_REGISTER_CHALLENGE
,
318 reply_name
->name
,reply_name
->name_type
,
319 nb_flags
,0,0,NULL
,NULL
,
321 n
->ip_flgs
[0].ip
, p
->ip
,
322 nmb
->header
.name_trn_id
);
326 /* Send a NAME REGISTRATION RESPONSE (pos/neg) see rfc1002.txt 4.2.5-6
327 or an END-NODE CHALLENGE REGISTRATION RESPONSE see rfc1002.txt 4.2.7
330 send_name_response(p
->fd
,p
->ip
, nmb
->header
.name_trn_id
, NMB_REG
,
333 reply_name
, nb_flags
, ttl
, ip
);
337 /* this is used to sort names for a name status into a sensible order
338 we put our own names first, then in alphabetical order */
339 static int status_compare(char *n1
,char *n2
)
341 extern pstring myname
;
344 /* its a bit tricky because the names are space padded */
345 for (l1
=0;l1
<15 && n1
[l1
] && n1
[l1
] != ' ';l1
++) ;
346 for (l2
=0;l2
<15 && n2
[l2
] && n2
[l2
] != ' ';l2
++) ;
349 if ((l1
==l3
) && strncmp(n1
,myname
,l3
) == 0 &&
350 (l2
!=l3
|| strncmp(n2
,myname
,l3
) != 0))
353 if ((l2
==l3
) && strncmp(n2
,myname
,l3
) == 0 &&
354 (l1
!=l3
|| strncmp(n1
,myname
,l3
) != 0))
357 return memcmp(n1
,n2
,18);
361 /****************************************************************************
362 reply to a name status query
364 combine the list of the local interface on which the query was made with
365 the names registered via wins.
366 ****************************************************************************/
367 void reply_name_status(struct packet_struct
*p
)
369 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
370 char *qname
= nmb
->question
.question_name
.name
;
371 int ques_type
= nmb
->question
.question_name
.name_type
;
372 char rdata
[MAX_DGRAM_SIZE
];
373 char *countptr
, *buf
, *bufend
, *buf0
;
375 struct name_record
*n
;
376 struct subnet_record
*d
= NULL
;
377 int search
= FIND_SELF
| FIND_WINS
| FIND_LOCAL
;
379 /* NOTE: we always treat a name status lookup as a bcast */
380 if (!(d
= find_req_subnet(p
->ip
, True
)))
382 DEBUG(3,("Name status req: bcast %s not known\n",
387 DEBUG(3,("Name status for name %s %s\n",
388 namestr(&nmb
->question
.question_name
),
391 n
= find_name_search(&d
, &nmb
->question
.question_name
,
396 /* XXXX hack, we should calculate exactly how many will fit */
397 bufend
= &rdata
[MAX_DGRAM_SIZE
] - 18;
398 countptr
= buf
= rdata
;
408 if (n
->source
== SELF
)
410 int name_type
= n
->name
.name_type
;
412 /* check if we want to exclude other workgroup names
413 from the response. if we don't exclude them, windows clients
414 get confused and will respond with an error for NET VIEW */
416 if (!strequal(n
->name
.name
,"*") &&
417 !strequal(n
->name
.name
,"__SAMBA__") &&
418 (name_type
< 0x1b || name_type
>= 0x20 ||
419 ques_type
< 0x1b || ques_type
>= 0x20 ||
420 strequal(qname
, n
->name
.name
)))
422 /* start with first bit of putting info in buffer: the name */
424 sprintf(buf
,"%-15.15s",n
->name
.name
);
427 /* put name type and netbios flags in buffer */
429 buf
[16] = n
->ip_flgs
[0].nb_flags
;
437 /* remove duplicate names */
438 qsort(buf0
,names_added
,18,QSORT_CAST status_compare
);
440 for (i
=1;i
<names_added
;i
++) {
441 if (memcmp(buf0
+ 18*i
,buf0
+ 18*(i
-1),16) == 0) {
443 if (names_added
== i
) break;
444 memmove(buf0
+ 18*i
,buf0
+ 18*(i
+1),18*(names_added
-i
));
449 buf
= buf0
+ 18*names_added
;
455 /* end of this name list: add wins names too? */
456 struct subnet_record
*w_d
;
458 if (!(w_d
= wins_subnet
)) break;
463 n
= d
->namelist
; /* start on the wins name list */
469 SCVAL(countptr
,0,names_added
);
471 /* XXXXXXX we should fill in more fields of the statistics structure */
474 extern int num_good_sends
,num_good_receives
;
475 SIVAL(buf
,20,num_good_sends
);
476 SIVAL(buf
,24,num_good_receives
);
481 /* Send a POSITIVE NAME STATUS RESPONSE */
482 reply_netbios_packet(p
,nmb
->header
.name_trn_id
,
483 0,NMB_STATUS
,0,False
, False
,
484 &nmb
->question
.question_name
,
486 0, rdata
,PTR_DIFF(buf
,rdata
));
490 /***************************************************************************
491 reply to a name query.
493 with broadcast name queries:
495 - only reply if the query is for one of YOUR names. all other machines on
496 the network will be doing the same thing (that is, only replying to a
497 broadcast query if they own it)
498 NOTE: broadcast name queries should only be sent out by a machine
499 if they HAVEN'T been configured to use WINS. this is generally bad news
500 in a wide area tcp/ip network and should be rectified by the systems
501 administrator. USE WINS! :-)
502 - the exception to this is if the query is for a Primary Domain Controller
503 type name (0x1b), in which case, a reply is sent.
505 - NEVER send a negative response to a broadcast query. no-one else will!
507 with directed name queries:
509 - if you are the WINS server, you are expected to respond with either
510 a negative response, a positive response, or a wait-for-acknowledgement
511 packet, and then later on a pos/neg response.
513 ****************************************************************************/
514 void reply_name_query(struct packet_struct
*p
)
516 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
517 struct nmb_name
*question
= &nmb
->question
.question_name
;
518 int name_type
= question
->name_type
;
520 BOOL bcast
= nmb
->header
.nm_flags
.bcast
;
521 BOOL query_is_to_wins_server
= (!bcast
&&
522 nmb
->header
.nm_flags
.recursion_desired
);
526 struct in_addr retip
;
528 struct subnet_record
*d
= NULL
;
530 struct name_record
*n
= NULL
;
531 BOOL acting_as_wins_server
= lp_wins_support();
533 /* directed queries are for WINS server: broadcasts are local SELF queries.
534 the exception is Domain Master names. */
536 if (query_is_to_wins_server
)
538 /* queries to the WINS server involve the WINS server subnet */
539 if (!(d
= wins_subnet
))
541 DEBUG(3,("name query: wins search %s not known\n",
548 /* queries to the WINS client involve, unfortunately, the WINS subnet
549 because it contains WINS client (SELF) entries, as _well_ as WINS
550 server entries. not good.
553 if (!(d
= find_subnet(*iface_bcast(p
->ip
))))
555 DEBUG(3,("name query: interface for %s not known\n",
561 DEBUG(3,("Name query from %s for name %s<0x%x>\n",
562 inet_ntoa(p
->ip
), question
->name
, question
->name_type
));
564 if (!bcast
&& (name_type
== 0x1d) && lp_wins_support())
566 /* see WINS manager HELP - 'How WINS Handles Special Names' */
567 /* a WINS query (unicasted) for a 0x1d name must always return False */
573 /* look up the name in the cache */
574 n
= find_name_search(&d
, question
, FIND_LOCAL
, p
->ip
);
576 /* it is a name that already failed DNS lookup or it's expired */
577 if (n
&& (n
->source
== DNSFAIL
||
578 (n
->death_time
&& n
->death_time
< p
->timestamp
)))
583 /* do we want to do dns lookups? */
584 /* XXXX this DELAYS nmbd while it does a search. lp_dns_proxy()
585 can be switched off, to ensure that the blocking doesn't occur.
586 a better solution would be to fork, but this will require a
587 mechanism to carry on processing after the query is resolved
588 (similar to the netbios queue).
590 if (success
&& !n
&& (lp_dns_proxy() || !bcast
))
592 n
= dns_name_search(question
, p
->timestamp
);
596 if (!n
) success
= False
;
600 if (bcast
&& n
->source
!= SELF
&& name_type
!= 0x1b)
602 /* don't respond to broadcast queries unless the query is for
603 a name we own or it is for a Primary Domain Controller name */
605 if (!lp_wins_proxy() ||
606 same_net(p
->ip
,n
->ip_flgs
[0].ip
,*iface_nmask(p
->ip
)))
608 /* never reply with a negative response to broadcast queries */
613 /* name is directed query, or it's self, or it's a Domain Master type
614 name, or we're replying on behalf of a caller because they are on a
615 different subnet and cannot hear the broadcast. XXXX lp_wins_proxy
616 should be switched off in environments where broadcasts are forwarded
619 /* XXXX note: for proxy servers, we should forward the query on to
620 another WINS server if the name is not in our database, or we are
621 not a WINS server ourselves
623 ttl
= n
->death_time
? n
->death_time
- p
->timestamp
: GET_TTL(0);
624 retip
= n
->ip_flgs
[0].ip
;
625 nb_flags
= n
->ip_flgs
[0].nb_flags
;
628 if (!success
&& bcast
) return; /* never reply negative response to bcasts */
630 /* if the IP is 0 then substitute my IP */
631 if (zero_ip(retip
)) retip
= *iface_ip(p
->ip
);
633 /* SPECIAL CASE... If we are a WINS server and the request is explicitly
634 *to* the WINS server and the name type is WORKGROUP<0x1e> we should
635 respond with the local broadcast address 255.255.255.255.
637 if(!bcast
&& (name_type
== 0x1e) && lp_wins_support())
638 retip
= *interpret_addr2("255.255.255.255");
643 DEBUG(3,("OK %s\n",inet_ntoa(retip
)));
648 DEBUG(3,("UNKNOWN\n"));
655 putip(&rdata
[2],(char *)&retip
);
658 /* see rfc1002.txt 4.2.13 */
660 reply_netbios_packet(p
,nmb
->header
.name_trn_id
,
662 (query_is_to_wins_server
&& acting_as_wins_server
?
663 True
: False
), /* recursion_available flag */
664 True
, /* recursion_desired_flag */
665 &nmb
->question
.question_name
,
668 rdata
, success
? 6 : 0);