s3-talloc Change TALLOC_ARRAY() to talloc_array()
[samba.git] / source3 / libads / dns.c
index 5cf768de67241decbfe4f3ac42c81f35b5ae11d6..c1332849bff6bc5988327b844c1880cf87acea60 100644 (file)
@@ -19,6 +19,8 @@
 */
 
 #include "includes.h"
+#include "libads/dns.h"
+#include "../librpc/ndr/libndr.h"
 
 /* AIX resolv.h uses 'class' in struct ns_rr */
 
@@ -296,7 +298,7 @@ static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
        int resp_len = NS_PACKETSZ;
        static time_t last_dns_check = 0;
        static NTSTATUS last_dns_status = NT_STATUS_OK;
-       time_t now = time(NULL);
+       time_t now = time_mono(NULL);
 
        /* Try to prevent bursts of DNS lookups if the server is down */
 
@@ -326,12 +328,12 @@ static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
                buf_len = resp_len * sizeof(uint8);
 
                if (buf_len) {
-                       if ((buffer = TALLOC_ARRAY(ctx, uint8, buf_len))
+                       if ((buffer = talloc_array(ctx, uint8, buf_len))
                                        == NULL ) {
                                DEBUG(0,("ads_dns_lookup_srv: "
                                        "talloc() failed!\n"));
                                last_dns_status = NT_STATUS_NO_MEMORY;
-                               last_dns_check = time(NULL);
+                               last_dns_check = time_mono(NULL);
                                return last_dns_status;
                        }
                }
@@ -350,7 +352,7 @@ static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
                        if (errno == ECONNREFUSED) {
                                last_dns_status = NT_STATUS_CONNECTION_REFUSED;
                        }
-                       last_dns_check = time(NULL);
+                       last_dns_check = time_mono(NULL);
                        return last_dns_status;
                }
 
@@ -364,7 +366,7 @@ static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
                                        name));
                                TALLOC_FREE( buffer );
                                last_dns_status = NT_STATUS_BUFFER_TOO_SMALL;
-                               last_dns_check = time(NULL);
+                               last_dns_check = time_mono(NULL);
                                return last_dns_status;
                        }
 
@@ -377,7 +379,7 @@ static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
        *buf = buffer;
        *resp_length = resp_len;
 
-       last_dns_check = time(NULL);
+       last_dns_check = time_mono(NULL);
        last_dns_status = NT_STATUS_OK;
        return last_dns_status;
 }
@@ -399,11 +401,19 @@ static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx,
        int rrnum;
        int idx = 0;
        NTSTATUS status;
+       const char *dns_hosts_file;
 
        if ( !ctx || !name || !dclist ) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       dns_hosts_file = lp_parm_const_string(-1, "resolv", "host file", NULL);
+       if (dns_hosts_file) {
+               return resolve_dns_hosts_file_as_dns_rr(dns_hosts_file,
+                                                       name, true, ctx,
+                                                       dclist, numdcs);
+       }
+
        /* Send the request.  May have to loop several times in case
           of large replies */
 
@@ -506,9 +516,10 @@ static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx,
 
                if (rr.type != T_A || rr.rdatalen != 4) {
 #if defined(HAVE_IPV6)
-                       /* FIXME. RFC2874 defines A6 records. This
+                       /* RFC2874 defines A6 records. This
                         * requires recusive and horribly complex lookups.
                         * Bastards. Ignore this for now.... JRA.
+                        * Luckily RFC3363 reprecates A6 records.
                         */
                        if (rr.type != T_AAAA || rr.rdatalen != 16)
 #endif
@@ -523,13 +534,13 @@ static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx,
                                /* allocate new memory */
 
                                if (dcs[i].num_ips == 0) {
-                                       if ((dcs[i].ss_s = TALLOC_ARRAY(dcs,
+                                       if ((dcs[i].ss_s = talloc_array(dcs,
                                                struct sockaddr_storage, 1 ))
                                                        == NULL ) {
                                                return NT_STATUS_NO_MEMORY;
                                        }
                                } else {
-                                       if ((tmp_ss_s = TALLOC_REALLOC_ARRAY(dcs,
+                                       if ((tmp_ss_s = talloc_realloc(dcs,
                                                        dcs[i].ss_s,
                                                        struct sockaddr_storage,
                                                        dcs[i].num_ips+1))
@@ -562,7 +573,7 @@ static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx,
                }
        }
 
-       qsort( dcs, idx, sizeof(struct dns_rr_srv), QSORT_CAST dnssrvcmp );
+       TYPESAFE_QSORT(dcs, idx, dnssrvcmp );
 
        *dclist = dcs;
        *numdcs = idx;
@@ -587,11 +598,18 @@ NTSTATUS ads_dns_lookup_ns(TALLOC_CTX *ctx,
        int rrnum;
        int idx = 0;
        NTSTATUS status;
+       const char *dns_hosts_file;
 
        if ( !ctx || !dnsdomain || !nslist ) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       dns_hosts_file = lp_parm_const_string(-1, "resolv", "host file", NULL);
+       if (dns_hosts_file) {
+               DEBUG(1, ("NO 'NS' lookup available when using resolv:host file"));
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
        /* Send the request.  May have to loop several times in case
           of large replies */
 
@@ -620,7 +638,7 @@ NTSTATUS ads_dns_lookup_ns(TALLOC_CTX *ctx,
                answer_count));
 
        if (answer_count) {
-               if ((nsarray = TALLOC_ARRAY(ctx, struct dns_rr_ns,
+               if ((nsarray = talloc_array(ctx, struct dns_rr_ns,
                                                answer_count)) == NULL ) {
                        DEBUG(0,("ads_dns_lookup_ns: "
                                "talloc() failure for %d char*'s\n",
@@ -725,119 +743,6 @@ NTSTATUS ads_dns_lookup_ns(TALLOC_CTX *ctx,
        return NT_STATUS_OK;
 }
 
-/****************************************************************************
- Store and fetch the AD client sitename.
-****************************************************************************/
-
-#define SITENAME_KEY   "AD_SITENAME/DOMAIN/%s"
-
-static char *sitename_key(const char *realm)
-{
-       char *keystr;
-
-       if (asprintf_strupper_m(&keystr, SITENAME_KEY, realm) == -1) {
-               return NULL;
-       }
-
-       return keystr;
-}
-
-
-/****************************************************************************
- Store the AD client sitename.
- We store indefinately as every new CLDAP query will re-write this.
-****************************************************************************/
-
-bool sitename_store(const char *realm, const char *sitename)
-{
-       time_t expire;
-       bool ret = False;
-       char *key;
-
-       if (!realm || (strlen(realm) == 0)) {
-               DEBUG(0,("sitename_store: no realm\n"));
-               return False;
-       }
-
-       key = sitename_key(realm);
-
-       if (!sitename || (sitename && !*sitename)) {
-               DEBUG(5,("sitename_store: deleting empty sitename!\n"));
-               ret = gencache_del(key);
-               SAFE_FREE(key);
-               return ret;
-       }
-
-       expire = get_time_t_max(); /* Store indefinately. */
-
-       DEBUG(10,("sitename_store: realm = [%s], sitename = [%s], expire = [%u]\n",
-               realm, sitename, (unsigned int)expire ));
-
-       ret = gencache_set( key, sitename, expire );
-       SAFE_FREE(key);
-       return ret;
-}
-
-/****************************************************************************
- Fetch the AD client sitename.
- Caller must free.
-****************************************************************************/
-
-char *sitename_fetch(const char *realm)
-{
-       char *sitename = NULL;
-       time_t timeout;
-       bool ret = False;
-       const char *query_realm;
-       char *key;
-
-       if (!realm || (strlen(realm) == 0)) {
-               query_realm = lp_realm();
-       } else {
-               query_realm = realm;
-       }
-
-       key = sitename_key(query_realm);
-
-       ret = gencache_get( key, &sitename, &timeout );
-       SAFE_FREE(key);
-       if ( !ret ) {
-               DEBUG(5,("sitename_fetch: No stored sitename for %s\n",
-                       query_realm));
-       } else {
-               DEBUG(5,("sitename_fetch: Returning sitename for %s: \"%s\"\n",
-                       query_realm, sitename ));
-       }
-       return sitename;
-}
-
-/****************************************************************************
- Did the sitename change ?
-****************************************************************************/
-
-bool stored_sitename_changed(const char *realm, const char *sitename)
-{
-       bool ret = False;
-
-       char *new_sitename;
-
-       if (!realm || (strlen(realm) == 0)) {
-               DEBUG(0,("stored_sitename_changed: no realm\n"));
-               return False;
-       }
-
-       new_sitename = sitename_fetch(realm);
-
-       if (sitename && new_sitename && !strequal(sitename, new_sitename)) {
-               ret = True;
-       } else if ((sitename && !new_sitename) ||
-                       (!sitename && new_sitename)) {
-               ret = True;
-       }
-       SAFE_FREE(new_sitename);
-       return ret;
-}
-
 /********************************************************************
  Query with optional sitename.
 ********************************************************************/
@@ -985,7 +890,7 @@ NTSTATUS ads_dns_query_dcs_guid(TALLOC_CTX *ctx,
        /*_ldap._tcp.DomainGuid.domains._msdcs.DnsForestName */
 
        const char *domains;
-       const char *guid_string;
+       char *guid_string;
 
        guid_string = GUID_string(ctx, domain_guid);
        if (!guid_string) {
@@ -997,6 +902,7 @@ NTSTATUS ads_dns_query_dcs_guid(TALLOC_CTX *ctx,
        if (!domains) {
                return NT_STATUS_NO_MEMORY;
        }
+       TALLOC_FREE(guid_string);
 
        return ads_dns_query_internal(ctx, "_ldap", domains, dns_forest_name,
                                      NULL, dclist, numdcs);