LDB ASYNC: misc changes
[Samba/vfs_proxy.git] / source4 / dsdb / samdb / cracknames.c
blobe02e8d81a6a6b46a7812c1ef5f95f8f2c28afe9e
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 "rpc_server/common/common.h"
27 #include "lib/events/events.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 "librpc/gen_ndr/ndr_misc.h"
35 #include "auth/auth.h"
36 #include "util/util_ldb.h"
37 #include "dsdb/samdb/samdb.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 char **realm;
59 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
60 KRB5_PRINCIPAL_PARSE_MUST_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_princ_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 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 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_MUST_REALM, &principal);
287 if (ret) {
288 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
289 return WERR_OK;
292 realm = krb5_princ_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;
360 ret = smb_krb5_init_context(mem_ctx,
361 ldb_get_event_context(sam_ctx),
362 (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"),
363 &smb_krb5_context);
365 if (ret) {
366 return WERR_NOMEM;
369 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
370 info1->dns_domain_name = NULL;
371 info1->result_name = NULL;
373 if (!name) {
374 return WERR_INVALID_PARAM;
377 /* TODO: - fill the correct names in all cases!
378 * - handle format_flags
381 /* here we need to set the domain_filter and/or the result_filter */
382 switch (format_offered) {
383 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
384 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
386 char *str, *s, *account;
388 if (strlen(name) == 0) {
389 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
390 return WERR_OK;
393 str = talloc_strdup(mem_ctx, name);
394 W_ERROR_HAVE_NO_MEMORY(str);
396 if (format_offered == DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX) {
397 /* Look backwards for the \n, and replace it with / */
398 s = strrchr(str, '\n');
399 if (!s) {
400 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
401 return WERR_OK;
403 s[0] = '/';
406 s = strchr(str, '/');
407 if (!s) {
408 /* there must be at least one / */
409 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
410 return WERR_OK;
413 s[0] = '\0';
414 s++;
416 domain_filter = talloc_asprintf(mem_ctx, "(&(objectClass=crossRef)(ncName=%s))",
417 ldb_dn_get_linearized(samdb_dns_domain_to_dn(sam_ctx, mem_ctx, str)));
418 W_ERROR_HAVE_NO_MEMORY(domain_filter);
420 /* There may not be anything after the domain component (search for the domain itself) */
421 if (s[0]) {
423 account = strrchr(s, '/');
424 if (!account) {
425 account = s;
426 } else {
427 account++;
429 account = ldb_binary_encode_string(mem_ctx, account);
430 W_ERROR_HAVE_NO_MEMORY(account);
431 result_filter = talloc_asprintf(mem_ctx, "(name=%s)",
432 account);
433 W_ERROR_HAVE_NO_MEMORY(result_filter);
435 break;
437 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
438 char *p;
439 char *domain;
440 const char *account = NULL;
442 domain = talloc_strdup(mem_ctx, name);
443 W_ERROR_HAVE_NO_MEMORY(domain);
445 p = strchr(domain, '\\');
446 if (!p) {
447 /* invalid input format */
448 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
449 return WERR_OK;
451 p[0] = '\0';
453 if (p[1]) {
454 account = &p[1];
457 domain_filter = talloc_asprintf(mem_ctx,
458 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
459 ldb_binary_encode_string(mem_ctx, domain));
460 W_ERROR_HAVE_NO_MEMORY(domain_filter);
461 if (account) {
462 result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
463 ldb_binary_encode_string(mem_ctx, account));
464 W_ERROR_HAVE_NO_MEMORY(result_filter);
467 talloc_free(domain);
468 break;
471 /* A LDAP DN as a string */
472 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
473 domain_filter = NULL;
474 name_dn = ldb_dn_new(mem_ctx, sam_ctx, name);
475 if (! ldb_dn_validate(name_dn)) {
476 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
477 return WERR_OK;
479 break;
482 /* A GUID as a string */
483 case DRSUAPI_DS_NAME_FORMAT_GUID: {
484 struct GUID guid;
485 char *ldap_guid;
486 NTSTATUS nt_status;
487 domain_filter = NULL;
489 nt_status = GUID_from_string(name, &guid);
490 if (!NT_STATUS_IS_OK(nt_status)) {
491 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
492 return WERR_OK;
495 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
496 if (!ldap_guid) {
497 return WERR_NOMEM;
499 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
500 ldap_guid);
501 W_ERROR_HAVE_NO_MEMORY(result_filter);
502 break;
504 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
505 domain_filter = NULL;
507 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
508 ldb_binary_encode_string(mem_ctx, name),
509 ldb_binary_encode_string(mem_ctx, name));
510 W_ERROR_HAVE_NO_MEMORY(result_filter);
511 break;
514 /* A S-1234-5678 style string */
515 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
516 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
517 char *ldap_sid;
519 domain_filter = NULL;
520 if (!sid) {
521 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
522 return WERR_OK;
524 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx,
525 sid);
526 if (!ldap_sid) {
527 return WERR_NOMEM;
529 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
530 ldap_sid);
531 W_ERROR_HAVE_NO_MEMORY(result_filter);
532 break;
534 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
535 krb5_principal principal;
536 char *unparsed_name;
537 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
538 if (ret) {
539 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
540 return WERR_OK;
543 domain_filter = NULL;
545 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
546 if (ret) {
547 krb5_free_principal(smb_krb5_context->krb5_context, principal);
548 return WERR_NOMEM;
551 krb5_free_principal(smb_krb5_context->krb5_context, principal);
552 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))",
553 ldb_binary_encode_string(mem_ctx, unparsed_name));
555 free(unparsed_name);
556 W_ERROR_HAVE_NO_MEMORY(result_filter);
557 break;
559 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
560 krb5_principal principal;
561 char *unparsed_name_short;
562 char *service;
563 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
564 if (ret == 0 && principal->name.name_string.len < 2) {
565 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
566 krb5_free_principal(smb_krb5_context->krb5_context, principal);
567 return WERR_OK;
569 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
570 KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
571 if (ret) {
572 krb5_free_principal(smb_krb5_context->krb5_context, principal);
574 return dns_domain_from_principal(mem_ctx, smb_krb5_context,
575 name, info1);
578 domain_filter = NULL;
580 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
581 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
582 if (ret) {
583 krb5_free_principal(smb_krb5_context->krb5_context, principal);
584 return WERR_NOMEM;
587 service = principal->name.name_string.val[0];
588 if ((principal->name.name_string.len == 2) && (strcasecmp(service, "host") == 0)) {
589 /* the 'cn' attribute is just the leading part of the name */
590 char *computer_name;
591 computer_name = talloc_strndup(mem_ctx, principal->name.name_string.val[1],
592 strcspn(principal->name.name_string.val[1], "."));
593 if (computer_name == NULL) {
594 return WERR_NOMEM;
597 result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
598 ldb_binary_encode_string(mem_ctx, unparsed_name_short),
599 ldb_binary_encode_string(mem_ctx, computer_name));
600 } else {
601 result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
602 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
604 krb5_free_principal(smb_krb5_context->krb5_context, principal);
605 free(unparsed_name_short);
606 W_ERROR_HAVE_NO_MEMORY(result_filter);
608 break;
610 default: {
611 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
612 return WERR_OK;
617 if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
618 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
619 name_dn, name, info1);
622 return DsCrackNameOneFilter(sam_ctx, mem_ctx,
623 smb_krb5_context,
624 format_flags, format_offered, format_desired,
625 name_dn, name,
626 domain_filter, result_filter,
627 info1);
630 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
631 * (FQDN_1779) into a canoical name without actually searching the
632 * database */
634 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
635 uint32_t format_offered, uint32_t format_desired,
636 struct ldb_dn *name_dn, const char *name,
637 struct drsuapi_DsNameInfo1 *info1)
639 char *cracked;
640 if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
641 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
642 return WERR_OK;
645 switch (format_desired) {
646 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
647 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
648 break;
649 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
650 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
651 break;
652 default:
653 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
654 return WERR_OK;
656 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
657 info1->result_name = cracked;
658 if (!cracked) {
659 return WERR_NOMEM;
662 return WERR_OK;
665 /* Given a filter for the domain, and one for the result, perform the
666 * ldb search. The format offered and desired flags change the
667 * behaviours, including what attributes to return.
669 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
672 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
673 struct smb_krb5_context *smb_krb5_context,
674 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
675 struct ldb_dn *name_dn, const char *name,
676 const char *domain_filter, const char *result_filter,
677 struct drsuapi_DsNameInfo1 *info1)
679 int ldb_ret;
680 struct ldb_result *domain_res = NULL;
681 const char * const *domain_attrs;
682 const char * const *result_attrs;
683 struct ldb_message **result_res = NULL;
684 struct ldb_message *result = NULL;
685 struct ldb_dn *result_basedn = NULL;
686 int i;
687 char *p;
688 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
690 const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
691 const char * const _result_attrs_null[] = { NULL };
693 const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
694 const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
696 const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
697 const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", "objectClass", NULL};
699 const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
700 const char * const _result_attrs_guid[] = { "objectGUID", NULL};
702 const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
703 const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
705 const char * const _domain_attrs_none[] = { "ncName", "dnsRoot" , NULL};
706 const char * const _result_attrs_none[] = { NULL};
708 /* here we need to set the attrs lists for domain and result lookups */
709 switch (format_desired) {
710 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
711 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
712 domain_attrs = _domain_attrs_1779;
713 result_attrs = _result_attrs_null;
714 break;
715 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
716 domain_attrs = _domain_attrs_canonical;
717 result_attrs = _result_attrs_canonical;
718 break;
719 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
720 domain_attrs = _domain_attrs_nt4;
721 result_attrs = _result_attrs_nt4;
722 break;
723 case DRSUAPI_DS_NAME_FORMAT_GUID:
724 domain_attrs = _domain_attrs_guid;
725 result_attrs = _result_attrs_guid;
726 break;
727 case DRSUAPI_DS_NAME_FORMAT_DISPLAY:
728 domain_attrs = _domain_attrs_display;
729 result_attrs = _result_attrs_display;
730 break;
731 default:
732 domain_attrs = _domain_attrs_none;
733 result_attrs = _result_attrs_none;
734 break;
737 if (domain_filter) {
738 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
739 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
740 partitions_basedn,
741 LDB_SCOPE_ONELEVEL,
742 domain_attrs,
743 "%s", domain_filter);
745 if (ldb_ret != LDB_SUCCESS) {
746 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
747 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
748 return WERR_OK;
751 switch (domain_res->count) {
752 case 1:
753 break;
754 case 0:
755 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
756 return WERR_OK;
757 default:
758 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
759 return WERR_OK;
762 info1->dns_domain_name = samdb_result_string(domain_res->msgs[0], "dnsRoot", NULL);
763 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
764 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
765 } else {
766 info1->dns_domain_name = NULL;
767 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
770 if (result_filter) {
771 int ret;
772 struct ldb_result *res;
773 if (domain_res) {
774 result_basedn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
776 ret = ldb_search(sam_ctx, mem_ctx, &res,
777 result_basedn, LDB_SCOPE_SUBTREE,
778 result_attrs, "%s", result_filter);
779 if (ret != LDB_SUCCESS) {
780 talloc_free(result_res);
781 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
782 return WERR_OK;
784 ldb_ret = res->count;
785 result_res = res->msgs;
786 } else {
787 /* search with the 'phantom root' flag */
788 struct ldb_request *req;
790 res = talloc_zero(mem_ctx, struct ldb_result);
791 W_ERROR_HAVE_NO_MEMORY(res);
793 ret = ldb_build_search_req(&req, sam_ctx, mem_ctx,
794 ldb_get_root_basedn(sam_ctx),
795 LDB_SCOPE_SUBTREE,
796 result_filter,
797 result_attrs,
798 NULL,
799 res,
800 ldb_search_default_callback,
801 NULL);
802 if (ret == LDB_SUCCESS) {
803 struct ldb_search_options_control *search_options;
804 search_options = talloc(req, struct ldb_search_options_control);
805 W_ERROR_HAVE_NO_MEMORY(search_options);
806 search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
808 ret = ldb_request_add_control(req, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
810 if (ret != LDB_SUCCESS) {
811 talloc_free(res);
812 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
813 return WERR_OK;
816 ret = ldb_request(sam_ctx, req);
818 if (ret == LDB_SUCCESS) {
819 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
822 talloc_free(req);
824 if (ret != LDB_SUCCESS) {
825 DEBUG(2, ("DsCrackNameOneFilter phantom root search failed: %s",
826 ldb_errstring(sam_ctx)));
827 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
828 return WERR_OK;
830 ldb_ret = res->count;
831 result_res = res->msgs;
833 } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
834 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
835 result_attrs);
836 } else if (domain_res) {
837 name_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
838 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
839 result_attrs);
840 } else {
841 /* Can't happen */
842 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not availible: This can't happen..."));
843 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
844 return WERR_OK;
847 switch (ldb_ret) {
848 case 1:
849 result = result_res[0];
850 break;
851 case 0:
852 switch (format_offered) {
853 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL:
854 return DsCrackNameSPNAlias(sam_ctx, mem_ctx,
855 smb_krb5_context,
856 format_flags, format_offered, format_desired,
857 name, info1);
859 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
860 return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context,
861 format_flags, format_offered, format_desired,
862 name, info1);
864 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
865 return WERR_OK;
866 case -1:
867 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s", ldb_errstring(sam_ctx)));
868 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
869 return WERR_OK;
870 default:
871 switch (format_offered) {
872 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
873 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
875 const char *canonical_name = NULL; /* Not required, but we get warnings... */
876 /* We may need to manually filter further */
877 for (i = 0; i < ldb_ret; i++) {
878 switch (format_offered) {
879 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
880 canonical_name = ldb_dn_canonical_string(mem_ctx, result_res[i]->dn);
881 break;
882 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
883 canonical_name = ldb_dn_canonical_ex_string(mem_ctx, result_res[i]->dn);
884 break;
886 if (strcasecmp_m(canonical_name, name) == 0) {
887 result = result_res[i];
888 break;
891 if (!result) {
892 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
893 return WERR_OK;
896 default:
897 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
898 return WERR_OK;
902 info1->dns_domain_name = ldb_dn_canonical_string(mem_ctx, result->dn);
903 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
904 p = strchr(info1->dns_domain_name, '/');
905 if (p) {
906 p[0] = '\0';
909 /* here we can use result and domain_res[0] */
910 switch (format_desired) {
911 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
912 info1->result_name = ldb_dn_alloc_linearized(mem_ctx, result->dn);
913 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
915 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
916 return WERR_OK;
918 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
919 info1->result_name = samdb_result_string(result, "canonicalName", NULL);
920 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
921 return WERR_OK;
923 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
924 /* Not in the virtual ldb attribute */
925 return DsCrackNameOneSyntactical(mem_ctx,
926 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
927 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
928 result->dn, name, info1);
930 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
932 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result, "objectSid");
933 const char *_acc = "", *_dom = "";
935 if (samdb_find_attribute(sam_ctx, result, "objectClass", "domain")) {
937 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
938 partitions_basedn,
939 LDB_SCOPE_ONELEVEL,
940 domain_attrs,
941 "(ncName=%s)", ldb_dn_get_linearized(result->dn));
943 if (ldb_ret != LDB_SUCCESS) {
944 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
945 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
946 return WERR_OK;
949 switch (domain_res->count) {
950 case 1:
951 break;
952 case 0:
953 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
954 return WERR_OK;
955 default:
956 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
957 return WERR_OK;
959 _dom = samdb_result_string(domain_res->msgs[0], "nETBIOSName", NULL);
960 W_ERROR_HAVE_NO_MEMORY(_dom);
961 } else {
962 _acc = samdb_result_string(result, "sAMAccountName", NULL);
963 if (!_acc) {
964 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
965 return WERR_OK;
967 if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx, SID_BUILTIN), sid)) {
968 _dom = "BUILTIN";
969 } else {
970 const char *attrs[] = { NULL };
971 struct ldb_result *domain_res2;
972 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
973 if (!dom_sid) {
974 return WERR_OK;
976 dom_sid->num_auths--;
977 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
978 NULL,
979 LDB_SCOPE_BASE,
980 attrs,
981 "(&(objectSid=%s)(objectClass=domain))",
982 ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
984 if (ldb_ret != LDB_SUCCESS) {
985 DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s", ldb_errstring(sam_ctx)));
986 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
987 return WERR_OK;
990 switch (domain_res->count) {
991 case 1:
992 break;
993 case 0:
994 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
995 return WERR_OK;
996 default:
997 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
998 return WERR_OK;
1001 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res2,
1002 partitions_basedn,
1003 LDB_SCOPE_ONELEVEL,
1004 domain_attrs,
1005 "(ncName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
1007 if (ldb_ret != LDB_SUCCESS) {
1008 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
1009 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1010 return WERR_OK;
1013 switch (domain_res2->count) {
1014 case 1:
1015 break;
1016 case 0:
1017 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1018 return WERR_OK;
1019 default:
1020 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1021 return WERR_OK;
1023 _dom = samdb_result_string(domain_res2->msgs[0], "nETBIOSName", NULL);
1024 W_ERROR_HAVE_NO_MEMORY(_dom);
1028 info1->result_name = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
1029 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1031 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1032 return WERR_OK;
1034 case DRSUAPI_DS_NAME_FORMAT_GUID: {
1035 struct GUID guid;
1037 guid = samdb_result_guid(result, "objectGUID");
1039 info1->result_name = GUID_string2(mem_ctx, &guid);
1040 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1042 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1043 return WERR_OK;
1045 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
1046 info1->result_name = samdb_result_string(result, "displayName", NULL);
1047 if (!info1->result_name) {
1048 info1->result_name = samdb_result_string(result, "sAMAccountName", NULL);
1050 if (!info1->result_name) {
1051 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1052 } else {
1053 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1055 return WERR_OK;
1057 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
1058 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1059 return WERR_OK;
1061 case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN:
1062 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
1063 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1064 return WERR_OK;
1066 default:
1067 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1068 return WERR_OK;
1072 /* Given a user Principal Name (such as foo@bar.com),
1073 * return the user and domain DNs. This is used in the KDC to then
1074 * return the Keys and evaluate policy */
1076 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx,
1077 TALLOC_CTX *mem_ctx,
1078 const char *user_principal_name,
1079 struct ldb_dn **user_dn,
1080 struct ldb_dn **domain_dn)
1082 WERROR werr;
1083 struct drsuapi_DsNameInfo1 info1;
1084 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1085 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
1086 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1087 user_principal_name,
1088 &info1);
1089 if (!W_ERROR_IS_OK(werr)) {
1090 return werror_to_ntstatus(werr);
1092 switch (info1.status) {
1093 case DRSUAPI_DS_NAME_STATUS_OK:
1094 break;
1095 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1096 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1097 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1098 return NT_STATUS_NO_SUCH_USER;
1099 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1100 default:
1101 return NT_STATUS_UNSUCCESSFUL;
1104 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1106 if (domain_dn) {
1107 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1108 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1109 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1110 talloc_asprintf(mem_ctx, "%s/",
1111 info1.dns_domain_name),
1112 &info1);
1113 if (!W_ERROR_IS_OK(werr)) {
1114 return werror_to_ntstatus(werr);
1116 switch (info1.status) {
1117 case DRSUAPI_DS_NAME_STATUS_OK:
1118 break;
1119 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1120 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1121 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1122 return NT_STATUS_NO_SUCH_USER;
1123 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1124 default:
1125 return NT_STATUS_UNSUCCESSFUL;
1128 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1131 return NT_STATUS_OK;
1135 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1136 * return the user and domain DNs. This is used in the KDC to then
1137 * return the Keys and evaluate policy */
1139 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx,
1140 TALLOC_CTX *mem_ctx,
1141 const char *service_principal_name,
1142 struct ldb_dn **user_dn,
1143 struct ldb_dn **domain_dn)
1145 WERROR werr;
1146 struct drsuapi_DsNameInfo1 info1;
1147 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1148 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
1149 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1150 service_principal_name,
1151 &info1);
1152 if (!W_ERROR_IS_OK(werr)) {
1153 return werror_to_ntstatus(werr);
1155 switch (info1.status) {
1156 case DRSUAPI_DS_NAME_STATUS_OK:
1157 break;
1158 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1159 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1160 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1161 return NT_STATUS_NO_SUCH_USER;
1162 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1163 default:
1164 return NT_STATUS_UNSUCCESSFUL;
1167 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1169 if (domain_dn) {
1170 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1171 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1172 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1173 talloc_asprintf(mem_ctx, "%s/",
1174 info1.dns_domain_name),
1175 &info1);
1176 if (!W_ERROR_IS_OK(werr)) {
1177 return werror_to_ntstatus(werr);
1179 switch (info1.status) {
1180 case DRSUAPI_DS_NAME_STATUS_OK:
1181 break;
1182 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1183 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1184 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1185 return NT_STATUS_NO_SUCH_USER;
1186 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1187 default:
1188 return NT_STATUS_UNSUCCESSFUL;
1191 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1194 return NT_STATUS_OK;
1198 NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1199 struct event_context *ev_ctx,
1200 struct loadparm_context *lp_ctx,
1201 uint32_t format_offered,
1202 const char *name,
1203 const char **nt4_domain, const char **nt4_account)
1205 WERROR werr;
1206 struct drsuapi_DsNameInfo1 info1;
1207 struct ldb_context *ldb;
1208 char *p;
1210 /* Handle anonymous bind */
1211 if (!name || !*name) {
1212 *nt4_domain = "";
1213 *nt4_account = "";
1214 return NT_STATUS_OK;
1217 ldb = samdb_connect(mem_ctx, ev_ctx, lp_ctx, system_session(mem_ctx, lp_ctx));
1218 if (ldb == NULL) {
1219 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1222 werr = DsCrackNameOneName(ldb, mem_ctx, 0,
1223 format_offered,
1224 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
1225 name,
1226 &info1);
1227 if (!W_ERROR_IS_OK(werr)) {
1228 return werror_to_ntstatus(werr);
1230 switch (info1.status) {
1231 case DRSUAPI_DS_NAME_STATUS_OK:
1232 break;
1233 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1234 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1235 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1236 return NT_STATUS_NO_SUCH_USER;
1237 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1238 default:
1239 return NT_STATUS_UNSUCCESSFUL;
1242 *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
1244 p = strchr(*nt4_domain, '\\');
1245 if (!p) {
1246 return NT_STATUS_INVALID_PARAMETER;
1248 p[0] = '\0';
1250 if (p[1]) {
1251 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
1254 if (!*nt4_account || !*nt4_domain) {
1255 return NT_STATUS_NO_MEMORY;
1258 return NT_STATUS_OK;
1261 NTSTATUS crack_auto_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1262 struct event_context *ev_ctx,
1263 struct loadparm_context *lp_ctx,
1264 const char *name,
1265 const char **nt4_domain,
1266 const char **nt4_account)
1268 uint32_t format_offered = DRSUAPI_DS_NAME_FORMAT_UKNOWN;
1270 /* Handle anonymous bind */
1271 if (!name || !*name) {
1272 *nt4_domain = "";
1273 *nt4_account = "";
1274 return NT_STATUS_OK;
1277 if (strchr_m(name, '=')) {
1278 format_offered = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
1279 } else if (strchr_m(name, '@')) {
1280 format_offered = DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL;
1281 } else if (strchr_m(name, '\\')) {
1282 format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
1283 } else if (strchr_m(name, '/')) {
1284 format_offered = DRSUAPI_DS_NAME_FORMAT_CANONICAL;
1287 return crack_name_to_nt4_name(mem_ctx, ev_ctx, lp_ctx, format_offered, name, nt4_domain, nt4_account);