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 structutres 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)) {
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);
802 v = talloc_strdup(mem_ctx, value);
805 ret = ldb_msg_add_string(msg, a, v);
808 el = ldb_msg_find_element(msg, a);
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);
827 v = talloc_strdup(mem_ctx, value);
830 ret = ldb_msg_add_string(msg, a, v);
833 el = ldb_msg_find_element(msg, a);
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);
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, unsigned int count)
901 val.data = talloc_array_size(mem_ctx, 16, count);
902 val.length = count*16;
906 for (i=0;i<count;i++) {
907 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
909 return ldb_msg_add_value(msg, attr_name, &val, NULL);
913 add a acct_flags element to a message
915 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
916 const char *attr_name, uint32_t v)
918 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
922 add a logon_hours element to a message
924 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
925 const char *attr_name, struct samr_LogonHours *hours)
928 val.length = hours->units_per_week / 8;
929 val.data = hours->bits;
930 return ldb_msg_add_value(msg, attr_name, &val, NULL);
934 add a parameters element to a message
936 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
937 const char *attr_name, struct lsa_BinaryString *parameters)
940 val.length = parameters->length;
941 val.data = (uint8_t *)parameters->array;
942 return ldb_msg_add_value(msg, attr_name, &val, NULL);
945 add a general value element to a message
947 int samdb_msg_add_value(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
948 const char *attr_name, const struct ldb_val *val)
950 return ldb_msg_add_value(msg, attr_name, val, NULL);
954 sets a general value element to a message
956 int samdb_msg_set_value(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
957 const char *attr_name, const struct ldb_val *val)
959 struct ldb_message_element *el;
961 el = ldb_msg_find_element(msg, attr_name);
965 return ldb_msg_add_value(msg, attr_name, val, NULL);
969 set a string element in a message
971 int samdb_msg_set_string(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
972 const char *attr_name, const char *str)
974 struct ldb_message_element *el;
976 el = ldb_msg_find_element(msg, attr_name);
980 return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, str);
984 * Handle ldb_request in transaction
986 static int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
987 struct ldb_request *req)
991 ret = ldb_transaction_start(sam_ldb);
992 if (ret != LDB_SUCCESS) {
996 ret = ldb_request(sam_ldb, req);
997 if (ret == LDB_SUCCESS) {
998 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1001 if (ret == LDB_SUCCESS) {
1002 return ldb_transaction_commit(sam_ldb);
1004 ldb_transaction_cancel(sam_ldb);
1010 return a default security descriptor
1012 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1014 struct security_descriptor *sd;
1016 sd = security_descriptor_initialise(mem_ctx);
1021 struct ldb_dn *samdb_base_dn(struct ldb_context *sam_ctx)
1023 return ldb_get_default_basedn(sam_ctx);
1026 struct ldb_dn *samdb_config_dn(struct ldb_context *sam_ctx)
1028 return ldb_get_config_basedn(sam_ctx);
1031 struct ldb_dn *samdb_schema_dn(struct ldb_context *sam_ctx)
1033 return ldb_get_schema_basedn(sam_ctx);
1036 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1038 struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1039 struct ldb_dn *aggregate_dn;
1044 aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1045 if (!aggregate_dn) {
1048 if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1051 return aggregate_dn;
1054 struct ldb_dn *samdb_root_dn(struct ldb_context *sam_ctx)
1056 return ldb_get_root_basedn(sam_ctx);
1059 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1061 struct ldb_dn *new_dn;
1063 new_dn = ldb_dn_copy(mem_ctx, samdb_config_dn(sam_ctx));
1064 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1065 talloc_free(new_dn);
1071 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1073 struct ldb_dn *new_dn;
1075 new_dn = ldb_dn_copy(mem_ctx, samdb_config_dn(sam_ctx));
1076 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1077 talloc_free(new_dn);
1084 work out the domain sid for the current open ldb
1086 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1088 TALLOC_CTX *tmp_ctx;
1089 const struct dom_sid *domain_sid;
1090 const char *attrs[] = {
1094 struct ldb_result *res;
1097 /* see if we have a cached copy */
1098 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1103 tmp_ctx = talloc_new(ldb);
1104 if (tmp_ctx == NULL) {
1108 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1110 if (ret != LDB_SUCCESS) {
1114 if (res->count != 1) {
1118 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1119 if (domain_sid == NULL) {
1123 /* cache the domain_sid in the ldb */
1124 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1128 talloc_steal(ldb, domain_sid);
1129 talloc_free(tmp_ctx);
1134 talloc_free(tmp_ctx);
1139 get domain sid from cache
1141 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1143 return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1146 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1148 TALLOC_CTX *tmp_ctx;
1149 struct dom_sid *dom_sid_new;
1150 struct dom_sid *dom_sid_old;
1152 /* see if we have a cached copy */
1153 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1154 "cache.domain_sid"), struct dom_sid);
1156 tmp_ctx = talloc_new(ldb);
1157 if (tmp_ctx == NULL) {
1161 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1166 /* cache the domain_sid in the ldb */
1167 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1171 talloc_steal(ldb, dom_sid_new);
1172 talloc_free(tmp_ctx);
1173 talloc_free(dom_sid_old);
1178 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1179 talloc_free(tmp_ctx);
1183 /* Obtain the short name of the flexible single master operator
1184 * (FSMO), such as the PDC Emulator */
1185 const char *samdb_result_fsmo_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
1188 /* Format is cn=NTDS Settings,cn=<NETBIOS name of FSMO>,.... */
1189 struct ldb_dn *fsmo_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, msg, attr);
1190 const struct ldb_val *val = ldb_dn_get_component_val(fsmo_dn, 1);
1191 const char *name = ldb_dn_get_component_name(fsmo_dn, 1);
1193 if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
1194 /* Ensure this matches the format. This gives us a
1195 * bit more confidence that a 'cn' value will be a
1200 return (char *)val->data;
1206 work out the ntds settings dn for the current open ldb
1208 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb)
1210 TALLOC_CTX *tmp_ctx;
1211 const char *root_attrs[] = { "dsServiceName", NULL };
1213 struct ldb_result *root_res;
1214 struct ldb_dn *settings_dn;
1216 /* see if we have a cached copy */
1217 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "cache.settings_dn");
1222 tmp_ctx = talloc_new(ldb);
1223 if (tmp_ctx == NULL) {
1227 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1229 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1230 ldb_errstring(ldb)));
1234 if (root_res->count != 1) {
1238 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1240 /* cache the domain_sid in the ldb */
1241 if (ldb_set_opaque(ldb, "cache.settings_dn", settings_dn) != LDB_SUCCESS) {
1245 talloc_steal(ldb, settings_dn);
1246 talloc_free(tmp_ctx);
1251 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1252 talloc_free(tmp_ctx);
1257 work out the ntds settings invocationId for the current open ldb
1259 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1261 TALLOC_CTX *tmp_ctx;
1262 const char *attrs[] = { "invocationId", NULL };
1264 struct ldb_result *res;
1265 struct GUID *invocation_id;
1267 /* see if we have a cached copy */
1268 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1269 if (invocation_id) {
1270 return invocation_id;
1273 tmp_ctx = talloc_new(ldb);
1274 if (tmp_ctx == NULL) {
1278 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1283 if (res->count != 1) {
1287 invocation_id = talloc(tmp_ctx, struct GUID);
1288 if (!invocation_id) {
1292 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1294 /* cache the domain_sid in the ldb */
1295 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1299 talloc_steal(ldb, invocation_id);
1300 talloc_free(tmp_ctx);
1302 return invocation_id;
1305 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1306 talloc_free(tmp_ctx);
1310 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1312 TALLOC_CTX *tmp_ctx;
1313 struct GUID *invocation_id_new;
1314 struct GUID *invocation_id_old;
1316 /* see if we have a cached copy */
1317 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1318 "cache.invocation_id");
1320 tmp_ctx = talloc_new(ldb);
1321 if (tmp_ctx == NULL) {
1325 invocation_id_new = talloc(tmp_ctx, struct GUID);
1326 if (!invocation_id_new) {
1330 *invocation_id_new = *invocation_id_in;
1332 /* cache the domain_sid in the ldb */
1333 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1337 talloc_steal(ldb, invocation_id_new);
1338 talloc_free(tmp_ctx);
1339 talloc_free(invocation_id_old);
1344 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1345 talloc_free(tmp_ctx);
1350 work out the ntds settings objectGUID for the current open ldb
1352 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1354 TALLOC_CTX *tmp_ctx;
1355 const char *attrs[] = { "objectGUID", NULL };
1357 struct ldb_result *res;
1358 struct GUID *ntds_guid;
1360 /* see if we have a cached copy */
1361 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1366 tmp_ctx = talloc_new(ldb);
1367 if (tmp_ctx == NULL) {
1371 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1376 if (res->count != 1) {
1380 ntds_guid = talloc(tmp_ctx, struct GUID);
1385 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1387 /* cache the domain_sid in the ldb */
1388 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1392 talloc_steal(ldb, ntds_guid);
1393 talloc_free(tmp_ctx);
1398 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1399 talloc_free(tmp_ctx);
1403 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1405 TALLOC_CTX *tmp_ctx;
1406 struct GUID *ntds_guid_new;
1407 struct GUID *ntds_guid_old;
1409 /* see if we have a cached copy */
1410 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1412 tmp_ctx = talloc_new(ldb);
1413 if (tmp_ctx == NULL) {
1417 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1418 if (!ntds_guid_new) {
1422 *ntds_guid_new = *ntds_guid_in;
1424 /* cache the domain_sid in the ldb */
1425 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1429 talloc_steal(ldb, ntds_guid_new);
1430 talloc_free(tmp_ctx);
1431 talloc_free(ntds_guid_old);
1436 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1437 talloc_free(tmp_ctx);
1442 work out the server dn for the current open ldb
1444 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1446 return ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb));
1450 work out the server dn for the current open ldb
1452 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1454 struct ldb_dn *server_dn;
1455 struct ldb_dn *server_site_dn;
1457 server_dn = samdb_server_dn(ldb, mem_ctx);
1458 if (!server_dn) return NULL;
1460 server_site_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1462 talloc_free(server_dn);
1463 return server_site_dn;
1467 find a 'reference' DN that points at another object
1468 (eg. serverReference, rIDManagerReference etc)
1470 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1471 const char *attribute, struct ldb_dn **dn)
1473 const char *attrs[2];
1474 struct ldb_result *res;
1477 attrs[0] = attribute;
1480 ret = ldb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, NULL);
1481 if (ret != LDB_SUCCESS) {
1484 if (res->count != 1) {
1486 return LDB_ERR_NO_SUCH_OBJECT;
1489 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1492 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1500 find our machine account via the serverReference attribute in the
1503 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1505 struct ldb_dn *server_dn;
1508 server_dn = samdb_server_dn(ldb, mem_ctx);
1509 if (server_dn == NULL) {
1510 return LDB_ERR_NO_SUCH_OBJECT;
1513 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1514 talloc_free(server_dn);
1520 find the RID Manager$ DN via the rIDManagerReference attribute in the
1523 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1525 return samdb_reference_dn(ldb, mem_ctx, samdb_base_dn(ldb), "rIDManagerReference", dn);
1529 find the RID Set DN via the rIDSetReferences attribute in our
1532 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1534 struct ldb_dn *server_ref_dn;
1537 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1538 if (ret != LDB_SUCCESS) {
1541 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1542 talloc_free(server_ref_dn);
1546 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1548 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb, mem_ctx));
1551 return (const char *) val->data;
1557 work out if we are the PDC for the domain of the current open ldb
1559 bool samdb_is_pdc(struct ldb_context *ldb)
1561 const char *dom_attrs[] = { "fSMORoleOwner", NULL };
1563 struct ldb_result *dom_res;
1564 TALLOC_CTX *tmp_ctx;
1568 tmp_ctx = talloc_new(ldb);
1569 if (tmp_ctx == NULL) {
1570 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1574 ret = ldb_search(ldb, tmp_ctx, &dom_res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, dom_attrs, NULL);
1576 DEBUG(1,("Searching for fSMORoleOwner in %s failed: %s\n",
1577 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1578 ldb_errstring(ldb)));
1581 if (dom_res->count != 1) {
1585 pdc = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, dom_res->msgs[0], "fSMORoleOwner");
1587 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), pdc) == 0) {
1593 talloc_free(tmp_ctx);
1598 DEBUG(1,("Failed to find if we are the PDC for this ldb\n"));
1599 talloc_free(tmp_ctx);
1604 work out if we are a Global Catalog server for the domain of the current open ldb
1606 bool samdb_is_gc(struct ldb_context *ldb)
1608 const char *attrs[] = { "options", NULL };
1610 struct ldb_result *res;
1611 TALLOC_CTX *tmp_ctx;
1613 tmp_ctx = talloc_new(ldb);
1614 if (tmp_ctx == NULL) {
1615 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1619 /* Query cn=ntds settings,.... */
1620 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1622 talloc_free(tmp_ctx);
1625 if (res->count != 1) {
1626 talloc_free(tmp_ctx);
1630 options = ldb_msg_find_attr_as_int(res->msgs[0], "options", 0);
1631 talloc_free(tmp_ctx);
1633 /* if options attribute has the 0x00000001 flag set, then enable the global catlog */
1634 if (options & 0x000000001) {
1640 /* Find a domain object in the parents of a particular DN. */
1641 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1642 struct ldb_dn **parent_dn, const char **errstring)
1644 TALLOC_CTX *local_ctx;
1645 struct ldb_dn *sdn = dn;
1646 struct ldb_result *res = NULL;
1648 const char *attrs[] = { NULL };
1650 local_ctx = talloc_new(mem_ctx);
1651 if (local_ctx == NULL) return LDB_ERR_OPERATIONS_ERROR;
1653 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1654 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1655 "(|(objectClass=domain)(objectClass=builtinDomain))");
1656 if (ret == LDB_SUCCESS) {
1657 if (res->count == 1) {
1665 if (ret != LDB_SUCCESS) {
1666 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1667 ldb_dn_get_linearized(dn),
1668 ldb_dn_get_linearized(sdn),
1669 ldb_errstring(ldb));
1670 talloc_free(local_ctx);
1673 if (res->count != 1) {
1674 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1675 ldb_dn_get_linearized(dn));
1676 DEBUG(0,(__location__ ": %s\n", *errstring));
1677 talloc_free(local_ctx);
1678 return LDB_ERR_CONSTRAINT_VIOLATION;
1681 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1682 talloc_free(local_ctx);
1688 * Performs checks on a user password (plaintext UNIX format - attribute
1689 * "password"). The remaining parameters have to be extracted from the domain
1692 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1694 enum samr_ValidationStatus samdb_check_password(const DATA_BLOB *password,
1695 const uint32_t pwdProperties,
1696 const uint32_t minPwdLength)
1698 /* checks if the "minPwdLength" property is satisfied */
1699 if (minPwdLength > password->length)
1700 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
1702 /* checks the password complexity */
1703 if (((pwdProperties & DOMAIN_PASSWORD_COMPLEX) != 0)
1704 && (password->data != NULL)
1705 && (!check_password_quality((const char *) password->data)))
1706 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1708 return SAMR_VALIDATION_STATUS_SUCCESS;
1712 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1713 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1714 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
1715 * gives some more informations if the changed failed.
1717 * The caller should have a LDB transaction wrapping this.
1719 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
1720 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
1721 * NT_STATUS_PASSWORD_RESTRICTION
1723 NTSTATUS samdb_set_password(struct ldb_context *ctx, TALLOC_CTX *mem_ctx,
1724 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
1725 struct ldb_message *mod,
1726 const DATA_BLOB *new_password,
1727 struct samr_Password *param_lmNewHash,
1728 struct samr_Password *param_ntNewHash,
1730 enum samPwdChangeReason *reject_reason,
1731 struct samr_DomInfo1 **_dominfo)
1733 const char * const user_attrs[] = { "userAccountControl",
1736 "dBCSPwd", "unicodePwd",
1738 "pwdLastSet", NULL };
1739 const char * const domain_attrs[] = { "minPwdLength", "pwdProperties",
1741 "maxPwdAge", "minPwdAge", NULL };
1743 uint32_t minPwdLength, pwdProperties, pwdHistoryLength;
1744 int64_t maxPwdAge, minPwdAge;
1745 uint32_t userAccountControl;
1746 struct samr_Password *sambaLMPwdHistory, *sambaNTPwdHistory,
1747 *lmPwdHash, *ntPwdHash, *lmNewHash, *ntNewHash;
1748 struct samr_Password local_lmNewHash, local_ntNewHash;
1749 int sambaLMPwdHistory_len, sambaNTPwdHistory_len;
1750 struct dom_sid *domain_sid;
1751 struct ldb_message **res;
1754 time_t now = time(NULL);
1758 /* we need to know the time to compute password age */
1759 unix_to_nt_time(&now_nt, now);
1761 /* pull all the user parameters */
1762 count = gendb_search_dn(ctx, mem_ctx, user_dn, &res, user_attrs);
1764 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1766 userAccountControl = samdb_result_uint(res[0], "userAccountControl", 0);
1767 sambaLMPwdHistory_len = samdb_result_hashes(mem_ctx, res[0],
1768 "lmPwdHistory", &sambaLMPwdHistory);
1769 sambaNTPwdHistory_len = samdb_result_hashes(mem_ctx, res[0],
1770 "ntPwdHistory", &sambaNTPwdHistory);
1771 lmPwdHash = samdb_result_hash(mem_ctx, res[0], "dBCSPwd");
1772 ntPwdHash = samdb_result_hash(mem_ctx, res[0], "unicodePwd");
1773 pwdLastSet = samdb_result_uint64(res[0], "pwdLastSet", 0);
1775 /* Copy parameters */
1776 lmNewHash = param_lmNewHash;
1777 ntNewHash = param_ntNewHash;
1779 /* Only non-trust accounts have restrictions (possibly this
1780 * test is the wrong way around, but I like to be restrictive
1782 restrictions = !(userAccountControl & (UF_INTERDOMAIN_TRUST_ACCOUNT
1783 |UF_WORKSTATION_TRUST_ACCOUNT
1784 |UF_SERVER_TRUST_ACCOUNT));
1786 if (domain_dn != NULL) {
1787 /* pull the domain parameters */
1788 count = gendb_search_dn(ctx, mem_ctx, domain_dn, &res,
1791 DEBUG(2, ("samdb_set_password: Domain DN %s is invalid, for user %s\n",
1792 ldb_dn_get_linearized(domain_dn),
1793 ldb_dn_get_linearized(user_dn)));
1794 return NT_STATUS_NO_SUCH_DOMAIN;
1797 /* work out the domain sid, and pull the domain from there */
1798 domain_sid = samdb_result_sid_prefix(mem_ctx, res[0],
1800 if (domain_sid == NULL) {
1801 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1804 count = gendb_search(ctx, mem_ctx, NULL, &res, domain_attrs,
1806 ldap_encode_ndr_dom_sid(mem_ctx, domain_sid));
1808 DEBUG(2, ("samdb_set_password: Could not find domain to match SID: %s, for user %s\n",
1809 dom_sid_string(mem_ctx, domain_sid),
1810 ldb_dn_get_linearized(user_dn)));
1811 return NT_STATUS_NO_SUCH_DOMAIN;
1815 minPwdLength = samdb_result_uint(res[0], "minPwdLength", 0);
1816 pwdProperties = samdb_result_uint(res[0], "pwdProperties", 0);
1817 pwdHistoryLength = samdb_result_uint(res[0], "pwdHistoryLength", 0);
1818 maxPwdAge = samdb_result_int64(res[0], "maxPwdAge", 0);
1819 minPwdAge = samdb_result_int64(res[0], "minPwdAge", 0);
1821 if ((userAccountControl & UF_PASSWD_NOTREQD) != 0) {
1822 /* see [MS-ADTS] 2.2.15 */
1826 if (_dominfo != NULL) {
1827 struct samr_DomInfo1 *dominfo;
1828 /* on failure we need to fill in the reject reasons */
1829 dominfo = talloc(mem_ctx, struct samr_DomInfo1);
1830 if (dominfo == NULL) {
1831 return NT_STATUS_NO_MEMORY;
1833 dominfo->min_password_length = minPwdLength;
1834 dominfo->password_properties = pwdProperties;
1835 dominfo->password_history_length = pwdHistoryLength;
1836 dominfo->max_password_age = maxPwdAge;
1837 dominfo->min_password_age = minPwdAge;
1838 *_dominfo = dominfo;
1841 if ((restrictions != 0) && (new_password != 0)) {
1844 /* checks if the "minPwdLength" property is satisfied */
1845 if ((restrictions != 0)
1846 && (minPwdLength > utf16_len_n(
1847 new_password->data, new_password->length)/2)) {
1848 if (reject_reason) {
1849 *reject_reason = SAM_PWD_CHANGE_PASSWORD_TOO_SHORT;
1851 return NT_STATUS_PASSWORD_RESTRICTION;
1854 /* Create the NT hash */
1855 mdfour(local_ntNewHash.hash, new_password->data,
1856 new_password->length);
1858 ntNewHash = &local_ntNewHash;
1860 /* Only check complexity if we can convert it at all. Assuming unconvertable passwords are 'strong' */
1861 if (convert_string_talloc_convenience(mem_ctx,
1862 lp_iconv_convenience(ldb_get_opaque(ctx, "loadparm")),
1864 new_password->data, new_password->length,
1865 (void **)&new_pass, NULL, false)) {
1867 /* checks the password complexity */
1868 if ((restrictions != 0)
1870 & DOMAIN_PASSWORD_COMPLEX) != 0)
1871 && (!check_password_quality(new_pass))) {
1872 if (reject_reason) {
1873 *reject_reason = SAM_PWD_CHANGE_NOT_COMPLEX;
1875 return NT_STATUS_PASSWORD_RESTRICTION;
1878 /* compute the new lm hashes (for checking history - case insenitivly!) */
1879 if (E_deshash(new_pass, local_lmNewHash.hash)) {
1880 lmNewHash = &local_lmNewHash;
1885 if ((restrictions != 0) && user_change) {
1886 /* are all password changes disallowed? */
1887 if ((pwdProperties & DOMAIN_REFUSE_PASSWORD_CHANGE) != 0) {
1888 if (reject_reason) {
1889 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
1891 return NT_STATUS_PASSWORD_RESTRICTION;
1894 /* can this user change the password? */
1895 if ((userAccountControl & UF_PASSWD_CANT_CHANGE) != 0) {
1896 if (reject_reason) {
1897 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
1899 return NT_STATUS_PASSWORD_RESTRICTION;
1902 /* Password minimum age: yes, this is a minus. The ages are in negative 100nsec units! */
1903 if (pwdLastSet - minPwdAge > now_nt) {
1904 if (reject_reason) {
1905 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
1907 return NT_STATUS_PASSWORD_RESTRICTION;
1910 /* check the immediately past password */
1911 if (pwdHistoryLength > 0) {
1912 if (lmNewHash && lmPwdHash && memcmp(lmNewHash->hash,
1913 lmPwdHash->hash, 16) == 0) {
1914 if (reject_reason) {
1915 *reject_reason = SAM_PWD_CHANGE_PWD_IN_HISTORY;
1917 return NT_STATUS_PASSWORD_RESTRICTION;
1919 if (ntNewHash && ntPwdHash && memcmp(ntNewHash->hash,
1920 ntPwdHash->hash, 16) == 0) {
1921 if (reject_reason) {
1922 *reject_reason = SAM_PWD_CHANGE_PWD_IN_HISTORY;
1924 return NT_STATUS_PASSWORD_RESTRICTION;
1928 /* check the password history */
1929 sambaLMPwdHistory_len = MIN(sambaLMPwdHistory_len,
1931 sambaNTPwdHistory_len = MIN(sambaNTPwdHistory_len,
1934 for (i=0; lmNewHash && i<sambaLMPwdHistory_len;i++) {
1935 if (memcmp(lmNewHash->hash, sambaLMPwdHistory[i].hash,
1937 if (reject_reason) {
1938 *reject_reason = SAM_PWD_CHANGE_PWD_IN_HISTORY;
1940 return NT_STATUS_PASSWORD_RESTRICTION;
1943 for (i=0; ntNewHash && i<sambaNTPwdHistory_len;i++) {
1944 if (memcmp(ntNewHash->hash, sambaNTPwdHistory[i].hash,
1946 if (reject_reason) {
1947 *reject_reason = SAM_PWD_CHANGE_PWD_IN_HISTORY;
1949 return NT_STATUS_PASSWORD_RESTRICTION;
1954 #define CHECK_RET(x) do { if (x != 0) return NT_STATUS_NO_MEMORY; } while(0)
1956 /* the password is acceptable. Start forming the new fields */
1957 if (new_password != NULL) {
1958 /* if we know the cleartext UTF16 password, then set it.
1959 * Modules in ldb will set all the appropriate
1961 CHECK_RET(ldb_msg_add_value(mod, "clearTextPassword", new_password, NULL));
1963 /* we don't have the cleartext, so set what we have of the
1967 CHECK_RET(samdb_msg_add_hash(ctx, mem_ctx, mod, "dBCSPwd", lmNewHash));
1971 CHECK_RET(samdb_msg_add_hash(ctx, mem_ctx, mod, "unicodePwd", ntNewHash));
1975 if (reject_reason) {
1976 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
1978 return NT_STATUS_OK;
1983 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1984 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1985 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
1986 * gives some more informations if the changed failed.
1988 * This wrapper function for "samdb_set_password" takes a SID as input rather
1991 * This call encapsulates a new LDB transaction for changing the password;
1992 * therefore the user hasn't to start a new one.
1994 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
1995 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
1996 * NT_STATUS_PASSWORD_RESTRICTION
1998 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1999 const struct dom_sid *user_sid,
2000 const DATA_BLOB *new_password,
2001 struct samr_Password *lmNewHash,
2002 struct samr_Password *ntNewHash,
2004 enum samPwdChangeReason *reject_reason,
2005 struct samr_DomInfo1 **_dominfo)
2008 struct ldb_dn *user_dn;
2009 struct ldb_message *msg;
2012 ret = ldb_transaction_start(ldb);
2013 if (ret != LDB_SUCCESS) {
2014 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2015 return NT_STATUS_TRANSACTION_ABORTED;
2018 user_dn = samdb_search_dn(ldb, mem_ctx, NULL,
2019 "(&(objectSid=%s)(objectClass=user))",
2020 ldap_encode_ndr_dom_sid(mem_ctx, user_sid));
2022 ldb_transaction_cancel(ldb);
2023 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2024 dom_sid_string(mem_ctx, user_sid)));
2025 return NT_STATUS_NO_SUCH_USER;
2028 msg = ldb_msg_new(mem_ctx);
2030 ldb_transaction_cancel(ldb);
2031 talloc_free(user_dn);
2032 return NT_STATUS_NO_MEMORY;
2035 msg->dn = ldb_dn_copy(msg, user_dn);
2037 ldb_transaction_cancel(ldb);
2038 talloc_free(user_dn);
2040 return NT_STATUS_NO_MEMORY;
2043 nt_status = samdb_set_password(ldb, mem_ctx,
2046 lmNewHash, ntNewHash,
2048 reject_reason, _dominfo);
2049 if (!NT_STATUS_IS_OK(nt_status)) {
2050 ldb_transaction_cancel(ldb);
2051 talloc_free(user_dn);
2056 /* modify the samdb record */
2057 ret = dsdb_replace(ldb, msg, 0);
2058 if (ret != LDB_SUCCESS) {
2059 ldb_transaction_cancel(ldb);
2060 talloc_free(user_dn);
2062 return NT_STATUS_ACCESS_DENIED;
2067 ret = ldb_transaction_commit(ldb);
2068 if (ret != LDB_SUCCESS) {
2069 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2070 ldb_dn_get_linearized(user_dn),
2071 ldb_errstring(ldb)));
2072 talloc_free(user_dn);
2073 return NT_STATUS_TRANSACTION_ABORTED;
2076 talloc_free(user_dn);
2077 return NT_STATUS_OK;
2081 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2082 struct dom_sid *sid, struct ldb_dn **ret_dn)
2084 struct ldb_message *msg;
2085 struct ldb_dn *basedn;
2089 sidstr = dom_sid_string(mem_ctx, sid);
2090 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2092 /* We might have to create a ForeignSecurityPrincipal, even if this user
2093 * is in our own domain */
2095 msg = ldb_msg_new(sidstr);
2097 talloc_free(sidstr);
2098 return NT_STATUS_NO_MEMORY;
2101 ret = dsdb_wellknown_dn(sam_ctx, sidstr, samdb_base_dn(sam_ctx),
2102 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2104 if (ret != LDB_SUCCESS) {
2105 DEBUG(0, ("Failed to find DN for "
2106 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2107 talloc_free(sidstr);
2108 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2111 /* add core elements to the ldb_message for the alias */
2113 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2114 talloc_free(sidstr);
2115 return NT_STATUS_NO_MEMORY;
2118 samdb_msg_add_string(sam_ctx, msg, msg,
2120 "foreignSecurityPrincipal");
2122 /* create the alias */
2123 ret = ldb_add(sam_ctx, msg);
2124 if (ret != LDB_SUCCESS) {
2125 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2127 ldb_dn_get_linearized(msg->dn),
2128 ldb_errstring(sam_ctx)));
2129 talloc_free(sidstr);
2130 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2133 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2134 talloc_free(sidstr);
2136 return NT_STATUS_OK;
2141 Find the DN of a domain, assuming it to be a dotted.dns name
2144 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2147 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2148 const char *binary_encoded;
2149 const char **split_realm;
2156 split_realm = (const char **)str_list_make(tmp_ctx, dns_domain, ".");
2158 talloc_free(tmp_ctx);
2161 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2162 for (i=0; split_realm[i]; i++) {
2163 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2164 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2165 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2166 binary_encoded, ldb_dn_get_linearized(dn)));
2167 talloc_free(tmp_ctx);
2171 if (!ldb_dn_validate(dn)) {
2172 DEBUG(2, ("Failed to validated DN %s\n",
2173 ldb_dn_get_linearized(dn)));
2174 talloc_free(tmp_ctx);
2177 talloc_free(tmp_ctx);
2182 Find the DN of a domain, be it the netbios or DNS name
2184 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2185 const char *domain_name)
2187 const char * const domain_ref_attrs[] = {
2190 const char * const domain_ref2_attrs[] = {
2193 struct ldb_result *res_domain_ref;
2194 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2195 /* find the domain's DN */
2196 int ret_domain = ldb_search(ldb, mem_ctx,
2198 samdb_partitions_dn(ldb, mem_ctx),
2201 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2203 if (ret_domain != 0) {
2207 if (res_domain_ref->count == 0) {
2208 ret_domain = ldb_search(ldb, mem_ctx,
2210 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2213 "(objectclass=domain)");
2214 if (ret_domain != 0) {
2218 if (res_domain_ref->count == 1) {
2219 return res_domain_ref->msgs[0]->dn;
2224 if (res_domain_ref->count > 1) {
2225 DEBUG(0,("Found %d records matching domain [%s]\n",
2226 ret_domain, domain_name));
2230 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
2236 use a GUID to find a DN
2238 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
2239 TALLOC_CTX *mem_ctx,
2240 const char *guid_str, struct ldb_dn **dn)
2243 struct ldb_result *res;
2244 const char *attrs[] = { NULL };
2246 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2247 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2248 DSDB_SEARCH_SHOW_EXTENDED_DN,
2249 "objectGUID=%s", guid_str);
2250 if (ret != LDB_SUCCESS) {
2253 if (res->count == 0) {
2255 return LDB_ERR_NO_SUCH_OBJECT;
2257 if (res->count != 1) {
2258 DEBUG(1,(__location__ ": found %u records with GUID %s\n", res->count, guid_str));
2260 return LDB_ERR_OPERATIONS_ERROR;
2263 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2270 use a DN to find a GUID with a given attribute name
2272 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
2273 struct ldb_dn *dn, const char *attribute,
2277 struct ldb_result *res;
2278 const char *attrs[2];
2279 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2281 attrs[0] = attribute;
2284 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs, DSDB_SEARCH_SHOW_DELETED);
2285 if (ret != LDB_SUCCESS) {
2286 talloc_free(tmp_ctx);
2289 if (res->count < 1) {
2290 talloc_free(tmp_ctx);
2291 return LDB_ERR_NO_SUCH_OBJECT;
2293 *guid = samdb_result_guid(res->msgs[0], attribute);
2294 talloc_free(tmp_ctx);
2299 use a DN to find a GUID
2301 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
2302 struct ldb_dn *dn, struct GUID *guid)
2304 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
2310 adds the given GUID to the given ldb_message. This value is added
2311 for the given attr_name (may be either "objectGUID" or "parentGUID").
2313 int dsdb_msg_add_guid(struct ldb_message *msg,
2315 const char *attr_name)
2320 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
2322 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
2323 if (!NT_STATUS_IS_OK(status)) {
2324 ret = LDB_ERR_OPERATIONS_ERROR;
2328 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
2329 if (ret != LDB_SUCCESS) {
2330 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
2338 talloc_free(tmp_ctx);
2345 use a DN to find a SID
2347 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
2348 struct ldb_dn *dn, struct dom_sid *sid)
2351 struct ldb_result *res;
2352 const char *attrs[] = { "objectSID", NULL };
2353 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2358 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs, DSDB_SEARCH_SHOW_DELETED);
2359 if (ret != LDB_SUCCESS) {
2360 talloc_free(tmp_ctx);
2363 if (res->count < 1) {
2364 talloc_free(tmp_ctx);
2365 return LDB_ERR_NO_SUCH_OBJECT;
2367 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSID");
2369 talloc_free(tmp_ctx);
2370 return LDB_ERR_NO_SUCH_OBJECT;
2373 talloc_free(tmp_ctx);
2380 load a repsFromTo blob list for a given partition GUID
2381 attr must be "repsFrom" or "repsTo"
2383 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2384 const char *attr, struct repsFromToBlob **r, uint32_t *count)
2386 const char *attrs[] = { attr, NULL };
2387 struct ldb_result *res = NULL;
2388 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2390 struct ldb_message_element *el;
2395 if (ldb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, NULL) != LDB_SUCCESS ||
2397 DEBUG(0,("dsdb_loadreps: failed to read partition object\n"));
2398 talloc_free(tmp_ctx);
2399 return WERR_DS_DRA_INTERNAL_ERROR;
2402 el = ldb_msg_find_element(res->msgs[0], attr);
2404 /* it's OK to be empty */
2405 talloc_free(tmp_ctx);
2409 *count = el->num_values;
2410 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
2412 talloc_free(tmp_ctx);
2413 return WERR_DS_DRA_INTERNAL_ERROR;
2416 for (i=0; i<(*count); i++) {
2417 enum ndr_err_code ndr_err;
2418 ndr_err = ndr_pull_struct_blob(&el->values[i],
2419 mem_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
2421 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
2422 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2423 talloc_free(tmp_ctx);
2424 return WERR_DS_DRA_INTERNAL_ERROR;
2428 talloc_free(tmp_ctx);
2434 save the repsFromTo blob list for a given partition GUID
2435 attr must be "repsFrom" or "repsTo"
2437 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2438 const char *attr, struct repsFromToBlob *r, uint32_t count)
2440 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2441 struct ldb_message *msg;
2442 struct ldb_message_element *el;
2445 msg = ldb_msg_new(tmp_ctx);
2447 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
2451 el->values = talloc_array(msg, struct ldb_val, count);
2456 for (i=0; i<count; i++) {
2458 enum ndr_err_code ndr_err;
2460 ndr_err = ndr_push_struct_blob(&v, tmp_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
2462 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
2463 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2471 if (ldb_modify(sam_ctx, msg) != LDB_SUCCESS) {
2472 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
2476 talloc_free(tmp_ctx);
2481 talloc_free(tmp_ctx);
2482 return WERR_DS_DRA_INTERNAL_ERROR;
2487 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2488 object for a partition
2490 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
2491 uint64_t *uSN, uint64_t *urgent_uSN)
2493 struct ldb_request *req;
2495 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2496 struct dsdb_control_current_partition *p_ctrl;
2497 struct ldb_result *res;
2499 res = talloc_zero(tmp_ctx, struct ldb_result);
2501 talloc_free(tmp_ctx);
2502 return LDB_ERR_OPERATIONS_ERROR;
2505 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
2506 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
2510 res, ldb_search_default_callback,
2512 if (ret != LDB_SUCCESS) {
2513 talloc_free(tmp_ctx);
2517 p_ctrl = talloc(req, struct dsdb_control_current_partition);
2518 if (p_ctrl == NULL) {
2520 return LDB_ERR_OPERATIONS_ERROR;
2522 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2526 ret = ldb_request_add_control(req,
2527 DSDB_CONTROL_CURRENT_PARTITION_OID,
2529 if (ret != LDB_SUCCESS) {
2530 talloc_free(tmp_ctx);
2534 /* Run the new request */
2535 ret = ldb_request(ldb, req);
2537 if (ret == LDB_SUCCESS) {
2538 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2541 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2542 /* it hasn't been created yet, which means
2543 an implicit value of zero */
2545 talloc_free(tmp_ctx);
2549 if (ret != LDB_SUCCESS) {
2550 talloc_free(tmp_ctx);
2554 if (res->count < 1) {
2560 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
2562 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
2566 talloc_free(tmp_ctx);
2572 save uSNHighest and uSNUrgent attributes in the @REPLCHANGED object for a
2575 int dsdb_save_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
2576 uint64_t uSN, uint64_t urgent_uSN)
2578 struct ldb_request *req;
2579 struct ldb_message *msg;
2580 struct dsdb_control_current_partition *p_ctrl;
2583 msg = ldb_msg_new(ldb);
2585 return LDB_ERR_OPERATIONS_ERROR;
2588 msg->dn = ldb_dn_new(msg, ldb, "@REPLCHANGED");
2589 if (msg->dn == NULL) {
2591 return LDB_ERR_OPERATIONS_ERROR;
2594 ret = ldb_msg_add_fmt(msg, "uSNHighest", "%llu", (unsigned long long)uSN);
2595 if (ret != LDB_SUCCESS) {
2599 msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
2601 /* urgent_uSN is optional so may not be stored */
2603 ret = ldb_msg_add_fmt(msg, "uSNUrgent", "%llu", (unsigned long long)urgent_uSN);
2604 if (ret != LDB_SUCCESS) {
2608 msg->elements[1].flags = LDB_FLAG_MOD_REPLACE;
2612 p_ctrl = talloc(msg, struct dsdb_control_current_partition);
2613 if (p_ctrl == NULL) {
2615 return LDB_ERR_OPERATIONS_ERROR;
2617 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2620 ret = ldb_build_mod_req(&req, ldb, msg,
2623 NULL, ldb_op_default_callback,
2626 if (ret != LDB_SUCCESS) {
2631 ret = ldb_request_add_control(req,
2632 DSDB_CONTROL_CURRENT_PARTITION_OID,
2634 if (ret != LDB_SUCCESS) {
2639 /* Run the new request */
2640 ret = ldb_request(ldb, req);
2642 if (ret == LDB_SUCCESS) {
2643 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2645 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2646 ret = ldb_build_add_req(&req, ldb, msg,
2649 NULL, ldb_op_default_callback,
2659 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
2660 const struct drsuapi_DsReplicaCursor2 *c2)
2662 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2665 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
2666 const struct drsuapi_DsReplicaCursor *c2)
2668 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2672 see if we are a RODC
2674 TODO: This should take a sam_ctx, and lookup the right object (with
2677 bool samdb_rodc(struct loadparm_context *lp_ctx)
2679 return lp_parm_bool(lp_ctx, NULL, "repl", "RODC", false);
2684 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
2686 flags are DS_NTDS_OPTION_*
2688 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
2690 TALLOC_CTX *tmp_ctx;
2691 const char *attrs[] = { "options", NULL };
2693 struct ldb_result *res;
2695 tmp_ctx = talloc_new(ldb);
2696 if (tmp_ctx == NULL) {
2700 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
2705 if (res->count != 1) {
2709 *options = samdb_result_uint(res->msgs[0], "options", 0);
2711 talloc_free(tmp_ctx);
2716 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
2717 talloc_free(tmp_ctx);
2718 return LDB_ERR_NO_SUCH_OBJECT;
2722 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
2723 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
2725 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
2727 char **tokens, *ret;
2730 tokens = str_list_make(mem_ctx, cn, " -_");
2734 /* "tolower()" and "toupper()" should also work properly on 0x00 */
2735 tokens[0][0] = tolower(tokens[0][0]);
2736 for (i = 1; i < str_list_length((const char **)tokens); i++)
2737 tokens[i][0] = toupper(tokens[i][0]);
2739 ret = talloc_strdup(mem_ctx, tokens[0]);
2740 for (i = 1; i < str_list_length((const char **)tokens); i++)
2741 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
2743 talloc_free(tokens);
2749 return domain functional level
2750 returns DS_DOMAIN_FUNCTION_*
2752 int dsdb_functional_level(struct ldb_context *ldb)
2754 int *domainFunctionality =
2755 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
2756 if (!domainFunctionality) {
2757 DEBUG(0,(__location__ ": WARNING: domainFunctionality not setup\n"));
2758 return DS_DOMAIN_FUNCTION_2000;
2760 return *domainFunctionality;
2764 set a GUID in an extended DN structure
2766 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
2772 status = GUID_to_ndr_blob(guid, dn, &v);
2773 if (!NT_STATUS_IS_OK(status)) {
2774 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
2777 ret = ldb_dn_set_extended_component(dn, component_name, &v);
2783 return a GUID from a extended DN structure
2785 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
2787 const struct ldb_val *v;
2789 v = ldb_dn_get_extended_component(dn, component_name);
2791 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2794 return GUID_from_ndr_blob(v, guid);
2798 return a uint64_t from a extended DN structure
2800 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
2802 const struct ldb_val *v;
2805 v = ldb_dn_get_extended_component(dn, component_name);
2807 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2809 s = talloc_strndup(dn, (const char *)v->data, v->length);
2810 NT_STATUS_HAVE_NO_MEMORY(s);
2812 *val = strtoull(s, NULL, 0);
2815 return NT_STATUS_OK;
2819 return a NTTIME from a extended DN structure
2821 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
2823 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
2827 return a uint32_t from a extended DN structure
2829 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
2831 const struct ldb_val *v;
2834 v = ldb_dn_get_extended_component(dn, component_name);
2836 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2839 s = talloc_strndup(dn, (const char *)v->data, v->length);
2840 NT_STATUS_HAVE_NO_MEMORY(s);
2842 *val = strtoul(s, NULL, 0);
2845 return NT_STATUS_OK;
2849 return RMD_FLAGS directly from a ldb_dn
2850 returns 0 if not found
2852 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
2854 const struct ldb_val *v;
2856 v = ldb_dn_get_extended_component(dn, "RMD_FLAGS");
2857 if (!v || v->length > sizeof(buf)-1) return 0;
2858 strncpy(buf, (const char *)v->data, v->length);
2860 return strtoul(buf, NULL, 10);
2864 return RMD_FLAGS directly from a ldb_val for a DN
2865 returns 0 if RMD_FLAGS is not found
2867 uint32_t dsdb_dn_val_rmd_flags(struct ldb_val *val)
2873 if (val->length < 13) {
2876 p = memmem(val->data, val->length-2, "<RMD_FLAGS=", 11);
2880 flags = strtoul(p+11, &end, 10);
2881 if (!end || *end != '>') {
2882 /* it must end in a > */
2889 return true if a ldb_val containing a DN in storage form is deleted
2891 bool dsdb_dn_is_deleted_val(struct ldb_val *val)
2893 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
2897 return true if a ldb_val containing a DN in storage form is
2898 in the upgraded w2k3 linked attribute format
2900 bool dsdb_dn_is_upgraded_link_val(struct ldb_val *val)
2902 return memmem(val->data, val->length, "<RMD_ADDTIME=", 13) != NULL;
2906 return a DN for a wellknown GUID
2908 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
2909 struct ldb_dn *nc_root, const char *wk_guid,
2910 struct ldb_dn **wkguid_dn)
2912 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2913 const char *attrs[] = { NULL };
2916 struct ldb_result *res;
2918 /* construct the magic WKGUID DN */
2919 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
2920 wk_guid, ldb_dn_get_linearized(nc_root));
2922 talloc_free(tmp_ctx);
2923 return LDB_ERR_OPERATIONS_ERROR;
2926 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs, DSDB_SEARCH_SHOW_DELETED);
2927 if (ret != LDB_SUCCESS) {
2928 talloc_free(tmp_ctx);
2932 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
2933 talloc_free(tmp_ctx);
2938 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
2940 return ldb_dn_compare(*dn1, *dn2);
2944 find a NC root given a DN within the NC
2946 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2947 struct ldb_dn **nc_root)
2949 const char *root_attrs[] = { "namingContexts", NULL };
2950 TALLOC_CTX *tmp_ctx;
2952 struct ldb_message_element *el;
2953 struct ldb_result *root_res;
2955 struct ldb_dn **nc_dns;
2957 tmp_ctx = talloc_new(samdb);
2958 if (tmp_ctx == NULL) {
2959 return LDB_ERR_OPERATIONS_ERROR;
2962 ret = ldb_search(samdb, tmp_ctx, &root_res,
2963 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
2965 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
2966 talloc_free(tmp_ctx);
2970 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
2972 DEBUG(1,("Finding namingContexts element in root_res failed: %s\n",
2973 ldb_errstring(samdb)));
2974 talloc_free(tmp_ctx);
2975 return LDB_ERR_NO_SUCH_ATTRIBUTE;
2978 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
2980 talloc_free(tmp_ctx);
2981 return LDB_ERR_OPERATIONS_ERROR;
2984 for (i=0; i<el->num_values; i++) {
2985 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
2986 if (nc_dns[i] == NULL) {
2987 talloc_free(tmp_ctx);
2988 return LDB_ERR_OPERATIONS_ERROR;
2992 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
2994 for (i=0; i<el->num_values; i++) {
2995 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
2996 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
2997 talloc_free(tmp_ctx);
3002 talloc_free(tmp_ctx);
3003 return LDB_ERR_NO_SUCH_OBJECT;
3008 find the deleted objects DN for any object, by looking for the NC
3009 root, then looking up the wellknown GUID
3011 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
3012 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
3013 struct ldb_dn **do_dn)
3015 struct ldb_dn *nc_root;
3018 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
3019 if (ret != LDB_SUCCESS) {
3023 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
3024 talloc_free(nc_root);
3029 return the tombstoneLifetime, in days
3031 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
3034 dn = samdb_config_dn(ldb);
3036 return LDB_ERR_NO_SUCH_OBJECT;
3038 dn = ldb_dn_copy(ldb, dn);
3040 return LDB_ERR_OPERATIONS_ERROR;
3042 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3043 be a wellknown GUID for this */
3044 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3046 return LDB_ERR_OPERATIONS_ERROR;
3049 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
3055 compare a ldb_val to a string case insensitively
3057 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
3059 size_t len = strlen(s);
3061 if (len > v->length) return 1;
3062 ret = strncasecmp(s, (const char *)v->data, v->length);
3063 if (ret != 0) return ret;
3064 if (v->length > len && v->data[len] != 0) {
3072 load the UDV for a partition in v2 format
3073 The list is returned sorted, and with our local cursor added
3075 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3076 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
3078 static const char *attrs[] = { "replUpToDateVector", NULL };
3079 struct ldb_result *r;
3080 const struct ldb_val *ouv_value;
3082 uint64_t highest_usn;
3083 const struct GUID *our_invocation_id;
3084 struct timeval now = timeval_current();
3086 ret = ldb_search(samdb, mem_ctx, &r, dn, LDB_SCOPE_BASE, attrs, NULL);
3087 if (ret != LDB_SUCCESS) {
3091 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
3093 enum ndr_err_code ndr_err;
3094 struct replUpToDateVectorBlob ouv;
3096 ndr_err = ndr_pull_struct_blob(ouv_value, r,
3097 lp_iconv_convenience(ldb_get_opaque(samdb, "loadparm")), &ouv,
3098 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
3099 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3101 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3103 if (ouv.version != 2) {
3104 /* we always store as version 2, and
3105 * replUpToDateVector is not replicated
3107 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3110 *count = ouv.ctr.ctr2.count;
3111 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
3119 our_invocation_id = samdb_ntds_invocation_id(samdb);
3120 if (!our_invocation_id) {
3121 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
3122 talloc_free(*cursors);
3123 return LDB_ERR_OPERATIONS_ERROR;
3126 ret = dsdb_load_partition_usn(samdb, dn, &highest_usn, NULL);
3127 if (ret != LDB_SUCCESS) {
3128 /* nothing to add - this can happen after a vampire */
3129 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3133 for (i=0; i<*count; i++) {
3134 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
3135 (*cursors)[i].highest_usn = highest_usn;
3136 (*cursors)[i].last_sync_success = timeval_to_nttime(&now);
3137 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3142 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
3144 return LDB_ERR_OPERATIONS_ERROR;
3147 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
3148 (*cursors)[*count].highest_usn = highest_usn;
3149 (*cursors)[*count].last_sync_success = timeval_to_nttime(&now);
3152 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3158 load the UDV for a partition in version 1 format
3159 The list is returned sorted, and with our local cursor added
3161 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3162 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
3164 struct drsuapi_DsReplicaCursor2 *v2;
3167 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
3168 if (ret != LDB_SUCCESS) {
3178 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
3179 if (*cursors == NULL) {
3181 return LDB_ERR_OPERATIONS_ERROR;
3184 for (i=0; i<*count; i++) {
3185 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
3186 (*cursors)[i].highest_usn = v2[i].highest_usn;
3193 add a set of controls to a ldb_request structure based on a set of
3194 flags. See util.h for a list of available flags
3196 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
3199 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
3200 struct ldb_search_options_control *options;
3201 /* Using the phantom root control allows us to search all partitions */
3202 options = talloc(req, struct ldb_search_options_control);
3203 if (options == NULL) {
3204 return LDB_ERR_OPERATIONS_ERROR;
3206 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
3208 ret = ldb_request_add_control(req,
3209 LDB_CONTROL_SEARCH_OPTIONS_OID,
3211 if (ret != LDB_SUCCESS) {
3216 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
3217 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
3218 if (ret != LDB_SUCCESS) {
3223 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
3224 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, true, NULL);
3225 if (ret != LDB_SUCCESS) {
3230 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
3231 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
3232 if (!extended_ctrl) {
3233 return LDB_ERR_OPERATIONS_ERROR;
3235 extended_ctrl->type = 1;
3237 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
3238 if (ret != LDB_SUCCESS) {
3243 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
3244 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
3245 if (ret != LDB_SUCCESS) {
3250 if (dsdb_flags & DSDB_MODIFY_RELAX) {
3251 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
3252 if (ret != LDB_SUCCESS) {
3257 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
3258 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
3259 if (ret != LDB_SUCCESS) {
3264 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
3265 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
3266 if (ret != LDB_SUCCESS) {
3275 a modify with a set of controls
3277 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
3278 uint32_t dsdb_flags)
3280 struct ldb_request *req;
3283 ret = ldb_build_mod_req(&req, ldb, ldb,
3287 ldb_op_default_callback,
3290 if (ret != LDB_SUCCESS) return ret;
3292 ret = dsdb_request_add_controls(req, dsdb_flags);
3293 if (ret != LDB_SUCCESS) {
3298 ret = dsdb_autotransaction_request(ldb, req);
3305 like dsdb_modify() but set all the element flags to
3306 LDB_FLAG_MOD_REPLACE
3308 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
3312 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3313 for (i=0;i<msg->num_elements;i++) {
3314 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3317 return dsdb_modify(ldb, msg, dsdb_flags);
3322 search for attrs on one DN, allowing for dsdb_flags controls
3324 int dsdb_search_dn(struct ldb_context *ldb,
3325 TALLOC_CTX *mem_ctx,
3326 struct ldb_result **_res,
3327 struct ldb_dn *basedn,
3328 const char * const *attrs,
3329 uint32_t dsdb_flags)
3332 struct ldb_request *req;
3333 struct ldb_result *res;
3335 res = talloc_zero(mem_ctx, struct ldb_result);
3337 return LDB_ERR_OPERATIONS_ERROR;
3340 ret = ldb_build_search_req(&req, ldb, res,
3347 ldb_search_default_callback,
3349 if (ret != LDB_SUCCESS) {
3354 ret = dsdb_request_add_controls(req, dsdb_flags);
3355 if (ret != LDB_SUCCESS) {
3360 ret = ldb_request(ldb, req);
3361 if (ret == LDB_SUCCESS) {
3362 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3366 if (ret != LDB_SUCCESS) {
3376 general search with dsdb_flags for controls
3378 int dsdb_search(struct ldb_context *ldb,
3379 TALLOC_CTX *mem_ctx,
3380 struct ldb_result **_res,
3381 struct ldb_dn *basedn,
3382 enum ldb_scope scope,
3383 const char * const *attrs,
3384 uint32_t dsdb_flags,
3385 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
3388 struct ldb_request *req;
3389 struct ldb_result *res;
3391 char *expression = NULL;
3392 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3394 res = talloc_zero(tmp_ctx, struct ldb_result);
3396 talloc_free(tmp_ctx);
3397 return LDB_ERR_OPERATIONS_ERROR;
3401 va_start(ap, exp_fmt);
3402 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
3406 talloc_free(tmp_ctx);
3407 return LDB_ERR_OPERATIONS_ERROR;
3411 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3418 ldb_search_default_callback,
3420 if (ret != LDB_SUCCESS) {
3421 talloc_free(tmp_ctx);
3425 ret = dsdb_request_add_controls(req, dsdb_flags);
3426 if (ret != LDB_SUCCESS) {
3427 talloc_free(tmp_ctx);
3431 ret = ldb_request(ldb, req);
3432 if (ret == LDB_SUCCESS) {
3433 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3436 if (ret != LDB_SUCCESS) {
3437 talloc_free(tmp_ctx);
3441 *_res = talloc_steal(mem_ctx, res);
3442 talloc_free(tmp_ctx);