2 Unix SMB/CIFS implementation.
4 crachnames implementation for the drsuapi pipe
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
10 Copyright (C) Catalyst .Net Ltd 2017
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "librpc/gen_ndr/drsuapi.h"
28 #include "lib/events/events.h"
30 #include <ldb_errors.h>
31 #include "auth/kerberos/kerberos.h"
32 #include "libcli/ldap/ldap_ndr.h"
33 #include "libcli/security/security.h"
34 #include "auth/auth.h"
35 #include "../lib/util/util_ldb.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "dsdb/common/util.h"
38 #include "param/param.h"
42 static WERROR
DsCrackNameOneFilter(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
43 struct smb_krb5_context
*smb_krb5_context
,
44 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
45 enum drsuapi_DsNameFormat format_desired
,
46 struct ldb_dn
*name_dn
, const char *name
,
47 const char *domain_filter
, const char *result_filter
,
48 struct drsuapi_DsNameInfo1
*info1
, int scope
, struct ldb_dn
*search_dn
);
49 static WERROR
DsCrackNameOneSyntactical(TALLOC_CTX
*mem_ctx
,
50 enum drsuapi_DsNameFormat format_offered
,
51 enum drsuapi_DsNameFormat format_desired
,
52 struct ldb_dn
*name_dn
, const char *name
,
53 struct drsuapi_DsNameInfo1
*info1
);
55 static WERROR
dns_domain_from_principal(TALLOC_CTX
*mem_ctx
, struct smb_krb5_context
*smb_krb5_context
,
57 struct drsuapi_DsNameInfo1
*info1
)
60 krb5_principal principal
;
61 /* perhaps it's a principal with a realm, so return the right 'domain only' response */
62 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
, name
,
63 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
, &principal
);
65 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
69 info1
->dns_domain_name
= smb_krb5_principal_get_realm(
70 mem_ctx
, smb_krb5_context
->krb5_context
, principal
);
71 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
73 W_ERROR_HAVE_NO_MEMORY(info1
->dns_domain_name
);
75 info1
->status
= DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
;
79 static enum drsuapi_DsNameStatus
LDB_lookup_spn_alias(struct ldb_context
*ldb_ctx
,
81 const char *alias_from
,
85 * Some of the logic of this function is mirrored in find_spn_alias()
86 * in source4/dsdb/samdb/ldb_modules/samldb.c. If you change this to
87 * not return the first matched alias, you will need to rethink that
92 struct ldb_result
*res
;
93 struct ldb_message_element
*spnmappings
;
95 struct ldb_dn
*service_dn
;
98 const char *directory_attrs
[] = {
103 tmp_ctx
= talloc_new(mem_ctx
);
105 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
108 service_dn
= ldb_dn_new(tmp_ctx
, ldb_ctx
, "CN=Directory Service,CN=Windows NT,CN=Services");
109 if ( ! ldb_dn_add_base(service_dn
, ldb_get_config_basedn(ldb_ctx
))) {
110 talloc_free(tmp_ctx
);
111 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
113 service_dn_str
= ldb_dn_alloc_linearized(tmp_ctx
, service_dn
);
114 if ( ! service_dn_str
) {
115 talloc_free(tmp_ctx
);
116 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
119 ret
= ldb_search(ldb_ctx
, tmp_ctx
, &res
, service_dn
, LDB_SCOPE_BASE
,
120 directory_attrs
, "(objectClass=nTDSService)");
122 if (ret
!= LDB_SUCCESS
&& ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
123 DEBUG(1, ("ldb_search: dn: %s not found: %s\n", service_dn_str
, ldb_errstring(ldb_ctx
)));
124 talloc_free(tmp_ctx
);
125 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
126 } else if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
127 DEBUG(1, ("ldb_search: dn: %s not found\n", service_dn_str
));
128 talloc_free(tmp_ctx
);
129 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
130 } else if (res
->count
!= 1) {
131 DEBUG(1, ("ldb_search: dn: %s not found\n", service_dn_str
));
132 talloc_free(tmp_ctx
);
133 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
136 spnmappings
= ldb_msg_find_element(res
->msgs
[0], "sPNMappings");
137 if (!spnmappings
|| spnmappings
->num_values
== 0) {
138 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute\n", service_dn_str
));
139 talloc_free(tmp_ctx
);
140 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
143 for (i
= 0; i
< spnmappings
->num_values
; i
++) {
144 char *mapping
, *p
, *str
;
145 mapping
= talloc_strdup(tmp_ctx
,
146 (const char *)spnmappings
->values
[i
].data
);
148 DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str
));
149 talloc_free(tmp_ctx
);
150 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
153 /* C string manipulation sucks */
155 p
= strchr(mapping
, '=');
157 DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n",
158 service_dn_str
, mapping
));
159 talloc_free(tmp_ctx
);
160 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
171 if (strcasecmp(str
, alias_from
) == 0) {
173 talloc_steal(mem_ctx
, mapping
);
174 talloc_free(tmp_ctx
);
175 return DRSUAPI_DS_NAME_STATUS_OK
;
179 DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from
));
180 talloc_free(tmp_ctx
);
181 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
184 /* When cracking a ServicePrincipalName, many services may be served
185 * by the host/ servicePrincipalName. The incoming query is for cifs/
186 * but we translate it here, and search on host/. This is done after
187 * the cifs/ entry has been searched for, making this a fallback */
189 static WERROR
DsCrackNameSPNAlias(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
190 struct smb_krb5_context
*smb_krb5_context
,
191 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
192 enum drsuapi_DsNameFormat format_desired
,
193 const char *name
, struct drsuapi_DsNameInfo1
*info1
)
197 krb5_principal principal
;
199 const char *service
, *dns_name
;
202 enum drsuapi_DsNameStatus namestatus
;
204 /* parse principal */
205 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
,
206 name
, KRB5_PRINCIPAL_PARSE_NO_REALM
, &principal
);
208 DEBUG(2, ("Could not parse principal: %s: %s\n",
209 name
, smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
211 return WERR_NOT_ENOUGH_MEMORY
;
214 /* grab cifs/, http/ etc */
216 ret
= smb_krb5_princ_component(smb_krb5_context
->krb5_context
,
217 principal
, 0, &component
);
219 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
220 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
223 service
= (const char *)component
.data
;
224 ret
= smb_krb5_princ_component(smb_krb5_context
->krb5_context
,
225 principal
, 1, &component
);
227 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
228 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
231 dns_name
= (const char *)component
.data
;
234 namestatus
= LDB_lookup_spn_alias(sam_ctx
, mem_ctx
,
235 service
, &new_service
);
237 if (namestatus
== DRSUAPI_DS_NAME_STATUS_NOT_FOUND
) {
239 info1
->status
= DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
;
240 info1
->dns_domain_name
= talloc_strdup(mem_ctx
, dns_name
);
241 if (!info1
->dns_domain_name
) {
242 wret
= WERR_NOT_ENOUGH_MEMORY
;
244 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
246 } else if (namestatus
!= DRSUAPI_DS_NAME_STATUS_OK
) {
247 info1
->status
= namestatus
;
248 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
252 /* reform principal */
253 new_princ
= talloc_asprintf(mem_ctx
, "%s/%s", new_service
, dns_name
);
255 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
256 return WERR_NOT_ENOUGH_MEMORY
;
259 wret
= DsCrackNameOneName(sam_ctx
, mem_ctx
, format_flags
, format_offered
, format_desired
,
261 talloc_free(new_princ
);
262 if (W_ERROR_IS_OK(wret
) && (info1
->status
== DRSUAPI_DS_NAME_STATUS_NOT_FOUND
)) {
263 info1
->status
= DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
;
264 info1
->dns_domain_name
= talloc_strdup(mem_ctx
, dns_name
);
265 if (!info1
->dns_domain_name
) {
266 wret
= WERR_NOT_ENOUGH_MEMORY
;
269 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
273 /* Subcase of CrackNames, for the userPrincipalName */
275 static WERROR
DsCrackNameUPN(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
276 struct smb_krb5_context
*smb_krb5_context
,
277 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
278 enum drsuapi_DsNameFormat format_desired
,
279 const char *name
, struct drsuapi_DsNameInfo1
*info1
)
283 const char *domain_filter
= NULL
;
284 const char *result_filter
= NULL
;
286 krb5_principal principal
;
288 char *realm_encoded
= NULL
;
289 char *unparsed_name_short
;
290 const char *unparsed_name_short_encoded
= NULL
;
291 const char *domain_attrs
[] = { NULL
};
292 struct ldb_result
*domain_res
= NULL
;
294 /* Prevent recursion */
296 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
300 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
, name
,
301 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
, &principal
);
303 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
307 realm
= smb_krb5_principal_get_realm(
308 mem_ctx
, smb_krb5_context
->krb5_context
, principal
);
310 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
311 return WERR_NOT_ENOUGH_MEMORY
;
314 realm_encoded
= ldb_binary_encode_string(mem_ctx
, realm
);
315 if (realm_encoded
== NULL
) {
317 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
318 return WERR_NOT_ENOUGH_MEMORY
;
321 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
322 samdb_partitions_dn(sam_ctx
, mem_ctx
),
325 "(&(objectClass=crossRef)(|(dnsRoot=%s)(netbiosName=%s))"
326 "(systemFlags:"LDB_OID_COMPARATOR_AND
":=%u))",
329 SYSTEM_FLAG_CR_NTDS_DOMAIN
);
330 TALLOC_FREE(realm_encoded
);
333 if (ldb_ret
!= LDB_SUCCESS
) {
334 DEBUG(2, ("DsCrackNameUPN domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
335 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
336 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
340 switch (domain_res
->count
) {
344 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
345 return dns_domain_from_principal(mem_ctx
, smb_krb5_context
,
348 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
349 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
354 * The important thing here is that a samAccountName may have
355 * a space in it, and this must not be kerberos escaped to
356 * match this filter, so we specify
357 * KRB5_PRINCIPAL_UNPARSE_DISPLAY
359 ret
= krb5_unparse_name_flags(smb_krb5_context
->krb5_context
, principal
,
360 KRB5_PRINCIPAL_UNPARSE_NO_REALM
|
361 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
362 &unparsed_name_short
);
363 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
366 free(unparsed_name_short
);
367 return WERR_NOT_ENOUGH_MEMORY
;
370 unparsed_name_short_encoded
= ldb_binary_encode_string(mem_ctx
, unparsed_name_short
);
371 if (unparsed_name_short_encoded
== NULL
) {
372 free(unparsed_name_short
);
373 return WERR_NOT_ENOUGH_MEMORY
;
376 /* This may need to be extended for more userPrincipalName variations */
377 result_filter
= talloc_asprintf(mem_ctx
, "(&(samAccountName=%s)(objectClass=user))",
378 unparsed_name_short_encoded
);
380 domain_filter
= talloc_asprintf(mem_ctx
, "(distinguishedName=%s)", ldb_dn_get_linearized(domain_res
->msgs
[0]->dn
));
382 if (!result_filter
|| !domain_filter
) {
383 free(unparsed_name_short
);
384 return WERR_NOT_ENOUGH_MEMORY
;
386 status
= DsCrackNameOneFilter(sam_ctx
, mem_ctx
,
388 format_flags
, format_offered
, format_desired
,
389 NULL
, unparsed_name_short
, domain_filter
, result_filter
,
390 info1
, LDB_SCOPE_SUBTREE
, NULL
);
391 free(unparsed_name_short
);
397 * This function will workout the filtering parameter in order to be able to do
398 * the adapted search when the incoming format is format_functional.
399 * This boils down to defining the search_dn (passed as pointer to ldb_dn *) and the
400 * ldap filter request.
401 * Main input parameters are:
402 * * name, which is the portion of the functional name after the
404 * * domain_filter, which is a ldap search filter used to find the NC DN given the
405 * function name to crack.
407 static WERROR
get_format_functional_filtering_param(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
408 char *name
, struct drsuapi_DsNameInfo1
*info1
,
409 struct ldb_dn
**psearch_dn
, const char *domain_filter
, const char **presult_filter
)
411 struct ldb_result
*domain_res
= NULL
;
412 const char * const domain_attrs
[] = {"ncName", NULL
};
413 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(sam_ctx
, mem_ctx
);
415 char *account
, *s
, *result_filter
= NULL
;
416 struct ldb_dn
*search_dn
= NULL
;
419 *presult_filter
= NULL
;
421 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
425 "%s", domain_filter
);
427 if (ldb_ret
!= LDB_SUCCESS
) {
428 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
429 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
433 if (domain_res
->count
== 1) {
434 struct ldb_dn
*tmp_dn
= samdb_result_dn(sam_ctx
, mem_ctx
, domain_res
->msgs
[0], "ncName", NULL
);
435 const char * const name_attrs
[] = {"name", NULL
};
438 s
= strchr(account
, '/');
439 talloc_free(domain_res
);
444 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
450 if (ldb_ret
!= LDB_SUCCESS
) {
451 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
452 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
456 switch (domain_res
->count
) {
460 talloc_free(domain_res
);
461 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
464 talloc_free(domain_res
);
465 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
469 tmp_dn
= talloc_steal(mem_ctx
, domain_res
->msgs
[0]->dn
);
470 talloc_free(domain_res
);
473 s
= strchr(account
, '/');
475 account
= ldb_binary_encode_string(mem_ctx
, account
);
476 W_ERROR_HAVE_NO_MEMORY(account
);
477 result_filter
= talloc_asprintf(mem_ctx
, "(name=%s)",
479 W_ERROR_HAVE_NO_MEMORY(result_filter
);
481 *psearch_dn
= search_dn
;
482 *presult_filter
= result_filter
;
486 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
488 WERROR
DsCrackNameOneName(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
489 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
490 enum drsuapi_DsNameFormat format_desired
,
491 const char *name
, struct drsuapi_DsNameInfo1
*info1
)
494 const char *domain_filter
= NULL
;
495 const char *result_filter
= NULL
;
496 struct ldb_dn
*name_dn
= NULL
;
497 struct ldb_dn
*search_dn
= NULL
;
499 struct smb_krb5_context
*smb_krb5_context
= NULL
;
500 int scope
= LDB_SCOPE_SUBTREE
;
502 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
503 info1
->dns_domain_name
= NULL
;
504 info1
->result_name
= NULL
;
507 return WERR_INVALID_PARAMETER
;
510 /* TODO: - fill the correct names in all cases!
511 * - handle format_flags
513 if (format_desired
== DRSUAPI_DS_NAME_FORMAT_UNKNOWN
) {
516 /* here we need to set the domain_filter and/or the result_filter */
517 switch (format_offered
) {
518 case DRSUAPI_DS_NAME_FORMAT_UNKNOWN
:
521 enum drsuapi_DsNameFormat formats
[] = {
522 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
, DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
,
523 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
, DRSUAPI_DS_NAME_FORMAT_CANONICAL
,
524 DRSUAPI_DS_NAME_FORMAT_GUID
, DRSUAPI_DS_NAME_FORMAT_DISPLAY
,
525 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
,
526 DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
,
527 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
530 for (i
=0; i
< ARRAY_SIZE(formats
); i
++) {
531 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, format_flags
, formats
[i
], format_desired
, name
, info1
);
532 if (!W_ERROR_IS_OK(werr
)) {
535 if (info1
->status
!= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
&&
536 (formats
[i
] != DRSUAPI_DS_NAME_FORMAT_CANONICAL
||
537 info1
->status
!= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
))
545 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
546 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
548 char *str
, *s
, *account
;
549 const char *str_encoded
= NULL
;
550 scope
= LDB_SCOPE_ONELEVEL
;
552 if (strlen(name
) == 0) {
553 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
557 str
= talloc_strdup(mem_ctx
, name
);
558 W_ERROR_HAVE_NO_MEMORY(str
);
560 if (format_offered
== DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
) {
561 /* Look backwards for the \n, and replace it with / */
562 s
= strrchr(str
, '\n');
564 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
570 s
= strchr(str
, '/');
572 /* there must be at least one / */
573 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
580 str_encoded
= ldb_binary_encode_string(mem_ctx
, str
);
581 if (str_encoded
== NULL
) {
582 return WERR_NOT_ENOUGH_MEMORY
;
585 domain_filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=crossRef)(dnsRoot=%s)(systemFlags:%s:=%u))",
587 LDB_OID_COMPARATOR_AND
,
588 SYSTEM_FLAG_CR_NTDS_DOMAIN
);
589 W_ERROR_HAVE_NO_MEMORY(domain_filter
);
591 /* There may not be anything after the domain component (search for the domain itself) */
593 if (account
&& *account
) {
594 WERROR werr
= get_format_functional_filtering_param(sam_ctx
,
601 if (!W_ERROR_IS_OK(werr
)) {
604 if (info1
->status
!= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
)
609 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
: {
612 char *domain_encoded
= NULL
;
613 const char *account
= NULL
;
615 domain
= talloc_strdup(mem_ctx
, name
);
616 W_ERROR_HAVE_NO_MEMORY(domain
);
618 p
= strchr(domain
, '\\');
620 /* invalid input format */
621 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
630 domain_encoded
= ldb_binary_encode_string(mem_ctx
, domain
);
631 if (domain_encoded
== NULL
) {
632 return WERR_NOT_ENOUGH_MEMORY
;
635 domain_filter
= talloc_asprintf(mem_ctx
,
636 "(&(objectClass=crossRef)(netbiosName=%s)(systemFlags:%s:=%u))",
638 LDB_OID_COMPARATOR_AND
,
639 SYSTEM_FLAG_CR_NTDS_DOMAIN
);
640 W_ERROR_HAVE_NO_MEMORY(domain_filter
);
642 const char *account_encoded
= NULL
;
644 account_encoded
= ldb_binary_encode_string(mem_ctx
, account
);
645 if (account_encoded
== NULL
) {
646 return WERR_NOT_ENOUGH_MEMORY
;
649 result_filter
= talloc_asprintf(mem_ctx
, "(sAMAccountName=%s)",
651 W_ERROR_HAVE_NO_MEMORY(result_filter
);
658 /* A LDAP DN as a string */
659 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779
: {
660 domain_filter
= NULL
;
661 name_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, name
);
662 if (! ldb_dn_validate(name_dn
)) {
663 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
669 /* A GUID as a string */
670 case DRSUAPI_DS_NAME_FORMAT_GUID
: {
674 domain_filter
= NULL
;
676 nt_status
= GUID_from_string(name
, &guid
);
677 if (!NT_STATUS_IS_OK(nt_status
)) {
678 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
682 ldap_guid
= ldap_encode_ndr_GUID(mem_ctx
, &guid
);
684 return WERR_NOT_ENOUGH_MEMORY
;
686 result_filter
= talloc_asprintf(mem_ctx
, "(objectGUID=%s)",
688 W_ERROR_HAVE_NO_MEMORY(result_filter
);
691 case DRSUAPI_DS_NAME_FORMAT_DISPLAY
: {
692 const char *name_encoded
= NULL
;
694 domain_filter
= NULL
;
696 name_encoded
= ldb_binary_encode_string(mem_ctx
, name
);
697 if (name_encoded
== NULL
) {
698 return WERR_NOT_ENOUGH_MEMORY
;
701 result_filter
= talloc_asprintf(mem_ctx
, "(|(displayName=%s)(samAccountName=%s))",
704 W_ERROR_HAVE_NO_MEMORY(result_filter
);
708 /* A S-1234-5678 style string */
709 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
: {
710 struct dom_sid
*sid
= dom_sid_parse_talloc(mem_ctx
, name
);
713 domain_filter
= NULL
;
715 info1
->dns_domain_name
= NULL
;
716 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
719 ldap_sid
= ldap_encode_ndr_dom_sid(mem_ctx
,
722 return WERR_NOT_ENOUGH_MEMORY
;
724 result_filter
= talloc_asprintf(mem_ctx
, "(objectSid=%s)",
726 W_ERROR_HAVE_NO_MEMORY(result_filter
);
729 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
: {
730 krb5_principal principal
;
732 const char *unparsed_name_encoded
= NULL
;
734 ret
= smb_krb5_init_context(mem_ctx
,
735 (struct loadparm_context
*)ldb_get_opaque(sam_ctx
, "loadparm"),
739 return WERR_NOT_ENOUGH_MEMORY
;
742 /* Ensure we reject complete junk first */
743 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
, name
, &principal
);
745 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
749 domain_filter
= NULL
;
752 * By getting the unparsed name here, we ensure the
753 * escaping is removed correctly (and trust the client
754 * less). The important thing here is that a
755 * userPrincipalName may have a space in it, and this
756 * must not be kerberos escaped to match this filter,
757 * so we specify KRB5_PRINCIPAL_UNPARSE_DISPLAY
759 ret
= krb5_unparse_name_flags(smb_krb5_context
->krb5_context
,
761 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
764 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
765 return WERR_NOT_ENOUGH_MEMORY
;
768 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
770 /* The ldb_binary_encode_string() here avoids LDAP filter injection attacks */
771 unparsed_name_encoded
= ldb_binary_encode_string(mem_ctx
, unparsed_name
);
772 if (unparsed_name_encoded
== NULL
) {
773 return WERR_NOT_ENOUGH_MEMORY
;
776 result_filter
= talloc_asprintf(mem_ctx
, "(&(userPrincipalName=%s)(objectClass=user))",
777 unparsed_name_encoded
);
780 W_ERROR_HAVE_NO_MEMORY(result_filter
);
783 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
: {
784 krb5_principal principal
;
785 char *unparsed_name_short
;
786 const char *unparsed_name_short_encoded
= NULL
;
787 bool principal_is_host
= false;
789 ret
= smb_krb5_init_context(mem_ctx
,
790 (struct loadparm_context
*)ldb_get_opaque(sam_ctx
, "loadparm"),
794 return WERR_NOT_ENOUGH_MEMORY
;
797 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
, name
, &principal
);
799 krb5_princ_size(smb_krb5_context
->krb5_context
,
801 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
802 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
804 } else if (ret
== 0) {
805 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
807 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
, name
,
808 KRB5_PRINCIPAL_PARSE_NO_REALM
, &principal
);
810 return dns_domain_from_principal(mem_ctx
, smb_krb5_context
,
814 domain_filter
= NULL
;
816 ret
= krb5_unparse_name_flags(smb_krb5_context
->krb5_context
, principal
,
817 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &unparsed_name_short
);
819 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
820 return WERR_NOT_ENOUGH_MEMORY
;
823 unparsed_name_short_encoded
= ldb_binary_encode_string(mem_ctx
, unparsed_name_short
);
824 if (unparsed_name_short_encoded
== NULL
) {
825 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
826 free(unparsed_name_short
);
827 return WERR_NOT_ENOUGH_MEMORY
;
830 if ((krb5_princ_size(smb_krb5_context
->krb5_context
, principal
) == 2)) {
833 ret
= smb_krb5_princ_component(smb_krb5_context
->krb5_context
,
834 principal
, 0, &component
);
836 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
837 free(unparsed_name_short
);
838 return WERR_INTERNAL_ERROR
;
841 principal_is_host
= strcasecmp(component
.data
, "host") == 0;
844 if (principal_is_host
) {
845 /* the 'cn' attribute is just the leading part of the name */
848 const char *computer_name_encoded
= NULL
;
849 ret
= smb_krb5_princ_component(
850 smb_krb5_context
->krb5_context
,
851 principal
, 1, &component
);
853 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
854 free(unparsed_name_short
);
855 return WERR_INTERNAL_ERROR
;
857 computer_name
= talloc_strndup(mem_ctx
, (char *)component
.data
,
858 strcspn((char *)component
.data
, "."));
859 if (computer_name
== NULL
) {
860 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
861 free(unparsed_name_short
);
862 return WERR_NOT_ENOUGH_MEMORY
;
865 computer_name_encoded
= ldb_binary_encode_string(mem_ctx
, computer_name
);
866 if (computer_name_encoded
== NULL
) {
867 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
868 free(unparsed_name_short
);
869 return WERR_NOT_ENOUGH_MEMORY
;
872 result_filter
= talloc_asprintf(mem_ctx
, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
873 unparsed_name_short_encoded
,
874 computer_name_encoded
);
876 result_filter
= talloc_asprintf(mem_ctx
, "(&(servicePrincipalName=%s)(objectClass=user))",
877 unparsed_name_short_encoded
);
879 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
880 free(unparsed_name_short
);
881 W_ERROR_HAVE_NO_MEMORY(result_filter
);
886 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
891 if (format_flags
& DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY
) {
892 return DsCrackNameOneSyntactical(mem_ctx
, format_offered
, format_desired
,
893 name_dn
, name
, info1
);
896 return DsCrackNameOneFilter(sam_ctx
, mem_ctx
,
898 format_flags
, format_offered
, format_desired
,
900 domain_filter
, result_filter
,
901 info1
, scope
, search_dn
);
904 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
905 * (FQDN_1779) into a canonical name without actually searching the
908 static WERROR
DsCrackNameOneSyntactical(TALLOC_CTX
*mem_ctx
,
909 enum drsuapi_DsNameFormat format_offered
,
910 enum drsuapi_DsNameFormat format_desired
,
911 struct ldb_dn
*name_dn
, const char *name
,
912 struct drsuapi_DsNameInfo1
*info1
)
915 if (format_offered
!= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
) {
916 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING
;
920 switch (format_desired
) {
921 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
922 cracked
= ldb_dn_canonical_string(mem_ctx
, name_dn
);
924 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
925 cracked
= ldb_dn_canonical_ex_string(mem_ctx
, name_dn
);
928 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING
;
931 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
932 info1
->result_name
= cracked
;
934 return WERR_NOT_ENOUGH_MEMORY
;
940 /* Given a filter for the domain, and one for the result, perform the
941 * ldb search. The format offered and desired flags change the
942 * behaviours, including what attributes to return.
944 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
947 static WERROR
DsCrackNameOneFilter(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
948 struct smb_krb5_context
*smb_krb5_context
,
949 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
950 enum drsuapi_DsNameFormat format_desired
,
951 struct ldb_dn
*name_dn
, const char *name
,
952 const char *domain_filter
, const char *result_filter
,
953 struct drsuapi_DsNameInfo1
*info1
,
954 int scope
, struct ldb_dn
*search_dn
)
957 struct ldb_result
*domain_res
= NULL
;
958 const char * const *domain_attrs
;
959 const char * const *result_attrs
;
960 struct ldb_message
**result_res
= NULL
;
961 struct ldb_message
*result
= NULL
;
964 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(sam_ctx
, mem_ctx
);
966 const char * const _domain_attrs_1779
[] = { "ncName", "dnsRoot", NULL
};
967 const char * const _result_attrs_null
[] = { NULL
};
969 const char * const _domain_attrs_canonical
[] = { "ncName", "dnsRoot", NULL
};
970 const char * const _result_attrs_canonical
[] = { "canonicalName", NULL
};
972 const char * const _domain_attrs_nt4
[] = { "ncName", "dnsRoot", "nETBIOSName", NULL
};
973 const char * const _result_attrs_nt4
[] = { "sAMAccountName", "objectSid", "objectClass", NULL
};
975 const char * const _domain_attrs_guid
[] = { "ncName", "dnsRoot", NULL
};
976 const char * const _result_attrs_guid
[] = { "objectGUID", NULL
};
978 const char * const _domain_attrs_upn
[] = { "ncName", "dnsRoot", NULL
};
979 const char * const _result_attrs_upn
[] = { "userPrincipalName", NULL
};
981 const char * const _domain_attrs_spn
[] = { "ncName", "dnsRoot", NULL
};
982 const char * const _result_attrs_spn
[] = { "servicePrincipalName", NULL
};
984 const char * const _domain_attrs_display
[] = { "ncName", "dnsRoot", NULL
};
985 const char * const _result_attrs_display
[] = { "displayName", "sAMAccountName", NULL
};
987 const char * const _domain_attrs_sid
[] = { "ncName", "dnsRoot", NULL
};
988 const char * const _result_attrs_sid
[] = { "objectSid", NULL
};
990 const char * const _domain_attrs_none
[] = { "ncName", "dnsRoot" , NULL
};
991 const char * const _result_attrs_none
[] = { NULL
};
993 /* here we need to set the attrs lists for domain and result lookups */
994 switch (format_desired
) {
995 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779
:
996 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
997 domain_attrs
= _domain_attrs_1779
;
998 result_attrs
= _result_attrs_null
;
1000 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
1001 domain_attrs
= _domain_attrs_canonical
;
1002 result_attrs
= _result_attrs_canonical
;
1004 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
:
1005 domain_attrs
= _domain_attrs_nt4
;
1006 result_attrs
= _result_attrs_nt4
;
1008 case DRSUAPI_DS_NAME_FORMAT_GUID
:
1009 domain_attrs
= _domain_attrs_guid
;
1010 result_attrs
= _result_attrs_guid
;
1012 case DRSUAPI_DS_NAME_FORMAT_DISPLAY
:
1013 domain_attrs
= _domain_attrs_display
;
1014 result_attrs
= _result_attrs_display
;
1016 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
:
1017 domain_attrs
= _domain_attrs_upn
;
1018 result_attrs
= _result_attrs_upn
;
1020 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
:
1021 domain_attrs
= _domain_attrs_spn
;
1022 result_attrs
= _result_attrs_spn
;
1024 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
:
1025 domain_attrs
= _domain_attrs_sid
;
1026 result_attrs
= _result_attrs_sid
;
1029 domain_attrs
= _domain_attrs_none
;
1030 result_attrs
= _result_attrs_none
;
1034 if (domain_filter
) {
1035 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
1036 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
1040 "%s", domain_filter
);
1042 if (ldb_ret
!= LDB_SUCCESS
) {
1043 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
1044 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1048 switch (domain_res
->count
) {
1052 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1055 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1059 info1
->dns_domain_name
= ldb_msg_find_attr_as_string(domain_res
->msgs
[0], "dnsRoot", NULL
);
1060 W_ERROR_HAVE_NO_MEMORY(info1
->dns_domain_name
);
1061 info1
->status
= DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
;
1063 info1
->dns_domain_name
= NULL
;
1064 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1067 if (result_filter
) {
1069 struct ldb_result
*res
;
1070 uint32_t dsdb_flags
= 0;
1071 struct ldb_dn
*real_search_dn
= NULL
;
1072 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1075 * From 4.1.4.2.11 of MS-DRSR
1076 * if DS_NAME_FLAG_GCVERIFY in flags then
1077 * rt := select all O from all
1078 * where attrValue in GetAttrVals(O, att, false)
1080 * rt := select all O from subtree DefaultNC()
1081 * where attrValue in GetAttrVals(O, att, false)
1085 if (format_flags
& DRSUAPI_DS_NAME_FLAG_GCVERIFY
||
1086 format_offered
== DRSUAPI_DS_NAME_FORMAT_GUID
)
1088 dsdb_flags
= DSDB_SEARCH_SEARCH_ALL_PARTITIONS
;
1089 } else if (domain_res
) {
1091 struct ldb_dn
*tmp_dn
= samdb_result_dn(sam_ctx
, mem_ctx
, domain_res
->msgs
[0], "ncName", NULL
);
1092 real_search_dn
= tmp_dn
;
1094 real_search_dn
= search_dn
;
1097 real_search_dn
= ldb_get_default_basedn(sam_ctx
);
1099 if (format_offered
== DRSUAPI_DS_NAME_FORMAT_GUID
){
1100 dsdb_flags
|= DSDB_SEARCH_SHOW_RECYCLED
;
1102 /* search with the 'phantom root' flag */
1103 ret
= dsdb_search(sam_ctx
, mem_ctx
, &res
,
1108 "%s", result_filter
);
1109 if (ret
!= LDB_SUCCESS
) {
1110 DEBUG(2, ("DsCrackNameOneFilter search from '%s' with flags 0x%08x failed: %s\n",
1111 ldb_dn_get_linearized(real_search_dn
),
1113 ldb_errstring(sam_ctx
)));
1114 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1118 ldb_ret
= res
->count
;
1119 result_res
= res
->msgs
;
1120 } else if (format_offered
== DRSUAPI_DS_NAME_FORMAT_FQDN_1779
) {
1121 ldb_ret
= gendb_search_dn(sam_ctx
, mem_ctx
, name_dn
, &result_res
,
1123 } else if (domain_res
) {
1124 name_dn
= samdb_result_dn(sam_ctx
, mem_ctx
, domain_res
->msgs
[0], "ncName", NULL
);
1125 ldb_ret
= gendb_search_dn(sam_ctx
, mem_ctx
, name_dn
, &result_res
,
1129 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not available: This can't happen...\n"));
1130 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1136 result
= result_res
[0];
1139 switch (format_offered
) {
1140 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
:
1141 return DsCrackNameSPNAlias(sam_ctx
, mem_ctx
,
1143 format_flags
, format_offered
, format_desired
,
1146 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
:
1147 return DsCrackNameUPN(sam_ctx
, mem_ctx
, smb_krb5_context
,
1148 format_flags
, format_offered
, format_desired
,
1153 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1156 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s\n", ldb_errstring(sam_ctx
)));
1157 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1160 switch (format_offered
) {
1161 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
1162 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
1164 const char *canonical_name
= NULL
; /* Not required, but we get warnings... */
1165 /* We may need to manually filter further */
1166 for (i
= 0; i
< ldb_ret
; i
++) {
1167 switch (format_offered
) {
1168 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
1169 canonical_name
= ldb_dn_canonical_string(mem_ctx
, result_res
[i
]->dn
);
1171 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
1172 canonical_name
= ldb_dn_canonical_ex_string(mem_ctx
, result_res
[i
]->dn
);
1177 if (strcasecmp_m(canonical_name
, name
) == 0) {
1178 result
= result_res
[i
];
1183 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1189 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1194 info1
->dns_domain_name
= ldb_dn_canonical_string(mem_ctx
, result
->dn
);
1195 W_ERROR_HAVE_NO_MEMORY(info1
->dns_domain_name
);
1196 p
= strchr(info1
->dns_domain_name
, '/');
1201 /* here we can use result and domain_res[0] */
1202 switch (format_desired
) {
1203 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779
: {
1204 info1
->result_name
= ldb_dn_alloc_linearized(mem_ctx
, result
->dn
);
1205 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
1207 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1210 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
: {
1211 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "canonicalName", NULL
);
1212 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1215 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
: {
1216 /* Not in the virtual ldb attribute */
1217 return DsCrackNameOneSyntactical(mem_ctx
,
1218 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1219 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
,
1220 result
->dn
, name
, info1
);
1222 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
: {
1224 const struct dom_sid
*sid
= samdb_result_dom_sid(mem_ctx
, result
, "objectSid");
1225 const char *_acc
= "", *_dom
= "";
1227 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1231 if (samdb_find_attribute(sam_ctx
, result
, "objectClass",
1233 /* This can also find a DomainDNSZones entry,
1234 * but it won't have the SID we just
1236 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
1240 "(ncName=%s)", ldb_dn_get_linearized(result
->dn
));
1242 if (ldb_ret
!= LDB_SUCCESS
) {
1243 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
1244 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1248 switch (domain_res
->count
) {
1252 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1255 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1258 _dom
= ldb_msg_find_attr_as_string(domain_res
->msgs
[0], "nETBIOSName", NULL
);
1259 W_ERROR_HAVE_NO_MEMORY(_dom
);
1261 _acc
= ldb_msg_find_attr_as_string(result
, "sAMAccountName", NULL
);
1263 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1266 if (dom_sid_in_domain(&global_sid_Builtin
, sid
)) {
1269 const char *attrs
[] = { NULL
};
1270 struct ldb_result
*domain_res2
;
1271 struct dom_sid
*dom_sid
= dom_sid_dup(mem_ctx
, sid
);
1275 dom_sid
->num_auths
--;
1276 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
1280 "(&(objectSid=%s)(objectClass=domain))",
1281 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid
));
1283 if (ldb_ret
!= LDB_SUCCESS
) {
1284 DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s\n", ldb_errstring(sam_ctx
)));
1285 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1289 switch (domain_res
->count
) {
1293 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1296 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1300 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res2
,
1304 "(ncName=%s)", ldb_dn_get_linearized(domain_res
->msgs
[0]->dn
));
1306 if (ldb_ret
!= LDB_SUCCESS
) {
1307 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
1308 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1312 switch (domain_res2
->count
) {
1316 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1319 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1322 _dom
= ldb_msg_find_attr_as_string(domain_res2
->msgs
[0], "nETBIOSName", NULL
);
1323 W_ERROR_HAVE_NO_MEMORY(_dom
);
1327 info1
->result_name
= talloc_asprintf(mem_ctx
, "%s\\%s", _dom
, _acc
);
1328 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
1330 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1333 case DRSUAPI_DS_NAME_FORMAT_GUID
: {
1336 guid
= samdb_result_guid(result
, "objectGUID");
1338 info1
->result_name
= GUID_string2(mem_ctx
, &guid
);
1339 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
1341 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1344 case DRSUAPI_DS_NAME_FORMAT_DISPLAY
: {
1345 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "displayName", NULL
);
1346 if (!info1
->result_name
) {
1347 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "sAMAccountName", NULL
);
1349 if (!info1
->result_name
) {
1350 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1352 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1356 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
: {
1357 struct ldb_message_element
*el
1358 = ldb_msg_find_element(result
,
1359 "servicePrincipalName");
1361 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1363 } else if (el
->num_values
> 1) {
1364 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1368 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "servicePrincipalName", NULL
);
1369 if (!info1
->result_name
) {
1370 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1372 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1376 case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN
: {
1377 info1
->dns_domain_name
= NULL
;
1378 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1381 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
: {
1382 const struct dom_sid
*sid
= samdb_result_dom_sid(mem_ctx
, result
, "objectSid");
1385 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1389 info1
->result_name
= dom_sid_string(mem_ctx
, sid
);
1390 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
1392 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1395 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
: {
1396 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "userPrincipalName", NULL
);
1397 if (!info1
->result_name
) {
1398 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1400 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1405 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1410 /* Given a user Principal Name (such as foo@bar.com),
1411 * return the user and domain DNs. This is used in the KDC to then
1412 * return the Keys and evaluate policy */
1414 NTSTATUS
crack_user_principal_name(struct ldb_context
*sam_ctx
,
1415 TALLOC_CTX
*mem_ctx
,
1416 const char *user_principal_name
,
1417 struct ldb_dn
**user_dn
,
1418 struct ldb_dn
**domain_dn
)
1421 struct drsuapi_DsNameInfo1 info1
;
1422 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1423 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
,
1424 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1425 user_principal_name
,
1427 if (!W_ERROR_IS_OK(werr
)) {
1428 return werror_to_ntstatus(werr
);
1430 switch (info1
.status
) {
1431 case DRSUAPI_DS_NAME_STATUS_OK
:
1433 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1434 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1435 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1436 return NT_STATUS_NO_SUCH_USER
;
1437 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1439 return NT_STATUS_UNSUCCESSFUL
;
1442 *user_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1445 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1446 DRSUAPI_DS_NAME_FORMAT_CANONICAL
,
1447 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1448 talloc_asprintf(mem_ctx
, "%s/",
1449 info1
.dns_domain_name
),
1451 if (!W_ERROR_IS_OK(werr
)) {
1452 return werror_to_ntstatus(werr
);
1454 switch (info1
.status
) {
1455 case DRSUAPI_DS_NAME_STATUS_OK
:
1457 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1458 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1459 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1460 return NT_STATUS_NO_SUCH_USER
;
1461 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1463 return NT_STATUS_UNSUCCESSFUL
;
1466 *domain_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1469 return NT_STATUS_OK
;
1472 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1473 * return the user and domain DNs. This is used in the KDC to then
1474 * return the Keys and evaluate policy */
1476 NTSTATUS
crack_service_principal_name(struct ldb_context
*sam_ctx
,
1477 TALLOC_CTX
*mem_ctx
,
1478 const char *service_principal_name
,
1479 struct ldb_dn
**user_dn
,
1480 struct ldb_dn
**domain_dn
)
1483 struct drsuapi_DsNameInfo1 info1
;
1484 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1485 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
,
1486 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1487 service_principal_name
,
1489 if (!W_ERROR_IS_OK(werr
)) {
1490 return werror_to_ntstatus(werr
);
1492 switch (info1
.status
) {
1493 case DRSUAPI_DS_NAME_STATUS_OK
:
1495 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1496 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1497 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1498 return NT_STATUS_NO_SUCH_USER
;
1499 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1501 return NT_STATUS_UNSUCCESSFUL
;
1504 *user_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1507 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1508 DRSUAPI_DS_NAME_FORMAT_CANONICAL
,
1509 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1510 talloc_asprintf(mem_ctx
, "%s/",
1511 info1
.dns_domain_name
),
1513 if (!W_ERROR_IS_OK(werr
)) {
1514 return werror_to_ntstatus(werr
);
1516 switch (info1
.status
) {
1517 case DRSUAPI_DS_NAME_STATUS_OK
:
1519 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1520 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1521 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1522 return NT_STATUS_NO_SUCH_USER
;
1523 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1525 return NT_STATUS_UNSUCCESSFUL
;
1528 *domain_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1531 return NT_STATUS_OK
;
1534 NTSTATUS
crack_name_to_nt4_name(TALLOC_CTX
*mem_ctx
,
1535 struct ldb_context
*ldb
,
1536 enum drsuapi_DsNameFormat format_offered
,
1538 const char **nt4_domain
, const char **nt4_account
)
1541 struct drsuapi_DsNameInfo1 info1
;
1544 /* Handle anonymous bind */
1545 if (!name
|| !*name
) {
1548 return NT_STATUS_OK
;
1551 werr
= DsCrackNameOneName(ldb
, mem_ctx
, 0,
1553 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
,
1556 if (!W_ERROR_IS_OK(werr
)) {
1557 return werror_to_ntstatus(werr
);
1559 switch (info1
.status
) {
1560 case DRSUAPI_DS_NAME_STATUS_OK
:
1562 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1563 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1564 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1565 return NT_STATUS_NO_SUCH_USER
;
1566 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1568 return NT_STATUS_UNSUCCESSFUL
;
1571 *nt4_domain
= talloc_strdup(mem_ctx
, info1
.result_name
);
1572 if (*nt4_domain
== NULL
) {
1573 return NT_STATUS_NO_MEMORY
;
1576 p
= strchr(*nt4_domain
, '\\');
1578 return NT_STATUS_INVALID_PARAMETER
;
1582 *nt4_account
= talloc_strdup(mem_ctx
, &p
[1]);
1583 if (*nt4_account
== NULL
) {
1584 return NT_STATUS_NO_MEMORY
;
1587 return NT_STATUS_OK
;
1590 NTSTATUS
crack_auto_name_to_nt4_name(TALLOC_CTX
*mem_ctx
,
1591 struct ldb_context
*ldb
,
1593 const char **nt4_domain
,
1594 const char **nt4_account
)
1596 enum drsuapi_DsNameFormat format_offered
= DRSUAPI_DS_NAME_FORMAT_UNKNOWN
;
1598 /* Handle anonymous bind */
1599 if (!name
|| !*name
) {
1602 return NT_STATUS_OK
;
1606 * Here we only consider a subset of the possible name forms listed in
1607 * [MS-ADTS] 5.1.1.1.1, and we don't retry with a different name form if
1608 * the first attempt fails.
1611 if (strchr_m(name
, '=')) {
1612 format_offered
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
1613 } else if (strchr_m(name
, '@')) {
1614 format_offered
= DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
;
1615 } else if (strchr_m(name
, '\\')) {
1616 format_offered
= DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
;
1617 } else if (strchr_m(name
, '\n')) {
1618 format_offered
= DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
;
1619 } else if (strchr_m(name
, '/')) {
1620 format_offered
= DRSUAPI_DS_NAME_FORMAT_CANONICAL
;
1621 } else if ((name
[0] == 'S' || name
[0] == 's') && name
[1] == '-') {
1622 format_offered
= DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
;
1624 return NT_STATUS_NO_SUCH_USER
;
1627 return crack_name_to_nt4_name(mem_ctx
, ldb
, format_offered
, name
, nt4_domain
, nt4_account
);
1631 WERROR
dcesrv_drsuapi_ListRoles(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
1632 const struct drsuapi_DsNameRequest1
*req1
,
1633 struct drsuapi_DsNameCtr1
**ctr1
)
1635 struct drsuapi_DsNameInfo1
*names
;
1637 uint32_t count
= 5;/*number of fsmo role owners we are going to return*/
1639 *ctr1
= talloc(mem_ctx
, struct drsuapi_DsNameCtr1
);
1640 W_ERROR_HAVE_NO_MEMORY(*ctr1
);
1641 names
= talloc_array(mem_ctx
, struct drsuapi_DsNameInfo1
, count
);
1642 W_ERROR_HAVE_NO_MEMORY(names
);
1644 for (i
= 0; i
< count
; i
++) {
1646 struct ldb_dn
*role_owner_dn
, *fsmo_role_dn
, *server_dn
;
1647 werr
= dsdb_get_fsmo_role_info(mem_ctx
, sam_ctx
, i
,
1648 &fsmo_role_dn
, &role_owner_dn
);
1649 if(!W_ERROR_IS_OK(werr
)) {
1652 server_dn
= ldb_dn_copy(mem_ctx
, role_owner_dn
);
1653 ldb_dn_remove_child_components(server_dn
, 1);
1654 names
[i
].status
= DRSUAPI_DS_NAME_STATUS_OK
;
1655 names
[i
].dns_domain_name
= samdb_dn_to_dnshostname(sam_ctx
, mem_ctx
,
1657 if(!names
[i
].dns_domain_name
) {
1658 DEBUG(4, ("list_roles: Failed to find dNSHostName for server %s\n",
1659 ldb_dn_get_linearized(server_dn
)));
1661 names
[i
].result_name
= talloc_strdup(mem_ctx
, ldb_dn_get_linearized(role_owner_dn
));
1664 (*ctr1
)->count
= count
;
1665 (*ctr1
)->array
= names
;
1670 WERROR
dcesrv_drsuapi_CrackNamesByNameFormat(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
1671 const struct drsuapi_DsNameRequest1
*req1
,
1672 struct drsuapi_DsNameCtr1
**ctr1
)
1674 struct drsuapi_DsNameInfo1
*names
;
1678 *ctr1
= talloc_zero(mem_ctx
, struct drsuapi_DsNameCtr1
);
1679 W_ERROR_HAVE_NO_MEMORY(*ctr1
);
1681 count
= req1
->count
;
1682 names
= talloc_array(mem_ctx
, struct drsuapi_DsNameInfo1
, count
);
1683 W_ERROR_HAVE_NO_MEMORY(names
);
1685 for (i
=0; i
< count
; i
++) {
1686 status
= DsCrackNameOneName(sam_ctx
, mem_ctx
,
1688 req1
->format_offered
,
1689 req1
->format_desired
,
1692 if (!W_ERROR_IS_OK(status
)) {
1697 (*ctr1
)->count
= count
;
1698 (*ctr1
)->array
= names
;
1703 WERROR
dcesrv_drsuapi_ListInfoServer(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
1704 const struct drsuapi_DsNameRequest1
*req1
,
1705 struct drsuapi_DsNameCtr1
**_ctr1
)
1707 struct drsuapi_DsNameInfo1
*names
;
1708 struct ldb_result
*res
;
1709 struct ldb_dn
*server_dn
, *dn
;
1710 struct drsuapi_DsNameCtr1
*ctr1
;
1713 const char *attrs
[] = {
1721 ctr1
= talloc_zero(mem_ctx
, struct drsuapi_DsNameCtr1
);
1722 W_ERROR_HAVE_NO_MEMORY(ctr1
);
1725 * No magic value here, we have to return 3 entries according to the
1729 names
= talloc_zero_array(ctr1
, struct drsuapi_DsNameInfo1
,
1731 W_ERROR_HAVE_NO_MEMORY(names
);
1732 ctr1
->array
= names
;
1734 for (i
=0; i
< ctr1
->count
; i
++) {
1735 names
[i
].status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1739 if (req1
->count
!= 1) {
1740 DEBUG(1, ("Expected a count of 1 for the ListInfoServer crackname \n"));
1744 if (req1
->names
[0].str
== NULL
) {
1748 server_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, req1
->names
[0].str
);
1749 W_ERROR_HAVE_NO_MEMORY(server_dn
);
1751 ret
= ldb_search(sam_ctx
, mem_ctx
, &res
, server_dn
, LDB_SCOPE_ONELEVEL
,
1752 NULL
, "(objectClass=nTDSDSA)");
1754 if (ret
!= LDB_SUCCESS
) {
1755 DEBUG(1, ("Search for objectClass=nTDSDSA "
1756 "returned less than 1 objects\n"));
1760 if (res
->count
!= 1) {
1761 DEBUG(1, ("Search for objectClass=nTDSDSA "
1762 "returned less than 1 objects\n"));
1766 if (res
->msgs
[0]->dn
) {
1767 names
[0].result_name
= ldb_dn_alloc_linearized(names
, res
->msgs
[0]->dn
);
1768 W_ERROR_HAVE_NO_MEMORY(names
[0].result_name
);
1769 names
[0].status
= DRSUAPI_DS_NAME_STATUS_OK
;
1774 ret
= ldb_search(sam_ctx
, mem_ctx
, &res
, server_dn
, LDB_SCOPE_BASE
,
1775 attrs
, "(objectClass=*)");
1776 if (ret
!= LDB_SUCCESS
) {
1777 DEBUG(1, ("Search for objectClass=* on dn %s"
1778 "returned %s\n", req1
->names
[0].str
,
1779 ldb_strerror(ret
)));
1783 if (res
->count
!= 1) {
1784 DEBUG(1, ("Search for objectClass=* on dn %s"
1785 "returned less than 1 objects\n", req1
->names
[0].str
));
1789 str
= ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
1791 names
[1].result_name
= talloc_strdup(names
, str
);
1792 W_ERROR_HAVE_NO_MEMORY(names
[1].result_name
);
1793 names
[1].status
= DRSUAPI_DS_NAME_STATUS_OK
;
1796 dn
= ldb_msg_find_attr_as_dn(sam_ctx
, mem_ctx
, res
->msgs
[0], "serverReference");
1798 names
[2].result_name
= ldb_dn_alloc_linearized(names
, dn
);
1799 W_ERROR_HAVE_NO_MEMORY(names
[2].result_name
);
1800 names
[2].status
= DRSUAPI_DS_NAME_STATUS_OK
;