docs: fix type for server schannel parameter
[Samba.git] / lib / addns / dnsquery.c
blob57ef8d92f4ac7b9dff4b170c860bc88d50cbc00b
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 "lib/util/util_net.h"
23 #include "lib/util/tsort.h"
24 #include "dnsquery.h"
26 /* AIX resolv.h uses 'class' in struct ns_rr */
28 #if defined(AIX)
29 # if defined(class)
30 # undef class
31 # endif
32 #endif /* AIX */
34 /* resolver headers */
36 #include <sys/types.h>
37 #include <netinet/in.h>
38 #include <arpa/nameser.h>
39 #include <resolv.h>
40 #include <netdb.h>
42 #define MAX_DNS_PACKET_SIZE 0xffff
44 #ifdef NS_HFIXEDSZ /* Bind 8/9 interface */
45 #if !defined(C_IN) /* AIX 5.3 already defines C_IN */
46 # define C_IN ns_c_in
47 #endif
48 #if !defined(T_A) /* AIX 5.3 already defines T_A */
49 # define T_A ns_t_a
50 #endif
52 #if defined(HAVE_IPV6)
53 #if !defined(T_AAAA)
54 # define T_AAAA ns_t_aaaa
55 #endif
56 #endif
58 # define T_SRV ns_t_srv
59 #if !defined(T_NS) /* AIX 5.3 already defines T_NS */
60 # define T_NS ns_t_ns
61 #endif
62 #else
63 # ifdef HFIXEDSZ
64 # define NS_HFIXEDSZ HFIXEDSZ
65 # else
66 # define NS_HFIXEDSZ sizeof(HEADER)
67 # endif /* HFIXEDSZ */
68 # ifdef PACKETSZ
69 # define NS_PACKETSZ PACKETSZ
70 # else /* 512 is usually the default */
71 # define NS_PACKETSZ 512
72 # endif /* PACKETSZ */
73 # define T_SRV 33
74 #endif
76 /*********************************************************************
77 *********************************************************************/
79 static bool ads_dns_parse_query( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
80 uint8_t **ptr, struct dns_query *q )
82 uint8_t *p = *ptr;
83 char hostname[MAX_DNS_NAME_LENGTH];
84 int namelen;
86 ZERO_STRUCTP( q );
88 if ( !start || !end || !q || !*ptr)
89 return false;
91 /* See RFC 1035 for details. If this fails, then return. */
93 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
94 if ( namelen < 0 ) {
95 return false;
97 p += namelen;
98 q->hostname = talloc_strdup( ctx, hostname );
100 /* check that we have space remaining */
102 if ( PTR_DIFF(p+4, end) > 0 )
103 return false;
105 q->type = RSVAL( p, 0 );
106 q->in_class = RSVAL( p, 2 );
107 p += 4;
109 *ptr = p;
111 return true;
114 /*********************************************************************
115 *********************************************************************/
117 static bool ads_dns_parse_rr( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
118 uint8_t **ptr, struct dns_rr *rr )
120 uint8_t *p = *ptr;
121 char hostname[MAX_DNS_NAME_LENGTH];
122 int namelen;
124 if ( !start || !end || !rr || !*ptr)
125 return -1;
127 ZERO_STRUCTP( rr );
128 /* pull the name from the answer */
130 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
131 if ( namelen < 0 ) {
132 return -1;
134 p += namelen;
135 rr->hostname = talloc_strdup( ctx, hostname );
137 /* check that we have space remaining */
139 if ( PTR_DIFF(p+10, end) > 0 )
140 return false;
142 /* pull some values and then skip onto the string */
144 rr->type = RSVAL(p, 0);
145 rr->in_class = RSVAL(p, 2);
146 rr->ttl = RIVAL(p, 4);
147 rr->rdatalen = RSVAL(p, 8);
149 p += 10;
151 /* sanity check the available space */
153 if ( PTR_DIFF(p+rr->rdatalen, end ) > 0 ) {
154 return false;
158 /* save a point to the rdata for this section */
160 rr->rdata = p;
161 p += rr->rdatalen;
163 *ptr = p;
165 return true;
168 /*********************************************************************
169 *********************************************************************/
171 static bool ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
172 uint8_t **ptr, struct dns_rr_srv *srv )
174 struct dns_rr rr;
175 uint8_t *p;
176 char dcname[MAX_DNS_NAME_LENGTH];
177 int namelen;
179 if ( !start || !end || !srv || !*ptr)
180 return -1;
182 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
183 of the next record */
185 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
186 DEBUG(1,("ads_dns_parse_rr_srv: Failed to parse RR record\n"));
187 return false;
190 if ( rr.type != T_SRV ) {
191 DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n",
192 rr.type));
193 return false;
196 p = rr.rdata;
198 srv->priority = RSVAL(p, 0);
199 srv->weight = RSVAL(p, 2);
200 srv->port = RSVAL(p, 4);
202 p += 6;
204 namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
205 if ( namelen < 0 ) {
206 DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
207 return false;
210 srv->hostname = talloc_strdup( ctx, dcname );
212 DEBUG(10,("ads_dns_parse_rr_srv: Parsed %s [%u, %u, %u]\n",
213 srv->hostname,
214 srv->priority,
215 srv->weight,
216 srv->port));
218 return true;
221 /*********************************************************************
222 *********************************************************************/
224 static bool ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8_t *start, uint8_t *end,
225 uint8_t **ptr, struct dns_rr_ns *nsrec )
227 struct dns_rr rr;
228 uint8_t *p;
229 char nsname[MAX_DNS_NAME_LENGTH];
230 int namelen;
232 if ( !start || !end || !nsrec || !*ptr)
233 return -1;
235 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
236 of the next record */
238 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
239 DEBUG(1,("ads_dns_parse_rr_ns: Failed to parse RR record\n"));
240 return false;
243 if ( rr.type != T_NS ) {
244 DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n",
245 rr.type));
246 return false;
249 p = rr.rdata;
251 /* ame server hostname */
253 namelen = dn_expand( start, end, p, nsname, sizeof(nsname) );
254 if ( namelen < 0 ) {
255 DEBUG(1,("ads_dns_parse_rr_ns: Failed to uncompress name!\n"));
256 return false;
258 nsrec->hostname = talloc_strdup( ctx, nsname );
260 return true;
263 /*********************************************************************
264 Sort SRV record list based on weight and priority. See RFC 2782.
265 *********************************************************************/
267 static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b )
269 if ( a->priority == b->priority ) {
271 /* randomize entries with an equal weight and priority */
272 if ( a->weight == b->weight )
273 return 0;
275 /* higher weights should be sorted lower */
276 if ( a->weight > b->weight )
277 return -1;
278 else
279 return 1;
282 if ( a->priority < b->priority )
283 return -1;
285 return 1;
288 /*********************************************************************
289 Simple wrapper for a DNS query
290 *********************************************************************/
292 #define DNS_FAILED_WAITTIME 30
294 static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
295 uint8_t **buf, int *resp_length )
297 uint8_t *buffer = NULL;
298 size_t buf_len = 0;
299 int resp_len = NS_PACKETSZ;
300 static time_t last_dns_check = 0;
301 static NTSTATUS last_dns_status = NT_STATUS_OK;
302 time_t now = time_mono(NULL);
304 /* Try to prevent bursts of DNS lookups if the server is down */
306 /* Protect against large clock changes */
308 if ( last_dns_check > now )
309 last_dns_check = 0;
311 /* IF we had a DNS timeout or a bad server and we are still
312 in the 30 second cache window, just return the previous
313 status and save the network timeout. */
315 if ( (NT_STATUS_EQUAL(last_dns_status,NT_STATUS_IO_TIMEOUT) ||
316 NT_STATUS_EQUAL(last_dns_status,NT_STATUS_CONNECTION_REFUSED)) &&
317 (last_dns_check+DNS_FAILED_WAITTIME) > now )
319 DEBUG(10,("dns_send_req: last dns check returning cached status (%s)\n",
320 nt_errstr(last_dns_status) ));
321 return last_dns_status;
324 /* Send the Query */
325 do {
326 if ( buffer )
327 TALLOC_FREE( buffer );
329 buf_len = resp_len * sizeof(uint8_t);
331 if (buf_len) {
332 if ((buffer = talloc_array(ctx, uint8_t, buf_len))
333 == NULL ) {
334 DEBUG(0,("dns_send_req: "
335 "talloc() failed!\n"));
336 last_dns_status = NT_STATUS_NO_MEMORY;
337 last_dns_check = time_mono(NULL);
338 return last_dns_status;
342 if ((resp_len = res_query(name, C_IN, q_type, buffer, buf_len))
343 < 0 ) {
344 DEBUG(3,("dns_send_req: "
345 "Failed to resolve %s (%s)\n",
346 name, strerror(errno)));
347 TALLOC_FREE( buffer );
348 last_dns_status = NT_STATUS_UNSUCCESSFUL;
350 if (errno == ETIMEDOUT) {
351 last_dns_status = NT_STATUS_IO_TIMEOUT;
353 if (errno == ECONNREFUSED) {
354 last_dns_status = NT_STATUS_CONNECTION_REFUSED;
356 last_dns_check = time_mono(NULL);
357 return last_dns_status;
360 /* On AIX, Solaris, and possibly some older glibc systems (e.g. SLES8)
361 truncated replies never give back a resp_len > buflen
362 which ends up causing DNS resolve failures on large tcp DNS replies */
364 if (buf_len == resp_len) {
365 if (resp_len == MAX_DNS_PACKET_SIZE) {
366 DEBUG(1,("dns_send_req: DNS reply too large when resolving %s\n",
367 name));
368 TALLOC_FREE( buffer );
369 last_dns_status = NT_STATUS_BUFFER_TOO_SMALL;
370 last_dns_check = time_mono(NULL);
371 return last_dns_status;
374 resp_len = MIN(resp_len*2, MAX_DNS_PACKET_SIZE);
378 } while ( buf_len < resp_len && resp_len <= MAX_DNS_PACKET_SIZE );
380 *buf = buffer;
381 *resp_length = resp_len;
383 last_dns_check = time_mono(NULL);
384 last_dns_status = NT_STATUS_OK;
385 return last_dns_status;
388 /*********************************************************************
389 Simple wrapper for a DNS SRV query
390 *********************************************************************/
392 NTSTATUS ads_dns_lookup_srv(TALLOC_CTX *ctx,
393 const char *dns_hosts_file,
394 const char *name,
395 struct dns_rr_srv **dclist,
396 int *numdcs)
398 uint8_t *buffer = NULL;
399 int resp_len = 0;
400 struct dns_rr_srv *dcs = NULL;
401 int query_count, answer_count, auth_count, additional_count;
402 uint8_t *p = buffer;
403 int rrnum;
404 int idx = 0;
405 NTSTATUS status;
407 if ( !ctx || !name || !dclist ) {
408 return NT_STATUS_INVALID_PARAMETER;
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 *dns_hosts_file,
590 const char *dnsdomain,
591 struct dns_rr_ns **nslist,
592 int *numns)
594 uint8_t *buffer = NULL;
595 int resp_len = 0;
596 struct dns_rr_ns *nsarray = NULL;
597 int query_count, answer_count, auth_count, additional_count;
598 uint8_t *p;
599 int rrnum;
600 int idx = 0;
601 NTSTATUS status;
603 if ( !ctx || !dnsdomain || !nslist ) {
604 return NT_STATUS_INVALID_PARAMETER;
607 if (dns_hosts_file) {
608 DEBUG(1, ("NO 'NS' lookup available when using resolv:host file"));
609 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
612 /* Send the request. May have to loop several times in case
613 of large replies */
615 status = dns_send_req( ctx, dnsdomain, T_NS, &buffer, &resp_len );
616 if ( !NT_STATUS_IS_OK(status) ) {
617 DEBUG(3,("ads_dns_lookup_ns: Failed to send DNS query (%s)\n",
618 nt_errstr(status)));
619 return status;
621 p = buffer;
623 /* For some insane reason, the ns_initparse() et. al. routines are only
624 available in libresolv.a, and not the shared lib. Who knows why....
625 So we have to parse the DNS reply ourselves */
627 /* Pull the answer RR's count from the header.
628 * Use the NMB ordering macros */
630 query_count = RSVAL( p, 4 );
631 answer_count = RSVAL( p, 6 );
632 auth_count = RSVAL( p, 8 );
633 additional_count = RSVAL( p, 10 );
635 DEBUG(4,("ads_dns_lookup_ns: "
636 "%d records returned in the answer section.\n",
637 answer_count));
639 if (answer_count) {
640 if ((nsarray = talloc_array(ctx, struct dns_rr_ns,
641 answer_count)) == NULL ) {
642 DEBUG(0,("ads_dns_lookup_ns: "
643 "talloc() failure for %d char*'s\n",
644 answer_count));
645 return NT_STATUS_NO_MEMORY;
647 } else {
648 nsarray = NULL;
651 /* now skip the header */
653 p += NS_HFIXEDSZ;
655 /* parse the query section */
657 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
658 struct dns_query q;
660 if (!ads_dns_parse_query(ctx, buffer, buffer+resp_len,
661 &p, &q)) {
662 DEBUG(1,("ads_dns_lookup_ns: "
663 " Failed to parse query record!\n"));
664 return NT_STATUS_UNSUCCESSFUL;
668 /* now we are at the answer section */
670 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
671 if (!ads_dns_parse_rr_ns(ctx, buffer, buffer+resp_len,
672 &p, &nsarray[rrnum])) {
673 DEBUG(1,("ads_dns_lookup_ns: "
674 "Failed to parse answer record!\n"));
675 return NT_STATUS_UNSUCCESSFUL;
678 idx = rrnum;
680 /* Parse the authority section */
681 /* just skip these for now */
683 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
684 struct dns_rr rr;
686 if ( !ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
687 &p, &rr)) {
688 DEBUG(1,("ads_dns_lookup_ns: "
689 "Failed to parse authority record!\n"));
690 return NT_STATUS_UNSUCCESSFUL;
694 /* Parse the additional records section */
696 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
697 struct dns_rr rr;
698 int i;
700 if (!ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
701 &p, &rr)) {
702 DEBUG(1,("ads_dns_lookup_ns: Failed "
703 "to parse additional records section!\n"));
704 return NT_STATUS_UNSUCCESSFUL;
707 /* only interested in A records as a shortcut for having to come
708 back later and lookup the name */
710 if (rr.type != T_A || rr.rdatalen != 4) {
711 #if defined(HAVE_IPV6)
712 if (rr.type != T_AAAA || rr.rdatalen != 16)
713 #endif
714 continue;
717 for ( i=0; i<idx; i++ ) {
718 if (strcmp(rr.hostname, nsarray[i].hostname) == 0) {
719 if (rr.type == T_A) {
720 struct in_addr ip;
721 memcpy(&ip, rr.rdata, 4);
722 in_addr_to_sockaddr_storage(
723 &nsarray[i].ss,
724 ip);
726 #if defined(HAVE_IPV6)
727 if (rr.type == T_AAAA) {
728 struct in6_addr ip6;
729 memcpy(&ip6, rr.rdata, rr.rdatalen);
730 in6_addr_to_sockaddr_storage(
731 &nsarray[i].ss,
732 ip6);
734 #endif
739 *nslist = nsarray;
740 *numns = idx;
742 return NT_STATUS_OK;
745 /********************************************************************
746 Query with optional sitename.
747 ********************************************************************/
749 static NTSTATUS ads_dns_query_internal(TALLOC_CTX *ctx,
750 const char *dns_hosts_file,
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 && strlen(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, dns_hosts_file, name, dclist, numdcs);
773 /********************************************************************
774 Query for AD DC's.
775 ********************************************************************/
777 NTSTATUS ads_dns_query_dcs(TALLOC_CTX *ctx,
778 const char *dns_hosts_file,
779 const char *realm,
780 const char *sitename,
781 struct dns_rr_srv **dclist,
782 int *numdcs )
784 NTSTATUS status;
786 status = ads_dns_query_internal(ctx, dns_hosts_file, "_ldap", "dc",
787 realm, sitename, dclist, numdcs);
789 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
790 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
791 return status;
794 if (sitename &&
795 ((!NT_STATUS_IS_OK(status)) ||
796 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
797 /* Sitename DNS query may have failed. Try without. */
798 status = ads_dns_query_internal(ctx, dns_hosts_file,
799 "_ldap", "dc", realm,
800 NULL, dclist, numdcs);
802 return status;
805 /********************************************************************
806 Query for AD GC's.
807 ********************************************************************/
809 NTSTATUS ads_dns_query_gcs(TALLOC_CTX *ctx,
810 const char *dns_hosts_file,
811 const char *realm,
812 const char *sitename,
813 struct dns_rr_srv **dclist,
814 int *numdcs )
816 NTSTATUS status;
818 status = ads_dns_query_internal(ctx, dns_hosts_file, "_ldap", "gc",
819 realm, sitename, dclist, numdcs);
821 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
822 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
823 return status;
826 if (sitename &&
827 ((!NT_STATUS_IS_OK(status)) ||
828 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
829 /* Sitename DNS query may have failed. Try without. */
830 status = ads_dns_query_internal(ctx, dns_hosts_file,
831 "_ldap", "gc", realm,
832 NULL, dclist, numdcs);
834 return status;
837 /********************************************************************
838 Query for AD KDC's.
839 Even if our underlying kerberos libraries are UDP only, this
840 is pretty safe as it's unlikely that a KDC supports TCP and not UDP.
841 ********************************************************************/
843 NTSTATUS ads_dns_query_kdcs(TALLOC_CTX *ctx,
844 const char *dns_hosts_file,
845 const char *dns_forest_name,
846 const char *sitename,
847 struct dns_rr_srv **dclist,
848 int *numdcs )
850 NTSTATUS status;
852 status = ads_dns_query_internal(ctx, dns_hosts_file, "_kerberos", "dc",
853 dns_forest_name, sitename, dclist,
854 numdcs);
856 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
857 NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
858 return status;
861 if (sitename &&
862 ((!NT_STATUS_IS_OK(status)) ||
863 (NT_STATUS_IS_OK(status) && (numdcs == 0)))) {
864 /* Sitename DNS query may have failed. Try without. */
865 status = ads_dns_query_internal(ctx, dns_hosts_file,
866 "_kerberos", "dc",
867 dns_forest_name, NULL,
868 dclist, numdcs);
870 return status;
873 /********************************************************************
874 Query for AD PDC. Sitename is obsolete here.
875 ********************************************************************/
877 NTSTATUS ads_dns_query_pdc(TALLOC_CTX *ctx,
878 const char *dns_hosts_file,
879 const char *dns_domain_name,
880 struct dns_rr_srv **dclist,
881 int *numdcs )
883 return ads_dns_query_internal(ctx, dns_hosts_file, "_ldap", "pdc",
884 dns_domain_name, NULL, dclist, numdcs);
887 /********************************************************************
888 Query for AD DC by guid. Sitename is obsolete here.
889 ********************************************************************/
891 NTSTATUS ads_dns_query_dcs_guid(TALLOC_CTX *ctx,
892 const char *dns_hosts_file,
893 const char *dns_forest_name,
894 const char *domain_guid,
895 struct dns_rr_srv **dclist,
896 int *numdcs )
898 /*_ldap._tcp.DomainGuid.domains._msdcs.DnsForestName */
900 const char *domains;
902 /* little hack */
903 domains = talloc_asprintf(ctx, "%s.domains", domain_guid);
904 if (!domains) {
905 return NT_STATUS_NO_MEMORY;
908 return ads_dns_query_internal(ctx, dns_hosts_file, "_ldap", domains,
909 dns_forest_name, NULL, dclist, numdcs);