notify_inotify: Slightly simplify inotify_watch
[Samba.git] / source4 / dsdb / samdb / cracknames.c
blob0d1a80050a336a7e6121922c925b3f7553d1b469
1 /*
2 Unix SMB/CIFS implementation.
4 crachnames implementation for the drsuapi pipe
5 DsCrackNames()
7 Copyright (C) Stefan Metzmacher 2004
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
9 Copyright (C) Matthieu Patou <mat@matws.net> 2012
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "librpc/gen_ndr/drsuapi.h"
27 #include "lib/events/events.h"
28 #include <ldb.h>
29 #include <ldb_errors.h>
30 #include "auth/kerberos/kerberos.h"
31 #include "libcli/ldap/ldap_ndr.h"
32 #include "libcli/security/security.h"
33 #include "auth/auth.h"
34 #include "../lib/util/util_ldb.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/common/util.h"
37 #include "param/param.h"
39 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
40 struct smb_krb5_context *smb_krb5_context,
41 uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
42 enum drsuapi_DsNameFormat 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, int scope, struct ldb_dn *search_dn);
46 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
47 enum drsuapi_DsNameFormat format_offered,
48 enum drsuapi_DsNameFormat format_desired,
49 struct ldb_dn *name_dn, const char *name,
50 struct drsuapi_DsNameInfo1 *info1);
52 static WERROR dns_domain_from_principal(TALLOC_CTX *mem_ctx, struct smb_krb5_context *smb_krb5_context,
53 const char *name,
54 struct drsuapi_DsNameInfo1 *info1)
56 krb5_error_code ret;
57 krb5_principal principal;
58 /* perhaps it's a principal with a realm, so return the right 'domain only' response */
59 char *realm;
60 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
61 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM, &principal);
62 if (ret) {
63 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
64 return WERR_OK;
67 realm = smb_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);
71 free(realm);
73 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
75 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
76 return WERR_OK;
79 static enum drsuapi_DsNameStatus LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx,
80 TALLOC_CTX *mem_ctx,
81 const char *alias_from,
82 char **alias_to)
84 unsigned int i;
85 int ret;
86 struct ldb_result *res;
87 struct ldb_message_element *spnmappings;
88 TALLOC_CTX *tmp_ctx;
89 struct ldb_dn *service_dn;
90 char *service_dn_str;
92 const char *directory_attrs[] = {
93 "sPNMappings",
94 NULL
97 tmp_ctx = talloc_new(mem_ctx);
98 if (!tmp_ctx) {
99 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
102 service_dn = ldb_dn_new(tmp_ctx, ldb_ctx, "CN=Directory Service,CN=Windows NT,CN=Services");
103 if ( ! ldb_dn_add_base(service_dn, ldb_get_config_basedn(ldb_ctx))) {
104 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
106 service_dn_str = ldb_dn_alloc_linearized(tmp_ctx, service_dn);
107 if ( ! service_dn_str) {
108 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
111 ret = ldb_search(ldb_ctx, tmp_ctx, &res, service_dn, LDB_SCOPE_BASE,
112 directory_attrs, "(objectClass=nTDSService)");
114 if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
115 DEBUG(1, ("ldb_search: dn: %s not found: %s\n", service_dn_str, ldb_errstring(ldb_ctx)));
116 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
117 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
118 DEBUG(1, ("ldb_search: dn: %s not found\n", service_dn_str));
119 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
120 } else if (res->count != 1) {
121 talloc_free(res);
122 DEBUG(1, ("ldb_search: dn: %s not found\n", service_dn_str));
123 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
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\n", 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, enum drsuapi_DsNameFormat format_offered,
182 enum drsuapi_DsNameFormat format_desired,
183 const char *name, struct drsuapi_DsNameInfo1 *info1)
185 WERROR wret;
186 krb5_error_code ret;
187 krb5_principal principal;
188 const krb5_data *component;
189 const char *service, *dns_name;
190 char *new_service;
191 char *new_princ;
192 enum drsuapi_DsNameStatus namestatus;
194 /* parse principal */
195 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context,
196 name, KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
197 if (ret) {
198 DEBUG(2, ("Could not parse principal: %s: %s\n",
199 name, smb_get_krb5_error_message(smb_krb5_context->krb5_context,
200 ret, mem_ctx)));
201 return WERR_NOMEM;
204 /* grab cifs/, http/ etc */
206 /* This is checked for in callers, but be safe */
207 if (krb5_princ_size(smb_krb5_context->krb5_context, principal) < 2) {
208 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
209 krb5_free_principal(smb_krb5_context->krb5_context, principal);
210 return WERR_OK;
212 component = krb5_princ_component(smb_krb5_context->krb5_context,
213 principal, 0);
214 service = (const char *)component->data;
215 component = krb5_princ_component(smb_krb5_context->krb5_context,
216 principal, 1);
217 dns_name = (const char *)component->data;
219 /* MAP it */
220 namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context,
221 sam_ctx, mem_ctx,
222 service, &new_service);
224 if (namestatus == DRSUAPI_DS_NAME_STATUS_NOT_FOUND) {
225 wret = WERR_OK;
226 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
227 info1->dns_domain_name = talloc_strdup(mem_ctx, dns_name);
228 if (!info1->dns_domain_name) {
229 wret = WERR_NOMEM;
231 krb5_free_principal(smb_krb5_context->krb5_context, principal);
232 return wret;
233 } else if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
234 info1->status = namestatus;
235 krb5_free_principal(smb_krb5_context->krb5_context, principal);
236 return WERR_OK;
239 /* reform principal */
240 new_princ = talloc_asprintf(mem_ctx, "%s/%s", new_service, dns_name);
241 if (!new_princ) {
242 krb5_free_principal(smb_krb5_context->krb5_context, principal);
243 return WERR_NOMEM;
246 wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
247 new_princ, info1);
248 talloc_free(new_princ);
249 if (W_ERROR_IS_OK(wret) && (info1->status == DRSUAPI_DS_NAME_STATUS_NOT_FOUND)) {
250 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
251 info1->dns_domain_name = talloc_strdup(mem_ctx, dns_name);
252 if (!info1->dns_domain_name) {
253 wret = WERR_NOMEM;
256 krb5_free_principal(smb_krb5_context->krb5_context, principal);
257 return wret;
260 /* Subcase of CrackNames, for the userPrincipalName */
262 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
263 struct smb_krb5_context *smb_krb5_context,
264 uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
265 enum drsuapi_DsNameFormat 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_REQUIRE_REALM, &principal);
287 if (ret) {
288 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
289 return WERR_OK;
292 realm = smb_krb5_principal_get_realm(smb_krb5_context->krb5_context,
293 principal);
295 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
296 samdb_partitions_dn(sam_ctx, mem_ctx),
297 LDB_SCOPE_ONELEVEL,
298 domain_attrs,
299 "(&(objectClass=crossRef)(|(dnsRoot=%s)(netbiosName=%s))(systemFlags:%s:=%u))",
300 ldb_binary_encode_string(mem_ctx, realm),
301 ldb_binary_encode_string(mem_ctx, realm),
302 LDB_OID_COMPARATOR_AND,
303 SYSTEM_FLAG_CR_NTDS_DOMAIN);
304 free(realm);
306 if (ldb_ret != LDB_SUCCESS) {
307 DEBUG(2, ("DsCrackNameUPN domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
308 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
309 krb5_free_principal(smb_krb5_context->krb5_context, principal);
310 return WERR_OK;
313 switch (domain_res->count) {
314 case 1:
315 break;
316 case 0:
317 krb5_free_principal(smb_krb5_context->krb5_context, principal);
318 return dns_domain_from_principal(mem_ctx, smb_krb5_context,
319 name, info1);
320 default:
321 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
322 krb5_free_principal(smb_krb5_context->krb5_context, principal);
323 return WERR_OK;
326 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
327 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
328 krb5_free_principal(smb_krb5_context->krb5_context, principal);
330 if (ret) {
331 free(unparsed_name_short);
332 return WERR_NOMEM;
335 /* This may need to be extended for more userPrincipalName variations */
336 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
337 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
339 domain_filter = talloc_asprintf(mem_ctx, "(distinguishedName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
341 if (!result_filter || !domain_filter) {
342 free(unparsed_name_short);
343 return WERR_NOMEM;
345 status = DsCrackNameOneFilter(sam_ctx, mem_ctx,
346 smb_krb5_context,
347 format_flags, format_offered, format_desired,
348 NULL, unparsed_name_short, domain_filter, result_filter,
349 info1, LDB_SCOPE_SUBTREE, NULL);
350 free(unparsed_name_short);
352 return status;
356 * This function will workout the filtering parameter in order to be able to do
357 * the adapted search when the incomming format is format_functional.
358 * This boils down to defining the search_dn (passed as pointer to ldb_dn *) and the
359 * ldap filter request.
360 * Main input parameters are:
361 * * name, which is the portion of the functional name after the
362 * first '/'.
363 * * domain_filter, which is a ldap search filter used to find the NC DN given the
364 * function name to crack.
366 static WERROR get_format_functional_filtering_param(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
367 char *name, struct drsuapi_DsNameInfo1 *info1,
368 struct ldb_dn **psearch_dn, const char *domain_filter, const char **presult_filter)
370 struct ldb_result *domain_res = NULL;
371 const char * const domain_attrs[] = {"ncName", NULL};
372 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
373 int ldb_ret;
374 char *account, *s, *result_filter = NULL;
375 struct ldb_dn *search_dn = NULL;
377 *psearch_dn = NULL;
378 *presult_filter = NULL;
380 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
381 partitions_basedn,
382 LDB_SCOPE_ONELEVEL,
383 domain_attrs,
384 "%s", domain_filter);
386 if (ldb_ret != LDB_SUCCESS) {
387 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
388 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
389 return WERR_FOOBAR;
392 if (domain_res->count == 1) {
393 struct ldb_dn *tmp_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
394 const char * const name_attrs[] = {"name", NULL};
396 account = name;
397 s = strchr(account, '/');
398 talloc_free(domain_res);
399 while(s) {
400 s[0] = '\0';
401 s++;
403 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
404 tmp_dn,
405 LDB_SCOPE_ONELEVEL,
406 name_attrs,
407 "name=%s", account);
409 if (ldb_ret != LDB_SUCCESS) {
410 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
411 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
412 return WERR_OK;
414 talloc_free(tmp_dn);
415 switch (domain_res->count) {
416 case 1:
417 break;
418 case 0:
419 talloc_free(domain_res);
420 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
421 return WERR_OK;
422 default:
423 talloc_free(domain_res);
424 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
425 return WERR_OK;
428 tmp_dn = talloc_steal(mem_ctx, domain_res->msgs[0]->dn);
429 talloc_free(domain_res);
430 search_dn = tmp_dn;
431 account = s;
432 s = strchr(account, '/');
434 account = ldb_binary_encode_string(mem_ctx, account);
435 W_ERROR_HAVE_NO_MEMORY(account);
436 result_filter = talloc_asprintf(mem_ctx, "(name=%s)",
437 account);
438 W_ERROR_HAVE_NO_MEMORY(result_filter);
440 *psearch_dn = search_dn;
441 *presult_filter = result_filter;
442 return WERR_OK;
445 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
447 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
448 uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
449 enum drsuapi_DsNameFormat format_desired,
450 const char *name, struct drsuapi_DsNameInfo1 *info1)
452 krb5_error_code ret;
453 const char *domain_filter = NULL;
454 const char *result_filter = NULL;
455 struct ldb_dn *name_dn = NULL;
456 struct ldb_dn *search_dn = NULL;
458 struct smb_krb5_context *smb_krb5_context = NULL;
459 int scope = LDB_SCOPE_SUBTREE;
461 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
462 info1->dns_domain_name = NULL;
463 info1->result_name = NULL;
465 if (!name) {
466 return WERR_INVALID_PARAM;
469 /* TODO: - fill the correct names in all cases!
470 * - handle format_flags
472 if (format_desired == DRSUAPI_DS_NAME_FORMAT_UNKNOWN) {
473 return WERR_OK;
475 /* here we need to set the domain_filter and/or the result_filter */
476 switch (format_offered) {
477 case DRSUAPI_DS_NAME_FORMAT_UNKNOWN:
479 unsigned int i;
480 enum drsuapi_DsNameFormat formats[] = {
481 DRSUAPI_DS_NAME_FORMAT_FQDN_1779, DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
482 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT, DRSUAPI_DS_NAME_FORMAT_CANONICAL,
483 DRSUAPI_DS_NAME_FORMAT_GUID, DRSUAPI_DS_NAME_FORMAT_DISPLAY,
484 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
485 DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY,
486 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
488 WERROR werr;
489 for (i=0; i < ARRAY_SIZE(formats); i++) {
490 werr = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, formats[i], format_desired, name, info1);
491 if (!W_ERROR_IS_OK(werr)) {
492 return werr;
494 if (info1->status != DRSUAPI_DS_NAME_STATUS_NOT_FOUND &&
495 (formats[i] != DRSUAPI_DS_NAME_FORMAT_CANONICAL ||
496 info1->status != DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR))
498 return werr;
501 return werr;
504 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
505 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
507 char *str, *s, *account;
508 scope = LDB_SCOPE_ONELEVEL;
510 if (strlen(name) == 0) {
511 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
512 return WERR_OK;
515 str = talloc_strdup(mem_ctx, name);
516 W_ERROR_HAVE_NO_MEMORY(str);
518 if (format_offered == DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX) {
519 /* Look backwards for the \n, and replace it with / */
520 s = strrchr(str, '\n');
521 if (!s) {
522 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
523 return WERR_OK;
525 s[0] = '/';
528 s = strchr(str, '/');
529 if (!s) {
530 /* there must be at least one / */
531 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
532 return WERR_OK;
535 s[0] = '\0';
536 s++;
538 domain_filter = talloc_asprintf(mem_ctx, "(&(objectClass=crossRef)(dnsRoot=%s)(systemFlags:%s:=%u))",
539 ldb_binary_encode_string(mem_ctx, str),
540 LDB_OID_COMPARATOR_AND,
541 SYSTEM_FLAG_CR_NTDS_DOMAIN);
542 W_ERROR_HAVE_NO_MEMORY(domain_filter);
544 /* There may not be anything after the domain component (search for the domain itself) */
545 account = s;
546 if (account && *account) {
547 WERROR werr = get_format_functional_filtering_param(sam_ctx,
548 mem_ctx,
549 account,
550 info1,
551 &search_dn,
552 domain_filter,
553 &result_filter);
554 if (!W_ERROR_IS_OK(werr)) {
555 return werr;
557 if (info1->status != DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR)
558 return WERR_OK;
560 break;
562 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
563 char *p;
564 char *domain;
565 const char *account = NULL;
567 domain = talloc_strdup(mem_ctx, name);
568 W_ERROR_HAVE_NO_MEMORY(domain);
570 p = strchr(domain, '\\');
571 if (!p) {
572 /* invalid input format */
573 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
574 return WERR_OK;
576 p[0] = '\0';
578 if (p[1]) {
579 account = &p[1];
582 domain_filter = talloc_asprintf(mem_ctx,
583 "(&(objectClass=crossRef)(netbiosName=%s)(systemFlags:%s:=%u))",
584 ldb_binary_encode_string(mem_ctx, domain),
585 LDB_OID_COMPARATOR_AND,
586 SYSTEM_FLAG_CR_NTDS_DOMAIN);
587 W_ERROR_HAVE_NO_MEMORY(domain_filter);
588 if (account) {
589 result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
590 ldb_binary_encode_string(mem_ctx, account));
591 W_ERROR_HAVE_NO_MEMORY(result_filter);
594 talloc_free(domain);
595 break;
598 /* A LDAP DN as a string */
599 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
600 domain_filter = NULL;
601 name_dn = ldb_dn_new(mem_ctx, sam_ctx, name);
602 if (! ldb_dn_validate(name_dn)) {
603 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
604 return WERR_OK;
606 break;
609 /* A GUID as a string */
610 case DRSUAPI_DS_NAME_FORMAT_GUID: {
611 struct GUID guid;
612 char *ldap_guid;
613 NTSTATUS nt_status;
614 domain_filter = NULL;
616 nt_status = GUID_from_string(name, &guid);
617 if (!NT_STATUS_IS_OK(nt_status)) {
618 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
619 return WERR_OK;
622 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
623 if (!ldap_guid) {
624 return WERR_NOMEM;
626 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
627 ldap_guid);
628 W_ERROR_HAVE_NO_MEMORY(result_filter);
629 break;
631 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
632 domain_filter = NULL;
634 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
635 ldb_binary_encode_string(mem_ctx, name),
636 ldb_binary_encode_string(mem_ctx, name));
637 W_ERROR_HAVE_NO_MEMORY(result_filter);
638 break;
641 /* A S-1234-5678 style string */
642 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
643 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
644 char *ldap_sid;
646 domain_filter = NULL;
647 if (!sid) {
648 info1->dns_domain_name = NULL;
649 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
650 return WERR_OK;
652 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx,
653 sid);
654 if (!ldap_sid) {
655 return WERR_NOMEM;
657 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
658 ldap_sid);
659 W_ERROR_HAVE_NO_MEMORY(result_filter);
660 break;
662 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
663 krb5_principal principal;
664 char *unparsed_name;
666 ret = smb_krb5_init_context(mem_ctx,
667 (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"),
668 &smb_krb5_context);
670 if (ret) {
671 return WERR_NOMEM;
674 /* Ensure we reject compleate junk first */
675 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
676 if (ret) {
677 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
678 return WERR_OK;
681 domain_filter = NULL;
683 /* By getting the unparsed name here, we ensure the escaping is correct (and trust the client less) */
684 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
685 if (ret) {
686 krb5_free_principal(smb_krb5_context->krb5_context, principal);
687 return WERR_NOMEM;
690 krb5_free_principal(smb_krb5_context->krb5_context, principal);
692 /* The ldb_binary_encode_string() here avoid LDAP filter injection attacks */
693 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))",
694 ldb_binary_encode_string(mem_ctx, unparsed_name));
696 free(unparsed_name);
697 W_ERROR_HAVE_NO_MEMORY(result_filter);
698 break;
700 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
701 krb5_principal principal;
702 char *unparsed_name_short;
703 const krb5_data *component;
704 char *service;
706 ret = smb_krb5_init_context(mem_ctx,
707 (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"),
708 &smb_krb5_context);
710 if (ret) {
711 return WERR_NOMEM;
714 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
715 if (ret == 0 &&
716 krb5_princ_size(smb_krb5_context->krb5_context,
717 principal) < 2) {
718 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
719 krb5_free_principal(smb_krb5_context->krb5_context, principal);
720 return WERR_OK;
721 } else if (ret == 0) {
722 krb5_free_principal(smb_krb5_context->krb5_context, principal);
724 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name,
725 KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
726 if (ret) {
727 return dns_domain_from_principal(mem_ctx, smb_krb5_context,
728 name, info1);
731 domain_filter = NULL;
733 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal,
734 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
735 if (ret) {
736 krb5_free_principal(smb_krb5_context->krb5_context, principal);
737 return WERR_NOMEM;
740 component = krb5_princ_component(smb_krb5_context->krb5_context,
741 principal, 0);
742 service = (char *)component->data;
743 if ((krb5_princ_size(smb_krb5_context->krb5_context,
744 principal) == 2) &&
745 (strcasecmp(service, "host") == 0)) {
746 /* the 'cn' attribute is just the leading part of the name */
747 char *computer_name;
748 component = krb5_princ_component(
749 smb_krb5_context->krb5_context,
750 principal, 1);
751 computer_name = talloc_strndup(mem_ctx, (char *)component->data,
752 strcspn((char *)component->data, "."));
753 if (computer_name == NULL) {
754 krb5_free_principal(smb_krb5_context->krb5_context, principal);
755 free(unparsed_name_short);
756 return WERR_NOMEM;
759 result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
760 ldb_binary_encode_string(mem_ctx, unparsed_name_short),
761 ldb_binary_encode_string(mem_ctx, computer_name));
762 } else {
763 result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
764 ldb_binary_encode_string(mem_ctx, unparsed_name_short));
766 krb5_free_principal(smb_krb5_context->krb5_context, principal);
767 free(unparsed_name_short);
768 W_ERROR_HAVE_NO_MEMORY(result_filter);
770 break;
772 default: {
773 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
774 return WERR_OK;
778 if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
779 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
780 name_dn, name, info1);
783 return DsCrackNameOneFilter(sam_ctx, mem_ctx,
784 smb_krb5_context,
785 format_flags, format_offered, format_desired,
786 name_dn, name,
787 domain_filter, result_filter,
788 info1, scope, search_dn);
791 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
792 * (FQDN_1779) into a canoical name without actually searching the
793 * database */
795 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
796 enum drsuapi_DsNameFormat format_offered,
797 enum drsuapi_DsNameFormat format_desired,
798 struct ldb_dn *name_dn, const char *name,
799 struct drsuapi_DsNameInfo1 *info1)
801 char *cracked;
802 if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
803 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
804 return WERR_OK;
807 switch (format_desired) {
808 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
809 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
810 break;
811 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
812 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
813 break;
814 default:
815 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
816 return WERR_OK;
818 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
819 info1->result_name = cracked;
820 if (!cracked) {
821 return WERR_NOMEM;
824 return WERR_OK;
827 /* Given a filter for the domain, and one for the result, perform the
828 * ldb search. The format offered and desired flags change the
829 * behaviours, including what attributes to return.
831 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
834 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
835 struct smb_krb5_context *smb_krb5_context,
836 uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
837 enum drsuapi_DsNameFormat format_desired,
838 struct ldb_dn *name_dn, const char *name,
839 const char *domain_filter, const char *result_filter,
840 struct drsuapi_DsNameInfo1 *info1,
841 int scope, struct ldb_dn *search_dn)
843 int ldb_ret;
844 struct ldb_result *domain_res = NULL;
845 const char * const *domain_attrs;
846 const char * const *result_attrs;
847 struct ldb_message **result_res = NULL;
848 struct ldb_message *result = NULL;
849 int i;
850 char *p;
851 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
853 const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
854 const char * const _result_attrs_null[] = { NULL };
856 const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
857 const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
859 const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
860 const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", "objectClass", NULL};
862 const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
863 const char * const _result_attrs_guid[] = { "objectGUID", NULL};
865 const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
866 const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
868 const char * const _domain_attrs_none[] = { "ncName", "dnsRoot" , NULL};
869 const char * const _result_attrs_none[] = { NULL};
871 /* here we need to set the attrs lists for domain and result lookups */
872 switch (format_desired) {
873 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
874 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
875 domain_attrs = _domain_attrs_1779;
876 result_attrs = _result_attrs_null;
877 break;
878 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
879 domain_attrs = _domain_attrs_canonical;
880 result_attrs = _result_attrs_canonical;
881 break;
882 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
883 domain_attrs = _domain_attrs_nt4;
884 result_attrs = _result_attrs_nt4;
885 break;
886 case DRSUAPI_DS_NAME_FORMAT_GUID:
887 domain_attrs = _domain_attrs_guid;
888 result_attrs = _result_attrs_guid;
889 break;
890 case DRSUAPI_DS_NAME_FORMAT_DISPLAY:
891 domain_attrs = _domain_attrs_display;
892 result_attrs = _result_attrs_display;
893 break;
894 default:
895 domain_attrs = _domain_attrs_none;
896 result_attrs = _result_attrs_none;
897 break;
900 if (domain_filter) {
901 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
902 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
903 partitions_basedn,
904 LDB_SCOPE_ONELEVEL,
905 domain_attrs,
906 "%s", domain_filter);
908 if (ldb_ret != LDB_SUCCESS) {
909 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
910 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
911 return WERR_OK;
914 switch (domain_res->count) {
915 case 1:
916 break;
917 case 0:
918 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
919 return WERR_OK;
920 default:
921 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
922 return WERR_OK;
925 info1->dns_domain_name = ldb_msg_find_attr_as_string(domain_res->msgs[0], "dnsRoot", NULL);
926 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
927 info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
928 } else {
929 info1->dns_domain_name = NULL;
930 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
933 if (result_filter) {
934 int ret;
935 struct ldb_result *res;
936 uint32_t dsdb_flags = 0;
937 struct ldb_dn *real_search_dn = NULL;
938 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
941 * From 4.1.4.2.11 of MS-DRSR
942 * if DS_NAME_FLAG_GCVERIFY in flags then
943 * rt := select all O from all
944 * where attrValue in GetAttrVals(O, att, false)
945 * else
946 * rt := select all O from subtree DefaultNC()
947 * where attrValue in GetAttrVals(O, att, false)
948 * endif
949 * return rt
951 if (format_flags & DRSUAPI_DS_NAME_FLAG_GCVERIFY ||
952 format_offered == DRSUAPI_DS_NAME_FORMAT_GUID)
954 dsdb_flags = DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
955 } else if (domain_res) {
956 if (!search_dn) {
957 struct ldb_dn *tmp_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
958 real_search_dn = tmp_dn;
959 } else {
960 real_search_dn = search_dn;
962 } else {
963 real_search_dn = ldb_get_default_basedn(sam_ctx);
965 if (format_desired == DRSUAPI_DS_NAME_FORMAT_GUID){
966 dsdb_flags |= DSDB_SEARCH_SHOW_RECYCLED;
968 /* search with the 'phantom root' flag */
969 ret = dsdb_search(sam_ctx, mem_ctx, &res,
970 real_search_dn,
971 scope,
972 result_attrs,
973 dsdb_flags,
974 "%s", result_filter);
975 if (ret != LDB_SUCCESS) {
976 DEBUG(2, ("DsCrackNameOneFilter search from '%s' with flags 0x%08x failed: %s\n",
977 ldb_dn_get_linearized(real_search_dn),
978 dsdb_flags,
979 ldb_errstring(sam_ctx)));
980 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
981 return WERR_OK;
984 ldb_ret = res->count;
985 result_res = res->msgs;
986 } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
987 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
988 result_attrs);
989 } else if (domain_res) {
990 name_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
991 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
992 result_attrs);
993 } else {
994 /* Can't happen */
995 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not available: This can't happen...\n"));
996 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
997 return WERR_OK;
1000 switch (ldb_ret) {
1001 case 1:
1002 result = result_res[0];
1003 break;
1004 case 0:
1005 switch (format_offered) {
1006 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL:
1007 return DsCrackNameSPNAlias(sam_ctx, mem_ctx,
1008 smb_krb5_context,
1009 format_flags, format_offered, format_desired,
1010 name, info1);
1012 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
1013 return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context,
1014 format_flags, format_offered, format_desired,
1015 name, info1);
1016 default:
1017 break;
1019 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1020 return WERR_OK;
1021 case -1:
1022 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s\n", ldb_errstring(sam_ctx)));
1023 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1024 return WERR_OK;
1025 default:
1026 switch (format_offered) {
1027 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
1028 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
1030 const char *canonical_name = NULL; /* Not required, but we get warnings... */
1031 /* We may need to manually filter further */
1032 for (i = 0; i < ldb_ret; i++) {
1033 switch (format_offered) {
1034 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
1035 canonical_name = ldb_dn_canonical_string(mem_ctx, result_res[i]->dn);
1036 break;
1037 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
1038 canonical_name = ldb_dn_canonical_ex_string(mem_ctx, result_res[i]->dn);
1039 break;
1040 default:
1041 break;
1043 if (strcasecmp_m(canonical_name, name) == 0) {
1044 result = result_res[i];
1045 break;
1048 if (!result) {
1049 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1050 return WERR_OK;
1053 /* FALL TROUGH */
1054 default:
1055 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1056 return WERR_OK;
1060 info1->dns_domain_name = ldb_dn_canonical_string(mem_ctx, result->dn);
1061 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
1062 p = strchr(info1->dns_domain_name, '/');
1063 if (p) {
1064 p[0] = '\0';
1067 /* here we can use result and domain_res[0] */
1068 switch (format_desired) {
1069 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
1070 info1->result_name = ldb_dn_alloc_linearized(mem_ctx, result->dn);
1071 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1073 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1074 return WERR_OK;
1076 case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
1077 info1->result_name = ldb_msg_find_attr_as_string(result, "canonicalName", NULL);
1078 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1079 return WERR_OK;
1081 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
1082 /* Not in the virtual ldb attribute */
1083 return DsCrackNameOneSyntactical(mem_ctx,
1084 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1085 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
1086 result->dn, name, info1);
1088 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
1090 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result, "objectSid");
1091 const char *_acc = "", *_dom = "";
1092 if (sid == NULL) {
1093 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1094 return WERR_OK;
1097 if (samdb_find_attribute(sam_ctx, result, "objectClass",
1098 "domain")) {
1099 /* This can also find a DomainDNSZones entry,
1100 * but it won't have the SID we just
1101 * checked. */
1102 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
1103 partitions_basedn,
1104 LDB_SCOPE_ONELEVEL,
1105 domain_attrs,
1106 "(ncName=%s)", ldb_dn_get_linearized(result->dn));
1108 if (ldb_ret != LDB_SUCCESS) {
1109 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
1110 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1111 return WERR_OK;
1114 switch (domain_res->count) {
1115 case 1:
1116 break;
1117 case 0:
1118 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1119 return WERR_OK;
1120 default:
1121 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1122 return WERR_OK;
1124 _dom = ldb_msg_find_attr_as_string(domain_res->msgs[0], "nETBIOSName", NULL);
1125 W_ERROR_HAVE_NO_MEMORY(_dom);
1126 } else {
1127 _acc = ldb_msg_find_attr_as_string(result, "sAMAccountName", NULL);
1128 if (!_acc) {
1129 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1130 return WERR_OK;
1132 if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx, SID_BUILTIN), sid)) {
1133 _dom = "BUILTIN";
1134 } else {
1135 const char *attrs[] = { NULL };
1136 struct ldb_result *domain_res2;
1137 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
1138 if (!dom_sid) {
1139 return WERR_OK;
1141 dom_sid->num_auths--;
1142 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
1143 NULL,
1144 LDB_SCOPE_BASE,
1145 attrs,
1146 "(&(objectSid=%s)(objectClass=domain))",
1147 ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
1149 if (ldb_ret != LDB_SUCCESS) {
1150 DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s\n", ldb_errstring(sam_ctx)));
1151 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1152 return WERR_OK;
1155 switch (domain_res->count) {
1156 case 1:
1157 break;
1158 case 0:
1159 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1160 return WERR_OK;
1161 default:
1162 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1163 return WERR_OK;
1166 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res2,
1167 partitions_basedn,
1168 LDB_SCOPE_ONELEVEL,
1169 domain_attrs,
1170 "(ncName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
1172 if (ldb_ret != LDB_SUCCESS) {
1173 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
1174 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1175 return WERR_OK;
1178 switch (domain_res2->count) {
1179 case 1:
1180 break;
1181 case 0:
1182 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1183 return WERR_OK;
1184 default:
1185 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1186 return WERR_OK;
1188 _dom = ldb_msg_find_attr_as_string(domain_res2->msgs[0], "nETBIOSName", NULL);
1189 W_ERROR_HAVE_NO_MEMORY(_dom);
1193 info1->result_name = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
1194 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1196 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1197 return WERR_OK;
1199 case DRSUAPI_DS_NAME_FORMAT_GUID: {
1200 struct GUID guid;
1202 guid = samdb_result_guid(result, "objectGUID");
1204 info1->result_name = GUID_string2(mem_ctx, &guid);
1205 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1207 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1208 return WERR_OK;
1210 case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
1211 info1->result_name = ldb_msg_find_attr_as_string(result, "displayName", NULL);
1212 if (!info1->result_name) {
1213 info1->result_name = ldb_msg_find_attr_as_string(result, "sAMAccountName", NULL);
1215 if (!info1->result_name) {
1216 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1217 } else {
1218 info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1220 return WERR_OK;
1222 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
1223 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1224 return WERR_OK;
1226 case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN:
1227 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
1228 info1->dns_domain_name = NULL;
1229 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1230 return WERR_OK;
1232 default:
1233 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1234 return WERR_OK;
1238 /* Given a user Principal Name (such as foo@bar.com),
1239 * return the user and domain DNs. This is used in the KDC to then
1240 * return the Keys and evaluate policy */
1242 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx,
1243 TALLOC_CTX *mem_ctx,
1244 const char *user_principal_name,
1245 struct ldb_dn **user_dn,
1246 struct ldb_dn **domain_dn)
1248 WERROR werr;
1249 struct drsuapi_DsNameInfo1 info1;
1250 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1251 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
1252 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1253 user_principal_name,
1254 &info1);
1255 if (!W_ERROR_IS_OK(werr)) {
1256 return werror_to_ntstatus(werr);
1258 switch (info1.status) {
1259 case DRSUAPI_DS_NAME_STATUS_OK:
1260 break;
1261 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1262 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1263 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1264 return NT_STATUS_NO_SUCH_USER;
1265 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1266 default:
1267 return NT_STATUS_UNSUCCESSFUL;
1270 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1272 if (domain_dn) {
1273 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1274 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1275 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1276 talloc_asprintf(mem_ctx, "%s/",
1277 info1.dns_domain_name),
1278 &info1);
1279 if (!W_ERROR_IS_OK(werr)) {
1280 return werror_to_ntstatus(werr);
1282 switch (info1.status) {
1283 case DRSUAPI_DS_NAME_STATUS_OK:
1284 break;
1285 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1286 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1287 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1288 return NT_STATUS_NO_SUCH_USER;
1289 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1290 default:
1291 return NT_STATUS_UNSUCCESSFUL;
1294 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1297 return NT_STATUS_OK;
1300 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1301 * return the user and domain DNs. This is used in the KDC to then
1302 * return the Keys and evaluate policy */
1304 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx,
1305 TALLOC_CTX *mem_ctx,
1306 const char *service_principal_name,
1307 struct ldb_dn **user_dn,
1308 struct ldb_dn **domain_dn)
1310 WERROR werr;
1311 struct drsuapi_DsNameInfo1 info1;
1312 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1313 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
1314 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1315 service_principal_name,
1316 &info1);
1317 if (!W_ERROR_IS_OK(werr)) {
1318 return werror_to_ntstatus(werr);
1320 switch (info1.status) {
1321 case DRSUAPI_DS_NAME_STATUS_OK:
1322 break;
1323 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1324 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1325 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1326 return NT_STATUS_NO_SUCH_USER;
1327 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1328 default:
1329 return NT_STATUS_UNSUCCESSFUL;
1332 *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1334 if (domain_dn) {
1335 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1336 DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1337 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
1338 talloc_asprintf(mem_ctx, "%s/",
1339 info1.dns_domain_name),
1340 &info1);
1341 if (!W_ERROR_IS_OK(werr)) {
1342 return werror_to_ntstatus(werr);
1344 switch (info1.status) {
1345 case DRSUAPI_DS_NAME_STATUS_OK:
1346 break;
1347 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1348 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1349 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1350 return NT_STATUS_NO_SUCH_USER;
1351 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1352 default:
1353 return NT_STATUS_UNSUCCESSFUL;
1356 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1359 return NT_STATUS_OK;
1362 NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1363 struct tevent_context *ev_ctx,
1364 struct loadparm_context *lp_ctx,
1365 enum drsuapi_DsNameFormat format_offered,
1366 const char *name,
1367 const char **nt4_domain, const char **nt4_account)
1369 WERROR werr;
1370 struct drsuapi_DsNameInfo1 info1;
1371 struct ldb_context *ldb;
1372 char *p;
1374 /* Handle anonymous bind */
1375 if (!name || !*name) {
1376 *nt4_domain = "";
1377 *nt4_account = "";
1378 return NT_STATUS_OK;
1381 ldb = samdb_connect(mem_ctx, ev_ctx, lp_ctx, system_session(lp_ctx), 0);
1382 if (ldb == NULL) {
1383 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1386 werr = DsCrackNameOneName(ldb, mem_ctx, 0,
1387 format_offered,
1388 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
1389 name,
1390 &info1);
1391 if (!W_ERROR_IS_OK(werr)) {
1392 return werror_to_ntstatus(werr);
1394 switch (info1.status) {
1395 case DRSUAPI_DS_NAME_STATUS_OK:
1396 break;
1397 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1398 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1399 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1400 return NT_STATUS_NO_SUCH_USER;
1401 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1402 default:
1403 return NT_STATUS_UNSUCCESSFUL;
1406 *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
1407 if (*nt4_domain == NULL) {
1408 return NT_STATUS_NO_MEMORY;
1411 p = strchr(*nt4_domain, '\\');
1412 if (!p) {
1413 return NT_STATUS_INVALID_PARAMETER;
1415 p[0] = '\0';
1417 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
1418 if (*nt4_account == NULL) {
1419 return NT_STATUS_NO_MEMORY;
1422 return NT_STATUS_OK;
1425 NTSTATUS crack_auto_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1426 struct tevent_context *ev_ctx,
1427 struct loadparm_context *lp_ctx,
1428 const char *name,
1429 const char **nt4_domain,
1430 const char **nt4_account)
1432 enum drsuapi_DsNameFormat format_offered = DRSUAPI_DS_NAME_FORMAT_UNKNOWN;
1434 /* Handle anonymous bind */
1435 if (!name || !*name) {
1436 *nt4_domain = "";
1437 *nt4_account = "";
1438 return NT_STATUS_OK;
1441 if (strchr_m(name, '=')) {
1442 format_offered = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
1443 } else if (strchr_m(name, '@')) {
1444 format_offered = DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL;
1445 } else if (strchr_m(name, '\\')) {
1446 format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
1447 } else if (strchr_m(name, '/')) {
1448 format_offered = DRSUAPI_DS_NAME_FORMAT_CANONICAL;
1449 } else {
1450 return NT_STATUS_NO_SUCH_USER;
1453 return crack_name_to_nt4_name(mem_ctx, ev_ctx, lp_ctx, format_offered, name, nt4_domain, nt4_account);
1457 WERROR dcesrv_drsuapi_ListRoles(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1458 const struct drsuapi_DsNameRequest1 *req1,
1459 struct drsuapi_DsNameCtr1 **ctr1)
1461 struct drsuapi_DsNameInfo1 *names;
1462 uint32_t i;
1463 uint32_t count = 5;/*number of fsmo role owners we are going to return*/
1465 *ctr1 = talloc(mem_ctx, struct drsuapi_DsNameCtr1);
1466 W_ERROR_HAVE_NO_MEMORY(*ctr1);
1467 names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
1468 W_ERROR_HAVE_NO_MEMORY(names);
1470 for (i = 0; i < count; i++) {
1471 WERROR werr;
1472 struct ldb_dn *role_owner_dn, *fsmo_role_dn, *server_dn;
1473 werr = dsdb_get_fsmo_role_info(mem_ctx, sam_ctx, i,
1474 &fsmo_role_dn, &role_owner_dn);
1475 if(!W_ERROR_IS_OK(werr)) {
1476 return werr;
1478 server_dn = ldb_dn_copy(mem_ctx, role_owner_dn);
1479 ldb_dn_remove_child_components(server_dn, 1);
1480 names[i].status = DRSUAPI_DS_NAME_STATUS_OK;
1481 names[i].dns_domain_name = samdb_dn_to_dnshostname(sam_ctx, mem_ctx,
1482 server_dn);
1483 if(!names[i].dns_domain_name) {
1484 DEBUG(4, ("list_roles: Failed to find dNSHostName for server %s\n",
1485 ldb_dn_get_linearized(server_dn)));
1487 names[i].result_name = talloc_strdup(mem_ctx, ldb_dn_get_linearized(role_owner_dn));
1490 (*ctr1)->count = count;
1491 (*ctr1)->array = names;
1493 return WERR_OK;
1496 WERROR dcesrv_drsuapi_CrackNamesByNameFormat(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1497 const struct drsuapi_DsNameRequest1 *req1,
1498 struct drsuapi_DsNameCtr1 **ctr1)
1500 struct drsuapi_DsNameInfo1 *names;
1501 uint32_t i, count;
1502 WERROR status;
1504 *ctr1 = talloc_zero(mem_ctx, struct drsuapi_DsNameCtr1);
1505 W_ERROR_HAVE_NO_MEMORY(*ctr1);
1507 count = req1->count;
1508 names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
1509 W_ERROR_HAVE_NO_MEMORY(names);
1511 for (i=0; i < count; i++) {
1512 status = DsCrackNameOneName(sam_ctx, mem_ctx,
1513 req1->format_flags,
1514 req1->format_offered,
1515 req1->format_desired,
1516 req1->names[i].str,
1517 &names[i]);
1518 if (!W_ERROR_IS_OK(status)) {
1519 return status;
1523 (*ctr1)->count = count;
1524 (*ctr1)->array = names;
1526 return WERR_OK;
1529 WERROR dcesrv_drsuapi_ListInfoServer(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1530 const struct drsuapi_DsNameRequest1 *req1,
1531 struct drsuapi_DsNameCtr1 **_ctr1)
1533 struct drsuapi_DsNameInfo1 *names;
1534 struct ldb_result *res;
1535 struct ldb_dn *server_dn, *dn;
1536 struct drsuapi_DsNameCtr1 *ctr1;
1537 int ret, i;
1538 const char *str;
1539 const char *attrs[] = {
1540 "dn",
1541 "dNSHostName",
1542 "serverReference",
1543 NULL
1546 *_ctr1 = NULL;
1548 ctr1 = talloc_zero(mem_ctx, struct drsuapi_DsNameCtr1);
1549 W_ERROR_HAVE_NO_MEMORY(ctr1);
1552 * No magic value here, we have to return 3 entries according to the
1553 * MS-DRSR.pdf
1555 ctr1->count = 3;
1556 names = talloc_zero_array(ctr1, struct drsuapi_DsNameInfo1,
1557 ctr1->count);
1558 W_ERROR_HAVE_NO_MEMORY(names);
1559 ctr1->array = names;
1561 for (i=0; i < ctr1->count; i++) {
1562 names[i].status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1564 *_ctr1 = ctr1;
1566 if (req1->count != 1) {
1567 DEBUG(1, ("Expected a count of 1 for the ListInfoServer crackname \n"));
1568 return WERR_OK;
1571 if (req1->names[0].str == NULL) {
1572 return WERR_OK;
1575 server_dn = ldb_dn_new(mem_ctx, sam_ctx, req1->names[0].str);
1576 W_ERROR_HAVE_NO_MEMORY(server_dn);
1578 ret = ldb_search(sam_ctx, mem_ctx, &res, server_dn, LDB_SCOPE_ONELEVEL,
1579 NULL, "(objectClass=nTDSDSA)");
1581 if (ret != LDB_SUCCESS) {
1582 DEBUG(1, ("Search for objectClass=nTDSDSA "
1583 "returned less than 1 objects\n"));
1584 return WERR_OK;
1587 if (res->count != 1) {
1588 DEBUG(1, ("Search for objectClass=nTDSDSA "
1589 "returned less than 1 objects\n"));
1590 return WERR_OK;
1593 if (res->msgs[0]->dn) {
1594 names[0].result_name = ldb_dn_alloc_linearized(names, res->msgs[0]->dn);
1595 W_ERROR_HAVE_NO_MEMORY(names[0].result_name);
1596 names[0].status = DRSUAPI_DS_NAME_STATUS_OK;
1599 talloc_free(res);
1601 ret = ldb_search(sam_ctx, mem_ctx, &res, server_dn, LDB_SCOPE_BASE,
1602 attrs, "(objectClass=*)");
1603 if (ret != LDB_SUCCESS) {
1604 DEBUG(1, ("Search for objectClass=* on dn %s"
1605 "returned %s\n", req1->names[0].str,
1606 ldb_strerror(ret)));
1607 return WERR_OK;
1610 if (res->count != 1) {
1611 DEBUG(1, ("Search for objectClass=* on dn %s"
1612 "returned less than 1 objects\n", req1->names[0].str));
1613 return WERR_OK;
1616 str = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
1617 if (str != NULL) {
1618 names[1].result_name = talloc_strdup(names, str);
1619 W_ERROR_HAVE_NO_MEMORY(names[1].result_name);
1620 names[1].status = DRSUAPI_DS_NAME_STATUS_OK;
1623 dn = ldb_msg_find_attr_as_dn(sam_ctx, mem_ctx, res->msgs[0], "serverReference");
1624 if (dn != NULL) {
1625 names[2].result_name = ldb_dn_alloc_linearized(names, dn);
1626 W_ERROR_HAVE_NO_MEMORY(names[2].result_name);
1627 names[2].status = DRSUAPI_DS_NAME_STATUS_OK;
1630 talloc_free(dn);
1631 talloc_free(res);
1633 return WERR_OK;