2 Unix SMB/CIFS implementation.
4 endpoint server for the drsuapi pipe
7 Copyright (C) Stefan Metzmacher 2004
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "librpc/gen_ndr/drsuapi.h"
27 #include "rpc_server/common/common.h"
28 #include "lib/ldb/include/ldb_errors.h"
29 #include "system/kerberos.h"
30 #include "auth/kerberos/kerberos.h"
31 #include "libcli/ldap/ldap.h"
32 #include "libcli/security/security.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "auth/auth.h"
36 #include "dsdb/samdb/samdb.h"
38 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
39 struct smb_krb5_context *smb_krb5_context,
40 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
41 struct ldb_dn *name_dn, const char *name,
42 const char *domain_filter, const char *result_filter,
43 struct drsuapi_DsNameInfo1 *info1);
44 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
45 uint32_t format_offered, uint32_t format_desired,
46 struct ldb_dn *name_dn, const char *name,
47 struct drsuapi_DsNameInfo1 *info1);
49 static enum drsuapi_DsNameStatus LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx,
51 const char *alias_from,
56 struct ldb_result *res;
57 struct ldb_message_element *spnmappings;
59 struct ldb_dn *service_dn;
62 const char *directory_attrs[] = {
67 tmp_ctx = talloc_new(mem_ctx);
69 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
72 service_dn = ldb_dn_new(tmp_ctx, ldb_ctx, "CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration");
73 if ( ! ldb_dn_add_base(service_dn, samdb_base_dn(ldb_ctx))) {
74 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
76 service_dn_str = ldb_dn_alloc_linearized(tmp_ctx, service_dn);
77 if ( ! service_dn_str) {
78 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
81 ret = ldb_search(ldb_ctx, service_dn, LDB_SCOPE_BASE, "(objectClass=nTDSService)",
82 directory_attrs, &res);
84 if (ret != LDB_SUCCESS) {
85 DEBUG(1, ("ldb_search: dn: %s not found: %s", service_dn_str, ldb_errstring(ldb_ctx)));
86 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
87 } else if (res->count != 1) {
89 DEBUG(1, ("ldb_search: dn: %s found %d times!", service_dn_str, res->count));
90 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
92 talloc_steal(tmp_ctx, res);
94 spnmappings = ldb_msg_find_element(res->msgs[0], "sPNMappings");
95 if (!spnmappings || spnmappings->num_values == 0) {
96 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute", service_dn_str));
98 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
101 for (i = 0; i < spnmappings->num_values; i++) {
102 char *mapping, *p, *str;
103 mapping = talloc_strdup(tmp_ctx,
104 (const char *)spnmappings->values[i].data);
106 DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str));
107 talloc_free(tmp_ctx);
108 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
111 /* C string manipulation sucks */
113 p = strchr(mapping, '=');
115 DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n",
116 service_dn_str, mapping));
117 talloc_free(tmp_ctx);
118 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
129 if (strcasecmp(str, alias_from) == 0) {
131 talloc_steal(mem_ctx, mapping);
132 talloc_free(tmp_ctx);
133 return DRSUAPI_DS_NAME_STATUS_OK;
137 DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from));
138 talloc_free(tmp_ctx);
139 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
142 /* When cracking a ServicePrincipalName, many services may be served
143 * by the host/ servicePrincipalName. The incoming query is for cifs/
144 * but we translate it here, and search on host/. This is done after
145 * the cifs/ entry has been searched for, making this a fallback */
147 static WERROR DsCrackNameSPNAlias(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
148 struct smb_krb5_context *smb_krb5_context,
149 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
150 const char *name, struct drsuapi_DsNameInfo1 *info1)
154 krb5_principal principal;
158 enum drsuapi_DsNameStatus namestatus;
160 /* parse principal */
161 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context,
162 name, KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
164 DEBUG(2, ("Could not parse principal: %s: %s",
165 name, smb_get_krb5_error_message(smb_krb5_context->krb5_context,
170 /* grab cifs/, http/ etc */
172 /* This is checked for in callers, but be safe */
173 if (principal->name.name_string.len < 2) {
174 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
177 service = principal->name.name_string.val[0];
180 namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context,
182 service, &new_service);
184 if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
185 info1->status = namestatus;
190 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
194 /* ooh, very nasty playing around in the Principal... */
195 free(principal->name.name_string.val[0]);
196 principal->name.name_string.val[0] = strdup(new_service);
197 if (!principal->name.name_string.val[0]) {
198 krb5_free_principal(smb_krb5_context->krb5_context, principal);
202 /* reform principal */
203 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
204 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &new_princ);
206 krb5_free_principal(smb_krb5_context->krb5_context, principal);
212 wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
218 /* Subcase of CrackNames, for the userPrincipalName */
220 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
221 struct smb_krb5_context *smb_krb5_context,
222 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
223 const char *name, struct drsuapi_DsNameInfo1 *info1)
226 const char *domain_filter = NULL;
227 const char *result_filter = NULL;
229 krb5_principal principal;
231 char *unparsed_name_short;
233 /* Prevent recursion */
235 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
239 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
240 KRB5_PRINCIPAL_PARSE_MUST_REALM, &principal);
242 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
246 domain_filter = NULL;
247 realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
248 domain_filter = talloc_asprintf(mem_ctx,
249 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
250 ldb_binary_encode_string(mem_ctx, *realm),
251 ldb_binary_encode_string(mem_ctx, *realm));
252 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
253 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
254 krb5_free_principal(smb_krb5_context->krb5_context, principal);
257 free(unparsed_name_short);
261 /* This may need to be extended for more userPrincipalName variations */
262 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
263 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
264 if (!result_filter || !domain_filter) {
265 free(unparsed_name_short);
268 status = DsCrackNameOneFilter(sam_ctx, mem_ctx,
270 format_flags, format_offered, format_desired,
271 NULL, unparsed_name_short, domain_filter, result_filter,
273 free(unparsed_name_short);
277 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
279 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
280 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
281 const char *name, struct drsuapi_DsNameInfo1 *info1)
284 const char *domain_filter = NULL;
285 const char *result_filter = NULL;
286 struct ldb_dn *name_dn = NULL;
288 struct smb_krb5_context *smb_krb5_context;
289 ret = smb_krb5_init_context(mem_ctx, &smb_krb5_context);
295 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
296 info1->dns_domain_name = NULL;
297 info1->result_name = NULL;
300 return WERR_INVALID_PARAM;
303 /* TODO: - fill the correct names in all cases!
304 * - handle format_flags
307 /* here we need to set the domain_filter and/or the result_filter */
308 switch (format_offered) {
309 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
310 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
312 char *str, *s, *account;
314 if (strlen(name) == 0) {
315 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
319 str = talloc_strdup(mem_ctx, name);
320 W_ERROR_HAVE_NO_MEMORY(str);
322 if (format_offered == DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX) {
323 /* Look backwards for the \n, and replace it with / */
324 s = strrchr(str, '\n');
326 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
332 s = strchr(str, '/');
334 /* there must be at least one / */
335 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
342 domain_filter = talloc_asprintf(mem_ctx, "(&(objectClass=crossRef)(ncName=%s))",
343 ldb_dn_get_linearized(samdb_dns_domain_to_dn(sam_ctx, mem_ctx, str)));
344 W_ERROR_HAVE_NO_MEMORY(domain_filter);
346 /* There may not be anything after the domain component (search for the domain itself) */
349 account = strrchr(s, '/');
355 account = ldb_binary_encode_string(mem_ctx, account);
356 W_ERROR_HAVE_NO_MEMORY(account);
357 result_filter = talloc_asprintf(mem_ctx, "(name=%s)",
359 W_ERROR_HAVE_NO_MEMORY(result_filter);
363 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
366 const char *account = NULL;
368 domain = talloc_strdup(mem_ctx, name);
369 W_ERROR_HAVE_NO_MEMORY(domain);
371 p = strchr(domain, '\\');
373 /* invalid input format */
374 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
383 domain_filter = talloc_asprintf(mem_ctx,
384 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
385 ldb_binary_encode_string(mem_ctx, domain));
386 W_ERROR_HAVE_NO_MEMORY(domain_filter);
388 result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
389 ldb_binary_encode_string(mem_ctx, account));
390 W_ERROR_HAVE_NO_MEMORY(result_filter);
397 /* A LDAP DN as a string */
398 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
399 domain_filter = NULL;
400 name_dn = ldb_dn_new(mem_ctx, sam_ctx, name);
401 if (! ldb_dn_validate(name_dn)) {
402 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
408 /* A GUID as a string */
409 case DRSUAPI_DS_NAME_FORMAT_GUID: {
413 domain_filter = NULL;
415 nt_status = GUID_from_string(name, &guid);
416 if (!NT_STATUS_IS_OK(nt_status)) {
417 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
421 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
425 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
427 W_ERROR_HAVE_NO_MEMORY(result_filter);
430 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
431 domain_filter = NULL;
433 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
434 ldb_binary_encode_string(mem_ctx, name),
435 ldb_binary_encode_string(mem_ctx, name));
436 W_ERROR_HAVE_NO_MEMORY(result_filter);
440 /* A S-1234-5678 style string */
441 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
442 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
445 domain_filter = NULL;
447 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
450 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx,
455 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
457 W_ERROR_HAVE_NO_MEMORY(result_filter);
460 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
461 krb5_principal principal;
463 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
465 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
469 domain_filter = NULL;
471 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
473 krb5_free_principal(smb_krb5_context->krb5_context, principal);
477 krb5_free_principal(smb_krb5_context->krb5_context, principal);
478 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))",
479 ldb_binary_encode_string(mem_ctx, unparsed_name));
482 W_ERROR_HAVE_NO_MEMORY(result_filter);
485 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
486 krb5_principal principal;
487 char *unparsed_name_short;
489 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
490 KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
492 /* perhaps it's a principal with a realm, so return the right 'domain only' response */
494 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
495 KRB5_PRINCIPAL_PARSE_MUST_REALM, &principal);
497 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
501 /* This isn't an allocation assignemnt, so it is free'ed with the krb5_free_principal */
502 realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
504 info1->dns_domain_name = talloc_strdup(info1, *realm);
505 krb5_free_principal(smb_krb5_context->krb5_context, principal);
507 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
509 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
512 } else if (principal->name.name_string.len < 2) {
513 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
517 domain_filter = NULL;
519 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
520 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
522 krb5_free_principal(smb_krb5_context->krb5_context, principal);
526 service = principal->name.name_string.val[0];
527 if ((principal->name.name_string.len == 2) && (strcasecmp(service, "host") == 0)) {
528 /* the 'cn' attribute is just the leading part of the name */
530 computer_name = talloc_strndup(mem_ctx, principal->name.name_string.val[1],
531 strcspn(principal->name.name_string.val[1], "."));
532 if (computer_name == NULL) {
536 result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
537 ldb_binary_encode_string(mem_ctx, unparsed_name_short),
538 ldb_binary_encode_string(mem_ctx, computer_name));
540 result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
541 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
543 krb5_free_principal(smb_krb5_context->krb5_context, principal);
544 free(unparsed_name_short);
545 W_ERROR_HAVE_NO_MEMORY(result_filter);
550 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
556 if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
557 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
558 name_dn, name, info1);
561 return DsCrackNameOneFilter(sam_ctx, mem_ctx,
563 format_flags, format_offered, format_desired,
565 domain_filter, result_filter,
569 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
570 * (FQDN_1779) into a canoical name without actually searching the
573 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
574 uint32_t format_offered, uint32_t format_desired,
575 struct ldb_dn *name_dn, const char *name,
576 struct drsuapi_DsNameInfo1 *info1)
579 if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
580 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
584 switch (format_desired) {
585 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
586 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
588 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
589 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
592 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
595 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
596 info1->result_name = cracked;
604 /* Given a filter for the domain, and one for the result, perform the
605 * ldb search. The format offered and desired flags change the
606 * behaviours, including what attributes to return.
608 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
611 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
612 struct smb_krb5_context *smb_krb5_context,
613 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
614 struct ldb_dn *name_dn, const char *name,
615 const char *domain_filter, const char *result_filter,
616 struct drsuapi_DsNameInfo1 *info1)
619 struct ldb_message **domain_res = NULL;
620 const char * const *domain_attrs;
621 const char * const *result_attrs;
622 struct ldb_message **result_res = NULL;
623 struct ldb_message *result = NULL;
624 struct ldb_dn *result_basedn;
625 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
628 const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
629 const char * const _result_attrs_null[] = { NULL };
631 const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
632 const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
634 const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
635 const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", "objectClass", NULL};
637 const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
638 const char * const _result_attrs_guid[] = { "objectGUID", NULL};
640 const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
641 const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
643 /* here we need to set the attrs lists for domain and result lookups */
644 switch (format_desired) {
645 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
646 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
647 domain_attrs = _domain_attrs_1779;
648 result_attrs = _result_attrs_null;
650 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
651 domain_attrs = _domain_attrs_canonical;
652 result_attrs = _result_attrs_canonical;
654 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
655 domain_attrs = _domain_attrs_nt4;
656 result_attrs = _result_attrs_nt4;
658 case DRSUAPI_DS_NAME_FORMAT_GUID:
659 domain_attrs = _domain_attrs_guid;
660 result_attrs = _result_attrs_guid;
662 case DRSUAPI_DS_NAME_FORMAT_DISPLAY:
663 domain_attrs = _domain_attrs_display;
664 result_attrs = _result_attrs_display;
671 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
672 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res, domain_attrs,
673 "%s", domain_filter);
675 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res, domain_attrs,
676 "(ncName=%s)", ldb_dn_get_linearized(samdb_base_dn(sam_ctx)));
683 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
686 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
687 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
690 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
694 info1->dns_domain_name = samdb_result_string(domain_res[0], "dnsRoot", NULL);
695 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
696 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
699 result_basedn = samdb_result_dn(sam_ctx, mem_ctx, domain_res[0], "ncName", NULL);
701 ldb_ret = gendb_search(sam_ctx, mem_ctx, result_basedn, &result_res,
702 result_attrs, "%s", result_filter);
703 } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
704 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
707 name_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res[0], "ncName", NULL);
708 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
714 result = result_res[0];
717 switch (format_offered) {
718 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL:
719 return DsCrackNameSPNAlias(sam_ctx, mem_ctx,
721 format_flags, format_offered, format_desired,
724 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
725 return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context,
726 format_flags, format_offered, format_desired,
729 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
732 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s", ldb_errstring(sam_ctx)));
733 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
736 switch (format_offered) {
737 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
738 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
740 const char *canonical_name;
741 /* We may need to manually filter further */
742 for (i = 0; i < ldb_ret; i++) {
743 switch (format_offered) {
744 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
745 canonical_name = ldb_dn_canonical_string(mem_ctx, result_res[i]->dn);
747 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
748 canonical_name = ldb_dn_canonical_ex_string(mem_ctx, result_res[i]->dn);
751 if (strcasecmp_m(canonical_name, name) == 0) {
752 result = result_res[i];
757 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
762 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
767 /* here we can use result and domain_res[0] */
768 switch (format_desired) {
769 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
770 info1->result_name = ldb_dn_alloc_linearized(mem_ctx, result->dn);
771 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
773 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
776 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
777 info1->result_name = samdb_result_string(result, "canonicalName", NULL);
778 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
781 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
782 /* Not in the virtual ldb attribute */
783 return DsCrackNameOneSyntactical(mem_ctx,
784 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
785 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
786 result->dn, name, info1);
788 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
790 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result, "objectSid");
791 const char *_acc = "", *_dom = "";
793 if (samdb_find_attribute(sam_ctx, result, "objectClass", "domain")) {
794 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res, domain_attrs,
795 "(ncName=%s)", ldb_dn_get_linearized(result->dn));
797 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
800 _dom = samdb_result_string(domain_res[0], "nETBIOSName", NULL);
801 W_ERROR_HAVE_NO_MEMORY(_dom);
803 _acc = samdb_result_string(result, "sAMAccountName", NULL);
805 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
808 if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx, SID_BUILTIN), sid)) {
811 const char *attrs[] = { NULL };
812 struct ldb_message **domain_res2;
813 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
817 dom_sid->num_auths--;
818 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, attrs,
819 "(&(objectSid=%s)(objectClass=domain))", ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
821 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
824 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res2, domain_attrs,
825 "(ncName=%s)", ldb_dn_get_linearized(domain_res[0]->dn));
827 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
831 _dom = samdb_result_string(domain_res2[0], "nETBIOSName", NULL);
832 W_ERROR_HAVE_NO_MEMORY(_dom);
836 info1->result_name = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
837 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
839 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
842 case DRSUAPI_DS_NAME_FORMAT_GUID: {
845 guid = samdb_result_guid(result, "objectGUID");
847 info1->result_name = GUID_string2(mem_ctx, &guid);
848 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
850 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
853 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
854 info1->result_name = samdb_result_string(result, "displayName", NULL);
855 if (!info1->result_name) {
856 info1->result_name = samdb_result_string(result, "sAMAccountName", NULL);
858 if (!info1->result_name) {
859 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
861 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
866 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
870 return WERR_INVALID_PARAM;
873 /* Given a user Principal Name (such as foo@bar.com),
874 * return the user and domain DNs. This is used in the KDC to then
875 * return the Keys and evaluate policy */
877 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx,
879 const char *user_principal_name,
880 struct ldb_dn **user_dn,
881 struct ldb_dn **domain_dn)
884 struct drsuapi_DsNameInfo1 info1;
885 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
886 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
887 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
890 if (!W_ERROR_IS_OK(werr)) {
891 return werror_to_ntstatus(werr);
893 switch (info1.status) {
894 case DRSUAPI_DS_NAME_STATUS_OK:
896 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
897 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
898 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
899 return NT_STATUS_NO_SUCH_USER;
900 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
902 return NT_STATUS_UNSUCCESSFUL;
905 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
908 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
909 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
910 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
911 talloc_asprintf(mem_ctx, "%s/",
912 info1.dns_domain_name),
914 if (!W_ERROR_IS_OK(werr)) {
915 return werror_to_ntstatus(werr);
917 switch (info1.status) {
918 case DRSUAPI_DS_NAME_STATUS_OK:
920 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
921 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
922 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
923 return NT_STATUS_NO_SUCH_USER;
924 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
926 return NT_STATUS_UNSUCCESSFUL;
929 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
936 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
937 * return the user and domain DNs. This is used in the KDC to then
938 * return the Keys and evaluate policy */
940 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx,
942 const char *service_principal_name,
943 struct ldb_dn **user_dn,
944 struct ldb_dn **domain_dn)
947 struct drsuapi_DsNameInfo1 info1;
948 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
949 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
950 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
951 service_principal_name,
953 if (!W_ERROR_IS_OK(werr)) {
954 return werror_to_ntstatus(werr);
956 switch (info1.status) {
957 case DRSUAPI_DS_NAME_STATUS_OK:
959 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
960 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
961 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
962 return NT_STATUS_NO_SUCH_USER;
963 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
965 return NT_STATUS_UNSUCCESSFUL;
968 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
971 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
972 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
973 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
974 talloc_asprintf(mem_ctx, "%s/",
975 info1.dns_domain_name),
977 if (!W_ERROR_IS_OK(werr)) {
978 return werror_to_ntstatus(werr);
980 switch (info1.status) {
981 case DRSUAPI_DS_NAME_STATUS_OK:
983 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
984 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
985 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
986 return NT_STATUS_NO_SUCH_USER;
987 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
989 return NT_STATUS_UNSUCCESSFUL;
992 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
999 NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1000 uint32_t format_offered,
1002 const char **nt4_domain, const char **nt4_account)
1005 struct drsuapi_DsNameInfo1 info1;
1006 struct ldb_context *ldb;
1009 /* Handle anonymous bind */
1010 if (!name || !*name) {
1013 return NT_STATUS_OK;
1016 ldb = samdb_connect(mem_ctx, system_session(mem_ctx));
1018 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1021 werr = DsCrackNameOneName(ldb, mem_ctx, 0,
1023 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
1026 if (!W_ERROR_IS_OK(werr)) {
1027 return werror_to_ntstatus(werr);
1029 switch (info1.status) {
1030 case DRSUAPI_DS_NAME_STATUS_OK:
1032 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1033 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1034 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1035 return NT_STATUS_NO_SUCH_USER;
1036 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1038 return NT_STATUS_UNSUCCESSFUL;
1041 *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
1043 p = strchr(*nt4_domain, '\\');
1045 return NT_STATUS_INVALID_PARAMETER;
1050 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
1053 if (!*nt4_account || !*nt4_domain) {
1054 return NT_STATUS_NO_MEMORY;
1057 return NT_STATUS_OK;
1060 NTSTATUS crack_auto_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1062 const char **nt4_domain,
1063 const char **nt4_account)
1065 uint32_t format_offered = DRSUAPI_DS_NAME_FORMAT_UKNOWN;
1067 /* Handle anonymous bind */
1068 if (!name || !*name) {
1071 return NT_STATUS_OK;
1074 if (strchr_m(name, '=')) {
1075 format_offered = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
1076 } else if (strchr_m(name, '@')) {
1077 format_offered = DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL;
1078 } else if (strchr_m(name, '\\')) {
1079 format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
1080 } else if (strchr_m(name, '/')) {
1081 format_offered = DRSUAPI_DS_NAME_FORMAT_CANONICAL;
1084 return crack_name_to_nt4_name(mem_ctx, format_offered, name, nt4_domain, nt4_account);