2 Unix SMB/CIFS implementation.
4 Copyright (C) Gerald (Jerry) Carter 2006.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* AIX resolv.h uses 'class' in struct ns_rr */
31 /* resolver headers */
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
39 #define MAX_DNS_PACKET_SIZE 0xffff
41 #ifdef NS_HFIXEDSZ /* Bind 8/9 interface */
42 #if !defined(C_IN) /* AIX 5.3 already defines C_IN */
45 #if !defined(T_A) /* AIX 5.3 already defines T_A */
48 # define T_SRV ns_t_srv
52 # define NS_HFIXEDSZ HFIXEDSZ
54 # define NS_HFIXEDSZ sizeof(HEADER)
55 # endif /* HFIXEDSZ */
57 # define NS_PACKETSZ PACKETSZ
58 # else /* 512 is usually the default */
59 # define NS_PACKETSZ 512
60 # endif /* PACKETSZ */
64 /*********************************************************************
65 *********************************************************************/
67 static BOOL ads_dns_parse_query( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
68 uint8 **ptr, struct dns_query *q )
76 if ( !start || !end || !q || !*ptr)
79 /* See RFC 1035 for details. If this fails, then return. */
81 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
86 q->hostname = talloc_strdup( ctx, hostname );
88 /* check that we have space remaining */
90 if ( PTR_DIFF(p+4, end) > 0 )
93 q->type = RSVAL( p, 0 );
94 q->in_class = RSVAL( p, 2 );
102 /*********************************************************************
103 *********************************************************************/
105 static BOOL ads_dns_parse_rr( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
106 uint8 **ptr, struct dns_rr *rr )
112 if ( !start || !end || !rr || !*ptr)
116 /* pull the name from the answer */
118 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
123 rr->hostname = talloc_strdup( ctx, hostname );
125 /* check that we have space remaining */
127 if ( PTR_DIFF(p+10, end) > 0 )
130 /* pull some values and then skip onto the string */
132 rr->type = RSVAL(p, 0);
133 rr->in_class = RSVAL(p, 2);
134 rr->ttl = RIVAL(p, 4);
135 rr->rdatalen = RSVAL(p, 8);
139 /* sanity check the available space */
141 if ( PTR_DIFF(p+rr->rdatalen, end ) > 0 ) {
146 /* save a point to the rdata for this section */
156 /*********************************************************************
157 *********************************************************************/
159 static BOOL ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
160 uint8 **ptr, struct dns_rr_srv *srv )
167 if ( !start || !end || !srv || !*ptr)
170 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
171 of the next record */
173 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
174 DEBUG(1,("ads_dns_parse_rr_srv: Failed to parse RR record\n"));
178 if ( rr.type != T_SRV ) {
179 DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n", rr.type));
185 srv->priority = RSVAL(p, 0);
186 srv->weight = RSVAL(p, 2);
187 srv->port = RSVAL(p, 4);
191 namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
193 DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
196 srv->hostname = talloc_strdup( ctx, dcname );
201 /*********************************************************************
202 *********************************************************************/
204 static BOOL ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
205 uint8 **ptr, struct dns_rr_ns *nsrec )
212 if ( !start || !end || !nsrec || !*ptr)
215 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
216 of the next record */
218 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
219 DEBUG(1,("ads_dns_parse_rr_ns: Failed to parse RR record\n"));
223 if ( rr.type != T_NS ) {
224 DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n", rr.type));
230 /* ame server hostname */
232 namelen = dn_expand( start, end, p, nsname, sizeof(nsname) );
234 DEBUG(1,("ads_dns_parse_rr_ns: Failed to uncompress name!\n"));
237 nsrec->hostname = talloc_strdup( ctx, nsname );
242 /*********************************************************************
243 Sort SRV record list based on weight and priority. See RFC 2782.
244 *********************************************************************/
246 static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b )
248 if ( a->priority == b->priority ) {
250 /* randomize entries with an equal weight and priority */
251 if ( a->weight == b->weight )
254 /* higher weights should be sorted lower */
255 if ( a->weight > b->weight )
261 if ( a->priority < b->priority )
267 /*********************************************************************
268 Simple wrapper for a DNS query
269 *********************************************************************/
271 static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
272 uint8 **buf, int *resp_length )
274 uint8 *buffer = NULL;
276 int resp_len = NS_PACKETSZ;
280 TALLOC_FREE( buffer );
282 buf_len = resp_len * sizeof(uint8);
284 if ( (buffer = TALLOC_ARRAY(ctx, uint8, buf_len)) == NULL ) {
285 DEBUG(0,("ads_dns_lookup_srv: talloc() failed!\n"));
286 return NT_STATUS_NO_MEMORY;
289 if ( (resp_len = res_query(name, C_IN, q_type, buffer, buf_len)) < 0 ) {
290 DEBUG(1,("ads_dns_lookup_srv: Failed to resolve %s (%s)\n", name, strerror(errno)));
291 TALLOC_FREE( buffer );
292 return NT_STATUS_UNSUCCESSFUL;
294 } while ( buf_len < resp_len && resp_len < MAX_DNS_PACKET_SIZE );
297 *resp_length = resp_len;
302 /*********************************************************************
303 Simple wrapper for a DNS SRV query
304 *********************************************************************/
306 static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, const char *name, struct dns_rr_srv **dclist, int *numdcs )
308 uint8 *buffer = NULL;
310 struct dns_rr_srv *dcs = NULL;
311 int query_count, answer_count, auth_count, additional_count;
317 if ( !ctx || !name || !dclist ) {
318 return NT_STATUS_INVALID_PARAMETER;
321 /* Send the request. May have to loop several times in case
324 status = dns_send_req( ctx, name, T_SRV, &buffer, &resp_len );
325 if ( !NT_STATUS_IS_OK(status) ) {
326 DEBUG(0,("ads_dns_lookup_srv: Failed to send DNS query (%s)\n",
332 /* For some insane reason, the ns_initparse() et. al. routines are only
333 available in libresolv.a, and not the shared lib. Who knows why....
334 So we have to parse the DNS reply ourselves */
336 /* Pull the answer RR's count from the header. Use the NMB ordering macros */
338 query_count = RSVAL( p, 4 );
339 answer_count = RSVAL( p, 6 );
340 auth_count = RSVAL( p, 8 );
341 additional_count = RSVAL( p, 10 );
343 DEBUG(4,("ads_dns_lookup_srv: %d records returned in the answer section.\n",
346 if ( (dcs = TALLOC_ZERO_ARRAY(ctx, struct dns_rr_srv, answer_count)) == NULL ) {
347 DEBUG(0,("ads_dns_lookup_srv: talloc() failure for %d char*'s\n",
349 return NT_STATUS_NO_MEMORY;
352 /* now skip the header */
356 /* parse the query section */
358 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
361 if ( !ads_dns_parse_query( ctx, buffer, buffer+resp_len, &p, &q ) ) {
362 DEBUG(1,("ads_dns_lookup_srv: Failed to parse query record!\n"));
363 return NT_STATUS_UNSUCCESSFUL;
367 /* now we are at the answer section */
369 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
370 if ( !ads_dns_parse_rr_srv( ctx, buffer, buffer+resp_len, &p, &dcs[rrnum] ) ) {
371 DEBUG(1,("ads_dns_lookup_srv: Failed to parse answer record!\n"));
372 return NT_STATUS_UNSUCCESSFUL;
377 /* Parse the authority section */
378 /* just skip these for now */
380 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
383 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
384 DEBUG(1,("ads_dns_lookup_srv: Failed to parse authority record!\n"));
385 return NT_STATUS_UNSUCCESSFUL;
389 /* Parse the additional records section */
391 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
395 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
396 DEBUG(1,("ads_dns_lookup_srv: Failed to parse additional records section!\n"));
397 return NT_STATUS_UNSUCCESSFUL;
400 /* only interested in A records as a shortcut for having to come
401 back later and lookup the name. For multi-homed hosts, the
402 number of additional records and exceed the number of answer
406 if ( (rr.type != T_A) || (rr.rdatalen != 4) )
409 for ( i=0; i<idx; i++ ) {
410 if ( strcmp( rr.hostname, dcs[i].hostname ) == 0 ) {
411 int num_ips = dcs[i].num_ips;
413 struct in_addr *tmp_ips;
415 /* allocate new memory */
417 if ( dcs[i].num_ips == 0 ) {
418 if ( (dcs[i].ips = TALLOC_ARRAY( dcs,
419 struct in_addr, 1 )) == NULL )
421 return NT_STATUS_NO_MEMORY;
424 if ( (tmp_ips = TALLOC_REALLOC_ARRAY( dcs, dcs[i].ips,
425 struct in_addr, dcs[i].num_ips+1)) == NULL )
427 return NT_STATUS_NO_MEMORY;
430 dcs[i].ips = tmp_ips;
434 /* copy the new IP address */
436 buf = (uint8*)&dcs[i].ips[num_ips].s_addr;
437 memcpy( buf, rr.rdata, 4 );
442 qsort( dcs, idx, sizeof(struct dns_rr_srv), QSORT_CAST dnssrvcmp );
450 /*********************************************************************
451 Simple wrapper for a DNS NS query
452 *********************************************************************/
454 NTSTATUS ads_dns_lookup_ns( TALLOC_CTX *ctx, const char *dnsdomain, struct dns_rr_ns **nslist, int *numns )
456 uint8 *buffer = NULL;
458 struct dns_rr_ns *nsarray = NULL;
459 int query_count, answer_count, auth_count, additional_count;
465 if ( !ctx || !dnsdomain || !nslist ) {
466 return NT_STATUS_INVALID_PARAMETER;
469 /* Send the request. May have to loop several times in case
472 status = dns_send_req( ctx, dnsdomain, T_NS, &buffer, &resp_len );
473 if ( !NT_STATUS_IS_OK(status) ) {
474 DEBUG(0,("ads_dns_lookup_ns: Failed to send DNS query (%s)\n",
480 /* For some insane reason, the ns_initparse() et. al. routines are only
481 available in libresolv.a, and not the shared lib. Who knows why....
482 So we have to parse the DNS reply ourselves */
484 /* Pull the answer RR's count from the header. Use the NMB ordering macros */
486 query_count = RSVAL( p, 4 );
487 answer_count = RSVAL( p, 6 );
488 auth_count = RSVAL( p, 8 );
489 additional_count = RSVAL( p, 10 );
491 DEBUG(4,("ads_dns_lookup_ns: %d records returned in the answer section.\n",
494 if ( (nsarray = TALLOC_ARRAY(ctx, struct dns_rr_ns, answer_count)) == NULL ) {
495 DEBUG(0,("ads_dns_lookup_ns: talloc() failure for %d char*'s\n",
497 return NT_STATUS_NO_MEMORY;
500 /* now skip the header */
504 /* parse the query section */
506 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
509 if ( !ads_dns_parse_query( ctx, buffer, buffer+resp_len, &p, &q ) ) {
510 DEBUG(1,("ads_dns_lookup_ns: Failed to parse query record!\n"));
511 return NT_STATUS_UNSUCCESSFUL;
515 /* now we are at the answer section */
517 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
518 if ( !ads_dns_parse_rr_ns( ctx, buffer, buffer+resp_len, &p, &nsarray[rrnum] ) ) {
519 DEBUG(1,("ads_dns_lookup_ns: Failed to parse answer record!\n"));
520 return NT_STATUS_UNSUCCESSFUL;
525 /* Parse the authority section */
526 /* just skip these for now */
528 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
531 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
532 DEBUG(1,("ads_dns_lookup_ns: Failed to parse authority record!\n"));
533 return NT_STATUS_UNSUCCESSFUL;
537 /* Parse the additional records section */
539 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
543 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
544 DEBUG(1,("ads_dns_lookup_ns: Failed to parse additional records section!\n"));
545 return NT_STATUS_UNSUCCESSFUL;
548 /* only interested in A records as a shortcut for having to come
549 back later and lookup the name */
551 if ( (rr.type != T_A) || (rr.rdatalen != 4) )
554 for ( i=0; i<idx; i++ ) {
555 if ( strcmp( rr.hostname, nsarray[i].hostname ) == 0 ) {
556 uint8 *buf = (uint8*)&nsarray[i].ip.s_addr;
557 memcpy( buf, rr.rdata, 4 );
569 /********************************************************************
570 ********************************************************************/
572 NTSTATUS ads_dns_query_dcs( TALLOC_CTX *ctx, const char *domain, struct dns_rr_srv **dclist, int *numdcs )
576 snprintf( name, sizeof(name), "_ldap._tcp.dc._msdcs.%s", domain );
578 return ads_dns_lookup_srv( ctx, name, dclist, numdcs );