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 const 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 const 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_string_compose(tmp_ctx, NULL,
73 "CN=Directory Service,CN=Windows NT"
74 ",CN=Services,CN=Configuration");
75 service_dn_str = ldb_dn_linearize(tmp_ctx, service_dn);
77 ret = ldb_search(ldb_ctx, service_dn, LDB_SCOPE_BASE, "(objectClass=nTDSService)",
78 directory_attrs, &res);
80 if (ret != LDB_SUCCESS) {
81 DEBUG(1, ("ldb_search: dn: %s not found: %s", service_dn_str, ldb_errstring(ldb_ctx)));
82 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
83 } else if (res->count != 1) {
85 DEBUG(1, ("ldb_search: dn: %s found %d times!", service_dn_str, res->count));
86 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
88 talloc_steal(tmp_ctx, res);
90 spnmappings = ldb_msg_find_element(res->msgs[0], "sPNMappings");
91 if (!spnmappings || spnmappings->num_values == 0) {
92 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute", service_dn_str));
94 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
97 for (i = 0; i < spnmappings->num_values; i++) {
98 char *mapping, *p, *str;
99 mapping = talloc_strdup(tmp_ctx,
100 (const char *)spnmappings->values[i].data);
102 DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str));
103 talloc_free(tmp_ctx);
104 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
107 /* C string manipulation sucks */
109 p = strchr(mapping, '=');
111 DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n",
112 service_dn_str, mapping));
113 talloc_free(tmp_ctx);
114 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
125 if (strcasecmp(str, alias_from) == 0) {
127 talloc_steal(mem_ctx, mapping);
128 talloc_free(tmp_ctx);
129 return DRSUAPI_DS_NAME_STATUS_OK;
133 DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from));
134 talloc_free(tmp_ctx);
135 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
138 /* When cracking a ServicePrincipalName, many services may be served
139 * by the host/ servicePrincipalName. The incoming query is for cifs/
140 * but we translate it here, and search on host/. This is done after
141 * the cifs/ entry has been searched for, making this a fallback */
143 static WERROR DsCrackNameSPNAlias(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
144 struct smb_krb5_context *smb_krb5_context,
145 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
146 const char *name, struct drsuapi_DsNameInfo1 *info1)
150 krb5_principal principal;
154 enum drsuapi_DsNameStatus namestatus;
156 /* parse principal */
157 ret = krb5_parse_name_norealm(smb_krb5_context->krb5_context,
160 DEBUG(2, ("Could not parse principal: %s: %s",
161 name, smb_get_krb5_error_message(smb_krb5_context->krb5_context,
166 /* grab cifs/, http/ etc */
168 /* This is checked for in callers, but be safe */
169 if (principal->name.name_string.len < 2) {
170 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
173 service = principal->name.name_string.val[0];
176 namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context,
178 service, &new_service);
180 if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
181 info1->status = namestatus;
186 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
190 /* ooh, very nasty playing around in the Principal... */
191 free(principal->name.name_string.val[0]);
192 principal->name.name_string.val[0] = strdup(new_service);
193 if (!principal->name.name_string.val[0]) {
194 krb5_free_principal(smb_krb5_context->krb5_context, principal);
198 /* reform principal */
199 ret = krb5_unparse_name_norealm(smb_krb5_context->krb5_context, principal, &new_princ);
201 krb5_free_principal(smb_krb5_context->krb5_context, principal);
207 wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
213 /* Subcase of CrackNames, for the userPrincipalName */
215 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
216 struct smb_krb5_context *smb_krb5_context,
217 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
218 const char *name, struct drsuapi_DsNameInfo1 *info1)
221 const char *domain_filter = NULL;
222 const char *result_filter = NULL;
224 krb5_principal principal;
226 char *unparsed_name_short;
228 /* Prevent recursion */
230 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
234 ret = krb5_parse_name_mustrealm(smb_krb5_context->krb5_context, name, &principal);
236 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
240 domain_filter = NULL;
241 realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
242 domain_filter = talloc_asprintf(mem_ctx,
243 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
244 ldb_binary_encode_string(mem_ctx, *realm),
245 ldb_binary_encode_string(mem_ctx, *realm));
246 ret = krb5_unparse_name_norealm(smb_krb5_context->krb5_context, principal, &unparsed_name_short);
247 krb5_free_principal(smb_krb5_context->krb5_context, principal);
250 free(unparsed_name_short);
254 /* This may need to be extended for more userPrincipalName variations */
255 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
256 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
257 if (!result_filter || !domain_filter) {
258 free(unparsed_name_short);
261 status = DsCrackNameOneFilter(sam_ctx, mem_ctx,
263 format_flags, format_offered, format_desired,
264 NULL, unparsed_name_short, domain_filter, result_filter,
266 free(unparsed_name_short);
270 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
272 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
273 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
274 const char *name, struct drsuapi_DsNameInfo1 *info1)
277 const char *domain_filter = NULL;
278 const char *result_filter = NULL;
279 struct ldb_dn *name_dn = NULL;
281 struct smb_krb5_context *smb_krb5_context;
282 ret = smb_krb5_init_context(mem_ctx, &smb_krb5_context);
288 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
289 info1->dns_domain_name = NULL;
290 info1->result_name = NULL;
293 return WERR_INVALID_PARAM;
296 /* TODO: - fill the correct names in all cases!
297 * - handle format_flags
300 /* here we need to set the domain_filter and/or the result_filter */
301 switch (format_offered) {
302 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
305 str = talloc_strdup(mem_ctx, name);
306 W_ERROR_HAVE_NO_MEMORY(str);
308 if (strlen(str) == 0 || str[strlen(str)-1] != '/') {
309 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
313 str[strlen(str)-1] = '\0';
315 domain_filter = talloc_asprintf(mem_ctx,
316 "(&(&(&(dnsRoot=%s)(objectclass=crossRef)))(nETBIOSName=*)(ncName=*))",
317 ldb_binary_encode_string(mem_ctx, str));
318 W_ERROR_HAVE_NO_MEMORY(domain_filter);
322 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
325 const char *account = NULL;
327 domain = talloc_strdup(mem_ctx, name);
328 W_ERROR_HAVE_NO_MEMORY(domain);
330 p = strchr(domain, '\\');
332 /* invalid input format */
333 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
342 domain_filter = talloc_asprintf(mem_ctx,
343 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
344 ldb_binary_encode_string(mem_ctx, domain));
345 W_ERROR_HAVE_NO_MEMORY(domain_filter);
347 result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
348 ldb_binary_encode_string(mem_ctx, account));
349 W_ERROR_HAVE_NO_MEMORY(result_filter);
356 /* A LDAP DN as a string */
357 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
358 name_dn = ldb_dn_explode(mem_ctx, name);
359 domain_filter = NULL;
361 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
367 /* A GUID as a string */
368 case DRSUAPI_DS_NAME_FORMAT_GUID: {
372 domain_filter = NULL;
374 nt_status = GUID_from_string(name, &guid);
375 if (!NT_STATUS_IS_OK(nt_status)) {
376 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
380 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
384 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
386 W_ERROR_HAVE_NO_MEMORY(result_filter);
389 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
390 domain_filter = NULL;
392 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
393 ldb_binary_encode_string(mem_ctx, name),
394 ldb_binary_encode_string(mem_ctx, name));
395 W_ERROR_HAVE_NO_MEMORY(result_filter);
399 /* A S-1234-5678 style string */
400 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
401 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
404 domain_filter = NULL;
406 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
409 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx,
414 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
416 W_ERROR_HAVE_NO_MEMORY(result_filter);
419 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
420 krb5_principal principal;
422 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
424 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
428 domain_filter = NULL;
430 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
432 krb5_free_principal(smb_krb5_context->krb5_context, principal);
436 krb5_free_principal(smb_krb5_context->krb5_context, principal);
437 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))",
438 ldb_binary_encode_string(mem_ctx, unparsed_name));
441 W_ERROR_HAVE_NO_MEMORY(result_filter);
444 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
445 krb5_principal principal;
446 char *unparsed_name_short;
448 ret = krb5_parse_name_norealm(smb_krb5_context->krb5_context, name, &principal);
450 /* perhaps it's a principal with a realm, so return the right 'domain only' response */
452 ret = krb5_parse_name_mustrealm(smb_krb5_context->krb5_context, name, &principal);
454 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
458 /* This isn't an allocation assignemnt, so it is free'ed with the krb5_free_principal */
459 realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
461 info1->dns_domain_name = talloc_strdup(info1, *realm);
462 krb5_free_principal(smb_krb5_context->krb5_context, principal);
464 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
466 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
469 } else if (principal->name.name_string.len < 2) {
470 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
474 domain_filter = NULL;
476 ret = krb5_unparse_name_norealm(smb_krb5_context->krb5_context, principal, &unparsed_name_short);
478 krb5_free_principal(smb_krb5_context->krb5_context, principal);
482 service = principal->name.name_string.val[0];
483 if ((principal->name.name_string.len == 2) && (strcasecmp(service, "host") == 0)) {
484 /* the 'cn' attribute is just the leading part of the name */
486 computer_name = talloc_strndup(mem_ctx, principal->name.name_string.val[1],
487 strcspn(principal->name.name_string.val[1], "."));
488 if (computer_name == NULL) {
492 result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
493 ldb_binary_encode_string(mem_ctx, unparsed_name_short),
494 ldb_binary_encode_string(mem_ctx, computer_name));
496 result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
497 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
499 krb5_free_principal(smb_krb5_context->krb5_context, principal);
500 free(unparsed_name_short);
501 W_ERROR_HAVE_NO_MEMORY(result_filter);
506 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
512 if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
513 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
514 name_dn, name, info1);
517 return DsCrackNameOneFilter(sam_ctx, mem_ctx,
519 format_flags, format_offered, format_desired,
521 domain_filter, result_filter,
525 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
526 * (FQDN_1779) into a canoical name without actually searching the
529 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
530 uint32_t format_offered, uint32_t format_desired,
531 const struct ldb_dn *name_dn, const char *name,
532 struct drsuapi_DsNameInfo1 *info1)
535 if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
536 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
540 switch (format_desired) {
541 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
542 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
544 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
545 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
548 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
551 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
552 info1->result_name = cracked;
560 /* Given a filter for the domain, and one for the result, perform the
561 * ldb search. The format offered and desired flags change the
562 * behaviours, including what attributes to return.
564 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
567 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
568 struct smb_krb5_context *smb_krb5_context,
569 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
570 const struct ldb_dn *name_dn, const char *name,
571 const char *domain_filter, const char *result_filter,
572 struct drsuapi_DsNameInfo1 *info1)
575 struct ldb_message **domain_res = NULL;
576 const char * const *domain_attrs;
577 const char * const *result_attrs;
578 struct ldb_message **result_res = NULL;
579 const struct ldb_dn *result_basedn;
580 const struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
582 const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
583 const char * const _result_attrs_null[] = { NULL };
585 const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
586 const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
588 const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
589 const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", NULL};
591 const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
592 const char * const _result_attrs_guid[] = { "objectGUID", NULL};
594 const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
595 const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
597 /* here we need to set the attrs lists for domain and result lookups */
598 switch (format_desired) {
599 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
600 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
601 domain_attrs = _domain_attrs_1779;
602 result_attrs = _result_attrs_null;
604 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
605 domain_attrs = _domain_attrs_canonical;
606 result_attrs = _result_attrs_canonical;
608 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
609 domain_attrs = _domain_attrs_nt4;
610 result_attrs = _result_attrs_nt4;
612 case DRSUAPI_DS_NAME_FORMAT_GUID:
613 domain_attrs = _domain_attrs_guid;
614 result_attrs = _result_attrs_guid;
616 case DRSUAPI_DS_NAME_FORMAT_DISPLAY:
617 domain_attrs = _domain_attrs_display;
618 result_attrs = _result_attrs_display;
625 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
626 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res, domain_attrs,
627 "%s", domain_filter);
629 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res, domain_attrs,
630 "(ncName=%s)", ldb_dn_linearize(mem_ctx, samdb_base_dn(sam_ctx)));
637 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
640 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
643 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
647 info1->dns_domain_name = samdb_result_string(domain_res[0], "dnsRoot", NULL);
648 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
649 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
652 result_basedn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
654 ldb_ret = gendb_search(sam_ctx, mem_ctx, result_basedn, &result_res,
655 result_attrs, "%s", result_filter);
656 } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
657 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
660 name_dn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
661 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
669 switch (format_offered) {
670 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL:
671 return DsCrackNameSPNAlias(sam_ctx, mem_ctx,
673 format_flags, format_offered, format_desired,
676 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
677 return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context,
678 format_flags, format_offered, format_desired,
681 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
684 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
687 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
691 /* here we can use result_res[0] and domain_res[0] */
692 switch (format_desired) {
693 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
694 info1->result_name = ldb_dn_linearize(mem_ctx, result_res[0]->dn);
695 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
697 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
700 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
701 info1->result_name = samdb_result_string(result_res[0], "canonicalName", NULL);
702 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
705 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
706 /* Not in the virtual ldb attribute */
707 return DsCrackNameOneSyntactical(mem_ctx,
708 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
709 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
710 result_res[0]->dn, name, info1);
712 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
713 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result_res[0], "objectSid");
714 const char *_acc = "", *_dom = "";
716 if (!sid || (sid->num_auths < 4) || (sid->num_auths > 5)) {
717 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
721 if (sid->num_auths == 4) {
722 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res, domain_attrs,
723 "(ncName=%s)", ldb_dn_linearize(mem_ctx, result_res[0]->dn));
725 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
728 _dom = samdb_result_string(domain_res[0], "nETBIOSName", NULL);
729 W_ERROR_HAVE_NO_MEMORY(_dom);
731 } else if (sid->num_auths == 5) {
732 const char *attrs[] = { NULL };
733 struct ldb_message **domain_res2;
734 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
738 dom_sid->num_auths--;
739 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, attrs,
740 "(&(objectSid=%s)(objectClass=domain))", ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
742 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
745 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res2, domain_attrs,
746 "(ncName=%s)", ldb_dn_linearize(mem_ctx, domain_res[0]->dn));
748 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
752 _dom = samdb_result_string(domain_res2[0], "nETBIOSName", NULL);
753 W_ERROR_HAVE_NO_MEMORY(_dom);
755 _acc = samdb_result_string(result_res[0], "sAMAccountName", NULL);
756 W_ERROR_HAVE_NO_MEMORY(_acc);
759 info1->result_name = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
760 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
762 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
765 case DRSUAPI_DS_NAME_FORMAT_GUID: {
768 guid = samdb_result_guid(result_res[0], "objectGUID");
770 info1->result_name = GUID_string2(mem_ctx, &guid);
771 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
773 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
776 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
777 info1->result_name = samdb_result_string(result_res[0], "displayName", NULL);
778 if (!info1->result_name) {
779 info1->result_name = samdb_result_string(result_res[0], "sAMAccountName", NULL);
781 if (!info1->result_name) {
782 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
784 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
792 return WERR_INVALID_PARAM;
795 /* Given a user Principal Name (such as foo@bar.com),
796 * return the user and domain DNs. This is used in the KDC to then
797 * return the Keys and evaluate policy */
799 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx,
801 const char *user_principal_name,
802 struct ldb_dn **user_dn,
803 struct ldb_dn **domain_dn)
806 struct drsuapi_DsNameInfo1 info1;
807 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
808 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
809 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
812 if (!W_ERROR_IS_OK(werr)) {
813 return werror_to_ntstatus(werr);
815 switch (info1.status) {
816 case DRSUAPI_DS_NAME_STATUS_OK:
818 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
819 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
820 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
821 return NT_STATUS_NO_SUCH_USER;
822 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
824 return NT_STATUS_UNSUCCESSFUL;
827 *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
830 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
831 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
832 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
833 talloc_asprintf(mem_ctx, "%s/",
834 info1.dns_domain_name),
836 if (!W_ERROR_IS_OK(werr)) {
837 return werror_to_ntstatus(werr);
839 switch (info1.status) {
840 case DRSUAPI_DS_NAME_STATUS_OK:
842 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
843 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
844 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
845 return NT_STATUS_NO_SUCH_USER;
846 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
848 return NT_STATUS_UNSUCCESSFUL;
851 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
858 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
859 * return the user and domain DNs. This is used in the KDC to then
860 * return the Keys and evaluate policy */
862 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx,
864 const char *service_principal_name,
865 struct ldb_dn **user_dn,
866 struct ldb_dn **domain_dn)
869 struct drsuapi_DsNameInfo1 info1;
870 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
871 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
872 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
873 service_principal_name,
875 if (!W_ERROR_IS_OK(werr)) {
876 return werror_to_ntstatus(werr);
878 switch (info1.status) {
879 case DRSUAPI_DS_NAME_STATUS_OK:
881 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
882 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
883 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
884 return NT_STATUS_NO_SUCH_USER;
885 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
887 return NT_STATUS_UNSUCCESSFUL;
890 *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
893 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
894 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
895 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
896 talloc_asprintf(mem_ctx, "%s/",
897 info1.dns_domain_name),
899 if (!W_ERROR_IS_OK(werr)) {
900 return werror_to_ntstatus(werr);
902 switch (info1.status) {
903 case DRSUAPI_DS_NAME_STATUS_OK:
905 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
906 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
907 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
908 return NT_STATUS_NO_SUCH_USER;
909 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
911 return NT_STATUS_UNSUCCESSFUL;
914 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
921 NTSTATUS crack_dn_to_nt4_name(TALLOC_CTX *mem_ctx,
923 const char **nt4_domain, const char **nt4_account)
926 struct drsuapi_DsNameInfo1 info1;
927 struct ldb_context *ldb;
930 /* Handle anonymous bind */
937 ldb = samdb_connect(mem_ctx, system_session(mem_ctx));
939 return NT_STATUS_INTERNAL_DB_CORRUPTION;
942 werr = DsCrackNameOneName(ldb, mem_ctx, 0,
943 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
944 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
947 if (!W_ERROR_IS_OK(werr)) {
948 return werror_to_ntstatus(werr);
950 switch (info1.status) {
951 case DRSUAPI_DS_NAME_STATUS_OK:
953 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
954 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
955 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
956 return NT_STATUS_NO_SUCH_USER;
957 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
959 return NT_STATUS_UNSUCCESSFUL;
962 *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
964 p = strchr(*nt4_domain, '\\');
966 return NT_STATUS_INVALID_PARAMETER;
971 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
974 if (!*nt4_account || !*nt4_domain) {
975 return NT_STATUS_NO_MEMORY;