*/
#include "includes.h"
+#include "libsmb/dsgetdcname.h"
+#include "libsmb/namequery.h"
#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"
/* 15 minutes */
struct ip_service_name {
struct sockaddr_storage ss;
- unsigned port;
const char *hostname;
};
-static NTSTATUS make_dc_info_from_cldap_reply(TALLOC_CTX *mem_ctx,
- uint32_t flags,
- struct sockaddr_storage *ss,
- struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
- struct netr_DsRGetDCNameInfo **info);
+static NTSTATUS make_dc_info_from_cldap_reply(
+ TALLOC_CTX *mem_ctx,
+ uint32_t flags,
+ const struct sockaddr_storage *ss,
+ struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
+ struct netr_DsRGetDCNameInfo **info);
/****************************************************************
****************************************************************/
-void debug_dsdcinfo_flags(int lvl, uint32_t flags)
+static void debug_dsdcinfo_flags(int lvl, uint32_t flags)
{
DEBUG(lvl,("debug_dsdcinfo_flags: 0x%08x\n\t", flags));
static NTSTATUS dsgetdcname_cache_store(TALLOC_CTX *mem_ctx,
const char *domain_name,
- const DATA_BLOB *blob)
+ DATA_BLOB blob)
{
time_t expire_time;
char *key;
}
if (r->domain_name) {
- status = dsgetdcname_cache_store(mem_ctx, r->domain_name, &blob);
+ status = dsgetdcname_cache_store(mem_ctx, r->domain_name,
+ blob);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
}
}
if (r->dns_domain) {
- status = dsgetdcname_cache_store(mem_ctx, r->dns_domain, &blob);
+ status = dsgetdcname_cache_store(mem_ctx, r->dns_domain, blob);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
/****************************************************************
****************************************************************/
-#define RETURN_ON_FALSE(x) if (!(x)) return false;
-
-static bool check_cldap_reply_required_flags(uint32_t ret_flags,
- uint32_t req_flags)
-{
- if (ret_flags == 0) {
- return true;
- }
-
- if (req_flags & DS_PDC_REQUIRED)
- RETURN_ON_FALSE(ret_flags & NBT_SERVER_PDC);
-
- if (req_flags & DS_GC_SERVER_REQUIRED)
- RETURN_ON_FALSE(ret_flags & NBT_SERVER_GC);
-
- if (req_flags & DS_ONLY_LDAP_NEEDED)
- RETURN_ON_FALSE(ret_flags & NBT_SERVER_LDAP);
-
- if ((req_flags & DS_DIRECTORY_SERVICE_REQUIRED) ||
- (req_flags & DS_DIRECTORY_SERVICE_PREFERRED))
- RETURN_ON_FALSE(ret_flags & NBT_SERVER_DS);
-
- if (req_flags & DS_KDC_REQUIRED)
- RETURN_ON_FALSE(ret_flags & NBT_SERVER_KDC);
-
- if (req_flags & DS_TIMESERV_REQUIRED)
- RETURN_ON_FALSE(ret_flags & NBT_SERVER_TIMESERV);
-
- if (req_flags & DS_WRITABLE_REQUIRED)
- RETURN_ON_FALSE(ret_flags & NBT_SERVER_WRITABLE);
-
- return true;
-}
-
-/****************************************************************
-****************************************************************/
-
static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
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;
- if (lp_disable_netbios()) {
- return NT_STATUS_NOT_SUPPORTED;
- }
-
if (flags & DS_PDC_REQUIRED) {
name_type = NBT_NAME_PDC;
}
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);
struct ip_service_name **returned_dclist,
int *return_count)
{
- int i, j;
+ int i;
+ size_t j;
NTSTATUS status;
struct dns_rr_srv *dcs = NULL;
int numdcs = 0;
int count = 0;
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);
+ struct GUID_txt_buf buf;
+ GUID_buf_string(domain_guid, &buf);
+
+ status = ads_dns_query_dcs_guid(mem_ctx,
+ domain_name,
+ buf.buf,
+ &dcs,
+ &numdcs);
} 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) {
*domain_p = domain_name;
break;
}
+
+ FALL_THROUGH;
case DS_RETURN_DNS_NAME:
default:
if (dns_dc_name && dns_domain_name &&
/****************************************************************
****************************************************************/
-static NTSTATUS make_dc_info_from_cldap_reply(TALLOC_CTX *mem_ctx,
- uint32_t flags,
- struct sockaddr_storage *ss,
- struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
- struct netr_DsRGetDCNameInfo **info)
+static NTSTATUS make_dc_info_from_cldap_reply(
+ TALLOC_CTX *mem_ctx,
+ uint32_t flags,
+ const struct sockaddr_storage *ss,
+ struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
+ struct netr_DsRGetDCNameInfo **info)
{
const char *dc_hostname = NULL;
const char *dc_domain_name = NULL;
print_sockaddr(addr, sizeof(addr), ss);
dc_address = addr;
dc_address_type = DS_ADDRESS_TYPE_INET;
- }
-
- if (!ss && r->sockaddr.pdc_ip) {
- dc_address = r->sockaddr.pdc_ip;
- dc_address_type = DS_ADDRESS_TYPE_INET;
} else {
- dc_address = r->pdc_name;
- dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
+ if (r->sockaddr.pdc_ip) {
+ dc_address = r->sockaddr.pdc_ip;
+ dc_address_type = DS_ADDRESS_TYPE_INET;
+ } else {
+ dc_address = r->pdc_name;
+ dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
+ }
}
map_dc_and_domain_names(flags,
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);
- if (ads_cldap_netlogon(mem_ctx, dclist[i].hostname,
+ DEBUG(10,("LDAP ping to %s (%s)\n", dclist[i].hostname, addr));
+
+ if (ads_cldap_netlogon(mem_ctx, &dclist[i].ss,
domain_name,
nt_version,
&r))
uint32_t nt_version = NETLOGON_NT_VERSION_1 |
NETLOGON_NT_VERSION_5 |
NETLOGON_NT_VERSION_5EX_WITH_IP;
+ size_t len = strlen(lp_netbios_name());
+ char my_acct_name[len+2];
if (msg_ctx == NULL) {
return NT_STATUS_INVALID_PARAMETER;
nt_version |= map_ds_flags_to_nt_version(flags);
+ snprintf(my_acct_name,
+ sizeof(my_acct_name),
+ "%s$",
+ lp_netbios_name());
+
DEBUG(10,("process_dc_netbios\n"));
for (i=0; i<num_dcs; i++) {
uint16_t val;
- int dgm_id;
generate_random_buffer((uint8_t *)&val, 2);
- dgm_id = val;
ip_list.ss = dclist[i].ss;
ip_list.port = 0;
return NT_STATUS_UNSUCCESSFUL;
}
- status = nbt_getdc(msg_ctx, &dclist[i].ss, domain_name,
- NULL, nt_version,
+ status = nbt_getdc(msg_ctx, 10, &dclist[i].ss, domain_name,
+ NULL, my_acct_name, ACB_WSTRUST, nt_version,
mem_ctx, &nt_version, &dc_name, &r);
if (NT_STATUS_IS_OK(status)) {
store_cache = true;
{
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);
if (flags & DS_IS_FLAT_NAME) {
+ if (lp_disable_netbios()) {
+ return NT_STATUS_NOT_SUPPORTED;
+ }
+
status = discover_dc_netbios(mem_ctx, domain_name, flags,
&dclist, &num_dcs);
NT_STATUS_NOT_OK_RETURN(status);
}
}
+ if (lp_disable_netbios()) {
+ return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
+ }
+
status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
&num_dcs);
NT_STATUS_NOT_OK_RETURN(status);
}
/********************************************************************
- 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;
+}