r20862: When in disconnected mode there is no need to try a fallback to a site
[nivanova/samba-autobuild/.git] / source3 / libads / dns.c
1 /* 
2    Unix SMB/CIFS implementation.
3    DNS utility library
4    Copyright (C) Gerald (Jerry) Carter           2006.
5
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23 /* AIX resolv.h uses 'class' in struct ns_rr */
24
25 #if defined(AIX)
26 #  if defined(class)
27 #    undef class
28 #  endif
29 #endif  /* AIX */
30
31 /* resolver headers */
32
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
36 #include <resolv.h>
37 #include <netdb.h>
38
39 #define MAX_DNS_PACKET_SIZE 0xffff
40
41 #ifdef NS_HFIXEDSZ      /* Bind 8/9 interface */
42 #if !defined(C_IN)      /* AIX 5.3 already defines C_IN */
43 #  define C_IN          ns_c_in
44 #endif
45 #if !defined(T_A)       /* AIX 5.3 already defines T_A */
46 #  define T_A           ns_t_a
47 #endif
48 #  define T_SRV         ns_t_srv
49 #if !defined(T_NS)      /* AIX 5.3 already defines T_NS */
50 #  define T_NS          ns_t_ns
51 #endif
52 #else
53 #  ifdef HFIXEDSZ
54 #    define NS_HFIXEDSZ HFIXEDSZ
55 #  else
56 #    define NS_HFIXEDSZ sizeof(HEADER)
57 #  endif        /* HFIXEDSZ */
58 #  ifdef PACKETSZ
59 #    define NS_PACKETSZ PACKETSZ
60 #  else /* 512 is usually the default */
61 #    define NS_PACKETSZ 512
62 #  endif        /* PACKETSZ */
63 #  define T_SRV         33
64 #endif
65
66 /*********************************************************************
67 *********************************************************************/
68
69 static BOOL ads_dns_parse_query( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
70                           uint8 **ptr, struct dns_query *q )
71 {
72         uint8 *p = *ptr;
73         pstring hostname;
74         int namelen;
75
76         ZERO_STRUCTP( q );
77         
78         if ( !start || !end || !q || !*ptr)
79                 return False;
80
81         /* See RFC 1035 for details. If this fails, then return. */
82
83         namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
84         if ( namelen < 0 ) {
85                 return False;
86         }
87         p += namelen;
88         q->hostname = talloc_strdup( ctx, hostname );
89
90         /* check that we have space remaining */
91
92         if ( PTR_DIFF(p+4, end) > 0 )
93                 return False;
94
95         q->type     = RSVAL( p, 0 );
96         q->in_class = RSVAL( p, 2 );
97         p += 4;
98
99         *ptr = p;
100
101         return True;
102 }
103
104 /*********************************************************************
105 *********************************************************************/
106
107 static BOOL ads_dns_parse_rr( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
108                        uint8 **ptr, struct dns_rr *rr )
109 {
110         uint8 *p = *ptr;
111         pstring hostname;
112         int namelen;
113
114         if ( !start || !end || !rr || !*ptr)
115                 return -1;
116
117         ZERO_STRUCTP( rr );
118         /* pull the name from the answer */
119
120         namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
121         if ( namelen < 0 ) {
122                 return -1;
123         }
124         p += namelen;
125         rr->hostname = talloc_strdup( ctx, hostname );
126
127         /* check that we have space remaining */
128
129         if ( PTR_DIFF(p+10, end) > 0 )
130                 return False;
131
132         /* pull some values and then skip onto the string */
133
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);
138         
139         p += 10;
140
141         /* sanity check the available space */
142
143         if ( PTR_DIFF(p+rr->rdatalen, end ) > 0 ) {
144                 return False;
145
146         }
147
148         /* save a point to the rdata for this section */
149
150         rr->rdata = p;
151         p += rr->rdatalen;
152
153         *ptr = p;
154
155         return True;
156 }
157
158 /*********************************************************************
159 *********************************************************************/
160
161 static BOOL ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
162                        uint8 **ptr, struct dns_rr_srv *srv )
163 {
164         struct dns_rr rr;
165         uint8 *p;
166         pstring dcname;
167         int namelen;
168
169         if ( !start || !end || !srv || !*ptr)
170                 return -1;
171
172         /* Parse the RR entry.  Coming out of the this, ptr is at the beginning 
173            of the next record */
174
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"));
177                 return False;
178         }
179
180         if ( rr.type != T_SRV ) {
181                 DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n", rr.type));
182                 return False;
183         }
184
185         p = rr.rdata;
186
187         srv->priority = RSVAL(p, 0);
188         srv->weight   = RSVAL(p, 2);
189         srv->port     = RSVAL(p, 4);
190
191         p += 6;
192
193         namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
194         if ( namelen < 0 ) {
195                 DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
196                 return False;
197         }
198         srv->hostname = talloc_strdup( ctx, dcname );
199
200         return True;
201 }
202
203 /*********************************************************************
204 *********************************************************************/
205
206 static BOOL ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
207                        uint8 **ptr, struct dns_rr_ns *nsrec )
208 {
209         struct dns_rr rr;
210         uint8 *p;
211         pstring nsname;
212         int namelen;
213
214         if ( !start || !end || !nsrec || !*ptr)
215                 return -1;
216
217         /* Parse the RR entry.  Coming out of the this, ptr is at the beginning 
218            of the next record */
219
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"));
222                 return False;
223         }
224
225         if ( rr.type != T_NS ) {
226                 DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n", rr.type));
227                 return False;
228         }
229
230         p = rr.rdata;
231
232         /* ame server hostname */
233         
234         namelen = dn_expand( start, end, p, nsname, sizeof(nsname) );
235         if ( namelen < 0 ) {
236                 DEBUG(1,("ads_dns_parse_rr_ns: Failed to uncompress name!\n"));
237                 return False;
238         }
239         nsrec->hostname = talloc_strdup( ctx, nsname );
240
241         return True;
242 }
243
244 /*********************************************************************
245  Sort SRV record list based on weight and priority.  See RFC 2782.
246 *********************************************************************/
247
248 static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b )
249 {
250         if ( a->priority == b->priority ) {
251
252                 /* randomize entries with an equal weight and priority */
253                 if ( a->weight == b->weight ) 
254                         return 0;
255
256                 /* higher weights should be sorted lower */ 
257                 if ( a->weight > b->weight )
258                         return -1;
259                 else
260                         return 1;
261         }
262                 
263         if ( a->priority < b->priority )
264                 return -1;
265
266         return 1;
267 }
268
269 /*********************************************************************
270  Simple wrapper for a DNS query
271 *********************************************************************/
272
273 static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type, 
274                               uint8 **buf, int *resp_length )
275 {
276         uint8 *buffer = NULL;
277         size_t buf_len;
278         int resp_len = NS_PACKETSZ;     
279         
280         do {
281                 if ( buffer )
282                         TALLOC_FREE( buffer );
283                 
284                 buf_len = resp_len * sizeof(uint8);
285
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;
289                 }
290
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                         if (errno == ETIMEDOUT) {
295                                 return NT_STATUS_IO_TIMEOUT;
296                         }
297                         if (errno == ECONNREFUSED) {
298                                 return NT_STATUS_CONNECTION_REFUSED;
299                         }
300                         return NT_STATUS_UNSUCCESSFUL;
301                 }
302         } while ( buf_len < resp_len && resp_len < MAX_DNS_PACKET_SIZE );
303         
304         *buf = buffer;
305         *resp_length = resp_len;
306
307         return NT_STATUS_OK;
308 }
309
310 /*********************************************************************
311  Simple wrapper for a DNS SRV query
312 *********************************************************************/
313
314 static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, const char *name, struct dns_rr_srv **dclist, int *numdcs )
315 {
316         uint8 *buffer = NULL;
317         int resp_len = 0;
318         struct dns_rr_srv *dcs = NULL;
319         int query_count, answer_count, auth_count, additional_count;
320         uint8 *p = buffer;
321         int rrnum;
322         int idx = 0;
323         NTSTATUS status;
324
325         if ( !ctx || !name || !dclist ) {
326                 return NT_STATUS_INVALID_PARAMETER;
327         }
328         
329         /* Send the request.  May have to loop several times in case 
330            of large replies */
331
332         status = dns_send_req( ctx, name, T_SRV, &buffer, &resp_len );
333         if ( !NT_STATUS_IS_OK(status) ) {
334                 DEBUG(3,("ads_dns_lookup_srv: Failed to send DNS query (%s)\n",
335                         nt_errstr(status)));
336                 return status;
337         }
338         p = buffer;
339
340         /* For some insane reason, the ns_initparse() et. al. routines are only
341            available in libresolv.a, and not the shared lib.  Who knows why....
342            So we have to parse the DNS reply ourselves */
343
344         /* Pull the answer RR's count from the header.  Use the NMB ordering macros */
345
346         query_count      = RSVAL( p, 4 );
347         answer_count     = RSVAL( p, 6 );
348         auth_count       = RSVAL( p, 8 );
349         additional_count = RSVAL( p, 10 );
350
351         DEBUG(4,("ads_dns_lookup_srv: %d records returned in the answer section.\n", 
352                 answer_count));
353                 
354         if ( (dcs = TALLOC_ZERO_ARRAY(ctx, struct dns_rr_srv, answer_count)) == NULL ) {
355                 DEBUG(0,("ads_dns_lookup_srv: talloc() failure for %d char*'s\n", 
356                         answer_count));
357                 return NT_STATUS_NO_MEMORY;
358         }
359
360         /* now skip the header */
361
362         p += NS_HFIXEDSZ;
363
364         /* parse the query section */
365
366         for ( rrnum=0; rrnum<query_count; rrnum++ ) {
367                 struct dns_query q;
368
369                 if ( !ads_dns_parse_query( ctx, buffer, buffer+resp_len, &p, &q ) ) {
370                         DEBUG(1,("ads_dns_lookup_srv: Failed to parse query record!\n"));
371                         return NT_STATUS_UNSUCCESSFUL;
372                 }
373         }
374
375         /* now we are at the answer section */
376
377         for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
378                 if ( !ads_dns_parse_rr_srv( ctx, buffer, buffer+resp_len, &p, &dcs[rrnum] ) ) {
379                         DEBUG(1,("ads_dns_lookup_srv: Failed to parse answer record!\n"));
380                         return NT_STATUS_UNSUCCESSFUL;
381                 }               
382         }
383         idx = rrnum;
384
385         /* Parse the authority section */
386         /* just skip these for now */
387
388         for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
389                 struct dns_rr rr;
390
391                 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
392                         DEBUG(1,("ads_dns_lookup_srv: Failed to parse authority record!\n"));
393                         return NT_STATUS_UNSUCCESSFUL;
394                 }
395         }
396
397         /* Parse the additional records section */
398
399         for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
400                 struct dns_rr rr;
401                 int i;
402
403                 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
404                         DEBUG(1,("ads_dns_lookup_srv: Failed to parse additional records section!\n"));
405                         return NT_STATUS_UNSUCCESSFUL;
406                 }
407
408                 /* only interested in A records as a shortcut for having to come 
409                    back later and lookup the name.  For multi-homed hosts, the 
410                    number of additional records and exceed the number of answer 
411                    records. */
412                   
413
414                 if ( (rr.type != T_A) || (rr.rdatalen != 4) ) 
415                         continue;
416
417                 for ( i=0; i<idx; i++ ) {
418                         if ( strcmp( rr.hostname, dcs[i].hostname ) == 0 ) {
419                                 int num_ips = dcs[i].num_ips;
420                                 uint8 *buf;
421                                 struct in_addr *tmp_ips;
422
423                                 /* allocate new memory */
424                                 
425                                 if ( dcs[i].num_ips == 0 ) {
426                                         if ( (dcs[i].ips = TALLOC_ARRAY( dcs, 
427                                                 struct in_addr, 1 )) == NULL ) 
428                                         {
429                                                 return NT_STATUS_NO_MEMORY;
430                                         }
431                                 } else {
432                                         if ( (tmp_ips = TALLOC_REALLOC_ARRAY( dcs, dcs[i].ips,
433                                                 struct in_addr, dcs[i].num_ips+1)) == NULL ) 
434                                         {
435                                                 return NT_STATUS_NO_MEMORY;
436                                         }
437                                         
438                                         dcs[i].ips = tmp_ips;
439                                 }
440                                 dcs[i].num_ips++;
441                                 
442                                 /* copy the new IP address */
443                                 
444                                 buf = (uint8*)&dcs[i].ips[num_ips].s_addr;
445                                 memcpy( buf, rr.rdata, 4 );
446                         }
447                 }
448         }
449
450         qsort( dcs, idx, sizeof(struct dns_rr_srv), QSORT_CAST dnssrvcmp );
451         
452         *dclist = dcs;
453         *numdcs = idx;
454         
455         return NT_STATUS_OK;
456 }
457
458 /*********************************************************************
459  Simple wrapper for a DNS NS query
460 *********************************************************************/
461
462 NTSTATUS ads_dns_lookup_ns( TALLOC_CTX *ctx, const char *dnsdomain, struct dns_rr_ns **nslist, int *numns )
463 {
464         uint8 *buffer = NULL;
465         int resp_len = 0;
466         struct dns_rr_ns *nsarray = NULL;
467         int query_count, answer_count, auth_count, additional_count;
468         uint8 *p;
469         int rrnum;
470         int idx = 0;
471         NTSTATUS status;
472
473         if ( !ctx || !dnsdomain || !nslist ) {
474                 return NT_STATUS_INVALID_PARAMETER;
475         }
476         
477         /* Send the request.  May have to loop several times in case 
478            of large replies */
479            
480         status = dns_send_req( ctx, dnsdomain, T_NS, &buffer, &resp_len );
481         if ( !NT_STATUS_IS_OK(status) ) {
482                 DEBUG(3,("ads_dns_lookup_ns: Failed to send DNS query (%s)\n",
483                         nt_errstr(status)));
484                 return status;
485         }
486         p = buffer;
487
488         /* For some insane reason, the ns_initparse() et. al. routines are only
489            available in libresolv.a, and not the shared lib.  Who knows why....
490            So we have to parse the DNS reply ourselves */
491
492         /* Pull the answer RR's count from the header.  Use the NMB ordering macros */
493
494         query_count      = RSVAL( p, 4 );
495         answer_count     = RSVAL( p, 6 );
496         auth_count       = RSVAL( p, 8 );
497         additional_count = RSVAL( p, 10 );
498
499         DEBUG(4,("ads_dns_lookup_ns: %d records returned in the answer section.\n", 
500                 answer_count));
501                 
502         if ( (nsarray = TALLOC_ARRAY(ctx, struct dns_rr_ns, answer_count)) == NULL ) {
503                 DEBUG(0,("ads_dns_lookup_ns: talloc() failure for %d char*'s\n", 
504                         answer_count));
505                 return NT_STATUS_NO_MEMORY;
506         }
507
508         /* now skip the header */
509
510         p += NS_HFIXEDSZ;
511
512         /* parse the query section */
513
514         for ( rrnum=0; rrnum<query_count; rrnum++ ) {
515                 struct dns_query q;
516
517                 if ( !ads_dns_parse_query( ctx, buffer, buffer+resp_len, &p, &q ) ) {
518                         DEBUG(1,("ads_dns_lookup_ns: Failed to parse query record!\n"));
519                         return NT_STATUS_UNSUCCESSFUL;
520                 }
521         }
522
523         /* now we are at the answer section */
524
525         for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
526                 if ( !ads_dns_parse_rr_ns( ctx, buffer, buffer+resp_len, &p, &nsarray[rrnum] ) ) {
527                         DEBUG(1,("ads_dns_lookup_ns: Failed to parse answer record!\n"));
528                         return NT_STATUS_UNSUCCESSFUL;
529                 }               
530         }
531         idx = rrnum;
532
533         /* Parse the authority section */
534         /* just skip these for now */
535
536         for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
537                 struct dns_rr rr;
538
539                 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
540                         DEBUG(1,("ads_dns_lookup_ns: Failed to parse authority record!\n"));
541                         return NT_STATUS_UNSUCCESSFUL;
542                 }
543         }
544
545         /* Parse the additional records section */
546
547         for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
548                 struct dns_rr rr;
549                 int i;
550
551                 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
552                         DEBUG(1,("ads_dns_lookup_ns: Failed to parse additional records section!\n"));
553                         return NT_STATUS_UNSUCCESSFUL;
554                 }
555
556                 /* only interested in A records as a shortcut for having to come 
557                    back later and lookup the name */
558
559                 if ( (rr.type != T_A) || (rr.rdatalen != 4) ) 
560                         continue;
561
562                 for ( i=0; i<idx; i++ ) {
563                         if ( strcmp( rr.hostname, nsarray[i].hostname ) == 0 ) {
564                                 uint8 *buf = (uint8*)&nsarray[i].ip.s_addr;
565                                 memcpy( buf, rr.rdata, 4 );
566                         }
567                 }
568         }
569         
570         *nslist = nsarray;
571         *numns = idx;
572         
573         return NT_STATUS_OK;
574 }
575
576 /****************************************************************************
577  Store and fetch the AD client sitename.
578 ****************************************************************************/
579
580 #define SITENAME_KEY    "AD_SITENAME"
581
582 /****************************************************************************
583  Store the AD client sitename.
584  We store indefinately as every new CLDAP query will re-write this.
585  If the sitename is "Default-First-Site-Name" we don't store it
586  as this isn't a valid DNS name.
587 ****************************************************************************/
588
589 BOOL sitename_store(const char *sitename)
590 {
591         time_t expire;
592         BOOL ret = False;
593
594         if (!gencache_init()) {
595                 return False;
596         }
597         
598         if (!sitename || (sitename && !*sitename)) {
599                 DEBUG(5,("sitename_store: deleting empty sitename!\n"));
600                 return gencache_del(SITENAME_KEY);
601         }
602
603         expire = get_time_t_max(); /* Store indefinately. */
604         
605         DEBUG(10,("sitename_store: sitename = [%s], expire = [%u]\n",
606                 sitename, (unsigned int)expire ));
607
608         ret = gencache_set( SITENAME_KEY, sitename, expire );
609         return ret;
610 }
611
612 /****************************************************************************
613  Fetch the AD client sitename.
614  Caller must free.
615 ****************************************************************************/
616
617 char *sitename_fetch(void)
618 {
619         char *sitename = NULL;
620         time_t timeout;
621         BOOL ret = False;
622         
623         if (!gencache_init()) {
624                 return False;
625         }
626         
627         ret = gencache_get( SITENAME_KEY, &sitename, &timeout );
628         if ( !ret ) {
629                 DEBUG(5,("sitename_fetch: No stored sitename\n"));
630         } else {
631                 DEBUG(5,("sitename_fetch: Returning sitename \"%s\"\n",
632                         sitename ));
633         }
634         return sitename;
635 }
636
637 /****************************************************************************
638  Did the sitename change ?
639 ****************************************************************************/
640
641 BOOL stored_sitename_changed(const char *sitename)
642 {
643         BOOL ret = False;
644         char *new_sitename = sitename_fetch();
645
646         if (sitename && new_sitename && !strequal(sitename, new_sitename)) {
647                 ret = True;
648         } else if ((sitename && !new_sitename) ||
649                         (!sitename && new_sitename)) {
650                 ret = True;
651         }
652         SAFE_FREE(new_sitename);
653         return ret;
654 }
655
656 /********************************************************************
657  Query with optional sitename.
658 ********************************************************************/
659
660 NTSTATUS ads_dns_query_internal(TALLOC_CTX *ctx,
661                                 const char *servicename,
662                                 const char *realm,
663                                 const char *sitename,
664                                 struct dns_rr_srv **dclist,
665                                 int *numdcs )
666 {
667         char *name;
668         if (sitename) {
669                 name = talloc_asprintf(ctx, "%s._tcp.%s._sites.dc._msdcs.%s",
670                                 servicename, sitename, realm );
671         } else {
672                 name = talloc_asprintf(ctx, "%s._tcp.dc._msdcs.%s",
673                                 servicename, realm );
674         }
675         if (!name) {
676                 return NT_STATUS_NO_MEMORY;
677         }
678         return ads_dns_lookup_srv( ctx, name, dclist, numdcs );
679 }
680
681 /********************************************************************
682  Query for AD DC's.
683 ********************************************************************/
684
685 NTSTATUS ads_dns_query_dcs(TALLOC_CTX *ctx,
686                         const char *realm,
687                         const char *sitename,
688                         struct dns_rr_srv **dclist,
689                         int *numdcs )
690 {
691         NTSTATUS status;
692
693         status = ads_dns_query_internal(ctx, "_ldap", realm, sitename,
694                                         dclist, numdcs);
695
696         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
697             NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
698                 return status;
699         }
700
701         if (sitename && !NT_STATUS_IS_OK(status)) {
702                 /* Sitename DNS query may have failed. Try without. */
703                 status = ads_dns_query_internal(ctx, "_ldap", realm, NULL,
704                                                 dclist, numdcs);
705         }
706         return status;
707 }
708
709 /********************************************************************
710  Query for AD KDC's.
711  Even if our underlying kerberos libraries are UDP only, this
712  is pretty safe as it's unlikely that a KDC supports TCP and not UDP.
713 ********************************************************************/
714
715 NTSTATUS ads_dns_query_kdcs(TALLOC_CTX *ctx,
716                         const char *realm,
717                         const char *sitename,
718                         struct dns_rr_srv **dclist,
719                         int *numdcs )
720 {
721         NTSTATUS status;
722
723         status = ads_dns_query_internal(ctx, "_kerberos", realm, sitename,
724                                         dclist, numdcs);
725
726         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
727             NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
728                 return status;
729         }
730
731         if (sitename && !NT_STATUS_IS_OK(status)) {
732                 /* Sitename DNS query may have failed. Try without. */
733                 status = ads_dns_query_internal(ctx, "_kerberos", realm, NULL,
734                                                 dclist, numdcs);
735         }
736         return status;
737 }