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_errors.h"
28 #include "../lib/util/util_ldb.h"
29 #include "../lib/crypto/crypto.h"
30 #include "dsdb/samdb/samdb.h"
31 #include "libcli/security/security.h"
32 #include "librpc/gen_ndr/ndr_security.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "../libds/common/flags.h"
35 #include "dsdb/common/proto.h"
36 #include "libcli/ldap/ldap_ndr.h"
37 #include "param/param.h"
38 #include "libcli/auth/libcli_auth.h"
39 #include "librpc/gen_ndr/ndr_drsblobs.h"
40 #include "system/locale.h"
41 #include "lib/util/tsort.h"
42 #include "dsdb/common/util.h"
45 search the sam for the specified attributes in a specific domain, filter on
46 objectSid being in domain_sid.
48 int samdb_search_domain(struct ldb_context
*sam_ldb
,
50 struct ldb_dn
*basedn
,
51 struct ldb_message
***res
,
52 const char * const *attrs
,
53 const struct dom_sid
*domain_sid
,
54 const char *format
, ...) _PRINTF_ATTRIBUTE(7,8)
60 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
,
61 res
, attrs
, format
, ap
);
67 struct dom_sid
*entry_sid
;
69 entry_sid
= samdb_result_dom_sid(mem_ctx
, (*res
)[i
], "objectSid");
71 if ((entry_sid
== NULL
) ||
72 (!dom_sid_in_domain(domain_sid
, entry_sid
))) {
73 /* Delete that entry from the result set */
74 (*res
)[i
] = (*res
)[count
-1];
76 talloc_free(entry_sid
);
79 talloc_free(entry_sid
);
87 search the sam for a single string attribute in exactly 1 record
89 const char *samdb_search_string_v(struct ldb_context
*sam_ldb
,
91 struct ldb_dn
*basedn
,
92 const char *attr_name
,
93 const char *format
, va_list ap
) _PRINTF_ATTRIBUTE(5,0)
96 const char *attrs
[2] = { NULL
, NULL
};
97 struct ldb_message
**res
= NULL
;
101 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
103 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
104 attr_name
, format
, count
));
111 return samdb_result_string(res
[0], attr_name
, NULL
);
115 search the sam for a single string attribute in exactly 1 record
117 const char *samdb_search_string(struct ldb_context
*sam_ldb
,
119 struct ldb_dn
*basedn
,
120 const char *attr_name
,
121 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
126 va_start(ap
, format
);
127 str
= samdb_search_string_v(sam_ldb
, mem_ctx
, basedn
, attr_name
, format
, ap
);
133 struct ldb_dn
*samdb_search_dn(struct ldb_context
*sam_ldb
,
135 struct ldb_dn
*basedn
,
136 const char *format
, ...) _PRINTF_ATTRIBUTE(4,5)
140 struct ldb_message
**res
= NULL
;
143 va_start(ap
, format
);
144 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, NULL
, format
, ap
);
147 if (count
!= 1) return NULL
;
149 ret
= talloc_steal(mem_ctx
, res
[0]->dn
);
156 search the sam for a dom_sid attribute in exactly 1 record
158 struct dom_sid
*samdb_search_dom_sid(struct ldb_context
*sam_ldb
,
160 struct ldb_dn
*basedn
,
161 const char *attr_name
,
162 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
166 struct ldb_message
**res
;
167 const char *attrs
[2] = { NULL
, NULL
};
170 attrs
[0] = attr_name
;
172 va_start(ap
, format
);
173 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
176 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
177 attr_name
, format
, count
));
183 sid
= samdb_result_dom_sid(mem_ctx
, res
[0], attr_name
);
189 return the count of the number of records in the sam matching the query
191 int samdb_search_count(struct ldb_context
*sam_ldb
,
192 struct ldb_dn
*basedn
,
193 const char *format
, ...) _PRINTF_ATTRIBUTE(3,4)
196 struct ldb_message
**res
;
197 const char *attrs
[] = { NULL
};
199 TALLOC_CTX
*tmp_ctx
= talloc_new(sam_ldb
);
201 va_start(ap
, format
);
202 ret
= gendb_search_v(sam_ldb
, tmp_ctx
, basedn
, &res
, attrs
, format
, ap
);
204 talloc_free(tmp_ctx
);
211 search the sam for a single integer attribute in exactly 1 record
213 unsigned int samdb_search_uint(struct ldb_context
*sam_ldb
,
215 unsigned int default_value
,
216 struct ldb_dn
*basedn
,
217 const char *attr_name
,
218 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
222 struct ldb_message
**res
;
223 const char *attrs
[2] = { NULL
, NULL
};
225 attrs
[0] = attr_name
;
227 va_start(ap
, format
);
228 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
232 return default_value
;
235 return samdb_result_uint(res
[0], attr_name
, default_value
);
239 search the sam for a single signed 64 bit integer attribute in exactly 1 record
241 int64_t samdb_search_int64(struct ldb_context
*sam_ldb
,
243 int64_t default_value
,
244 struct ldb_dn
*basedn
,
245 const char *attr_name
,
246 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
250 struct ldb_message
**res
;
251 const char *attrs
[2] = { NULL
, NULL
};
253 attrs
[0] = attr_name
;
255 va_start(ap
, format
);
256 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
260 return default_value
;
263 return samdb_result_int64(res
[0], attr_name
, default_value
);
267 search the sam for multipe records each giving a single string attribute
268 return the number of matches, or -1 on error
270 int samdb_search_string_multiple(struct ldb_context
*sam_ldb
,
272 struct ldb_dn
*basedn
,
274 const char *attr_name
,
275 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
279 const char *attrs
[2] = { NULL
, NULL
};
280 struct ldb_message
**res
= NULL
;
282 attrs
[0] = attr_name
;
284 va_start(ap
, format
);
285 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
292 /* make sure its single valued */
293 for (i
=0;i
<count
;i
++) {
294 if (res
[i
]->num_elements
!= 1) {
295 DEBUG(1,("samdb: search for %s %s not single valued\n",
302 *strs
= talloc_array(mem_ctx
, const char *, count
+1);
308 for (i
=0;i
<count
;i
++) {
309 (*strs
)[i
] = samdb_result_string(res
[i
], attr_name
, NULL
);
311 (*strs
)[count
] = NULL
;
317 pull a uint from a result set.
319 unsigned int samdb_result_uint(const struct ldb_message
*msg
, const char *attr
, unsigned int default_value
)
321 return ldb_msg_find_attr_as_uint(msg
, attr
, default_value
);
325 pull a (signed) int64 from a result set.
327 int64_t samdb_result_int64(const struct ldb_message
*msg
, const char *attr
, int64_t default_value
)
329 return ldb_msg_find_attr_as_int64(msg
, attr
, default_value
);
333 pull a string from a result set.
335 const char *samdb_result_string(const struct ldb_message
*msg
, const char *attr
,
336 const char *default_value
)
338 return ldb_msg_find_attr_as_string(msg
, attr
, default_value
);
341 struct ldb_dn
*samdb_result_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
342 const char *attr
, struct ldb_dn
*default_value
)
344 struct ldb_dn
*ret_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, msg
, attr
);
346 return default_value
;
352 pull a rid from a objectSid in a result set.
354 uint32_t samdb_result_rid_from_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
355 const char *attr
, uint32_t default_value
)
360 sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
362 return default_value
;
364 rid
= sid
->sub_auths
[sid
->num_auths
-1];
370 pull a dom_sid structure from a objectSid in a result set.
372 struct dom_sid
*samdb_result_dom_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
375 const struct ldb_val
*v
;
377 enum ndr_err_code ndr_err
;
378 v
= ldb_msg_find_ldb_val(msg
, attr
);
382 sid
= talloc(mem_ctx
, struct dom_sid
);
386 ndr_err
= ndr_pull_struct_blob(v
, sid
, NULL
, sid
,
387 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
388 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
396 pull a guid structure from a objectGUID in a result set.
398 struct GUID
samdb_result_guid(const struct ldb_message
*msg
, const char *attr
)
400 const struct ldb_val
*v
;
404 v
= ldb_msg_find_ldb_val(msg
, attr
);
405 if (!v
) return GUID_zero();
407 status
= GUID_from_ndr_blob(v
, &guid
);
408 if (!NT_STATUS_IS_OK(status
)) {
416 pull a sid prefix from a objectSid in a result set.
417 this is used to find the domain sid for a user
419 struct dom_sid
*samdb_result_sid_prefix(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
422 struct dom_sid
*sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
423 if (!sid
|| sid
->num_auths
< 1) return NULL
;
429 pull a NTTIME in a result set.
431 NTTIME
samdb_result_nttime(const struct ldb_message
*msg
, const char *attr
,
432 NTTIME default_value
)
434 return ldb_msg_find_attr_as_uint64(msg
, attr
, default_value
);
438 * Windows stores 0 for lastLogoff.
439 * But when a MS DC return the lastLogoff (as Logoff Time)
440 * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
441 * cause windows 2008 and newer version to fail for SMB requests
443 NTTIME
samdb_result_last_logoff(const struct ldb_message
*msg
)
445 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "lastLogoff",0);
448 ret
= 0x7FFFFFFFFFFFFFFFULL
;
454 * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
455 * indicate an account doesn't expire.
457 * When Windows initially creates an account, it sets
458 * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF). However,
459 * when changing from an account having a specific expiration date to
460 * that account never expiring, it sets accountExpires = 0.
462 * Consolidate that logic here to allow clearer logic for account expiry in
463 * the rest of the code.
465 NTTIME
samdb_result_account_expires(const struct ldb_message
*msg
)
467 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "accountExpires",
471 ret
= 0x7FFFFFFFFFFFFFFFULL
;
477 pull a uint64_t from a result set.
479 uint64_t samdb_result_uint64(const struct ldb_message
*msg
, const char *attr
,
480 uint64_t default_value
)
482 return ldb_msg_find_attr_as_uint64(msg
, attr
, default_value
);
487 construct the allow_password_change field from the PwdLastSet attribute and the
488 domain password settings
490 NTTIME
samdb_result_allow_password_change(struct ldb_context
*sam_ldb
,
492 struct ldb_dn
*domain_dn
,
493 struct ldb_message
*msg
,
496 uint64_t attr_time
= samdb_result_uint64(msg
, attr
, 0);
499 if (attr_time
== 0) {
503 minPwdAge
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
, "minPwdAge", NULL
);
505 /* yes, this is a -= not a += as minPwdAge is stored as the negative
506 of the number of 100-nano-seconds */
507 attr_time
-= minPwdAge
;
513 construct the force_password_change field from the PwdLastSet
514 attribute, the userAccountControl and the domain password settings
516 NTTIME
samdb_result_force_password_change(struct ldb_context
*sam_ldb
,
518 struct ldb_dn
*domain_dn
,
519 struct ldb_message
*msg
)
521 uint64_t attr_time
= samdb_result_uint64(msg
, "pwdLastSet", 0);
522 uint32_t userAccountControl
= samdb_result_uint64(msg
, "userAccountControl", 0);
525 /* Machine accounts don't expire, and there is a flag for 'no expiry' */
526 if (!(userAccountControl
& UF_NORMAL_ACCOUNT
)
527 || (userAccountControl
& UF_DONT_EXPIRE_PASSWD
)) {
528 return 0x7FFFFFFFFFFFFFFFULL
;
531 if (attr_time
== 0) {
535 maxPwdAge
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
, "maxPwdAge", NULL
);
536 if (maxPwdAge
== 0) {
537 return 0x7FFFFFFFFFFFFFFFULL
;
539 attr_time
-= maxPwdAge
;
546 pull a samr_Password structutre from a result set.
548 struct samr_Password
*samdb_result_hash(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
, const char *attr
)
550 struct samr_Password
*hash
= NULL
;
551 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
552 if (val
&& (val
->length
>= sizeof(hash
->hash
))) {
553 hash
= talloc(mem_ctx
, struct samr_Password
);
554 memcpy(hash
->hash
, val
->data
, MIN(val
->length
, sizeof(hash
->hash
)));
560 pull an array of samr_Password structures from a result set.
562 unsigned int samdb_result_hashes(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
563 const char *attr
, struct samr_Password
**hashes
)
565 unsigned int count
, i
;
566 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
572 count
= val
->length
/ 16;
577 *hashes
= talloc_array(mem_ctx
, struct samr_Password
, count
);
582 for (i
=0;i
<count
;i
++) {
583 memcpy((*hashes
)[i
].hash
, (i
*16)+(char *)val
->data
, 16);
589 NTSTATUS
samdb_result_passwords(TALLOC_CTX
*mem_ctx
, struct loadparm_context
*lp_ctx
, struct ldb_message
*msg
,
590 struct samr_Password
**lm_pwd
, struct samr_Password
**nt_pwd
)
592 struct samr_Password
*lmPwdHash
, *ntPwdHash
;
595 num_nt
= samdb_result_hashes(mem_ctx
, msg
, "unicodePwd", &ntPwdHash
);
598 } else if (num_nt
> 1) {
599 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
601 *nt_pwd
= &ntPwdHash
[0];
605 /* Ensure that if we have turned off LM
606 * authentication, that we never use the LM hash, even
608 if (lp_lanman_auth(lp_ctx
)) {
610 num_lm
= samdb_result_hashes(mem_ctx
, msg
, "dBCSPwd", &lmPwdHash
);
613 } else if (num_lm
> 1) {
614 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
616 *lm_pwd
= &lmPwdHash
[0];
626 pull a samr_LogonHours structutre from a result set.
628 struct samr_LogonHours
samdb_result_logon_hours(TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
, const char *attr
)
630 struct samr_LogonHours hours
;
631 const int units_per_week
= 168;
632 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
634 hours
.bits
= talloc_array(mem_ctx
, uint8_t, units_per_week
);
638 hours
.units_per_week
= units_per_week
;
639 memset(hours
.bits
, 0xFF, units_per_week
);
641 memcpy(hours
.bits
, val
->data
, MIN(val
->length
, units_per_week
));
647 pull a set of account_flags from a result set.
649 This requires that the attributes:
654 uint32_t samdb_result_acct_flags(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
655 struct ldb_message
*msg
, struct ldb_dn
*domain_dn
)
657 uint32_t userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
658 uint32_t acct_flags
= ds_uf2acb(userAccountControl
);
659 NTTIME must_change_time
;
662 must_change_time
= samdb_result_force_password_change(sam_ctx
, mem_ctx
,
665 /* Test account expire time */
666 unix_to_nt_time(&now
, time(NULL
));
667 /* check for expired password */
668 if (must_change_time
< now
) {
669 acct_flags
|= ACB_PW_EXPIRED
;
674 struct lsa_BinaryString
samdb_result_parameters(TALLOC_CTX
*mem_ctx
,
675 struct ldb_message
*msg
,
678 struct lsa_BinaryString s
;
679 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
687 s
.array
= talloc_array(mem_ctx
, uint16_t, val
->length
/2);
691 s
.length
= s
.size
= val
->length
;
692 memcpy(s
.array
, val
->data
, val
->length
);
697 /* Find an attribute, with a particular value */
699 /* The current callers of this function expect a very specific
700 * behaviour: In particular, objectClass subclass equivilance is not
701 * wanted. This means that we should not lookup the schema for the
702 * comparison function */
703 struct ldb_message_element
*samdb_find_attribute(struct ldb_context
*ldb
,
704 const struct ldb_message
*msg
,
705 const char *name
, const char *value
)
708 struct ldb_message_element
*el
= ldb_msg_find_element(msg
, name
);
714 for (i
=0;i
<el
->num_values
;i
++) {
715 if (ldb_attr_cmp(value
, (char *)el
->values
[i
].data
) == 0) {
723 int samdb_find_or_add_value(struct ldb_context
*ldb
, struct ldb_message
*msg
, const char *name
, const char *set_value
)
725 if (samdb_find_attribute(ldb
, msg
, name
, set_value
) == NULL
) {
726 return samdb_msg_add_string(ldb
, msg
, msg
, name
, set_value
);
731 int samdb_find_or_add_attribute(struct ldb_context
*ldb
, struct ldb_message
*msg
, const char *name
, const char *set_value
)
733 struct ldb_message_element
*el
;
735 el
= ldb_msg_find_element(msg
, name
);
740 return samdb_msg_add_string(ldb
, msg
, msg
, name
, set_value
);
746 add a string element to a message
748 int samdb_msg_add_string(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
749 const char *attr_name
, const char *str
)
751 char *s
= talloc_strdup(mem_ctx
, str
);
752 char *a
= talloc_strdup(mem_ctx
, attr_name
);
753 if (s
== NULL
|| a
== NULL
) {
754 return LDB_ERR_OPERATIONS_ERROR
;
756 return ldb_msg_add_string(msg
, a
, s
);
760 add a dom_sid element to a message
762 int samdb_msg_add_dom_sid(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
763 const char *attr_name
, struct dom_sid
*sid
)
766 enum ndr_err_code ndr_err
;
768 ndr_err
= ndr_push_struct_blob(&v
, mem_ctx
,
769 lp_iconv_convenience(ldb_get_opaque(sam_ldb
, "loadparm")),
771 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
772 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
773 return LDB_ERR_OPERATIONS_ERROR
;
775 return ldb_msg_add_value(msg
, attr_name
, &v
, NULL
);
780 add a delete element operation to a message
782 int samdb_msg_add_delete(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
783 const char *attr_name
)
785 /* we use an empty replace rather than a delete, as it allows for
786 dsdb_replace() to be used everywhere */
787 return ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_REPLACE
, NULL
);
791 add a add attribute value to a message
793 int samdb_msg_add_addval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
794 const char *attr_name
, const char *value
)
796 struct ldb_message_element
*el
;
799 a
= talloc_strdup(mem_ctx
, attr_name
);
801 return LDB_ERR_OPERATIONS_ERROR
;
802 v
= talloc_strdup(mem_ctx
, value
);
804 return LDB_ERR_OPERATIONS_ERROR
;
805 ret
= ldb_msg_add_string(msg
, a
, v
);
808 el
= ldb_msg_find_element(msg
, a
);
810 return LDB_ERR_OPERATIONS_ERROR
;
811 el
->flags
= LDB_FLAG_MOD_ADD
;
816 add a delete attribute value to a message
818 int samdb_msg_add_delval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
819 const char *attr_name
, const char *value
)
821 struct ldb_message_element
*el
;
824 a
= talloc_strdup(mem_ctx
, attr_name
);
826 return LDB_ERR_OPERATIONS_ERROR
;
827 v
= talloc_strdup(mem_ctx
, value
);
829 return LDB_ERR_OPERATIONS_ERROR
;
830 ret
= ldb_msg_add_string(msg
, a
, v
);
833 el
= ldb_msg_find_element(msg
, a
);
835 return LDB_ERR_OPERATIONS_ERROR
;
836 el
->flags
= LDB_FLAG_MOD_DELETE
;
841 add a int element to a message
843 int samdb_msg_add_int(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
844 const char *attr_name
, int v
)
846 const char *s
= talloc_asprintf(mem_ctx
, "%d", v
);
847 return samdb_msg_add_string(sam_ldb
, mem_ctx
, msg
, attr_name
, s
);
851 add a unsigned int element to a message
853 int samdb_msg_add_uint(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
854 const char *attr_name
, unsigned int v
)
856 return samdb_msg_add_int(sam_ldb
, mem_ctx
, msg
, attr_name
, (int)v
);
860 add a (signed) int64_t element to a message
862 int samdb_msg_add_int64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
863 const char *attr_name
, int64_t v
)
865 const char *s
= talloc_asprintf(mem_ctx
, "%lld", (long long)v
);
866 return samdb_msg_add_string(sam_ldb
, mem_ctx
, msg
, attr_name
, s
);
870 add a uint64_t element to a message
872 int samdb_msg_add_uint64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
873 const char *attr_name
, uint64_t v
)
875 return samdb_msg_add_int64(sam_ldb
, mem_ctx
, msg
, attr_name
, (int64_t)v
);
879 add a samr_Password element to a message
881 int samdb_msg_add_hash(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
882 const char *attr_name
, struct samr_Password
*hash
)
885 val
.data
= talloc_memdup(mem_ctx
, hash
->hash
, 16);
887 return LDB_ERR_OPERATIONS_ERROR
;
890 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
894 add a samr_Password array to a message
896 int samdb_msg_add_hashes(TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
897 const char *attr_name
, struct samr_Password
*hashes
,
902 val
.data
= talloc_array_size(mem_ctx
, 16, count
);
903 val
.length
= count
*16;
905 return LDB_ERR_OPERATIONS_ERROR
;
907 for (i
=0;i
<count
;i
++) {
908 memcpy(i
*16 + (char *)val
.data
, hashes
[i
].hash
, 16);
910 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
914 add a acct_flags element to a message
916 int samdb_msg_add_acct_flags(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
917 const char *attr_name
, uint32_t v
)
919 return samdb_msg_add_uint(sam_ldb
, mem_ctx
, msg
, attr_name
, ds_acb2uf(v
));
923 add a logon_hours element to a message
925 int samdb_msg_add_logon_hours(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
926 const char *attr_name
, struct samr_LogonHours
*hours
)
929 val
.length
= hours
->units_per_week
/ 8;
930 val
.data
= hours
->bits
;
931 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
935 add a parameters element to a message
937 int samdb_msg_add_parameters(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
938 const char *attr_name
, struct lsa_BinaryString
*parameters
)
941 val
.length
= parameters
->length
;
942 val
.data
= (uint8_t *)parameters
->array
;
943 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
946 add a general value element to a message
948 int samdb_msg_add_value(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
949 const char *attr_name
, const struct ldb_val
*val
)
951 return ldb_msg_add_value(msg
, attr_name
, val
, NULL
);
955 sets a general value element to a message
957 int samdb_msg_set_value(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
958 const char *attr_name
, const struct ldb_val
*val
)
960 struct ldb_message_element
*el
;
962 el
= ldb_msg_find_element(msg
, attr_name
);
966 return ldb_msg_add_value(msg
, attr_name
, val
, NULL
);
970 set a string element in a message
972 int samdb_msg_set_string(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
973 const char *attr_name
, const char *str
)
975 struct ldb_message_element
*el
;
977 el
= ldb_msg_find_element(msg
, attr_name
);
981 return samdb_msg_add_string(sam_ldb
, mem_ctx
, msg
, attr_name
, str
);
985 * Handle ldb_request in transaction
987 static int dsdb_autotransaction_request(struct ldb_context
*sam_ldb
,
988 struct ldb_request
*req
)
992 ret
= ldb_transaction_start(sam_ldb
);
993 if (ret
!= LDB_SUCCESS
) {
997 ret
= ldb_request(sam_ldb
, req
);
998 if (ret
== LDB_SUCCESS
) {
999 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1002 if (ret
== LDB_SUCCESS
) {
1003 return ldb_transaction_commit(sam_ldb
);
1005 ldb_transaction_cancel(sam_ldb
);
1011 return a default security descriptor
1013 struct security_descriptor
*samdb_default_security_descriptor(TALLOC_CTX
*mem_ctx
)
1015 struct security_descriptor
*sd
;
1017 sd
= security_descriptor_initialise(mem_ctx
);
1022 struct ldb_dn
*samdb_base_dn(struct ldb_context
*sam_ctx
)
1024 return ldb_get_default_basedn(sam_ctx
);
1027 struct ldb_dn
*samdb_config_dn(struct ldb_context
*sam_ctx
)
1029 return ldb_get_config_basedn(sam_ctx
);
1032 struct ldb_dn
*samdb_schema_dn(struct ldb_context
*sam_ctx
)
1034 return ldb_get_schema_basedn(sam_ctx
);
1037 struct ldb_dn
*samdb_aggregate_schema_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1039 struct ldb_dn
*schema_dn
= ldb_get_schema_basedn(sam_ctx
);
1040 struct ldb_dn
*aggregate_dn
;
1045 aggregate_dn
= ldb_dn_copy(mem_ctx
, schema_dn
);
1046 if (!aggregate_dn
) {
1049 if (!ldb_dn_add_child_fmt(aggregate_dn
, "CN=Aggregate")) {
1052 return aggregate_dn
;
1055 struct ldb_dn
*samdb_root_dn(struct ldb_context
*sam_ctx
)
1057 return ldb_get_root_basedn(sam_ctx
);
1060 struct ldb_dn
*samdb_partitions_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1062 struct ldb_dn
*new_dn
;
1064 new_dn
= ldb_dn_copy(mem_ctx
, samdb_config_dn(sam_ctx
));
1065 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Partitions")) {
1066 talloc_free(new_dn
);
1072 struct ldb_dn
*samdb_sites_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1074 struct ldb_dn
*new_dn
;
1076 new_dn
= ldb_dn_copy(mem_ctx
, samdb_config_dn(sam_ctx
));
1077 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Sites")) {
1078 talloc_free(new_dn
);
1085 work out the domain sid for the current open ldb
1087 const struct dom_sid
*samdb_domain_sid(struct ldb_context
*ldb
)
1089 TALLOC_CTX
*tmp_ctx
;
1090 const struct dom_sid
*domain_sid
;
1091 const char *attrs
[] = {
1095 struct ldb_result
*res
;
1098 /* see if we have a cached copy */
1099 domain_sid
= (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1104 tmp_ctx
= talloc_new(ldb
);
1105 if (tmp_ctx
== NULL
) {
1109 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_get_default_basedn(ldb
), LDB_SCOPE_BASE
, attrs
, "objectSid=*");
1111 if (ret
!= LDB_SUCCESS
) {
1115 if (res
->count
!= 1) {
1119 domain_sid
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSid");
1120 if (domain_sid
== NULL
) {
1124 /* cache the domain_sid in the ldb */
1125 if (ldb_set_opaque(ldb
, "cache.domain_sid", discard_const_p(struct dom_sid
, domain_sid
)) != LDB_SUCCESS
) {
1129 talloc_steal(ldb
, domain_sid
);
1130 talloc_free(tmp_ctx
);
1135 talloc_free(tmp_ctx
);
1140 get domain sid from cache
1142 const struct dom_sid
*samdb_domain_sid_cache_only(struct ldb_context
*ldb
)
1144 return (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1147 bool samdb_set_domain_sid(struct ldb_context
*ldb
, const struct dom_sid
*dom_sid_in
)
1149 TALLOC_CTX
*tmp_ctx
;
1150 struct dom_sid
*dom_sid_new
;
1151 struct dom_sid
*dom_sid_old
;
1153 /* see if we have a cached copy */
1154 dom_sid_old
= talloc_get_type(ldb_get_opaque(ldb
,
1155 "cache.domain_sid"), struct dom_sid
);
1157 tmp_ctx
= talloc_new(ldb
);
1158 if (tmp_ctx
== NULL
) {
1162 dom_sid_new
= dom_sid_dup(tmp_ctx
, dom_sid_in
);
1167 /* cache the domain_sid in the ldb */
1168 if (ldb_set_opaque(ldb
, "cache.domain_sid", dom_sid_new
) != LDB_SUCCESS
) {
1172 talloc_steal(ldb
, dom_sid_new
);
1173 talloc_free(tmp_ctx
);
1174 talloc_free(dom_sid_old
);
1179 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1180 talloc_free(tmp_ctx
);
1184 /* Obtain the short name of the flexible single master operator
1185 * (FSMO), such as the PDC Emulator */
1186 const char *samdb_result_fsmo_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
1189 /* Format is cn=NTDS Settings,cn=<NETBIOS name of FSMO>,.... */
1190 struct ldb_dn
*fsmo_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, msg
, attr
);
1191 const struct ldb_val
*val
= ldb_dn_get_component_val(fsmo_dn
, 1);
1192 const char *name
= ldb_dn_get_component_name(fsmo_dn
, 1);
1194 if (!name
|| (ldb_attr_cmp(name
, "cn") != 0)) {
1195 /* Ensure this matches the format. This gives us a
1196 * bit more confidence that a 'cn' value will be a
1201 return (char *)val
->data
;
1207 work out the ntds settings dn for the current open ldb
1209 struct ldb_dn
*samdb_ntds_settings_dn(struct ldb_context
*ldb
)
1211 TALLOC_CTX
*tmp_ctx
;
1212 const char *root_attrs
[] = { "dsServiceName", NULL
};
1214 struct ldb_result
*root_res
;
1215 struct ldb_dn
*settings_dn
;
1217 /* see if we have a cached copy */
1218 settings_dn
= (struct ldb_dn
*)ldb_get_opaque(ldb
, "cache.settings_dn");
1223 tmp_ctx
= talloc_new(ldb
);
1224 if (tmp_ctx
== NULL
) {
1228 ret
= ldb_search(ldb
, tmp_ctx
, &root_res
, ldb_dn_new(tmp_ctx
, ldb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
1230 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1231 ldb_errstring(ldb
)));
1235 if (root_res
->count
!= 1) {
1239 settings_dn
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, root_res
->msgs
[0], "dsServiceName");
1241 /* cache the domain_sid in the ldb */
1242 if (ldb_set_opaque(ldb
, "cache.settings_dn", settings_dn
) != LDB_SUCCESS
) {
1246 talloc_steal(ldb
, settings_dn
);
1247 talloc_free(tmp_ctx
);
1252 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1253 talloc_free(tmp_ctx
);
1258 work out the ntds settings invocationId for the current open ldb
1260 const struct GUID
*samdb_ntds_invocation_id(struct ldb_context
*ldb
)
1262 TALLOC_CTX
*tmp_ctx
;
1263 const char *attrs
[] = { "invocationId", NULL
};
1265 struct ldb_result
*res
;
1266 struct GUID
*invocation_id
;
1268 /* see if we have a cached copy */
1269 invocation_id
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.invocation_id");
1270 if (invocation_id
) {
1271 return invocation_id
;
1274 tmp_ctx
= talloc_new(ldb
);
1275 if (tmp_ctx
== NULL
) {
1279 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
1284 if (res
->count
!= 1) {
1288 invocation_id
= talloc(tmp_ctx
, struct GUID
);
1289 if (!invocation_id
) {
1293 *invocation_id
= samdb_result_guid(res
->msgs
[0], "invocationId");
1295 /* cache the domain_sid in the ldb */
1296 if (ldb_set_opaque(ldb
, "cache.invocation_id", invocation_id
) != LDB_SUCCESS
) {
1300 talloc_steal(ldb
, invocation_id
);
1301 talloc_free(tmp_ctx
);
1303 return invocation_id
;
1306 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1307 talloc_free(tmp_ctx
);
1311 bool samdb_set_ntds_invocation_id(struct ldb_context
*ldb
, const struct GUID
*invocation_id_in
)
1313 TALLOC_CTX
*tmp_ctx
;
1314 struct GUID
*invocation_id_new
;
1315 struct GUID
*invocation_id_old
;
1317 /* see if we have a cached copy */
1318 invocation_id_old
= (struct GUID
*)ldb_get_opaque(ldb
,
1319 "cache.invocation_id");
1321 tmp_ctx
= talloc_new(ldb
);
1322 if (tmp_ctx
== NULL
) {
1326 invocation_id_new
= talloc(tmp_ctx
, struct GUID
);
1327 if (!invocation_id_new
) {
1331 *invocation_id_new
= *invocation_id_in
;
1333 /* cache the domain_sid in the ldb */
1334 if (ldb_set_opaque(ldb
, "cache.invocation_id", invocation_id_new
) != LDB_SUCCESS
) {
1338 talloc_steal(ldb
, invocation_id_new
);
1339 talloc_free(tmp_ctx
);
1340 talloc_free(invocation_id_old
);
1345 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1346 talloc_free(tmp_ctx
);
1351 work out the ntds settings objectGUID for the current open ldb
1353 const struct GUID
*samdb_ntds_objectGUID(struct ldb_context
*ldb
)
1355 TALLOC_CTX
*tmp_ctx
;
1356 const char *attrs
[] = { "objectGUID", NULL
};
1358 struct ldb_result
*res
;
1359 struct GUID
*ntds_guid
;
1361 /* see if we have a cached copy */
1362 ntds_guid
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.ntds_guid");
1367 tmp_ctx
= talloc_new(ldb
);
1368 if (tmp_ctx
== NULL
) {
1372 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
1377 if (res
->count
!= 1) {
1381 ntds_guid
= talloc(tmp_ctx
, struct GUID
);
1386 *ntds_guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
1388 /* cache the domain_sid in the ldb */
1389 if (ldb_set_opaque(ldb
, "cache.ntds_guid", ntds_guid
) != LDB_SUCCESS
) {
1393 talloc_steal(ldb
, ntds_guid
);
1394 talloc_free(tmp_ctx
);
1399 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1400 talloc_free(tmp_ctx
);
1404 bool samdb_set_ntds_objectGUID(struct ldb_context
*ldb
, const struct GUID
*ntds_guid_in
)
1406 TALLOC_CTX
*tmp_ctx
;
1407 struct GUID
*ntds_guid_new
;
1408 struct GUID
*ntds_guid_old
;
1410 /* see if we have a cached copy */
1411 ntds_guid_old
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.ntds_guid");
1413 tmp_ctx
= talloc_new(ldb
);
1414 if (tmp_ctx
== NULL
) {
1418 ntds_guid_new
= talloc(tmp_ctx
, struct GUID
);
1419 if (!ntds_guid_new
) {
1423 *ntds_guid_new
= *ntds_guid_in
;
1425 /* cache the domain_sid in the ldb */
1426 if (ldb_set_opaque(ldb
, "cache.ntds_guid", ntds_guid_new
) != LDB_SUCCESS
) {
1430 talloc_steal(ldb
, ntds_guid_new
);
1431 talloc_free(tmp_ctx
);
1432 talloc_free(ntds_guid_old
);
1437 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1438 talloc_free(tmp_ctx
);
1443 work out the server dn for the current open ldb
1445 struct ldb_dn
*samdb_server_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1447 return ldb_dn_get_parent(mem_ctx
, samdb_ntds_settings_dn(ldb
));
1451 work out the server dn for the current open ldb
1453 struct ldb_dn
*samdb_server_site_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1455 struct ldb_dn
*server_dn
;
1456 struct ldb_dn
*servers_dn
;
1457 struct ldb_dn
*server_site_dn
;
1459 /* TODO: there must be a saner way to do this!! */
1461 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1462 if (!server_dn
) return NULL
;
1464 servers_dn
= ldb_dn_get_parent(mem_ctx
, server_dn
);
1465 if (!servers_dn
) return NULL
;
1467 server_site_dn
= ldb_dn_get_parent(mem_ctx
, servers_dn
);
1469 talloc_free(server_dn
);
1470 return server_site_dn
;
1474 find a 'reference' DN that points at another object
1475 (eg. serverReference, rIDManagerReference etc)
1477 int samdb_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*base
,
1478 const char *attribute
, struct ldb_dn
**dn
)
1480 const char *attrs
[2];
1481 struct ldb_result
*res
;
1484 attrs
[0] = attribute
;
1487 ret
= ldb_search(ldb
, mem_ctx
, &res
, base
, LDB_SCOPE_BASE
, attrs
, NULL
);
1488 if (ret
!= LDB_SUCCESS
) {
1491 if (res
->count
!= 1) {
1493 return LDB_ERR_NO_SUCH_OBJECT
;
1496 *dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, res
->msgs
[0], attribute
);
1499 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
1507 find our machine account via the serverReference attribute in the
1510 int samdb_server_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1512 struct ldb_dn
*server_dn
;
1515 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1516 if (server_dn
== NULL
) {
1517 return LDB_ERR_NO_SUCH_OBJECT
;
1520 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_dn
, "serverReference", dn
);
1521 talloc_free(server_dn
);
1527 find the RID Manager$ DN via the rIDManagerReference attribute in the
1530 int samdb_rid_manager_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1532 return samdb_reference_dn(ldb
, mem_ctx
, samdb_base_dn(ldb
), "rIDManagerReference", dn
);
1536 find the RID Set DN via the rIDSetReferences attribute in our
1539 int samdb_rid_set_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1541 struct ldb_dn
*server_ref_dn
;
1544 ret
= samdb_server_reference_dn(ldb
, mem_ctx
, &server_ref_dn
);
1545 if (ret
!= LDB_SUCCESS
) {
1548 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_ref_dn
, "rIDSetReferences", dn
);
1549 talloc_free(server_ref_dn
);
1553 const char *samdb_server_site_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1555 const struct ldb_val
*val
= ldb_dn_get_rdn_val(samdb_server_site_dn(ldb
, mem_ctx
));
1558 return (const char *) val
->data
;
1564 work out if we are the PDC for the domain of the current open ldb
1566 bool samdb_is_pdc(struct ldb_context
*ldb
)
1568 const char *dom_attrs
[] = { "fSMORoleOwner", NULL
};
1570 struct ldb_result
*dom_res
;
1571 TALLOC_CTX
*tmp_ctx
;
1575 tmp_ctx
= talloc_new(ldb
);
1576 if (tmp_ctx
== NULL
) {
1577 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1581 ret
= ldb_search(ldb
, tmp_ctx
, &dom_res
, ldb_get_default_basedn(ldb
), LDB_SCOPE_BASE
, dom_attrs
, NULL
);
1583 DEBUG(1,("Searching for fSMORoleOwner in %s failed: %s\n",
1584 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)),
1585 ldb_errstring(ldb
)));
1588 if (dom_res
->count
!= 1) {
1592 pdc
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, dom_res
->msgs
[0], "fSMORoleOwner");
1594 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb
), pdc
) == 0) {
1600 talloc_free(tmp_ctx
);
1605 DEBUG(1,("Failed to find if we are the PDC for this ldb\n"));
1606 talloc_free(tmp_ctx
);
1611 work out if we are a Global Catalog server for the domain of the current open ldb
1613 bool samdb_is_gc(struct ldb_context
*ldb
)
1615 const char *attrs
[] = { "options", NULL
};
1617 struct ldb_result
*res
;
1618 TALLOC_CTX
*tmp_ctx
;
1620 tmp_ctx
= talloc_new(ldb
);
1621 if (tmp_ctx
== NULL
) {
1622 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1626 /* Query cn=ntds settings,.... */
1627 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
1629 talloc_free(tmp_ctx
);
1632 if (res
->count
!= 1) {
1633 talloc_free(tmp_ctx
);
1637 options
= ldb_msg_find_attr_as_int(res
->msgs
[0], "options", 0);
1638 talloc_free(tmp_ctx
);
1640 /* if options attribute has the 0x00000001 flag set, then enable the global catlog */
1641 if (options
& 0x000000001) {
1647 /* Find a domain object in the parents of a particular DN. */
1648 int samdb_search_for_parent_domain(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
1649 struct ldb_dn
**parent_dn
, const char **errstring
)
1651 TALLOC_CTX
*local_ctx
;
1652 struct ldb_dn
*sdn
= dn
;
1653 struct ldb_result
*res
= NULL
;
1655 const char *attrs
[] = { NULL
};
1657 local_ctx
= talloc_new(mem_ctx
);
1658 if (local_ctx
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
1660 while ((sdn
= ldb_dn_get_parent(local_ctx
, sdn
))) {
1661 ret
= ldb_search(ldb
, local_ctx
, &res
, sdn
, LDB_SCOPE_BASE
, attrs
,
1662 "(|(objectClass=domain)(objectClass=builtinDomain))");
1663 if (ret
== LDB_SUCCESS
) {
1664 if (res
->count
== 1) {
1672 if (ret
!= LDB_SUCCESS
) {
1673 *errstring
= talloc_asprintf(mem_ctx
, "Error searching for parent domain of %s, failed searching for %s: %s",
1674 ldb_dn_get_linearized(dn
),
1675 ldb_dn_get_linearized(sdn
),
1676 ldb_errstring(ldb
));
1677 talloc_free(local_ctx
);
1680 if (res
->count
!= 1) {
1681 *errstring
= talloc_asprintf(mem_ctx
, "Invalid dn (%s), not child of a domain object",
1682 ldb_dn_get_linearized(dn
));
1683 DEBUG(0,(__location__
": %s\n", *errstring
));
1684 talloc_free(local_ctx
);
1685 return LDB_ERR_CONSTRAINT_VIOLATION
;
1688 *parent_dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
1689 talloc_free(local_ctx
);
1695 * Performs checks on a user password (plaintext UNIX format - attribute
1696 * "password"). The remaining parameters have to be extracted from the domain
1699 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1701 enum samr_ValidationStatus
samdb_check_password(const DATA_BLOB
*password
,
1702 const uint32_t pwdProperties
,
1703 const uint32_t minPwdLength
)
1705 /* checks if the "minPwdLength" property is satisfied */
1706 if (minPwdLength
> password
->length
)
1707 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT
;
1709 /* checks the password complexity */
1710 if (((pwdProperties
& DOMAIN_PASSWORD_COMPLEX
) != 0)
1711 && (password
->data
!= NULL
)
1712 && (!check_password_quality((const char *) password
->data
)))
1713 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH
;
1715 return SAMR_VALIDATION_STATUS_SUCCESS
;
1719 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1720 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1721 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
1722 * gives some more informations if the changed failed.
1724 * The caller should have a LDB transaction wrapping this.
1726 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
1727 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
1728 * NT_STATUS_PASSWORD_RESTRICTION
1730 NTSTATUS
samdb_set_password(struct ldb_context
*ctx
, TALLOC_CTX
*mem_ctx
,
1731 struct ldb_dn
*user_dn
, struct ldb_dn
*domain_dn
,
1732 struct ldb_message
*mod
,
1733 const DATA_BLOB
*new_password
,
1734 struct samr_Password
*param_lmNewHash
,
1735 struct samr_Password
*param_ntNewHash
,
1737 enum samPwdChangeReason
*reject_reason
,
1738 struct samr_DomInfo1
**_dominfo
)
1740 const char * const user_attrs
[] = { "userAccountControl",
1743 "dBCSPwd", "unicodePwd",
1745 "pwdLastSet", NULL
};
1746 const char * const domain_attrs
[] = { "minPwdLength", "pwdProperties",
1748 "maxPwdAge", "minPwdAge", NULL
};
1750 uint32_t minPwdLength
, pwdProperties
, pwdHistoryLength
;
1751 int64_t maxPwdAge
, minPwdAge
;
1752 uint32_t userAccountControl
;
1753 struct samr_Password
*sambaLMPwdHistory
, *sambaNTPwdHistory
,
1754 *lmPwdHash
, *ntPwdHash
, *lmNewHash
, *ntNewHash
;
1755 struct samr_Password local_lmNewHash
, local_ntNewHash
;
1756 int sambaLMPwdHistory_len
, sambaNTPwdHistory_len
;
1757 struct dom_sid
*domain_sid
;
1758 struct ldb_message
**res
;
1761 time_t now
= time(NULL
);
1765 /* we need to know the time to compute password age */
1766 unix_to_nt_time(&now_nt
, now
);
1768 /* pull all the user parameters */
1769 count
= gendb_search_dn(ctx
, mem_ctx
, user_dn
, &res
, user_attrs
);
1771 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1773 userAccountControl
= samdb_result_uint(res
[0], "userAccountControl", 0);
1774 sambaLMPwdHistory_len
= samdb_result_hashes(mem_ctx
, res
[0],
1775 "lmPwdHistory", &sambaLMPwdHistory
);
1776 sambaNTPwdHistory_len
= samdb_result_hashes(mem_ctx
, res
[0],
1777 "ntPwdHistory", &sambaNTPwdHistory
);
1778 lmPwdHash
= samdb_result_hash(mem_ctx
, res
[0], "dBCSPwd");
1779 ntPwdHash
= samdb_result_hash(mem_ctx
, res
[0], "unicodePwd");
1780 pwdLastSet
= samdb_result_uint64(res
[0], "pwdLastSet", 0);
1782 /* Copy parameters */
1783 lmNewHash
= param_lmNewHash
;
1784 ntNewHash
= param_ntNewHash
;
1786 /* Only non-trust accounts have restrictions (possibly this
1787 * test is the wrong way around, but I like to be restrictive
1789 restrictions
= !(userAccountControl
& (UF_INTERDOMAIN_TRUST_ACCOUNT
1790 |UF_WORKSTATION_TRUST_ACCOUNT
1791 |UF_SERVER_TRUST_ACCOUNT
));
1793 if (domain_dn
!= NULL
) {
1794 /* pull the domain parameters */
1795 count
= gendb_search_dn(ctx
, mem_ctx
, domain_dn
, &res
,
1798 DEBUG(2, ("samdb_set_password: Domain DN %s is invalid, for user %s\n",
1799 ldb_dn_get_linearized(domain_dn
),
1800 ldb_dn_get_linearized(user_dn
)));
1801 return NT_STATUS_NO_SUCH_DOMAIN
;
1804 /* work out the domain sid, and pull the domain from there */
1805 domain_sid
= samdb_result_sid_prefix(mem_ctx
, res
[0],
1807 if (domain_sid
== NULL
) {
1808 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1811 count
= gendb_search(ctx
, mem_ctx
, NULL
, &res
, domain_attrs
,
1813 ldap_encode_ndr_dom_sid(mem_ctx
, domain_sid
));
1815 DEBUG(2, ("samdb_set_password: Could not find domain to match SID: %s, for user %s\n",
1816 dom_sid_string(mem_ctx
, domain_sid
),
1817 ldb_dn_get_linearized(user_dn
)));
1818 return NT_STATUS_NO_SUCH_DOMAIN
;
1822 minPwdLength
= samdb_result_uint(res
[0], "minPwdLength", 0);
1823 pwdProperties
= samdb_result_uint(res
[0], "pwdProperties", 0);
1824 pwdHistoryLength
= samdb_result_uint(res
[0], "pwdHistoryLength", 0);
1825 maxPwdAge
= samdb_result_int64(res
[0], "maxPwdAge", 0);
1826 minPwdAge
= samdb_result_int64(res
[0], "minPwdAge", 0);
1828 if ((userAccountControl
& UF_PASSWD_NOTREQD
) != 0) {
1829 /* see [MS-ADTS] 2.2.15 */
1833 if (_dominfo
!= NULL
) {
1834 struct samr_DomInfo1
*dominfo
;
1835 /* on failure we need to fill in the reject reasons */
1836 dominfo
= talloc(mem_ctx
, struct samr_DomInfo1
);
1837 if (dominfo
== NULL
) {
1838 return NT_STATUS_NO_MEMORY
;
1840 dominfo
->min_password_length
= minPwdLength
;
1841 dominfo
->password_properties
= pwdProperties
;
1842 dominfo
->password_history_length
= pwdHistoryLength
;
1843 dominfo
->max_password_age
= maxPwdAge
;
1844 dominfo
->min_password_age
= minPwdAge
;
1845 *_dominfo
= dominfo
;
1848 if ((restrictions
!= 0) && (new_password
!= 0)) {
1851 /* checks if the "minPwdLength" property is satisfied */
1852 if ((restrictions
!= 0)
1853 && (minPwdLength
> utf16_len_n(
1854 new_password
->data
, new_password
->length
)/2)) {
1855 if (reject_reason
) {
1856 *reject_reason
= SAM_PWD_CHANGE_PASSWORD_TOO_SHORT
;
1858 return NT_STATUS_PASSWORD_RESTRICTION
;
1861 /* Create the NT hash */
1862 mdfour(local_ntNewHash
.hash
, new_password
->data
,
1863 new_password
->length
);
1865 ntNewHash
= &local_ntNewHash
;
1867 /* Only check complexity if we can convert it at all. Assuming unconvertable passwords are 'strong' */
1868 if (convert_string_talloc_convenience(mem_ctx
,
1869 lp_iconv_convenience(ldb_get_opaque(ctx
, "loadparm")),
1871 new_password
->data
, new_password
->length
,
1872 (void **)&new_pass
, NULL
, false)) {
1874 /* checks the password complexity */
1875 if ((restrictions
!= 0)
1877 & DOMAIN_PASSWORD_COMPLEX
) != 0)
1878 && (!check_password_quality(new_pass
))) {
1879 if (reject_reason
) {
1880 *reject_reason
= SAM_PWD_CHANGE_NOT_COMPLEX
;
1882 return NT_STATUS_PASSWORD_RESTRICTION
;
1885 /* compute the new lm hashes (for checking history - case insenitivly!) */
1886 if (E_deshash(new_pass
, local_lmNewHash
.hash
)) {
1887 lmNewHash
= &local_lmNewHash
;
1892 if ((restrictions
!= 0) && user_change
) {
1893 /* are all password changes disallowed? */
1894 if ((pwdProperties
& DOMAIN_REFUSE_PASSWORD_CHANGE
) != 0) {
1895 if (reject_reason
) {
1896 *reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
1898 return NT_STATUS_PASSWORD_RESTRICTION
;
1901 /* can this user change the password? */
1902 if ((userAccountControl
& UF_PASSWD_CANT_CHANGE
) != 0) {
1903 if (reject_reason
) {
1904 *reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
1906 return NT_STATUS_PASSWORD_RESTRICTION
;
1909 /* Password minimum age: yes, this is a minus. The ages are in negative 100nsec units! */
1910 if (pwdLastSet
- minPwdAge
> now_nt
) {
1911 if (reject_reason
) {
1912 *reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
1914 return NT_STATUS_PASSWORD_RESTRICTION
;
1917 /* check the immediately past password */
1918 if (pwdHistoryLength
> 0) {
1919 if (lmNewHash
&& lmPwdHash
&& memcmp(lmNewHash
->hash
,
1920 lmPwdHash
->hash
, 16) == 0) {
1921 if (reject_reason
) {
1922 *reject_reason
= SAM_PWD_CHANGE_PWD_IN_HISTORY
;
1924 return NT_STATUS_PASSWORD_RESTRICTION
;
1926 if (ntNewHash
&& ntPwdHash
&& memcmp(ntNewHash
->hash
,
1927 ntPwdHash
->hash
, 16) == 0) {
1928 if (reject_reason
) {
1929 *reject_reason
= SAM_PWD_CHANGE_PWD_IN_HISTORY
;
1931 return NT_STATUS_PASSWORD_RESTRICTION
;
1935 /* check the password history */
1936 sambaLMPwdHistory_len
= MIN(sambaLMPwdHistory_len
,
1938 sambaNTPwdHistory_len
= MIN(sambaNTPwdHistory_len
,
1941 for (i
=0; lmNewHash
&& i
<sambaLMPwdHistory_len
;i
++) {
1942 if (memcmp(lmNewHash
->hash
, sambaLMPwdHistory
[i
].hash
,
1944 if (reject_reason
) {
1945 *reject_reason
= SAM_PWD_CHANGE_PWD_IN_HISTORY
;
1947 return NT_STATUS_PASSWORD_RESTRICTION
;
1950 for (i
=0; ntNewHash
&& i
<sambaNTPwdHistory_len
;i
++) {
1951 if (memcmp(ntNewHash
->hash
, sambaNTPwdHistory
[i
].hash
,
1953 if (reject_reason
) {
1954 *reject_reason
= SAM_PWD_CHANGE_PWD_IN_HISTORY
;
1956 return NT_STATUS_PASSWORD_RESTRICTION
;
1961 #define CHECK_RET(x) do { if (x != 0) return NT_STATUS_NO_MEMORY; } while(0)
1963 /* the password is acceptable. Start forming the new fields */
1964 if (new_password
!= NULL
) {
1965 /* if we know the cleartext UTF16 password, then set it.
1966 * Modules in ldb will set all the appropriate
1968 CHECK_RET(ldb_msg_add_value(mod
, "clearTextPassword", new_password
, NULL
));
1970 /* we don't have the cleartext, so set what we have of the
1974 CHECK_RET(samdb_msg_add_hash(ctx
, mem_ctx
, mod
, "dBCSPwd", lmNewHash
));
1978 CHECK_RET(samdb_msg_add_hash(ctx
, mem_ctx
, mod
, "unicodePwd", ntNewHash
));
1982 if (reject_reason
) {
1983 *reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
1985 return NT_STATUS_OK
;
1990 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1991 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1992 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
1993 * gives some more informations if the changed failed.
1995 * This wrapper function for "samdb_set_password" takes a SID as input rather
1998 * This call encapsulates a new LDB transaction for changing the password;
1999 * therefore the user hasn't to start a new one.
2001 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2002 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2003 * NT_STATUS_PASSWORD_RESTRICTION
2005 NTSTATUS
samdb_set_password_sid(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2006 const struct dom_sid
*user_sid
,
2007 const DATA_BLOB
*new_password
,
2008 struct samr_Password
*lmNewHash
,
2009 struct samr_Password
*ntNewHash
,
2011 enum samPwdChangeReason
*reject_reason
,
2012 struct samr_DomInfo1
**_dominfo
)
2015 struct ldb_dn
*user_dn
;
2016 struct ldb_message
*msg
;
2019 ret
= ldb_transaction_start(ldb
);
2020 if (ret
!= LDB_SUCCESS
) {
2021 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb
)));
2022 return NT_STATUS_TRANSACTION_ABORTED
;
2025 user_dn
= samdb_search_dn(ldb
, mem_ctx
, NULL
,
2026 "(&(objectSid=%s)(objectClass=user))",
2027 ldap_encode_ndr_dom_sid(mem_ctx
, user_sid
));
2029 ldb_transaction_cancel(ldb
);
2030 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2031 dom_sid_string(mem_ctx
, user_sid
)));
2032 return NT_STATUS_NO_SUCH_USER
;
2035 msg
= ldb_msg_new(mem_ctx
);
2037 ldb_transaction_cancel(ldb
);
2038 talloc_free(user_dn
);
2039 return NT_STATUS_NO_MEMORY
;
2042 msg
->dn
= ldb_dn_copy(msg
, user_dn
);
2044 ldb_transaction_cancel(ldb
);
2045 talloc_free(user_dn
);
2047 return NT_STATUS_NO_MEMORY
;
2050 nt_status
= samdb_set_password(ldb
, mem_ctx
,
2053 lmNewHash
, ntNewHash
,
2055 reject_reason
, _dominfo
);
2056 if (!NT_STATUS_IS_OK(nt_status
)) {
2057 ldb_transaction_cancel(ldb
);
2058 talloc_free(user_dn
);
2063 /* modify the samdb record */
2064 ret
= dsdb_replace(ldb
, msg
, 0);
2065 if (ret
!= LDB_SUCCESS
) {
2066 ldb_transaction_cancel(ldb
);
2067 talloc_free(user_dn
);
2069 return NT_STATUS_ACCESS_DENIED
;
2074 ret
= ldb_transaction_commit(ldb
);
2075 if (ret
!= LDB_SUCCESS
) {
2076 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2077 ldb_dn_get_linearized(user_dn
),
2078 ldb_errstring(ldb
)));
2079 talloc_free(user_dn
);
2080 return NT_STATUS_TRANSACTION_ABORTED
;
2083 talloc_free(user_dn
);
2084 return NT_STATUS_OK
;
2088 NTSTATUS
samdb_create_foreign_security_principal(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
2089 struct dom_sid
*sid
, struct ldb_dn
**ret_dn
)
2091 struct ldb_message
*msg
;
2092 struct ldb_dn
*basedn
;
2096 sidstr
= dom_sid_string(mem_ctx
, sid
);
2097 NT_STATUS_HAVE_NO_MEMORY(sidstr
);
2099 /* We might have to create a ForeignSecurityPrincipal, even if this user
2100 * is in our own domain */
2102 msg
= ldb_msg_new(sidstr
);
2104 talloc_free(sidstr
);
2105 return NT_STATUS_NO_MEMORY
;
2108 ret
= dsdb_wellknown_dn(sam_ctx
, sidstr
, samdb_base_dn(sam_ctx
),
2109 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER
,
2111 if (ret
!= LDB_SUCCESS
) {
2112 DEBUG(0, ("Failed to find DN for "
2113 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx
)));
2114 talloc_free(sidstr
);
2115 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2118 /* add core elements to the ldb_message for the alias */
2120 if ( ! ldb_dn_add_child_fmt(msg
->dn
, "CN=%s", sidstr
)) {
2121 talloc_free(sidstr
);
2122 return NT_STATUS_NO_MEMORY
;
2125 samdb_msg_add_string(sam_ctx
, msg
, msg
,
2127 "foreignSecurityPrincipal");
2129 /* create the alias */
2130 ret
= ldb_add(sam_ctx
, msg
);
2131 if (ret
!= LDB_SUCCESS
) {
2132 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2134 ldb_dn_get_linearized(msg
->dn
),
2135 ldb_errstring(sam_ctx
)));
2136 talloc_free(sidstr
);
2137 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2140 *ret_dn
= talloc_steal(mem_ctx
, msg
->dn
);
2141 talloc_free(sidstr
);
2143 return NT_STATUS_OK
;
2148 Find the DN of a domain, assuming it to be a dotted.dns name
2151 struct ldb_dn
*samdb_dns_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char *dns_domain
)
2154 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2155 const char *binary_encoded
;
2156 const char **split_realm
;
2163 split_realm
= (const char **)str_list_make(tmp_ctx
, dns_domain
, ".");
2165 talloc_free(tmp_ctx
);
2168 dn
= ldb_dn_new(mem_ctx
, ldb
, NULL
);
2169 for (i
=0; split_realm
[i
]; i
++) {
2170 binary_encoded
= ldb_binary_encode_string(tmp_ctx
, split_realm
[i
]);
2171 if (!ldb_dn_add_base_fmt(dn
, "dc=%s", binary_encoded
)) {
2172 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2173 binary_encoded
, ldb_dn_get_linearized(dn
)));
2174 talloc_free(tmp_ctx
);
2178 if (!ldb_dn_validate(dn
)) {
2179 DEBUG(2, ("Failed to validated DN %s\n",
2180 ldb_dn_get_linearized(dn
)));
2181 talloc_free(tmp_ctx
);
2184 talloc_free(tmp_ctx
);
2189 Find the DN of a domain, be it the netbios or DNS name
2191 struct ldb_dn
*samdb_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2192 const char *domain_name
)
2194 const char * const domain_ref_attrs
[] = {
2197 const char * const domain_ref2_attrs
[] = {
2200 struct ldb_result
*res_domain_ref
;
2201 char *escaped_domain
= ldb_binary_encode_string(mem_ctx
, domain_name
);
2202 /* find the domain's DN */
2203 int ret_domain
= ldb_search(ldb
, mem_ctx
,
2205 samdb_partitions_dn(ldb
, mem_ctx
),
2208 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2210 if (ret_domain
!= 0) {
2214 if (res_domain_ref
->count
== 0) {
2215 ret_domain
= ldb_search(ldb
, mem_ctx
,
2217 samdb_dns_domain_to_dn(ldb
, mem_ctx
, domain_name
),
2220 "(objectclass=domain)");
2221 if (ret_domain
!= 0) {
2225 if (res_domain_ref
->count
== 1) {
2226 return res_domain_ref
->msgs
[0]->dn
;
2231 if (res_domain_ref
->count
> 1) {
2232 DEBUG(0,("Found %d records matching domain [%s]\n",
2233 ret_domain
, domain_name
));
2237 return samdb_result_dn(ldb
, mem_ctx
, res_domain_ref
->msgs
[0], "nCName", NULL
);
2243 use a GUID to find a DN
2245 int dsdb_find_dn_by_guid(struct ldb_context
*ldb
,
2246 TALLOC_CTX
*mem_ctx
,
2247 const struct GUID
*guid
, struct ldb_dn
**dn
)
2250 struct ldb_result
*res
;
2251 const char *attrs
[] = { NULL
};
2252 char *guid_str
= GUID_string(mem_ctx
, guid
);
2255 return LDB_ERR_OPERATIONS_ERROR
;
2258 ret
= dsdb_search(ldb
, mem_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, attrs
,
2259 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
|
2260 DSDB_SEARCH_SHOW_EXTENDED_DN
|
2261 DSDB_SEARCH_ONE_ONLY
,
2262 "objectGUID=%s", guid_str
);
2263 talloc_free(guid_str
);
2264 if (ret
!= LDB_SUCCESS
) {
2268 *dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
2275 use a DN to find a GUID with a given attribute name
2277 int dsdb_find_guid_attr_by_dn(struct ldb_context
*ldb
,
2278 struct ldb_dn
*dn
, const char *attribute
,
2282 struct ldb_result
*res
;
2283 const char *attrs
[2];
2284 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2286 attrs
[0] = attribute
;
2289 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
, DSDB_SEARCH_SHOW_DELETED
);
2290 if (ret
!= LDB_SUCCESS
) {
2291 talloc_free(tmp_ctx
);
2294 if (res
->count
< 1) {
2295 talloc_free(tmp_ctx
);
2296 return LDB_ERR_NO_SUCH_OBJECT
;
2298 *guid
= samdb_result_guid(res
->msgs
[0], attribute
);
2299 talloc_free(tmp_ctx
);
2304 use a DN to find a GUID
2306 int dsdb_find_guid_by_dn(struct ldb_context
*ldb
,
2307 struct ldb_dn
*dn
, struct GUID
*guid
)
2309 return dsdb_find_guid_attr_by_dn(ldb
, dn
, "objectGUID", guid
);
2315 adds the given GUID to the given ldb_message. This value is added
2316 for the given attr_name (may be either "objectGUID" or "parentGUID").
2318 int dsdb_msg_add_guid(struct ldb_message
*msg
,
2320 const char *attr_name
)
2325 TALLOC_CTX
*tmp_ctx
= talloc_init("dsdb_msg_add_guid");
2327 status
= GUID_to_ndr_blob(guid
, tmp_ctx
, &v
);
2328 if (!NT_STATUS_IS_OK(status
)) {
2329 ret
= LDB_ERR_OPERATIONS_ERROR
;
2333 ret
= ldb_msg_add_steal_value(msg
, attr_name
, &v
);
2334 if (ret
!= LDB_SUCCESS
) {
2335 DEBUG(4,(__location__
": Failed to add %s to the message\n",
2343 talloc_free(tmp_ctx
);
2350 use a DN to find a SID
2352 int dsdb_find_sid_by_dn(struct ldb_context
*ldb
,
2353 struct ldb_dn
*dn
, struct dom_sid
*sid
)
2356 struct ldb_result
*res
;
2357 const char *attrs
[] = { "objectSID", NULL
};
2358 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2363 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
, DSDB_SEARCH_SHOW_DELETED
);
2364 if (ret
!= LDB_SUCCESS
) {
2365 talloc_free(tmp_ctx
);
2368 if (res
->count
< 1) {
2369 talloc_free(tmp_ctx
);
2370 return LDB_ERR_NO_SUCH_OBJECT
;
2372 s
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSID");
2374 talloc_free(tmp_ctx
);
2375 return LDB_ERR_NO_SUCH_OBJECT
;
2378 talloc_free(tmp_ctx
);
2385 load a repsFromTo blob list for a given partition GUID
2386 attr must be "repsFrom" or "repsTo"
2388 WERROR
dsdb_loadreps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
2389 const char *attr
, struct repsFromToBlob
**r
, uint32_t *count
)
2391 const char *attrs
[] = { attr
, NULL
};
2392 struct ldb_result
*res
= NULL
;
2393 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2395 struct ldb_message_element
*el
;
2400 if (ldb_search(sam_ctx
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, NULL
) != LDB_SUCCESS
||
2402 DEBUG(0,("dsdb_loadreps: failed to read partition object\n"));
2403 talloc_free(tmp_ctx
);
2404 return WERR_DS_DRA_INTERNAL_ERROR
;
2407 el
= ldb_msg_find_element(res
->msgs
[0], attr
);
2409 /* it's OK to be empty */
2410 talloc_free(tmp_ctx
);
2414 *count
= el
->num_values
;
2415 *r
= talloc_array(mem_ctx
, struct repsFromToBlob
, *count
);
2417 talloc_free(tmp_ctx
);
2418 return WERR_DS_DRA_INTERNAL_ERROR
;
2421 for (i
=0; i
<(*count
); i
++) {
2422 enum ndr_err_code ndr_err
;
2423 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
],
2424 mem_ctx
, lp_iconv_convenience(ldb_get_opaque(sam_ctx
, "loadparm")),
2426 (ndr_pull_flags_fn_t
)ndr_pull_repsFromToBlob
);
2427 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2428 talloc_free(tmp_ctx
);
2429 return WERR_DS_DRA_INTERNAL_ERROR
;
2433 talloc_free(tmp_ctx
);
2439 save the repsFromTo blob list for a given partition GUID
2440 attr must be "repsFrom" or "repsTo"
2442 WERROR
dsdb_savereps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
2443 const char *attr
, struct repsFromToBlob
*r
, uint32_t count
)
2445 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2446 struct ldb_message
*msg
;
2447 struct ldb_message_element
*el
;
2450 msg
= ldb_msg_new(tmp_ctx
);
2452 if (ldb_msg_add_empty(msg
, attr
, LDB_FLAG_MOD_REPLACE
, &el
) != LDB_SUCCESS
) {
2456 el
->values
= talloc_array(msg
, struct ldb_val
, count
);
2461 for (i
=0; i
<count
; i
++) {
2463 enum ndr_err_code ndr_err
;
2465 ndr_err
= ndr_push_struct_blob(&v
, tmp_ctx
, lp_iconv_convenience(ldb_get_opaque(sam_ctx
, "loadparm")),
2467 (ndr_push_flags_fn_t
)ndr_push_repsFromToBlob
);
2468 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2476 if (ldb_modify(sam_ctx
, msg
) != LDB_SUCCESS
) {
2477 DEBUG(0,("Failed to store %s - %s\n", attr
, ldb_errstring(sam_ctx
)));
2481 talloc_free(tmp_ctx
);
2486 talloc_free(tmp_ctx
);
2487 return WERR_DS_DRA_INTERNAL_ERROR
;
2492 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2493 object for a partition
2495 int dsdb_load_partition_usn(struct ldb_context
*ldb
, struct ldb_dn
*dn
,
2496 uint64_t *uSN
, uint64_t *urgent_uSN
)
2498 struct ldb_request
*req
;
2500 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2501 struct dsdb_control_current_partition
*p_ctrl
;
2502 struct ldb_result
*res
;
2504 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
2506 talloc_free(tmp_ctx
);
2507 return LDB_ERR_OPERATIONS_ERROR
;
2510 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
2511 ldb_dn_new(tmp_ctx
, ldb
, "@REPLCHANGED"),
2515 res
, ldb_search_default_callback
,
2517 if (ret
!= LDB_SUCCESS
) {
2518 talloc_free(tmp_ctx
);
2522 p_ctrl
= talloc(req
, struct dsdb_control_current_partition
);
2523 if (p_ctrl
== NULL
) {
2525 return LDB_ERR_OPERATIONS_ERROR
;
2527 p_ctrl
->version
= DSDB_CONTROL_CURRENT_PARTITION_VERSION
;
2531 ret
= ldb_request_add_control(req
,
2532 DSDB_CONTROL_CURRENT_PARTITION_OID
,
2534 if (ret
!= LDB_SUCCESS
) {
2535 talloc_free(tmp_ctx
);
2539 /* Run the new request */
2540 ret
= ldb_request(ldb
, req
);
2542 if (ret
== LDB_SUCCESS
) {
2543 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
2546 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
2547 /* it hasn't been created yet, which means
2548 an implicit value of zero */
2550 talloc_free(tmp_ctx
);
2554 if (ret
!= LDB_SUCCESS
) {
2555 talloc_free(tmp_ctx
);
2559 if (res
->count
< 1) {
2565 *uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNHighest", 0);
2567 *urgent_uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNUrgent", 0);
2571 talloc_free(tmp_ctx
);
2577 save uSNHighest and uSNUrgent attributes in the @REPLCHANGED object for a
2580 int dsdb_save_partition_usn(struct ldb_context
*ldb
, struct ldb_dn
*dn
,
2581 uint64_t uSN
, uint64_t urgent_uSN
)
2583 struct ldb_request
*req
;
2584 struct ldb_message
*msg
;
2585 struct dsdb_control_current_partition
*p_ctrl
;
2588 msg
= ldb_msg_new(ldb
);
2590 return LDB_ERR_OPERATIONS_ERROR
;
2593 msg
->dn
= ldb_dn_new(msg
, ldb
, "@REPLCHANGED");
2594 if (msg
->dn
== NULL
) {
2596 return LDB_ERR_OPERATIONS_ERROR
;
2599 ret
= ldb_msg_add_fmt(msg
, "uSNHighest", "%llu", (unsigned long long)uSN
);
2600 if (ret
!= LDB_SUCCESS
) {
2604 msg
->elements
[0].flags
= LDB_FLAG_MOD_REPLACE
;
2606 /* urgent_uSN is optional so may not be stored */
2608 ret
= ldb_msg_add_fmt(msg
, "uSNUrgent", "%llu", (unsigned long long)urgent_uSN
);
2609 if (ret
!= LDB_SUCCESS
) {
2613 msg
->elements
[1].flags
= LDB_FLAG_MOD_REPLACE
;
2617 p_ctrl
= talloc(msg
, struct dsdb_control_current_partition
);
2618 if (p_ctrl
== NULL
) {
2620 return LDB_ERR_OPERATIONS_ERROR
;
2622 p_ctrl
->version
= DSDB_CONTROL_CURRENT_PARTITION_VERSION
;
2625 ret
= ldb_build_mod_req(&req
, ldb
, msg
,
2628 NULL
, ldb_op_default_callback
,
2631 if (ret
!= LDB_SUCCESS
) {
2636 ret
= ldb_request_add_control(req
,
2637 DSDB_CONTROL_CURRENT_PARTITION_OID
,
2639 if (ret
!= LDB_SUCCESS
) {
2644 /* Run the new request */
2645 ret
= ldb_request(ldb
, req
);
2647 if (ret
== LDB_SUCCESS
) {
2648 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
2650 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
2651 ret
= ldb_build_add_req(&req
, ldb
, msg
,
2654 NULL
, ldb_op_default_callback
,
2664 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2
*c1
,
2665 const struct drsuapi_DsReplicaCursor2
*c2
)
2667 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
2670 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor
*c1
,
2671 const struct drsuapi_DsReplicaCursor
*c2
)
2673 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
2677 see if we are a RODC
2679 bool samdb_rodc(struct ldb_context
*sam_ctx
)
2681 TALLOC_CTX
*tmp_ctx
;
2682 const char *obj_category
;
2683 struct ldb_dn
*obj_category_dn
;
2684 const struct ldb_val
*obj_category_dn_rdn_val
;
2686 tmp_ctx
= talloc_new(sam_ctx
);
2687 if (tmp_ctx
== NULL
) {
2688 DEBUG(1,("samdb_rodc: Failed to talloc new context.\n"));
2692 obj_category
= samdb_ntds_object_category(tmp_ctx
, sam_ctx
);
2693 if (!obj_category
) {
2694 DEBUG(1,("samdb_rodc: Failed to get object category.\n"));
2698 obj_category_dn
= ldb_dn_new(tmp_ctx
, sam_ctx
, obj_category
);
2699 if (!obj_category_dn
) {
2700 DEBUG(1,("samdb_rodc: Failed to create object category dn.\n"));
2704 obj_category_dn_rdn_val
= ldb_dn_get_rdn_val(obj_category_dn
);
2705 if (!obj_category_dn_rdn_val
) {
2706 DEBUG(1, ("samdb_rodc: Failed to get object category dn rdn value.\n"));
2710 if (strequal((const char*)obj_category_dn_rdn_val
->data
, "NTDS-DSA-RO")) {
2711 talloc_free(tmp_ctx
);
2716 talloc_free(tmp_ctx
);
2721 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
2723 flags are DS_NTDS_OPTION_*
2725 int samdb_ntds_options(struct ldb_context
*ldb
, uint32_t *options
)
2727 TALLOC_CTX
*tmp_ctx
;
2728 const char *attrs
[] = { "options", NULL
};
2730 struct ldb_result
*res
;
2732 tmp_ctx
= talloc_new(ldb
);
2733 if (tmp_ctx
== NULL
) {
2737 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
2742 if (res
->count
!= 1) {
2746 *options
= samdb_result_uint(res
->msgs
[0], "options", 0);
2748 talloc_free(tmp_ctx
);
2753 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
2754 talloc_free(tmp_ctx
);
2755 return LDB_ERR_NO_SUCH_OBJECT
;
2758 const char* samdb_ntds_object_category(TALLOC_CTX
*tmp_ctx
, struct ldb_context
*ldb
)
2760 const char *attrs
[] = { "objectCategory", NULL
};
2762 struct ldb_result
*res
;
2764 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
2769 if (res
->count
!= 1) {
2773 return samdb_result_string(res
->msgs
[0], "objectCategory", NULL
);
2776 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
2781 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
2782 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
2784 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX
*mem_ctx
, const char *cn
)
2786 char **tokens
, *ret
;
2789 tokens
= str_list_make(mem_ctx
, cn
, " -_");
2793 /* "tolower()" and "toupper()" should also work properly on 0x00 */
2794 tokens
[0][0] = tolower(tokens
[0][0]);
2795 for (i
= 1; i
< str_list_length((const char **)tokens
); i
++)
2796 tokens
[i
][0] = toupper(tokens
[i
][0]);
2798 ret
= talloc_strdup(mem_ctx
, tokens
[0]);
2799 for (i
= 1; i
< str_list_length((const char **)tokens
); i
++)
2800 ret
= talloc_asprintf_append_buffer(ret
, "%s", tokens
[i
]);
2802 talloc_free(tokens
);
2808 return domain functional level
2809 returns DS_DOMAIN_FUNCTION_*
2811 int dsdb_functional_level(struct ldb_context
*ldb
)
2813 int *domainFunctionality
=
2814 talloc_get_type(ldb_get_opaque(ldb
, "domainFunctionality"), int);
2815 if (!domainFunctionality
) {
2816 DEBUG(0,(__location__
": WARNING: domainFunctionality not setup\n"));
2817 return DS_DOMAIN_FUNCTION_2000
;
2819 return *domainFunctionality
;
2823 set a GUID in an extended DN structure
2825 int dsdb_set_extended_dn_guid(struct ldb_dn
*dn
, const struct GUID
*guid
, const char *component_name
)
2831 status
= GUID_to_ndr_blob(guid
, dn
, &v
);
2832 if (!NT_STATUS_IS_OK(status
)) {
2833 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
2836 ret
= ldb_dn_set_extended_component(dn
, component_name
, &v
);
2842 return a GUID from a extended DN structure
2844 NTSTATUS
dsdb_get_extended_dn_guid(struct ldb_dn
*dn
, struct GUID
*guid
, const char *component_name
)
2846 const struct ldb_val
*v
;
2848 v
= ldb_dn_get_extended_component(dn
, component_name
);
2850 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2853 return GUID_from_ndr_blob(v
, guid
);
2857 return a uint64_t from a extended DN structure
2859 NTSTATUS
dsdb_get_extended_dn_uint64(struct ldb_dn
*dn
, uint64_t *val
, const char *component_name
)
2861 const struct ldb_val
*v
;
2864 v
= ldb_dn_get_extended_component(dn
, component_name
);
2866 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2868 s
= talloc_strndup(dn
, (const char *)v
->data
, v
->length
);
2869 NT_STATUS_HAVE_NO_MEMORY(s
);
2871 *val
= strtoull(s
, NULL
, 0);
2874 return NT_STATUS_OK
;
2878 return a NTTIME from a extended DN structure
2880 NTSTATUS
dsdb_get_extended_dn_nttime(struct ldb_dn
*dn
, NTTIME
*nttime
, const char *component_name
)
2882 return dsdb_get_extended_dn_uint64(dn
, nttime
, component_name
);
2886 return a uint32_t from a extended DN structure
2888 NTSTATUS
dsdb_get_extended_dn_uint32(struct ldb_dn
*dn
, uint32_t *val
, const char *component_name
)
2890 const struct ldb_val
*v
;
2893 v
= ldb_dn_get_extended_component(dn
, component_name
);
2895 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2898 s
= talloc_strndup(dn
, (const char *)v
->data
, v
->length
);
2899 NT_STATUS_HAVE_NO_MEMORY(s
);
2901 *val
= strtoul(s
, NULL
, 0);
2904 return NT_STATUS_OK
;
2908 return RMD_FLAGS directly from a ldb_dn
2909 returns 0 if not found
2911 uint32_t dsdb_dn_rmd_flags(struct ldb_dn
*dn
)
2913 const struct ldb_val
*v
;
2915 v
= ldb_dn_get_extended_component(dn
, "RMD_FLAGS");
2916 if (!v
|| v
->length
> sizeof(buf
)-1) return 0;
2917 strncpy(buf
, (const char *)v
->data
, v
->length
);
2919 return strtoul(buf
, NULL
, 10);
2923 return RMD_FLAGS directly from a ldb_val for a DN
2924 returns 0 if RMD_FLAGS is not found
2926 uint32_t dsdb_dn_val_rmd_flags(struct ldb_val
*val
)
2932 if (val
->length
< 13) {
2935 p
= memmem(val
->data
, val
->length
-2, "<RMD_FLAGS=", 11);
2939 flags
= strtoul(p
+11, &end
, 10);
2940 if (!end
|| *end
!= '>') {
2941 /* it must end in a > */
2948 return true if a ldb_val containing a DN in storage form is deleted
2950 bool dsdb_dn_is_deleted_val(struct ldb_val
*val
)
2952 return (dsdb_dn_val_rmd_flags(val
) & DSDB_RMD_FLAG_DELETED
) != 0;
2956 return true if a ldb_val containing a DN in storage form is
2957 in the upgraded w2k3 linked attribute format
2959 bool dsdb_dn_is_upgraded_link_val(struct ldb_val
*val
)
2961 return memmem(val
->data
, val
->length
, "<RMD_ADDTIME=", 13) != NULL
;
2965 return a DN for a wellknown GUID
2967 int dsdb_wellknown_dn(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
,
2968 struct ldb_dn
*nc_root
, const char *wk_guid
,
2969 struct ldb_dn
**wkguid_dn
)
2971 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2972 const char *attrs
[] = { NULL
};
2975 struct ldb_result
*res
;
2977 /* construct the magic WKGUID DN */
2978 dn
= ldb_dn_new_fmt(tmp_ctx
, samdb
, "<WKGUID=%s,%s>",
2979 wk_guid
, ldb_dn_get_linearized(nc_root
));
2981 talloc_free(tmp_ctx
);
2982 return LDB_ERR_OPERATIONS_ERROR
;
2985 ret
= dsdb_search_dn(samdb
, tmp_ctx
, &res
, dn
, attrs
, DSDB_SEARCH_SHOW_DELETED
);
2986 if (ret
!= LDB_SUCCESS
) {
2987 talloc_free(tmp_ctx
);
2991 (*wkguid_dn
) = talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
2992 talloc_free(tmp_ctx
);
2997 static int dsdb_dn_compare_ptrs(struct ldb_dn
**dn1
, struct ldb_dn
**dn2
)
2999 return ldb_dn_compare(*dn1
, *dn2
);
3003 find a NC root given a DN within the NC
3005 int dsdb_find_nc_root(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
3006 struct ldb_dn
**nc_root
)
3008 const char *root_attrs
[] = { "namingContexts", NULL
};
3009 TALLOC_CTX
*tmp_ctx
;
3011 struct ldb_message_element
*el
;
3012 struct ldb_result
*root_res
;
3014 struct ldb_dn
**nc_dns
;
3016 tmp_ctx
= talloc_new(samdb
);
3017 if (tmp_ctx
== NULL
) {
3018 return LDB_ERR_OPERATIONS_ERROR
;
3021 ret
= ldb_search(samdb
, tmp_ctx
, &root_res
,
3022 ldb_dn_new(tmp_ctx
, samdb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
3024 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb
)));
3025 talloc_free(tmp_ctx
);
3029 el
= ldb_msg_find_element(root_res
->msgs
[0], "namingContexts");
3031 DEBUG(1,("Finding namingContexts element in root_res failed: %s\n",
3032 ldb_errstring(samdb
)));
3033 talloc_free(tmp_ctx
);
3034 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
3037 nc_dns
= talloc_array(tmp_ctx
, struct ldb_dn
*, el
->num_values
);
3039 talloc_free(tmp_ctx
);
3040 return LDB_ERR_OPERATIONS_ERROR
;
3043 for (i
=0; i
<el
->num_values
; i
++) {
3044 nc_dns
[i
] = ldb_dn_from_ldb_val(nc_dns
, samdb
, &el
->values
[i
]);
3045 if (nc_dns
[i
] == NULL
) {
3046 talloc_free(tmp_ctx
);
3047 return LDB_ERR_OPERATIONS_ERROR
;
3051 TYPESAFE_QSORT(nc_dns
, el
->num_values
, dsdb_dn_compare_ptrs
);
3053 for (i
=0; i
<el
->num_values
; i
++) {
3054 if (ldb_dn_compare_base(nc_dns
[i
], dn
) == 0) {
3055 (*nc_root
) = talloc_steal(mem_ctx
, nc_dns
[i
]);
3056 talloc_free(tmp_ctx
);
3061 talloc_free(tmp_ctx
);
3062 return LDB_ERR_NO_SUCH_OBJECT
;
3067 find the deleted objects DN for any object, by looking for the NC
3068 root, then looking up the wellknown GUID
3070 int dsdb_get_deleted_objects_dn(struct ldb_context
*ldb
,
3071 TALLOC_CTX
*mem_ctx
, struct ldb_dn
*obj_dn
,
3072 struct ldb_dn
**do_dn
)
3074 struct ldb_dn
*nc_root
;
3077 ret
= dsdb_find_nc_root(ldb
, mem_ctx
, obj_dn
, &nc_root
);
3078 if (ret
!= LDB_SUCCESS
) {
3082 ret
= dsdb_wellknown_dn(ldb
, mem_ctx
, nc_root
, DS_GUID_DELETED_OBJECTS_CONTAINER
, do_dn
);
3083 talloc_free(nc_root
);
3088 return the tombstoneLifetime, in days
3090 int dsdb_tombstone_lifetime(struct ldb_context
*ldb
, uint32_t *lifetime
)
3093 dn
= samdb_config_dn(ldb
);
3095 return LDB_ERR_NO_SUCH_OBJECT
;
3097 dn
= ldb_dn_copy(ldb
, dn
);
3099 return LDB_ERR_OPERATIONS_ERROR
;
3101 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3102 be a wellknown GUID for this */
3103 if (!ldb_dn_add_child_fmt(dn
, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3105 return LDB_ERR_OPERATIONS_ERROR
;
3108 *lifetime
= samdb_search_uint(ldb
, dn
, 180, dn
, "tombstoneLifetime", "objectClass=nTDSService");
3114 compare a ldb_val to a string case insensitively
3116 int samdb_ldb_val_case_cmp(const char *s
, struct ldb_val
*v
)
3118 size_t len
= strlen(s
);
3120 if (len
> v
->length
) return 1;
3121 ret
= strncasecmp(s
, (const char *)v
->data
, v
->length
);
3122 if (ret
!= 0) return ret
;
3123 if (v
->length
> len
&& v
->data
[len
] != 0) {
3131 load the UDV for a partition in v2 format
3132 The list is returned sorted, and with our local cursor added
3134 int dsdb_load_udv_v2(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
3135 struct drsuapi_DsReplicaCursor2
**cursors
, uint32_t *count
)
3137 static const char *attrs
[] = { "replUpToDateVector", NULL
};
3138 struct ldb_result
*r
;
3139 const struct ldb_val
*ouv_value
;
3142 uint64_t highest_usn
;
3143 const struct GUID
*our_invocation_id
;
3144 struct timeval now
= timeval_current();
3146 ret
= ldb_search(samdb
, mem_ctx
, &r
, dn
, LDB_SCOPE_BASE
, attrs
, NULL
);
3147 if (ret
!= LDB_SUCCESS
) {
3151 ouv_value
= ldb_msg_find_ldb_val(r
->msgs
[0], "replUpToDateVector");
3153 enum ndr_err_code ndr_err
;
3154 struct replUpToDateVectorBlob ouv
;
3156 ndr_err
= ndr_pull_struct_blob(ouv_value
, r
,
3157 lp_iconv_convenience(ldb_get_opaque(samdb
, "loadparm")), &ouv
,
3158 (ndr_pull_flags_fn_t
)ndr_pull_replUpToDateVectorBlob
);
3159 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3161 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
3163 if (ouv
.version
!= 2) {
3164 /* we always store as version 2, and
3165 * replUpToDateVector is not replicated
3167 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
3170 *count
= ouv
.ctr
.ctr2
.count
;
3171 *cursors
= talloc_steal(mem_ctx
, ouv
.ctr
.ctr2
.cursors
);
3179 our_invocation_id
= samdb_ntds_invocation_id(samdb
);
3180 if (!our_invocation_id
) {
3181 DEBUG(0,(__location__
": No invocationID on samdb - %s\n", ldb_errstring(samdb
)));
3182 talloc_free(*cursors
);
3183 return LDB_ERR_OPERATIONS_ERROR
;
3186 ret
= dsdb_load_partition_usn(samdb
, dn
, &highest_usn
, NULL
);
3187 if (ret
!= LDB_SUCCESS
) {
3188 /* nothing to add - this can happen after a vampire */
3189 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
3193 for (i
=0; i
<*count
; i
++) {
3194 if (GUID_equal(our_invocation_id
, &(*cursors
)[i
].source_dsa_invocation_id
)) {
3195 (*cursors
)[i
].highest_usn
= highest_usn
;
3196 (*cursors
)[i
].last_sync_success
= timeval_to_nttime(&now
);
3197 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
3202 (*cursors
) = talloc_realloc(mem_ctx
, *cursors
, struct drsuapi_DsReplicaCursor2
, (*count
)+1);
3204 return LDB_ERR_OPERATIONS_ERROR
;
3207 (*cursors
)[*count
].source_dsa_invocation_id
= *our_invocation_id
;
3208 (*cursors
)[*count
].highest_usn
= highest_usn
;
3209 (*cursors
)[*count
].last_sync_success
= timeval_to_nttime(&now
);
3212 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
3218 load the UDV for a partition in version 1 format
3219 The list is returned sorted, and with our local cursor added
3221 int dsdb_load_udv_v1(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
3222 struct drsuapi_DsReplicaCursor
**cursors
, uint32_t *count
)
3224 struct drsuapi_DsReplicaCursor2
*v2
;
3228 ret
= dsdb_load_udv_v2(samdb
, dn
, mem_ctx
, &v2
, count
);
3229 if (ret
!= LDB_SUCCESS
) {
3239 *cursors
= talloc_array(mem_ctx
, struct drsuapi_DsReplicaCursor
, *count
);
3240 if (*cursors
== NULL
) {
3242 return LDB_ERR_OPERATIONS_ERROR
;
3245 for (i
=0; i
<*count
; i
++) {
3246 (*cursors
)[i
].source_dsa_invocation_id
= v2
[i
].source_dsa_invocation_id
;
3247 (*cursors
)[i
].highest_usn
= v2
[i
].highest_usn
;
3254 add a set of controls to a ldb_request structure based on a set of
3255 flags. See util.h for a list of available flags
3257 int dsdb_request_add_controls(struct ldb_request
*req
, uint32_t dsdb_flags
)
3260 if (dsdb_flags
& DSDB_SEARCH_SEARCH_ALL_PARTITIONS
) {
3261 struct ldb_search_options_control
*options
;
3262 /* Using the phantom root control allows us to search all partitions */
3263 options
= talloc(req
, struct ldb_search_options_control
);
3264 if (options
== NULL
) {
3265 return LDB_ERR_OPERATIONS_ERROR
;
3267 options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
3269 ret
= ldb_request_add_control(req
,
3270 LDB_CONTROL_SEARCH_OPTIONS_OID
,
3272 if (ret
!= LDB_SUCCESS
) {
3277 if (dsdb_flags
& DSDB_SEARCH_SHOW_DELETED
) {
3278 ret
= ldb_request_add_control(req
, LDB_CONTROL_SHOW_DELETED_OID
, true, NULL
);
3279 if (ret
!= LDB_SUCCESS
) {
3284 if (dsdb_flags
& DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
) {
3285 ret
= ldb_request_add_control(req
, DSDB_CONTROL_DN_STORAGE_FORMAT_OID
, true, NULL
);
3286 if (ret
!= LDB_SUCCESS
) {
3291 if (dsdb_flags
& DSDB_SEARCH_SHOW_EXTENDED_DN
) {
3292 struct ldb_extended_dn_control
*extended_ctrl
= talloc(req
, struct ldb_extended_dn_control
);
3293 if (!extended_ctrl
) {
3294 return LDB_ERR_OPERATIONS_ERROR
;
3296 extended_ctrl
->type
= 1;
3298 ret
= ldb_request_add_control(req
, LDB_CONTROL_EXTENDED_DN_OID
, true, extended_ctrl
);
3299 if (ret
!= LDB_SUCCESS
) {
3304 if (dsdb_flags
& DSDB_SEARCH_REVEAL_INTERNALS
) {
3305 ret
= ldb_request_add_control(req
, LDB_CONTROL_REVEAL_INTERNALS
, false, NULL
);
3306 if (ret
!= LDB_SUCCESS
) {
3311 if (dsdb_flags
& DSDB_MODIFY_RELAX
) {
3312 ret
= ldb_request_add_control(req
, LDB_CONTROL_RELAX_OID
, false, NULL
);
3313 if (ret
!= LDB_SUCCESS
) {
3318 if (dsdb_flags
& DSDB_MODIFY_PERMISSIVE
) {
3319 ret
= ldb_request_add_control(req
, LDB_CONTROL_PERMISSIVE_MODIFY_OID
, false, NULL
);
3320 if (ret
!= LDB_SUCCESS
) {
3325 if (dsdb_flags
& DSDB_FLAG_AS_SYSTEM
) {
3326 ret
= ldb_request_add_control(req
, LDB_CONTROL_AS_SYSTEM_OID
, false, NULL
);
3327 if (ret
!= LDB_SUCCESS
) {
3336 a modify with a set of controls
3338 int dsdb_modify(struct ldb_context
*ldb
, const struct ldb_message
*message
,
3339 uint32_t dsdb_flags
)
3341 struct ldb_request
*req
;
3344 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
3348 ldb_op_default_callback
,
3351 if (ret
!= LDB_SUCCESS
) return ret
;
3353 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3354 if (ret
!= LDB_SUCCESS
) {
3359 ret
= dsdb_autotransaction_request(ldb
, req
);
3366 like dsdb_modify() but set all the element flags to
3367 LDB_FLAG_MOD_REPLACE
3369 int dsdb_replace(struct ldb_context
*ldb
, struct ldb_message
*msg
, uint32_t dsdb_flags
)
3373 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3374 for (i
=0;i
<msg
->num_elements
;i
++) {
3375 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
3378 return dsdb_modify(ldb
, msg
, dsdb_flags
);
3383 search for attrs on one DN, allowing for dsdb_flags controls
3385 int dsdb_search_dn(struct ldb_context
*ldb
,
3386 TALLOC_CTX
*mem_ctx
,
3387 struct ldb_result
**_res
,
3388 struct ldb_dn
*basedn
,
3389 const char * const *attrs
,
3390 uint32_t dsdb_flags
)
3393 struct ldb_request
*req
;
3394 struct ldb_result
*res
;
3396 res
= talloc_zero(mem_ctx
, struct ldb_result
);
3398 return LDB_ERR_OPERATIONS_ERROR
;
3401 ret
= ldb_build_search_req(&req
, ldb
, res
,
3408 ldb_search_default_callback
,
3410 if (ret
!= LDB_SUCCESS
) {
3415 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3416 if (ret
!= LDB_SUCCESS
) {
3421 ret
= ldb_request(ldb
, req
);
3422 if (ret
== LDB_SUCCESS
) {
3423 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
3427 if (ret
!= LDB_SUCCESS
) {
3437 general search with dsdb_flags for controls
3439 int dsdb_search(struct ldb_context
*ldb
,
3440 TALLOC_CTX
*mem_ctx
,
3441 struct ldb_result
**_res
,
3442 struct ldb_dn
*basedn
,
3443 enum ldb_scope scope
,
3444 const char * const *attrs
,
3445 uint32_t dsdb_flags
,
3446 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
3449 struct ldb_request
*req
;
3450 struct ldb_result
*res
;
3452 char *expression
= NULL
;
3453 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3455 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
3457 talloc_free(tmp_ctx
);
3458 return LDB_ERR_OPERATIONS_ERROR
;
3462 va_start(ap
, exp_fmt
);
3463 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
3467 talloc_free(tmp_ctx
);
3468 return LDB_ERR_OPERATIONS_ERROR
;
3472 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
3479 ldb_search_default_callback
,
3481 if (ret
!= LDB_SUCCESS
) {
3482 talloc_free(tmp_ctx
);
3486 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3487 if (ret
!= LDB_SUCCESS
) {
3488 talloc_free(tmp_ctx
);
3492 ret
= ldb_request(ldb
, req
);
3493 if (ret
== LDB_SUCCESS
) {
3494 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
3497 if (ret
!= LDB_SUCCESS
) {
3498 talloc_free(tmp_ctx
);
3502 if (dsdb_flags
& DSDB_SEARCH_ONE_ONLY
) {
3503 if (res
->count
== 0) {
3504 talloc_free(tmp_ctx
);
3505 return LDB_ERR_NO_SUCH_OBJECT
;
3507 if (res
->count
!= 1) {
3508 talloc_free(tmp_ctx
);
3509 return LDB_ERR_CONSTRAINT_VIOLATION
;
3513 *_res
= talloc_steal(mem_ctx
, res
);
3514 talloc_free(tmp_ctx
);
3521 general search with dsdb_flags for controls
3522 returns exactly 1 record or an error
3524 int dsdb_search_one(struct ldb_context
*ldb
,
3525 TALLOC_CTX
*mem_ctx
,
3526 struct ldb_message
**msg
,
3527 struct ldb_dn
*basedn
,
3528 enum ldb_scope scope
,
3529 const char * const *attrs
,
3530 uint32_t dsdb_flags
,
3531 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
3534 struct ldb_result
*res
;
3536 char *expression
= NULL
;
3537 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3539 dsdb_flags
|= DSDB_SEARCH_ONE_ONLY
;
3541 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
3543 talloc_free(tmp_ctx
);
3544 return LDB_ERR_OPERATIONS_ERROR
;
3548 va_start(ap
, exp_fmt
);
3549 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
3553 talloc_free(tmp_ctx
);
3554 return LDB_ERR_OPERATIONS_ERROR
;
3558 ret
= dsdb_search(ldb
, tmp_ctx
, &res
, basedn
, scope
, attrs
,
3559 dsdb_flags
, "%s", expression
);
3560 if (ret
!= LDB_SUCCESS
) {
3561 talloc_free(tmp_ctx
);
3565 *msg
= talloc_steal(mem_ctx
, res
->msgs
[0]);
3566 talloc_free(tmp_ctx
);