2 Unix SMB/CIFS implementation.
4 crachnames implementation for the drsuapi pipe
7 Copyright (C) Stefan Metzmacher 2004
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
9 Copyright (C) Matthieu Patou <mat@matws.net> 2012
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "librpc/gen_ndr/drsuapi.h"
27 #include "lib/events/events.h"
29 #include <ldb_errors.h>
30 #include "auth/kerberos/kerberos.h"
31 #include "libcli/ldap/ldap_ndr.h"
32 #include "libcli/security/security.h"
33 #include "auth/auth.h"
34 #include "../lib/util/util_ldb.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/common/util.h"
37 #include "param/param.h"
39 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
40 struct smb_krb5_context *smb_krb5_context,
41 uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
42 enum drsuapi_DsNameFormat format_desired,
43 struct ldb_dn *name_dn, const char *name,
44 const char *domain_filter, const char *result_filter,
45 struct drsuapi_DsNameInfo1 *info1, int scope, struct ldb_dn *search_dn);
46 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
47 enum drsuapi_DsNameFormat format_offered,
48 enum drsuapi_DsNameFormat format_desired,
49 struct ldb_dn *name_dn, const char *name,
50 struct drsuapi_DsNameInfo1 *info1);
52 static WERROR dns_domain_from_principal(TALLOC_CTX *mem_ctx, struct smb_krb5_context *smb_krb5_context,
54 struct drsuapi_DsNameInfo1 *info1)
57 krb5_principal principal;
58 /* perhaps it's a principal with a realm, so return the right 'domain only' response */
60 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
61 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM, &principal);
63 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
67 realm = smb_krb5_principal_get_realm(smb_krb5_context->krb5_context, principal);
69 info1->dns_domain_name = talloc_strdup(mem_ctx, realm);
70 krb5_free_principal(smb_krb5_context->krb5_context, principal);
73 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
75 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
79 static enum drsuapi_DsNameStatus LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx,
81 const char *alias_from,
86 struct ldb_result *res;
87 struct ldb_message_element *spnmappings;
89 struct ldb_dn *service_dn;
92 const char *directory_attrs[] = {
97 tmp_ctx = talloc_new(mem_ctx);
99 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
102 service_dn = ldb_dn_new(tmp_ctx, ldb_ctx, "CN=Directory Service,CN=Windows NT,CN=Services");
103 if ( ! ldb_dn_add_base(service_dn, ldb_get_config_basedn(ldb_ctx))) {
104 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
106 service_dn_str = ldb_dn_alloc_linearized(tmp_ctx, service_dn);
107 if ( ! service_dn_str) {
108 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
111 ret = ldb_search(ldb_ctx, tmp_ctx, &res, service_dn, LDB_SCOPE_BASE,
112 directory_attrs, "(objectClass=nTDSService)");
114 if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
115 DEBUG(1, ("ldb_search: dn: %s not found: %s\n", service_dn_str, ldb_errstring(ldb_ctx)));
116 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
117 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
118 DEBUG(1, ("ldb_search: dn: %s not found\n", service_dn_str));
119 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
120 } else if (res->count != 1) {
122 DEBUG(1, ("ldb_search: dn: %s not found\n", service_dn_str));
123 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
126 spnmappings = ldb_msg_find_element(res->msgs[0], "sPNMappings");
127 if (!spnmappings || spnmappings->num_values == 0) {
128 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute\n", service_dn_str));
129 talloc_free(tmp_ctx);
130 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
133 for (i = 0; i < spnmappings->num_values; i++) {
134 char *mapping, *p, *str;
135 mapping = talloc_strdup(tmp_ctx,
136 (const char *)spnmappings->values[i].data);
138 DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str));
139 talloc_free(tmp_ctx);
140 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
143 /* C string manipulation sucks */
145 p = strchr(mapping, '=');
147 DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n",
148 service_dn_str, mapping));
149 talloc_free(tmp_ctx);
150 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
161 if (strcasecmp(str, alias_from) == 0) {
163 talloc_steal(mem_ctx, mapping);
164 talloc_free(tmp_ctx);
165 return DRSUAPI_DS_NAME_STATUS_OK;
169 DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from));
170 talloc_free(tmp_ctx);
171 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
174 /* When cracking a ServicePrincipalName, many services may be served
175 * by the host/ servicePrincipalName. The incoming query is for cifs/
176 * but we translate it here, and search on host/. This is done after
177 * the cifs/ entry has been searched for, making this a fallback */
179 static WERROR DsCrackNameSPNAlias(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
180 struct smb_krb5_context *smb_krb5_context,
181 uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
182 enum drsuapi_DsNameFormat format_desired,
183 const char *name, struct drsuapi_DsNameInfo1 *info1)
187 krb5_principal principal;
188 const krb5_data *component;
189 const char *service, *dns_name;
192 enum drsuapi_DsNameStatus namestatus;
194 /* parse principal */
195 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context,
196 name, KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
198 DEBUG(2, ("Could not parse principal: %s: %s\n",
199 name, smb_get_krb5_error_message(smb_krb5_context->krb5_context,
204 /* grab cifs/, http/ etc */
206 /* This is checked for in callers, but be safe */
207 if (krb5_princ_size(smb_krb5_context->krb5_context, principal) < 2) {
208 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
209 krb5_free_principal(smb_krb5_context->krb5_context, principal);
212 component = krb5_princ_component(smb_krb5_context->krb5_context,
214 service = (const char *)component->data;
215 component = krb5_princ_component(smb_krb5_context->krb5_context,
217 dns_name = (const char *)component->data;
220 namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context,
222 service, &new_service);
224 if (namestatus == DRSUAPI_DS_NAME_STATUS_NOT_FOUND) {
226 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
227 info1->dns_domain_name = talloc_strdup(mem_ctx, dns_name);
228 if (!info1->dns_domain_name) {
231 krb5_free_principal(smb_krb5_context->krb5_context, principal);
233 } else if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
234 info1->status = namestatus;
235 krb5_free_principal(smb_krb5_context->krb5_context, principal);
239 /* reform principal */
240 new_princ = talloc_asprintf(mem_ctx, "%s/%s", new_service, dns_name);
242 krb5_free_principal(smb_krb5_context->krb5_context, principal);
246 wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
248 talloc_free(new_princ);
249 if (W_ERROR_IS_OK(wret) && (info1->status == DRSUAPI_DS_NAME_STATUS_NOT_FOUND)) {
250 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
251 info1->dns_domain_name = talloc_strdup(mem_ctx, dns_name);
252 if (!info1->dns_domain_name) {
256 krb5_free_principal(smb_krb5_context->krb5_context, principal);
260 /* Subcase of CrackNames, for the userPrincipalName */
262 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
263 struct smb_krb5_context *smb_krb5_context,
264 uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
265 enum drsuapi_DsNameFormat format_desired,
266 const char *name, struct drsuapi_DsNameInfo1 *info1)
270 const char *domain_filter = NULL;
271 const char *result_filter = NULL;
273 krb5_principal principal;
275 char *unparsed_name_short;
276 const char *domain_attrs[] = { NULL };
277 struct ldb_result *domain_res = NULL;
279 /* Prevent recursion */
281 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
285 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
286 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM, &principal);
288 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
292 realm = smb_krb5_principal_get_realm(smb_krb5_context->krb5_context,
295 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
296 samdb_partitions_dn(sam_ctx, mem_ctx),
299 "(&(objectClass=crossRef)(|(dnsRoot=%s)(netbiosName=%s))(systemFlags:%s:=%u))",
300 ldb_binary_encode_string(mem_ctx, realm),
301 ldb_binary_encode_string(mem_ctx, realm),
302 LDB_OID_COMPARATOR_AND,
303 SYSTEM_FLAG_CR_NTDS_DOMAIN);
306 if (ldb_ret != LDB_SUCCESS) {
307 DEBUG(2, ("DsCrackNameUPN domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
308 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
309 krb5_free_principal(smb_krb5_context->krb5_context, principal);
313 switch (domain_res->count) {
317 krb5_free_principal(smb_krb5_context->krb5_context, principal);
318 return dns_domain_from_principal(mem_ctx, smb_krb5_context,
321 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
322 krb5_free_principal(smb_krb5_context->krb5_context, principal);
326 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
327 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
328 krb5_free_principal(smb_krb5_context->krb5_context, principal);
331 free(unparsed_name_short);
335 /* This may need to be extended for more userPrincipalName variations */
336 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
337 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
339 domain_filter = talloc_asprintf(mem_ctx, "(distinguishedName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
341 if (!result_filter || !domain_filter) {
342 free(unparsed_name_short);
345 status = DsCrackNameOneFilter(sam_ctx, mem_ctx,
347 format_flags, format_offered, format_desired,
348 NULL, unparsed_name_short, domain_filter, result_filter,
349 info1, LDB_SCOPE_SUBTREE, NULL);
350 free(unparsed_name_short);
356 * This function will workout the filtering parameter in order to be able to do
357 * the adapted search when the incomming format is format_functional.
358 * This boils down to defining the search_dn (passed as pointer to ldb_dn *) and the
359 * ldap filter request.
360 * Main input parameters are:
361 * * name, which is the portion of the functional name after the
363 * * domain_filter, which is a ldap search filter used to find the NC DN given the
364 * function name to crack.
366 static WERROR get_format_functional_filtering_param(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
367 char *name, struct drsuapi_DsNameInfo1 *info1,
368 struct ldb_dn **psearch_dn, const char *domain_filter, const char **presult_filter)
370 struct ldb_result *domain_res = NULL;
371 const char * const domain_attrs[] = {"ncName", NULL};
372 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
374 char *account, *s, *result_filter = NULL;
375 struct ldb_dn *search_dn = NULL;
378 *presult_filter = NULL;
380 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
384 "%s", domain_filter);
386 if (ldb_ret != LDB_SUCCESS) {
387 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
388 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
392 if (domain_res->count == 1) {
393 struct ldb_dn *tmp_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
394 const char * const name_attrs[] = {"name", NULL};
397 s = strchr(account, '/');
398 talloc_free(domain_res);
403 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
409 if (ldb_ret != LDB_SUCCESS) {
410 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
411 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
415 switch (domain_res->count) {
419 talloc_free(domain_res);
420 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
423 talloc_free(domain_res);
424 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
428 tmp_dn = talloc_steal(mem_ctx, domain_res->msgs[0]->dn);
429 talloc_free(domain_res);
432 s = strchr(account, '/');
434 account = ldb_binary_encode_string(mem_ctx, account);
435 W_ERROR_HAVE_NO_MEMORY(account);
436 result_filter = talloc_asprintf(mem_ctx, "(name=%s)",
438 W_ERROR_HAVE_NO_MEMORY(result_filter);
440 *psearch_dn = search_dn;
441 *presult_filter = result_filter;
445 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
447 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
448 uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
449 enum drsuapi_DsNameFormat format_desired,
450 const char *name, struct drsuapi_DsNameInfo1 *info1)
453 const char *domain_filter = NULL;
454 const char *result_filter = NULL;
455 struct ldb_dn *name_dn = NULL;
456 struct ldb_dn *search_dn = NULL;
458 struct smb_krb5_context *smb_krb5_context = NULL;
459 int scope = LDB_SCOPE_SUBTREE;
461 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
462 info1->dns_domain_name = NULL;
463 info1->result_name = NULL;
466 return WERR_INVALID_PARAM;
469 /* TODO: - fill the correct names in all cases!
470 * - handle format_flags
472 if (format_desired == DRSUAPI_DS_NAME_FORMAT_UNKNOWN) {
475 /* here we need to set the domain_filter and/or the result_filter */
476 switch (format_offered) {
477 case DRSUAPI_DS_NAME_FORMAT_UNKNOWN:
480 enum drsuapi_DsNameFormat formats[] = {
481 DRSUAPI_DS_NAME_FORMAT_FQDN_1779, DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
482 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT, DRSUAPI_DS_NAME_FORMAT_CANONICAL,
483 DRSUAPI_DS_NAME_FORMAT_GUID, DRSUAPI_DS_NAME_FORMAT_DISPLAY,
484 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
485 DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY,
486 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
489 for (i=0; i < ARRAY_SIZE(formats); i++) {
490 werr = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, formats[i], format_desired, name, info1);
491 if (!W_ERROR_IS_OK(werr)) {
494 if (info1->status != DRSUAPI_DS_NAME_STATUS_NOT_FOUND &&
495 (formats[i] != DRSUAPI_DS_NAME_FORMAT_CANONICAL ||
496 info1->status != DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR))
504 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
505 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
507 char *str, *s, *account;
508 scope = LDB_SCOPE_ONELEVEL;
510 if (strlen(name) == 0) {
511 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
515 str = talloc_strdup(mem_ctx, name);
516 W_ERROR_HAVE_NO_MEMORY(str);
518 if (format_offered == DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX) {
519 /* Look backwards for the \n, and replace it with / */
520 s = strrchr(str, '\n');
522 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
528 s = strchr(str, '/');
530 /* there must be at least one / */
531 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
538 domain_filter = talloc_asprintf(mem_ctx, "(&(objectClass=crossRef)(dnsRoot=%s)(systemFlags:%s:=%u))",
539 ldb_binary_encode_string(mem_ctx, str),
540 LDB_OID_COMPARATOR_AND,
541 SYSTEM_FLAG_CR_NTDS_DOMAIN);
542 W_ERROR_HAVE_NO_MEMORY(domain_filter);
544 /* There may not be anything after the domain component (search for the domain itself) */
546 if (account && *account) {
547 WERROR werr = get_format_functional_filtering_param(sam_ctx,
554 if (!W_ERROR_IS_OK(werr)) {
557 if (info1->status != DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR)
562 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
565 const char *account = NULL;
567 domain = talloc_strdup(mem_ctx, name);
568 W_ERROR_HAVE_NO_MEMORY(domain);
570 p = strchr(domain, '\\');
572 /* invalid input format */
573 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
582 domain_filter = talloc_asprintf(mem_ctx,
583 "(&(objectClass=crossRef)(netbiosName=%s)(systemFlags:%s:=%u))",
584 ldb_binary_encode_string(mem_ctx, domain),
585 LDB_OID_COMPARATOR_AND,
586 SYSTEM_FLAG_CR_NTDS_DOMAIN);
587 W_ERROR_HAVE_NO_MEMORY(domain_filter);
589 result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
590 ldb_binary_encode_string(mem_ctx, account));
591 W_ERROR_HAVE_NO_MEMORY(result_filter);
598 /* A LDAP DN as a string */
599 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
600 domain_filter = NULL;
601 name_dn = ldb_dn_new(mem_ctx, sam_ctx, name);
602 if (! ldb_dn_validate(name_dn)) {
603 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
609 /* A GUID as a string */
610 case DRSUAPI_DS_NAME_FORMAT_GUID: {
614 domain_filter = NULL;
616 nt_status = GUID_from_string(name, &guid);
617 if (!NT_STATUS_IS_OK(nt_status)) {
618 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
622 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
626 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
628 W_ERROR_HAVE_NO_MEMORY(result_filter);
631 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
632 domain_filter = NULL;
634 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
635 ldb_binary_encode_string(mem_ctx, name),
636 ldb_binary_encode_string(mem_ctx, name));
637 W_ERROR_HAVE_NO_MEMORY(result_filter);
641 /* A S-1234-5678 style string */
642 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
643 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
646 domain_filter = NULL;
648 info1->dns_domain_name = NULL;
649 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
652 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx,
657 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
659 W_ERROR_HAVE_NO_MEMORY(result_filter);
662 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
663 krb5_principal principal;
666 ret = smb_krb5_init_context(mem_ctx,
667 (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"),
674 /* Ensure we reject compleate junk first */
675 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
677 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
681 domain_filter = NULL;
683 /* By getting the unparsed name here, we ensure the escaping is correct (and trust the client less) */
684 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
686 krb5_free_principal(smb_krb5_context->krb5_context, principal);
690 krb5_free_principal(smb_krb5_context->krb5_context, principal);
692 /* The ldb_binary_encode_string() here avoid LDAP filter injection attacks */
693 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))",
694 ldb_binary_encode_string(mem_ctx, unparsed_name));
697 W_ERROR_HAVE_NO_MEMORY(result_filter);
700 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
701 krb5_principal principal;
702 char *unparsed_name_short;
703 const krb5_data *component;
706 ret = smb_krb5_init_context(mem_ctx,
707 (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"),
714 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
716 krb5_princ_size(smb_krb5_context->krb5_context,
718 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
719 krb5_free_principal(smb_krb5_context->krb5_context, principal);
721 } else if (ret == 0) {
722 krb5_free_principal(smb_krb5_context->krb5_context, principal);
724 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
725 KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
727 return dns_domain_from_principal(mem_ctx, smb_krb5_context,
731 domain_filter = NULL;
733 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
734 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
736 krb5_free_principal(smb_krb5_context->krb5_context, principal);
740 component = krb5_princ_component(smb_krb5_context->krb5_context,
742 service = (char *)component->data;
743 if ((krb5_princ_size(smb_krb5_context->krb5_context,
745 (strcasecmp(service, "host") == 0)) {
746 /* the 'cn' attribute is just the leading part of the name */
748 component = krb5_princ_component(
749 smb_krb5_context->krb5_context,
751 computer_name = talloc_strndup(mem_ctx, (char *)component->data,
752 strcspn((char *)component->data, "."));
753 if (computer_name == NULL) {
754 krb5_free_principal(smb_krb5_context->krb5_context, principal);
755 free(unparsed_name_short);
759 result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
760 ldb_binary_encode_string(mem_ctx, unparsed_name_short),
761 ldb_binary_encode_string(mem_ctx, computer_name));
763 result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
764 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
766 krb5_free_principal(smb_krb5_context->krb5_context, principal);
767 free(unparsed_name_short);
768 W_ERROR_HAVE_NO_MEMORY(result_filter);
773 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
778 if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
779 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
780 name_dn, name, info1);
783 return DsCrackNameOneFilter(sam_ctx, mem_ctx,
785 format_flags, format_offered, format_desired,
787 domain_filter, result_filter,
788 info1, scope, search_dn);
791 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
792 * (FQDN_1779) into a canoical name without actually searching the
795 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
796 enum drsuapi_DsNameFormat format_offered,
797 enum drsuapi_DsNameFormat format_desired,
798 struct ldb_dn *name_dn, const char *name,
799 struct drsuapi_DsNameInfo1 *info1)
802 if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
803 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
807 switch (format_desired) {
808 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
809 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
811 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
812 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
815 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
818 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
819 info1->result_name = cracked;
827 /* Given a filter for the domain, and one for the result, perform the
828 * ldb search. The format offered and desired flags change the
829 * behaviours, including what attributes to return.
831 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
834 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
835 struct smb_krb5_context *smb_krb5_context,
836 uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
837 enum drsuapi_DsNameFormat format_desired,
838 struct ldb_dn *name_dn, const char *name,
839 const char *domain_filter, const char *result_filter,
840 struct drsuapi_DsNameInfo1 *info1,
841 int scope, struct ldb_dn *search_dn)
844 struct ldb_result *domain_res = NULL;
845 const char * const *domain_attrs;
846 const char * const *result_attrs;
847 struct ldb_message **result_res = NULL;
848 struct ldb_message *result = NULL;
851 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
853 const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
854 const char * const _result_attrs_null[] = { NULL };
856 const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
857 const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
859 const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
860 const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", "objectClass", NULL};
862 const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
863 const char * const _result_attrs_guid[] = { "objectGUID", NULL};
865 const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
866 const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
868 const char * const _domain_attrs_none[] = { "ncName", "dnsRoot" , NULL};
869 const char * const _result_attrs_none[] = { NULL};
871 /* here we need to set the attrs lists for domain and result lookups */
872 switch (format_desired) {
873 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
874 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
875 domain_attrs = _domain_attrs_1779;
876 result_attrs = _result_attrs_null;
878 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
879 domain_attrs = _domain_attrs_canonical;
880 result_attrs = _result_attrs_canonical;
882 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
883 domain_attrs = _domain_attrs_nt4;
884 result_attrs = _result_attrs_nt4;
886 case DRSUAPI_DS_NAME_FORMAT_GUID:
887 domain_attrs = _domain_attrs_guid;
888 result_attrs = _result_attrs_guid;
890 case DRSUAPI_DS_NAME_FORMAT_DISPLAY:
891 domain_attrs = _domain_attrs_display;
892 result_attrs = _result_attrs_display;
895 domain_attrs = _domain_attrs_none;
896 result_attrs = _result_attrs_none;
901 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
902 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
906 "%s", domain_filter);
908 if (ldb_ret != LDB_SUCCESS) {
909 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
910 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
914 switch (domain_res->count) {
918 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
921 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
925 info1->dns_domain_name = ldb_msg_find_attr_as_string(domain_res->msgs[0], "dnsRoot", NULL);
926 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
927 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
929 info1->dns_domain_name = NULL;
930 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
935 struct ldb_result *res;
936 uint32_t dsdb_flags = 0;
937 struct ldb_dn *real_search_dn = NULL;
938 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
941 * From 4.1.4.2.11 of MS-DRSR
942 * if DS_NAME_FLAG_GCVERIFY in flags then
943 * rt := select all O from all
944 * where attrValue in GetAttrVals(O, att, false)
946 * rt := select all O from subtree DefaultNC()
947 * where attrValue in GetAttrVals(O, att, false)
951 if (format_flags & DRSUAPI_DS_NAME_FLAG_GCVERIFY ||
952 format_offered == DRSUAPI_DS_NAME_FORMAT_GUID)
954 dsdb_flags = DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
955 } else if (domain_res) {
957 struct ldb_dn *tmp_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
958 real_search_dn = tmp_dn;
960 real_search_dn = search_dn;
963 real_search_dn = ldb_get_default_basedn(sam_ctx);
965 if (format_desired == DRSUAPI_DS_NAME_FORMAT_GUID){
966 dsdb_flags |= DSDB_SEARCH_SHOW_RECYCLED;
968 /* search with the 'phantom root' flag */
969 ret = dsdb_search(sam_ctx, mem_ctx, &res,
974 "%s", result_filter);
975 if (ret != LDB_SUCCESS) {
976 DEBUG(2, ("DsCrackNameOneFilter search from '%s' with flags 0x%08x failed: %s\n",
977 ldb_dn_get_linearized(real_search_dn),
979 ldb_errstring(sam_ctx)));
980 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
984 ldb_ret = res->count;
985 result_res = res->msgs;
986 } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
987 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
989 } else if (domain_res) {
990 name_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
991 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
995 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not available: This can't happen...\n"));
996 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1002 result = result_res[0];
1005 switch (format_offered) {
1006 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL:
1007 return DsCrackNameSPNAlias(sam_ctx, mem_ctx,
1009 format_flags, format_offered, format_desired,
1012 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
1013 return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context,
1014 format_flags, format_offered, format_desired,
1019 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1022 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s\n", ldb_errstring(sam_ctx)));
1023 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1026 switch (format_offered) {
1027 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
1028 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
1030 const char *canonical_name = NULL; /* Not required, but we get warnings... */
1031 /* We may need to manually filter further */
1032 for (i = 0; i < ldb_ret; i++) {
1033 switch (format_offered) {
1034 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
1035 canonical_name = ldb_dn_canonical_string(mem_ctx, result_res[i]->dn);
1037 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
1038 canonical_name = ldb_dn_canonical_ex_string(mem_ctx, result_res[i]->dn);
1043 if (strcasecmp_m(canonical_name, name) == 0) {
1044 result = result_res[i];
1049 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1055 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1060 info1->dns_domain_name = ldb_dn_canonical_string(mem_ctx, result->dn);
1061 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
1062 p = strchr(info1->dns_domain_name, '/');
1067 /* here we can use result and domain_res[0] */
1068 switch (format_desired) {
1069 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
1070 info1->result_name = ldb_dn_alloc_linearized(mem_ctx, result->dn);
1071 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1073 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1076 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
1077 info1->result_name = ldb_msg_find_attr_as_string(result, "canonicalName", NULL);
1078 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1081 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
1082 /* Not in the virtual ldb attribute */
1083 return DsCrackNameOneSyntactical(mem_ctx,
1084 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1085 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
1086 result->dn, name, info1);
1088 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
1090 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result, "objectSid");
1091 const char *_acc = "", *_dom = "";
1093 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1097 if (samdb_find_attribute(sam_ctx, result, "objectClass",
1099 /* This can also find a DomainDNSZones entry,
1100 * but it won't have the SID we just
1102 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
1106 "(ncName=%s)", ldb_dn_get_linearized(result->dn));
1108 if (ldb_ret != LDB_SUCCESS) {
1109 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
1110 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1114 switch (domain_res->count) {
1118 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1121 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1124 _dom = ldb_msg_find_attr_as_string(domain_res->msgs[0], "nETBIOSName", NULL);
1125 W_ERROR_HAVE_NO_MEMORY(_dom);
1127 _acc = ldb_msg_find_attr_as_string(result, "sAMAccountName", NULL);
1129 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1132 if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx, SID_BUILTIN), sid)) {
1135 const char *attrs[] = { NULL };
1136 struct ldb_result *domain_res2;
1137 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
1141 dom_sid->num_auths--;
1142 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
1146 "(&(objectSid=%s)(objectClass=domain))",
1147 ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
1149 if (ldb_ret != LDB_SUCCESS) {
1150 DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s\n", ldb_errstring(sam_ctx)));
1151 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1155 switch (domain_res->count) {
1159 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1162 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1166 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res2,
1170 "(ncName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
1172 if (ldb_ret != LDB_SUCCESS) {
1173 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
1174 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1178 switch (domain_res2->count) {
1182 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1185 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1188 _dom = ldb_msg_find_attr_as_string(domain_res2->msgs[0], "nETBIOSName", NULL);
1189 W_ERROR_HAVE_NO_MEMORY(_dom);
1193 info1->result_name = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
1194 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1196 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1199 case DRSUAPI_DS_NAME_FORMAT_GUID: {
1202 guid = samdb_result_guid(result, "objectGUID");
1204 info1->result_name = GUID_string2(mem_ctx, &guid);
1205 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1207 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1210 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
1211 info1->result_name = ldb_msg_find_attr_as_string(result, "displayName", NULL);
1212 if (!info1->result_name) {
1213 info1->result_name = ldb_msg_find_attr_as_string(result, "sAMAccountName", NULL);
1215 if (!info1->result_name) {
1216 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1218 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1222 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
1223 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1226 case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN:
1227 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
1228 info1->dns_domain_name = NULL;
1229 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1233 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1238 /* Given a user Principal Name (such as foo@bar.com),
1239 * return the user and domain DNs. This is used in the KDC to then
1240 * return the Keys and evaluate policy */
1242 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx,
1243 TALLOC_CTX *mem_ctx,
1244 const char *user_principal_name,
1245 struct ldb_dn **user_dn,
1246 struct ldb_dn **domain_dn)
1249 struct drsuapi_DsNameInfo1 info1;
1250 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1251 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
1252 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1253 user_principal_name,
1255 if (!W_ERROR_IS_OK(werr)) {
1256 return werror_to_ntstatus(werr);
1258 switch (info1.status) {
1259 case DRSUAPI_DS_NAME_STATUS_OK:
1261 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1262 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1263 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1264 return NT_STATUS_NO_SUCH_USER;
1265 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1267 return NT_STATUS_UNSUCCESSFUL;
1270 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1273 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1274 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1275 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1276 talloc_asprintf(mem_ctx, "%s/",
1277 info1.dns_domain_name),
1279 if (!W_ERROR_IS_OK(werr)) {
1280 return werror_to_ntstatus(werr);
1282 switch (info1.status) {
1283 case DRSUAPI_DS_NAME_STATUS_OK:
1285 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1286 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1287 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1288 return NT_STATUS_NO_SUCH_USER;
1289 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1291 return NT_STATUS_UNSUCCESSFUL;
1294 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1297 return NT_STATUS_OK;
1300 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1301 * return the user and domain DNs. This is used in the KDC to then
1302 * return the Keys and evaluate policy */
1304 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx,
1305 TALLOC_CTX *mem_ctx,
1306 const char *service_principal_name,
1307 struct ldb_dn **user_dn,
1308 struct ldb_dn **domain_dn)
1311 struct drsuapi_DsNameInfo1 info1;
1312 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1313 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
1314 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1315 service_principal_name,
1317 if (!W_ERROR_IS_OK(werr)) {
1318 return werror_to_ntstatus(werr);
1320 switch (info1.status) {
1321 case DRSUAPI_DS_NAME_STATUS_OK:
1323 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1324 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1325 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1326 return NT_STATUS_NO_SUCH_USER;
1327 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1329 return NT_STATUS_UNSUCCESSFUL;
1332 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1335 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1336 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1337 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1338 talloc_asprintf(mem_ctx, "%s/",
1339 info1.dns_domain_name),
1341 if (!W_ERROR_IS_OK(werr)) {
1342 return werror_to_ntstatus(werr);
1344 switch (info1.status) {
1345 case DRSUAPI_DS_NAME_STATUS_OK:
1347 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1348 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1349 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1350 return NT_STATUS_NO_SUCH_USER;
1351 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1353 return NT_STATUS_UNSUCCESSFUL;
1356 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1359 return NT_STATUS_OK;
1362 NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1363 struct tevent_context *ev_ctx,
1364 struct loadparm_context *lp_ctx,
1365 enum drsuapi_DsNameFormat format_offered,
1367 const char **nt4_domain, const char **nt4_account)
1370 struct drsuapi_DsNameInfo1 info1;
1371 struct ldb_context *ldb;
1374 /* Handle anonymous bind */
1375 if (!name || !*name) {
1378 return NT_STATUS_OK;
1381 ldb = samdb_connect(mem_ctx, ev_ctx, lp_ctx, system_session(lp_ctx), 0);
1383 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1386 werr = DsCrackNameOneName(ldb, mem_ctx, 0,
1388 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
1391 if (!W_ERROR_IS_OK(werr)) {
1392 return werror_to_ntstatus(werr);
1394 switch (info1.status) {
1395 case DRSUAPI_DS_NAME_STATUS_OK:
1397 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1398 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1399 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1400 return NT_STATUS_NO_SUCH_USER;
1401 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1403 return NT_STATUS_UNSUCCESSFUL;
1406 *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
1407 if (*nt4_domain == NULL) {
1408 return NT_STATUS_NO_MEMORY;
1411 p = strchr(*nt4_domain, '\\');
1413 return NT_STATUS_INVALID_PARAMETER;
1417 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
1418 if (*nt4_account == NULL) {
1419 return NT_STATUS_NO_MEMORY;
1422 return NT_STATUS_OK;
1425 NTSTATUS crack_auto_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1426 struct tevent_context *ev_ctx,
1427 struct loadparm_context *lp_ctx,
1429 const char **nt4_domain,
1430 const char **nt4_account)
1432 enum drsuapi_DsNameFormat format_offered = DRSUAPI_DS_NAME_FORMAT_UNKNOWN;
1434 /* Handle anonymous bind */
1435 if (!name || !*name) {
1438 return NT_STATUS_OK;
1441 if (strchr_m(name, '=')) {
1442 format_offered = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
1443 } else if (strchr_m(name, '@')) {
1444 format_offered = DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL;
1445 } else if (strchr_m(name, '\\')) {
1446 format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
1447 } else if (strchr_m(name, '/')) {
1448 format_offered = DRSUAPI_DS_NAME_FORMAT_CANONICAL;
1450 return NT_STATUS_NO_SUCH_USER;
1453 return crack_name_to_nt4_name(mem_ctx, ev_ctx, lp_ctx, format_offered, name, nt4_domain, nt4_account);
1457 WERROR dcesrv_drsuapi_ListRoles(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1458 const struct drsuapi_DsNameRequest1 *req1,
1459 struct drsuapi_DsNameCtr1 **ctr1)
1461 struct drsuapi_DsNameInfo1 *names;
1463 uint32_t count = 5;/*number of fsmo role owners we are going to return*/
1465 *ctr1 = talloc(mem_ctx, struct drsuapi_DsNameCtr1);
1466 W_ERROR_HAVE_NO_MEMORY(*ctr1);
1467 names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
1468 W_ERROR_HAVE_NO_MEMORY(names);
1470 for (i = 0; i < count; i++) {
1472 struct ldb_dn *role_owner_dn, *fsmo_role_dn, *server_dn;
1473 werr = dsdb_get_fsmo_role_info(mem_ctx, sam_ctx, i,
1474 &fsmo_role_dn, &role_owner_dn);
1475 if(!W_ERROR_IS_OK(werr)) {
1478 server_dn = ldb_dn_copy(mem_ctx, role_owner_dn);
1479 ldb_dn_remove_child_components(server_dn, 1);
1480 names[i].status = DRSUAPI_DS_NAME_STATUS_OK;
1481 names[i].dns_domain_name = samdb_dn_to_dnshostname(sam_ctx, mem_ctx,
1483 if(!names[i].dns_domain_name) {
1484 DEBUG(4, ("list_roles: Failed to find dNSHostName for server %s\n",
1485 ldb_dn_get_linearized(server_dn)));
1487 names[i].result_name = talloc_strdup(mem_ctx, ldb_dn_get_linearized(role_owner_dn));
1490 (*ctr1)->count = count;
1491 (*ctr1)->array = names;
1496 WERROR dcesrv_drsuapi_CrackNamesByNameFormat(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1497 const struct drsuapi_DsNameRequest1 *req1,
1498 struct drsuapi_DsNameCtr1 **ctr1)
1500 struct drsuapi_DsNameInfo1 *names;
1504 *ctr1 = talloc_zero(mem_ctx, struct drsuapi_DsNameCtr1);
1505 W_ERROR_HAVE_NO_MEMORY(*ctr1);
1507 count = req1->count;
1508 names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
1509 W_ERROR_HAVE_NO_MEMORY(names);
1511 for (i=0; i < count; i++) {
1512 status = DsCrackNameOneName(sam_ctx, mem_ctx,
1514 req1->format_offered,
1515 req1->format_desired,
1518 if (!W_ERROR_IS_OK(status)) {
1523 (*ctr1)->count = count;
1524 (*ctr1)->array = names;
1529 WERROR dcesrv_drsuapi_ListInfoServer(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1530 const struct drsuapi_DsNameRequest1 *req1,
1531 struct drsuapi_DsNameCtr1 **_ctr1)
1533 struct drsuapi_DsNameInfo1 *names;
1534 struct ldb_result *res;
1535 struct ldb_dn *server_dn, *dn;
1536 struct drsuapi_DsNameCtr1 *ctr1;
1539 const char *attrs[] = {
1548 ctr1 = talloc_zero(mem_ctx, struct drsuapi_DsNameCtr1);
1549 W_ERROR_HAVE_NO_MEMORY(ctr1);
1552 * No magic value here, we have to return 3 entries according to the
1556 names = talloc_zero_array(ctr1, struct drsuapi_DsNameInfo1,
1558 W_ERROR_HAVE_NO_MEMORY(names);
1559 ctr1->array = names;
1561 for (i=0; i < ctr1->count; i++) {
1562 names[i].status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1566 if (req1->count != 1) {
1567 DEBUG(1, ("Expected a count of 1 for the ListInfoServer crackname \n"));
1571 if (req1->names[0].str == NULL) {
1575 server_dn = ldb_dn_new(mem_ctx, sam_ctx, req1->names[0].str);
1576 W_ERROR_HAVE_NO_MEMORY(server_dn);
1578 ret = ldb_search(sam_ctx, mem_ctx, &res, server_dn, LDB_SCOPE_ONELEVEL,
1579 NULL, "(objectClass=nTDSDSA)");
1581 if (ret != LDB_SUCCESS) {
1582 DEBUG(1, ("Search for objectClass=nTDSDSA "
1583 "returned less than 1 objects\n"));
1587 if (res->count != 1) {
1588 DEBUG(1, ("Search for objectClass=nTDSDSA "
1589 "returned less than 1 objects\n"));
1593 if (res->msgs[0]->dn) {
1594 names[0].result_name = ldb_dn_alloc_linearized(names, res->msgs[0]->dn);
1595 W_ERROR_HAVE_NO_MEMORY(names[0].result_name);
1596 names[0].status = DRSUAPI_DS_NAME_STATUS_OK;
1601 ret = ldb_search(sam_ctx, mem_ctx, &res, server_dn, LDB_SCOPE_BASE,
1602 attrs, "(objectClass=*)");
1603 if (ret != LDB_SUCCESS) {
1604 DEBUG(1, ("Search for objectClass=* on dn %s"
1605 "returned %s\n", req1->names[0].str,
1606 ldb_strerror(ret)));
1610 if (res->count != 1) {
1611 DEBUG(1, ("Search for objectClass=* on dn %s"
1612 "returned less than 1 objects\n", req1->names[0].str));
1616 str = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
1618 names[1].result_name = talloc_strdup(names, str);
1619 W_ERROR_HAVE_NO_MEMORY(names[1].result_name);
1620 names[1].status = DRSUAPI_DS_NAME_STATUS_OK;
1623 dn = ldb_msg_find_attr_as_dn(sam_ctx, mem_ctx, res->msgs[0], "serverReference");
1625 names[2].result_name = ldb_dn_alloc_linearized(names, dn);
1626 W_ERROR_HAVE_NO_MEMORY(names[2].result_name);
1627 names[2].status = DRSUAPI_DS_NAME_STATUS_OK;