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"
26 #define DSGETDCNAME_FMT "DSGETDCNAME/DOMAIN/%s"
28 #define DSGETDCNAME_CACHE_TTL 60*15
30 struct ip_service_name
{
31 struct sockaddr_storage ss
;
36 static NTSTATUS
make_dc_info_from_cldap_reply(TALLOC_CTX
*mem_ctx
,
38 struct sockaddr_storage
*ss
,
39 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
,
40 struct netr_DsRGetDCNameInfo
**info
);
42 /****************************************************************
43 ****************************************************************/
45 void debug_dsdcinfo_flags(int lvl
, uint32_t flags
)
47 DEBUG(lvl
,("debug_dsdcinfo_flags: 0x%08x\n\t", flags
));
49 if (flags
& DS_FORCE_REDISCOVERY
)
50 DEBUGADD(lvl
,("DS_FORCE_REDISCOVERY "));
51 if (flags
& 0x000000002)
52 DEBUGADD(lvl
,("0x00000002 "));
53 if (flags
& 0x000000004)
54 DEBUGADD(lvl
,("0x00000004 "));
55 if (flags
& 0x000000008)
56 DEBUGADD(lvl
,("0x00000008 "));
57 if (flags
& DS_DIRECTORY_SERVICE_REQUIRED
)
58 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_REQUIRED "));
59 if (flags
& DS_DIRECTORY_SERVICE_PREFERRED
)
60 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_PREFERRED "));
61 if (flags
& DS_GC_SERVER_REQUIRED
)
62 DEBUGADD(lvl
,("DS_GC_SERVER_REQUIRED "));
63 if (flags
& DS_PDC_REQUIRED
)
64 DEBUGADD(lvl
,("DS_PDC_REQUIRED "));
65 if (flags
& DS_BACKGROUND_ONLY
)
66 DEBUGADD(lvl
,("DS_BACKGROUND_ONLY "));
67 if (flags
& DS_IP_REQUIRED
)
68 DEBUGADD(lvl
,("DS_IP_REQUIRED "));
69 if (flags
& DS_KDC_REQUIRED
)
70 DEBUGADD(lvl
,("DS_KDC_REQUIRED "));
71 if (flags
& DS_TIMESERV_REQUIRED
)
72 DEBUGADD(lvl
,("DS_TIMESERV_REQUIRED "));
73 if (flags
& DS_WRITABLE_REQUIRED
)
74 DEBUGADD(lvl
,("DS_WRITABLE_REQUIRED "));
75 if (flags
& DS_GOOD_TIMESERV_PREFERRED
)
76 DEBUGADD(lvl
,("DS_GOOD_TIMESERV_PREFERRED "));
77 if (flags
& DS_AVOID_SELF
)
78 DEBUGADD(lvl
,("DS_AVOID_SELF "));
79 if (flags
& DS_ONLY_LDAP_NEEDED
)
80 DEBUGADD(lvl
,("DS_ONLY_LDAP_NEEDED "));
81 if (flags
& DS_IS_FLAT_NAME
)
82 DEBUGADD(lvl
,("DS_IS_FLAT_NAME "));
83 if (flags
& DS_IS_DNS_NAME
)
84 DEBUGADD(lvl
,("DS_IS_DNS_NAME "));
85 if (flags
& 0x00040000)
86 DEBUGADD(lvl
,("0x00040000 "));
87 if (flags
& 0x00080000)
88 DEBUGADD(lvl
,("0x00080000 "));
89 if (flags
& 0x00100000)
90 DEBUGADD(lvl
,("0x00100000 "));
91 if (flags
& 0x00200000)
92 DEBUGADD(lvl
,("0x00200000 "));
93 if (flags
& 0x00400000)
94 DEBUGADD(lvl
,("0x00400000 "));
95 if (flags
& 0x00800000)
96 DEBUGADD(lvl
,("0x00800000 "));
97 if (flags
& 0x01000000)
98 DEBUGADD(lvl
,("0x01000000 "));
99 if (flags
& 0x02000000)
100 DEBUGADD(lvl
,("0x02000000 "));
101 if (flags
& 0x04000000)
102 DEBUGADD(lvl
,("0x04000000 "));
103 if (flags
& 0x08000000)
104 DEBUGADD(lvl
,("0x08000000 "));
105 if (flags
& 0x10000000)
106 DEBUGADD(lvl
,("0x10000000 "));
107 if (flags
& 0x20000000)
108 DEBUGADD(lvl
,("0x20000000 "));
109 if (flags
& DS_RETURN_DNS_NAME
)
110 DEBUGADD(lvl
,("DS_RETURN_DNS_NAME "));
111 if (flags
& DS_RETURN_FLAT_NAME
)
112 DEBUGADD(lvl
,("DS_RETURN_FLAT_NAME "));
114 DEBUGADD(lvl
,("\n"));
117 /****************************************************************
118 ****************************************************************/
120 static char *dsgetdcname_cache_key(TALLOC_CTX
*mem_ctx
, const char *domain
)
126 return talloc_asprintf_strupper_m(mem_ctx
, DSGETDCNAME_FMT
, domain
);
129 /****************************************************************
130 ****************************************************************/
132 static NTSTATUS
dsgetdcname_cache_delete(TALLOC_CTX
*mem_ctx
,
133 const char *domain_name
)
137 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
139 return NT_STATUS_NO_MEMORY
;
142 if (!gencache_del(key
)) {
143 return NT_STATUS_UNSUCCESSFUL
;
149 /****************************************************************
150 ****************************************************************/
152 static NTSTATUS
dsgetdcname_cache_store(TALLOC_CTX
*mem_ctx
,
153 const char *domain_name
,
154 const DATA_BLOB
*blob
)
160 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
162 return NT_STATUS_NO_MEMORY
;
165 expire_time
= time(NULL
) + DSGETDCNAME_CACHE_TTL
;
167 ret
= gencache_set_data_blob(key
, blob
, expire_time
);
169 return ret
? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
172 /****************************************************************
173 ****************************************************************/
175 static NTSTATUS
store_cldap_reply(TALLOC_CTX
*mem_ctx
,
177 struct sockaddr_storage
*ss
,
179 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
)
182 enum ndr_err_code ndr_err
;
184 char addr
[INET6_ADDRSTRLEN
];
186 print_sockaddr(addr
, sizeof(addr
), ss
);
189 r
->sockaddr_size
= 0x10; /* the w32 winsock addr size */
190 r
->sockaddr
.sockaddr_family
= 2; /* AF_INET */
191 r
->sockaddr
.pdc_ip
= talloc_strdup(mem_ctx
, addr
);
193 ndr_err
= ndr_push_struct_blob(&blob
, mem_ctx
, NULL
, r
,
194 (ndr_push_flags_fn_t
)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX
);
195 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
196 return ndr_map_error2ntstatus(ndr_err
);
199 if (r
->domain_name
) {
200 status
= dsgetdcname_cache_store(mem_ctx
, r
->domain_name
, &blob
);
201 if (!NT_STATUS_IS_OK(status
)) {
204 if (r
->client_site
) {
205 sitename_store(r
->domain_name
, r
->client_site
);
209 status
= dsgetdcname_cache_store(mem_ctx
, r
->dns_domain
, &blob
);
210 if (!NT_STATUS_IS_OK(status
)) {
213 if (r
->client_site
) {
214 sitename_store(r
->dns_domain
, r
->client_site
);
218 status
= NT_STATUS_OK
;
221 data_blob_free(&blob
);
226 /****************************************************************
227 ****************************************************************/
229 static uint32_t get_cldap_reply_server_flags(struct netlogon_samlogon_response
*r
,
232 switch (nt_version
& 0x0000001f) {
242 return r
->data
.nt5
.server_type
;
247 return r
->data
.nt5_ex
.server_type
;
256 return r
->data
.nt5_ex
.server_type
;
266 return r
->data
.nt5_ex
.server_type
;
270 return r
->data
.nt5_ex
.server_type
;
276 /****************************************************************
277 ****************************************************************/
279 #define RETURN_ON_FALSE(x) if (!(x)) return false;
281 static bool check_cldap_reply_required_flags(uint32_t ret_flags
,
284 if (ret_flags
== 0) {
288 if (req_flags
& DS_PDC_REQUIRED
)
289 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_PDC
);
291 if (req_flags
& DS_GC_SERVER_REQUIRED
)
292 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_GC
);
294 if (req_flags
& DS_ONLY_LDAP_NEEDED
)
295 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_LDAP
);
297 if ((req_flags
& DS_DIRECTORY_SERVICE_REQUIRED
) ||
298 (req_flags
& DS_DIRECTORY_SERVICE_PREFERRED
))
299 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_DS
);
301 if (req_flags
& DS_KDC_REQUIRED
)
302 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_KDC
);
304 if (req_flags
& DS_TIMESERV_REQUIRED
)
305 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_TIMESERV
);
307 if (req_flags
& DS_WRITABLE_REQUIRED
)
308 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_WRITABLE
);
313 /****************************************************************
314 ****************************************************************/
316 static NTSTATUS
dsgetdcname_cache_fetch(TALLOC_CTX
*mem_ctx
,
317 const char *domain_name
,
318 const struct GUID
*domain_guid
,
320 const char *site_name
,
321 struct netr_DsRGetDCNameInfo
**info_p
)
325 enum ndr_err_code ndr_err
;
326 struct netr_DsRGetDCNameInfo
*info
;
327 struct NETLOGON_SAM_LOGON_RESPONSE_EX r
;
330 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
332 return NT_STATUS_NO_MEMORY
;
335 if (!gencache_get_data_blob(key
, &blob
, NULL
, NULL
)) {
336 return NT_STATUS_NOT_FOUND
;
339 info
= TALLOC_ZERO_P(mem_ctx
, struct netr_DsRGetDCNameInfo
);
341 return NT_STATUS_NO_MEMORY
;
344 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, NULL
, &r
,
345 (ndr_pull_flags_fn_t
)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX
);
347 data_blob_free(&blob
);
348 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
349 dsgetdcname_cache_delete(mem_ctx
, domain_name
);
350 return ndr_map_error2ntstatus(ndr_err
);
353 status
= make_dc_info_from_cldap_reply(mem_ctx
, flags
, NULL
,
355 if (!NT_STATUS_IS_OK(status
)) {
359 if (DEBUGLEVEL
>= 10) {
360 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo
, info
);
364 if (!check_cldap_reply_required_flags(info
->dc_flags
, flags
)) {
365 DEBUG(10,("invalid flags\n"));
366 return NT_STATUS_INVALID_PARAMETER
;
369 if ((flags
& DS_IP_REQUIRED
) &&
370 (info
->dc_address_type
!= DS_ADDRESS_TYPE_INET
)) {
371 return NT_STATUS_INVALID_PARAMETER_MIX
;
379 /****************************************************************
380 ****************************************************************/
382 static NTSTATUS
dsgetdcname_cached(TALLOC_CTX
*mem_ctx
,
383 struct messaging_context
*msg_ctx
,
384 const char *domain_name
,
385 const struct GUID
*domain_guid
,
387 const char *site_name
,
388 struct netr_DsRGetDCNameInfo
**info
)
392 status
= dsgetdcname_cache_fetch(mem_ctx
, domain_name
, domain_guid
,
393 flags
, site_name
, info
);
394 if (!NT_STATUS_IS_OK(status
)
395 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
396 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
398 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
401 if (flags
& DS_BACKGROUND_ONLY
) {
405 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
406 struct netr_DsRGetDCNameInfo
*dc_info
;
408 status
= dsgetdcname(mem_ctx
, msg_ctx
, domain_name
,
409 domain_guid
, site_name
,
410 flags
| DS_FORCE_REDISCOVERY
,
413 if (!NT_STATUS_IS_OK(status
)) {
423 /****************************************************************
424 ****************************************************************/
426 static bool check_allowed_required_flags(uint32_t flags
,
427 const char *site_name
)
429 uint32_t return_type
= flags
& (DS_RETURN_FLAT_NAME
|DS_RETURN_DNS_NAME
);
430 uint32_t offered_type
= flags
& (DS_IS_FLAT_NAME
|DS_IS_DNS_NAME
);
431 uint32_t query_type
= flags
& (DS_BACKGROUND_ONLY
|DS_FORCE_REDISCOVERY
);
433 /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
434 * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
436 debug_dsdcinfo_flags(10, flags
);
438 if ((flags
& DS_TRY_NEXTCLOSEST_SITE
) && site_name
) {
442 if (return_type
== (DS_RETURN_FLAT_NAME
|DS_RETURN_DNS_NAME
)) {
446 if (offered_type
== (DS_IS_DNS_NAME
|DS_IS_FLAT_NAME
)) {
450 if (query_type
== (DS_BACKGROUND_ONLY
|DS_FORCE_REDISCOVERY
)) {
455 if ((flags
& DS_RETURN_DNS_NAME
) && (!(flags
& DS_IP_REQUIRED
))) {
456 printf("gd: here5 \n");
463 /****************************************************************
464 ****************************************************************/
466 static NTSTATUS
discover_dc_netbios(TALLOC_CTX
*mem_ctx
,
467 const char *domain_name
,
469 struct ip_service_name
**returned_dclist
,
473 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
474 struct ip_service
*iplist
;
476 struct ip_service_name
*dclist
= NULL
;
479 *returned_dclist
= NULL
;
482 if (lp_disable_netbios()) {
483 return NT_STATUS_NOT_SUPPORTED
;
486 if (flags
& DS_PDC_REQUIRED
) {
487 name_type
= NBT_NAME_PDC
;
490 status
= internal_resolve_name(domain_name
, name_type
, NULL
,
492 "lmhosts wins bcast");
493 if (!NT_STATUS_IS_OK(status
)) {
494 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
498 dclist
= TALLOC_ZERO_ARRAY(mem_ctx
, struct ip_service_name
, count
);
501 return NT_STATUS_NO_MEMORY
;
504 for (i
=0; i
<count
; i
++) {
506 char addr
[INET6_ADDRSTRLEN
];
507 struct ip_service_name
*r
= &dclist
[i
];
509 print_sockaddr(addr
, sizeof(addr
),
512 r
->ss
= iplist
[i
].ss
;
513 r
->port
= iplist
[i
].port
;
514 r
->hostname
= talloc_strdup(mem_ctx
, addr
);
517 return NT_STATUS_NO_MEMORY
;
522 *returned_dclist
= dclist
;
523 *returned_count
= count
;
529 /****************************************************************
530 ****************************************************************/
532 static NTSTATUS
discover_dc_dns(TALLOC_CTX
*mem_ctx
,
533 const char *domain_name
,
534 const struct GUID
*domain_guid
,
536 const char *site_name
,
537 struct ip_service_name
**returned_dclist
,
542 struct dns_rr_srv
*dcs
= NULL
;
545 struct ip_service_name
*dclist
= NULL
;
548 if (flags
& DS_PDC_REQUIRED
) {
549 status
= ads_dns_query_pdc(mem_ctx
, domain_name
,
551 } else if (flags
& DS_GC_SERVER_REQUIRED
) {
552 status
= ads_dns_query_gcs(mem_ctx
, domain_name
, site_name
,
554 } else if (flags
& DS_KDC_REQUIRED
) {
555 status
= ads_dns_query_kdcs(mem_ctx
, domain_name
, site_name
,
557 } else if (flags
& DS_DIRECTORY_SERVICE_REQUIRED
) {
558 status
= ads_dns_query_dcs(mem_ctx
, domain_name
, site_name
,
560 } else if (domain_guid
) {
561 status
= ads_dns_query_dcs_guid(mem_ctx
, domain_name
,
562 domain_guid
, &dcs
, &numdcs
);
564 status
= ads_dns_query_dcs(mem_ctx
, domain_name
, site_name
,
568 if (!NT_STATUS_IS_OK(status
)) {
573 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
576 for (i
=0;i
<numdcs
;i
++) {
577 numaddrs
+= MAX(dcs
[i
].num_ips
,1);
580 dclist
= TALLOC_ZERO_ARRAY(mem_ctx
,
581 struct ip_service_name
,
584 return NT_STATUS_NO_MEMORY
;
587 /* now unroll the list of IP addresses */
593 while ((i
< numdcs
) && (count
< numaddrs
)) {
595 struct ip_service_name
*r
= &dclist
[count
];
597 r
->port
= dcs
[i
].port
;
598 r
->hostname
= dcs
[i
].hostname
;
600 /* If we don't have an IP list for a name, lookup it up */
603 interpret_string_addr_prefer_ipv4(&r
->ss
,
608 /* use the IP addresses from the SRV sresponse */
610 if (j
>= dcs
[i
].num_ips
) {
616 r
->ss
= dcs
[i
].ss_s
[j
];
620 /* make sure it is a valid IP. I considered checking the
621 * negative connection cache, but this is the wrong place for
622 * it. Maybe only as a hac. After think about it, if all of
623 * the IP addresses retuend from DNS are dead, what hope does a
624 * netbios name lookup have? The standard reason for falling
625 * back to netbios lookups is that our DNS server doesn't know
626 * anything about the DC's -- jerry */
628 if (!is_zero_addr((struct sockaddr
*)(void *)&r
->ss
)) {
634 *returned_dclist
= dclist
;
635 *return_count
= count
;
641 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
644 /****************************************************************
645 ****************************************************************/
647 static NTSTATUS
make_domain_controller_info(TALLOC_CTX
*mem_ctx
,
649 const char *dc_address
,
650 uint32_t dc_address_type
,
651 const struct GUID
*domain_guid
,
652 const char *domain_name
,
653 const char *forest_name
,
655 const char *dc_site_name
,
656 const char *client_site_name
,
657 struct netr_DsRGetDCNameInfo
**info_out
)
659 struct netr_DsRGetDCNameInfo
*info
;
661 info
= TALLOC_ZERO_P(mem_ctx
, struct netr_DsRGetDCNameInfo
);
662 NT_STATUS_HAVE_NO_MEMORY(info
);
665 info
->dc_unc
= talloc_strdup(mem_ctx
, dc_unc
);
666 NT_STATUS_HAVE_NO_MEMORY(info
->dc_unc
);
670 if (!(dc_address
[0] == '\\' && dc_address
[1] == '\\')) {
671 info
->dc_address
= talloc_asprintf(mem_ctx
, "\\\\%s",
674 info
->dc_address
= talloc_strdup(mem_ctx
, dc_address
);
676 NT_STATUS_HAVE_NO_MEMORY(info
->dc_address
);
679 info
->dc_address_type
= dc_address_type
;
682 info
->domain_guid
= *domain_guid
;
686 info
->domain_name
= talloc_strdup(mem_ctx
, domain_name
);
687 NT_STATUS_HAVE_NO_MEMORY(info
->domain_name
);
690 if (forest_name
&& *forest_name
) {
691 info
->forest_name
= talloc_strdup(mem_ctx
, forest_name
);
692 NT_STATUS_HAVE_NO_MEMORY(info
->forest_name
);
693 flags
|= DS_DNS_FOREST_ROOT
;
696 info
->dc_flags
= flags
;
699 info
->dc_site_name
= talloc_strdup(mem_ctx
, dc_site_name
);
700 NT_STATUS_HAVE_NO_MEMORY(info
->dc_site_name
);
703 if (client_site_name
) {
704 info
->client_site_name
= talloc_strdup(mem_ctx
,
706 NT_STATUS_HAVE_NO_MEMORY(info
->client_site_name
);
714 /****************************************************************
715 ****************************************************************/
717 static void map_dc_and_domain_names(uint32_t flags
,
719 const char *domain_name
,
720 const char *dns_dc_name
,
721 const char *dns_domain_name
,
723 const char **hostname_p
,
724 const char **domain_p
)
726 switch (flags
& 0xf0000000) {
727 case DS_RETURN_FLAT_NAME
:
728 if (dc_name
&& domain_name
&&
729 *dc_name
&& *domain_name
) {
730 *hostname_p
= dc_name
;
731 *domain_p
= domain_name
;
734 case DS_RETURN_DNS_NAME
:
736 if (dns_dc_name
&& dns_domain_name
&&
737 *dns_dc_name
&& *dns_domain_name
) {
738 *hostname_p
= dns_dc_name
;
739 *domain_p
= dns_domain_name
;
740 *dc_flags
|= DS_DNS_DOMAIN
| DS_DNS_CONTROLLER
;
743 if (dc_name
&& domain_name
&&
744 *dc_name
&& *domain_name
) {
745 *hostname_p
= dc_name
;
746 *domain_p
= domain_name
;
752 /****************************************************************
753 ****************************************************************/
755 static NTSTATUS
make_dc_info_from_cldap_reply(TALLOC_CTX
*mem_ctx
,
757 struct sockaddr_storage
*ss
,
758 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*r
,
759 struct netr_DsRGetDCNameInfo
**info
)
761 const char *dc_hostname
= NULL
;
762 const char *dc_domain_name
= NULL
;
763 const char *dc_address
= NULL
;
764 const char *dc_forest
= NULL
;
765 uint32_t dc_address_type
= 0;
766 uint32_t dc_flags
= 0;
767 struct GUID
*dc_domain_guid
= NULL
;
768 const char *dc_server_site
= NULL
;
769 const char *dc_client_site
= NULL
;
771 char addr
[INET6_ADDRSTRLEN
];
774 print_sockaddr(addr
, sizeof(addr
), ss
);
776 dc_address_type
= DS_ADDRESS_TYPE_INET
;
779 if (!ss
&& r
->sockaddr
.pdc_ip
) {
780 dc_address
= r
->sockaddr
.pdc_ip
;
781 dc_address_type
= DS_ADDRESS_TYPE_INET
;
783 dc_address
= r
->pdc_name
;
784 dc_address_type
= DS_ADDRESS_TYPE_NETBIOS
;
787 map_dc_and_domain_names(flags
,
796 dc_flags
|= r
->server_type
;
797 dc_forest
= r
->forest
;
798 dc_domain_guid
= &r
->domain_uuid
;
799 dc_server_site
= r
->server_site
;
800 dc_client_site
= r
->client_site
;
802 return make_domain_controller_info(mem_ctx
,
815 /****************************************************************
816 ****************************************************************/
818 static uint32_t map_ds_flags_to_nt_version(uint32_t flags
)
820 uint32_t nt_version
= 0;
822 if (flags
& DS_PDC_REQUIRED
) {
823 nt_version
|= NETLOGON_NT_VERSION_PDC
;
826 if (flags
& DS_GC_SERVER_REQUIRED
) {
827 nt_version
|= NETLOGON_NT_VERSION_GC
;
830 if (flags
& DS_TRY_NEXTCLOSEST_SITE
) {
831 nt_version
|= NETLOGON_NT_VERSION_WITH_CLOSEST_SITE
;
834 if (flags
& DS_IP_REQUIRED
) {
835 nt_version
|= NETLOGON_NT_VERSION_IP
;
841 /****************************************************************
842 ****************************************************************/
844 static NTSTATUS
process_dc_dns(TALLOC_CTX
*mem_ctx
,
845 const char *domain_name
,
847 struct ip_service_name
*dclist
,
849 struct netr_DsRGetDCNameInfo
**info
)
852 bool valid_dc
= false;
853 struct netlogon_samlogon_response
*r
= NULL
;
854 uint32_t nt_version
= NETLOGON_NT_VERSION_5
|
855 NETLOGON_NT_VERSION_5EX
;
856 uint32_t ret_flags
= 0;
859 nt_version
|= map_ds_flags_to_nt_version(flags
);
861 for (i
=0; i
<num_dcs
; i
++) {
863 DEBUG(10,("LDAP ping to %s\n", dclist
[i
].hostname
));
865 if (ads_cldap_netlogon(mem_ctx
, dclist
[i
].hostname
,
870 nt_version
= r
->ntver
;
871 ret_flags
= get_cldap_reply_server_flags(r
, nt_version
);
873 if (check_cldap_reply_required_flags(ret_flags
, flags
)) {
883 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
886 status
= make_dc_info_from_cldap_reply(mem_ctx
, flags
, &dclist
[i
].ss
,
887 &r
->data
.nt5_ex
, info
);
888 if (NT_STATUS_IS_OK(status
)) {
889 return store_cldap_reply(mem_ctx
, flags
, &dclist
[i
].ss
,
890 nt_version
, &r
->data
.nt5_ex
);
896 /****************************************************************
897 ****************************************************************/
899 static struct event_context
*ev_context(void)
901 static struct event_context
*ctx
;
903 if (!ctx
&& !(ctx
= event_context_init(NULL
))) {
904 smb_panic("Could not init event context");
909 /****************************************************************
910 ****************************************************************/
912 static struct messaging_context
*msg_context(TALLOC_CTX
*mem_ctx
)
914 static struct messaging_context
*ctx
;
916 if (!ctx
&& !(ctx
= messaging_init(mem_ctx
, procid_self(),
918 smb_panic("Could not init messaging context");
923 /****************************************************************
924 ****************************************************************/
926 static NTSTATUS
process_dc_netbios(TALLOC_CTX
*mem_ctx
,
927 struct messaging_context
*msg_ctx
,
928 const char *domain_name
,
930 struct ip_service_name
*dclist
,
932 struct netr_DsRGetDCNameInfo
**info
)
934 struct sockaddr_storage ss
;
935 struct ip_service ip_list
;
936 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
939 const char *dc_name
= NULL
;
941 struct netlogon_samlogon_response
*r
= NULL
;
942 bool store_cache
= false;
943 uint32_t nt_version
= NETLOGON_NT_VERSION_1
|
944 NETLOGON_NT_VERSION_5
|
945 NETLOGON_NT_VERSION_5EX_WITH_IP
;
948 msg_ctx
= msg_context(mem_ctx
);
951 if (flags
& DS_PDC_REQUIRED
) {
952 name_type
= NBT_NAME_PDC
;
955 nt_version
|= map_ds_flags_to_nt_version(flags
);
957 DEBUG(10,("process_dc_netbios\n"));
959 for (i
=0; i
<num_dcs
; i
++) {
961 ip_list
.ss
= dclist
[i
].ss
;
964 if (!interpret_string_addr_prefer_ipv4(&ss
, dclist
[i
].hostname
, AI_NUMERICHOST
)) {
965 return NT_STATUS_UNSUCCESSFUL
;
968 if (send_getdc_request(mem_ctx
, msg_ctx
,
969 &dclist
[i
].ss
, domain_name
,
974 for (k
=0; k
<5; k
++) {
975 if (receive_getdc_response(mem_ctx
,
982 namecache_store(dc_name
, NBT_NAME_SERVER
, 1, &ip_list
);
989 if (name_status_find(domain_name
,
995 struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1
;
997 r
= TALLOC_ZERO_P(mem_ctx
, struct netlogon_samlogon_response
);
998 NT_STATUS_HAVE_NO_MEMORY(r
);
1000 ZERO_STRUCT(logon1
);
1002 nt_version
= NETLOGON_NT_VERSION_1
;
1004 logon1
.nt_version
= nt_version
;
1005 logon1
.pdc_name
= tmp_dc_name
;
1006 logon1
.domain_name
= talloc_strdup_upper(mem_ctx
, domain_name
);
1007 NT_STATUS_HAVE_NO_MEMORY(logon1
.domain_name
);
1009 r
->data
.nt4
= logon1
;
1010 r
->ntver
= nt_version
;
1012 map_netlogon_samlogon_response(r
);
1014 namecache_store(tmp_dc_name
, NBT_NAME_SERVER
, 1, &ip_list
);
1020 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1024 status
= make_dc_info_from_cldap_reply(mem_ctx
, flags
, &dclist
[i
].ss
,
1025 &r
->data
.nt5_ex
, info
);
1026 if (NT_STATUS_IS_OK(status
) && store_cache
) {
1027 return store_cldap_reply(mem_ctx
, flags
, &dclist
[i
].ss
,
1028 nt_version
, &r
->data
.nt5_ex
);
1034 /****************************************************************
1035 ****************************************************************/
1037 static NTSTATUS
dsgetdcname_rediscover(TALLOC_CTX
*mem_ctx
,
1038 struct messaging_context
*msg_ctx
,
1039 const char *domain_name
,
1040 const struct GUID
*domain_guid
,
1042 const char *site_name
,
1043 struct netr_DsRGetDCNameInfo
**info
)
1045 NTSTATUS status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1046 struct ip_service_name
*dclist
= NULL
;
1049 DEBUG(10,("dsgetdcname_rediscover\n"));
1051 if (flags
& DS_IS_FLAT_NAME
) {
1053 status
= discover_dc_netbios(mem_ctx
, domain_name
, flags
,
1055 NT_STATUS_NOT_OK_RETURN(status
);
1057 return process_dc_netbios(mem_ctx
, msg_ctx
, domain_name
, flags
,
1058 dclist
, num_dcs
, info
);
1061 if (flags
& DS_IS_DNS_NAME
) {
1063 status
= discover_dc_dns(mem_ctx
, domain_name
, domain_guid
,
1064 flags
, site_name
, &dclist
, &num_dcs
);
1065 NT_STATUS_NOT_OK_RETURN(status
);
1067 return process_dc_dns(mem_ctx
, domain_name
, flags
,
1068 dclist
, num_dcs
, info
);
1071 status
= discover_dc_dns(mem_ctx
, domain_name
, domain_guid
, flags
,
1072 site_name
, &dclist
, &num_dcs
);
1074 if (NT_STATUS_IS_OK(status
) && num_dcs
!= 0) {
1076 status
= process_dc_dns(mem_ctx
, domain_name
, flags
, dclist
,
1078 if (NT_STATUS_IS_OK(status
)) {
1083 status
= discover_dc_netbios(mem_ctx
, domain_name
, flags
, &dclist
,
1085 NT_STATUS_NOT_OK_RETURN(status
);
1087 return process_dc_netbios(mem_ctx
, msg_ctx
, domain_name
, flags
, dclist
,
1091 static bool is_closest_site(struct netr_DsRGetDCNameInfo
*info
)
1093 if (info
->dc_flags
& DS_SERVER_CLOSEST
) {
1097 if (!info
->client_site_name
) {
1101 if (!info
->dc_site_name
) {
1105 if (strcmp(info
->client_site_name
, info
->dc_site_name
) == 0) {
1112 /********************************************************************
1115 This will be the only public function here.
1116 ********************************************************************/
1118 NTSTATUS
dsgetdcname(TALLOC_CTX
*mem_ctx
,
1119 struct messaging_context
*msg_ctx
,
1120 const char *domain_name
,
1121 const struct GUID
*domain_guid
,
1122 const char *site_name
,
1124 struct netr_DsRGetDCNameInfo
**info
)
1126 NTSTATUS status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1127 struct netr_DsRGetDCNameInfo
*myinfo
= NULL
;
1128 char *query_site
= NULL
;
1130 struct netr_DsRGetDCNameInfo
*first_info
= NULL
;
1132 DEBUG(10,("dsgetdcname: domain_name: %s, "
1133 "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
1135 domain_guid
? GUID_string(mem_ctx
, domain_guid
) : "(null)",
1140 if (!check_allowed_required_flags(flags
, site_name
)) {
1141 DEBUG(0,("invalid flags specified\n"));
1142 return NT_STATUS_INVALID_PARAMETER
;
1145 if ((site_name
== NULL
) || (site_name
[0] == '\0')) {
1146 query_site
= sitename_fetch(domain_name
);
1148 query_site
= SMB_STRDUP(site_name
);
1151 if (flags
& DS_FORCE_REDISCOVERY
) {
1155 status
= dsgetdcname_cached(mem_ctx
, msg_ctx
, domain_name
, domain_guid
,
1156 flags
, query_site
, &myinfo
);
1157 if (NT_STATUS_IS_OK(status
)) {
1161 if (flags
& DS_BACKGROUND_ONLY
) {
1166 status
= dsgetdcname_rediscover(mem_ctx
, msg_ctx
, domain_name
,
1167 domain_guid
, flags
, query_site
,
1171 SAFE_FREE(query_site
);
1173 if (!NT_STATUS_IS_OK(status
)) {
1176 return NT_STATUS_OK
;
1182 TALLOC_FREE(first_info
);
1183 } else if (!is_closest_site(myinfo
)) {
1185 first_info
= myinfo
;
1186 /* TODO: may use the next_closest_site here */
1187 query_site
= SMB_STRDUP(myinfo
->client_site_name
);
1192 return NT_STATUS_OK
;