s3-talloc Change TALLOC_ZERO_P() to talloc_zero()
[nivanova/samba-autobuild/.git] / source3 / libsmb / dsgetdcname.c
index de020d4791e753420f68b763ef9472516bbef4e2..c6add7ecdd4502b33992f15a7070ea73705a5b60 100644 (file)
 */
 
 #include "includes.h"
+#include "libads/sitename_cache.h"
+#include "../librpc/gen_ndr/ndr_netlogon.h"
+#include "libads/cldap.h"
+#include "libads/dns.h"
+#include "libsmb/clidgram.h"
 
-#define DSGETDCNAME_FMT        "DSGETDCNAME/DOMAIN/%s"
 /* 15 minutes */
 #define DSGETDCNAME_CACHE_TTL  60*15
 
 struct ip_service_name {
        struct sockaddr_storage ss;
-       unsigned port;
        const char *hostname;
 };
 
@@ -122,7 +125,8 @@ static char *dsgetdcname_cache_key(TALLOC_CTX *mem_ctx, const char *domain)
                return NULL;
        }
 
-       return talloc_asprintf_strupper_m(mem_ctx, DSGETDCNAME_FMT, domain);
+       return talloc_asprintf_strupper_m(mem_ctx, "DSGETDCNAME/DOMAIN/%s",
+                                         domain);
 }
 
 /****************************************************************
@@ -189,19 +193,19 @@ static NTSTATUS store_cldap_reply(TALLOC_CTX *mem_ctx,
        r->sockaddr.sockaddr_family = 2; /* AF_INET */
        r->sockaddr.pdc_ip = talloc_strdup(mem_ctx, addr);
 
-       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, r,
+       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, r,
                       (ndr_push_flags_fn_t)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                return ndr_map_error2ntstatus(ndr_err);
        }
 
-       if (r->domain) {
-               status = dsgetdcname_cache_store(mem_ctx, r->domain, &blob);
+       if (r->domain_name) {
+               status = dsgetdcname_cache_store(mem_ctx, r->domain_name, &blob);
                if (!NT_STATUS_IS_OK(status)) {
                        goto done;
                }
                if (r->client_site) {
-                       sitename_store(r->domain, r->client_site);
+                       sitename_store(r->domain_name, r->client_site);
                }
        }
        if (r->dns_domain) {
@@ -225,28 +229,6 @@ static NTSTATUS store_cldap_reply(TALLOC_CTX *mem_ctx,
 /****************************************************************
 ****************************************************************/
 
-static NTSTATUS dsgetdcname_cache_refresh(TALLOC_CTX *mem_ctx,
-                                         struct messaging_context *msg_ctx,
-                                         const char *domain_name,
-                                         struct GUID *domain_guid,
-                                         uint32_t flags,
-                                         const char *site_name,
-                                         struct netr_DsRGetDCNameInfo *info)
-{
-       struct netr_DsRGetDCNameInfo *dc_info;
-
-       return dsgetdcname(mem_ctx,
-                          msg_ctx,
-                          domain_name,
-                          domain_guid,
-                          site_name,
-                          flags | DS_FORCE_REDISCOVERY,
-                          &dc_info);
-}
-
-/****************************************************************
-****************************************************************/
-
 static uint32_t get_cldap_reply_server_flags(struct netlogon_samlogon_response *r,
                                             uint32_t nt_version)
 {
@@ -336,7 +318,7 @@ static bool check_cldap_reply_required_flags(uint32_t ret_flags,
 
 static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
                                        const char *domain_name,
-                                       struct GUID *domain_guid,
+                                       const struct GUID *domain_guid,
                                        uint32_t flags,
                                        const char *site_name,
                                        struct netr_DsRGetDCNameInfo **info_p)
@@ -353,16 +335,16 @@ static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
                return NT_STATUS_NO_MEMORY;
        }
 
-       if (!gencache_get_data_blob(key, &blob, NULL)) {
+       if (!gencache_get_data_blob(key, &blob, NULL, NULL)) {
                return NT_STATUS_NOT_FOUND;
        }
 
-       info = TALLOC_ZERO_P(mem_ctx, struct netr_DsRGetDCNameInfo);
+       info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
        if (!info) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, NULL, &r,
+       ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
                      (ndr_pull_flags_fn_t)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX);
 
        data_blob_free(&blob);
@@ -403,7 +385,7 @@ static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
 static NTSTATUS dsgetdcname_cached(TALLOC_CTX *mem_ctx,
                                   struct messaging_context *msg_ctx,
                                   const char *domain_name,
-                                  struct GUID *domain_guid,
+                                  const struct GUID *domain_guid,
                                   uint32_t flags,
                                   const char *site_name,
                                   struct netr_DsRGetDCNameInfo **info)
@@ -424,13 +406,18 @@ static NTSTATUS dsgetdcname_cached(TALLOC_CTX *mem_ctx,
        }
 
        if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
-               status = dsgetdcname_cache_refresh(mem_ctx, msg_ctx,
-                                                  domain_name,
-                                                  domain_guid, flags,
-                                                  site_name, *info);
+               struct netr_DsRGetDCNameInfo *dc_info;
+
+               status = dsgetdcname(mem_ctx, msg_ctx, domain_name,
+                                    domain_guid, site_name,
+                                    flags | DS_FORCE_REDISCOVERY,
+                                    &dc_info);
+
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
+
+               *info = dc_info;
        }
 
        return status;
@@ -513,6 +500,7 @@ static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
 
        dclist = TALLOC_ZERO_ARRAY(mem_ctx, struct ip_service_name, count);
        if (!dclist) {
+               SAFE_FREE(iplist);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -525,9 +513,9 @@ static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
                               &iplist[i].ss);
 
                r->ss   = iplist[i].ss;
-               r->port = iplist[i].port;
                r->hostname = talloc_strdup(mem_ctx, addr);
                if (!r->hostname) {
+                       SAFE_FREE(iplist);
                        return NT_STATUS_NO_MEMORY;
                }
 
@@ -535,6 +523,7 @@ static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
 
        *returned_dclist = dclist;
        *returned_count = count;
+       SAFE_FREE(iplist);
 
        return NT_STATUS_OK;
 }
@@ -544,7 +533,7 @@ static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
                                const char *domain_name,
-                               struct GUID *domain_guid,
+                               const struct GUID *domain_guid,
                                uint32_t flags,
                                const char *site_name,
                                struct ip_service_name **returned_dclist,
@@ -607,7 +596,6 @@ static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
 
                struct ip_service_name *r = &dclist[count];
 
-               r->port = dcs[i].port;
                r->hostname = dcs[i].hostname;
 
                /* If we don't have an IP list for a name, lookup it up */
@@ -618,7 +606,7 @@ static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
                        i++;
                        j = 0;
                } else {
-                       /* use the IP addresses from the SRV sresponse */
+                       /* use the IP addresses from the SRV response */
 
                        if (j >= dcs[i].num_ips) {
                                i++;
@@ -632,13 +620,13 @@ static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
 
                /* 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
+                * 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((struct sockaddr *)(void *)&r->ss)) {
+               if (!is_zero_addr(&r->ss)) {
                        count++;
                        continue;
                }
@@ -671,7 +659,7 @@ static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
 {
        struct netr_DsRGetDCNameInfo *info;
 
-       info = TALLOC_ZERO_P(mem_ctx, struct netr_DsRGetDCNameInfo);
+       info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
        NT_STATUS_HAVE_NO_MEMORY(info);
 
        if (dc_unc) {
@@ -703,7 +691,7 @@ static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
        if (forest_name && *forest_name) {
                info->forest_name = talloc_strdup(mem_ctx, forest_name);
                NT_STATUS_HAVE_NO_MEMORY(info->forest_name);
-               flags |= DS_DNS_FOREST;
+               flags |= DS_DNS_FOREST_ROOT;
        }
 
        info->dc_flags = flags;
@@ -799,7 +787,7 @@ static NTSTATUS make_dc_info_from_cldap_reply(TALLOC_CTX *mem_ctx,
 
        map_dc_and_domain_names(flags,
                                r->pdc_name,
-                               r->domain,
+                               r->domain_name,
                                r->pdc_dns_name,
                                r->dns_domain,
                                &dc_flags,
@@ -873,9 +861,10 @@ static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
 
        for (i=0; i<num_dcs; i++) {
 
+
                DEBUG(10,("LDAP ping to %s\n", dclist[i].hostname));
 
-               if (ads_cldap_netlogon(mem_ctx, dclist[i].hostname,
+               if (ads_cldap_netlogon(mem_ctx, &dclist[i].ss,
                                        domain_name,
                                        nt_version,
                                        &r))
@@ -909,30 +898,6 @@ static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
 /****************************************************************
 ****************************************************************/
 
-static struct event_context *ev_context(void)
-{
-       static struct event_context *ctx;
-
-       if (!ctx && !(ctx = event_context_init(NULL))) {
-               smb_panic("Could not init event context");
-       }
-       return ctx;
-}
-
-/****************************************************************
-****************************************************************/
-
-static struct messaging_context *msg_context(TALLOC_CTX *mem_ctx)
-{
-       static struct messaging_context *ctx;
-
-       if (!ctx && !(ctx = messaging_init(mem_ctx, server_id_self(),
-                                          ev_context()))) {
-               smb_panic("Could not init messaging context");
-       }
-       return ctx;
-}
-
 /****************************************************************
 ****************************************************************/
 
@@ -957,8 +922,8 @@ static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
                              NETLOGON_NT_VERSION_5 |
                              NETLOGON_NT_VERSION_5EX_WITH_IP;
 
-       if (!msg_ctx) {
-               msg_ctx = msg_context(mem_ctx);
+       if (msg_ctx == NULL) {
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
        if (flags & DS_PDC_REQUIRED) {
@@ -970,6 +935,9 @@ static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
        DEBUG(10,("process_dc_netbios\n"));
 
        for (i=0; i<num_dcs; i++) {
+               uint16_t val;
+
+               generate_random_buffer((uint8_t *)&val, 2);
 
                ip_list.ss = dclist[i].ss;
                ip_list.port = 0;
@@ -978,25 +946,13 @@ static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
                        return NT_STATUS_UNSUCCESSFUL;
                }
 
-               if (send_getdc_request(mem_ctx, msg_ctx,
-                                      &dclist[i].ss, domain_name,
-                                      NULL, nt_version))
-               {
-                       int k;
-                       smb_msleep(300);
-                       for (k=0; k<5; k++) {
-                               if (receive_getdc_response(mem_ctx,
-                                                          &dclist[i].ss,
-                                                          domain_name,
-                                                          &nt_version,
-                                                          &dc_name,
-                                                          &r)) {
-                                       store_cache = true;
-                                       namecache_store(dc_name, NBT_NAME_SERVER, 1, &ip_list);
-                                       goto make_reply;
-                               }
-                               smb_msleep(1500);
-                       }
+               status = nbt_getdc(msg_ctx, &dclist[i].ss, domain_name,
+                                  NULL, nt_version,
+                                  mem_ctx, &nt_version, &dc_name, &r);
+               if (NT_STATUS_IS_OK(status)) {
+                       store_cache = true;
+                       namecache_store(dc_name, NBT_NAME_SERVER, 1, &ip_list);
+                       goto make_reply;
                }
 
                if (name_status_find(domain_name,
@@ -1007,7 +963,7 @@ static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
                {
                        struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1;
 
-                       r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
+                       r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
                        NT_STATUS_HAVE_NO_MEMORY(r);
 
                        ZERO_STRUCT(logon1);
@@ -1015,9 +971,9 @@ static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
                        nt_version = NETLOGON_NT_VERSION_1;
 
                        logon1.nt_version = nt_version;
-                       logon1.server = tmp_dc_name;
-                       logon1.domain = talloc_strdup_upper(mem_ctx, domain_name);
-                       NT_STATUS_HAVE_NO_MEMORY(logon1.domain);
+                       logon1.pdc_name = tmp_dc_name;
+                       logon1.domain_name = talloc_strdup_upper(mem_ctx, domain_name);
+                       NT_STATUS_HAVE_NO_MEMORY(logon1.domain_name);
 
                        r->data.nt4 = logon1;
                        r->ntver = nt_version;
@@ -1050,7 +1006,7 @@ static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
                                       struct messaging_context *msg_ctx,
                                       const char *domain_name,
-                                      struct GUID *domain_guid,
+                                      const struct GUID *domain_guid,
                                       uint32_t flags,
                                       const char *site_name,
                                       struct netr_DsRGetDCNameInfo **info)
@@ -1131,7 +1087,7 @@ static bool is_closest_site(struct netr_DsRGetDCNameInfo *info)
 NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
                     struct messaging_context *msg_ctx,
                     const char *domain_name,
-                    struct GUID *domain_guid,
+                    const struct GUID *domain_guid,
                     const char *site_name,
                     uint32_t flags,
                     struct netr_DsRGetDCNameInfo **info)
@@ -1155,7 +1111,7 @@ NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       if (!site_name) {
+       if ((site_name == NULL) || (site_name[0] == '\0')) {
                query_site = sitename_fetch(domain_name);
        } else {
                query_site = SMB_STRDUP(site_name);