Make use of ZERO_STRUCT instead of memset in namequery.c
[jra/samba/.git] / source / libsmb / namequery.c
index dcbd11048e2ff880f358c00edffda313d92f64c5..893c9265fd241b07a21fadbb406fe968b08a9964 100644 (file)
-/* 
+/*
    Unix SMB/CIFS implementation.
    name query routines
    Copyright (C) Andrew Tridgell 1994-1998
-   
+   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 2 of the License, or
+   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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-   
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 
 /* nmbd.c sets this to True. */
-BOOL global_in_nmbd = False;
+bool global_in_nmbd = False;
+
+/****************************
+ * SERVER AFFINITY ROUTINES *
+ ****************************/
+
+ /* Server affinity is the concept of preferring the last domain
+    controller with whom you had a successful conversation */
 
 /****************************************************************************
- Generate a random trn_id.
 ****************************************************************************/
+#define SAFKEY_FMT     "SAF/DOMAIN/%s"
+#define SAF_TTL                900
 
-static int generate_trn_id(void)
+static char *saf_key(const char *domain)
+{
+       char *keystr;
+
+       asprintf_strupper_m(&keystr, SAFKEY_FMT, domain);
+
+       return keystr;
+}
+
+/****************************************************************************
+****************************************************************************/
+
+bool saf_store( const char *domain, const char *servername )
+{
+       char *key;
+       time_t expire;
+       bool ret = False;
+
+       if ( !domain || !servername ) {
+               DEBUG(2,("saf_store: "
+                       "Refusing to store empty domain or servername!\n"));
+               return False;
+       }
+
+       if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
+               DEBUG(0,("saf_store: "
+                       "refusing to store 0 length domain or servername!\n"));
+               return False;
+       }
+
+       if ( !gencache_init() )
+               return False;
+
+       key = saf_key( domain );
+       expire = time( NULL ) + SAF_TTL;
+
+       DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
+               domain, servername, (unsigned int)expire ));
+
+       ret = gencache_set( key, servername, expire );
+
+       SAFE_FREE( key );
+
+       return ret;
+}
+
+bool saf_delete( const char *domain )
 {
-       static int trn_id;
+       char *key;
+       bool ret = False;
 
-       if (trn_id == 0) {
-               sys_srandom(sys_getpid());
+       if ( !domain ) {
+               DEBUG(2,("saf_delete: Refusing to delete empty domain\n"));
+               return False;
        }
 
-       trn_id = sys_random();
+       if ( !gencache_init() )
+               return False;
+
+       key = saf_key(domain);
+       ret = gencache_del(key);
+
+       if (ret) {
+               DEBUG(10,("saf_delete: domain = [%s]\n", domain ));
+       }
+
+       SAFE_FREE( key );
+
+       return ret;
+}
+
+/****************************************************************************
+****************************************************************************/
+
+char *saf_fetch( const char *domain )
+{
+       char *server = NULL;
+       time_t timeout;
+       bool ret = False;
+       char *key = NULL;
+
+       if ( !domain || strlen(domain) == 0) {
+               DEBUG(2,("saf_fetch: Empty domain name!\n"));
+               return NULL;
+       }
+
+       if ( !gencache_init() )
+               return False;
+
+       key = saf_key( domain );
+
+       ret = gencache_get( key, &server, &timeout );
+
+       SAFE_FREE( key );
+
+       if ( !ret ) {
+               DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
+                                       domain ));
+       } else {
+               DEBUG(5,("saf_fetch: Returning \"%s\" for \"%s\" domain\n",
+                       server, domain ));
+       }
 
-       return trn_id % (unsigned)0x7FFF;
+       return server;
+}
+
+/****************************************************************************
+ Generate a random trn_id.
+****************************************************************************/
+
+static int generate_trn_id(void)
+{
+       uint16 id;
+
+       generate_random_buffer((uint8 *)&id, sizeof(id));
+
+       return id % (unsigned)0x7FFF;
 }
 
 /****************************************************************************
  Parse a node status response into an array of structures.
 ****************************************************************************/
 
-static struct node_status *parse_node_status(char *p, int *num_names)
+static NODE_STATUS_STRUCT *parse_node_status(char *p,
+                               int *num_names,
+                               struct node_status_extra *extra)
 {
-       struct node_status *ret;
+       NODE_STATUS_STRUCT *ret;
        int i;
 
        *num_names = CVAL(p,0);
@@ -55,8 +170,9 @@ static struct node_status *parse_node_status(char *p, int *num_names)
        if (*num_names == 0)
                return NULL;
 
-       ret = (struct node_status *)malloc(sizeof(struct node_status)* (*num_names));
-       if (!ret) return NULL;
+       ret = SMB_MALLOC_ARRAY(NODE_STATUS_STRUCT,*num_names);
+       if (!ret)
+               return NULL;
 
        p++;
        for (i=0;i< *num_names;i++) {
@@ -65,9 +181,15 @@ static struct node_status *parse_node_status(char *p, int *num_names)
                ret[i].type = CVAL(p,15);
                ret[i].flags = p[16];
                p += 18;
-               DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name, 
+               DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
                           ret[i].type, ret[i].flags));
        }
+       /*
+        * Also, pick up the MAC address ...
+        */
+       if (extra) {
+               memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
+       }
        return ret;
 }
 
@@ -77,28 +199,35 @@ static struct node_status *parse_node_status(char *p, int *num_names)
  structures holding the returned names or NULL if the query failed.
 **************************************************************************/
 
-struct node_status *node_status_query(int fd,struct nmb_name *name,
-                                     struct in_addr to_ip, int *num_names)
+NODE_STATUS_STRUCT *node_status_query(int fd,
+                                       struct nmb_name *name,
+                                       const struct sockaddr_storage *to_ss,
+                                       int *num_names,
+                                       struct node_status_extra *extra)
 {
-       BOOL found=False;
+       bool found=False;
        int retries = 2;
        int retry_time = 2000;
        struct timeval tval;
        struct packet_struct p;
        struct packet_struct *p2;
        struct nmb_packet *nmb = &p.packet.nmb;
-       struct node_status *ret;
+       NODE_STATUS_STRUCT *ret;
 
        ZERO_STRUCT(p);
 
+       if (to_ss->ss_family != AF_INET) {
+               /* Can't do node status to IPv6 */
+               return NULL;
+       }
        nmb->header.name_trn_id = generate_trn_id();
        nmb->header.opcode = 0;
-       nmb->header.response = False;
-       nmb->header.nm_flags.bcast = False;
-       nmb->header.nm_flags.recursion_available = False;
-       nmb->header.nm_flags.recursion_desired = False;
-       nmb->header.nm_flags.trunc = False;
-       nmb->header.nm_flags.authoritative = False;
+       nmb->header.response = false;
+       nmb->header.nm_flags.bcast = false;
+       nmb->header.nm_flags.recursion_available = false;
+       nmb->header.nm_flags.recursion_desired = false;
+       nmb->header.nm_flags.trunc = false;
+       nmb->header.nm_flags.authoritative = false;
        nmb->header.rcode = 0;
        nmb->header.qdcount = 1;
        nmb->header.ancount = 0;
@@ -108,15 +237,15 @@ struct node_status *node_status_query(int fd,struct nmb_name *name,
        nmb->question.question_type = 0x21;
        nmb->question.question_class = 0x1;
 
-       p.ip = to_ip;
+       p.ip = ((const struct sockaddr_in *)to_ss)->sin_addr;
        p.port = NMB_PORT;
        p.fd = fd;
        p.timestamp = time(NULL);
        p.packet_type = NMB_PACKET;
-       
+
        GetTimeOfDay(&tval);
-  
-       if (!send_packet(&p)) 
+
+       if (!send_packet(&p))
                return NULL;
 
        retries--;
@@ -133,10 +262,10 @@ struct node_status *node_status_query(int fd,struct nmb_name *name,
                        retries--;
                }
 
-               if ((p2=receive_nmb_packet(fd,90,nmb->header.name_trn_id))) {     
+               if ((p2=receive_nmb_packet(fd,90,nmb->header.name_trn_id))) {
                        struct nmb_packet *nmb2 = &p2->packet.nmb;
                        debug_nmb_packet(p2);
-                       
+
                        if (nmb2->header.opcode != 0 ||
                            nmb2->header.nm_flags.bcast ||
                            nmb2->header.rcode ||
@@ -149,12 +278,13 @@ struct node_status *node_status_query(int fd,struct nmb_name *name,
                                continue;
                        }
 
-                       ret = parse_node_status(&nmb2->answers->rdata[0], num_names);
+                       ret = parse_node_status(&nmb2->answers->rdata[0],
+                                       num_names, extra);
                        free_packet(p2);
                        return ret;
                }
        }
-       
+
        return NULL;
 }
 
@@ -163,34 +293,54 @@ struct node_status *node_status_query(int fd,struct nmb_name *name,
  a servers name given its IP. Return the matched name in *name.
 **************************************************************************/
 
-BOOL name_status_find(const char *q_name, int q_type, int type, struct in_addr to_ip, char *name)
+bool name_status_find(const char *q_name,
+                       int q_type,
+                       int type,
+                       const struct sockaddr_storage *to_ss,
+                       fstring name)
 {
-       struct node_status *status = NULL;
+       char addr[INET6_ADDRSTRLEN];
+       struct sockaddr_storage ss;
+       NODE_STATUS_STRUCT *status = NULL;
        struct nmb_name nname;
        int count, i;
        int sock;
-       BOOL result = False;
+       bool result = false;
 
        if (lp_disable_netbios()) {
-               DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n", q_name, q_type));
+               DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n",
+                                       q_name, q_type));
                return False;
        }
 
-       DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name, 
-                  q_type, inet_ntoa(to_ip)));
+       print_sockaddr(addr, sizeof(addr), to_ss);
+
+       DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name,
+                  q_type, addr));
 
        /* Check the cache first. */
 
-       if (namecache_status_fetch(q_name, q_type, type, to_ip, name))
+       if (namecache_status_fetch(q_name, q_type, type, to_ss, name)) {
                return True;
+       }
 
-       sock = open_socket_in(SOCK_DGRAM, 0, 3, interpret_addr(lp_socket_address()), True);
+       if (to_ss->ss_family != AF_INET) {
+               /* Can't do node status to IPv6 */
+               return false;
+       }
+
+       if (!interpret_string_addr(&ss, lp_socket_address(),
+                               AI_NUMERICHOST|AI_PASSIVE)) {
+               zero_addr(&ss);
+       }
+
+       sock = open_socket_in(SOCK_DGRAM, 0, 3, &ss, True);
        if (sock == -1)
                goto done;
 
        /* W2K PDC's seem not to respond to '*'#0. JRA */
        make_nmb_name(&nname, q_name, q_type);
-       status = node_status_query(sock, &nname, to_ip, &count);
+       status = node_status_query(sock, &nname, to_ss, &count, NULL);
        close(sock);
        if (!status)
                goto done;
@@ -202,16 +352,17 @@ BOOL name_status_find(const char *q_name, int q_type, int type, struct in_addr t
        if (i == count)
                goto done;
 
-       pull_ascii(name, status[i].name, 16, 15, STR_TERMINATE);
+       pull_ascii_nstring(name, sizeof(fstring), status[i].name);
 
        /* Store the result in the cache. */
-       /* but don't store an entry for 0x1c names here.  Here we have 
+       /* but don't store an entry for 0x1c names here.  Here we have
           a single host and DOMAIN<0x1c> names should be a list of hosts */
-          
-       if ( q_type != 0x1c )
-               namecache_status_store(q_name, q_type, type, to_ip, name);
 
-       result = True;
+       if ( q_type != 0x1c ) {
+               namecache_status_store(q_name, q_type, type, to_ss, name);
+       }
+
+       result = true;
 
  done:
        SAFE_FREE(status);
@@ -219,41 +370,92 @@ BOOL name_status_find(const char *q_name, int q_type, int type, struct in_addr t
        DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
 
        if (result)
-               DEBUGADD(10, (", name %s ip address is %s", name, inet_ntoa(to_ip)));
+               DEBUGADD(10, (", name %s ip address is %s", name, addr));
 
-       DEBUG(10, ("\n"));      
+       DEBUG(10, ("\n"));
 
        return result;
 }
 
 /*
-  comparison function used by sort_ip_list
+  comparison function used by sort_addr_list
 */
 
-int ip_compare(struct in_addr *ip1, struct in_addr *ip2)
+static int addr_compare(const struct sockaddr_storage *ss1,
+               const struct sockaddr_storage *ss2)
 {
        int max_bits1=0, max_bits2=0;
        int num_interfaces = iface_count();
        int i;
 
+       /* Sort IPv6 addresses first. */
+       if (ss1->ss_family != ss2->ss_family) {
+               if (ss2->ss_family == AF_INET) {
+                       return -1;
+               } else {
+                       return 1;
+               }
+       }
+
+       /* Here we know both addresses are of the same
+        * family. */
+
        for (i=0;i<num_interfaces;i++) {
-               struct in_addr ip;
+               const struct sockaddr_storage *pss = iface_n_bcast(i);
+               unsigned char *p_ss1 = NULL;
+               unsigned char *p_ss2 = NULL;
+               unsigned char *p_if = NULL;
+               size_t len = 0;
                int bits1, bits2;
-               ip = *iface_n_bcast(i);
-               bits1 = matching_quad_bits((uchar *)&ip1->s_addr, (uchar *)&ip.s_addr);
-               bits2 = matching_quad_bits((uchar *)&ip2->s_addr, (uchar *)&ip.s_addr);
+
+               if (pss->ss_family != ss1->ss_family) {
+                       /* Ignore interfaces of the wrong type. */
+                       continue;
+               }
+               if (pss->ss_family == AF_INET) {
+                       p_if = (unsigned char *)
+                               &((const struct sockaddr_in *)pss)->sin_addr;
+                       p_ss1 = (unsigned char *)
+                               &((const struct sockaddr_in *)ss1)->sin_addr;
+                       p_ss2 = (unsigned char *)
+                               &((const struct sockaddr_in *)ss2)->sin_addr;
+                       len = 4;
+               }
+#if defined(HAVE_IPV6)
+               if (pss->ss_family == AF_INET6) {
+                       p_if = (unsigned char *)
+                               &((const struct sockaddr_in6 *)pss)->sin6_addr;
+                       p_ss1 = (unsigned char *)
+                               &((const struct sockaddr_in6 *)ss1)->sin6_addr;
+                       p_ss2 = (unsigned char *)
+                               &((const struct sockaddr_in6 *)ss2)->sin6_addr;
+                       len = 16;
+               }
+#endif
+               if (!p_ss1 || !p_ss2 || !p_if || len == 0) {
+                       continue;
+               }
+               bits1 = matching_len_bits(p_ss1, p_if, len);
+               bits2 = matching_len_bits(p_ss2, p_if, len);
                max_bits1 = MAX(bits1, max_bits1);
                max_bits2 = MAX(bits2, max_bits2);
-       }       
-       
-       /* bias towards directly reachable IPs */
-       if (iface_local(*ip1)) {
-               max_bits1 += 32;
-       }
-       if (iface_local(*ip2)) {
-               max_bits2 += 32;
        }
 
+       /* Bias towards directly reachable IPs */
+       if (iface_local(ss1)) {
+               if (ss1->ss_family == AF_INET) {
+                       max_bits1 += 32;
+               } else {
+                       max_bits1 += 128;
+               }
+       }
+       if (iface_local(ss2)) {
+               if (ss2->ss_family == AF_INET) {
+                       max_bits2 += 32;
+               } else {
+                       max_bits2 += 128;
+               }
+       }
        return max_bits2 - max_bits1;
 }
 
@@ -261,73 +463,84 @@ int ip_compare(struct in_addr *ip1, struct in_addr *ip2)
  compare 2 ldap IPs by nearness to our interfaces - used in qsort
 *******************************************************************/
 
-static int ip_service_compare(struct ip_service *ip1, struct ip_service *ip2)
+int ip_service_compare(struct ip_service *ss1, struct ip_service *ss2)
 {
        int result;
-       
-       if ( (result = ip_compare(&ip1->ip, &ip2->ip)) != 0 )
+
+       if ((result = addr_compare(&ss1->ss, &ss2->ss)) != 0) {
                return result;
-               
-       if ( ip1->port > ip2->port )
+       }
+
+       if (ss1->port > ss2->port) {
                return 1;
-       
-       if ( ip1->port < ip2->port )
+       }
+
+       if (ss1->port < ss2->port) {
                return -1;
-               
+       }
+
        return 0;
 }
 
 /*
-  sort an IP list so that names that are close to one of our interfaces 
-  are at the top. This prevents the problem where a WINS server returns an IP that
-  is not reachable from our subnet as the first match
+  sort an IP list so that names that are close to one of our interfaces
+  are at the top. This prevents the problem where a WINS server returns an IP
+  that is not reachable from our subnet as the first match
 */
 
-static void sort_ip_list(struct in_addr *iplist, int count)
+static void sort_addr_list(struct sockaddr_storage *sslist, int count)
 {
        if (count <= 1) {
                return;
        }
 
-       qsort(iplist, count, sizeof(struct in_addr), QSORT_CAST ip_compare);    
+       qsort(sslist, count, sizeof(struct sockaddr_storage),
+                       QSORT_CAST addr_compare);
 }
 
-void sort_ip_list2(struct ip_service *iplist, int count)
+static void sort_service_list(struct ip_service *servlist, int count)
 {
        if (count <= 1) {
                return;
        }
 
-       qsort(iplist, count, sizeof(struct ip_service), QSORT_CAST ip_service_compare); 
+       qsort(servlist, count, sizeof(struct ip_service),
+                       QSORT_CAST ip_service_compare);
 }
 
 /**********************************************************************
- Remove any duplicate address/port pairs in the list 
+ Remove any duplicate address/port pairs in the list
  *********************************************************************/
 
-static int remove_duplicate_addrs2( struct ip_service *iplist, int count )
+static int remove_duplicate_addrs2(struct ip_service *iplist, int count )
 {
        int i, j;
-       
-       DEBUG(10,("remove_duplicate_addrs2: looking for duplicate address/port pairs\n"));
-       
+
+       DEBUG(10,("remove_duplicate_addrs2: "
+                       "looking for duplicate address/port pairs\n"));
+
        /* one loop to remove duplicates */
        for ( i=0; i<count; i++ ) {
-               if ( is_zero_ip(iplist[i].ip) )
+               if ( is_zero_addr(&iplist[i].ss)) {
                        continue;
-                                       
+               }
+
                for ( j=i+1; j<count; j++ ) {
-                       if ( ip_service_equal(iplist[i], iplist[j]) )
-                               zero_ip(&iplist[j].ip);
+                       if (addr_equal(&iplist[i].ss, &iplist[j].ss) &&
+                                       iplist[i].port == iplist[j].port) {
+                               zero_addr(&iplist[j].ss);
+                       }
                }
        }
-                       
+
        /* one loop to clean up any holes we left */
        /* first ip should never be a zero_ip() */
        for (i = 0; i<count; ) {
-               if ( is_zero_ip(iplist[i].ip) ) {
-                       if (i != count-1 )
-                               memmove(&iplist[i], &iplist[i+1], (count - i - 1)*sizeof(iplist[i]));
+               if (is_zero_addr(&iplist[i].ss) ) {
+                       if (i != count-1) {
+                               memmove(&iplist[i], &iplist[i+1],
+                                       (count - i - 1)*sizeof(iplist[i]));
+                       }
                        count--;
                        continue;
                }
@@ -344,69 +557,78 @@ static int remove_duplicate_addrs2( struct ip_service *iplist, int count )
  *timed_out is set if we failed by timing out
 ****************************************************************************/
 
-struct in_addr *name_query(int fd,const char *name,int name_type, 
-                          BOOL bcast,BOOL recurse,
-                          struct in_addr to_ip, int *count, int *flags,
-                          BOOL *timed_out)
+struct sockaddr_storage *name_query(int fd,
+                       const char *name,
+                       int name_type,
+                       bool bcast,
+                       bool recurse,
+                       const struct sockaddr_storage *to_ss,
+                       int *count,
+                       int *flags,
+                       bool *timed_out)
 {
-       BOOL found=False;
+       bool found=false;
        int i, retries = 3;
        int retry_time = bcast?250:2000;
        struct timeval tval;
        struct packet_struct p;
        struct packet_struct *p2;
        struct nmb_packet *nmb = &p.packet.nmb;
-       struct in_addr *ip_list = NULL;
+       struct sockaddr_storage *ss_list = NULL;
 
        if (lp_disable_netbios()) {
-               DEBUG(5,("name_query(%s#%02x): netbios is disabled\n", name, name_type));
+               DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
+                                       name, name_type));
+               return NULL;
+       }
+
+       if (to_ss->ss_family != AF_INET) {
                return NULL;
        }
 
        if (timed_out) {
-               *timed_out = False;
+               *timed_out = false;
        }
-       
+
        memset((char *)&p,'\0',sizeof(p));
        (*count) = 0;
        (*flags) = 0;
-       
+
        nmb->header.name_trn_id = generate_trn_id();
        nmb->header.opcode = 0;
-       nmb->header.response = False;
+       nmb->header.response = false;
        nmb->header.nm_flags.bcast = bcast;
-       nmb->header.nm_flags.recursion_available = False;
+       nmb->header.nm_flags.recursion_available = false;
        nmb->header.nm_flags.recursion_desired = recurse;
-       nmb->header.nm_flags.trunc = False;
-       nmb->header.nm_flags.authoritative = False;
+       nmb->header.nm_flags.trunc = false;
+       nmb->header.nm_flags.authoritative = false;
        nmb->header.rcode = 0;
        nmb->header.qdcount = 1;
        nmb->header.ancount = 0;
        nmb->header.nscount = 0;
        nmb->header.arcount = 0;
-       
+
        make_nmb_name(&nmb->question.question_name,name,name_type);
-       
+
        nmb->question.question_type = 0x20;
        nmb->question.question_class = 0x1;
-       
-       p.ip = to_ip;
+
+       p.ip = ((struct sockaddr_in *)to_ss)->sin_addr;
        p.port = NMB_PORT;
        p.fd = fd;
        p.timestamp = time(NULL);
        p.packet_type = NMB_PACKET;
-       
+
        GetTimeOfDay(&tval);
-       
-       if (!send_packet(&p)) 
+
+       if (!send_packet(&p))
                return NULL;
-       
+
        retries--;
-       
+
        while (1) {
                struct timeval tval2;
-               struct in_addr *tmp_ip_list;
-               
+
                GetTimeOfDay(&tval2);
                if (TvalDiff(&tval,&tval2) > retry_time) {
                        if (!retries)
@@ -416,52 +638,60 @@ struct in_addr *name_query(int fd,const char *name,int name_type,
                        GetTimeOfDay(&tval);
                        retries--;
                }
-               
-               if ((p2=receive_nmb_packet(fd,90,nmb->header.name_trn_id))) {     
+
+               if ((p2=receive_nmb_packet(fd,90,nmb->header.name_trn_id))) {
                        struct nmb_packet *nmb2 = &p2->packet.nmb;
                        debug_nmb_packet(p2);
-                       
+
                        /* If we get a Negative Name Query Response from a WINS
                         * server, we should report it and give up.
                         */
-                       if( 0 == nmb2->header.opcode            /* A query response   */
+                       if( 0 == nmb2->header.opcode    /* A query response   */
                            && !(bcast)                 /* from a WINS server */
-                           && nmb2->header.rcode               /* Error returned     */
+                           && nmb2->header.rcode       /* Error returned     */
                                ) {
-                               
+
                                if( DEBUGLVL( 3 ) ) {
                                        /* Only executed if DEBUGLEVEL >= 3 */
-                                       dbgtext( "Negative name query response, rcode 0x%02x: ", nmb2->header.rcode );
+                                       dbgtext( "Negative name query "
+                                               "response, rcode 0x%02x: ",
+                                               nmb2->header.rcode );
                                        switch( nmb2->header.rcode ) {
                                        case 0x01:
-                                               dbgtext( "Request was invalidly formatted.\n" );
+                                               dbgtext( "Request "
+                                               "was invalidly formatted.\n" );
                                                break;
                                        case 0x02:
-                                               dbgtext( "Problem with NBNS, cannot process name.\n");
+                                               dbgtext( "Problem with NBNS, "
+                                               "cannot process name.\n");
                                                break;
                                        case 0x03:
-                                               dbgtext( "The name requested does not exist.\n" );
+                                               dbgtext( "The name requested "
+                                               "does not exist.\n" );
                                                break;
                                        case 0x04:
-                                               dbgtext( "Unsupported request error.\n" );
+                                               dbgtext( "Unsupported request "
+                                               "error.\n" );
                                                break;
                                        case 0x05:
-                                               dbgtext( "Query refused error.\n" );
+                                               dbgtext( "Query refused "
+                                               "error.\n" );
                                                break;
                                        default:
-                                               dbgtext( "Unrecognized error code.\n" );
+                                               dbgtext( "Unrecognized error "
+                                               "code.\n" );
                                                break;
                                        }
                                }
                                free_packet(p2);
                                return( NULL );
                        }
-                       
+
                        if (nmb2->header.opcode != 0 ||
                            nmb2->header.nm_flags.bcast ||
                            nmb2->header.rcode ||
                            !nmb2->header.ancount) {
-                               /* 
+                               /*
                                 * XXXX what do we do with this? Could be a
                                 * redirect, but we'll discard it for the
                                 * moment.
@@ -469,28 +699,33 @@ struct in_addr *name_query(int fd,const char *name,int name_type,
                                free_packet(p2);
                                continue;
                        }
-                       
-                       tmp_ip_list = (struct in_addr *)Realloc( ip_list, sizeof( ip_list[0] )
-                                                                * ( (*count) + nmb2->answers->rdlength/6 ) );
-                       
-                       if (!tmp_ip_list) {
+
+                       ss_list = SMB_REALLOC_ARRAY(ss_list,
+                                               struct sockaddr_storage,
+                                               (*count) +
+                                               nmb2->answers->rdlength/6);
+
+                       if (!ss_list) {
                                DEBUG(0,("name_query: Realloc failed.\n"));
-                               SAFE_FREE(ip_list);
+                               free_packet(p2);
+                               return NULL;
                        }
-                       
-                       ip_list = tmp_ip_list;
-                       
-                       if (ip_list) {
-                               DEBUG(2,("Got a positive name query response from %s ( ", inet_ntoa(p2->ip)));
-                               for (i=0;i<nmb2->answers->rdlength/6;i++) {
-                                       putip((char *)&ip_list[(*count)],&nmb2->answers->rdata[2+i*6]);
-                                       DEBUGADD(2,("%s ",inet_ntoa(ip_list[(*count)])));
-                                       (*count)++;
-                               }
-                               DEBUGADD(2,(")\n"));
+
+                       DEBUG(2,("Got a positive name query response "
+                                       "from %s ( ",
+                                       inet_ntoa(p2->ip)));
+
+                       for (i=0;i<nmb2->answers->rdlength/6;i++) {
+                               struct in_addr ip;
+                               putip((char *)&ip,&nmb2->answers->rdata[2+i*6]);
+                               in_addr_to_sockaddr_storage(&ss_list[(*count)],
+                                               ip);
+                               DEBUGADD(2,("%s ",inet_ntoa(ip)));
+                               (*count)++;
                        }
-                       
-                       found=True;
+                       DEBUGADD(2,(")\n"));
+
+                       found=true;
                        retries=0;
                        /* We add the flags back ... */
                        if (nmb2->header.response)
@@ -517,27 +752,28 @@ struct in_addr *name_query(int fd,const char *name,int name_type,
        }
 
        /* only set timed_out if we didn't fund what we where looking for*/
-       
+
        if ( !found && timed_out ) {
-               *timed_out = True;
+               *timed_out = true;
        }
 
        /* sort the ip list so we choose close servers first if possible */
-       sort_ip_list(ip_list, *count);
+       sort_addr_list(ss_list, *count);
 
-       return ip_list;
+       return ss_list;
 }
 
 /********************************************************
  Start parsing the lmhosts file.
 *********************************************************/
 
-XFILE *startlmhosts(char *fname)
+XFILE *startlmhosts(const char *fname)
 {
        XFILE *fp = x_fopen(fname,O_RDONLY, 0);
        if (!fp) {
-               DEBUG(4,("startlmhosts: Can't open lmhosts file %s. Error was %s\n",
-                        fname, strerror(errno)));
+               DEBUG(4,("startlmhosts: Can't open lmhosts file %s. "
+                       "Error was %s\n",
+                       fname, strerror(errno)));
                return NULL;
        }
        return fp;
@@ -547,86 +783,103 @@ XFILE *startlmhosts(char *fname)
  Parse the next line in the lmhosts file.
 *********************************************************/
 
-BOOL getlmhostsent( XFILE *fp, pstring name, int *name_type, struct in_addr *ipaddr)
+bool getlmhostsent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, int *name_type,
+               struct sockaddr_storage *pss)
 {
-  pstring line;
+       char line[1024];
 
-  while(!x_feof(fp) && !x_ferror(fp)) {
-    pstring ip,flags,extra;
-    const char *ptr;
-    char *ptr1;
-    int count = 0;
+       *pp_name = NULL;
 
-    *name_type = -1;
+       while(!x_feof(fp) && !x_ferror(fp)) {
+               char *ip = NULL;
+               char *flags = NULL;
+               char *extra = NULL;
+               char *name = NULL;
+               const char *ptr;
+               char *ptr1 = NULL;
+               int count = 0;
 
-    if (!fgets_slash(line,sizeof(pstring),fp))
-      continue;
+               *name_type = -1;
 
-    if (*line == '#')
-      continue;
+               if (!fgets_slash(line,sizeof(line),fp)) {
+                       continue;
+               }
 
-    pstrcpy(ip,"");
-    pstrcpy(name,"");
-    pstrcpy(flags,"");
+               if (*line == '#') {
+                       continue;
+               }
 
-    ptr = line;
+               ptr = line;
 
-    if (next_token(&ptr,ip   ,NULL,sizeof(ip)))
-      ++count;
-    if (next_token(&ptr,name ,NULL, sizeof(pstring)))
-      ++count;
-    if (next_token(&ptr,flags,NULL, sizeof(flags)))
-      ++count;
-    if (next_token(&ptr,extra,NULL, sizeof(extra)))
-      ++count;
+               if (next_token_talloc(ctx, &ptr, &ip, NULL))
+                       ++count;
+               if (next_token_talloc(ctx, &ptr, &name, NULL))
+                       ++count;
+               if (next_token_talloc(ctx, &ptr, &flags, NULL))
+                       ++count;
+               if (next_token_talloc(ctx, &ptr, &extra, NULL))
+                       ++count;
 
-    if (count <= 0)
-      continue;
+               if (count <= 0)
+                       continue;
 
-    if (count > 0 && count < 2)
-    {
-      DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n",line));
-      continue;
-    }
+               if (count > 0 && count < 2) {
+                       DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n",
+                                               line));
+                       continue;
+               }
 
-    if (count >= 4)
-    {
-      DEBUG(0,("getlmhostsent: too many columns in lmhosts file (obsolete syntax)\n"));
-      continue;
-    }
+               if (count >= 4) {
+                       DEBUG(0,("getlmhostsent: too many columns "
+                               "in lmhosts file (obsolete syntax)\n"));
+                       continue;
+               }
 
-    DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n", ip, name, flags));
+               if (!flags) {
+                       flags = talloc_strdup(ctx, "");
+                       if (!flags) {
+                               continue;
+                       }
+               }
 
-    if (strchr_m(flags,'G') || strchr_m(flags,'S'))
-    {
-      DEBUG(0,("getlmhostsent: group flag in lmhosts ignored (obsolete)\n"));
-      continue;
-    }
+               DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n",
+                                       ip, name, flags));
 
-    *ipaddr = *interpret_addr2(ip);
+               if (strchr_m(flags,'G') || strchr_m(flags,'S')) {
+                       DEBUG(0,("getlmhostsent: group flag "
+                               "in lmhosts ignored (obsolete)\n"));
+                       continue;
+               }
 
-    /* Extra feature. If the name ends in '#XX', where XX is a hex number,
-       then only add that name type. */
-    if((ptr1 = strchr_m(name, '#')) != NULL)
-    {
-      char *endptr;
+               if (!interpret_string_addr(pss, ip, AI_NUMERICHOST)) {
+                       DEBUG(0,("getlmhostsent: invalid address "
+                               "%s.\n", ip));
+               }
 
-      ptr1++;
-      *name_type = (int)strtol(ptr1, &endptr, 16);
+               /* Extra feature. If the name ends in '#XX',
+                * where XX is a hex number, then only add that name type. */
+               if((ptr1 = strchr_m(name, '#')) != NULL) {
+                       char *endptr;
+                       ptr1++;
 
-      if(!*ptr1 || (endptr == ptr1))
-      {
-        DEBUG(0,("getlmhostsent: invalid name %s containing '#'.\n", name));
-        continue;
-      }
+                       *name_type = (int)strtol(ptr1, &endptr, 16);
+                       if(!*ptr1 || (endptr == ptr1)) {
+                               DEBUG(0,("getlmhostsent: invalid name "
+                                       "%s containing '#'.\n", name));
+                               continue;
+                       }
 
-      *(--ptr1) = '\0'; /* Truncate at the '#' */
-    }
+                       *(--ptr1) = '\0'; /* Truncate at the '#' */
+               }
 
-    return True;
-  }
+               *pp_name = talloc_strdup(ctx, name);
+               if (!*pp_name) {
+                       return false;
+               }
+               return true;
+       }
 
-  return False;
+       return false;
 }
 
 /********************************************************
@@ -639,60 +892,75 @@ void endlmhosts(XFILE *fp)
 }
 
 /********************************************************
- convert an array if struct in_addrs to struct ip_service
- return False on failure.  Port is set to PORT_NONE;
+ convert an array if struct sockaddr_storage to struct ip_service
+ return false on failure.  Port is set to PORT_NONE;
 *********************************************************/
 
-static BOOL convert_ip2service( struct ip_service **return_iplist, struct in_addr *ip_list, int count )
+static bool convert_ss2service(struct ip_service **return_iplist,
+               const struct sockaddr_storage *ss_list,
+               int count)
 {
        int i;
 
-       if ( count==0 || !ip_list )
+       if ( count==0 || !ss_list )
                return False;
-               
+
        /* copy the ip address; port will be PORT_NONE */
-       if ( (*return_iplist = (struct ip_service*)malloc(count*sizeof(struct ip_service))) == NULL ) {
-               DEBUG(0,("convert_ip2service: malloc failed for %d enetries!\n", count ));
+       if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, count)) ==
+                       NULL) {
+               DEBUG(0,("convert_ip2service: malloc failed "
+                       "for %d enetries!\n", count ));
                return False;
        }
-       
+
        for ( i=0; i<count; i++ ) {
-               (*return_iplist)[i].ip   = ip_list[i];
+               (*return_iplist)[i].ss   = ss_list[i];
                (*return_iplist)[i].port = PORT_NONE;
        }
 
-       return True;
-}      
+       return true;
+}
+
 /********************************************************
  Resolve via "bcast" method.
 *********************************************************/
 
-BOOL name_resolve_bcast(const char *name, int name_type,
-                       struct ip_service **return_iplist, int *return_count)
+NTSTATUS name_resolve_bcast(const char *name,
+                       int name_type,
+                       struct ip_service **return_iplist,
+                       int *return_count)
 {
        int sock, i;
        int num_interfaces = iface_count();
-       struct in_addr *ip_list;
-       BOOL ret;
+       struct sockaddr_storage *ss_list;
+       struct sockaddr_storage ss;
+       NTSTATUS status;
 
        if (lp_disable_netbios()) {
-               DEBUG(5,("name_resolve_bcast(%s#%02x): netbios is disabled\n", name, name_type));
-               return False;
+               DEBUG(5,("name_resolve_bcast(%s#%02x): netbios is disabled\n",
+                                       name, name_type));
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
        *return_iplist = NULL;
        *return_count = 0;
-       
+
        /*
         * "bcast" means do a broadcast lookup on all the local interfaces.
         */
 
-       DEBUG(3,("name_resolve_bcast: Attempting broadcast lookup for name %s<0x%x>\n", name, name_type));
+       DEBUG(3,("name_resolve_bcast: Attempting broadcast lookup "
+               "for name %s<0x%x>\n", name, name_type));
 
-       sock = open_socket_in( SOCK_DGRAM, 0, 3,
-                              interpret_addr(lp_socket_address()), True );
+       if (!interpret_string_addr(&ss, lp_socket_address(),
+                               AI_NUMERICHOST|AI_PASSIVE)) {
+               zero_addr(&ss);
+       }
 
-       if (sock == -1) return False;
+       sock = open_socket_in( SOCK_DGRAM, 0, 3, &ss, true );
+       if (sock == -1) {
+               return NT_STATUS_UNSUCCESSFUL;
+       }
 
        set_socket_options(sock,"SO_BROADCAST");
        /*
@@ -700,56 +968,67 @@ BOOL name_resolve_bcast(const char *name, int name_type,
         * the first successful match.
         */
        for( i = num_interfaces-1; i >= 0; i--) {
-               struct in_addr sendto_ip;
+               const struct sockaddr_storage *pss = iface_n_bcast(i);
                int flags;
+
                /* Done this way to fix compiler error on IRIX 5.x */
-               sendto_ip = *iface_n_bcast(i);
-               ip_list = name_query(sock, name, name_type, True, 
-                                   True, sendto_ip, return_count, &flags, NULL);
-               if( ip_list ) 
+               if (!pss) {
+                       continue;
+               }
+               ss_list = name_query(sock, name, name_type, true,
+                                   true, pss, return_count, &flags, NULL);
+               if (ss_list) {
                        goto success;
+               }
        }
-       
+
        /* failed - no response */
-       
+
        close(sock);
-       return False;
-       
+       return NT_STATUS_UNSUCCESSFUL;
+
 success:
-       ret = True;
-       if ( !convert_ip2service(return_iplist, ip_list, *return_count) )
-               ret = False;
-       
-       SAFE_FREE( ip_list );
+
+       status = NT_STATUS_OK;
+       if (!convert_ss2service(return_iplist, ss_list, *return_count) )
+               status = NT_STATUS_INVALID_PARAMETER;
+
+       SAFE_FREE(ss_list);
        close(sock);
-       return ret;
+       return status;
 }
 
 /********************************************************
  Resolve via "wins" method.
 *********************************************************/
 
-BOOL resolve_wins(const char *name, int name_type,
-                 struct ip_service **return_iplist, int *return_count)
+NTSTATUS resolve_wins(const char *name,
+               int name_type,
+               struct ip_service **return_iplist,
+               int *return_count)
 {
        int sock, t, i;
        char **wins_tags;
-       struct in_addr src_ip, *ip_list = NULL;
-       BOOL ret;
+       struct sockaddr_storage src_ss, *ss_list = NULL;
+       struct in_addr src_ip;
+       NTSTATUS status;
 
        if (lp_disable_netbios()) {
-               DEBUG(5,("resolve_wins(%s#%02x): netbios is disabled\n", name, name_type));
-               return False;
+               DEBUG(5,("resolve_wins(%s#%02x): netbios is disabled\n",
+                                       name, name_type));
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
        *return_iplist = NULL;
        *return_count = 0;
-       
-       DEBUG(3,("resolve_wins: Attempting wins lookup for name %s<0x%x>\n", name, name_type));
+
+       DEBUG(3,("resolve_wins: Attempting wins lookup for name %s<0x%x>\n",
+                               name, name_type));
 
        if (wins_srv_count() < 1) {
-               DEBUG(3,("resolve_wins: WINS server resolution selected and no WINS servers listed.\n"));
-               return False;
+               DEBUG(3,("resolve_wins: WINS server resolution selected "
+                       "and no WINS servers listed.\n"));
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
        /* we try a lookup on each of the WINS tags in turn */
@@ -757,24 +1036,40 @@ BOOL resolve_wins(const char *name, int name_type,
 
        if (!wins_tags) {
                /* huh? no tags?? give up in disgust */
-               return False;
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
        /* the address we will be sending from */
-       src_ip = *interpret_addr2(lp_socket_address());
+       if (!interpret_string_addr(&src_ss, lp_socket_address(),
+                               AI_NUMERICHOST|AI_PASSIVE)) {
+               zero_addr(&src_ss);
+       }
+
+       if (src_ss.ss_family != AF_INET) {
+               char addr[INET6_ADDRSTRLEN];
+               print_sockaddr(addr, sizeof(addr), &src_ss);
+               DEBUG(3,("resolve_wins: cannot receive WINS replies "
+                       "on IPv6 address %s\n",
+                       addr));
+               wins_srv_tags_free(wins_tags);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       src_ip = ((struct sockaddr_in *)&src_ss)->sin_addr;
 
        /* in the worst case we will try every wins server with every
           tag! */
        for (t=0; wins_tags && wins_tags[t]; t++) {
                int srv_count = wins_srv_count_tag(wins_tags[t]);
                for (i=0; i<srv_count; i++) {
+                       struct sockaddr_storage wins_ss;
                        struct in_addr wins_ip;
                        int flags;
-                       BOOL timed_out;
+                       bool timed_out;
 
                        wins_ip = wins_srv_ip_tag(wins_tags[t], src_ip);
 
-                       if (global_in_nmbd && ismyip(wins_ip)) {
+                       if (global_in_nmbd && ismyip_v4(wins_ip)) {
                                /* yikes! we'll loop forever */
                                continue;
                        }
@@ -784,91 +1079,137 @@ BOOL resolve_wins(const char *name, int name_type,
                                continue;
                        }
 
-                       DEBUG(3,("resolve_wins: using WINS server %s and tag '%s'\n", inet_ntoa(wins_ip), wins_tags[t]));
+                       DEBUG(3,("resolve_wins: using WINS server %s "
+                               "and tag '%s'\n",
+                               inet_ntoa(wins_ip), wins_tags[t]));
 
-                       sock = open_socket_in(SOCK_DGRAM, 0, 3, src_ip.s_addr, True);
+                       sock = open_socket_in(SOCK_DGRAM, 0, 3, &src_ss, true);
                        if (sock == -1) {
                                continue;
                        }
 
-                       ip_list = name_query(sock,name,name_type, False, 
-                                                   True, wins_ip, return_count, &flags, 
-                                                   &timed_out);
-                                                   
+                       in_addr_to_sockaddr_storage(&wins_ss, wins_ip);
+                       ss_list = name_query(sock,
+                                               name,
+                                               name_type,
+                                               false,
+                                               true,
+                                               &wins_ss,
+                                               return_count,
+                                               &flags,
+                                               &timed_out);
+
                        /* exit loop if we got a list of addresses */
-                       
-                       if ( ip_list ) 
+
+                       if (ss_list)
                                goto success;
-                               
+
                        close(sock);
 
                        if (timed_out) {
-                               /* Timed out wating for WINS server to respond.  Mark it dead. */
+                               /* Timed out wating for WINS server to respond.
+                                * Mark it dead. */
                                wins_srv_died(wins_ip, src_ip);
                        } else {
                                /* The name definately isn't in this
-                                  group of WINS servers. goto the next group  */
+                                  group of WINS servers.
+                                  goto the next group  */
                                break;
                        }
                }
        }
 
        wins_srv_tags_free(wins_tags);
-       return False;
+       return NT_STATUS_NO_LOGON_SERVERS;
 
 success:
-       ret = True;
-       if ( !convert_ip2service( return_iplist, ip_list, *return_count ) )
-               ret = False;
-       
-       SAFE_FREE( ip_list );
+
+       status = NT_STATUS_OK;
+       if (!convert_ss2service(return_iplist, ss_list, *return_count))
+               status = NT_STATUS_INVALID_PARAMETER;
+
+       SAFE_FREE(ss_list);
        wins_srv_tags_free(wins_tags);
        close(sock);
-       
-       return ret;
+
+       return status;
 }
 
 /********************************************************
  Resolve via "lmhosts" method.
 *********************************************************/
 
-static BOOL resolve_lmhosts(const char *name, int name_type,
-                         struct ip_service **return_iplist, int *return_count)
+static NTSTATUS resolve_lmhosts(const char *name, int name_type,
+                               struct ip_service **return_iplist,
+                               int *return_count)
 {
        /*
         * "lmhosts" means parse the local lmhosts file.
         */
-       
+
        XFILE *fp;
-       pstring lmhost_name;
+       char *lmhost_name = NULL;
        int name_type2;
-       struct in_addr return_ip;
+       struct sockaddr_storage return_ss;
+       NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
+       TALLOC_CTX *ctx = NULL;
 
        *return_iplist = NULL;
        *return_count = 0;
 
-       DEBUG(3,("resolve_lmhosts: Attempting lmhosts lookup for name %s<0x%x>\n", name, name_type));
-
-       fp = startlmhosts(dyn_LMHOSTSFILE);
-       if(fp) {
-               while (getlmhostsent(fp, lmhost_name, &name_type2, &return_ip)) {
-                       if (strequal(name, lmhost_name) && 
-                ((name_type2 == -1) || (name_type == name_type2))
-               ) {
-                               endlmhosts(fp);
-                               if ( (*return_iplist = (struct ip_service *)malloc(sizeof(struct ip_service))) == NULL ) {
-                                       DEBUG(3,("resolve_lmhosts: malloc fail !\n"));
-                                       return False;
-                               }
-                               (*return_iplist)[0].ip   = return_ip;
-                               (*return_iplist)[0].port = PORT_NONE;
-                               *return_count = 1;
-                               return True; 
-                       }
-               }
+       DEBUG(3,("resolve_lmhosts: "
+               "Attempting lmhosts lookup for name %s<0x%x>\n",
+               name, name_type));
+
+       fp = startlmhosts(get_dyn_LMHOSTSFILE());
+
+       if ( fp == NULL )
+               return NT_STATUS_NO_SUCH_FILE;
+
+       ctx = talloc_init("resolve_lmhosts");
+       if (!ctx) {
                endlmhosts(fp);
+               return NT_STATUS_NO_MEMORY;
        }
-       return False;
+
+       while (getlmhostsent(ctx, fp, &lmhost_name, &name_type2, &return_ss)) {
+
+               if (!strequal(name, lmhost_name)) {
+                       TALLOC_FREE(lmhost_name);
+                       continue;
+               }
+
+               if ((name_type2 != -1) && (name_type != name_type2)) {
+                       TALLOC_FREE(lmhost_name);
+                       continue;
+               }
+
+               *return_iplist = SMB_REALLOC_ARRAY((*return_iplist),
+                                       struct ip_service,
+                                       (*return_count)+1);
+
+               if ((*return_iplist) == NULL) {
+                       TALLOC_FREE(ctx);
+                       endlmhosts(fp);
+                       DEBUG(3,("resolve_lmhosts: malloc fail !\n"));
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               (*return_iplist)[*return_count].ss = return_ss;
+               (*return_iplist)[*return_count].port = PORT_NONE;
+               *return_count += 1;
+
+               /* we found something */
+               status = NT_STATUS_OK;
+
+               /* Multiple names only for DC lookup */
+               if (name_type != 0x1c)
+                       break;
+       }
+
+       TALLOC_FREE(ctx);
+       endlmhosts(fp);
+       return status;
 }
 
 
@@ -876,104 +1217,199 @@ static BOOL resolve_lmhosts(const char *name, int name_type,
  Resolve via "hosts" method.
 *********************************************************/
 
-static BOOL resolve_hosts(const char *name, int name_type,
-                         struct ip_service **return_iplist, int *return_count)
+static NTSTATUS resolve_hosts(const char *name, int name_type,
+                             struct ip_service **return_iplist,
+                             int *return_count)
 {
        /*
         * "host" means do a localhost, or dns lookup.
         */
-       struct hostent *hp;
-       
+       struct addrinfo hints;
+       struct addrinfo *ailist = NULL;
+       struct addrinfo *res = NULL;
+       int ret = -1;
+       int i = 0;
+
        if ( name_type != 0x20 && name_type != 0x0) {
-               DEBUG(5, ("resolve_hosts: not appropriate for name type <0x%x>\n", name_type));
-               return False;
+               DEBUG(5, ("resolve_hosts: not appropriate "
+                       "for name type <0x%x>\n",
+                       name_type));
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
        *return_iplist = NULL;
        *return_count = 0;
 
-       DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n", name, name_type));
-       
-       if (((hp = sys_gethostbyname(name)) != NULL) && (hp->h_addr != NULL)) {
-               struct in_addr return_ip;
-               putip((char *)&return_ip,(char *)hp->h_addr);
-               *return_iplist = (struct ip_service *)malloc(sizeof(struct ip_service));
-               if(*return_iplist == NULL) {
+       DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
+                               name, name_type));
+
+       ZERO_STRUCT(hints);
+       /* By default make sure it supports TCP. */
+       hints.ai_socktype = SOCK_STREAM;
+       hints.ai_flags = AI_ADDRCONFIG;
+
+#if !defined(HAVE_IPV6)
+       /* Unless we have IPv6, we really only want IPv4 addresses back. */
+       hints.ai_family = AF_INET;
+#endif
+
+       ret = getaddrinfo(name,
+                       NULL,
+                       &hints,
+                       &ailist);
+       if (ret) {
+               DEBUG(3,("resolve_hosts: getaddrinfo failed for name %s [%s]\n",
+                       name,
+                       gai_strerror(ret) ));
+       }
+
+       for (res = ailist; res; res = res->ai_next) {
+               struct sockaddr_storage ss;
+
+               if (!res->ai_addr || res->ai_addrlen == 0) {
+                       continue;
+               }
+
+               ZERO_STRUCT(ss);
+               memcpy(&ss, res->ai_addr, res->ai_addrlen);
+
+               *return_count += 1;
+
+               *return_iplist = SMB_REALLOC_ARRAY(*return_iplist,
+                                               struct ip_service,
+                                               *return_count);
+               if (!*return_iplist) {
                        DEBUG(3,("resolve_hosts: malloc fail !\n"));
-                       return False;
+                       freeaddrinfo(ailist);
+                       return NT_STATUS_NO_MEMORY;
                }
-               (*return_iplist)->ip   = return_ip;
-               (*return_iplist)->port = PORT_NONE;
-               *return_count = 1;
-               return True;
+               (*return_iplist)[i].ss = ss;
+               (*return_iplist)[i].port = PORT_NONE;
+               i++;
        }
-       return False;
+       if (ailist) {
+               freeaddrinfo(ailist);
+       }
+       if (*return_count) {
+               return NT_STATUS_OK;
+       }
+       return NT_STATUS_UNSUCCESSFUL;
 }
 
 /********************************************************
  Resolve via "ADS" method.
 *********************************************************/
 
-static BOOL resolve_ads(const char *name, int name_type,
-                         struct ip_service **return_iplist, int *return_count)
+static NTSTATUS resolve_ads(const char *name,
+                           int name_type,
+                           const char *sitename,
+                           struct ip_service **return_iplist,
+                           int *return_count)
 {
-       
-#ifdef HAVE_ADS
-       if ( name_type == 0x1c ) {
-               int                     count, i = 0;
-               char                    *list = NULL;
-               const char              *ptr;
-               pstring                 tok;
-               
-               /* try to lookup the _ldap._tcp.<domain> if we are using ADS */
-               if ( lp_security() != SEC_ADS )
-                       return False;
-                       
-               DEBUG(5,("resolve_hosts: Attempting to resolve DC's for %s using DNS\n",
-                       name));
-                       
-               if (ldap_domain2hostlist(name, &list) != LDAP_SUCCESS)
-                       return False;
-                               
-               count = count_chars(list, ' ') + 1;
-               if ( (*return_iplist = malloc(count * sizeof(struct ip_service))) == NULL ) {
-                       DEBUG(0,("resolve_hosts: malloc failed for %d entries\n", count ));
-                       return False;
-               }
+       int                     i, j;
+       NTSTATUS                status;
+       TALLOC_CTX              *ctx;
+       struct dns_rr_srv       *dcs = NULL;
+       int                     numdcs = 0;
+       int                     numaddrs = 0;
+
+       if ((name_type != 0x1c) && (name_type != KDC_NAME_TYPE) &&
+           (name_type != 0x1b)) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-               ptr = list;
-               while (next_token(&ptr, tok, " ", sizeof(tok))) {
-                       unsigned port = LDAP_PORT;      
-                       char *p = strchr(tok, ':');
-                       if (p) {
-                               *p = 0;
-                               port = atoi(p+1);
-                       }
-                       (*return_iplist)[i].ip   = *interpret_addr2(tok);
-                       (*return_iplist)[i].port = port;
-                       
-                       /* 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 hac.  After think about it, if all of the IP addresses retuend
-                          from DNS are dead, what hope does a netbios name lookup have?
-                          The standard reason for falling back to netbios lookups is that 
-                          our DNS server doesn't know anything about the DC's   -- jerry */    
-                          
-                       if ( is_zero_ip((*return_iplist)[i].ip) )
-                               continue;
-               
+       if ( (ctx = talloc_init("resolve_ads")) == NULL ) {
+               DEBUG(0,("resolve_ads: talloc_init() failed!\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       /* The DNS code needs fixing to find IPv6 addresses... JRA. */
+
+       switch (name_type) {
+               case 0x1b:
+                       DEBUG(5,("resolve_ads: Attempting to resolve "
+                                "PDC for %s using DNS\n", name));
+                       status = ads_dns_query_pdc(ctx, name, &dcs, &numdcs);
+                       break;
+
+               case 0x1c:
+                       DEBUG(5,("resolve_ads: Attempting to resolve "
+                                "DCs for %s using DNS\n", name));
+                       status = ads_dns_query_dcs(ctx, name, sitename, &dcs,
+                                                  &numdcs);
+                       break;
+               case KDC_NAME_TYPE:
+                       DEBUG(5,("resolve_ads: Attempting to resolve "
+                                "KDCs for %s using DNS\n", name));
+                       status = ads_dns_query_kdcs(ctx, name, sitename, &dcs,
+                                                   &numdcs);
+                       break;
+               default:
+                       status = NT_STATUS_INVALID_PARAMETER;
+                       break;
+       }
+
+       if ( !NT_STATUS_IS_OK( status ) ) {
+               talloc_destroy(ctx);
+               return status;
+       }
+
+       for (i=0;i<numdcs;i++) {
+               numaddrs += MAX(dcs[i].num_ips,1);
+       }
+
+       if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, numaddrs)) ==
+                       NULL ) {
+               DEBUG(0,("resolve_ads: malloc failed for %d entries\n",
+                                       numaddrs ));
+               talloc_destroy(ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       /* now unroll the list of IP addresses */
+
+       *return_count = 0;
+       i = 0;
+       j = 0;
+       while ( i < numdcs && (*return_count<numaddrs) ) {
+               struct ip_service *r = &(*return_iplist)[*return_count];
+
+               r->port = dcs[i].port;
+
+               /* If we don't have an IP list for a name, lookup it up */
+
+               if (!dcs[i].ss_s) {
+                       interpret_string_addr(&r->ss, dcs[i].hostname, 0);
                        i++;
+                       j = 0;
+               } else {
+                       /* use the IP addresses from the SRV sresponse */
+
+                       if ( j >= dcs[i].num_ips ) {
+                               i++;
+                               j = 0;
+                               continue;
+                       }
+
+                       r->ss = dcs[i].ss_s[j];
+                       j++;
+               }
+
+               /* 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
+                * all of the IP addresses returned from DNS are dead, what
+                * hope does a netbios name lookup have ? The standard reason
+                * for falling back to netbios lookups is that our DNS server
+                * doesn't know anything about the DC's   -- jerry */
+
+               if (!is_zero_addr(&r->ss)) {
+                       (*return_count)++;
                }
-               SAFE_FREE(list);
-               
-               *return_count = i;
-                               
-               return True;
-       } else 
-#endif         /* HAVE_ADS */
-       { 
-               return False;
        }
+
+       talloc_destroy(ctx);
+       return NT_STATUS_OK;
 }
 
 /*******************************************************************
@@ -981,155 +1417,191 @@ static BOOL resolve_ads(const char *name, int name_type,
  Use this function if the string is either an IP address, DNS
  or host name or NetBIOS name. This uses the name switch in the
  smb.conf to determine the order of name resolution.
+
  Added support for ip addr/port to support ADS ldap servers.
- the only place we currently care about the port is in the 
+ the only place we currently care about the port is in the
  resolve_hosts() when looking up DC's via SRV RR entries in DNS
 **********************************************************************/
 
-static BOOL internal_resolve_name(const char *name, int name_type,
-                                 struct ip_service **return_iplist, 
-                                 int *return_count, const char *resolve_order)
+static NTSTATUS internal_resolve_name(const char *name,
+                               int name_type,
+                               const char *sitename,
+                               struct ip_service **return_iplist,
+                               int *return_count,
+                               const char *resolve_order)
 {
-  pstring name_resolve_list;
-  fstring tok;
-  const char *ptr;
-  BOOL allones = (strcmp(name,"255.255.255.255") == 0);
-  BOOL allzeros = (strcmp(name,"0.0.0.0") == 0);
-  BOOL is_address = is_ipaddress(name);
-  BOOL result = False;
-  int i;
-
-  *return_iplist = NULL;
-  *return_count = 0;
-
-  DEBUG(10, ("internal_resolve_name: looking up %s#%x\n", name, name_type));
-
-  if (allzeros || allones || is_address) {
-  
-       if ( (*return_iplist = (struct ip_service *)malloc(sizeof(struct ip_service))) == NULL ) {
-               DEBUG(0,("internal_resolve_name: malloc fail !\n"));
-               return False;
-       }
-       
-       if(is_address) { 
+       char *tok;
+       const char *ptr;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       int i;
+       TALLOC_CTX *frame = NULL;
+
+       *return_iplist = NULL;
+       *return_count = 0;
+
+       DEBUG(10, ("internal_resolve_name: looking up %s#%x (sitename %s)\n",
+                       name, name_type, sitename ? sitename : NULL));
+
+       if (is_ipaddress(name)) {
+               if ((*return_iplist = SMB_MALLOC_P(struct ip_service)) ==
+                               NULL) {
+                       DEBUG(0,("internal_resolve_name: malloc fail !\n"));
+                       return NT_STATUS_NO_MEMORY;
+               }
+
                /* ignore the port here */
                (*return_iplist)->port = PORT_NONE;
-               
+
                /* if it's in the form of an IP address then get the lib to interpret it */
-               if (((*return_iplist)->ip.s_addr = inet_addr(name)) == 0xFFFFFFFF ){
-                       DEBUG(1,("internal_resolve_name: inet_addr failed on %s\n", name));
-                       return False;
+               if (!interpret_string_addr(&(*return_iplist)->ss,
+                                       name, AI_NUMERICHOST)) {
+                       DEBUG(1,("internal_resolve_name: interpret_string_addr "
+                               "failed on %s\n",
+                               name));
+                       SAFE_FREE(*return_iplist);
+                       return NT_STATUS_INVALID_PARAMETER;
                }
+               *return_count = 1;
+               return NT_STATUS_OK;
+       }
+
+       /* Check name cache */
+
+       if (namecache_fetch(name, name_type, return_iplist, return_count)) {
+               /* This could be a negative response */
+               if (*return_count > 0) {
+                       return NT_STATUS_OK;
+               } else {
+                       return NT_STATUS_UNSUCCESSFUL;
+               }
+       }
+
+       /* set the name resolution order */
+
+       if (strcmp( resolve_order, "NULL") == 0) {
+               DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (!resolve_order[0]) {
+               ptr = "host";
        } else {
-               (*return_iplist)->ip.s_addr = allones ? 0xFFFFFFFF : 0;
-       }
-       *return_count = 1;
-       return True;
-  }
-  
-  /* Check name cache */
-
-  if (namecache_fetch(name, name_type, return_iplist, return_count)) {
-       /* This could be a negative response */
-       return (*return_count > 0);
-  }
-
-  /* set the name resolution order */
-
-  if ( strcmp( resolve_order, "NULL") == 0 ) {
-    DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
-    return False;
-  }
-  
-  if ( !resolve_order )
-    pstrcpy(name_resolve_list, lp_name_resolve_order());
-  else
-    pstrcpy(name_resolve_list, resolve_order);
-  
-  if ( !name_resolve_list[0] )
-    ptr = "host";
-  else
-    ptr = name_resolve_list;
-
-  /* iterate through the name resolution backends */
-  
-  while (next_token(&ptr, tok, LIST_SEP, sizeof(tok))) {
-         if((strequal(tok, "host") || strequal(tok, "hosts"))) {
-                 if (resolve_hosts(name, name_type, return_iplist, return_count)) {
-                         result = True;
-                         goto done;
-                 }
-         } else if(strequal( tok, "ads")) {
-                  /* deal with 0x1c names here.  This will result in a
-                    SRV record lookup for _ldap._tcp.<domain> if we
-                    are using 'security = ads' */
-                 if (resolve_ads(name, name_type, return_iplist, return_count)) {
-                   result = True;
-                   goto done;
-                 }
-         } else if(strequal( tok, "lmhosts")) {
-                 if (resolve_lmhosts(name, name_type, return_iplist, return_count)) {
-                   result = True;
-                   goto done;
-                 }
-         } else if(strequal( tok, "wins")) {
-                 /* don't resolve 1D via WINS */
-                 if (name_type != 0x1D &&
-                     resolve_wins(name, name_type, return_iplist, return_count)) {
-                   result = True;
-                   goto done;
-                 }
-         } else if(strequal( tok, "bcast")) {
-                 if (name_resolve_bcast(name, name_type, return_iplist, return_count)) {
-                   result = True;
-                   goto done;
-                 }
-         } else {
-                 DEBUG(0,("resolve_name: unknown name switch type %s\n", tok));
-         }
-  }
-
-  /* All of the resolve_* functions above have returned false. */
-
-  SAFE_FREE(*return_iplist);
-  *return_count = 0;
-
-  return False;
+               ptr = resolve_order;
+       }
 
- done:
+       /* iterate through the name resolution backends */
+
+       frame = talloc_stackframe();
+       while (next_token_talloc(frame, &ptr, &tok, LIST_SEP)) {
+               if((strequal(tok, "host") || strequal(tok, "hosts"))) {
+                       status = resolve_hosts(name, name_type, return_iplist,
+                                              return_count);
+                       if (NT_STATUS_IS_OK(status)) {
+                               goto done;
+                       }
+               } else if(strequal( tok, "kdc")) {
+                       /* deal with KDC_NAME_TYPE names here.
+                        * This will result in a SRV record lookup */
+                       status = resolve_ads(name, KDC_NAME_TYPE, sitename,
+                                            return_iplist, return_count);
+                       if (NT_STATUS_IS_OK(status)) {
+                               /* Ensure we don't namecache
+                                * this with the KDC port. */
+                               name_type = KDC_NAME_TYPE;
+                               goto done;
+                       }
+               } else if(strequal( tok, "ads")) {
+                       /* deal with 0x1c and 0x1b names here.
+                        * This will result in a SRV record lookup */
+                       status = resolve_ads(name, name_type, sitename,
+                                            return_iplist, return_count);
+                       if (NT_STATUS_IS_OK(status)) {
+                               goto done;
+                       }
+               } else if(strequal( tok, "lmhosts")) {
+                       status = resolve_lmhosts(name, name_type,
+                                                return_iplist, return_count);
+                       if (NT_STATUS_IS_OK(status)) {
+                               goto done;
+                       }
+               } else if(strequal( tok, "wins")) {
+                       /* don't resolve 1D via WINS */
+                       if (name_type != 0x1D) {
+                               status = resolve_wins(name, name_type,
+                                                     return_iplist,
+                                                     return_count);
+                               if (NT_STATUS_IS_OK(status)) {
+                                       goto done;
+                               }
+                       }
+               } else if(strequal( tok, "bcast")) {
+                       status = name_resolve_bcast(name, name_type,
+                                                   return_iplist,
+                                                   return_count);
+                       if (NT_STATUS_IS_OK(status)) {
+                               goto done;
+                       }
+               } else {
+                       DEBUG(0,("resolve_name: unknown name switch type %s\n",
+                               tok));
+               }
+       }
+
+       /* All of the resolve_* functions above have returned false. */
+
+       TALLOC_FREE(frame);
+       SAFE_FREE(*return_iplist);
+       *return_count = 0;
+
+       return NT_STATUS_UNSUCCESSFUL;
+
+  done:
+
+       /* Remove duplicate entries.  Some queries, notably #1c (domain
+       controllers) return the PDC in iplist[0] and then all domain
+       controllers including the PDC in iplist[1..n].  Iterating over
+       the iplist when the PDC is down will cause two sets of timeouts. */
+
+       if ( *return_count ) {
+               *return_count = remove_duplicate_addrs2(*return_iplist,
+                                       *return_count );
+       }
 
-  /* Remove duplicate entries.  Some queries, notably #1c (domain
-     controllers) return the PDC in iplist[0] and then all domain
-     controllers including the PDC in iplist[1..n].  Iterating over
-     the iplist when the PDC is down will cause two sets of timeouts. */
-
-  if ( *return_count ) {
-    *return_count = remove_duplicate_addrs2( *return_iplist, *return_count );
-  }
-  /* Save in name cache */
-  if ( DEBUGLEVEL >= 100 ) {
-    for (i = 0; i < *return_count && DEBUGLEVEL == 100; i++)
-      DEBUG(100, ("Storing name %s of type %d (%s:%d)\n", name,
-                name_type, inet_ntoa((*return_iplist)[i].ip), (*return_iplist)[i].port));
-  }
-   
-  namecache_store(name, name_type, *return_count, *return_iplist);
-
-  /* Display some debugging info */
-
-  if ( DEBUGLEVEL >= 10 ) {
-    DEBUG(10, ("internal_resolve_name: returning %d addresses: ", 
-              *return_count));
-
-    for (i = 0; i < *return_count; i++)
-         DEBUGADD(10, ("%s:%d ", inet_ntoa((*return_iplist)[i].ip), (*return_iplist)[i].port));
-
-    DEBUG(10, ("\n"));
-  }
-  
-  return result;
+       /* Save in name cache */
+       if ( DEBUGLEVEL >= 100 ) {
+               for (i = 0; i < *return_count && DEBUGLEVEL == 100; i++) {
+                       char addr[INET6_ADDRSTRLEN];
+                       print_sockaddr(addr, sizeof(addr),
+                                       &(*return_iplist)[i].ss);
+                       DEBUG(100, ("Storing name %s of type %d (%s:%d)\n",
+                                       name,
+                                       name_type,
+                                       addr,
+                                       (*return_iplist)[i].port));
+               }
+       }
+
+       namecache_store(name, name_type, *return_count, *return_iplist);
+
+       /* Display some debugging info */
+
+       if ( DEBUGLEVEL >= 10 ) {
+               DEBUG(10, ("internal_resolve_name: returning %d addresses: ",
+                                       *return_count));
+
+               for (i = 0; i < *return_count; i++) {
+                       char addr[INET6_ADDRSTRLEN];
+                       print_sockaddr(addr, sizeof(addr),
+                                       &(*return_iplist)[i].ss);
+                       DEBUGADD(10, ("%s:%d ",
+                                       addr,
+                                       (*return_iplist)[i].port));
+               }
+               DEBUG(10, ("\n"));
+       }
+
+       TALLOC_FREE(frame);
+       return status;
 }
 
 /********************************************************
@@ -1139,64 +1611,156 @@ static BOOL internal_resolve_name(const char *name, int name_type,
  smb.conf to determine the order of name resolution.
 *********************************************************/
 
-BOOL resolve_name(const char *name, struct in_addr *return_ip, int name_type)
+bool resolve_name(const char *name,
+               struct sockaddr_storage *return_ss,
+               int name_type)
 {
-       struct ip_service *ip_list = NULL;
+       struct ip_service *ss_list = NULL;
+       char *sitename = NULL;
        int count = 0;
 
        if (is_ipaddress(name)) {
-               *return_ip = *interpret_addr2(name);
-               return True;
+               return interpret_string_addr(return_ss, name, AI_NUMERICHOST);
        }
 
-       if (internal_resolve_name(name, name_type, &ip_list, &count, lp_name_resolve_order())) {
+       sitename = sitename_fetch(lp_realm()); /* wild guess */
+
+       if (NT_STATUS_IS_OK(internal_resolve_name(name, name_type, sitename,
+                                                 &ss_list, &count,
+                                                 lp_name_resolve_order()))) {
                int i;
-               
+
                /* only return valid addresses for TCP connections */
                for (i=0; i<count; i++) {
-                       char *ip_str = inet_ntoa(ip_list[i].ip);
-                       if (ip_str &&
-                           strcmp(ip_str, "255.255.255.255") != 0 &&
-                           strcmp(ip_str, "0.0.0.0") != 0) 
-                       {
-                               *return_ip = ip_list[i].ip;
-                               SAFE_FREE(ip_list);
+                       if (!is_zero_addr(&ss_list[i].ss) &&
+                                       !is_broadcast_addr(&ss_list[i].ss)) {
+                               *return_ss = ss_list[i].ss;
+                               SAFE_FREE(ss_list);
+                               SAFE_FREE(sitename);
                                return True;
                        }
                }
        }
-       
-       SAFE_FREE(ip_list);
+
+       SAFE_FREE(ss_list);
+       SAFE_FREE(sitename);
        return False;
 }
 
+/********************************************************
+ Internal interface to resolve a name into a list of IP addresses.
+ Use this function if the string is either an IP address, DNS
+ or host name or NetBIOS name. This uses the name switch in the
+ smb.conf to determine the order of name resolution.
+*********************************************************/
+
+NTSTATUS resolve_name_list(TALLOC_CTX *ctx,
+               const char *name,
+               int name_type,
+               struct sockaddr_storage **return_ss_arr,
+               unsigned int *p_num_entries)
+{
+       struct ip_service *ss_list = NULL;
+       char *sitename = NULL;
+       int count = 0;
+       int i;
+       unsigned int num_entries;
+       NTSTATUS status;
+
+       *p_num_entries = 0;
+       *return_ss_arr = NULL;
+
+       if (is_ipaddress(name)) {
+               *return_ss_arr = TALLOC_P(ctx, struct sockaddr_storage);
+               if (!*return_ss_arr) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               if (!interpret_string_addr(*return_ss_arr, name, AI_NUMERICHOST)) {
+                       TALLOC_FREE(*return_ss_arr);
+                       return NT_STATUS_BAD_NETWORK_NAME;
+               }
+               *p_num_entries = 1;
+               return NT_STATUS_OK;
+       }
+
+       sitename = sitename_fetch(lp_realm()); /* wild guess */
+
+       status = internal_resolve_name(name, name_type, sitename,
+                                                 &ss_list, &count,
+                                                 lp_name_resolve_order());
+       SAFE_FREE(sitename);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       /* only return valid addresses for TCP connections */
+       for (i=0, num_entries = 0; i<count; i++) {
+               if (!is_zero_addr(&ss_list[i].ss) &&
+                               !is_broadcast_addr(&ss_list[i].ss)) {
+                       num_entries++;
+               }
+       }
+       if (num_entries == 0) {
+               SAFE_FREE(ss_list);
+               return NT_STATUS_BAD_NETWORK_NAME;
+       }
+
+       *return_ss_arr = TALLOC_ARRAY(ctx,
+                               struct sockaddr_storage,
+                               num_entries);
+       if (!(*return_ss_arr)) {
+               SAFE_FREE(ss_list);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       for (i=0, num_entries = 0; i<count; i++) {
+               if (!is_zero_addr(&ss_list[i].ss) &&
+                               !is_broadcast_addr(&ss_list[i].ss)) {
+                       (*return_ss_arr)[num_entries++] = ss_list[i].ss;
+               }
+       }
+
+       status = NT_STATUS_OK;
+       *p_num_entries = num_entries;
+
+       SAFE_FREE(ss_list);
+       return NT_STATUS_OK;
+}
+
 /********************************************************
  Find the IP address of the master browser or DMB for a workgroup.
 *********************************************************/
 
-BOOL find_master_ip(const char *group, struct in_addr *master_ip)
+bool find_master_ip(const char *group, struct sockaddr_storage *master_ss)
 {
        struct ip_service *ip_list = NULL;
        int count = 0;
+       NTSTATUS status;
 
        if (lp_disable_netbios()) {
                DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
-               return False;
+               return false;
        }
 
-       if (internal_resolve_name(group, 0x1D, &ip_list, &count, lp_name_resolve_order())) {
-               *master_ip = ip_list[0].ip;
+       status = internal_resolve_name(group, 0x1D, NULL, &ip_list, &count,
+                                      lp_name_resolve_order());
+       if (NT_STATUS_IS_OK(status)) {
+               *master_ss = ip_list[0].ss;
                SAFE_FREE(ip_list);
-               return True;
+               return true;
        }
-       if(internal_resolve_name(group, 0x1B, &ip_list, &count, lp_name_resolve_order())) {
-               *master_ip = ip_list[0].ip;
+
+       status = internal_resolve_name(group, 0x1B, NULL, &ip_list, &count,
+                                      lp_name_resolve_order());
+       if (NT_STATUS_IS_OK(status)) {
+               *master_ss = ip_list[0].ss;
                SAFE_FREE(ip_list);
-               return True;
+               return true;
        }
 
        SAFE_FREE(ip_list);
-       return False;
+       return false;
 }
 
 /********************************************************
@@ -1204,60 +1768,78 @@ BOOL find_master_ip(const char *group, struct in_addr *master_ip)
  for a domain.
 *********************************************************/
 
-BOOL get_pdc_ip(const char *domain, struct in_addr *ip)
+bool get_pdc_ip(const char *domain, struct sockaddr_storage *pss)
 {
-       struct ip_service *ip_list;
-       int count;
+       struct ip_service *ip_list = NULL;
+       int count = 0;
+       NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
 
        /* Look up #1B name */
 
-       if (!internal_resolve_name(domain, 0x1b, &ip_list, &count, lp_name_resolve_order()))
-               return False;
+       if (lp_security() == SEC_ADS) {
+               status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
+                                              &count, "ads");
+       }
+
+       if (!NT_STATUS_IS_OK(status) || count == 0) {
+               status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
+                                              &count,
+                                              lp_name_resolve_order());
+               if (!NT_STATUS_IS_OK(status)) {
+                       return false;
+               }
+       }
 
        /* if we get more than 1 IP back we have to assume it is a
           multi-homed PDC and not a mess up */
 
        if ( count > 1 ) {
-               DEBUG(6,("get_pdc_ip: PDC has %d IP addresses!\n", count));             
-               sort_ip_list2( ip_list, count );
+               DEBUG(6,("get_pdc_ip: PDC has %d IP addresses!\n", count));
+               sort_service_list(ip_list, count);
        }
 
-       *ip = ip_list[0].ip;
-       
+       *pss = ip_list[0].ss;
        SAFE_FREE(ip_list);
-
-       return True;
+       return true;
 }
 
-/*********************************************************************
- small wrapper function to get the DC list and sort it if neccessary 
-*********************************************************************/
-BOOL get_sorted_dc_list( const char *domain, struct ip_service **ip_list, int *count, BOOL ads_only )
-{
-       BOOL ordered;
-       
-       DEBUG(8,("get_sorted_dc_list: attempting lookup using [%s]\n",
-               (ads_only ? "ads" : lp_name_resolve_order())));
-       
-       if ( !get_dc_list(domain, ip_list, count, ads_only, &ordered) )
-               return False;
-               
-       /* only sort if we don't already have an ordered list */
-       if ( !ordered )
-               sort_ip_list2( *ip_list, *count );
-               
-       return True;
-}
+/* Private enum type for lookups. */
+
+enum dc_lookup_type { DC_NORMAL_LOOKUP, DC_ADS_ONLY, DC_KDC_ONLY };
 
 /********************************************************
  Get the IP address list of the domain controllers for
  a domain.
 *********************************************************/
 
-BOOL get_dc_list(const char *domain, struct ip_service **ip_list, 
-                 int *count, BOOL ads_only, int *ordered)
+static NTSTATUS get_dc_list(const char *domain,
+                       const char *sitename,
+                       struct ip_service **ip_list,
+                       int *count,
+                       enum dc_lookup_type lookup_type,
+                       bool *ordered)
 {
-       fstring resolve_order;
+       char *resolve_order = NULL;
+       char *saf_servername = NULL;
+       char *pserver = NULL;
+       const char *p;
+       char *port_str = NULL;
+       int port;
+       char *name;
+       int num_addresses = 0;
+       int  local_count, i, j;
+       struct ip_service *return_iplist = NULL;
+       struct ip_service *auto_ip_list = NULL;
+       bool done_auto_lookup = false;
+       int auto_count = 0;
+       NTSTATUS status;
+       TALLOC_CTX *ctx = talloc_init("get_dc_list");
+
+       if (!ctx) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       *ordered = False;
 
        /* if we are restricted to solely using DNS for looking
           up a domain controller, make sure that host lookups
@@ -1265,141 +1847,283 @@ BOOL get_dc_list(const char *domain, struct ip_service **ip_list,
           are disabled and ads_only is True, then set the string to
           NULL. */
 
-       fstrcpy( resolve_order, lp_name_resolve_order() );
-       strlower_m( resolve_order );
-       if ( ads_only )  {
-               if ( strstr( resolve_order, "host" ) )
-                       fstrcpy( resolve_order, "ads" );
-               else
-                       fstrcpy( resolve_order, "NULL" );
+       resolve_order = talloc_strdup(ctx, lp_name_resolve_order());
+       if (!resolve_order) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
+       }
+       strlower_m(resolve_order);
+       if (lookup_type == DC_ADS_ONLY)  {
+               if (strstr( resolve_order, "host")) {
+                       resolve_order = talloc_strdup(ctx, "ads");
+
+                       /* DNS SRV lookups used by the ads resolver
+                          are already sorted by priority and weight */
+                       *ordered = true;
+               } else {
+                        resolve_order = talloc_strdup(ctx, "NULL");
+               }
+       } else if (lookup_type == DC_KDC_ONLY) {
+               /* DNS SRV lookups used by the ads/kdc resolver
+                  are already sorted by priority and weight */
+               *ordered = true;
+               resolve_order = talloc_strdup(ctx, "kdc");
+       }
+       if (!resolve_order) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
        }
 
-       
-       *ordered = False;
-               
-       /* If it's our domain then use the 'password server' parameter. */
-
-       if ( strequal(domain, lp_workgroup()) || strequal(domain, lp_realm()) ) {
-               const char *p;
-               char *pserver = lp_passwordserver(); /* UNIX charset. */
-               char *port_str;
-               int port;
-               fstring name;
-               int num_addresses = 0;
-               int  local_count, i, j;
-               struct ip_service *return_iplist = NULL;
-               struct ip_service *auto_ip_list = NULL;
-               BOOL done_auto_lookup = False;
-               int auto_count = 0;
-               
-
-               if (!*pserver)
-                       return internal_resolve_name(domain, 0x1C, ip_list, count, resolve_order);
-
-               p = pserver;
-
-               /*
-                * if '*' appears in the "password server" list then add
-                * an auto lookup to the list of manually configured
-                * DC's.  If any DC is listed by name, then the list should be 
-                * considered to be ordered 
-                */
-                
-               while (next_token(&p,name,LIST_SEP,sizeof(name))) {
-                       if (strequal(name, "*")) {
-                               if ( internal_resolve_name(domain, 0x1C, &auto_ip_list, &auto_count, resolve_order) )
-                                       num_addresses += auto_count;
-                               done_auto_lookup = True;
-                               DEBUG(8,("Adding %d DC's from auto lookup\n", auto_count));
+       /* fetch the server we have affinity for.  Add the
+          'password server' list to a search for our domain controllers */
+
+       saf_servername = saf_fetch( domain);
+
+       if (strequal(domain, lp_workgroup()) || strequal(domain, lp_realm())) {
+               pserver = talloc_asprintf(NULL, "%s, %s",
+                       saf_servername ? saf_servername : "",
+                       lp_passwordserver());
+       } else {
+               pserver = talloc_asprintf(NULL, "%s, *",
+                       saf_servername ? saf_servername : "");
+       }
+
+       SAFE_FREE(saf_servername);
+       if (!pserver) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
+       }
+
+       /* if we are starting from scratch, just lookup DOMAIN<0x1c> */
+
+       if (!*pserver ) {
+               DEBUG(10,("get_dc_list: no preferred domain controllers.\n"));
+               status = internal_resolve_name(domain, 0x1C, sitename, ip_list,
+                                            count, resolve_order);
+               goto out;
+       }
+
+       DEBUG(3,("get_dc_list: preferred server list: \"%s\"\n", pserver ));
+
+       /*
+        * if '*' appears in the "password server" list then add
+        * an auto lookup to the list of manually configured
+        * DC's.  If any DC is listed by name, then the list should be
+        * considered to be ordered
+        */
+
+       p = pserver;
+       while (next_token_talloc(ctx, &p, &name, LIST_SEP)) {
+               if (strequal(name, "*")) {
+                       status = internal_resolve_name(domain, 0x1C, sitename,
+                                                      &auto_ip_list,
+                                                      &auto_count,
+                                                      resolve_order);
+                       if (NT_STATUS_IS_OK(status)) {
+                               num_addresses += auto_count;
                        }
-                       else 
-                               num_addresses++;
+                       done_auto_lookup = true;
+                       DEBUG(8,("Adding %d DC's from auto lookup\n",
+                                               auto_count));
+               } else  {
+                       num_addresses++;
                }
+       }
 
-               /* if we have no addresses and haven't done the auto lookup, then
-                  just return the list of DC's */
-                  
-               if ( (num_addresses == 0) && !done_auto_lookup )
-                       return internal_resolve_name(domain, 0x1C, ip_list, count, resolve_order);
+       /* if we have no addresses and haven't done the auto lookup, then
+          just return the list of DC's.  Or maybe we just failed. */
 
-               /* maybe we just failed? */
-               
-               if ( num_addresses == 0 ) {
+       if ((num_addresses == 0)) {
+               if (done_auto_lookup) {
                        DEBUG(4,("get_dc_list: no servers found\n"));
-                       return False;
-               }
-               
-               if ( (return_iplist = (struct ip_service *)
-                       malloc(num_addresses * sizeof(struct ip_service))) == NULL ) 
-               {
-                       DEBUG(3,("get_dc_list: malloc fail !\n"));
-                       return False;
+                       status = NT_STATUS_NO_LOGON_SERVERS;
+                       goto out;
                }
+               status = internal_resolve_name(domain, 0x1C, sitename, ip_list,
+                                            count, resolve_order);
+               goto out;
+       }
 
-               p = pserver;
-               local_count = 0;
-
-               /* fill in the return list now with real IP's */
-                               
-               while ( (local_count<num_addresses) && next_token(&p,name,LIST_SEP,sizeof(name)) ) {
-                       struct in_addr name_ip;
-                       
-                       /* copy any addersses from the auto lookup */
-                       
-                       if ( strequal(name, "*") ) {
-                               for ( j=0; j<auto_count; j++ ) {
-                                       return_iplist[local_count].ip   = auto_ip_list[j].ip;
-                                       return_iplist[local_count].port = auto_ip_list[j].port;
-                                       local_count++;
+       if ((return_iplist = SMB_MALLOC_ARRAY(struct ip_service,
+                                       num_addresses)) == NULL) {
+               DEBUG(3,("get_dc_list: malloc fail !\n"));
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
+       }
+
+       p = pserver;
+       local_count = 0;
+
+       /* fill in the return list now with real IP's */
+
+       while ((local_count<num_addresses) &&
+                       next_token_talloc(ctx, &p, &name, LIST_SEP)) {
+               struct sockaddr_storage name_ss;
+
+               /* copy any addersses from the auto lookup */
+
+               if (strequal(name, "*")) {
+                       for (j=0; j<auto_count; j++) {
+                               char addr[INET6_ADDRSTRLEN];
+                               print_sockaddr(addr,
+                                               sizeof(addr),
+                                               &auto_ip_list[j].ss);
+                               /* Check for and don't copy any
+                                * known bad DC IP's. */
+                               if(!NT_STATUS_IS_OK(check_negative_conn_cache(
+                                               domain,
+                                               addr))) {
+                                       DEBUG(5,("get_dc_list: "
+                                               "negative entry %s removed "
+                                               "from DC list\n",
+                                               addr));
+                                       continue;
                                }
-                               continue;
-                       }
-                       
-                       
-                       /* added support for address:port syntax for ads (not that I think 
-                          anyone will ever run the LDAP server in an AD domain on something 
-                          other than port 389 */
-                       
-                       port = (lp_security() == SEC_ADS) ? LDAP_PORT : PORT_NONE;
-                       if ( (port_str=strchr(name, ':')) != NULL ) {
-                               *port_str = '\0';
-                               port_str++;
-                               port = atoi( port_str );
+                               return_iplist[local_count].ss =
+                                       auto_ip_list[j].ss;
+                               return_iplist[local_count].port =
+                                       auto_ip_list[j].port;
+                               local_count++;
                        }
+                       continue;
+               }
 
-                       /* explicit lookup; resolve_name() will handle names & IP addresses */
-                       if ( resolve_name( name, &name_ip, 0x20 ) ) {
-                               return_iplist[local_count].ip   = name_ip;
-                               return_iplist[local_count].port = port;
-                               local_count++;
-                               *ordered = True;
+               /* added support for address:port syntax for ads
+                * (not that I think anyone will ever run the LDAP
+                * server in an AD domain on something other than
+                * port 389 */
+
+               port = (lp_security() == SEC_ADS) ? LDAP_PORT : PORT_NONE;
+               if ((port_str=strchr(name, ':')) != NULL) {
+                       *port_str = '\0';
+                       port_str++;
+                       port = atoi(port_str);
+               }
+
+               /* explicit lookup; resolve_name() will
+                * handle names & IP addresses */
+               if (resolve_name( name, &name_ss, 0x20 )) {
+                       char addr[INET6_ADDRSTRLEN];
+                       print_sockaddr(addr,
+                                       sizeof(addr),
+                                       &name_ss);
+
+                       /* Check for and don't copy any known bad DC IP's. */
+                       if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain,
+                                                       addr)) ) {
+                               DEBUG(5,("get_dc_list: negative entry %s "
+                                       "removed from DC list\n",
+                                       name ));
+                               continue;
                        }
+
+                       return_iplist[local_count].ss = name_ss;
+                       return_iplist[local_count].port = port;
+                       local_count++;
+                       *ordered = true;
                }
-                               
-               SAFE_FREE(auto_ip_list);
-
-               /* need to remove duplicates in the list if we have any 
-                  explicit password servers */
-                  
-               if ( local_count )
-                       local_count = remove_duplicate_addrs2( return_iplist, local_count );
-               
-               if ( DEBUGLEVEL >= 4 ) {
-                       DEBUG(4,("get_dc_list: returning %d ip addresses in an %sordered list\n", local_count, 
+       }
+
+       /* need to remove duplicates in the list if we have any
+          explicit password servers */
+
+       if (local_count) {
+               local_count = remove_duplicate_addrs2(return_iplist,
+                               local_count );
+       }
+
+       if ( DEBUGLEVEL >= 4 ) {
+               DEBUG(4,("get_dc_list: returning %d ip addresses "
+                               "in an %sordered list\n",
+                               local_count,
                                *ordered ? "":"un"));
-                       DEBUG(4,("get_dc_list: "));
-                       for ( i=0; i<local_count; i++ )
-                               DEBUGADD(4,("%s:%d ", inet_ntoa(return_iplist[i].ip), return_iplist[i].port ));
-                       DEBUGADD(4,("\n"));
+               DEBUG(4,("get_dc_list: "));
+               for ( i=0; i<local_count; i++ ) {
+                       char addr[INET6_ADDRSTRLEN];
+                       print_sockaddr(addr,
+                                       sizeof(addr),
+                                       &return_iplist[i].ss);
+                       DEBUGADD(4,("%s:%d ", addr, return_iplist[i].port ));
                }
-                       
-               *ip_list = return_iplist;
-               *count = local_count;
+               DEBUGADD(4,("\n"));
+       }
+
+       *ip_list = return_iplist;
+       *count = local_count;
+
+       status = ( *count != 0 ? NT_STATUS_OK : NT_STATUS_NO_LOGON_SERVERS );
+
+  out:
 
-               return (*count != 0);
+       SAFE_FREE(auto_ip_list);
+       TALLOC_FREE(ctx);
+       return status;
+}
+
+/*********************************************************************
+ Small wrapper function to get the DC list and sort it if neccessary.
+*********************************************************************/
+
+NTSTATUS get_sorted_dc_list( const char *domain,
+                       const char *sitename,
+                       struct ip_service **ip_list,
+                       int *count,
+                       bool ads_only )
+{
+       bool ordered;
+       NTSTATUS status;
+       enum dc_lookup_type lookup_type = DC_NORMAL_LOOKUP;
+
+       DEBUG(8,("get_sorted_dc_list: attempting lookup "
+               "for name %s (sitename %s) using [%s]\n",
+               domain,
+               sitename ? sitename : "NULL",
+               (ads_only ? "ads" : lp_name_resolve_order())));
+
+       if (ads_only) {
+               lookup_type = DC_ADS_ONLY;
+       }
+
+       status = get_dc_list(domain, sitename, ip_list,
+                       count, lookup_type, &ordered);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       /* only sort if we don't already have an ordered list */
+       if (!ordered) {
+               sort_service_list(*ip_list, *count);
+       }
+
+       return NT_STATUS_OK;
+}
+
+/*********************************************************************
+ Get the KDC list - re-use all the logic in get_dc_list.
+*********************************************************************/
+
+NTSTATUS get_kdc_list( const char *realm,
+                       const char *sitename,
+                       struct ip_service **ip_list,
+                       int *count)
+{
+       bool ordered;
+       NTSTATUS status;
+
+       *count = 0;
+       *ip_list = NULL;
+
+       status = get_dc_list(realm, sitename, ip_list,
+                       count, DC_KDC_ONLY, &ordered);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
-       
-       DEBUG(10,("get_dc_list: defaulting to internal auto lookup for domain %s\n", domain));
-       
-       return internal_resolve_name(domain, 0x1C, ip_list, count, resolve_order);
+
+       /* only sort if we don't already have an ordered list */
+       if ( !ordered ) {
+               sort_service_list(*ip_list, *count);
+       }
+
+       return NT_STATUS_OK;
 }