Add some missing whitespaces.
[Samba.git] / source / libsmb / dsgetdcname.c
blob5af65c5dcaab1709600683e29c4dd0adaa004a3f
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 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 */
204 ZERO_STRUCT(r);
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,
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 & 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);
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 *returned_count)
396 NTSTATUS status;
397 enum nbt_name_type name_type = NBT_NAME_LOGON;
398 struct ip_service *iplist;
399 int i;
400 struct ip_service_name *dclist = NULL;
401 int count;
403 *returned_dclist = NULL;
404 *returned_count = 0;
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,
415 &iplist, &count,
416 "lmhosts wins bcast");
417 if (!NT_STATUS_IS_OK(status)) {
418 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
419 return status;
422 dclist = TALLOC_ZERO_ARRAY(mem_ctx, struct ip_service_name, count);
423 if (!dclist) {
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),
433 &iplist[i].ss);
435 r->ss = iplist[i].ss;
436 r->port = iplist[i].port;
437 r->hostname = talloc_strdup(mem_ctx, addr);
438 if (!r->hostname) {
439 return NT_STATUS_NO_MEMORY;
444 *returned_dclist = dclist;
445 *returned_count = count;
447 return NT_STATUS_OK;
450 /****************************************************************
451 ****************************************************************/
453 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
454 const char *domain_name,
455 struct GUID *domain_guid,
456 uint32_t flags,
457 const char *site_name,
458 struct ip_service_name **returned_dclist,
459 int *return_count)
461 int i, j;
462 NTSTATUS status;
463 struct dns_rr_srv *dcs = NULL;
464 int numdcs = 0;
465 int numaddrs = 0;
466 struct ip_service_name *dclist = NULL;
467 int count = 0;
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,
479 &dcs, &numdcs);
480 } else if (flags & DS_GC_SERVER_REQUIRED) {
481 status = ads_dns_query_gcs(mem_ctx, domain_name, site_name,
482 &dcs, &numdcs);
483 } else if (flags & DS_KDC_REQUIRED) {
484 status = ads_dns_query_kdcs(mem_ctx, domain_name, site_name,
485 &dcs, &numdcs);
486 } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
487 status = ads_dns_query_dcs(mem_ctx, domain_name, site_name,
488 &dcs, &numdcs);
489 } else if (domain_guid) {
490 status = ads_dns_query_dcs_guid(mem_ctx, domain_name,
491 domain_guid, &dcs, &numdcs);
492 } else {
493 /* FIXME: ? */
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)) {
499 return status;
502 if (numdcs == 0) {
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,
512 numaddrs);
513 if (!dclist) {
514 return NT_STATUS_NO_MEMORY;
517 /* now unroll the list of IP addresses */
519 *return_count = 0;
520 i = 0;
521 j = 0;
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)) {
531 count++;
532 continue;
535 /* If we don't have an IP list for a name, lookup it up */
537 if (!dcs[i].ss_s) {
538 interpret_string_addr(&r->ss, dcs[i].hostname, 0);
539 i++;
540 j = 0;
541 } else {
542 /* use the IP addresses from the SRV sresponse */
544 if (j >= dcs[i].num_ips) {
545 i++;
546 j = 0;
547 continue;
550 r->ss = dcs[i].ss_s[j];
551 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)) {
563 count++;
564 continue;
568 *returned_dclist = dclist;
569 *return_count = count;
571 if (count > 0) {
572 return NT_STATUS_OK;
575 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
578 /****************************************************************
579 ****************************************************************/
581 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
582 const char *dc_unc,
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,
588 uint32_t flags,
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);
598 if (dc_unc) {
599 info->dc_unc = talloc_strdup(mem_ctx, dc_unc);
600 NT_STATUS_HAVE_NO_MEMORY(info->dc_unc);
603 if (dc_address) {
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;
610 if (domain_guid) {
611 info->domain_guid = *domain_guid;
614 if (domain_name) {
615 info->domain_name = talloc_strdup(mem_ctx, domain_name);
616 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
619 if (forest_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;
626 if (dc_site_name) {
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,
633 client_site_name);
634 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
637 *info_out = info;
639 return NT_STATUS_OK;
642 /****************************************************************
643 ****************************************************************/
645 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
646 const char *domain_name,
647 uint32_t flags,
648 struct ip_service_name *dclist,
649 int num_dcs,
650 struct netr_DsRGetDCNameInfo **info)
652 int i = 0;
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;
658 uint32_t dc_flags;
660 for (i=0; i<num_dcs; i++) {
662 ZERO_STRUCT(r);
664 DEBUG(10,("LDAP ping to %s\n", dclist[i].hostname));
666 if ((ads_cldap_netlogon(mem_ctx, dclist[i].hostname,
667 domain_name, &r)) &&
668 (check_cldap_reply_required_flags(r.server_type, flags))) {
669 valid_dc = true;
670 break;
673 continue;
676 if (!valid_dc) {
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;
695 } else {
696 /* FIXME */
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",
706 addr);
707 dc_address_type = DS_ADDRESS_TYPE_INET;
708 } else {
709 dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
710 r.pdc_name);
711 dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
713 NT_STATUS_HAVE_NO_MEMORY(dc_address);
715 if (r.forest) {
716 dc_flags |= DS_DNS_FOREST;
719 return make_domain_controller_info(mem_ctx,
720 dc_hostname,
721 dc_address,
722 dc_address_type,
723 &r.domain_uuid,
724 dc_domain_name,
725 r.forest,
726 dc_flags,
727 r.server_site,
728 r.client_site,
729 info);
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");
743 return ctx;
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(),
754 ev_context()))) {
755 smb_panic("Could not init messaging context");
757 return ctx;
760 /****************************************************************
761 ****************************************************************/
763 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
764 const char *domain_name,
765 uint32_t flags,
766 struct ip_service_name *dclist,
767 int num_dcs,
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;
774 int i;
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;
780 fstring tmp_dc_name;
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;
792 ip_list.port = 0;
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))
801 int k;
802 smb_msleep(100);
803 for (k=0; k<5; k++) {
804 if (receive_getdc_response(mem_ctx,
805 &dclist[i].ss,
806 domain_name,
807 &dc_name)) {
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);
812 goto make_reply;
814 smb_msleep(500);
818 if (name_status_find(domain_name,
819 name_type,
820 NBT_NAME_SERVER,
821 &dclist[i].ss,
822 tmp_dc_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);
827 goto make_reply;
831 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
833 make_reply:
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;
840 } else {
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,
850 dc_hostname,
851 dc_address,
852 dc_address_type,
853 NULL,
854 dc_domain_name,
855 NULL,
856 dc_flags,
857 NULL,
858 NULL,
859 info);
862 /****************************************************************
863 ****************************************************************/
865 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
866 const char *domain_name,
867 struct GUID *domain_guid,
868 uint32_t flags,
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;
874 int num_dcs;
876 DEBUG(10,("dsgetdcname_rediscover\n"));
878 if (flags & DS_IS_FLAT_NAME) {
880 status = discover_dc_netbios(mem_ctx, domain_name, flags,
881 &dclist, &num_dcs);
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,
904 num_dcs, info);
905 if (NT_STATUS_IS_OK(status)) {
906 return status;
910 status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
911 &num_dcs);
912 NT_STATUS_NOT_OK_RETURN(status);
914 return process_dc_netbios(mem_ctx, domain_name, flags, dclist,
915 num_dcs, info);
918 /********************************************************************
919 dsgetdcname.
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,
928 uint32_t flags,
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",
936 domain_name,
937 domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)",
938 site_name, flags));
940 *info = 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) {
948 goto rediscover;
951 status = dsgetdcname_cached(mem_ctx, domain_name, domain_guid,
952 flags, site_name, &myinfo);
953 if (NT_STATUS_IS_OK(status)) {
954 *info = myinfo;
955 return status;
958 if (flags & DS_BACKGROUND_ONLY) {
959 return status;
962 rediscover:
963 status = dsgetdcname_rediscover(mem_ctx, domain_name,
964 domain_guid, flags, site_name,
965 &myinfo);
967 if (NT_STATUS_IS_OK(status)) {
968 dsgetdcname_cache_store(mem_ctx, domain_name, myinfo);
969 *info = myinfo;
972 return status;