2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 2004
6 Copyright (C) Volker Lendecke 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
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 "events/events.h"
27 #include "ldb_module.h"
28 #include "ldb_errors.h"
29 #include "../lib/util/util_ldb.h"
30 #include "../lib/crypto/crypto.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "libcli/security/security.h"
33 #include "librpc/gen_ndr/ndr_security.h"
34 #include "librpc/gen_ndr/ndr_misc.h"
35 #include "../libds/common/flags.h"
36 #include "dsdb/common/proto.h"
37 #include "libcli/ldap/ldap_ndr.h"
38 #include "param/param.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "librpc/gen_ndr/ndr_drsblobs.h"
41 #include "system/locale.h"
42 #include "lib/util/tsort.h"
43 #include "dsdb/common/util.h"
44 #include "lib/socket/socket.h"
45 #include "librpc/gen_ndr/irpc.h"
48 search the sam for the specified attributes in a specific domain, filter on
49 objectSid being in domain_sid.
51 int samdb_search_domain(struct ldb_context
*sam_ldb
,
53 struct ldb_dn
*basedn
,
54 struct ldb_message
***res
,
55 const char * const *attrs
,
56 const struct dom_sid
*domain_sid
,
57 const char *format
, ...) _PRINTF_ATTRIBUTE(7,8)
63 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
,
64 res
, attrs
, format
, ap
);
70 struct dom_sid
*entry_sid
;
72 entry_sid
= samdb_result_dom_sid(mem_ctx
, (*res
)[i
], "objectSid");
74 if ((entry_sid
== NULL
) ||
75 (!dom_sid_in_domain(domain_sid
, entry_sid
))) {
76 /* Delete that entry from the result set */
77 (*res
)[i
] = (*res
)[count
-1];
79 talloc_free(entry_sid
);
82 talloc_free(entry_sid
);
90 search the sam for a single string attribute in exactly 1 record
92 const char *samdb_search_string_v(struct ldb_context
*sam_ldb
,
94 struct ldb_dn
*basedn
,
95 const char *attr_name
,
96 const char *format
, va_list ap
) _PRINTF_ATTRIBUTE(5,0)
99 const char *attrs
[2] = { NULL
, NULL
};
100 struct ldb_message
**res
= NULL
;
102 attrs
[0] = attr_name
;
104 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
106 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
107 attr_name
, format
, count
));
114 return ldb_msg_find_attr_as_string(res
[0], attr_name
, NULL
);
118 search the sam for a single string attribute in exactly 1 record
120 const char *samdb_search_string(struct ldb_context
*sam_ldb
,
122 struct ldb_dn
*basedn
,
123 const char *attr_name
,
124 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
129 va_start(ap
, format
);
130 str
= samdb_search_string_v(sam_ldb
, mem_ctx
, basedn
, attr_name
, format
, ap
);
136 struct ldb_dn
*samdb_search_dn(struct ldb_context
*sam_ldb
,
138 struct ldb_dn
*basedn
,
139 const char *format
, ...) _PRINTF_ATTRIBUTE(4,5)
143 struct ldb_message
**res
= NULL
;
146 va_start(ap
, format
);
147 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, NULL
, format
, ap
);
150 if (count
!= 1) return NULL
;
152 ret
= talloc_steal(mem_ctx
, res
[0]->dn
);
159 search the sam for a dom_sid attribute in exactly 1 record
161 struct dom_sid
*samdb_search_dom_sid(struct ldb_context
*sam_ldb
,
163 struct ldb_dn
*basedn
,
164 const char *attr_name
,
165 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
169 struct ldb_message
**res
;
170 const char *attrs
[2] = { NULL
, NULL
};
173 attrs
[0] = attr_name
;
175 va_start(ap
, format
);
176 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
179 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
180 attr_name
, format
, count
));
186 sid
= samdb_result_dom_sid(mem_ctx
, res
[0], attr_name
);
192 return the count of the number of records in the sam matching the query
194 int samdb_search_count(struct ldb_context
*sam_ldb
,
196 struct ldb_dn
*basedn
,
197 const char *format
, ...) _PRINTF_ATTRIBUTE(4,5)
200 const char *attrs
[] = { NULL
};
203 va_start(ap
, format
);
204 ret
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, NULL
, attrs
, format
, ap
);
212 search the sam for a single integer attribute in exactly 1 record
214 unsigned int samdb_search_uint(struct ldb_context
*sam_ldb
,
216 unsigned int default_value
,
217 struct ldb_dn
*basedn
,
218 const char *attr_name
,
219 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
223 struct ldb_message
**res
;
224 const char *attrs
[2] = { NULL
, NULL
};
226 attrs
[0] = attr_name
;
228 va_start(ap
, format
);
229 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
233 return default_value
;
236 return ldb_msg_find_attr_as_uint(res
[0], attr_name
, default_value
);
240 search the sam for a single signed 64 bit integer attribute in exactly 1 record
242 int64_t samdb_search_int64(struct ldb_context
*sam_ldb
,
244 int64_t default_value
,
245 struct ldb_dn
*basedn
,
246 const char *attr_name
,
247 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
251 struct ldb_message
**res
;
252 const char *attrs
[2] = { NULL
, NULL
};
254 attrs
[0] = attr_name
;
256 va_start(ap
, format
);
257 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
261 return default_value
;
264 return ldb_msg_find_attr_as_int64(res
[0], attr_name
, default_value
);
268 search the sam for multipe records each giving a single string attribute
269 return the number of matches, or -1 on error
271 int samdb_search_string_multiple(struct ldb_context
*sam_ldb
,
273 struct ldb_dn
*basedn
,
275 const char *attr_name
,
276 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
280 const char *attrs
[2] = { NULL
, NULL
};
281 struct ldb_message
**res
= NULL
;
283 attrs
[0] = attr_name
;
285 va_start(ap
, format
);
286 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
293 /* make sure its single valued */
294 for (i
=0;i
<count
;i
++) {
295 if (res
[i
]->num_elements
!= 1) {
296 DEBUG(1,("samdb: search for %s %s not single valued\n",
303 *strs
= talloc_array(mem_ctx
, const char *, count
+1);
309 for (i
=0;i
<count
;i
++) {
310 (*strs
)[i
] = ldb_msg_find_attr_as_string(res
[i
], attr_name
, NULL
);
312 (*strs
)[count
] = NULL
;
317 struct ldb_dn
*samdb_result_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
318 const char *attr
, struct ldb_dn
*default_value
)
320 struct ldb_dn
*ret_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, msg
, attr
);
322 return default_value
;
328 pull a rid from a objectSid in a result set.
330 uint32_t samdb_result_rid_from_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
331 const char *attr
, uint32_t default_value
)
336 sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
338 return default_value
;
340 rid
= sid
->sub_auths
[sid
->num_auths
-1];
346 pull a dom_sid structure from a objectSid in a result set.
348 struct dom_sid
*samdb_result_dom_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
351 const struct ldb_val
*v
;
353 enum ndr_err_code ndr_err
;
354 v
= ldb_msg_find_ldb_val(msg
, attr
);
358 sid
= talloc(mem_ctx
, struct dom_sid
);
362 ndr_err
= ndr_pull_struct_blob(v
, sid
, sid
,
363 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
364 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
372 pull a guid structure from a objectGUID in a result set.
374 struct GUID
samdb_result_guid(const struct ldb_message
*msg
, const char *attr
)
376 const struct ldb_val
*v
;
380 v
= ldb_msg_find_ldb_val(msg
, attr
);
381 if (!v
) return GUID_zero();
383 status
= GUID_from_ndr_blob(v
, &guid
);
384 if (!NT_STATUS_IS_OK(status
)) {
392 pull a sid prefix from a objectSid in a result set.
393 this is used to find the domain sid for a user
395 struct dom_sid
*samdb_result_sid_prefix(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
398 struct dom_sid
*sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
399 if (!sid
|| sid
->num_auths
< 1) return NULL
;
405 pull a NTTIME in a result set.
407 NTTIME
samdb_result_nttime(const struct ldb_message
*msg
, const char *attr
,
408 NTTIME default_value
)
410 return ldb_msg_find_attr_as_uint64(msg
, attr
, default_value
);
414 * Windows stores 0 for lastLogoff.
415 * But when a MS DC return the lastLogoff (as Logoff Time)
416 * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
417 * cause windows 2008 and newer version to fail for SMB requests
419 NTTIME
samdb_result_last_logoff(const struct ldb_message
*msg
)
421 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "lastLogoff",0);
424 ret
= 0x7FFFFFFFFFFFFFFFULL
;
430 * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
431 * indicate an account doesn't expire.
433 * When Windows initially creates an account, it sets
434 * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF). However,
435 * when changing from an account having a specific expiration date to
436 * that account never expiring, it sets accountExpires = 0.
438 * Consolidate that logic here to allow clearer logic for account expiry in
439 * the rest of the code.
441 NTTIME
samdb_result_account_expires(const struct ldb_message
*msg
)
443 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "accountExpires",
447 ret
= 0x7FFFFFFFFFFFFFFFULL
;
453 construct the allow_password_change field from the PwdLastSet attribute and the
454 domain password settings
456 NTTIME
samdb_result_allow_password_change(struct ldb_context
*sam_ldb
,
458 struct ldb_dn
*domain_dn
,
459 struct ldb_message
*msg
,
462 uint64_t attr_time
= ldb_msg_find_attr_as_uint64(msg
, attr
, 0);
465 if (attr_time
== 0) {
469 minPwdAge
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
, "minPwdAge", NULL
);
471 /* yes, this is a -= not a += as minPwdAge is stored as the negative
472 of the number of 100-nano-seconds */
473 attr_time
-= minPwdAge
;
479 construct the force_password_change field from the PwdLastSet
480 attribute, the userAccountControl and the domain password settings
482 NTTIME
samdb_result_force_password_change(struct ldb_context
*sam_ldb
,
484 struct ldb_dn
*domain_dn
,
485 struct ldb_message
*msg
)
487 int64_t attr_time
= ldb_msg_find_attr_as_int64(msg
, "pwdLastSet", 0);
488 uint32_t userAccountControl
= ldb_msg_find_attr_as_uint(msg
,
489 "userAccountControl",
493 /* Machine accounts don't expire, and there is a flag for 'no expiry' */
494 if (!(userAccountControl
& UF_NORMAL_ACCOUNT
)
495 || (userAccountControl
& UF_DONT_EXPIRE_PASSWD
)) {
496 return 0x7FFFFFFFFFFFFFFFULL
;
499 if (attr_time
== 0) {
502 if (attr_time
== -1) {
503 return 0x7FFFFFFFFFFFFFFFULL
;
506 maxPwdAge
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
,
508 if (maxPwdAge
== 0) {
509 return 0x7FFFFFFFFFFFFFFFULL
;
511 attr_time
-= maxPwdAge
;
518 pull a samr_Password structutre from a result set.
520 struct samr_Password
*samdb_result_hash(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
, const char *attr
)
522 struct samr_Password
*hash
= NULL
;
523 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
524 if (val
&& (val
->length
>= sizeof(hash
->hash
))) {
525 hash
= talloc(mem_ctx
, struct samr_Password
);
526 memcpy(hash
->hash
, val
->data
, MIN(val
->length
, sizeof(hash
->hash
)));
532 pull an array of samr_Password structures from a result set.
534 unsigned int samdb_result_hashes(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
535 const char *attr
, struct samr_Password
**hashes
)
537 unsigned int count
, i
;
538 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
544 count
= val
->length
/ 16;
549 *hashes
= talloc_array(mem_ctx
, struct samr_Password
, count
);
554 for (i
=0;i
<count
;i
++) {
555 memcpy((*hashes
)[i
].hash
, (i
*16)+(char *)val
->data
, 16);
561 NTSTATUS
samdb_result_passwords(TALLOC_CTX
*mem_ctx
, struct loadparm_context
*lp_ctx
, struct ldb_message
*msg
,
562 struct samr_Password
**lm_pwd
, struct samr_Password
**nt_pwd
)
564 struct samr_Password
*lmPwdHash
, *ntPwdHash
;
567 num_nt
= samdb_result_hashes(mem_ctx
, msg
, "unicodePwd", &ntPwdHash
);
570 } else if (num_nt
> 1) {
571 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
573 *nt_pwd
= &ntPwdHash
[0];
577 /* Ensure that if we have turned off LM
578 * authentication, that we never use the LM hash, even
580 if (lpcfg_lanman_auth(lp_ctx
)) {
582 num_lm
= samdb_result_hashes(mem_ctx
, msg
, "dBCSPwd", &lmPwdHash
);
585 } else if (num_lm
> 1) {
586 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
588 *lm_pwd
= &lmPwdHash
[0];
598 pull a samr_LogonHours structutre from a result set.
600 struct samr_LogonHours
samdb_result_logon_hours(TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
, const char *attr
)
602 struct samr_LogonHours hours
;
603 size_t units_per_week
= 168;
604 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
609 units_per_week
= val
->length
* 8;
612 hours
.bits
= talloc_array(mem_ctx
, uint8_t, units_per_week
/8);
616 hours
.units_per_week
= units_per_week
;
617 memset(hours
.bits
, 0xFF, units_per_week
/8);
619 memcpy(hours
.bits
, val
->data
, val
->length
);
626 pull a set of account_flags from a result set.
628 This requires that the attributes:
633 uint32_t samdb_result_acct_flags(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
634 struct ldb_message
*msg
, struct ldb_dn
*domain_dn
)
636 uint32_t userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
637 uint32_t acct_flags
= ds_uf2acb(userAccountControl
);
638 NTTIME must_change_time
;
641 must_change_time
= samdb_result_force_password_change(sam_ctx
, mem_ctx
,
644 /* Test account expire time */
645 unix_to_nt_time(&now
, time(NULL
));
646 /* check for expired password */
647 if (must_change_time
< now
) {
648 acct_flags
|= ACB_PW_EXPIRED
;
653 struct lsa_BinaryString
samdb_result_parameters(TALLOC_CTX
*mem_ctx
,
654 struct ldb_message
*msg
,
657 struct lsa_BinaryString s
;
658 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
666 s
.array
= talloc_array(mem_ctx
, uint16_t, val
->length
/2);
670 s
.length
= s
.size
= val
->length
;
671 memcpy(s
.array
, val
->data
, val
->length
);
676 /* Find an attribute, with a particular value */
678 /* The current callers of this function expect a very specific
679 * behaviour: In particular, objectClass subclass equivilance is not
680 * wanted. This means that we should not lookup the schema for the
681 * comparison function */
682 struct ldb_message_element
*samdb_find_attribute(struct ldb_context
*ldb
,
683 const struct ldb_message
*msg
,
684 const char *name
, const char *value
)
687 struct ldb_message_element
*el
= ldb_msg_find_element(msg
, name
);
693 for (i
=0;i
<el
->num_values
;i
++) {
694 if (ldb_attr_cmp(value
, (char *)el
->values
[i
].data
) == 0) {
702 int samdb_find_or_add_value(struct ldb_context
*ldb
, struct ldb_message
*msg
, const char *name
, const char *set_value
)
704 if (samdb_find_attribute(ldb
, msg
, name
, set_value
) == NULL
) {
705 return samdb_msg_add_string(ldb
, msg
, msg
, name
, set_value
);
710 int samdb_find_or_add_attribute(struct ldb_context
*ldb
, struct ldb_message
*msg
, const char *name
, const char *set_value
)
712 struct ldb_message_element
*el
;
714 el
= ldb_msg_find_element(msg
, name
);
719 return samdb_msg_add_string(ldb
, msg
, msg
, name
, set_value
);
725 add a string element to a message
727 int samdb_msg_add_string(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
728 const char *attr_name
, const char *str
)
730 const char *s
= talloc_strdup(mem_ctx
, str
);
732 return ldb_oom(sam_ldb
);
734 return ldb_msg_add_string(msg
, attr_name
, s
);
738 add a dom_sid element to a message
740 int samdb_msg_add_dom_sid(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
741 const char *attr_name
, struct dom_sid
*sid
)
744 enum ndr_err_code ndr_err
;
746 ndr_err
= ndr_push_struct_blob(&v
, mem_ctx
,
748 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
749 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
750 return ldb_operr(sam_ldb
);
752 return ldb_msg_add_value(msg
, attr_name
, &v
, NULL
);
757 add a delete element operation to a message
759 int samdb_msg_add_delete(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
760 const char *attr_name
)
762 /* we use an empty replace rather than a delete, as it allows for
763 dsdb_replace() to be used everywhere */
764 return ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_REPLACE
, NULL
);
768 add an add attribute value to a message or enhance an existing attribute
769 which has the same name and the add flag set.
771 int samdb_msg_add_addval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
772 struct ldb_message
*msg
, const char *attr_name
,
775 struct ldb_message_element
*el
;
776 struct ldb_val val
, *vals
;
782 v
= talloc_strdup(mem_ctx
, value
);
784 return ldb_oom(sam_ldb
);
787 val
.data
= (uint8_t *) v
;
788 val
.length
= strlen(v
);
790 if (val
.length
== 0) {
791 /* allow empty strings as non-existent attributes */
795 for (i
= 0; i
< msg
->num_elements
; i
++) {
796 el
= &msg
->elements
[i
];
797 if ((ldb_attr_cmp(el
->name
, attr_name
) == 0) &&
798 (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_ADD
)) {
804 ret
= ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_ADD
,
806 if (ret
!= LDB_SUCCESS
) {
811 vals
= talloc_realloc(msg
, el
->values
, struct ldb_val
,
814 return ldb_oom(sam_ldb
);
817 el
->values
[el
->num_values
] = val
;
824 add a delete attribute value to a message or enhance an existing attribute
825 which has the same name and the delete flag set.
827 int samdb_msg_add_delval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
828 struct ldb_message
*msg
, const char *attr_name
,
831 struct ldb_message_element
*el
;
832 struct ldb_val val
, *vals
;
838 v
= talloc_strdup(mem_ctx
, value
);
840 return ldb_oom(sam_ldb
);
843 val
.data
= (uint8_t *) v
;
844 val
.length
= strlen(v
);
846 if (val
.length
== 0) {
847 /* allow empty strings as non-existent attributes */
851 for (i
= 0; i
< msg
->num_elements
; i
++) {
852 el
= &msg
->elements
[i
];
853 if ((ldb_attr_cmp(el
->name
, attr_name
) == 0) &&
854 (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
)) {
860 ret
= ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_DELETE
,
862 if (ret
!= LDB_SUCCESS
) {
867 vals
= talloc_realloc(msg
, el
->values
, struct ldb_val
,
870 return ldb_oom(sam_ldb
);
873 el
->values
[el
->num_values
] = val
;
880 add a int element to a message
882 int samdb_msg_add_int(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
883 const char *attr_name
, int v
)
885 const char *s
= talloc_asprintf(mem_ctx
, "%d", v
);
887 return ldb_oom(sam_ldb
);
889 return ldb_msg_add_string(msg
, attr_name
, s
);
893 * Add an unsigned int element to a message
895 * The issue here is that we have not yet first cast to int32_t explicitly,
896 * before we cast to an signed int to printf() into the %d or cast to a
897 * int64_t before we then cast to a long long to printf into a %lld.
899 * There are *no* unsigned integers in Active Directory LDAP, even the RID
900 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
901 * (See the schema, and the syntax definitions in schema_syntax.c).
904 int samdb_msg_add_uint(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
905 const char *attr_name
, unsigned int v
)
907 return samdb_msg_add_int(sam_ldb
, mem_ctx
, msg
, attr_name
, (int)v
);
911 add a (signed) int64_t element to a message
913 int samdb_msg_add_int64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
914 const char *attr_name
, int64_t v
)
916 const char *s
= talloc_asprintf(mem_ctx
, "%lld", (long long)v
);
918 return ldb_oom(sam_ldb
);
920 return ldb_msg_add_string(msg
, attr_name
, s
);
924 * Add an unsigned int64_t (uint64_t) element to a message
926 * The issue here is that we have not yet first cast to int32_t explicitly,
927 * before we cast to an signed int to printf() into the %d or cast to a
928 * int64_t before we then cast to a long long to printf into a %lld.
930 * There are *no* unsigned integers in Active Directory LDAP, even the RID
931 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
932 * (See the schema, and the syntax definitions in schema_syntax.c).
935 int samdb_msg_add_uint64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
936 const char *attr_name
, uint64_t v
)
938 return samdb_msg_add_int64(sam_ldb
, mem_ctx
, msg
, attr_name
, (int64_t)v
);
942 add a samr_Password element to a message
944 int samdb_msg_add_hash(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
945 const char *attr_name
, const struct samr_Password
*hash
)
948 val
.data
= talloc_memdup(mem_ctx
, hash
->hash
, 16);
950 return ldb_oom(sam_ldb
);
953 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
957 add a samr_Password array to a message
959 int samdb_msg_add_hashes(struct ldb_context
*ldb
,
960 TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
961 const char *attr_name
, struct samr_Password
*hashes
,
966 val
.data
= talloc_array_size(mem_ctx
, 16, count
);
967 val
.length
= count
*16;
971 for (i
=0;i
<count
;i
++) {
972 memcpy(i
*16 + (char *)val
.data
, hashes
[i
].hash
, 16);
974 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
978 add a acct_flags element to a message
980 int samdb_msg_add_acct_flags(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
981 const char *attr_name
, uint32_t v
)
983 return samdb_msg_add_uint(sam_ldb
, mem_ctx
, msg
, attr_name
, ds_acb2uf(v
));
987 add a logon_hours element to a message
989 int samdb_msg_add_logon_hours(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
990 const char *attr_name
, struct samr_LogonHours
*hours
)
993 val
.length
= hours
->units_per_week
/ 8;
994 val
.data
= hours
->bits
;
995 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
999 add a parameters element to a message
1001 int samdb_msg_add_parameters(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1002 const char *attr_name
, struct lsa_BinaryString
*parameters
)
1005 val
.length
= parameters
->length
;
1006 val
.data
= (uint8_t *)parameters
->array
;
1007 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
1011 sets a general value element to a message
1013 int samdb_msg_set_value(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1014 const char *attr_name
, const struct ldb_val
*val
)
1016 struct ldb_message_element
*el
;
1018 el
= ldb_msg_find_element(msg
, attr_name
);
1022 return ldb_msg_add_value(msg
, attr_name
, val
, NULL
);
1026 set a string element in a message
1028 int samdb_msg_set_string(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1029 const char *attr_name
, const char *str
)
1031 struct ldb_message_element
*el
;
1033 el
= ldb_msg_find_element(msg
, attr_name
);
1037 return samdb_msg_add_string(sam_ldb
, mem_ctx
, msg
, attr_name
, str
);
1041 * sets a signed integer in a message
1043 int samdb_msg_set_int(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
1044 struct ldb_message
*msg
, const char *attr_name
, int v
)
1046 struct ldb_message_element
*el
;
1048 el
= ldb_msg_find_element(msg
, attr_name
);
1052 return samdb_msg_add_int(sam_ldb
, mem_ctx
, msg
, attr_name
, v
);
1056 * Sets an unsigned int element in a message
1058 * The issue here is that we have not yet first cast to int32_t explicitly,
1059 * before we cast to an signed int to printf() into the %d or cast to a
1060 * int64_t before we then cast to a long long to printf into a %lld.
1062 * There are *no* unsigned integers in Active Directory LDAP, even the RID
1063 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
1064 * (See the schema, and the syntax definitions in schema_syntax.c).
1067 int samdb_msg_set_uint(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
1068 struct ldb_message
*msg
, const char *attr_name
,
1071 struct ldb_message_element
*el
;
1073 el
= ldb_msg_find_element(msg
, attr_name
);
1077 return samdb_msg_add_uint(sam_ldb
, mem_ctx
, msg
, attr_name
, v
);
1081 * Handle ldb_request in transaction
1083 static int dsdb_autotransaction_request(struct ldb_context
*sam_ldb
,
1084 struct ldb_request
*req
)
1088 ret
= ldb_transaction_start(sam_ldb
);
1089 if (ret
!= LDB_SUCCESS
) {
1093 ret
= ldb_request(sam_ldb
, req
);
1094 if (ret
== LDB_SUCCESS
) {
1095 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1098 if (ret
== LDB_SUCCESS
) {
1099 return ldb_transaction_commit(sam_ldb
);
1101 ldb_transaction_cancel(sam_ldb
);
1107 return a default security descriptor
1109 struct security_descriptor
*samdb_default_security_descriptor(TALLOC_CTX
*mem_ctx
)
1111 struct security_descriptor
*sd
;
1113 sd
= security_descriptor_initialise(mem_ctx
);
1118 struct ldb_dn
*samdb_aggregate_schema_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1120 struct ldb_dn
*schema_dn
= ldb_get_schema_basedn(sam_ctx
);
1121 struct ldb_dn
*aggregate_dn
;
1126 aggregate_dn
= ldb_dn_copy(mem_ctx
, schema_dn
);
1127 if (!aggregate_dn
) {
1130 if (!ldb_dn_add_child_fmt(aggregate_dn
, "CN=Aggregate")) {
1133 return aggregate_dn
;
1136 struct ldb_dn
*samdb_partitions_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1138 struct ldb_dn
*new_dn
;
1140 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_config_basedn(sam_ctx
));
1141 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Partitions")) {
1142 talloc_free(new_dn
);
1148 struct ldb_dn
*samdb_infrastructure_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1150 struct ldb_dn
*new_dn
;
1152 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_default_basedn(sam_ctx
));
1153 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Infrastructure")) {
1154 talloc_free(new_dn
);
1160 struct ldb_dn
*samdb_sites_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1162 struct ldb_dn
*new_dn
;
1164 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_config_basedn(sam_ctx
));
1165 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Sites")) {
1166 talloc_free(new_dn
);
1173 work out the domain sid for the current open ldb
1175 const struct dom_sid
*samdb_domain_sid(struct ldb_context
*ldb
)
1177 TALLOC_CTX
*tmp_ctx
;
1178 const struct dom_sid
*domain_sid
;
1179 const char *attrs
[] = {
1183 struct ldb_result
*res
;
1186 /* see if we have a cached copy */
1187 domain_sid
= (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1192 tmp_ctx
= talloc_new(ldb
);
1193 if (tmp_ctx
== NULL
) {
1197 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_get_default_basedn(ldb
), LDB_SCOPE_BASE
, attrs
, "objectSid=*");
1199 if (ret
!= LDB_SUCCESS
) {
1203 if (res
->count
!= 1) {
1207 domain_sid
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSid");
1208 if (domain_sid
== NULL
) {
1212 /* cache the domain_sid in the ldb */
1213 if (ldb_set_opaque(ldb
, "cache.domain_sid", discard_const_p(struct dom_sid
, domain_sid
)) != LDB_SUCCESS
) {
1217 talloc_steal(ldb
, domain_sid
);
1218 talloc_free(tmp_ctx
);
1223 talloc_free(tmp_ctx
);
1228 get domain sid from cache
1230 const struct dom_sid
*samdb_domain_sid_cache_only(struct ldb_context
*ldb
)
1232 return (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1235 bool samdb_set_domain_sid(struct ldb_context
*ldb
, const struct dom_sid
*dom_sid_in
)
1237 TALLOC_CTX
*tmp_ctx
;
1238 struct dom_sid
*dom_sid_new
;
1239 struct dom_sid
*dom_sid_old
;
1241 /* see if we have a cached copy */
1242 dom_sid_old
= talloc_get_type(ldb_get_opaque(ldb
,
1243 "cache.domain_sid"), struct dom_sid
);
1245 tmp_ctx
= talloc_new(ldb
);
1246 if (tmp_ctx
== NULL
) {
1250 dom_sid_new
= dom_sid_dup(tmp_ctx
, dom_sid_in
);
1255 /* cache the domain_sid in the ldb */
1256 if (ldb_set_opaque(ldb
, "cache.domain_sid", dom_sid_new
) != LDB_SUCCESS
) {
1260 talloc_steal(ldb
, dom_sid_new
);
1261 talloc_free(tmp_ctx
);
1262 talloc_free(dom_sid_old
);
1267 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1268 talloc_free(tmp_ctx
);
1272 bool samdb_set_ntds_settings_dn(struct ldb_context
*ldb
, struct ldb_dn
*ntds_settings_dn_in
)
1274 TALLOC_CTX
*tmp_ctx
;
1275 struct ldb_dn
*ntds_settings_dn_new
;
1276 struct ldb_dn
*ntds_settings_dn_old
;
1278 /* see if we have a cached copy */
1279 ntds_settings_dn_old
= talloc_get_type(ldb_get_opaque(ldb
,
1280 "cache.ntds_settings_dn"), struct ldb_dn
);
1282 tmp_ctx
= talloc_new(ldb
);
1283 if (tmp_ctx
== NULL
) {
1287 ntds_settings_dn_new
= ldb_dn_copy(tmp_ctx
, ntds_settings_dn_in
);
1288 if (!ntds_settings_dn_new
) {
1292 /* cache the domain_sid in the ldb */
1293 if (ldb_set_opaque(ldb
, "cache.ntds_settings_dn", ntds_settings_dn_new
) != LDB_SUCCESS
) {
1297 talloc_steal(ldb
, ntds_settings_dn_new
);
1298 talloc_free(tmp_ctx
);
1299 talloc_free(ntds_settings_dn_old
);
1304 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1305 talloc_free(tmp_ctx
);
1310 work out the ntds settings dn for the current open ldb
1312 struct ldb_dn
*samdb_ntds_settings_dn(struct ldb_context
*ldb
)
1314 TALLOC_CTX
*tmp_ctx
;
1315 const char *root_attrs
[] = { "dsServiceName", NULL
};
1317 struct ldb_result
*root_res
;
1318 struct ldb_dn
*settings_dn
;
1320 /* see if we have a cached copy */
1321 settings_dn
= (struct ldb_dn
*)ldb_get_opaque(ldb
, "cache.ntds_settings_dn");
1326 tmp_ctx
= talloc_new(ldb
);
1327 if (tmp_ctx
== NULL
) {
1331 ret
= ldb_search(ldb
, tmp_ctx
, &root_res
, ldb_dn_new(tmp_ctx
, ldb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
1333 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1334 ldb_errstring(ldb
)));
1338 if (root_res
->count
!= 1) {
1342 settings_dn
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, root_res
->msgs
[0], "dsServiceName");
1344 /* cache the domain_sid in the ldb */
1345 if (ldb_set_opaque(ldb
, "cache.ntds_settings_dn", settings_dn
) != LDB_SUCCESS
) {
1349 talloc_steal(ldb
, settings_dn
);
1350 talloc_free(tmp_ctx
);
1355 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1356 talloc_free(tmp_ctx
);
1361 work out the ntds settings invocationId for the current open ldb
1363 const struct GUID
*samdb_ntds_invocation_id(struct ldb_context
*ldb
)
1365 TALLOC_CTX
*tmp_ctx
;
1366 const char *attrs
[] = { "invocationId", NULL
};
1368 struct ldb_result
*res
;
1369 struct GUID
*invocation_id
;
1371 /* see if we have a cached copy */
1372 invocation_id
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.invocation_id");
1373 if (invocation_id
) {
1374 return invocation_id
;
1377 tmp_ctx
= talloc_new(ldb
);
1378 if (tmp_ctx
== NULL
) {
1382 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
1387 if (res
->count
!= 1) {
1391 invocation_id
= talloc(tmp_ctx
, struct GUID
);
1392 if (!invocation_id
) {
1396 *invocation_id
= samdb_result_guid(res
->msgs
[0], "invocationId");
1398 /* cache the domain_sid in the ldb */
1399 if (ldb_set_opaque(ldb
, "cache.invocation_id", invocation_id
) != LDB_SUCCESS
) {
1403 talloc_steal(ldb
, invocation_id
);
1404 talloc_free(tmp_ctx
);
1406 return invocation_id
;
1409 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1410 talloc_free(tmp_ctx
);
1414 bool samdb_set_ntds_invocation_id(struct ldb_context
*ldb
, const struct GUID
*invocation_id_in
)
1416 TALLOC_CTX
*tmp_ctx
;
1417 struct GUID
*invocation_id_new
;
1418 struct GUID
*invocation_id_old
;
1420 /* see if we have a cached copy */
1421 invocation_id_old
= (struct GUID
*)ldb_get_opaque(ldb
,
1422 "cache.invocation_id");
1424 tmp_ctx
= talloc_new(ldb
);
1425 if (tmp_ctx
== NULL
) {
1429 invocation_id_new
= talloc(tmp_ctx
, struct GUID
);
1430 if (!invocation_id_new
) {
1434 *invocation_id_new
= *invocation_id_in
;
1436 /* cache the domain_sid in the ldb */
1437 if (ldb_set_opaque(ldb
, "cache.invocation_id", invocation_id_new
) != LDB_SUCCESS
) {
1441 talloc_steal(ldb
, invocation_id_new
);
1442 talloc_free(tmp_ctx
);
1443 talloc_free(invocation_id_old
);
1448 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1449 talloc_free(tmp_ctx
);
1454 work out the ntds settings objectGUID for the current open ldb
1456 const struct GUID
*samdb_ntds_objectGUID(struct ldb_context
*ldb
)
1458 TALLOC_CTX
*tmp_ctx
;
1459 const char *attrs
[] = { "objectGUID", NULL
};
1461 struct ldb_result
*res
;
1462 struct GUID
*ntds_guid
;
1464 /* see if we have a cached copy */
1465 ntds_guid
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.ntds_guid");
1470 tmp_ctx
= talloc_new(ldb
);
1471 if (tmp_ctx
== NULL
) {
1475 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
1480 if (res
->count
!= 1) {
1484 ntds_guid
= talloc(tmp_ctx
, struct GUID
);
1489 *ntds_guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
1491 /* cache the domain_sid in the ldb */
1492 if (ldb_set_opaque(ldb
, "cache.ntds_guid", ntds_guid
) != LDB_SUCCESS
) {
1496 talloc_steal(ldb
, ntds_guid
);
1497 talloc_free(tmp_ctx
);
1502 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1503 talloc_free(tmp_ctx
);
1507 bool samdb_set_ntds_objectGUID(struct ldb_context
*ldb
, const struct GUID
*ntds_guid_in
)
1509 TALLOC_CTX
*tmp_ctx
;
1510 struct GUID
*ntds_guid_new
;
1511 struct GUID
*ntds_guid_old
;
1513 /* see if we have a cached copy */
1514 ntds_guid_old
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.ntds_guid");
1516 tmp_ctx
= talloc_new(ldb
);
1517 if (tmp_ctx
== NULL
) {
1521 ntds_guid_new
= talloc(tmp_ctx
, struct GUID
);
1522 if (!ntds_guid_new
) {
1526 *ntds_guid_new
= *ntds_guid_in
;
1528 /* cache the domain_sid in the ldb */
1529 if (ldb_set_opaque(ldb
, "cache.ntds_guid", ntds_guid_new
) != LDB_SUCCESS
) {
1533 talloc_steal(ldb
, ntds_guid_new
);
1534 talloc_free(tmp_ctx
);
1535 talloc_free(ntds_guid_old
);
1540 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1541 talloc_free(tmp_ctx
);
1546 work out the server dn for the current open ldb
1548 struct ldb_dn
*samdb_server_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1550 return ldb_dn_get_parent(mem_ctx
, samdb_ntds_settings_dn(ldb
));
1554 work out the server dn for the current open ldb
1556 struct ldb_dn
*samdb_server_site_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1558 struct ldb_dn
*server_dn
;
1559 struct ldb_dn
*servers_dn
;
1560 struct ldb_dn
*server_site_dn
;
1562 /* TODO: there must be a saner way to do this!! */
1563 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1564 if (!server_dn
) return NULL
;
1566 servers_dn
= ldb_dn_get_parent(mem_ctx
, server_dn
);
1567 talloc_free(server_dn
);
1568 if (!servers_dn
) return NULL
;
1570 server_site_dn
= ldb_dn_get_parent(mem_ctx
, servers_dn
);
1571 talloc_free(servers_dn
);
1573 return server_site_dn
;
1577 find the site name from a computers DN record
1579 int samdb_find_site_for_computer(struct ldb_context
*ldb
,
1580 TALLOC_CTX
*mem_ctx
, struct ldb_dn
*computer_dn
,
1581 const char **site_name
)
1585 const struct ldb_val
*rdn_val
;
1589 ret
= samdb_reference_dn(ldb
, mem_ctx
, computer_dn
, "serverReferenceBL", &dn
);
1590 if (ret
!= LDB_SUCCESS
) {
1594 if (!ldb_dn_remove_child_components(dn
, 2)) {
1596 return LDB_ERR_INVALID_DN_SYNTAX
;
1599 rdn_val
= ldb_dn_get_rdn_val(dn
);
1600 if (rdn_val
== NULL
) {
1601 return LDB_ERR_OPERATIONS_ERROR
;
1604 (*site_name
) = talloc_strndup(mem_ctx
, (const char *)rdn_val
->data
, rdn_val
->length
);
1607 return LDB_ERR_OPERATIONS_ERROR
;
1613 find the NTDS GUID from a computers DN record
1615 int samdb_find_ntdsguid_for_computer(struct ldb_context
*ldb
, struct ldb_dn
*computer_dn
,
1616 struct GUID
*ntds_guid
)
1621 *ntds_guid
= GUID_zero();
1623 ret
= samdb_reference_dn(ldb
, ldb
, computer_dn
, "serverReferenceBL", &dn
);
1624 if (ret
!= LDB_SUCCESS
) {
1628 if (!ldb_dn_add_child_fmt(dn
, "CN=NTDS Settings")) {
1630 return LDB_ERR_OPERATIONS_ERROR
;
1633 ret
= dsdb_find_guid_by_dn(ldb
, dn
, ntds_guid
);
1639 find a 'reference' DN that points at another object
1640 (eg. serverReference, rIDManagerReference etc)
1642 int samdb_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*base
,
1643 const char *attribute
, struct ldb_dn
**dn
)
1645 const char *attrs
[2];
1646 struct ldb_result
*res
;
1649 attrs
[0] = attribute
;
1652 ret
= dsdb_search(ldb
, mem_ctx
, &res
, base
, LDB_SCOPE_BASE
, attrs
, DSDB_SEARCH_ONE_ONLY
, NULL
);
1653 if (ret
!= LDB_SUCCESS
) {
1657 *dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, res
->msgs
[0], attribute
);
1659 if (!ldb_msg_find_element(res
->msgs
[0], attribute
)) {
1660 ldb_asprintf_errstring(ldb
, "Cannot find attribute %s of %s to calculate reference dn", attribute
,
1661 ldb_dn_get_linearized(base
));
1663 ldb_asprintf_errstring(ldb
, "Cannot interpret attribute %s of %s as a dn", attribute
,
1664 ldb_dn_get_linearized(base
));
1667 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
1675 find our machine account via the serverReference attribute in the
1678 int samdb_server_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1680 struct ldb_dn
*server_dn
;
1683 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1684 if (server_dn
== NULL
) {
1685 return LDB_ERR_NO_SUCH_OBJECT
;
1688 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_dn
, "serverReference", dn
);
1689 talloc_free(server_dn
);
1695 find the RID Manager$ DN via the rIDManagerReference attribute in the
1698 int samdb_rid_manager_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1700 return samdb_reference_dn(ldb
, mem_ctx
, ldb_get_default_basedn(ldb
),
1701 "rIDManagerReference", dn
);
1705 find the RID Set DN via the rIDSetReferences attribute in our
1708 int samdb_rid_set_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1710 struct ldb_dn
*server_ref_dn
;
1713 ret
= samdb_server_reference_dn(ldb
, mem_ctx
, &server_ref_dn
);
1714 if (ret
!= LDB_SUCCESS
) {
1717 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_ref_dn
, "rIDSetReferences", dn
);
1718 talloc_free(server_ref_dn
);
1722 const char *samdb_server_site_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1724 const struct ldb_val
*val
= ldb_dn_get_rdn_val(samdb_server_site_dn(ldb
,
1731 return (const char *) val
->data
;
1735 * Finds the client site by using the client's IP address.
1736 * The "subnet_name" returns the name of the subnet if parameter != NULL
1738 const char *samdb_client_site_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1739 const char *ip_address
, char **subnet_name
)
1741 const char *attrs
[] = { "cn", "siteObject", NULL
};
1742 struct ldb_dn
*sites_container_dn
, *subnets_dn
, *sites_dn
;
1743 struct ldb_result
*res
;
1744 const struct ldb_val
*val
;
1745 const char *site_name
= NULL
, *l_subnet_name
= NULL
;
1746 const char *allow_list
[2] = { NULL
, NULL
};
1747 unsigned int i
, count
;
1751 * if we don't have a client ip e.g. ncalrpc
1752 * the server site is the client site
1754 if (ip_address
== NULL
) {
1755 return samdb_server_site_name(ldb
, mem_ctx
);
1758 sites_container_dn
= samdb_sites_dn(ldb
, mem_ctx
);
1759 if (sites_container_dn
== NULL
) {
1763 subnets_dn
= ldb_dn_copy(mem_ctx
, sites_container_dn
);
1764 if ( ! ldb_dn_add_child_fmt(subnets_dn
, "CN=Subnets")) {
1765 talloc_free(sites_container_dn
);
1766 talloc_free(subnets_dn
);
1770 ret
= ldb_search(ldb
, mem_ctx
, &res
, subnets_dn
, LDB_SCOPE_ONELEVEL
,
1772 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1774 } else if (ret
!= LDB_SUCCESS
) {
1775 talloc_free(sites_container_dn
);
1776 talloc_free(subnets_dn
);
1782 for (i
= 0; i
< count
; i
++) {
1783 l_subnet_name
= ldb_msg_find_attr_as_string(res
->msgs
[i
], "cn",
1786 allow_list
[0] = l_subnet_name
;
1788 if (allow_access(mem_ctx
, NULL
, allow_list
, "", ip_address
)) {
1789 sites_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
,
1792 if (sites_dn
== NULL
) {
1793 /* No reference, maybe another subnet matches */
1797 /* "val" cannot be NULL here since "sites_dn" != NULL */
1798 val
= ldb_dn_get_rdn_val(sites_dn
);
1799 site_name
= talloc_strdup(mem_ctx
,
1800 (const char *) val
->data
);
1802 talloc_free(sites_dn
);
1808 if (site_name
== NULL
) {
1809 /* This is the Windows Server fallback rule: when no subnet
1810 * exists and we have only one site available then use it (it
1811 * is for sure the same as our server site). If more sites do
1812 * exist then we don't know which one to use and set the site
1814 cnt
= samdb_search_count(ldb
, mem_ctx
, sites_container_dn
,
1815 "(objectClass=site)");
1817 site_name
= samdb_server_site_name(ldb
, mem_ctx
);
1819 site_name
= talloc_strdup(mem_ctx
, "");
1821 l_subnet_name
= NULL
;
1824 if (subnet_name
!= NULL
) {
1825 *subnet_name
= talloc_strdup(mem_ctx
, l_subnet_name
);
1828 talloc_free(sites_container_dn
);
1829 talloc_free(subnets_dn
);
1836 work out if we are the PDC for the domain of the current open ldb
1838 bool samdb_is_pdc(struct ldb_context
*ldb
)
1840 const char *dom_attrs
[] = { "fSMORoleOwner", NULL
};
1842 struct ldb_result
*dom_res
;
1843 TALLOC_CTX
*tmp_ctx
;
1847 tmp_ctx
= talloc_new(ldb
);
1848 if (tmp_ctx
== NULL
) {
1849 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1853 ret
= ldb_search(ldb
, tmp_ctx
, &dom_res
, ldb_get_default_basedn(ldb
), LDB_SCOPE_BASE
, dom_attrs
, NULL
);
1854 if (ret
!= LDB_SUCCESS
) {
1855 DEBUG(1,("Searching for fSMORoleOwner in %s failed: %s\n",
1856 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)),
1857 ldb_errstring(ldb
)));
1860 if (dom_res
->count
!= 1) {
1864 pdc
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, dom_res
->msgs
[0], "fSMORoleOwner");
1866 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb
), pdc
) == 0) {
1872 talloc_free(tmp_ctx
);
1877 DEBUG(1,("Failed to find if we are the PDC for this ldb\n"));
1878 talloc_free(tmp_ctx
);
1883 work out if we are a Global Catalog server for the domain of the current open ldb
1885 bool samdb_is_gc(struct ldb_context
*ldb
)
1887 const char *attrs
[] = { "options", NULL
};
1889 struct ldb_result
*res
;
1890 TALLOC_CTX
*tmp_ctx
;
1892 tmp_ctx
= talloc_new(ldb
);
1893 if (tmp_ctx
== NULL
) {
1894 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1898 /* Query cn=ntds settings,.... */
1899 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
1900 if (ret
!= LDB_SUCCESS
) {
1901 talloc_free(tmp_ctx
);
1904 if (res
->count
!= 1) {
1905 talloc_free(tmp_ctx
);
1909 options
= ldb_msg_find_attr_as_int(res
->msgs
[0], "options", 0);
1910 talloc_free(tmp_ctx
);
1912 /* if options attribute has the 0x00000001 flag set, then enable the global catlog */
1913 if (options
& 0x000000001) {
1919 /* Find a domain object in the parents of a particular DN. */
1920 int samdb_search_for_parent_domain(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
1921 struct ldb_dn
**parent_dn
, const char **errstring
)
1923 TALLOC_CTX
*local_ctx
;
1924 struct ldb_dn
*sdn
= dn
;
1925 struct ldb_result
*res
= NULL
;
1926 int ret
= LDB_SUCCESS
;
1927 const char *attrs
[] = { NULL
};
1929 local_ctx
= talloc_new(mem_ctx
);
1930 if (local_ctx
== NULL
) return ldb_oom(ldb
);
1932 while ((sdn
= ldb_dn_get_parent(local_ctx
, sdn
))) {
1933 ret
= ldb_search(ldb
, local_ctx
, &res
, sdn
, LDB_SCOPE_BASE
, attrs
,
1934 "(|(objectClass=domain)(objectClass=builtinDomain))");
1935 if (ret
== LDB_SUCCESS
) {
1936 if (res
->count
== 1) {
1944 if (ret
!= LDB_SUCCESS
) {
1945 *errstring
= talloc_asprintf(mem_ctx
, "Error searching for parent domain of %s, failed searching for %s: %s",
1946 ldb_dn_get_linearized(dn
),
1947 ldb_dn_get_linearized(sdn
),
1948 ldb_errstring(ldb
));
1949 talloc_free(local_ctx
);
1952 if (res
->count
!= 1) {
1953 *errstring
= talloc_asprintf(mem_ctx
, "Invalid dn (%s), not child of a domain object",
1954 ldb_dn_get_linearized(dn
));
1955 DEBUG(0,(__location__
": %s\n", *errstring
));
1956 talloc_free(local_ctx
);
1957 return LDB_ERR_CONSTRAINT_VIOLATION
;
1960 *parent_dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
1961 talloc_free(local_ctx
);
1967 * Performs checks on a user password (plaintext UNIX format - attribute
1968 * "password"). The remaining parameters have to be extracted from the domain
1971 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1973 enum samr_ValidationStatus
samdb_check_password(const DATA_BLOB
*password
,
1974 const uint32_t pwdProperties
,
1975 const uint32_t minPwdLength
)
1977 /* checks if the "minPwdLength" property is satisfied */
1978 if (minPwdLength
> password
->length
)
1979 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT
;
1981 /* checks the password complexity */
1982 if (((pwdProperties
& DOMAIN_PASSWORD_COMPLEX
) != 0)
1983 && (password
->data
!= NULL
)
1984 && (!check_password_quality((const char *) password
->data
)))
1985 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH
;
1987 return SAMR_VALIDATION_STATUS_SUCCESS
;
1991 * Callback for "samdb_set_password" password change
1993 int samdb_set_password_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1998 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
2001 if (ares
->error
!= LDB_SUCCESS
) {
2003 req
->context
= talloc_steal(req
,
2004 ldb_reply_get_control(ares
, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
));
2006 return ldb_request_done(req
, ret
);
2009 if (ares
->type
!= LDB_REPLY_DONE
) {
2011 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
2014 req
->context
= talloc_steal(req
,
2015 ldb_reply_get_control(ares
, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
));
2017 return ldb_request_done(req
, LDB_SUCCESS
);
2021 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2022 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2023 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2024 * user change or not. The "rejectReason" gives some more informations if the
2027 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2028 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2030 NTSTATUS
samdb_set_password(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2031 struct ldb_dn
*user_dn
, struct ldb_dn
*domain_dn
,
2032 const DATA_BLOB
*new_password
,
2033 const struct samr_Password
*lmNewHash
,
2034 const struct samr_Password
*ntNewHash
,
2035 const struct samr_Password
*lmOldHash
,
2036 const struct samr_Password
*ntOldHash
,
2037 enum samPwdChangeReason
*reject_reason
,
2038 struct samr_DomInfo1
**_dominfo
)
2040 struct ldb_message
*msg
;
2041 struct ldb_message_element
*el
;
2042 struct ldb_request
*req
;
2043 struct dsdb_control_password_change_status
*pwd_stat
= NULL
;
2045 NTSTATUS status
= NT_STATUS_OK
;
2047 #define CHECK_RET(x) \
2048 if (x != LDB_SUCCESS) { \
2050 return NT_STATUS_NO_MEMORY; \
2053 msg
= ldb_msg_new(mem_ctx
);
2055 return NT_STATUS_NO_MEMORY
;
2058 if ((new_password
!= NULL
)
2059 && ((lmNewHash
== NULL
) && (ntNewHash
== NULL
))) {
2060 /* we have the password as plaintext UTF16 */
2061 CHECK_RET(ldb_msg_add_value(msg
, "clearTextPassword",
2062 new_password
, NULL
));
2063 el
= ldb_msg_find_element(msg
, "clearTextPassword");
2064 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2065 } else if ((new_password
== NULL
)
2066 && ((lmNewHash
!= NULL
) || (ntNewHash
!= NULL
))) {
2067 /* we have a password as LM and/or NT hash */
2068 if (lmNewHash
!= NULL
) {
2069 CHECK_RET(samdb_msg_add_hash(ldb
, mem_ctx
, msg
,
2070 "dBCSPwd", lmNewHash
));
2071 el
= ldb_msg_find_element(msg
, "dBCSPwd");
2072 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2074 if (ntNewHash
!= NULL
) {
2075 CHECK_RET(samdb_msg_add_hash(ldb
, mem_ctx
, msg
,
2076 "unicodePwd", ntNewHash
));
2077 el
= ldb_msg_find_element(msg
, "unicodePwd");
2078 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2081 /* the password wasn't specified correctly */
2083 return NT_STATUS_INVALID_PARAMETER
;
2086 /* build modify request */
2087 ret
= ldb_build_mod_req(&req
, ldb
, mem_ctx
, msg
, NULL
, NULL
,
2088 samdb_set_password_callback
, NULL
);
2089 if (ret
!= LDB_SUCCESS
) {
2091 return NT_STATUS_NO_MEMORY
;
2094 /* A password change operation */
2095 if ((ntOldHash
!= NULL
) || (lmOldHash
!= NULL
)) {
2096 struct dsdb_control_password_change
*change
;
2098 change
= talloc(req
, struct dsdb_control_password_change
);
2099 if (change
== NULL
) {
2102 return NT_STATUS_NO_MEMORY
;
2105 change
->old_nt_pwd_hash
= ntOldHash
;
2106 change
->old_lm_pwd_hash
= lmOldHash
;
2108 ret
= ldb_request_add_control(req
,
2109 DSDB_CONTROL_PASSWORD_CHANGE_OID
,
2111 if (ret
!= LDB_SUCCESS
) {
2114 return NT_STATUS_NO_MEMORY
;
2117 ret
= ldb_request_add_control(req
,
2118 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID
,
2120 if (ret
!= LDB_SUCCESS
) {
2123 return NT_STATUS_NO_MEMORY
;
2125 ret
= ldb_request_add_control(req
,
2126 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
,
2128 if (ret
!= LDB_SUCCESS
) {
2131 return NT_STATUS_NO_MEMORY
;
2134 ret
= dsdb_autotransaction_request(ldb
, req
);
2136 if (req
->context
!= NULL
) {
2137 pwd_stat
= talloc_steal(mem_ctx
,
2138 ((struct ldb_control
*)req
->context
)->data
);
2144 /* Sets the domain info (if requested) */
2145 if (_dominfo
!= NULL
) {
2146 struct samr_DomInfo1
*dominfo
;
2148 dominfo
= talloc_zero(mem_ctx
, struct samr_DomInfo1
);
2149 if (dominfo
== NULL
) {
2150 return NT_STATUS_NO_MEMORY
;
2153 if (pwd_stat
!= NULL
) {
2154 dominfo
->min_password_length
= pwd_stat
->domain_data
.minPwdLength
;
2155 dominfo
->password_properties
= pwd_stat
->domain_data
.pwdProperties
;
2156 dominfo
->password_history_length
= pwd_stat
->domain_data
.pwdHistoryLength
;
2157 dominfo
->max_password_age
= pwd_stat
->domain_data
.maxPwdAge
;
2158 dominfo
->min_password_age
= pwd_stat
->domain_data
.minPwdAge
;
2161 *_dominfo
= dominfo
;
2164 if (reject_reason
!= NULL
) {
2165 if (pwd_stat
!= NULL
) {
2166 *reject_reason
= pwd_stat
->reject_reason
;
2168 *reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
2172 if (pwd_stat
!= NULL
) {
2173 talloc_free(pwd_stat
);
2176 if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
2177 const char *errmsg
= ldb_errstring(ldb
);
2178 char *endptr
= NULL
;
2179 WERROR werr
= WERR_GENERAL_FAILURE
;
2180 status
= NT_STATUS_UNSUCCESSFUL
;
2181 if (errmsg
!= NULL
) {
2182 werr
= W_ERROR(strtol(errmsg
, &endptr
, 16));
2184 if (endptr
!= errmsg
) {
2185 if (W_ERROR_EQUAL(werr
, WERR_INVALID_PASSWORD
)) {
2186 status
= NT_STATUS_WRONG_PASSWORD
;
2188 if (W_ERROR_EQUAL(werr
, WERR_PASSWORD_RESTRICTION
)) {
2189 status
= NT_STATUS_PASSWORD_RESTRICTION
;
2192 } else if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
2193 /* don't let the caller know if an account doesn't exist */
2194 status
= NT_STATUS_WRONG_PASSWORD
;
2195 } else if (ret
!= LDB_SUCCESS
) {
2196 status
= NT_STATUS_UNSUCCESSFUL
;
2203 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2204 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2205 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2206 * user change or not. The "rejectReason" gives some more informations if the
2209 * This wrapper function for "samdb_set_password" takes a SID as input rather
2212 * This call encapsulates a new LDB transaction for changing the password;
2213 * therefore the user hasn't to start a new one.
2215 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2216 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2217 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2218 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2220 NTSTATUS
samdb_set_password_sid(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2221 const struct dom_sid
*user_sid
,
2222 const DATA_BLOB
*new_password
,
2223 const struct samr_Password
*lmNewHash
,
2224 const struct samr_Password
*ntNewHash
,
2225 const struct samr_Password
*lmOldHash
,
2226 const struct samr_Password
*ntOldHash
,
2227 enum samPwdChangeReason
*reject_reason
,
2228 struct samr_DomInfo1
**_dominfo
)
2231 struct ldb_dn
*user_dn
;
2234 ret
= ldb_transaction_start(ldb
);
2235 if (ret
!= LDB_SUCCESS
) {
2236 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb
)));
2237 return NT_STATUS_TRANSACTION_ABORTED
;
2240 user_dn
= samdb_search_dn(ldb
, mem_ctx
, NULL
,
2241 "(&(objectSid=%s)(objectClass=user))",
2242 ldap_encode_ndr_dom_sid(mem_ctx
, user_sid
));
2244 ldb_transaction_cancel(ldb
);
2245 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2246 dom_sid_string(mem_ctx
, user_sid
)));
2247 return NT_STATUS_NO_SUCH_USER
;
2250 nt_status
= samdb_set_password(ldb
, mem_ctx
,
2253 lmNewHash
, ntNewHash
,
2254 lmOldHash
, ntOldHash
,
2255 reject_reason
, _dominfo
);
2256 if (!NT_STATUS_IS_OK(nt_status
)) {
2257 ldb_transaction_cancel(ldb
);
2258 talloc_free(user_dn
);
2262 ret
= ldb_transaction_commit(ldb
);
2263 if (ret
!= LDB_SUCCESS
) {
2264 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2265 ldb_dn_get_linearized(user_dn
),
2266 ldb_errstring(ldb
)));
2267 talloc_free(user_dn
);
2268 return NT_STATUS_TRANSACTION_ABORTED
;
2271 talloc_free(user_dn
);
2272 return NT_STATUS_OK
;
2276 NTSTATUS
samdb_create_foreign_security_principal(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
2277 struct dom_sid
*sid
, struct ldb_dn
**ret_dn
)
2279 struct ldb_message
*msg
;
2280 struct ldb_dn
*basedn
;
2284 sidstr
= dom_sid_string(mem_ctx
, sid
);
2285 NT_STATUS_HAVE_NO_MEMORY(sidstr
);
2287 /* We might have to create a ForeignSecurityPrincipal, even if this user
2288 * is in our own domain */
2290 msg
= ldb_msg_new(sidstr
);
2292 talloc_free(sidstr
);
2293 return NT_STATUS_NO_MEMORY
;
2296 ret
= dsdb_wellknown_dn(sam_ctx
, sidstr
,
2297 ldb_get_default_basedn(sam_ctx
),
2298 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER
,
2300 if (ret
!= LDB_SUCCESS
) {
2301 DEBUG(0, ("Failed to find DN for "
2302 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx
)));
2303 talloc_free(sidstr
);
2304 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2307 /* add core elements to the ldb_message for the alias */
2309 if ( ! ldb_dn_add_child_fmt(msg
->dn
, "CN=%s", sidstr
)) {
2310 talloc_free(sidstr
);
2311 return NT_STATUS_NO_MEMORY
;
2314 ret
= samdb_msg_add_string(sam_ctx
, msg
, msg
,
2315 "objectClass", "foreignSecurityPrincipal");
2316 if (ret
!= LDB_SUCCESS
) {
2317 talloc_free(sidstr
);
2318 return NT_STATUS_NO_MEMORY
;
2321 /* create the alias */
2322 ret
= ldb_add(sam_ctx
, msg
);
2323 if (ret
!= LDB_SUCCESS
) {
2324 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2326 ldb_dn_get_linearized(msg
->dn
),
2327 ldb_errstring(sam_ctx
)));
2328 talloc_free(sidstr
);
2329 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2332 *ret_dn
= talloc_steal(mem_ctx
, msg
->dn
);
2333 talloc_free(sidstr
);
2335 return NT_STATUS_OK
;
2340 Find the DN of a domain, assuming it to be a dotted.dns name
2343 struct ldb_dn
*samdb_dns_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char *dns_domain
)
2346 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2347 const char *binary_encoded
;
2348 const char **split_realm
;
2355 split_realm
= (const char **)str_list_make(tmp_ctx
, dns_domain
, ".");
2357 talloc_free(tmp_ctx
);
2360 dn
= ldb_dn_new(mem_ctx
, ldb
, NULL
);
2361 for (i
=0; split_realm
[i
]; i
++) {
2362 binary_encoded
= ldb_binary_encode_string(tmp_ctx
, split_realm
[i
]);
2363 if (!ldb_dn_add_base_fmt(dn
, "dc=%s", binary_encoded
)) {
2364 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2365 binary_encoded
, ldb_dn_get_linearized(dn
)));
2366 talloc_free(tmp_ctx
);
2370 if (!ldb_dn_validate(dn
)) {
2371 DEBUG(2, ("Failed to validated DN %s\n",
2372 ldb_dn_get_linearized(dn
)));
2373 talloc_free(tmp_ctx
);
2376 talloc_free(tmp_ctx
);
2381 Find the DN of a domain, be it the netbios or DNS name
2383 struct ldb_dn
*samdb_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2384 const char *domain_name
)
2386 const char * const domain_ref_attrs
[] = {
2389 const char * const domain_ref2_attrs
[] = {
2392 struct ldb_result
*res_domain_ref
;
2393 char *escaped_domain
= ldb_binary_encode_string(mem_ctx
, domain_name
);
2394 /* find the domain's DN */
2395 int ret_domain
= ldb_search(ldb
, mem_ctx
,
2397 samdb_partitions_dn(ldb
, mem_ctx
),
2400 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2402 if (ret_domain
!= LDB_SUCCESS
) {
2406 if (res_domain_ref
->count
== 0) {
2407 ret_domain
= ldb_search(ldb
, mem_ctx
,
2409 samdb_dns_domain_to_dn(ldb
, mem_ctx
, domain_name
),
2412 "(objectclass=domain)");
2413 if (ret_domain
!= LDB_SUCCESS
) {
2417 if (res_domain_ref
->count
== 1) {
2418 return res_domain_ref
->msgs
[0]->dn
;
2423 if (res_domain_ref
->count
> 1) {
2424 DEBUG(0,("Found %d records matching domain [%s]\n",
2425 ret_domain
, domain_name
));
2429 return samdb_result_dn(ldb
, mem_ctx
, res_domain_ref
->msgs
[0], "nCName", NULL
);
2435 use a GUID to find a DN
2437 int dsdb_find_dn_by_guid(struct ldb_context
*ldb
,
2438 TALLOC_CTX
*mem_ctx
,
2439 const struct GUID
*guid
, struct ldb_dn
**dn
)
2442 struct ldb_result
*res
;
2443 const char *attrs
[] = { NULL
};
2444 char *guid_str
= GUID_string(mem_ctx
, guid
);
2447 return ldb_operr(ldb
);
2450 ret
= dsdb_search(ldb
, mem_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, attrs
,
2451 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
|
2452 DSDB_SEARCH_SHOW_EXTENDED_DN
|
2453 DSDB_SEARCH_ONE_ONLY
,
2454 "objectGUID=%s", guid_str
);
2455 talloc_free(guid_str
);
2456 if (ret
!= LDB_SUCCESS
) {
2460 *dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
2467 use a DN to find a GUID with a given attribute name
2469 int dsdb_find_guid_attr_by_dn(struct ldb_context
*ldb
,
2470 struct ldb_dn
*dn
, const char *attribute
,
2474 struct ldb_result
*res
;
2475 const char *attrs
[2];
2476 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2478 attrs
[0] = attribute
;
2481 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
,
2482 DSDB_SEARCH_SHOW_DELETED
|
2483 DSDB_SEARCH_SHOW_RECYCLED
);
2484 if (ret
!= LDB_SUCCESS
) {
2485 talloc_free(tmp_ctx
);
2488 if (res
->count
< 1) {
2489 talloc_free(tmp_ctx
);
2490 return LDB_ERR_NO_SUCH_OBJECT
;
2492 *guid
= samdb_result_guid(res
->msgs
[0], attribute
);
2493 talloc_free(tmp_ctx
);
2498 use a DN to find a GUID
2500 int dsdb_find_guid_by_dn(struct ldb_context
*ldb
,
2501 struct ldb_dn
*dn
, struct GUID
*guid
)
2503 return dsdb_find_guid_attr_by_dn(ldb
, dn
, "objectGUID", guid
);
2509 adds the given GUID to the given ldb_message. This value is added
2510 for the given attr_name (may be either "objectGUID" or "parentGUID").
2512 int dsdb_msg_add_guid(struct ldb_message
*msg
,
2514 const char *attr_name
)
2519 TALLOC_CTX
*tmp_ctx
= talloc_init("dsdb_msg_add_guid");
2521 status
= GUID_to_ndr_blob(guid
, tmp_ctx
, &v
);
2522 if (!NT_STATUS_IS_OK(status
)) {
2523 ret
= LDB_ERR_OPERATIONS_ERROR
;
2527 ret
= ldb_msg_add_steal_value(msg
, attr_name
, &v
);
2528 if (ret
!= LDB_SUCCESS
) {
2529 DEBUG(4,(__location__
": Failed to add %s to the message\n",
2537 talloc_free(tmp_ctx
);
2544 use a DN to find a SID
2546 int dsdb_find_sid_by_dn(struct ldb_context
*ldb
,
2547 struct ldb_dn
*dn
, struct dom_sid
*sid
)
2550 struct ldb_result
*res
;
2551 const char *attrs
[] = { "objectSid", NULL
};
2552 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2557 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
,
2558 DSDB_SEARCH_SHOW_DELETED
|
2559 DSDB_SEARCH_SHOW_RECYCLED
);
2560 if (ret
!= LDB_SUCCESS
) {
2561 talloc_free(tmp_ctx
);
2564 if (res
->count
< 1) {
2565 talloc_free(tmp_ctx
);
2566 return LDB_ERR_NO_SUCH_OBJECT
;
2568 s
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSid");
2570 talloc_free(tmp_ctx
);
2571 return LDB_ERR_NO_SUCH_OBJECT
;
2574 talloc_free(tmp_ctx
);
2579 use a SID to find a DN
2581 int dsdb_find_dn_by_sid(struct ldb_context
*ldb
,
2582 TALLOC_CTX
*mem_ctx
,
2583 struct dom_sid
*sid
, struct ldb_dn
**dn
)
2586 struct ldb_result
*res
;
2587 const char *attrs
[] = { NULL
};
2588 char *sid_str
= ldap_encode_ndr_dom_sid(mem_ctx
, sid
);
2591 return ldb_operr(ldb
);
2594 ret
= dsdb_search(ldb
, mem_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, attrs
,
2595 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
|
2596 DSDB_SEARCH_SHOW_EXTENDED_DN
|
2597 DSDB_SEARCH_ONE_ONLY
,
2598 "objectSid=%s", sid_str
);
2599 talloc_free(sid_str
);
2600 if (ret
!= LDB_SUCCESS
) {
2604 *dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
2611 load a repsFromTo blob list for a given partition GUID
2612 attr must be "repsFrom" or "repsTo"
2614 WERROR
dsdb_loadreps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
2615 const char *attr
, struct repsFromToBlob
**r
, uint32_t *count
)
2617 const char *attrs
[] = { attr
, NULL
};
2618 struct ldb_result
*res
= NULL
;
2619 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2621 struct ldb_message_element
*el
;
2626 if (ldb_search(sam_ctx
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, NULL
) != LDB_SUCCESS
||
2628 DEBUG(0,("dsdb_loadreps: failed to read partition object\n"));
2629 talloc_free(tmp_ctx
);
2630 return WERR_DS_DRA_INTERNAL_ERROR
;
2633 el
= ldb_msg_find_element(res
->msgs
[0], attr
);
2635 /* it's OK to be empty */
2636 talloc_free(tmp_ctx
);
2640 *count
= el
->num_values
;
2641 *r
= talloc_array(mem_ctx
, struct repsFromToBlob
, *count
);
2643 talloc_free(tmp_ctx
);
2644 return WERR_DS_DRA_INTERNAL_ERROR
;
2647 for (i
=0; i
<(*count
); i
++) {
2648 enum ndr_err_code ndr_err
;
2649 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
],
2652 (ndr_pull_flags_fn_t
)ndr_pull_repsFromToBlob
);
2653 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2654 talloc_free(tmp_ctx
);
2655 return WERR_DS_DRA_INTERNAL_ERROR
;
2659 talloc_free(tmp_ctx
);
2665 save the repsFromTo blob list for a given partition GUID
2666 attr must be "repsFrom" or "repsTo"
2668 WERROR
dsdb_savereps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
2669 const char *attr
, struct repsFromToBlob
*r
, uint32_t count
)
2671 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2672 struct ldb_message
*msg
;
2673 struct ldb_message_element
*el
;
2676 msg
= ldb_msg_new(tmp_ctx
);
2678 if (ldb_msg_add_empty(msg
, attr
, LDB_FLAG_MOD_REPLACE
, &el
) != LDB_SUCCESS
) {
2682 el
->values
= talloc_array(msg
, struct ldb_val
, count
);
2687 for (i
=0; i
<count
; i
++) {
2689 enum ndr_err_code ndr_err
;
2691 ndr_err
= ndr_push_struct_blob(&v
, tmp_ctx
,
2693 (ndr_push_flags_fn_t
)ndr_push_repsFromToBlob
);
2694 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2702 if (ldb_modify(sam_ctx
, msg
) != LDB_SUCCESS
) {
2703 DEBUG(0,("Failed to store %s - %s\n", attr
, ldb_errstring(sam_ctx
)));
2707 talloc_free(tmp_ctx
);
2712 talloc_free(tmp_ctx
);
2713 return WERR_DS_DRA_INTERNAL_ERROR
;
2718 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2719 object for a partition
2721 int dsdb_load_partition_usn(struct ldb_context
*ldb
, struct ldb_dn
*dn
,
2722 uint64_t *uSN
, uint64_t *urgent_uSN
)
2724 struct ldb_request
*req
;
2726 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2727 struct dsdb_control_current_partition
*p_ctrl
;
2728 struct ldb_result
*res
;
2730 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
2732 talloc_free(tmp_ctx
);
2733 return ldb_oom(ldb
);
2736 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
2737 ldb_dn_new(tmp_ctx
, ldb
, "@REPLCHANGED"),
2741 res
, ldb_search_default_callback
,
2743 if (ret
!= LDB_SUCCESS
) {
2744 talloc_free(tmp_ctx
);
2748 p_ctrl
= talloc(req
, struct dsdb_control_current_partition
);
2749 if (p_ctrl
== NULL
) {
2750 talloc_free(tmp_ctx
);
2751 return ldb_oom(ldb
);
2753 p_ctrl
->version
= DSDB_CONTROL_CURRENT_PARTITION_VERSION
;
2756 ret
= ldb_request_add_control(req
,
2757 DSDB_CONTROL_CURRENT_PARTITION_OID
,
2759 if (ret
!= LDB_SUCCESS
) {
2760 talloc_free(tmp_ctx
);
2764 /* Run the new request */
2765 ret
= ldb_request(ldb
, req
);
2767 if (ret
== LDB_SUCCESS
) {
2768 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
2771 if (ret
== LDB_ERR_NO_SUCH_OBJECT
|| ret
== LDB_ERR_INVALID_DN_SYNTAX
) {
2772 /* it hasn't been created yet, which means
2773 an implicit value of zero */
2775 talloc_free(tmp_ctx
);
2779 if (ret
!= LDB_SUCCESS
) {
2780 talloc_free(tmp_ctx
);
2784 if (res
->count
< 1) {
2790 *uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNHighest", 0);
2792 *urgent_uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNUrgent", 0);
2796 talloc_free(tmp_ctx
);
2801 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2
*c1
,
2802 const struct drsuapi_DsReplicaCursor2
*c2
)
2804 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
2807 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor
*c1
,
2808 const struct drsuapi_DsReplicaCursor
*c2
)
2810 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
2815 see if a computer identified by its invocationId is a RODC
2817 int samdb_is_rodc(struct ldb_context
*sam_ctx
, const struct GUID
*objectGUID
, bool *is_rodc
)
2819 /* 1) find the DN for this servers NTDSDSA object
2820 2) search for the msDS-isRODC attribute
2821 3) if not present then not a RODC
2822 4) if present and TRUE then is a RODC
2824 struct ldb_dn
*config_dn
;
2825 const char *attrs
[] = { "msDS-isRODC", NULL
};
2827 struct ldb_result
*res
;
2828 TALLOC_CTX
*tmp_ctx
= talloc_new(sam_ctx
);
2830 config_dn
= ldb_get_config_basedn(sam_ctx
);
2832 talloc_free(tmp_ctx
);
2833 return ldb_operr(sam_ctx
);
2836 ret
= dsdb_search(sam_ctx
, tmp_ctx
, &res
, config_dn
, LDB_SCOPE_SUBTREE
, attrs
,
2837 DSDB_SEARCH_ONE_ONLY
, "objectGUID=%s", GUID_string(tmp_ctx
, objectGUID
));
2839 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
2841 talloc_free(tmp_ctx
);
2845 if (ret
!= LDB_SUCCESS
) {
2846 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
2847 GUID_string(tmp_ctx
, objectGUID
)));
2849 talloc_free(tmp_ctx
);
2853 ret
= ldb_msg_find_attr_as_bool(res
->msgs
[0], "msDS-isRODC", 0);
2854 *is_rodc
= (ret
== 1);
2856 talloc_free(tmp_ctx
);
2862 see if we are a RODC
2864 int samdb_rodc(struct ldb_context
*sam_ctx
, bool *am_rodc
)
2866 const struct GUID
*objectGUID
;
2870 /* see if we have a cached copy */
2871 cached
= (bool *)ldb_get_opaque(sam_ctx
, "cache.am_rodc");
2877 objectGUID
= samdb_ntds_objectGUID(sam_ctx
);
2879 return ldb_operr(sam_ctx
);
2882 ret
= samdb_is_rodc(sam_ctx
, objectGUID
, am_rodc
);
2883 if (ret
!= LDB_SUCCESS
) {
2887 cached
= talloc(sam_ctx
, bool);
2888 if (cached
== NULL
) {
2889 return ldb_oom(sam_ctx
);
2893 ret
= ldb_set_opaque(sam_ctx
, "cache.am_rodc", cached
);
2894 if (ret
!= LDB_SUCCESS
) {
2895 talloc_free(cached
);
2896 return ldb_operr(sam_ctx
);
2902 bool samdb_set_am_rodc(struct ldb_context
*ldb
, bool am_rodc
)
2904 TALLOC_CTX
*tmp_ctx
;
2907 tmp_ctx
= talloc_new(ldb
);
2908 if (tmp_ctx
== NULL
) {
2912 cached
= talloc(tmp_ctx
, bool);
2918 if (ldb_set_opaque(ldb
, "cache.am_rodc", cached
) != LDB_SUCCESS
) {
2922 talloc_steal(ldb
, cached
);
2923 talloc_free(tmp_ctx
);
2927 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
2928 talloc_free(tmp_ctx
);
2934 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
2936 flags are DS_NTDS_OPTION_*
2938 int samdb_ntds_options(struct ldb_context
*ldb
, uint32_t *options
)
2940 TALLOC_CTX
*tmp_ctx
;
2941 const char *attrs
[] = { "options", NULL
};
2943 struct ldb_result
*res
;
2945 tmp_ctx
= talloc_new(ldb
);
2946 if (tmp_ctx
== NULL
) {
2950 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
2951 if (ret
!= LDB_SUCCESS
) {
2955 if (res
->count
!= 1) {
2959 *options
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "options", 0);
2961 talloc_free(tmp_ctx
);
2966 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
2967 talloc_free(tmp_ctx
);
2968 return LDB_ERR_NO_SUCH_OBJECT
;
2971 const char* samdb_ntds_object_category(TALLOC_CTX
*tmp_ctx
, struct ldb_context
*ldb
)
2973 const char *attrs
[] = { "objectCategory", NULL
};
2975 struct ldb_result
*res
;
2977 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
2978 if (ret
!= LDB_SUCCESS
) {
2982 if (res
->count
!= 1) {
2986 return ldb_msg_find_attr_as_string(res
->msgs
[0], "objectCategory", NULL
);
2989 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
2994 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
2995 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
2997 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX
*mem_ctx
, const char *cn
)
2999 char **tokens
, *ret
;
3002 tokens
= str_list_make(mem_ctx
, cn
, " -_");
3006 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3007 tokens
[0][0] = tolower(tokens
[0][0]);
3008 for (i
= 1; i
< str_list_length((const char **)tokens
); i
++)
3009 tokens
[i
][0] = toupper(tokens
[i
][0]);
3011 ret
= talloc_strdup(mem_ctx
, tokens
[0]);
3012 for (i
= 1; i
< str_list_length((const char **)tokens
); i
++)
3013 ret
= talloc_asprintf_append_buffer(ret
, "%s", tokens
[i
]);
3015 talloc_free(tokens
);
3021 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3023 int dsdb_functional_level(struct ldb_context
*ldb
)
3025 int *domainFunctionality
=
3026 talloc_get_type(ldb_get_opaque(ldb
, "domainFunctionality"), int);
3027 if (!domainFunctionality
) {
3028 /* this is expected during initial provision */
3029 DEBUG(4,(__location__
": WARNING: domainFunctionality not setup\n"));
3030 return DS_DOMAIN_FUNCTION_2000
;
3032 return *domainFunctionality
;
3036 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3038 int dsdb_forest_functional_level(struct ldb_context
*ldb
)
3040 int *forestFunctionality
=
3041 talloc_get_type(ldb_get_opaque(ldb
, "forestFunctionality"), int);
3042 if (!forestFunctionality
) {
3043 DEBUG(0,(__location__
": WARNING: forestFunctionality not setup\n"));
3044 return DS_DOMAIN_FUNCTION_2000
;
3046 return *forestFunctionality
;
3050 set a GUID in an extended DN structure
3052 int dsdb_set_extended_dn_guid(struct ldb_dn
*dn
, const struct GUID
*guid
, const char *component_name
)
3058 status
= GUID_to_ndr_blob(guid
, dn
, &v
);
3059 if (!NT_STATUS_IS_OK(status
)) {
3060 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
3063 ret
= ldb_dn_set_extended_component(dn
, component_name
, &v
);
3069 return a GUID from a extended DN structure
3071 NTSTATUS
dsdb_get_extended_dn_guid(struct ldb_dn
*dn
, struct GUID
*guid
, const char *component_name
)
3073 const struct ldb_val
*v
;
3075 v
= ldb_dn_get_extended_component(dn
, component_name
);
3077 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3080 return GUID_from_ndr_blob(v
, guid
);
3084 return a uint64_t from a extended DN structure
3086 NTSTATUS
dsdb_get_extended_dn_uint64(struct ldb_dn
*dn
, uint64_t *val
, const char *component_name
)
3088 const struct ldb_val
*v
;
3091 v
= ldb_dn_get_extended_component(dn
, component_name
);
3093 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3095 s
= talloc_strndup(dn
, (const char *)v
->data
, v
->length
);
3096 NT_STATUS_HAVE_NO_MEMORY(s
);
3098 *val
= strtoull(s
, NULL
, 0);
3101 return NT_STATUS_OK
;
3105 return a NTTIME from a extended DN structure
3107 NTSTATUS
dsdb_get_extended_dn_nttime(struct ldb_dn
*dn
, NTTIME
*nttime
, const char *component_name
)
3109 return dsdb_get_extended_dn_uint64(dn
, nttime
, component_name
);
3113 return a uint32_t from a extended DN structure
3115 NTSTATUS
dsdb_get_extended_dn_uint32(struct ldb_dn
*dn
, uint32_t *val
, const char *component_name
)
3117 const struct ldb_val
*v
;
3120 v
= ldb_dn_get_extended_component(dn
, component_name
);
3122 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3125 s
= talloc_strndup(dn
, (const char *)v
->data
, v
->length
);
3126 NT_STATUS_HAVE_NO_MEMORY(s
);
3128 *val
= strtoul(s
, NULL
, 0);
3131 return NT_STATUS_OK
;
3135 return a dom_sid from a extended DN structure
3137 NTSTATUS
dsdb_get_extended_dn_sid(struct ldb_dn
*dn
, struct dom_sid
*sid
, const char *component_name
)
3139 const struct ldb_val
*sid_blob
;
3140 struct TALLOC_CTX
*tmp_ctx
;
3141 enum ndr_err_code ndr_err
;
3143 sid_blob
= ldb_dn_get_extended_component(dn
, component_name
);
3145 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3148 tmp_ctx
= talloc_new(NULL
);
3150 ndr_err
= ndr_pull_struct_blob_all(sid_blob
, tmp_ctx
, sid
,
3151 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
3152 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3153 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
3154 talloc_free(tmp_ctx
);
3158 talloc_free(tmp_ctx
);
3159 return NT_STATUS_OK
;
3164 return RMD_FLAGS directly from a ldb_dn
3165 returns 0 if not found
3167 uint32_t dsdb_dn_rmd_flags(struct ldb_dn
*dn
)
3169 const struct ldb_val
*v
;
3171 v
= ldb_dn_get_extended_component(dn
, "RMD_FLAGS");
3172 if (!v
|| v
->length
> sizeof(buf
)-1) return 0;
3173 strncpy(buf
, (const char *)v
->data
, v
->length
);
3175 return strtoul(buf
, NULL
, 10);
3179 return RMD_FLAGS directly from a ldb_val for a DN
3180 returns 0 if RMD_FLAGS is not found
3182 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val
*val
)
3188 if (val
->length
< 13) {
3191 p
= memmem(val
->data
, val
->length
, "<RMD_FLAGS=", 11);
3195 flags
= strtoul(p
+11, &end
, 10);
3196 if (!end
|| *end
!= '>') {
3197 /* it must end in a > */
3204 return true if a ldb_val containing a DN in storage form is deleted
3206 bool dsdb_dn_is_deleted_val(const struct ldb_val
*val
)
3208 return (dsdb_dn_val_rmd_flags(val
) & DSDB_RMD_FLAG_DELETED
) != 0;
3212 return true if a ldb_val containing a DN in storage form is
3213 in the upgraded w2k3 linked attribute format
3215 bool dsdb_dn_is_upgraded_link_val(struct ldb_val
*val
)
3217 return memmem(val
->data
, val
->length
, "<RMD_ADDTIME=", 13) != NULL
;
3221 return a DN for a wellknown GUID
3223 int dsdb_wellknown_dn(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
,
3224 struct ldb_dn
*nc_root
, const char *wk_guid
,
3225 struct ldb_dn
**wkguid_dn
)
3227 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3228 const char *attrs
[] = { NULL
};
3231 struct ldb_result
*res
;
3233 /* construct the magic WKGUID DN */
3234 dn
= ldb_dn_new_fmt(tmp_ctx
, samdb
, "<WKGUID=%s,%s>",
3235 wk_guid
, ldb_dn_get_linearized(nc_root
));
3237 talloc_free(tmp_ctx
);
3238 return ldb_operr(samdb
);
3241 ret
= dsdb_search_dn(samdb
, tmp_ctx
, &res
, dn
, attrs
,
3242 DSDB_SEARCH_SHOW_DELETED
|
3243 DSDB_SEARCH_SHOW_RECYCLED
);
3244 if (ret
!= LDB_SUCCESS
) {
3245 talloc_free(tmp_ctx
);
3249 (*wkguid_dn
) = talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
3250 talloc_free(tmp_ctx
);
3255 static int dsdb_dn_compare_ptrs(struct ldb_dn
**dn1
, struct ldb_dn
**dn2
)
3257 return ldb_dn_compare(*dn1
, *dn2
);
3261 find a NC root given a DN within the NC
3263 int dsdb_find_nc_root(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
3264 struct ldb_dn
**nc_root
)
3266 const char *root_attrs
[] = { "namingContexts", NULL
};
3267 TALLOC_CTX
*tmp_ctx
;
3269 struct ldb_message_element
*el
;
3270 struct ldb_result
*root_res
;
3272 struct ldb_dn
**nc_dns
;
3274 tmp_ctx
= talloc_new(samdb
);
3275 if (tmp_ctx
== NULL
) {
3276 return ldb_oom(samdb
);
3279 ret
= ldb_search(samdb
, tmp_ctx
, &root_res
,
3280 ldb_dn_new(tmp_ctx
, samdb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
3281 if (ret
!= LDB_SUCCESS
) {
3282 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb
)));
3283 talloc_free(tmp_ctx
);
3287 el
= ldb_msg_find_element(root_res
->msgs
[0], "namingContexts");
3289 struct ldb_message
*tmp_msg
;
3291 DEBUG(5,("Finding namingContexts element in root_res failed. Using a temporary list."));
3293 /* This generates a temporary list of NCs in order to let the
3294 * provisioning work. */
3295 tmp_msg
= ldb_msg_new(tmp_ctx
);
3296 if (tmp_msg
== NULL
) {
3297 talloc_free(tmp_ctx
);
3298 return ldb_oom(samdb
);
3300 ret
= ldb_msg_add_steal_string(tmp_msg
, "namingContexts",
3301 ldb_dn_alloc_linearized(tmp_msg
, ldb_get_schema_basedn(samdb
)));
3302 if (ret
!= LDB_SUCCESS
) {
3303 talloc_free(tmp_ctx
);
3306 ret
= ldb_msg_add_steal_string(tmp_msg
, "namingContexts",
3307 ldb_dn_alloc_linearized(tmp_msg
, ldb_get_config_basedn(samdb
)));
3308 if (ret
!= LDB_SUCCESS
) {
3309 talloc_free(tmp_ctx
);
3312 ret
= ldb_msg_add_steal_string(tmp_msg
, "namingContexts",
3313 ldb_dn_alloc_linearized(tmp_msg
, ldb_get_default_basedn(samdb
)));
3314 if (ret
!= LDB_SUCCESS
) {
3315 talloc_free(tmp_ctx
);
3318 el
= &tmp_msg
->elements
[0];
3321 nc_dns
= talloc_array(tmp_ctx
, struct ldb_dn
*, el
->num_values
);
3323 talloc_free(tmp_ctx
);
3324 return ldb_oom(samdb
);
3327 for (i
=0; i
<el
->num_values
; i
++) {
3328 nc_dns
[i
] = ldb_dn_from_ldb_val(nc_dns
, samdb
, &el
->values
[i
]);
3329 if (nc_dns
[i
] == NULL
) {
3330 talloc_free(tmp_ctx
);
3331 return ldb_operr(samdb
);
3335 TYPESAFE_QSORT(nc_dns
, el
->num_values
, dsdb_dn_compare_ptrs
);
3337 for (i
=0; i
<el
->num_values
; i
++) {
3338 if (ldb_dn_compare_base(nc_dns
[i
], dn
) == 0) {
3339 (*nc_root
) = talloc_steal(mem_ctx
, nc_dns
[i
]);
3340 talloc_free(tmp_ctx
);
3345 talloc_free(tmp_ctx
);
3346 return LDB_ERR_NO_SUCH_OBJECT
;
3351 find the deleted objects DN for any object, by looking for the NC
3352 root, then looking up the wellknown GUID
3354 int dsdb_get_deleted_objects_dn(struct ldb_context
*ldb
,
3355 TALLOC_CTX
*mem_ctx
, struct ldb_dn
*obj_dn
,
3356 struct ldb_dn
**do_dn
)
3358 struct ldb_dn
*nc_root
;
3361 ret
= dsdb_find_nc_root(ldb
, mem_ctx
, obj_dn
, &nc_root
);
3362 if (ret
!= LDB_SUCCESS
) {
3366 ret
= dsdb_wellknown_dn(ldb
, mem_ctx
, nc_root
, DS_GUID_DELETED_OBJECTS_CONTAINER
, do_dn
);
3367 talloc_free(nc_root
);
3372 return the tombstoneLifetime, in days
3374 int dsdb_tombstone_lifetime(struct ldb_context
*ldb
, uint32_t *lifetime
)
3377 dn
= ldb_get_config_basedn(ldb
);
3379 return LDB_ERR_NO_SUCH_OBJECT
;
3381 dn
= ldb_dn_copy(ldb
, dn
);
3383 return ldb_operr(ldb
);
3385 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3386 be a wellknown GUID for this */
3387 if (!ldb_dn_add_child_fmt(dn
, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3389 return ldb_operr(ldb
);
3392 *lifetime
= samdb_search_uint(ldb
, dn
, 180, dn
, "tombstoneLifetime", "objectClass=nTDSService");
3398 compare a ldb_val to a string case insensitively
3400 int samdb_ldb_val_case_cmp(const char *s
, struct ldb_val
*v
)
3402 size_t len
= strlen(s
);
3404 if (len
> v
->length
) return 1;
3405 ret
= strncasecmp(s
, (const char *)v
->data
, v
->length
);
3406 if (ret
!= 0) return ret
;
3407 if (v
->length
> len
&& v
->data
[len
] != 0) {
3415 load the UDV for a partition in v2 format
3416 The list is returned sorted, and with our local cursor added
3418 int dsdb_load_udv_v2(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
3419 struct drsuapi_DsReplicaCursor2
**cursors
, uint32_t *count
)
3421 static const char *attrs
[] = { "replUpToDateVector", NULL
};
3422 struct ldb_result
*r
;
3423 const struct ldb_val
*ouv_value
;
3426 uint64_t highest_usn
;
3427 const struct GUID
*our_invocation_id
;
3428 struct timeval now
= timeval_current();
3430 ret
= ldb_search(samdb
, mem_ctx
, &r
, dn
, LDB_SCOPE_BASE
, attrs
, NULL
);
3431 if (ret
!= LDB_SUCCESS
) {
3435 ouv_value
= ldb_msg_find_ldb_val(r
->msgs
[0], "replUpToDateVector");
3437 enum ndr_err_code ndr_err
;
3438 struct replUpToDateVectorBlob ouv
;
3440 ndr_err
= ndr_pull_struct_blob(ouv_value
, r
, &ouv
,
3441 (ndr_pull_flags_fn_t
)ndr_pull_replUpToDateVectorBlob
);
3442 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3444 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
3446 if (ouv
.version
!= 2) {
3447 /* we always store as version 2, and
3448 * replUpToDateVector is not replicated
3450 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
3453 *count
= ouv
.ctr
.ctr2
.count
;
3454 *cursors
= talloc_steal(mem_ctx
, ouv
.ctr
.ctr2
.cursors
);
3462 our_invocation_id
= samdb_ntds_invocation_id(samdb
);
3463 if (!our_invocation_id
) {
3464 DEBUG(0,(__location__
": No invocationID on samdb - %s\n", ldb_errstring(samdb
)));
3465 talloc_free(*cursors
);
3466 return ldb_operr(samdb
);
3469 ret
= dsdb_load_partition_usn(samdb
, dn
, &highest_usn
, NULL
);
3470 if (ret
!= LDB_SUCCESS
) {
3471 /* nothing to add - this can happen after a vampire */
3472 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
3476 for (i
=0; i
<*count
; i
++) {
3477 if (GUID_equal(our_invocation_id
, &(*cursors
)[i
].source_dsa_invocation_id
)) {
3478 (*cursors
)[i
].highest_usn
= highest_usn
;
3479 (*cursors
)[i
].last_sync_success
= timeval_to_nttime(&now
);
3480 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
3485 (*cursors
) = talloc_realloc(mem_ctx
, *cursors
, struct drsuapi_DsReplicaCursor2
, (*count
)+1);
3487 return ldb_oom(samdb
);
3490 (*cursors
)[*count
].source_dsa_invocation_id
= *our_invocation_id
;
3491 (*cursors
)[*count
].highest_usn
= highest_usn
;
3492 (*cursors
)[*count
].last_sync_success
= timeval_to_nttime(&now
);
3495 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
3501 load the UDV for a partition in version 1 format
3502 The list is returned sorted, and with our local cursor added
3504 int dsdb_load_udv_v1(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
3505 struct drsuapi_DsReplicaCursor
**cursors
, uint32_t *count
)
3507 struct drsuapi_DsReplicaCursor2
*v2
;
3511 ret
= dsdb_load_udv_v2(samdb
, dn
, mem_ctx
, &v2
, count
);
3512 if (ret
!= LDB_SUCCESS
) {
3522 *cursors
= talloc_array(mem_ctx
, struct drsuapi_DsReplicaCursor
, *count
);
3523 if (*cursors
== NULL
) {
3525 return ldb_oom(samdb
);
3528 for (i
=0; i
<*count
; i
++) {
3529 (*cursors
)[i
].source_dsa_invocation_id
= v2
[i
].source_dsa_invocation_id
;
3530 (*cursors
)[i
].highest_usn
= v2
[i
].highest_usn
;
3537 add a set of controls to a ldb_request structure based on a set of
3538 flags. See util.h for a list of available flags
3540 int dsdb_request_add_controls(struct ldb_request
*req
, uint32_t dsdb_flags
)
3543 if (dsdb_flags
& DSDB_SEARCH_SEARCH_ALL_PARTITIONS
) {
3544 struct ldb_search_options_control
*options
;
3545 /* Using the phantom root control allows us to search all partitions */
3546 options
= talloc(req
, struct ldb_search_options_control
);
3547 if (options
== NULL
) {
3548 return LDB_ERR_OPERATIONS_ERROR
;
3550 options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
3552 ret
= ldb_request_add_control(req
,
3553 LDB_CONTROL_SEARCH_OPTIONS_OID
,
3555 if (ret
!= LDB_SUCCESS
) {
3560 if (dsdb_flags
& DSDB_SEARCH_SHOW_DELETED
) {
3561 ret
= ldb_request_add_control(req
, LDB_CONTROL_SHOW_DELETED_OID
, true, NULL
);
3562 if (ret
!= LDB_SUCCESS
) {
3567 if (dsdb_flags
& DSDB_SEARCH_SHOW_RECYCLED
) {
3568 ret
= ldb_request_add_control(req
, LDB_CONTROL_SHOW_RECYCLED_OID
, false, NULL
);
3569 if (ret
!= LDB_SUCCESS
) {
3574 if (dsdb_flags
& DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
) {
3575 ret
= ldb_request_add_control(req
, DSDB_CONTROL_DN_STORAGE_FORMAT_OID
, true, NULL
);
3576 if (ret
!= LDB_SUCCESS
) {
3581 if (dsdb_flags
& DSDB_SEARCH_SHOW_EXTENDED_DN
) {
3582 struct ldb_extended_dn_control
*extended_ctrl
= talloc(req
, struct ldb_extended_dn_control
);
3583 if (!extended_ctrl
) {
3584 return LDB_ERR_OPERATIONS_ERROR
;
3586 extended_ctrl
->type
= 1;
3588 ret
= ldb_request_add_control(req
, LDB_CONTROL_EXTENDED_DN_OID
, true, extended_ctrl
);
3589 if (ret
!= LDB_SUCCESS
) {
3594 if (dsdb_flags
& DSDB_SEARCH_REVEAL_INTERNALS
) {
3595 ret
= ldb_request_add_control(req
, LDB_CONTROL_REVEAL_INTERNALS
, false, NULL
);
3596 if (ret
!= LDB_SUCCESS
) {
3601 if (dsdb_flags
& DSDB_MODIFY_RELAX
) {
3602 ret
= ldb_request_add_control(req
, LDB_CONTROL_RELAX_OID
, false, NULL
);
3603 if (ret
!= LDB_SUCCESS
) {
3608 if (dsdb_flags
& DSDB_MODIFY_PERMISSIVE
) {
3609 ret
= ldb_request_add_control(req
, LDB_CONTROL_PERMISSIVE_MODIFY_OID
, false, NULL
);
3610 if (ret
!= LDB_SUCCESS
) {
3615 if (dsdb_flags
& DSDB_FLAG_AS_SYSTEM
) {
3616 ret
= ldb_request_add_control(req
, LDB_CONTROL_AS_SYSTEM_OID
, false, NULL
);
3617 if (ret
!= LDB_SUCCESS
) {
3622 if (dsdb_flags
& DSDB_TREE_DELETE
) {
3623 ret
= ldb_request_add_control(req
, LDB_CONTROL_TREE_DELETE_OID
, false, NULL
);
3624 if (ret
!= LDB_SUCCESS
) {
3629 if (dsdb_flags
& DSDB_PROVISION
) {
3630 ret
= ldb_request_add_control(req
, LDB_CONTROL_PROVISION_OID
, false, NULL
);
3631 if (ret
!= LDB_SUCCESS
) {
3640 an add with a set of controls
3642 int dsdb_add(struct ldb_context
*ldb
, const struct ldb_message
*message
,
3643 uint32_t dsdb_flags
)
3645 struct ldb_request
*req
;
3648 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
3652 ldb_op_default_callback
,
3655 if (ret
!= LDB_SUCCESS
) return ret
;
3657 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3658 if (ret
!= LDB_SUCCESS
) {
3663 ret
= dsdb_autotransaction_request(ldb
, req
);
3670 a modify with a set of controls
3672 int dsdb_modify(struct ldb_context
*ldb
, const struct ldb_message
*message
,
3673 uint32_t dsdb_flags
)
3675 struct ldb_request
*req
;
3678 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
3682 ldb_op_default_callback
,
3685 if (ret
!= LDB_SUCCESS
) return ret
;
3687 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3688 if (ret
!= LDB_SUCCESS
) {
3693 ret
= dsdb_autotransaction_request(ldb
, req
);
3700 like dsdb_modify() but set all the element flags to
3701 LDB_FLAG_MOD_REPLACE
3703 int dsdb_replace(struct ldb_context
*ldb
, struct ldb_message
*msg
, uint32_t dsdb_flags
)
3707 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3708 for (i
=0;i
<msg
->num_elements
;i
++) {
3709 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
3712 return dsdb_modify(ldb
, msg
, dsdb_flags
);
3717 search for attrs on one DN, allowing for dsdb_flags controls
3719 int dsdb_search_dn(struct ldb_context
*ldb
,
3720 TALLOC_CTX
*mem_ctx
,
3721 struct ldb_result
**_res
,
3722 struct ldb_dn
*basedn
,
3723 const char * const *attrs
,
3724 uint32_t dsdb_flags
)
3727 struct ldb_request
*req
;
3728 struct ldb_result
*res
;
3730 res
= talloc_zero(mem_ctx
, struct ldb_result
);
3732 return ldb_oom(ldb
);
3735 ret
= ldb_build_search_req(&req
, ldb
, res
,
3742 ldb_search_default_callback
,
3744 if (ret
!= LDB_SUCCESS
) {
3749 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3750 if (ret
!= LDB_SUCCESS
) {
3755 ret
= ldb_request(ldb
, req
);
3756 if (ret
== LDB_SUCCESS
) {
3757 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
3761 if (ret
!= LDB_SUCCESS
) {
3771 search for attrs on one DN, by the GUID of the DN, allowing for
3774 int dsdb_search_by_dn_guid(struct ldb_context
*ldb
,
3775 TALLOC_CTX
*mem_ctx
,
3776 struct ldb_result
**_res
,
3777 const struct GUID
*guid
,
3778 const char * const *attrs
,
3779 uint32_t dsdb_flags
)
3781 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3785 dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "<GUID=%s>", GUID_string(tmp_ctx
, guid
));
3786 if (!ldb_dn_validate(dn
)) {
3787 talloc_free(tmp_ctx
);
3788 return LDB_ERR_INVALID_DN_SYNTAX
;
3791 ret
= dsdb_search_dn(ldb
, mem_ctx
, _res
, dn
, attrs
, dsdb_flags
);
3792 talloc_free(tmp_ctx
);
3797 general search with dsdb_flags for controls
3799 int dsdb_search(struct ldb_context
*ldb
,
3800 TALLOC_CTX
*mem_ctx
,
3801 struct ldb_result
**_res
,
3802 struct ldb_dn
*basedn
,
3803 enum ldb_scope scope
,
3804 const char * const *attrs
,
3805 uint32_t dsdb_flags
,
3806 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
3809 struct ldb_request
*req
;
3810 struct ldb_result
*res
;
3812 char *expression
= NULL
;
3813 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3815 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
3817 talloc_free(tmp_ctx
);
3818 return ldb_oom(ldb
);
3822 va_start(ap
, exp_fmt
);
3823 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
3827 talloc_free(tmp_ctx
);
3828 return ldb_oom(ldb
);
3832 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
3839 ldb_search_default_callback
,
3841 if (ret
!= LDB_SUCCESS
) {
3842 talloc_free(tmp_ctx
);
3846 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3847 if (ret
!= LDB_SUCCESS
) {
3848 talloc_free(tmp_ctx
);
3849 ldb_reset_err_string(ldb
);
3853 ret
= ldb_request(ldb
, req
);
3854 if (ret
== LDB_SUCCESS
) {
3855 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
3858 if (ret
!= LDB_SUCCESS
) {
3859 talloc_free(tmp_ctx
);
3863 if (dsdb_flags
& DSDB_SEARCH_ONE_ONLY
) {
3864 if (res
->count
== 0) {
3865 talloc_free(tmp_ctx
);
3866 ldb_reset_err_string(ldb
);
3867 return LDB_ERR_NO_SUCH_OBJECT
;
3869 if (res
->count
!= 1) {
3870 talloc_free(tmp_ctx
);
3871 ldb_reset_err_string(ldb
);
3872 return LDB_ERR_CONSTRAINT_VIOLATION
;
3876 *_res
= talloc_steal(mem_ctx
, res
);
3877 talloc_free(tmp_ctx
);
3884 general search with dsdb_flags for controls
3885 returns exactly 1 record or an error
3887 int dsdb_search_one(struct ldb_context
*ldb
,
3888 TALLOC_CTX
*mem_ctx
,
3889 struct ldb_message
**msg
,
3890 struct ldb_dn
*basedn
,
3891 enum ldb_scope scope
,
3892 const char * const *attrs
,
3893 uint32_t dsdb_flags
,
3894 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
3897 struct ldb_result
*res
;
3899 char *expression
= NULL
;
3900 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3902 dsdb_flags
|= DSDB_SEARCH_ONE_ONLY
;
3904 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
3906 talloc_free(tmp_ctx
);
3907 return ldb_oom(ldb
);
3911 va_start(ap
, exp_fmt
);
3912 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
3916 talloc_free(tmp_ctx
);
3917 return ldb_oom(ldb
);
3919 ret
= dsdb_search(ldb
, tmp_ctx
, &res
, basedn
, scope
, attrs
,
3920 dsdb_flags
, "%s", expression
);
3922 ret
= dsdb_search(ldb
, tmp_ctx
, &res
, basedn
, scope
, attrs
,
3926 if (ret
!= LDB_SUCCESS
) {
3927 talloc_free(tmp_ctx
);
3931 *msg
= talloc_steal(mem_ctx
, res
->msgs
[0]);
3932 talloc_free(tmp_ctx
);
3937 /* returns back the forest DNS name */
3938 const char *samdb_forest_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
3940 const char *forest_name
= ldb_dn_canonical_string(mem_ctx
,
3941 ldb_get_root_basedn(ldb
));
3944 if (forest_name
== NULL
) {
3948 p
= strchr(forest_name
, '/');
3957 validate that an DSA GUID belongs to the specified user sid.
3958 The user SID must be a domain controller account (either RODC or
3961 int dsdb_validate_dsa_guid(struct ldb_context
*ldb
,
3962 const struct GUID
*dsa_guid
,
3963 const struct dom_sid
*sid
)
3966 - find DN of record with the DSA GUID in the
3967 configuration partition (objectGUID)
3968 - remove "NTDS Settings" component from DN
3969 - do a base search on that DN for serverReference with
3971 - extract objectSid from resulting serverReference
3973 - check this sid matches the sid argument
3975 struct ldb_dn
*config_dn
;
3976 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
3977 struct ldb_message
*msg
;
3978 const char *attrs1
[] = { NULL
};
3979 const char *attrs2
[] = { "serverReference", NULL
};
3981 struct ldb_dn
*dn
, *account_dn
;
3982 struct dom_sid sid2
;
3985 config_dn
= ldb_get_config_basedn(ldb
);
3987 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, config_dn
, LDB_SCOPE_SUBTREE
,
3988 attrs1
, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx
, dsa_guid
));
3989 if (ret
!= LDB_SUCCESS
) {
3990 DEBUG(1,(__location__
": Failed to find DSA objectGUID %s for sid %s\n",
3991 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
3992 talloc_free(tmp_ctx
);
3993 return ldb_operr(ldb
);
3997 if (!ldb_dn_remove_child_components(dn
, 1)) {
3998 talloc_free(tmp_ctx
);
3999 return ldb_operr(ldb
);
4002 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, dn
, LDB_SCOPE_BASE
,
4003 attrs2
, DSDB_SEARCH_SHOW_EXTENDED_DN
,
4004 "(objectClass=server)");
4005 if (ret
!= LDB_SUCCESS
) {
4006 DEBUG(1,(__location__
": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4007 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
4008 talloc_free(tmp_ctx
);
4009 return ldb_operr(ldb
);
4012 account_dn
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, msg
, "serverReference");
4013 if (account_dn
== NULL
) {
4014 DEBUG(1,(__location__
": Failed to find account_dn for DSA with objectGUID %s, sid %s\n",
4015 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
4016 talloc_free(tmp_ctx
);
4017 return ldb_operr(ldb
);
4020 status
= dsdb_get_extended_dn_sid(account_dn
, &sid2
, "SID");
4021 if (!NT_STATUS_IS_OK(status
)) {
4022 DEBUG(1,(__location__
": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4023 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
4024 talloc_free(tmp_ctx
);
4025 return ldb_operr(ldb
);
4028 if (!dom_sid_equal(sid
, &sid2
)) {
4029 /* someone is trying to spoof another account */
4030 DEBUG(0,(__location__
": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4031 GUID_string(tmp_ctx
, dsa_guid
),
4032 dom_sid_string(tmp_ctx
, sid
),
4033 dom_sid_string(tmp_ctx
, &sid2
)));
4034 talloc_free(tmp_ctx
);
4035 return ldb_operr(ldb
);
4038 talloc_free(tmp_ctx
);
4042 static const char * const secret_attributes
[] = {
4043 DSDB_SECRET_ATTRIBUTES
,
4048 check if the attribute belongs to the RODC filtered attribute set
4049 Note that attributes that are in the filtered attribute set are the
4050 ones that _are_ always sent to a RODC
4052 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute
*sa
)
4054 /* they never get secret attributes */
4055 if (is_attr_in_list(secret_attributes
, sa
->lDAPDisplayName
)) {
4059 /* they do get non-secret critical attributes */
4060 if (sa
->schemaFlagsEx
& SCHEMA_FLAG_ATTR_IS_CRITICAL
) {
4064 /* they do get non-secret attributes marked as being in the FAS */
4065 if (sa
->searchFlags
& SEARCH_FLAG_RODC_ATTRIBUTE
) {
4069 /* other attributes are denied */
4073 /* return fsmo role dn and role owner dn for a particular role*/
4074 WERROR
dsdb_get_fsmo_role_info(TALLOC_CTX
*tmp_ctx
,
4075 struct ldb_context
*ldb
,
4077 struct ldb_dn
**fsmo_role_dn
,
4078 struct ldb_dn
**role_owner_dn
)
4082 case DREPL_NAMING_MASTER
:
4083 *fsmo_role_dn
= samdb_partitions_dn(ldb
, tmp_ctx
);
4084 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4085 if (ret
!= LDB_SUCCESS
) {
4086 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Naming Master object - %s",
4087 ldb_errstring(ldb
)));
4088 talloc_free(tmp_ctx
);
4089 return WERR_DS_DRA_INTERNAL_ERROR
;
4092 case DREPL_INFRASTRUCTURE_MASTER
:
4093 *fsmo_role_dn
= samdb_infrastructure_dn(ldb
, tmp_ctx
);
4094 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4095 if (ret
!= LDB_SUCCESS
) {
4096 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Schema Master object - %s",
4097 ldb_errstring(ldb
)));
4098 talloc_free(tmp_ctx
);
4099 return WERR_DS_DRA_INTERNAL_ERROR
;
4102 case DREPL_RID_MASTER
:
4103 ret
= samdb_rid_manager_dn(ldb
, tmp_ctx
, fsmo_role_dn
);
4104 if (ret
!= LDB_SUCCESS
) {
4105 DEBUG(0, (__location__
": Failed to find RID Manager object - %s", ldb_errstring(ldb
)));
4106 talloc_free(tmp_ctx
);
4107 return WERR_DS_DRA_INTERNAL_ERROR
;
4110 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4111 if (ret
!= LDB_SUCCESS
) {
4112 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in RID Manager object - %s",
4113 ldb_errstring(ldb
)));
4114 talloc_free(tmp_ctx
);
4115 return WERR_DS_DRA_INTERNAL_ERROR
;
4118 case DREPL_SCHEMA_MASTER
:
4119 *fsmo_role_dn
= ldb_get_schema_basedn(ldb
);
4120 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4121 if (ret
!= LDB_SUCCESS
) {
4122 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Schema Master object - %s",
4123 ldb_errstring(ldb
)));
4124 talloc_free(tmp_ctx
);
4125 return WERR_DS_DRA_INTERNAL_ERROR
;
4128 case DREPL_PDC_MASTER
:
4129 *fsmo_role_dn
= ldb_get_default_basedn(ldb
);
4130 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4131 if (ret
!= LDB_SUCCESS
) {
4132 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Pd Master object - %s",
4133 ldb_errstring(ldb
)));
4134 talloc_free(tmp_ctx
);
4135 return WERR_DS_DRA_INTERNAL_ERROR
;
4139 return WERR_DS_DRA_INTERNAL_ERROR
;
4144 const char *samdb_dn_to_dnshostname(struct ldb_context
*ldb
,
4145 TALLOC_CTX
*mem_ctx
,
4146 struct ldb_dn
*server_dn
)
4149 struct ldb_result
*res
= NULL
;
4150 const char * const attrs
[] = { "dNSHostName", NULL
};
4152 ldb_ret
= ldb_search(ldb
, mem_ctx
, &res
,
4156 if (ldb_ret
!= LDB_SUCCESS
) {
4157 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
4158 ldb_dn_get_linearized(server_dn
), ldb_errstring(ldb
)));
4162 return ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
4166 returns true if an attribute is in the filter,
4167 false otherwise, provided that attribute value is provided with the expression
4169 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree
*tree
,
4173 switch (tree
->operation
) {
4176 for (i
=0;i
<tree
->u
.list
.num_elements
;i
++) {
4177 if (dsdb_attr_in_parse_tree(tree
->u
.list
.elements
[i
],
4183 return dsdb_attr_in_parse_tree(tree
->u
.isnot
.child
, attr
);
4184 case LDB_OP_EQUALITY
:
4185 case LDB_OP_GREATER
:
4188 if (ldb_attr_cmp(tree
->u
.equality
.attr
, attr
) == 0) {
4192 case LDB_OP_SUBSTRING
:
4193 if (ldb_attr_cmp(tree
->u
.substring
.attr
, attr
) == 0) {
4197 case LDB_OP_PRESENT
:
4198 /* (attrname=*) is not filtered out */
4200 case LDB_OP_EXTENDED
:
4201 if (tree
->u
.extended
.attr
&&
4202 ldb_attr_cmp(tree
->u
.extended
.attr
, attr
) == 0) {
4210 bool is_attr_in_list(const char * const * attrs
, const char *attr
)
4214 for (i
= 0; attrs
[i
]; i
++) {
4215 if (ldb_attr_cmp(attrs
[i
], attr
) == 0)