diff --git a/source3/libsmb/dsgetdcname.c b/source3/libsmb/dsgetdcname.c index 442f8ed..c22159d 100644 --- a/source3/libsmb/dsgetdcname.c +++ b/source3/libsmb/dsgetdcname.c @@ -1083,12 +1083,9 @@ static bool is_closest_site(struct netr_DsRGetDCNameInfo *info) } /******************************************************************** - dsgetdcname. - - This will be the only public function here. ********************************************************************/ -NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx, +static NTSTATUS dsgetdcname_internal(TALLOC_CTX *mem_ctx, struct messaging_context *msg_ctx, const char *domain_name, const struct GUID *domain_guid, @@ -1098,15 +1095,15 @@ NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx, { NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; struct netr_DsRGetDCNameInfo *myinfo = NULL; - char *query_site = NULL; bool first = true; struct netr_DsRGetDCNameInfo *first_info = NULL; - DEBUG(10,("dsgetdcname: domain_name: %s, " - "domain_guid: %s, site_name: %s, flags: 0x%08x\n", - domain_name, - domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)", - site_name, flags)); + DEBUG(10,("dsgetdcname_internal: domain_name: %s, " + "domain_guid: %s, site_name: %s, flags: 0x%08x\n", + domain_name, + domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)", + site_name ? site_name : "(null)", + flags)); *info = NULL; @@ -1115,18 +1112,12 @@ NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx, return NT_STATUS_INVALID_PARAMETER; } - if ((site_name == NULL) || (site_name[0] == '\0')) { - query_site = sitename_fetch(domain_name); - } else { - query_site = SMB_STRDUP(site_name); - } - if (flags & DS_FORCE_REDISCOVERY) { goto rediscover; } status = dsgetdcname_cached(mem_ctx, msg_ctx, domain_name, domain_guid, - flags, query_site, &myinfo); + flags, site_name, &myinfo); if (NT_STATUS_IS_OK(status)) { goto done; } @@ -1137,12 +1128,10 @@ NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx, rediscover: status = dsgetdcname_rediscover(mem_ctx, msg_ctx, domain_name, - domain_guid, flags, query_site, + domain_guid, flags, site_name, &myinfo); done: - SAFE_FREE(query_site); - if (!NT_STATUS_IS_OK(status)) { if (!first) { *info = first_info; @@ -1157,10 +1146,63 @@ NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx, first = false; first_info = myinfo; /* TODO: may use the next_closest_site here */ - query_site = SMB_STRDUP(myinfo->client_site_name); + site_name = myinfo->client_site_name; goto rediscover; } *info = myinfo; return NT_STATUS_OK; } + +/******************************************************************** + dsgetdcname. + + This will be the only public function here. +********************************************************************/ + +NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx, + struct messaging_context *msg_ctx, + const char *domain_name, + const struct GUID *domain_guid, + const char *site_name, + uint32_t flags, + struct netr_DsRGetDCNameInfo **info) +{ + NTSTATUS status; + const char *query_site = NULL; + char *ptr_to_free = NULL; + + if ((site_name == NULL) || (site_name[0] == '\0')) { + ptr_to_free = sitename_fetch(domain_name); + query_site = ptr_to_free; + } else { + query_site = site_name; + } + + status = dsgetdcname_internal(mem_ctx, + msg_ctx, + domain_name, + domain_guid, + query_site, + flags, + info); + + SAFE_FREE(ptr_to_free); + + if (!NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) { + return status; + } + + /* Should we try again with site_name == NULL ? */ + if ((site_name == NULL) || (site_name[0] == '\0')) { + status = dsgetdcname_internal(mem_ctx, + msg_ctx, + domain_name, + domain_guid, + NULL, + flags, + info); + } + + return status; +}