2 Unix SMB/CIFS implementation.
4 Copyright (C) Gerald (Jerry) Carter 2006.
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.
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.
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.
23 /* AIX resolv.h uses 'class' in struct ns_rr */
31 /* resolver headers */
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
39 #define MAX_DNS_PACKET_SIZE 0xffff
41 #ifdef NS_HFIXEDSZ /* Bind 8/9 interface */
44 # define T_SRV ns_t_srv
47 # define NS_HFIXEDSZ HFIXEDSZ
49 # define NS_HFIXEDSZ sizeof(HEADER)
50 # endif /* HFIXEDSZ */
52 # define NS_PACKETSZ PACKETSZ
53 # else /* 512 is usually the default */
54 # define NS_PACKETSZ 512
55 # endif /* PACKETSZ */
59 /*********************************************************************
60 *********************************************************************/
62 static BOOL ads_dns_parse_query( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
63 uint8 **ptr, struct dns_query *q )
71 if ( !start || !end || !q || !*ptr)
74 /* See RFC 1035 for details. If this fails, then return. */
76 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
81 q->hostname = talloc_strdup( ctx, hostname );
83 /* check that we have space remaining */
85 if ( PTR_DIFF(p+4, end) > 0 )
88 q->type = RSVAL( p, 0 );
89 q->in_class = RSVAL( p, 2 );
97 /*********************************************************************
98 *********************************************************************/
100 static BOOL ads_dns_parse_rr( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
101 uint8 **ptr, struct dns_rr *rr )
107 if ( !start || !end || !rr || !*ptr)
111 /* pull the name from the answer */
113 namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
118 rr->hostname = talloc_strdup( ctx, hostname );
120 /* check that we have space remaining */
122 if ( PTR_DIFF(p+10, end) > 0 )
125 /* pull some values and then skip onto the string */
127 rr->type = RSVAL(p, 0);
128 rr->in_class = RSVAL(p, 2);
129 rr->ttl = RIVAL(p, 4);
130 rr->rdatalen = RSVAL(p, 8);
134 /* sanity check the available space */
136 if ( PTR_DIFF(p+rr->rdatalen, end ) > 0 ) {
141 /* save a point to the rdata for this section */
151 /*********************************************************************
152 *********************************************************************/
154 static BOOL ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
155 uint8 **ptr, struct dns_rr_srv *srv )
162 if ( !start || !end || !srv || !*ptr)
165 /* Parse the RR entry. Coming out of the this, ptr is at the beginning
166 of the next record */
168 if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
169 DEBUG(1,("ads_dns_parse_rr_srv: Failed to parse RR record\n"));
173 if ( rr.type != T_SRV ) {
174 DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n", rr.type));
180 srv->priority = RSVAL(p, 0);
181 srv->weight = RSVAL(p, 2);
182 srv->port = RSVAL(p, 4);
186 namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
188 DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
191 srv->hostname = talloc_strdup( ctx, dcname );
197 /*********************************************************************
198 Sort SRV record list based on weight and priority. See RFC 2782.
199 *********************************************************************/
201 static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b )
206 srand( (uint32)time(NULL) );
209 if ( a->priority == b->priority ) {
211 /* randomize entries with an equal weight and priority */
212 if ( a->weight == b->weight )
213 return rand() % 2 ? -1 : 1;
215 /* higher weights should be sorted lower */
216 if ( a->weight > b->weight )
222 if ( a->priority < b->priority )
228 /*********************************************************************
229 Simple wrapper for a DNS SRV query
230 *********************************************************************/
232 NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, const char *name, struct dns_rr_srv **dclist, int *numdcs )
234 uint8 *buffer = NULL;
236 int resp_len = NS_PACKETSZ;
237 struct dns_rr_srv *dcs = NULL;
238 int query_count, answer_count, auth_count, additional_count;
243 if ( !ctx || !name || !dclist ) {
244 return NT_STATUS_INVALID_PARAMETER;
247 /* Send the request. May have to loop several times in case
252 TALLOC_FREE( buffer );
254 buf_len = resp_len * sizeof(uint8);
256 if ( (buffer = TALLOC_ARRAY(ctx, uint8, buf_len)) == NULL ) {
257 DEBUG(0,("ads_dns_lookup_srv: talloc() failed!\n"));
258 return NT_STATUS_NO_MEMORY;
261 if ( (resp_len = res_query(name, C_IN, T_SRV, buffer, buf_len)) < 0 ) {
262 DEBUG(1,("ads_dns_lookup_srv: Failed to resolve %s (%s)\n", name, strerror(errno)));
263 TALLOC_FREE( buffer );
264 return NT_STATUS_UNSUCCESSFUL;
266 } while ( buf_len < resp_len && resp_len < MAX_DNS_PACKET_SIZE );
270 /* For some insane reason, the ns_initparse() et. al. routines are only
271 available in libresolv.a, and not the shared lib. Who knows why....
272 So we have to parse the DNS reply ourselves */
274 /* Pull the answer RR's count from the header. Use the NMB ordering macros */
276 query_count = RSVAL( p, 4 );
277 answer_count = RSVAL( p, 6 );
278 auth_count = RSVAL( p, 8 );
279 additional_count = RSVAL( p, 10 );
281 DEBUG(4,("ads_dns_lookup_srv: %d records returned in the answer section.\n",
284 if ( (dcs = TALLOC_ARRAY(ctx, struct dns_rr_srv, answer_count)) == NULL ) {
285 DEBUG(0,("ads_dns_lookup_srv: talloc() failure for %d char*'s\n",
287 return NT_STATUS_NO_MEMORY;
290 /* now skip the header */
294 /* parse the query section */
296 for ( rrnum=0; rrnum<query_count; rrnum++ ) {
299 if ( !ads_dns_parse_query( ctx, buffer, buffer+resp_len, &p, &q ) ) {
300 DEBUG(1,("ads_dns_lookup_srv: Failed to parse query record!\n"));
301 return NT_STATUS_UNSUCCESSFUL;
305 /* now we are at the answer section */
307 for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
308 if ( !ads_dns_parse_rr_srv( ctx, buffer, buffer+resp_len, &p, &dcs[rrnum] ) ) {
309 DEBUG(1,("ads_dns_lookup_srv: Failed to parse answer record!\n"));
310 return NT_STATUS_UNSUCCESSFUL;
315 /* Parse the authority section */
316 /* just skip these for now */
318 for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
321 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
322 DEBUG(1,("ads_dns_lookup_srv: Failed to parse authority record!\n"));
323 return NT_STATUS_UNSUCCESSFUL;
327 /* Parse the additional records section */
329 for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
333 if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
334 DEBUG(1,("ads_dns_lookup_srv: Failed to parse additional records section!\n"));
335 return NT_STATUS_UNSUCCESSFUL;
338 /* only interested in A records as a shortcut for having to come
339 back later and lookup the name */
341 if ( (rr.type != T_A) || (rr.rdatalen != 4) )
344 for ( i=0; i<idx; i++ ) {
345 if ( strcmp( rr.hostname, dcs[i].hostname ) == 0 ) {
346 uint8 *buf = (uint8*)&dcs[i].ip.s_addr;
347 memcpy( buf, rr.rdata, 4 );
352 qsort( dcs, idx, sizeof(struct dns_rr_srv), QSORT_CAST dnssrvcmp );
360 /********************************************************************
361 ********************************************************************/
363 NTSTATUS ads_dns_query_dcs( TALLOC_CTX *ctx, const char *domain, struct dns_rr_srv **dclist, int *numdcs )
367 snprintf( name, sizeof(name), "_ldap._tcp.dc._msdcs.%s", domain );
369 return ads_dns_lookup_srv( ctx, name, dclist, numdcs );