s3: smbd: Add SMB2_FILE_POSIX_INFORMATION getinfo info level (100 on the wire).
[Samba.git] / source3 / libsmb / dsgetdcname.c
blob42714fcb2a116d8666a2cfd21cafd6a70cdeec1d
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 "libsmb/dsgetdcname.h"
25 #include "libsmb/namequery.h"
26 #include "libads/sitename_cache.h"
27 #include "../librpc/gen_ndr/ndr_netlogon.h"
28 #include "libads/cldap.h"
29 #include "lib/addns/dnsquery_srv.h"
30 #include "libsmb/clidgram.h"
31 #include "lib/gencache.h"
32 #include "lib/util/util_net.h"
34 /* 15 minutes */
35 #define DSGETDCNAME_CACHE_TTL 60*15
37 struct ip_service_name {
38 struct samba_sockaddr sa;
39 const char *hostname;
42 static NTSTATUS make_dc_info_from_cldap_reply(
43 TALLOC_CTX *mem_ctx,
44 uint32_t flags,
45 const struct samba_sockaddr *sa,
46 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
47 struct netr_DsRGetDCNameInfo **info);
49 /****************************************************************
50 ****************************************************************/
52 static void debug_dsdcinfo_flags(int lvl, uint32_t flags)
54 DEBUG(lvl,("debug_dsdcinfo_flags: 0x%08x\n\t", flags));
56 if (flags & DS_FORCE_REDISCOVERY)
57 DEBUGADD(lvl,("DS_FORCE_REDISCOVERY "));
58 if (flags & 0x000000002)
59 DEBUGADD(lvl,("0x00000002 "));
60 if (flags & 0x000000004)
61 DEBUGADD(lvl,("0x00000004 "));
62 if (flags & 0x000000008)
63 DEBUGADD(lvl,("0x00000008 "));
64 if (flags & DS_DIRECTORY_SERVICE_REQUIRED)
65 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_REQUIRED "));
66 if (flags & DS_DIRECTORY_SERVICE_PREFERRED)
67 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_PREFERRED "));
68 if (flags & DS_GC_SERVER_REQUIRED)
69 DEBUGADD(lvl,("DS_GC_SERVER_REQUIRED "));
70 if (flags & DS_PDC_REQUIRED)
71 DEBUGADD(lvl,("DS_PDC_REQUIRED "));
72 if (flags & DS_BACKGROUND_ONLY)
73 DEBUGADD(lvl,("DS_BACKGROUND_ONLY "));
74 if (flags & DS_IP_REQUIRED)
75 DEBUGADD(lvl,("DS_IP_REQUIRED "));
76 if (flags & DS_KDC_REQUIRED)
77 DEBUGADD(lvl,("DS_KDC_REQUIRED "));
78 if (flags & DS_TIMESERV_REQUIRED)
79 DEBUGADD(lvl,("DS_TIMESERV_REQUIRED "));
80 if (flags & DS_WRITABLE_REQUIRED)
81 DEBUGADD(lvl,("DS_WRITABLE_REQUIRED "));
82 if (flags & DS_GOOD_TIMESERV_PREFERRED)
83 DEBUGADD(lvl,("DS_GOOD_TIMESERV_PREFERRED "));
84 if (flags & DS_AVOID_SELF)
85 DEBUGADD(lvl,("DS_AVOID_SELF "));
86 if (flags & DS_ONLY_LDAP_NEEDED)
87 DEBUGADD(lvl,("DS_ONLY_LDAP_NEEDED "));
88 if (flags & DS_IS_FLAT_NAME)
89 DEBUGADD(lvl,("DS_IS_FLAT_NAME "));
90 if (flags & DS_IS_DNS_NAME)
91 DEBUGADD(lvl,("DS_IS_DNS_NAME "));
92 if (flags & DS_TRY_NEXTCLOSEST_SITE)
93 DEBUGADD(lvl,("DS_TRY_NEXTCLOSEST_SITE "));
94 if (flags & DS_DIRECTORY_SERVICE_6_REQUIRED)
95 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_6_REQUIRED "));
96 if (flags & DS_WEB_SERVICE_REQUIRED)
97 DEBUGADD(lvl,("DS_WEB_SERVICE_REQUIRED "));
98 if (flags & DS_DIRECTORY_SERVICE_8_REQUIRED)
99 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_8_REQUIRED "));
100 if (flags & DS_DIRECTORY_SERVICE_9_REQUIRED)
101 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_9_REQUIRED "));
102 if (flags & DS_DIRECTORY_SERVICE_10_REQUIRED)
103 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_10_REQUIRED "));
104 if (flags & 0x01000000)
105 DEBUGADD(lvl,("0x01000000 "));
106 if (flags & 0x02000000)
107 DEBUGADD(lvl,("0x02000000 "));
108 if (flags & 0x04000000)
109 DEBUGADD(lvl,("0x04000000 "));
110 if (flags & 0x08000000)
111 DEBUGADD(lvl,("0x08000000 "));
112 if (flags & 0x10000000)
113 DEBUGADD(lvl,("0x10000000 "));
114 if (flags & 0x20000000)
115 DEBUGADD(lvl,("0x20000000 "));
116 if (flags & DS_RETURN_DNS_NAME)
117 DEBUGADD(lvl,("DS_RETURN_DNS_NAME "));
118 if (flags & DS_RETURN_FLAT_NAME)
119 DEBUGADD(lvl,("DS_RETURN_FLAT_NAME "));
120 if (flags)
121 DEBUGADD(lvl,("\n"));
124 /****************************************************************
125 ****************************************************************/
127 static char *dsgetdcname_cache_key(TALLOC_CTX *mem_ctx, const char *domain)
129 if (!domain) {
130 return NULL;
133 return talloc_asprintf_strupper_m(mem_ctx, "DSGETDCNAME/DOMAIN/%s",
134 domain);
137 /****************************************************************
138 ****************************************************************/
140 static NTSTATUS dsgetdcname_cache_delete(TALLOC_CTX *mem_ctx,
141 const char *domain_name)
143 char *key;
145 key = dsgetdcname_cache_key(mem_ctx, domain_name);
146 if (!key) {
147 return NT_STATUS_NO_MEMORY;
150 if (!gencache_del(key)) {
151 return NT_STATUS_UNSUCCESSFUL;
154 return NT_STATUS_OK;
157 /****************************************************************
158 ****************************************************************/
160 static NTSTATUS dsgetdcname_cache_store(TALLOC_CTX *mem_ctx,
161 const char *domain_name,
162 DATA_BLOB blob)
164 time_t expire_time;
165 char *key;
166 bool ret = false;
168 key = dsgetdcname_cache_key(mem_ctx, domain_name);
169 if (!key) {
170 return NT_STATUS_NO_MEMORY;
173 expire_time = time(NULL) + DSGETDCNAME_CACHE_TTL;
175 ret = gencache_set_data_blob(key, blob, expire_time);
177 return ret ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
180 /****************************************************************
181 ****************************************************************/
183 static NTSTATUS store_cldap_reply(TALLOC_CTX *mem_ctx,
184 uint32_t flags,
185 struct samba_sockaddr *sa,
186 uint32_t nt_version,
187 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r)
189 DATA_BLOB blob;
190 enum ndr_err_code ndr_err;
191 NTSTATUS status;
192 char addr[INET6_ADDRSTRLEN];
194 print_sockaddr(addr, sizeof(addr), &sa->u.ss);
196 /* FIXME */
197 r->sockaddr_size = 0x10; /* the w32 winsock addr size */
198 r->sockaddr.sockaddr_family = 2; /* AF_INET */
199 r->sockaddr.pdc_ip = talloc_strdup(mem_ctx, addr);
201 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, r,
202 (ndr_push_flags_fn_t)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX);
203 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
204 return ndr_map_error2ntstatus(ndr_err);
207 if (r->domain_name) {
208 status = dsgetdcname_cache_store(mem_ctx, r->domain_name,
209 blob);
210 if (!NT_STATUS_IS_OK(status)) {
211 goto done;
213 if (r->client_site) {
214 sitename_store(r->domain_name, r->client_site);
217 if (r->dns_domain) {
218 status = dsgetdcname_cache_store(mem_ctx, r->dns_domain, blob);
219 if (!NT_STATUS_IS_OK(status)) {
220 goto done;
222 if (r->client_site) {
223 sitename_store(r->dns_domain, r->client_site);
227 status = NT_STATUS_OK;
229 done:
230 data_blob_free(&blob);
232 return status;
235 /****************************************************************
236 ****************************************************************/
238 static uint32_t get_cldap_reply_server_flags(struct netlogon_samlogon_response *r,
239 uint32_t nt_version)
241 switch (nt_version & 0x0000001f) {
242 case 0:
243 case 1:
244 case 16:
245 case 17:
246 return 0;
247 case 2:
248 case 3:
249 case 18:
250 case 19:
251 return r->data.nt5.server_type;
252 case 4:
253 case 5:
254 case 6:
255 case 7:
256 return r->data.nt5_ex.server_type;
257 case 8:
258 case 9:
259 case 10:
260 case 11:
261 case 12:
262 case 13:
263 case 14:
264 case 15:
265 return r->data.nt5_ex.server_type;
266 case 20:
267 case 21:
268 case 22:
269 case 23:
270 case 24:
271 case 25:
272 case 26:
273 case 27:
274 case 28:
275 return r->data.nt5_ex.server_type;
276 case 29:
277 case 30:
278 case 31:
279 return r->data.nt5_ex.server_type;
280 default:
281 return 0;
285 /****************************************************************
286 ****************************************************************/
288 static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
289 const char *domain_name,
290 const struct GUID *domain_guid,
291 uint32_t flags,
292 struct netr_DsRGetDCNameInfo **info_p)
294 char *key;
295 DATA_BLOB blob;
296 enum ndr_err_code ndr_err;
297 struct netr_DsRGetDCNameInfo *info;
298 struct NETLOGON_SAM_LOGON_RESPONSE_EX r;
299 NTSTATUS status;
301 key = dsgetdcname_cache_key(mem_ctx, domain_name);
302 if (!key) {
303 return NT_STATUS_NO_MEMORY;
306 if (!gencache_get_data_blob(key, NULL, &blob, NULL, NULL)) {
307 return NT_STATUS_NOT_FOUND;
310 info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
311 if (!info) {
312 data_blob_free(&blob);
313 return NT_STATUS_NO_MEMORY;
316 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
317 (ndr_pull_flags_fn_t)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX);
319 data_blob_free(&blob);
320 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
321 dsgetdcname_cache_delete(mem_ctx, domain_name);
322 return ndr_map_error2ntstatus(ndr_err);
325 status = make_dc_info_from_cldap_reply(mem_ctx, flags, NULL,
326 &r, &info);
327 if (!NT_STATUS_IS_OK(status)) {
328 return status;
331 if (DEBUGLEVEL >= 10) {
332 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, info);
335 /* check flags */
336 if (!check_cldap_reply_required_flags(info->dc_flags, flags)) {
337 DEBUG(10,("invalid flags\n"));
338 return NT_STATUS_INVALID_PARAMETER;
341 if ((flags & DS_IP_REQUIRED) &&
342 (info->dc_address_type != DS_ADDRESS_TYPE_INET)) {
343 return NT_STATUS_INVALID_PARAMETER_MIX;
346 *info_p = info;
348 return NT_STATUS_OK;
351 /****************************************************************
352 ****************************************************************/
354 static NTSTATUS dsgetdcname_cached(TALLOC_CTX *mem_ctx,
355 struct messaging_context *msg_ctx,
356 const char *domain_name,
357 const struct GUID *domain_guid,
358 uint32_t flags,
359 const char *site_name,
360 struct netr_DsRGetDCNameInfo **info)
362 NTSTATUS status;
364 status = dsgetdcname_cache_fetch(mem_ctx, domain_name, domain_guid,
365 flags, info);
366 if (!NT_STATUS_IS_OK(status)
367 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
368 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
369 nt_errstr(status)));
370 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
373 if (flags & DS_BACKGROUND_ONLY) {
374 return status;
377 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
378 struct netr_DsRGetDCNameInfo *dc_info;
380 status = dsgetdcname(mem_ctx, msg_ctx, domain_name,
381 domain_guid, site_name,
382 flags | DS_FORCE_REDISCOVERY,
383 &dc_info);
385 if (!NT_STATUS_IS_OK(status)) {
386 return status;
389 *info = dc_info;
392 return status;
395 /****************************************************************
396 ****************************************************************/
398 static bool check_allowed_required_flags(uint32_t flags,
399 const char *site_name)
401 uint32_t return_type = flags & (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME);
402 uint32_t offered_type = flags & (DS_IS_FLAT_NAME|DS_IS_DNS_NAME);
403 uint32_t query_type = flags & (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY);
405 /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
406 * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
408 debug_dsdcinfo_flags(10, flags);
410 if ((flags & DS_TRY_NEXTCLOSEST_SITE) && site_name) {
411 return false;
414 if (return_type == (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME)) {
415 return false;
418 if (offered_type == (DS_IS_DNS_NAME|DS_IS_FLAT_NAME)) {
419 return false;
422 if (query_type == (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY)) {
423 return false;
426 #if 0
427 if ((flags & DS_RETURN_DNS_NAME) && (!(flags & DS_IP_REQUIRED))) {
428 printf("gd: here5 \n");
429 return false;
431 #endif
432 return true;
435 /****************************************************************
436 ****************************************************************/
438 static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
439 const char *domain_name,
440 uint32_t flags,
441 struct ip_service_name **returned_dclist,
442 size_t *returned_count)
444 NTSTATUS status;
445 enum nbt_name_type name_type = NBT_NAME_LOGON;
446 struct samba_sockaddr *salist = NULL;
447 size_t i;
448 struct ip_service_name *dclist = NULL;
449 size_t count = 0;
450 static const char *resolve_order[] = { "lmhosts", "wins", "bcast", NULL };
452 if (flags & DS_PDC_REQUIRED) {
453 name_type = NBT_NAME_PDC;
456 status = internal_resolve_name(mem_ctx,
457 domain_name,
458 name_type,
459 NULL,
460 &salist,
461 &count,
462 resolve_order);
463 if (!NT_STATUS_IS_OK(status)) {
464 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
465 return status;
468 dclist = talloc_zero_array(mem_ctx, struct ip_service_name, count);
469 if (!dclist) {
470 TALLOC_FREE(salist);
471 return NT_STATUS_NO_MEMORY;
474 for (i=0; i<count; i++) {
475 char addr[INET6_ADDRSTRLEN];
476 struct ip_service_name *r = &dclist[i];
478 print_sockaddr(addr, sizeof(addr),
479 &salist[i].u.ss);
481 r->sa = salist[i];
482 r->hostname = talloc_strdup(mem_ctx, addr);
483 if (!r->hostname) {
484 TALLOC_FREE(salist);
485 TALLOC_FREE(dclist);
486 return NT_STATUS_NO_MEMORY;
491 TALLOC_FREE(salist);
493 *returned_dclist = dclist;
494 *returned_count = count;
496 return NT_STATUS_OK;
499 /****************************************************************
500 ****************************************************************/
502 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
503 const char *domain_name,
504 const struct GUID *domain_guid,
505 uint32_t flags,
506 const char *site_name,
507 struct ip_service_name **returned_dclist,
508 size_t *return_count)
510 size_t i;
511 NTSTATUS status;
512 struct dns_rr_srv *dcs = NULL;
513 size_t numdcs = 0;
514 struct ip_service_name *dclist = NULL;
515 size_t ret_count = 0;
516 char *query = NULL;
518 if (flags & DS_PDC_REQUIRED) {
519 query = ads_dns_query_string_pdc(mem_ctx, domain_name);
520 } else if (flags & DS_GC_SERVER_REQUIRED) {
521 query = ads_dns_query_string_gcs(mem_ctx, domain_name);
522 } else if (flags & DS_KDC_REQUIRED) {
523 query = ads_dns_query_string_kdcs(mem_ctx, domain_name);
524 } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
525 query = ads_dns_query_string_dcs(mem_ctx, domain_name);
526 } else if (domain_guid) {
527 query = ads_dns_query_string_dcs_guid(
528 mem_ctx, domain_guid, domain_name);
529 } else {
530 query = ads_dns_query_string_dcs(mem_ctx, domain_name);
533 if (query == NULL) {
534 return NT_STATUS_NO_MEMORY;
537 status = ads_dns_query_srv(
538 mem_ctx,
539 lp_get_async_dns_timeout(),
540 site_name,
541 query,
542 &dcs,
543 &numdcs);
544 TALLOC_FREE(query);
545 if (!NT_STATUS_IS_OK(status)) {
546 return status;
549 if (numdcs == 0) {
550 TALLOC_FREE(dcs);
551 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
555 * We're only returning one address per
556 * DC name, so just allocate size numdcs.
559 dclist = talloc_zero_array(mem_ctx,
560 struct ip_service_name,
561 numdcs);
562 if (!dclist) {
563 TALLOC_FREE(dcs);
564 return NT_STATUS_NO_MEMORY;
568 * First, copy the SRV record replies that
569 * have IP addresses returned with them.
571 ret_count = 0;
572 for (i = 0; i < numdcs; i++) {
573 size_t j;
575 if (dcs[i].num_ips == 0) {
576 continue;
579 dclist[ret_count].hostname =
580 talloc_move(dclist, &dcs[i].hostname);
583 * Pick the first IPv4 address,
584 * if none pick the first address.
586 * This is different from the previous
587 * code which picked a 'next ip' address
588 * each time, incrementing an index.
589 * Too complex to maintain :-(.
591 for (j = 0; j < dcs[i].num_ips; j++) {
592 if (dcs[i].ss_s[j].ss_family == AF_INET) {
593 bool ok;
594 ok = sockaddr_storage_to_samba_sockaddr(
595 &dclist[ret_count].sa,
596 &dcs[i].ss_s[j]);
597 if (!ok) {
598 TALLOC_FREE(dcs);
599 TALLOC_FREE(dclist);
600 return NT_STATUS_INVALID_PARAMETER;
602 break;
605 if (j == dcs[i].num_ips) {
606 /* No IPv4- use the first IPv6 addr. */
607 bool ok;
608 ok = sockaddr_storage_to_samba_sockaddr(
609 &dclist[ret_count].sa,
610 &dcs[i].ss_s[0]);
611 if (!ok) {
612 TALLOC_FREE(dcs);
613 TALLOC_FREE(dclist);
614 return NT_STATUS_INVALID_PARAMETER;
617 ret_count++;
620 TALLOC_FREE(dcs);
622 if (ret_count == 0) {
623 TALLOC_FREE(dclist);
624 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
627 *returned_dclist = dclist;
628 *return_count = ret_count;
629 return NT_STATUS_OK;
632 /****************************************************************
633 ****************************************************************/
635 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
636 const char *dc_unc,
637 const char *dc_address,
638 uint32_t dc_address_type,
639 const struct GUID *domain_guid,
640 const char *domain_name,
641 const char *forest_name,
642 uint32_t flags,
643 const char *dc_site_name,
644 const char *client_site_name,
645 struct netr_DsRGetDCNameInfo **info_out)
647 struct netr_DsRGetDCNameInfo *info;
649 info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
650 NT_STATUS_HAVE_NO_MEMORY(info);
652 if (dc_unc) {
653 if (!(dc_unc[0] == '\\' && dc_unc[1] == '\\')) {
654 info->dc_unc = talloc_asprintf(mem_ctx, "\\\\%s",
655 dc_unc);
656 } else {
657 info->dc_unc = talloc_strdup(mem_ctx, dc_unc);
659 NT_STATUS_HAVE_NO_MEMORY(info->dc_unc);
662 if (dc_address) {
663 if (!(dc_address[0] == '\\' && dc_address[1] == '\\')) {
664 info->dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
665 dc_address);
666 } else {
667 info->dc_address = talloc_strdup(mem_ctx, dc_address);
669 NT_STATUS_HAVE_NO_MEMORY(info->dc_address);
672 info->dc_address_type = dc_address_type;
674 if (domain_guid) {
675 info->domain_guid = *domain_guid;
678 if (domain_name) {
679 info->domain_name = talloc_strdup(mem_ctx, domain_name);
680 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
683 if (forest_name && *forest_name) {
684 info->forest_name = talloc_strdup(mem_ctx, forest_name);
685 NT_STATUS_HAVE_NO_MEMORY(info->forest_name);
686 flags |= DS_DNS_FOREST_ROOT;
689 info->dc_flags = flags;
691 if (dc_site_name) {
692 info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name);
693 NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name);
696 if (client_site_name) {
697 info->client_site_name = talloc_strdup(mem_ctx,
698 client_site_name);
699 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
702 *info_out = info;
704 return NT_STATUS_OK;
707 /****************************************************************
708 ****************************************************************/
710 static void map_dc_and_domain_names(uint32_t flags,
711 const char *dc_name,
712 const char *domain_name,
713 const char *dns_dc_name,
714 const char *dns_domain_name,
715 uint32_t *dc_flags,
716 const char **hostname_p,
717 const char **domain_p)
719 switch (flags & 0xf0000000) {
720 case DS_RETURN_FLAT_NAME:
721 if (dc_name && domain_name &&
722 *dc_name && *domain_name) {
723 *hostname_p = dc_name;
724 *domain_p = domain_name;
725 break;
728 FALL_THROUGH;
729 case DS_RETURN_DNS_NAME:
730 default:
731 if (dns_dc_name && dns_domain_name &&
732 *dns_dc_name && *dns_domain_name) {
733 *hostname_p = dns_dc_name;
734 *domain_p = dns_domain_name;
735 *dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
736 break;
738 if (dc_name && domain_name &&
739 *dc_name && *domain_name) {
740 *hostname_p = dc_name;
741 *domain_p = domain_name;
742 break;
747 /****************************************************************
748 ****************************************************************/
750 static NTSTATUS make_dc_info_from_cldap_reply(
751 TALLOC_CTX *mem_ctx,
752 uint32_t flags,
753 const struct samba_sockaddr *sa,
754 struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
755 struct netr_DsRGetDCNameInfo **info)
757 const char *dc_hostname = NULL;
758 const char *dc_domain_name = NULL;
759 const char *dc_address = NULL;
760 const char *dc_forest = NULL;
761 uint32_t dc_address_type = 0;
762 uint32_t dc_flags = 0;
763 struct GUID *dc_domain_guid = NULL;
764 const char *dc_server_site = NULL;
765 const char *dc_client_site = NULL;
767 char addr[INET6_ADDRSTRLEN];
769 if (sa != NULL) {
770 print_sockaddr(addr, sizeof(addr), &sa->u.ss);
771 dc_address = addr;
772 dc_address_type = DS_ADDRESS_TYPE_INET;
773 } else {
774 if (r->sockaddr.pdc_ip) {
775 dc_address = r->sockaddr.pdc_ip;
776 dc_address_type = DS_ADDRESS_TYPE_INET;
777 } else {
778 dc_address = r->pdc_name;
779 dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
783 map_dc_and_domain_names(flags,
784 r->pdc_name,
785 r->domain_name,
786 r->pdc_dns_name,
787 r->dns_domain,
788 &dc_flags,
789 &dc_hostname,
790 &dc_domain_name);
792 dc_flags |= r->server_type;
793 dc_forest = r->forest;
794 dc_domain_guid = &r->domain_uuid;
795 dc_server_site = r->server_site;
796 dc_client_site = r->client_site;
798 return make_domain_controller_info(mem_ctx,
799 dc_hostname,
800 dc_address,
801 dc_address_type,
802 dc_domain_guid,
803 dc_domain_name,
804 dc_forest,
805 dc_flags,
806 dc_server_site,
807 dc_client_site,
808 info);
811 /****************************************************************
812 ****************************************************************/
814 static uint32_t map_ds_flags_to_nt_version(uint32_t flags)
816 uint32_t nt_version = 0;
818 if (flags & DS_PDC_REQUIRED) {
819 nt_version |= NETLOGON_NT_VERSION_PDC;
822 if (flags & DS_GC_SERVER_REQUIRED) {
823 nt_version |= NETLOGON_NT_VERSION_GC;
826 if (flags & DS_TRY_NEXTCLOSEST_SITE) {
827 nt_version |= NETLOGON_NT_VERSION_WITH_CLOSEST_SITE;
830 if (flags & DS_IP_REQUIRED) {
831 nt_version |= NETLOGON_NT_VERSION_IP;
834 return nt_version;
837 /****************************************************************
838 ****************************************************************/
840 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
841 const char *domain_name,
842 uint32_t flags,
843 struct ip_service_name *dclist,
844 size_t num_dcs,
845 struct netr_DsRGetDCNameInfo **info)
847 size_t i = 0;
848 bool valid_dc = false;
849 struct netlogon_samlogon_response *r = NULL;
850 uint32_t nt_version = NETLOGON_NT_VERSION_5 |
851 NETLOGON_NT_VERSION_5EX;
852 uint32_t ret_flags = 0;
853 NTSTATUS status;
855 nt_version |= map_ds_flags_to_nt_version(flags);
857 for (i=0; i<num_dcs; i++) {
858 char addr[INET6_ADDRSTRLEN];
860 print_sockaddr(addr, sizeof(addr), &dclist[i].sa.u.ss);
862 DEBUG(10,("LDAP ping to %s (%s)\n", dclist[i].hostname, addr));
864 if (ads_cldap_netlogon(mem_ctx, &dclist[i].sa.u.ss,
865 domain_name,
866 nt_version,
867 &r))
869 nt_version = r->ntver;
870 ret_flags = get_cldap_reply_server_flags(r, nt_version);
872 if (check_cldap_reply_required_flags(ret_flags, flags)) {
873 valid_dc = true;
874 break;
878 continue;
881 if (!valid_dc) {
882 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
885 status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].sa,
886 &r->data.nt5_ex, info);
887 if (NT_STATUS_IS_OK(status)) {
888 return store_cldap_reply(mem_ctx, flags, &dclist[i].sa,
889 nt_version, &r->data.nt5_ex);
892 return status;
895 /****************************************************************
896 ****************************************************************/
898 /****************************************************************
899 ****************************************************************/
901 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
902 struct messaging_context *msg_ctx,
903 const char *domain_name,
904 uint32_t flags,
905 struct ip_service_name *dclist,
906 size_t num_dcs,
907 struct netr_DsRGetDCNameInfo **info)
909 enum nbt_name_type name_type = NBT_NAME_LOGON;
910 NTSTATUS status;
911 size_t i;
912 const char *dc_name = NULL;
913 fstring tmp_dc_name;
914 struct netlogon_samlogon_response *r = NULL;
915 bool store_cache = false;
916 uint32_t nt_version = NETLOGON_NT_VERSION_1 |
917 NETLOGON_NT_VERSION_5 |
918 NETLOGON_NT_VERSION_5EX_WITH_IP;
919 size_t len = strlen(lp_netbios_name());
920 char my_acct_name[len+2];
922 if (msg_ctx == NULL) {
923 return NT_STATUS_INVALID_PARAMETER;
926 if (flags & DS_PDC_REQUIRED) {
927 name_type = NBT_NAME_PDC;
930 nt_version |= map_ds_flags_to_nt_version(flags);
932 snprintf(my_acct_name,
933 sizeof(my_acct_name),
934 "%s$",
935 lp_netbios_name());
937 DEBUG(10,("process_dc_netbios\n"));
939 for (i=0; i<num_dcs; i++) {
940 uint16_t val;
942 generate_random_buffer((uint8_t *)&val, 2);
944 status = nbt_getdc(msg_ctx, 10, &dclist[i].sa.u.ss, domain_name,
945 NULL, my_acct_name, ACB_WSTRUST, nt_version,
946 mem_ctx, &nt_version, &dc_name, &r);
947 if (NT_STATUS_IS_OK(status)) {
948 store_cache = true;
949 namecache_store(dc_name,
950 NBT_NAME_SERVER,
952 &dclist[i].sa);
953 goto make_reply;
956 if (name_status_find(domain_name,
957 name_type,
958 NBT_NAME_SERVER,
959 &dclist[i].sa.u.ss,
960 tmp_dc_name))
962 struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1;
964 r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
965 NT_STATUS_HAVE_NO_MEMORY(r);
967 ZERO_STRUCT(logon1);
969 nt_version = NETLOGON_NT_VERSION_1;
971 logon1.nt_version = nt_version;
972 logon1.pdc_name = tmp_dc_name;
973 logon1.domain_name = talloc_strdup_upper(mem_ctx, domain_name);
974 NT_STATUS_HAVE_NO_MEMORY(logon1.domain_name);
976 r->data.nt4 = logon1;
977 r->ntver = nt_version;
979 map_netlogon_samlogon_response(r);
981 namecache_store(tmp_dc_name,
982 NBT_NAME_SERVER,
984 &dclist[i].sa);
986 goto make_reply;
990 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
992 make_reply:
994 status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].sa,
995 &r->data.nt5_ex, info);
996 if (NT_STATUS_IS_OK(status) && store_cache) {
997 return store_cldap_reply(mem_ctx, flags, &dclist[i].sa,
998 nt_version, &r->data.nt5_ex);
1001 return status;
1004 /****************************************************************
1005 ****************************************************************/
1007 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
1008 struct messaging_context *msg_ctx,
1009 const char *domain_name,
1010 const struct GUID *domain_guid,
1011 uint32_t flags,
1012 const char *site_name,
1013 struct netr_DsRGetDCNameInfo **info)
1015 NTSTATUS status;
1016 struct ip_service_name *dclist = NULL;
1017 size_t num_dcs = 0;
1019 DEBUG(10,("dsgetdcname_rediscover\n"));
1021 if (flags & DS_IS_FLAT_NAME) {
1023 if (lp_disable_netbios()) {
1024 return NT_STATUS_NOT_SUPPORTED;
1027 status = discover_dc_netbios(mem_ctx, domain_name, flags,
1028 &dclist, &num_dcs);
1029 NT_STATUS_NOT_OK_RETURN(status);
1031 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags,
1032 dclist, num_dcs, info);
1035 if (flags & DS_IS_DNS_NAME) {
1037 status = discover_dc_dns(mem_ctx, domain_name, domain_guid,
1038 flags, site_name, &dclist, &num_dcs);
1039 NT_STATUS_NOT_OK_RETURN(status);
1041 return process_dc_dns(mem_ctx, domain_name, flags,
1042 dclist, num_dcs, info);
1045 status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags,
1046 site_name, &dclist, &num_dcs);
1048 if (NT_STATUS_IS_OK(status) && num_dcs != 0) {
1050 status = process_dc_dns(mem_ctx, domain_name, flags, dclist,
1051 num_dcs, info);
1052 if (NT_STATUS_IS_OK(status)) {
1053 return status;
1057 if (lp_disable_netbios()) {
1058 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1061 status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
1062 &num_dcs);
1063 NT_STATUS_NOT_OK_RETURN(status);
1065 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags, dclist,
1066 num_dcs, info);
1069 static bool is_closest_site(struct netr_DsRGetDCNameInfo *info)
1071 if (info->dc_flags & DS_SERVER_CLOSEST) {
1072 return true;
1075 if (!info->client_site_name) {
1076 return true;
1079 if (!info->dc_site_name) {
1080 return false;
1083 if (strcmp(info->client_site_name, info->dc_site_name) == 0) {
1084 return true;
1087 return false;
1090 /********************************************************************
1091 Internal dsgetdcname.
1092 ********************************************************************/
1094 static NTSTATUS dsgetdcname_internal(TALLOC_CTX *mem_ctx,
1095 struct messaging_context *msg_ctx,
1096 const char *domain_name,
1097 const struct GUID *domain_guid,
1098 const char *site_name,
1099 uint32_t flags,
1100 struct netr_DsRGetDCNameInfo **info)
1102 NTSTATUS status;
1103 struct netr_DsRGetDCNameInfo *myinfo = NULL;
1104 bool first = true;
1105 struct netr_DsRGetDCNameInfo *first_info = NULL;
1107 DEBUG(10,("dsgetdcname_internal: domain_name: %s, "
1108 "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
1109 domain_name,
1110 domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)",
1111 site_name ? site_name : "(null)", flags));
1113 *info = NULL;
1115 if (!check_allowed_required_flags(flags, site_name)) {
1116 DEBUG(0,("invalid flags specified\n"));
1117 return NT_STATUS_INVALID_PARAMETER;
1120 if (flags & DS_FORCE_REDISCOVERY) {
1121 goto rediscover;
1124 status = dsgetdcname_cached(mem_ctx, msg_ctx, domain_name, domain_guid,
1125 flags, site_name, &myinfo);
1126 if (NT_STATUS_IS_OK(status)) {
1127 *info = myinfo;
1128 goto done;
1131 if (flags & DS_BACKGROUND_ONLY) {
1132 goto done;
1135 rediscover:
1136 status = dsgetdcname_rediscover(mem_ctx, msg_ctx, domain_name,
1137 domain_guid, flags, site_name,
1138 &myinfo);
1140 done:
1141 if (!NT_STATUS_IS_OK(status)) {
1142 if (!first) {
1143 *info = first_info;
1144 return NT_STATUS_OK;
1146 return status;
1149 if (!first) {
1150 TALLOC_FREE(first_info);
1151 } else if (!is_closest_site(myinfo)) {
1152 first = false;
1153 first_info = myinfo;
1154 /* TODO: may use the next_closest_site here */
1155 site_name = myinfo->client_site_name;
1156 goto rediscover;
1159 *info = myinfo;
1160 return NT_STATUS_OK;
1163 /********************************************************************
1164 dsgetdcname.
1166 This will be the only public function here.
1167 ********************************************************************/
1169 NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
1170 struct messaging_context *msg_ctx,
1171 const char *domain_name,
1172 const struct GUID *domain_guid,
1173 const char *site_name,
1174 uint32_t flags,
1175 struct netr_DsRGetDCNameInfo **info)
1177 NTSTATUS status;
1178 const char *query_site = NULL;
1179 char *ptr_to_free = NULL;
1180 bool retry_query_with_null = false;
1182 if ((site_name == NULL) || (site_name[0] == '\0')) {
1183 ptr_to_free = sitename_fetch(mem_ctx, domain_name);
1184 if (ptr_to_free != NULL) {
1185 retry_query_with_null = true;
1187 query_site = ptr_to_free;
1188 } else {
1189 query_site = site_name;
1192 status = dsgetdcname_internal(mem_ctx,
1193 msg_ctx,
1194 domain_name,
1195 domain_guid,
1196 query_site,
1197 flags,
1198 info);
1200 TALLOC_FREE(ptr_to_free);
1202 if (!NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1203 return status;
1206 /* Should we try again with site_name == NULL ? */
1207 if (retry_query_with_null) {
1208 status = dsgetdcname_internal(mem_ctx,
1209 msg_ctx,
1210 domain_name,
1211 domain_guid,
1212 NULL,
1213 flags,
1214 info);
1217 return status;
1220 NTSTATUS dsgetonedcname(TALLOC_CTX *mem_ctx,
1221 struct messaging_context *msg_ctx,
1222 const char *domain_name,
1223 const char *dcname,
1224 uint32_t flags,
1225 struct netr_DsRGetDCNameInfo **info)
1227 NTSTATUS status;
1228 struct sockaddr_storage *addrs;
1229 unsigned int num_addrs, i;
1230 const char *hostname = strip_hostname(dcname);
1232 status = resolve_name_list(mem_ctx, hostname, 0x20,
1233 &addrs, &num_addrs);
1234 if (!NT_STATUS_IS_OK(status)) {
1235 return status;
1238 for (i = 0; i < num_addrs; i++) {
1240 bool ok;
1241 struct ip_service_name dclist;
1243 dclist.hostname = hostname;
1244 ok = sockaddr_storage_to_samba_sockaddr(&dclist.sa, &addrs[i]);
1245 if (!ok) {
1246 TALLOC_FREE(addrs);
1247 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1250 status = process_dc_dns(mem_ctx, domain_name, flags,
1251 &dclist, 1, info);
1252 if (NT_STATUS_IS_OK(status)) {
1253 TALLOC_FREE(addrs);
1254 return NT_STATUS_OK;
1257 if (lp_disable_netbios()) {
1258 continue;
1261 status = process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags,
1262 &dclist, 1, info);
1263 if (NT_STATUS_IS_OK(status)) {
1264 TALLOC_FREE(addrs);
1265 return NT_STATUS_OK;
1269 TALLOC_FREE(addrs);
1270 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;