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
49 #if !defined(T_NS) /* AIX 5.3 already defines T_NS */
54 # define NS_HFIXEDSZ HFIXEDSZ
56 # define NS_HFIXEDSZ sizeof(HEADER)
57 # endif /* HFIXEDSZ */
59 # define NS_PACKETSZ PACKETSZ
60 # else /* 512 is usually the default */
61 # define NS_PACKETSZ 512
62 # endif /* PACKETSZ */
66 /*********************************************************************
67 *********************************************************************/
69 static BOOL ads_dns_parse_query( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
70 uint8 **ptr, struct dns_query *q )
78 if ( !start || !end || !q || !*ptr)
81 /* See RFC 1035 for details. If this fails, then return. */
83 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
88 q->hostname = talloc_strdup( ctx, hostname );
90 /* check that we have space remaining */
92 if ( PTR_DIFF(p+4, end) > 0 )
95 q->type = RSVAL( p, 0 );
96 q->in_class = RSVAL( p, 2 );
104 /*********************************************************************
105 *********************************************************************/
107 static BOOL ads_dns_parse_rr( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
108 uint8 **ptr, struct dns_rr *rr )
114 if ( !start || !end || !rr || !*ptr)
118 /* pull the name from the answer */
120 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
125 rr->hostname = talloc_strdup( ctx, hostname );
127 /* check that we have space remaining */
129 if ( PTR_DIFF(p+10, end) > 0 )
132 /* pull some values and then skip onto the string */
134 rr->type = RSVAL(p, 0);
135 rr->in_class = RSVAL(p, 2);
136 rr->ttl = RIVAL(p, 4);
137 rr->rdatalen = RSVAL(p, 8);
141 /* sanity check the available space */
143 if ( PTR_DIFF(p+rr->rdatalen, end ) > 0 ) {
148 /* save a point to the rdata for this section */
158 /*********************************************************************
159 *********************************************************************/
161 static BOOL ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
162 uint8 **ptr, struct dns_rr_srv *srv )
169 if ( !start || !end || !srv || !*ptr)
172 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
173 of the next record */
175 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
176 DEBUG(1,("ads_dns_parse_rr_srv: Failed to parse RR record\n"));
180 if ( rr.type != T_SRV ) {
181 DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n", rr.type));
187 srv->priority = RSVAL(p, 0);
188 srv->weight = RSVAL(p, 2);
189 srv->port = RSVAL(p, 4);
193 namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
195 DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
198 srv->hostname = talloc_strdup( ctx, dcname );
203 /*********************************************************************
204 *********************************************************************/
206 static BOOL ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
207 uint8 **ptr, struct dns_rr_ns *nsrec )
214 if ( !start || !end || !nsrec || !*ptr)
217 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
218 of the next record */
220 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
221 DEBUG(1,("ads_dns_parse_rr_ns: Failed to parse RR record\n"));
225 if ( rr.type != T_NS ) {
226 DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n", rr.type));
232 /* ame server hostname */
234 namelen = dn_expand( start, end, p, nsname, sizeof(nsname) );
236 DEBUG(1,("ads_dns_parse_rr_ns: Failed to uncompress name!\n"));
239 nsrec->hostname = talloc_strdup( ctx, nsname );
244 /*********************************************************************
245 Sort SRV record list based on weight and priority. See RFC 2782.
246 *********************************************************************/
248 static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b )
250 if ( a->priority == b->priority ) {
252 /* randomize entries with an equal weight and priority */
253 if ( a->weight == b->weight )
256 /* higher weights should be sorted lower */
257 if ( a->weight > b->weight )
263 if ( a->priority < b->priority )
269 /*********************************************************************
270 Simple wrapper for a DNS query
271 *********************************************************************/
273 static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
274 uint8 **buf, int *resp_length )
276 uint8 *buffer = NULL;
278 int resp_len = NS_PACKETSZ;
282 TALLOC_FREE( buffer );
284 buf_len = resp_len * sizeof(uint8);
286 if ( (buffer = TALLOC_ARRAY(ctx, uint8, buf_len)) == NULL ) {
287 DEBUG(0,("ads_dns_lookup_srv: talloc() failed!\n"));
288 return NT_STATUS_NO_MEMORY;
291 if ( (resp_len = res_query(name, C_IN, q_type, buffer, buf_len)) < 0 ) {
292 DEBUG(3,("ads_dns_lookup_srv: Failed to resolve %s (%s)\n", name, strerror(errno)));
293 TALLOC_FREE( buffer );
294 return NT_STATUS_UNSUCCESSFUL;
296 } while ( buf_len < resp_len && resp_len < MAX_DNS_PACKET_SIZE );
299 *resp_length = resp_len;
304 /*********************************************************************
305 Simple wrapper for a DNS SRV query
306 *********************************************************************/
308 static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, const char *name, struct dns_rr_srv **dclist, int *numdcs )
310 uint8 *buffer = NULL;
312 struct dns_rr_srv *dcs = NULL;
313 int query_count, answer_count, auth_count, additional_count;
319 if ( !ctx || !name || !dclist ) {
320 return NT_STATUS_INVALID_PARAMETER;
323 /* Send the request. May have to loop several times in case
326 status = dns_send_req( ctx, name, T_SRV, &buffer, &resp_len );
327 if ( !NT_STATUS_IS_OK(status) ) {
328 DEBUG(3,("ads_dns_lookup_srv: Failed to send DNS query (%s)\n",
334 /* For some insane reason, the ns_initparse() et. al. routines are only
335 available in libresolv.a, and not the shared lib. Who knows why....
336 So we have to parse the DNS reply ourselves */
338 /* Pull the answer RR's count from the header. Use the NMB ordering macros */
340 query_count = RSVAL( p, 4 );
341 answer_count = RSVAL( p, 6 );
342 auth_count = RSVAL( p, 8 );
343 additional_count = RSVAL( p, 10 );
345 DEBUG(4,("ads_dns_lookup_srv: %d records returned in the answer section.\n",
348 if ( (dcs = TALLOC_ZERO_ARRAY(ctx, struct dns_rr_srv, answer_count)) == NULL ) {
349 DEBUG(0,("ads_dns_lookup_srv: talloc() failure for %d char*'s\n",
351 return NT_STATUS_NO_MEMORY;
354 /* now skip the header */
358 /* parse the query section */
360 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
363 if ( !ads_dns_parse_query( ctx, buffer, buffer+resp_len, &p, &q ) ) {
364 DEBUG(1,("ads_dns_lookup_srv: Failed to parse query record!\n"));
365 return NT_STATUS_UNSUCCESSFUL;
369 /* now we are at the answer section */
371 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
372 if ( !ads_dns_parse_rr_srv( ctx, buffer, buffer+resp_len, &p, &dcs[rrnum] ) ) {
373 DEBUG(1,("ads_dns_lookup_srv: Failed to parse answer record!\n"));
374 return NT_STATUS_UNSUCCESSFUL;
379 /* Parse the authority section */
380 /* just skip these for now */
382 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
385 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
386 DEBUG(1,("ads_dns_lookup_srv: Failed to parse authority record!\n"));
387 return NT_STATUS_UNSUCCESSFUL;
391 /* Parse the additional records section */
393 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
397 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
398 DEBUG(1,("ads_dns_lookup_srv: Failed to parse additional records section!\n"));
399 return NT_STATUS_UNSUCCESSFUL;
402 /* only interested in A records as a shortcut for having to come
403 back later and lookup the name. For multi-homed hosts, the
404 number of additional records and exceed the number of answer
408 if ( (rr.type != T_A) || (rr.rdatalen != 4) )
411 for ( i=0; i<idx; i++ ) {
412 if ( strcmp( rr.hostname, dcs[i].hostname ) == 0 ) {
413 int num_ips = dcs[i].num_ips;
415 struct in_addr *tmp_ips;
417 /* allocate new memory */
419 if ( dcs[i].num_ips == 0 ) {
420 if ( (dcs[i].ips = TALLOC_ARRAY( dcs,
421 struct in_addr, 1 )) == NULL )
423 return NT_STATUS_NO_MEMORY;
426 if ( (tmp_ips = TALLOC_REALLOC_ARRAY( dcs, dcs[i].ips,
427 struct in_addr, dcs[i].num_ips+1)) == NULL )
429 return NT_STATUS_NO_MEMORY;
432 dcs[i].ips = tmp_ips;
436 /* copy the new IP address */
438 buf = (uint8*)&dcs[i].ips[num_ips].s_addr;
439 memcpy( buf, rr.rdata, 4 );
444 qsort( dcs, idx, sizeof(struct dns_rr_srv), QSORT_CAST dnssrvcmp );
452 /*********************************************************************
453 Simple wrapper for a DNS NS query
454 *********************************************************************/
456 NTSTATUS ads_dns_lookup_ns( TALLOC_CTX *ctx, const char *dnsdomain, struct dns_rr_ns **nslist, int *numns )
458 uint8 *buffer = NULL;
460 struct dns_rr_ns *nsarray = NULL;
461 int query_count, answer_count, auth_count, additional_count;
467 if ( !ctx || !dnsdomain || !nslist ) {
468 return NT_STATUS_INVALID_PARAMETER;
471 /* Send the request. May have to loop several times in case
474 status = dns_send_req( ctx, dnsdomain, T_NS, &buffer, &resp_len );
475 if ( !NT_STATUS_IS_OK(status) ) {
476 DEBUG(3,("ads_dns_lookup_ns: Failed to send DNS query (%s)\n",
482 /* For some insane reason, the ns_initparse() et. al. routines are only
483 available in libresolv.a, and not the shared lib. Who knows why....
484 So we have to parse the DNS reply ourselves */
486 /* Pull the answer RR's count from the header. Use the NMB ordering macros */
488 query_count = RSVAL( p, 4 );
489 answer_count = RSVAL( p, 6 );
490 auth_count = RSVAL( p, 8 );
491 additional_count = RSVAL( p, 10 );
493 DEBUG(4,("ads_dns_lookup_ns: %d records returned in the answer section.\n",
496 if ( (nsarray = TALLOC_ARRAY(ctx, struct dns_rr_ns, answer_count)) == NULL ) {
497 DEBUG(0,("ads_dns_lookup_ns: talloc() failure for %d char*'s\n",
499 return NT_STATUS_NO_MEMORY;
502 /* now skip the header */
506 /* parse the query section */
508 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
511 if ( !ads_dns_parse_query( ctx, buffer, buffer+resp_len, &p, &q ) ) {
512 DEBUG(1,("ads_dns_lookup_ns: Failed to parse query record!\n"));
513 return NT_STATUS_UNSUCCESSFUL;
517 /* now we are at the answer section */
519 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
520 if ( !ads_dns_parse_rr_ns( ctx, buffer, buffer+resp_len, &p, &nsarray[rrnum] ) ) {
521 DEBUG(1,("ads_dns_lookup_ns: Failed to parse answer record!\n"));
522 return NT_STATUS_UNSUCCESSFUL;
527 /* Parse the authority section */
528 /* just skip these for now */
530 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
533 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
534 DEBUG(1,("ads_dns_lookup_ns: Failed to parse authority record!\n"));
535 return NT_STATUS_UNSUCCESSFUL;
539 /* Parse the additional records section */
541 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
545 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
546 DEBUG(1,("ads_dns_lookup_ns: Failed to parse additional records section!\n"));
547 return NT_STATUS_UNSUCCESSFUL;
550 /* only interested in A records as a shortcut for having to come
551 back later and lookup the name */
553 if ( (rr.type != T_A) || (rr.rdatalen != 4) )
556 for ( i=0; i<idx; i++ ) {
557 if ( strcmp( rr.hostname, nsarray[i].hostname ) == 0 ) {
558 uint8 *buf = (uint8*)&nsarray[i].ip.s_addr;
559 memcpy( buf, rr.rdata, 4 );
570 /****************************************************************************
571 Store and fetch the AD client sitename.
572 ****************************************************************************/
574 #define SITENAME_KEY "AD_SITENAME"
576 /****************************************************************************
577 Store the AD client sitename.
578 We store indefinately as every new CLDAP query will re-write this.
579 If the sitename is "Default-First-Site-Name" we don't store it
580 as this isn't a valid DNS name.
581 ****************************************************************************/
583 BOOL sitename_store(const char *sitename)
588 if (!gencache_init()) {
592 if (!sitename || (sitename && !*sitename)) {
593 DEBUG(5,("sitename_store: deleting empty sitename!\n"));
594 return gencache_del(SITENAME_KEY);
597 expire = get_time_t_max(); /* Store indefinately. */
599 DEBUG(10,("sitename_store: sitename = [%s], expire = [%u]\n",
600 sitename, (unsigned int)expire ));
602 ret = gencache_set( SITENAME_KEY, sitename, expire );
606 /****************************************************************************
607 Fetch the AD client sitename.
609 ****************************************************************************/
611 char *sitename_fetch(void)
613 char *sitename = NULL;
617 if (!gencache_init()) {
621 ret = gencache_get( SITENAME_KEY, &sitename, &timeout );
623 DEBUG(5,("sitename_fetch: No stored sitename\n"));
625 DEBUG(5,("sitename_fetch: Returning sitename \"%s\"\n",
631 /****************************************************************************
632 Did the sitename change ?
633 ****************************************************************************/
635 BOOL stored_sitename_changed(const char *sitename)
638 char *new_sitename = sitename_fetch();
640 if (sitename && new_sitename && !strequal(sitename, new_sitename)) {
642 } else if ((sitename && !new_sitename) ||
643 (!sitename && new_sitename)) {
646 SAFE_FREE(new_sitename);
650 /********************************************************************
651 Query with optional sitename.
652 ********************************************************************/
654 NTSTATUS ads_dns_query_internal(TALLOC_CTX *ctx,
655 const char *servicename,
657 const char *sitename,
658 struct dns_rr_srv **dclist,
663 name = talloc_asprintf(ctx, "%s._tcp.%s._sites.dc._msdcs.%s",
664 servicename, sitename, realm );
666 name = talloc_asprintf(ctx, "%s._tcp.dc._msdcs.%s",
667 servicename, realm );
670 return NT_STATUS_NO_MEMORY;
672 return ads_dns_lookup_srv( ctx, name, dclist, numdcs );
675 /********************************************************************
676 Query for AD DC's. Transparently use sitename.
677 ********************************************************************/
679 NTSTATUS ads_dns_query_dcs(TALLOC_CTX *ctx,
681 struct dns_rr_srv **dclist,
685 char *sitename = sitename_fetch();
687 status = ads_dns_query_internal(ctx, "_ldap", realm, sitename,
689 if (sitename && !NT_STATUS_IS_OK(status)) {
690 /* Sitename DNS query may have failed. Try without. */
691 status = ads_dns_query_internal(ctx, "_ldap", realm, NULL,
698 /********************************************************************
699 Query for AD KDC's. Transparently use sitename.
700 Even if our underlying kerberos libraries are UDP only, this
701 is pretty safe as it's unlikely that a KDC supports TCP and not UDP.
702 ********************************************************************/
704 NTSTATUS ads_dns_query_kdcs(TALLOC_CTX *ctx,
706 struct dns_rr_srv **dclist,
710 char *sitename = sitename_fetch();
712 status = ads_dns_query_internal(ctx, "_kerberos", realm, sitename,
714 if (sitename && !NT_STATUS_IS_OK(status)) {
715 /* Sitename DNS query may have failed. Try without. */
716 status = ads_dns_query_internal(ctx, "_kerberos", realm, NULL,