dsgetdcname_cache_fetch() doesn't use the site_name parameter so don't pass it.
[Samba/wip.git] / source3 / libsmb / dsgetdcname.c
blob326f6f77cc7e2717c02e21ff8a06a561f59bcd41
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, &blob, NULL, NULL)) {
338 return NT_STATUS_NOT_FOUND;
341 info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
342 if (!info) {
343 return NT_STATUS_NO_MEMORY;
346 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
347 (ndr_pull_flags_fn_t)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX);
349 data_blob_free(&blob);
350 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
351 dsgetdcname_cache_delete(mem_ctx, domain_name);
352 return ndr_map_error2ntstatus(ndr_err);
355 status = make_dc_info_from_cldap_reply(mem_ctx, flags, NULL,
356 &r, &info);
357 if (!NT_STATUS_IS_OK(status)) {
358 return status;
361 if (DEBUGLEVEL >= 10) {
362 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, info);
365 /* check flags */
366 if (!check_cldap_reply_required_flags(info->dc_flags, flags)) {
367 DEBUG(10,("invalid flags\n"));
368 return NT_STATUS_INVALID_PARAMETER;
371 if ((flags & DS_IP_REQUIRED) &&
372 (info->dc_address_type != DS_ADDRESS_TYPE_INET)) {
373 return NT_STATUS_INVALID_PARAMETER_MIX;
376 *info_p = info;
378 return NT_STATUS_OK;
381 /****************************************************************
382 ****************************************************************/
384 static NTSTATUS dsgetdcname_cached(TALLOC_CTX *mem_ctx,
385 struct messaging_context *msg_ctx,
386 const char *domain_name,
387 const struct GUID *domain_guid,
388 uint32_t flags,
389 const char *site_name,
390 struct netr_DsRGetDCNameInfo **info)
392 NTSTATUS status;
394 status = dsgetdcname_cache_fetch(mem_ctx, domain_name, domain_guid,
395 flags, info);
396 if (!NT_STATUS_IS_OK(status)
397 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
398 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
399 nt_errstr(status)));
400 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
403 if (flags & DS_BACKGROUND_ONLY) {
404 return status;
407 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
408 struct netr_DsRGetDCNameInfo *dc_info;
410 status = dsgetdcname(mem_ctx, msg_ctx, domain_name,
411 domain_guid, site_name,
412 flags | DS_FORCE_REDISCOVERY,
413 &dc_info);
415 if (!NT_STATUS_IS_OK(status)) {
416 return status;
419 *info = dc_info;
422 return status;
425 /****************************************************************
426 ****************************************************************/
428 static bool check_allowed_required_flags(uint32_t flags,
429 const char *site_name)
431 uint32_t return_type = flags & (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME);
432 uint32_t offered_type = flags & (DS_IS_FLAT_NAME|DS_IS_DNS_NAME);
433 uint32_t query_type = flags & (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY);
435 /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
436 * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
438 debug_dsdcinfo_flags(10, flags);
440 if ((flags & DS_TRY_NEXTCLOSEST_SITE) && site_name) {
441 return false;
444 if (return_type == (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME)) {
445 return false;
448 if (offered_type == (DS_IS_DNS_NAME|DS_IS_FLAT_NAME)) {
449 return false;
452 if (query_type == (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY)) {
453 return false;
456 #if 0
457 if ((flags & DS_RETURN_DNS_NAME) && (!(flags & DS_IP_REQUIRED))) {
458 printf("gd: here5 \n");
459 return false;
461 #endif
462 return true;
465 /****************************************************************
466 ****************************************************************/
468 static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
469 const char *domain_name,
470 uint32_t flags,
471 struct ip_service_name **returned_dclist,
472 int *returned_count)
474 NTSTATUS status;
475 enum nbt_name_type name_type = NBT_NAME_LOGON;
476 struct ip_service *iplist;
477 int i;
478 struct ip_service_name *dclist = NULL;
479 int count;
480 static const char *resolve_order[] = { "lmhosts", "wins", "bcast", NULL };
482 *returned_dclist = NULL;
483 *returned_count = 0;
485 if (lp_disable_netbios()) {
486 return NT_STATUS_NOT_SUPPORTED;
489 if (flags & DS_PDC_REQUIRED) {
490 name_type = NBT_NAME_PDC;
493 status = internal_resolve_name(domain_name, name_type, NULL,
494 &iplist, &count,
495 resolve_order);
496 if (!NT_STATUS_IS_OK(status)) {
497 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
498 return status;
501 dclist = talloc_zero_array(mem_ctx, struct ip_service_name, count);
502 if (!dclist) {
503 SAFE_FREE(iplist);
504 return NT_STATUS_NO_MEMORY;
507 for (i=0; i<count; i++) {
509 char addr[INET6_ADDRSTRLEN];
510 struct ip_service_name *r = &dclist[i];
512 print_sockaddr(addr, sizeof(addr),
513 &iplist[i].ss);
515 r->ss = iplist[i].ss;
516 r->hostname = talloc_strdup(mem_ctx, addr);
517 if (!r->hostname) {
518 SAFE_FREE(iplist);
519 return NT_STATUS_NO_MEMORY;
524 *returned_dclist = dclist;
525 *returned_count = count;
526 SAFE_FREE(iplist);
528 return NT_STATUS_OK;
531 /****************************************************************
532 ****************************************************************/
534 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
535 const char *domain_name,
536 const struct GUID *domain_guid,
537 uint32_t flags,
538 const char *site_name,
539 struct ip_service_name **returned_dclist,
540 int *return_count)
542 int i, j;
543 NTSTATUS status;
544 struct dns_rr_srv *dcs = NULL;
545 int numdcs = 0;
546 int numaddrs = 0;
547 struct ip_service_name *dclist = NULL;
548 int count = 0;
549 const char *dns_hosts_file;
550 char *guid_string;
552 dns_hosts_file = lp_parm_const_string(-1, "resolv", "host file", NULL);
553 if (flags & DS_PDC_REQUIRED) {
554 status = ads_dns_query_pdc(mem_ctx, dns_hosts_file,
555 domain_name, &dcs, &numdcs);
556 } else if (flags & DS_GC_SERVER_REQUIRED) {
557 status = ads_dns_query_gcs(mem_ctx, dns_hosts_file,
558 domain_name, site_name,
559 &dcs, &numdcs);
560 } else if (flags & DS_KDC_REQUIRED) {
561 status = ads_dns_query_kdcs(mem_ctx, dns_hosts_file,
562 domain_name, site_name,
563 &dcs, &numdcs);
564 } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
565 status = ads_dns_query_dcs(mem_ctx, dns_hosts_file,
566 domain_name, site_name,
567 &dcs, &numdcs);
568 } else if (domain_guid) {
569 guid_string = GUID_string(mem_ctx, domain_guid);
570 if (!guid_string) {
571 return NT_STATUS_NO_MEMORY;
574 status = ads_dns_query_dcs_guid(mem_ctx, dns_hosts_file,
575 domain_name, guid_string,
576 &dcs, &numdcs);
577 TALLOC_FREE(guid_string);
578 } else {
579 status = ads_dns_query_dcs(mem_ctx, dns_hosts_file,
580 domain_name, site_name,
581 &dcs, &numdcs);
584 if (!NT_STATUS_IS_OK(status)) {
585 return status;
588 if (numdcs == 0) {
589 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
592 for (i=0;i<numdcs;i++) {
593 numaddrs += MAX(dcs[i].num_ips,1);
596 dclist = talloc_zero_array(mem_ctx,
597 struct ip_service_name,
598 numaddrs);
599 if (!dclist) {
600 return NT_STATUS_NO_MEMORY;
603 /* now unroll the list of IP addresses */
605 *return_count = 0;
606 i = 0;
607 j = 0;
609 while ((i < numdcs) && (count < numaddrs)) {
611 struct ip_service_name *r = &dclist[count];
613 r->hostname = dcs[i].hostname;
615 /* If we don't have an IP list for a name, lookup it up */
617 if (!dcs[i].ss_s) {
618 interpret_string_addr_prefer_ipv4(&r->ss,
619 dcs[i].hostname, 0);
620 i++;
621 j = 0;
622 } else {
623 /* use the IP addresses from the SRV response */
625 if (j >= dcs[i].num_ips) {
626 i++;
627 j = 0;
628 continue;
631 r->ss = dcs[i].ss_s[j];
632 j++;
635 /* make sure it is a valid IP. I considered checking the
636 * negative connection cache, but this is the wrong place for
637 * it. Maybe only as a hack. After think about it, if all of
638 * the IP addresses returned from DNS are dead, what hope does a
639 * netbios name lookup have? The standard reason for falling
640 * back to netbios lookups is that our DNS server doesn't know
641 * anything about the DC's -- jerry */
643 if (!is_zero_addr(&r->ss)) {
644 count++;
645 continue;
649 *returned_dclist = dclist;
650 *return_count = count;
652 if (count > 0) {
653 return NT_STATUS_OK;
656 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
659 /****************************************************************
660 ****************************************************************/
662 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
663 const char *dc_unc,
664 const char *dc_address,
665 uint32_t dc_address_type,
666 const struct GUID *domain_guid,
667 const char *domain_name,
668 const char *forest_name,
669 uint32_t flags,
670 const char *dc_site_name,
671 const char *client_site_name,
672 struct netr_DsRGetDCNameInfo **info_out)
674 struct netr_DsRGetDCNameInfo *info;
676 info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
677 NT_STATUS_HAVE_NO_MEMORY(info);
679 if (dc_unc) {
680 info->dc_unc = talloc_strdup(mem_ctx, dc_unc);
681 NT_STATUS_HAVE_NO_MEMORY(info->dc_unc);
684 if (dc_address) {
685 if (!(dc_address[0] == '\\' && dc_address[1] == '\\')) {
686 info->dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
687 dc_address);
688 } else {
689 info->dc_address = talloc_strdup(mem_ctx, dc_address);
691 NT_STATUS_HAVE_NO_MEMORY(info->dc_address);
694 info->dc_address_type = dc_address_type;
696 if (domain_guid) {
697 info->domain_guid = *domain_guid;
700 if (domain_name) {
701 info->domain_name = talloc_strdup(mem_ctx, domain_name);
702 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
705 if (forest_name && *forest_name) {
706 info->forest_name = talloc_strdup(mem_ctx, forest_name);
707 NT_STATUS_HAVE_NO_MEMORY(info->forest_name);
708 flags |= DS_DNS_FOREST_ROOT;
711 info->dc_flags = flags;
713 if (dc_site_name) {
714 info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name);
715 NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name);
718 if (client_site_name) {
719 info->client_site_name = talloc_strdup(mem_ctx,
720 client_site_name);
721 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
724 *info_out = info;
726 return NT_STATUS_OK;
729 /****************************************************************
730 ****************************************************************/
732 static void map_dc_and_domain_names(uint32_t flags,
733 const char *dc_name,
734 const char *domain_name,
735 const char *dns_dc_name,
736 const char *dns_domain_name,
737 uint32_t *dc_flags,
738 const char **hostname_p,
739 const char **domain_p)
741 switch (flags & 0xf0000000) {
742 case DS_RETURN_FLAT_NAME:
743 if (dc_name && domain_name &&
744 *dc_name && *domain_name) {
745 *hostname_p = dc_name;
746 *domain_p = domain_name;
747 break;
749 case DS_RETURN_DNS_NAME:
750 default:
751 if (dns_dc_name && dns_domain_name &&
752 *dns_dc_name && *dns_domain_name) {
753 *hostname_p = dns_dc_name;
754 *domain_p = dns_domain_name;
755 *dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
756 break;
758 if (dc_name && domain_name &&
759 *dc_name && *domain_name) {
760 *hostname_p = dc_name;
761 *domain_p = domain_name;
762 break;
767 /****************************************************************
768 ****************************************************************/
770 static NTSTATUS make_dc_info_from_cldap_reply(TALLOC_CTX *mem_ctx,
771 uint32_t flags,
772 struct sockaddr_storage *ss,
773 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
774 struct netr_DsRGetDCNameInfo **info)
776 const char *dc_hostname = NULL;
777 const char *dc_domain_name = NULL;
778 const char *dc_address = NULL;
779 const char *dc_forest = NULL;
780 uint32_t dc_address_type = 0;
781 uint32_t dc_flags = 0;
782 struct GUID *dc_domain_guid = NULL;
783 const char *dc_server_site = NULL;
784 const char *dc_client_site = NULL;
786 char addr[INET6_ADDRSTRLEN];
788 if (ss) {
789 print_sockaddr(addr, sizeof(addr), ss);
790 dc_address = addr;
791 dc_address_type = DS_ADDRESS_TYPE_INET;
794 if (!ss && r->sockaddr.pdc_ip) {
795 dc_address = r->sockaddr.pdc_ip;
796 dc_address_type = DS_ADDRESS_TYPE_INET;
797 } else {
798 dc_address = r->pdc_name;
799 dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
802 map_dc_and_domain_names(flags,
803 r->pdc_name,
804 r->domain_name,
805 r->pdc_dns_name,
806 r->dns_domain,
807 &dc_flags,
808 &dc_hostname,
809 &dc_domain_name);
811 dc_flags |= r->server_type;
812 dc_forest = r->forest;
813 dc_domain_guid = &r->domain_uuid;
814 dc_server_site = r->server_site;
815 dc_client_site = r->client_site;
817 return make_domain_controller_info(mem_ctx,
818 dc_hostname,
819 dc_address,
820 dc_address_type,
821 dc_domain_guid,
822 dc_domain_name,
823 dc_forest,
824 dc_flags,
825 dc_server_site,
826 dc_client_site,
827 info);
830 /****************************************************************
831 ****************************************************************/
833 static uint32_t map_ds_flags_to_nt_version(uint32_t flags)
835 uint32_t nt_version = 0;
837 if (flags & DS_PDC_REQUIRED) {
838 nt_version |= NETLOGON_NT_VERSION_PDC;
841 if (flags & DS_GC_SERVER_REQUIRED) {
842 nt_version |= NETLOGON_NT_VERSION_GC;
845 if (flags & DS_TRY_NEXTCLOSEST_SITE) {
846 nt_version |= NETLOGON_NT_VERSION_WITH_CLOSEST_SITE;
849 if (flags & DS_IP_REQUIRED) {
850 nt_version |= NETLOGON_NT_VERSION_IP;
853 return nt_version;
856 /****************************************************************
857 ****************************************************************/
859 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
860 const char *domain_name,
861 uint32_t flags,
862 struct ip_service_name *dclist,
863 int num_dcs,
864 struct netr_DsRGetDCNameInfo **info)
866 int i = 0;
867 bool valid_dc = false;
868 struct netlogon_samlogon_response *r = NULL;
869 uint32_t nt_version = NETLOGON_NT_VERSION_5 |
870 NETLOGON_NT_VERSION_5EX;
871 uint32_t ret_flags = 0;
872 NTSTATUS status;
874 nt_version |= map_ds_flags_to_nt_version(flags);
876 for (i=0; i<num_dcs; i++) {
879 DEBUG(10,("LDAP ping to %s\n", dclist[i].hostname));
881 if (ads_cldap_netlogon(mem_ctx, &dclist[i].ss,
882 domain_name,
883 nt_version,
884 &r))
886 nt_version = r->ntver;
887 ret_flags = get_cldap_reply_server_flags(r, nt_version);
889 if (check_cldap_reply_required_flags(ret_flags, flags)) {
890 valid_dc = true;
891 break;
895 continue;
898 if (!valid_dc) {
899 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
902 status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].ss,
903 &r->data.nt5_ex, info);
904 if (NT_STATUS_IS_OK(status)) {
905 return store_cldap_reply(mem_ctx, flags, &dclist[i].ss,
906 nt_version, &r->data.nt5_ex);
909 return status;
912 /****************************************************************
913 ****************************************************************/
915 /****************************************************************
916 ****************************************************************/
918 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
919 struct messaging_context *msg_ctx,
920 const char *domain_name,
921 uint32_t flags,
922 struct ip_service_name *dclist,
923 int num_dcs,
924 struct netr_DsRGetDCNameInfo **info)
926 struct sockaddr_storage ss;
927 struct ip_service ip_list;
928 enum nbt_name_type name_type = NBT_NAME_LOGON;
929 NTSTATUS status;
930 int i;
931 const char *dc_name = NULL;
932 fstring tmp_dc_name;
933 struct netlogon_samlogon_response *r = NULL;
934 bool store_cache = false;
935 uint32_t nt_version = NETLOGON_NT_VERSION_1 |
936 NETLOGON_NT_VERSION_5 |
937 NETLOGON_NT_VERSION_5EX_WITH_IP;
939 if (msg_ctx == NULL) {
940 return NT_STATUS_INVALID_PARAMETER;
943 if (flags & DS_PDC_REQUIRED) {
944 name_type = NBT_NAME_PDC;
947 nt_version |= map_ds_flags_to_nt_version(flags);
949 DEBUG(10,("process_dc_netbios\n"));
951 for (i=0; i<num_dcs; i++) {
952 uint16_t val;
954 generate_random_buffer((uint8_t *)&val, 2);
956 ip_list.ss = dclist[i].ss;
957 ip_list.port = 0;
959 if (!interpret_string_addr_prefer_ipv4(&ss, dclist[i].hostname, AI_NUMERICHOST)) {
960 return NT_STATUS_UNSUCCESSFUL;
963 status = nbt_getdc(msg_ctx, 10, &dclist[i].ss, domain_name,
964 NULL, nt_version,
965 mem_ctx, &nt_version, &dc_name, &r);
966 if (NT_STATUS_IS_OK(status)) {
967 store_cache = true;
968 namecache_store(dc_name, NBT_NAME_SERVER, 1, &ip_list);
969 goto make_reply;
972 if (name_status_find(domain_name,
973 name_type,
974 NBT_NAME_SERVER,
975 &dclist[i].ss,
976 tmp_dc_name))
978 struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1;
980 r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
981 NT_STATUS_HAVE_NO_MEMORY(r);
983 ZERO_STRUCT(logon1);
985 nt_version = NETLOGON_NT_VERSION_1;
987 logon1.nt_version = nt_version;
988 logon1.pdc_name = tmp_dc_name;
989 logon1.domain_name = talloc_strdup_upper(mem_ctx, domain_name);
990 NT_STATUS_HAVE_NO_MEMORY(logon1.domain_name);
992 r->data.nt4 = logon1;
993 r->ntver = nt_version;
995 map_netlogon_samlogon_response(r);
997 namecache_store(tmp_dc_name, NBT_NAME_SERVER, 1, &ip_list);
999 goto make_reply;
1003 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1005 make_reply:
1007 status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].ss,
1008 &r->data.nt5_ex, info);
1009 if (NT_STATUS_IS_OK(status) && store_cache) {
1010 return store_cldap_reply(mem_ctx, flags, &dclist[i].ss,
1011 nt_version, &r->data.nt5_ex);
1014 return status;
1017 /****************************************************************
1018 ****************************************************************/
1020 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
1021 struct messaging_context *msg_ctx,
1022 const char *domain_name,
1023 const struct GUID *domain_guid,
1024 uint32_t flags,
1025 const char *site_name,
1026 struct netr_DsRGetDCNameInfo **info)
1028 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1029 struct ip_service_name *dclist = NULL;
1030 int num_dcs;
1032 DEBUG(10,("dsgetdcname_rediscover\n"));
1034 if (flags & DS_IS_FLAT_NAME) {
1036 status = discover_dc_netbios(mem_ctx, domain_name, flags,
1037 &dclist, &num_dcs);
1038 NT_STATUS_NOT_OK_RETURN(status);
1040 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags,
1041 dclist, num_dcs, info);
1044 if (flags & DS_IS_DNS_NAME) {
1046 status = discover_dc_dns(mem_ctx, domain_name, domain_guid,
1047 flags, site_name, &dclist, &num_dcs);
1048 NT_STATUS_NOT_OK_RETURN(status);
1050 return process_dc_dns(mem_ctx, domain_name, flags,
1051 dclist, num_dcs, info);
1054 status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags,
1055 site_name, &dclist, &num_dcs);
1057 if (NT_STATUS_IS_OK(status) && num_dcs != 0) {
1059 status = process_dc_dns(mem_ctx, domain_name, flags, dclist,
1060 num_dcs, info);
1061 if (NT_STATUS_IS_OK(status)) {
1062 return status;
1066 status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
1067 &num_dcs);
1068 NT_STATUS_NOT_OK_RETURN(status);
1070 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags, dclist,
1071 num_dcs, info);
1074 static bool is_closest_site(struct netr_DsRGetDCNameInfo *info)
1076 if (info->dc_flags & DS_SERVER_CLOSEST) {
1077 return true;
1080 if (!info->client_site_name) {
1081 return true;
1084 if (!info->dc_site_name) {
1085 return false;
1088 if (strcmp(info->client_site_name, info->dc_site_name) == 0) {
1089 return true;
1092 return false;
1095 /********************************************************************
1096 dsgetdcname.
1098 This will be the only public function here.
1099 ********************************************************************/
1101 NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
1102 struct messaging_context *msg_ctx,
1103 const char *domain_name,
1104 const struct GUID *domain_guid,
1105 const char *site_name,
1106 uint32_t flags,
1107 struct netr_DsRGetDCNameInfo **info)
1109 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1110 struct netr_DsRGetDCNameInfo *myinfo = NULL;
1111 char *query_site = NULL;
1112 bool first = true;
1113 struct netr_DsRGetDCNameInfo *first_info = NULL;
1115 DEBUG(10,("dsgetdcname: 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, 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 ((site_name == NULL) || (site_name[0] == '\0')) {
1129 query_site = sitename_fetch(domain_name);
1130 } else {
1131 query_site = SMB_STRDUP(site_name);
1134 if (flags & DS_FORCE_REDISCOVERY) {
1135 goto rediscover;
1138 status = dsgetdcname_cached(mem_ctx, msg_ctx, domain_name, domain_guid,
1139 flags, query_site, &myinfo);
1140 if (NT_STATUS_IS_OK(status)) {
1141 goto done;
1144 if (flags & DS_BACKGROUND_ONLY) {
1145 goto done;
1148 rediscover:
1149 status = dsgetdcname_rediscover(mem_ctx, msg_ctx, domain_name,
1150 domain_guid, flags, query_site,
1151 &myinfo);
1153 done:
1154 SAFE_FREE(query_site);
1156 if (!NT_STATUS_IS_OK(status)) {
1157 if (!first) {
1158 *info = first_info;
1159 return NT_STATUS_OK;
1161 return status;
1164 if (!first) {
1165 TALLOC_FREE(first_info);
1166 } else if (!is_closest_site(myinfo)) {
1167 first = false;
1168 first_info = myinfo;
1169 /* TODO: may use the next_closest_site here */
1170 query_site = SMB_STRDUP(myinfo->client_site_name);
1171 goto rediscover;
1174 *info = myinfo;
1175 return NT_STATUS_OK;