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 "libads/sitename_cache.h"
25 #include "../librpc/gen_ndr/ndr_netlogon.h"
26 #include "libads/cldap.h"
27 #include "libads/dns.h"
28 #include "libsmb/clidgram.h"
31 #define DSGETDCNAME_CACHE_TTL 60*15
33 struct ip_service_name
{
34 struct sockaddr_storage ss
;
38 static NTSTATUS
make_dc_info_from_cldap_reply(TALLOC_CTX
*mem_ctx
,
40 struct sockaddr_storage
*ss
,
41 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
,
42 struct netr_DsRGetDCNameInfo
**info
);
44 /****************************************************************
45 ****************************************************************/
47 void debug_dsdcinfo_flags(int lvl
, uint32_t flags
)
49 DEBUG(lvl
,("debug_dsdcinfo_flags: 0x%08x\n\t", flags
));
51 if (flags
& DS_FORCE_REDISCOVERY
)
52 DEBUGADD(lvl
,("DS_FORCE_REDISCOVERY "));
53 if (flags
& 0x000000002)
54 DEBUGADD(lvl
,("0x00000002 "));
55 if (flags
& 0x000000004)
56 DEBUGADD(lvl
,("0x00000004 "));
57 if (flags
& 0x000000008)
58 DEBUGADD(lvl
,("0x00000008 "));
59 if (flags
& DS_DIRECTORY_SERVICE_REQUIRED
)
60 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_REQUIRED "));
61 if (flags
& DS_DIRECTORY_SERVICE_PREFERRED
)
62 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_PREFERRED "));
63 if (flags
& DS_GC_SERVER_REQUIRED
)
64 DEBUGADD(lvl
,("DS_GC_SERVER_REQUIRED "));
65 if (flags
& DS_PDC_REQUIRED
)
66 DEBUGADD(lvl
,("DS_PDC_REQUIRED "));
67 if (flags
& DS_BACKGROUND_ONLY
)
68 DEBUGADD(lvl
,("DS_BACKGROUND_ONLY "));
69 if (flags
& DS_IP_REQUIRED
)
70 DEBUGADD(lvl
,("DS_IP_REQUIRED "));
71 if (flags
& DS_KDC_REQUIRED
)
72 DEBUGADD(lvl
,("DS_KDC_REQUIRED "));
73 if (flags
& DS_TIMESERV_REQUIRED
)
74 DEBUGADD(lvl
,("DS_TIMESERV_REQUIRED "));
75 if (flags
& DS_WRITABLE_REQUIRED
)
76 DEBUGADD(lvl
,("DS_WRITABLE_REQUIRED "));
77 if (flags
& DS_GOOD_TIMESERV_PREFERRED
)
78 DEBUGADD(lvl
,("DS_GOOD_TIMESERV_PREFERRED "));
79 if (flags
& DS_AVOID_SELF
)
80 DEBUGADD(lvl
,("DS_AVOID_SELF "));
81 if (flags
& DS_ONLY_LDAP_NEEDED
)
82 DEBUGADD(lvl
,("DS_ONLY_LDAP_NEEDED "));
83 if (flags
& DS_IS_FLAT_NAME
)
84 DEBUGADD(lvl
,("DS_IS_FLAT_NAME "));
85 if (flags
& DS_IS_DNS_NAME
)
86 DEBUGADD(lvl
,("DS_IS_DNS_NAME "));
87 if (flags
& 0x00040000)
88 DEBUGADD(lvl
,("0x00040000 "));
89 if (flags
& 0x00080000)
90 DEBUGADD(lvl
,("0x00080000 "));
91 if (flags
& 0x00100000)
92 DEBUGADD(lvl
,("0x00100000 "));
93 if (flags
& 0x00200000)
94 DEBUGADD(lvl
,("0x00200000 "));
95 if (flags
& 0x00400000)
96 DEBUGADD(lvl
,("0x00400000 "));
97 if (flags
& 0x00800000)
98 DEBUGADD(lvl
,("0x00800000 "));
99 if (flags
& 0x01000000)
100 DEBUGADD(lvl
,("0x01000000 "));
101 if (flags
& 0x02000000)
102 DEBUGADD(lvl
,("0x02000000 "));
103 if (flags
& 0x04000000)
104 DEBUGADD(lvl
,("0x04000000 "));
105 if (flags
& 0x08000000)
106 DEBUGADD(lvl
,("0x08000000 "));
107 if (flags
& 0x10000000)
108 DEBUGADD(lvl
,("0x10000000 "));
109 if (flags
& 0x20000000)
110 DEBUGADD(lvl
,("0x20000000 "));
111 if (flags
& DS_RETURN_DNS_NAME
)
112 DEBUGADD(lvl
,("DS_RETURN_DNS_NAME "));
113 if (flags
& DS_RETURN_FLAT_NAME
)
114 DEBUGADD(lvl
,("DS_RETURN_FLAT_NAME "));
116 DEBUGADD(lvl
,("\n"));
119 /****************************************************************
120 ****************************************************************/
122 static char *dsgetdcname_cache_key(TALLOC_CTX
*mem_ctx
, const char *domain
)
128 return talloc_asprintf_strupper_m(mem_ctx
, "DSGETDCNAME/DOMAIN/%s",
132 /****************************************************************
133 ****************************************************************/
135 static NTSTATUS
dsgetdcname_cache_delete(TALLOC_CTX
*mem_ctx
,
136 const char *domain_name
)
140 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
142 return NT_STATUS_NO_MEMORY
;
145 if (!gencache_del(key
)) {
146 return NT_STATUS_UNSUCCESSFUL
;
152 /****************************************************************
153 ****************************************************************/
155 static NTSTATUS
dsgetdcname_cache_store(TALLOC_CTX
*mem_ctx
,
156 const char *domain_name
,
157 const DATA_BLOB
*blob
)
163 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
165 return NT_STATUS_NO_MEMORY
;
168 expire_time
= time(NULL
) + DSGETDCNAME_CACHE_TTL
;
170 ret
= gencache_set_data_blob(key
, blob
, expire_time
);
172 return ret
? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
175 /****************************************************************
176 ****************************************************************/
178 static NTSTATUS
store_cldap_reply(TALLOC_CTX
*mem_ctx
,
180 struct sockaddr_storage
*ss
,
182 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
)
185 enum ndr_err_code ndr_err
;
187 char addr
[INET6_ADDRSTRLEN
];
189 print_sockaddr(addr
, sizeof(addr
), ss
);
192 r
->sockaddr_size
= 0x10; /* the w32 winsock addr size */
193 r
->sockaddr
.sockaddr_family
= 2; /* AF_INET */
194 r
->sockaddr
.pdc_ip
= talloc_strdup(mem_ctx
, addr
);
196 ndr_err
= ndr_push_struct_blob(&blob
, mem_ctx
, r
,
197 (ndr_push_flags_fn_t
)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX
);
198 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
199 return ndr_map_error2ntstatus(ndr_err
);
202 if (r
->domain_name
) {
203 status
= dsgetdcname_cache_store(mem_ctx
, r
->domain_name
, &blob
);
204 if (!NT_STATUS_IS_OK(status
)) {
207 if (r
->client_site
) {
208 sitename_store(r
->domain_name
, r
->client_site
);
212 status
= dsgetdcname_cache_store(mem_ctx
, r
->dns_domain
, &blob
);
213 if (!NT_STATUS_IS_OK(status
)) {
216 if (r
->client_site
) {
217 sitename_store(r
->dns_domain
, r
->client_site
);
221 status
= NT_STATUS_OK
;
224 data_blob_free(&blob
);
229 /****************************************************************
230 ****************************************************************/
232 static uint32_t get_cldap_reply_server_flags(struct netlogon_samlogon_response
*r
,
235 switch (nt_version
& 0x0000001f) {
245 return r
->data
.nt5
.server_type
;
250 return r
->data
.nt5_ex
.server_type
;
259 return r
->data
.nt5_ex
.server_type
;
269 return r
->data
.nt5_ex
.server_type
;
273 return r
->data
.nt5_ex
.server_type
;
279 /****************************************************************
280 ****************************************************************/
282 #define RETURN_ON_FALSE(x) if (!(x)) return false;
284 static bool check_cldap_reply_required_flags(uint32_t ret_flags
,
287 if (ret_flags
== 0) {
291 if (req_flags
& DS_PDC_REQUIRED
)
292 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_PDC
);
294 if (req_flags
& DS_GC_SERVER_REQUIRED
)
295 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_GC
);
297 if (req_flags
& DS_ONLY_LDAP_NEEDED
)
298 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_LDAP
);
300 if ((req_flags
& DS_DIRECTORY_SERVICE_REQUIRED
) ||
301 (req_flags
& DS_DIRECTORY_SERVICE_PREFERRED
))
302 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_DS
);
304 if (req_flags
& DS_KDC_REQUIRED
)
305 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_KDC
);
307 if (req_flags
& DS_TIMESERV_REQUIRED
)
308 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_TIMESERV
);
310 if (req_flags
& DS_WRITABLE_REQUIRED
)
311 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_WRITABLE
);
316 /****************************************************************
317 ****************************************************************/
319 static NTSTATUS
dsgetdcname_cache_fetch(TALLOC_CTX
*mem_ctx
,
320 const char *domain_name
,
321 const struct GUID
*domain_guid
,
323 const char *site_name
,
324 struct netr_DsRGetDCNameInfo
**info_p
)
328 enum ndr_err_code ndr_err
;
329 struct netr_DsRGetDCNameInfo
*info
;
330 struct NETLOGON_SAM_LOGON_RESPONSE_EX r
;
333 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
335 return NT_STATUS_NO_MEMORY
;
338 if (!gencache_get_data_blob(key
, &blob
, NULL
, NULL
)) {
339 return NT_STATUS_NOT_FOUND
;
342 info
= talloc_zero(mem_ctx
, struct netr_DsRGetDCNameInfo
);
344 return NT_STATUS_NO_MEMORY
;
347 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &r
,
348 (ndr_pull_flags_fn_t
)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX
);
350 data_blob_free(&blob
);
351 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
352 dsgetdcname_cache_delete(mem_ctx
, domain_name
);
353 return ndr_map_error2ntstatus(ndr_err
);
356 status
= make_dc_info_from_cldap_reply(mem_ctx
, flags
, NULL
,
358 if (!NT_STATUS_IS_OK(status
)) {
362 if (DEBUGLEVEL
>= 10) {
363 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo
, info
);
367 if (!check_cldap_reply_required_flags(info
->dc_flags
, flags
)) {
368 DEBUG(10,("invalid flags\n"));
369 return NT_STATUS_INVALID_PARAMETER
;
372 if ((flags
& DS_IP_REQUIRED
) &&
373 (info
->dc_address_type
!= DS_ADDRESS_TYPE_INET
)) {
374 return NT_STATUS_INVALID_PARAMETER_MIX
;
382 /****************************************************************
383 ****************************************************************/
385 static NTSTATUS
dsgetdcname_cached(TALLOC_CTX
*mem_ctx
,
386 struct messaging_context
*msg_ctx
,
387 const char *domain_name
,
388 const struct GUID
*domain_guid
,
390 const char *site_name
,
391 struct netr_DsRGetDCNameInfo
**info
)
395 status
= dsgetdcname_cache_fetch(mem_ctx
, domain_name
, domain_guid
,
396 flags
, site_name
, info
);
397 if (!NT_STATUS_IS_OK(status
)
398 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
399 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
401 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
404 if (flags
& DS_BACKGROUND_ONLY
) {
408 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
409 struct netr_DsRGetDCNameInfo
*dc_info
;
411 status
= dsgetdcname(mem_ctx
, msg_ctx
, domain_name
,
412 domain_guid
, site_name
,
413 flags
| DS_FORCE_REDISCOVERY
,
416 if (!NT_STATUS_IS_OK(status
)) {
426 /****************************************************************
427 ****************************************************************/
429 static bool check_allowed_required_flags(uint32_t flags
,
430 const char *site_name
)
432 uint32_t return_type
= flags
& (DS_RETURN_FLAT_NAME
|DS_RETURN_DNS_NAME
);
433 uint32_t offered_type
= flags
& (DS_IS_FLAT_NAME
|DS_IS_DNS_NAME
);
434 uint32_t query_type
= flags
& (DS_BACKGROUND_ONLY
|DS_FORCE_REDISCOVERY
);
436 /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
437 * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
439 debug_dsdcinfo_flags(10, flags
);
441 if ((flags
& DS_TRY_NEXTCLOSEST_SITE
) && site_name
) {
445 if (return_type
== (DS_RETURN_FLAT_NAME
|DS_RETURN_DNS_NAME
)) {
449 if (offered_type
== (DS_IS_DNS_NAME
|DS_IS_FLAT_NAME
)) {
453 if (query_type
== (DS_BACKGROUND_ONLY
|DS_FORCE_REDISCOVERY
)) {
458 if ((flags
& DS_RETURN_DNS_NAME
) && (!(flags
& DS_IP_REQUIRED
))) {
459 printf("gd: here5 \n");
466 /****************************************************************
467 ****************************************************************/
469 static NTSTATUS
discover_dc_netbios(TALLOC_CTX
*mem_ctx
,
470 const char *domain_name
,
472 struct ip_service_name
**returned_dclist
,
476 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
477 struct ip_service
*iplist
;
479 struct ip_service_name
*dclist
= NULL
;
482 *returned_dclist
= NULL
;
485 if (lp_disable_netbios()) {
486 return NT_STATUS_NOT_SUPPORTED
;
489 if (flags
& DS_PDC_REQUIRED
) {
490 name_type
= NBT_NAME_PDC
;
493 status
= internal_resolve_name(domain_name
, name_type
, NULL
,
495 "lmhosts wins bcast");
496 if (!NT_STATUS_IS_OK(status
)) {
497 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
501 dclist
= talloc_zero_array(mem_ctx
, struct ip_service_name
, count
);
504 return NT_STATUS_NO_MEMORY
;
507 for (i
=0; i
<count
; i
++) {
509 char addr
[INET6_ADDRSTRLEN
];
510 struct ip_service_name
*r
= &dclist
[i
];
512 print_sockaddr(addr
, sizeof(addr
),
515 r
->ss
= iplist
[i
].ss
;
516 r
->hostname
= talloc_strdup(mem_ctx
, addr
);
519 return NT_STATUS_NO_MEMORY
;
524 *returned_dclist
= dclist
;
525 *returned_count
= count
;
531 /****************************************************************
532 ****************************************************************/
534 static NTSTATUS
discover_dc_dns(TALLOC_CTX
*mem_ctx
,
535 const char *domain_name
,
536 const struct GUID
*domain_guid
,
538 const char *site_name
,
539 struct ip_service_name
**returned_dclist
,
544 struct dns_rr_srv
*dcs
= NULL
;
547 struct ip_service_name
*dclist
= NULL
;
550 if (flags
& DS_PDC_REQUIRED
) {
551 status
= ads_dns_query_pdc(mem_ctx
, domain_name
,
553 } else if (flags
& DS_GC_SERVER_REQUIRED
) {
554 status
= ads_dns_query_gcs(mem_ctx
, domain_name
, site_name
,
556 } else if (flags
& DS_KDC_REQUIRED
) {
557 status
= ads_dns_query_kdcs(mem_ctx
, domain_name
, site_name
,
559 } else if (flags
& DS_DIRECTORY_SERVICE_REQUIRED
) {
560 status
= ads_dns_query_dcs(mem_ctx
, domain_name
, site_name
,
562 } else if (domain_guid
) {
563 status
= ads_dns_query_dcs_guid(mem_ctx
, domain_name
,
564 domain_guid
, &dcs
, &numdcs
);
566 status
= ads_dns_query_dcs(mem_ctx
, domain_name
, site_name
,
570 if (!NT_STATUS_IS_OK(status
)) {
575 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
578 for (i
=0;i
<numdcs
;i
++) {
579 numaddrs
+= MAX(dcs
[i
].num_ips
,1);
582 dclist
= talloc_zero_array(mem_ctx
,
583 struct ip_service_name
,
586 return NT_STATUS_NO_MEMORY
;
589 /* now unroll the list of IP addresses */
595 while ((i
< numdcs
) && (count
< numaddrs
)) {
597 struct ip_service_name
*r
= &dclist
[count
];
599 r
->hostname
= dcs
[i
].hostname
;
601 /* If we don't have an IP list for a name, lookup it up */
604 interpret_string_addr_prefer_ipv4(&r
->ss
,
609 /* use the IP addresses from the SRV response */
611 if (j
>= dcs
[i
].num_ips
) {
617 r
->ss
= dcs
[i
].ss_s
[j
];
621 /* make sure it is a valid IP. I considered checking the
622 * negative connection cache, but this is the wrong place for
623 * it. Maybe only as a hack. After think about it, if all of
624 * the IP addresses returned from DNS are dead, what hope does a
625 * netbios name lookup have? The standard reason for falling
626 * back to netbios lookups is that our DNS server doesn't know
627 * anything about the DC's -- jerry */
629 if (!is_zero_addr(&r
->ss
)) {
635 *returned_dclist
= dclist
;
636 *return_count
= count
;
642 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
645 /****************************************************************
646 ****************************************************************/
648 static NTSTATUS
make_domain_controller_info(TALLOC_CTX
*mem_ctx
,
650 const char *dc_address
,
651 uint32_t dc_address_type
,
652 const struct GUID
*domain_guid
,
653 const char *domain_name
,
654 const char *forest_name
,
656 const char *dc_site_name
,
657 const char *client_site_name
,
658 struct netr_DsRGetDCNameInfo
**info_out
)
660 struct netr_DsRGetDCNameInfo
*info
;
662 info
= talloc_zero(mem_ctx
, struct netr_DsRGetDCNameInfo
);
663 NT_STATUS_HAVE_NO_MEMORY(info
);
666 info
->dc_unc
= talloc_strdup(mem_ctx
, dc_unc
);
667 NT_STATUS_HAVE_NO_MEMORY(info
->dc_unc
);
671 if (!(dc_address
[0] == '\\' && dc_address
[1] == '\\')) {
672 info
->dc_address
= talloc_asprintf(mem_ctx
, "\\\\%s",
675 info
->dc_address
= talloc_strdup(mem_ctx
, dc_address
);
677 NT_STATUS_HAVE_NO_MEMORY(info
->dc_address
);
680 info
->dc_address_type
= dc_address_type
;
683 info
->domain_guid
= *domain_guid
;
687 info
->domain_name
= talloc_strdup(mem_ctx
, domain_name
);
688 NT_STATUS_HAVE_NO_MEMORY(info
->domain_name
);
691 if (forest_name
&& *forest_name
) {
692 info
->forest_name
= talloc_strdup(mem_ctx
, forest_name
);
693 NT_STATUS_HAVE_NO_MEMORY(info
->forest_name
);
694 flags
|= DS_DNS_FOREST_ROOT
;
697 info
->dc_flags
= flags
;
700 info
->dc_site_name
= talloc_strdup(mem_ctx
, dc_site_name
);
701 NT_STATUS_HAVE_NO_MEMORY(info
->dc_site_name
);
704 if (client_site_name
) {
705 info
->client_site_name
= talloc_strdup(mem_ctx
,
707 NT_STATUS_HAVE_NO_MEMORY(info
->client_site_name
);
715 /****************************************************************
716 ****************************************************************/
718 static void map_dc_and_domain_names(uint32_t flags
,
720 const char *domain_name
,
721 const char *dns_dc_name
,
722 const char *dns_domain_name
,
724 const char **hostname_p
,
725 const char **domain_p
)
727 switch (flags
& 0xf0000000) {
728 case DS_RETURN_FLAT_NAME
:
729 if (dc_name
&& domain_name
&&
730 *dc_name
&& *domain_name
) {
731 *hostname_p
= dc_name
;
732 *domain_p
= domain_name
;
735 case DS_RETURN_DNS_NAME
:
737 if (dns_dc_name
&& dns_domain_name
&&
738 *dns_dc_name
&& *dns_domain_name
) {
739 *hostname_p
= dns_dc_name
;
740 *domain_p
= dns_domain_name
;
741 *dc_flags
|= DS_DNS_DOMAIN
| DS_DNS_CONTROLLER
;
744 if (dc_name
&& domain_name
&&
745 *dc_name
&& *domain_name
) {
746 *hostname_p
= dc_name
;
747 *domain_p
= domain_name
;
753 /****************************************************************
754 ****************************************************************/
756 static NTSTATUS
make_dc_info_from_cldap_reply(TALLOC_CTX
*mem_ctx
,
758 struct sockaddr_storage
*ss
,
759 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
,
760 struct netr_DsRGetDCNameInfo
**info
)
762 const char *dc_hostname
= NULL
;
763 const char *dc_domain_name
= NULL
;
764 const char *dc_address
= NULL
;
765 const char *dc_forest
= NULL
;
766 uint32_t dc_address_type
= 0;
767 uint32_t dc_flags
= 0;
768 struct GUID
*dc_domain_guid
= NULL
;
769 const char *dc_server_site
= NULL
;
770 const char *dc_client_site
= NULL
;
772 char addr
[INET6_ADDRSTRLEN
];
775 print_sockaddr(addr
, sizeof(addr
), ss
);
777 dc_address_type
= DS_ADDRESS_TYPE_INET
;
780 if (!ss
&& r
->sockaddr
.pdc_ip
) {
781 dc_address
= r
->sockaddr
.pdc_ip
;
782 dc_address_type
= DS_ADDRESS_TYPE_INET
;
784 dc_address
= r
->pdc_name
;
785 dc_address_type
= DS_ADDRESS_TYPE_NETBIOS
;
788 map_dc_and_domain_names(flags
,
797 dc_flags
|= r
->server_type
;
798 dc_forest
= r
->forest
;
799 dc_domain_guid
= &r
->domain_uuid
;
800 dc_server_site
= r
->server_site
;
801 dc_client_site
= r
->client_site
;
803 return make_domain_controller_info(mem_ctx
,
816 /****************************************************************
817 ****************************************************************/
819 static uint32_t map_ds_flags_to_nt_version(uint32_t flags
)
821 uint32_t nt_version
= 0;
823 if (flags
& DS_PDC_REQUIRED
) {
824 nt_version
|= NETLOGON_NT_VERSION_PDC
;
827 if (flags
& DS_GC_SERVER_REQUIRED
) {
828 nt_version
|= NETLOGON_NT_VERSION_GC
;
831 if (flags
& DS_TRY_NEXTCLOSEST_SITE
) {
832 nt_version
|= NETLOGON_NT_VERSION_WITH_CLOSEST_SITE
;
835 if (flags
& DS_IP_REQUIRED
) {
836 nt_version
|= NETLOGON_NT_VERSION_IP
;
842 /****************************************************************
843 ****************************************************************/
845 static NTSTATUS
process_dc_dns(TALLOC_CTX
*mem_ctx
,
846 const char *domain_name
,
848 struct ip_service_name
*dclist
,
850 struct netr_DsRGetDCNameInfo
**info
)
853 bool valid_dc
= false;
854 struct netlogon_samlogon_response
*r
= NULL
;
855 uint32_t nt_version
= NETLOGON_NT_VERSION_5
|
856 NETLOGON_NT_VERSION_5EX
;
857 uint32_t ret_flags
= 0;
860 nt_version
|= map_ds_flags_to_nt_version(flags
);
862 for (i
=0; i
<num_dcs
; i
++) {
865 DEBUG(10,("LDAP ping to %s\n", dclist
[i
].hostname
));
867 if (ads_cldap_netlogon(mem_ctx
, &dclist
[i
].ss
,
872 nt_version
= r
->ntver
;
873 ret_flags
= get_cldap_reply_server_flags(r
, nt_version
);
875 if (check_cldap_reply_required_flags(ret_flags
, flags
)) {
885 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
888 status
= make_dc_info_from_cldap_reply(mem_ctx
, flags
, &dclist
[i
].ss
,
889 &r
->data
.nt5_ex
, info
);
890 if (NT_STATUS_IS_OK(status
)) {
891 return store_cldap_reply(mem_ctx
, flags
, &dclist
[i
].ss
,
892 nt_version
, &r
->data
.nt5_ex
);
898 /****************************************************************
899 ****************************************************************/
901 /****************************************************************
902 ****************************************************************/
904 static NTSTATUS
process_dc_netbios(TALLOC_CTX
*mem_ctx
,
905 struct messaging_context
*msg_ctx
,
906 const char *domain_name
,
908 struct ip_service_name
*dclist
,
910 struct netr_DsRGetDCNameInfo
**info
)
912 struct sockaddr_storage ss
;
913 struct ip_service ip_list
;
914 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
917 const char *dc_name
= NULL
;
919 struct netlogon_samlogon_response
*r
= NULL
;
920 bool store_cache
= false;
921 uint32_t nt_version
= NETLOGON_NT_VERSION_1
|
922 NETLOGON_NT_VERSION_5
|
923 NETLOGON_NT_VERSION_5EX_WITH_IP
;
925 if (msg_ctx
== NULL
) {
926 return NT_STATUS_INVALID_PARAMETER
;
929 if (flags
& DS_PDC_REQUIRED
) {
930 name_type
= NBT_NAME_PDC
;
933 nt_version
|= map_ds_flags_to_nt_version(flags
);
935 DEBUG(10,("process_dc_netbios\n"));
937 for (i
=0; i
<num_dcs
; i
++) {
940 generate_random_buffer((uint8_t *)&val
, 2);
942 ip_list
.ss
= dclist
[i
].ss
;
945 if (!interpret_string_addr_prefer_ipv4(&ss
, dclist
[i
].hostname
, AI_NUMERICHOST
)) {
946 return NT_STATUS_UNSUCCESSFUL
;
949 status
= nbt_getdc(msg_ctx
, &dclist
[i
].ss
, domain_name
,
951 mem_ctx
, &nt_version
, &dc_name
, &r
);
952 if (NT_STATUS_IS_OK(status
)) {
954 namecache_store(dc_name
, NBT_NAME_SERVER
, 1, &ip_list
);
958 if (name_status_find(domain_name
,
964 struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1
;
966 r
= talloc_zero(mem_ctx
, struct netlogon_samlogon_response
);
967 NT_STATUS_HAVE_NO_MEMORY(r
);
971 nt_version
= NETLOGON_NT_VERSION_1
;
973 logon1
.nt_version
= nt_version
;
974 logon1
.pdc_name
= tmp_dc_name
;
975 logon1
.domain_name
= talloc_strdup_upper(mem_ctx
, domain_name
);
976 NT_STATUS_HAVE_NO_MEMORY(logon1
.domain_name
);
978 r
->data
.nt4
= logon1
;
979 r
->ntver
= nt_version
;
981 map_netlogon_samlogon_response(r
);
983 namecache_store(tmp_dc_name
, NBT_NAME_SERVER
, 1, &ip_list
);
989 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
993 status
= make_dc_info_from_cldap_reply(mem_ctx
, flags
, &dclist
[i
].ss
,
994 &r
->data
.nt5_ex
, info
);
995 if (NT_STATUS_IS_OK(status
) && store_cache
) {
996 return store_cldap_reply(mem_ctx
, flags
, &dclist
[i
].ss
,
997 nt_version
, &r
->data
.nt5_ex
);
1003 /****************************************************************
1004 ****************************************************************/
1006 static NTSTATUS
dsgetdcname_rediscover(TALLOC_CTX
*mem_ctx
,
1007 struct messaging_context
*msg_ctx
,
1008 const char *domain_name
,
1009 const struct GUID
*domain_guid
,
1011 const char *site_name
,
1012 struct netr_DsRGetDCNameInfo
**info
)
1014 NTSTATUS status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1015 struct ip_service_name
*dclist
= NULL
;
1018 DEBUG(10,("dsgetdcname_rediscover\n"));
1020 if (flags
& DS_IS_FLAT_NAME
) {
1022 status
= discover_dc_netbios(mem_ctx
, domain_name
, flags
,
1024 NT_STATUS_NOT_OK_RETURN(status
);
1026 return process_dc_netbios(mem_ctx
, msg_ctx
, domain_name
, flags
,
1027 dclist
, num_dcs
, info
);
1030 if (flags
& DS_IS_DNS_NAME
) {
1032 status
= discover_dc_dns(mem_ctx
, domain_name
, domain_guid
,
1033 flags
, site_name
, &dclist
, &num_dcs
);
1034 NT_STATUS_NOT_OK_RETURN(status
);
1036 return process_dc_dns(mem_ctx
, domain_name
, flags
,
1037 dclist
, num_dcs
, info
);
1040 status
= discover_dc_dns(mem_ctx
, domain_name
, domain_guid
, flags
,
1041 site_name
, &dclist
, &num_dcs
);
1043 if (NT_STATUS_IS_OK(status
) && num_dcs
!= 0) {
1045 status
= process_dc_dns(mem_ctx
, domain_name
, flags
, dclist
,
1047 if (NT_STATUS_IS_OK(status
)) {
1052 status
= discover_dc_netbios(mem_ctx
, domain_name
, flags
, &dclist
,
1054 NT_STATUS_NOT_OK_RETURN(status
);
1056 return process_dc_netbios(mem_ctx
, msg_ctx
, domain_name
, flags
, dclist
,
1060 static bool is_closest_site(struct netr_DsRGetDCNameInfo
*info
)
1062 if (info
->dc_flags
& DS_SERVER_CLOSEST
) {
1066 if (!info
->client_site_name
) {
1070 if (!info
->dc_site_name
) {
1074 if (strcmp(info
->client_site_name
, info
->dc_site_name
) == 0) {
1081 /********************************************************************
1084 This will be the only public function here.
1085 ********************************************************************/
1087 NTSTATUS
dsgetdcname(TALLOC_CTX
*mem_ctx
,
1088 struct messaging_context
*msg_ctx
,
1089 const char *domain_name
,
1090 const struct GUID
*domain_guid
,
1091 const char *site_name
,
1093 struct netr_DsRGetDCNameInfo
**info
)
1095 NTSTATUS status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1096 struct netr_DsRGetDCNameInfo
*myinfo
= NULL
;
1097 char *query_site
= NULL
;
1099 struct netr_DsRGetDCNameInfo
*first_info
= NULL
;
1101 DEBUG(10,("dsgetdcname: domain_name: %s, "
1102 "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
1104 domain_guid
? GUID_string(mem_ctx
, domain_guid
) : "(null)",
1109 if (!check_allowed_required_flags(flags
, site_name
)) {
1110 DEBUG(0,("invalid flags specified\n"));
1111 return NT_STATUS_INVALID_PARAMETER
;
1114 if ((site_name
== NULL
) || (site_name
[0] == '\0')) {
1115 query_site
= sitename_fetch(domain_name
);
1117 query_site
= SMB_STRDUP(site_name
);
1120 if (flags
& DS_FORCE_REDISCOVERY
) {
1124 status
= dsgetdcname_cached(mem_ctx
, msg_ctx
, domain_name
, domain_guid
,
1125 flags
, query_site
, &myinfo
);
1126 if (NT_STATUS_IS_OK(status
)) {
1130 if (flags
& DS_BACKGROUND_ONLY
) {
1135 status
= dsgetdcname_rediscover(mem_ctx
, msg_ctx
, domain_name
,
1136 domain_guid
, flags
, query_site
,
1140 SAFE_FREE(query_site
);
1142 if (!NT_STATUS_IS_OK(status
)) {
1145 return NT_STATUS_OK
;
1151 TALLOC_FREE(first_info
);
1152 } else if (!is_closest_site(myinfo
)) {
1154 first_info
= myinfo
;
1155 /* TODO: may use the next_closest_site here */
1156 query_site
= SMB_STRDUP(myinfo
->client_site_name
);
1161 return NT_STATUS_OK
;