smbd: use fsctl_get_reparse_point() in smb3_file_posix_information_init()
[samba.git] / source3 / libsmb / dsgetdcname.c
blob9053ee5c8b05f3fcf7ca1d65f22f208dc8027b17
1 /*
2 Unix SMB/CIFS implementation.
4 dsgetdcname
6 Copyright (C) Gerald Carter 2006
7 Copyright (C) Guenther Deschner 2007-2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "libsmb/dsgetdcname.h"
25 #include "libsmb/namequery.h"
26 #include "libads/sitename_cache.h"
27 #include "../librpc/gen_ndr/ndr_netlogon.h"
28 #include "libads/cldap.h"
29 #include "libads/netlogon_ping.h"
30 #include "lib/addns/dnsquery_srv.h"
31 #include "libsmb/clidgram.h"
32 #include "lib/gencache.h"
33 #include "lib/util/util_net.h"
34 #include "lib/tsocket/tsocket.h"
36 /* 15 minutes */
37 #define DSGETDCNAME_CACHE_TTL 60*15
39 struct ip_service_name {
40 struct samba_sockaddr sa;
41 const char *hostname;
44 static NTSTATUS make_dc_info_from_cldap_reply(
45 TALLOC_CTX *mem_ctx,
46 uint32_t flags,
47 const struct samba_sockaddr *sa,
48 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
49 struct netr_DsRGetDCNameInfo **info);
51 /****************************************************************
52 ****************************************************************/
54 static void debug_dsdcinfo_flags(int lvl, uint32_t flags)
56 DEBUG(lvl,("debug_dsdcinfo_flags: 0x%08x\n\t", flags));
58 if (flags & DS_FORCE_REDISCOVERY)
59 DEBUGADD(lvl,("DS_FORCE_REDISCOVERY "));
60 if (flags & 0x00000002)
61 DEBUGADD(lvl,("0x00000002 "));
62 if (flags & 0x00000004)
63 DEBUGADD(lvl,("0x00000004 "));
64 if (flags & 0x00000008)
65 DEBUGADD(lvl,("0x00000008 "));
66 if (flags & DS_DIRECTORY_SERVICE_REQUIRED)
67 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_REQUIRED "));
68 if (flags & DS_DIRECTORY_SERVICE_PREFERRED)
69 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_PREFERRED "));
70 if (flags & DS_GC_SERVER_REQUIRED)
71 DEBUGADD(lvl,("DS_GC_SERVER_REQUIRED "));
72 if (flags & DS_PDC_REQUIRED)
73 DEBUGADD(lvl,("DS_PDC_REQUIRED "));
74 if (flags & DS_BACKGROUND_ONLY)
75 DEBUGADD(lvl,("DS_BACKGROUND_ONLY "));
76 if (flags & DS_IP_REQUIRED)
77 DEBUGADD(lvl,("DS_IP_REQUIRED "));
78 if (flags & DS_KDC_REQUIRED)
79 DEBUGADD(lvl,("DS_KDC_REQUIRED "));
80 if (flags & DS_TIMESERV_REQUIRED)
81 DEBUGADD(lvl,("DS_TIMESERV_REQUIRED "));
82 if (flags & DS_WRITABLE_REQUIRED)
83 DEBUGADD(lvl,("DS_WRITABLE_REQUIRED "));
84 if (flags & DS_GOOD_TIMESERV_PREFERRED)
85 DEBUGADD(lvl,("DS_GOOD_TIMESERV_PREFERRED "));
86 if (flags & DS_AVOID_SELF)
87 DEBUGADD(lvl,("DS_AVOID_SELF "));
88 if (flags & DS_ONLY_LDAP_NEEDED)
89 DEBUGADD(lvl,("DS_ONLY_LDAP_NEEDED "));
90 if (flags & DS_IS_FLAT_NAME)
91 DEBUGADD(lvl,("DS_IS_FLAT_NAME "));
92 if (flags & DS_IS_DNS_NAME)
93 DEBUGADD(lvl,("DS_IS_DNS_NAME "));
94 if (flags & DS_TRY_NEXTCLOSEST_SITE)
95 DEBUGADD(lvl,("DS_TRY_NEXTCLOSEST_SITE "));
96 if (flags & DS_DIRECTORY_SERVICE_6_REQUIRED)
97 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_6_REQUIRED "));
98 if (flags & DS_WEB_SERVICE_REQUIRED)
99 DEBUGADD(lvl,("DS_WEB_SERVICE_REQUIRED "));
100 if (flags & DS_DIRECTORY_SERVICE_8_REQUIRED)
101 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_8_REQUIRED "));
102 if (flags & DS_DIRECTORY_SERVICE_9_REQUIRED)
103 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_9_REQUIRED "));
104 if (flags & DS_DIRECTORY_SERVICE_10_REQUIRED)
105 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_10_REQUIRED "));
106 if (flags & 0x01000000)
107 DEBUGADD(lvl,("0x01000000 "));
108 if (flags & 0x02000000)
109 DEBUGADD(lvl,("0x02000000 "));
110 if (flags & 0x04000000)
111 DEBUGADD(lvl,("0x04000000 "));
112 if (flags & 0x08000000)
113 DEBUGADD(lvl,("0x08000000 "));
114 if (flags & 0x10000000)
115 DEBUGADD(lvl,("0x10000000 "));
116 if (flags & 0x20000000)
117 DEBUGADD(lvl,("0x20000000 "));
118 if (flags & DS_RETURN_DNS_NAME)
119 DEBUGADD(lvl,("DS_RETURN_DNS_NAME "));
120 if (flags & DS_RETURN_FLAT_NAME)
121 DEBUGADD(lvl,("DS_RETURN_FLAT_NAME "));
122 if (flags)
123 DEBUGADD(lvl,("\n"));
126 /****************************************************************
127 ****************************************************************/
129 static char *dsgetdcname_cache_key(TALLOC_CTX *mem_ctx, const char *domain)
131 if (!domain) {
132 return NULL;
135 return talloc_asprintf_strupper_m(mem_ctx, "DSGETDCNAME/DOMAIN/%s",
136 domain);
139 /****************************************************************
140 ****************************************************************/
142 static NTSTATUS dsgetdcname_cache_delete(TALLOC_CTX *mem_ctx,
143 const char *domain_name)
145 char *key;
147 key = dsgetdcname_cache_key(mem_ctx, domain_name);
148 if (!key) {
149 return NT_STATUS_NO_MEMORY;
152 if (!gencache_del(key)) {
153 return NT_STATUS_UNSUCCESSFUL;
156 return NT_STATUS_OK;
159 /****************************************************************
160 ****************************************************************/
162 static NTSTATUS dsgetdcname_cache_store(TALLOC_CTX *mem_ctx,
163 const char *domain_name,
164 DATA_BLOB blob)
166 time_t expire_time;
167 char *key;
168 bool ret = false;
170 key = dsgetdcname_cache_key(mem_ctx, domain_name);
171 if (!key) {
172 return NT_STATUS_NO_MEMORY;
175 expire_time = time(NULL) + DSGETDCNAME_CACHE_TTL;
177 ret = gencache_set_data_blob(key, blob, expire_time);
179 return ret ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
182 /****************************************************************
183 ****************************************************************/
185 static NTSTATUS store_cldap_reply(TALLOC_CTX *mem_ctx,
186 uint32_t flags,
187 struct samba_sockaddr *sa,
188 uint32_t nt_version,
189 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r)
191 DATA_BLOB blob;
192 enum ndr_err_code ndr_err;
193 NTSTATUS status;
194 char addr[INET6_ADDRSTRLEN];
196 print_sockaddr(addr, sizeof(addr), &sa->u.ss);
198 /* FIXME */
199 r->sockaddr_size = 0x10; /* the w32 winsock addr size */
200 r->sockaddr.sockaddr_family = 2; /* AF_INET */
201 if (is_ipaddress_v4(addr)) {
202 r->sockaddr.pdc_ip = talloc_strdup(mem_ctx, addr);
203 if (r->sockaddr.pdc_ip == NULL) {
204 return NT_STATUS_NO_MEMORY;
206 } else {
208 * ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX will
209 * fail with an ipv6 address.
211 * This matches windows behaviour in the CLDAP
212 * response when NETLOGON_NT_VERSION_5EX_WITH_IP
213 * is used.
215 * Windows returns the ipv4 address of the ipv6
216 * server interface and falls back to 127.0.0.1
217 * if there's no ipv4 address.
219 r->sockaddr.pdc_ip = talloc_strdup(mem_ctx, "127.0.0.1");
220 if (r->sockaddr.pdc_ip == NULL) {
221 return NT_STATUS_NO_MEMORY;
225 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, r,
226 (ndr_push_flags_fn_t)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX);
227 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
228 return ndr_map_error2ntstatus(ndr_err);
231 if (r->domain_name) {
232 status = dsgetdcname_cache_store(mem_ctx, r->domain_name,
233 blob);
234 if (!NT_STATUS_IS_OK(status)) {
235 goto done;
237 if (r->client_site) {
238 sitename_store(r->domain_name, r->client_site);
241 if (r->dns_domain) {
242 status = dsgetdcname_cache_store(mem_ctx, r->dns_domain, blob);
243 if (!NT_STATUS_IS_OK(status)) {
244 goto done;
246 if (r->client_site) {
247 sitename_store(r->dns_domain, r->client_site);
251 status = NT_STATUS_OK;
253 done:
254 data_blob_free(&blob);
256 return status;
259 /****************************************************************
260 ****************************************************************/
262 static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
263 const char *domain_name,
264 const struct GUID *domain_guid,
265 uint32_t flags,
266 struct netr_DsRGetDCNameInfo **info_p)
268 char *key;
269 DATA_BLOB blob;
270 enum ndr_err_code ndr_err;
271 struct netr_DsRGetDCNameInfo *info;
272 struct NETLOGON_SAM_LOGON_RESPONSE_EX r;
273 NTSTATUS status;
275 key = dsgetdcname_cache_key(mem_ctx, domain_name);
276 if (!key) {
277 return NT_STATUS_NO_MEMORY;
280 if (!gencache_get_data_blob(key, NULL, &blob, NULL, NULL)) {
281 return NT_STATUS_NOT_FOUND;
284 info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
285 if (!info) {
286 data_blob_free(&blob);
287 return NT_STATUS_NO_MEMORY;
290 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
291 (ndr_pull_flags_fn_t)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX);
293 data_blob_free(&blob);
294 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
295 dsgetdcname_cache_delete(mem_ctx, domain_name);
296 return ndr_map_error2ntstatus(ndr_err);
299 status = make_dc_info_from_cldap_reply(mem_ctx, flags, NULL,
300 &r, &info);
301 if (!NT_STATUS_IS_OK(status)) {
302 return status;
305 if (DEBUGLEVEL >= 10) {
306 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, info);
309 /* check flags */
310 if (!check_cldap_reply_required_flags(info->dc_flags, flags)) {
311 DEBUG(10,("invalid flags\n"));
312 return NT_STATUS_INVALID_PARAMETER;
315 if ((flags & DS_IP_REQUIRED) &&
316 (info->dc_address_type != DS_ADDRESS_TYPE_INET)) {
317 return NT_STATUS_INVALID_PARAMETER_MIX;
320 *info_p = info;
322 return NT_STATUS_OK;
325 /****************************************************************
326 ****************************************************************/
328 static NTSTATUS dsgetdcname_cached(TALLOC_CTX *mem_ctx,
329 struct messaging_context *msg_ctx,
330 const char *domain_name,
331 const struct GUID *domain_guid,
332 uint32_t flags,
333 const char *site_name,
334 struct netr_DsRGetDCNameInfo **info)
336 NTSTATUS status;
338 status = dsgetdcname_cache_fetch(mem_ctx, domain_name, domain_guid,
339 flags, info);
340 if (!NT_STATUS_IS_OK(status)
341 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
342 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
343 nt_errstr(status)));
344 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
347 if (flags & DS_BACKGROUND_ONLY) {
348 return status;
351 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
352 struct netr_DsRGetDCNameInfo *dc_info;
354 status = dsgetdcname(mem_ctx, msg_ctx, domain_name,
355 domain_guid, site_name,
356 flags | DS_FORCE_REDISCOVERY,
357 &dc_info);
359 if (!NT_STATUS_IS_OK(status)) {
360 return status;
363 *info = dc_info;
366 return status;
369 /****************************************************************
370 ****************************************************************/
372 static bool check_allowed_required_flags(uint32_t flags,
373 const char *site_name)
375 uint32_t return_type = flags & (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME);
376 uint32_t offered_type = flags & (DS_IS_FLAT_NAME|DS_IS_DNS_NAME);
377 uint32_t query_type = flags & (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY);
379 /* FIXME: check for DSGETDC_VALID_FLAGS and check for exclusive bits
380 * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
382 debug_dsdcinfo_flags(10, flags);
384 if ((flags & DS_TRY_NEXTCLOSEST_SITE) && site_name) {
385 return false;
388 if (return_type == (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME)) {
389 return false;
392 if (offered_type == (DS_IS_DNS_NAME|DS_IS_FLAT_NAME)) {
393 return false;
396 if (query_type == (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY)) {
397 return false;
400 #if 0
401 if ((flags & DS_RETURN_DNS_NAME) && (!(flags & DS_IP_REQUIRED))) {
402 printf("gd: here5 \n");
403 return false;
405 #endif
406 return true;
409 /****************************************************************
410 ****************************************************************/
412 static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
413 const char *domain_name,
414 uint32_t flags,
415 struct ip_service_name **returned_dclist,
416 size_t *returned_count)
418 NTSTATUS status;
419 enum nbt_name_type name_type = NBT_NAME_LOGON;
420 struct samba_sockaddr *salist = NULL;
421 size_t i;
422 struct ip_service_name *dclist = NULL;
423 size_t count = 0;
424 static const char *resolve_order[] = { "lmhosts", "wins", "bcast", NULL };
426 if (flags & DS_PDC_REQUIRED) {
427 name_type = NBT_NAME_PDC;
430 status = internal_resolve_name(mem_ctx,
431 domain_name,
432 name_type,
433 NULL,
434 &salist,
435 &count,
436 resolve_order);
437 if (!NT_STATUS_IS_OK(status)) {
438 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
439 return status;
442 dclist = talloc_zero_array(mem_ctx, struct ip_service_name, count);
443 if (!dclist) {
444 TALLOC_FREE(salist);
445 return NT_STATUS_NO_MEMORY;
448 for (i=0; i<count; i++) {
449 char addr[INET6_ADDRSTRLEN];
450 struct ip_service_name *r = &dclist[i];
452 print_sockaddr(addr, sizeof(addr),
453 &salist[i].u.ss);
455 r->sa = salist[i];
456 r->hostname = talloc_strdup(mem_ctx, addr);
457 if (!r->hostname) {
458 TALLOC_FREE(salist);
459 TALLOC_FREE(dclist);
460 return NT_STATUS_NO_MEMORY;
465 TALLOC_FREE(salist);
467 *returned_dclist = dclist;
468 *returned_count = count;
470 return NT_STATUS_OK;
473 /****************************************************************
474 ****************************************************************/
476 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
477 const char *domain_name,
478 const struct GUID *domain_guid,
479 uint32_t flags,
480 const char *site_name,
481 struct ip_service_name **returned_dclist,
482 size_t *return_count)
484 size_t i;
485 NTSTATUS status;
486 struct dns_rr_srv *dcs = NULL;
487 size_t numdcs = 0;
488 struct ip_service_name *dclist = NULL;
489 size_t ret_count = 0;
490 char *query = NULL;
492 if (flags & DS_PDC_REQUIRED) {
493 query = ads_dns_query_string_pdc(mem_ctx, domain_name);
494 } else if (flags & DS_GC_SERVER_REQUIRED) {
495 query = ads_dns_query_string_gcs(mem_ctx, domain_name);
496 } else if (flags & DS_KDC_REQUIRED) {
497 query = ads_dns_query_string_kdcs(mem_ctx, domain_name);
498 } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
499 query = ads_dns_query_string_dcs(mem_ctx, domain_name);
500 } else if (domain_guid) {
501 query = ads_dns_query_string_dcs_guid(
502 mem_ctx, domain_guid, domain_name);
503 } else {
504 query = ads_dns_query_string_dcs(mem_ctx, domain_name);
507 if (query == NULL) {
508 return NT_STATUS_NO_MEMORY;
511 status = ads_dns_query_srv(
512 mem_ctx,
513 lp_get_async_dns_timeout(),
514 site_name,
515 query,
516 &dcs,
517 &numdcs);
518 TALLOC_FREE(query);
519 if (!NT_STATUS_IS_OK(status)) {
520 return status;
523 if (numdcs == 0) {
524 TALLOC_FREE(dcs);
525 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
528 /* Check for integer wrap. */
529 if (numdcs + numdcs < numdcs) {
530 TALLOC_FREE(dcs);
531 return NT_STATUS_INVALID_PARAMETER;
535 * We're only returning up to 2 addresses per
536 * DC name, so just allocate size numdcs x 2.
539 dclist = talloc_zero_array(mem_ctx,
540 struct ip_service_name,
541 numdcs * 2);
542 if (!dclist) {
543 TALLOC_FREE(dcs);
544 return NT_STATUS_NO_MEMORY;
548 * First, copy the SRV record replies that
549 * have IP addresses returned with them.
551 ret_count = 0;
552 for (i = 0; i < numdcs; i++) {
553 size_t j;
554 bool have_v4_addr = false;
555 bool have_v6_addr = false;
557 if (dcs[i].num_ips == 0) {
558 continue;
562 * Pick up to 1 address from each address
563 * family (IPv4, IPv6).
565 * This is different from the previous
566 * code which picked a 'next ip' address
567 * each time, incrementing an index.
568 * Too complex to maintain :-(.
570 for (j = 0; j < dcs[i].num_ips; j++) {
571 if ((dcs[i].ss_s[j].ss_family == AF_INET && !have_v4_addr) ||
572 (dcs[i].ss_s[j].ss_family == AF_INET6 && !have_v6_addr)) {
573 bool ok;
574 dclist[ret_count].hostname =
575 talloc_strdup(dclist, dcs[i].hostname);
576 ok = sockaddr_storage_to_samba_sockaddr(
577 &dclist[ret_count].sa,
578 &dcs[i].ss_s[j]);
579 if (!ok) {
580 TALLOC_FREE(dcs);
581 TALLOC_FREE(dclist);
582 return NT_STATUS_INVALID_PARAMETER;
584 ret_count++;
585 if (dcs[i].ss_s[j].ss_family == AF_INET) {
586 have_v4_addr = true;
587 } else {
588 have_v6_addr = true;
590 if (have_v4_addr && have_v6_addr) {
591 break;
597 TALLOC_FREE(dcs);
599 if (ret_count == 0) {
600 TALLOC_FREE(dclist);
601 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
604 *returned_dclist = dclist;
605 *return_count = ret_count;
606 return NT_STATUS_OK;
609 /****************************************************************
610 ****************************************************************/
612 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
613 const char *dc_unc,
614 const char *dc_address,
615 uint32_t dc_address_type,
616 const struct GUID *domain_guid,
617 const char *domain_name,
618 const char *forest_name,
619 uint32_t flags,
620 const char *dc_site_name,
621 const char *client_site_name,
622 struct netr_DsRGetDCNameInfo **info_out)
624 struct netr_DsRGetDCNameInfo *info;
626 info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
627 NT_STATUS_HAVE_NO_MEMORY(info);
629 if (dc_unc) {
630 if (!(dc_unc[0] == '\\' && dc_unc[1] == '\\')) {
631 info->dc_unc = talloc_asprintf(mem_ctx, "\\\\%s",
632 dc_unc);
633 } else {
634 info->dc_unc = talloc_strdup(mem_ctx, dc_unc);
636 NT_STATUS_HAVE_NO_MEMORY(info->dc_unc);
639 if (dc_address) {
640 if (!(dc_address[0] == '\\' && dc_address[1] == '\\')) {
641 info->dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
642 dc_address);
643 } else {
644 info->dc_address = talloc_strdup(mem_ctx, dc_address);
646 NT_STATUS_HAVE_NO_MEMORY(info->dc_address);
649 info->dc_address_type = dc_address_type;
651 if (domain_guid) {
652 info->domain_guid = *domain_guid;
655 if (domain_name) {
656 info->domain_name = talloc_strdup(mem_ctx, domain_name);
657 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
660 if (forest_name && *forest_name) {
661 info->forest_name = talloc_strdup(mem_ctx, forest_name);
662 NT_STATUS_HAVE_NO_MEMORY(info->forest_name);
663 flags |= DS_DNS_FOREST_ROOT;
666 info->dc_flags = flags;
668 if (dc_site_name) {
669 info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name);
670 NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name);
673 if (client_site_name) {
674 info->client_site_name = talloc_strdup(mem_ctx,
675 client_site_name);
676 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
679 *info_out = info;
681 return NT_STATUS_OK;
684 /****************************************************************
685 ****************************************************************/
687 static void map_dc_and_domain_names(uint32_t flags,
688 const char *dc_name,
689 const char *domain_name,
690 const char *dns_dc_name,
691 const char *dns_domain_name,
692 uint32_t *dc_flags,
693 const char **hostname_p,
694 const char **domain_p)
696 switch (flags & 0xf0000000) {
697 case DS_RETURN_FLAT_NAME:
698 if (dc_name && domain_name &&
699 *dc_name && *domain_name) {
700 *hostname_p = dc_name;
701 *domain_p = domain_name;
702 break;
705 FALL_THROUGH;
706 case DS_RETURN_DNS_NAME:
707 default:
708 if (dns_dc_name && dns_domain_name &&
709 *dns_dc_name && *dns_domain_name) {
710 *hostname_p = dns_dc_name;
711 *domain_p = dns_domain_name;
712 *dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
713 break;
715 if (dc_name && domain_name &&
716 *dc_name && *domain_name) {
717 *hostname_p = dc_name;
718 *domain_p = domain_name;
719 break;
724 /****************************************************************
725 ****************************************************************/
727 static NTSTATUS make_dc_info_from_cldap_reply(
728 TALLOC_CTX *mem_ctx,
729 uint32_t flags,
730 const struct samba_sockaddr *sa,
731 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
732 struct netr_DsRGetDCNameInfo **info)
734 const char *dc_hostname = NULL;
735 const char *dc_domain_name = NULL;
736 const char *dc_address = NULL;
737 const char *dc_forest = NULL;
738 uint32_t dc_address_type = 0;
739 uint32_t dc_flags = 0;
740 struct GUID *dc_domain_guid = NULL;
741 const char *dc_server_site = NULL;
742 const char *dc_client_site = NULL;
744 char addr[INET6_ADDRSTRLEN];
746 if (sa != NULL) {
747 print_sockaddr(addr, sizeof(addr), &sa->u.ss);
748 dc_address = addr;
749 dc_address_type = DS_ADDRESS_TYPE_INET;
750 } else {
751 if (r->sockaddr.pdc_ip) {
752 dc_address = r->sockaddr.pdc_ip;
753 dc_address_type = DS_ADDRESS_TYPE_INET;
754 } else {
755 dc_address = r->pdc_name;
756 dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
760 map_dc_and_domain_names(flags,
761 r->pdc_name,
762 r->domain_name,
763 r->pdc_dns_name,
764 r->dns_domain,
765 &dc_flags,
766 &dc_hostname,
767 &dc_domain_name);
769 dc_flags |= r->server_type;
770 dc_forest = r->forest;
771 dc_domain_guid = &r->domain_uuid;
772 dc_server_site = r->server_site;
773 dc_client_site = r->client_site;
775 return make_domain_controller_info(mem_ctx,
776 dc_hostname,
777 dc_address,
778 dc_address_type,
779 dc_domain_guid,
780 dc_domain_name,
781 dc_forest,
782 dc_flags,
783 dc_server_site,
784 dc_client_site,
785 info);
788 /****************************************************************
789 ****************************************************************/
791 static uint32_t map_ds_flags_to_nt_version(uint32_t flags)
793 uint32_t nt_version = 0;
795 if (flags & DS_PDC_REQUIRED) {
796 nt_version |= NETLOGON_NT_VERSION_PDC;
799 if (flags & DS_GC_SERVER_REQUIRED) {
800 nt_version |= NETLOGON_NT_VERSION_GC;
803 if (flags & DS_TRY_NEXTCLOSEST_SITE) {
804 nt_version |= NETLOGON_NT_VERSION_WITH_CLOSEST_SITE;
807 if (flags & DS_IP_REQUIRED) {
808 nt_version |= NETLOGON_NT_VERSION_IP;
811 return nt_version;
814 /****************************************************************
815 ****************************************************************/
817 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
818 const char *domain_name,
819 uint32_t flags,
820 struct ip_service_name *dclist,
821 size_t num_dcs,
822 struct netr_DsRGetDCNameInfo **info)
824 size_t i = 0;
825 struct tsocket_address **addrs = NULL;
826 struct netlogon_samlogon_response **responses = NULL;
827 const uint32_t nt_version = NETLOGON_NT_VERSION_5 |
828 NETLOGON_NT_VERSION_5EX;
829 NTSTATUS status;
831 addrs = talloc_array(mem_ctx, struct tsocket_address *, num_dcs);
832 if (addrs == NULL) {
833 return NT_STATUS_NO_MEMORY;
836 for (i=0; i<num_dcs; i++) {
837 int ret = tsocket_address_bsd_from_samba_sockaddr(
838 addrs, &dclist[i].sa, &addrs[i]);
839 if (ret != 0) {
840 status = map_nt_error_from_unix(errno);
841 goto done;
845 status = netlogon_pings(
846 addrs, /* mem_ctx */
847 lp_client_netlogon_ping_protocol(), /* proto */
848 addrs, /* servers */
849 num_dcs, /* num_servers */
850 (struct netlogon_ping_filter){
851 .ntversion = nt_version,
852 .acct_ctrl = -1,
853 .domain = domain_name,
854 .required_flags = flags,
856 1, /* min_servers */
857 timeval_current_ofs(MAX(3, lp_ldap_timeout() / 2), 0),
858 &responses);
860 if (!NT_STATUS_IS_OK(status)) {
861 status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
862 goto done;
865 for (i = 0; i < num_dcs; i++) {
866 struct netlogon_samlogon_response *r = responses[i];
868 if (r == NULL) {
869 continue;
871 if (r->ntver != NETLOGON_NT_VERSION_5EX) {
872 continue;
874 status = make_dc_info_from_cldap_reply(
875 mem_ctx, flags, &dclist[i].sa, &r->data.nt5_ex, info);
876 if (!NT_STATUS_IS_OK(status)) {
877 continue;
879 status = store_cldap_reply(mem_ctx,
880 flags,
881 &dclist[i].sa,
882 nt_version,
883 &r->data.nt5_ex);
884 if (!NT_STATUS_IS_OK(status)) {
885 continue;
887 goto done;
890 status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
891 done:
892 TALLOC_FREE(addrs);
893 return status;
896 /****************************************************************
897 ****************************************************************/
899 /****************************************************************
900 ****************************************************************/
902 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
903 struct messaging_context *msg_ctx,
904 const char *domain_name,
905 uint32_t flags,
906 struct ip_service_name *dclist,
907 size_t num_dcs,
908 struct netr_DsRGetDCNameInfo **info)
910 enum nbt_name_type name_type = NBT_NAME_LOGON;
911 NTSTATUS status;
912 size_t i;
913 const char *dc_name = NULL;
914 fstring tmp_dc_name;
915 struct netlogon_samlogon_response *r = NULL;
916 bool store_cache = false;
917 uint32_t nt_version = NETLOGON_NT_VERSION_1 |
918 NETLOGON_NT_VERSION_5 |
919 NETLOGON_NT_VERSION_5EX_WITH_IP;
920 size_t len = strlen(lp_netbios_name());
921 char my_acct_name[len+2];
923 if (msg_ctx == NULL) {
924 return NT_STATUS_INVALID_PARAMETER;
927 if (flags & DS_PDC_REQUIRED) {
928 name_type = NBT_NAME_PDC;
932 * It's 2024 we always want an AD style response!
934 nt_version |= NETLOGON_NT_VERSION_AVOID_NT4EMUL;
936 nt_version |= map_ds_flags_to_nt_version(flags);
938 snprintf(my_acct_name,
939 sizeof(my_acct_name),
940 "%s$",
941 lp_netbios_name());
943 DEBUG(10,("process_dc_netbios\n"));
945 for (i=0; i<num_dcs; i++) {
946 uint16_t val;
948 generate_random_buffer((uint8_t *)&val, 2);
950 status = nbt_getdc(msg_ctx, 10, &dclist[i].sa.u.ss, domain_name,
951 NULL, my_acct_name, ACB_WSTRUST, nt_version,
952 mem_ctx, &nt_version, &dc_name, &r);
953 if (NT_STATUS_IS_OK(status)) {
954 store_cache = true;
955 namecache_store(dc_name,
956 NBT_NAME_SERVER,
958 &dclist[i].sa);
959 goto make_reply;
962 if (name_status_find(domain_name,
963 name_type,
964 NBT_NAME_SERVER,
965 &dclist[i].sa.u.ss,
966 tmp_dc_name))
968 struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1;
970 r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
971 NT_STATUS_HAVE_NO_MEMORY(r);
973 ZERO_STRUCT(logon1);
975 nt_version = NETLOGON_NT_VERSION_1;
977 logon1.nt_version = nt_version;
978 logon1.pdc_name = tmp_dc_name;
979 logon1.domain_name = talloc_strdup_upper(mem_ctx, domain_name);
980 NT_STATUS_HAVE_NO_MEMORY(logon1.domain_name);
982 r->data.nt4 = logon1;
983 r->ntver = nt_version;
985 map_netlogon_samlogon_response(r);
987 namecache_store(tmp_dc_name,
988 NBT_NAME_SERVER,
990 &dclist[i].sa);
992 goto make_reply;
996 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
998 make_reply:
1000 status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].sa,
1001 &r->data.nt5_ex, info);
1002 if (NT_STATUS_IS_OK(status) && store_cache) {
1003 return store_cldap_reply(mem_ctx, flags, &dclist[i].sa,
1004 nt_version, &r->data.nt5_ex);
1007 return status;
1010 /****************************************************************
1011 ****************************************************************/
1013 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
1014 struct messaging_context *msg_ctx,
1015 const char *domain_name,
1016 const struct GUID *domain_guid,
1017 uint32_t flags,
1018 const char *site_name,
1019 struct netr_DsRGetDCNameInfo **info)
1021 NTSTATUS status;
1022 struct ip_service_name *dclist = NULL;
1023 size_t num_dcs = 0;
1025 DEBUG(10,("dsgetdcname_rediscover\n"));
1027 if (flags & DS_IS_FLAT_NAME) {
1029 if (lp_disable_netbios()) {
1030 return NT_STATUS_NOT_SUPPORTED;
1033 status = discover_dc_netbios(mem_ctx, domain_name, flags,
1034 &dclist, &num_dcs);
1035 NT_STATUS_NOT_OK_RETURN(status);
1037 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags,
1038 dclist, num_dcs, info);
1041 if (flags & DS_IS_DNS_NAME) {
1043 status = discover_dc_dns(mem_ctx, domain_name, domain_guid,
1044 flags, site_name, &dclist, &num_dcs);
1045 NT_STATUS_NOT_OK_RETURN(status);
1047 return process_dc_dns(mem_ctx, domain_name, flags,
1048 dclist, num_dcs, info);
1051 status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags,
1052 site_name, &dclist, &num_dcs);
1054 if (NT_STATUS_IS_OK(status) && num_dcs != 0) {
1056 status = process_dc_dns(mem_ctx, domain_name, flags, dclist,
1057 num_dcs, info);
1058 if (NT_STATUS_IS_OK(status)) {
1059 return status;
1063 if (lp_disable_netbios()) {
1064 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1067 status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
1068 &num_dcs);
1069 NT_STATUS_NOT_OK_RETURN(status);
1071 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags, dclist,
1072 num_dcs, info);
1075 static bool is_closest_site(struct netr_DsRGetDCNameInfo *info)
1077 if (info->dc_flags & DS_SERVER_CLOSEST) {
1078 return true;
1081 if (!info->client_site_name) {
1082 return true;
1085 if (!info->dc_site_name) {
1086 return false;
1089 if (strcmp(info->client_site_name, info->dc_site_name) == 0) {
1090 return true;
1093 return false;
1096 /********************************************************************
1097 Internal dsgetdcname.
1098 ********************************************************************/
1100 static NTSTATUS dsgetdcname_internal(TALLOC_CTX *mem_ctx,
1101 struct messaging_context *msg_ctx,
1102 const char *domain_name,
1103 const struct GUID *domain_guid,
1104 const char *site_name,
1105 uint32_t flags,
1106 struct netr_DsRGetDCNameInfo **info)
1108 NTSTATUS status;
1109 struct netr_DsRGetDCNameInfo *myinfo = NULL;
1110 bool first = true;
1111 struct netr_DsRGetDCNameInfo *first_info = NULL;
1113 DEBUG(10,("dsgetdcname_internal: domain_name: %s, "
1114 "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
1115 domain_name,
1116 domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)",
1117 site_name ? site_name : "(null)", flags));
1119 *info = NULL;
1121 if (!check_allowed_required_flags(flags, site_name)) {
1122 DEBUG(0,("invalid flags specified\n"));
1123 return NT_STATUS_INVALID_PARAMETER;
1126 if (flags & DS_FORCE_REDISCOVERY) {
1127 goto rediscover;
1130 status = dsgetdcname_cached(mem_ctx, msg_ctx, domain_name, domain_guid,
1131 flags, site_name, &myinfo);
1132 if (NT_STATUS_IS_OK(status)) {
1133 *info = myinfo;
1134 goto done;
1137 if (flags & DS_BACKGROUND_ONLY) {
1138 goto done;
1141 rediscover:
1142 status = dsgetdcname_rediscover(mem_ctx, msg_ctx, domain_name,
1143 domain_guid, flags, site_name,
1144 &myinfo);
1146 done:
1147 if (!NT_STATUS_IS_OK(status)) {
1148 if (!first) {
1149 *info = first_info;
1150 return NT_STATUS_OK;
1152 return status;
1155 if (!first) {
1156 TALLOC_FREE(first_info);
1157 } else if (!is_closest_site(myinfo)) {
1158 first = false;
1159 first_info = myinfo;
1160 /* TODO: may use the next_closest_site here */
1161 site_name = myinfo->client_site_name;
1162 goto rediscover;
1165 *info = myinfo;
1166 return NT_STATUS_OK;
1169 /********************************************************************
1170 dsgetdcname.
1172 This will be the only public function here.
1173 ********************************************************************/
1175 NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
1176 struct messaging_context *msg_ctx,
1177 const char *domain_name,
1178 const struct GUID *domain_guid,
1179 const char *site_name,
1180 uint32_t flags,
1181 struct netr_DsRGetDCNameInfo **info)
1183 NTSTATUS status;
1184 const char *query_site = NULL;
1185 char *ptr_to_free = NULL;
1186 bool retry_query_with_null = false;
1188 if ((site_name == NULL) || (site_name[0] == '\0')) {
1189 ptr_to_free = sitename_fetch(mem_ctx, domain_name);
1190 if (ptr_to_free != NULL) {
1191 retry_query_with_null = true;
1193 query_site = ptr_to_free;
1194 } else {
1195 query_site = site_name;
1198 status = dsgetdcname_internal(mem_ctx,
1199 msg_ctx,
1200 domain_name,
1201 domain_guid,
1202 query_site,
1203 flags,
1204 info);
1206 TALLOC_FREE(ptr_to_free);
1208 if (!NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1209 return status;
1212 /* Should we try again with site_name == NULL ? */
1213 if (retry_query_with_null) {
1214 status = dsgetdcname_internal(mem_ctx,
1215 msg_ctx,
1216 domain_name,
1217 domain_guid,
1218 NULL,
1219 flags,
1220 info);
1223 return status;
1226 NTSTATUS dsgetonedcname(TALLOC_CTX *mem_ctx,
1227 struct messaging_context *msg_ctx,
1228 const char *domain_name,
1229 const char *dcname,
1230 uint32_t flags,
1231 struct netr_DsRGetDCNameInfo **info)
1233 NTSTATUS status;
1234 struct sockaddr_storage *addrs;
1235 unsigned int num_addrs, i;
1236 const char *hostname = strip_hostname(dcname);
1238 status = resolve_name_list(mem_ctx, hostname, 0x20,
1239 &addrs, &num_addrs);
1240 if (!NT_STATUS_IS_OK(status)) {
1241 return status;
1244 for (i = 0; i < num_addrs; i++) {
1246 bool ok;
1247 struct ip_service_name dclist;
1249 dclist.hostname = hostname;
1250 ok = sockaddr_storage_to_samba_sockaddr(&dclist.sa, &addrs[i]);
1251 if (!ok) {
1252 TALLOC_FREE(addrs);
1253 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1256 status = process_dc_dns(mem_ctx, domain_name, flags,
1257 &dclist, 1, info);
1258 if (NT_STATUS_IS_OK(status)) {
1259 TALLOC_FREE(addrs);
1260 return NT_STATUS_OK;
1263 if (lp_disable_netbios()) {
1264 continue;
1267 status = process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags,
1268 &dclist, 1, info);
1269 if (NT_STATUS_IS_OK(status)) {
1270 TALLOC_FREE(addrs);
1271 return NT_STATUS_OK;
1275 TALLOC_FREE(addrs);
1276 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;