s3: re-run make samba3-idl.
[Samba/gbeck.git] / source4 / dsdb / samdb / cracknames.c
blob369b06c30595428bbe5de7ec963b9b7c898f201f
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, service_dn, LDB_SCOPE_BASE, "(objectClass=nTDSService)",
111 directory_attrs, &res);
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;
124 talloc_steal(tmp_ctx, res);
126 spnmappings = ldb_msg_find_element(res->msgs[0], "sPNMappings");
127 if (!spnmappings || spnmappings->num_values == 0) {
128 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute", service_dn_str));
129 talloc_free(tmp_ctx);
130 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
133 for (i = 0; i < spnmappings->num_values; i++) {
134 char *mapping, *p, *str;
135 mapping = talloc_strdup(tmp_ctx,
136 (const char *)spnmappings->values[i].data);
137 if (!mapping) {
138 DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str));
139 talloc_free(tmp_ctx);
140 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
143 /* C string manipulation sucks */
145 p = strchr(mapping, '=');
146 if (!p) {
147 DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n",
148 service_dn_str, mapping));
149 talloc_free(tmp_ctx);
150 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
152 p[0] = '\0';
153 p++;
154 do {
155 str = p;
156 p = strchr(p, ',');
157 if (p) {
158 p[0] = '\0';
159 p++;
161 if (strcasecmp(str, alias_from) == 0) {
162 *alias_to = mapping;
163 talloc_steal(mem_ctx, mapping);
164 talloc_free(tmp_ctx);
165 return DRSUAPI_DS_NAME_STATUS_OK;
167 } while (p);
169 DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from));
170 talloc_free(tmp_ctx);
171 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
174 /* When cracking a ServicePrincipalName, many services may be served
175 * by the host/ servicePrincipalName. The incoming query is for cifs/
176 * but we translate it here, and search on host/. This is done after
177 * the cifs/ entry has been searched for, making this a fallback */
179 static WERROR DsCrackNameSPNAlias(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
180 struct smb_krb5_context *smb_krb5_context,
181 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
182 const char *name, struct drsuapi_DsNameInfo1 *info1)
184 WERROR wret;
185 krb5_error_code ret;
186 krb5_principal principal;
187 const char *service, *dns_name;
188 char *new_service;
189 char *new_princ;
190 enum drsuapi_DsNameStatus namestatus;
192 /* parse principal */
193 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context,
194 name, KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
195 if (ret) {
196 DEBUG(2, ("Could not parse principal: %s: %s",
197 name, smb_get_krb5_error_message(smb_krb5_context->krb5_context,
198 ret, mem_ctx)));
199 return WERR_NOMEM;
202 /* grab cifs/, http/ etc */
204 /* This is checked for in callers, but be safe */
205 if (principal->name.name_string.len < 2) {
206 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
207 return WERR_OK;
209 service = principal->name.name_string.val[0];
210 dns_name = principal->name.name_string.val[1];
212 /* MAP it */
213 namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context,
214 sam_ctx, mem_ctx,
215 service, &new_service);
217 if (namestatus == DRSUAPI_DS_NAME_STATUS_NOT_FOUND) {
218 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
219 info1->dns_domain_name = talloc_strdup(mem_ctx, dns_name);
220 if (!info1->dns_domain_name) {
221 krb5_free_principal(smb_krb5_context->krb5_context, principal);
222 return WERR_NOMEM;
224 return WERR_OK;
225 } else if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
226 info1->status = namestatus;
227 krb5_free_principal(smb_krb5_context->krb5_context, principal);
228 return WERR_OK;
231 /* ooh, very nasty playing around in the Principal... */
232 free(principal->name.name_string.val[0]);
233 principal->name.name_string.val[0] = strdup(new_service);
234 if (!principal->name.name_string.val[0]) {
235 krb5_free_principal(smb_krb5_context->krb5_context, principal);
236 return WERR_NOMEM;
239 /* reform principal */
240 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
241 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &new_princ);
243 if (ret) {
244 krb5_free_principal(smb_krb5_context->krb5_context, principal);
245 return WERR_NOMEM;
248 wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
249 new_princ, info1);
250 free(new_princ);
251 if (W_ERROR_IS_OK(wret) && (info1->status == DRSUAPI_DS_NAME_STATUS_NOT_FOUND)) {
252 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
253 info1->dns_domain_name = talloc_strdup(mem_ctx, dns_name);
254 if (!info1->dns_domain_name) {
255 wret = WERR_NOMEM;
258 krb5_free_principal(smb_krb5_context->krb5_context, principal);
259 return wret;
262 /* Subcase of CrackNames, for the userPrincipalName */
264 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
265 struct smb_krb5_context *smb_krb5_context,
266 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
267 const char *name, struct drsuapi_DsNameInfo1 *info1)
269 int ldb_ret;
270 WERROR status;
271 const char *domain_filter = NULL;
272 const char *result_filter = NULL;
273 krb5_error_code ret;
274 krb5_principal principal;
275 char **realm;
276 char *unparsed_name_short;
277 const char *domain_attrs[] = { NULL };
278 struct ldb_result *domain_res = NULL;
280 /* Prevent recursion */
281 if (!name) {
282 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
283 return WERR_OK;
286 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
287 KRB5_PRINCIPAL_PARSE_MUST_REALM, &principal);
288 if (ret) {
289 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
290 return WERR_OK;
293 realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
295 ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res,
296 samdb_partitions_dn(sam_ctx, mem_ctx),
297 LDB_SCOPE_ONELEVEL,
298 domain_attrs,
299 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
300 ldb_binary_encode_string(mem_ctx, *realm),
301 ldb_binary_encode_string(mem_ctx, *realm));
303 if (ldb_ret != LDB_SUCCESS) {
304 DEBUG(2, ("DsCrackNameUPN domain ref search failed: %s", ldb_errstring(sam_ctx)));
305 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
306 return WERR_OK;
309 switch (domain_res->count) {
310 case 1:
311 break;
312 case 0:
313 return dns_domain_from_principal(mem_ctx, smb_krb5_context,
314 name, info1);
315 default:
316 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
317 return WERR_OK;
320 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
321 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
322 krb5_free_principal(smb_krb5_context->krb5_context, principal);
324 if (ret) {
325 free(unparsed_name_short);
326 return WERR_NOMEM;
329 /* This may need to be extended for more userPrincipalName variations */
330 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
331 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
333 domain_filter = talloc_asprintf(mem_ctx, "(distinguishedName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
335 if (!result_filter || !domain_filter) {
336 free(unparsed_name_short);
337 return WERR_NOMEM;
339 status = DsCrackNameOneFilter(sam_ctx, mem_ctx,
340 smb_krb5_context,
341 format_flags, format_offered, format_desired,
342 NULL, unparsed_name_short, domain_filter, result_filter,
343 info1);
344 free(unparsed_name_short);
346 return status;
349 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
351 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
352 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
353 const char *name, struct drsuapi_DsNameInfo1 *info1)
355 krb5_error_code ret;
356 const char *domain_filter = NULL;
357 const char *result_filter = NULL;
358 struct ldb_dn *name_dn = NULL;
360 struct smb_krb5_context *smb_krb5_context;
361 ret = smb_krb5_init_context(mem_ctx,
362 ldb_get_event_context(sam_ctx),
363 (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"),
364 &smb_krb5_context);
366 if (ret) {
367 return WERR_NOMEM;
370 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
371 info1->dns_domain_name = NULL;
372 info1->result_name = NULL;
374 if (!name) {
375 return WERR_INVALID_PARAM;
378 /* TODO: - fill the correct names in all cases!
379 * - handle format_flags
382 /* here we need to set the domain_filter and/or the result_filter */
383 switch (format_offered) {
384 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
385 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
387 char *str, *s, *account;
389 if (strlen(name) == 0) {
390 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
391 return WERR_OK;
394 str = talloc_strdup(mem_ctx, name);
395 W_ERROR_HAVE_NO_MEMORY(str);
397 if (format_offered == DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX) {
398 /* Look backwards for the \n, and replace it with / */
399 s = strrchr(str, '\n');
400 if (!s) {
401 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
402 return WERR_OK;
404 s[0] = '/';
407 s = strchr(str, '/');
408 if (!s) {
409 /* there must be at least one / */
410 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
411 return WERR_OK;
414 s[0] = '\0';
415 s++;
417 domain_filter = talloc_asprintf(mem_ctx, "(&(objectClass=crossRef)(ncName=%s))",
418 ldb_dn_get_linearized(samdb_dns_domain_to_dn(sam_ctx, mem_ctx, str)));
419 W_ERROR_HAVE_NO_MEMORY(domain_filter);
421 /* There may not be anything after the domain component (search for the domain itself) */
422 if (s[0]) {
424 account = strrchr(s, '/');
425 if (!account) {
426 account = s;
427 } else {
428 account++;
430 account = ldb_binary_encode_string(mem_ctx, account);
431 W_ERROR_HAVE_NO_MEMORY(account);
432 result_filter = talloc_asprintf(mem_ctx, "(name=%s)",
433 account);
434 W_ERROR_HAVE_NO_MEMORY(result_filter);
436 break;
438 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
439 char *p;
440 char *domain;
441 const char *account = NULL;
443 domain = talloc_strdup(mem_ctx, name);
444 W_ERROR_HAVE_NO_MEMORY(domain);
446 p = strchr(domain, '\\');
447 if (!p) {
448 /* invalid input format */
449 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
450 return WERR_OK;
452 p[0] = '\0';
454 if (p[1]) {
455 account = &p[1];
458 domain_filter = talloc_asprintf(mem_ctx,
459 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
460 ldb_binary_encode_string(mem_ctx, domain));
461 W_ERROR_HAVE_NO_MEMORY(domain_filter);
462 if (account) {
463 result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
464 ldb_binary_encode_string(mem_ctx, account));
465 W_ERROR_HAVE_NO_MEMORY(result_filter);
468 talloc_free(domain);
469 break;
472 /* A LDAP DN as a string */
473 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
474 domain_filter = NULL;
475 name_dn = ldb_dn_new(mem_ctx, sam_ctx, name);
476 if (! ldb_dn_validate(name_dn)) {
477 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
478 return WERR_OK;
480 break;
483 /* A GUID as a string */
484 case DRSUAPI_DS_NAME_FORMAT_GUID: {
485 struct GUID guid;
486 char *ldap_guid;
487 NTSTATUS nt_status;
488 domain_filter = NULL;
490 nt_status = GUID_from_string(name, &guid);
491 if (!NT_STATUS_IS_OK(nt_status)) {
492 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
493 return WERR_OK;
496 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
497 if (!ldap_guid) {
498 return WERR_NOMEM;
500 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
501 ldap_guid);
502 W_ERROR_HAVE_NO_MEMORY(result_filter);
503 break;
505 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
506 domain_filter = NULL;
508 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
509 ldb_binary_encode_string(mem_ctx, name),
510 ldb_binary_encode_string(mem_ctx, name));
511 W_ERROR_HAVE_NO_MEMORY(result_filter);
512 break;
515 /* A S-1234-5678 style string */
516 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
517 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
518 char *ldap_sid;
520 domain_filter = NULL;
521 if (!sid) {
522 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
523 return WERR_OK;
525 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx,
526 sid);
527 if (!ldap_sid) {
528 return WERR_NOMEM;
530 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
531 ldap_sid);
532 W_ERROR_HAVE_NO_MEMORY(result_filter);
533 break;
535 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
536 krb5_principal principal;
537 char *unparsed_name;
538 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
539 if (ret) {
540 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
541 return WERR_OK;
544 domain_filter = NULL;
546 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
547 if (ret) {
548 krb5_free_principal(smb_krb5_context->krb5_context, principal);
549 return WERR_NOMEM;
552 krb5_free_principal(smb_krb5_context->krb5_context, principal);
553 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))",
554 ldb_binary_encode_string(mem_ctx, unparsed_name));
556 free(unparsed_name);
557 W_ERROR_HAVE_NO_MEMORY(result_filter);
558 break;
560 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
561 krb5_principal principal;
562 char *unparsed_name_short;
563 char *service;
564 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
565 if (ret == 0 && principal->name.name_string.len < 2) {
566 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
567 krb5_free_principal(smb_krb5_context->krb5_context, principal);
568 return WERR_OK;
570 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
571 KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
572 if (ret) {
573 krb5_free_principal(smb_krb5_context->krb5_context, principal);
575 return dns_domain_from_principal(mem_ctx, smb_krb5_context,
576 name, info1);
579 domain_filter = NULL;
581 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
582 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
583 if (ret) {
584 krb5_free_principal(smb_krb5_context->krb5_context, principal);
585 return WERR_NOMEM;
588 service = principal->name.name_string.val[0];
589 if ((principal->name.name_string.len == 2) && (strcasecmp(service, "host") == 0)) {
590 /* the 'cn' attribute is just the leading part of the name */
591 char *computer_name;
592 computer_name = talloc_strndup(mem_ctx, principal->name.name_string.val[1],
593 strcspn(principal->name.name_string.val[1], "."));
594 if (computer_name == NULL) {
595 return WERR_NOMEM;
598 result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
599 ldb_binary_encode_string(mem_ctx, unparsed_name_short),
600 ldb_binary_encode_string(mem_ctx, computer_name));
601 } else {
602 result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
603 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
605 krb5_free_principal(smb_krb5_context->krb5_context, principal);
606 free(unparsed_name_short);
607 W_ERROR_HAVE_NO_MEMORY(result_filter);
609 break;
611 default: {
612 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
613 return WERR_OK;
618 if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
619 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
620 name_dn, name, info1);
623 return DsCrackNameOneFilter(sam_ctx, mem_ctx,
624 smb_krb5_context,
625 format_flags, format_offered, format_desired,
626 name_dn, name,
627 domain_filter, result_filter,
628 info1);
631 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
632 * (FQDN_1779) into a canoical name without actually searching the
633 * database */
635 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
636 uint32_t format_offered, uint32_t format_desired,
637 struct ldb_dn *name_dn, const char *name,
638 struct drsuapi_DsNameInfo1 *info1)
640 char *cracked;
641 if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
642 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
643 return WERR_OK;
646 switch (format_desired) {
647 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
648 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
649 break;
650 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
651 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
652 break;
653 default:
654 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
655 return WERR_OK;
657 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
658 info1->result_name = cracked;
659 if (!cracked) {
660 return WERR_NOMEM;
663 return WERR_OK;
666 /* Given a filter for the domain, and one for the result, perform the
667 * ldb search. The format offered and desired flags change the
668 * behaviours, including what attributes to return.
670 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
673 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
674 struct smb_krb5_context *smb_krb5_context,
675 uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
676 struct ldb_dn *name_dn, const char *name,
677 const char *domain_filter, const char *result_filter,
678 struct drsuapi_DsNameInfo1 *info1)
680 int ldb_ret;
681 struct ldb_result *domain_res = NULL;
682 const char * const *domain_attrs;
683 const char * const *result_attrs;
684 struct ldb_message **result_res = NULL;
685 struct ldb_message *result = NULL;
686 struct ldb_dn *result_basedn = NULL;
687 int i;
688 char *p;
689 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
691 const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
692 const char * const _result_attrs_null[] = { NULL };
694 const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
695 const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
697 const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
698 const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", "objectClass", NULL};
700 const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
701 const char * const _result_attrs_guid[] = { "objectGUID", NULL};
703 const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
704 const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
706 const char * const _domain_attrs_none[] = { "ncName", "dnsRoot" , NULL};
707 const char * const _result_attrs_none[] = { NULL};
709 /* here we need to set the attrs lists for domain and result lookups */
710 switch (format_desired) {
711 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
712 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
713 domain_attrs = _domain_attrs_1779;
714 result_attrs = _result_attrs_null;
715 break;
716 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
717 domain_attrs = _domain_attrs_canonical;
718 result_attrs = _result_attrs_canonical;
719 break;
720 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
721 domain_attrs = _domain_attrs_nt4;
722 result_attrs = _result_attrs_nt4;
723 break;
724 case DRSUAPI_DS_NAME_FORMAT_GUID:
725 domain_attrs = _domain_attrs_guid;
726 result_attrs = _result_attrs_guid;
727 break;
728 case DRSUAPI_DS_NAME_FORMAT_DISPLAY:
729 domain_attrs = _domain_attrs_display;
730 result_attrs = _result_attrs_display;
731 break;
732 default:
733 domain_attrs = _domain_attrs_none;
734 result_attrs = _result_attrs_none;
735 break;
738 if (domain_filter) {
739 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
740 ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res,
741 partitions_basedn,
742 LDB_SCOPE_ONELEVEL,
743 domain_attrs,
744 "%s", domain_filter);
746 if (ldb_ret != LDB_SUCCESS) {
747 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
748 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
749 return WERR_OK;
752 switch (domain_res->count) {
753 case 1:
754 break;
755 case 0:
756 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
757 return WERR_OK;
758 default:
759 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
760 return WERR_OK;
763 info1->dns_domain_name = samdb_result_string(domain_res->msgs[0], "dnsRoot", NULL);
764 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
765 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
766 } else {
767 info1->dns_domain_name = NULL;
768 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
771 if (result_filter) {
772 int ret;
773 struct ldb_result *res;
774 if (domain_res) {
775 result_basedn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
777 ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &res,
778 result_basedn, LDB_SCOPE_SUBTREE,
779 result_attrs, "%s", result_filter);
780 if (ret != LDB_SUCCESS) {
781 talloc_free(result_res);
782 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
783 return WERR_OK;
785 ldb_ret = res->count;
786 result_res = res->msgs;
787 } else {
788 /* search with the 'phantom root' flag */
789 struct ldb_request *req;
791 res = talloc_zero(mem_ctx, struct ldb_result);
792 W_ERROR_HAVE_NO_MEMORY(res);
794 ret = ldb_build_search_req(&req, sam_ctx, mem_ctx,
795 ldb_get_root_basedn(sam_ctx),
796 LDB_SCOPE_SUBTREE,
797 result_filter,
798 result_attrs,
799 NULL,
800 res,
801 ldb_search_default_callback);
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 ldb_set_timeout(sam_ctx, req, 0); /* use default timeout */
818 ret = ldb_request(sam_ctx, req);
820 if (ret == LDB_SUCCESS) {
821 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
824 talloc_free(req);
826 if (ret != LDB_SUCCESS) {
827 DEBUG(2, ("DsCrackNameOneFilter phantom root search failed: %s",
828 ldb_errstring(sam_ctx)));
829 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
830 return WERR_OK;
832 ldb_ret = res->count;
833 result_res = res->msgs;
835 } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
836 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
837 result_attrs);
838 } else if (domain_res) {
839 name_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
840 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
841 result_attrs);
842 } else {
843 /* Can't happen */
844 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not availible: This can't happen..."));
845 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
846 return WERR_OK;
849 switch (ldb_ret) {
850 case 1:
851 result = result_res[0];
852 break;
853 case 0:
854 switch (format_offered) {
855 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL:
856 return DsCrackNameSPNAlias(sam_ctx, mem_ctx,
857 smb_krb5_context,
858 format_flags, format_offered, format_desired,
859 name, info1);
861 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
862 return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context,
863 format_flags, format_offered, format_desired,
864 name, info1);
866 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
867 return WERR_OK;
868 case -1:
869 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s", ldb_errstring(sam_ctx)));
870 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
871 return WERR_OK;
872 default:
873 switch (format_offered) {
874 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
875 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
877 const char *canonical_name = NULL; /* Not required, but we get warnings... */
878 /* We may need to manually filter further */
879 for (i = 0; i < ldb_ret; i++) {
880 switch (format_offered) {
881 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
882 canonical_name = ldb_dn_canonical_string(mem_ctx, result_res[i]->dn);
883 break;
884 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
885 canonical_name = ldb_dn_canonical_ex_string(mem_ctx, result_res[i]->dn);
886 break;
888 if (strcasecmp_m(canonical_name, name) == 0) {
889 result = result_res[i];
890 break;
893 if (!result) {
894 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
895 return WERR_OK;
898 default:
899 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
900 return WERR_OK;
904 info1->dns_domain_name = ldb_dn_canonical_string(mem_ctx, result->dn);
905 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
906 p = strchr(info1->dns_domain_name, '/');
907 if (p) {
908 p[0] = '\0';
911 /* here we can use result and domain_res[0] */
912 switch (format_desired) {
913 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
914 info1->result_name = ldb_dn_alloc_linearized(mem_ctx, result->dn);
915 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
917 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
918 return WERR_OK;
920 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
921 info1->result_name = samdb_result_string(result, "canonicalName", NULL);
922 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
923 return WERR_OK;
925 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
926 /* Not in the virtual ldb attribute */
927 return DsCrackNameOneSyntactical(mem_ctx,
928 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
929 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
930 result->dn, name, info1);
932 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
934 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result, "objectSid");
935 const char *_acc = "", *_dom = "";
937 if (samdb_find_attribute(sam_ctx, result, "objectClass", "domain")) {
939 ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res,
940 partitions_basedn,
941 LDB_SCOPE_ONELEVEL,
942 domain_attrs,
943 "(ncName=%s)", ldb_dn_get_linearized(result->dn));
945 if (ldb_ret != LDB_SUCCESS) {
946 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
947 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
948 return WERR_OK;
951 switch (domain_res->count) {
952 case 1:
953 break;
954 case 0:
955 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
956 return WERR_OK;
957 default:
958 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
959 return WERR_OK;
961 _dom = samdb_result_string(domain_res->msgs[0], "nETBIOSName", NULL);
962 W_ERROR_HAVE_NO_MEMORY(_dom);
963 } else {
964 _acc = samdb_result_string(result, "sAMAccountName", NULL);
965 if (!_acc) {
966 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
967 return WERR_OK;
969 if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx, SID_BUILTIN), sid)) {
970 _dom = "BUILTIN";
971 } else {
972 const char *attrs[] = { NULL };
973 struct ldb_result *domain_res2;
974 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
975 if (!dom_sid) {
976 return WERR_OK;
978 dom_sid->num_auths--;
979 ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res,
980 NULL,
981 LDB_SCOPE_BASE,
982 attrs,
983 "(&(objectSid=%s)(objectClass=domain))",
984 ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
986 if (ldb_ret != LDB_SUCCESS) {
987 DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s", ldb_errstring(sam_ctx)));
988 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
989 return WERR_OK;
992 switch (domain_res->count) {
993 case 1:
994 break;
995 case 0:
996 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
997 return WERR_OK;
998 default:
999 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1000 return WERR_OK;
1003 ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res2,
1004 partitions_basedn,
1005 LDB_SCOPE_ONELEVEL,
1006 domain_attrs,
1007 "(ncName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
1009 if (ldb_ret != LDB_SUCCESS) {
1010 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
1011 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1012 return WERR_OK;
1015 switch (domain_res2->count) {
1016 case 1:
1017 break;
1018 case 0:
1019 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1020 return WERR_OK;
1021 default:
1022 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1023 return WERR_OK;
1025 _dom = samdb_result_string(domain_res2->msgs[0], "nETBIOSName", NULL);
1026 W_ERROR_HAVE_NO_MEMORY(_dom);
1030 info1->result_name = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
1031 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1033 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1034 return WERR_OK;
1036 case DRSUAPI_DS_NAME_FORMAT_GUID: {
1037 struct GUID guid;
1039 guid = samdb_result_guid(result, "objectGUID");
1041 info1->result_name = GUID_string2(mem_ctx, &guid);
1042 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1044 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1045 return WERR_OK;
1047 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
1048 info1->result_name = samdb_result_string(result, "displayName", NULL);
1049 if (!info1->result_name) {
1050 info1->result_name = samdb_result_string(result, "sAMAccountName", NULL);
1052 if (!info1->result_name) {
1053 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1054 } else {
1055 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1057 return WERR_OK;
1059 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
1060 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1061 return WERR_OK;
1063 case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN:
1064 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
1065 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1066 return WERR_OK;
1068 default:
1069 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1070 return WERR_OK;
1074 /* Given a user Principal Name (such as foo@bar.com),
1075 * return the user and domain DNs. This is used in the KDC to then
1076 * return the Keys and evaluate policy */
1078 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx,
1079 TALLOC_CTX *mem_ctx,
1080 const char *user_principal_name,
1081 struct ldb_dn **user_dn,
1082 struct ldb_dn **domain_dn)
1084 WERROR werr;
1085 struct drsuapi_DsNameInfo1 info1;
1086 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1087 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
1088 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1089 user_principal_name,
1090 &info1);
1091 if (!W_ERROR_IS_OK(werr)) {
1092 return werror_to_ntstatus(werr);
1094 switch (info1.status) {
1095 case DRSUAPI_DS_NAME_STATUS_OK:
1096 break;
1097 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1098 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1099 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1100 return NT_STATUS_NO_SUCH_USER;
1101 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1102 default:
1103 return NT_STATUS_UNSUCCESSFUL;
1106 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1108 if (domain_dn) {
1109 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1110 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1111 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1112 talloc_asprintf(mem_ctx, "%s/",
1113 info1.dns_domain_name),
1114 &info1);
1115 if (!W_ERROR_IS_OK(werr)) {
1116 return werror_to_ntstatus(werr);
1118 switch (info1.status) {
1119 case DRSUAPI_DS_NAME_STATUS_OK:
1120 break;
1121 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1122 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1123 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1124 return NT_STATUS_NO_SUCH_USER;
1125 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1126 default:
1127 return NT_STATUS_UNSUCCESSFUL;
1130 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1133 return NT_STATUS_OK;
1137 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1138 * return the user and domain DNs. This is used in the KDC to then
1139 * return the Keys and evaluate policy */
1141 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx,
1142 TALLOC_CTX *mem_ctx,
1143 const char *service_principal_name,
1144 struct ldb_dn **user_dn,
1145 struct ldb_dn **domain_dn)
1147 WERROR werr;
1148 struct drsuapi_DsNameInfo1 info1;
1149 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1150 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
1151 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1152 service_principal_name,
1153 &info1);
1154 if (!W_ERROR_IS_OK(werr)) {
1155 return werror_to_ntstatus(werr);
1157 switch (info1.status) {
1158 case DRSUAPI_DS_NAME_STATUS_OK:
1159 break;
1160 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1161 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1162 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1163 return NT_STATUS_NO_SUCH_USER;
1164 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1165 default:
1166 return NT_STATUS_UNSUCCESSFUL;
1169 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1171 if (domain_dn) {
1172 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1173 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1174 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1175 talloc_asprintf(mem_ctx, "%s/",
1176 info1.dns_domain_name),
1177 &info1);
1178 if (!W_ERROR_IS_OK(werr)) {
1179 return werror_to_ntstatus(werr);
1181 switch (info1.status) {
1182 case DRSUAPI_DS_NAME_STATUS_OK:
1183 break;
1184 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1185 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1186 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1187 return NT_STATUS_NO_SUCH_USER;
1188 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1189 default:
1190 return NT_STATUS_UNSUCCESSFUL;
1193 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1196 return NT_STATUS_OK;
1200 NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1201 struct event_context *ev_ctx,
1202 struct loadparm_context *lp_ctx,
1203 uint32_t format_offered,
1204 const char *name,
1205 const char **nt4_domain, const char **nt4_account)
1207 WERROR werr;
1208 struct drsuapi_DsNameInfo1 info1;
1209 struct ldb_context *ldb;
1210 char *p;
1212 /* Handle anonymous bind */
1213 if (!name || !*name) {
1214 *nt4_domain = "";
1215 *nt4_account = "";
1216 return NT_STATUS_OK;
1219 ldb = samdb_connect(mem_ctx, ev_ctx, lp_ctx, system_session(mem_ctx, lp_ctx));
1220 if (ldb == NULL) {
1221 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1224 werr = DsCrackNameOneName(ldb, mem_ctx, 0,
1225 format_offered,
1226 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
1227 name,
1228 &info1);
1229 if (!W_ERROR_IS_OK(werr)) {
1230 return werror_to_ntstatus(werr);
1232 switch (info1.status) {
1233 case DRSUAPI_DS_NAME_STATUS_OK:
1234 break;
1235 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1236 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1237 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1238 return NT_STATUS_NO_SUCH_USER;
1239 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1240 default:
1241 return NT_STATUS_UNSUCCESSFUL;
1244 *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
1246 p = strchr(*nt4_domain, '\\');
1247 if (!p) {
1248 return NT_STATUS_INVALID_PARAMETER;
1250 p[0] = '\0';
1252 if (p[1]) {
1253 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
1256 if (!*nt4_account || !*nt4_domain) {
1257 return NT_STATUS_NO_MEMORY;
1260 return NT_STATUS_OK;
1263 NTSTATUS crack_auto_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1264 struct event_context *ev_ctx,
1265 struct loadparm_context *lp_ctx,
1266 const char *name,
1267 const char **nt4_domain,
1268 const char **nt4_account)
1270 uint32_t format_offered = DRSUAPI_DS_NAME_FORMAT_UKNOWN;
1272 /* Handle anonymous bind */
1273 if (!name || !*name) {
1274 *nt4_domain = "";
1275 *nt4_account = "";
1276 return NT_STATUS_OK;
1279 if (strchr_m(name, '=')) {
1280 format_offered = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
1281 } else if (strchr_m(name, '@')) {
1282 format_offered = DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL;
1283 } else if (strchr_m(name, '\\')) {
1284 format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
1285 } else if (strchr_m(name, '/')) {
1286 format_offered = DRSUAPI_DS_NAME_FORMAT_CANONICAL;
1289 return crack_name_to_nt4_name(mem_ctx, ev_ctx, lp_ctx, format_offered, name, nt4_domain, nt4_account);