diff --git a/source/Makefile.in b/source/Makefile.in index 149b7445a8c..4e67f80c17f 100644 --- a/source/Makefile.in +++ b/source/Makefile.in @@ -485,7 +485,7 @@ PLAINTEXT_AUTH_OBJ = auth/pampass.o auth/pass_check.o SLCACHE_OBJ = libsmb/samlogon_cache.o -DCUTIL_OBJ = libsmb/namequery_dc.o libsmb/trustdom_cache.o libsmb/trusts_util.o +DCUTIL_OBJ = libsmb/namequery_dc.o libsmb/trustdom_cache.o libsmb/trusts_util.o libsmb/dsgetdcname.o AUTH_BUILTIN_OBJ = auth/auth_builtin.o AUTH_DOMAIN_OBJ = auth/auth_domain.o @@ -608,6 +608,7 @@ PDBEDIT_OBJ = utils/pdbedit.o $(PASSWD_UTIL_OBJ) $(PARAM_OBJ) $(PASSDB_OBJ) \ SMBGET_OBJ = utils/smbget.o $(POPT_LIB_OBJ) $(LIBSMBCLIENT_OBJ) DISPLAY_SEC_OBJ= lib/display_sec.o +DISPLAY_DSDCINFO_OBJ= lib/display_dsdcinfo.o RPCCLIENT_OBJ1 = rpcclient/rpcclient.o rpcclient/cmd_lsarpc.o \ rpcclient/cmd_samr.o rpcclient/cmd_spoolss.o \ @@ -616,7 +617,7 @@ RPCCLIENT_OBJ1 = rpcclient/rpcclient.o rpcclient/cmd_lsarpc.o \ rpcclient/cmd_ds.o rpcclient/cmd_echo.o \ rpcclient/cmd_shutdown.o rpcclient/cmd_test.o \ rpcclient/cmd_unixinfo.o \ - $(DISPLAY_SEC_OBJ) + $(DISPLAY_SEC_OBJ) $(DISPLAY_DSDCINFO_OBJ) RPCCLIENT_OBJ = $(RPCCLIENT_OBJ1) \ $(PARAM_OBJ) $(LIBSMB_OBJ) $(LIB_NONSMBD_OBJ) \ @@ -708,7 +709,7 @@ NET_OBJ = $(NET_OBJ1) $(PARAM_WITHOUT_REG_OBJ) $(SECRETS_OBJ) $(LIBSMB_OBJ) \ $(SMBLDAP_OBJ) $(DCUTIL_OBJ) $(SERVER_MUTEX_OBJ) \ $(AFS_OBJ) $(AFS_SETTOKEN_OBJ) $(REGFIO_OBJ) $(READLINE_OBJ) \ $(LDB_OBJ) $(LIBGPO_OBJ) @BUILD_INIPARSER@ $(DISPLAY_SEC_OBJ) \ - $(REG_API_OBJ) + $(REG_API_OBJ) $(DISPLAY_DSDCINFO_OBJ) CUPS_OBJ = client/smbspool.o $(PARAM_OBJ) $(LIBSMB_OBJ) \ $(LIB_NONSMBD_OBJ) $(KRBCLIENT_OBJ) $(SECRETS_OBJ) @@ -829,7 +830,7 @@ PROTO_OBJ = $(SMBD_OBJ_MAIN) $(LIBNDR_OBJ) $(LIBNDR_GEN_OBJ) \ $(IDMAP_OBJ) libsmb/spnego.o $(PASSCHANGE_OBJ) $(RPC_UNIXINFO_OBJ) \ $(RPC_NTSVCS_OBJ) $(RPC_INITSHUTDOWN_OBJ) \ utils/passwd_util.o $(LIBGPO_OBJ) $(NSS_INFO_OBJ) \ - $(RPC_EPMAPPER_OBJ) + $(RPC_EPMAPPER_OBJ) $(DISPLAY_DSDCINFO_OBJ) WINBIND_WINS_NSS_OBJ = nsswitch/wins.o $(PARAM_OBJ) \ $(LIBSMB_OBJ) $(LIB_NONSMBD_OBJ) $(NSSWINS_OBJ) $(KRBCLIENT_OBJ) $(SECRETS_OBJ) diff --git a/source/lib/display_dsdcinfo.c b/source/lib/display_dsdcinfo.c new file mode 100644 index 00000000000..dcb05297a18 --- /dev/null +++ b/source/lib/display_dsdcinfo.c @@ -0,0 +1,72 @@ +/* + Unix SMB/CIFS implementation. + + Copyright (C) Guenther Deschner 2007 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "includes.h" + +/**************************************************************** +****************************************************************/ + +void display_ds_domain_controller_info(TALLOC_CTX *mem_ctx, + const struct DS_DOMAIN_CONTROLLER_INFO *info) +{ + d_printf("domain_controller_name: %s\n", + info->domain_controller_name); + d_printf("domain_controller_address: %s\n", + info->domain_controller_address); + d_printf("domain_controller_address_type: %d\n", + info->domain_controller_address_type); + d_printf("domain_guid: %s\n", + GUID_string(mem_ctx, info->domain_guid)); + d_printf("domain_name: %s\n", + info->domain_name); + d_printf("dns_forest_name: %s\n", + info->dns_forest_name); + + d_printf("flags: 0x%08x\n" + "\tIs a PDC: %s\n" + "\tIs a GC of the forest: %s\n" + "\tIs an LDAP server: %s\n" + "\tSupports DS: %s\n" + "\tIs running a KDC: %s\n" + "\tIs running time services: %s\n" + "\tIs the closest DC: %s\n" + "\tIs writable: %s\n" + "\tHas a hardware clock: %s\n" + "\tIs a non-domain NC serviced by LDAP server: %s\n" + "\tDomainControllerName is a DNS name: %s\n" + "\tDomainName is a DNS name: %s\n" + "\tDnsForestName is a DNS name: %s\n", + info->flags, + (info->flags & ADS_PDC) ? "yes" : "no", + (info->flags & ADS_GC) ? "yes" : "no", + (info->flags & ADS_LDAP) ? "yes" : "no", + (info->flags & ADS_DS) ? "yes" : "no", + (info->flags & ADS_KDC) ? "yes" : "no", + (info->flags & ADS_TIMESERV) ? "yes" : "no", + (info->flags & ADS_CLOSEST) ? "yes" : "no", + (info->flags & ADS_WRITABLE) ? "yes" : "no", + (info->flags & ADS_GOOD_TIMESERV) ? "yes" : "no", + (info->flags & ADS_NDNC) ? "yes" : "no", + (info->flags & ADS_DNS_CONTROLLER) ? "yes":"no", + (info->flags & ADS_DNS_DOMAIN) ? "yes":"no", + (info->flags & ADS_DNS_FOREST) ? "yes":"no"); + + d_printf("dc_site_name: %s\n", info->dc_site_name); + d_printf("client_site_name: %s\n", info->client_site_name); +} diff --git a/source/libsmb/dsgetdcname.c b/source/libsmb/dsgetdcname.c new file mode 100644 index 00000000000..6f4961b3c3c --- /dev/null +++ b/source/libsmb/dsgetdcname.c @@ -0,0 +1,955 @@ +/* + Unix SMB/CIFS implementation. + + DsGetDcname + + Copyright (C) Gerald Carter 2006 + Copyright (C) Guenther Deschner 2007 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "includes.h" + +#define DSGETDCNAME_FMT "DSGETDCNAME/DOMAIN/%s" +/* 15 minutes */ +#define DSGETDCNAME_CACHE_TTL 60*15 + +struct ip_service_name { + struct in_addr ip; + unsigned port; + const char *hostname; +}; + +/**************************************************************** +****************************************************************/ + +void debug_dsdcinfo_flags(int lvl, uint32_t flags) +{ + DEBUG(lvl,("debug_dsdcinfo_flags: 0x%08x\n\t", flags)); + + if (flags & DS_FORCE_REDISCOVERY) + DEBUGADD(lvl,("DS_FORCE_REDISCOVERY ")); + if (flags & 0x000000002) + DEBUGADD(lvl,("0x00000002 ")); + if (flags & 0x000000004) + DEBUGADD(lvl,("0x00000004 ")); + if (flags & 0x000000008) + DEBUGADD(lvl,("0x00000008 ")); + if (flags & DS_DIRECTORY_SERVICE_REQUIRED) + DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_REQUIRED ")); + if (flags & DS_DIRECTORY_SERVICE_PREFERRED) + DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_PREFERRED ")); + if (flags & DS_GC_SERVER_REQUIRED) + DEBUGADD(lvl,("DS_GC_SERVER_REQUIRED ")); + if (flags & DS_PDC_REQUIRED) + DEBUGADD(lvl,("DS_PDC_REQUIRED ")); + if (flags & DS_BACKGROUND_ONLY) + DEBUGADD(lvl,("DS_BACKGROUND_ONLY ")); + if (flags & DS_IP_REQUIRED) + DEBUGADD(lvl,("DS_IP_REQUIRED ")); + if (flags & DS_KDC_REQUIRED) + DEBUGADD(lvl,("DS_KDC_REQUIRED ")); + if (flags & DS_TIMESERV_REQUIRED) + DEBUGADD(lvl,("DS_TIMESERV_REQUIRED ")); + if (flags & DS_WRITABLE_REQUIRED) + DEBUGADD(lvl,("DS_WRITABLE_REQUIRED ")); + if (flags & DS_GOOD_TIMESERV_PREFERRED) + DEBUGADD(lvl,("DS_GOOD_TIMESERV_PREFERRED ")); + if (flags & DS_AVOID_SELF) + DEBUGADD(lvl,("DS_AVOID_SELF ")); + if (flags & DS_ONLY_LDAP_NEEDED) + DEBUGADD(lvl,("DS_ONLY_LDAP_NEEDED ")); + if (flags & 0x00010000) + DEBUGADD(lvl,("0x00010000 ")); + if (flags & 0x00020000) + DEBUGADD(lvl,("0x00020000 ")); + if (flags & 0x00040000) + DEBUGADD(lvl,("0x00040000 ")); + if (flags & 0x00080000) + DEBUGADD(lvl,("0x00080000 ")); + if (flags & DS_IS_FLAT_NAME) + DEBUGADD(lvl,("DS_IS_FLAT_NAME ")); + if (flags & DS_IS_DNS_NAME) + DEBUGADD(lvl,("DS_IS_DNS_NAME ")); + if (flags & 0x00040000) + DEBUGADD(lvl,("0x00040000 ")); + if (flags & 0x00080000) + DEBUGADD(lvl,("0x00080000 ")); + if (flags & 0x00100000) + DEBUGADD(lvl,("0x00100000 ")); + if (flags & 0x00200000) + DEBUGADD(lvl,("0x00200000 ")); + if (flags & 0x00400000) + DEBUGADD(lvl,("0x00400000 ")); + if (flags & 0x00800000) + DEBUGADD(lvl,("0x00800000 ")); + if (flags & 0x01000000) + DEBUGADD(lvl,("0x01000000 ")); + if (flags & 0x02000000) + DEBUGADD(lvl,("0x02000000 ")); + if (flags & 0x04000000) + DEBUGADD(lvl,("0x04000000 ")); + if (flags & 0x08000000) + DEBUGADD(lvl,("0x08000000 ")); + if (flags & 0x10000000) + DEBUGADD(lvl,("0x10000000 ")); + if (flags & 0x20000000) + DEBUGADD(lvl,("0x20000000 ")); + if (flags & DS_RETURN_DNS_NAME) + DEBUGADD(lvl,("DS_RETURN_DNS_NAME ")); + if (flags & DS_RETURN_FLAT_NAME) + DEBUGADD(lvl,("DS_RETURN_FLAT_NAME ")); + if (flags) + DEBUGADD(lvl,("\n")); +} + +/********************************************************************* + ********************************************************************/ + +static int pack_dsdcinfo(struct DS_DOMAIN_CONTROLLER_INFO *info, + unsigned char **buf) +{ + unsigned char *buffer = NULL; + int len = 0; + int buflen = 0; + UUID_FLAT guid_flat; + + DEBUG(10,("pack_dsdcinfo: Packing dsdcinfo\n")); + + ZERO_STRUCT(guid_flat); + + if (info->domain_guid) { + const struct GUID *guid = info->domain_guid; + smb_uuid_pack(*guid, &guid_flat); + } + + again: + len = 0; + + if (buflen > 0) { + DEBUG(10,("pack_dsdcinfo: Packing domain %s (%s)\n", + info->domain_name, info->domain_controller_name)); + } + + len += tdb_pack(buffer+len, buflen-len, "ffdBffdff", + info->domain_controller_name, + info->domain_controller_address, + info->domain_controller_address_type, + UUID_FLAT_SIZE, guid_flat.info, + info->domain_name, + info->dns_forest_name, + info->flags, + info->dc_site_name, + info->client_site_name); + + if (buflen < len) { + SAFE_FREE(buffer); + if ((buffer = SMB_MALLOC_ARRAY(unsigned char, len)) == NULL ) { + DEBUG(0,("pack_dsdcinfo: failed to alloc buffer!\n")); + buflen = -1; + goto done; + } + buflen = len; + goto again; + } + + *buf = buffer; + + done: + return buflen; +} + +/********************************************************************* + ********************************************************************/ + +static NTSTATUS unpack_dsdcinfo(TALLOC_CTX *mem_ctx, + unsigned char *buf, + int buflen, + struct DS_DOMAIN_CONTROLLER_INFO **info_ret) +{ + int len = 0; + struct DS_DOMAIN_CONTROLLER_INFO *info = NULL; + uint32_t guid_len = 0; + unsigned char *guid_buf = NULL; + UUID_FLAT guid_flat; + + /* forgive me 6 times */ + fstring domain_controller_name; + fstring domain_controller_address; + fstring domain_name; + fstring dns_forest_name; + fstring dc_site_name; + fstring client_site_name; + + info = TALLOC_ZERO_P(mem_ctx, struct DS_DOMAIN_CONTROLLER_INFO); + NT_STATUS_HAVE_NO_MEMORY(info); + + len += tdb_unpack(buf+len, buflen-len, "ffdBffdff", + &domain_controller_name, + &domain_controller_address, + &info->domain_controller_address_type, + &guid_len, &guid_buf, + &domain_name, + &dns_forest_name, + &info->flags, + &dc_site_name, + &client_site_name); + if (len == -1) { + DEBUG(5,("unpack_dsdcinfo: Failed to unpack domain\n")); + goto failed; + } + + info->domain_controller_name = + talloc_strdup(mem_ctx, domain_controller_name); + info->domain_controller_address = + talloc_strdup(mem_ctx, domain_controller_address); + info->domain_name = + talloc_strdup(mem_ctx, domain_name); + info->dns_forest_name = + talloc_strdup(mem_ctx, dns_forest_name); + info->dc_site_name = + talloc_strdup(mem_ctx, dc_site_name); + info->client_site_name = + talloc_strdup(mem_ctx, client_site_name); + + if (!info->domain_controller_name || + !info->domain_controller_address || + !info->domain_name || + !info->dns_forest_name || + !info->dc_site_name || + !info->client_site_name) { + goto failed; + } + + if (guid_len > 0) { + struct GUID guid; + + if (guid_len != UUID_FLAT_SIZE) { + goto failed; + } + + memcpy(&guid_flat.info, guid_buf, guid_len); + smb_uuid_unpack(guid_flat, &guid); + + info->domain_guid = talloc_memdup(mem_ctx, &guid, sizeof(guid)); + if (!info->domain_guid) { + goto failed; + } + SAFE_FREE(guid_buf); + } + + DEBUG(10,("unpack_dcscinfo: Unpacked domain %s (%s)\n", + info->domain_name, info->domain_controller_name)); + + *info_ret = info; + + return NT_STATUS_OK; + + failed: + TALLOC_FREE(info); + SAFE_FREE(guid_buf); + return NT_STATUS_NO_MEMORY; +} + +/**************************************************************** +****************************************************************/ + +static char *DsGetDcName_cache_key(TALLOC_CTX *mem_ctx, const char *domain) +{ + if (!mem_ctx || !domain) { + return NULL; + } + + return talloc_asprintf(mem_ctx, DSGETDCNAME_FMT, + strupper_static(domain)); +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS DsGetDcName_cache_delete(TALLOC_CTX *mem_ctx, + const char *domain_name) +{ + char *key; + + if (!gencache_init()) { + return NT_STATUS_INTERNAL_DB_ERROR; + } + + key = DsGetDcName_cache_key(mem_ctx, domain_name); + if (!key) { + return NT_STATUS_NO_MEMORY; + } + + if (!gencache_del(key)) { + return NT_STATUS_UNSUCCESSFUL; + } + + return NT_STATUS_OK; +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS DsGetDcName_cache_store(TALLOC_CTX *mem_ctx, + const char *domain_name, + struct DS_DOMAIN_CONTROLLER_INFO *info) +{ + time_t expire_time; + char *key; + BOOL ret = False; + DATA_BLOB blob; + unsigned char *buf = NULL; + int len = 0; + + if (!gencache_init()) { + return NT_STATUS_INTERNAL_DB_ERROR; + } + + key = DsGetDcName_cache_key(mem_ctx, domain_name); + if (!key) { + return NT_STATUS_NO_MEMORY; + } + + expire_time = time(NULL) + DSGETDCNAME_CACHE_TTL; + + len = pack_dsdcinfo(info, &buf); + if (len == -1) { + return NT_STATUS_UNSUCCESSFUL; + } + + blob = data_blob(buf, len); + SAFE_FREE(buf); + + if (gencache_lock_entry(key) != 0) { + data_blob_free(&blob); + return NT_STATUS_LOCK_NOT_GRANTED; + } + + ret = gencache_set_data_blob(key, &blob, expire_time); + data_blob_free(&blob); + + gencache_unlock_entry(key); + + return ret ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS DsGetDcName_cache_refresh(TALLOC_CTX *mem_ctx, + const char *domain_name, + struct GUID *domain_guid, + uint32_t flags, + const char *site_name, + struct DS_DOMAIN_CONTROLLER_INFO *info) +{ + struct cldap_netlogon_reply r; + + /* check if matching entry is older then 15 minutes, if yes, send + * CLDAP/MAILSLOT ping again and store the cached data */ + + ZERO_STRUCT(r); + + if (ads_cldap_netlogon(info->domain_controller_name, + info->domain_name, &r)) { + + DsGetDcName_cache_delete(mem_ctx, domain_name); + + return DsGetDcName_cache_store(mem_ctx, + info->domain_name, + info); + } + + return NT_STATUS_INVALID_NETWORK_RESPONSE; +} + +/**************************************************************** +****************************************************************/ + +#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 (req_flags & DS_PDC_REQUIRED) + RETURN_ON_FALSE(ret_flags & ADS_PDC); + + if (req_flags & DS_GC_SERVER_REQUIRED) + RETURN_ON_FALSE(ret_flags & ADS_GC); + + if (req_flags & DS_ONLY_LDAP_NEEDED) + RETURN_ON_FALSE(ret_flags & ADS_LDAP); + + if ((req_flags & DS_DIRECTORY_SERVICE_REQUIRED) || + (req_flags & DS_DIRECTORY_SERVICE_PREFERRED)) + RETURN_ON_FALSE(ret_flags & ADS_DS); + + if (req_flags & DS_KDC_REQUIRED) + RETURN_ON_FALSE(ret_flags & ADS_KDC); + + if (req_flags & DS_TIMESERV_REQUIRED) + RETURN_ON_FALSE(ret_flags & ADS_TIMESERV); + + if (req_flags & DS_WRITABLE_REQUIRED) + RETURN_ON_FALSE(ret_flags & ADS_WRITABLE); + + return True; +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS DsGetDcName_cache_fetch(TALLOC_CTX *mem_ctx, + const char *domain_name, + struct GUID *domain_guid, + uint32_t flags, + const char *site_name, + struct DS_DOMAIN_CONTROLLER_INFO **info, + BOOL *expired) +{ + char *key; + DATA_BLOB blob; + NTSTATUS status; + + if (!gencache_init()) { + return NT_STATUS_INTERNAL_DB_ERROR; + } + + key = DsGetDcName_cache_key(mem_ctx, domain_name); + if (!key) { + return NT_STATUS_NO_MEMORY; + } + + if (!gencache_get_data_blob(key, &blob, expired)) { + return NT_STATUS_OBJECT_NAME_NOT_FOUND; + } + + status = unpack_dsdcinfo(mem_ctx, blob.data, blob.length, info); + if (!NT_STATUS_IS_OK(status)) { + data_blob_free(&blob); + return status; + } + + data_blob_free(&blob); + + /* check flags */ + if (!check_cldap_reply_required_flags((*info)->flags, flags)) { + DEBUG(10,("invalid flags\n")); + return NT_STATUS_INVALID_PARAMETER; + } + + if ((flags & DS_IP_REQUIRED) && + !((*info)->domain_controller_address_type != ADS_INET_ADDRESS)) { + return NT_STATUS_INVALID_PARAMETER_MIX; + } + + return NT_STATUS_OK; +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS DsGetDcName_cached(TALLOC_CTX *mem_ctx, + const char *domain_name, + struct GUID *domain_guid, + uint32_t flags, + const char *site_name, + struct DS_DOMAIN_CONTROLLER_INFO **info) +{ + NTSTATUS status; + BOOL expired = False; + + status = DsGetDcName_cache_fetch(mem_ctx, domain_name, domain_guid, + flags, site_name, info, &expired); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(10,("DsGetDcName_cached: cache fetch failed with: %s\n", + nt_errstr(status))); + return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; + } + + if (flags & DS_BACKGROUND_ONLY) { + return status; + } + + if (expired) { + status = DsGetDcName_cache_refresh(mem_ctx, domain_name, + domain_guid, flags, + site_name, *info); + if (!NT_STATUS_IS_OK(status)) { + return status; + } + } + + return status; +} + +/**************************************************************** +****************************************************************/ + +static BOOL check_allowed_required_flags(uint32_t flags) +{ + uint32_t return_type = flags & (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME); + uint32_t offered_type = flags & (DS_IS_FLAT_NAME|DS_IS_DNS_NAME); + uint32_t query_type = flags & (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY); + + /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits + * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */ + + debug_dsdcinfo_flags(0, flags); + + if (return_type == (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME)) { + return False; + } + + if (offered_type == (DS_IS_DNS_NAME|DS_IS_FLAT_NAME)) { + return False; + } + + if (query_type == (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY)) { + return False; + } + +#if 0 + if ((flags & DS_RETURN_DNS_NAME) && (!(flags & DS_IP_REQUIRED))) { + printf("gd: here5 \n"); + return False; + } +#endif + return True; +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx, + const char *domain_name, + uint32_t flags, + struct ip_service_name **returned_dclist, + int *return_count) +{ + if (lp_disable_netbios()) { + return NT_STATUS_NOT_SUPPORTED; + } + + /* FIXME: code here */ + + return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx, + const char *domain_name, + struct GUID *domain_guid, + uint32_t flags, + const char *site_name, + struct ip_service_name **returned_dclist, + int *return_count) +{ + int i, j; + NTSTATUS status; + struct dns_rr_srv *dcs = NULL; + int numdcs = 0; + int numaddrs = 0; + + if ((!(flags & DS_DIRECTORY_SERVICE_REQUIRED)) && + (!(flags & DS_KDC_REQUIRED)) && + (!(flags & DS_GC_SERVER_REQUIRED)) && + (!(flags & DS_PDC_REQUIRED))) { + DEBUG(1,("discover_dc_dns: invalid flags\n")); + return NT_STATUS_INVALID_PARAMETER; + } + + if (flags & DS_PDC_REQUIRED) { + 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); + } else if (flags & DS_KDC_REQUIRED) { + 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); + } else if (domain_guid) { + status = ads_dns_query_dcs_guid(mem_ctx, domain_name, + domain_guid, &dcs, &numdcs); + } else { + /* FIXME: ? */ + DEBUG(1,("discover_dc_dns: not enough input\n")); + status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; + } + + if (!NT_STATUS_IS_OK(status)) { + return status; + } + + if (numdcs == 0) { + return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; + } + + for (i=0;iport = dcs[i].port; + r->hostname = dcs[i].hostname; + + if (!(flags & DS_IP_REQUIRED)) { + (*return_count)++; + continue; + } + + /* If we don't have an IP list for a name, lookup it up */ + + if (!dcs[i].ips) { + r->ip = *interpret_addr2(dcs[i].hostname); + i++; + j = 0; + } else { + /* use the IP addresses from the SRV sresponse */ + + if (j >= dcs[i].num_ips) { + i++; + j = 0; + continue; + } + + r->ip = dcs[i].ips[j]; + j++; + } + + /* 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 + * 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_ip(r->ip)) { + (*return_count)++; + continue; + } + } + + return (*return_count > 0) ? NT_STATUS_OK : + NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx, + const char *domain_controller_name, + const char *domain_controller_address, + uint32_t domain_controller_address_type, + const struct GUID *domain_guid, + const char *domain_name, + const char *dns_forest_name, + uint32_t flags, + const char *dc_site_name, + const char *client_site_name, + struct DS_DOMAIN_CONTROLLER_INFO **info_out) +{ + struct DS_DOMAIN_CONTROLLER_INFO *info; + + info = TALLOC_ZERO_P(mem_ctx, struct DS_DOMAIN_CONTROLLER_INFO); + NT_STATUS_HAVE_NO_MEMORY(info); + + if (domain_controller_name) { + info->domain_controller_name = talloc_strdup(mem_ctx, + domain_controller_name); + NT_STATUS_HAVE_NO_MEMORY(info->domain_controller_name); + } + + if (domain_controller_address) { + info->domain_controller_address = talloc_strdup(mem_ctx, + domain_controller_address); + NT_STATUS_HAVE_NO_MEMORY(info->domain_controller_address); + } + + info->domain_controller_address_type = domain_controller_address_type; + + if (domain_guid) { + info->domain_guid = talloc_memdup(mem_ctx, domain_guid, + sizeof(*domain_guid)); + NT_STATUS_HAVE_NO_MEMORY(info->domain_guid); + } + + if (domain_name) { + info->domain_name = talloc_strdup(mem_ctx, domain_name); + NT_STATUS_HAVE_NO_MEMORY(info->domain_name); + } + + if (dns_forest_name) { + info->dns_forest_name = talloc_strdup(mem_ctx, + dns_forest_name); + NT_STATUS_HAVE_NO_MEMORY(info->dns_forest_name); + } + + info->flags = flags; + + if (dc_site_name) { + info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name); + NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name); + } + + if (client_site_name) { + info->client_site_name = talloc_strdup(mem_ctx, + client_site_name); + NT_STATUS_HAVE_NO_MEMORY(info->client_site_name); + } + + *info_out = info; + + return NT_STATUS_OK; +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx, + const char *domain_name, + uint32_t flags, + struct ip_service_name **dclist, + int num_dcs, + struct DS_DOMAIN_CONTROLLER_INFO **info) +{ + int i = 0; + BOOL valid_dc = False; + struct cldap_netlogon_reply r; + const char *dc_hostname, *dc_domain_name; + const char *dc_address; + uint32_t dc_address_type; + uint32_t dc_flags; + struct GUID dc_guid; + + for (i=0; ihostname, + domain_name, &r)) && + (check_cldap_reply_required_flags(r.flags, flags))) { + valid_dc = True; + break; + } + + continue; + } + + if (!valid_dc) { + return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; + } + + dc_flags = r.flags; + + if (flags & DS_RETURN_FLAT_NAME) { + if (!strlen(r.netbios_hostname) || !strlen(r.netbios_domain)) { + return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; + } + dc_hostname = r.netbios_hostname; + dc_domain_name = r.netbios_domain; + } else if (flags & DS_RETURN_DNS_NAME) { + if (!strlen(r.hostname) || !strlen(r.domain)) { + return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; + } + dc_hostname = r.hostname; + dc_domain_name = r.domain; + dc_flags |= ADS_DNS_DOMAIN | ADS_DNS_CONTROLLER; + } else { + /* FIXME */ + dc_hostname = r.hostname; + dc_domain_name = r.domain; + dc_flags |= ADS_DNS_DOMAIN | ADS_DNS_CONTROLLER; + } + + if (flags & DS_IP_REQUIRED) { + dc_address = talloc_asprintf(mem_ctx, "\\\\%s", + inet_ntoa(dclist[i]->ip)); + dc_address_type = ADS_INET_ADDRESS; + } else { + dc_address = talloc_asprintf(mem_ctx, "\\\\%s", + r.netbios_hostname); + dc_address_type = ADS_NETBIOS_ADDRESS; + } + NT_STATUS_HAVE_NO_MEMORY(dc_address); + dc_guid = smb_uuid_unpack_static(r.guid); + + if (r.forest) { + dc_flags |= ADS_DNS_FOREST; + } + + return make_domain_controller_info(mem_ctx, + dc_hostname, + dc_address, + dc_address_type, + &dc_guid, + dc_domain_name, + r.forest, + dc_flags, + r.server_site_name, + r.client_site_name, + info); + +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx, + const char *domain_name, + uint32_t flags, + struct ip_service_name **dclist, + int num_dcs, + struct DS_DOMAIN_CONTROLLER_INFO **info) +{ + /* FIXME: code here */ + + return NT_STATUS_NOT_SUPPORTED; +} + +/**************************************************************** +****************************************************************/ + +static NTSTATUS DsGetDcName_rediscover(TALLOC_CTX *mem_ctx, + const char *domain_name, + struct GUID *domain_guid, + uint32_t flags, + const char *site_name, + struct DS_DOMAIN_CONTROLLER_INFO **info) +{ + NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; + struct ip_service_name *dclist; + int num_dcs; + + DEBUG(10,("DsGetDcName_rediscover\n")); + + if (flags & DS_IS_FLAT_NAME) { + + status = discover_dc_netbios(mem_ctx, domain_name, flags, + &dclist, &num_dcs); + NT_STATUS_NOT_OK_RETURN(status); + + return process_dc_netbios(mem_ctx, domain_name, flags, + &dclist, num_dcs, info); + } + + if (flags & DS_IS_DNS_NAME) { + + status = discover_dc_dns(mem_ctx, domain_name, domain_guid, + flags, site_name, &dclist, &num_dcs); + NT_STATUS_NOT_OK_RETURN(status); + + return process_dc_dns(mem_ctx, domain_name, flags, + &dclist, num_dcs, info); + } + + status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags, + site_name, &dclist, &num_dcs); + + if (NT_STATUS_IS_OK(status) && num_dcs != 0) { + + status = process_dc_dns(mem_ctx, domain_name, flags, &dclist, + num_dcs, info); + if (NT_STATUS_IS_OK(status)) { + return status; + } + } + + status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist, + &num_dcs); + NT_STATUS_NOT_OK_RETURN(status); + + return process_dc_netbios(mem_ctx, domain_name, flags, &dclist, + num_dcs, info); +} + +/******************************************************************** + DsGetDcName. + + This will be the only public function here. +********************************************************************/ + +NTSTATUS DsGetDcName(TALLOC_CTX *mem_ctx, + const char *computer_name, + const char *domain_name, + struct GUID *domain_guid, + const char *site_name, + uint32_t flags, + struct DS_DOMAIN_CONTROLLER_INFO **info) +{ + NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND; + struct DS_DOMAIN_CONTROLLER_INFO *myinfo = NULL; + + DEBUG(10,("DsGetDcName: computer_name: %s, domain_name: %s, " + "domain_guid: %s, site_name: %s, flags: 0x%08x\n", + computer_name, domain_name, + domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)", + site_name, flags)); + + *info = NULL; + + if (!check_allowed_required_flags(flags)) { + DEBUG(0,("invalid flags specified\n")); + return NT_STATUS_INVALID_PARAMETER; + } + + if (flags & DS_FORCE_REDISCOVERY) { + goto rediscover; + } + + status = DsGetDcName_cached(mem_ctx, domain_name, domain_guid, + flags, site_name, &myinfo); + if (NT_STATUS_IS_OK(status)) { + *info = myinfo; + return status; + } + + if (flags & DS_BACKGROUND_ONLY) { + return status; + } + + rediscover: + status = DsGetDcName_rediscover(mem_ctx, domain_name, + domain_guid, flags, site_name, + &myinfo); + + if (NT_STATUS_IS_OK(status)) { + DsGetDcName_cache_store(mem_ctx, domain_name, myinfo); + *info = myinfo; + } + + return status; +} diff --git a/source/rpcclient/cmd_netlogon.c b/source/rpcclient/cmd_netlogon.c index b22de0bb54d..dd8cb6afc51 100644 --- a/source/rpcclient/cmd_netlogon.c +++ b/source/rpcclient/cmd_netlogon.c @@ -106,47 +106,6 @@ static WERROR cmd_netlogon_getdcname(struct rpc_pipe_client *cli, return result; } -static void display_ds_domain_controller_info(TALLOC_CTX *mem_ctx, const struct DS_DOMAIN_CONTROLLER_INFO *info) -{ - d_printf("domain_controller_name: %s\n", info->domain_controller_name); - d_printf("domain_controller_address: %s\n", info->domain_controller_address); - d_printf("domain_controller_address_type: %d\n", info->domain_controller_address_type); - d_printf("domain_guid: %s\n", GUID_string(mem_ctx, info->domain_guid)); - d_printf("domain_name: %s\n", info->domain_name); - d_printf("dns_forest_name: %s\n", info->dns_forest_name); - d_printf("flags: 0x%08x\n" - "\tIs a PDC: %s\n" - "\tIs a GC of the forest: %s\n" - "\tIs an LDAP server: %s\n" - "\tSupports DS: %s\n" - "\tIs running a KDC: %s\n" - "\tIs running time services: %s\n" - "\tIs the closest DC: %s\n" - "\tIs writable: %s\n" - "\tHas a hardware clock: %s\n" - "\tIs a non-domain NC serviced by LDAP server: %s\n" - "\tDomainControllerName is a DNS name: %s\n" - "\tDomainName is a DNS name: %s\n" - "\tDnsForestName is a DNS name: %s\n", - info->flags, - (info->flags & ADS_PDC) ? "yes" : "no", - (info->flags & ADS_GC) ? "yes" : "no", - (info->flags & ADS_LDAP) ? "yes" : "no", - (info->flags & ADS_DS) ? "yes" : "no", - (info->flags & ADS_KDC) ? "yes" : "no", - (info->flags & ADS_TIMESERV) ? "yes" : "no", - (info->flags & ADS_CLOSEST) ? "yes" : "no", - (info->flags & ADS_WRITABLE) ? "yes" : "no", - (info->flags & ADS_GOOD_TIMESERV) ? "yes" : "no", - (info->flags & ADS_NDNC) ? "yes" : "no", - (info->flags & ADS_DNS_CONTROLLER) ? "yes":"no", - (info->flags & ADS_DNS_DOMAIN) ? "yes":"no", - (info->flags & ADS_DNS_FOREST) ? "yes":"no"); - - d_printf("dc_site_name: %s\n", info->dc_site_name); - d_printf("client_site_name: %s\n", info->client_site_name); -} - static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, int argc, const char **argv) @@ -160,7 +119,8 @@ static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli, struct DS_DOMAIN_CONTROLLER_INFO *info = NULL; if (argc < 2) { - fprintf(stderr, "Usage: %s [domainname] [domain_name] [domain_guid] [site_guid] [flags]\n", argv[0]); + fprintf(stderr, "Usage: %s [domain_name] [domain_guid] " + "[site_guid] [flags]\n", argv[0]); return WERR_OK; } @@ -181,7 +141,9 @@ static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli, if (argc >= 5) sscanf(argv[4], "%x", &flags); - + + debug_dsdcinfo_flags(1,flags); + result = rpccli_netlogon_dsr_getdcname(cli, mem_ctx, server_name, domain_name, &domain_guid, &site_guid, flags, &info); @@ -211,7 +173,8 @@ static WERROR cmd_netlogon_dsr_getdcnameex(struct rpc_pipe_client *cli, struct DS_DOMAIN_CONTROLLER_INFO *info = NULL; if (argc < 2) { - fprintf(stderr, "Usage: %s [domainname] [domain_name] [domain_guid] [site_name] [flags]\n", argv[0]); + fprintf(stderr, "Usage: %s [domain_name] [domain_guid] " + "[site_name] [flags]\n", argv[0]); return WERR_OK; } @@ -230,6 +193,8 @@ static WERROR cmd_netlogon_dsr_getdcnameex(struct rpc_pipe_client *cli, if (argc >= 5) sscanf(argv[4], "%x", &flags); + debug_dsdcinfo_flags(1,flags); + result = rpccli_netlogon_dsr_getdcnameex(cli, mem_ctx, server_name, domain_name, &domain_guid, site_name, flags, &info); @@ -261,7 +226,9 @@ static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli, struct DS_DOMAIN_CONTROLLER_INFO *info = NULL; if (argc < 2) { - fprintf(stderr, "Usage: %s [domainname] [client_account] [acb_mask] [domain_name] [domain_guid] [site_name] [flags]\n", argv[0]); + fprintf(stderr, "Usage: %s [client_account] [acb_mask] " + "[domain_name] [domain_guid] [site_name] " + "[flags]\n", argv[0]); return WERR_OK; } @@ -270,7 +237,7 @@ static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli, if (argc >= 3) mask = atoi(argv[2]); - + if (argc >= 4) domain_name = argv[3]; @@ -286,6 +253,8 @@ static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli, if (argc >= 7) sscanf(argv[6], "%x", &flags); + debug_dsdcinfo_flags(1,flags); + result = rpccli_netlogon_dsr_getdcnameex2(cli, mem_ctx, server_name, client_account, mask, domain_name, &domain_guid, diff --git a/source/utils/net_lookup.c b/source/utils/net_lookup.c index 6fad85857d4..a775afcdc1f 100644 --- a/source/utils/net_lookup.c +++ b/source/utils/net_lookup.c @@ -25,10 +25,12 @@ int net_lookup_usage(int argc, const char **argv) " net lookup [host] HOSTNAME[#]\n\tgives IP for a hostname\n\n" " net lookup ldap [domain]\n\tgives IP of domain's ldap server\n\n" " net lookup kdc [realm]\n\tgives IP of realm's kerberos KDC\n\n" +" net lookup pdc [domain|realm]\n\tgives IP of realm's kerberos KDC\n\n" " net lookup dc [domain]\n\tgives IP of domains Domain Controllers\n\n" " net lookup master [domain|wg]\n\tgive IP of master browser\n\n" " net lookup name [name]\n\tLookup name's sid and type\n\n" " net lookup sid [sid]\n\tGive sid's name and type\n\n" +" net lookup dsgetdcname [name] [flags] [sitename]\n\n" ); return -1; } @@ -95,7 +97,7 @@ static int net_lookup_ldap(int argc, const char **argv) sitename = sitename_fetch(domain); if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) { - d_fprintf(stderr, "net_lookup_ldap: talloc_inti() failed!\n"); + d_fprintf(stderr, "net_lookup_ldap: talloc_init() failed!\n"); SAFE_FREE(sitename); return -1; } @@ -110,7 +112,7 @@ static int net_lookup_ldap(int argc, const char **argv) return 0; } - DEBUG(9, ("Looking up DC for domain %s\n", domain)); + DEBUG(9, ("Looking up PDC for domain %s\n", domain)); if (!get_pdc_ip(domain, &addr)) { TALLOC_FREE( ctx ); SAFE_FREE(sitename); @@ -125,7 +127,7 @@ static int net_lookup_ldap(int argc, const char **argv) return -1; } - DEBUG(9, ("Found DC with DNS name %s\n", hostent->h_name)); + DEBUG(9, ("Found PDC with DNS name %s\n", hostent->h_name)); domain = strchr(hostent->h_name, '.'); if (!domain) { TALLOC_FREE( ctx ); @@ -158,9 +160,16 @@ static int net_lookup_dc(int argc, const char **argv) struct ip_service *ip_list; struct in_addr addr; char *pdc_str = NULL; - const char *domain=opt_target_workgroup; + const char *domain = NULL; char *sitename = NULL; int count, i; + BOOL sec_ads = (lp_security() == SEC_ADS); + + if (sec_ads) { + domain = lp_realm(); + } else { + domain = opt_target_workgroup; + } if (argc > 0) domain=argv[0]; @@ -173,7 +182,7 @@ static int net_lookup_dc(int argc, const char **argv) d_printf("%s\n", pdc_str); sitename = sitename_fetch(domain); - if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename, &ip_list, &count, False))) { + if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename, &ip_list, &count, sec_ads))) { SAFE_FREE(pdc_str); SAFE_FREE(sitename); return 0; @@ -188,6 +197,32 @@ static int net_lookup_dc(int argc, const char **argv) return 0; } +static int net_lookup_pdc(int argc, const char **argv) +{ + struct in_addr addr; + char *pdc_str = NULL; + const char *domain; + + if (lp_security() == SEC_ADS) { + domain = lp_realm(); + } else { + domain = opt_target_workgroup; + } + + if (argc > 0) + domain=argv[0]; + + /* first get PDC */ + if (!get_pdc_ip(domain, &addr)) + return -1; + + asprintf(&pdc_str, "%s", inet_ntoa(addr)); + d_printf("%s\n", pdc_str); + SAFE_FREE(pdc_str); + return 0; +} + + static int net_lookup_master(int argc, const char **argv) { struct in_addr master_ip; @@ -302,6 +337,64 @@ static int net_lookup_sid(int argc, const char **argv) return 0; } +static int net_lookup_dsgetdcname(int argc, const char **argv) +{ + NTSTATUS status; + const char *domain_name = NULL; + char *site_name = NULL; + uint32_t flags = 0; + struct DS_DOMAIN_CONTROLLER_INFO *info = NULL; + TALLOC_CTX *mem_ctx; + + if (argc < 1 || argc > 3) { + d_printf("usage: net lookup dsgetdcname " + " \n"); + return -1; + } + + mem_ctx = talloc_init("net_lookup_dsgetdcname"); + if (!mem_ctx) { + return -1; + } + + domain_name = argv[0]; + + if (argc >= 2) + sscanf(argv[1], "%x", &flags); + + if (!flags) { + flags |= DS_DIRECTORY_SERVICE_REQUIRED; + } + + if (argc == 3) { + site_name = SMB_STRDUP(argv[2]); + if (!site_name) { + TALLOC_FREE(mem_ctx); + return -1; + } + } + + if (!site_name) { + site_name = sitename_fetch(domain_name); + } + + status = DsGetDcName(mem_ctx, NULL, domain_name, NULL, site_name, + flags, &info); + if (!NT_STATUS_IS_OK(status)) { + d_printf("failed with: %s\n", nt_errstr(status)); + SAFE_FREE(site_name); + TALLOC_FREE(mem_ctx); + return -1; + } + + display_ds_domain_controller_info(mem_ctx, info); + + SAFE_FREE(site_name); + TALLOC_FREE(mem_ctx); + return 0; +} + + /* lookup hosts or IP addresses using internal samba lookup fns */ int net_lookup(int argc, const char **argv) { @@ -311,10 +404,12 @@ int net_lookup(int argc, const char **argv) {"HOST", net_lookup_host}, {"LDAP", net_lookup_ldap}, {"DC", net_lookup_dc}, + {"PDC", net_lookup_pdc}, {"MASTER", net_lookup_master}, {"KDC", net_lookup_kdc}, {"NAME", net_lookup_name}, {"SID", net_lookup_sid}, + {"DSGETDCNAME", net_lookup_dsgetdcname}, {NULL, NULL} };