#include "libads/sitename_cache.h"
#include "../librpc/gen_ndr/ndr_netlogon.h"
#include "libads/cldap.h"
-#include "libads/dns.h"
+#include "../lib/addns/dnsquery.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;
};
return NULL;
}
- return talloc_asprintf_strupper_m(mem_ctx, DSGETDCNAME_FMT, domain);
+ return talloc_asprintf_strupper_m(mem_ctx, "DSGETDCNAME/DOMAIN/%s",
+ domain);
}
/****************************************************************
const char *domain_name,
const struct GUID *domain_guid,
uint32_t flags,
- const char *site_name,
struct netr_DsRGetDCNameInfo **info_p)
{
char *key;
return NT_STATUS_NO_MEMORY;
}
- if (!gencache_get_data_blob(key, &blob, NULL, NULL)) {
+ if (!gencache_get_data_blob(key, NULL, &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) {
+ data_blob_free(&blob);
return NT_STATUS_NO_MEMORY;
}
NTSTATUS status;
status = dsgetdcname_cache_fetch(mem_ctx, domain_name, domain_guid,
- flags, site_name, info);
+ flags, info);
if (!NT_STATUS_IS_OK(status)
&& !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
int i;
struct ip_service_name *dclist = NULL;
int count;
+ static const char *resolve_order[] = { "lmhosts", "wins", "bcast", NULL };
*returned_dclist = NULL;
*returned_count = 0;
status = internal_resolve_name(domain_name, name_type, NULL,
&iplist, &count,
- "lmhosts wins bcast");
+ resolve_order);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
return status;
}
- dclist = TALLOC_ZERO_ARRAY(mem_ctx, struct ip_service_name, count);
+ dclist = talloc_zero_array(mem_ctx, struct ip_service_name, count);
if (!dclist) {
SAFE_FREE(iplist);
return NT_STATUS_NO_MEMORY;
&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);
int numaddrs = 0;
struct ip_service_name *dclist = NULL;
int count = 0;
+ char *guid_string;
if (flags & DS_PDC_REQUIRED) {
- status = ads_dns_query_pdc(mem_ctx, domain_name,
- &dcs, &numdcs);
+ status = ads_dns_query_pdc(mem_ctx,
+ domain_name,
+ &dcs,
+ &numdcs);
} else if (flags & DS_GC_SERVER_REQUIRED) {
- status = ads_dns_query_gcs(mem_ctx, domain_name, site_name,
- &dcs, &numdcs);
+ status = ads_dns_query_gcs(mem_ctx,
+ domain_name,
+ site_name,
+ &dcs,
+ &numdcs);
} else if (flags & DS_KDC_REQUIRED) {
- status = ads_dns_query_kdcs(mem_ctx, domain_name, site_name,
- &dcs, &numdcs);
+ status = ads_dns_query_kdcs(mem_ctx,
+ domain_name,
+ site_name,
+ &dcs,
+ &numdcs);
} else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
- status = ads_dns_query_dcs(mem_ctx, domain_name, site_name,
- &dcs, &numdcs);
+ status = ads_dns_query_dcs(mem_ctx,
+ domain_name,
+ site_name,
+ &dcs,
+ &numdcs);
} else if (domain_guid) {
- status = ads_dns_query_dcs_guid(mem_ctx, domain_name,
- domain_guid, &dcs, &numdcs);
+ guid_string = GUID_string(mem_ctx, domain_guid);
+ if (!guid_string) {
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ status = ads_dns_query_dcs_guid(mem_ctx,
+ domain_name,
+ guid_string,
+ &dcs,
+ &numdcs);
+ TALLOC_FREE(guid_string);
} else {
- status = ads_dns_query_dcs(mem_ctx, domain_name, site_name,
- &dcs, &numdcs);
+ status = ads_dns_query_dcs(mem_ctx,
+ domain_name,
+ site_name,
+ &dcs,
+ &numdcs);
}
if (!NT_STATUS_IS_OK(status)) {
numaddrs += MAX(dcs[i].num_ips,1);
}
- dclist = TALLOC_ZERO_ARRAY(mem_ctx,
+ dclist = talloc_zero_array(mem_ctx,
struct ip_service_name,
numaddrs);
if (!dclist) {
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 */
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++;
/* 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;
}
{
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) {
for (i=0; i<num_dcs; i++) {
- DEBUG(10,("LDAP ping to %s\n", dclist[i].hostname));
+ char addr[INET6_ADDRSTRLEN];
+ print_sockaddr(addr, sizeof(addr), &dclist[i].ss);
+
+ DEBUG(10,("LDAP ping to %s (%s)\n", dclist[i].hostname, addr));
- if (ads_cldap_netlogon(mem_ctx, dclist[i].hostname,
+ if (ads_cldap_netlogon(mem_ctx, &dclist[i].ss,
domain_name,
nt_version,
&r))
/****************************************************************
****************************************************************/
-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, procid_self(),
- ev_context()))) {
- smb_panic("Could not init messaging context");
- }
- return 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) {
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;
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, 10, &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,
{
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);
}
/********************************************************************
- dsgetdcname.
-
- This will be the only public function here.
+ Internal dsgetdcname.
********************************************************************/
-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,
{
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, "
+ 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, flags));
+ site_name ? site_name : "(null)", flags));
*info = NULL;
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;
}
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;
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;
+ bool retry_query_with_null = false;
+
+ if ((site_name == NULL) || (site_name[0] == '\0')) {
+ ptr_to_free = sitename_fetch(mem_ctx, domain_name);
+ if (ptr_to_free != NULL) {
+ retry_query_with_null = true;
+ }
+ 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);
+
+ TALLOC_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 (retry_query_with_null) {
+ status = dsgetdcname_internal(mem_ctx,
+ msg_ctx,
+ domain_name,
+ domain_guid,
+ NULL,
+ flags,
+ info);
+ }
+
+ return status;
+}