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/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/proto.h"
33 #include "auth/auth.h"
35 #include "dsdb/samdb/samdb.h"
37 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
38 struct smb_krb5_context *smb_krb5_context,
39 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
40 const struct ldb_dn *name_dn, const char *name,
41 const char *domain_filter, const char *result_filter,
42 struct drsuapi_DsNameInfo1 *info1);
43 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
44 uint32_t format_offered, uint32_t format_desired,
45 const struct ldb_dn *name_dn, const char *name,
46 struct drsuapi_DsNameInfo1 *info1);
48 static enum drsuapi_DsNameStatus LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx,
50 const char *alias_from,
55 struct ldb_result *res;
56 struct ldb_message_element *spnmappings;
58 struct ldb_dn *service_dn;
61 const char *directory_attrs[] = {
66 tmp_ctx = talloc_new(mem_ctx);
68 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
71 service_dn = ldb_dn_string_compose(tmp_ctx, samdb_base_dn(tmp_ctx),
72 "CN=Directory Service,CN=Windows NT"
73 ",CN=Services,CN=Configuration");
74 service_dn_str = ldb_dn_linearize(tmp_ctx, service_dn);
76 ret = ldb_search(ldb_ctx, service_dn, LDB_SCOPE_BASE, "(objectClass=nTDSService)",
77 directory_attrs, &res);
79 if (ret != LDB_SUCCESS) {
80 DEBUG(1, ("ldb_search: dn: %s not found: %s", service_dn_str, ldb_errstring(ldb_ctx)));
81 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
82 } else if (res->count != 1) {
84 DEBUG(1, ("ldb_search: dn: %s found %d times!", service_dn_str, res->count));
85 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
87 talloc_steal(tmp_ctx, res);
89 spnmappings = ldb_msg_find_element(res->msgs[0], "sPNMappings");
90 if (!spnmappings || spnmappings->num_values == 0) {
91 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute", service_dn_str));
93 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
96 for (i = 0; i < spnmappings->num_values; i++) {
97 char *mapping, *p, *str;
98 mapping = talloc_strdup(tmp_ctx,
99 (const char *)spnmappings->values[i].data);
101 DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str));
102 talloc_free(tmp_ctx);
103 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
106 /* C string manipulation sucks */
108 p = strchr(mapping, '=');
110 DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n",
111 service_dn_str, mapping));
112 talloc_free(tmp_ctx);
113 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
124 if (strcasecmp(str, alias_from) == 0) {
126 talloc_steal(mem_ctx, mapping);
127 talloc_free(tmp_ctx);
128 return DRSUAPI_DS_NAME_STATUS_OK;
132 DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from));
133 talloc_free(tmp_ctx);
134 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
137 /* When cracking a ServicePrincipalName, many services may be served
138 * by the host/ servicePrincipalName. The incoming query is for cifs/
139 * but we translate it here, and search on host/. This is done after
140 * the cifs/ entry has been searched for, making this a fallback */
142 static WERROR DsCrackNameSPNAlias(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
143 struct smb_krb5_context *smb_krb5_context,
144 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
145 const char *name, struct drsuapi_DsNameInfo1 *info1)
149 krb5_principal principal;
153 enum drsuapi_DsNameStatus namestatus;
155 /* parse principal */
156 ret = krb5_parse_name_norealm(smb_krb5_context->krb5_context,
159 DEBUG(2, ("Could not parse principal: %s: %s",
160 name, smb_get_krb5_error_message(smb_krb5_context->krb5_context,
165 /* grab cifs/, http/ etc */
167 /* This is checked for in callers, but be safe */
168 if (principal->name.name_string.len < 2) {
169 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
172 service = principal->name.name_string.val[0];
175 namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context,
177 service, &new_service);
179 if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
180 info1->status = namestatus;
185 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
189 /* ooh, very nasty playing around in the Principal... */
190 free(principal->name.name_string.val[0]);
191 principal->name.name_string.val[0] = strdup(new_service);
192 if (!principal->name.name_string.val[0]) {
193 krb5_free_principal(smb_krb5_context->krb5_context, principal);
197 /* reform principal */
198 ret = krb5_unparse_name_norealm(smb_krb5_context->krb5_context, principal, &new_princ);
200 krb5_free_principal(smb_krb5_context->krb5_context, principal);
206 wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
212 /* Subcase of CrackNames, for the userPrincipalName */
214 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
215 struct smb_krb5_context *smb_krb5_context,
216 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
217 const char *name, struct drsuapi_DsNameInfo1 *info1)
220 const char *domain_filter = NULL;
221 const char *result_filter = NULL;
223 krb5_principal principal;
225 char *unparsed_name_short;
227 /* Prevent recursion */
229 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
233 ret = krb5_parse_name_mustrealm(smb_krb5_context->krb5_context, name, &principal);
235 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
239 domain_filter = NULL;
240 realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
241 domain_filter = talloc_asprintf(mem_ctx,
242 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
243 ldb_binary_encode_string(mem_ctx, *realm),
244 ldb_binary_encode_string(mem_ctx, *realm));
245 ret = krb5_unparse_name_norealm(smb_krb5_context->krb5_context, principal, &unparsed_name_short);
246 krb5_free_principal(smb_krb5_context->krb5_context, principal);
249 free(unparsed_name_short);
253 /* This may need to be extended for more userPrincipalName variations */
254 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
255 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
256 if (!result_filter || !domain_filter) {
257 free(unparsed_name_short);
260 status = DsCrackNameOneFilter(sam_ctx, mem_ctx,
262 format_flags, format_offered, format_desired,
263 NULL, unparsed_name_short, domain_filter, result_filter,
265 free(unparsed_name_short);
269 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
271 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
272 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
273 const char *name, struct drsuapi_DsNameInfo1 *info1)
276 const char *domain_filter = NULL;
277 const char *result_filter = NULL;
278 struct ldb_dn *name_dn = NULL;
280 struct smb_krb5_context *smb_krb5_context;
281 ret = smb_krb5_init_context(mem_ctx, &smb_krb5_context);
287 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
288 info1->dns_domain_name = NULL;
289 info1->result_name = NULL;
292 return WERR_INVALID_PARAM;
295 /* TODO: - fill the correct names in all cases!
296 * - handle format_flags
299 /* here we need to set the domain_filter and/or the result_filter */
300 switch (format_offered) {
301 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
304 str = talloc_strdup(mem_ctx, name);
305 WERR_TALLOC_CHECK(str);
307 if (strlen(str) == 0 || str[strlen(str)-1] != '/') {
308 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
312 str[strlen(str)-1] = '\0';
314 domain_filter = talloc_asprintf(mem_ctx,
315 "(&(&(&(dnsRoot=%s)(objectclass=crossRef)))(nETBIOSName=*)(ncName=*))",
316 ldb_binary_encode_string(mem_ctx, str));
317 WERR_TALLOC_CHECK(domain_filter);
321 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
324 const char *account = NULL;
326 domain = talloc_strdup(mem_ctx, name);
327 WERR_TALLOC_CHECK(domain);
329 p = strchr(domain, '\\');
331 /* invalid input format */
332 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
341 domain_filter = talloc_asprintf(mem_ctx,
342 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
343 ldb_binary_encode_string(mem_ctx, domain));
344 WERR_TALLOC_CHECK(domain_filter);
346 result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
347 ldb_binary_encode_string(mem_ctx, account));
348 WERR_TALLOC_CHECK(result_filter);
355 /* A LDAP DN as a string */
356 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
357 name_dn = ldb_dn_explode(mem_ctx, name);
358 domain_filter = NULL;
360 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
366 /* A GUID as a string */
367 case DRSUAPI_DS_NAME_FORMAT_GUID: {
371 domain_filter = NULL;
373 nt_status = GUID_from_string(name, &guid);
374 if (!NT_STATUS_IS_OK(nt_status)) {
375 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
379 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
383 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
385 WERR_TALLOC_CHECK(result_filter);
388 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
389 domain_filter = NULL;
391 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
392 ldb_binary_encode_string(mem_ctx, name),
393 ldb_binary_encode_string(mem_ctx, name));
394 WERR_TALLOC_CHECK(result_filter);
398 /* A S-1234-5678 style string */
399 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
400 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
403 domain_filter = NULL;
405 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
408 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx,
413 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
415 WERR_TALLOC_CHECK(result_filter);
418 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
419 krb5_principal principal;
421 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
423 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
427 domain_filter = NULL;
429 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
431 krb5_free_principal(smb_krb5_context->krb5_context, principal);
435 krb5_free_principal(smb_krb5_context->krb5_context, principal);
436 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))",
437 ldb_binary_encode_string(mem_ctx, unparsed_name));
440 WERR_TALLOC_CHECK(result_filter);
443 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
444 krb5_principal principal;
445 char *unparsed_name_short;
447 ret = krb5_parse_name_norealm(smb_krb5_context->krb5_context, name, &principal);
449 /* perhaps it's a principal with a realm, so return the right 'domain only' response */
451 ret = krb5_parse_name_mustrealm(smb_krb5_context->krb5_context, name, &principal);
453 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
457 /* This isn't an allocation assignemnt, so it is free'ed with the krb5_free_principal */
458 realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
460 info1->dns_domain_name = talloc_strdup(info1, *realm);
461 krb5_free_principal(smb_krb5_context->krb5_context, principal);
463 WERR_TALLOC_CHECK(info1->dns_domain_name);
465 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
468 } else if (principal->name.name_string.len < 2) {
469 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
473 domain_filter = NULL;
475 ret = krb5_unparse_name_norealm(smb_krb5_context->krb5_context, principal, &unparsed_name_short);
477 krb5_free_principal(smb_krb5_context->krb5_context, principal);
481 service = principal->name.name_string.val[0];
482 if ((principal->name.name_string.len == 2) && (strcasecmp(service, "host") == 0)) {
483 /* the 'cn' attribute is just the leading part of the name */
485 computer_name = talloc_strndup(mem_ctx, principal->name.name_string.val[1],
486 strcspn(principal->name.name_string.val[1], "."));
487 if (computer_name == NULL) {
491 result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
492 ldb_binary_encode_string(mem_ctx, unparsed_name_short),
493 ldb_binary_encode_string(mem_ctx, computer_name));
495 result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
496 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
498 krb5_free_principal(smb_krb5_context->krb5_context, principal);
499 free(unparsed_name_short);
500 WERR_TALLOC_CHECK(result_filter);
505 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
511 if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
512 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
513 name_dn, name, info1);
516 return DsCrackNameOneFilter(sam_ctx, mem_ctx,
518 format_flags, format_offered, format_desired,
520 domain_filter, result_filter,
524 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
525 * (FQDN_1779) into a canoical name without actually searching the
528 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
529 uint32_t format_offered, uint32_t format_desired,
530 const struct ldb_dn *name_dn, const char *name,
531 struct drsuapi_DsNameInfo1 *info1)
534 if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
535 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
539 switch (format_desired) {
540 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
541 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
543 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
544 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
547 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
550 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
551 info1->result_name = cracked;
559 /* Given a filter for the domain, and one for the result, perform the
560 * ldb search. The format offered and desired flags change the
561 * behaviours, including what attributes to return.
563 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
566 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
567 struct smb_krb5_context *smb_krb5_context,
568 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
569 const struct ldb_dn *name_dn, const char *name,
570 const char *domain_filter, const char *result_filter,
571 struct drsuapi_DsNameInfo1 *info1)
574 struct ldb_message **domain_res = NULL;
575 const char * const *domain_attrs;
576 const char * const *result_attrs;
577 struct ldb_message **result_res = NULL;
578 const struct ldb_dn *result_basedn;
580 const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
581 const char * const _result_attrs_null[] = { NULL };
583 const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
584 const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
586 const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
587 const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", NULL};
589 const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
590 const char * const _result_attrs_guid[] = { "objectGUID", NULL};
592 const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
593 const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
595 /* here we need to set the attrs lists for domain and result lookups */
596 switch (format_desired) {
597 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
598 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
599 domain_attrs = _domain_attrs_1779;
600 result_attrs = _result_attrs_null;
602 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
603 domain_attrs = _domain_attrs_canonical;
604 result_attrs = _result_attrs_canonical;
606 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
607 domain_attrs = _domain_attrs_nt4;
608 result_attrs = _result_attrs_nt4;
610 case DRSUAPI_DS_NAME_FORMAT_GUID:
611 domain_attrs = _domain_attrs_guid;
612 result_attrs = _result_attrs_guid;
614 case DRSUAPI_DS_NAME_FORMAT_DISPLAY:
615 domain_attrs = _domain_attrs_display;
616 result_attrs = _result_attrs_display;
623 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
624 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, domain_attrs,
625 "%s", domain_filter);
627 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, domain_attrs,
628 "(ncName=%s)", ldb_dn_linearize(mem_ctx, samdb_base_dn(mem_ctx)));
635 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
638 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
641 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
645 info1->dns_domain_name = samdb_result_string(domain_res[0], "dnsRoot", NULL);
646 WERR_TALLOC_CHECK(info1->dns_domain_name);
647 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
650 result_basedn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
652 ldb_ret = gendb_search(sam_ctx, mem_ctx, result_basedn, &result_res,
653 result_attrs, "%s", result_filter);
654 } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
655 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
658 name_dn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
659 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
667 switch (format_offered) {
668 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL:
669 return DsCrackNameSPNAlias(sam_ctx, mem_ctx,
671 format_flags, format_offered, format_desired,
674 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
675 return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context,
676 format_flags, format_offered, format_desired,
679 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
682 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
685 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
689 /* here we can use result_res[0] and domain_res[0] */
690 switch (format_desired) {
691 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
692 info1->result_name = ldb_dn_linearize(mem_ctx, result_res[0]->dn);
693 WERR_TALLOC_CHECK(info1->result_name);
695 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
698 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
699 info1->result_name = samdb_result_string(result_res[0], "canonicalName", NULL);
700 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
703 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
704 /* Not in the virtual ldb attribute */
705 return DsCrackNameOneSyntactical(mem_ctx,
706 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
707 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
708 result_res[0]->dn, name, info1);
710 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
711 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result_res[0], "objectSid");
712 const char *_acc = "", *_dom = "";
714 if (!sid || (sid->num_auths < 4) || (sid->num_auths > 5)) {
715 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
719 if (sid->num_auths == 4) {
720 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, domain_attrs,
721 "(ncName=%s)", ldb_dn_linearize(mem_ctx, result_res[0]->dn));
723 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
726 _dom = samdb_result_string(domain_res[0], "nETBIOSName", NULL);
727 WERR_TALLOC_CHECK(_dom);
729 } else if (sid->num_auths == 5) {
730 const char *attrs[] = { NULL };
731 struct ldb_message **domain_res2;
732 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
736 dom_sid->num_auths--;
737 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, attrs,
738 "(&(objectSid=%s)(objectClass=domain))", ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
740 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
743 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res2, domain_attrs,
744 "(ncName=%s)", ldb_dn_linearize(mem_ctx, domain_res[0]->dn));
746 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
750 _dom = samdb_result_string(domain_res2[0], "nETBIOSName", NULL);
751 WERR_TALLOC_CHECK(_dom);
753 _acc = samdb_result_string(result_res[0], "sAMAccountName", NULL);
754 WERR_TALLOC_CHECK(_acc);
757 info1->result_name = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
758 WERR_TALLOC_CHECK(info1->result_name);
760 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
763 case DRSUAPI_DS_NAME_FORMAT_GUID: {
766 guid = samdb_result_guid(result_res[0], "objectGUID");
768 info1->result_name = GUID_string2(mem_ctx, &guid);
769 WERR_TALLOC_CHECK(info1->result_name);
771 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
774 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
775 info1->result_name = samdb_result_string(result_res[0], "displayName", NULL);
776 if (!info1->result_name) {
777 info1->result_name = samdb_result_string(result_res[0], "sAMAccountName", NULL);
779 if (!info1->result_name) {
780 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
782 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
790 return WERR_INVALID_PARAM;
793 /* Given a user Principal Name (such as foo@bar.com),
794 * return the user and domain DNs. This is used in the KDC to then
795 * return the Keys and evaluate policy */
797 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx,
799 const char *user_principal_name,
800 struct ldb_dn **user_dn,
801 struct ldb_dn **domain_dn)
804 struct drsuapi_DsNameInfo1 info1;
805 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
806 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
807 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
810 if (!W_ERROR_IS_OK(werr)) {
811 return werror_to_ntstatus(werr);
813 switch (info1.status) {
814 case DRSUAPI_DS_NAME_STATUS_OK:
816 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
817 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
818 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
819 return NT_STATUS_NO_SUCH_USER;
820 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
822 return NT_STATUS_UNSUCCESSFUL;
825 *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
828 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
829 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
830 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
831 talloc_asprintf(mem_ctx, "%s/",
832 info1.dns_domain_name),
834 if (!W_ERROR_IS_OK(werr)) {
835 return werror_to_ntstatus(werr);
837 switch (info1.status) {
838 case DRSUAPI_DS_NAME_STATUS_OK:
840 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
841 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
842 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
843 return NT_STATUS_NO_SUCH_USER;
844 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
846 return NT_STATUS_UNSUCCESSFUL;
849 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
856 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
857 * return the user and domain DNs. This is used in the KDC to then
858 * return the Keys and evaluate policy */
860 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx,
862 const char *service_principal_name,
863 struct ldb_dn **user_dn,
864 struct ldb_dn **domain_dn)
867 struct drsuapi_DsNameInfo1 info1;
868 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
869 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
870 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
871 service_principal_name,
873 if (!W_ERROR_IS_OK(werr)) {
874 return werror_to_ntstatus(werr);
876 switch (info1.status) {
877 case DRSUAPI_DS_NAME_STATUS_OK:
879 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
880 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
881 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
882 return NT_STATUS_NO_SUCH_USER;
883 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
885 return NT_STATUS_UNSUCCESSFUL;
888 *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
891 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
892 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
893 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
894 talloc_asprintf(mem_ctx, "%s/",
895 info1.dns_domain_name),
897 if (!W_ERROR_IS_OK(werr)) {
898 return werror_to_ntstatus(werr);
900 switch (info1.status) {
901 case DRSUAPI_DS_NAME_STATUS_OK:
903 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
904 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
905 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
906 return NT_STATUS_NO_SUCH_USER;
907 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
909 return NT_STATUS_UNSUCCESSFUL;
912 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
919 NTSTATUS crack_dn_to_nt4_name(TALLOC_CTX *mem_ctx,
921 const char **nt4_domain, const char **nt4_account)
924 struct drsuapi_DsNameInfo1 info1;
925 struct ldb_context *ldb;
928 /* Handle anonymous bind */
935 ldb = samdb_connect(mem_ctx, system_session(mem_ctx));
937 return NT_STATUS_INTERNAL_DB_CORRUPTION;
940 werr = DsCrackNameOneName(ldb, mem_ctx, 0,
941 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
942 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
945 if (!W_ERROR_IS_OK(werr)) {
946 return werror_to_ntstatus(werr);
948 switch (info1.status) {
949 case DRSUAPI_DS_NAME_STATUS_OK:
951 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
952 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
953 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
954 return NT_STATUS_NO_SUCH_USER;
955 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
957 return NT_STATUS_UNSUCCESSFUL;
960 *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
962 p = strchr(*nt4_domain, '\\');
964 return NT_STATUS_INVALID_PARAMETER;
969 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
972 if (!*nt4_account || !*nt4_domain) {
973 return NT_STATUS_NO_MEMORY;