2 Unix SMB/CIFS implementation.
4 Copyright (C) Gerald (Jerry) Carter 2006.
5 Copyright (C) Jeremy Allison 2007.
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "libads/dns.h"
24 /* AIX resolv.h uses 'class' in struct ns_rr */
32 /* resolver headers */
34 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/nameser.h>
40 #define MAX_DNS_PACKET_SIZE 0xffff
42 #ifdef NS_HFIXEDSZ /* Bind 8/9 interface */
43 #if !defined(C_IN) /* AIX 5.3 already defines C_IN */
46 #if !defined(T_A) /* AIX 5.3 already defines T_A */
50 #if defined(HAVE_IPV6)
52 # define T_AAAA ns_t_aaaa
56 # define T_SRV ns_t_srv
57 #if !defined(T_NS) /* AIX 5.3 already defines T_NS */
62 # define NS_HFIXEDSZ HFIXEDSZ
64 # define NS_HFIXEDSZ sizeof(HEADER)
65 # endif /* HFIXEDSZ */
67 # define NS_PACKETSZ PACKETSZ
68 # else /* 512 is usually the default */
69 # define NS_PACKETSZ 512
70 # endif /* PACKETSZ */
74 /*********************************************************************
75 *********************************************************************/
77 static bool ads_dns_parse_query( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
78 uint8_t **ptr, struct dns_query *q )
81 char hostname[MAX_DNS_NAME_LENGTH];
86 if ( !start || !end || !q || !*ptr)
89 /* See RFC 1035 for details. If this fails, then return. */
91 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
96 q->hostname = talloc_strdup( ctx, hostname );
98 /* check that we have space remaining */
100 if ( PTR_DIFF(p+4, end) > 0 )
103 q->type = RSVAL( p, 0 );
104 q->in_class = RSVAL( p, 2 );
112 /*********************************************************************
113 *********************************************************************/
115 static bool ads_dns_parse_rr( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
116 uint8_t **ptr, struct dns_rr *rr )
119 char hostname[MAX_DNS_NAME_LENGTH];
122 if ( !start || !end || !rr || !*ptr)
126 /* pull the name from the answer */
128 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
133 rr->hostname = talloc_strdup( ctx, hostname );
135 /* check that we have space remaining */
137 if ( PTR_DIFF(p+10, end) > 0 )
140 /* pull some values and then skip onto the string */
142 rr->type = RSVAL(p, 0);
143 rr->in_class = RSVAL(p, 2);
144 rr->ttl = RIVAL(p, 4);
145 rr->rdatalen = RSVAL(p, 8);
149 /* sanity check the available space */
151 if ( PTR_DIFF(p+rr->rdatalen, end ) > 0 ) {
156 /* save a point to the rdata for this section */
166 /*********************************************************************
167 *********************************************************************/
169 static bool ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
170 uint8_t **ptr, struct dns_rr_srv *srv )
174 char dcname[MAX_DNS_NAME_LENGTH];
177 if ( !start || !end || !srv || !*ptr)
180 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
181 of the next record */
183 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
184 DEBUG(1,("ads_dns_parse_rr_srv: Failed to parse RR record\n"));
188 if ( rr.type != T_SRV ) {
189 DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n",
196 srv->priority = RSVAL(p, 0);
197 srv->weight = RSVAL(p, 2);
198 srv->port = RSVAL(p, 4);
202 namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
204 DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
208 srv->hostname = talloc_strdup( ctx, dcname );
210 DEBUG(10,("ads_dns_parse_rr_srv: Parsed %s [%u, %u, %u]\n",
219 /*********************************************************************
220 *********************************************************************/
222 static bool ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
223 uint8_t **ptr, struct dns_rr_ns *nsrec )
227 char nsname[MAX_DNS_NAME_LENGTH];
230 if ( !start || !end || !nsrec || !*ptr)
233 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
234 of the next record */
236 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
237 DEBUG(1,("ads_dns_parse_rr_ns: Failed to parse RR record\n"));
241 if ( rr.type != T_NS ) {
242 DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n",
249 /* ame server hostname */
251 namelen = dn_expand( start, end, p, nsname, sizeof(nsname) );
253 DEBUG(1,("ads_dns_parse_rr_ns: Failed to uncompress name!\n"));
256 nsrec->hostname = talloc_strdup( ctx, nsname );
261 /*********************************************************************
262 Sort SRV record list based on weight and priority. See RFC 2782.
263 *********************************************************************/
265 static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b )
267 if ( a->priority == b->priority ) {
269 /* randomize entries with an equal weight and priority */
270 if ( a->weight == b->weight )
273 /* higher weights should be sorted lower */
274 if ( a->weight > b->weight )
280 if ( a->priority < b->priority )
286 /*********************************************************************
287 Simple wrapper for a DNS query
288 *********************************************************************/
290 #define DNS_FAILED_WAITTIME 30
292 static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
293 uint8_t **buf, int *resp_length )
295 uint8_t *buffer = NULL;
297 int resp_len = NS_PACKETSZ;
298 static time_t last_dns_check = 0;
299 static NTSTATUS last_dns_status = NT_STATUS_OK;
300 time_t now = time_mono(NULL);
302 /* Try to prevent bursts of DNS lookups if the server is down */
304 /* Protect against large clock changes */
306 if ( last_dns_check > now )
309 /* IF we had a DNS timeout or a bad server and we are still
310 in the 30 second cache window, just return the previous
311 status and save the network timeout. */
313 if ( (NT_STATUS_EQUAL(last_dns_status,NT_STATUS_IO_TIMEOUT) ||
314 NT_STATUS_EQUAL(last_dns_status,NT_STATUS_CONNECTION_REFUSED)) &&
315 (last_dns_check+DNS_FAILED_WAITTIME) > now )
317 DEBUG(10,("last_dns_check: Returning cached status (%s)\n",
318 nt_errstr(last_dns_status) ));
319 return last_dns_status;
325 TALLOC_FREE( buffer );
327 buf_len = resp_len * sizeof(uint8_t);
330 if ((buffer = talloc_array(ctx, uint8_t, buf_len))
332 DEBUG(0,("ads_dns_lookup_srv: "
333 "talloc() failed!\n"));
334 last_dns_status = NT_STATUS_NO_MEMORY;
335 last_dns_check = time_mono(NULL);
336 return last_dns_status;
340 if ((resp_len = res_query(name, C_IN, q_type, buffer, buf_len))
342 DEBUG(3,("ads_dns_lookup_srv: "
343 "Failed to resolve %s (%s)\n",
344 name, strerror(errno)));
345 TALLOC_FREE( buffer );
346 last_dns_status = NT_STATUS_UNSUCCESSFUL;
348 if (errno == ETIMEDOUT) {
349 last_dns_status = NT_STATUS_IO_TIMEOUT;
351 if (errno == ECONNREFUSED) {
352 last_dns_status = NT_STATUS_CONNECTION_REFUSED;
354 last_dns_check = time_mono(NULL);
355 return last_dns_status;
358 /* On AIX, Solaris, and possibly some older glibc systems (e.g. SLES8)
359 truncated replies never give back a resp_len > buflen
360 which ends up causing DNS resolve failures on large tcp DNS replies */
362 if (buf_len == resp_len) {
363 if (resp_len == MAX_DNS_PACKET_SIZE) {
364 DEBUG(1,("dns_send_req: DNS reply too large when resolving %s\n",
366 TALLOC_FREE( buffer );
367 last_dns_status = NT_STATUS_BUFFER_TOO_SMALL;
368 last_dns_check = time_mono(NULL);
369 return last_dns_status;
372 resp_len = MIN(resp_len*2, MAX_DNS_PACKET_SIZE);
376 } while ( buf_len < resp_len && resp_len <= MAX_DNS_PACKET_SIZE );
379 *resp_length = resp_len;
381 last_dns_check = time_mono(NULL);
382 last_dns_status = NT_STATUS_OK;
383 return last_dns_status;
386 /*********************************************************************
387 Simple wrapper for a DNS SRV query
388 *********************************************************************/
390 static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx,
391 const char *dns_hosts_file,
393 struct dns_rr_srv **dclist,
396 uint8_t *buffer = NULL;
398 struct dns_rr_srv *dcs = NULL;
399 int query_count, answer_count, auth_count, additional_count;
405 if ( !ctx || !name || !dclist ) {
406 return NT_STATUS_INVALID_PARAMETER;
409 if (dns_hosts_file) {
410 return resolve_dns_hosts_file_as_dns_rr(dns_hosts_file,
415 /* Send the request. May have to loop several times in case
418 status = dns_send_req( ctx, name, T_SRV, &buffer, &resp_len );
419 if ( !NT_STATUS_IS_OK(status) ) {
420 DEBUG(3,("ads_dns_lookup_srv: Failed to send DNS query (%s)\n",
426 /* For some insane reason, the ns_initparse() et. al. routines are only
427 available in libresolv.a, and not the shared lib. Who knows why....
428 So we have to parse the DNS reply ourselves */
430 /* Pull the answer RR's count from the header.
431 * Use the NMB ordering macros */
433 query_count = RSVAL( p, 4 );
434 answer_count = RSVAL( p, 6 );
435 auth_count = RSVAL( p, 8 );
436 additional_count = RSVAL( p, 10 );
438 DEBUG(4,("ads_dns_lookup_srv: "
439 "%d records returned in the answer section.\n",
443 if ((dcs = talloc_zero_array(ctx, struct dns_rr_srv,
444 answer_count)) == NULL ) {
445 DEBUG(0,("ads_dns_lookup_srv: "
446 "talloc() failure for %d char*'s\n",
448 return NT_STATUS_NO_MEMORY;
454 /* now skip the header */
458 /* parse the query section */
460 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
463 if (!ads_dns_parse_query(ctx, buffer,
464 buffer+resp_len, &p, &q)) {
465 DEBUG(1,("ads_dns_lookup_srv: "
466 "Failed to parse query record [%d]!\n", rrnum));
467 return NT_STATUS_UNSUCCESSFUL;
471 /* now we are at the answer section */
473 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
474 if (!ads_dns_parse_rr_srv(ctx, buffer, buffer+resp_len,
476 DEBUG(1,("ads_dns_lookup_srv: "
477 "Failed to parse answer recordi [%d]!\n", rrnum));
478 return NT_STATUS_UNSUCCESSFUL;
483 /* Parse the authority section */
484 /* just skip these for now */
486 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
489 if (!ads_dns_parse_rr( ctx, buffer,
490 buffer+resp_len, &p, &rr)) {
491 DEBUG(1,("ads_dns_lookup_srv: "
492 "Failed to parse authority record! [%d]\n", rrnum));
493 return NT_STATUS_UNSUCCESSFUL;
497 /* Parse the additional records section */
499 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
503 if (!ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
505 DEBUG(1,("ads_dns_lookup_srv: Failed "
506 "to parse additional records section! [%d]\n", rrnum));
507 return NT_STATUS_UNSUCCESSFUL;
510 /* Only interested in A or AAAA records as a shortcut for having
511 * to come back later and lookup the name. For multi-homed
512 * hosts, the number of additional records and exceed the
513 * number of answer records. */
515 if (rr.type != T_A || rr.rdatalen != 4) {
516 #if defined(HAVE_IPV6)
517 /* RFC2874 defines A6 records. This
518 * requires recusive and horribly complex lookups.
519 * Bastards. Ignore this for now.... JRA.
520 * Luckily RFC3363 reprecates A6 records.
522 if (rr.type != T_AAAA || rr.rdatalen != 16)
527 for ( i=0; i<idx; i++ ) {
528 if ( strcmp( rr.hostname, dcs[i].hostname ) == 0 ) {
529 int num_ips = dcs[i].num_ips;
530 struct sockaddr_storage *tmp_ss_s;
532 /* allocate new memory */
534 if (dcs[i].num_ips == 0) {
535 if ((dcs[i].ss_s = talloc_array(dcs,
536 struct sockaddr_storage, 1 ))
538 return NT_STATUS_NO_MEMORY;
541 if ((tmp_ss_s = talloc_realloc(dcs,
543 struct sockaddr_storage,
546 return NT_STATUS_NO_MEMORY;
549 dcs[i].ss_s = tmp_ss_s;
553 /* copy the new IP address */
554 if (rr.type == T_A) {
556 memcpy(&ip, rr.rdata, 4);
557 in_addr_to_sockaddr_storage(
558 &dcs[i].ss_s[num_ips],
561 #if defined(HAVE_IPV6)
562 if (rr.type == T_AAAA) {
564 memcpy(&ip6, rr.rdata, rr.rdatalen);
565 in6_addr_to_sockaddr_storage(
566 &dcs[i].ss_s[num_ips],
574 TYPESAFE_QSORT(dcs, idx, dnssrvcmp );
582 /*********************************************************************
583 Simple wrapper for a DNS NS query
584 *********************************************************************/
586 NTSTATUS ads_dns_lookup_ns(TALLOC_CTX *ctx,
587 const char *dns_hosts_file,
588 const char *dnsdomain,
589 struct dns_rr_ns **nslist,
592 uint8_t *buffer = NULL;
594 struct dns_rr_ns *nsarray = NULL;
595 int query_count, answer_count, auth_count, additional_count;
601 if ( !ctx || !dnsdomain || !nslist ) {
602 return NT_STATUS_INVALID_PARAMETER;
605 if (dns_hosts_file) {
606 DEBUG(1, ("NO 'NS' lookup available when using resolv:host file"));
607 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
610 /* Send the request. May have to loop several times in case
613 status = dns_send_req( ctx, dnsdomain, T_NS, &buffer, &resp_len );
614 if ( !NT_STATUS_IS_OK(status) ) {
615 DEBUG(3,("ads_dns_lookup_ns: Failed to send DNS query (%s)\n",
621 /* For some insane reason, the ns_initparse() et. al. routines are only
622 available in libresolv.a, and not the shared lib. Who knows why....
623 So we have to parse the DNS reply ourselves */
625 /* Pull the answer RR's count from the header.
626 * Use the NMB ordering macros */
628 query_count = RSVAL( p, 4 );
629 answer_count = RSVAL( p, 6 );
630 auth_count = RSVAL( p, 8 );
631 additional_count = RSVAL( p, 10 );
633 DEBUG(4,("ads_dns_lookup_ns: "
634 "%d records returned in the answer section.\n",
638 if ((nsarray = talloc_array(ctx, struct dns_rr_ns,
639 answer_count)) == NULL ) {
640 DEBUG(0,("ads_dns_lookup_ns: "
641 "talloc() failure for %d char*'s\n",
643 return NT_STATUS_NO_MEMORY;
649 /* now skip the header */
653 /* parse the query section */
655 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
658 if (!ads_dns_parse_query(ctx, buffer, buffer+resp_len,
660 DEBUG(1,("ads_dns_lookup_ns: "
661 " Failed to parse query record!\n"));
662 return NT_STATUS_UNSUCCESSFUL;
666 /* now we are at the answer section */
668 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
669 if (!ads_dns_parse_rr_ns(ctx, buffer, buffer+resp_len,
670 &p, &nsarray[rrnum])) {
671 DEBUG(1,("ads_dns_lookup_ns: "
672 "Failed to parse answer record!\n"));
673 return NT_STATUS_UNSUCCESSFUL;
678 /* Parse the authority section */
679 /* just skip these for now */
681 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
684 if ( !ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
686 DEBUG(1,("ads_dns_lookup_ns: "
687 "Failed to parse authority record!\n"));
688 return NT_STATUS_UNSUCCESSFUL;
692 /* Parse the additional records section */
694 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
698 if (!ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
700 DEBUG(1,("ads_dns_lookup_ns: Failed "
701 "to parse additional records section!\n"));
702 return NT_STATUS_UNSUCCESSFUL;
705 /* only interested in A records as a shortcut for having to come
706 back later and lookup the name */
708 if (rr.type != T_A || rr.rdatalen != 4) {
709 #if defined(HAVE_IPV6)
710 if (rr.type != T_AAAA || rr.rdatalen != 16)
715 for ( i=0; i<idx; i++ ) {
716 if (strcmp(rr.hostname, nsarray[i].hostname) == 0) {
717 if (rr.type == T_A) {
719 memcpy(&ip, rr.rdata, 4);
720 in_addr_to_sockaddr_storage(
724 #if defined(HAVE_IPV6)
725 if (rr.type == T_AAAA) {
727 memcpy(&ip6, rr.rdata, rr.rdatalen);
728 in6_addr_to_sockaddr_storage(
743 /********************************************************************
744 Query with optional sitename.
745 ********************************************************************/
747 static NTSTATUS ads_dns_query_internal(TALLOC_CTX *ctx,
748 const char *dns_hosts_file,
749 const char *servicename,
750 const char *dc_pdc_gc_domains,
752 const char *sitename,
753 struct dns_rr_srv **dclist,
757 if (sitename && strlen(sitename)) {
758 name = talloc_asprintf(ctx, "%s._tcp.%s._sites.%s._msdcs.%s",
759 servicename, sitename,
760 dc_pdc_gc_domains, realm);
762 name = talloc_asprintf(ctx, "%s._tcp.%s._msdcs.%s",
763 servicename, dc_pdc_gc_domains, realm);
766 return NT_STATUS_NO_MEMORY;
768 return ads_dns_lookup_srv(ctx, dns_hosts_file, name, dclist, numdcs);
771 /********************************************************************
773 ********************************************************************/
775 NTSTATUS ads_dns_query_dcs(TALLOC_CTX *ctx,
776 const char *dns_hosts_file,
778 const char *sitename,
779 struct dns_rr_srv **dclist,
784 status = ads_dns_query_internal(ctx, dns_hosts_file, "_ldap", "dc",
785 realm, sitename, dclist, numdcs);
787 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
788 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
793 ((!NT_STATUS_IS_OK(status)) ||
794 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
795 /* Sitename DNS query may have failed. Try without. */
796 status = ads_dns_query_internal(ctx, dns_hosts_file,
797 "_ldap", "dc", realm,
798 NULL, dclist, numdcs);
803 /********************************************************************
805 ********************************************************************/
807 NTSTATUS ads_dns_query_gcs(TALLOC_CTX *ctx,
808 const char *dns_hosts_file,
810 const char *sitename,
811 struct dns_rr_srv **dclist,
816 status = ads_dns_query_internal(ctx, dns_hosts_file, "_ldap", "gc",
817 realm, sitename, dclist, numdcs);
819 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
820 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
825 ((!NT_STATUS_IS_OK(status)) ||
826 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
827 /* Sitename DNS query may have failed. Try without. */
828 status = ads_dns_query_internal(ctx, dns_hosts_file,
829 "_ldap", "gc", realm,
830 NULL, dclist, numdcs);
835 /********************************************************************
837 Even if our underlying kerberos libraries are UDP only, this
838 is pretty safe as it's unlikely that a KDC supports TCP and not UDP.
839 ********************************************************************/
841 NTSTATUS ads_dns_query_kdcs(TALLOC_CTX *ctx,
842 const char *dns_hosts_file,
843 const char *dns_forest_name,
844 const char *sitename,
845 struct dns_rr_srv **dclist,
850 status = ads_dns_query_internal(ctx, dns_hosts_file, "_kerberos", "dc",
851 dns_forest_name, sitename, dclist,
854 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
855 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
860 ((!NT_STATUS_IS_OK(status)) ||
861 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
862 /* Sitename DNS query may have failed. Try without. */
863 status = ads_dns_query_internal(ctx, dns_hosts_file,
865 dns_forest_name, NULL,
871 /********************************************************************
872 Query for AD PDC. Sitename is obsolete here.
873 ********************************************************************/
875 NTSTATUS ads_dns_query_pdc(TALLOC_CTX *ctx,
876 const char *dns_hosts_file,
877 const char *dns_domain_name,
878 struct dns_rr_srv **dclist,
881 return ads_dns_query_internal(ctx, dns_hosts_file, "_ldap", "pdc",
882 dns_domain_name, NULL, dclist, numdcs);
885 /********************************************************************
886 Query for AD DC by guid. Sitename is obsolete here.
887 ********************************************************************/
889 NTSTATUS ads_dns_query_dcs_guid(TALLOC_CTX *ctx,
890 const char *dns_hosts_file,
891 const char *dns_forest_name,
892 const char *domain_guid,
893 struct dns_rr_srv **dclist,
896 /*_ldap._tcp.DomainGuid.domains._msdcs.DnsForestName */
901 domains = talloc_asprintf(ctx, "%s.domains", domain_guid);
903 return NT_STATUS_NO_MEMORY;
906 return ads_dns_query_internal(ctx, dns_hosts_file, "_ldap", domains,
907 dns_forest_name, NULL, dclist, numdcs);