s4:schema - Change also here counters to "unsigned" where needed
[Samba/cd1.git] / source4 / dsdb / samdb / cracknames.c
blob9430ce0a984d6acb07778757dac53cddd72ee839
1 /*
2 Unix SMB/CIFS implementation.
4 endpoint server for the drsuapi pipe
5 DsCrackNames()
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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "librpc/gen_ndr/drsuapi.h"
26 #include "lib/events/events.h"
27 #include "rpc_server/common/common.h"
28 #include "lib/ldb/include/ldb.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "system/kerberos.h"
31 #include "auth/kerberos/kerberos.h"
32 #include "libcli/ldap/ldap_ndr.h"
33 #include "libcli/security/security.h"
34 #include "auth/auth.h"
35 #include "../lib/util/util_ldb.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "dsdb/common/util.h"
38 #include "param/param.h"
40 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
41 struct smb_krb5_context *smb_krb5_context,
42 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
43 struct ldb_dn *name_dn, const char *name,
44 const char *domain_filter, const char *result_filter,
45 struct drsuapi_DsNameInfo1 *info1);
46 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
47 uint32_t format_offered, uint32_t format_desired,
48 struct ldb_dn *name_dn, const char *name,
49 struct drsuapi_DsNameInfo1 *info1);
51 static WERROR dns_domain_from_principal(TALLOC_CTX *mem_ctx, struct smb_krb5_context *smb_krb5_context,
52 const char *name,
53 struct drsuapi_DsNameInfo1 *info1)
55 krb5_error_code ret;
56 krb5_principal principal;
57 /* perhaps it's a principal with a realm, so return the right 'domain only' response */
58 const char *realm;
59 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
60 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM, &principal);
61 if (ret) {
62 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
63 return WERR_OK;
66 /* This isn't an allocation assignemnt, so it is free'ed with the krb5_free_principal */
67 realm = krb5_principal_get_realm(smb_krb5_context->krb5_context, principal);
69 info1->dns_domain_name = talloc_strdup(mem_ctx, realm);
70 krb5_free_principal(smb_krb5_context->krb5_context, principal);
72 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
74 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
75 return WERR_OK;
78 static enum drsuapi_DsNameStatus LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx,
79 TALLOC_CTX *mem_ctx,
80 const char *alias_from,
81 char **alias_to)
83 unsigned int i;
84 int ret;
85 struct ldb_result *res;
86 struct ldb_message_element *spnmappings;
87 TALLOC_CTX *tmp_ctx;
88 struct ldb_dn *service_dn;
89 char *service_dn_str;
91 const char *directory_attrs[] = {
92 "sPNMappings",
93 NULL
96 tmp_ctx = talloc_new(mem_ctx);
97 if (!tmp_ctx) {
98 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
101 service_dn = ldb_dn_new(tmp_ctx, ldb_ctx, "CN=Directory Service,CN=Windows NT,CN=Services");
102 if ( ! ldb_dn_add_base(service_dn, samdb_config_dn(ldb_ctx))) {
103 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
105 service_dn_str = ldb_dn_alloc_linearized(tmp_ctx, service_dn);
106 if ( ! service_dn_str) {
107 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
110 ret = ldb_search(ldb_ctx, tmp_ctx, &res, service_dn, LDB_SCOPE_BASE,
111 directory_attrs, "(objectClass=nTDSService)");
113 if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
114 DEBUG(1, ("ldb_search: dn: %s not found: %s", service_dn_str, ldb_errstring(ldb_ctx)));
115 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
116 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
117 DEBUG(1, ("ldb_search: dn: %s not found", service_dn_str));
118 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
119 } else if (res->count != 1) {
120 talloc_free(res);
121 DEBUG(1, ("ldb_search: dn: %s not found", service_dn_str));
122 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
125 spnmappings = ldb_msg_find_element(res->msgs[0], "sPNMappings");
126 if (!spnmappings || spnmappings->num_values == 0) {
127 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute", service_dn_str));
128 talloc_free(tmp_ctx);
129 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
132 for (i = 0; i < spnmappings->num_values; i++) {
133 char *mapping, *p, *str;
134 mapping = talloc_strdup(tmp_ctx,
135 (const char *)spnmappings->values[i].data);
136 if (!mapping) {
137 DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str));
138 talloc_free(tmp_ctx);
139 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
142 /* C string manipulation sucks */
144 p = strchr(mapping, '=');
145 if (!p) {
146 DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n",
147 service_dn_str, mapping));
148 talloc_free(tmp_ctx);
149 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
151 p[0] = '\0';
152 p++;
153 do {
154 str = p;
155 p = strchr(p, ',');
156 if (p) {
157 p[0] = '\0';
158 p++;
160 if (strcasecmp(str, alias_from) == 0) {
161 *alias_to = mapping;
162 talloc_steal(mem_ctx, mapping);
163 talloc_free(tmp_ctx);
164 return DRSUAPI_DS_NAME_STATUS_OK;
166 } while (p);
168 DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from));
169 talloc_free(tmp_ctx);
170 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
173 /* When cracking a ServicePrincipalName, many services may be served
174 * by the host/ servicePrincipalName. The incoming query is for cifs/
175 * but we translate it here, and search on host/. This is done after
176 * the cifs/ entry has been searched for, making this a fallback */
178 static WERROR DsCrackNameSPNAlias(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
179 struct smb_krb5_context *smb_krb5_context,
180 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
181 const char *name, struct drsuapi_DsNameInfo1 *info1)
183 WERROR wret;
184 krb5_error_code ret;
185 krb5_principal principal;
186 const char *service, *dns_name;
187 char *new_service;
188 char *new_princ;
189 enum drsuapi_DsNameStatus namestatus;
191 /* parse principal */
192 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context,
193 name, KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
194 if (ret) {
195 DEBUG(2, ("Could not parse principal: %s: %s",
196 name, smb_get_krb5_error_message(smb_krb5_context->krb5_context,
197 ret, mem_ctx)));
198 return WERR_NOMEM;
201 /* grab cifs/, http/ etc */
203 /* This is checked for in callers, but be safe */
204 if (principal->name.name_string.len < 2) {
205 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
206 return WERR_OK;
208 service = principal->name.name_string.val[0];
209 dns_name = principal->name.name_string.val[1];
211 /* MAP it */
212 namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context,
213 sam_ctx, mem_ctx,
214 service, &new_service);
216 if (namestatus == DRSUAPI_DS_NAME_STATUS_NOT_FOUND) {
217 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
218 info1->dns_domain_name = talloc_strdup(mem_ctx, dns_name);
219 if (!info1->dns_domain_name) {
220 krb5_free_principal(smb_krb5_context->krb5_context, principal);
221 return WERR_NOMEM;
223 return WERR_OK;
224 } else if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
225 info1->status = namestatus;
226 krb5_free_principal(smb_krb5_context->krb5_context, principal);
227 return WERR_OK;
230 /* ooh, very nasty playing around in the Principal... */
231 free(principal->name.name_string.val[0]);
232 principal->name.name_string.val[0] = strdup(new_service);
233 if (!principal->name.name_string.val[0]) {
234 krb5_free_principal(smb_krb5_context->krb5_context, principal);
235 return WERR_NOMEM;
238 /* reform principal */
239 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
240 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &new_princ);
242 if (ret) {
243 krb5_free_principal(smb_krb5_context->krb5_context, principal);
244 return WERR_NOMEM;
247 wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
248 new_princ, info1);
249 free(new_princ);
250 if (W_ERROR_IS_OK(wret) && (info1->status == DRSUAPI_DS_NAME_STATUS_NOT_FOUND)) {
251 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
252 info1->dns_domain_name = talloc_strdup(mem_ctx, dns_name);
253 if (!info1->dns_domain_name) {
254 wret = WERR_NOMEM;
257 krb5_free_principal(smb_krb5_context->krb5_context, principal);
258 return wret;
261 /* Subcase of CrackNames, for the userPrincipalName */
263 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
264 struct smb_krb5_context *smb_krb5_context,
265 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
266 const char *name, struct drsuapi_DsNameInfo1 *info1)
268 int ldb_ret;
269 WERROR status;
270 const char *domain_filter = NULL;
271 const char *result_filter = NULL;
272 krb5_error_code ret;
273 krb5_principal principal;
274 const char *realm;
275 char *unparsed_name_short;
276 const char *domain_attrs[] = { NULL };
277 struct ldb_result *domain_res = NULL;
279 /* Prevent recursion */
280 if (!name) {
281 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
282 return WERR_OK;
285 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
286 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM, &principal);
287 if (ret) {
288 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
289 return WERR_OK;
292 realm = krb5_principal_get_realm(smb_krb5_context->krb5_context, principal);
294 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
295 samdb_partitions_dn(sam_ctx, mem_ctx),
296 LDB_SCOPE_ONELEVEL,
297 domain_attrs,
298 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
299 ldb_binary_encode_string(mem_ctx, realm),
300 ldb_binary_encode_string(mem_ctx, realm));
302 if (ldb_ret != LDB_SUCCESS) {
303 DEBUG(2, ("DsCrackNameUPN domain ref search failed: %s", ldb_errstring(sam_ctx)));
304 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
305 return WERR_OK;
308 switch (domain_res->count) {
309 case 1:
310 break;
311 case 0:
312 return dns_domain_from_principal(mem_ctx, smb_krb5_context,
313 name, info1);
314 default:
315 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
316 return WERR_OK;
319 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
320 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
321 krb5_free_principal(smb_krb5_context->krb5_context, principal);
323 if (ret) {
324 free(unparsed_name_short);
325 return WERR_NOMEM;
328 /* This may need to be extended for more userPrincipalName variations */
329 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
330 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
332 domain_filter = talloc_asprintf(mem_ctx, "(distinguishedName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
334 if (!result_filter || !domain_filter) {
335 free(unparsed_name_short);
336 return WERR_NOMEM;
338 status = DsCrackNameOneFilter(sam_ctx, mem_ctx,
339 smb_krb5_context,
340 format_flags, format_offered, format_desired,
341 NULL, unparsed_name_short, domain_filter, result_filter,
342 info1);
343 free(unparsed_name_short);
345 return status;
348 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
350 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
351 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
352 const char *name, struct drsuapi_DsNameInfo1 *info1)
354 krb5_error_code ret;
355 const char *domain_filter = NULL;
356 const char *result_filter = NULL;
357 struct ldb_dn *name_dn = NULL;
359 struct smb_krb5_context *smb_krb5_context = NULL;
361 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
362 info1->dns_domain_name = NULL;
363 info1->result_name = NULL;
365 if (!name) {
366 return WERR_INVALID_PARAM;
369 /* TODO: - fill the correct names in all cases!
370 * - handle format_flags
373 /* here we need to set the domain_filter and/or the result_filter */
374 switch (format_offered) {
375 case DRSUAPI_DS_NAME_FORMAT_UNKNOWN:
377 unsigned int i;
378 enum drsuapi_DsNameFormat formats[] = {
379 DRSUAPI_DS_NAME_FORMAT_FQDN_1779, DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
380 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT, DRSUAPI_DS_NAME_FORMAT_CANONICAL,
381 DRSUAPI_DS_NAME_FORMAT_GUID, DRSUAPI_DS_NAME_FORMAT_DISPLAY,
382 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
383 DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY,
384 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
386 WERROR werr;
387 for (i=0; i < ARRAY_SIZE(formats); i++) {
388 werr = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, formats[i], format_desired, name, info1);
389 if (!W_ERROR_IS_OK(werr)) {
390 return werr;
392 if (info1->status != DRSUAPI_DS_NAME_STATUS_NOT_FOUND) {
393 return werr;
396 return werr;
399 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
400 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
402 char *str, *s, *account;
404 if (strlen(name) == 0) {
405 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
406 return WERR_OK;
409 str = talloc_strdup(mem_ctx, name);
410 W_ERROR_HAVE_NO_MEMORY(str);
412 if (format_offered == DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX) {
413 /* Look backwards for the \n, and replace it with / */
414 s = strrchr(str, '\n');
415 if (!s) {
416 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
417 return WERR_OK;
419 s[0] = '/';
422 s = strchr(str, '/');
423 if (!s) {
424 /* there must be at least one / */
425 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
426 return WERR_OK;
429 s[0] = '\0';
430 s++;
432 domain_filter = talloc_asprintf(mem_ctx, "(&(objectClass=crossRef)(ncName=%s))",
433 ldb_dn_get_linearized(samdb_dns_domain_to_dn(sam_ctx, mem_ctx, str)));
434 W_ERROR_HAVE_NO_MEMORY(domain_filter);
436 /* There may not be anything after the domain component (search for the domain itself) */
437 if (s[0]) {
439 account = strrchr(s, '/');
440 if (!account) {
441 account = s;
442 } else {
443 account++;
445 account = ldb_binary_encode_string(mem_ctx, account);
446 W_ERROR_HAVE_NO_MEMORY(account);
447 result_filter = talloc_asprintf(mem_ctx, "(name=%s)",
448 account);
449 W_ERROR_HAVE_NO_MEMORY(result_filter);
451 break;
453 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
454 char *p;
455 char *domain;
456 const char *account = NULL;
458 domain = talloc_strdup(mem_ctx, name);
459 W_ERROR_HAVE_NO_MEMORY(domain);
461 p = strchr(domain, '\\');
462 if (!p) {
463 /* invalid input format */
464 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
465 return WERR_OK;
467 p[0] = '\0';
469 if (p[1]) {
470 account = &p[1];
473 domain_filter = talloc_asprintf(mem_ctx,
474 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
475 ldb_binary_encode_string(mem_ctx, domain));
476 W_ERROR_HAVE_NO_MEMORY(domain_filter);
477 if (account) {
478 result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
479 ldb_binary_encode_string(mem_ctx, account));
480 W_ERROR_HAVE_NO_MEMORY(result_filter);
483 talloc_free(domain);
484 break;
487 /* A LDAP DN as a string */
488 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
489 domain_filter = NULL;
490 name_dn = ldb_dn_new(mem_ctx, sam_ctx, name);
491 if (! ldb_dn_validate(name_dn)) {
492 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
493 return WERR_OK;
495 break;
498 /* A GUID as a string */
499 case DRSUAPI_DS_NAME_FORMAT_GUID: {
500 struct GUID guid;
501 char *ldap_guid;
502 NTSTATUS nt_status;
503 domain_filter = NULL;
505 nt_status = GUID_from_string(name, &guid);
506 if (!NT_STATUS_IS_OK(nt_status)) {
507 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
508 return WERR_OK;
511 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
512 if (!ldap_guid) {
513 return WERR_NOMEM;
515 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
516 ldap_guid);
517 W_ERROR_HAVE_NO_MEMORY(result_filter);
518 break;
520 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
521 domain_filter = NULL;
523 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
524 ldb_binary_encode_string(mem_ctx, name),
525 ldb_binary_encode_string(mem_ctx, name));
526 W_ERROR_HAVE_NO_MEMORY(result_filter);
527 break;
530 /* A S-1234-5678 style string */
531 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
532 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
533 char *ldap_sid;
535 domain_filter = NULL;
536 if (!sid) {
537 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
538 return WERR_OK;
540 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx,
541 sid);
542 if (!ldap_sid) {
543 return WERR_NOMEM;
545 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
546 ldap_sid);
547 W_ERROR_HAVE_NO_MEMORY(result_filter);
548 break;
550 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
551 krb5_principal principal;
552 char *unparsed_name;
554 ret = smb_krb5_init_context(mem_ctx,
555 ldb_get_event_context(sam_ctx),
556 (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"),
557 &smb_krb5_context);
559 if (ret) {
560 return WERR_NOMEM;
563 /* Ensure we reject compleate junk first */
564 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
565 if (ret) {
566 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
567 return WERR_OK;
570 domain_filter = NULL;
572 /* By getting the unparsed name here, we ensure the escaping is correct (and trust the client less) */
573 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
574 if (ret) {
575 krb5_free_principal(smb_krb5_context->krb5_context, principal);
576 return WERR_NOMEM;
579 krb5_free_principal(smb_krb5_context->krb5_context, principal);
581 /* The ldb_binary_encode_string() here avoid LDAP filter injection attacks */
582 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))",
583 ldb_binary_encode_string(mem_ctx, unparsed_name));
585 free(unparsed_name);
586 W_ERROR_HAVE_NO_MEMORY(result_filter);
587 break;
589 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
590 krb5_principal principal;
591 char *unparsed_name_short;
592 char *service;
594 ret = smb_krb5_init_context(mem_ctx,
595 ldb_get_event_context(sam_ctx),
596 (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"),
597 &smb_krb5_context);
599 if (ret) {
600 return WERR_NOMEM;
603 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
604 if (ret == 0 && principal->name.name_string.len < 2) {
605 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
606 krb5_free_principal(smb_krb5_context->krb5_context, principal);
607 return WERR_OK;
609 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
610 KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
611 if (ret) {
612 krb5_free_principal(smb_krb5_context->krb5_context, principal);
614 return dns_domain_from_principal(mem_ctx, smb_krb5_context,
615 name, info1);
618 domain_filter = NULL;
620 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
621 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
622 if (ret) {
623 krb5_free_principal(smb_krb5_context->krb5_context, principal);
624 return WERR_NOMEM;
627 service = principal->name.name_string.val[0];
628 if ((principal->name.name_string.len == 2) && (strcasecmp(service, "host") == 0)) {
629 /* the 'cn' attribute is just the leading part of the name */
630 char *computer_name;
631 computer_name = talloc_strndup(mem_ctx, principal->name.name_string.val[1],
632 strcspn(principal->name.name_string.val[1], "."));
633 if (computer_name == NULL) {
634 return WERR_NOMEM;
637 result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
638 ldb_binary_encode_string(mem_ctx, unparsed_name_short),
639 ldb_binary_encode_string(mem_ctx, computer_name));
640 } else {
641 result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
642 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
644 krb5_free_principal(smb_krb5_context->krb5_context, principal);
645 free(unparsed_name_short);
646 W_ERROR_HAVE_NO_MEMORY(result_filter);
648 break;
650 default: {
651 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
652 return WERR_OK;
657 if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
658 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
659 name_dn, name, info1);
662 return DsCrackNameOneFilter(sam_ctx, mem_ctx,
663 smb_krb5_context,
664 format_flags, format_offered, format_desired,
665 name_dn, name,
666 domain_filter, result_filter,
667 info1);
670 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
671 * (FQDN_1779) into a canoical name without actually searching the
672 * database */
674 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
675 uint32_t format_offered, uint32_t format_desired,
676 struct ldb_dn *name_dn, const char *name,
677 struct drsuapi_DsNameInfo1 *info1)
679 char *cracked;
680 if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
681 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
682 return WERR_OK;
685 switch (format_desired) {
686 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
687 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
688 break;
689 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
690 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
691 break;
692 default:
693 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
694 return WERR_OK;
696 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
697 info1->result_name = cracked;
698 if (!cracked) {
699 return WERR_NOMEM;
702 return WERR_OK;
705 /* Given a filter for the domain, and one for the result, perform the
706 * ldb search. The format offered and desired flags change the
707 * behaviours, including what attributes to return.
709 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
712 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
713 struct smb_krb5_context *smb_krb5_context,
714 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
715 struct ldb_dn *name_dn, const char *name,
716 const char *domain_filter, const char *result_filter,
717 struct drsuapi_DsNameInfo1 *info1)
719 int ldb_ret;
720 struct ldb_result *domain_res = NULL;
721 const char * const *domain_attrs;
722 const char * const *result_attrs;
723 struct ldb_message **result_res = NULL;
724 struct ldb_message *result = NULL;
725 int i;
726 char *p;
727 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
729 const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
730 const char * const _result_attrs_null[] = { NULL };
732 const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
733 const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
735 const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
736 const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", "objectClass", NULL};
738 const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
739 const char * const _result_attrs_guid[] = { "objectGUID", NULL};
741 const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
742 const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
744 const char * const _domain_attrs_none[] = { "ncName", "dnsRoot" , NULL};
745 const char * const _result_attrs_none[] = { NULL};
747 /* here we need to set the attrs lists for domain and result lookups */
748 switch (format_desired) {
749 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
750 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
751 domain_attrs = _domain_attrs_1779;
752 result_attrs = _result_attrs_null;
753 break;
754 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
755 domain_attrs = _domain_attrs_canonical;
756 result_attrs = _result_attrs_canonical;
757 break;
758 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
759 domain_attrs = _domain_attrs_nt4;
760 result_attrs = _result_attrs_nt4;
761 break;
762 case DRSUAPI_DS_NAME_FORMAT_GUID:
763 domain_attrs = _domain_attrs_guid;
764 result_attrs = _result_attrs_guid;
765 break;
766 case DRSUAPI_DS_NAME_FORMAT_DISPLAY:
767 domain_attrs = _domain_attrs_display;
768 result_attrs = _result_attrs_display;
769 break;
770 default:
771 domain_attrs = _domain_attrs_none;
772 result_attrs = _result_attrs_none;
773 break;
776 if (domain_filter) {
777 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
778 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
779 partitions_basedn,
780 LDB_SCOPE_ONELEVEL,
781 domain_attrs,
782 "%s", domain_filter);
784 if (ldb_ret != LDB_SUCCESS) {
785 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
786 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
787 return WERR_OK;
790 switch (domain_res->count) {
791 case 1:
792 break;
793 case 0:
794 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
795 return WERR_OK;
796 default:
797 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
798 return WERR_OK;
801 info1->dns_domain_name = samdb_result_string(domain_res->msgs[0], "dnsRoot", NULL);
802 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
803 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
804 } else {
805 info1->dns_domain_name = NULL;
806 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
809 if (result_filter) {
810 int ret;
811 struct ldb_result *res;
812 uint32_t dsdb_flags = 0;
813 struct ldb_dn *search_dn;
815 if (domain_res) {
816 dsdb_flags = 0;
817 search_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
818 } else {
819 dsdb_flags = DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
820 search_dn = ldb_get_root_basedn(sam_ctx);
823 /* search with the 'phantom root' flag */
824 ret = dsdb_search(sam_ctx, mem_ctx, &res,
825 search_dn,
826 LDB_SCOPE_SUBTREE,
827 result_attrs,
828 DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
829 "%s", result_filter);
830 if (ret != LDB_SUCCESS) {
831 DEBUG(2, ("DsCrackNameOneFilter phantom root search failed: %s",
832 ldb_errstring(sam_ctx)));
833 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
834 return WERR_OK;
837 ldb_ret = res->count;
838 result_res = res->msgs;
839 } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
840 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
841 result_attrs);
842 } else if (domain_res) {
843 name_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
844 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
845 result_attrs);
846 } else {
847 /* Can't happen */
848 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not availible: This can't happen..."));
849 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
850 return WERR_OK;
853 switch (ldb_ret) {
854 case 1:
855 result = result_res[0];
856 break;
857 case 0:
858 switch (format_offered) {
859 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL:
860 return DsCrackNameSPNAlias(sam_ctx, mem_ctx,
861 smb_krb5_context,
862 format_flags, format_offered, format_desired,
863 name, info1);
865 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
866 return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context,
867 format_flags, format_offered, format_desired,
868 name, info1);
870 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
871 return WERR_OK;
872 case -1:
873 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s", ldb_errstring(sam_ctx)));
874 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
875 return WERR_OK;
876 default:
877 switch (format_offered) {
878 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
879 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
881 const char *canonical_name = NULL; /* Not required, but we get warnings... */
882 /* We may need to manually filter further */
883 for (i = 0; i < ldb_ret; i++) {
884 switch (format_offered) {
885 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
886 canonical_name = ldb_dn_canonical_string(mem_ctx, result_res[i]->dn);
887 break;
888 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
889 canonical_name = ldb_dn_canonical_ex_string(mem_ctx, result_res[i]->dn);
890 break;
892 if (strcasecmp_m(canonical_name, name) == 0) {
893 result = result_res[i];
894 break;
897 if (!result) {
898 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
899 return WERR_OK;
902 default:
903 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
904 return WERR_OK;
908 info1->dns_domain_name = ldb_dn_canonical_string(mem_ctx, result->dn);
909 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
910 p = strchr(info1->dns_domain_name, '/');
911 if (p) {
912 p[0] = '\0';
915 /* here we can use result and domain_res[0] */
916 switch (format_desired) {
917 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
918 info1->result_name = ldb_dn_alloc_linearized(mem_ctx, result->dn);
919 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
921 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
922 return WERR_OK;
924 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
925 info1->result_name = samdb_result_string(result, "canonicalName", NULL);
926 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
927 return WERR_OK;
929 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
930 /* Not in the virtual ldb attribute */
931 return DsCrackNameOneSyntactical(mem_ctx,
932 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
933 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
934 result->dn, name, info1);
936 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
938 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result, "objectSid");
939 const char *_acc = "", *_dom = "";
941 if (samdb_find_attribute(sam_ctx, result, "objectClass", "domain")) {
943 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
944 partitions_basedn,
945 LDB_SCOPE_ONELEVEL,
946 domain_attrs,
947 "(ncName=%s)", ldb_dn_get_linearized(result->dn));
949 if (ldb_ret != LDB_SUCCESS) {
950 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
951 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
952 return WERR_OK;
955 switch (domain_res->count) {
956 case 1:
957 break;
958 case 0:
959 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
960 return WERR_OK;
961 default:
962 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
963 return WERR_OK;
965 _dom = samdb_result_string(domain_res->msgs[0], "nETBIOSName", NULL);
966 W_ERROR_HAVE_NO_MEMORY(_dom);
967 } else {
968 _acc = samdb_result_string(result, "sAMAccountName", NULL);
969 if (!_acc) {
970 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
971 return WERR_OK;
973 if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx, SID_BUILTIN), sid)) {
974 _dom = "BUILTIN";
975 } else {
976 const char *attrs[] = { NULL };
977 struct ldb_result *domain_res2;
978 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
979 if (!dom_sid) {
980 return WERR_OK;
982 dom_sid->num_auths--;
983 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
984 NULL,
985 LDB_SCOPE_BASE,
986 attrs,
987 "(&(objectSid=%s)(objectClass=domain))",
988 ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
990 if (ldb_ret != LDB_SUCCESS) {
991 DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s", ldb_errstring(sam_ctx)));
992 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
993 return WERR_OK;
996 switch (domain_res->count) {
997 case 1:
998 break;
999 case 0:
1000 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1001 return WERR_OK;
1002 default:
1003 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1004 return WERR_OK;
1007 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res2,
1008 partitions_basedn,
1009 LDB_SCOPE_ONELEVEL,
1010 domain_attrs,
1011 "(ncName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
1013 if (ldb_ret != LDB_SUCCESS) {
1014 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
1015 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1016 return WERR_OK;
1019 switch (domain_res2->count) {
1020 case 1:
1021 break;
1022 case 0:
1023 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1024 return WERR_OK;
1025 default:
1026 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1027 return WERR_OK;
1029 _dom = samdb_result_string(domain_res2->msgs[0], "nETBIOSName", NULL);
1030 W_ERROR_HAVE_NO_MEMORY(_dom);
1034 info1->result_name = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
1035 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1037 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1038 return WERR_OK;
1040 case DRSUAPI_DS_NAME_FORMAT_GUID: {
1041 struct GUID guid;
1043 guid = samdb_result_guid(result, "objectGUID");
1045 info1->result_name = GUID_string2(mem_ctx, &guid);
1046 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1048 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1049 return WERR_OK;
1051 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
1052 info1->result_name = samdb_result_string(result, "displayName", NULL);
1053 if (!info1->result_name) {
1054 info1->result_name = samdb_result_string(result, "sAMAccountName", NULL);
1056 if (!info1->result_name) {
1057 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1058 } else {
1059 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1061 return WERR_OK;
1063 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
1064 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1065 return WERR_OK;
1067 case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN:
1068 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
1069 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1070 return WERR_OK;
1072 default:
1073 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1074 return WERR_OK;
1078 /* Given a user Principal Name (such as foo@bar.com),
1079 * return the user and domain DNs. This is used in the KDC to then
1080 * return the Keys and evaluate policy */
1082 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx,
1083 TALLOC_CTX *mem_ctx,
1084 const char *user_principal_name,
1085 struct ldb_dn **user_dn,
1086 struct ldb_dn **domain_dn)
1088 WERROR werr;
1089 struct drsuapi_DsNameInfo1 info1;
1090 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1091 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
1092 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1093 user_principal_name,
1094 &info1);
1095 if (!W_ERROR_IS_OK(werr)) {
1096 return werror_to_ntstatus(werr);
1098 switch (info1.status) {
1099 case DRSUAPI_DS_NAME_STATUS_OK:
1100 break;
1101 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1102 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1103 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1104 return NT_STATUS_NO_SUCH_USER;
1105 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1106 default:
1107 return NT_STATUS_UNSUCCESSFUL;
1110 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1112 if (domain_dn) {
1113 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1114 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1115 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1116 talloc_asprintf(mem_ctx, "%s/",
1117 info1.dns_domain_name),
1118 &info1);
1119 if (!W_ERROR_IS_OK(werr)) {
1120 return werror_to_ntstatus(werr);
1122 switch (info1.status) {
1123 case DRSUAPI_DS_NAME_STATUS_OK:
1124 break;
1125 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1126 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1127 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1128 return NT_STATUS_NO_SUCH_USER;
1129 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1130 default:
1131 return NT_STATUS_UNSUCCESSFUL;
1134 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1137 return NT_STATUS_OK;
1140 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1141 * return the user and domain DNs. This is used in the KDC to then
1142 * return the Keys and evaluate policy */
1144 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx,
1145 TALLOC_CTX *mem_ctx,
1146 const char *service_principal_name,
1147 struct ldb_dn **user_dn,
1148 struct ldb_dn **domain_dn)
1150 WERROR werr;
1151 struct drsuapi_DsNameInfo1 info1;
1152 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1153 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
1154 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1155 service_principal_name,
1156 &info1);
1157 if (!W_ERROR_IS_OK(werr)) {
1158 return werror_to_ntstatus(werr);
1160 switch (info1.status) {
1161 case DRSUAPI_DS_NAME_STATUS_OK:
1162 break;
1163 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1164 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1165 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1166 return NT_STATUS_NO_SUCH_USER;
1167 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1168 default:
1169 return NT_STATUS_UNSUCCESSFUL;
1172 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1174 if (domain_dn) {
1175 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1176 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1177 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1178 talloc_asprintf(mem_ctx, "%s/",
1179 info1.dns_domain_name),
1180 &info1);
1181 if (!W_ERROR_IS_OK(werr)) {
1182 return werror_to_ntstatus(werr);
1184 switch (info1.status) {
1185 case DRSUAPI_DS_NAME_STATUS_OK:
1186 break;
1187 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1188 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1189 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1190 return NT_STATUS_NO_SUCH_USER;
1191 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1192 default:
1193 return NT_STATUS_UNSUCCESSFUL;
1196 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1199 return NT_STATUS_OK;
1202 NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1203 struct tevent_context *ev_ctx,
1204 struct loadparm_context *lp_ctx,
1205 uint32_t format_offered,
1206 const char *name,
1207 const char **nt4_domain, const char **nt4_account)
1209 WERROR werr;
1210 struct drsuapi_DsNameInfo1 info1;
1211 struct ldb_context *ldb;
1212 char *p;
1214 /* Handle anonymous bind */
1215 if (!name || !*name) {
1216 *nt4_domain = "";
1217 *nt4_account = "";
1218 return NT_STATUS_OK;
1221 ldb = samdb_connect(mem_ctx, ev_ctx, lp_ctx, system_session(lp_ctx));
1222 if (ldb == NULL) {
1223 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1226 werr = DsCrackNameOneName(ldb, mem_ctx, 0,
1227 format_offered,
1228 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
1229 name,
1230 &info1);
1231 if (!W_ERROR_IS_OK(werr)) {
1232 return werror_to_ntstatus(werr);
1234 switch (info1.status) {
1235 case DRSUAPI_DS_NAME_STATUS_OK:
1236 break;
1237 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1238 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1239 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1240 return NT_STATUS_NO_SUCH_USER;
1241 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1242 default:
1243 return NT_STATUS_UNSUCCESSFUL;
1246 *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
1247 if (*nt4_domain == NULL) {
1248 return NT_STATUS_NO_MEMORY;
1251 p = strchr(*nt4_domain, '\\');
1252 if (!p) {
1253 return NT_STATUS_INVALID_PARAMETER;
1255 p[0] = '\0';
1257 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
1258 if (*nt4_account == NULL) {
1259 return NT_STATUS_NO_MEMORY;
1262 return NT_STATUS_OK;
1265 NTSTATUS crack_auto_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1266 struct tevent_context *ev_ctx,
1267 struct loadparm_context *lp_ctx,
1268 const char *name,
1269 const char **nt4_domain,
1270 const char **nt4_account)
1272 uint32_t format_offered = DRSUAPI_DS_NAME_FORMAT_UNKNOWN;
1274 /* Handle anonymous bind */
1275 if (!name || !*name) {
1276 *nt4_domain = "";
1277 *nt4_account = "";
1278 return NT_STATUS_OK;
1281 if (strchr_m(name, '=')) {
1282 format_offered = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
1283 } else if (strchr_m(name, '@')) {
1284 format_offered = DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL;
1285 } else if (strchr_m(name, '\\')) {
1286 format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
1287 } else if (strchr_m(name, '/')) {
1288 format_offered = DRSUAPI_DS_NAME_FORMAT_CANONICAL;
1289 } else {
1290 return NT_STATUS_NO_SUCH_USER;
1293 return crack_name_to_nt4_name(mem_ctx, ev_ctx, lp_ctx, format_offered, name, nt4_domain, nt4_account);