2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 2004
6 Copyright (C) Volker Lendecke 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "events/events.h"
27 #include "ldb_module.h"
28 #include "ldb_errors.h"
29 #include "../lib/util/util_ldb.h"
30 #include "../lib/crypto/crypto.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "libcli/security/security.h"
33 #include "librpc/gen_ndr/ndr_security.h"
34 #include "librpc/gen_ndr/ndr_misc.h"
35 #include "../libds/common/flags.h"
36 #include "dsdb/common/proto.h"
37 #include "libcli/ldap/ldap_ndr.h"
38 #include "param/param.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "librpc/gen_ndr/ndr_drsblobs.h"
41 #include "system/locale.h"
42 #include "system/filesys.h"
43 #include "lib/util/tsort.h"
44 #include "dsdb/common/util.h"
45 #include "lib/socket/socket.h"
46 #include "librpc/gen_ndr/irpc.h"
47 #include "libds/common/flag_mapping.h"
48 #include "../lib/util/util_runcmd.h"
49 #include "lib/util/access.h"
50 #include "lib/util/util_str_hex.h"
51 #include "libcli/util/ntstatus.h"
54 * This included to allow us to handle DSDB_FLAG_REPLICATED_UPDATE in
55 * dsdb_request_add_controls()
57 #include "dsdb/samdb/ldb_modules/util.h"
60 search the sam for the specified attributes in a specific domain, filter on
61 objectSid being in domain_sid.
63 int samdb_search_domain(struct ldb_context *sam_ldb,
65 struct ldb_dn *basedn,
66 struct ldb_message ***res,
67 const char * const *attrs,
68 const struct dom_sid *domain_sid,
69 const char *format, ...) _PRINTF_ATTRIBUTE(7,8)
75 count = gendb_search_v(sam_ldb, mem_ctx, basedn,
76 res, attrs, format, ap);
82 struct dom_sid *entry_sid;
84 entry_sid = samdb_result_dom_sid(mem_ctx, (*res)[i], "objectSid");
86 if ((entry_sid == NULL) ||
87 (!dom_sid_in_domain(domain_sid, entry_sid))) {
88 /* Delete that entry from the result set */
89 (*res)[i] = (*res)[count-1];
91 talloc_free(entry_sid);
94 talloc_free(entry_sid);
102 search the sam for a single string attribute in exactly 1 record
104 const char *samdb_search_string_v(struct ldb_context *sam_ldb,
106 struct ldb_dn *basedn,
107 const char *attr_name,
108 const char *format, va_list ap) _PRINTF_ATTRIBUTE(5,0)
111 const char *attrs[2] = { NULL, NULL };
112 struct ldb_message **res = NULL;
114 attrs[0] = attr_name;
116 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
118 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
119 attr_name, format, count));
126 return ldb_msg_find_attr_as_string(res[0], attr_name, NULL);
130 search the sam for a single string attribute in exactly 1 record
132 const char *samdb_search_string(struct ldb_context *sam_ldb,
134 struct ldb_dn *basedn,
135 const char *attr_name,
136 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
141 va_start(ap, format);
142 str = samdb_search_string_v(sam_ldb, mem_ctx, basedn, attr_name, format, ap);
148 struct ldb_dn *samdb_search_dn(struct ldb_context *sam_ldb,
150 struct ldb_dn *basedn,
151 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
155 struct ldb_message **res = NULL;
158 va_start(ap, format);
159 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, NULL, format, ap);
162 if (count != 1) return NULL;
164 ret = talloc_steal(mem_ctx, res[0]->dn);
171 search the sam for a dom_sid attribute in exactly 1 record
173 struct dom_sid *samdb_search_dom_sid(struct ldb_context *sam_ldb,
175 struct ldb_dn *basedn,
176 const char *attr_name,
177 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
181 struct ldb_message **res;
182 const char *attrs[2] = { NULL, NULL };
185 attrs[0] = attr_name;
187 va_start(ap, format);
188 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
191 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
192 attr_name, format, count));
198 sid = samdb_result_dom_sid(mem_ctx, res[0], attr_name);
204 return the count of the number of records in the sam matching the query
206 int samdb_search_count(struct ldb_context *sam_ldb,
208 struct ldb_dn *basedn,
209 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
212 const char *attrs[] = { NULL };
215 va_start(ap, format);
216 ret = gendb_search_v(sam_ldb, mem_ctx, basedn, NULL, attrs, format, ap);
224 search the sam for a single integer attribute in exactly 1 record
226 unsigned int samdb_search_uint(struct ldb_context *sam_ldb,
228 unsigned int default_value,
229 struct ldb_dn *basedn,
230 const char *attr_name,
231 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
235 struct ldb_message **res;
236 const char *attrs[2] = { NULL, NULL };
238 attrs[0] = attr_name;
240 va_start(ap, format);
241 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
245 return default_value;
248 return ldb_msg_find_attr_as_uint(res[0], attr_name, default_value);
252 search the sam for a single signed 64 bit integer attribute in exactly 1 record
254 int64_t samdb_search_int64(struct ldb_context *sam_ldb,
256 int64_t default_value,
257 struct ldb_dn *basedn,
258 const char *attr_name,
259 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
263 struct ldb_message **res;
264 const char *attrs[2] = { NULL, NULL };
266 attrs[0] = attr_name;
268 va_start(ap, format);
269 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
273 return default_value;
276 return ldb_msg_find_attr_as_int64(res[0], attr_name, default_value);
280 search the sam for multipe records each giving a single string attribute
281 return the number of matches, or -1 on error
283 int samdb_search_string_multiple(struct ldb_context *sam_ldb,
285 struct ldb_dn *basedn,
287 const char *attr_name,
288 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
292 const char *attrs[2] = { NULL, NULL };
293 struct ldb_message **res = NULL;
295 attrs[0] = attr_name;
297 va_start(ap, format);
298 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
305 /* make sure its single valued */
306 for (i=0;i<count;i++) {
307 if (res[i]->num_elements != 1) {
308 DEBUG(1,("samdb: search for %s %s not single valued\n",
315 *strs = talloc_array(mem_ctx, const char *, count+1);
321 for (i=0;i<count;i++) {
322 (*strs)[i] = ldb_msg_find_attr_as_string(res[i], attr_name, NULL);
324 (*strs)[count] = NULL;
329 struct ldb_dn *samdb_result_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
330 const char *attr, struct ldb_dn *default_value)
332 struct ldb_dn *ret_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, msg, attr);
334 return default_value;
340 pull a rid from a objectSid in a result set.
342 uint32_t samdb_result_rid_from_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
343 const char *attr, uint32_t default_value)
348 sid = samdb_result_dom_sid(mem_ctx, msg, attr);
350 return default_value;
352 rid = sid->sub_auths[sid->num_auths-1];
358 pull a dom_sid structure from a objectSid in a result set.
360 struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
364 const struct ldb_val *v;
366 v = ldb_msg_find_ldb_val(msg, attr);
370 sid = talloc(mem_ctx, struct dom_sid);
374 ok = sid_parse(v->data, v->length, sid);
383 pull a guid structure from a objectGUID in a result set.
385 struct GUID samdb_result_guid(const struct ldb_message *msg, const char *attr)
387 const struct ldb_val *v;
391 v = ldb_msg_find_ldb_val(msg, attr);
392 if (!v) return GUID_zero();
394 status = GUID_from_ndr_blob(v, &guid);
395 if (!NT_STATUS_IS_OK(status)) {
403 pull a sid prefix from a objectSid in a result set.
404 this is used to find the domain sid for a user
406 struct dom_sid *samdb_result_sid_prefix(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
409 struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, msg, attr);
410 if (!sid || sid->num_auths < 1) return NULL;
416 pull a NTTIME in a result set.
418 NTTIME samdb_result_nttime(const struct ldb_message *msg, const char *attr,
419 NTTIME default_value)
421 return ldb_msg_find_attr_as_uint64(msg, attr, default_value);
425 * Windows stores 0 for lastLogoff.
426 * But when a MS DC return the lastLogoff (as Logoff Time)
427 * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
428 * cause windows 2008 and newer version to fail for SMB requests
430 NTTIME samdb_result_last_logoff(const struct ldb_message *msg)
432 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "lastLogoff",0);
435 ret = 0x7FFFFFFFFFFFFFFFULL;
441 * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
442 * indicate an account doesn't expire.
444 * When Windows initially creates an account, it sets
445 * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF). However,
446 * when changing from an account having a specific expiration date to
447 * that account never expiring, it sets accountExpires = 0.
449 * Consolidate that logic here to allow clearer logic for account expiry in
450 * the rest of the code.
452 NTTIME samdb_result_account_expires(const struct ldb_message *msg)
454 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "accountExpires",
458 ret = 0x7FFFFFFFFFFFFFFFULL;
464 construct the allow_password_change field from the PwdLastSet attribute and the
465 domain password settings
467 NTTIME samdb_result_allow_password_change(struct ldb_context *sam_ldb,
469 struct ldb_dn *domain_dn,
470 struct ldb_message *msg,
473 uint64_t attr_time = ldb_msg_find_attr_as_uint64(msg, attr, 0);
476 if (attr_time == 0) {
480 minPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "minPwdAge", NULL);
482 /* yes, this is a -= not a += as minPwdAge is stored as the negative
483 of the number of 100-nano-seconds */
484 attr_time -= minPwdAge;
490 pull a samr_Password structutre from a result set.
492 struct samr_Password *samdb_result_hash(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, const char *attr)
494 struct samr_Password *hash = NULL;
495 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
496 if (val && (val->length >= sizeof(hash->hash))) {
497 hash = talloc(mem_ctx, struct samr_Password);
498 memcpy(hash->hash, val->data, MIN(val->length, sizeof(hash->hash)));
504 pull an array of samr_Password structures from a result set.
506 unsigned int samdb_result_hashes(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
507 const char *attr, struct samr_Password **hashes)
509 unsigned int count, i;
510 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
516 count = val->length / 16;
521 *hashes = talloc_array(mem_ctx, struct samr_Password, count);
526 for (i=0;i<count;i++) {
527 memcpy((*hashes)[i].hash, (i*16)+(char *)val->data, 16);
533 NTSTATUS samdb_result_passwords_from_history(TALLOC_CTX *mem_ctx,
534 struct loadparm_context *lp_ctx,
535 struct ldb_message *msg,
537 struct samr_Password **lm_pwd,
538 struct samr_Password **nt_pwd)
540 struct samr_Password *lmPwdHash, *ntPwdHash;
544 num_nt = samdb_result_hashes(mem_ctx, msg, "ntPwdHistory", &ntPwdHash);
548 *nt_pwd = &ntPwdHash[idx];
552 /* Ensure that if we have turned off LM
553 * authentication, that we never use the LM hash, even
555 if (lpcfg_lanman_auth(lp_ctx)) {
557 num_lm = samdb_result_hashes(mem_ctx, msg, "lmPwdHistory", &lmPwdHash);
561 *lm_pwd = &lmPwdHash[idx];
570 NTSTATUS samdb_result_passwords_no_lockout(TALLOC_CTX *mem_ctx,
571 struct loadparm_context *lp_ctx,
572 const struct ldb_message *msg,
573 struct samr_Password **lm_pwd,
574 struct samr_Password **nt_pwd)
576 struct samr_Password *lmPwdHash, *ntPwdHash;
580 num_nt = samdb_result_hashes(mem_ctx, msg, "unicodePwd", &ntPwdHash);
583 } else if (num_nt > 1) {
584 return NT_STATUS_INTERNAL_DB_CORRUPTION;
586 *nt_pwd = &ntPwdHash[0];
590 /* Ensure that if we have turned off LM
591 * authentication, that we never use the LM hash, even
593 if (lpcfg_lanman_auth(lp_ctx)) {
595 num_lm = samdb_result_hashes(mem_ctx, msg, "dBCSPwd", &lmPwdHash);
598 } else if (num_lm > 1) {
599 return NT_STATUS_INTERNAL_DB_CORRUPTION;
601 *lm_pwd = &lmPwdHash[0];
610 NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx,
611 struct loadparm_context *lp_ctx,
612 const struct ldb_message *msg,
613 struct samr_Password **lm_pwd,
614 struct samr_Password **nt_pwd)
618 acct_flags = samdb_result_acct_flags(msg,
619 "msDS-User-Account-Control-Computed");
620 /* Quit if the account was locked out. */
621 if (acct_flags & ACB_AUTOLOCK) {
622 DEBUG(3,("samdb_result_passwords: Account for user %s was locked out.\n",
623 ldb_dn_get_linearized(msg->dn)));
624 return NT_STATUS_ACCOUNT_LOCKED_OUT;
627 return samdb_result_passwords_no_lockout(mem_ctx, lp_ctx, msg,
632 pull a samr_LogonHours structutre from a result set.
634 struct samr_LogonHours samdb_result_logon_hours(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char *attr)
636 struct samr_LogonHours hours;
637 size_t units_per_week = 168;
638 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
643 units_per_week = val->length * 8;
646 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week/8);
650 hours.units_per_week = units_per_week;
651 memset(hours.bits, 0xFF, units_per_week/8);
653 memcpy(hours.bits, val->data, val->length);
660 pull a set of account_flags from a result set.
662 Naturally, this requires that userAccountControl and
663 (if not null) the attributes 'attr' be already
666 uint32_t samdb_result_acct_flags(const struct ldb_message *msg, const char *attr)
668 uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
669 uint32_t attr_flags = 0;
670 uint32_t acct_flags = ds_uf2acb(userAccountControl);
672 attr_flags = ldb_msg_find_attr_as_uint(msg, attr, UF_ACCOUNTDISABLE);
673 if (attr_flags == UF_ACCOUNTDISABLE) {
674 DEBUG(0, ("Attribute %s not found, disabling account %s!\n", attr,
675 ldb_dn_get_linearized(msg->dn)));
677 acct_flags |= ds_uf2acb(attr_flags);
683 NTSTATUS samdb_result_parameters(TALLOC_CTX *mem_ctx,
684 struct ldb_message *msg,
686 struct lsa_BinaryString *s)
689 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
697 if ((val->length % 2) != 0) {
699 * If the on-disk data is not even in length, we know
700 * it is corrupt, and can not be safely pushed. We
701 * would either truncate, send either a un-initilaised
702 * byte or send a forced zero byte
704 return NT_STATUS_INTERNAL_DB_CORRUPTION;
707 s->array = talloc_array(mem_ctx, uint16_t, val->length/2);
709 return NT_STATUS_NO_MEMORY;
711 s->length = s->size = val->length;
713 /* The on-disk format is the 'network' format, being UTF16LE (sort of) */
714 for (i = 0; i < s->length / 2; i++) {
715 s->array[i] = SVAL(val->data, i * 2);
721 /* Find an attribute, with a particular value */
723 /* The current callers of this function expect a very specific
724 * behaviour: In particular, objectClass subclass equivilance is not
725 * wanted. This means that we should not lookup the schema for the
726 * comparison function */
727 struct ldb_message_element *samdb_find_attribute(struct ldb_context *ldb,
728 const struct ldb_message *msg,
729 const char *name, const char *value)
732 struct ldb_message_element *el = ldb_msg_find_element(msg, name);
738 for (i=0;i<el->num_values;i++) {
739 if (ldb_attr_cmp(value, (char *)el->values[i].data) == 0) {
747 static int samdb_find_or_add_attribute_ex(struct ldb_context *ldb,
748 struct ldb_message *msg,
750 const char *set_value,
755 struct ldb_message_element *el;
757 SMB_ASSERT(attr_flags != 0);
759 el = ldb_msg_find_element(msg, name);
768 ret = ldb_msg_add_empty(msg, name,
771 if (ret != LDB_SUCCESS) {
775 if (set_value != NULL) {
776 ret = ldb_msg_add_string(msg, name, set_value);
777 if (ret != LDB_SUCCESS) {
788 int samdb_find_or_add_attribute(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
790 return samdb_find_or_add_attribute_ex(ldb, msg, name, set_value, LDB_FLAG_MOD_ADD, NULL);
794 add a dom_sid element to a message
796 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
797 const char *attr_name, const struct dom_sid *sid)
800 enum ndr_err_code ndr_err;
802 ndr_err = ndr_push_struct_blob(&v, mem_ctx,
804 (ndr_push_flags_fn_t)ndr_push_dom_sid);
805 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
806 return ldb_operr(sam_ldb);
808 return ldb_msg_add_value(msg, attr_name, &v, NULL);
813 add a delete element operation to a message
815 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
816 const char *attr_name)
818 /* we use an empty replace rather than a delete, as it allows for
819 dsdb_replace() to be used everywhere */
820 return ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_REPLACE, NULL);
824 add an add attribute value to a message or enhance an existing attribute
825 which has the same name and the add flag set.
827 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
828 struct ldb_message *msg, const char *attr_name,
831 struct ldb_message_element *el;
832 struct ldb_val val, *vals;
838 v = talloc_strdup(mem_ctx, value);
840 return ldb_oom(sam_ldb);
843 val.data = (uint8_t *) v;
844 val.length = strlen(v);
846 if (val.length == 0) {
847 /* allow empty strings as non-existent attributes */
851 for (i = 0; i < msg->num_elements; i++) {
852 el = &msg->elements[i];
853 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
854 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD)) {
860 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_ADD,
862 if (ret != LDB_SUCCESS) {
867 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
870 return ldb_oom(sam_ldb);
873 el->values[el->num_values] = val;
880 add a delete attribute value to a message or enhance an existing attribute
881 which has the same name and the delete flag set.
883 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
884 struct ldb_message *msg, const char *attr_name,
887 struct ldb_message_element *el;
888 struct ldb_val val, *vals;
894 v = talloc_strdup(mem_ctx, value);
896 return ldb_oom(sam_ldb);
899 val.data = (uint8_t *) v;
900 val.length = strlen(v);
902 if (val.length == 0) {
903 /* allow empty strings as non-existent attributes */
907 for (i = 0; i < msg->num_elements; i++) {
908 el = &msg->elements[i];
909 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
910 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
916 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_DELETE,
918 if (ret != LDB_SUCCESS) {
923 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
926 return ldb_oom(sam_ldb);
929 el->values[el->num_values] = val;
936 add a int element to a message
938 int samdb_msg_add_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
939 const char *attr_name, int v)
941 const char *s = talloc_asprintf(mem_ctx, "%d", v);
943 return ldb_oom(sam_ldb);
945 return ldb_msg_add_string(msg, attr_name, s);
949 * Add an unsigned int element to a message
951 * The issue here is that we have not yet first cast to int32_t explicitly,
952 * before we cast to an signed int to printf() into the %d or cast to a
953 * int64_t before we then cast to a long long to printf into a %lld.
955 * There are *no* unsigned integers in Active Directory LDAP, even the RID
956 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
957 * (See the schema, and the syntax definitions in schema_syntax.c).
960 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
961 const char *attr_name, unsigned int v)
963 return samdb_msg_add_int(sam_ldb, mem_ctx, msg, attr_name, (int)v);
967 add a (signed) int64_t element to a message
969 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
970 const char *attr_name, int64_t v)
972 const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
974 return ldb_oom(sam_ldb);
976 return ldb_msg_add_string(msg, attr_name, s);
980 * Add an unsigned int64_t (uint64_t) element to a message
982 * The issue here is that we have not yet first cast to int32_t explicitly,
983 * before we cast to an signed int to printf() into the %d or cast to a
984 * int64_t before we then cast to a long long to printf into a %lld.
986 * There are *no* unsigned integers in Active Directory LDAP, even the RID
987 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
988 * (See the schema, and the syntax definitions in schema_syntax.c).
991 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
992 const char *attr_name, uint64_t v)
994 return samdb_msg_add_int64(sam_ldb, mem_ctx, msg, attr_name, (int64_t)v);
998 add a samr_Password element to a message
1000 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1001 const char *attr_name, const struct samr_Password *hash)
1004 val.data = talloc_memdup(mem_ctx, hash->hash, 16);
1006 return ldb_oom(sam_ldb);
1009 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1013 add a samr_Password array to a message
1015 int samdb_msg_add_hashes(struct ldb_context *ldb,
1016 TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1017 const char *attr_name, struct samr_Password *hashes,
1022 val.data = talloc_array_size(mem_ctx, 16, count);
1023 val.length = count*16;
1025 return ldb_oom(ldb);
1027 for (i=0;i<count;i++) {
1028 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
1030 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1034 add a acct_flags element to a message
1036 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1037 const char *attr_name, uint32_t v)
1039 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
1043 add a logon_hours element to a message
1045 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1046 const char *attr_name, struct samr_LogonHours *hours)
1049 val.length = hours->units_per_week / 8;
1050 val.data = hours->bits;
1051 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1055 add a parameters element to a message
1057 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1058 const char *attr_name, struct lsa_BinaryString *parameters)
1062 if ((parameters->length % 2) != 0) {
1063 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
1066 val.data = talloc_array(mem_ctx, uint8_t, parameters->length);
1067 if (val.data == NULL) {
1068 return LDB_ERR_OPERATIONS_ERROR;
1070 val.length = parameters->length;
1071 for (i = 0; i < parameters->length / 2; i++) {
1073 * The on-disk format needs to be in the 'network'
1074 * format, parmeters->array is a uint16_t array of
1075 * length parameters->length / 2
1077 SSVAL(val.data, i * 2, parameters->array[i]);
1079 return ldb_msg_add_steal_value(msg, attr_name, &val);
1083 * Sets an unsigned int element in a message
1085 * The issue here is that we have not yet first cast to int32_t explicitly,
1086 * before we cast to an signed int to printf() into the %d or cast to a
1087 * int64_t before we then cast to a long long to printf into a %lld.
1089 * There are *no* unsigned integers in Active Directory LDAP, even the RID
1090 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
1091 * (See the schema, and the syntax definitions in schema_syntax.c).
1094 int samdb_msg_set_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
1095 struct ldb_message *msg, const char *attr_name,
1098 struct ldb_message_element *el;
1100 el = ldb_msg_find_element(msg, attr_name);
1104 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, v);
1108 * Handle ldb_request in transaction
1110 int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
1111 struct ldb_request *req)
1115 ret = ldb_transaction_start(sam_ldb);
1116 if (ret != LDB_SUCCESS) {
1120 ret = ldb_request(sam_ldb, req);
1121 if (ret == LDB_SUCCESS) {
1122 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1125 if (ret == LDB_SUCCESS) {
1126 return ldb_transaction_commit(sam_ldb);
1128 ldb_transaction_cancel(sam_ldb);
1134 return a default security descriptor
1136 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1138 struct security_descriptor *sd;
1140 sd = security_descriptor_initialise(mem_ctx);
1145 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1147 struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1148 struct ldb_dn *aggregate_dn;
1153 aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1154 if (!aggregate_dn) {
1157 if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1160 return aggregate_dn;
1163 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1165 struct ldb_dn *new_dn;
1167 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1168 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1169 talloc_free(new_dn);
1175 struct ldb_dn *samdb_infrastructure_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1177 struct ldb_dn *new_dn;
1179 new_dn = ldb_dn_copy(mem_ctx, ldb_get_default_basedn(sam_ctx));
1180 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Infrastructure")) {
1181 talloc_free(new_dn);
1187 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1189 struct ldb_dn *new_dn;
1191 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1192 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1193 talloc_free(new_dn);
1200 work out the domain sid for the current open ldb
1202 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1204 TALLOC_CTX *tmp_ctx;
1205 const struct dom_sid *domain_sid;
1206 const char *attrs[] = {
1210 struct ldb_result *res;
1213 /* see if we have a cached copy */
1214 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1219 tmp_ctx = talloc_new(ldb);
1220 if (tmp_ctx == NULL) {
1224 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1226 if (ret != LDB_SUCCESS) {
1230 if (res->count != 1) {
1234 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1235 if (domain_sid == NULL) {
1239 /* cache the domain_sid in the ldb */
1240 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1244 talloc_steal(ldb, domain_sid);
1245 talloc_free(tmp_ctx);
1250 talloc_free(tmp_ctx);
1255 get domain sid from cache
1257 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1259 return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1262 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1264 TALLOC_CTX *tmp_ctx;
1265 struct dom_sid *dom_sid_new;
1266 struct dom_sid *dom_sid_old;
1268 /* see if we have a cached copy */
1269 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1270 "cache.domain_sid"), struct dom_sid);
1272 tmp_ctx = talloc_new(ldb);
1273 if (tmp_ctx == NULL) {
1277 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1282 /* cache the domain_sid in the ldb */
1283 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1287 talloc_steal(ldb, dom_sid_new);
1288 talloc_free(tmp_ctx);
1289 talloc_free(dom_sid_old);
1294 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1295 talloc_free(tmp_ctx);
1300 work out the domain guid for the current open ldb
1302 const struct GUID *samdb_domain_guid(struct ldb_context *ldb)
1304 TALLOC_CTX *tmp_ctx = NULL;
1305 struct GUID *domain_guid = NULL;
1306 const char *attrs[] = {
1310 struct ldb_result *res = NULL;
1313 /* see if we have a cached copy */
1314 domain_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.domain_guid");
1319 tmp_ctx = talloc_new(ldb);
1320 if (tmp_ctx == NULL) {
1324 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectGUID=*");
1325 if (ret != LDB_SUCCESS) {
1329 if (res->count != 1) {
1333 domain_guid = talloc(tmp_ctx, struct GUID);
1334 if (domain_guid == NULL) {
1337 *domain_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1339 /* cache the domain_sid in the ldb */
1340 if (ldb_set_opaque(ldb, "cache.domain_guid", domain_guid) != LDB_SUCCESS) {
1344 talloc_steal(ldb, domain_guid);
1345 talloc_free(tmp_ctx);
1350 talloc_free(tmp_ctx);
1354 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
1356 TALLOC_CTX *tmp_ctx;
1357 struct ldb_dn *ntds_settings_dn_new;
1358 struct ldb_dn *ntds_settings_dn_old;
1360 /* see if we have a forced copy from provision */
1361 ntds_settings_dn_old = talloc_get_type(ldb_get_opaque(ldb,
1362 "forced.ntds_settings_dn"), struct ldb_dn);
1364 tmp_ctx = talloc_new(ldb);
1365 if (tmp_ctx == NULL) {
1369 ntds_settings_dn_new = ldb_dn_copy(tmp_ctx, ntds_settings_dn_in);
1370 if (!ntds_settings_dn_new) {
1374 /* set the DN in the ldb to avoid lookups during provision */
1375 if (ldb_set_opaque(ldb, "forced.ntds_settings_dn", ntds_settings_dn_new) != LDB_SUCCESS) {
1379 talloc_steal(ldb, ntds_settings_dn_new);
1380 talloc_free(tmp_ctx);
1381 talloc_free(ntds_settings_dn_old);
1386 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1387 talloc_free(tmp_ctx);
1392 work out the ntds settings dn for the current open ldb
1394 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1396 TALLOC_CTX *tmp_ctx;
1397 const char *root_attrs[] = { "dsServiceName", NULL };
1399 struct ldb_result *root_res;
1400 struct ldb_dn *settings_dn;
1402 /* see if we have a cached copy */
1403 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "forced.ntds_settings_dn");
1405 return ldb_dn_copy(mem_ctx, settings_dn);
1408 tmp_ctx = talloc_new(mem_ctx);
1409 if (tmp_ctx == NULL) {
1413 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1414 if (ret != LDB_SUCCESS) {
1415 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1416 ldb_errstring(ldb)));
1420 if (root_res->count != 1) {
1424 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1426 /* note that we do not cache the DN here, as that would mean
1427 * we could not handle server renames at runtime. Only
1428 * provision sets up forced.ntds_settings_dn */
1430 talloc_steal(mem_ctx, settings_dn);
1431 talloc_free(tmp_ctx);
1436 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1437 talloc_free(tmp_ctx);
1442 work out the ntds settings invocationId for the current open ldb
1444 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1446 TALLOC_CTX *tmp_ctx;
1447 const char *attrs[] = { "invocationId", NULL };
1449 struct ldb_result *res;
1450 struct GUID *invocation_id;
1452 /* see if we have a cached copy */
1453 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1454 if (invocation_id) {
1455 SMB_ASSERT(!GUID_all_zero(invocation_id));
1456 return invocation_id;
1459 tmp_ctx = talloc_new(ldb);
1460 if (tmp_ctx == NULL) {
1464 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1469 if (res->count != 1) {
1473 invocation_id = talloc(tmp_ctx, struct GUID);
1474 if (!invocation_id) {
1478 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1479 if (GUID_all_zero(invocation_id)) {
1480 if (ldb_msg_find_ldb_val(res->msgs[0], "invocationId")) {
1481 DEBUG(0, ("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1483 DEBUG(0, ("Failed to find parse own NTDS Settings invocationId from the ldb!\n"));
1488 /* cache the domain_sid in the ldb */
1489 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1493 talloc_steal(ldb, invocation_id);
1494 talloc_free(tmp_ctx);
1496 return invocation_id;
1499 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1500 talloc_free(tmp_ctx);
1504 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1506 TALLOC_CTX *tmp_ctx;
1507 struct GUID *invocation_id_new;
1508 struct GUID *invocation_id_old;
1510 /* see if we have a cached copy */
1511 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1512 "cache.invocation_id");
1514 tmp_ctx = talloc_new(ldb);
1515 if (tmp_ctx == NULL) {
1519 invocation_id_new = talloc(tmp_ctx, struct GUID);
1520 if (!invocation_id_new) {
1524 SMB_ASSERT(!GUID_all_zero(invocation_id_in));
1525 *invocation_id_new = *invocation_id_in;
1527 /* cache the domain_sid in the ldb */
1528 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1532 talloc_steal(ldb, invocation_id_new);
1533 talloc_free(tmp_ctx);
1534 talloc_free(invocation_id_old);
1539 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1540 talloc_free(tmp_ctx);
1545 work out the ntds settings objectGUID for the current open ldb
1547 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1549 TALLOC_CTX *tmp_ctx;
1550 const char *attrs[] = { "objectGUID", NULL };
1552 struct ldb_result *res;
1553 struct GUID *ntds_guid;
1555 /* see if we have a cached copy */
1556 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1561 tmp_ctx = talloc_new(ldb);
1562 if (tmp_ctx == NULL) {
1566 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1571 if (res->count != 1) {
1575 ntds_guid = talloc(tmp_ctx, struct GUID);
1580 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1582 /* cache the domain_sid in the ldb */
1583 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1587 talloc_steal(ldb, ntds_guid);
1588 talloc_free(tmp_ctx);
1593 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1594 talloc_free(tmp_ctx);
1598 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1600 TALLOC_CTX *tmp_ctx;
1601 struct GUID *ntds_guid_new;
1602 struct GUID *ntds_guid_old;
1604 /* see if we have a cached copy */
1605 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1607 tmp_ctx = talloc_new(ldb);
1608 if (tmp_ctx == NULL) {
1612 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1613 if (!ntds_guid_new) {
1617 *ntds_guid_new = *ntds_guid_in;
1619 /* cache the domain_sid in the ldb */
1620 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1624 talloc_steal(ldb, ntds_guid_new);
1625 talloc_free(tmp_ctx);
1626 talloc_free(ntds_guid_old);
1631 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1632 talloc_free(tmp_ctx);
1637 work out the server dn for the current open ldb
1639 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1641 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1646 dn = ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb, tmp_ctx));
1647 talloc_free(tmp_ctx);
1653 work out the server dn for the current open ldb
1655 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1657 struct ldb_dn *server_dn;
1658 struct ldb_dn *servers_dn;
1659 struct ldb_dn *server_site_dn;
1661 /* TODO: there must be a saner way to do this!! */
1662 server_dn = samdb_server_dn(ldb, mem_ctx);
1663 if (!server_dn) return NULL;
1665 servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1666 talloc_free(server_dn);
1667 if (!servers_dn) return NULL;
1669 server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1670 talloc_free(servers_dn);
1672 return server_site_dn;
1676 find the site name from a computers DN record
1678 int samdb_find_site_for_computer(struct ldb_context *ldb,
1679 TALLOC_CTX *mem_ctx, struct ldb_dn *computer_dn,
1680 const char **site_name)
1684 const struct ldb_val *rdn_val;
1688 ret = samdb_reference_dn(ldb, mem_ctx, computer_dn, "serverReferenceBL", &dn);
1689 if (ret != LDB_SUCCESS) {
1693 if (!ldb_dn_remove_child_components(dn, 2)) {
1695 return LDB_ERR_INVALID_DN_SYNTAX;
1698 rdn_val = ldb_dn_get_rdn_val(dn);
1699 if (rdn_val == NULL) {
1700 return LDB_ERR_OPERATIONS_ERROR;
1703 (*site_name) = talloc_strndup(mem_ctx, (const char *)rdn_val->data, rdn_val->length);
1706 return LDB_ERR_OPERATIONS_ERROR;
1712 find the NTDS GUID from a computers DN record
1714 int samdb_find_ntdsguid_for_computer(struct ldb_context *ldb, struct ldb_dn *computer_dn,
1715 struct GUID *ntds_guid)
1720 *ntds_guid = GUID_zero();
1722 ret = samdb_reference_dn(ldb, ldb, computer_dn, "serverReferenceBL", &dn);
1723 if (ret != LDB_SUCCESS) {
1727 if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Settings")) {
1729 return LDB_ERR_OPERATIONS_ERROR;
1732 ret = dsdb_find_guid_by_dn(ldb, dn, ntds_guid);
1738 find a 'reference' DN that points at another object
1739 (eg. serverReference, rIDManagerReference etc)
1741 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1742 const char *attribute, struct ldb_dn **dn)
1744 const char *attrs[2];
1745 struct ldb_result *res;
1748 attrs[0] = attribute;
1751 ret = dsdb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_ONE_ONLY|DSDB_SEARCH_SHOW_EXTENDED_DN, NULL);
1752 if (ret != LDB_SUCCESS) {
1753 ldb_asprintf_errstring(ldb, "Cannot find DN %s to get attribute %s for reference dn: %s",
1754 ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb));
1758 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1760 if (!ldb_msg_find_element(res->msgs[0], attribute)) {
1761 ldb_asprintf_errstring(ldb, "Cannot find attribute %s of %s to calculate reference dn", attribute,
1762 ldb_dn_get_linearized(base));
1764 ldb_asprintf_errstring(ldb, "Cannot interpret attribute %s of %s as a dn", attribute,
1765 ldb_dn_get_linearized(base));
1768 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1776 find if a DN (must have GUID component!) is our ntdsDsa
1778 int samdb_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *dn, bool *is_ntdsa)
1781 struct GUID dn_guid;
1782 const struct GUID *our_ntds_guid;
1783 status = dsdb_get_extended_dn_guid(dn, &dn_guid, "GUID");
1784 if (!NT_STATUS_IS_OK(status)) {
1785 return LDB_ERR_OPERATIONS_ERROR;
1788 our_ntds_guid = samdb_ntds_objectGUID(ldb);
1789 if (!our_ntds_guid) {
1790 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
1791 return LDB_ERR_OPERATIONS_ERROR;
1794 *is_ntdsa = GUID_equal(&dn_guid, our_ntds_guid);
1799 find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1801 int samdb_reference_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *base,
1802 const char *attribute, bool *is_ntdsa)
1805 struct ldb_dn *referenced_dn;
1806 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1807 if (tmp_ctx == NULL) {
1808 return LDB_ERR_OPERATIONS_ERROR;
1810 ret = samdb_reference_dn(ldb, tmp_ctx, base, attribute, &referenced_dn);
1811 if (ret != LDB_SUCCESS) {
1812 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb)));
1816 ret = samdb_dn_is_our_ntdsa(ldb, referenced_dn, is_ntdsa);
1818 talloc_free(tmp_ctx);
1823 find our machine account via the serverReference attribute in the
1826 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1828 struct ldb_dn *server_dn;
1831 server_dn = samdb_server_dn(ldb, mem_ctx);
1832 if (server_dn == NULL) {
1833 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
1836 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1837 talloc_free(server_dn);
1843 find the RID Manager$ DN via the rIDManagerReference attribute in the
1846 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1848 return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1849 "rIDManagerReference", dn);
1853 find the RID Set DN via the rIDSetReferences attribute in our
1856 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1858 struct ldb_dn *server_ref_dn;
1861 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1862 if (ret != LDB_SUCCESS) {
1865 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1866 talloc_free(server_ref_dn);
1870 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1872 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1879 return (const char *) val->data;
1883 * Finds the client site by using the client's IP address.
1884 * The "subnet_name" returns the name of the subnet if parameter != NULL
1886 * Has a Windows-based fallback to provide the only site available, or an empty
1887 * string if there are multiple sites.
1889 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1890 const char *ip_address, char **subnet_name,
1893 const char *attrs[] = { "cn", "siteObject", NULL };
1894 struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
1895 struct ldb_result *res;
1896 const struct ldb_val *val;
1897 const char *site_name = NULL, *l_subnet_name = NULL;
1898 const char *allow_list[2] = { NULL, NULL };
1899 unsigned int i, count;
1903 * if we don't have a client ip e.g. ncalrpc
1904 * the server site is the client site
1906 if (ip_address == NULL) {
1907 return samdb_server_site_name(ldb, mem_ctx);
1910 sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1911 if (sites_container_dn == NULL) {
1915 subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1916 if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1917 talloc_free(sites_container_dn);
1918 talloc_free(subnets_dn);
1922 ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1924 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1926 } else if (ret != LDB_SUCCESS) {
1927 talloc_free(sites_container_dn);
1928 talloc_free(subnets_dn);
1934 for (i = 0; i < count; i++) {
1935 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1938 allow_list[0] = l_subnet_name;
1940 if (allow_access_nolog(NULL, allow_list, "", ip_address)) {
1941 sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1944 if (sites_dn == NULL) {
1945 /* No reference, maybe another subnet matches */
1949 /* "val" cannot be NULL here since "sites_dn" != NULL */
1950 val = ldb_dn_get_rdn_val(sites_dn);
1951 site_name = talloc_strdup(mem_ctx,
1952 (const char *) val->data);
1954 talloc_free(sites_dn);
1960 if (site_name == NULL && fallback) {
1961 /* This is the Windows Server fallback rule: when no subnet
1962 * exists and we have only one site available then use it (it
1963 * is for sure the same as our server site). If more sites do
1964 * exist then we don't know which one to use and set the site
1966 cnt = samdb_search_count(ldb, mem_ctx, sites_container_dn,
1967 "(objectClass=site)");
1969 site_name = samdb_server_site_name(ldb, mem_ctx);
1971 site_name = talloc_strdup(mem_ctx, "");
1973 l_subnet_name = NULL;
1976 if (subnet_name != NULL) {
1977 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1980 talloc_free(sites_container_dn);
1981 talloc_free(subnets_dn);
1988 work out if we are the PDC for the domain of the current open ldb
1990 bool samdb_is_pdc(struct ldb_context *ldb)
1995 ret = samdb_reference_dn_is_our_ntdsa(ldb, ldb_get_default_basedn(ldb), "fsmoRoleOwner",
1997 if (ret != LDB_SUCCESS) {
1998 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n",
1999 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
2000 ldb_errstring(ldb)));
2008 work out if we are a Global Catalog server for the domain of the current open ldb
2010 bool samdb_is_gc(struct ldb_context *ldb)
2013 if (samdb_ntds_options(ldb, &options) != LDB_SUCCESS) {
2016 return (options & DS_NTDSDSA_OPT_IS_GC) != 0;
2019 /* Find a domain object in the parents of a particular DN. */
2020 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2021 struct ldb_dn **parent_dn, const char **errstring)
2023 TALLOC_CTX *local_ctx;
2024 struct ldb_dn *sdn = dn;
2025 struct ldb_result *res = NULL;
2026 int ret = LDB_SUCCESS;
2027 const char *attrs[] = { NULL };
2029 local_ctx = talloc_new(mem_ctx);
2030 if (local_ctx == NULL) return ldb_oom(ldb);
2032 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
2033 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
2034 "(|(objectClass=domain)(objectClass=builtinDomain))");
2035 if (ret == LDB_SUCCESS) {
2036 if (res->count == 1) {
2044 if (ret != LDB_SUCCESS) {
2045 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
2046 ldb_dn_get_linearized(dn),
2047 ldb_dn_get_linearized(sdn),
2048 ldb_errstring(ldb));
2049 talloc_free(local_ctx);
2052 if (res->count != 1) {
2053 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
2054 ldb_dn_get_linearized(dn));
2055 DEBUG(0,(__location__ ": %s\n", *errstring));
2056 talloc_free(local_ctx);
2057 return LDB_ERR_CONSTRAINT_VIOLATION;
2060 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2061 talloc_free(local_ctx);
2065 static void pwd_timeout_debug(struct tevent_context *unused1,
2066 struct tevent_timer *unused2,
2067 struct timeval unused3,
2070 DEBUG(0, ("WARNING: check_password_complexity: password script "
2071 "took more than 1 second to run\n"));
2076 * Performs checks on a user password (plaintext UNIX format - attribute
2077 * "password"). The remaining parameters have to be extracted from the domain
2080 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
2082 enum samr_ValidationStatus samdb_check_password(TALLOC_CTX *mem_ctx,
2083 struct loadparm_context *lp_ctx,
2084 const DATA_BLOB *utf8_blob,
2085 const uint32_t pwdProperties,
2086 const uint32_t minPwdLength)
2088 const char *utf8_pw = (const char *)utf8_blob->data;
2089 size_t utf8_len = strlen_m(utf8_pw);
2090 char *password_script = NULL;
2092 /* checks if the "minPwdLength" property is satisfied */
2093 if (minPwdLength > utf8_len) {
2094 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
2097 /* checks the password complexity */
2098 if (!(pwdProperties & DOMAIN_PASSWORD_COMPLEX)) {
2099 return SAMR_VALIDATION_STATUS_SUCCESS;
2102 if (utf8_len == 0) {
2103 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2106 password_script = lpcfg_check_password_script(lp_ctx, mem_ctx);
2107 if (password_script != NULL && *password_script != '\0') {
2110 struct tevent_context *event_ctx = NULL;
2111 struct tevent_req *req = NULL;
2112 struct samba_runcmd_state *run_cmd = NULL;
2113 const char * const cmd[4] = {
2119 event_ctx = tevent_context_init(mem_ctx);
2120 if (event_ctx == NULL) {
2121 TALLOC_FREE(password_script);
2122 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2125 /* Gives a warning after 1 second, terminates after 10 */
2126 tevent_add_timer(event_ctx, event_ctx,
2127 tevent_timeval_current_ofs(1, 0),
2128 pwd_timeout_debug, NULL);
2130 req = samba_runcmd_send(event_ctx, event_ctx,
2131 tevent_timeval_current_ofs(10, 0),
2132 100, 100, cmd, NULL);
2133 run_cmd = tevent_req_data(req, struct samba_runcmd_state);
2134 if (write(run_cmd->fd_stdin, utf8_pw, utf8_len) != utf8_len) {
2135 TALLOC_FREE(password_script);
2136 TALLOC_FREE(event_ctx);
2137 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2140 close(run_cmd->fd_stdin);
2141 run_cmd->fd_stdin = -1;
2143 if (!tevent_req_poll(req, event_ctx)) {
2144 TALLOC_FREE(password_script);
2145 TALLOC_FREE(event_ctx);
2146 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2149 check_ret = samba_runcmd_recv(req, &error);
2150 TALLOC_FREE(event_ctx);
2152 if (error == ETIMEDOUT) {
2153 DEBUG(0, ("check_password_complexity: check password script took too long!\n"));
2154 TALLOC_FREE(password_script);
2155 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2157 DEBUG(5,("check_password_complexity: check password script (%s) "
2158 "returned [%d]\n", password_script, check_ret));
2160 if (check_ret != 0) {
2161 DEBUG(1,("check_password_complexity: "
2162 "check password script said new password is not good "
2164 TALLOC_FREE(password_script);
2165 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2168 TALLOC_FREE(password_script);
2169 return SAMR_VALIDATION_STATUS_SUCCESS;
2172 TALLOC_FREE(password_script);
2175 * Here are the standard AD password quality rules, which we
2176 * run after the script.
2179 if (!check_password_quality(utf8_pw)) {
2180 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2183 return SAMR_VALIDATION_STATUS_SUCCESS;
2187 * Callback for "samdb_set_password" password change
2189 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
2194 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2197 if (ares->error != LDB_SUCCESS) {
2199 req->context = talloc_steal(req,
2200 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2202 return ldb_request_done(req, ret);
2205 if (ares->type != LDB_REPLY_DONE) {
2207 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2210 req->context = talloc_steal(req,
2211 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2213 return ldb_request_done(req, LDB_SUCCESS);
2217 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2218 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2219 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2220 * user change or not. The "rejectReason" gives some more information if the
2223 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2224 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2226 static NTSTATUS samdb_set_password_internal(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2227 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2228 const DATA_BLOB *new_password,
2229 const struct samr_Password *lmNewHash,
2230 const struct samr_Password *ntNewHash,
2231 const struct samr_Password *lmOldHash,
2232 const struct samr_Password *ntOldHash,
2233 enum samPwdChangeReason *reject_reason,
2234 struct samr_DomInfo1 **_dominfo,
2235 bool permit_interdomain_trust)
2237 struct ldb_message *msg;
2238 struct ldb_message_element *el;
2239 struct ldb_request *req;
2240 struct dsdb_control_password_change_status *pwd_stat = NULL;
2242 bool hash_values = false;
2243 NTSTATUS status = NT_STATUS_OK;
2245 #define CHECK_RET(x) \
2246 if (x != LDB_SUCCESS) { \
2248 return NT_STATUS_NO_MEMORY; \
2251 msg = ldb_msg_new(mem_ctx);
2253 return NT_STATUS_NO_MEMORY;
2256 if ((new_password != NULL)
2257 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
2258 /* we have the password as plaintext UTF16 */
2259 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
2260 new_password, NULL));
2261 el = ldb_msg_find_element(msg, "clearTextPassword");
2262 el->flags = LDB_FLAG_MOD_REPLACE;
2263 } else if ((new_password == NULL)
2264 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2265 /* we have a password as LM and/or NT hash */
2266 if (lmNewHash != NULL) {
2267 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2268 "dBCSPwd", lmNewHash));
2269 el = ldb_msg_find_element(msg, "dBCSPwd");
2270 el->flags = LDB_FLAG_MOD_REPLACE;
2272 if (ntNewHash != NULL) {
2273 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2274 "unicodePwd", ntNewHash));
2275 el = ldb_msg_find_element(msg, "unicodePwd");
2276 el->flags = LDB_FLAG_MOD_REPLACE;
2280 /* the password wasn't specified correctly */
2282 return NT_STATUS_INVALID_PARAMETER;
2285 /* build modify request */
2286 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2287 samdb_set_password_callback, NULL);
2288 if (ret != LDB_SUCCESS) {
2290 return NT_STATUS_NO_MEMORY;
2293 /* A password change operation */
2294 if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2295 struct dsdb_control_password_change *change;
2297 change = talloc(req, struct dsdb_control_password_change);
2298 if (change == NULL) {
2301 return NT_STATUS_NO_MEMORY;
2304 change->old_nt_pwd_hash = ntOldHash;
2305 change->old_lm_pwd_hash = lmOldHash;
2307 ret = ldb_request_add_control(req,
2308 DSDB_CONTROL_PASSWORD_CHANGE_OID,
2310 if (ret != LDB_SUCCESS) {
2313 return NT_STATUS_NO_MEMORY;
2317 ret = ldb_request_add_control(req,
2318 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2320 if (ret != LDB_SUCCESS) {
2323 return NT_STATUS_NO_MEMORY;
2326 if (permit_interdomain_trust) {
2327 ret = ldb_request_add_control(req,
2328 DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID,
2330 if (ret != LDB_SUCCESS) {
2333 return NT_STATUS_NO_MEMORY;
2336 ret = ldb_request_add_control(req,
2337 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2339 if (ret != LDB_SUCCESS) {
2342 return NT_STATUS_NO_MEMORY;
2345 ret = dsdb_autotransaction_request(ldb, req);
2347 if (req->context != NULL) {
2348 struct ldb_control *control = talloc_get_type_abort(req->context,
2349 struct ldb_control);
2350 pwd_stat = talloc_get_type_abort(control->data,
2351 struct dsdb_control_password_change_status);
2352 talloc_steal(mem_ctx, pwd_stat);
2358 /* Sets the domain info (if requested) */
2359 if (_dominfo != NULL) {
2360 struct samr_DomInfo1 *dominfo;
2362 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2363 if (dominfo == NULL) {
2364 return NT_STATUS_NO_MEMORY;
2367 if (pwd_stat != NULL) {
2368 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2369 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2370 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2371 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2372 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2375 *_dominfo = dominfo;
2378 if (reject_reason != NULL) {
2379 if (pwd_stat != NULL) {
2380 *reject_reason = pwd_stat->reject_reason;
2382 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2386 if (pwd_stat != NULL) {
2387 talloc_free(pwd_stat);
2390 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2391 const char *errmsg = ldb_errstring(ldb);
2392 char *endptr = NULL;
2393 WERROR werr = WERR_GEN_FAILURE;
2394 status = NT_STATUS_UNSUCCESSFUL;
2395 if (errmsg != NULL) {
2396 werr = W_ERROR(strtol(errmsg, &endptr, 16));
2397 DBG_WARNING("%s\n", errmsg);
2399 if (endptr != errmsg) {
2400 if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2401 status = NT_STATUS_WRONG_PASSWORD;
2403 if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2404 status = NT_STATUS_PASSWORD_RESTRICTION;
2407 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2408 /* don't let the caller know if an account doesn't exist */
2409 status = NT_STATUS_WRONG_PASSWORD;
2410 } else if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
2411 status = NT_STATUS_ACCESS_DENIED;
2412 } else if (ret != LDB_SUCCESS) {
2413 DEBUG(1, ("Failed to set password on %s: %s\n",
2414 ldb_dn_get_linearized(user_dn),
2415 ldb_errstring(ldb)));
2416 status = NT_STATUS_UNSUCCESSFUL;
2422 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2423 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2424 const DATA_BLOB *new_password,
2425 const struct samr_Password *lmNewHash,
2426 const struct samr_Password *ntNewHash,
2427 const struct samr_Password *lmOldHash,
2428 const struct samr_Password *ntOldHash,
2429 enum samPwdChangeReason *reject_reason,
2430 struct samr_DomInfo1 **_dominfo)
2432 return samdb_set_password_internal(ldb, mem_ctx,
2435 lmNewHash, ntNewHash,
2436 lmOldHash, ntOldHash,
2437 reject_reason, _dominfo,
2438 false); /* reject trusts */
2442 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2443 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2444 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2445 * user change or not. The "rejectReason" gives some more information if the
2448 * This wrapper function for "samdb_set_password" takes a SID as input rather
2451 * This call encapsulates a new LDB transaction for changing the password;
2452 * therefore the user hasn't to start a new one.
2454 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2455 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2456 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2457 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2459 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2460 const struct dom_sid *user_sid,
2461 const uint32_t *new_version, /* optional for trusts */
2462 const DATA_BLOB *new_password,
2463 const struct samr_Password *lmNewHash,
2464 const struct samr_Password *ntNewHash,
2465 const struct samr_Password *lmOldHash,
2466 const struct samr_Password *ntOldHash,
2467 enum samPwdChangeReason *reject_reason,
2468 struct samr_DomInfo1 **_dominfo)
2470 TALLOC_CTX *frame = talloc_stackframe();
2472 const char * const user_attrs[] = {
2473 "userAccountControl",
2477 struct ldb_message *user_msg = NULL;
2481 ret = ldb_transaction_start(ldb);
2482 if (ret != LDB_SUCCESS) {
2483 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2485 return NT_STATUS_TRANSACTION_ABORTED;
2488 ret = dsdb_search_one(ldb, frame, &user_msg, ldb_get_default_basedn(ldb),
2489 LDB_SCOPE_SUBTREE, user_attrs, 0,
2490 "(&(objectSid=%s)(objectClass=user))",
2491 ldap_encode_ndr_dom_sid(frame, user_sid));
2492 if (ret != LDB_SUCCESS) {
2493 ldb_transaction_cancel(ldb);
2494 DEBUG(3, ("samdb_set_password_sid: SID[%s] not found in samdb %s - %s, "
2495 "returning NO_SUCH_USER\n",
2496 dom_sid_string(frame, user_sid),
2497 ldb_strerror(ret), ldb_errstring(ldb)));
2499 return NT_STATUS_NO_SUCH_USER;
2502 uac = ldb_msg_find_attr_as_uint(user_msg, "userAccountControl", 0);
2503 if (!(uac & UF_ACCOUNT_TYPE_MASK)) {
2504 ldb_transaction_cancel(ldb);
2505 DEBUG(1, ("samdb_set_password_sid: invalid "
2506 "userAccountControl[0x%08X] for SID[%s] DN[%s], "
2507 "returning NO_SUCH_USER\n",
2508 (unsigned)uac, dom_sid_string(frame, user_sid),
2509 ldb_dn_get_linearized(user_msg->dn)));
2511 return NT_STATUS_NO_SUCH_USER;
2514 if (uac & UF_INTERDOMAIN_TRUST_ACCOUNT) {
2515 const char * const tdo_attrs[] = {
2516 "trustAuthIncoming",
2520 struct ldb_message *tdo_msg = NULL;
2521 const char *account_name = NULL;
2522 uint32_t trust_direction;
2524 const struct ldb_val *old_val = NULL;
2525 struct trustAuthInOutBlob old_blob = {};
2526 uint32_t old_version = 0;
2527 struct AuthenticationInformation *old_version_a = NULL;
2528 uint32_t _new_version = 0;
2529 struct trustAuthInOutBlob new_blob = {};
2530 struct ldb_val new_val = {};
2531 struct timeval tv = timeval_current();
2532 NTTIME now = timeval_to_nttime(&tv);
2533 enum ndr_err_code ndr_err;
2535 if (new_password == NULL && ntNewHash == NULL) {
2536 ldb_transaction_cancel(ldb);
2537 DEBUG(1, ("samdb_set_password_sid: "
2538 "no new password provided "
2539 "sAMAccountName for SID[%s] DN[%s], "
2540 "returning INVALID_PARAMETER\n",
2541 dom_sid_string(frame, user_sid),
2542 ldb_dn_get_linearized(user_msg->dn)));
2544 return NT_STATUS_INVALID_PARAMETER;
2547 if (new_password != NULL && ntNewHash != NULL) {
2548 ldb_transaction_cancel(ldb);
2549 DEBUG(1, ("samdb_set_password_sid: "
2550 "two new passwords provided "
2551 "sAMAccountName for SID[%s] DN[%s], "
2552 "returning INVALID_PARAMETER\n",
2553 dom_sid_string(frame, user_sid),
2554 ldb_dn_get_linearized(user_msg->dn)));
2556 return NT_STATUS_INVALID_PARAMETER;
2559 if (new_password != NULL && (new_password->length % 2)) {
2560 ldb_transaction_cancel(ldb);
2561 DEBUG(2, ("samdb_set_password_sid: "
2562 "invalid utf16 length (%zu) "
2563 "sAMAccountName for SID[%s] DN[%s], "
2564 "returning WRONG_PASSWORD\n",
2565 new_password->length,
2566 dom_sid_string(frame, user_sid),
2567 ldb_dn_get_linearized(user_msg->dn)));
2569 return NT_STATUS_WRONG_PASSWORD;
2572 if (new_password != NULL && new_password->length >= 500) {
2573 ldb_transaction_cancel(ldb);
2574 DEBUG(2, ("samdb_set_password_sid: "
2575 "utf16 password too long (%zu) "
2576 "sAMAccountName for SID[%s] DN[%s], "
2577 "returning WRONG_PASSWORD\n",
2578 new_password->length,
2579 dom_sid_string(frame, user_sid),
2580 ldb_dn_get_linearized(user_msg->dn)));
2582 return NT_STATUS_WRONG_PASSWORD;
2585 account_name = ldb_msg_find_attr_as_string(user_msg,
2586 "sAMAccountName", NULL);
2587 if (account_name == NULL) {
2588 ldb_transaction_cancel(ldb);
2589 DEBUG(1, ("samdb_set_password_sid: missing "
2590 "sAMAccountName for SID[%s] DN[%s], "
2591 "returning NO_SUCH_USER\n",
2592 dom_sid_string(frame, user_sid),
2593 ldb_dn_get_linearized(user_msg->dn)));
2595 return NT_STATUS_NO_SUCH_USER;
2598 nt_status = dsdb_trust_search_tdo_by_type(ldb,
2603 if (!NT_STATUS_IS_OK(nt_status)) {
2604 ldb_transaction_cancel(ldb);
2605 DEBUG(1, ("samdb_set_password_sid: dsdb_trust_search_tdo "
2606 "failed(%s) for sAMAccountName[%s] SID[%s] DN[%s], "
2607 "returning INTERNAL_DB_CORRUPTION\n",
2608 nt_errstr(nt_status), account_name,
2609 dom_sid_string(frame, user_sid),
2610 ldb_dn_get_linearized(user_msg->dn)));
2612 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2615 trust_direction = ldb_msg_find_attr_as_int(tdo_msg,
2616 "trustDirection", 0);
2617 if (!(trust_direction & LSA_TRUST_DIRECTION_INBOUND)) {
2618 ldb_transaction_cancel(ldb);
2619 DEBUG(1, ("samdb_set_password_sid: direction[0x%08X] is "
2620 "not inbound for sAMAccountName[%s] "
2622 "returning INTERNAL_DB_CORRUPTION\n",
2623 (unsigned)trust_direction,
2625 ldb_dn_get_linearized(user_msg->dn),
2626 ldb_dn_get_linearized(tdo_msg->dn)));
2628 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2631 old_val = ldb_msg_find_ldb_val(tdo_msg, "trustAuthIncoming");
2632 if (old_val != NULL) {
2633 ndr_err = ndr_pull_struct_blob(old_val, frame, &old_blob,
2634 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2635 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2636 ldb_transaction_cancel(ldb);
2637 DEBUG(1, ("samdb_set_password_sid: "
2638 "failed(%s) to parse "
2639 "trustAuthOutgoing sAMAccountName[%s] "
2641 "returning INTERNAL_DB_CORRUPTION\n",
2642 ndr_map_error2string(ndr_err),
2644 ldb_dn_get_linearized(user_msg->dn),
2645 ldb_dn_get_linearized(tdo_msg->dn)));
2648 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2652 for (i = old_blob.current.count; i > 0; i--) {
2653 struct AuthenticationInformation *a =
2654 &old_blob.current.array[i - 1];
2656 switch (a->AuthType) {
2657 case TRUST_AUTH_TYPE_NONE:
2658 if (i == old_blob.current.count) {
2660 * remove TRUST_AUTH_TYPE_NONE at the
2663 old_blob.current.count--;
2667 case TRUST_AUTH_TYPE_VERSION:
2669 old_version = a->AuthInfo.version.version;
2672 case TRUST_AUTH_TYPE_CLEAR:
2675 case TRUST_AUTH_TYPE_NT4OWF:
2680 if (new_version == NULL) {
2682 new_version = &_new_version;
2685 if (old_version_a != NULL && *new_version != (old_version + 1)) {
2686 old_version_a->LastUpdateTime = now;
2687 old_version_a->AuthType = TRUST_AUTH_TYPE_NONE;
2690 new_blob.count = MAX(old_blob.current.count, 2);
2691 new_blob.current.array = talloc_zero_array(frame,
2692 struct AuthenticationInformation,
2694 if (new_blob.current.array == NULL) {
2695 ldb_transaction_cancel(ldb);
2697 return NT_STATUS_NO_MEMORY;
2699 new_blob.previous.array = talloc_zero_array(frame,
2700 struct AuthenticationInformation,
2702 if (new_blob.current.array == NULL) {
2703 ldb_transaction_cancel(ldb);
2705 return NT_STATUS_NO_MEMORY;
2708 for (i = 0; i < old_blob.current.count; i++) {
2709 struct AuthenticationInformation *o =
2710 &old_blob.current.array[i];
2711 struct AuthenticationInformation *p =
2712 &new_blob.previous.array[i];
2715 new_blob.previous.count++;
2717 for (; i < new_blob.count; i++) {
2718 struct AuthenticationInformation *pi =
2719 &new_blob.previous.array[i];
2723 * new_blob.previous is still empty so
2724 * we'll do new_blob.previous = new_blob.current
2730 pi->LastUpdateTime = now;
2731 pi->AuthType = TRUST_AUTH_TYPE_NONE;
2732 new_blob.previous.count++;
2735 for (i = 0; i < new_blob.count; i++) {
2736 struct AuthenticationInformation *ci =
2737 &new_blob.current.array[i];
2739 ci->LastUpdateTime = now;
2742 if (ntNewHash != NULL) {
2743 ci->AuthType = TRUST_AUTH_TYPE_NT4OWF;
2744 ci->AuthInfo.nt4owf.password = *ntNewHash;
2748 ci->AuthType = TRUST_AUTH_TYPE_CLEAR;
2749 ci->AuthInfo.clear.size = new_password->length;
2750 ci->AuthInfo.clear.password = new_password->data;
2753 ci->AuthType = TRUST_AUTH_TYPE_VERSION;
2754 ci->AuthInfo.version.version = *new_version;
2757 ci->AuthType = TRUST_AUTH_TYPE_NONE;
2761 new_blob.current.count++;
2764 if (new_blob.previous.count == 0) {
2765 TALLOC_FREE(new_blob.previous.array);
2766 new_blob.previous = new_blob.current;
2769 ndr_err = ndr_push_struct_blob(&new_val, frame, &new_blob,
2770 (ndr_push_flags_fn_t)ndr_push_trustAuthInOutBlob);
2771 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2772 ldb_transaction_cancel(ldb);
2773 DEBUG(1, ("samdb_set_password_sid: "
2774 "failed(%s) to generate "
2775 "trustAuthOutgoing sAMAccountName[%s] "
2777 "returning UNSUCCESSFUL\n",
2778 ndr_map_error2string(ndr_err),
2780 ldb_dn_get_linearized(user_msg->dn),
2781 ldb_dn_get_linearized(tdo_msg->dn)));
2783 return NT_STATUS_UNSUCCESSFUL;
2786 tdo_msg->num_elements = 0;
2787 TALLOC_FREE(tdo_msg->elements);
2789 ret = ldb_msg_add_empty(tdo_msg, "trustAuthIncoming",
2790 LDB_FLAG_MOD_REPLACE, NULL);
2791 if (ret != LDB_SUCCESS) {
2792 ldb_transaction_cancel(ldb);
2794 return NT_STATUS_NO_MEMORY;
2796 ret = ldb_msg_add_value(tdo_msg, "trustAuthIncoming",
2798 if (ret != LDB_SUCCESS) {
2799 ldb_transaction_cancel(ldb);
2801 return NT_STATUS_NO_MEMORY;
2804 ret = ldb_modify(ldb, tdo_msg);
2805 if (ret != LDB_SUCCESS) {
2806 nt_status = dsdb_ldb_err_to_ntstatus(ret);
2807 ldb_transaction_cancel(ldb);
2808 DEBUG(1, ("samdb_set_password_sid: "
2809 "failed to replace "
2810 "trustAuthOutgoing sAMAccountName[%s] "
2814 ldb_dn_get_linearized(user_msg->dn),
2815 ldb_dn_get_linearized(tdo_msg->dn),
2816 nt_errstr(nt_status), ldb_errstring(ldb)));
2822 nt_status = samdb_set_password_internal(ldb, mem_ctx,
2825 lmNewHash, ntNewHash,
2826 lmOldHash, ntOldHash,
2827 reject_reason, _dominfo,
2828 true); /* permit trusts */
2829 if (!NT_STATUS_IS_OK(nt_status)) {
2830 ldb_transaction_cancel(ldb);
2835 ret = ldb_transaction_commit(ldb);
2836 if (ret != LDB_SUCCESS) {
2837 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2838 ldb_dn_get_linearized(user_msg->dn),
2839 ldb_errstring(ldb)));
2841 return NT_STATUS_TRANSACTION_ABORTED;
2845 return NT_STATUS_OK;
2849 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2850 struct dom_sid *sid, struct ldb_dn **ret_dn)
2852 struct ldb_message *msg;
2853 struct ldb_dn *basedn;
2857 sidstr = dom_sid_string(mem_ctx, sid);
2858 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2860 /* We might have to create a ForeignSecurityPrincipal, even if this user
2861 * is in our own domain */
2863 msg = ldb_msg_new(sidstr);
2865 talloc_free(sidstr);
2866 return NT_STATUS_NO_MEMORY;
2869 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2870 ldb_get_default_basedn(sam_ctx),
2871 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2873 if (ret != LDB_SUCCESS) {
2874 DEBUG(0, ("Failed to find DN for "
2875 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2876 talloc_free(sidstr);
2877 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2880 /* add core elements to the ldb_message for the alias */
2882 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2883 talloc_free(sidstr);
2884 return NT_STATUS_NO_MEMORY;
2887 ret = ldb_msg_add_string(msg, "objectClass",
2888 "foreignSecurityPrincipal");
2889 if (ret != LDB_SUCCESS) {
2890 talloc_free(sidstr);
2891 return NT_STATUS_NO_MEMORY;
2894 /* create the alias */
2895 ret = ldb_add(sam_ctx, msg);
2896 if (ret != LDB_SUCCESS) {
2897 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2899 ldb_dn_get_linearized(msg->dn),
2900 ldb_errstring(sam_ctx)));
2901 talloc_free(sidstr);
2902 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2905 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2906 talloc_free(sidstr);
2908 return NT_STATUS_OK;
2913 Find the DN of a domain, assuming it to be a dotted.dns name
2916 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2919 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2920 const char *binary_encoded;
2921 const char * const *split_realm;
2928 split_realm = (const char * const *)str_list_make(tmp_ctx, dns_domain, ".");
2930 talloc_free(tmp_ctx);
2933 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2934 for (i=0; split_realm[i]; i++) {
2935 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2936 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2937 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2938 binary_encoded, ldb_dn_get_linearized(dn)));
2939 talloc_free(tmp_ctx);
2943 if (!ldb_dn_validate(dn)) {
2944 DEBUG(2, ("Failed to validated DN %s\n",
2945 ldb_dn_get_linearized(dn)));
2946 talloc_free(tmp_ctx);
2949 talloc_free(tmp_ctx);
2955 Find the DNS equivalent of a DN, in dotted DNS form
2957 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2959 int i, num_components = ldb_dn_get_comp_num(dn);
2960 char *dns_name = talloc_strdup(mem_ctx, "");
2961 if (dns_name == NULL) {
2965 for (i=0; i<num_components; i++) {
2966 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2969 talloc_free(dns_name);
2972 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2973 (int)v->length, (int)v->length, (char *)v->data);
2975 talloc_free(dns_name);
2981 /* remove the last '.' */
2982 if (dns_name[0] != 0) {
2983 dns_name[strlen(dns_name)-1] = 0;
2990 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2991 name is based on the forest DNS name
2993 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
2994 TALLOC_CTX *mem_ctx,
2995 const struct GUID *ntds_guid)
2997 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2998 const char *guid_str;
2999 struct ldb_dn *forest_dn;
3000 const char *dnsforest;
3003 guid_str = GUID_string(tmp_ctx, ntds_guid);
3004 if (guid_str == NULL) {
3005 talloc_free(tmp_ctx);
3008 forest_dn = ldb_get_root_basedn(samdb);
3009 if (forest_dn == NULL) {
3010 talloc_free(tmp_ctx);
3013 dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
3014 if (dnsforest == NULL) {
3015 talloc_free(tmp_ctx);
3018 ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
3019 talloc_free(tmp_ctx);
3025 Find the DN of a domain, be it the netbios or DNS name
3027 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
3028 const char *domain_name)
3030 const char * const domain_ref_attrs[] = {
3033 const char * const domain_ref2_attrs[] = {
3036 struct ldb_result *res_domain_ref;
3037 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
3038 /* find the domain's DN */
3039 int ret_domain = ldb_search(ldb, mem_ctx,
3041 samdb_partitions_dn(ldb, mem_ctx),
3044 "(&(nETBIOSName=%s)(objectclass=crossRef))",
3046 if (ret_domain != LDB_SUCCESS) {
3050 if (res_domain_ref->count == 0) {
3051 ret_domain = ldb_search(ldb, mem_ctx,
3053 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
3056 "(objectclass=domain)");
3057 if (ret_domain != LDB_SUCCESS) {
3061 if (res_domain_ref->count == 1) {
3062 return res_domain_ref->msgs[0]->dn;
3067 if (res_domain_ref->count > 1) {
3068 DEBUG(0,("Found %d records matching domain [%s]\n",
3069 ret_domain, domain_name));
3073 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
3079 use a GUID to find a DN
3081 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
3082 TALLOC_CTX *mem_ctx,
3083 const struct GUID *guid,
3084 uint32_t dsdb_flags,
3088 struct ldb_result *res;
3089 const char *attrs[] = { NULL };
3090 char *guid_str = GUID_string(mem_ctx, guid);
3093 return ldb_operr(ldb);
3096 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3097 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3098 DSDB_SEARCH_SHOW_EXTENDED_DN |
3099 DSDB_SEARCH_ONE_ONLY | dsdb_flags,
3100 "objectGUID=%s", guid_str);
3101 talloc_free(guid_str);
3102 if (ret != LDB_SUCCESS) {
3106 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3113 use a DN to find a GUID with a given attribute name
3115 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
3116 struct ldb_dn *dn, const char *attribute,
3120 struct ldb_result *res;
3121 const char *attrs[2];
3122 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3124 attrs[0] = attribute;
3127 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3128 DSDB_SEARCH_SHOW_DELETED |
3129 DSDB_SEARCH_SHOW_RECYCLED);
3130 if (ret != LDB_SUCCESS) {
3131 talloc_free(tmp_ctx);
3134 if (res->count < 1) {
3135 talloc_free(tmp_ctx);
3136 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3138 *guid = samdb_result_guid(res->msgs[0], attribute);
3139 talloc_free(tmp_ctx);
3144 use a DN to find a GUID
3146 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
3147 struct ldb_dn *dn, struct GUID *guid)
3149 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
3155 adds the given GUID to the given ldb_message. This value is added
3156 for the given attr_name (may be either "objectGUID" or "parentGUID").
3158 int dsdb_msg_add_guid(struct ldb_message *msg,
3160 const char *attr_name)
3165 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
3167 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
3168 if (!NT_STATUS_IS_OK(status)) {
3169 ret = LDB_ERR_OPERATIONS_ERROR;
3173 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
3174 if (ret != LDB_SUCCESS) {
3175 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
3183 talloc_free(tmp_ctx);
3190 use a DN to find a SID
3192 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
3193 struct ldb_dn *dn, struct dom_sid *sid)
3196 struct ldb_result *res;
3197 const char *attrs[] = { "objectSid", NULL };
3198 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3203 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3204 DSDB_SEARCH_SHOW_DELETED |
3205 DSDB_SEARCH_SHOW_RECYCLED);
3206 if (ret != LDB_SUCCESS) {
3207 talloc_free(tmp_ctx);
3210 if (res->count < 1) {
3211 talloc_free(tmp_ctx);
3212 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3214 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
3216 talloc_free(tmp_ctx);
3217 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3220 talloc_free(tmp_ctx);
3225 use a SID to find a DN
3227 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
3228 TALLOC_CTX *mem_ctx,
3229 struct dom_sid *sid, struct ldb_dn **dn)
3232 struct ldb_result *res;
3233 const char *attrs[] = { NULL };
3234 char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
3237 return ldb_operr(ldb);
3240 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3241 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3242 DSDB_SEARCH_SHOW_EXTENDED_DN |
3243 DSDB_SEARCH_ONE_ONLY,
3244 "objectSid=%s", sid_str);
3245 talloc_free(sid_str);
3246 if (ret != LDB_SUCCESS) {
3250 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3257 load a repsFromTo blob list for a given partition GUID
3258 attr must be "repsFrom" or "repsTo"
3260 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3261 const char *attr, struct repsFromToBlob **r, uint32_t *count)
3263 const char *attrs[] = { attr, NULL };
3264 struct ldb_result *res = NULL;
3265 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3267 struct ldb_message_element *el;
3273 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
3274 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3275 /* partition hasn't been replicated yet */
3278 if (ret != LDB_SUCCESS) {
3279 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
3280 talloc_free(tmp_ctx);
3281 return WERR_DS_DRA_INTERNAL_ERROR;
3284 el = ldb_msg_find_element(res->msgs[0], attr);
3286 /* it's OK to be empty */
3287 talloc_free(tmp_ctx);
3291 *count = el->num_values;
3292 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
3294 talloc_free(tmp_ctx);
3295 return WERR_DS_DRA_INTERNAL_ERROR;
3298 for (i=0; i<(*count); i++) {
3299 enum ndr_err_code ndr_err;
3300 ndr_err = ndr_pull_struct_blob(&el->values[i],
3303 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
3304 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3305 talloc_free(tmp_ctx);
3306 return WERR_DS_DRA_INTERNAL_ERROR;
3310 talloc_free(tmp_ctx);
3316 save the repsFromTo blob list for a given partition GUID
3317 attr must be "repsFrom" or "repsTo"
3319 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3320 const char *attr, struct repsFromToBlob *r, uint32_t count)
3322 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3323 struct ldb_message *msg;
3324 struct ldb_message_element *el;
3327 msg = ldb_msg_new(tmp_ctx);
3329 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
3333 el->values = talloc_array(msg, struct ldb_val, count);
3338 for (i=0; i<count; i++) {
3340 enum ndr_err_code ndr_err;
3342 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
3344 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
3345 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3353 if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
3354 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
3358 talloc_free(tmp_ctx);
3363 talloc_free(tmp_ctx);
3364 return WERR_DS_DRA_INTERNAL_ERROR;
3369 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
3370 object for a partition
3372 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
3373 uint64_t *uSN, uint64_t *urgent_uSN)
3375 struct ldb_request *req;
3377 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3378 struct dsdb_control_current_partition *p_ctrl;
3379 struct ldb_result *res;
3381 res = talloc_zero(tmp_ctx, struct ldb_result);
3383 talloc_free(tmp_ctx);
3384 return ldb_oom(ldb);
3387 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3388 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
3392 res, ldb_search_default_callback,
3394 if (ret != LDB_SUCCESS) {
3395 talloc_free(tmp_ctx);
3399 p_ctrl = talloc(req, struct dsdb_control_current_partition);
3400 if (p_ctrl == NULL) {
3401 talloc_free(tmp_ctx);
3402 return ldb_oom(ldb);
3404 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
3407 ret = ldb_request_add_control(req,
3408 DSDB_CONTROL_CURRENT_PARTITION_OID,
3410 if (ret != LDB_SUCCESS) {
3411 talloc_free(tmp_ctx);
3415 /* Run the new request */
3416 ret = ldb_request(ldb, req);
3418 if (ret == LDB_SUCCESS) {
3419 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3422 if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
3423 /* it hasn't been created yet, which means
3424 an implicit value of zero */
3426 talloc_free(tmp_ctx);
3430 if (ret != LDB_SUCCESS) {
3431 talloc_free(tmp_ctx);
3435 if (res->count < 1) {
3441 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
3443 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
3447 talloc_free(tmp_ctx);
3452 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
3453 const struct drsuapi_DsReplicaCursor2 *c2)
3455 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3458 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
3459 const struct drsuapi_DsReplicaCursor *c2)
3461 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3466 see if a computer identified by its invocationId is a RODC
3468 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
3470 /* 1) find the DN for this servers NTDSDSA object
3471 2) search for the msDS-isRODC attribute
3472 3) if not present then not a RODC
3473 4) if present and TRUE then is a RODC
3475 struct ldb_dn *config_dn;
3476 const char *attrs[] = { "msDS-isRODC", NULL };
3478 struct ldb_result *res;
3479 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
3481 config_dn = ldb_get_config_basedn(sam_ctx);
3483 talloc_free(tmp_ctx);
3484 return ldb_operr(sam_ctx);
3487 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
3488 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
3490 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3492 talloc_free(tmp_ctx);
3496 if (ret != LDB_SUCCESS) {
3497 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
3498 GUID_string(tmp_ctx, objectGUID)));
3500 talloc_free(tmp_ctx);
3504 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
3505 *is_rodc = (ret == 1);
3507 talloc_free(tmp_ctx);
3513 see if we are a RODC
3515 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
3517 const struct GUID *objectGUID;
3521 /* see if we have a cached copy */
3522 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
3528 objectGUID = samdb_ntds_objectGUID(sam_ctx);
3530 return ldb_operr(sam_ctx);
3533 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
3534 if (ret != LDB_SUCCESS) {
3538 cached = talloc(sam_ctx, bool);
3539 if (cached == NULL) {
3540 return ldb_oom(sam_ctx);
3544 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
3545 if (ret != LDB_SUCCESS) {
3546 talloc_free(cached);
3547 return ldb_operr(sam_ctx);
3553 int samdb_dns_host_name(struct ldb_context *sam_ctx, const char **host_name)
3555 const char *_host_name = NULL;
3556 const char *attrs[] = { "dnsHostName", NULL };
3557 TALLOC_CTX *tmp_ctx = NULL;
3559 struct ldb_result *res = NULL;
3561 _host_name = (const char *)ldb_get_opaque(sam_ctx, "cache.dns_host_name");
3562 if (_host_name != NULL) {
3563 *host_name = _host_name;
3567 tmp_ctx = talloc_new(sam_ctx);
3569 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, NULL, attrs, 0);
3571 if (res->count != 1 || ret != LDB_SUCCESS) {
3572 DEBUG(0, ("Failed to get rootDSE for dnsHostName: %s",
3573 ldb_errstring(sam_ctx)));
3574 TALLOC_FREE(tmp_ctx);
3579 _host_name = ldb_msg_find_attr_as_string(res->msgs[0],
3582 if (_host_name == NULL) {
3583 DEBUG(0, ("Failed to get dnsHostName from rootDSE"));
3584 TALLOC_FREE(tmp_ctx);
3585 return LDB_ERR_OPERATIONS_ERROR;
3587 ret = ldb_set_opaque(sam_ctx, "cache.dns_host_name",
3588 discard_const_p(char *, _host_name));
3589 if (ret != LDB_SUCCESS) {
3590 TALLOC_FREE(tmp_ctx);
3591 return ldb_operr(sam_ctx);
3594 *host_name = talloc_steal(sam_ctx, _host_name);
3596 TALLOC_FREE(tmp_ctx);
3600 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
3602 TALLOC_CTX *tmp_ctx;
3605 tmp_ctx = talloc_new(ldb);
3606 if (tmp_ctx == NULL) {
3610 cached = talloc(tmp_ctx, bool);
3616 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
3620 talloc_steal(ldb, cached);
3621 talloc_free(tmp_ctx);
3625 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
3626 talloc_free(tmp_ctx);
3632 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
3633 * flags are DS_NTDSSETTINGS_OPT_*
3635 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
3639 TALLOC_CTX *tmp_ctx;
3640 struct ldb_result *res;
3641 struct ldb_dn *site_dn;
3642 const char *attrs[] = { "options", NULL };
3644 tmp_ctx = talloc_new(ldb_ctx);
3645 if (tmp_ctx == NULL)
3648 /* Retrieve the site dn for the ldb that we
3649 * have open. This is our local site.
3651 site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
3652 if (site_dn == NULL)
3655 /* Perform a one level (child) search from the local
3656 * site distinguided name. We're looking for the
3657 * "options" attribute within the nTDSSiteSettings
3660 rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
3661 LDB_SCOPE_ONELEVEL, attrs,
3662 "objectClass=nTDSSiteSettings");
3664 if (rc != LDB_SUCCESS || res->count != 1)
3667 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3669 talloc_free(tmp_ctx);
3674 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3675 talloc_free(tmp_ctx);
3676 return ldb_error(ldb_ctx, LDB_ERR_NO_SUCH_OBJECT, __func__);
3680 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3682 flags are DS_NTDS_OPTION_*
3684 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
3686 TALLOC_CTX *tmp_ctx;
3687 const char *attrs[] = { "options", NULL };
3689 struct ldb_result *res;
3691 tmp_ctx = talloc_new(ldb);
3692 if (tmp_ctx == NULL) {
3696 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3697 if (ret != LDB_SUCCESS) {
3701 if (res->count != 1) {
3705 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3707 talloc_free(tmp_ctx);
3712 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3713 talloc_free(tmp_ctx);
3714 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3717 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3719 const char *attrs[] = { "objectCategory", NULL };
3721 struct ldb_result *res;
3723 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3724 if (ret != LDB_SUCCESS) {
3728 if (res->count != 1) {
3732 return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3735 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3740 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3741 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3743 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3745 char **tokens, *ret;
3748 tokens = str_list_make(mem_ctx, cn, " -_");
3749 if (tokens == NULL || tokens[0] == NULL) {
3753 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3754 tokens[0][0] = tolower(tokens[0][0]);
3755 for (i = 1; tokens[i] != NULL; i++)
3756 tokens[i][0] = toupper(tokens[i][0]);
3758 ret = talloc_strdup(mem_ctx, tokens[0]);
3759 for (i = 1; tokens[i] != NULL; i++)
3760 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3762 talloc_free(tokens);
3768 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3770 int dsdb_functional_level(struct ldb_context *ldb)
3772 int *domainFunctionality =
3773 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3774 if (!domainFunctionality) {
3775 /* this is expected during initial provision */
3776 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3777 return DS_DOMAIN_FUNCTION_2000;
3779 return *domainFunctionality;
3783 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3785 int dsdb_forest_functional_level(struct ldb_context *ldb)
3787 int *forestFunctionality =
3788 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3789 if (!forestFunctionality) {
3790 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3791 return DS_DOMAIN_FUNCTION_2000;
3793 return *forestFunctionality;
3797 set a GUID in an extended DN structure
3799 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3805 status = GUID_to_ndr_blob(guid, dn, &v);
3806 if (!NT_STATUS_IS_OK(status)) {
3807 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3810 ret = ldb_dn_set_extended_component(dn, component_name, &v);
3816 return a GUID from a extended DN structure
3818 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3820 const struct ldb_val *v;
3822 v = ldb_dn_get_extended_component(dn, component_name);
3824 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3827 return GUID_from_ndr_blob(v, guid);
3831 return a uint64_t from a extended DN structure
3833 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3835 const struct ldb_val *v;
3837 v = ldb_dn_get_extended_component(dn, component_name);
3839 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3842 /* Just check we don't allow the caller to fill our stack */
3843 if (v->length >= 64) {
3844 return NT_STATUS_INVALID_PARAMETER;
3846 char s[v->length+1];
3847 memcpy(s, v->data, v->length);
3850 *val = strtoull(s, NULL, 0);
3852 return NT_STATUS_OK;
3856 return a NTTIME from a extended DN structure
3858 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3860 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3864 return a uint32_t from a extended DN structure
3866 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3868 const struct ldb_val *v;
3870 v = ldb_dn_get_extended_component(dn, component_name);
3872 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3875 /* Just check we don't allow the caller to fill our stack */
3876 if (v->length >= 32) {
3877 return NT_STATUS_INVALID_PARAMETER;
3879 char s[v->length + 1];
3880 memcpy(s, v->data, v->length);
3882 *val = strtoul(s, NULL, 0);
3885 return NT_STATUS_OK;
3889 return a dom_sid from a extended DN structure
3891 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3893 const struct ldb_val *sid_blob;
3894 enum ndr_err_code ndr_err;
3896 sid_blob = ldb_dn_get_extended_component(dn, component_name);
3898 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3901 ndr_err = ndr_pull_struct_blob_all_noalloc(sid_blob, sid,
3902 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3903 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3904 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3908 return NT_STATUS_OK;
3913 return RMD_FLAGS directly from a ldb_dn
3914 returns 0 if not found
3916 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3918 uint32_t rmd_flags = 0;
3919 NTSTATUS status = dsdb_get_extended_dn_uint32(dn, &rmd_flags,
3921 if (NT_STATUS_IS_OK(status)) {
3928 return RMD_FLAGS directly from a ldb_val for a DN
3929 returns 0 if RMD_FLAGS is not found
3931 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3937 if (val->length < 13) {
3940 p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3944 flags = strtoul(p+11, &end, 10);
3945 if (!end || *end != '>') {
3946 /* it must end in a > */
3953 return true if a ldb_val containing a DN in storage form is deleted
3955 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3957 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3961 return true if a ldb_val containing a DN in storage form is
3962 in the upgraded w2k3 linked attribute format
3964 bool dsdb_dn_is_upgraded_link_val(const struct ldb_val *val)
3966 return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
3970 return a DN for a wellknown GUID
3972 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3973 struct ldb_dn *nc_root, const char *wk_guid,
3974 struct ldb_dn **wkguid_dn)
3976 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3977 const char *attrs[] = { NULL };
3980 struct ldb_result *res;
3982 /* construct the magic WKGUID DN */
3983 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3984 wk_guid, ldb_dn_get_linearized(nc_root));
3986 talloc_free(tmp_ctx);
3987 return ldb_operr(samdb);
3990 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
3991 DSDB_SEARCH_SHOW_DELETED |
3992 DSDB_SEARCH_SHOW_RECYCLED);
3993 if (ret != LDB_SUCCESS) {
3994 talloc_free(tmp_ctx);
3998 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
3999 talloc_free(tmp_ctx);
4004 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
4006 return ldb_dn_compare(*dn1, *dn2);
4010 find a NC root given a DN within the NC
4012 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
4013 struct ldb_dn **nc_root)
4015 const char *root_attrs[] = { "namingContexts", NULL };
4016 TALLOC_CTX *tmp_ctx;
4018 struct ldb_message_element *el;
4019 struct ldb_result *root_res;
4021 struct ldb_dn **nc_dns;
4023 tmp_ctx = talloc_new(samdb);
4024 if (tmp_ctx == NULL) {
4025 return ldb_oom(samdb);
4028 ret = ldb_search(samdb, tmp_ctx, &root_res,
4029 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
4030 if (ret != LDB_SUCCESS || root_res->count == 0) {
4031 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
4032 talloc_free(tmp_ctx);
4036 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
4037 if ((el == NULL) || (el->num_values < 3)) {
4038 struct ldb_message *tmp_msg;
4040 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list.\n"));
4042 /* This generates a temporary list of NCs in order to let the
4043 * provisioning work. */
4044 tmp_msg = ldb_msg_new(tmp_ctx);
4045 if (tmp_msg == NULL) {
4046 talloc_free(tmp_ctx);
4047 return ldb_oom(samdb);
4049 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
4050 ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
4051 if (ret != LDB_SUCCESS) {
4052 talloc_free(tmp_ctx);
4055 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
4056 ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
4057 if (ret != LDB_SUCCESS) {
4058 talloc_free(tmp_ctx);
4061 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
4062 ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
4063 if (ret != LDB_SUCCESS) {
4064 talloc_free(tmp_ctx);
4067 el = &tmp_msg->elements[0];
4070 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
4072 talloc_free(tmp_ctx);
4073 return ldb_oom(samdb);
4076 for (i=0; i<el->num_values; i++) {
4077 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
4078 if (nc_dns[i] == NULL) {
4079 talloc_free(tmp_ctx);
4080 return ldb_operr(samdb);
4084 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
4086 for (i=0; i<el->num_values; i++) {
4087 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
4088 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
4089 talloc_free(tmp_ctx);
4094 talloc_free(tmp_ctx);
4095 return ldb_error(samdb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4100 find the deleted objects DN for any object, by looking for the NC
4101 root, then looking up the wellknown GUID
4103 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
4104 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
4105 struct ldb_dn **do_dn)
4107 struct ldb_dn *nc_root;
4110 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
4111 if (ret != LDB_SUCCESS) {
4115 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
4116 talloc_free(nc_root);
4121 return the tombstoneLifetime, in days
4123 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
4126 dn = ldb_get_config_basedn(ldb);
4128 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4130 dn = ldb_dn_copy(ldb, dn);
4132 return ldb_operr(ldb);
4134 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
4135 be a wellknown GUID for this */
4136 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
4138 return ldb_operr(ldb);
4141 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
4147 compare a ldb_val to a string case insensitively
4149 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
4151 size_t len = strlen(s);
4153 if (len > v->length) return 1;
4154 ret = strncasecmp(s, (const char *)v->data, v->length);
4155 if (ret != 0) return ret;
4156 if (v->length > len && v->data[len] != 0) {
4164 load the UDV for a partition in v2 format
4165 The list is returned sorted, and with our local cursor added
4167 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4168 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
4170 static const char *attrs[] = { "replUpToDateVector", NULL };
4171 struct ldb_result *r;
4172 const struct ldb_val *ouv_value;
4175 uint64_t highest_usn = 0;
4176 const struct GUID *our_invocation_id;
4177 static const struct timeval tv1970;
4178 NTTIME nt1970 = timeval_to_nttime(&tv1970);
4180 ret = dsdb_search_dn(samdb, mem_ctx, &r, dn, attrs, DSDB_SEARCH_SHOW_RECYCLED|DSDB_SEARCH_SHOW_DELETED);
4181 if (ret != LDB_SUCCESS) {
4185 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
4187 enum ndr_err_code ndr_err;
4188 struct replUpToDateVectorBlob ouv;
4190 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
4191 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
4192 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
4194 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4196 if (ouv.version != 2) {
4197 /* we always store as version 2, and
4198 * replUpToDateVector is not replicated
4200 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4203 *count = ouv.ctr.ctr2.count;
4204 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
4212 our_invocation_id = samdb_ntds_invocation_id(samdb);
4213 if (!our_invocation_id) {
4214 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
4215 talloc_free(*cursors);
4216 return ldb_operr(samdb);
4219 ret = ldb_sequence_number(samdb, LDB_SEQ_HIGHEST_SEQ, &highest_usn);
4220 if (ret != LDB_SUCCESS) {
4221 /* nothing to add - this can happen after a vampire */
4222 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4226 for (i=0; i<*count; i++) {
4227 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
4228 (*cursors)[i].highest_usn = highest_usn;
4229 (*cursors)[i].last_sync_success = nt1970;
4230 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4235 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
4237 return ldb_oom(samdb);
4240 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
4241 (*cursors)[*count].highest_usn = highest_usn;
4242 (*cursors)[*count].last_sync_success = nt1970;
4245 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4251 load the UDV for a partition in version 1 format
4252 The list is returned sorted, and with our local cursor added
4254 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4255 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
4257 struct drsuapi_DsReplicaCursor2 *v2;
4261 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
4262 if (ret != LDB_SUCCESS) {
4272 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
4273 if (*cursors == NULL) {
4275 return ldb_oom(samdb);
4278 for (i=0; i<*count; i++) {
4279 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
4280 (*cursors)[i].highest_usn = v2[i].highest_usn;
4287 add a set of controls to a ldb_request structure based on a set of
4288 flags. See util.h for a list of available flags
4290 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
4293 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
4294 struct ldb_search_options_control *options;
4295 /* Using the phantom root control allows us to search all partitions */
4296 options = talloc(req, struct ldb_search_options_control);
4297 if (options == NULL) {
4298 return LDB_ERR_OPERATIONS_ERROR;
4300 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
4302 ret = ldb_request_add_control(req,
4303 LDB_CONTROL_SEARCH_OPTIONS_OID,
4305 if (ret != LDB_SUCCESS) {
4310 if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
4311 ret = ldb_request_add_control(req,
4312 DSDB_CONTROL_NO_GLOBAL_CATALOG,
4314 if (ret != LDB_SUCCESS) {
4319 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
4320 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
4321 if (ret != LDB_SUCCESS) {
4326 if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
4327 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
4328 if (ret != LDB_SUCCESS) {
4333 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
4334 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
4335 if (ret != LDB_SUCCESS) {
4340 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
4341 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
4342 if (!extended_ctrl) {
4343 return LDB_ERR_OPERATIONS_ERROR;
4345 extended_ctrl->type = 1;
4347 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
4348 if (ret != LDB_SUCCESS) {
4353 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
4354 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
4355 if (ret != LDB_SUCCESS) {
4360 if (dsdb_flags & DSDB_MODIFY_RELAX) {
4361 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
4362 if (ret != LDB_SUCCESS) {
4367 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
4368 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
4369 if (ret != LDB_SUCCESS) {
4374 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
4375 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
4376 if (ret != LDB_SUCCESS) {
4381 if (dsdb_flags & DSDB_TREE_DELETE) {
4382 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
4383 if (ret != LDB_SUCCESS) {
4388 if (dsdb_flags & DSDB_PROVISION) {
4389 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
4390 if (ret != LDB_SUCCESS) {
4395 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
4396 if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
4397 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
4398 if (ret != LDB_SUCCESS) {
4403 if (dsdb_flags & DSDB_PASSWORD_BYPASS_LAST_SET) {
4405 * This must not be critical, as it will only be
4406 * handled (and need to be handled) if the other
4407 * attributes in the request bring password_hash into
4410 ret = ldb_request_add_control(req, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID, false, NULL);
4411 if (ret != LDB_SUCCESS) {
4416 if (dsdb_flags & DSDB_REPLMD_VANISH_LINKS) {
4417 ret = ldb_request_add_control(req, DSDB_CONTROL_REPLMD_VANISH_LINKS, true, NULL);
4418 if (ret != LDB_SUCCESS) {
4423 if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
4424 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
4425 if (ret != LDB_SUCCESS) {
4430 if (dsdb_flags & DSDB_FLAG_REPLICATED_UPDATE) {
4431 ret = ldb_request_add_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID, false, NULL);
4432 if (ret != LDB_SUCCESS) {
4441 returns true if a control with the specified "oid" exists
4443 bool dsdb_request_has_control(struct ldb_request *req, const char *oid)
4445 return (ldb_request_get_control(req, oid) != NULL);
4449 an add with a set of controls
4451 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
4452 uint32_t dsdb_flags)
4454 struct ldb_request *req;
4457 ret = ldb_build_add_req(&req, ldb, ldb,
4461 ldb_op_default_callback,
4464 if (ret != LDB_SUCCESS) return ret;
4466 ret = dsdb_request_add_controls(req, dsdb_flags);
4467 if (ret != LDB_SUCCESS) {
4472 ret = dsdb_autotransaction_request(ldb, req);
4479 a modify with a set of controls
4481 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
4482 uint32_t dsdb_flags)
4484 struct ldb_request *req;
4487 ret = ldb_build_mod_req(&req, ldb, ldb,
4491 ldb_op_default_callback,
4494 if (ret != LDB_SUCCESS) return ret;
4496 ret = dsdb_request_add_controls(req, dsdb_flags);
4497 if (ret != LDB_SUCCESS) {
4502 ret = dsdb_autotransaction_request(ldb, req);
4509 a delete with a set of flags
4511 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
4512 uint32_t dsdb_flags)
4514 struct ldb_request *req;
4517 ret = ldb_build_del_req(&req, ldb, ldb,
4521 ldb_op_default_callback,
4524 if (ret != LDB_SUCCESS) return ret;
4526 ret = dsdb_request_add_controls(req, dsdb_flags);
4527 if (ret != LDB_SUCCESS) {
4532 ret = dsdb_autotransaction_request(ldb, req);
4539 like dsdb_modify() but set all the element flags to
4540 LDB_FLAG_MOD_REPLACE
4542 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
4546 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
4547 for (i=0;i<msg->num_elements;i++) {
4548 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
4551 return dsdb_modify(ldb, msg, dsdb_flags);
4556 search for attrs on one DN, allowing for dsdb_flags controls
4558 int dsdb_search_dn(struct ldb_context *ldb,
4559 TALLOC_CTX *mem_ctx,
4560 struct ldb_result **_result,
4561 struct ldb_dn *basedn,
4562 const char * const *attrs,
4563 uint32_t dsdb_flags)
4566 struct ldb_request *req;
4567 struct ldb_result *res;
4569 res = talloc_zero(mem_ctx, struct ldb_result);
4571 return ldb_oom(ldb);
4574 ret = ldb_build_search_req(&req, ldb, res,
4581 ldb_search_default_callback,
4583 if (ret != LDB_SUCCESS) {
4588 ret = dsdb_request_add_controls(req, dsdb_flags);
4589 if (ret != LDB_SUCCESS) {
4594 ret = ldb_request(ldb, req);
4595 if (ret == LDB_SUCCESS) {
4596 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4600 if (ret != LDB_SUCCESS) {
4610 search for attrs on one DN, by the GUID of the DN, allowing for
4613 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
4614 TALLOC_CTX *mem_ctx,
4615 struct ldb_result **_result,
4616 const struct GUID *guid,
4617 const char * const *attrs,
4618 uint32_t dsdb_flags)
4620 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4624 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
4626 talloc_free(tmp_ctx);
4627 return ldb_oom(ldb);
4630 ret = dsdb_search_dn(ldb, mem_ctx, _result, dn, attrs, dsdb_flags);
4631 talloc_free(tmp_ctx);
4636 general search with dsdb_flags for controls
4638 int dsdb_search(struct ldb_context *ldb,
4639 TALLOC_CTX *mem_ctx,
4640 struct ldb_result **_result,
4641 struct ldb_dn *basedn,
4642 enum ldb_scope scope,
4643 const char * const *attrs,
4644 uint32_t dsdb_flags,
4645 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4648 struct ldb_request *req;
4649 struct ldb_result *res;
4651 char *expression = NULL;
4652 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4654 /* cross-partitions searches with a basedn break multi-domain support */
4655 SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
4657 res = talloc_zero(tmp_ctx, struct ldb_result);
4659 talloc_free(tmp_ctx);
4660 return ldb_oom(ldb);
4664 va_start(ap, exp_fmt);
4665 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4669 talloc_free(tmp_ctx);
4670 return ldb_oom(ldb);
4674 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
4681 ldb_search_default_callback,
4683 if (ret != LDB_SUCCESS) {
4684 talloc_free(tmp_ctx);
4688 ret = dsdb_request_add_controls(req, dsdb_flags);
4689 if (ret != LDB_SUCCESS) {
4690 talloc_free(tmp_ctx);
4691 ldb_reset_err_string(ldb);
4695 ret = ldb_request(ldb, req);
4696 if (ret == LDB_SUCCESS) {
4697 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4700 if (ret != LDB_SUCCESS) {
4701 talloc_free(tmp_ctx);
4705 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
4706 if (res->count == 0) {
4707 talloc_free(tmp_ctx);
4708 ldb_reset_err_string(ldb);
4709 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4711 if (res->count != 1) {
4712 talloc_free(tmp_ctx);
4713 ldb_reset_err_string(ldb);
4714 return LDB_ERR_CONSTRAINT_VIOLATION;
4718 *_result = talloc_steal(mem_ctx, res);
4719 talloc_free(tmp_ctx);
4726 general search with dsdb_flags for controls
4727 returns exactly 1 record or an error
4729 int dsdb_search_one(struct ldb_context *ldb,
4730 TALLOC_CTX *mem_ctx,
4731 struct ldb_message **msg,
4732 struct ldb_dn *basedn,
4733 enum ldb_scope scope,
4734 const char * const *attrs,
4735 uint32_t dsdb_flags,
4736 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4739 struct ldb_result *res;
4741 char *expression = NULL;
4742 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4744 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4746 res = talloc_zero(tmp_ctx, struct ldb_result);
4748 talloc_free(tmp_ctx);
4749 return ldb_oom(ldb);
4753 va_start(ap, exp_fmt);
4754 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4758 talloc_free(tmp_ctx);
4759 return ldb_oom(ldb);
4761 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4762 dsdb_flags, "%s", expression);
4764 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4768 if (ret != LDB_SUCCESS) {
4769 talloc_free(tmp_ctx);
4773 *msg = talloc_steal(mem_ctx, res->msgs[0]);
4774 talloc_free(tmp_ctx);
4779 /* returns back the forest DNS name */
4780 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4782 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4783 ldb_get_root_basedn(ldb));
4786 if (forest_name == NULL) {
4790 p = strchr(forest_name, '/');
4798 /* returns back the default domain DNS name */
4799 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4801 const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4802 ldb_get_default_basedn(ldb));
4805 if (domain_name == NULL) {
4809 p = strchr(domain_name, '/');
4818 validate that an DSA GUID belongs to the specified user sid.
4819 The user SID must be a domain controller account (either RODC or
4822 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4823 const struct GUID *dsa_guid,
4824 const struct dom_sid *sid)
4827 - find DN of record with the DSA GUID in the
4828 configuration partition (objectGUID)
4829 - remove "NTDS Settings" component from DN
4830 - do a base search on that DN for serverReference with
4832 - extract objectSid from resulting serverReference
4834 - check this sid matches the sid argument
4836 struct ldb_dn *config_dn;
4837 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4838 struct ldb_message *msg;
4839 const char *attrs1[] = { NULL };
4840 const char *attrs2[] = { "serverReference", NULL };
4842 struct ldb_dn *dn, *account_dn;
4843 struct dom_sid sid2;
4846 config_dn = ldb_get_config_basedn(ldb);
4848 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4849 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4850 if (ret != LDB_SUCCESS) {
4851 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4852 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4853 talloc_free(tmp_ctx);
4854 return ldb_operr(ldb);
4858 if (!ldb_dn_remove_child_components(dn, 1)) {
4859 talloc_free(tmp_ctx);
4860 return ldb_operr(ldb);
4863 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4864 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4865 "(objectClass=server)");
4866 if (ret != LDB_SUCCESS) {
4867 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4868 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4869 talloc_free(tmp_ctx);
4870 return ldb_operr(ldb);
4873 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4874 if (account_dn == NULL) {
4875 DEBUG(1,(__location__ ": Failed to find account dn "
4876 "(serverReference) for %s, parent of DSA with "
4877 "objectGUID %s, sid %s\n",
4878 ldb_dn_get_linearized(msg->dn),
4879 GUID_string(tmp_ctx, dsa_guid),
4880 dom_sid_string(tmp_ctx, sid)));
4881 talloc_free(tmp_ctx);
4882 return ldb_operr(ldb);
4885 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4886 if (!NT_STATUS_IS_OK(status)) {
4887 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4888 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4889 talloc_free(tmp_ctx);
4890 return ldb_operr(ldb);
4893 if (!dom_sid_equal(sid, &sid2)) {
4894 /* someone is trying to spoof another account */
4895 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4896 GUID_string(tmp_ctx, dsa_guid),
4897 dom_sid_string(tmp_ctx, sid),
4898 dom_sid_string(tmp_ctx, &sid2)));
4899 talloc_free(tmp_ctx);
4900 return ldb_operr(ldb);
4903 talloc_free(tmp_ctx);
4907 static const char * const secret_attributes[] = {
4908 DSDB_SECRET_ATTRIBUTES,
4913 check if the attribute belongs to the RODC filtered attribute set
4914 Note that attributes that are in the filtered attribute set are the
4915 ones that _are_ always sent to a RODC
4917 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4919 /* they never get secret attributes */
4920 if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4924 /* they do get non-secret critical attributes */
4925 if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4929 /* they do get non-secret attributes marked as being in the FAS */
4930 if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4934 /* other attributes are denied */
4938 /* return fsmo role dn and role owner dn for a particular role*/
4939 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4940 struct ldb_context *ldb,
4942 struct ldb_dn **fsmo_role_dn,
4943 struct ldb_dn **role_owner_dn)
4947 case DREPL_NAMING_MASTER:
4948 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
4949 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4950 if (ret != LDB_SUCCESS) {
4951 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
4952 ldb_errstring(ldb)));
4953 talloc_free(tmp_ctx);
4954 return WERR_DS_DRA_INTERNAL_ERROR;
4957 case DREPL_INFRASTRUCTURE_MASTER:
4958 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
4959 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4960 if (ret != LDB_SUCCESS) {
4961 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4962 ldb_errstring(ldb)));
4963 talloc_free(tmp_ctx);
4964 return WERR_DS_DRA_INTERNAL_ERROR;
4967 case DREPL_RID_MASTER:
4968 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
4969 if (ret != LDB_SUCCESS) {
4970 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
4971 talloc_free(tmp_ctx);
4972 return WERR_DS_DRA_INTERNAL_ERROR;
4975 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4976 if (ret != LDB_SUCCESS) {
4977 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
4978 ldb_errstring(ldb)));
4979 talloc_free(tmp_ctx);
4980 return WERR_DS_DRA_INTERNAL_ERROR;
4983 case DREPL_SCHEMA_MASTER:
4984 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
4985 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4986 if (ret != LDB_SUCCESS) {
4987 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4988 ldb_errstring(ldb)));
4989 talloc_free(tmp_ctx);
4990 return WERR_DS_DRA_INTERNAL_ERROR;
4993 case DREPL_PDC_MASTER:
4994 *fsmo_role_dn = ldb_get_default_basedn(ldb);
4995 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4996 if (ret != LDB_SUCCESS) {
4997 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
4998 ldb_errstring(ldb)));
4999 talloc_free(tmp_ctx);
5000 return WERR_DS_DRA_INTERNAL_ERROR;
5004 return WERR_DS_DRA_INTERNAL_ERROR;
5009 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
5010 TALLOC_CTX *mem_ctx,
5011 struct ldb_dn *server_dn)
5014 struct ldb_result *res = NULL;
5015 const char * const attrs[] = { "dNSHostName", NULL};
5017 ldb_ret = ldb_search(ldb, mem_ctx, &res,
5021 if (ldb_ret != LDB_SUCCESS) {
5022 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
5023 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
5027 return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
5031 returns true if an attribute is in the filter,
5032 false otherwise, provided that attribute value is provided with the expression
5034 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
5038 switch (tree->operation) {
5041 for (i=0;i<tree->u.list.num_elements;i++) {
5042 if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
5048 return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
5049 case LDB_OP_EQUALITY:
5050 case LDB_OP_GREATER:
5053 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
5057 case LDB_OP_SUBSTRING:
5058 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
5062 case LDB_OP_PRESENT:
5063 /* (attrname=*) is not filtered out */
5065 case LDB_OP_EXTENDED:
5066 if (tree->u.extended.attr &&
5067 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
5075 bool is_attr_in_list(const char * const * attrs, const char *attr)
5079 for (i = 0; attrs[i]; i++) {
5080 if (ldb_attr_cmp(attrs[i], attr) == 0)
5087 int dsdb_werror_at(struct ldb_context *ldb, int ldb_ecode, WERROR werr,
5088 const char *location, const char *func,
5091 if (reason == NULL) {
5092 reason = win_errstr(werr);
5094 ldb_asprintf_errstring(ldb, "%08X: %s at %s:%s",
5095 W_ERROR_V(werr), reason, location, func);
5100 map an ldb error code to an approximate NTSTATUS code
5102 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
5106 return NT_STATUS_OK;
5108 case LDB_ERR_PROTOCOL_ERROR:
5109 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
5111 case LDB_ERR_TIME_LIMIT_EXCEEDED:
5112 return NT_STATUS_IO_TIMEOUT;
5114 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
5115 return NT_STATUS_BUFFER_TOO_SMALL;
5117 case LDB_ERR_COMPARE_FALSE:
5118 case LDB_ERR_COMPARE_TRUE:
5119 return NT_STATUS_REVISION_MISMATCH;
5121 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
5122 return NT_STATUS_NOT_SUPPORTED;
5124 case LDB_ERR_STRONG_AUTH_REQUIRED:
5125 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
5126 case LDB_ERR_SASL_BIND_IN_PROGRESS:
5127 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
5128 case LDB_ERR_INVALID_CREDENTIALS:
5129 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
5130 case LDB_ERR_UNWILLING_TO_PERFORM:
5131 return NT_STATUS_ACCESS_DENIED;
5133 case LDB_ERR_NO_SUCH_OBJECT:
5134 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5136 case LDB_ERR_REFERRAL:
5137 case LDB_ERR_NO_SUCH_ATTRIBUTE:
5138 return NT_STATUS_NOT_FOUND;
5140 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
5141 return NT_STATUS_NOT_SUPPORTED;
5143 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
5144 return NT_STATUS_BUFFER_TOO_SMALL;
5146 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
5147 case LDB_ERR_INAPPROPRIATE_MATCHING:
5148 case LDB_ERR_CONSTRAINT_VIOLATION:
5149 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
5150 case LDB_ERR_INVALID_DN_SYNTAX:
5151 case LDB_ERR_NAMING_VIOLATION:
5152 case LDB_ERR_OBJECT_CLASS_VIOLATION:
5153 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
5154 case LDB_ERR_NOT_ALLOWED_ON_RDN:
5155 return NT_STATUS_INVALID_PARAMETER;
5157 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
5158 case LDB_ERR_ENTRY_ALREADY_EXISTS:
5159 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
5162 return NT_STATUS_NETWORK_BUSY;
5164 case LDB_ERR_ALIAS_PROBLEM:
5165 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
5166 case LDB_ERR_UNAVAILABLE:
5167 case LDB_ERR_LOOP_DETECT:
5168 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
5169 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
5171 case LDB_ERR_OPERATIONS_ERROR:
5174 return NT_STATUS_UNSUCCESSFUL;
5179 create a new naming context that will hold a partial replica
5181 int dsdb_create_partial_replica_NC(struct ldb_context *ldb, struct ldb_dn *dn)
5183 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
5184 struct ldb_message *msg;
5187 msg = ldb_msg_new(tmp_ctx);
5189 talloc_free(tmp_ctx);
5190 return ldb_oom(ldb);
5194 ret = ldb_msg_add_string(msg, "objectClass", "top");
5195 if (ret != LDB_SUCCESS) {
5196 talloc_free(tmp_ctx);
5197 return ldb_oom(ldb);
5200 /* [MS-DRSR] implies that we should only add the 'top'
5201 * objectclass, but that would cause lots of problems with our
5202 * objectclass code as top is not structural, so we add
5203 * 'domainDNS' as well to keep things sane. We're expecting
5204 * this new NC to be of objectclass domainDNS after
5205 * replication anyway
5207 ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
5208 if (ret != LDB_SUCCESS) {
5209 talloc_free(tmp_ctx);
5210 return ldb_oom(ldb);
5213 ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
5214 INSTANCE_TYPE_IS_NC_HEAD|
5215 INSTANCE_TYPE_NC_ABOVE|
5216 INSTANCE_TYPE_UNINSTANT);
5217 if (ret != LDB_SUCCESS) {
5218 talloc_free(tmp_ctx);
5219 return ldb_oom(ldb);
5222 ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
5223 if (ret != LDB_SUCCESS && ret != LDB_ERR_ENTRY_ALREADY_EXISTS) {
5224 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
5225 ldb_dn_get_linearized(dn),
5226 ldb_errstring(ldb), ldb_strerror(ret)));
5227 talloc_free(tmp_ctx);
5231 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
5233 talloc_free(tmp_ctx);
5238 build a GUID from a string
5240 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
5242 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
5244 uint32_t time_mid, time_hi_and_version;
5245 uint32_t clock_seq[2];
5250 return NT_STATUS_INVALID_PARAMETER;
5253 status = parse_guid_string(s,
5256 &time_hi_and_version,
5260 if (!NT_STATUS_IS_OK(status)) {
5264 guid->time_low = time_low;
5265 guid->time_mid = time_mid;
5266 guid->time_hi_and_version = time_hi_and_version;
5267 guid->clock_seq[0] = clock_seq[0];
5268 guid->clock_seq[1] = clock_seq[1];
5270 guid->node[i] = node[i];
5273 return NT_STATUS_OK;
5276 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
5278 return talloc_asprintf(mem_ctx,
5279 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
5280 guid->time_low, guid->time_mid,
5281 guid->time_hi_and_version,
5284 guid->node[0], guid->node[1],
5285 guid->node[2], guid->node[3],
5286 guid->node[4], guid->node[5]);
5290 * Return the effective badPwdCount
5292 * This requires that the user_msg have (if present):
5296 * This also requires that the domain_msg have (if present):
5297 * - lockOutObservationWindow
5299 static int dsdb_effective_badPwdCount(const struct ldb_message *user_msg,
5300 int64_t lockOutObservationWindow,
5303 int64_t badPasswordTime;
5304 badPasswordTime = ldb_msg_find_attr_as_int64(user_msg, "badPasswordTime", 0);
5306 if (badPasswordTime - lockOutObservationWindow >= now) {
5307 return ldb_msg_find_attr_as_int(user_msg, "badPwdCount", 0);
5314 * Returns a user's PSO, or NULL if none was found
5316 static struct ldb_result *lookup_user_pso(struct ldb_context *sam_ldb,
5317 TALLOC_CTX *mem_ctx,
5318 const struct ldb_message *user_msg,
5319 const char * const *attrs)
5321 struct ldb_result *res = NULL;
5322 struct ldb_dn *pso_dn = NULL;
5325 /* if the user has a PSO that applies, then use the PSO's setting */
5326 pso_dn = ldb_msg_find_attr_as_dn(sam_ldb, mem_ctx, user_msg,
5327 "msDS-ResultantPSO");
5329 if (pso_dn != NULL) {
5331 ret = dsdb_search_dn(sam_ldb, mem_ctx, &res, pso_dn, attrs, 0);
5332 if (ret != LDB_SUCCESS) {
5335 * log the error. The caller should fallback to using
5336 * the default domain password settings
5338 DBG_ERR("Error retrieving msDS-ResultantPSO %s for %s",
5339 ldb_dn_get_linearized(pso_dn),
5340 ldb_dn_get_linearized(user_msg->dn));
5342 talloc_free(pso_dn);
5348 * Return the effective badPwdCount
5350 * This requires that the user_msg have (if present):
5353 * - msDS-ResultantPSO
5355 int samdb_result_effective_badPwdCount(struct ldb_context *sam_ldb,
5356 TALLOC_CTX *mem_ctx,
5357 struct ldb_dn *domain_dn,
5358 const struct ldb_message *user_msg)
5360 struct timeval tv_now = timeval_current();
5361 NTTIME now = timeval_to_nttime(&tv_now);
5362 int64_t lockOutObservationWindow;
5363 struct ldb_result *res = NULL;
5364 const char *attrs[] = { "msDS-LockoutObservationWindow",
5367 res = lookup_user_pso(sam_ldb, mem_ctx, user_msg, attrs);
5370 lockOutObservationWindow =
5371 ldb_msg_find_attr_as_int64(res->msgs[0],
5372 "msDS-LockoutObservationWindow",
5377 /* no PSO was found, lookup the default domain setting */
5378 lockOutObservationWindow =
5379 samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn,
5380 "lockOutObservationWindow", NULL);
5383 return dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5387 * Returns the lockoutThreshold that applies. If a PSO is specified, then that
5388 * setting is used over the domain defaults
5390 static int64_t get_lockout_threshold(struct ldb_message *domain_msg,
5391 struct ldb_message *pso_msg)
5393 if (pso_msg != NULL) {
5394 return ldb_msg_find_attr_as_int(pso_msg,
5395 "msDS-LockoutThreshold", 0);
5397 return ldb_msg_find_attr_as_int(domain_msg,
5398 "lockoutThreshold", 0);
5403 * Returns the lockOutObservationWindow that applies. If a PSO is specified,
5404 * then that setting is used over the domain defaults
5406 static int64_t get_lockout_observation_window(struct ldb_message *domain_msg,
5407 struct ldb_message *pso_msg)
5409 if (pso_msg != NULL) {
5410 return ldb_msg_find_attr_as_int64(pso_msg,
5411 "msDS-LockoutObservationWindow",
5414 return ldb_msg_find_attr_as_int64(domain_msg,
5415 "lockOutObservationWindow", 0);
5420 * Prepare an update to the badPwdCount and associated attributes.
5422 * This requires that the user_msg have (if present):
5427 * This also requires that the domain_msg have (if present):
5429 * - lockoutThreshold
5430 * - lockOutObservationWindow
5432 * This also requires that the pso_msg have (if present):
5433 * - msDS-LockoutThreshold
5434 * - msDS-LockoutObservationWindow
5436 NTSTATUS dsdb_update_bad_pwd_count(TALLOC_CTX *mem_ctx,
5437 struct ldb_context *sam_ctx,
5438 struct ldb_message *user_msg,
5439 struct ldb_message *domain_msg,
5440 struct ldb_message *pso_msg,
5441 struct ldb_message **_mod_msg)
5443 int i, ret, badPwdCount;
5444 int64_t lockoutThreshold, lockOutObservationWindow;
5445 struct dom_sid *sid;
5446 struct timeval tv_now = timeval_current();
5447 NTTIME now = timeval_to_nttime(&tv_now);
5449 uint32_t pwdProperties, rid = 0;
5450 struct ldb_message *mod_msg;
5452 sid = samdb_result_dom_sid(mem_ctx, user_msg, "objectSid");
5454 pwdProperties = ldb_msg_find_attr_as_uint(domain_msg,
5455 "pwdProperties", -1);
5456 if (sid && !(pwdProperties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
5457 status = dom_sid_split_rid(NULL, sid, NULL, &rid);
5458 if (!NT_STATUS_IS_OK(status)) {
5460 * This can't happen anyway, but always try
5461 * and update the badPwdCount on failure
5469 * Work out if we are doing password lockout on the domain.
5470 * Also, the built in administrator account is exempt:
5471 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa375371%28v=vs.85%29.aspx
5473 lockoutThreshold = get_lockout_threshold(domain_msg, pso_msg);
5474 if (lockoutThreshold == 0 || (rid == DOMAIN_RID_ADMINISTRATOR)) {
5475 DEBUG(5, ("Not updating badPwdCount on %s after wrong password\n",
5476 ldb_dn_get_linearized(user_msg->dn)));
5477 return NT_STATUS_OK;
5480 mod_msg = ldb_msg_new(mem_ctx);
5481 if (mod_msg == NULL) {
5482 return NT_STATUS_NO_MEMORY;
5484 mod_msg->dn = ldb_dn_copy(mod_msg, user_msg->dn);
5485 if (mod_msg->dn == NULL) {
5486 TALLOC_FREE(mod_msg);
5487 return NT_STATUS_NO_MEMORY;
5490 lockOutObservationWindow = get_lockout_observation_window(domain_msg,
5493 badPwdCount = dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5497 ret = samdb_msg_add_int(sam_ctx, mod_msg, mod_msg, "badPwdCount", badPwdCount);
5498 if (ret != LDB_SUCCESS) {
5499 TALLOC_FREE(mod_msg);
5500 return NT_STATUS_NO_MEMORY;
5502 ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "badPasswordTime", now);
5503 if (ret != LDB_SUCCESS) {
5504 TALLOC_FREE(mod_msg);
5505 return NT_STATUS_NO_MEMORY;
5508 if (badPwdCount >= lockoutThreshold) {
5509 ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "lockoutTime", now);
5510 if (ret != LDB_SUCCESS) {
5511 TALLOC_FREE(mod_msg);
5512 return NT_STATUS_NO_MEMORY;
5514 DEBUGC( DBGC_AUTH, 1, ("Locked out user %s after %d wrong passwords\n",
5515 ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5517 DEBUGC( DBGC_AUTH, 5, ("Updated badPwdCount on %s after %d wrong passwords\n",
5518 ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5521 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
5522 for (i=0; i< mod_msg->num_elements; i++) {
5523 mod_msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
5526 *_mod_msg = mod_msg;
5527 return NT_STATUS_OK;
5531 * Sets defaults for a User object
5532 * List of default attributes set:
5533 * accountExpires, badPasswordTime, badPwdCount,
5534 * codePage, countryCode, lastLogoff, lastLogon
5535 * logonCount, pwdLastSet
5537 int dsdb_user_obj_set_defaults(struct ldb_context *ldb,
5538 struct ldb_message *usr_obj,
5539 struct ldb_request *req)
5542 const struct attribute_values {
5545 const char *add_value;
5546 const char *mod_value;
5547 const char *control;
5552 .name = "accountExpires",
5553 .add_value = "9223372036854775807",
5557 .name = "badPasswordTime",
5561 .name = "badPwdCount",
5569 .name = "countryCode",
5573 .name = "lastLogoff",
5577 .name = "lastLogon",
5581 .name = "logonCount",
5585 .name = "logonHours",
5586 .add_flags = DSDB_FLAG_INTERNAL_FORCE_META_DATA,
5589 .name = "pwdLastSet",
5591 .control = DSDB_CONTROL_PASSWORD_DEFAULT_LAST_SET_OID,
5594 .name = "adminCount",
5598 .name = "operatorCount",
5603 for (i = 0; i < ARRAY_SIZE(map); i++) {
5605 const char *value = NULL;
5608 if (req != NULL && req->operation == LDB_ADD) {
5609 value = map[i].add_value;
5610 flags = map[i].add_flags;
5612 value = map[i].mod_value;
5613 flags = map[i].mod_flags;
5616 if (value == NULL) {
5617 value = map[i].value;
5620 if (value != NULL) {
5621 flags |= LDB_FLAG_MOD_ADD;
5628 ret = samdb_find_or_add_attribute_ex(ldb, usr_obj,
5632 if (ret != LDB_SUCCESS) {
5636 if (req != NULL && added && map[i].control != NULL) {
5637 ret = ldb_request_add_control(req,
5640 if (ret != LDB_SUCCESS) {
5650 * Sets 'sAMAccountType on user object based on userAccountControl
5651 * @param ldb Current ldb_context
5652 * @param usr_obj ldb_message representing User object
5653 * @param user_account_control Value for userAccountControl flags
5654 * @param account_type_p Optional pointer to account_type to return
5655 * @return LDB_SUCCESS or LDB_ERR* code on failure
5657 int dsdb_user_obj_set_account_type(struct ldb_context *ldb, struct ldb_message *usr_obj,
5658 uint32_t user_account_control, uint32_t *account_type_p)
5661 uint32_t account_type;
5662 struct ldb_message_element *el;
5664 account_type = ds_uf2atype(user_account_control);
5665 if (account_type == 0) {
5666 ldb_set_errstring(ldb, "dsdb: Unrecognized account type!");
5667 return LDB_ERR_UNWILLING_TO_PERFORM;
5669 ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5672 if (ret != LDB_SUCCESS) {
5675 el = ldb_msg_find_element(usr_obj, "sAMAccountType");
5676 el->flags = LDB_FLAG_MOD_REPLACE;
5678 if (account_type_p) {
5679 *account_type_p = account_type;
5686 * Determine and set primaryGroupID based on userAccountControl value
5687 * @param ldb Current ldb_context
5688 * @param usr_obj ldb_message representing User object
5689 * @param user_account_control Value for userAccountControl flags
5690 * @param group_rid_p Optional pointer to group RID to return
5691 * @return LDB_SUCCESS or LDB_ERR* code on failure
5693 int dsdb_user_obj_set_primary_group_id(struct ldb_context *ldb, struct ldb_message *usr_obj,
5694 uint32_t user_account_control, uint32_t *group_rid_p)
5698 struct ldb_message_element *el;
5700 rid = ds_uf2prim_group_rid(user_account_control);
5702 ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5703 "primaryGroupID", rid);
5704 if (ret != LDB_SUCCESS) {
5707 el = ldb_msg_find_element(usr_obj, "primaryGroupID");
5708 el->flags = LDB_FLAG_MOD_REPLACE;
5718 * Returns True if the source and target DNs both have the same naming context,
5719 * i.e. they're both in the same partition.
5721 bool dsdb_objects_have_same_nc(struct ldb_context *ldb,
5722 TALLOC_CTX *mem_ctx,
5723 struct ldb_dn *source_dn,
5724 struct ldb_dn *target_dn)
5726 TALLOC_CTX *tmp_ctx;
5727 struct ldb_dn *source_nc;
5728 struct ldb_dn *target_nc;
5730 bool same_nc = true;
5732 tmp_ctx = talloc_new(mem_ctx);
5734 ret = dsdb_find_nc_root(ldb, tmp_ctx, source_dn, &source_nc);
5735 if (ret != LDB_SUCCESS) {
5736 DBG_ERR("Failed to find base DN for source %s\n",
5737 ldb_dn_get_linearized(source_dn));
5738 talloc_free(tmp_ctx);
5742 ret = dsdb_find_nc_root(ldb, tmp_ctx, target_dn, &target_nc);
5743 if (ret != LDB_SUCCESS) {
5744 DBG_ERR("Failed to find base DN for target %s\n",
5745 ldb_dn_get_linearized(target_dn));
5746 talloc_free(tmp_ctx);
5750 same_nc = (ldb_dn_compare(source_nc, target_nc) == 0);
5752 talloc_free(tmp_ctx);