From e2d0526c9799a75f87bbbe24f2e5a268df89fea9 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 29 Oct 2007 13:34:00 -0700 Subject: Change our DNS code to cope with AAAA records. A6 records look like a nightmare to use, so ignore them for now. Jeremy. (This used to be commit 814daded90781dc5a5bdd522ea8cfe5d47e6d7a7) --- source3/include/ads_dns.h | 6 +- source3/libads/dns.c | 294 +++++++++++++++++++++++++++---------------- source3/libsmb/dsgetdcname.c | 14 ++- source3/libsmb/namequery.c | 9 +- 4 files changed, 203 insertions(+), 120 deletions(-) diff --git a/source3/include/ads_dns.h b/source3/include/ads_dns.h index a9515f3199..24805f85a3 100644 --- a/source3/include/ads_dns.h +++ b/source3/include/ads_dns.h @@ -36,7 +36,7 @@ struct dns_rr { uint16 in_class; uint32 ttl; uint16 rdatalen; - uint8 *rdata; + uint8 *rdata; }; /* SRV records */ @@ -47,14 +47,14 @@ struct dns_rr_srv { uint16 weight; uint16 port; size_t num_ips; - struct in_addr *ips; /* support multi-homed hosts */ + struct sockaddr_storage *ss_s; /* support multi-homed hosts */ }; /* NS records */ struct dns_rr_ns { const char *hostname; - struct in_addr ip; + struct sockaddr_storage ss; }; diff --git a/source3/libads/dns.c b/source3/libads/dns.c index 7959e910a8..8aca5b083e 100644 --- a/source3/libads/dns.c +++ b/source3/libads/dns.c @@ -1,18 +1,19 @@ -/* +/* Unix SMB/CIFS implementation. DNS utility library Copyright (C) Gerald (Jerry) Carter 2006. + Copyright (C) Jeremy Allison 2007. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . */ @@ -44,6 +45,13 @@ #if !defined(T_A) /* AIX 5.3 already defines T_A */ # define T_A ns_t_a #endif + +#if defined(HAVE_IPV6) +#if !defined(T_AAAA) +# define T_AAAA ns_t_aaaa +#endif +#endif + # define T_SRV ns_t_srv #if !defined(T_NS) /* AIX 5.3 already defines T_NS */ # define T_NS ns_t_ns @@ -73,7 +81,7 @@ static bool ads_dns_parse_query( TALLOC_CTX *ctx, uint8 *start, uint8 *end, int namelen; ZERO_STRUCTP( q ); - + if ( !start || !end || !q || !*ptr) return False; @@ -134,7 +142,7 @@ static bool ads_dns_parse_rr( TALLOC_CTX *ctx, uint8 *start, uint8 *end, rr->in_class = RSVAL(p, 2); rr->ttl = RIVAL(p, 4); rr->rdatalen = RSVAL(p, 8); - + p += 10; /* sanity check the available space */ @@ -168,7 +176,7 @@ static bool ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8 *start, uint8 *end, if ( !start || !end || !srv || !*ptr) return -1; - /* Parse the RR entry. Coming out of the this, ptr is at the beginning + /* Parse the RR entry. Coming out of the this, ptr is at the beginning of the next record */ if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) { @@ -177,7 +185,8 @@ static bool ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8 *start, uint8 *end, } if ( rr.type != T_SRV ) { - DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n", rr.type)); + DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n", + rr.type)); return False; } @@ -213,7 +222,7 @@ static bool ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8 *start, uint8 *end, if ( !start || !end || !nsrec || !*ptr) return -1; - /* Parse the RR entry. Coming out of the this, ptr is at the beginning + /* Parse the RR entry. Coming out of the this, ptr is at the beginning of the next record */ if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) { @@ -222,14 +231,15 @@ static bool ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8 *start, uint8 *end, } if ( rr.type != T_NS ) { - DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n", rr.type)); + DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n", + rr.type)); return False; } p = rr.rdata; /* ame server hostname */ - + namelen = dn_expand( start, end, p, nsname, sizeof(nsname) ); if ( namelen < 0 ) { DEBUG(1,("ads_dns_parse_rr_ns: Failed to uncompress name!\n")); @@ -249,16 +259,16 @@ static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b ) if ( a->priority == b->priority ) { /* randomize entries with an equal weight and priority */ - if ( a->weight == b->weight ) + if ( a->weight == b->weight ) return 0; - /* higher weights should be sorted lower */ + /* higher weights should be sorted lower */ if ( a->weight > b->weight ) return -1; else return 1; } - + if ( a->priority < b->priority ) return -1; @@ -271,14 +281,14 @@ static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b ) #define DNS_FAILED_WAITTIME 30 -static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type, +static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type, uint8 **buf, int *resp_length ) { uint8 *buffer = NULL; size_t buf_len; - int resp_len = NS_PACKETSZ; + int resp_len = NS_PACKETSZ; static time_t last_dns_check = 0; - static NTSTATUS last_dns_status = NT_STATUS_OK; + static NTSTATUS last_dns_status = NT_STATUS_OK; time_t now = time(NULL); /* Try to prevent bursts of DNS lookups if the server is down */ @@ -288,13 +298,13 @@ static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type, if ( last_dns_check > now ) last_dns_check = 0; - /* IF we had a DNS timeout or a bad server and we are still - in the 30 second cache window, just return the previous + /* IF we had a DNS timeout or a bad server and we are still + in the 30 second cache window, just return the previous status and save the network timeout. */ if ( (NT_STATUS_EQUAL(last_dns_status,NT_STATUS_IO_TIMEOUT) || NT_STATUS_EQUAL(last_dns_status,NT_STATUS_CONNECTION_REFUSED)) && - (last_dns_check+DNS_FAILED_WAITTIME) > now ) + (last_dns_check+DNS_FAILED_WAITTIME) > now ) { DEBUG(10,("last_dns_check: Returning cached status (%s)\n", nt_errstr(last_dns_status) )); @@ -305,39 +315,44 @@ static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type, do { if ( buffer ) TALLOC_FREE( buffer ); - + buf_len = resp_len * sizeof(uint8); - if (buf_len) { - if ( (buffer = TALLOC_ARRAY(ctx, uint8, buf_len)) == NULL ) { - DEBUG(0,("ads_dns_lookup_srv: talloc() failed!\n")); + if (buf_len) { + if ((buffer = TALLOC_ARRAY(ctx, uint8, buf_len)) + == NULL ) { + DEBUG(0,("ads_dns_lookup_srv: " + "talloc() failed!\n")); last_dns_status = NT_STATUS_NO_MEMORY; last_dns_check = time(NULL); - return last_dns_status; + return last_dns_status; } } - if ( (resp_len = res_query(name, C_IN, q_type, buffer, buf_len)) < 0 ) { - DEBUG(3,("ads_dns_lookup_srv: Failed to resolve %s (%s)\n", name, strerror(errno))); + if ((resp_len = res_query(name, C_IN, q_type, buffer, buf_len)) + < 0 ) { + DEBUG(3,("ads_dns_lookup_srv: " + "Failed to resolve %s (%s)\n", + name, strerror(errno))); TALLOC_FREE( buffer ); last_dns_status = NT_STATUS_UNSUCCESSFUL; - + if (errno == ETIMEDOUT) { - last_dns_status = NT_STATUS_IO_TIMEOUT; + last_dns_status = NT_STATUS_IO_TIMEOUT; } if (errno == ECONNREFUSED) { - last_dns_status = NT_STATUS_CONNECTION_REFUSED; + last_dns_status = NT_STATUS_CONNECTION_REFUSED; } last_dns_check = time(NULL); return last_dns_status; } } while ( buf_len < resp_len && resp_len < MAX_DNS_PACKET_SIZE ); - + *buf = buffer; *resp_length = resp_len; last_dns_check = time(NULL); - last_dns_status = NT_STATUS_OK; + last_dns_status = NT_STATUS_OK; return last_dns_status; } @@ -345,7 +360,10 @@ static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type, Simple wrapper for a DNS SRV query *********************************************************************/ -static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, const char *name, struct dns_rr_srv **dclist, int *numdcs ) +static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, + const char *name, + struct dns_rr_srv **dclist, + int *numdcs) { uint8 *buffer = NULL; int resp_len = 0; @@ -359,8 +377,8 @@ static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, const char *name, struct dn if ( !ctx || !name || !dclist ) { return NT_STATUS_INVALID_PARAMETER; } - - /* Send the request. May have to loop several times in case + + /* Send the request. May have to loop several times in case of large replies */ status = dns_send_req( ctx, name, T_SRV, &buffer, &resp_len ); @@ -375,19 +393,23 @@ static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, const char *name, struct dn available in libresolv.a, and not the shared lib. Who knows why.... So we have to parse the DNS reply ourselves */ - /* Pull the answer RR's count from the header. Use the NMB ordering macros */ + /* Pull the answer RR's count from the header. + * Use the NMB ordering macros */ query_count = RSVAL( p, 4 ); answer_count = RSVAL( p, 6 ); auth_count = RSVAL( p, 8 ); additional_count = RSVAL( p, 10 ); - DEBUG(4,("ads_dns_lookup_srv: %d records returned in the answer section.\n", + DEBUG(4,("ads_dns_lookup_srv: " + "%d records returned in the answer section.\n", answer_count)); - + if (answer_count) { - if ( (dcs = TALLOC_ZERO_ARRAY(ctx, struct dns_rr_srv, answer_count)) == NULL ) { - DEBUG(0,("ads_dns_lookup_srv: talloc() failure for %d char*'s\n", + if ((dcs = TALLOC_ZERO_ARRAY(ctx, struct dns_rr_srv, + answer_count)) == NULL ) { + DEBUG(0,("ads_dns_lookup_srv: " + "talloc() failure for %d char*'s\n", answer_count)); return NT_STATUS_NO_MEMORY; } @@ -404,8 +426,10 @@ static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, const char *name, struct dn for ( rrnum=0; rrnumip = *interpret_addr2(dcs[i].hostname); + if (!dcs[i].ss_s) { + interpret_string_addr(&r->ss, dcs[i].hostname, 0); i++; j = 0; } else { @@ -638,7 +638,7 @@ static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx, continue; } - r->ip = dcs[i].ips[j]; + r->ss = dcs[i].ss_s[j]; j++; } @@ -650,7 +650,7 @@ static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx, * back to netbios lookups is that our DNS server doesn't know * anything about the DC's -- jerry */ - if (!is_zero_ip_v4(r->ip)) { + if (!is_zero_addr(&r->ss)) { (*return_count)++; continue; } @@ -789,8 +789,10 @@ static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx, } if (flags & DS_IP_REQUIRED) { + char addr[INET6_ADDRSTRLEN]; + print_sockaddr(addr, sizeof(addr), &dclist[i]->ss); dc_address = talloc_asprintf(mem_ctx, "\\\\%s", - inet_ntoa(dclist[i]->ip)); + addr); dc_address_type = ADS_INET_ADDRESS; } else { dc_address = talloc_asprintf(mem_ctx, "\\\\%s", diff --git a/source3/libsmb/namequery.c b/source3/libsmb/namequery.c index f4c516921c..dde758b41c 100644 --- a/source3/libsmb/namequery.c +++ b/source3/libsmb/namequery.c @@ -1341,15 +1341,14 @@ NTSTATUS resolve_ads(const char *name, i = 0; j = 0; while ( i < numdcs && (*return_countport = dcs[i].port; /* If we don't have an IP list for a name, lookup it up */ - if (!dcs[i].ips) { - ip = *interpret_addr2(dcs[i].hostname); + if (!dcs[i].ss_s) { + interpret_string_addr(&r->ss, dcs[i].hostname, 0); i++; j = 0; } else { @@ -1361,12 +1360,10 @@ NTSTATUS resolve_ads(const char *name, continue; } - ip = dcs[i].ips[j]; + r->ss = dcs[i].ss_s[j]; j++; } - in_addr_to_sockaddr_storage(&r->ss, ip); - /* make sure it is a valid IP. I considered checking the * negative connection cache, but this is the wrong place * for it. Maybe only as a hack. After think about it, if -- cgit