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 "lib/addns/dnsquery_srv.h"
30 #include "libsmb/clidgram.h"
31 #include "lib/gencache.h"
32 #include "lib/util/util_net.h"
35 #define DSGETDCNAME_CACHE_TTL 60*15
37 struct ip_service_name
{
38 struct samba_sockaddr sa
;
42 static NTSTATUS
make_dc_info_from_cldap_reply(
45 const struct samba_sockaddr
*sa
,
46 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
,
47 struct netr_DsRGetDCNameInfo
**info
);
49 /****************************************************************
50 ****************************************************************/
52 static void debug_dsdcinfo_flags(int lvl
, uint32_t flags
)
54 DEBUG(lvl
,("debug_dsdcinfo_flags: 0x%08x\n\t", flags
));
56 if (flags
& DS_FORCE_REDISCOVERY
)
57 DEBUGADD(lvl
,("DS_FORCE_REDISCOVERY "));
58 if (flags
& 0x000000002)
59 DEBUGADD(lvl
,("0x00000002 "));
60 if (flags
& 0x000000004)
61 DEBUGADD(lvl
,("0x00000004 "));
62 if (flags
& 0x000000008)
63 DEBUGADD(lvl
,("0x00000008 "));
64 if (flags
& DS_DIRECTORY_SERVICE_REQUIRED
)
65 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_REQUIRED "));
66 if (flags
& DS_DIRECTORY_SERVICE_PREFERRED
)
67 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_PREFERRED "));
68 if (flags
& DS_GC_SERVER_REQUIRED
)
69 DEBUGADD(lvl
,("DS_GC_SERVER_REQUIRED "));
70 if (flags
& DS_PDC_REQUIRED
)
71 DEBUGADD(lvl
,("DS_PDC_REQUIRED "));
72 if (flags
& DS_BACKGROUND_ONLY
)
73 DEBUGADD(lvl
,("DS_BACKGROUND_ONLY "));
74 if (flags
& DS_IP_REQUIRED
)
75 DEBUGADD(lvl
,("DS_IP_REQUIRED "));
76 if (flags
& DS_KDC_REQUIRED
)
77 DEBUGADD(lvl
,("DS_KDC_REQUIRED "));
78 if (flags
& DS_TIMESERV_REQUIRED
)
79 DEBUGADD(lvl
,("DS_TIMESERV_REQUIRED "));
80 if (flags
& DS_WRITABLE_REQUIRED
)
81 DEBUGADD(lvl
,("DS_WRITABLE_REQUIRED "));
82 if (flags
& DS_GOOD_TIMESERV_PREFERRED
)
83 DEBUGADD(lvl
,("DS_GOOD_TIMESERV_PREFERRED "));
84 if (flags
& DS_AVOID_SELF
)
85 DEBUGADD(lvl
,("DS_AVOID_SELF "));
86 if (flags
& DS_ONLY_LDAP_NEEDED
)
87 DEBUGADD(lvl
,("DS_ONLY_LDAP_NEEDED "));
88 if (flags
& DS_IS_FLAT_NAME
)
89 DEBUGADD(lvl
,("DS_IS_FLAT_NAME "));
90 if (flags
& DS_IS_DNS_NAME
)
91 DEBUGADD(lvl
,("DS_IS_DNS_NAME "));
92 if (flags
& DS_TRY_NEXTCLOSEST_SITE
)
93 DEBUGADD(lvl
,("DS_TRY_NEXTCLOSEST_SITE "));
94 if (flags
& DS_DIRECTORY_SERVICE_6_REQUIRED
)
95 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_6_REQUIRED "));
96 if (flags
& DS_WEB_SERVICE_REQUIRED
)
97 DEBUGADD(lvl
,("DS_WEB_SERVICE_REQUIRED "));
98 if (flags
& DS_DIRECTORY_SERVICE_8_REQUIRED
)
99 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_8_REQUIRED "));
100 if (flags
& DS_DIRECTORY_SERVICE_9_REQUIRED
)
101 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_9_REQUIRED "));
102 if (flags
& DS_DIRECTORY_SERVICE_10_REQUIRED
)
103 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_10_REQUIRED "));
104 if (flags
& 0x01000000)
105 DEBUGADD(lvl
,("0x01000000 "));
106 if (flags
& 0x02000000)
107 DEBUGADD(lvl
,("0x02000000 "));
108 if (flags
& 0x04000000)
109 DEBUGADD(lvl
,("0x04000000 "));
110 if (flags
& 0x08000000)
111 DEBUGADD(lvl
,("0x08000000 "));
112 if (flags
& 0x10000000)
113 DEBUGADD(lvl
,("0x10000000 "));
114 if (flags
& 0x20000000)
115 DEBUGADD(lvl
,("0x20000000 "));
116 if (flags
& DS_RETURN_DNS_NAME
)
117 DEBUGADD(lvl
,("DS_RETURN_DNS_NAME "));
118 if (flags
& DS_RETURN_FLAT_NAME
)
119 DEBUGADD(lvl
,("DS_RETURN_FLAT_NAME "));
121 DEBUGADD(lvl
,("\n"));
124 /****************************************************************
125 ****************************************************************/
127 static char *dsgetdcname_cache_key(TALLOC_CTX
*mem_ctx
, const char *domain
)
133 return talloc_asprintf_strupper_m(mem_ctx
, "DSGETDCNAME/DOMAIN/%s",
137 /****************************************************************
138 ****************************************************************/
140 static NTSTATUS
dsgetdcname_cache_delete(TALLOC_CTX
*mem_ctx
,
141 const char *domain_name
)
145 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
147 return NT_STATUS_NO_MEMORY
;
150 if (!gencache_del(key
)) {
151 return NT_STATUS_UNSUCCESSFUL
;
157 /****************************************************************
158 ****************************************************************/
160 static NTSTATUS
dsgetdcname_cache_store(TALLOC_CTX
*mem_ctx
,
161 const char *domain_name
,
168 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
170 return NT_STATUS_NO_MEMORY
;
173 expire_time
= time(NULL
) + DSGETDCNAME_CACHE_TTL
;
175 ret
= gencache_set_data_blob(key
, blob
, expire_time
);
177 return ret
? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
180 /****************************************************************
181 ****************************************************************/
183 static NTSTATUS
store_cldap_reply(TALLOC_CTX
*mem_ctx
,
185 struct samba_sockaddr
*sa
,
187 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
)
190 enum ndr_err_code ndr_err
;
192 char addr
[INET6_ADDRSTRLEN
];
194 print_sockaddr(addr
, sizeof(addr
), &sa
->u
.ss
);
197 r
->sockaddr_size
= 0x10; /* the w32 winsock addr size */
198 r
->sockaddr
.sockaddr_family
= 2; /* AF_INET */
199 r
->sockaddr
.pdc_ip
= talloc_strdup(mem_ctx
, addr
);
201 ndr_err
= ndr_push_struct_blob(&blob
, mem_ctx
, r
,
202 (ndr_push_flags_fn_t
)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX
);
203 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
204 return ndr_map_error2ntstatus(ndr_err
);
207 if (r
->domain_name
) {
208 status
= dsgetdcname_cache_store(mem_ctx
, r
->domain_name
,
210 if (!NT_STATUS_IS_OK(status
)) {
213 if (r
->client_site
) {
214 sitename_store(r
->domain_name
, r
->client_site
);
218 status
= dsgetdcname_cache_store(mem_ctx
, r
->dns_domain
, blob
);
219 if (!NT_STATUS_IS_OK(status
)) {
222 if (r
->client_site
) {
223 sitename_store(r
->dns_domain
, r
->client_site
);
227 status
= NT_STATUS_OK
;
230 data_blob_free(&blob
);
235 /****************************************************************
236 ****************************************************************/
238 static uint32_t get_cldap_reply_server_flags(struct netlogon_samlogon_response
*r
,
241 switch (nt_version
& 0x0000001f) {
251 return r
->data
.nt5
.server_type
;
256 return r
->data
.nt5_ex
.server_type
;
265 return r
->data
.nt5_ex
.server_type
;
275 return r
->data
.nt5_ex
.server_type
;
279 return r
->data
.nt5_ex
.server_type
;
285 /****************************************************************
286 ****************************************************************/
288 static NTSTATUS
dsgetdcname_cache_fetch(TALLOC_CTX
*mem_ctx
,
289 const char *domain_name
,
290 const struct GUID
*domain_guid
,
292 struct netr_DsRGetDCNameInfo
**info_p
)
296 enum ndr_err_code ndr_err
;
297 struct netr_DsRGetDCNameInfo
*info
;
298 struct NETLOGON_SAM_LOGON_RESPONSE_EX r
;
301 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
303 return NT_STATUS_NO_MEMORY
;
306 if (!gencache_get_data_blob(key
, NULL
, &blob
, NULL
, NULL
)) {
307 return NT_STATUS_NOT_FOUND
;
310 info
= talloc_zero(mem_ctx
, struct netr_DsRGetDCNameInfo
);
312 data_blob_free(&blob
);
313 return NT_STATUS_NO_MEMORY
;
316 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &r
,
317 (ndr_pull_flags_fn_t
)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX
);
319 data_blob_free(&blob
);
320 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
321 dsgetdcname_cache_delete(mem_ctx
, domain_name
);
322 return ndr_map_error2ntstatus(ndr_err
);
325 status
= make_dc_info_from_cldap_reply(mem_ctx
, flags
, NULL
,
327 if (!NT_STATUS_IS_OK(status
)) {
331 if (DEBUGLEVEL
>= 10) {
332 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo
, info
);
336 if (!check_cldap_reply_required_flags(info
->dc_flags
, flags
)) {
337 DEBUG(10,("invalid flags\n"));
338 return NT_STATUS_INVALID_PARAMETER
;
341 if ((flags
& DS_IP_REQUIRED
) &&
342 (info
->dc_address_type
!= DS_ADDRESS_TYPE_INET
)) {
343 return NT_STATUS_INVALID_PARAMETER_MIX
;
351 /****************************************************************
352 ****************************************************************/
354 static NTSTATUS
dsgetdcname_cached(TALLOC_CTX
*mem_ctx
,
355 struct messaging_context
*msg_ctx
,
356 const char *domain_name
,
357 const struct GUID
*domain_guid
,
359 const char *site_name
,
360 struct netr_DsRGetDCNameInfo
**info
)
364 status
= dsgetdcname_cache_fetch(mem_ctx
, domain_name
, domain_guid
,
366 if (!NT_STATUS_IS_OK(status
)
367 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
368 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
370 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
373 if (flags
& DS_BACKGROUND_ONLY
) {
377 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
378 struct netr_DsRGetDCNameInfo
*dc_info
;
380 status
= dsgetdcname(mem_ctx
, msg_ctx
, domain_name
,
381 domain_guid
, site_name
,
382 flags
| DS_FORCE_REDISCOVERY
,
385 if (!NT_STATUS_IS_OK(status
)) {
395 /****************************************************************
396 ****************************************************************/
398 static bool check_allowed_required_flags(uint32_t flags
,
399 const char *site_name
)
401 uint32_t return_type
= flags
& (DS_RETURN_FLAT_NAME
|DS_RETURN_DNS_NAME
);
402 uint32_t offered_type
= flags
& (DS_IS_FLAT_NAME
|DS_IS_DNS_NAME
);
403 uint32_t query_type
= flags
& (DS_BACKGROUND_ONLY
|DS_FORCE_REDISCOVERY
);
405 /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
406 * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
408 debug_dsdcinfo_flags(10, flags
);
410 if ((flags
& DS_TRY_NEXTCLOSEST_SITE
) && site_name
) {
414 if (return_type
== (DS_RETURN_FLAT_NAME
|DS_RETURN_DNS_NAME
)) {
418 if (offered_type
== (DS_IS_DNS_NAME
|DS_IS_FLAT_NAME
)) {
422 if (query_type
== (DS_BACKGROUND_ONLY
|DS_FORCE_REDISCOVERY
)) {
427 if ((flags
& DS_RETURN_DNS_NAME
) && (!(flags
& DS_IP_REQUIRED
))) {
428 printf("gd: here5 \n");
435 /****************************************************************
436 ****************************************************************/
438 static NTSTATUS
discover_dc_netbios(TALLOC_CTX
*mem_ctx
,
439 const char *domain_name
,
441 struct ip_service_name
**returned_dclist
,
442 size_t *returned_count
)
445 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
446 struct samba_sockaddr
*salist
= NULL
;
448 struct ip_service_name
*dclist
= NULL
;
450 static const char *resolve_order
[] = { "lmhosts", "wins", "bcast", NULL
};
452 if (flags
& DS_PDC_REQUIRED
) {
453 name_type
= NBT_NAME_PDC
;
456 status
= internal_resolve_name(mem_ctx
,
463 if (!NT_STATUS_IS_OK(status
)) {
464 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
468 dclist
= talloc_zero_array(mem_ctx
, struct ip_service_name
, count
);
471 return NT_STATUS_NO_MEMORY
;
474 for (i
=0; i
<count
; i
++) {
475 char addr
[INET6_ADDRSTRLEN
];
476 struct ip_service_name
*r
= &dclist
[i
];
478 print_sockaddr(addr
, sizeof(addr
),
482 r
->hostname
= talloc_strdup(mem_ctx
, addr
);
486 return NT_STATUS_NO_MEMORY
;
493 *returned_dclist
= dclist
;
494 *returned_count
= count
;
499 /****************************************************************
500 ****************************************************************/
502 static NTSTATUS
discover_dc_dns(TALLOC_CTX
*mem_ctx
,
503 const char *domain_name
,
504 const struct GUID
*domain_guid
,
506 const char *site_name
,
507 struct ip_service_name
**returned_dclist
,
508 size_t *return_count
)
512 struct dns_rr_srv
*dcs
= NULL
;
514 struct ip_service_name
*dclist
= NULL
;
515 size_t ret_count
= 0;
518 if (flags
& DS_PDC_REQUIRED
) {
519 query
= ads_dns_query_string_pdc(mem_ctx
, domain_name
);
520 } else if (flags
& DS_GC_SERVER_REQUIRED
) {
521 query
= ads_dns_query_string_gcs(mem_ctx
, domain_name
);
522 } else if (flags
& DS_KDC_REQUIRED
) {
523 query
= ads_dns_query_string_kdcs(mem_ctx
, domain_name
);
524 } else if (flags
& DS_DIRECTORY_SERVICE_REQUIRED
) {
525 query
= ads_dns_query_string_dcs(mem_ctx
, domain_name
);
526 } else if (domain_guid
) {
527 query
= ads_dns_query_string_dcs_guid(
528 mem_ctx
, domain_guid
, domain_name
);
530 query
= ads_dns_query_string_dcs(mem_ctx
, domain_name
);
534 return NT_STATUS_NO_MEMORY
;
537 status
= ads_dns_query_srv(
539 lp_get_async_dns_timeout(),
545 if (!NT_STATUS_IS_OK(status
)) {
551 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
555 * We're only returning one address per
556 * DC name, so just allocate size numdcs.
559 dclist
= talloc_zero_array(mem_ctx
,
560 struct ip_service_name
,
564 return NT_STATUS_NO_MEMORY
;
568 * First, copy the SRV record replies that
569 * have IP addresses returned with them.
572 for (i
= 0; i
< numdcs
; i
++) {
575 if (dcs
[i
].num_ips
== 0) {
579 dclist
[ret_count
].hostname
=
580 talloc_move(dclist
, &dcs
[i
].hostname
);
583 * Pick the first IPv4 address,
584 * if none pick the first address.
586 * This is different from the previous
587 * code which picked a 'next ip' address
588 * each time, incrementing an index.
589 * Too complex to maintain :-(.
591 for (j
= 0; j
< dcs
[i
].num_ips
; j
++) {
592 if (dcs
[i
].ss_s
[j
].ss_family
== AF_INET
) {
594 ok
= sockaddr_storage_to_samba_sockaddr(
595 &dclist
[ret_count
].sa
,
600 return NT_STATUS_INVALID_PARAMETER
;
605 if (j
== dcs
[i
].num_ips
) {
606 /* No IPv4- use the first IPv6 addr. */
608 ok
= sockaddr_storage_to_samba_sockaddr(
609 &dclist
[ret_count
].sa
,
614 return NT_STATUS_INVALID_PARAMETER
;
622 if (ret_count
== 0) {
624 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
627 *returned_dclist
= dclist
;
628 *return_count
= ret_count
;
632 /****************************************************************
633 ****************************************************************/
635 static NTSTATUS
make_domain_controller_info(TALLOC_CTX
*mem_ctx
,
637 const char *dc_address
,
638 uint32_t dc_address_type
,
639 const struct GUID
*domain_guid
,
640 const char *domain_name
,
641 const char *forest_name
,
643 const char *dc_site_name
,
644 const char *client_site_name
,
645 struct netr_DsRGetDCNameInfo
**info_out
)
647 struct netr_DsRGetDCNameInfo
*info
;
649 info
= talloc_zero(mem_ctx
, struct netr_DsRGetDCNameInfo
);
650 NT_STATUS_HAVE_NO_MEMORY(info
);
653 if (!(dc_unc
[0] == '\\' && dc_unc
[1] == '\\')) {
654 info
->dc_unc
= talloc_asprintf(mem_ctx
, "\\\\%s",
657 info
->dc_unc
= talloc_strdup(mem_ctx
, dc_unc
);
659 NT_STATUS_HAVE_NO_MEMORY(info
->dc_unc
);
663 if (!(dc_address
[0] == '\\' && dc_address
[1] == '\\')) {
664 info
->dc_address
= talloc_asprintf(mem_ctx
, "\\\\%s",
667 info
->dc_address
= talloc_strdup(mem_ctx
, dc_address
);
669 NT_STATUS_HAVE_NO_MEMORY(info
->dc_address
);
672 info
->dc_address_type
= dc_address_type
;
675 info
->domain_guid
= *domain_guid
;
679 info
->domain_name
= talloc_strdup(mem_ctx
, domain_name
);
680 NT_STATUS_HAVE_NO_MEMORY(info
->domain_name
);
683 if (forest_name
&& *forest_name
) {
684 info
->forest_name
= talloc_strdup(mem_ctx
, forest_name
);
685 NT_STATUS_HAVE_NO_MEMORY(info
->forest_name
);
686 flags
|= DS_DNS_FOREST_ROOT
;
689 info
->dc_flags
= flags
;
692 info
->dc_site_name
= talloc_strdup(mem_ctx
, dc_site_name
);
693 NT_STATUS_HAVE_NO_MEMORY(info
->dc_site_name
);
696 if (client_site_name
) {
697 info
->client_site_name
= talloc_strdup(mem_ctx
,
699 NT_STATUS_HAVE_NO_MEMORY(info
->client_site_name
);
707 /****************************************************************
708 ****************************************************************/
710 static void map_dc_and_domain_names(uint32_t flags
,
712 const char *domain_name
,
713 const char *dns_dc_name
,
714 const char *dns_domain_name
,
716 const char **hostname_p
,
717 const char **domain_p
)
719 switch (flags
& 0xf0000000) {
720 case DS_RETURN_FLAT_NAME
:
721 if (dc_name
&& domain_name
&&
722 *dc_name
&& *domain_name
) {
723 *hostname_p
= dc_name
;
724 *domain_p
= domain_name
;
729 case DS_RETURN_DNS_NAME
:
731 if (dns_dc_name
&& dns_domain_name
&&
732 *dns_dc_name
&& *dns_domain_name
) {
733 *hostname_p
= dns_dc_name
;
734 *domain_p
= dns_domain_name
;
735 *dc_flags
|= DS_DNS_DOMAIN
| DS_DNS_CONTROLLER
;
738 if (dc_name
&& domain_name
&&
739 *dc_name
&& *domain_name
) {
740 *hostname_p
= dc_name
;
741 *domain_p
= domain_name
;
747 /****************************************************************
748 ****************************************************************/
750 static NTSTATUS
make_dc_info_from_cldap_reply(
753 const struct samba_sockaddr
*sa
,
754 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
,
755 struct netr_DsRGetDCNameInfo
**info
)
757 const char *dc_hostname
= NULL
;
758 const char *dc_domain_name
= NULL
;
759 const char *dc_address
= NULL
;
760 const char *dc_forest
= NULL
;
761 uint32_t dc_address_type
= 0;
762 uint32_t dc_flags
= 0;
763 struct GUID
*dc_domain_guid
= NULL
;
764 const char *dc_server_site
= NULL
;
765 const char *dc_client_site
= NULL
;
767 char addr
[INET6_ADDRSTRLEN
];
770 print_sockaddr(addr
, sizeof(addr
), &sa
->u
.ss
);
772 dc_address_type
= DS_ADDRESS_TYPE_INET
;
774 if (r
->sockaddr
.pdc_ip
) {
775 dc_address
= r
->sockaddr
.pdc_ip
;
776 dc_address_type
= DS_ADDRESS_TYPE_INET
;
778 dc_address
= r
->pdc_name
;
779 dc_address_type
= DS_ADDRESS_TYPE_NETBIOS
;
783 map_dc_and_domain_names(flags
,
792 dc_flags
|= r
->server_type
;
793 dc_forest
= r
->forest
;
794 dc_domain_guid
= &r
->domain_uuid
;
795 dc_server_site
= r
->server_site
;
796 dc_client_site
= r
->client_site
;
798 return make_domain_controller_info(mem_ctx
,
811 /****************************************************************
812 ****************************************************************/
814 static uint32_t map_ds_flags_to_nt_version(uint32_t flags
)
816 uint32_t nt_version
= 0;
818 if (flags
& DS_PDC_REQUIRED
) {
819 nt_version
|= NETLOGON_NT_VERSION_PDC
;
822 if (flags
& DS_GC_SERVER_REQUIRED
) {
823 nt_version
|= NETLOGON_NT_VERSION_GC
;
826 if (flags
& DS_TRY_NEXTCLOSEST_SITE
) {
827 nt_version
|= NETLOGON_NT_VERSION_WITH_CLOSEST_SITE
;
830 if (flags
& DS_IP_REQUIRED
) {
831 nt_version
|= NETLOGON_NT_VERSION_IP
;
837 /****************************************************************
838 ****************************************************************/
840 static NTSTATUS
process_dc_dns(TALLOC_CTX
*mem_ctx
,
841 const char *domain_name
,
843 struct ip_service_name
*dclist
,
845 struct netr_DsRGetDCNameInfo
**info
)
848 bool valid_dc
= false;
849 struct netlogon_samlogon_response
*r
= NULL
;
850 uint32_t nt_version
= NETLOGON_NT_VERSION_5
|
851 NETLOGON_NT_VERSION_5EX
;
852 uint32_t ret_flags
= 0;
855 nt_version
|= map_ds_flags_to_nt_version(flags
);
857 for (i
=0; i
<num_dcs
; i
++) {
858 char addr
[INET6_ADDRSTRLEN
];
860 print_sockaddr(addr
, sizeof(addr
), &dclist
[i
].sa
.u
.ss
);
862 DEBUG(10,("LDAP ping to %s (%s)\n", dclist
[i
].hostname
, addr
));
864 if (ads_cldap_netlogon(mem_ctx
, &dclist
[i
].sa
.u
.ss
,
869 nt_version
= r
->ntver
;
870 ret_flags
= get_cldap_reply_server_flags(r
, nt_version
);
872 if (check_cldap_reply_required_flags(ret_flags
, flags
)) {
882 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
885 status
= make_dc_info_from_cldap_reply(mem_ctx
, flags
, &dclist
[i
].sa
,
886 &r
->data
.nt5_ex
, info
);
887 if (NT_STATUS_IS_OK(status
)) {
888 return store_cldap_reply(mem_ctx
, flags
, &dclist
[i
].sa
,
889 nt_version
, &r
->data
.nt5_ex
);
895 /****************************************************************
896 ****************************************************************/
898 /****************************************************************
899 ****************************************************************/
901 static NTSTATUS
process_dc_netbios(TALLOC_CTX
*mem_ctx
,
902 struct messaging_context
*msg_ctx
,
903 const char *domain_name
,
905 struct ip_service_name
*dclist
,
907 struct netr_DsRGetDCNameInfo
**info
)
909 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
912 const char *dc_name
= NULL
;
914 struct netlogon_samlogon_response
*r
= NULL
;
915 bool store_cache
= false;
916 uint32_t nt_version
= NETLOGON_NT_VERSION_1
|
917 NETLOGON_NT_VERSION_5
|
918 NETLOGON_NT_VERSION_5EX_WITH_IP
;
919 size_t len
= strlen(lp_netbios_name());
920 char my_acct_name
[len
+2];
922 if (msg_ctx
== NULL
) {
923 return NT_STATUS_INVALID_PARAMETER
;
926 if (flags
& DS_PDC_REQUIRED
) {
927 name_type
= NBT_NAME_PDC
;
930 nt_version
|= map_ds_flags_to_nt_version(flags
);
932 snprintf(my_acct_name
,
933 sizeof(my_acct_name
),
937 DEBUG(10,("process_dc_netbios\n"));
939 for (i
=0; i
<num_dcs
; i
++) {
942 generate_random_buffer((uint8_t *)&val
, 2);
944 status
= nbt_getdc(msg_ctx
, 10, &dclist
[i
].sa
.u
.ss
, domain_name
,
945 NULL
, my_acct_name
, ACB_WSTRUST
, nt_version
,
946 mem_ctx
, &nt_version
, &dc_name
, &r
);
947 if (NT_STATUS_IS_OK(status
)) {
949 namecache_store(dc_name
,
956 if (name_status_find(domain_name
,
962 struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1
;
964 r
= talloc_zero(mem_ctx
, struct netlogon_samlogon_response
);
965 NT_STATUS_HAVE_NO_MEMORY(r
);
969 nt_version
= NETLOGON_NT_VERSION_1
;
971 logon1
.nt_version
= nt_version
;
972 logon1
.pdc_name
= tmp_dc_name
;
973 logon1
.domain_name
= talloc_strdup_upper(mem_ctx
, domain_name
);
974 NT_STATUS_HAVE_NO_MEMORY(logon1
.domain_name
);
976 r
->data
.nt4
= logon1
;
977 r
->ntver
= nt_version
;
979 map_netlogon_samlogon_response(r
);
981 namecache_store(tmp_dc_name
,
990 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
994 status
= make_dc_info_from_cldap_reply(mem_ctx
, flags
, &dclist
[i
].sa
,
995 &r
->data
.nt5_ex
, info
);
996 if (NT_STATUS_IS_OK(status
) && store_cache
) {
997 return store_cldap_reply(mem_ctx
, flags
, &dclist
[i
].sa
,
998 nt_version
, &r
->data
.nt5_ex
);
1004 /****************************************************************
1005 ****************************************************************/
1007 static NTSTATUS
dsgetdcname_rediscover(TALLOC_CTX
*mem_ctx
,
1008 struct messaging_context
*msg_ctx
,
1009 const char *domain_name
,
1010 const struct GUID
*domain_guid
,
1012 const char *site_name
,
1013 struct netr_DsRGetDCNameInfo
**info
)
1016 struct ip_service_name
*dclist
= NULL
;
1019 DEBUG(10,("dsgetdcname_rediscover\n"));
1021 if (flags
& DS_IS_FLAT_NAME
) {
1023 if (lp_disable_netbios()) {
1024 return NT_STATUS_NOT_SUPPORTED
;
1027 status
= discover_dc_netbios(mem_ctx
, domain_name
, flags
,
1029 NT_STATUS_NOT_OK_RETURN(status
);
1031 return process_dc_netbios(mem_ctx
, msg_ctx
, domain_name
, flags
,
1032 dclist
, num_dcs
, info
);
1035 if (flags
& DS_IS_DNS_NAME
) {
1037 status
= discover_dc_dns(mem_ctx
, domain_name
, domain_guid
,
1038 flags
, site_name
, &dclist
, &num_dcs
);
1039 NT_STATUS_NOT_OK_RETURN(status
);
1041 return process_dc_dns(mem_ctx
, domain_name
, flags
,
1042 dclist
, num_dcs
, info
);
1045 status
= discover_dc_dns(mem_ctx
, domain_name
, domain_guid
, flags
,
1046 site_name
, &dclist
, &num_dcs
);
1048 if (NT_STATUS_IS_OK(status
) && num_dcs
!= 0) {
1050 status
= process_dc_dns(mem_ctx
, domain_name
, flags
, dclist
,
1052 if (NT_STATUS_IS_OK(status
)) {
1057 if (lp_disable_netbios()) {
1058 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1061 status
= discover_dc_netbios(mem_ctx
, domain_name
, flags
, &dclist
,
1063 NT_STATUS_NOT_OK_RETURN(status
);
1065 return process_dc_netbios(mem_ctx
, msg_ctx
, domain_name
, flags
, dclist
,
1069 static bool is_closest_site(struct netr_DsRGetDCNameInfo
*info
)
1071 if (info
->dc_flags
& DS_SERVER_CLOSEST
) {
1075 if (!info
->client_site_name
) {
1079 if (!info
->dc_site_name
) {
1083 if (strcmp(info
->client_site_name
, info
->dc_site_name
) == 0) {
1090 /********************************************************************
1091 Internal dsgetdcname.
1092 ********************************************************************/
1094 static NTSTATUS
dsgetdcname_internal(TALLOC_CTX
*mem_ctx
,
1095 struct messaging_context
*msg_ctx
,
1096 const char *domain_name
,
1097 const struct GUID
*domain_guid
,
1098 const char *site_name
,
1100 struct netr_DsRGetDCNameInfo
**info
)
1103 struct netr_DsRGetDCNameInfo
*myinfo
= NULL
;
1105 struct netr_DsRGetDCNameInfo
*first_info
= NULL
;
1107 DEBUG(10,("dsgetdcname_internal: domain_name: %s, "
1108 "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
1110 domain_guid
? GUID_string(mem_ctx
, domain_guid
) : "(null)",
1111 site_name
? site_name
: "(null)", flags
));
1115 if (!check_allowed_required_flags(flags
, site_name
)) {
1116 DEBUG(0,("invalid flags specified\n"));
1117 return NT_STATUS_INVALID_PARAMETER
;
1120 if (flags
& DS_FORCE_REDISCOVERY
) {
1124 status
= dsgetdcname_cached(mem_ctx
, msg_ctx
, domain_name
, domain_guid
,
1125 flags
, site_name
, &myinfo
);
1126 if (NT_STATUS_IS_OK(status
)) {
1131 if (flags
& DS_BACKGROUND_ONLY
) {
1136 status
= dsgetdcname_rediscover(mem_ctx
, msg_ctx
, domain_name
,
1137 domain_guid
, flags
, site_name
,
1141 if (!NT_STATUS_IS_OK(status
)) {
1144 return NT_STATUS_OK
;
1150 TALLOC_FREE(first_info
);
1151 } else if (!is_closest_site(myinfo
)) {
1153 first_info
= myinfo
;
1154 /* TODO: may use the next_closest_site here */
1155 site_name
= myinfo
->client_site_name
;
1160 return NT_STATUS_OK
;
1163 /********************************************************************
1166 This will be the only public function here.
1167 ********************************************************************/
1169 NTSTATUS
dsgetdcname(TALLOC_CTX
*mem_ctx
,
1170 struct messaging_context
*msg_ctx
,
1171 const char *domain_name
,
1172 const struct GUID
*domain_guid
,
1173 const char *site_name
,
1175 struct netr_DsRGetDCNameInfo
**info
)
1178 const char *query_site
= NULL
;
1179 char *ptr_to_free
= NULL
;
1180 bool retry_query_with_null
= false;
1182 if ((site_name
== NULL
) || (site_name
[0] == '\0')) {
1183 ptr_to_free
= sitename_fetch(mem_ctx
, domain_name
);
1184 if (ptr_to_free
!= NULL
) {
1185 retry_query_with_null
= true;
1187 query_site
= ptr_to_free
;
1189 query_site
= site_name
;
1192 status
= dsgetdcname_internal(mem_ctx
,
1200 TALLOC_FREE(ptr_to_free
);
1202 if (!NT_STATUS_EQUAL(status
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
)) {
1206 /* Should we try again with site_name == NULL ? */
1207 if (retry_query_with_null
) {
1208 status
= dsgetdcname_internal(mem_ctx
,
1220 NTSTATUS
dsgetonedcname(TALLOC_CTX
*mem_ctx
,
1221 struct messaging_context
*msg_ctx
,
1222 const char *domain_name
,
1225 struct netr_DsRGetDCNameInfo
**info
)
1228 struct sockaddr_storage
*addrs
;
1229 unsigned int num_addrs
, i
;
1230 const char *hostname
= strip_hostname(dcname
);
1232 status
= resolve_name_list(mem_ctx
, hostname
, 0x20,
1233 &addrs
, &num_addrs
);
1234 if (!NT_STATUS_IS_OK(status
)) {
1238 for (i
= 0; i
< num_addrs
; i
++) {
1241 struct ip_service_name dclist
;
1243 dclist
.hostname
= hostname
;
1244 ok
= sockaddr_storage_to_samba_sockaddr(&dclist
.sa
, &addrs
[i
]);
1247 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1250 status
= process_dc_dns(mem_ctx
, domain_name
, flags
,
1252 if (NT_STATUS_IS_OK(status
)) {
1254 return NT_STATUS_OK
;
1257 if (lp_disable_netbios()) {
1261 status
= process_dc_netbios(mem_ctx
, msg_ctx
, domain_name
, flags
,
1263 if (NT_STATUS_IS_OK(status
)) {
1265 return NT_STATUS_OK
;
1270 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;