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/>.
25 #define DSGETDCNAME_FMT "DSGETDCNAME/DOMAIN/%s"
27 #define DSGETDCNAME_CACHE_TTL 60*15
29 struct ip_service_name
{
30 struct sockaddr_storage ss
;
35 /****************************************************************
36 ****************************************************************/
38 void debug_dsdcinfo_flags(int lvl
, uint32_t flags
)
40 DEBUG(lvl
,("debug_dsdcinfo_flags: 0x%08x\n\t", flags
));
42 if (flags
& DS_FORCE_REDISCOVERY
)
43 DEBUGADD(lvl
,("DS_FORCE_REDISCOVERY "));
44 if (flags
& 0x000000002)
45 DEBUGADD(lvl
,("0x00000002 "));
46 if (flags
& 0x000000004)
47 DEBUGADD(lvl
,("0x00000004 "));
48 if (flags
& 0x000000008)
49 DEBUGADD(lvl
,("0x00000008 "));
50 if (flags
& DS_DIRECTORY_SERVICE_REQUIRED
)
51 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_REQUIRED "));
52 if (flags
& DS_DIRECTORY_SERVICE_PREFERRED
)
53 DEBUGADD(lvl
,("DS_DIRECTORY_SERVICE_PREFERRED "));
54 if (flags
& DS_GC_SERVER_REQUIRED
)
55 DEBUGADD(lvl
,("DS_GC_SERVER_REQUIRED "));
56 if (flags
& DS_PDC_REQUIRED
)
57 DEBUGADD(lvl
,("DS_PDC_REQUIRED "));
58 if (flags
& DS_BACKGROUND_ONLY
)
59 DEBUGADD(lvl
,("DS_BACKGROUND_ONLY "));
60 if (flags
& DS_IP_REQUIRED
)
61 DEBUGADD(lvl
,("DS_IP_REQUIRED "));
62 if (flags
& DS_KDC_REQUIRED
)
63 DEBUGADD(lvl
,("DS_KDC_REQUIRED "));
64 if (flags
& DS_TIMESERV_REQUIRED
)
65 DEBUGADD(lvl
,("DS_TIMESERV_REQUIRED "));
66 if (flags
& DS_WRITABLE_REQUIRED
)
67 DEBUGADD(lvl
,("DS_WRITABLE_REQUIRED "));
68 if (flags
& DS_GOOD_TIMESERV_PREFERRED
)
69 DEBUGADD(lvl
,("DS_GOOD_TIMESERV_PREFERRED "));
70 if (flags
& DS_AVOID_SELF
)
71 DEBUGADD(lvl
,("DS_AVOID_SELF "));
72 if (flags
& DS_ONLY_LDAP_NEEDED
)
73 DEBUGADD(lvl
,("DS_ONLY_LDAP_NEEDED "));
74 if (flags
& DS_IS_FLAT_NAME
)
75 DEBUGADD(lvl
,("DS_IS_FLAT_NAME "));
76 if (flags
& DS_IS_DNS_NAME
)
77 DEBUGADD(lvl
,("DS_IS_DNS_NAME "));
78 if (flags
& 0x00040000)
79 DEBUGADD(lvl
,("0x00040000 "));
80 if (flags
& 0x00080000)
81 DEBUGADD(lvl
,("0x00080000 "));
82 if (flags
& 0x00100000)
83 DEBUGADD(lvl
,("0x00100000 "));
84 if (flags
& 0x00200000)
85 DEBUGADD(lvl
,("0x00200000 "));
86 if (flags
& 0x00400000)
87 DEBUGADD(lvl
,("0x00400000 "));
88 if (flags
& 0x00800000)
89 DEBUGADD(lvl
,("0x00800000 "));
90 if (flags
& 0x01000000)
91 DEBUGADD(lvl
,("0x01000000 "));
92 if (flags
& 0x02000000)
93 DEBUGADD(lvl
,("0x02000000 "));
94 if (flags
& 0x04000000)
95 DEBUGADD(lvl
,("0x04000000 "));
96 if (flags
& 0x08000000)
97 DEBUGADD(lvl
,("0x08000000 "));
98 if (flags
& 0x10000000)
99 DEBUGADD(lvl
,("0x10000000 "));
100 if (flags
& 0x20000000)
101 DEBUGADD(lvl
,("0x20000000 "));
102 if (flags
& DS_RETURN_DNS_NAME
)
103 DEBUGADD(lvl
,("DS_RETURN_DNS_NAME "));
104 if (flags
& DS_RETURN_FLAT_NAME
)
105 DEBUGADD(lvl
,("DS_RETURN_FLAT_NAME "));
107 DEBUGADD(lvl
,("\n"));
110 /****************************************************************
111 ****************************************************************/
113 static char *dsgetdcname_cache_key(TALLOC_CTX
*mem_ctx
, const char *domain
)
115 if (!mem_ctx
|| !domain
) {
119 return talloc_asprintf_strupper_m(mem_ctx
, DSGETDCNAME_FMT
, domain
);
122 /****************************************************************
123 ****************************************************************/
125 static NTSTATUS
dsgetdcname_cache_delete(TALLOC_CTX
*mem_ctx
,
126 const char *domain_name
)
130 if (!gencache_init()) {
131 return NT_STATUS_INTERNAL_DB_ERROR
;
134 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
136 return NT_STATUS_NO_MEMORY
;
139 if (!gencache_del(key
)) {
140 return NT_STATUS_UNSUCCESSFUL
;
146 /****************************************************************
147 ****************************************************************/
149 static NTSTATUS
dsgetdcname_cache_store(TALLOC_CTX
*mem_ctx
,
150 const char *domain_name
,
151 struct netr_DsRGetDCNameInfo
*info
)
157 enum ndr_err_code ndr_err
;
159 if (!gencache_init()) {
160 return NT_STATUS_INTERNAL_DB_ERROR
;
163 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
165 return NT_STATUS_NO_MEMORY
;
168 expire_time
= time(NULL
) + DSGETDCNAME_CACHE_TTL
;
170 ndr_err
= ndr_push_struct_blob(&blob
, mem_ctx
, info
,
171 (ndr_push_flags_fn_t
)ndr_push_netr_DsRGetDCNameInfo
);
172 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
173 return ndr_map_error2ntstatus(ndr_err
);
176 if (gencache_lock_entry(key
) != 0) {
177 data_blob_free(&blob
);
178 return NT_STATUS_LOCK_NOT_GRANTED
;
181 ret
= gencache_set_data_blob(key
, &blob
, expire_time
);
182 data_blob_free(&blob
);
184 gencache_unlock_entry(key
);
186 return ret
? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
189 /****************************************************************
190 ****************************************************************/
192 static NTSTATUS
dsgetdcname_cache_refresh(TALLOC_CTX
*mem_ctx
,
193 const char *domain_name
,
194 struct GUID
*domain_guid
,
196 const char *site_name
,
197 struct netr_DsRGetDCNameInfo
*info
)
199 struct nbt_cldap_netlogon_5 r
;
201 /* check if matching entry is older then 15 minutes, if yes, send
202 * CLDAP/MAILSLOT ping again and store the cached data */
206 if (ads_cldap_netlogon(mem_ctx
, info
->dc_unc
,
207 info
->domain_name
, &r
)) {
209 dsgetdcname_cache_delete(mem_ctx
, domain_name
);
211 return dsgetdcname_cache_store(mem_ctx
,
216 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
219 /****************************************************************
220 ****************************************************************/
222 #define RETURN_ON_FALSE(x) if (!x) return false;
224 static bool check_cldap_reply_required_flags(uint32_t ret_flags
,
227 if (req_flags
& DS_PDC_REQUIRED
)
228 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_PDC
);
230 if (req_flags
& DS_GC_SERVER_REQUIRED
)
231 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_GC
);
233 if (req_flags
& DS_ONLY_LDAP_NEEDED
)
234 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_LDAP
);
236 if ((req_flags
& DS_DIRECTORY_SERVICE_REQUIRED
) ||
237 (req_flags
& DS_DIRECTORY_SERVICE_PREFERRED
))
238 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_DS
);
240 if (req_flags
& DS_KDC_REQUIRED
)
241 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_KDC
);
243 if (req_flags
& DS_TIMESERV_REQUIRED
)
244 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_TIMESERV
);
246 if (req_flags
& DS_WRITABLE_REQUIRED
)
247 RETURN_ON_FALSE(ret_flags
& NBT_SERVER_WRITABLE
);
252 /****************************************************************
253 ****************************************************************/
255 static NTSTATUS
dsgetdcname_cache_fetch(TALLOC_CTX
*mem_ctx
,
256 const char *domain_name
,
257 struct GUID
*domain_guid
,
259 const char *site_name
,
260 struct netr_DsRGetDCNameInfo
**info_p
,
265 enum ndr_err_code ndr_err
;
266 struct netr_DsRGetDCNameInfo
*info
;
268 if (!gencache_init()) {
269 return NT_STATUS_INTERNAL_DB_ERROR
;
272 key
= dsgetdcname_cache_key(mem_ctx
, domain_name
);
274 return NT_STATUS_NO_MEMORY
;
277 if (!gencache_get_data_blob(key
, &blob
, expired
)) {
278 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
281 info
= TALLOC_ZERO_P(mem_ctx
, struct netr_DsRGetDCNameInfo
);
283 return NT_STATUS_NO_MEMORY
;
286 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, info
,
287 (ndr_pull_flags_fn_t
)ndr_pull_netr_DsRGetDCNameInfo
);
289 data_blob_free(&blob
);
290 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
291 dsgetdcname_cache_delete(mem_ctx
, domain_name
);
292 return ndr_map_error2ntstatus(ndr_err
);
295 if (DEBUGLEVEL
>= 10) {
296 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo
, info
);
300 if (!check_cldap_reply_required_flags(info
->dc_flags
, flags
)) {
301 DEBUG(10,("invalid flags\n"));
302 return NT_STATUS_INVALID_PARAMETER
;
305 if ((flags
& DS_IP_REQUIRED
) &&
306 (info
->dc_address_type
!= DS_ADDRESS_TYPE_INET
)) {
307 return NT_STATUS_INVALID_PARAMETER_MIX
;
315 /****************************************************************
316 ****************************************************************/
318 static NTSTATUS
dsgetdcname_cached(TALLOC_CTX
*mem_ctx
,
319 const char *domain_name
,
320 struct GUID
*domain_guid
,
322 const char *site_name
,
323 struct netr_DsRGetDCNameInfo
**info
)
326 bool expired
= false;
328 status
= dsgetdcname_cache_fetch(mem_ctx
, domain_name
, domain_guid
,
329 flags
, site_name
, info
, &expired
);
330 if (!NT_STATUS_IS_OK(status
)) {
331 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
333 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
336 if (flags
& DS_BACKGROUND_ONLY
) {
341 status
= dsgetdcname_cache_refresh(mem_ctx
, domain_name
,
344 if (!NT_STATUS_IS_OK(status
)) {
352 /****************************************************************
353 ****************************************************************/
355 static bool check_allowed_required_flags(uint32_t flags
)
357 uint32_t return_type
= flags
& (DS_RETURN_FLAT_NAME
|DS_RETURN_DNS_NAME
);
358 uint32_t offered_type
= flags
& (DS_IS_FLAT_NAME
|DS_IS_DNS_NAME
);
359 uint32_t query_type
= flags
& (DS_BACKGROUND_ONLY
|DS_FORCE_REDISCOVERY
);
361 /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
362 * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
364 debug_dsdcinfo_flags(10, flags
);
366 if (return_type
== (DS_RETURN_FLAT_NAME
|DS_RETURN_DNS_NAME
)) {
370 if (offered_type
== (DS_IS_DNS_NAME
|DS_IS_FLAT_NAME
)) {
374 if (query_type
== (DS_BACKGROUND_ONLY
|DS_FORCE_REDISCOVERY
)) {
379 if ((flags
& DS_RETURN_DNS_NAME
) && (!(flags
& DS_IP_REQUIRED
))) {
380 printf("gd: here5 \n");
387 /****************************************************************
388 ****************************************************************/
390 static NTSTATUS
discover_dc_netbios(TALLOC_CTX
*mem_ctx
,
391 const char *domain_name
,
393 struct ip_service_name
**returned_dclist
,
397 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
398 struct ip_service
*iplist
;
400 struct ip_service_name
*dclist
= NULL
;
403 *returned_dclist
= NULL
;
406 if (lp_disable_netbios()) {
407 return NT_STATUS_NOT_SUPPORTED
;
410 if (flags
& DS_PDC_REQUIRED
) {
411 name_type
= NBT_NAME_PDC
;
414 status
= internal_resolve_name(domain_name
, name_type
, NULL
,
416 "lmhosts wins bcast");
417 if (!NT_STATUS_IS_OK(status
)) {
418 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
422 dclist
= TALLOC_ZERO_ARRAY(mem_ctx
, struct ip_service_name
, count
);
424 return NT_STATUS_NO_MEMORY
;
427 for (i
=0; i
<count
; i
++) {
429 char addr
[INET6_ADDRSTRLEN
];
430 struct ip_service_name
*r
= &dclist
[i
];
432 print_sockaddr(addr
, sizeof(addr
),
435 r
->ss
= iplist
[i
].ss
;
436 r
->port
= iplist
[i
].port
;
437 r
->hostname
= talloc_strdup(mem_ctx
, addr
);
439 return NT_STATUS_NO_MEMORY
;
444 *returned_dclist
= dclist
;
445 *returned_count
= count
;
450 /****************************************************************
451 ****************************************************************/
453 static NTSTATUS
discover_dc_dns(TALLOC_CTX
*mem_ctx
,
454 const char *domain_name
,
455 struct GUID
*domain_guid
,
457 const char *site_name
,
458 struct ip_service_name
**returned_dclist
,
463 struct dns_rr_srv
*dcs
= NULL
;
466 struct ip_service_name
*dclist
= NULL
;
469 if ((!(flags
& DS_DIRECTORY_SERVICE_REQUIRED
)) &&
470 (!(flags
& DS_KDC_REQUIRED
)) &&
471 (!(flags
& DS_GC_SERVER_REQUIRED
)) &&
472 (!(flags
& DS_PDC_REQUIRED
))) {
473 DEBUG(1,("discover_dc_dns: invalid flags\n"));
474 return NT_STATUS_INVALID_PARAMETER
;
477 if (flags
& DS_PDC_REQUIRED
) {
478 status
= ads_dns_query_pdc(mem_ctx
, domain_name
,
480 } else if (flags
& DS_GC_SERVER_REQUIRED
) {
481 status
= ads_dns_query_gcs(mem_ctx
, domain_name
, site_name
,
483 } else if (flags
& DS_KDC_REQUIRED
) {
484 status
= ads_dns_query_kdcs(mem_ctx
, domain_name
, site_name
,
486 } else if (flags
& DS_DIRECTORY_SERVICE_REQUIRED
) {
487 status
= ads_dns_query_dcs(mem_ctx
, domain_name
, site_name
,
489 } else if (domain_guid
) {
490 status
= ads_dns_query_dcs_guid(mem_ctx
, domain_name
,
491 domain_guid
, &dcs
, &numdcs
);
494 DEBUG(1,("discover_dc_dns: not enough input\n"));
495 status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
498 if (!NT_STATUS_IS_OK(status
)) {
503 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
506 for (i
=0;i
<numdcs
;i
++) {
507 numaddrs
+= MAX(dcs
[i
].num_ips
,1);
510 dclist
= TALLOC_ZERO_ARRAY(mem_ctx
,
511 struct ip_service_name
,
514 return NT_STATUS_NO_MEMORY
;
517 /* now unroll the list of IP addresses */
523 while ((i
< numdcs
) && (count
< numaddrs
)) {
525 struct ip_service_name
*r
= &dclist
[count
];
527 r
->port
= dcs
[count
].port
;
528 r
->hostname
= dcs
[count
].hostname
;
530 if (!(flags
& DS_IP_REQUIRED
)) {
535 /* If we don't have an IP list for a name, lookup it up */
538 interpret_string_addr(&r
->ss
, dcs
[i
].hostname
, 0);
542 /* use the IP addresses from the SRV sresponse */
544 if (j
>= dcs
[i
].num_ips
) {
550 r
->ss
= dcs
[i
].ss_s
[j
];
554 /* make sure it is a valid IP. I considered checking the
555 * negative connection cache, but this is the wrong place for
556 * it. Maybe only as a hac. After think about it, if all of
557 * the IP addresses retuend from DNS are dead, what hope does a
558 * netbios name lookup have? The standard reason for falling
559 * back to netbios lookups is that our DNS server doesn't know
560 * anything about the DC's -- jerry */
562 if (!is_zero_addr(&r
->ss
)) {
568 *returned_dclist
= dclist
;
569 *return_count
= count
;
575 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
578 /****************************************************************
579 ****************************************************************/
581 static NTSTATUS
make_domain_controller_info(TALLOC_CTX
*mem_ctx
,
583 const char *dc_address
,
584 uint32_t dc_address_type
,
585 const struct GUID
*domain_guid
,
586 const char *domain_name
,
587 const char *forest_name
,
589 const char *dc_site_name
,
590 const char *client_site_name
,
591 struct netr_DsRGetDCNameInfo
**info_out
)
593 struct netr_DsRGetDCNameInfo
*info
;
595 info
= TALLOC_ZERO_P(mem_ctx
, struct netr_DsRGetDCNameInfo
);
596 NT_STATUS_HAVE_NO_MEMORY(info
);
599 info
->dc_unc
= talloc_strdup(mem_ctx
, dc_unc
);
600 NT_STATUS_HAVE_NO_MEMORY(info
->dc_unc
);
604 info
->dc_address
= talloc_strdup(mem_ctx
, dc_address
);
605 NT_STATUS_HAVE_NO_MEMORY(info
->dc_address
);
608 info
->dc_address_type
= dc_address_type
;
611 info
->domain_guid
= *domain_guid
;
615 info
->domain_name
= talloc_strdup(mem_ctx
, domain_name
);
616 NT_STATUS_HAVE_NO_MEMORY(info
->domain_name
);
620 info
->forest_name
= talloc_strdup(mem_ctx
, forest_name
);
621 NT_STATUS_HAVE_NO_MEMORY(info
->forest_name
);
624 info
->dc_flags
= flags
;
627 info
->dc_site_name
= talloc_strdup(mem_ctx
, dc_site_name
);
628 NT_STATUS_HAVE_NO_MEMORY(info
->dc_site_name
);
631 if (client_site_name
) {
632 info
->client_site_name
= talloc_strdup(mem_ctx
,
634 NT_STATUS_HAVE_NO_MEMORY(info
->client_site_name
);
642 /****************************************************************
643 ****************************************************************/
645 static NTSTATUS
process_dc_dns(TALLOC_CTX
*mem_ctx
,
646 const char *domain_name
,
648 struct ip_service_name
*dclist
,
650 struct netr_DsRGetDCNameInfo
**info
)
653 bool valid_dc
= false;
654 struct nbt_cldap_netlogon_5 r
;
655 const char *dc_hostname
, *dc_domain_name
;
656 const char *dc_address
;
657 uint32_t dc_address_type
;
660 for (i
=0; i
<num_dcs
; i
++) {
664 DEBUG(10,("LDAP ping to %s\n", dclist
[i
].hostname
));
666 if ((ads_cldap_netlogon(mem_ctx
, dclist
[i
].hostname
,
668 (check_cldap_reply_required_flags(r
.server_type
, flags
))) {
677 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
680 dc_flags
= r
.server_type
;
682 if (flags
& DS_RETURN_FLAT_NAME
) {
683 if (!strlen(r
.pdc_name
) || !strlen(r
.domain
)) {
684 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
686 dc_hostname
= r
.pdc_name
;
687 dc_domain_name
= r
.domain
;
688 } else if (flags
& DS_RETURN_DNS_NAME
) {
689 if (!strlen(r
.pdc_dns_name
) || !strlen(r
.dns_domain
)) {
690 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
692 dc_hostname
= r
.pdc_dns_name
;
693 dc_domain_name
= r
.dns_domain
;
694 dc_flags
|= DS_DNS_DOMAIN
| DS_DNS_CONTROLLER
;
697 dc_hostname
= r
.pdc_dns_name
;
698 dc_domain_name
= r
.dns_domain
;
699 dc_flags
|= DS_DNS_DOMAIN
| DS_DNS_CONTROLLER
;
702 if (flags
& DS_IP_REQUIRED
) {
703 char addr
[INET6_ADDRSTRLEN
];
704 print_sockaddr(addr
, sizeof(addr
), &dclist
[i
].ss
);
705 dc_address
= talloc_asprintf(mem_ctx
, "\\\\%s",
707 dc_address_type
= DS_ADDRESS_TYPE_INET
;
709 dc_address
= talloc_asprintf(mem_ctx
, "\\\\%s",
711 dc_address_type
= DS_ADDRESS_TYPE_NETBIOS
;
713 NT_STATUS_HAVE_NO_MEMORY(dc_address
);
716 dc_flags
|= DS_DNS_FOREST
;
719 return make_domain_controller_info(mem_ctx
,
733 /****************************************************************
734 ****************************************************************/
736 static struct event_context
*ev_context(void)
738 static struct event_context
*ctx
;
740 if (!ctx
&& !(ctx
= event_context_init(NULL
))) {
741 smb_panic("Could not init event context");
746 /****************************************************************
747 ****************************************************************/
749 static struct messaging_context
*msg_context(TALLOC_CTX
*mem_ctx
)
751 static struct messaging_context
*ctx
;
753 if (!ctx
&& !(ctx
= messaging_init(mem_ctx
, server_id_self(),
755 smb_panic("Could not init messaging context");
760 /****************************************************************
761 ****************************************************************/
763 static NTSTATUS
process_dc_netbios(TALLOC_CTX
*mem_ctx
,
764 const char *domain_name
,
766 struct ip_service_name
*dclist
,
768 struct netr_DsRGetDCNameInfo
**info
)
770 struct sockaddr_storage ss
;
771 struct ip_service ip_list
;
772 enum nbt_name_type name_type
= NBT_NAME_LOGON
;
775 const char *dc_hostname
, *dc_domain_name
;
776 const char *dc_address
;
777 uint32_t dc_address_type
;
778 uint32_t dc_flags
= 0;
779 const char *dc_name
= NULL
;
781 struct messaging_context
*msg_ctx
= msg_context(mem_ctx
);
783 if (flags
& DS_PDC_REQUIRED
) {
784 name_type
= NBT_NAME_PDC
;
787 DEBUG(10,("process_dc_netbios\n"));
789 for (i
=0; i
<num_dcs
; i
++) {
791 ip_list
.ss
= dclist
[i
].ss
;
794 if (!interpret_string_addr(&ss
, dclist
[i
].hostname
, AI_NUMERICHOST
)) {
795 return NT_STATUS_UNSUCCESSFUL
;
798 if (send_getdc_request(mem_ctx
, msg_ctx
,
799 &dclist
[i
].ss
, domain_name
, NULL
))
803 for (k
=0; k
<5; k
++) {
804 if (receive_getdc_response(mem_ctx
,
808 namecache_store(dc_name
, NBT_NAME_SERVER
, 1, &ip_list
);
809 dc_hostname
= dc_name
;
810 dc_domain_name
= talloc_strdup_upper(mem_ctx
, domain_name
);
811 NT_STATUS_HAVE_NO_MEMORY(dc_domain_name
);
818 if (name_status_find(domain_name
,
824 dc_hostname
= tmp_dc_name
;
825 dc_domain_name
= talloc_strdup_upper(mem_ctx
, domain_name
);
826 namecache_store(dc_name
, NBT_NAME_SERVER
, 1, &ip_list
);
831 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
835 if (flags
& DS_IP_REQUIRED
) {
836 char addr
[INET6_ADDRSTRLEN
];
837 print_sockaddr(addr
, sizeof(addr
), &dclist
[i
].ss
);
838 dc_address
= talloc_asprintf(mem_ctx
, "\\\\%s", addr
);
839 dc_address_type
= DS_ADDRESS_TYPE_INET
;
841 dc_address
= talloc_asprintf(mem_ctx
, "\\\\%s", dc_hostname
);
842 dc_address_type
= DS_ADDRESS_TYPE_NETBIOS
;
845 if (flags
& DS_PDC_REQUIRED
) {
846 dc_flags
|= NBT_SERVER_PDC
| NBT_SERVER_WRITABLE
;
849 return make_domain_controller_info(mem_ctx
,
862 /****************************************************************
863 ****************************************************************/
865 static NTSTATUS
dsgetdcname_rediscover(TALLOC_CTX
*mem_ctx
,
866 const char *domain_name
,
867 struct GUID
*domain_guid
,
869 const char *site_name
,
870 struct netr_DsRGetDCNameInfo
**info
)
872 NTSTATUS status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
873 struct ip_service_name
*dclist
= NULL
;
876 DEBUG(10,("dsgetdcname_rediscover\n"));
878 if (flags
& DS_IS_FLAT_NAME
) {
880 status
= discover_dc_netbios(mem_ctx
, domain_name
, flags
,
882 NT_STATUS_NOT_OK_RETURN(status
);
884 return process_dc_netbios(mem_ctx
, domain_name
, flags
,
885 dclist
, num_dcs
, info
);
888 if (flags
& DS_IS_DNS_NAME
) {
890 status
= discover_dc_dns(mem_ctx
, domain_name
, domain_guid
,
891 flags
, site_name
, &dclist
, &num_dcs
);
892 NT_STATUS_NOT_OK_RETURN(status
);
894 return process_dc_dns(mem_ctx
, domain_name
, flags
,
895 dclist
, num_dcs
, info
);
898 status
= discover_dc_dns(mem_ctx
, domain_name
, domain_guid
, flags
,
899 site_name
, &dclist
, &num_dcs
);
901 if (NT_STATUS_IS_OK(status
) && num_dcs
!= 0) {
903 status
= process_dc_dns(mem_ctx
, domain_name
, flags
, dclist
,
905 if (NT_STATUS_IS_OK(status
)) {
910 status
= discover_dc_netbios(mem_ctx
, domain_name
, flags
, &dclist
,
912 NT_STATUS_NOT_OK_RETURN(status
);
914 return process_dc_netbios(mem_ctx
, domain_name
, flags
, dclist
,
918 /********************************************************************
921 This will be the only public function here.
922 ********************************************************************/
924 NTSTATUS
dsgetdcname(TALLOC_CTX
*mem_ctx
,
925 const char *domain_name
,
926 struct GUID
*domain_guid
,
927 const char *site_name
,
929 struct netr_DsRGetDCNameInfo
**info
)
931 NTSTATUS status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
932 struct netr_DsRGetDCNameInfo
*myinfo
= NULL
;
934 DEBUG(10,("dsgetdcname: domain_name: %s, "
935 "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
937 domain_guid
? GUID_string(mem_ctx
, domain_guid
) : "(null)",
942 if (!check_allowed_required_flags(flags
)) {
943 DEBUG(0,("invalid flags specified\n"));
944 return NT_STATUS_INVALID_PARAMETER
;
947 if (flags
& DS_FORCE_REDISCOVERY
) {
951 status
= dsgetdcname_cached(mem_ctx
, domain_name
, domain_guid
,
952 flags
, site_name
, &myinfo
);
953 if (NT_STATUS_IS_OK(status
)) {
958 if (flags
& DS_BACKGROUND_ONLY
) {
963 status
= dsgetdcname_rediscover(mem_ctx
, domain_name
,
964 domain_guid
, flags
, site_name
,
967 if (NT_STATUS_IS_OK(status
)) {
968 dsgetdcname_cache_store(mem_ctx
, domain_name
, myinfo
);