IDL: Import drsuapi.idl from samba4.
[Samba/gebeck_regimport.git] / source3 / libsmb / dsgetdcname.c
blob7af43648d78f814d0fe9e0866e898931e2b05c3f
1 /*
2 Unix SMB/CIFS implementation.
4 dsgetdcname
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/>.
23 #include "includes.h"
25 #define DSGETDCNAME_FMT "DSGETDCNAME/DOMAIN/%s"
26 /* 15 minutes */
27 #define DSGETDCNAME_CACHE_TTL 60*15
29 struct ip_service_name {
30 struct sockaddr_storage ss;
31 unsigned port;
32 const char *hostname;
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 "));
106 if (flags)
107 DEBUGADD(lvl,("\n"));
110 /****************************************************************
111 ****************************************************************/
113 static char *dsgetdcname_cache_key(TALLOC_CTX *mem_ctx, const char *domain)
115 if (!mem_ctx || !domain) {
116 return NULL;
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)
128 char *key;
130 if (!gencache_init()) {
131 return NT_STATUS_INTERNAL_DB_ERROR;
134 key = dsgetdcname_cache_key(mem_ctx, domain_name);
135 if (!key) {
136 return NT_STATUS_NO_MEMORY;
139 if (!gencache_del(key)) {
140 return NT_STATUS_UNSUCCESSFUL;
143 return NT_STATUS_OK;
146 /****************************************************************
147 ****************************************************************/
149 static NTSTATUS dsgetdcname_cache_store(TALLOC_CTX *mem_ctx,
150 const char *domain_name,
151 struct netr_DsRGetDCNameInfo *info)
153 time_t expire_time;
154 char *key;
155 bool ret = false;
156 DATA_BLOB blob;
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);
164 if (!key) {
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,
195 uint32_t flags,
196 const char *site_name,
197 struct netr_DsRGetDCNameInfo *info)
199 struct cldap_netlogon_reply r;
201 /* check if matching entry is older then 15 minutes, if yes, send
202 * CLDAP/MAILSLOT ping again and store the cached data */
204 ZERO_STRUCT(r);
206 if (ads_cldap_netlogon(info->dc_unc,
207 info->domain_name, &r)) {
209 dsgetdcname_cache_delete(mem_ctx, domain_name);
211 return dsgetdcname_cache_store(mem_ctx,
212 info->domain_name,
213 info);
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,
225 uint32_t req_flags)
227 if (req_flags & DS_PDC_REQUIRED)
228 RETURN_ON_FALSE(ret_flags & ADS_PDC);
230 if (req_flags & DS_GC_SERVER_REQUIRED)
231 RETURN_ON_FALSE(ret_flags & ADS_GC);
233 if (req_flags & DS_ONLY_LDAP_NEEDED)
234 RETURN_ON_FALSE(ret_flags & ADS_LDAP);
236 if ((req_flags & DS_DIRECTORY_SERVICE_REQUIRED) ||
237 (req_flags & DS_DIRECTORY_SERVICE_PREFERRED))
238 RETURN_ON_FALSE(ret_flags & ADS_DS);
240 if (req_flags & DS_KDC_REQUIRED)
241 RETURN_ON_FALSE(ret_flags & ADS_KDC);
243 if (req_flags & DS_TIMESERV_REQUIRED)
244 RETURN_ON_FALSE(ret_flags & ADS_TIMESERV);
246 if (req_flags & DS_WRITABLE_REQUIRED)
247 RETURN_ON_FALSE(ret_flags & ADS_WRITABLE);
249 return true;
252 /****************************************************************
253 ****************************************************************/
255 static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
256 const char *domain_name,
257 struct GUID *domain_guid,
258 uint32_t flags,
259 const char *site_name,
260 struct netr_DsRGetDCNameInfo **info_p,
261 bool *expired)
263 char *key;
264 DATA_BLOB blob;
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);
273 if (!key) {
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);
282 if (!info) {
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);
299 /* check flags */
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;
310 *info_p = info;
312 return NT_STATUS_OK;
315 /****************************************************************
316 ****************************************************************/
318 static NTSTATUS dsgetdcname_cached(TALLOC_CTX *mem_ctx,
319 const char *domain_name,
320 struct GUID *domain_guid,
321 uint32_t flags,
322 const char *site_name,
323 struct netr_DsRGetDCNameInfo **info)
325 NTSTATUS status;
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",
332 nt_errstr(status)));
333 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
336 if (flags & DS_BACKGROUND_ONLY) {
337 return status;
340 if (expired) {
341 status = dsgetdcname_cache_refresh(mem_ctx, domain_name,
342 domain_guid, flags,
343 site_name, *info);
344 if (!NT_STATUS_IS_OK(status)) {
345 return status;
349 return 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)) {
367 return false;
370 if (offered_type == (DS_IS_DNS_NAME|DS_IS_FLAT_NAME)) {
371 return false;
374 if (query_type == (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY)) {
375 return false;
378 #if 0
379 if ((flags & DS_RETURN_DNS_NAME) && (!(flags & DS_IP_REQUIRED))) {
380 printf("gd: here5 \n");
381 return false;
383 #endif
384 return true;
387 /****************************************************************
388 ****************************************************************/
390 static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
391 const char *domain_name,
392 uint32_t flags,
393 struct ip_service_name **returned_dclist,
394 int *return_count)
396 if (lp_disable_netbios()) {
397 return NT_STATUS_NOT_SUPPORTED;
400 /* FIXME: code here */
402 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
405 /****************************************************************
406 ****************************************************************/
408 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
409 const char *domain_name,
410 struct GUID *domain_guid,
411 uint32_t flags,
412 const char *site_name,
413 struct ip_service_name **returned_dclist,
414 int *return_count)
416 int i, j;
417 NTSTATUS status;
418 struct dns_rr_srv *dcs = NULL;
419 int numdcs = 0;
420 int numaddrs = 0;
422 if ((!(flags & DS_DIRECTORY_SERVICE_REQUIRED)) &&
423 (!(flags & DS_KDC_REQUIRED)) &&
424 (!(flags & DS_GC_SERVER_REQUIRED)) &&
425 (!(flags & DS_PDC_REQUIRED))) {
426 DEBUG(1,("discover_dc_dns: invalid flags\n"));
427 return NT_STATUS_INVALID_PARAMETER;
430 if (flags & DS_PDC_REQUIRED) {
431 status = ads_dns_query_pdc(mem_ctx, domain_name,
432 &dcs, &numdcs);
433 } else if (flags & DS_GC_SERVER_REQUIRED) {
434 status = ads_dns_query_gcs(mem_ctx, domain_name, site_name,
435 &dcs, &numdcs);
436 } else if (flags & DS_KDC_REQUIRED) {
437 status = ads_dns_query_kdcs(mem_ctx, domain_name, site_name,
438 &dcs, &numdcs);
439 } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
440 status = ads_dns_query_dcs(mem_ctx, domain_name, site_name,
441 &dcs, &numdcs);
442 } else if (domain_guid) {
443 status = ads_dns_query_dcs_guid(mem_ctx, domain_name,
444 domain_guid, &dcs, &numdcs);
445 } else {
446 /* FIXME: ? */
447 DEBUG(1,("discover_dc_dns: not enough input\n"));
448 status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
451 if (!NT_STATUS_IS_OK(status)) {
452 return status;
455 if (numdcs == 0) {
456 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
459 for (i=0;i<numdcs;i++) {
460 numaddrs += MAX(dcs[i].num_ips,1);
463 if ((*returned_dclist = TALLOC_ZERO_ARRAY(mem_ctx,
464 struct ip_service_name,
465 numaddrs)) == NULL) {
466 return NT_STATUS_NO_MEMORY;
469 /* now unroll the list of IP addresses */
471 *return_count = 0;
472 i = 0;
473 j = 0;
474 while (i < numdcs && (*return_count<numaddrs)) {
476 struct ip_service_name *r = &(*returned_dclist)[*return_count];
478 r->port = dcs[i].port;
479 r->hostname = dcs[i].hostname;
481 if (!(flags & DS_IP_REQUIRED)) {
482 (*return_count)++;
483 continue;
486 /* If we don't have an IP list for a name, lookup it up */
488 if (!dcs[i].ss_s) {
489 interpret_string_addr(&r->ss, dcs[i].hostname, 0);
490 i++;
491 j = 0;
492 } else {
493 /* use the IP addresses from the SRV sresponse */
495 if (j >= dcs[i].num_ips) {
496 i++;
497 j = 0;
498 continue;
501 r->ss = dcs[i].ss_s[j];
502 j++;
505 /* make sure it is a valid IP. I considered checking the
506 * negative connection cache, but this is the wrong place for
507 * it. Maybe only as a hac. After think about it, if all of
508 * the IP addresses retuend from DNS are dead, what hope does a
509 * netbios name lookup have? The standard reason for falling
510 * back to netbios lookups is that our DNS server doesn't know
511 * anything about the DC's -- jerry */
513 if (!is_zero_addr(&r->ss)) {
514 (*return_count)++;
515 continue;
519 return (*return_count > 0) ? NT_STATUS_OK :
520 NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
523 /****************************************************************
524 ****************************************************************/
526 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
527 const char *dc_unc,
528 const char *dc_address,
529 uint32_t dc_address_type,
530 const struct GUID *domain_guid,
531 const char *domain_name,
532 const char *forest_name,
533 uint32_t flags,
534 const char *dc_site_name,
535 const char *client_site_name,
536 struct netr_DsRGetDCNameInfo **info_out)
538 struct netr_DsRGetDCNameInfo *info;
540 info = TALLOC_ZERO_P(mem_ctx, struct netr_DsRGetDCNameInfo);
541 NT_STATUS_HAVE_NO_MEMORY(info);
543 if (dc_unc) {
544 info->dc_unc = talloc_strdup(mem_ctx, dc_unc);
545 NT_STATUS_HAVE_NO_MEMORY(info->dc_unc);
548 if (dc_address) {
549 info->dc_address = talloc_strdup(mem_ctx, dc_address);
550 NT_STATUS_HAVE_NO_MEMORY(info->dc_address);
553 info->dc_address_type = dc_address_type;
555 if (domain_guid) {
556 info->domain_guid = *domain_guid;
559 if (domain_name) {
560 info->domain_name = talloc_strdup(mem_ctx, domain_name);
561 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
564 if (forest_name) {
565 info->forest_name = talloc_strdup(mem_ctx, forest_name);
566 NT_STATUS_HAVE_NO_MEMORY(info->forest_name);
569 info->dc_flags = flags;
571 if (dc_site_name) {
572 info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name);
573 NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name);
576 if (client_site_name) {
577 info->client_site_name = talloc_strdup(mem_ctx,
578 client_site_name);
579 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
582 *info_out = info;
584 return NT_STATUS_OK;
587 /****************************************************************
588 ****************************************************************/
590 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
591 const char *domain_name,
592 uint32_t flags,
593 struct ip_service_name **dclist,
594 int num_dcs,
595 struct netr_DsRGetDCNameInfo **info)
597 int i = 0;
598 bool valid_dc = false;
599 struct cldap_netlogon_reply r;
600 const char *dc_hostname, *dc_domain_name;
601 const char *dc_address;
602 uint32_t dc_address_type;
603 uint32_t dc_flags;
604 struct GUID dc_guid;
606 for (i=0; i<num_dcs; i++) {
608 ZERO_STRUCT(r);
610 if ((ads_cldap_netlogon(dclist[i]->hostname,
611 domain_name, &r)) &&
612 (check_cldap_reply_required_flags(r.flags, flags))) {
613 valid_dc = true;
614 break;
617 continue;
620 if (!valid_dc) {
621 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
624 dc_flags = r.flags;
626 if (flags & DS_RETURN_FLAT_NAME) {
627 if (!strlen(r.netbios_hostname) || !strlen(r.netbios_domain)) {
628 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
630 dc_hostname = r.netbios_hostname;
631 dc_domain_name = r.netbios_domain;
632 } else if (flags & DS_RETURN_DNS_NAME) {
633 if (!strlen(r.hostname) || !strlen(r.domain)) {
634 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
636 dc_hostname = r.hostname;
637 dc_domain_name = r.domain;
638 dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
639 } else {
640 /* FIXME */
641 dc_hostname = r.hostname;
642 dc_domain_name = r.domain;
643 dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
646 if (flags & DS_IP_REQUIRED) {
647 char addr[INET6_ADDRSTRLEN];
648 print_sockaddr(addr, sizeof(addr), &dclist[i]->ss);
649 dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
650 addr);
651 dc_address_type = DS_ADDRESS_TYPE_INET;
652 } else {
653 dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
654 r.netbios_hostname);
655 dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
657 NT_STATUS_HAVE_NO_MEMORY(dc_address);
658 smb_uuid_unpack(r.guid, &dc_guid);
660 if (r.forest) {
661 dc_flags |= DS_DNS_FOREST;
664 return make_domain_controller_info(mem_ctx,
665 dc_hostname,
666 dc_address,
667 dc_address_type,
668 &dc_guid,
669 dc_domain_name,
670 r.forest,
671 dc_flags,
672 r.server_site_name,
673 r.client_site_name,
674 info);
678 /****************************************************************
679 ****************************************************************/
681 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
682 const char *domain_name,
683 uint32_t flags,
684 struct ip_service_name **dclist,
685 int num_dcs,
686 struct netr_DsRGetDCNameInfo **info)
688 /* FIXME: code here */
690 return NT_STATUS_NOT_SUPPORTED;
693 /****************************************************************
694 ****************************************************************/
696 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
697 const char *domain_name,
698 struct GUID *domain_guid,
699 uint32_t flags,
700 const char *site_name,
701 struct netr_DsRGetDCNameInfo **info)
703 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
704 struct ip_service_name *dclist;
705 int num_dcs;
707 DEBUG(10,("dsgetdcname_rediscover\n"));
709 if (flags & DS_IS_FLAT_NAME) {
711 status = discover_dc_netbios(mem_ctx, domain_name, flags,
712 &dclist, &num_dcs);
713 NT_STATUS_NOT_OK_RETURN(status);
715 return process_dc_netbios(mem_ctx, domain_name, flags,
716 &dclist, num_dcs, info);
719 if (flags & DS_IS_DNS_NAME) {
721 status = discover_dc_dns(mem_ctx, domain_name, domain_guid,
722 flags, site_name, &dclist, &num_dcs);
723 NT_STATUS_NOT_OK_RETURN(status);
725 return process_dc_dns(mem_ctx, domain_name, flags,
726 &dclist, num_dcs, info);
729 status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags,
730 site_name, &dclist, &num_dcs);
732 if (NT_STATUS_IS_OK(status) && num_dcs != 0) {
734 status = process_dc_dns(mem_ctx, domain_name, flags, &dclist,
735 num_dcs, info);
736 if (NT_STATUS_IS_OK(status)) {
737 return status;
741 status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
742 &num_dcs);
743 NT_STATUS_NOT_OK_RETURN(status);
745 return process_dc_netbios(mem_ctx, domain_name, flags, &dclist,
746 num_dcs, info);
749 /********************************************************************
750 dsgetdcname.
752 This will be the only public function here.
753 ********************************************************************/
755 NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
756 const char *domain_name,
757 struct GUID *domain_guid,
758 const char *site_name,
759 uint32_t flags,
760 struct netr_DsRGetDCNameInfo **info)
762 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
763 struct netr_DsRGetDCNameInfo *myinfo = NULL;
765 DEBUG(10,("dsgetdcname: domain_name: %s, "
766 "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
767 domain_name,
768 domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)",
769 site_name, flags));
771 *info = NULL;
773 if (!check_allowed_required_flags(flags)) {
774 DEBUG(0,("invalid flags specified\n"));
775 return NT_STATUS_INVALID_PARAMETER;
778 if (flags & DS_FORCE_REDISCOVERY) {
779 goto rediscover;
782 status = dsgetdcname_cached(mem_ctx, domain_name, domain_guid,
783 flags, site_name, &myinfo);
784 if (NT_STATUS_IS_OK(status)) {
785 *info = myinfo;
786 return status;
789 if (flags & DS_BACKGROUND_ONLY) {
790 return status;
793 rediscover:
794 status = dsgetdcname_rediscover(mem_ctx, domain_name,
795 domain_guid, flags, site_name,
796 &myinfo);
798 if (NT_STATUS_IS_OK(status)) {
799 dsgetdcname_cache_store(mem_ctx, domain_name, myinfo);
800 *info = myinfo;
803 return status;