2 Unix SMB/CIFS implementation.
4 endpoint server for the drsuapi pipe
7 Copyright (C) Stefan Metzmacher 2004
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "librpc/gen_ndr/drsuapi.h"
26 #include "lib/events/events.h"
27 #include "rpc_server/common/common.h"
29 #include <ldb_errors.h>
30 #include "system/kerberos.h"
31 #include "auth/kerberos/kerberos.h"
32 #include "libcli/ldap/ldap_ndr.h"
33 #include "libcli/security/security.h"
34 #include "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"
40 static WERROR
DsCrackNameOneFilter(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
41 struct smb_krb5_context
*smb_krb5_context
,
42 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
43 enum drsuapi_DsNameFormat format_desired
,
44 struct ldb_dn
*name_dn
, const char *name
,
45 const char *domain_filter
, const char *result_filter
,
46 struct drsuapi_DsNameInfo1
*info1
);
47 static WERROR
DsCrackNameOneSyntactical(TALLOC_CTX
*mem_ctx
,
48 enum drsuapi_DsNameFormat format_offered
,
49 enum drsuapi_DsNameFormat format_desired
,
50 struct ldb_dn
*name_dn
, const char *name
,
51 struct drsuapi_DsNameInfo1
*info1
);
53 static WERROR
dns_domain_from_principal(TALLOC_CTX
*mem_ctx
, struct smb_krb5_context
*smb_krb5_context
,
55 struct drsuapi_DsNameInfo1
*info1
)
58 krb5_principal principal
;
59 /* perhaps it's a principal with a realm, so return the right 'domain only' response */
61 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
, name
,
62 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
, &principal
);
64 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
68 /* This isn't an allocation assignemnt, so it is free'ed with the krb5_free_principal */
69 realm
= krb5_principal_get_realm(smb_krb5_context
->krb5_context
, principal
);
71 info1
->dns_domain_name
= talloc_strdup(mem_ctx
, realm
);
72 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
74 W_ERROR_HAVE_NO_MEMORY(info1
->dns_domain_name
);
76 info1
->status
= DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
;
80 static enum drsuapi_DsNameStatus
LDB_lookup_spn_alias(krb5_context context
, struct ldb_context
*ldb_ctx
,
82 const char *alias_from
,
87 struct ldb_result
*res
;
88 struct ldb_message_element
*spnmappings
;
90 struct ldb_dn
*service_dn
;
93 const char *directory_attrs
[] = {
98 tmp_ctx
= talloc_new(mem_ctx
);
100 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
103 service_dn
= ldb_dn_new(tmp_ctx
, ldb_ctx
, "CN=Directory Service,CN=Windows NT,CN=Services");
104 if ( ! ldb_dn_add_base(service_dn
, ldb_get_config_basedn(ldb_ctx
))) {
105 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
107 service_dn_str
= ldb_dn_alloc_linearized(tmp_ctx
, service_dn
);
108 if ( ! service_dn_str
) {
109 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
112 ret
= ldb_search(ldb_ctx
, tmp_ctx
, &res
, service_dn
, LDB_SCOPE_BASE
,
113 directory_attrs
, "(objectClass=nTDSService)");
115 if (ret
!= LDB_SUCCESS
&& ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
116 DEBUG(1, ("ldb_search: dn: %s not found: %s", service_dn_str
, ldb_errstring(ldb_ctx
)));
117 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
118 } else if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
119 DEBUG(1, ("ldb_search: dn: %s not found", service_dn_str
));
120 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
121 } else if (res
->count
!= 1) {
123 DEBUG(1, ("ldb_search: dn: %s not found", service_dn_str
));
124 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
127 spnmappings
= ldb_msg_find_element(res
->msgs
[0], "sPNMappings");
128 if (!spnmappings
|| spnmappings
->num_values
== 0) {
129 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute", service_dn_str
));
130 talloc_free(tmp_ctx
);
131 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
134 for (i
= 0; i
< spnmappings
->num_values
; i
++) {
135 char *mapping
, *p
, *str
;
136 mapping
= talloc_strdup(tmp_ctx
,
137 (const char *)spnmappings
->values
[i
].data
);
139 DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str
));
140 talloc_free(tmp_ctx
);
141 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
144 /* C string manipulation sucks */
146 p
= strchr(mapping
, '=');
148 DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n",
149 service_dn_str
, mapping
));
150 talloc_free(tmp_ctx
);
151 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
162 if (strcasecmp(str
, alias_from
) == 0) {
164 talloc_steal(mem_ctx
, mapping
);
165 talloc_free(tmp_ctx
);
166 return DRSUAPI_DS_NAME_STATUS_OK
;
170 DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from
));
171 talloc_free(tmp_ctx
);
172 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
175 /* When cracking a ServicePrincipalName, many services may be served
176 * by the host/ servicePrincipalName. The incoming query is for cifs/
177 * but we translate it here, and search on host/. This is done after
178 * the cifs/ entry has been searched for, making this a fallback */
180 static WERROR
DsCrackNameSPNAlias(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
181 struct smb_krb5_context
*smb_krb5_context
,
182 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
183 enum drsuapi_DsNameFormat format_desired
,
184 const char *name
, struct drsuapi_DsNameInfo1
*info1
)
188 krb5_principal principal
;
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",
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 (principal
->name
.name_string
.len
< 2) {
208 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
209 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
212 service
= principal
->name
.name_string
.val
[0];
213 dns_name
= principal
->name
.name_string
.val
[1];
216 namestatus
= LDB_lookup_spn_alias(smb_krb5_context
->krb5_context
,
218 service
, &new_service
);
220 if (namestatus
== DRSUAPI_DS_NAME_STATUS_NOT_FOUND
) {
222 info1
->status
= DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
;
223 info1
->dns_domain_name
= talloc_strdup(mem_ctx
, dns_name
);
224 if (!info1
->dns_domain_name
) {
227 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
229 } else if (namestatus
!= DRSUAPI_DS_NAME_STATUS_OK
) {
230 info1
->status
= namestatus
;
231 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
235 /* ooh, very nasty playing around in the Principal... */
236 free(principal
->name
.name_string
.val
[0]);
237 principal
->name
.name_string
.val
[0] = strdup(new_service
);
238 if (!principal
->name
.name_string
.val
[0]) {
239 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
243 /* reform principal */
244 ret
= krb5_unparse_name_flags(smb_krb5_context
->krb5_context
, principal
,
245 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &new_princ
);
248 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
252 wret
= DsCrackNameOneName(sam_ctx
, mem_ctx
, format_flags
, format_offered
, format_desired
,
255 if (W_ERROR_IS_OK(wret
) && (info1
->status
== DRSUAPI_DS_NAME_STATUS_NOT_FOUND
)) {
256 info1
->status
= DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
;
257 info1
->dns_domain_name
= talloc_strdup(mem_ctx
, dns_name
);
258 if (!info1
->dns_domain_name
) {
262 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
266 /* Subcase of CrackNames, for the userPrincipalName */
268 static WERROR
DsCrackNameUPN(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
269 struct smb_krb5_context
*smb_krb5_context
,
270 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
271 enum drsuapi_DsNameFormat format_desired
,
272 const char *name
, struct drsuapi_DsNameInfo1
*info1
)
276 const char *domain_filter
= NULL
;
277 const char *result_filter
= NULL
;
279 krb5_principal principal
;
281 char *unparsed_name_short
;
282 const char *domain_attrs
[] = { NULL
};
283 struct ldb_result
*domain_res
= NULL
;
285 /* Prevent recursion */
287 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
291 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
, name
,
292 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
, &principal
);
294 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
298 realm
= krb5_principal_get_realm(smb_krb5_context
->krb5_context
,
301 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
302 samdb_partitions_dn(sam_ctx
, mem_ctx
),
305 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
306 ldb_binary_encode_string(mem_ctx
, realm
),
307 ldb_binary_encode_string(mem_ctx
, realm
));
309 if (ldb_ret
!= LDB_SUCCESS
) {
310 DEBUG(2, ("DsCrackNameUPN domain ref search failed: %s", ldb_errstring(sam_ctx
)));
311 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
312 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
316 switch (domain_res
->count
) {
320 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
321 return dns_domain_from_principal(mem_ctx
, smb_krb5_context
,
324 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
325 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
329 ret
= krb5_unparse_name_flags(smb_krb5_context
->krb5_context
, principal
,
330 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &unparsed_name_short
);
331 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
334 free(unparsed_name_short
);
338 /* This may need to be extended for more userPrincipalName variations */
339 result_filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
340 ldb_binary_encode_string(mem_ctx
, unparsed_name_short
));
342 domain_filter
= talloc_asprintf(mem_ctx
, "(distinguishedName=%s)", ldb_dn_get_linearized(domain_res
->msgs
[0]->dn
));
344 if (!result_filter
|| !domain_filter
) {
345 free(unparsed_name_short
);
348 status
= DsCrackNameOneFilter(sam_ctx
, mem_ctx
,
350 format_flags
, format_offered
, format_desired
,
351 NULL
, unparsed_name_short
, domain_filter
, result_filter
,
353 free(unparsed_name_short
);
358 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
360 WERROR
DsCrackNameOneName(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
361 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
362 enum drsuapi_DsNameFormat format_desired
,
363 const char *name
, struct drsuapi_DsNameInfo1
*info1
)
366 const char *domain_filter
= NULL
;
367 const char *result_filter
= NULL
;
368 struct ldb_dn
*name_dn
= NULL
;
370 struct smb_krb5_context
*smb_krb5_context
= NULL
;
372 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
373 info1
->dns_domain_name
= NULL
;
374 info1
->result_name
= NULL
;
377 return WERR_INVALID_PARAM
;
380 /* TODO: - fill the correct names in all cases!
381 * - handle format_flags
384 /* here we need to set the domain_filter and/or the result_filter */
385 switch (format_offered
) {
386 case DRSUAPI_DS_NAME_FORMAT_UNKNOWN
:
389 enum drsuapi_DsNameFormat formats
[] = {
390 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
, DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
,
391 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
, DRSUAPI_DS_NAME_FORMAT_CANONICAL
,
392 DRSUAPI_DS_NAME_FORMAT_GUID
, DRSUAPI_DS_NAME_FORMAT_DISPLAY
,
393 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
,
394 DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
,
395 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
398 for (i
=0; i
< ARRAY_SIZE(formats
); i
++) {
399 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, format_flags
, formats
[i
], format_desired
, name
, info1
);
400 if (!W_ERROR_IS_OK(werr
)) {
403 if (info1
->status
!= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
) {
410 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
411 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
413 char *str
, *s
, *account
;
415 if (strlen(name
) == 0) {
416 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
420 str
= talloc_strdup(mem_ctx
, name
);
421 W_ERROR_HAVE_NO_MEMORY(str
);
423 if (format_offered
== DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
) {
424 /* Look backwards for the \n, and replace it with / */
425 s
= strrchr(str
, '\n');
427 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
433 s
= strchr(str
, '/');
435 /* there must be at least one / */
436 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
443 domain_filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=crossRef)(ncName=%s))",
444 ldb_dn_get_linearized(samdb_dns_domain_to_dn(sam_ctx
, mem_ctx
, str
)));
445 W_ERROR_HAVE_NO_MEMORY(domain_filter
);
447 /* There may not be anything after the domain component (search for the domain itself) */
450 account
= strrchr(s
, '/');
456 account
= ldb_binary_encode_string(mem_ctx
, account
);
457 W_ERROR_HAVE_NO_MEMORY(account
);
458 result_filter
= talloc_asprintf(mem_ctx
, "(name=%s)",
460 W_ERROR_HAVE_NO_MEMORY(result_filter
);
464 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
: {
467 struct ldb_dn
*dn_domain
;
468 const char *account
= NULL
;
470 domain
= talloc_strdup(mem_ctx
, name
);
471 W_ERROR_HAVE_NO_MEMORY(domain
);
473 p
= strchr(domain
, '\\');
475 /* invalid input format */
476 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
485 /* it could be in DNS domain form */
486 dn_domain
= samdb_dns_domain_to_dn(sam_ctx
, mem_ctx
, domain
);
487 W_ERROR_HAVE_NO_MEMORY(dn_domain
);
489 domain_filter
= talloc_asprintf(mem_ctx
,
490 "(&(&(|(nETBIOSName=%s)(nCName=%s))(objectclass=crossRef))(ncName=*))",
491 ldb_binary_encode_string(mem_ctx
, domain
),
492 ldb_dn_get_linearized(dn_domain
));
493 W_ERROR_HAVE_NO_MEMORY(domain_filter
);
495 result_filter
= talloc_asprintf(mem_ctx
, "(sAMAccountName=%s)",
496 ldb_binary_encode_string(mem_ctx
, account
));
497 W_ERROR_HAVE_NO_MEMORY(result_filter
);
504 /* A LDAP DN as a string */
505 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779
: {
506 domain_filter
= NULL
;
507 name_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, name
);
508 if (! ldb_dn_validate(name_dn
)) {
509 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
515 /* A GUID as a string */
516 case DRSUAPI_DS_NAME_FORMAT_GUID
: {
520 domain_filter
= NULL
;
522 nt_status
= GUID_from_string(name
, &guid
);
523 if (!NT_STATUS_IS_OK(nt_status
)) {
524 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
528 ldap_guid
= ldap_encode_ndr_GUID(mem_ctx
, &guid
);
532 result_filter
= talloc_asprintf(mem_ctx
, "(objectGUID=%s)",
534 W_ERROR_HAVE_NO_MEMORY(result_filter
);
537 case DRSUAPI_DS_NAME_FORMAT_DISPLAY
: {
538 domain_filter
= NULL
;
540 result_filter
= talloc_asprintf(mem_ctx
, "(|(displayName=%s)(samAccountName=%s))",
541 ldb_binary_encode_string(mem_ctx
, name
),
542 ldb_binary_encode_string(mem_ctx
, name
));
543 W_ERROR_HAVE_NO_MEMORY(result_filter
);
547 /* A S-1234-5678 style string */
548 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
: {
549 struct dom_sid
*sid
= dom_sid_parse_talloc(mem_ctx
, name
);
552 domain_filter
= NULL
;
554 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
557 ldap_sid
= ldap_encode_ndr_dom_sid(mem_ctx
,
562 result_filter
= talloc_asprintf(mem_ctx
, "(objectSid=%s)",
564 W_ERROR_HAVE_NO_MEMORY(result_filter
);
567 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
: {
568 krb5_principal principal
;
571 ret
= smb_krb5_init_context(mem_ctx
,
572 ldb_get_event_context(sam_ctx
),
573 (struct loadparm_context
*)ldb_get_opaque(sam_ctx
, "loadparm"),
580 /* Ensure we reject compleate junk first */
581 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
, name
, &principal
);
583 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
587 domain_filter
= NULL
;
589 /* By getting the unparsed name here, we ensure the escaping is correct (and trust the client less) */
590 ret
= krb5_unparse_name(smb_krb5_context
->krb5_context
, principal
, &unparsed_name
);
592 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
596 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
598 /* The ldb_binary_encode_string() here avoid LDAP filter injection attacks */
599 result_filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(userPrincipalName=%s))",
600 ldb_binary_encode_string(mem_ctx
, unparsed_name
));
603 W_ERROR_HAVE_NO_MEMORY(result_filter
);
606 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
: {
607 krb5_principal principal
;
608 char *unparsed_name_short
;
611 ret
= smb_krb5_init_context(mem_ctx
,
612 ldb_get_event_context(sam_ctx
),
613 (struct loadparm_context
*)ldb_get_opaque(sam_ctx
, "loadparm"),
620 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
, name
, &principal
);
621 if (ret
== 0 && principal
->name
.name_string
.len
< 2) {
622 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
623 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
625 } else if (ret
== 0) {
626 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
628 ret
= krb5_parse_name_flags(smb_krb5_context
->krb5_context
, name
,
629 KRB5_PRINCIPAL_PARSE_NO_REALM
, &principal
);
631 return dns_domain_from_principal(mem_ctx
, smb_krb5_context
,
635 domain_filter
= NULL
;
637 ret
= krb5_unparse_name_flags(smb_krb5_context
->krb5_context
, principal
,
638 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &unparsed_name_short
);
640 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
644 service
= principal
->name
.name_string
.val
[0];
645 if ((principal
->name
.name_string
.len
== 2) && (strcasecmp(service
, "host") == 0)) {
646 /* the 'cn' attribute is just the leading part of the name */
648 computer_name
= talloc_strndup(mem_ctx
, principal
->name
.name_string
.val
[1],
649 strcspn(principal
->name
.name_string
.val
[1], "."));
650 if (computer_name
== NULL
) {
651 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
652 free(unparsed_name_short
);
656 result_filter
= talloc_asprintf(mem_ctx
, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))",
657 ldb_binary_encode_string(mem_ctx
, unparsed_name_short
),
658 ldb_binary_encode_string(mem_ctx
, computer_name
));
660 result_filter
= talloc_asprintf(mem_ctx
, "(&(servicePrincipalName=%s)(objectClass=user))",
661 ldb_binary_encode_string(mem_ctx
, unparsed_name_short
));
663 krb5_free_principal(smb_krb5_context
->krb5_context
, principal
);
664 free(unparsed_name_short
);
665 W_ERROR_HAVE_NO_MEMORY(result_filter
);
670 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
675 if (format_flags
& DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY
) {
676 return DsCrackNameOneSyntactical(mem_ctx
, format_offered
, format_desired
,
677 name_dn
, name
, info1
);
680 return DsCrackNameOneFilter(sam_ctx
, mem_ctx
,
682 format_flags
, format_offered
, format_desired
,
684 domain_filter
, result_filter
,
688 /* Subcase of CrackNames. It is possible to translate a LDAP-style DN
689 * (FQDN_1779) into a canoical name without actually searching the
692 static WERROR
DsCrackNameOneSyntactical(TALLOC_CTX
*mem_ctx
,
693 enum drsuapi_DsNameFormat format_offered
,
694 enum drsuapi_DsNameFormat format_desired
,
695 struct ldb_dn
*name_dn
, const char *name
,
696 struct drsuapi_DsNameInfo1
*info1
)
699 if (format_offered
!= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
) {
700 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING
;
704 switch (format_desired
) {
705 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
706 cracked
= ldb_dn_canonical_string(mem_ctx
, name_dn
);
708 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
709 cracked
= ldb_dn_canonical_ex_string(mem_ctx
, name_dn
);
712 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING
;
715 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
716 info1
->result_name
= cracked
;
724 /* Given a filter for the domain, and one for the result, perform the
725 * ldb search. The format offered and desired flags change the
726 * behaviours, including what attributes to return.
728 * The smb_krb5_context is required because we use the krb5 libs for principal parsing
731 static WERROR
DsCrackNameOneFilter(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
732 struct smb_krb5_context
*smb_krb5_context
,
733 uint32_t format_flags
, enum drsuapi_DsNameFormat format_offered
,
734 enum drsuapi_DsNameFormat format_desired
,
735 struct ldb_dn
*name_dn
, const char *name
,
736 const char *domain_filter
, const char *result_filter
,
737 struct drsuapi_DsNameInfo1
*info1
)
740 struct ldb_result
*domain_res
= NULL
;
741 const char * const *domain_attrs
;
742 const char * const *result_attrs
;
743 struct ldb_message
**result_res
= NULL
;
744 struct ldb_message
*result
= NULL
;
747 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(sam_ctx
, mem_ctx
);
749 const char * const _domain_attrs_1779
[] = { "ncName", "dnsRoot", NULL
};
750 const char * const _result_attrs_null
[] = { NULL
};
752 const char * const _domain_attrs_canonical
[] = { "ncName", "dnsRoot", NULL
};
753 const char * const _result_attrs_canonical
[] = { "canonicalName", NULL
};
755 const char * const _domain_attrs_nt4
[] = { "ncName", "dnsRoot", "nETBIOSName", NULL
};
756 const char * const _result_attrs_nt4
[] = { "sAMAccountName", "objectSid", "objectClass", NULL
};
758 const char * const _domain_attrs_guid
[] = { "ncName", "dnsRoot", NULL
};
759 const char * const _result_attrs_guid
[] = { "objectGUID", NULL
};
761 const char * const _domain_attrs_display
[] = { "ncName", "dnsRoot", NULL
};
762 const char * const _result_attrs_display
[] = { "displayName", "samAccountName", NULL
};
764 const char * const _domain_attrs_none
[] = { "ncName", "dnsRoot" , NULL
};
765 const char * const _result_attrs_none
[] = { NULL
};
767 /* here we need to set the attrs lists for domain and result lookups */
768 switch (format_desired
) {
769 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779
:
770 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
771 domain_attrs
= _domain_attrs_1779
;
772 result_attrs
= _result_attrs_null
;
774 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
775 domain_attrs
= _domain_attrs_canonical
;
776 result_attrs
= _result_attrs_canonical
;
778 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
:
779 domain_attrs
= _domain_attrs_nt4
;
780 result_attrs
= _result_attrs_nt4
;
782 case DRSUAPI_DS_NAME_FORMAT_GUID
:
783 domain_attrs
= _domain_attrs_guid
;
784 result_attrs
= _result_attrs_guid
;
786 case DRSUAPI_DS_NAME_FORMAT_DISPLAY
:
787 domain_attrs
= _domain_attrs_display
;
788 result_attrs
= _result_attrs_display
;
791 domain_attrs
= _domain_attrs_none
;
792 result_attrs
= _result_attrs_none
;
797 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
798 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
802 "%s", domain_filter
);
804 if (ldb_ret
!= LDB_SUCCESS
) {
805 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx
)));
806 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
810 switch (domain_res
->count
) {
814 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
817 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
821 info1
->dns_domain_name
= ldb_msg_find_attr_as_string(domain_res
->msgs
[0], "dnsRoot", NULL
);
822 W_ERROR_HAVE_NO_MEMORY(info1
->dns_domain_name
);
823 info1
->status
= DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
;
825 info1
->dns_domain_name
= NULL
;
826 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
831 struct ldb_result
*res
;
832 uint32_t dsdb_flags
= 0;
833 struct ldb_dn
*search_dn
;
837 search_dn
= samdb_result_dn(sam_ctx
, mem_ctx
, domain_res
->msgs
[0], "ncName", NULL
);
839 dsdb_flags
= DSDB_SEARCH_SEARCH_ALL_PARTITIONS
;
840 search_dn
= ldb_get_root_basedn(sam_ctx
);
843 /* search with the 'phantom root' flag */
844 ret
= dsdb_search(sam_ctx
, mem_ctx
, &res
,
848 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
,
849 "%s", result_filter
);
850 if (ret
!= LDB_SUCCESS
) {
851 DEBUG(2, ("DsCrackNameOneFilter phantom root search failed: %s",
852 ldb_errstring(sam_ctx
)));
853 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
857 ldb_ret
= res
->count
;
858 result_res
= res
->msgs
;
859 } else if (format_offered
== DRSUAPI_DS_NAME_FORMAT_FQDN_1779
) {
860 ldb_ret
= gendb_search_dn(sam_ctx
, mem_ctx
, name_dn
, &result_res
,
862 } else if (domain_res
) {
863 name_dn
= samdb_result_dn(sam_ctx
, mem_ctx
, domain_res
->msgs
[0], "ncName", NULL
);
864 ldb_ret
= gendb_search_dn(sam_ctx
, mem_ctx
, name_dn
, &result_res
,
868 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not available: This can't happen..."));
869 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
875 result
= result_res
[0];
878 switch (format_offered
) {
879 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
:
880 return DsCrackNameSPNAlias(sam_ctx
, mem_ctx
,
882 format_flags
, format_offered
, format_desired
,
885 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
:
886 return DsCrackNameUPN(sam_ctx
, mem_ctx
, smb_krb5_context
,
887 format_flags
, format_offered
, format_desired
,
892 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
895 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s", ldb_errstring(sam_ctx
)));
896 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
899 switch (format_offered
) {
900 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
901 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
903 const char *canonical_name
= NULL
; /* Not required, but we get warnings... */
904 /* We may need to manually filter further */
905 for (i
= 0; i
< ldb_ret
; i
++) {
906 switch (format_offered
) {
907 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
:
908 canonical_name
= ldb_dn_canonical_string(mem_ctx
, result_res
[i
]->dn
);
910 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
:
911 canonical_name
= ldb_dn_canonical_ex_string(mem_ctx
, result_res
[i
]->dn
);
916 if (strcasecmp_m(canonical_name
, name
) == 0) {
917 result
= result_res
[i
];
922 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
927 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
932 info1
->dns_domain_name
= ldb_dn_canonical_string(mem_ctx
, result
->dn
);
933 W_ERROR_HAVE_NO_MEMORY(info1
->dns_domain_name
);
934 p
= strchr(info1
->dns_domain_name
, '/');
939 /* here we can use result and domain_res[0] */
940 switch (format_desired
) {
941 case DRSUAPI_DS_NAME_FORMAT_FQDN_1779
: {
942 info1
->result_name
= ldb_dn_alloc_linearized(mem_ctx
, result
->dn
);
943 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
945 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
948 case DRSUAPI_DS_NAME_FORMAT_CANONICAL
: {
949 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "canonicalName", NULL
);
950 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
953 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
: {
954 /* Not in the virtual ldb attribute */
955 return DsCrackNameOneSyntactical(mem_ctx
,
956 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
957 DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
,
958 result
->dn
, name
, info1
);
960 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
: {
962 const struct dom_sid
*sid
= samdb_result_dom_sid(mem_ctx
, result
, "objectSid");
963 const char *_acc
= "", *_dom
= "";
965 if (samdb_find_attribute(sam_ctx
, result
, "objectClass", "domain")) {
967 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
971 "(ncName=%s)", ldb_dn_get_linearized(result
->dn
));
973 if (ldb_ret
!= LDB_SUCCESS
) {
974 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx
)));
975 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
979 switch (domain_res
->count
) {
983 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
986 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
989 _dom
= ldb_msg_find_attr_as_string(domain_res
->msgs
[0], "nETBIOSName", NULL
);
990 W_ERROR_HAVE_NO_MEMORY(_dom
);
992 _acc
= ldb_msg_find_attr_as_string(result
, "sAMAccountName", NULL
);
994 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
997 if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx
, SID_BUILTIN
), sid
)) {
1000 const char *attrs
[] = { NULL
};
1001 struct ldb_result
*domain_res2
;
1002 struct dom_sid
*dom_sid
= dom_sid_dup(mem_ctx
, sid
);
1006 dom_sid
->num_auths
--;
1007 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res
,
1011 "(&(objectSid=%s)(objectClass=domain))",
1012 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid
));
1014 if (ldb_ret
!= LDB_SUCCESS
) {
1015 DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s", ldb_errstring(sam_ctx
)));
1016 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1020 switch (domain_res
->count
) {
1024 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1027 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1031 ldb_ret
= ldb_search(sam_ctx
, mem_ctx
, &domain_res2
,
1035 "(ncName=%s)", ldb_dn_get_linearized(domain_res
->msgs
[0]->dn
));
1037 if (ldb_ret
!= LDB_SUCCESS
) {
1038 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx
)));
1039 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1043 switch (domain_res2
->count
) {
1047 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1050 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1053 _dom
= ldb_msg_find_attr_as_string(domain_res2
->msgs
[0], "nETBIOSName", NULL
);
1054 W_ERROR_HAVE_NO_MEMORY(_dom
);
1058 info1
->result_name
= talloc_asprintf(mem_ctx
, "%s\\%s", _dom
, _acc
);
1059 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
1061 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1064 case DRSUAPI_DS_NAME_FORMAT_GUID
: {
1067 guid
= samdb_result_guid(result
, "objectGUID");
1069 info1
->result_name
= GUID_string2(mem_ctx
, &guid
);
1070 W_ERROR_HAVE_NO_MEMORY(info1
->result_name
);
1072 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1075 case DRSUAPI_DS_NAME_FORMAT_DISPLAY
: {
1076 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "displayName", NULL
);
1077 if (!info1
->result_name
) {
1078 info1
->result_name
= ldb_msg_find_attr_as_string(result
, "sAMAccountName", NULL
);
1080 if (!info1
->result_name
) {
1081 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_FOUND
;
1083 info1
->status
= DRSUAPI_DS_NAME_STATUS_OK
;
1087 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
: {
1088 info1
->status
= DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
;
1091 case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN
:
1092 case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
: {
1093 info1
->status
= DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
;
1097 info1
->status
= DRSUAPI_DS_NAME_STATUS_NO_MAPPING
;
1102 /* Given a user Principal Name (such as foo@bar.com),
1103 * return the user and domain DNs. This is used in the KDC to then
1104 * return the Keys and evaluate policy */
1106 NTSTATUS
crack_user_principal_name(struct ldb_context
*sam_ctx
,
1107 TALLOC_CTX
*mem_ctx
,
1108 const char *user_principal_name
,
1109 struct ldb_dn
**user_dn
,
1110 struct ldb_dn
**domain_dn
)
1113 struct drsuapi_DsNameInfo1 info1
;
1114 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1115 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
,
1116 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1117 user_principal_name
,
1119 if (!W_ERROR_IS_OK(werr
)) {
1120 return werror_to_ntstatus(werr
);
1122 switch (info1
.status
) {
1123 case DRSUAPI_DS_NAME_STATUS_OK
:
1125 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1126 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1127 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1128 return NT_STATUS_NO_SUCH_USER
;
1129 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1131 return NT_STATUS_UNSUCCESSFUL
;
1134 *user_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1137 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1138 DRSUAPI_DS_NAME_FORMAT_CANONICAL
,
1139 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1140 talloc_asprintf(mem_ctx
, "%s/",
1141 info1
.dns_domain_name
),
1143 if (!W_ERROR_IS_OK(werr
)) {
1144 return werror_to_ntstatus(werr
);
1146 switch (info1
.status
) {
1147 case DRSUAPI_DS_NAME_STATUS_OK
:
1149 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1150 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1151 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1152 return NT_STATUS_NO_SUCH_USER
;
1153 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1155 return NT_STATUS_UNSUCCESSFUL
;
1158 *domain_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1161 return NT_STATUS_OK
;
1164 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1165 * return the user and domain DNs. This is used in the KDC to then
1166 * return the Keys and evaluate policy */
1168 NTSTATUS
crack_service_principal_name(struct ldb_context
*sam_ctx
,
1169 TALLOC_CTX
*mem_ctx
,
1170 const char *service_principal_name
,
1171 struct ldb_dn
**user_dn
,
1172 struct ldb_dn
**domain_dn
)
1175 struct drsuapi_DsNameInfo1 info1
;
1176 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1177 DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL
,
1178 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1179 service_principal_name
,
1181 if (!W_ERROR_IS_OK(werr
)) {
1182 return werror_to_ntstatus(werr
);
1184 switch (info1
.status
) {
1185 case DRSUAPI_DS_NAME_STATUS_OK
:
1187 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1188 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1189 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1190 return NT_STATUS_NO_SUCH_USER
;
1191 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1193 return NT_STATUS_UNSUCCESSFUL
;
1196 *user_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1199 werr
= DsCrackNameOneName(sam_ctx
, mem_ctx
, 0,
1200 DRSUAPI_DS_NAME_FORMAT_CANONICAL
,
1201 DRSUAPI_DS_NAME_FORMAT_FQDN_1779
,
1202 talloc_asprintf(mem_ctx
, "%s/",
1203 info1
.dns_domain_name
),
1205 if (!W_ERROR_IS_OK(werr
)) {
1206 return werror_to_ntstatus(werr
);
1208 switch (info1
.status
) {
1209 case DRSUAPI_DS_NAME_STATUS_OK
:
1211 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1212 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1213 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1214 return NT_STATUS_NO_SUCH_USER
;
1215 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1217 return NT_STATUS_UNSUCCESSFUL
;
1220 *domain_dn
= ldb_dn_new(mem_ctx
, sam_ctx
, info1
.result_name
);
1223 return NT_STATUS_OK
;
1226 NTSTATUS
crack_name_to_nt4_name(TALLOC_CTX
*mem_ctx
,
1227 struct tevent_context
*ev_ctx
,
1228 struct loadparm_context
*lp_ctx
,
1229 enum drsuapi_DsNameFormat format_offered
,
1231 const char **nt4_domain
, const char **nt4_account
)
1234 struct drsuapi_DsNameInfo1 info1
;
1235 struct ldb_context
*ldb
;
1238 /* Handle anonymous bind */
1239 if (!name
|| !*name
) {
1242 return NT_STATUS_OK
;
1245 ldb
= samdb_connect(mem_ctx
, ev_ctx
, lp_ctx
, system_session(lp_ctx
), 0);
1247 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1250 werr
= DsCrackNameOneName(ldb
, mem_ctx
, 0,
1252 DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
,
1255 if (!W_ERROR_IS_OK(werr
)) {
1256 return werror_to_ntstatus(werr
);
1258 switch (info1
.status
) {
1259 case DRSUAPI_DS_NAME_STATUS_OK
:
1261 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND
:
1262 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY
:
1263 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE
:
1264 return NT_STATUS_NO_SUCH_USER
;
1265 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR
:
1267 return NT_STATUS_UNSUCCESSFUL
;
1270 *nt4_domain
= talloc_strdup(mem_ctx
, info1
.result_name
);
1271 if (*nt4_domain
== NULL
) {
1272 return NT_STATUS_NO_MEMORY
;
1275 p
= strchr(*nt4_domain
, '\\');
1277 return NT_STATUS_INVALID_PARAMETER
;
1281 *nt4_account
= talloc_strdup(mem_ctx
, &p
[1]);
1282 if (*nt4_account
== NULL
) {
1283 return NT_STATUS_NO_MEMORY
;
1286 return NT_STATUS_OK
;
1289 NTSTATUS
crack_auto_name_to_nt4_name(TALLOC_CTX
*mem_ctx
,
1290 struct tevent_context
*ev_ctx
,
1291 struct loadparm_context
*lp_ctx
,
1293 const char **nt4_domain
,
1294 const char **nt4_account
)
1296 enum drsuapi_DsNameFormat format_offered
= DRSUAPI_DS_NAME_FORMAT_UNKNOWN
;
1298 /* Handle anonymous bind */
1299 if (!name
|| !*name
) {
1302 return NT_STATUS_OK
;
1305 if (strchr_m(name
, '=')) {
1306 format_offered
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
1307 } else if (strchr_m(name
, '@')) {
1308 format_offered
= DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL
;
1309 } else if (strchr_m(name
, '\\')) {
1310 format_offered
= DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
;
1311 } else if (strchr_m(name
, '/')) {
1312 format_offered
= DRSUAPI_DS_NAME_FORMAT_CANONICAL
;
1314 return NT_STATUS_NO_SUCH_USER
;
1317 return crack_name_to_nt4_name(mem_ctx
, ev_ctx
, lp_ctx
, format_offered
, name
, nt4_domain
, nt4_account
);
1321 WERROR
dcesrv_drsuapi_ListRoles(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
1322 const struct drsuapi_DsNameRequest1
*req1
,
1323 struct drsuapi_DsNameCtr1
**ctr1
)
1325 struct drsuapi_DsNameInfo1
*names
;
1327 uint32_t count
= 5;/*number of fsmo role owners we are going to return*/
1329 *ctr1
= talloc(mem_ctx
, struct drsuapi_DsNameCtr1
);
1330 W_ERROR_HAVE_NO_MEMORY(*ctr1
);
1331 names
= talloc_array(mem_ctx
, struct drsuapi_DsNameInfo1
, count
);
1332 W_ERROR_HAVE_NO_MEMORY(names
);
1334 for (i
= 0; i
< count
; i
++) {
1336 struct ldb_dn
*role_owner_dn
, *fsmo_role_dn
, *server_dn
;
1337 werr
= dsdb_get_fsmo_role_info(mem_ctx
, sam_ctx
, i
,
1338 &fsmo_role_dn
, &role_owner_dn
);
1339 if(!W_ERROR_IS_OK(werr
)) {
1342 server_dn
= ldb_dn_copy(mem_ctx
, role_owner_dn
);
1343 ldb_dn_remove_child_components(server_dn
, 1);
1344 names
[i
].status
= DRSUAPI_DS_NAME_STATUS_OK
;
1345 names
[i
].dns_domain_name
= samdb_dn_to_dnshostname(sam_ctx
, mem_ctx
,
1347 if(!names
[i
].dns_domain_name
) {
1348 DEBUG(4, ("list_roles: Failed to find dNSHostName for server %s",
1349 ldb_dn_get_linearized(server_dn
)));
1351 names
[i
].result_name
= talloc_strdup(mem_ctx
, ldb_dn_get_linearized(role_owner_dn
));
1354 (*ctr1
)->count
= count
;
1355 (*ctr1
)->array
= names
;
1360 WERROR
dcesrv_drsuapi_CrackNamesByNameFormat(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
1361 const struct drsuapi_DsNameRequest1
*req1
,
1362 struct drsuapi_DsNameCtr1
**ctr1
)
1364 struct drsuapi_DsNameInfo1
*names
;
1368 *ctr1
= talloc(mem_ctx
, struct drsuapi_DsNameCtr1
);
1369 W_ERROR_HAVE_NO_MEMORY(*ctr1
);
1371 count
= req1
->count
;
1372 names
= talloc_array(mem_ctx
, struct drsuapi_DsNameInfo1
, count
);
1373 W_ERROR_HAVE_NO_MEMORY(names
);
1375 for (i
=0; i
< count
; i
++) {
1376 status
= DsCrackNameOneName(sam_ctx
, mem_ctx
,
1378 req1
->format_offered
,
1379 req1
->format_desired
,
1382 if (!W_ERROR_IS_OK(status
)) {
1387 (*ctr1
)->count
= count
;
1388 (*ctr1
)->array
= names
;