s3/libads: use monotonic clock for DNS timeouts
[Samba/gebeck_regimport.git] / source3 / libads / dns.c
blobe7d8a40236b29f5e5ec136c97343fa7d2f0be92b
1 /*
2 Unix SMB/CIFS implementation.
3 DNS utility library
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/>.
21 #include "includes.h"
22 #include "libads/dns.h"
24 /* AIX resolv.h uses 'class' in struct ns_rr */
26 #if defined(AIX)
27 # if defined(class)
28 # undef class
29 # endif
30 #endif /* AIX */
32 /* resolver headers */
34 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/nameser.h>
37 #include <resolv.h>
38 #include <netdb.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 */
44 # define C_IN ns_c_in
45 #endif
46 #if !defined(T_A) /* AIX 5.3 already defines T_A */
47 # define T_A ns_t_a
48 #endif
50 #if defined(HAVE_IPV6)
51 #if !defined(T_AAAA)
52 # define T_AAAA ns_t_aaaa
53 #endif
54 #endif
56 # define T_SRV ns_t_srv
57 #if !defined(T_NS) /* AIX 5.3 already defines T_NS */
58 # define T_NS ns_t_ns
59 #endif
60 #else
61 # ifdef HFIXEDSZ
62 # define NS_HFIXEDSZ HFIXEDSZ
63 # else
64 # define NS_HFIXEDSZ sizeof(HEADER)
65 # endif /* HFIXEDSZ */
66 # ifdef PACKETSZ
67 # define NS_PACKETSZ PACKETSZ
68 # else /* 512 is usually the default */
69 # define NS_PACKETSZ 512
70 # endif /* PACKETSZ */
71 # define T_SRV 33
72 #endif
74 /*********************************************************************
75 *********************************************************************/
77 static bool ads_dns_parse_query( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
78 uint8 **ptr, struct dns_query *q )
80 uint8 *p = *ptr;
81 char hostname[MAX_DNS_NAME_LENGTH];
82 int namelen;
84 ZERO_STRUCTP( q );
86 if ( !start || !end || !q || !*ptr)
87 return False;
89 /* See RFC 1035 for details. If this fails, then return. */
91 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
92 if ( namelen < 0 ) {
93 return False;
95 p += namelen;
96 q->hostname = talloc_strdup( ctx, hostname );
98 /* check that we have space remaining */
100 if ( PTR_DIFF(p+4, end) > 0 )
101 return False;
103 q->type = RSVAL( p, 0 );
104 q->in_class = RSVAL( p, 2 );
105 p += 4;
107 *ptr = p;
109 return True;
112 /*********************************************************************
113 *********************************************************************/
115 static bool ads_dns_parse_rr( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
116 uint8 **ptr, struct dns_rr *rr )
118 uint8 *p = *ptr;
119 char hostname[MAX_DNS_NAME_LENGTH];
120 int namelen;
122 if ( !start || !end || !rr || !*ptr)
123 return -1;
125 ZERO_STRUCTP( rr );
126 /* pull the name from the answer */
128 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
129 if ( namelen < 0 ) {
130 return -1;
132 p += namelen;
133 rr->hostname = talloc_strdup( ctx, hostname );
135 /* check that we have space remaining */
137 if ( PTR_DIFF(p+10, end) > 0 )
138 return False;
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);
147 p += 10;
149 /* sanity check the available space */
151 if ( PTR_DIFF(p+rr->rdatalen, end ) > 0 ) {
152 return False;
156 /* save a point to the rdata for this section */
158 rr->rdata = p;
159 p += rr->rdatalen;
161 *ptr = p;
163 return True;
166 /*********************************************************************
167 *********************************************************************/
169 static bool ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
170 uint8 **ptr, struct dns_rr_srv *srv )
172 struct dns_rr rr;
173 uint8 *p;
174 char dcname[MAX_DNS_NAME_LENGTH];
175 int namelen;
177 if ( !start || !end || !srv || !*ptr)
178 return -1;
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"));
185 return False;
188 if ( rr.type != T_SRV ) {
189 DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n",
190 rr.type));
191 return False;
194 p = rr.rdata;
196 srv->priority = RSVAL(p, 0);
197 srv->weight = RSVAL(p, 2);
198 srv->port = RSVAL(p, 4);
200 p += 6;
202 namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
203 if ( namelen < 0 ) {
204 DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
205 return False;
208 srv->hostname = talloc_strdup( ctx, dcname );
210 DEBUG(10,("ads_dns_parse_rr_srv: Parsed %s [%u, %u, %u]\n",
211 srv->hostname,
212 srv->priority,
213 srv->weight,
214 srv->port));
216 return True;
219 /*********************************************************************
220 *********************************************************************/
222 static bool ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
223 uint8 **ptr, struct dns_rr_ns *nsrec )
225 struct dns_rr rr;
226 uint8 *p;
227 char nsname[MAX_DNS_NAME_LENGTH];
228 int namelen;
230 if ( !start || !end || !nsrec || !*ptr)
231 return -1;
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"));
238 return False;
241 if ( rr.type != T_NS ) {
242 DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n",
243 rr.type));
244 return False;
247 p = rr.rdata;
249 /* ame server hostname */
251 namelen = dn_expand( start, end, p, nsname, sizeof(nsname) );
252 if ( namelen < 0 ) {
253 DEBUG(1,("ads_dns_parse_rr_ns: Failed to uncompress name!\n"));
254 return False;
256 nsrec->hostname = talloc_strdup( ctx, nsname );
258 return True;
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 )
271 return 0;
273 /* higher weights should be sorted lower */
274 if ( a->weight > b->weight )
275 return -1;
276 else
277 return 1;
280 if ( a->priority < b->priority )
281 return -1;
283 return 1;
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 **buf, int *resp_length )
295 uint8 *buffer = NULL;
296 size_t buf_len = 0;
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 )
307 last_dns_check = 0;
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;
322 /* Send the Query */
323 do {
324 if ( buffer )
325 TALLOC_FREE( buffer );
327 buf_len = resp_len * sizeof(uint8);
329 if (buf_len) {
330 if ((buffer = TALLOC_ARRAY(ctx, uint8, buf_len))
331 == NULL ) {
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))
341 < 0 ) {
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",
365 name));
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 );
378 *buf = buffer;
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 *name,
392 struct dns_rr_srv **dclist,
393 int *numdcs)
395 uint8 *buffer = NULL;
396 int resp_len = 0;
397 struct dns_rr_srv *dcs = NULL;
398 int query_count, answer_count, auth_count, additional_count;
399 uint8 *p = buffer;
400 int rrnum;
401 int idx = 0;
402 NTSTATUS status;
404 if ( !ctx || !name || !dclist ) {
405 return NT_STATUS_INVALID_PARAMETER;
408 /* Send the request. May have to loop several times in case
409 of large replies */
411 status = dns_send_req( ctx, name, T_SRV, &buffer, &resp_len );
412 if ( !NT_STATUS_IS_OK(status) ) {
413 DEBUG(3,("ads_dns_lookup_srv: Failed to send DNS query (%s)\n",
414 nt_errstr(status)));
415 return status;
417 p = buffer;
419 /* For some insane reason, the ns_initparse() et. al. routines are only
420 available in libresolv.a, and not the shared lib. Who knows why....
421 So we have to parse the DNS reply ourselves */
423 /* Pull the answer RR's count from the header.
424 * Use the NMB ordering macros */
426 query_count = RSVAL( p, 4 );
427 answer_count = RSVAL( p, 6 );
428 auth_count = RSVAL( p, 8 );
429 additional_count = RSVAL( p, 10 );
431 DEBUG(4,("ads_dns_lookup_srv: "
432 "%d records returned in the answer section.\n",
433 answer_count));
435 if (answer_count) {
436 if ((dcs = TALLOC_ZERO_ARRAY(ctx, struct dns_rr_srv,
437 answer_count)) == NULL ) {
438 DEBUG(0,("ads_dns_lookup_srv: "
439 "talloc() failure for %d char*'s\n",
440 answer_count));
441 return NT_STATUS_NO_MEMORY;
443 } else {
444 dcs = NULL;
447 /* now skip the header */
449 p += NS_HFIXEDSZ;
451 /* parse the query section */
453 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
454 struct dns_query q;
456 if (!ads_dns_parse_query(ctx, buffer,
457 buffer+resp_len, &p, &q)) {
458 DEBUG(1,("ads_dns_lookup_srv: "
459 "Failed to parse query record [%d]!\n", rrnum));
460 return NT_STATUS_UNSUCCESSFUL;
464 /* now we are at the answer section */
466 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
467 if (!ads_dns_parse_rr_srv(ctx, buffer, buffer+resp_len,
468 &p, &dcs[rrnum])) {
469 DEBUG(1,("ads_dns_lookup_srv: "
470 "Failed to parse answer recordi [%d]!\n", rrnum));
471 return NT_STATUS_UNSUCCESSFUL;
474 idx = rrnum;
476 /* Parse the authority section */
477 /* just skip these for now */
479 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
480 struct dns_rr rr;
482 if (!ads_dns_parse_rr( ctx, buffer,
483 buffer+resp_len, &p, &rr)) {
484 DEBUG(1,("ads_dns_lookup_srv: "
485 "Failed to parse authority record! [%d]\n", rrnum));
486 return NT_STATUS_UNSUCCESSFUL;
490 /* Parse the additional records section */
492 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
493 struct dns_rr rr;
494 int i;
496 if (!ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
497 &p, &rr)) {
498 DEBUG(1,("ads_dns_lookup_srv: Failed "
499 "to parse additional records section! [%d]\n", rrnum));
500 return NT_STATUS_UNSUCCESSFUL;
503 /* Only interested in A or AAAA records as a shortcut for having
504 * to come back later and lookup the name. For multi-homed
505 * hosts, the number of additional records and exceed the
506 * number of answer records. */
508 if (rr.type != T_A || rr.rdatalen != 4) {
509 #if defined(HAVE_IPV6)
510 /* RFC2874 defines A6 records. This
511 * requires recusive and horribly complex lookups.
512 * Bastards. Ignore this for now.... JRA.
513 * Luckily RFC3363 reprecates A6 records.
515 if (rr.type != T_AAAA || rr.rdatalen != 16)
516 #endif
517 continue;
520 for ( i=0; i<idx; i++ ) {
521 if ( strcmp( rr.hostname, dcs[i].hostname ) == 0 ) {
522 int num_ips = dcs[i].num_ips;
523 struct sockaddr_storage *tmp_ss_s;
525 /* allocate new memory */
527 if (dcs[i].num_ips == 0) {
528 if ((dcs[i].ss_s = TALLOC_ARRAY(dcs,
529 struct sockaddr_storage, 1 ))
530 == NULL ) {
531 return NT_STATUS_NO_MEMORY;
533 } else {
534 if ((tmp_ss_s = TALLOC_REALLOC_ARRAY(dcs,
535 dcs[i].ss_s,
536 struct sockaddr_storage,
537 dcs[i].num_ips+1))
538 == NULL ) {
539 return NT_STATUS_NO_MEMORY;
542 dcs[i].ss_s = tmp_ss_s;
544 dcs[i].num_ips++;
546 /* copy the new IP address */
547 if (rr.type == T_A) {
548 struct in_addr ip;
549 memcpy(&ip, rr.rdata, 4);
550 in_addr_to_sockaddr_storage(
551 &dcs[i].ss_s[num_ips],
552 ip);
554 #if defined(HAVE_IPV6)
555 if (rr.type == T_AAAA) {
556 struct in6_addr ip6;
557 memcpy(&ip6, rr.rdata, rr.rdatalen);
558 in6_addr_to_sockaddr_storage(
559 &dcs[i].ss_s[num_ips],
560 ip6);
562 #endif
567 TYPESAFE_QSORT(dcs, idx, dnssrvcmp );
569 *dclist = dcs;
570 *numdcs = idx;
572 return NT_STATUS_OK;
575 /*********************************************************************
576 Simple wrapper for a DNS NS query
577 *********************************************************************/
579 NTSTATUS ads_dns_lookup_ns(TALLOC_CTX *ctx,
580 const char *dnsdomain,
581 struct dns_rr_ns **nslist,
582 int *numns)
584 uint8 *buffer = NULL;
585 int resp_len = 0;
586 struct dns_rr_ns *nsarray = NULL;
587 int query_count, answer_count, auth_count, additional_count;
588 uint8 *p;
589 int rrnum;
590 int idx = 0;
591 NTSTATUS status;
593 if ( !ctx || !dnsdomain || !nslist ) {
594 return NT_STATUS_INVALID_PARAMETER;
597 /* Send the request. May have to loop several times in case
598 of large replies */
600 status = dns_send_req( ctx, dnsdomain, T_NS, &buffer, &resp_len );
601 if ( !NT_STATUS_IS_OK(status) ) {
602 DEBUG(3,("ads_dns_lookup_ns: Failed to send DNS query (%s)\n",
603 nt_errstr(status)));
604 return status;
606 p = buffer;
608 /* For some insane reason, the ns_initparse() et. al. routines are only
609 available in libresolv.a, and not the shared lib. Who knows why....
610 So we have to parse the DNS reply ourselves */
612 /* Pull the answer RR's count from the header.
613 * Use the NMB ordering macros */
615 query_count = RSVAL( p, 4 );
616 answer_count = RSVAL( p, 6 );
617 auth_count = RSVAL( p, 8 );
618 additional_count = RSVAL( p, 10 );
620 DEBUG(4,("ads_dns_lookup_ns: "
621 "%d records returned in the answer section.\n",
622 answer_count));
624 if (answer_count) {
625 if ((nsarray = TALLOC_ARRAY(ctx, struct dns_rr_ns,
626 answer_count)) == NULL ) {
627 DEBUG(0,("ads_dns_lookup_ns: "
628 "talloc() failure for %d char*'s\n",
629 answer_count));
630 return NT_STATUS_NO_MEMORY;
632 } else {
633 nsarray = NULL;
636 /* now skip the header */
638 p += NS_HFIXEDSZ;
640 /* parse the query section */
642 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
643 struct dns_query q;
645 if (!ads_dns_parse_query(ctx, buffer, buffer+resp_len,
646 &p, &q)) {
647 DEBUG(1,("ads_dns_lookup_ns: "
648 " Failed to parse query record!\n"));
649 return NT_STATUS_UNSUCCESSFUL;
653 /* now we are at the answer section */
655 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
656 if (!ads_dns_parse_rr_ns(ctx, buffer, buffer+resp_len,
657 &p, &nsarray[rrnum])) {
658 DEBUG(1,("ads_dns_lookup_ns: "
659 "Failed to parse answer record!\n"));
660 return NT_STATUS_UNSUCCESSFUL;
663 idx = rrnum;
665 /* Parse the authority section */
666 /* just skip these for now */
668 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
669 struct dns_rr rr;
671 if ( !ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
672 &p, &rr)) {
673 DEBUG(1,("ads_dns_lookup_ns: "
674 "Failed to parse authority record!\n"));
675 return NT_STATUS_UNSUCCESSFUL;
679 /* Parse the additional records section */
681 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
682 struct dns_rr rr;
683 int i;
685 if (!ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
686 &p, &rr)) {
687 DEBUG(1,("ads_dns_lookup_ns: Failed "
688 "to parse additional records section!\n"));
689 return NT_STATUS_UNSUCCESSFUL;
692 /* only interested in A records as a shortcut for having to come
693 back later and lookup the name */
695 if (rr.type != T_A || rr.rdatalen != 4) {
696 #if defined(HAVE_IPV6)
697 if (rr.type != T_AAAA || rr.rdatalen != 16)
698 #endif
699 continue;
702 for ( i=0; i<idx; i++ ) {
703 if (strcmp(rr.hostname, nsarray[i].hostname) == 0) {
704 if (rr.type == T_A) {
705 struct in_addr ip;
706 memcpy(&ip, rr.rdata, 4);
707 in_addr_to_sockaddr_storage(
708 &nsarray[i].ss,
709 ip);
711 #if defined(HAVE_IPV6)
712 if (rr.type == T_AAAA) {
713 struct in6_addr ip6;
714 memcpy(&ip6, rr.rdata, rr.rdatalen);
715 in6_addr_to_sockaddr_storage(
716 &nsarray[i].ss,
717 ip6);
719 #endif
724 *nslist = nsarray;
725 *numns = idx;
727 return NT_STATUS_OK;
730 /********************************************************************
731 Query with optional sitename.
732 ********************************************************************/
734 static NTSTATUS ads_dns_query_internal(TALLOC_CTX *ctx,
735 const char *servicename,
736 const char *dc_pdc_gc_domains,
737 const char *realm,
738 const char *sitename,
739 struct dns_rr_srv **dclist,
740 int *numdcs )
742 char *name;
743 if (sitename) {
744 name = talloc_asprintf(ctx, "%s._tcp.%s._sites.%s._msdcs.%s",
745 servicename, sitename,
746 dc_pdc_gc_domains, realm);
747 } else {
748 name = talloc_asprintf(ctx, "%s._tcp.%s._msdcs.%s",
749 servicename, dc_pdc_gc_domains, realm);
751 if (!name) {
752 return NT_STATUS_NO_MEMORY;
754 return ads_dns_lookup_srv( ctx, name, dclist, numdcs );
757 /********************************************************************
758 Query for AD DC's.
759 ********************************************************************/
761 NTSTATUS ads_dns_query_dcs(TALLOC_CTX *ctx,
762 const char *realm,
763 const char *sitename,
764 struct dns_rr_srv **dclist,
765 int *numdcs )
767 NTSTATUS status;
769 status = ads_dns_query_internal(ctx, "_ldap", "dc", realm, sitename,
770 dclist, numdcs);
772 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
773 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
774 return status;
777 if (sitename &&
778 ((!NT_STATUS_IS_OK(status)) ||
779 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
780 /* Sitename DNS query may have failed. Try without. */
781 status = ads_dns_query_internal(ctx, "_ldap", "dc", realm,
782 NULL, dclist, numdcs);
784 return status;
787 /********************************************************************
788 Query for AD GC's.
789 ********************************************************************/
791 NTSTATUS ads_dns_query_gcs(TALLOC_CTX *ctx,
792 const char *realm,
793 const char *sitename,
794 struct dns_rr_srv **dclist,
795 int *numdcs )
797 NTSTATUS status;
799 status = ads_dns_query_internal(ctx, "_ldap", "gc", realm, sitename,
800 dclist, numdcs);
802 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
803 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
804 return status;
807 if (sitename &&
808 ((!NT_STATUS_IS_OK(status)) ||
809 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
810 /* Sitename DNS query may have failed. Try without. */
811 status = ads_dns_query_internal(ctx, "_ldap", "gc", realm,
812 NULL, dclist, numdcs);
814 return status;
817 /********************************************************************
818 Query for AD KDC's.
819 Even if our underlying kerberos libraries are UDP only, this
820 is pretty safe as it's unlikely that a KDC supports TCP and not UDP.
821 ********************************************************************/
823 NTSTATUS ads_dns_query_kdcs(TALLOC_CTX *ctx,
824 const char *dns_forest_name,
825 const char *sitename,
826 struct dns_rr_srv **dclist,
827 int *numdcs )
829 NTSTATUS status;
831 status = ads_dns_query_internal(ctx, "_kerberos", "dc",
832 dns_forest_name, sitename, dclist,
833 numdcs);
835 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
836 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
837 return status;
840 if (sitename &&
841 ((!NT_STATUS_IS_OK(status)) ||
842 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
843 /* Sitename DNS query may have failed. Try without. */
844 status = ads_dns_query_internal(ctx, "_kerberos", "dc",
845 dns_forest_name, NULL,
846 dclist, numdcs);
848 return status;
851 /********************************************************************
852 Query for AD PDC. Sitename is obsolete here.
853 ********************************************************************/
855 NTSTATUS ads_dns_query_pdc(TALLOC_CTX *ctx,
856 const char *dns_domain_name,
857 struct dns_rr_srv **dclist,
858 int *numdcs )
860 return ads_dns_query_internal(ctx, "_ldap", "pdc", dns_domain_name,
861 NULL, dclist, numdcs);
864 /********************************************************************
865 Query for AD DC by guid. Sitename is obsolete here.
866 ********************************************************************/
868 NTSTATUS ads_dns_query_dcs_guid(TALLOC_CTX *ctx,
869 const char *dns_forest_name,
870 const struct GUID *domain_guid,
871 struct dns_rr_srv **dclist,
872 int *numdcs )
874 /*_ldap._tcp.DomainGuid.domains._msdcs.DnsForestName */
876 const char *domains;
877 char *guid_string;
879 guid_string = GUID_string(ctx, domain_guid);
880 if (!guid_string) {
881 return NT_STATUS_NO_MEMORY;
884 /* little hack */
885 domains = talloc_asprintf(ctx, "%s.domains", guid_string);
886 if (!domains) {
887 return NT_STATUS_NO_MEMORY;
889 TALLOC_FREE(guid_string);
891 return ads_dns_query_internal(ctx, "_ldap", domains, dns_forest_name,
892 NULL, dclist, numdcs);