s3-libsmb/clidfs.c: remove cli_nt_error()
[Samba/gebeck_regimport.git] / source3 / libads / dns.c
blob5eae10ec28593fe572d08bea993a4080de26f959
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"
23 #include "../librpc/ndr/libndr.h"
25 /* AIX resolv.h uses 'class' in struct ns_rr */
27 #if defined(AIX)
28 # if defined(class)
29 # undef class
30 # endif
31 #endif /* AIX */
33 /* resolver headers */
35 #include <sys/types.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
38 #include <resolv.h>
39 #include <netdb.h>
41 #define MAX_DNS_PACKET_SIZE 0xffff
43 #ifdef NS_HFIXEDSZ /* Bind 8/9 interface */
44 #if !defined(C_IN) /* AIX 5.3 already defines C_IN */
45 # define C_IN ns_c_in
46 #endif
47 #if !defined(T_A) /* AIX 5.3 already defines T_A */
48 # define T_A ns_t_a
49 #endif
51 #if defined(HAVE_IPV6)
52 #if !defined(T_AAAA)
53 # define T_AAAA ns_t_aaaa
54 #endif
55 #endif
57 # define T_SRV ns_t_srv
58 #if !defined(T_NS) /* AIX 5.3 already defines T_NS */
59 # define T_NS ns_t_ns
60 #endif
61 #else
62 # ifdef HFIXEDSZ
63 # define NS_HFIXEDSZ HFIXEDSZ
64 # else
65 # define NS_HFIXEDSZ sizeof(HEADER)
66 # endif /* HFIXEDSZ */
67 # ifdef PACKETSZ
68 # define NS_PACKETSZ PACKETSZ
69 # else /* 512 is usually the default */
70 # define NS_PACKETSZ 512
71 # endif /* PACKETSZ */
72 # define T_SRV 33
73 #endif
75 /*********************************************************************
76 *********************************************************************/
78 static bool ads_dns_parse_query( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
79 uint8 **ptr, struct dns_query *q )
81 uint8 *p = *ptr;
82 char hostname[MAX_DNS_NAME_LENGTH];
83 int namelen;
85 ZERO_STRUCTP( q );
87 if ( !start || !end || !q || !*ptr)
88 return False;
90 /* See RFC 1035 for details. If this fails, then return. */
92 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
93 if ( namelen < 0 ) {
94 return False;
96 p += namelen;
97 q->hostname = talloc_strdup( ctx, hostname );
99 /* check that we have space remaining */
101 if ( PTR_DIFF(p+4, end) > 0 )
102 return False;
104 q->type = RSVAL( p, 0 );
105 q->in_class = RSVAL( p, 2 );
106 p += 4;
108 *ptr = p;
110 return True;
113 /*********************************************************************
114 *********************************************************************/
116 static bool ads_dns_parse_rr( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
117 uint8 **ptr, struct dns_rr *rr )
119 uint8 *p = *ptr;
120 char hostname[MAX_DNS_NAME_LENGTH];
121 int namelen;
123 if ( !start || !end || !rr || !*ptr)
124 return -1;
126 ZERO_STRUCTP( rr );
127 /* pull the name from the answer */
129 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
130 if ( namelen < 0 ) {
131 return -1;
133 p += namelen;
134 rr->hostname = talloc_strdup( ctx, hostname );
136 /* check that we have space remaining */
138 if ( PTR_DIFF(p+10, end) > 0 )
139 return False;
141 /* pull some values and then skip onto the string */
143 rr->type = RSVAL(p, 0);
144 rr->in_class = RSVAL(p, 2);
145 rr->ttl = RIVAL(p, 4);
146 rr->rdatalen = RSVAL(p, 8);
148 p += 10;
150 /* sanity check the available space */
152 if ( PTR_DIFF(p+rr->rdatalen, end ) > 0 ) {
153 return False;
157 /* save a point to the rdata for this section */
159 rr->rdata = p;
160 p += rr->rdatalen;
162 *ptr = p;
164 return True;
167 /*********************************************************************
168 *********************************************************************/
170 static bool ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
171 uint8 **ptr, struct dns_rr_srv *srv )
173 struct dns_rr rr;
174 uint8 *p;
175 char dcname[MAX_DNS_NAME_LENGTH];
176 int namelen;
178 if ( !start || !end || !srv || !*ptr)
179 return -1;
181 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
182 of the next record */
184 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
185 DEBUG(1,("ads_dns_parse_rr_srv: Failed to parse RR record\n"));
186 return False;
189 if ( rr.type != T_SRV ) {
190 DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n",
191 rr.type));
192 return False;
195 p = rr.rdata;
197 srv->priority = RSVAL(p, 0);
198 srv->weight = RSVAL(p, 2);
199 srv->port = RSVAL(p, 4);
201 p += 6;
203 namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
204 if ( namelen < 0 ) {
205 DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
206 return False;
209 srv->hostname = talloc_strdup( ctx, dcname );
211 DEBUG(10,("ads_dns_parse_rr_srv: Parsed %s [%u, %u, %u]\n",
212 srv->hostname,
213 srv->priority,
214 srv->weight,
215 srv->port));
217 return True;
220 /*********************************************************************
221 *********************************************************************/
223 static bool ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
224 uint8 **ptr, struct dns_rr_ns *nsrec )
226 struct dns_rr rr;
227 uint8 *p;
228 char nsname[MAX_DNS_NAME_LENGTH];
229 int namelen;
231 if ( !start || !end || !nsrec || !*ptr)
232 return -1;
234 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
235 of the next record */
237 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
238 DEBUG(1,("ads_dns_parse_rr_ns: Failed to parse RR record\n"));
239 return False;
242 if ( rr.type != T_NS ) {
243 DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n",
244 rr.type));
245 return False;
248 p = rr.rdata;
250 /* ame server hostname */
252 namelen = dn_expand( start, end, p, nsname, sizeof(nsname) );
253 if ( namelen < 0 ) {
254 DEBUG(1,("ads_dns_parse_rr_ns: Failed to uncompress name!\n"));
255 return False;
257 nsrec->hostname = talloc_strdup( ctx, nsname );
259 return True;
262 /*********************************************************************
263 Sort SRV record list based on weight and priority. See RFC 2782.
264 *********************************************************************/
266 static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b )
268 if ( a->priority == b->priority ) {
270 /* randomize entries with an equal weight and priority */
271 if ( a->weight == b->weight )
272 return 0;
274 /* higher weights should be sorted lower */
275 if ( a->weight > b->weight )
276 return -1;
277 else
278 return 1;
281 if ( a->priority < b->priority )
282 return -1;
284 return 1;
287 /*********************************************************************
288 Simple wrapper for a DNS query
289 *********************************************************************/
291 #define DNS_FAILED_WAITTIME 30
293 static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
294 uint8 **buf, int *resp_length )
296 uint8 *buffer = NULL;
297 size_t buf_len = 0;
298 int resp_len = NS_PACKETSZ;
299 static time_t last_dns_check = 0;
300 static NTSTATUS last_dns_status = NT_STATUS_OK;
301 time_t now = time_mono(NULL);
303 /* Try to prevent bursts of DNS lookups if the server is down */
305 /* Protect against large clock changes */
307 if ( last_dns_check > now )
308 last_dns_check = 0;
310 /* IF we had a DNS timeout or a bad server and we are still
311 in the 30 second cache window, just return the previous
312 status and save the network timeout. */
314 if ( (NT_STATUS_EQUAL(last_dns_status,NT_STATUS_IO_TIMEOUT) ||
315 NT_STATUS_EQUAL(last_dns_status,NT_STATUS_CONNECTION_REFUSED)) &&
316 (last_dns_check+DNS_FAILED_WAITTIME) > now )
318 DEBUG(10,("last_dns_check: Returning cached status (%s)\n",
319 nt_errstr(last_dns_status) ));
320 return last_dns_status;
323 /* Send the Query */
324 do {
325 if ( buffer )
326 TALLOC_FREE( buffer );
328 buf_len = resp_len * sizeof(uint8);
330 if (buf_len) {
331 if ((buffer = talloc_array(ctx, uint8, buf_len))
332 == NULL ) {
333 DEBUG(0,("ads_dns_lookup_srv: "
334 "talloc() failed!\n"));
335 last_dns_status = NT_STATUS_NO_MEMORY;
336 last_dns_check = time_mono(NULL);
337 return last_dns_status;
341 if ((resp_len = res_query(name, C_IN, q_type, buffer, buf_len))
342 < 0 ) {
343 DEBUG(3,("ads_dns_lookup_srv: "
344 "Failed to resolve %s (%s)\n",
345 name, strerror(errno)));
346 TALLOC_FREE( buffer );
347 last_dns_status = NT_STATUS_UNSUCCESSFUL;
349 if (errno == ETIMEDOUT) {
350 last_dns_status = NT_STATUS_IO_TIMEOUT;
352 if (errno == ECONNREFUSED) {
353 last_dns_status = NT_STATUS_CONNECTION_REFUSED;
355 last_dns_check = time_mono(NULL);
356 return last_dns_status;
359 /* On AIX, Solaris, and possibly some older glibc systems (e.g. SLES8)
360 truncated replies never give back a resp_len > buflen
361 which ends up causing DNS resolve failures on large tcp DNS replies */
363 if (buf_len == resp_len) {
364 if (resp_len == MAX_DNS_PACKET_SIZE) {
365 DEBUG(1,("dns_send_req: DNS reply too large when resolving %s\n",
366 name));
367 TALLOC_FREE( buffer );
368 last_dns_status = NT_STATUS_BUFFER_TOO_SMALL;
369 last_dns_check = time_mono(NULL);
370 return last_dns_status;
373 resp_len = MIN(resp_len*2, MAX_DNS_PACKET_SIZE);
377 } while ( buf_len < resp_len && resp_len <= MAX_DNS_PACKET_SIZE );
379 *buf = buffer;
380 *resp_length = resp_len;
382 last_dns_check = time_mono(NULL);
383 last_dns_status = NT_STATUS_OK;
384 return last_dns_status;
387 /*********************************************************************
388 Simple wrapper for a DNS SRV query
389 *********************************************************************/
391 static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx,
392 const char *name,
393 struct dns_rr_srv **dclist,
394 int *numdcs)
396 uint8 *buffer = NULL;
397 int resp_len = 0;
398 struct dns_rr_srv *dcs = NULL;
399 int query_count, answer_count, auth_count, additional_count;
400 uint8 *p = buffer;
401 int rrnum;
402 int idx = 0;
403 NTSTATUS status;
404 const char *dns_hosts_file;
406 if ( !ctx || !name || !dclist ) {
407 return NT_STATUS_INVALID_PARAMETER;
410 dns_hosts_file = lp_parm_const_string(-1, "resolv", "host file", NULL);
411 if (dns_hosts_file) {
412 return resolve_dns_hosts_file_as_dns_rr(dns_hosts_file,
413 name, true, ctx,
414 dclist, numdcs);
417 /* Send the request. May have to loop several times in case
418 of large replies */
420 status = dns_send_req( ctx, name, T_SRV, &buffer, &resp_len );
421 if ( !NT_STATUS_IS_OK(status) ) {
422 DEBUG(3,("ads_dns_lookup_srv: Failed to send DNS query (%s)\n",
423 nt_errstr(status)));
424 return status;
426 p = buffer;
428 /* For some insane reason, the ns_initparse() et. al. routines are only
429 available in libresolv.a, and not the shared lib. Who knows why....
430 So we have to parse the DNS reply ourselves */
432 /* Pull the answer RR's count from the header.
433 * Use the NMB ordering macros */
435 query_count = RSVAL( p, 4 );
436 answer_count = RSVAL( p, 6 );
437 auth_count = RSVAL( p, 8 );
438 additional_count = RSVAL( p, 10 );
440 DEBUG(4,("ads_dns_lookup_srv: "
441 "%d records returned in the answer section.\n",
442 answer_count));
444 if (answer_count) {
445 if ((dcs = talloc_zero_array(ctx, struct dns_rr_srv,
446 answer_count)) == NULL ) {
447 DEBUG(0,("ads_dns_lookup_srv: "
448 "talloc() failure for %d char*'s\n",
449 answer_count));
450 return NT_STATUS_NO_MEMORY;
452 } else {
453 dcs = NULL;
456 /* now skip the header */
458 p += NS_HFIXEDSZ;
460 /* parse the query section */
462 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
463 struct dns_query q;
465 if (!ads_dns_parse_query(ctx, buffer,
466 buffer+resp_len, &p, &q)) {
467 DEBUG(1,("ads_dns_lookup_srv: "
468 "Failed to parse query record [%d]!\n", rrnum));
469 return NT_STATUS_UNSUCCESSFUL;
473 /* now we are at the answer section */
475 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
476 if (!ads_dns_parse_rr_srv(ctx, buffer, buffer+resp_len,
477 &p, &dcs[rrnum])) {
478 DEBUG(1,("ads_dns_lookup_srv: "
479 "Failed to parse answer recordi [%d]!\n", rrnum));
480 return NT_STATUS_UNSUCCESSFUL;
483 idx = rrnum;
485 /* Parse the authority section */
486 /* just skip these for now */
488 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
489 struct dns_rr rr;
491 if (!ads_dns_parse_rr( ctx, buffer,
492 buffer+resp_len, &p, &rr)) {
493 DEBUG(1,("ads_dns_lookup_srv: "
494 "Failed to parse authority record! [%d]\n", rrnum));
495 return NT_STATUS_UNSUCCESSFUL;
499 /* Parse the additional records section */
501 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
502 struct dns_rr rr;
503 int i;
505 if (!ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
506 &p, &rr)) {
507 DEBUG(1,("ads_dns_lookup_srv: Failed "
508 "to parse additional records section! [%d]\n", rrnum));
509 return NT_STATUS_UNSUCCESSFUL;
512 /* Only interested in A or AAAA records as a shortcut for having
513 * to come back later and lookup the name. For multi-homed
514 * hosts, the number of additional records and exceed the
515 * number of answer records. */
517 if (rr.type != T_A || rr.rdatalen != 4) {
518 #if defined(HAVE_IPV6)
519 /* RFC2874 defines A6 records. This
520 * requires recusive and horribly complex lookups.
521 * Bastards. Ignore this for now.... JRA.
522 * Luckily RFC3363 reprecates A6 records.
524 if (rr.type != T_AAAA || rr.rdatalen != 16)
525 #endif
526 continue;
529 for ( i=0; i<idx; i++ ) {
530 if ( strcmp( rr.hostname, dcs[i].hostname ) == 0 ) {
531 int num_ips = dcs[i].num_ips;
532 struct sockaddr_storage *tmp_ss_s;
534 /* allocate new memory */
536 if (dcs[i].num_ips == 0) {
537 if ((dcs[i].ss_s = talloc_array(dcs,
538 struct sockaddr_storage, 1 ))
539 == NULL ) {
540 return NT_STATUS_NO_MEMORY;
542 } else {
543 if ((tmp_ss_s = talloc_realloc(dcs,
544 dcs[i].ss_s,
545 struct sockaddr_storage,
546 dcs[i].num_ips+1))
547 == NULL ) {
548 return NT_STATUS_NO_MEMORY;
551 dcs[i].ss_s = tmp_ss_s;
553 dcs[i].num_ips++;
555 /* copy the new IP address */
556 if (rr.type == T_A) {
557 struct in_addr ip;
558 memcpy(&ip, rr.rdata, 4);
559 in_addr_to_sockaddr_storage(
560 &dcs[i].ss_s[num_ips],
561 ip);
563 #if defined(HAVE_IPV6)
564 if (rr.type == T_AAAA) {
565 struct in6_addr ip6;
566 memcpy(&ip6, rr.rdata, rr.rdatalen);
567 in6_addr_to_sockaddr_storage(
568 &dcs[i].ss_s[num_ips],
569 ip6);
571 #endif
576 TYPESAFE_QSORT(dcs, idx, dnssrvcmp );
578 *dclist = dcs;
579 *numdcs = idx;
581 return NT_STATUS_OK;
584 /*********************************************************************
585 Simple wrapper for a DNS NS query
586 *********************************************************************/
588 NTSTATUS ads_dns_lookup_ns(TALLOC_CTX *ctx,
589 const char *dnsdomain,
590 struct dns_rr_ns **nslist,
591 int *numns)
593 uint8 *buffer = NULL;
594 int resp_len = 0;
595 struct dns_rr_ns *nsarray = NULL;
596 int query_count, answer_count, auth_count, additional_count;
597 uint8 *p;
598 int rrnum;
599 int idx = 0;
600 NTSTATUS status;
601 const char *dns_hosts_file;
603 if ( !ctx || !dnsdomain || !nslist ) {
604 return NT_STATUS_INVALID_PARAMETER;
607 dns_hosts_file = lp_parm_const_string(-1, "resolv", "host file", NULL);
608 if (dns_hosts_file) {
609 DEBUG(1, ("NO 'NS' lookup available when using resolv:host file"));
610 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
613 /* Send the request. May have to loop several times in case
614 of large replies */
616 status = dns_send_req( ctx, dnsdomain, T_NS, &buffer, &resp_len );
617 if ( !NT_STATUS_IS_OK(status) ) {
618 DEBUG(3,("ads_dns_lookup_ns: Failed to send DNS query (%s)\n",
619 nt_errstr(status)));
620 return status;
622 p = buffer;
624 /* For some insane reason, the ns_initparse() et. al. routines are only
625 available in libresolv.a, and not the shared lib. Who knows why....
626 So we have to parse the DNS reply ourselves */
628 /* Pull the answer RR's count from the header.
629 * Use the NMB ordering macros */
631 query_count = RSVAL( p, 4 );
632 answer_count = RSVAL( p, 6 );
633 auth_count = RSVAL( p, 8 );
634 additional_count = RSVAL( p, 10 );
636 DEBUG(4,("ads_dns_lookup_ns: "
637 "%d records returned in the answer section.\n",
638 answer_count));
640 if (answer_count) {
641 if ((nsarray = talloc_array(ctx, struct dns_rr_ns,
642 answer_count)) == NULL ) {
643 DEBUG(0,("ads_dns_lookup_ns: "
644 "talloc() failure for %d char*'s\n",
645 answer_count));
646 return NT_STATUS_NO_MEMORY;
648 } else {
649 nsarray = NULL;
652 /* now skip the header */
654 p += NS_HFIXEDSZ;
656 /* parse the query section */
658 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
659 struct dns_query q;
661 if (!ads_dns_parse_query(ctx, buffer, buffer+resp_len,
662 &p, &q)) {
663 DEBUG(1,("ads_dns_lookup_ns: "
664 " Failed to parse query record!\n"));
665 return NT_STATUS_UNSUCCESSFUL;
669 /* now we are at the answer section */
671 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
672 if (!ads_dns_parse_rr_ns(ctx, buffer, buffer+resp_len,
673 &p, &nsarray[rrnum])) {
674 DEBUG(1,("ads_dns_lookup_ns: "
675 "Failed to parse answer record!\n"));
676 return NT_STATUS_UNSUCCESSFUL;
679 idx = rrnum;
681 /* Parse the authority section */
682 /* just skip these for now */
684 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
685 struct dns_rr rr;
687 if ( !ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
688 &p, &rr)) {
689 DEBUG(1,("ads_dns_lookup_ns: "
690 "Failed to parse authority record!\n"));
691 return NT_STATUS_UNSUCCESSFUL;
695 /* Parse the additional records section */
697 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
698 struct dns_rr rr;
699 int i;
701 if (!ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
702 &p, &rr)) {
703 DEBUG(1,("ads_dns_lookup_ns: Failed "
704 "to parse additional records section!\n"));
705 return NT_STATUS_UNSUCCESSFUL;
708 /* only interested in A records as a shortcut for having to come
709 back later and lookup the name */
711 if (rr.type != T_A || rr.rdatalen != 4) {
712 #if defined(HAVE_IPV6)
713 if (rr.type != T_AAAA || rr.rdatalen != 16)
714 #endif
715 continue;
718 for ( i=0; i<idx; i++ ) {
719 if (strcmp(rr.hostname, nsarray[i].hostname) == 0) {
720 if (rr.type == T_A) {
721 struct in_addr ip;
722 memcpy(&ip, rr.rdata, 4);
723 in_addr_to_sockaddr_storage(
724 &nsarray[i].ss,
725 ip);
727 #if defined(HAVE_IPV6)
728 if (rr.type == T_AAAA) {
729 struct in6_addr ip6;
730 memcpy(&ip6, rr.rdata, rr.rdatalen);
731 in6_addr_to_sockaddr_storage(
732 &nsarray[i].ss,
733 ip6);
735 #endif
740 *nslist = nsarray;
741 *numns = idx;
743 return NT_STATUS_OK;
746 /********************************************************************
747 Query with optional sitename.
748 ********************************************************************/
750 static NTSTATUS ads_dns_query_internal(TALLOC_CTX *ctx,
751 const char *servicename,
752 const char *dc_pdc_gc_domains,
753 const char *realm,
754 const char *sitename,
755 struct dns_rr_srv **dclist,
756 int *numdcs )
758 char *name;
759 if (sitename) {
760 name = talloc_asprintf(ctx, "%s._tcp.%s._sites.%s._msdcs.%s",
761 servicename, sitename,
762 dc_pdc_gc_domains, realm);
763 } else {
764 name = talloc_asprintf(ctx, "%s._tcp.%s._msdcs.%s",
765 servicename, dc_pdc_gc_domains, realm);
767 if (!name) {
768 return NT_STATUS_NO_MEMORY;
770 return ads_dns_lookup_srv( ctx, name, dclist, numdcs );
773 /********************************************************************
774 Query for AD DC's.
775 ********************************************************************/
777 NTSTATUS ads_dns_query_dcs(TALLOC_CTX *ctx,
778 const char *realm,
779 const char *sitename,
780 struct dns_rr_srv **dclist,
781 int *numdcs )
783 NTSTATUS status;
785 status = ads_dns_query_internal(ctx, "_ldap", "dc", realm, sitename,
786 dclist, numdcs);
788 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
789 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
790 return status;
793 if (sitename &&
794 ((!NT_STATUS_IS_OK(status)) ||
795 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
796 /* Sitename DNS query may have failed. Try without. */
797 status = ads_dns_query_internal(ctx, "_ldap", "dc", realm,
798 NULL, dclist, numdcs);
800 return status;
803 /********************************************************************
804 Query for AD GC's.
805 ********************************************************************/
807 NTSTATUS ads_dns_query_gcs(TALLOC_CTX *ctx,
808 const char *realm,
809 const char *sitename,
810 struct dns_rr_srv **dclist,
811 int *numdcs )
813 NTSTATUS status;
815 status = ads_dns_query_internal(ctx, "_ldap", "gc", realm, sitename,
816 dclist, numdcs);
818 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
819 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
820 return status;
823 if (sitename &&
824 ((!NT_STATUS_IS_OK(status)) ||
825 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
826 /* Sitename DNS query may have failed. Try without. */
827 status = ads_dns_query_internal(ctx, "_ldap", "gc", realm,
828 NULL, dclist, numdcs);
830 return status;
833 /********************************************************************
834 Query for AD KDC's.
835 Even if our underlying kerberos libraries are UDP only, this
836 is pretty safe as it's unlikely that a KDC supports TCP and not UDP.
837 ********************************************************************/
839 NTSTATUS ads_dns_query_kdcs(TALLOC_CTX *ctx,
840 const char *dns_forest_name,
841 const char *sitename,
842 struct dns_rr_srv **dclist,
843 int *numdcs )
845 NTSTATUS status;
847 status = ads_dns_query_internal(ctx, "_kerberos", "dc",
848 dns_forest_name, sitename, dclist,
849 numdcs);
851 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
852 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
853 return status;
856 if (sitename &&
857 ((!NT_STATUS_IS_OK(status)) ||
858 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
859 /* Sitename DNS query may have failed. Try without. */
860 status = ads_dns_query_internal(ctx, "_kerberos", "dc",
861 dns_forest_name, NULL,
862 dclist, numdcs);
864 return status;
867 /********************************************************************
868 Query for AD PDC. Sitename is obsolete here.
869 ********************************************************************/
871 NTSTATUS ads_dns_query_pdc(TALLOC_CTX *ctx,
872 const char *dns_domain_name,
873 struct dns_rr_srv **dclist,
874 int *numdcs )
876 return ads_dns_query_internal(ctx, "_ldap", "pdc", dns_domain_name,
877 NULL, dclist, numdcs);
880 /********************************************************************
881 Query for AD DC by guid. Sitename is obsolete here.
882 ********************************************************************/
884 NTSTATUS ads_dns_query_dcs_guid(TALLOC_CTX *ctx,
885 const char *dns_forest_name,
886 const struct GUID *domain_guid,
887 struct dns_rr_srv **dclist,
888 int *numdcs )
890 /*_ldap._tcp.DomainGuid.domains._msdcs.DnsForestName */
892 const char *domains;
893 char *guid_string;
895 guid_string = GUID_string(ctx, domain_guid);
896 if (!guid_string) {
897 return NT_STATUS_NO_MEMORY;
900 /* little hack */
901 domains = talloc_asprintf(ctx, "%s.domains", guid_string);
902 if (!domains) {
903 return NT_STATUS_NO_MEMORY;
905 TALLOC_FREE(guid_string);
907 return ads_dns_query_internal(ctx, "_ldap", domains, dns_forest_name,
908 NULL, dclist, numdcs);