2 Unix SMB/CIFS implementation.
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/>.
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"
37 #define DSGETDCNAME_CACHE_TTL 60*15
39 struct ip_service_name
{
40 struct samba_sockaddr sa
;
44 static NTSTATUS
make_dc_info_from_cldap_reply(
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 "));
123 DEBUGADD(lvl
,("\n"));
126 /****************************************************************
127 ****************************************************************/
129 static char *dsgetdcname_cache_key(TALLOC_CTX
*mem_ctx
, const char *domain
)
135 return talloc_asprintf_strupper_m(mem_ctx
, "DSGETDCNAME/DOMAIN/%s",
139 /****************************************************************
140 ****************************************************************/
142 static NTSTATUS
dsgetdcname_cache_delete(TALLOC_CTX
*mem_ctx
,
143 const char *domain_name
)
147 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
149 return NT_STATUS_NO_MEMORY
;
152 if (!gencache_del(key
)) {
153 return NT_STATUS_UNSUCCESSFUL
;
159 /****************************************************************
160 ****************************************************************/
162 static NTSTATUS
dsgetdcname_cache_store(TALLOC_CTX
*mem_ctx
,
163 const char *domain_name
,
170 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
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
,
187 struct samba_sockaddr
*sa
,
189 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
)
192 enum ndr_err_code ndr_err
;
194 char addr
[INET6_ADDRSTRLEN
];
196 print_sockaddr(addr
, sizeof(addr
), &sa
->u
.ss
);
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
;
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
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
,
234 if (!NT_STATUS_IS_OK(status
)) {
237 if (r
->client_site
) {
238 sitename_store(r
->domain_name
, r
->client_site
);
242 status
= dsgetdcname_cache_store(mem_ctx
, r
->dns_domain
, blob
);
243 if (!NT_STATUS_IS_OK(status
)) {
246 if (r
->client_site
) {
247 sitename_store(r
->dns_domain
, r
->client_site
);
251 status
= NT_STATUS_OK
;
254 data_blob_free(&blob
);
259 /****************************************************************
260 ****************************************************************/
262 static NTSTATUS
dsgetdcname_cache_fetch(TALLOC_CTX
*mem_ctx
,
263 const char *domain_name
,
264 const struct GUID
*domain_guid
,
266 struct netr_DsRGetDCNameInfo
**info_p
)
270 enum ndr_err_code ndr_err
;
271 struct netr_DsRGetDCNameInfo
*info
;
272 struct NETLOGON_SAM_LOGON_RESPONSE_EX r
;
275 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
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
);
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
,
301 if (!NT_STATUS_IS_OK(status
)) {
305 if (DEBUGLEVEL
>= 10) {
306 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo
, info
);
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
;
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
,
333 const char *site_name
,
334 struct netr_DsRGetDCNameInfo
**info
)
338 status
= dsgetdcname_cache_fetch(mem_ctx
, domain_name
, domain_guid
,
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",
344 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
347 if (flags
& DS_BACKGROUND_ONLY
) {
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
,
359 if (!NT_STATUS_IS_OK(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
) {
388 if (return_type
== (DS_RETURN_FLAT_NAME
|DS_RETURN_DNS_NAME
)) {
392 if (offered_type
== (DS_IS_DNS_NAME
|DS_IS_FLAT_NAME
)) {
396 if (query_type
== (DS_BACKGROUND_ONLY
|DS_FORCE_REDISCOVERY
)) {
401 if ((flags
& DS_RETURN_DNS_NAME
) && (!(flags
& DS_IP_REQUIRED
))) {
402 printf("gd: here5 \n");
409 /****************************************************************
410 ****************************************************************/
412 static NTSTATUS
discover_dc_netbios(TALLOC_CTX
*mem_ctx
,
413 const char *domain_name
,
415 struct ip_service_name
**returned_dclist
,
416 size_t *returned_count
)
419 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
420 struct samba_sockaddr
*salist
= NULL
;
422 struct ip_service_name
*dclist
= NULL
;
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
,
437 if (!NT_STATUS_IS_OK(status
)) {
438 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
442 dclist
= talloc_zero_array(mem_ctx
, struct ip_service_name
, count
);
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
),
456 r
->hostname
= talloc_strdup(mem_ctx
, addr
);
460 return NT_STATUS_NO_MEMORY
;
467 *returned_dclist
= dclist
;
468 *returned_count
= count
;
473 /****************************************************************
474 ****************************************************************/
476 static NTSTATUS
discover_dc_dns(TALLOC_CTX
*mem_ctx
,
477 const char *domain_name
,
478 const struct GUID
*domain_guid
,
480 const char *site_name
,
481 struct ip_service_name
**returned_dclist
,
482 size_t *return_count
)
486 struct dns_rr_srv
*dcs
= NULL
;
488 struct ip_service_name
*dclist
= NULL
;
489 size_t ret_count
= 0;
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
);
504 query
= ads_dns_query_string_dcs(mem_ctx
, domain_name
);
508 return NT_STATUS_NO_MEMORY
;
511 status
= ads_dns_query_srv(
513 lp_get_async_dns_timeout(),
519 if (!NT_STATUS_IS_OK(status
)) {
525 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
528 /* Check for integer wrap. */
529 if (numdcs
+ numdcs
< numdcs
) {
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
,
544 return NT_STATUS_NO_MEMORY
;
548 * First, copy the SRV record replies that
549 * have IP addresses returned with them.
552 for (i
= 0; i
< numdcs
; i
++) {
554 bool have_v4_addr
= false;
555 bool have_v6_addr
= false;
557 if (dcs
[i
].num_ips
== 0) {
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
)) {
574 dclist
[ret_count
].hostname
=
575 talloc_strdup(dclist
, dcs
[i
].hostname
);
576 ok
= sockaddr_storage_to_samba_sockaddr(
577 &dclist
[ret_count
].sa
,
582 return NT_STATUS_INVALID_PARAMETER
;
585 if (dcs
[i
].ss_s
[j
].ss_family
== AF_INET
) {
590 if (have_v4_addr
&& have_v6_addr
) {
599 if (ret_count
== 0) {
601 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
604 *returned_dclist
= dclist
;
605 *return_count
= ret_count
;
609 /****************************************************************
610 ****************************************************************/
612 static NTSTATUS
make_domain_controller_info(TALLOC_CTX
*mem_ctx
,
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
,
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
);
630 if (!(dc_unc
[0] == '\\' && dc_unc
[1] == '\\')) {
631 info
->dc_unc
= talloc_asprintf(mem_ctx
, "\\\\%s",
634 info
->dc_unc
= talloc_strdup(mem_ctx
, dc_unc
);
636 NT_STATUS_HAVE_NO_MEMORY(info
->dc_unc
);
640 if (!(dc_address
[0] == '\\' && dc_address
[1] == '\\')) {
641 info
->dc_address
= talloc_asprintf(mem_ctx
, "\\\\%s",
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
;
652 info
->domain_guid
= *domain_guid
;
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
;
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
,
676 NT_STATUS_HAVE_NO_MEMORY(info
->client_site_name
);
684 /****************************************************************
685 ****************************************************************/
687 static void map_dc_and_domain_names(uint32_t flags
,
689 const char *domain_name
,
690 const char *dns_dc_name
,
691 const char *dns_domain_name
,
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
;
706 case DS_RETURN_DNS_NAME
:
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
;
715 if (dc_name
&& domain_name
&&
716 *dc_name
&& *domain_name
) {
717 *hostname_p
= dc_name
;
718 *domain_p
= domain_name
;
724 /****************************************************************
725 ****************************************************************/
727 static NTSTATUS
make_dc_info_from_cldap_reply(
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
];
747 print_sockaddr(addr
, sizeof(addr
), &sa
->u
.ss
);
749 dc_address_type
= DS_ADDRESS_TYPE_INET
;
751 if (r
->sockaddr
.pdc_ip
) {
752 dc_address
= r
->sockaddr
.pdc_ip
;
753 dc_address_type
= DS_ADDRESS_TYPE_INET
;
755 dc_address
= r
->pdc_name
;
756 dc_address_type
= DS_ADDRESS_TYPE_NETBIOS
;
760 map_dc_and_domain_names(flags
,
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
,
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
;
814 /****************************************************************
815 ****************************************************************/
817 static NTSTATUS
process_dc_dns(TALLOC_CTX
*mem_ctx
,
818 const char *domain_name
,
820 struct ip_service_name
*dclist
,
822 struct netr_DsRGetDCNameInfo
**info
)
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
;
831 addrs
= talloc_array(mem_ctx
, struct tsocket_address
*, num_dcs
);
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
]);
840 status
= map_nt_error_from_unix(errno
);
845 status
= netlogon_pings(
847 lp_client_netlogon_ping_protocol(), /* proto */
849 num_dcs
, /* num_servers */
850 (struct netlogon_ping_filter
){
851 .ntversion
= nt_version
,
853 .domain
= domain_name
,
854 .required_flags
= flags
,
857 timeval_current_ofs(MAX(3, lp_ldap_timeout() / 2), 0),
860 if (!NT_STATUS_IS_OK(status
)) {
861 status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
865 for (i
= 0; i
< num_dcs
; i
++) {
866 struct netlogon_samlogon_response
*r
= responses
[i
];
871 if (r
->ntver
!= NETLOGON_NT_VERSION_5EX
) {
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
)) {
879 status
= store_cldap_reply(mem_ctx
,
884 if (!NT_STATUS_IS_OK(status
)) {
890 status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
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
,
906 struct ip_service_name
*dclist
,
908 struct netr_DsRGetDCNameInfo
**info
)
910 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
913 const char *dc_name
= NULL
;
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
),
943 DEBUG(10,("process_dc_netbios\n"));
945 for (i
=0; i
<num_dcs
; i
++) {
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
)) {
955 namecache_store(dc_name
,
962 if (name_status_find(domain_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
);
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
,
996 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
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
);
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
,
1018 const char *site_name
,
1019 struct netr_DsRGetDCNameInfo
**info
)
1022 struct ip_service_name
*dclist
= NULL
;
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
,
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
,
1058 if (NT_STATUS_IS_OK(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
,
1069 NT_STATUS_NOT_OK_RETURN(status
);
1071 return process_dc_netbios(mem_ctx
, msg_ctx
, domain_name
, flags
, dclist
,
1075 static bool is_closest_site(struct netr_DsRGetDCNameInfo
*info
)
1077 if (info
->dc_flags
& DS_SERVER_CLOSEST
) {
1081 if (!info
->client_site_name
) {
1085 if (!info
->dc_site_name
) {
1089 if (strcmp(info
->client_site_name
, info
->dc_site_name
) == 0) {
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
,
1106 struct netr_DsRGetDCNameInfo
**info
)
1109 struct netr_DsRGetDCNameInfo
*myinfo
= NULL
;
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",
1116 domain_guid
? GUID_string(mem_ctx
, domain_guid
) : "(null)",
1117 site_name
? site_name
: "(null)", flags
));
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
) {
1130 status
= dsgetdcname_cached(mem_ctx
, msg_ctx
, domain_name
, domain_guid
,
1131 flags
, site_name
, &myinfo
);
1132 if (NT_STATUS_IS_OK(status
)) {
1137 if (flags
& DS_BACKGROUND_ONLY
) {
1142 status
= dsgetdcname_rediscover(mem_ctx
, msg_ctx
, domain_name
,
1143 domain_guid
, flags
, site_name
,
1147 if (!NT_STATUS_IS_OK(status
)) {
1150 return NT_STATUS_OK
;
1156 TALLOC_FREE(first_info
);
1157 } else if (!is_closest_site(myinfo
)) {
1159 first_info
= myinfo
;
1160 /* TODO: may use the next_closest_site here */
1161 site_name
= myinfo
->client_site_name
;
1166 return NT_STATUS_OK
;
1169 /********************************************************************
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
,
1181 struct netr_DsRGetDCNameInfo
**info
)
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
;
1195 query_site
= site_name
;
1198 status
= dsgetdcname_internal(mem_ctx
,
1206 TALLOC_FREE(ptr_to_free
);
1208 if (!NT_STATUS_EQUAL(status
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
)) {
1212 /* Should we try again with site_name == NULL ? */
1213 if (retry_query_with_null
) {
1214 status
= dsgetdcname_internal(mem_ctx
,
1226 NTSTATUS
dsgetonedcname(TALLOC_CTX
*mem_ctx
,
1227 struct messaging_context
*msg_ctx
,
1228 const char *domain_name
,
1231 struct netr_DsRGetDCNameInfo
**info
)
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
)) {
1244 for (i
= 0; i
< num_addrs
; i
++) {
1247 struct ip_service_name dclist
;
1249 dclist
.hostname
= hostname
;
1250 ok
= sockaddr_storage_to_samba_sockaddr(&dclist
.sa
, &addrs
[i
]);
1253 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1256 status
= process_dc_dns(mem_ctx
, domain_name
, flags
,
1258 if (NT_STATUS_IS_OK(status
)) {
1260 return NT_STATUS_OK
;
1263 if (lp_disable_netbios()) {
1267 status
= process_dc_netbios(mem_ctx
, msg_ctx
, domain_name
, flags
,
1269 if (NT_STATUS_IS_OK(status
)) {
1271 return NT_STATUS_OK
;
1276 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;