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 "auth/auth.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "librpc/gen_ndr/ndr_security.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, samdb_base_dn(tmp_ctx),
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;
581 const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
582 const char * const _result_attrs_null[] = { NULL };
584 const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
585 const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
587 const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
588 const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", NULL};
590 const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
591 const char * const _result_attrs_guid[] = { "objectGUID", NULL};
593 const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
594 const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
596 /* here we need to set the attrs lists for domain and result lookups */
597 switch (format_desired) {
598 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
599 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
600 domain_attrs = _domain_attrs_1779;
601 result_attrs = _result_attrs_null;
603 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
604 domain_attrs = _domain_attrs_canonical;
605 result_attrs = _result_attrs_canonical;
607 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
608 domain_attrs = _domain_attrs_nt4;
609 result_attrs = _result_attrs_nt4;
611 case DRSUAPI_DS_NAME_FORMAT_GUID:
612 domain_attrs = _domain_attrs_guid;
613 result_attrs = _result_attrs_guid;
615 case DRSUAPI_DS_NAME_FORMAT_DISPLAY:
616 domain_attrs = _domain_attrs_display;
617 result_attrs = _result_attrs_display;
624 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
625 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, domain_attrs,
626 "%s", domain_filter);
628 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, domain_attrs,
629 "(ncName=%s)", ldb_dn_linearize(mem_ctx, samdb_base_dn(mem_ctx)));
636 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
639 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
642 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
646 info1->dns_domain_name = samdb_result_string(domain_res[0], "dnsRoot", NULL);
647 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
648 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
651 result_basedn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
653 ldb_ret = gendb_search(sam_ctx, mem_ctx, result_basedn, &result_res,
654 result_attrs, "%s", result_filter);
655 } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
656 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
659 name_dn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
660 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
668 switch (format_offered) {
669 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL:
670 return DsCrackNameSPNAlias(sam_ctx, mem_ctx,
672 format_flags, format_offered, format_desired,
675 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
676 return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context,
677 format_flags, format_offered, format_desired,
680 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
683 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
686 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
690 /* here we can use result_res[0] and domain_res[0] */
691 switch (format_desired) {
692 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
693 info1->result_name = ldb_dn_linearize(mem_ctx, result_res[0]->dn);
694 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
696 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
699 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
700 info1->result_name = samdb_result_string(result_res[0], "canonicalName", NULL);
701 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
704 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
705 /* Not in the virtual ldb attribute */
706 return DsCrackNameOneSyntactical(mem_ctx,
707 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
708 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
709 result_res[0]->dn, name, info1);
711 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
712 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result_res[0], "objectSid");
713 const char *_acc = "", *_dom = "";
715 if (!sid || (sid->num_auths < 4) || (sid->num_auths > 5)) {
716 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
720 if (sid->num_auths == 4) {
721 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, domain_attrs,
722 "(ncName=%s)", ldb_dn_linearize(mem_ctx, result_res[0]->dn));
724 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
727 _dom = samdb_result_string(domain_res[0], "nETBIOSName", NULL);
728 W_ERROR_HAVE_NO_MEMORY(_dom);
730 } else if (sid->num_auths == 5) {
731 const char *attrs[] = { NULL };
732 struct ldb_message **domain_res2;
733 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
737 dom_sid->num_auths--;
738 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, attrs,
739 "(&(objectSid=%s)(objectClass=domain))", ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
741 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
744 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res2, domain_attrs,
745 "(ncName=%s)", ldb_dn_linearize(mem_ctx, domain_res[0]->dn));
747 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
751 _dom = samdb_result_string(domain_res2[0], "nETBIOSName", NULL);
752 W_ERROR_HAVE_NO_MEMORY(_dom);
754 _acc = samdb_result_string(result_res[0], "sAMAccountName", NULL);
755 W_ERROR_HAVE_NO_MEMORY(_acc);
758 info1->result_name = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
759 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
761 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
764 case DRSUAPI_DS_NAME_FORMAT_GUID: {
767 guid = samdb_result_guid(result_res[0], "objectGUID");
769 info1->result_name = GUID_string2(mem_ctx, &guid);
770 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
772 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
775 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
776 info1->result_name = samdb_result_string(result_res[0], "displayName", NULL);
777 if (!info1->result_name) {
778 info1->result_name = samdb_result_string(result_res[0], "sAMAccountName", NULL);
780 if (!info1->result_name) {
781 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
783 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
791 return WERR_INVALID_PARAM;
794 /* Given a user Principal Name (such as foo@bar.com),
795 * return the user and domain DNs. This is used in the KDC to then
796 * return the Keys and evaluate policy */
798 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx,
800 const char *user_principal_name,
801 struct ldb_dn **user_dn,
802 struct ldb_dn **domain_dn)
805 struct drsuapi_DsNameInfo1 info1;
806 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
807 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
808 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
811 if (!W_ERROR_IS_OK(werr)) {
812 return werror_to_ntstatus(werr);
814 switch (info1.status) {
815 case DRSUAPI_DS_NAME_STATUS_OK:
817 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
818 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
819 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
820 return NT_STATUS_NO_SUCH_USER;
821 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
823 return NT_STATUS_UNSUCCESSFUL;
826 *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
829 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
830 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
831 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
832 talloc_asprintf(mem_ctx, "%s/",
833 info1.dns_domain_name),
835 if (!W_ERROR_IS_OK(werr)) {
836 return werror_to_ntstatus(werr);
838 switch (info1.status) {
839 case DRSUAPI_DS_NAME_STATUS_OK:
841 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
842 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
843 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
844 return NT_STATUS_NO_SUCH_USER;
845 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
847 return NT_STATUS_UNSUCCESSFUL;
850 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
857 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
858 * return the user and domain DNs. This is used in the KDC to then
859 * return the Keys and evaluate policy */
861 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx,
863 const char *service_principal_name,
864 struct ldb_dn **user_dn,
865 struct ldb_dn **domain_dn)
868 struct drsuapi_DsNameInfo1 info1;
869 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
870 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
871 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
872 service_principal_name,
874 if (!W_ERROR_IS_OK(werr)) {
875 return werror_to_ntstatus(werr);
877 switch (info1.status) {
878 case DRSUAPI_DS_NAME_STATUS_OK:
880 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
881 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
882 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
883 return NT_STATUS_NO_SUCH_USER;
884 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
886 return NT_STATUS_UNSUCCESSFUL;
889 *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
892 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
893 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
894 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
895 talloc_asprintf(mem_ctx, "%s/",
896 info1.dns_domain_name),
898 if (!W_ERROR_IS_OK(werr)) {
899 return werror_to_ntstatus(werr);
901 switch (info1.status) {
902 case DRSUAPI_DS_NAME_STATUS_OK:
904 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
905 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
906 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
907 return NT_STATUS_NO_SUCH_USER;
908 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
910 return NT_STATUS_UNSUCCESSFUL;
913 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
920 NTSTATUS crack_dn_to_nt4_name(TALLOC_CTX *mem_ctx,
922 const char **nt4_domain, const char **nt4_account)
925 struct drsuapi_DsNameInfo1 info1;
926 struct ldb_context *ldb;
929 /* Handle anonymous bind */
936 ldb = samdb_connect(mem_ctx, system_session(mem_ctx));
938 return NT_STATUS_INTERNAL_DB_CORRUPTION;
941 werr = DsCrackNameOneName(ldb, mem_ctx, 0,
942 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
943 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
946 if (!W_ERROR_IS_OK(werr)) {
947 return werror_to_ntstatus(werr);
949 switch (info1.status) {
950 case DRSUAPI_DS_NAME_STATUS_OK:
952 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
953 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
954 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
955 return NT_STATUS_NO_SUCH_USER;
956 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
958 return NT_STATUS_UNSUCCESSFUL;
961 *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
963 p = strchr(*nt4_domain, '\\');
965 return NT_STATUS_INVALID_PARAMETER;
970 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
973 if (!*nt4_account || !*nt4_domain) {
974 return NT_STATUS_NO_MEMORY;