locking: use correct conversion specifier for printing variables
[Samba.git] / source3 / libsmb / dsgetdcname.c
blob0d1570a98547cd17b2c145d1d1df0de17e5424d2
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"
24 #include "libads/sitename_cache.h"
25 #include "../librpc/gen_ndr/ndr_netlogon.h"
26 #include "libads/cldap.h"
27 #include "../lib/addns/dnsquery.h"
28 #include "libsmb/clidgram.h"
30 /* 15 minutes */
31 #define DSGETDCNAME_CACHE_TTL 60*15
33 struct ip_service_name {
34 struct sockaddr_storage ss;
35 const char *hostname;
38 static NTSTATUS make_dc_info_from_cldap_reply(TALLOC_CTX *mem_ctx,
39 uint32_t flags,
40 struct sockaddr_storage *ss,
41 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
42 struct netr_DsRGetDCNameInfo **info);
44 /****************************************************************
45 ****************************************************************/
47 void debug_dsdcinfo_flags(int lvl, uint32_t flags)
49 DEBUG(lvl,("debug_dsdcinfo_flags: 0x%08x\n\t", flags));
51 if (flags & DS_FORCE_REDISCOVERY)
52 DEBUGADD(lvl,("DS_FORCE_REDISCOVERY "));
53 if (flags & 0x000000002)
54 DEBUGADD(lvl,("0x00000002 "));
55 if (flags & 0x000000004)
56 DEBUGADD(lvl,("0x00000004 "));
57 if (flags & 0x000000008)
58 DEBUGADD(lvl,("0x00000008 "));
59 if (flags & DS_DIRECTORY_SERVICE_REQUIRED)
60 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_REQUIRED "));
61 if (flags & DS_DIRECTORY_SERVICE_PREFERRED)
62 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_PREFERRED "));
63 if (flags & DS_GC_SERVER_REQUIRED)
64 DEBUGADD(lvl,("DS_GC_SERVER_REQUIRED "));
65 if (flags & DS_PDC_REQUIRED)
66 DEBUGADD(lvl,("DS_PDC_REQUIRED "));
67 if (flags & DS_BACKGROUND_ONLY)
68 DEBUGADD(lvl,("DS_BACKGROUND_ONLY "));
69 if (flags & DS_IP_REQUIRED)
70 DEBUGADD(lvl,("DS_IP_REQUIRED "));
71 if (flags & DS_KDC_REQUIRED)
72 DEBUGADD(lvl,("DS_KDC_REQUIRED "));
73 if (flags & DS_TIMESERV_REQUIRED)
74 DEBUGADD(lvl,("DS_TIMESERV_REQUIRED "));
75 if (flags & DS_WRITABLE_REQUIRED)
76 DEBUGADD(lvl,("DS_WRITABLE_REQUIRED "));
77 if (flags & DS_GOOD_TIMESERV_PREFERRED)
78 DEBUGADD(lvl,("DS_GOOD_TIMESERV_PREFERRED "));
79 if (flags & DS_AVOID_SELF)
80 DEBUGADD(lvl,("DS_AVOID_SELF "));
81 if (flags & DS_ONLY_LDAP_NEEDED)
82 DEBUGADD(lvl,("DS_ONLY_LDAP_NEEDED "));
83 if (flags & DS_IS_FLAT_NAME)
84 DEBUGADD(lvl,("DS_IS_FLAT_NAME "));
85 if (flags & DS_IS_DNS_NAME)
86 DEBUGADD(lvl,("DS_IS_DNS_NAME "));
87 if (flags & 0x00040000)
88 DEBUGADD(lvl,("0x00040000 "));
89 if (flags & 0x00080000)
90 DEBUGADD(lvl,("0x00080000 "));
91 if (flags & 0x00100000)
92 DEBUGADD(lvl,("0x00100000 "));
93 if (flags & 0x00200000)
94 DEBUGADD(lvl,("0x00200000 "));
95 if (flags & 0x00400000)
96 DEBUGADD(lvl,("0x00400000 "));
97 if (flags & 0x00800000)
98 DEBUGADD(lvl,("0x00800000 "));
99 if (flags & 0x01000000)
100 DEBUGADD(lvl,("0x01000000 "));
101 if (flags & 0x02000000)
102 DEBUGADD(lvl,("0x02000000 "));
103 if (flags & 0x04000000)
104 DEBUGADD(lvl,("0x04000000 "));
105 if (flags & 0x08000000)
106 DEBUGADD(lvl,("0x08000000 "));
107 if (flags & 0x10000000)
108 DEBUGADD(lvl,("0x10000000 "));
109 if (flags & 0x20000000)
110 DEBUGADD(lvl,("0x20000000 "));
111 if (flags & DS_RETURN_DNS_NAME)
112 DEBUGADD(lvl,("DS_RETURN_DNS_NAME "));
113 if (flags & DS_RETURN_FLAT_NAME)
114 DEBUGADD(lvl,("DS_RETURN_FLAT_NAME "));
115 if (flags)
116 DEBUGADD(lvl,("\n"));
119 /****************************************************************
120 ****************************************************************/
122 static char *dsgetdcname_cache_key(TALLOC_CTX *mem_ctx, const char *domain)
124 if (!domain) {
125 return NULL;
128 return talloc_asprintf_strupper_m(mem_ctx, "DSGETDCNAME/DOMAIN/%s",
129 domain);
132 /****************************************************************
133 ****************************************************************/
135 static NTSTATUS dsgetdcname_cache_delete(TALLOC_CTX *mem_ctx,
136 const char *domain_name)
138 char *key;
140 key = dsgetdcname_cache_key(mem_ctx, domain_name);
141 if (!key) {
142 return NT_STATUS_NO_MEMORY;
145 if (!gencache_del(key)) {
146 return NT_STATUS_UNSUCCESSFUL;
149 return NT_STATUS_OK;
152 /****************************************************************
153 ****************************************************************/
155 static NTSTATUS dsgetdcname_cache_store(TALLOC_CTX *mem_ctx,
156 const char *domain_name,
157 const DATA_BLOB *blob)
159 time_t expire_time;
160 char *key;
161 bool ret = false;
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 ret = gencache_set_data_blob(key, blob, expire_time);
172 return ret ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
175 /****************************************************************
176 ****************************************************************/
178 static NTSTATUS store_cldap_reply(TALLOC_CTX *mem_ctx,
179 uint32_t flags,
180 struct sockaddr_storage *ss,
181 uint32_t nt_version,
182 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r)
184 DATA_BLOB blob;
185 enum ndr_err_code ndr_err;
186 NTSTATUS status;
187 char addr[INET6_ADDRSTRLEN];
189 print_sockaddr(addr, sizeof(addr), ss);
191 /* FIXME */
192 r->sockaddr_size = 0x10; /* the w32 winsock addr size */
193 r->sockaddr.sockaddr_family = 2; /* AF_INET */
194 r->sockaddr.pdc_ip = talloc_strdup(mem_ctx, addr);
196 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, r,
197 (ndr_push_flags_fn_t)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX);
198 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
199 return ndr_map_error2ntstatus(ndr_err);
202 if (r->domain_name) {
203 status = dsgetdcname_cache_store(mem_ctx, r->domain_name, &blob);
204 if (!NT_STATUS_IS_OK(status)) {
205 goto done;
207 if (r->client_site) {
208 sitename_store(r->domain_name, r->client_site);
211 if (r->dns_domain) {
212 status = dsgetdcname_cache_store(mem_ctx, r->dns_domain, &blob);
213 if (!NT_STATUS_IS_OK(status)) {
214 goto done;
216 if (r->client_site) {
217 sitename_store(r->dns_domain, r->client_site);
221 status = NT_STATUS_OK;
223 done:
224 data_blob_free(&blob);
226 return status;
229 /****************************************************************
230 ****************************************************************/
232 static uint32_t get_cldap_reply_server_flags(struct netlogon_samlogon_response *r,
233 uint32_t nt_version)
235 switch (nt_version & 0x0000001f) {
236 case 0:
237 case 1:
238 case 16:
239 case 17:
240 return 0;
241 case 2:
242 case 3:
243 case 18:
244 case 19:
245 return r->data.nt5.server_type;
246 case 4:
247 case 5:
248 case 6:
249 case 7:
250 return r->data.nt5_ex.server_type;
251 case 8:
252 case 9:
253 case 10:
254 case 11:
255 case 12:
256 case 13:
257 case 14:
258 case 15:
259 return r->data.nt5_ex.server_type;
260 case 20:
261 case 21:
262 case 22:
263 case 23:
264 case 24:
265 case 25:
266 case 26:
267 case 27:
268 case 28:
269 return r->data.nt5_ex.server_type;
270 case 29:
271 case 30:
272 case 31:
273 return r->data.nt5_ex.server_type;
274 default:
275 return 0;
279 /****************************************************************
280 ****************************************************************/
282 #define RETURN_ON_FALSE(x) if (!(x)) return false;
284 static bool check_cldap_reply_required_flags(uint32_t ret_flags,
285 uint32_t req_flags)
287 if (ret_flags == 0) {
288 return true;
291 if (req_flags & DS_PDC_REQUIRED)
292 RETURN_ON_FALSE(ret_flags & NBT_SERVER_PDC);
294 if (req_flags & DS_GC_SERVER_REQUIRED)
295 RETURN_ON_FALSE(ret_flags & NBT_SERVER_GC);
297 if (req_flags & DS_ONLY_LDAP_NEEDED)
298 RETURN_ON_FALSE(ret_flags & NBT_SERVER_LDAP);
300 if ((req_flags & DS_DIRECTORY_SERVICE_REQUIRED) ||
301 (req_flags & DS_DIRECTORY_SERVICE_PREFERRED))
302 RETURN_ON_FALSE(ret_flags & NBT_SERVER_DS);
304 if (req_flags & DS_KDC_REQUIRED)
305 RETURN_ON_FALSE(ret_flags & NBT_SERVER_KDC);
307 if (req_flags & DS_TIMESERV_REQUIRED)
308 RETURN_ON_FALSE(ret_flags & NBT_SERVER_TIMESERV);
310 if (req_flags & DS_WRITABLE_REQUIRED)
311 RETURN_ON_FALSE(ret_flags & NBT_SERVER_WRITABLE);
313 return true;
316 /****************************************************************
317 ****************************************************************/
319 static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
320 const char *domain_name,
321 const struct GUID *domain_guid,
322 uint32_t flags,
323 struct netr_DsRGetDCNameInfo **info_p)
325 char *key;
326 DATA_BLOB blob;
327 enum ndr_err_code ndr_err;
328 struct netr_DsRGetDCNameInfo *info;
329 struct NETLOGON_SAM_LOGON_RESPONSE_EX r;
330 NTSTATUS status;
332 key = dsgetdcname_cache_key(mem_ctx, domain_name);
333 if (!key) {
334 return NT_STATUS_NO_MEMORY;
337 if (!gencache_get_data_blob(key, NULL, &blob, NULL, NULL)) {
338 return NT_STATUS_NOT_FOUND;
341 info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
342 if (!info) {
343 data_blob_free(&blob);
344 return NT_STATUS_NO_MEMORY;
347 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
348 (ndr_pull_flags_fn_t)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX);
350 data_blob_free(&blob);
351 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
352 dsgetdcname_cache_delete(mem_ctx, domain_name);
353 return ndr_map_error2ntstatus(ndr_err);
356 status = make_dc_info_from_cldap_reply(mem_ctx, flags, NULL,
357 &r, &info);
358 if (!NT_STATUS_IS_OK(status)) {
359 return status;
362 if (DEBUGLEVEL >= 10) {
363 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, info);
366 /* check flags */
367 if (!check_cldap_reply_required_flags(info->dc_flags, flags)) {
368 DEBUG(10,("invalid flags\n"));
369 return NT_STATUS_INVALID_PARAMETER;
372 if ((flags & DS_IP_REQUIRED) &&
373 (info->dc_address_type != DS_ADDRESS_TYPE_INET)) {
374 return NT_STATUS_INVALID_PARAMETER_MIX;
377 *info_p = info;
379 return NT_STATUS_OK;
382 /****************************************************************
383 ****************************************************************/
385 static NTSTATUS dsgetdcname_cached(TALLOC_CTX *mem_ctx,
386 struct messaging_context *msg_ctx,
387 const char *domain_name,
388 const struct GUID *domain_guid,
389 uint32_t flags,
390 const char *site_name,
391 struct netr_DsRGetDCNameInfo **info)
393 NTSTATUS status;
395 status = dsgetdcname_cache_fetch(mem_ctx, domain_name, domain_guid,
396 flags, info);
397 if (!NT_STATUS_IS_OK(status)
398 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
399 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
400 nt_errstr(status)));
401 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
404 if (flags & DS_BACKGROUND_ONLY) {
405 return status;
408 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
409 struct netr_DsRGetDCNameInfo *dc_info;
411 status = dsgetdcname(mem_ctx, msg_ctx, domain_name,
412 domain_guid, site_name,
413 flags | DS_FORCE_REDISCOVERY,
414 &dc_info);
416 if (!NT_STATUS_IS_OK(status)) {
417 return status;
420 *info = dc_info;
423 return status;
426 /****************************************************************
427 ****************************************************************/
429 static bool check_allowed_required_flags(uint32_t flags,
430 const char *site_name)
432 uint32_t return_type = flags & (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME);
433 uint32_t offered_type = flags & (DS_IS_FLAT_NAME|DS_IS_DNS_NAME);
434 uint32_t query_type = flags & (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY);
436 /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
437 * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
439 debug_dsdcinfo_flags(10, flags);
441 if ((flags & DS_TRY_NEXTCLOSEST_SITE) && site_name) {
442 return false;
445 if (return_type == (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME)) {
446 return false;
449 if (offered_type == (DS_IS_DNS_NAME|DS_IS_FLAT_NAME)) {
450 return false;
453 if (query_type == (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY)) {
454 return false;
457 #if 0
458 if ((flags & DS_RETURN_DNS_NAME) && (!(flags & DS_IP_REQUIRED))) {
459 printf("gd: here5 \n");
460 return false;
462 #endif
463 return true;
466 /****************************************************************
467 ****************************************************************/
469 static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
470 const char *domain_name,
471 uint32_t flags,
472 struct ip_service_name **returned_dclist,
473 int *returned_count)
475 NTSTATUS status;
476 enum nbt_name_type name_type = NBT_NAME_LOGON;
477 struct ip_service *iplist;
478 int i;
479 struct ip_service_name *dclist = NULL;
480 int count;
481 static const char *resolve_order[] = { "lmhosts", "wins", "bcast", NULL };
483 *returned_dclist = NULL;
484 *returned_count = 0;
486 if (lp_disable_netbios()) {
487 return NT_STATUS_NOT_SUPPORTED;
490 if (flags & DS_PDC_REQUIRED) {
491 name_type = NBT_NAME_PDC;
494 status = internal_resolve_name(domain_name, name_type, NULL,
495 &iplist, &count,
496 resolve_order);
497 if (!NT_STATUS_IS_OK(status)) {
498 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
499 return status;
502 dclist = talloc_zero_array(mem_ctx, struct ip_service_name, count);
503 if (!dclist) {
504 SAFE_FREE(iplist);
505 return NT_STATUS_NO_MEMORY;
508 for (i=0; i<count; i++) {
510 char addr[INET6_ADDRSTRLEN];
511 struct ip_service_name *r = &dclist[i];
513 print_sockaddr(addr, sizeof(addr),
514 &iplist[i].ss);
516 r->ss = iplist[i].ss;
517 r->hostname = talloc_strdup(mem_ctx, addr);
518 if (!r->hostname) {
519 SAFE_FREE(iplist);
520 return NT_STATUS_NO_MEMORY;
525 *returned_dclist = dclist;
526 *returned_count = count;
527 SAFE_FREE(iplist);
529 return NT_STATUS_OK;
532 /****************************************************************
533 ****************************************************************/
535 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
536 const char *domain_name,
537 const struct GUID *domain_guid,
538 uint32_t flags,
539 const char *site_name,
540 struct ip_service_name **returned_dclist,
541 int *return_count)
543 int i, j;
544 NTSTATUS status;
545 struct dns_rr_srv *dcs = NULL;
546 int numdcs = 0;
547 int numaddrs = 0;
548 struct ip_service_name *dclist = NULL;
549 int count = 0;
550 const char *dns_hosts_file;
551 char *guid_string;
553 dns_hosts_file = lp_parm_const_string(-1, "resolv", "host file", NULL);
554 if (flags & DS_PDC_REQUIRED) {
555 status = ads_dns_query_pdc(mem_ctx, dns_hosts_file,
556 domain_name, &dcs, &numdcs);
557 } else if (flags & DS_GC_SERVER_REQUIRED) {
558 status = ads_dns_query_gcs(mem_ctx, dns_hosts_file,
559 domain_name, site_name,
560 &dcs, &numdcs);
561 } else if (flags & DS_KDC_REQUIRED) {
562 status = ads_dns_query_kdcs(mem_ctx, dns_hosts_file,
563 domain_name, site_name,
564 &dcs, &numdcs);
565 } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
566 status = ads_dns_query_dcs(mem_ctx, dns_hosts_file,
567 domain_name, site_name,
568 &dcs, &numdcs);
569 } else if (domain_guid) {
570 guid_string = GUID_string(mem_ctx, domain_guid);
571 if (!guid_string) {
572 return NT_STATUS_NO_MEMORY;
575 status = ads_dns_query_dcs_guid(mem_ctx, dns_hosts_file,
576 domain_name, guid_string,
577 &dcs, &numdcs);
578 TALLOC_FREE(guid_string);
579 } else {
580 status = ads_dns_query_dcs(mem_ctx, dns_hosts_file,
581 domain_name, site_name,
582 &dcs, &numdcs);
585 if (!NT_STATUS_IS_OK(status)) {
586 return status;
589 if (numdcs == 0) {
590 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
593 for (i=0;i<numdcs;i++) {
594 numaddrs += MAX(dcs[i].num_ips,1);
597 dclist = talloc_zero_array(mem_ctx,
598 struct ip_service_name,
599 numaddrs);
600 if (!dclist) {
601 return NT_STATUS_NO_MEMORY;
604 /* now unroll the list of IP addresses */
606 *return_count = 0;
607 i = 0;
608 j = 0;
610 while ((i < numdcs) && (count < numaddrs)) {
612 struct ip_service_name *r = &dclist[count];
614 r->hostname = dcs[i].hostname;
616 /* If we don't have an IP list for a name, lookup it up */
618 if (!dcs[i].ss_s) {
619 interpret_string_addr_prefer_ipv4(&r->ss,
620 dcs[i].hostname, 0);
621 i++;
622 j = 0;
623 } else {
624 /* use the IP addresses from the SRV response */
626 if (j >= dcs[i].num_ips) {
627 i++;
628 j = 0;
629 continue;
632 r->ss = dcs[i].ss_s[j];
633 j++;
636 /* make sure it is a valid IP. I considered checking the
637 * negative connection cache, but this is the wrong place for
638 * it. Maybe only as a hack. After think about it, if all of
639 * the IP addresses returned from DNS are dead, what hope does a
640 * netbios name lookup have? The standard reason for falling
641 * back to netbios lookups is that our DNS server doesn't know
642 * anything about the DC's -- jerry */
644 if (!is_zero_addr(&r->ss)) {
645 count++;
646 continue;
650 *returned_dclist = dclist;
651 *return_count = count;
653 if (count > 0) {
654 return NT_STATUS_OK;
657 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
660 /****************************************************************
661 ****************************************************************/
663 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
664 const char *dc_unc,
665 const char *dc_address,
666 uint32_t dc_address_type,
667 const struct GUID *domain_guid,
668 const char *domain_name,
669 const char *forest_name,
670 uint32_t flags,
671 const char *dc_site_name,
672 const char *client_site_name,
673 struct netr_DsRGetDCNameInfo **info_out)
675 struct netr_DsRGetDCNameInfo *info;
677 info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
678 NT_STATUS_HAVE_NO_MEMORY(info);
680 if (dc_unc) {
681 info->dc_unc = talloc_strdup(mem_ctx, dc_unc);
682 NT_STATUS_HAVE_NO_MEMORY(info->dc_unc);
685 if (dc_address) {
686 if (!(dc_address[0] == '\\' && dc_address[1] == '\\')) {
687 info->dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
688 dc_address);
689 } else {
690 info->dc_address = talloc_strdup(mem_ctx, dc_address);
692 NT_STATUS_HAVE_NO_MEMORY(info->dc_address);
695 info->dc_address_type = dc_address_type;
697 if (domain_guid) {
698 info->domain_guid = *domain_guid;
701 if (domain_name) {
702 info->domain_name = talloc_strdup(mem_ctx, domain_name);
703 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
706 if (forest_name && *forest_name) {
707 info->forest_name = talloc_strdup(mem_ctx, forest_name);
708 NT_STATUS_HAVE_NO_MEMORY(info->forest_name);
709 flags |= DS_DNS_FOREST_ROOT;
712 info->dc_flags = flags;
714 if (dc_site_name) {
715 info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name);
716 NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name);
719 if (client_site_name) {
720 info->client_site_name = talloc_strdup(mem_ctx,
721 client_site_name);
722 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
725 *info_out = info;
727 return NT_STATUS_OK;
730 /****************************************************************
731 ****************************************************************/
733 static void map_dc_and_domain_names(uint32_t flags,
734 const char *dc_name,
735 const char *domain_name,
736 const char *dns_dc_name,
737 const char *dns_domain_name,
738 uint32_t *dc_flags,
739 const char **hostname_p,
740 const char **domain_p)
742 switch (flags & 0xf0000000) {
743 case DS_RETURN_FLAT_NAME:
744 if (dc_name && domain_name &&
745 *dc_name && *domain_name) {
746 *hostname_p = dc_name;
747 *domain_p = domain_name;
748 break;
750 case DS_RETURN_DNS_NAME:
751 default:
752 if (dns_dc_name && dns_domain_name &&
753 *dns_dc_name && *dns_domain_name) {
754 *hostname_p = dns_dc_name;
755 *domain_p = dns_domain_name;
756 *dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
757 break;
759 if (dc_name && domain_name &&
760 *dc_name && *domain_name) {
761 *hostname_p = dc_name;
762 *domain_p = domain_name;
763 break;
768 /****************************************************************
769 ****************************************************************/
771 static NTSTATUS make_dc_info_from_cldap_reply(TALLOC_CTX *mem_ctx,
772 uint32_t flags,
773 struct sockaddr_storage *ss,
774 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
775 struct netr_DsRGetDCNameInfo **info)
777 const char *dc_hostname = NULL;
778 const char *dc_domain_name = NULL;
779 const char *dc_address = NULL;
780 const char *dc_forest = NULL;
781 uint32_t dc_address_type = 0;
782 uint32_t dc_flags = 0;
783 struct GUID *dc_domain_guid = NULL;
784 const char *dc_server_site = NULL;
785 const char *dc_client_site = NULL;
787 char addr[INET6_ADDRSTRLEN];
789 if (ss) {
790 print_sockaddr(addr, sizeof(addr), ss);
791 dc_address = addr;
792 dc_address_type = DS_ADDRESS_TYPE_INET;
795 if (!ss && r->sockaddr.pdc_ip) {
796 dc_address = r->sockaddr.pdc_ip;
797 dc_address_type = DS_ADDRESS_TYPE_INET;
798 } else {
799 dc_address = r->pdc_name;
800 dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
803 map_dc_and_domain_names(flags,
804 r->pdc_name,
805 r->domain_name,
806 r->pdc_dns_name,
807 r->dns_domain,
808 &dc_flags,
809 &dc_hostname,
810 &dc_domain_name);
812 dc_flags |= r->server_type;
813 dc_forest = r->forest;
814 dc_domain_guid = &r->domain_uuid;
815 dc_server_site = r->server_site;
816 dc_client_site = r->client_site;
818 return make_domain_controller_info(mem_ctx,
819 dc_hostname,
820 dc_address,
821 dc_address_type,
822 dc_domain_guid,
823 dc_domain_name,
824 dc_forest,
825 dc_flags,
826 dc_server_site,
827 dc_client_site,
828 info);
831 /****************************************************************
832 ****************************************************************/
834 static uint32_t map_ds_flags_to_nt_version(uint32_t flags)
836 uint32_t nt_version = 0;
838 if (flags & DS_PDC_REQUIRED) {
839 nt_version |= NETLOGON_NT_VERSION_PDC;
842 if (flags & DS_GC_SERVER_REQUIRED) {
843 nt_version |= NETLOGON_NT_VERSION_GC;
846 if (flags & DS_TRY_NEXTCLOSEST_SITE) {
847 nt_version |= NETLOGON_NT_VERSION_WITH_CLOSEST_SITE;
850 if (flags & DS_IP_REQUIRED) {
851 nt_version |= NETLOGON_NT_VERSION_IP;
854 return nt_version;
857 /****************************************************************
858 ****************************************************************/
860 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
861 const char *domain_name,
862 uint32_t flags,
863 struct ip_service_name *dclist,
864 int num_dcs,
865 struct netr_DsRGetDCNameInfo **info)
867 int i = 0;
868 bool valid_dc = false;
869 struct netlogon_samlogon_response *r = NULL;
870 uint32_t nt_version = NETLOGON_NT_VERSION_5 |
871 NETLOGON_NT_VERSION_5EX;
872 uint32_t ret_flags = 0;
873 NTSTATUS status;
875 nt_version |= map_ds_flags_to_nt_version(flags);
877 for (i=0; i<num_dcs; i++) {
879 char addr[INET6_ADDRSTRLEN];
880 print_sockaddr(addr, sizeof(addr), &dclist[i].ss);
882 DEBUG(10,("LDAP ping to %s (%s)\n", dclist[i].hostname, addr));
884 if (ads_cldap_netlogon(mem_ctx, &dclist[i].ss,
885 domain_name,
886 nt_version,
887 &r))
889 nt_version = r->ntver;
890 ret_flags = get_cldap_reply_server_flags(r, nt_version);
892 if (check_cldap_reply_required_flags(ret_flags, flags)) {
893 valid_dc = true;
894 break;
898 continue;
901 if (!valid_dc) {
902 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
905 status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].ss,
906 &r->data.nt5_ex, info);
907 if (NT_STATUS_IS_OK(status)) {
908 return store_cldap_reply(mem_ctx, flags, &dclist[i].ss,
909 nt_version, &r->data.nt5_ex);
912 return status;
915 /****************************************************************
916 ****************************************************************/
918 /****************************************************************
919 ****************************************************************/
921 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
922 struct messaging_context *msg_ctx,
923 const char *domain_name,
924 uint32_t flags,
925 struct ip_service_name *dclist,
926 int num_dcs,
927 struct netr_DsRGetDCNameInfo **info)
929 struct sockaddr_storage ss;
930 struct ip_service ip_list;
931 enum nbt_name_type name_type = NBT_NAME_LOGON;
932 NTSTATUS status;
933 int i;
934 const char *dc_name = NULL;
935 fstring tmp_dc_name;
936 struct netlogon_samlogon_response *r = NULL;
937 bool store_cache = false;
938 uint32_t nt_version = NETLOGON_NT_VERSION_1 |
939 NETLOGON_NT_VERSION_5 |
940 NETLOGON_NT_VERSION_5EX_WITH_IP;
942 if (msg_ctx == NULL) {
943 return NT_STATUS_INVALID_PARAMETER;
946 if (flags & DS_PDC_REQUIRED) {
947 name_type = NBT_NAME_PDC;
950 nt_version |= map_ds_flags_to_nt_version(flags);
952 DEBUG(10,("process_dc_netbios\n"));
954 for (i=0; i<num_dcs; i++) {
955 uint16_t val;
957 generate_random_buffer((uint8_t *)&val, 2);
959 ip_list.ss = dclist[i].ss;
960 ip_list.port = 0;
962 if (!interpret_string_addr_prefer_ipv4(&ss, dclist[i].hostname, AI_NUMERICHOST)) {
963 return NT_STATUS_UNSUCCESSFUL;
966 status = nbt_getdc(msg_ctx, 10, &dclist[i].ss, domain_name,
967 NULL, nt_version,
968 mem_ctx, &nt_version, &dc_name, &r);
969 if (NT_STATUS_IS_OK(status)) {
970 store_cache = true;
971 namecache_store(dc_name, NBT_NAME_SERVER, 1, &ip_list);
972 goto make_reply;
975 if (name_status_find(domain_name,
976 name_type,
977 NBT_NAME_SERVER,
978 &dclist[i].ss,
979 tmp_dc_name))
981 struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1;
983 r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
984 NT_STATUS_HAVE_NO_MEMORY(r);
986 ZERO_STRUCT(logon1);
988 nt_version = NETLOGON_NT_VERSION_1;
990 logon1.nt_version = nt_version;
991 logon1.pdc_name = tmp_dc_name;
992 logon1.domain_name = talloc_strdup_upper(mem_ctx, domain_name);
993 NT_STATUS_HAVE_NO_MEMORY(logon1.domain_name);
995 r->data.nt4 = logon1;
996 r->ntver = nt_version;
998 map_netlogon_samlogon_response(r);
1000 namecache_store(tmp_dc_name, NBT_NAME_SERVER, 1, &ip_list);
1002 goto make_reply;
1006 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1008 make_reply:
1010 status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].ss,
1011 &r->data.nt5_ex, info);
1012 if (NT_STATUS_IS_OK(status) && store_cache) {
1013 return store_cldap_reply(mem_ctx, flags, &dclist[i].ss,
1014 nt_version, &r->data.nt5_ex);
1017 return status;
1020 /****************************************************************
1021 ****************************************************************/
1023 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
1024 struct messaging_context *msg_ctx,
1025 const char *domain_name,
1026 const struct GUID *domain_guid,
1027 uint32_t flags,
1028 const char *site_name,
1029 struct netr_DsRGetDCNameInfo **info)
1031 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1032 struct ip_service_name *dclist = NULL;
1033 int num_dcs;
1035 DEBUG(10,("dsgetdcname_rediscover\n"));
1037 if (flags & DS_IS_FLAT_NAME) {
1039 status = discover_dc_netbios(mem_ctx, domain_name, flags,
1040 &dclist, &num_dcs);
1041 NT_STATUS_NOT_OK_RETURN(status);
1043 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags,
1044 dclist, num_dcs, info);
1047 if (flags & DS_IS_DNS_NAME) {
1049 status = discover_dc_dns(mem_ctx, domain_name, domain_guid,
1050 flags, site_name, &dclist, &num_dcs);
1051 NT_STATUS_NOT_OK_RETURN(status);
1053 return process_dc_dns(mem_ctx, domain_name, flags,
1054 dclist, num_dcs, info);
1057 status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags,
1058 site_name, &dclist, &num_dcs);
1060 if (NT_STATUS_IS_OK(status) && num_dcs != 0) {
1062 status = process_dc_dns(mem_ctx, domain_name, flags, dclist,
1063 num_dcs, info);
1064 if (NT_STATUS_IS_OK(status)) {
1065 return status;
1069 status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
1070 &num_dcs);
1071 NT_STATUS_NOT_OK_RETURN(status);
1073 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags, dclist,
1074 num_dcs, info);
1077 static bool is_closest_site(struct netr_DsRGetDCNameInfo *info)
1079 if (info->dc_flags & DS_SERVER_CLOSEST) {
1080 return true;
1083 if (!info->client_site_name) {
1084 return true;
1087 if (!info->dc_site_name) {
1088 return false;
1091 if (strcmp(info->client_site_name, info->dc_site_name) == 0) {
1092 return true;
1095 return false;
1098 /********************************************************************
1099 Internal dsgetdcname.
1100 ********************************************************************/
1102 static NTSTATUS dsgetdcname_internal(TALLOC_CTX *mem_ctx,
1103 struct messaging_context *msg_ctx,
1104 const char *domain_name,
1105 const struct GUID *domain_guid,
1106 const char *site_name,
1107 uint32_t flags,
1108 struct netr_DsRGetDCNameInfo **info)
1110 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1111 struct netr_DsRGetDCNameInfo *myinfo = NULL;
1112 bool first = true;
1113 struct netr_DsRGetDCNameInfo *first_info = NULL;
1115 DEBUG(10,("dsgetdcname_internal: domain_name: %s, "
1116 "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
1117 domain_name,
1118 domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)",
1119 site_name ? site_name : "(null)", flags));
1121 *info = NULL;
1123 if (!check_allowed_required_flags(flags, site_name)) {
1124 DEBUG(0,("invalid flags specified\n"));
1125 return NT_STATUS_INVALID_PARAMETER;
1128 if (flags & DS_FORCE_REDISCOVERY) {
1129 goto rediscover;
1132 status = dsgetdcname_cached(mem_ctx, msg_ctx, domain_name, domain_guid,
1133 flags, site_name, &myinfo);
1134 if (NT_STATUS_IS_OK(status)) {
1135 goto done;
1138 if (flags & DS_BACKGROUND_ONLY) {
1139 goto done;
1142 rediscover:
1143 status = dsgetdcname_rediscover(mem_ctx, msg_ctx, domain_name,
1144 domain_guid, flags, site_name,
1145 &myinfo);
1147 done:
1148 if (!NT_STATUS_IS_OK(status)) {
1149 if (!first) {
1150 *info = first_info;
1151 return NT_STATUS_OK;
1153 return status;
1156 if (!first) {
1157 TALLOC_FREE(first_info);
1158 } else if (!is_closest_site(myinfo)) {
1159 first = false;
1160 first_info = myinfo;
1161 /* TODO: may use the next_closest_site here */
1162 site_name = myinfo->client_site_name;
1163 goto rediscover;
1166 *info = myinfo;
1167 return NT_STATUS_OK;
1170 /********************************************************************
1171 dsgetdcname.
1173 This will be the only public function here.
1174 ********************************************************************/
1176 NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
1177 struct messaging_context *msg_ctx,
1178 const char *domain_name,
1179 const struct GUID *domain_guid,
1180 const char *site_name,
1181 uint32_t flags,
1182 struct netr_DsRGetDCNameInfo **info)
1184 NTSTATUS status;
1185 const char *query_site = NULL;
1186 char *ptr_to_free = NULL;
1187 bool retry_query_with_null = false;
1189 if ((site_name == NULL) || (site_name[0] == '\0')) {
1190 ptr_to_free = sitename_fetch(mem_ctx, domain_name);
1191 if (ptr_to_free != NULL) {
1192 retry_query_with_null = true;
1194 query_site = ptr_to_free;
1195 } else {
1196 query_site = site_name;
1199 status = dsgetdcname_internal(mem_ctx,
1200 msg_ctx,
1201 domain_name,
1202 domain_guid,
1203 query_site,
1204 flags,
1205 info);
1207 TALLOC_FREE(ptr_to_free);
1209 if (!NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1210 return status;
1213 /* Should we try again with site_name == NULL ? */
1214 if (retry_query_with_null) {
1215 status = dsgetdcname_internal(mem_ctx,
1216 msg_ctx,
1217 domain_name,
1218 domain_guid,
1219 NULL,
1220 flags,
1221 info);
1224 return status;