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
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/>.
26 #include "librpc/gen_ndr/drsuapi.h"
27 #include "lib/events/events.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
,
54 struct drsuapi_DsNameInfo1
*info1
)
57 krb5_principal principal
;
58 /* perhaps it's a principal with a realm, so return the right 'domain only' response */
60 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
, name
,
61 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
, &principal
);
63 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
67 /* This isn't an allocation assignemnt, so it is free'ed with the krb5_free_principal */
68 realm
= smb_krb5_principal_get_realm(smb_krb5_context
->krb5_context
, principal
);
70 info1
->dns_domain_name
= talloc_strdup(mem_ctx
, realm
);
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(krb5_context context
, struct ldb_context
*ldb_ctx
,
81 const char *alias_from
,
86 struct ldb_result
*res
;
87 struct ldb_message_element
*spnmappings
;
89 struct ldb_dn
*service_dn
;
92 const char *directory_attrs
[] = {
97 tmp_ctx
= talloc_new(mem_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) {
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
);
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
, '=');
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
;
161 if (strcasecmp(str
, alias_from
) == 0) {
163 talloc_steal(mem_ctx
, mapping
);
164 talloc_free(tmp_ctx
);
165 return DRSUAPI_DS_NAME_STATUS_OK
;
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
)
187 krb5_principal principal
;
188 const krb5_data
*component
;
189 const char *service
, *dns_name
;
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
);
198 DEBUG(2, ("Could not parse principal: %s: %s\n",
199 name
, smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
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
);
212 component
= krb5_princ_component(smb_krb5_context
->krb5_context
,
214 service
= (const char *)component
->data
;
215 component
= krb5_princ_component(smb_krb5_context
->krb5_context
,
217 dns_name
= (const char *)component
->data
;
220 namestatus
= LDB_lookup_spn_alias(smb_krb5_context
->krb5_context
,
222 service
, &new_service
);
224 if (namestatus
== DRSUAPI_DS_NAME_STATUS_NOT_FOUND
) {
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
) {
231 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
233 } else if (namestatus
!= DRSUAPI_DS_NAME_STATUS_OK
) {
234 info1
->status
= namestatus
;
235 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
239 /* reform principal */
240 new_princ
= talloc_asprintf(mem_ctx
, "%s/%s", new_service
, dns_name
);
242 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
246 wret
= DsCrackNameOneName(sam_ctx
, mem_ctx
, format_flags
, format_offered
, format_desired
,
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
) {
256 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
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
)
270 const char *domain_filter
= NULL
;
271 const char *result_filter
= NULL
;
273 krb5_principal principal
;
275 char *unparsed_name_short
;
276 const char *domain_attrs
[] = { NULL
};
277 struct ldb_result
*domain_res
= NULL
;
279 /* Prevent recursion */
281 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
285 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
, name
,
286 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
, &principal
);
288 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
292 realm
= smb_krb5_principal_get_realm(smb_krb5_context
->krb5_context
,
295 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
296 samdb_partitions_dn(sam_ctx
, mem_ctx
),
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
);
305 if (ldb_ret
!= LDB_SUCCESS
) {
306 DEBUG(2, ("DsCrackNameUPN domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
307 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
308 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
312 switch (domain_res
->count
) {
316 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
317 return dns_domain_from_principal(mem_ctx
, smb_krb5_context
,
320 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
321 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
325 ret
= krb5_unparse_name_flags(smb_krb5_context
->krb5_context
, principal
,
326 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &unparsed_name_short
);
327 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
330 free(unparsed_name_short
);
334 /* This may need to be extended for more userPrincipalName variations */
335 result_filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
336 ldb_binary_encode_string(mem_ctx
, unparsed_name_short
));
338 domain_filter
= talloc_asprintf(mem_ctx
, "(distinguishedName=%s)", ldb_dn_get_linearized(domain_res
->msgs
[0]->dn
));
340 if (!result_filter
|| !domain_filter
) {
341 free(unparsed_name_short
);
344 status
= DsCrackNameOneFilter(sam_ctx
, mem_ctx
,
346 format_flags
, format_offered
, format_desired
,
347 NULL
, unparsed_name_short
, domain_filter
, result_filter
,
348 info1
, LDB_SCOPE_SUBTREE
, NULL
);
349 free(unparsed_name_short
);
355 * This function will workout the filtering parameter in order to be able to do
356 * the adapted search when the incomming format is format_functional.
357 * This boils down to defining the search_dn (passed as pointer to ldb_dn *) and the
358 * ldap filter request.
359 * Main input parameters are:
360 * * name, which is the portion of the functional name after the
362 * * domain_filter, which is a ldap search filter used to find the NC DN given the
363 * function name to crack.
365 static WERROR
get_format_functional_filtering_param(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
366 char *name
, struct drsuapi_DsNameInfo1
*info1
,
367 struct ldb_dn
**psearch_dn
, const char *domain_filter
, const char **presult_filter
)
369 struct ldb_result
*domain_res
= NULL
;
370 const char * const domain_attrs
[] = {"ncName", NULL
};
371 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(sam_ctx
, mem_ctx
);
373 char *account
, *s
, *result_filter
= NULL
;
374 struct ldb_dn
*search_dn
= NULL
;
377 *presult_filter
= NULL
;
379 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
383 "%s", domain_filter
);
385 if (ldb_ret
!= LDB_SUCCESS
) {
386 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
387 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
391 if (domain_res
->count
== 1) {
392 struct ldb_dn
*tmp_dn
= samdb_result_dn(sam_ctx
, mem_ctx
, domain_res
->msgs
[0], "ncName", NULL
);
393 const char * const name_attrs
[] = {"name", NULL
};
396 s
= strchr(account
, '/');
400 talloc_free(domain_res
);
402 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
408 if (ldb_ret
!= LDB_SUCCESS
) {
409 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
410 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
413 switch (domain_res
->count
) {
417 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
420 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
425 tmp_dn
= talloc_steal(mem_ctx
, domain_res
->msgs
[0]->dn
);
426 talloc_free(domain_res
);
429 s
= strchr(account
, '/');
431 account
= ldb_binary_encode_string(mem_ctx
, account
);
432 W_ERROR_HAVE_NO_MEMORY(account
);
433 result_filter
= talloc_asprintf(mem_ctx
, "(name=%s)",
435 W_ERROR_HAVE_NO_MEMORY(result_filter
);
437 *psearch_dn
= search_dn
;
438 *presult_filter
= result_filter
;
442 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
444 WERROR
DsCrackNameOneName(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
445 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
446 enum drsuapi_DsNameFormat format_desired
,
447 const char *name
, struct drsuapi_DsNameInfo1
*info1
)
450 const char *domain_filter
= NULL
;
451 const char *result_filter
= NULL
;
452 struct ldb_dn
*name_dn
= NULL
;
453 struct ldb_dn
*search_dn
= NULL
;
455 struct smb_krb5_context
*smb_krb5_context
= NULL
;
456 int scope
= LDB_SCOPE_SUBTREE
;
458 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
459 info1
->dns_domain_name
= NULL
;
460 info1
->result_name
= NULL
;
463 return WERR_INVALID_PARAM
;
466 /* TODO: - fill the correct names in all cases!
467 * - handle format_flags
470 /* here we need to set the domain_filter and/or the result_filter */
471 switch (format_offered
) {
472 case DRSUAPI_DS_NAME_FORMAT_UNKNOWN
:
475 enum drsuapi_DsNameFormat formats
[] = {
476 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
, DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
,
477 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
, DRSUAPI_DS_NAME_FORMAT_CANONICAL
,
478 DRSUAPI_DS_NAME_FORMAT_GUID
, DRSUAPI_DS_NAME_FORMAT_DISPLAY
,
479 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
,
480 DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
,
481 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
484 for (i
=0; i
< ARRAY_SIZE(formats
); i
++) {
485 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, format_flags
, formats
[i
], format_desired
, name
, info1
);
486 if (!W_ERROR_IS_OK(werr
)) {
489 if (info1
->status
!= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
) {
496 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
497 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
499 char *str
, *s
, *account
;
500 scope
= LDB_SCOPE_ONELEVEL
;
502 if (strlen(name
) == 0) {
503 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
507 str
= talloc_strdup(mem_ctx
, name
);
508 W_ERROR_HAVE_NO_MEMORY(str
);
510 if (format_offered
== DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
) {
511 /* Look backwards for the \n, and replace it with / */
512 s
= strrchr(str
, '\n');
514 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
520 s
= strchr(str
, '/');
522 /* there must be at least one / */
523 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
530 domain_filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=crossRef)(dnsRoot=%s)(systemFlags:%s:=%u))",
531 ldb_binary_encode_string(mem_ctx
, str
),
532 LDB_OID_COMPARATOR_AND
,
533 SYSTEM_FLAG_CR_NTDS_DOMAIN
);
534 W_ERROR_HAVE_NO_MEMORY(domain_filter
);
536 /* There may not be anything after the domain component (search for the domain itself) */
538 if (account
&& *account
) {
539 WERROR werr
= get_format_functional_filtering_param(sam_ctx
,
546 if (!W_ERROR_IS_OK(werr
)) {
549 if (info1
->status
!= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
)
554 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
: {
557 const char *account
= NULL
;
559 domain
= talloc_strdup(mem_ctx
, name
);
560 W_ERROR_HAVE_NO_MEMORY(domain
);
562 p
= strchr(domain
, '\\');
564 /* invalid input format */
565 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
574 domain_filter
= talloc_asprintf(mem_ctx
,
575 "(&(objectClass=crossRef)(|(dnsRoot=%s)(netbiosName=%s))(systemFlags:%s:=%u))",
576 ldb_binary_encode_string(mem_ctx
, domain
),
577 ldb_binary_encode_string(mem_ctx
, domain
),
578 LDB_OID_COMPARATOR_AND
,
579 SYSTEM_FLAG_CR_NTDS_DOMAIN
);
580 W_ERROR_HAVE_NO_MEMORY(domain_filter
);
582 result_filter
= talloc_asprintf(mem_ctx
, "(sAMAccountName=%s)",
583 ldb_binary_encode_string(mem_ctx
, account
));
584 W_ERROR_HAVE_NO_MEMORY(result_filter
);
591 /* A LDAP DN as a string */
592 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779
: {
593 domain_filter
= NULL
;
594 name_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, name
);
595 if (! ldb_dn_validate(name_dn
)) {
596 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
602 /* A GUID as a string */
603 case DRSUAPI_DS_NAME_FORMAT_GUID
: {
607 domain_filter
= NULL
;
609 nt_status
= GUID_from_string(name
, &guid
);
610 if (!NT_STATUS_IS_OK(nt_status
)) {
611 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
615 ldap_guid
= ldap_encode_ndr_GUID(mem_ctx
, &guid
);
619 result_filter
= talloc_asprintf(mem_ctx
, "(objectGUID=%s)",
621 W_ERROR_HAVE_NO_MEMORY(result_filter
);
624 case DRSUAPI_DS_NAME_FORMAT_DISPLAY
: {
625 domain_filter
= NULL
;
627 result_filter
= talloc_asprintf(mem_ctx
, "(|(displayName=%s)(samAccountName=%s))",
628 ldb_binary_encode_string(mem_ctx
, name
),
629 ldb_binary_encode_string(mem_ctx
, name
));
630 W_ERROR_HAVE_NO_MEMORY(result_filter
);
634 /* A S-1234-5678 style string */
635 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
: {
636 struct dom_sid
*sid
= dom_sid_parse_talloc(mem_ctx
, name
);
639 domain_filter
= NULL
;
641 info1
->dns_domain_name
= NULL
;
642 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
645 ldap_sid
= ldap_encode_ndr_dom_sid(mem_ctx
,
650 result_filter
= talloc_asprintf(mem_ctx
, "(objectSid=%s)",
652 W_ERROR_HAVE_NO_MEMORY(result_filter
);
655 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
: {
656 krb5_principal principal
;
659 ret
= smb_krb5_init_context(mem_ctx
,
660 ldb_get_event_context(sam_ctx
),
661 (struct loadparm_context
*)ldb_get_opaque(sam_ctx
, "loadparm"),
668 /* Ensure we reject compleate junk first */
669 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
, name
, &principal
);
671 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
675 domain_filter
= NULL
;
677 /* By getting the unparsed name here, we ensure the escaping is correct (and trust the client less) */
678 ret
= krb5_unparse_name(smb_krb5_context
->krb5_context
, principal
, &unparsed_name
);
680 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
684 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
686 /* The ldb_binary_encode_string() here avoid LDAP filter injection attacks */
687 result_filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(userPrincipalName=%s))",
688 ldb_binary_encode_string(mem_ctx
, unparsed_name
));
691 W_ERROR_HAVE_NO_MEMORY(result_filter
);
694 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
: {
695 krb5_principal principal
;
696 char *unparsed_name_short
;
697 const krb5_data
*component
;
700 ret
= smb_krb5_init_context(mem_ctx
,
701 ldb_get_event_context(sam_ctx
),
702 (struct loadparm_context
*)ldb_get_opaque(sam_ctx
, "loadparm"),
709 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
, name
, &principal
);
711 krb5_princ_size(smb_krb5_context
->krb5_context
,
713 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
714 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
716 } else if (ret
== 0) {
717 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
719 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
, name
,
720 KRB5_PRINCIPAL_PARSE_NO_REALM
, &principal
);
722 return dns_domain_from_principal(mem_ctx
, smb_krb5_context
,
726 domain_filter
= NULL
;
728 ret
= krb5_unparse_name_flags(smb_krb5_context
->krb5_context
, principal
,
729 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &unparsed_name_short
);
731 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
735 component
= krb5_princ_component(smb_krb5_context
->krb5_context
,
737 service
= (char *)component
->data
;
738 if ((krb5_princ_size(smb_krb5_context
->krb5_context
,
740 (strcasecmp(service
, "host") == 0)) {
741 /* the 'cn' attribute is just the leading part of the name */
743 component
= krb5_princ_component(
744 smb_krb5_context
->krb5_context
,
746 computer_name
= talloc_strndup(mem_ctx
, (char *)component
->data
,
747 strcspn((char *)component
->data
, "."));
748 if (computer_name
== NULL
) {
749 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
750 free(unparsed_name_short
);
754 result_filter
= talloc_asprintf(mem_ctx
, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
755 ldb_binary_encode_string(mem_ctx
, unparsed_name_short
),
756 ldb_binary_encode_string(mem_ctx
, computer_name
));
758 result_filter
= talloc_asprintf(mem_ctx
, "(&(servicePrincipalName=%s)(objectClass=user))",
759 ldb_binary_encode_string(mem_ctx
, unparsed_name_short
));
761 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
762 free(unparsed_name_short
);
763 W_ERROR_HAVE_NO_MEMORY(result_filter
);
768 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
773 if (format_flags
& DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY
) {
774 return DsCrackNameOneSyntactical(mem_ctx
, format_offered
, format_desired
,
775 name_dn
, name
, info1
);
778 return DsCrackNameOneFilter(sam_ctx
, mem_ctx
,
780 format_flags
, format_offered
, format_desired
,
782 domain_filter
, result_filter
,
783 info1
, scope
, search_dn
);
786 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
787 * (FQDN_1779) into a canoical name without actually searching the
790 static WERROR
DsCrackNameOneSyntactical(TALLOC_CTX
*mem_ctx
,
791 enum drsuapi_DsNameFormat format_offered
,
792 enum drsuapi_DsNameFormat format_desired
,
793 struct ldb_dn
*name_dn
, const char *name
,
794 struct drsuapi_DsNameInfo1
*info1
)
797 if (format_offered
!= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
) {
798 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING
;
802 switch (format_desired
) {
803 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
804 cracked
= ldb_dn_canonical_string(mem_ctx
, name_dn
);
806 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
807 cracked
= ldb_dn_canonical_ex_string(mem_ctx
, name_dn
);
810 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING
;
813 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
814 info1
->result_name
= cracked
;
822 /* Given a filter for the domain, and one for the result, perform the
823 * ldb search. The format offered and desired flags change the
824 * behaviours, including what attributes to return.
826 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
829 static WERROR
DsCrackNameOneFilter(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
830 struct smb_krb5_context
*smb_krb5_context
,
831 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
832 enum drsuapi_DsNameFormat format_desired
,
833 struct ldb_dn
*name_dn
, const char *name
,
834 const char *domain_filter
, const char *result_filter
,
835 struct drsuapi_DsNameInfo1
*info1
,
836 int scope
, struct ldb_dn
*search_dn
)
839 struct ldb_result
*domain_res
= NULL
;
840 const char * const *domain_attrs
;
841 const char * const *result_attrs
;
842 struct ldb_message
**result_res
= NULL
;
843 struct ldb_message
*result
= NULL
;
846 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(sam_ctx
, mem_ctx
);
848 const char * const _domain_attrs_1779
[] = { "ncName", "dnsRoot", NULL
};
849 const char * const _result_attrs_null
[] = { NULL
};
851 const char * const _domain_attrs_canonical
[] = { "ncName", "dnsRoot", NULL
};
852 const char * const _result_attrs_canonical
[] = { "canonicalName", NULL
};
854 const char * const _domain_attrs_nt4
[] = { "ncName", "dnsRoot", "nETBIOSName", NULL
};
855 const char * const _result_attrs_nt4
[] = { "sAMAccountName", "objectSid", "objectClass", NULL
};
857 const char * const _domain_attrs_guid
[] = { "ncName", "dnsRoot", NULL
};
858 const char * const _result_attrs_guid
[] = { "objectGUID", NULL
};
860 const char * const _domain_attrs_display
[] = { "ncName", "dnsRoot", NULL
};
861 const char * const _result_attrs_display
[] = { "displayName", "samAccountName", NULL
};
863 const char * const _domain_attrs_none
[] = { "ncName", "dnsRoot" , NULL
};
864 const char * const _result_attrs_none
[] = { NULL
};
866 /* here we need to set the attrs lists for domain and result lookups */
867 switch (format_desired
) {
868 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779
:
869 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
870 domain_attrs
= _domain_attrs_1779
;
871 result_attrs
= _result_attrs_null
;
873 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
874 domain_attrs
= _domain_attrs_canonical
;
875 result_attrs
= _result_attrs_canonical
;
877 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
:
878 domain_attrs
= _domain_attrs_nt4
;
879 result_attrs
= _result_attrs_nt4
;
881 case DRSUAPI_DS_NAME_FORMAT_GUID
:
882 domain_attrs
= _domain_attrs_guid
;
883 result_attrs
= _result_attrs_guid
;
885 case DRSUAPI_DS_NAME_FORMAT_DISPLAY
:
886 domain_attrs
= _domain_attrs_display
;
887 result_attrs
= _result_attrs_display
;
890 domain_attrs
= _domain_attrs_none
;
891 result_attrs
= _result_attrs_none
;
896 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
897 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
901 "%s", domain_filter
);
903 if (ldb_ret
!= LDB_SUCCESS
) {
904 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
905 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
909 switch (domain_res
->count
) {
913 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
916 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
920 info1
->dns_domain_name
= ldb_msg_find_attr_as_string(domain_res
->msgs
[0], "dnsRoot", NULL
);
921 W_ERROR_HAVE_NO_MEMORY(info1
->dns_domain_name
);
922 info1
->status
= DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
;
924 info1
->dns_domain_name
= NULL
;
925 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
930 struct ldb_result
*res
;
931 uint32_t dsdb_flags
= 0;
932 struct ldb_dn
*real_search_dn
;
936 struct ldb_dn
*tmp_dn
= samdb_result_dn(sam_ctx
, mem_ctx
, domain_res
->msgs
[0], "ncName", NULL
);
937 real_search_dn
= tmp_dn
;
939 real_search_dn
= search_dn
;
942 dsdb_flags
= DSDB_SEARCH_SEARCH_ALL_PARTITIONS
;
943 real_search_dn
= NULL
;
945 if (format_desired
== DRSUAPI_DS_NAME_FORMAT_GUID
){
946 dsdb_flags
= dsdb_flags
| DSDB_SEARCH_SHOW_DELETED
;
949 /* search with the 'phantom root' flag */
950 ret
= dsdb_search(sam_ctx
, mem_ctx
, &res
,
955 "%s", result_filter
);
956 if (ret
!= LDB_SUCCESS
) {
957 DEBUG(2, ("DsCrackNameOneFilter phantom root search failed: %s\n",
958 ldb_errstring(sam_ctx
)));
959 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
963 ldb_ret
= res
->count
;
964 result_res
= res
->msgs
;
965 } else if (format_offered
== DRSUAPI_DS_NAME_FORMAT_FQDN_1779
) {
966 ldb_ret
= gendb_search_dn(sam_ctx
, mem_ctx
, name_dn
, &result_res
,
968 } else if (domain_res
) {
969 name_dn
= samdb_result_dn(sam_ctx
, mem_ctx
, domain_res
->msgs
[0], "ncName", NULL
);
970 ldb_ret
= gendb_search_dn(sam_ctx
, mem_ctx
, name_dn
, &result_res
,
974 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not available: This can't happen...\n"));
975 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
981 result
= result_res
[0];
984 switch (format_offered
) {
985 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
:
986 return DsCrackNameSPNAlias(sam_ctx
, mem_ctx
,
988 format_flags
, format_offered
, format_desired
,
991 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
:
992 return DsCrackNameUPN(sam_ctx
, mem_ctx
, smb_krb5_context
,
993 format_flags
, format_offered
, format_desired
,
998 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1001 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s\n", ldb_errstring(sam_ctx
)));
1002 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1005 switch (format_offered
) {
1006 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
1007 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
1009 const char *canonical_name
= NULL
; /* Not required, but we get warnings... */
1010 /* We may need to manually filter further */
1011 for (i
= 0; i
< ldb_ret
; i
++) {
1012 switch (format_offered
) {
1013 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
1014 canonical_name
= ldb_dn_canonical_string(mem_ctx
, result_res
[i
]->dn
);
1016 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
1017 canonical_name
= ldb_dn_canonical_ex_string(mem_ctx
, result_res
[i
]->dn
);
1022 if (strcasecmp_m(canonical_name
, name
) == 0) {
1023 result
= result_res
[i
];
1028 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1034 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1039 info1
->dns_domain_name
= ldb_dn_canonical_string(mem_ctx
, result
->dn
);
1040 W_ERROR_HAVE_NO_MEMORY(info1
->dns_domain_name
);
1041 p
= strchr(info1
->dns_domain_name
, '/');
1046 /* here we can use result and domain_res[0] */
1047 switch (format_desired
) {
1048 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779
: {
1049 info1
->result_name
= ldb_dn_alloc_linearized(mem_ctx
, result
->dn
);
1050 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
1052 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1055 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
: {
1056 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "canonicalName", NULL
);
1057 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1060 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
: {
1061 /* Not in the virtual ldb attribute */
1062 return DsCrackNameOneSyntactical(mem_ctx
,
1063 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1064 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
,
1065 result
->dn
, name
, info1
);
1067 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
: {
1069 const struct dom_sid
*sid
= samdb_result_dom_sid(mem_ctx
, result
, "objectSid");
1070 const char *_acc
= "", *_dom
= "";
1072 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1076 if (samdb_find_attribute(sam_ctx
, result
, "objectClass",
1078 /* This can also find a DomainDNSZones entry,
1079 * but it won't have the SID we just
1081 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
1085 "(ncName=%s)", ldb_dn_get_linearized(result
->dn
));
1087 if (ldb_ret
!= LDB_SUCCESS
) {
1088 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
1089 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1093 switch (domain_res
->count
) {
1097 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1100 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1103 _dom
= ldb_msg_find_attr_as_string(domain_res
->msgs
[0], "nETBIOSName", NULL
);
1104 W_ERROR_HAVE_NO_MEMORY(_dom
);
1106 _acc
= ldb_msg_find_attr_as_string(result
, "sAMAccountName", NULL
);
1108 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1111 if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx
, SID_BUILTIN
), sid
)) {
1114 const char *attrs
[] = { NULL
};
1115 struct ldb_result
*domain_res2
;
1116 struct dom_sid
*dom_sid
= dom_sid_dup(mem_ctx
, sid
);
1120 dom_sid
->num_auths
--;
1121 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
1125 "(&(objectSid=%s)(objectClass=domain))",
1126 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid
));
1128 if (ldb_ret
!= LDB_SUCCESS
) {
1129 DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s\n", ldb_errstring(sam_ctx
)));
1130 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1134 switch (domain_res
->count
) {
1138 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1141 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1145 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res2
,
1149 "(ncName=%s)", ldb_dn_get_linearized(domain_res
->msgs
[0]->dn
));
1151 if (ldb_ret
!= LDB_SUCCESS
) {
1152 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx
)));
1153 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1157 switch (domain_res2
->count
) {
1161 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1164 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1167 _dom
= ldb_msg_find_attr_as_string(domain_res2
->msgs
[0], "nETBIOSName", NULL
);
1168 W_ERROR_HAVE_NO_MEMORY(_dom
);
1172 info1
->result_name
= talloc_asprintf(mem_ctx
, "%s\\%s", _dom
, _acc
);
1173 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
1175 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1178 case DRSUAPI_DS_NAME_FORMAT_GUID
: {
1181 guid
= samdb_result_guid(result
, "objectGUID");
1183 info1
->result_name
= GUID_string2(mem_ctx
, &guid
);
1184 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
1186 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1189 case DRSUAPI_DS_NAME_FORMAT_DISPLAY
: {
1190 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "displayName", NULL
);
1191 if (!info1
->result_name
) {
1192 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "sAMAccountName", NULL
);
1194 if (!info1
->result_name
) {
1195 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1197 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1201 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
: {
1202 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1205 case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN
:
1206 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
: {
1207 info1
->dns_domain_name
= NULL
;
1208 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1212 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1217 /* Given a user Principal Name (such as foo@bar.com),
1218 * return the user and domain DNs. This is used in the KDC to then
1219 * return the Keys and evaluate policy */
1221 NTSTATUS
crack_user_principal_name(struct ldb_context
*sam_ctx
,
1222 TALLOC_CTX
*mem_ctx
,
1223 const char *user_principal_name
,
1224 struct ldb_dn
**user_dn
,
1225 struct ldb_dn
**domain_dn
)
1228 struct drsuapi_DsNameInfo1 info1
;
1229 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1230 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
,
1231 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1232 user_principal_name
,
1234 if (!W_ERROR_IS_OK(werr
)) {
1235 return werror_to_ntstatus(werr
);
1237 switch (info1
.status
) {
1238 case DRSUAPI_DS_NAME_STATUS_OK
:
1240 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1241 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1242 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1243 return NT_STATUS_NO_SUCH_USER
;
1244 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1246 return NT_STATUS_UNSUCCESSFUL
;
1249 *user_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1252 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1253 DRSUAPI_DS_NAME_FORMAT_CANONICAL
,
1254 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1255 talloc_asprintf(mem_ctx
, "%s/",
1256 info1
.dns_domain_name
),
1258 if (!W_ERROR_IS_OK(werr
)) {
1259 return werror_to_ntstatus(werr
);
1261 switch (info1
.status
) {
1262 case DRSUAPI_DS_NAME_STATUS_OK
:
1264 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1265 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1266 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1267 return NT_STATUS_NO_SUCH_USER
;
1268 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1270 return NT_STATUS_UNSUCCESSFUL
;
1273 *domain_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1276 return NT_STATUS_OK
;
1279 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1280 * return the user and domain DNs. This is used in the KDC to then
1281 * return the Keys and evaluate policy */
1283 NTSTATUS
crack_service_principal_name(struct ldb_context
*sam_ctx
,
1284 TALLOC_CTX
*mem_ctx
,
1285 const char *service_principal_name
,
1286 struct ldb_dn
**user_dn
,
1287 struct ldb_dn
**domain_dn
)
1290 struct drsuapi_DsNameInfo1 info1
;
1291 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1292 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
,
1293 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1294 service_principal_name
,
1296 if (!W_ERROR_IS_OK(werr
)) {
1297 return werror_to_ntstatus(werr
);
1299 switch (info1
.status
) {
1300 case DRSUAPI_DS_NAME_STATUS_OK
:
1302 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1303 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1304 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1305 return NT_STATUS_NO_SUCH_USER
;
1306 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1308 return NT_STATUS_UNSUCCESSFUL
;
1311 *user_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1314 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1315 DRSUAPI_DS_NAME_FORMAT_CANONICAL
,
1316 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1317 talloc_asprintf(mem_ctx
, "%s/",
1318 info1
.dns_domain_name
),
1320 if (!W_ERROR_IS_OK(werr
)) {
1321 return werror_to_ntstatus(werr
);
1323 switch (info1
.status
) {
1324 case DRSUAPI_DS_NAME_STATUS_OK
:
1326 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1327 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1328 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1329 return NT_STATUS_NO_SUCH_USER
;
1330 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1332 return NT_STATUS_UNSUCCESSFUL
;
1335 *domain_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1338 return NT_STATUS_OK
;
1341 NTSTATUS
crack_name_to_nt4_name(TALLOC_CTX
*mem_ctx
,
1342 struct tevent_context
*ev_ctx
,
1343 struct loadparm_context
*lp_ctx
,
1344 enum drsuapi_DsNameFormat format_offered
,
1346 const char **nt4_domain
, const char **nt4_account
)
1349 struct drsuapi_DsNameInfo1 info1
;
1350 struct ldb_context
*ldb
;
1353 /* Handle anonymous bind */
1354 if (!name
|| !*name
) {
1357 return NT_STATUS_OK
;
1360 ldb
= samdb_connect(mem_ctx
, ev_ctx
, lp_ctx
, system_session(lp_ctx
), 0);
1362 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1365 werr
= DsCrackNameOneName(ldb
, mem_ctx
, 0,
1367 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
,
1370 if (!W_ERROR_IS_OK(werr
)) {
1371 return werror_to_ntstatus(werr
);
1373 switch (info1
.status
) {
1374 case DRSUAPI_DS_NAME_STATUS_OK
:
1376 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1377 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1378 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1379 return NT_STATUS_NO_SUCH_USER
;
1380 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1382 return NT_STATUS_UNSUCCESSFUL
;
1385 *nt4_domain
= talloc_strdup(mem_ctx
, info1
.result_name
);
1386 if (*nt4_domain
== NULL
) {
1387 return NT_STATUS_NO_MEMORY
;
1390 p
= strchr(*nt4_domain
, '\\');
1392 return NT_STATUS_INVALID_PARAMETER
;
1396 *nt4_account
= talloc_strdup(mem_ctx
, &p
[1]);
1397 if (*nt4_account
== NULL
) {
1398 return NT_STATUS_NO_MEMORY
;
1401 return NT_STATUS_OK
;
1404 NTSTATUS
crack_auto_name_to_nt4_name(TALLOC_CTX
*mem_ctx
,
1405 struct tevent_context
*ev_ctx
,
1406 struct loadparm_context
*lp_ctx
,
1408 const char **nt4_domain
,
1409 const char **nt4_account
)
1411 enum drsuapi_DsNameFormat format_offered
= DRSUAPI_DS_NAME_FORMAT_UNKNOWN
;
1413 /* Handle anonymous bind */
1414 if (!name
|| !*name
) {
1417 return NT_STATUS_OK
;
1420 if (strchr_m(name
, '=')) {
1421 format_offered
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
1422 } else if (strchr_m(name
, '@')) {
1423 format_offered
= DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
;
1424 } else if (strchr_m(name
, '\\')) {
1425 format_offered
= DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
;
1426 } else if (strchr_m(name
, '/')) {
1427 format_offered
= DRSUAPI_DS_NAME_FORMAT_CANONICAL
;
1429 return NT_STATUS_NO_SUCH_USER
;
1432 return crack_name_to_nt4_name(mem_ctx
, ev_ctx
, lp_ctx
, format_offered
, name
, nt4_domain
, nt4_account
);
1436 WERROR
dcesrv_drsuapi_ListRoles(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
1437 const struct drsuapi_DsNameRequest1
*req1
,
1438 struct drsuapi_DsNameCtr1
**ctr1
)
1440 struct drsuapi_DsNameInfo1
*names
;
1442 uint32_t count
= 5;/*number of fsmo role owners we are going to return*/
1444 *ctr1
= talloc(mem_ctx
, struct drsuapi_DsNameCtr1
);
1445 W_ERROR_HAVE_NO_MEMORY(*ctr1
);
1446 names
= talloc_array(mem_ctx
, struct drsuapi_DsNameInfo1
, count
);
1447 W_ERROR_HAVE_NO_MEMORY(names
);
1449 for (i
= 0; i
< count
; i
++) {
1451 struct ldb_dn
*role_owner_dn
, *fsmo_role_dn
, *server_dn
;
1452 werr
= dsdb_get_fsmo_role_info(mem_ctx
, sam_ctx
, i
,
1453 &fsmo_role_dn
, &role_owner_dn
);
1454 if(!W_ERROR_IS_OK(werr
)) {
1457 server_dn
= ldb_dn_copy(mem_ctx
, role_owner_dn
);
1458 ldb_dn_remove_child_components(server_dn
, 1);
1459 names
[i
].status
= DRSUAPI_DS_NAME_STATUS_OK
;
1460 names
[i
].dns_domain_name
= samdb_dn_to_dnshostname(sam_ctx
, mem_ctx
,
1462 if(!names
[i
].dns_domain_name
) {
1463 DEBUG(4, ("list_roles: Failed to find dNSHostName for server %s\n",
1464 ldb_dn_get_linearized(server_dn
)));
1466 names
[i
].result_name
= talloc_strdup(mem_ctx
, ldb_dn_get_linearized(role_owner_dn
));
1469 (*ctr1
)->count
= count
;
1470 (*ctr1
)->array
= names
;
1475 WERROR
dcesrv_drsuapi_CrackNamesByNameFormat(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
1476 const struct drsuapi_DsNameRequest1
*req1
,
1477 struct drsuapi_DsNameCtr1
**ctr1
)
1479 struct drsuapi_DsNameInfo1
*names
;
1483 *ctr1
= talloc_zero(mem_ctx
, struct drsuapi_DsNameCtr1
);
1484 W_ERROR_HAVE_NO_MEMORY(*ctr1
);
1486 count
= req1
->count
;
1487 names
= talloc_array(mem_ctx
, struct drsuapi_DsNameInfo1
, count
);
1488 W_ERROR_HAVE_NO_MEMORY(names
);
1490 for (i
=0; i
< count
; i
++) {
1491 status
= DsCrackNameOneName(sam_ctx
, mem_ctx
,
1493 req1
->format_offered
,
1494 req1
->format_desired
,
1497 if (!W_ERROR_IS_OK(status
)) {
1502 (*ctr1
)->count
= count
;
1503 (*ctr1
)->array
= names
;
1508 WERROR
dcesrv_drsuapi_ListInfoServer(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
1509 const struct drsuapi_DsNameRequest1
*req1
,
1510 struct drsuapi_DsNameCtr1
**_ctr1
)
1512 struct drsuapi_DsNameInfo1
*names
;
1513 struct ldb_result
*res
;
1514 struct ldb_dn
*server_dn
, *dn
;
1515 struct drsuapi_DsNameCtr1
*ctr1
;
1518 const char *attrs
[] = {
1527 ctr1
= talloc_zero(mem_ctx
, struct drsuapi_DsNameCtr1
);
1528 W_ERROR_HAVE_NO_MEMORY(ctr1
);
1531 * No magic value here, we have to return 3 entries according to the
1535 names
= talloc_zero_array(ctr1
, struct drsuapi_DsNameInfo1
,
1537 W_ERROR_HAVE_NO_MEMORY(names
);
1538 ctr1
->array
= names
;
1540 for (i
=0; i
< ctr1
->count
; i
++) {
1541 names
[i
].status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1545 if (req1
->count
!= 1) {
1546 DEBUG(1, ("Expected a count of 1 for the ListInfoServer crackname \n"));
1550 if (req1
->names
[0].str
== NULL
) {
1554 server_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, req1
->names
[0].str
);
1555 W_ERROR_HAVE_NO_MEMORY(server_dn
);
1557 ret
= ldb_search(sam_ctx
, mem_ctx
, &res
, server_dn
, LDB_SCOPE_ONELEVEL
,
1558 NULL
, "(objectClass=nTDSDSA)");
1560 if (ret
!= LDB_SUCCESS
) {
1561 DEBUG(1, ("Search for objectClass=nTDSDSA "
1562 "returned less than 1 objects\n"));
1566 if (res
->count
!= 1) {
1567 DEBUG(1, ("Search for objectClass=nTDSDSA "
1568 "returned less than 1 objects\n"));
1572 if (res
->msgs
[0]->dn
) {
1573 names
[0].result_name
= ldb_dn_alloc_linearized(names
, res
->msgs
[0]->dn
);
1574 W_ERROR_HAVE_NO_MEMORY(names
[0].result_name
);
1575 names
[0].status
= DRSUAPI_DS_NAME_STATUS_OK
;
1580 ret
= ldb_search(sam_ctx
, mem_ctx
, &res
, server_dn
, LDB_SCOPE_BASE
,
1581 attrs
, "(objectClass=*)");
1582 if (ret
!= LDB_SUCCESS
) {
1583 DEBUG(1, ("Search for objectClass=* on dn %s"
1584 "returned %s\n", req1
->names
[0].str
,
1585 ldb_strerror(ret
)));
1589 if (res
->count
!= 1) {
1590 DEBUG(1, ("Search for objectClass=* on dn %s"
1591 "returned less than 1 objects\n", req1
->names
[0].str
));
1595 str
= ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
1597 names
[1].result_name
= talloc_strdup(names
, str
);
1598 W_ERROR_HAVE_NO_MEMORY(names
[1].result_name
);
1599 names
[1].status
= DRSUAPI_DS_NAME_STATUS_OK
;
1602 dn
= ldb_msg_find_attr_as_dn(sam_ctx
, mem_ctx
, res
->msgs
[0], "serverReference");
1604 names
[2].result_name
= ldb_dn_alloc_linearized(names
, dn
);
1605 W_ERROR_HAVE_NO_MEMORY(names
[2].result_name
);
1606 names
[2].status
= DRSUAPI_DS_NAME_STATUS_OK
;