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;
2169 TALLOC_FREE(password_script);
2170 return SAMR_VALIDATION_STATUS_SUCCESS;
2173 TALLOC_FREE(password_script);
2176 * Here are the standard AD password quality rules, which we
2177 * run after the script.
2180 if (!check_password_quality(utf8_pw)) {
2181 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2184 return SAMR_VALIDATION_STATUS_SUCCESS;
2188 * Callback for "samdb_set_password" password change
2190 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
2195 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2198 if (ares->error != LDB_SUCCESS) {
2200 req->context = talloc_steal(req,
2201 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2203 return ldb_request_done(req, ret);
2206 if (ares->type != LDB_REPLY_DONE) {
2208 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2211 req->context = talloc_steal(req,
2212 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2214 return ldb_request_done(req, LDB_SUCCESS);
2218 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2219 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2220 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2221 * user change or not. The "rejectReason" gives some more information if the
2224 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2225 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2227 static NTSTATUS samdb_set_password_internal(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2228 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2229 const DATA_BLOB *new_password,
2230 const struct samr_Password *lmNewHash,
2231 const struct samr_Password *ntNewHash,
2232 const struct samr_Password *lmOldHash,
2233 const struct samr_Password *ntOldHash,
2234 enum samPwdChangeReason *reject_reason,
2235 struct samr_DomInfo1 **_dominfo,
2236 bool permit_interdomain_trust)
2238 struct ldb_message *msg;
2239 struct ldb_message_element *el;
2240 struct ldb_request *req;
2241 struct dsdb_control_password_change_status *pwd_stat = NULL;
2243 bool hash_values = false;
2244 NTSTATUS status = NT_STATUS_OK;
2246 #define CHECK_RET(x) \
2247 if (x != LDB_SUCCESS) { \
2249 return NT_STATUS_NO_MEMORY; \
2252 msg = ldb_msg_new(mem_ctx);
2254 return NT_STATUS_NO_MEMORY;
2257 if ((new_password != NULL)
2258 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
2259 /* we have the password as plaintext UTF16 */
2260 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
2261 new_password, NULL));
2262 el = ldb_msg_find_element(msg, "clearTextPassword");
2263 el->flags = LDB_FLAG_MOD_REPLACE;
2264 } else if ((new_password == NULL)
2265 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2266 /* we have a password as LM and/or NT hash */
2267 if (lmNewHash != NULL) {
2268 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2269 "dBCSPwd", lmNewHash));
2270 el = ldb_msg_find_element(msg, "dBCSPwd");
2271 el->flags = LDB_FLAG_MOD_REPLACE;
2273 if (ntNewHash != NULL) {
2274 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2275 "unicodePwd", ntNewHash));
2276 el = ldb_msg_find_element(msg, "unicodePwd");
2277 el->flags = LDB_FLAG_MOD_REPLACE;
2281 /* the password wasn't specified correctly */
2283 return NT_STATUS_INVALID_PARAMETER;
2286 /* build modify request */
2287 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2288 samdb_set_password_callback, NULL);
2289 if (ret != LDB_SUCCESS) {
2291 return NT_STATUS_NO_MEMORY;
2294 /* A password change operation */
2295 if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2296 struct dsdb_control_password_change *change;
2298 change = talloc(req, struct dsdb_control_password_change);
2299 if (change == NULL) {
2302 return NT_STATUS_NO_MEMORY;
2305 change->old_nt_pwd_hash = ntOldHash;
2306 change->old_lm_pwd_hash = lmOldHash;
2308 ret = ldb_request_add_control(req,
2309 DSDB_CONTROL_PASSWORD_CHANGE_OID,
2311 if (ret != LDB_SUCCESS) {
2314 return NT_STATUS_NO_MEMORY;
2318 ret = ldb_request_add_control(req,
2319 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2321 if (ret != LDB_SUCCESS) {
2324 return NT_STATUS_NO_MEMORY;
2327 if (permit_interdomain_trust) {
2328 ret = ldb_request_add_control(req,
2329 DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID,
2331 if (ret != LDB_SUCCESS) {
2334 return NT_STATUS_NO_MEMORY;
2337 ret = ldb_request_add_control(req,
2338 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2340 if (ret != LDB_SUCCESS) {
2343 return NT_STATUS_NO_MEMORY;
2346 ret = dsdb_autotransaction_request(ldb, req);
2348 if (req->context != NULL) {
2349 struct ldb_control *control = talloc_get_type_abort(req->context,
2350 struct ldb_control);
2351 pwd_stat = talloc_get_type_abort(control->data,
2352 struct dsdb_control_password_change_status);
2353 talloc_steal(mem_ctx, pwd_stat);
2359 /* Sets the domain info (if requested) */
2360 if (_dominfo != NULL) {
2361 struct samr_DomInfo1 *dominfo;
2363 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2364 if (dominfo == NULL) {
2365 return NT_STATUS_NO_MEMORY;
2368 if (pwd_stat != NULL) {
2369 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2370 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2371 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2372 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2373 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2376 *_dominfo = dominfo;
2379 if (reject_reason != NULL) {
2380 if (pwd_stat != NULL) {
2381 *reject_reason = pwd_stat->reject_reason;
2383 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2387 if (pwd_stat != NULL) {
2388 talloc_free(pwd_stat);
2391 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2392 const char *errmsg = ldb_errstring(ldb);
2393 char *endptr = NULL;
2394 WERROR werr = WERR_GEN_FAILURE;
2395 status = NT_STATUS_UNSUCCESSFUL;
2396 if (errmsg != NULL) {
2397 werr = W_ERROR(strtol(errmsg, &endptr, 16));
2398 DBG_WARNING("%s\n", errmsg);
2400 if (endptr != errmsg) {
2401 if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2402 status = NT_STATUS_WRONG_PASSWORD;
2404 if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2405 status = NT_STATUS_PASSWORD_RESTRICTION;
2408 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2409 /* don't let the caller know if an account doesn't exist */
2410 status = NT_STATUS_WRONG_PASSWORD;
2411 } else if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
2412 status = NT_STATUS_ACCESS_DENIED;
2413 } else if (ret != LDB_SUCCESS) {
2414 DEBUG(1, ("Failed to set password on %s: %s\n",
2415 ldb_dn_get_linearized(user_dn),
2416 ldb_errstring(ldb)));
2417 status = NT_STATUS_UNSUCCESSFUL;
2423 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2424 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2425 const DATA_BLOB *new_password,
2426 const struct samr_Password *lmNewHash,
2427 const struct samr_Password *ntNewHash,
2428 const struct samr_Password *lmOldHash,
2429 const struct samr_Password *ntOldHash,
2430 enum samPwdChangeReason *reject_reason,
2431 struct samr_DomInfo1 **_dominfo)
2433 return samdb_set_password_internal(ldb, mem_ctx,
2436 lmNewHash, ntNewHash,
2437 lmOldHash, ntOldHash,
2438 reject_reason, _dominfo,
2439 false); /* reject trusts */
2443 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2444 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2445 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2446 * user change or not. The "rejectReason" gives some more information if the
2449 * This wrapper function for "samdb_set_password" takes a SID as input rather
2452 * This call encapsulates a new LDB transaction for changing the password;
2453 * therefore the user hasn't to start a new one.
2455 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2456 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2457 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2458 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2460 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2461 const struct dom_sid *user_sid,
2462 const uint32_t *new_version, /* optional for trusts */
2463 const DATA_BLOB *new_password,
2464 const struct samr_Password *lmNewHash,
2465 const struct samr_Password *ntNewHash,
2466 const struct samr_Password *lmOldHash,
2467 const struct samr_Password *ntOldHash,
2468 enum samPwdChangeReason *reject_reason,
2469 struct samr_DomInfo1 **_dominfo)
2471 TALLOC_CTX *frame = talloc_stackframe();
2473 const char * const user_attrs[] = {
2474 "userAccountControl",
2478 struct ldb_message *user_msg = NULL;
2482 ret = ldb_transaction_start(ldb);
2483 if (ret != LDB_SUCCESS) {
2484 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2486 return NT_STATUS_TRANSACTION_ABORTED;
2489 ret = dsdb_search_one(ldb, frame, &user_msg, ldb_get_default_basedn(ldb),
2490 LDB_SCOPE_SUBTREE, user_attrs, 0,
2491 "(&(objectSid=%s)(objectClass=user))",
2492 ldap_encode_ndr_dom_sid(frame, user_sid));
2493 if (ret != LDB_SUCCESS) {
2494 ldb_transaction_cancel(ldb);
2495 DEBUG(3, ("samdb_set_password_sid: SID[%s] not found in samdb %s - %s, "
2496 "returning NO_SUCH_USER\n",
2497 dom_sid_string(frame, user_sid),
2498 ldb_strerror(ret), ldb_errstring(ldb)));
2500 return NT_STATUS_NO_SUCH_USER;
2503 uac = ldb_msg_find_attr_as_uint(user_msg, "userAccountControl", 0);
2504 if (!(uac & UF_ACCOUNT_TYPE_MASK)) {
2505 ldb_transaction_cancel(ldb);
2506 DEBUG(1, ("samdb_set_password_sid: invalid "
2507 "userAccountControl[0x%08X] for SID[%s] DN[%s], "
2508 "returning NO_SUCH_USER\n",
2509 (unsigned)uac, dom_sid_string(frame, user_sid),
2510 ldb_dn_get_linearized(user_msg->dn)));
2512 return NT_STATUS_NO_SUCH_USER;
2515 if (uac & UF_INTERDOMAIN_TRUST_ACCOUNT) {
2516 const char * const tdo_attrs[] = {
2517 "trustAuthIncoming",
2521 struct ldb_message *tdo_msg = NULL;
2522 const char *account_name = NULL;
2523 uint32_t trust_direction;
2525 const struct ldb_val *old_val = NULL;
2526 struct trustAuthInOutBlob old_blob = {};
2527 uint32_t old_version = 0;
2528 struct AuthenticationInformation *old_version_a = NULL;
2529 uint32_t _new_version = 0;
2530 struct trustAuthInOutBlob new_blob = {};
2531 struct ldb_val new_val = {};
2532 struct timeval tv = timeval_current();
2533 NTTIME now = timeval_to_nttime(&tv);
2534 enum ndr_err_code ndr_err;
2536 if (new_password == NULL && ntNewHash == NULL) {
2537 ldb_transaction_cancel(ldb);
2538 DEBUG(1, ("samdb_set_password_sid: "
2539 "no new password provided "
2540 "sAMAccountName for SID[%s] DN[%s], "
2541 "returning INVALID_PARAMETER\n",
2542 dom_sid_string(frame, user_sid),
2543 ldb_dn_get_linearized(user_msg->dn)));
2545 return NT_STATUS_INVALID_PARAMETER;
2548 if (new_password != NULL && ntNewHash != NULL) {
2549 ldb_transaction_cancel(ldb);
2550 DEBUG(1, ("samdb_set_password_sid: "
2551 "two new passwords provided "
2552 "sAMAccountName for SID[%s] DN[%s], "
2553 "returning INVALID_PARAMETER\n",
2554 dom_sid_string(frame, user_sid),
2555 ldb_dn_get_linearized(user_msg->dn)));
2557 return NT_STATUS_INVALID_PARAMETER;
2560 if (new_password != NULL && (new_password->length % 2)) {
2561 ldb_transaction_cancel(ldb);
2562 DEBUG(2, ("samdb_set_password_sid: "
2563 "invalid utf16 length (%zu) "
2564 "sAMAccountName for SID[%s] DN[%s], "
2565 "returning WRONG_PASSWORD\n",
2566 new_password->length,
2567 dom_sid_string(frame, user_sid),
2568 ldb_dn_get_linearized(user_msg->dn)));
2570 return NT_STATUS_WRONG_PASSWORD;
2573 if (new_password != NULL && new_password->length >= 500) {
2574 ldb_transaction_cancel(ldb);
2575 DEBUG(2, ("samdb_set_password_sid: "
2576 "utf16 password too long (%zu) "
2577 "sAMAccountName for SID[%s] DN[%s], "
2578 "returning WRONG_PASSWORD\n",
2579 new_password->length,
2580 dom_sid_string(frame, user_sid),
2581 ldb_dn_get_linearized(user_msg->dn)));
2583 return NT_STATUS_WRONG_PASSWORD;
2586 account_name = ldb_msg_find_attr_as_string(user_msg,
2587 "sAMAccountName", NULL);
2588 if (account_name == NULL) {
2589 ldb_transaction_cancel(ldb);
2590 DEBUG(1, ("samdb_set_password_sid: missing "
2591 "sAMAccountName for SID[%s] DN[%s], "
2592 "returning NO_SUCH_USER\n",
2593 dom_sid_string(frame, user_sid),
2594 ldb_dn_get_linearized(user_msg->dn)));
2596 return NT_STATUS_NO_SUCH_USER;
2599 nt_status = dsdb_trust_search_tdo_by_type(ldb,
2604 if (!NT_STATUS_IS_OK(nt_status)) {
2605 ldb_transaction_cancel(ldb);
2606 DEBUG(1, ("samdb_set_password_sid: dsdb_trust_search_tdo "
2607 "failed(%s) for sAMAccountName[%s] SID[%s] DN[%s], "
2608 "returning INTERNAL_DB_CORRUPTION\n",
2609 nt_errstr(nt_status), account_name,
2610 dom_sid_string(frame, user_sid),
2611 ldb_dn_get_linearized(user_msg->dn)));
2613 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2616 trust_direction = ldb_msg_find_attr_as_int(tdo_msg,
2617 "trustDirection", 0);
2618 if (!(trust_direction & LSA_TRUST_DIRECTION_INBOUND)) {
2619 ldb_transaction_cancel(ldb);
2620 DEBUG(1, ("samdb_set_password_sid: direction[0x%08X] is "
2621 "not inbound for sAMAccountName[%s] "
2623 "returning INTERNAL_DB_CORRUPTION\n",
2624 (unsigned)trust_direction,
2626 ldb_dn_get_linearized(user_msg->dn),
2627 ldb_dn_get_linearized(tdo_msg->dn)));
2629 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2632 old_val = ldb_msg_find_ldb_val(tdo_msg, "trustAuthIncoming");
2633 if (old_val != NULL) {
2634 ndr_err = ndr_pull_struct_blob(old_val, frame, &old_blob,
2635 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2636 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2637 ldb_transaction_cancel(ldb);
2638 DEBUG(1, ("samdb_set_password_sid: "
2639 "failed(%s) to parse "
2640 "trustAuthOutgoing sAMAccountName[%s] "
2642 "returning INTERNAL_DB_CORRUPTION\n",
2643 ndr_map_error2string(ndr_err),
2645 ldb_dn_get_linearized(user_msg->dn),
2646 ldb_dn_get_linearized(tdo_msg->dn)));
2649 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2653 for (i = old_blob.current.count; i > 0; i--) {
2654 struct AuthenticationInformation *a =
2655 &old_blob.current.array[i - 1];
2657 switch (a->AuthType) {
2658 case TRUST_AUTH_TYPE_NONE:
2659 if (i == old_blob.current.count) {
2661 * remove TRUST_AUTH_TYPE_NONE at the
2664 old_blob.current.count--;
2668 case TRUST_AUTH_TYPE_VERSION:
2670 old_version = a->AuthInfo.version.version;
2673 case TRUST_AUTH_TYPE_CLEAR:
2676 case TRUST_AUTH_TYPE_NT4OWF:
2681 if (new_version == NULL) {
2683 new_version = &_new_version;
2686 if (old_version_a != NULL && *new_version != (old_version + 1)) {
2687 old_version_a->LastUpdateTime = now;
2688 old_version_a->AuthType = TRUST_AUTH_TYPE_NONE;
2691 new_blob.count = MAX(old_blob.current.count, 2);
2692 new_blob.current.array = talloc_zero_array(frame,
2693 struct AuthenticationInformation,
2695 if (new_blob.current.array == NULL) {
2696 ldb_transaction_cancel(ldb);
2698 return NT_STATUS_NO_MEMORY;
2700 new_blob.previous.array = talloc_zero_array(frame,
2701 struct AuthenticationInformation,
2703 if (new_blob.current.array == NULL) {
2704 ldb_transaction_cancel(ldb);
2706 return NT_STATUS_NO_MEMORY;
2709 for (i = 0; i < old_blob.current.count; i++) {
2710 struct AuthenticationInformation *o =
2711 &old_blob.current.array[i];
2712 struct AuthenticationInformation *p =
2713 &new_blob.previous.array[i];
2716 new_blob.previous.count++;
2718 for (; i < new_blob.count; i++) {
2719 struct AuthenticationInformation *pi =
2720 &new_blob.previous.array[i];
2724 * new_blob.previous is still empty so
2725 * we'll do new_blob.previous = new_blob.current
2731 pi->LastUpdateTime = now;
2732 pi->AuthType = TRUST_AUTH_TYPE_NONE;
2733 new_blob.previous.count++;
2736 for (i = 0; i < new_blob.count; i++) {
2737 struct AuthenticationInformation *ci =
2738 &new_blob.current.array[i];
2740 ci->LastUpdateTime = now;
2743 if (ntNewHash != NULL) {
2744 ci->AuthType = TRUST_AUTH_TYPE_NT4OWF;
2745 ci->AuthInfo.nt4owf.password = *ntNewHash;
2749 ci->AuthType = TRUST_AUTH_TYPE_CLEAR;
2750 ci->AuthInfo.clear.size = new_password->length;
2751 ci->AuthInfo.clear.password = new_password->data;
2754 ci->AuthType = TRUST_AUTH_TYPE_VERSION;
2755 ci->AuthInfo.version.version = *new_version;
2758 ci->AuthType = TRUST_AUTH_TYPE_NONE;
2762 new_blob.current.count++;
2765 if (new_blob.previous.count == 0) {
2766 TALLOC_FREE(new_blob.previous.array);
2767 new_blob.previous = new_blob.current;
2770 ndr_err = ndr_push_struct_blob(&new_val, frame, &new_blob,
2771 (ndr_push_flags_fn_t)ndr_push_trustAuthInOutBlob);
2772 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2773 ldb_transaction_cancel(ldb);
2774 DEBUG(1, ("samdb_set_password_sid: "
2775 "failed(%s) to generate "
2776 "trustAuthOutgoing sAMAccountName[%s] "
2778 "returning UNSUCCESSFUL\n",
2779 ndr_map_error2string(ndr_err),
2781 ldb_dn_get_linearized(user_msg->dn),
2782 ldb_dn_get_linearized(tdo_msg->dn)));
2784 return NT_STATUS_UNSUCCESSFUL;
2787 tdo_msg->num_elements = 0;
2788 TALLOC_FREE(tdo_msg->elements);
2790 ret = ldb_msg_add_empty(tdo_msg, "trustAuthIncoming",
2791 LDB_FLAG_MOD_REPLACE, NULL);
2792 if (ret != LDB_SUCCESS) {
2793 ldb_transaction_cancel(ldb);
2795 return NT_STATUS_NO_MEMORY;
2797 ret = ldb_msg_add_value(tdo_msg, "trustAuthIncoming",
2799 if (ret != LDB_SUCCESS) {
2800 ldb_transaction_cancel(ldb);
2802 return NT_STATUS_NO_MEMORY;
2805 ret = ldb_modify(ldb, tdo_msg);
2806 if (ret != LDB_SUCCESS) {
2807 nt_status = dsdb_ldb_err_to_ntstatus(ret);
2808 ldb_transaction_cancel(ldb);
2809 DEBUG(1, ("samdb_set_password_sid: "
2810 "failed to replace "
2811 "trustAuthOutgoing sAMAccountName[%s] "
2815 ldb_dn_get_linearized(user_msg->dn),
2816 ldb_dn_get_linearized(tdo_msg->dn),
2817 nt_errstr(nt_status), ldb_errstring(ldb)));
2823 nt_status = samdb_set_password_internal(ldb, mem_ctx,
2826 lmNewHash, ntNewHash,
2827 lmOldHash, ntOldHash,
2828 reject_reason, _dominfo,
2829 true); /* permit trusts */
2830 if (!NT_STATUS_IS_OK(nt_status)) {
2831 ldb_transaction_cancel(ldb);
2836 ret = ldb_transaction_commit(ldb);
2837 if (ret != LDB_SUCCESS) {
2838 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2839 ldb_dn_get_linearized(user_msg->dn),
2840 ldb_errstring(ldb)));
2842 return NT_STATUS_TRANSACTION_ABORTED;
2846 return NT_STATUS_OK;
2850 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2851 struct dom_sid *sid, struct ldb_dn **ret_dn)
2853 struct ldb_message *msg;
2854 struct ldb_dn *basedn;
2858 sidstr = dom_sid_string(mem_ctx, sid);
2859 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2861 /* We might have to create a ForeignSecurityPrincipal, even if this user
2862 * is in our own domain */
2864 msg = ldb_msg_new(sidstr);
2866 talloc_free(sidstr);
2867 return NT_STATUS_NO_MEMORY;
2870 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2871 ldb_get_default_basedn(sam_ctx),
2872 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2874 if (ret != LDB_SUCCESS) {
2875 DEBUG(0, ("Failed to find DN for "
2876 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2877 talloc_free(sidstr);
2878 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2881 /* add core elements to the ldb_message for the alias */
2883 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2884 talloc_free(sidstr);
2885 return NT_STATUS_NO_MEMORY;
2888 ret = ldb_msg_add_string(msg, "objectClass",
2889 "foreignSecurityPrincipal");
2890 if (ret != LDB_SUCCESS) {
2891 talloc_free(sidstr);
2892 return NT_STATUS_NO_MEMORY;
2895 /* create the alias */
2896 ret = ldb_add(sam_ctx, msg);
2897 if (ret != LDB_SUCCESS) {
2898 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2900 ldb_dn_get_linearized(msg->dn),
2901 ldb_errstring(sam_ctx)));
2902 talloc_free(sidstr);
2903 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2906 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2907 talloc_free(sidstr);
2909 return NT_STATUS_OK;
2914 Find the DN of a domain, assuming it to be a dotted.dns name
2917 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2920 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2921 const char *binary_encoded;
2922 const char * const *split_realm;
2929 split_realm = (const char * const *)str_list_make(tmp_ctx, dns_domain, ".");
2931 talloc_free(tmp_ctx);
2934 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2935 for (i=0; split_realm[i]; i++) {
2936 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2937 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2938 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2939 binary_encoded, ldb_dn_get_linearized(dn)));
2940 talloc_free(tmp_ctx);
2944 if (!ldb_dn_validate(dn)) {
2945 DEBUG(2, ("Failed to validated DN %s\n",
2946 ldb_dn_get_linearized(dn)));
2947 talloc_free(tmp_ctx);
2950 talloc_free(tmp_ctx);
2956 Find the DNS equivalent of a DN, in dotted DNS form
2958 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2960 int i, num_components = ldb_dn_get_comp_num(dn);
2961 char *dns_name = talloc_strdup(mem_ctx, "");
2962 if (dns_name == NULL) {
2966 for (i=0; i<num_components; i++) {
2967 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2970 talloc_free(dns_name);
2973 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2974 (int)v->length, (int)v->length, (char *)v->data);
2976 talloc_free(dns_name);
2982 /* remove the last '.' */
2983 if (dns_name[0] != 0) {
2984 dns_name[strlen(dns_name)-1] = 0;
2991 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2992 name is based on the forest DNS name
2994 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
2995 TALLOC_CTX *mem_ctx,
2996 const struct GUID *ntds_guid)
2998 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2999 const char *guid_str;
3000 struct ldb_dn *forest_dn;
3001 const char *dnsforest;
3004 guid_str = GUID_string(tmp_ctx, ntds_guid);
3005 if (guid_str == NULL) {
3006 talloc_free(tmp_ctx);
3009 forest_dn = ldb_get_root_basedn(samdb);
3010 if (forest_dn == NULL) {
3011 talloc_free(tmp_ctx);
3014 dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
3015 if (dnsforest == NULL) {
3016 talloc_free(tmp_ctx);
3019 ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
3020 talloc_free(tmp_ctx);
3026 Find the DN of a domain, be it the netbios or DNS name
3028 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
3029 const char *domain_name)
3031 const char * const domain_ref_attrs[] = {
3034 const char * const domain_ref2_attrs[] = {
3037 struct ldb_result *res_domain_ref;
3038 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
3039 /* find the domain's DN */
3040 int ret_domain = ldb_search(ldb, mem_ctx,
3042 samdb_partitions_dn(ldb, mem_ctx),
3045 "(&(nETBIOSName=%s)(objectclass=crossRef))",
3047 if (ret_domain != LDB_SUCCESS) {
3051 if (res_domain_ref->count == 0) {
3052 ret_domain = ldb_search(ldb, mem_ctx,
3054 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
3057 "(objectclass=domain)");
3058 if (ret_domain != LDB_SUCCESS) {
3062 if (res_domain_ref->count == 1) {
3063 return res_domain_ref->msgs[0]->dn;
3068 if (res_domain_ref->count > 1) {
3069 DEBUG(0,("Found %d records matching domain [%s]\n",
3070 ret_domain, domain_name));
3074 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
3080 use a GUID to find a DN
3082 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
3083 TALLOC_CTX *mem_ctx,
3084 const struct GUID *guid,
3085 uint32_t dsdb_flags,
3089 struct ldb_result *res;
3090 const char *attrs[] = { NULL };
3091 char *guid_str = GUID_string(mem_ctx, guid);
3094 return ldb_operr(ldb);
3097 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3098 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3099 DSDB_SEARCH_SHOW_EXTENDED_DN |
3100 DSDB_SEARCH_ONE_ONLY | dsdb_flags,
3101 "objectGUID=%s", guid_str);
3102 talloc_free(guid_str);
3103 if (ret != LDB_SUCCESS) {
3107 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3114 use a DN to find a GUID with a given attribute name
3116 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
3117 struct ldb_dn *dn, const char *attribute,
3121 struct ldb_result *res;
3122 const char *attrs[2];
3123 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3125 attrs[0] = attribute;
3128 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3129 DSDB_SEARCH_SHOW_DELETED |
3130 DSDB_SEARCH_SHOW_RECYCLED);
3131 if (ret != LDB_SUCCESS) {
3132 talloc_free(tmp_ctx);
3135 if (res->count < 1) {
3136 talloc_free(tmp_ctx);
3137 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3139 *guid = samdb_result_guid(res->msgs[0], attribute);
3140 talloc_free(tmp_ctx);
3145 use a DN to find a GUID
3147 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
3148 struct ldb_dn *dn, struct GUID *guid)
3150 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
3156 adds the given GUID to the given ldb_message. This value is added
3157 for the given attr_name (may be either "objectGUID" or "parentGUID").
3159 int dsdb_msg_add_guid(struct ldb_message *msg,
3161 const char *attr_name)
3166 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
3168 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
3169 if (!NT_STATUS_IS_OK(status)) {
3170 ret = LDB_ERR_OPERATIONS_ERROR;
3174 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
3175 if (ret != LDB_SUCCESS) {
3176 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
3184 talloc_free(tmp_ctx);
3191 use a DN to find a SID
3193 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
3194 struct ldb_dn *dn, struct dom_sid *sid)
3197 struct ldb_result *res;
3198 const char *attrs[] = { "objectSid", NULL };
3199 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3204 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3205 DSDB_SEARCH_SHOW_DELETED |
3206 DSDB_SEARCH_SHOW_RECYCLED);
3207 if (ret != LDB_SUCCESS) {
3208 talloc_free(tmp_ctx);
3211 if (res->count < 1) {
3212 talloc_free(tmp_ctx);
3213 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3215 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
3217 talloc_free(tmp_ctx);
3218 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3221 talloc_free(tmp_ctx);
3226 use a SID to find a DN
3228 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
3229 TALLOC_CTX *mem_ctx,
3230 struct dom_sid *sid, struct ldb_dn **dn)
3233 struct ldb_result *res;
3234 const char *attrs[] = { NULL };
3235 char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
3238 return ldb_operr(ldb);
3241 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3242 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3243 DSDB_SEARCH_SHOW_EXTENDED_DN |
3244 DSDB_SEARCH_ONE_ONLY,
3245 "objectSid=%s", sid_str);
3246 talloc_free(sid_str);
3247 if (ret != LDB_SUCCESS) {
3251 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3258 load a repsFromTo blob list for a given partition GUID
3259 attr must be "repsFrom" or "repsTo"
3261 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3262 const char *attr, struct repsFromToBlob **r, uint32_t *count)
3264 const char *attrs[] = { attr, NULL };
3265 struct ldb_result *res = NULL;
3266 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3268 struct ldb_message_element *el;
3274 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
3275 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3276 /* partition hasn't been replicated yet */
3279 if (ret != LDB_SUCCESS) {
3280 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
3281 talloc_free(tmp_ctx);
3282 return WERR_DS_DRA_INTERNAL_ERROR;
3285 el = ldb_msg_find_element(res->msgs[0], attr);
3287 /* it's OK to be empty */
3288 talloc_free(tmp_ctx);
3292 *count = el->num_values;
3293 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
3295 talloc_free(tmp_ctx);
3296 return WERR_DS_DRA_INTERNAL_ERROR;
3299 for (i=0; i<(*count); i++) {
3300 enum ndr_err_code ndr_err;
3301 ndr_err = ndr_pull_struct_blob(&el->values[i],
3304 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
3305 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3306 talloc_free(tmp_ctx);
3307 return WERR_DS_DRA_INTERNAL_ERROR;
3311 talloc_free(tmp_ctx);
3317 save the repsFromTo blob list for a given partition GUID
3318 attr must be "repsFrom" or "repsTo"
3320 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3321 const char *attr, struct repsFromToBlob *r, uint32_t count)
3323 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3324 struct ldb_message *msg;
3325 struct ldb_message_element *el;
3328 msg = ldb_msg_new(tmp_ctx);
3330 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
3334 el->values = talloc_array(msg, struct ldb_val, count);
3339 for (i=0; i<count; i++) {
3341 enum ndr_err_code ndr_err;
3343 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
3345 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
3346 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3354 if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
3355 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
3359 talloc_free(tmp_ctx);
3364 talloc_free(tmp_ctx);
3365 return WERR_DS_DRA_INTERNAL_ERROR;
3370 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
3371 object for a partition
3373 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
3374 uint64_t *uSN, uint64_t *urgent_uSN)
3376 struct ldb_request *req;
3378 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3379 struct dsdb_control_current_partition *p_ctrl;
3380 struct ldb_result *res;
3382 res = talloc_zero(tmp_ctx, struct ldb_result);
3384 talloc_free(tmp_ctx);
3385 return ldb_oom(ldb);
3388 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3389 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
3393 res, ldb_search_default_callback,
3395 if (ret != LDB_SUCCESS) {
3396 talloc_free(tmp_ctx);
3400 p_ctrl = talloc(req, struct dsdb_control_current_partition);
3401 if (p_ctrl == NULL) {
3402 talloc_free(tmp_ctx);
3403 return ldb_oom(ldb);
3405 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
3408 ret = ldb_request_add_control(req,
3409 DSDB_CONTROL_CURRENT_PARTITION_OID,
3411 if (ret != LDB_SUCCESS) {
3412 talloc_free(tmp_ctx);
3416 /* Run the new request */
3417 ret = ldb_request(ldb, req);
3419 if (ret == LDB_SUCCESS) {
3420 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3423 if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
3424 /* it hasn't been created yet, which means
3425 an implicit value of zero */
3427 talloc_free(tmp_ctx);
3431 if (ret != LDB_SUCCESS) {
3432 talloc_free(tmp_ctx);
3436 if (res->count < 1) {
3442 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
3444 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
3448 talloc_free(tmp_ctx);
3453 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
3454 const struct drsuapi_DsReplicaCursor2 *c2)
3456 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3459 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
3460 const struct drsuapi_DsReplicaCursor *c2)
3462 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3467 see if a computer identified by its invocationId is a RODC
3469 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
3471 /* 1) find the DN for this servers NTDSDSA object
3472 2) search for the msDS-isRODC attribute
3473 3) if not present then not a RODC
3474 4) if present and TRUE then is a RODC
3476 struct ldb_dn *config_dn;
3477 const char *attrs[] = { "msDS-isRODC", NULL };
3479 struct ldb_result *res;
3480 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
3482 config_dn = ldb_get_config_basedn(sam_ctx);
3484 talloc_free(tmp_ctx);
3485 return ldb_operr(sam_ctx);
3488 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
3489 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
3491 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3493 talloc_free(tmp_ctx);
3497 if (ret != LDB_SUCCESS) {
3498 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
3499 GUID_string(tmp_ctx, objectGUID)));
3501 talloc_free(tmp_ctx);
3505 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
3506 *is_rodc = (ret == 1);
3508 talloc_free(tmp_ctx);
3514 see if we are a RODC
3516 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
3518 const struct GUID *objectGUID;
3522 /* see if we have a cached copy */
3523 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
3529 objectGUID = samdb_ntds_objectGUID(sam_ctx);
3531 return ldb_operr(sam_ctx);
3534 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
3535 if (ret != LDB_SUCCESS) {
3539 cached = talloc(sam_ctx, bool);
3540 if (cached == NULL) {
3541 return ldb_oom(sam_ctx);
3545 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
3546 if (ret != LDB_SUCCESS) {
3547 talloc_free(cached);
3548 return ldb_operr(sam_ctx);
3554 int samdb_dns_host_name(struct ldb_context *sam_ctx, const char **host_name)
3556 const char *_host_name = NULL;
3557 const char *attrs[] = { "dnsHostName", NULL };
3558 TALLOC_CTX *tmp_ctx = NULL;
3560 struct ldb_result *res = NULL;
3562 _host_name = (const char *)ldb_get_opaque(sam_ctx, "cache.dns_host_name");
3563 if (_host_name != NULL) {
3564 *host_name = _host_name;
3568 tmp_ctx = talloc_new(sam_ctx);
3570 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, NULL, attrs, 0);
3572 if (res->count != 1 || ret != LDB_SUCCESS) {
3573 DEBUG(0, ("Failed to get rootDSE for dnsHostName: %s",
3574 ldb_errstring(sam_ctx)));
3575 TALLOC_FREE(tmp_ctx);
3580 _host_name = ldb_msg_find_attr_as_string(res->msgs[0],
3583 if (_host_name == NULL) {
3584 DEBUG(0, ("Failed to get dnsHostName from rootDSE"));
3585 TALLOC_FREE(tmp_ctx);
3586 return LDB_ERR_OPERATIONS_ERROR;
3588 ret = ldb_set_opaque(sam_ctx, "cache.dns_host_name",
3589 discard_const_p(char *, _host_name));
3590 if (ret != LDB_SUCCESS) {
3591 TALLOC_FREE(tmp_ctx);
3592 return ldb_operr(sam_ctx);
3595 *host_name = talloc_steal(sam_ctx, _host_name);
3597 TALLOC_FREE(tmp_ctx);
3601 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
3603 TALLOC_CTX *tmp_ctx;
3606 tmp_ctx = talloc_new(ldb);
3607 if (tmp_ctx == NULL) {
3611 cached = talloc(tmp_ctx, bool);
3617 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
3621 talloc_steal(ldb, cached);
3622 talloc_free(tmp_ctx);
3626 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
3627 talloc_free(tmp_ctx);
3633 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
3634 * flags are DS_NTDSSETTINGS_OPT_*
3636 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
3640 TALLOC_CTX *tmp_ctx;
3641 struct ldb_result *res;
3642 struct ldb_dn *site_dn;
3643 const char *attrs[] = { "options", NULL };
3645 tmp_ctx = talloc_new(ldb_ctx);
3646 if (tmp_ctx == NULL)
3649 /* Retrieve the site dn for the ldb that we
3650 * have open. This is our local site.
3652 site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
3653 if (site_dn == NULL)
3656 /* Perform a one level (child) search from the local
3657 * site distinguided name. We're looking for the
3658 * "options" attribute within the nTDSSiteSettings
3661 rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
3662 LDB_SCOPE_ONELEVEL, attrs,
3663 "objectClass=nTDSSiteSettings");
3665 if (rc != LDB_SUCCESS || res->count != 1)
3668 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3670 talloc_free(tmp_ctx);
3675 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3676 talloc_free(tmp_ctx);
3677 return ldb_error(ldb_ctx, LDB_ERR_NO_SUCH_OBJECT, __func__);
3681 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3683 flags are DS_NTDS_OPTION_*
3685 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
3687 TALLOC_CTX *tmp_ctx;
3688 const char *attrs[] = { "options", NULL };
3690 struct ldb_result *res;
3692 tmp_ctx = talloc_new(ldb);
3693 if (tmp_ctx == NULL) {
3697 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3698 if (ret != LDB_SUCCESS) {
3702 if (res->count != 1) {
3706 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3708 talloc_free(tmp_ctx);
3713 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3714 talloc_free(tmp_ctx);
3715 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3718 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3720 const char *attrs[] = { "objectCategory", NULL };
3722 struct ldb_result *res;
3724 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3725 if (ret != LDB_SUCCESS) {
3729 if (res->count != 1) {
3733 return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3736 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3741 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3742 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3744 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3746 char **tokens, *ret;
3749 tokens = str_list_make(mem_ctx, cn, " -_");
3750 if (tokens == NULL || tokens[0] == NULL) {
3754 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3755 tokens[0][0] = tolower(tokens[0][0]);
3756 for (i = 1; tokens[i] != NULL; i++)
3757 tokens[i][0] = toupper(tokens[i][0]);
3759 ret = talloc_strdup(mem_ctx, tokens[0]);
3760 for (i = 1; tokens[i] != NULL; i++)
3761 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3763 talloc_free(tokens);
3769 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3771 int dsdb_functional_level(struct ldb_context *ldb)
3773 int *domainFunctionality =
3774 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3775 if (!domainFunctionality) {
3776 /* this is expected during initial provision */
3777 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3778 return DS_DOMAIN_FUNCTION_2000;
3780 return *domainFunctionality;
3784 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3786 int dsdb_forest_functional_level(struct ldb_context *ldb)
3788 int *forestFunctionality =
3789 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3790 if (!forestFunctionality) {
3791 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3792 return DS_DOMAIN_FUNCTION_2000;
3794 return *forestFunctionality;
3798 set a GUID in an extended DN structure
3800 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3806 status = GUID_to_ndr_blob(guid, dn, &v);
3807 if (!NT_STATUS_IS_OK(status)) {
3808 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3811 ret = ldb_dn_set_extended_component(dn, component_name, &v);
3817 return a GUID from a extended DN structure
3819 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3821 const struct ldb_val *v;
3823 v = ldb_dn_get_extended_component(dn, component_name);
3825 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3828 return GUID_from_ndr_blob(v, guid);
3832 return a uint64_t from a extended DN structure
3834 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3836 const struct ldb_val *v;
3838 v = ldb_dn_get_extended_component(dn, component_name);
3840 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3843 /* Just check we don't allow the caller to fill our stack */
3844 if (v->length >= 64) {
3845 return NT_STATUS_INVALID_PARAMETER;
3847 char s[v->length+1];
3848 memcpy(s, v->data, v->length);
3851 *val = strtoull(s, NULL, 0);
3853 return NT_STATUS_OK;
3857 return a NTTIME from a extended DN structure
3859 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3861 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3865 return a uint32_t from a extended DN structure
3867 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3869 const struct ldb_val *v;
3871 v = ldb_dn_get_extended_component(dn, component_name);
3873 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3876 /* Just check we don't allow the caller to fill our stack */
3877 if (v->length >= 32) {
3878 return NT_STATUS_INVALID_PARAMETER;
3880 char s[v->length + 1];
3881 memcpy(s, v->data, v->length);
3883 *val = strtoul(s, NULL, 0);
3886 return NT_STATUS_OK;
3890 return a dom_sid from a extended DN structure
3892 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3894 const struct ldb_val *sid_blob;
3895 enum ndr_err_code ndr_err;
3897 sid_blob = ldb_dn_get_extended_component(dn, component_name);
3899 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3902 ndr_err = ndr_pull_struct_blob_all_noalloc(sid_blob, sid,
3903 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3904 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3905 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3909 return NT_STATUS_OK;
3914 return RMD_FLAGS directly from a ldb_dn
3915 returns 0 if not found
3917 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3919 uint32_t rmd_flags = 0;
3920 NTSTATUS status = dsdb_get_extended_dn_uint32(dn, &rmd_flags,
3922 if (NT_STATUS_IS_OK(status)) {
3929 return RMD_FLAGS directly from a ldb_val for a DN
3930 returns 0 if RMD_FLAGS is not found
3932 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3938 if (val->length < 13) {
3941 p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3945 flags = strtoul(p+11, &end, 10);
3946 if (!end || *end != '>') {
3947 /* it must end in a > */
3954 return true if a ldb_val containing a DN in storage form is deleted
3956 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3958 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3962 return true if a ldb_val containing a DN in storage form is
3963 in the upgraded w2k3 linked attribute format
3965 bool dsdb_dn_is_upgraded_link_val(const struct ldb_val *val)
3967 return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
3971 return a DN for a wellknown GUID
3973 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3974 struct ldb_dn *nc_root, const char *wk_guid,
3975 struct ldb_dn **wkguid_dn)
3977 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3978 const char *attrs[] = { NULL };
3981 struct ldb_result *res;
3983 /* construct the magic WKGUID DN */
3984 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3985 wk_guid, ldb_dn_get_linearized(nc_root));
3987 talloc_free(tmp_ctx);
3988 return ldb_operr(samdb);
3991 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
3992 DSDB_SEARCH_SHOW_DELETED |
3993 DSDB_SEARCH_SHOW_RECYCLED);
3994 if (ret != LDB_SUCCESS) {
3995 talloc_free(tmp_ctx);
3999 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
4000 talloc_free(tmp_ctx);
4005 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
4007 return ldb_dn_compare(*dn1, *dn2);
4011 find a NC root given a DN within the NC
4013 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
4014 struct ldb_dn **nc_root)
4016 const char *root_attrs[] = { "namingContexts", NULL };
4017 TALLOC_CTX *tmp_ctx;
4019 struct ldb_message_element *el;
4020 struct ldb_result *root_res;
4022 struct ldb_dn **nc_dns;
4024 tmp_ctx = talloc_new(samdb);
4025 if (tmp_ctx == NULL) {
4026 return ldb_oom(samdb);
4029 ret = ldb_search(samdb, tmp_ctx, &root_res,
4030 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
4031 if (ret != LDB_SUCCESS || root_res->count == 0) {
4032 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
4033 talloc_free(tmp_ctx);
4037 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
4038 if ((el == NULL) || (el->num_values < 3)) {
4039 struct ldb_message *tmp_msg;
4041 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list.\n"));
4043 /* This generates a temporary list of NCs in order to let the
4044 * provisioning work. */
4045 tmp_msg = ldb_msg_new(tmp_ctx);
4046 if (tmp_msg == NULL) {
4047 talloc_free(tmp_ctx);
4048 return ldb_oom(samdb);
4050 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
4051 ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
4052 if (ret != LDB_SUCCESS) {
4053 talloc_free(tmp_ctx);
4056 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
4057 ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
4058 if (ret != LDB_SUCCESS) {
4059 talloc_free(tmp_ctx);
4062 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
4063 ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
4064 if (ret != LDB_SUCCESS) {
4065 talloc_free(tmp_ctx);
4068 el = &tmp_msg->elements[0];
4071 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
4073 talloc_free(tmp_ctx);
4074 return ldb_oom(samdb);
4077 for (i=0; i<el->num_values; i++) {
4078 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
4079 if (nc_dns[i] == NULL) {
4080 talloc_free(tmp_ctx);
4081 return ldb_operr(samdb);
4085 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
4087 for (i=0; i<el->num_values; i++) {
4088 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
4089 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
4090 talloc_free(tmp_ctx);
4095 talloc_free(tmp_ctx);
4096 return ldb_error(samdb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4101 find the deleted objects DN for any object, by looking for the NC
4102 root, then looking up the wellknown GUID
4104 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
4105 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
4106 struct ldb_dn **do_dn)
4108 struct ldb_dn *nc_root;
4111 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
4112 if (ret != LDB_SUCCESS) {
4116 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
4117 talloc_free(nc_root);
4122 return the tombstoneLifetime, in days
4124 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
4127 dn = ldb_get_config_basedn(ldb);
4129 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4131 dn = ldb_dn_copy(ldb, dn);
4133 return ldb_operr(ldb);
4135 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
4136 be a wellknown GUID for this */
4137 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
4139 return ldb_operr(ldb);
4142 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
4148 compare a ldb_val to a string case insensitively
4150 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
4152 size_t len = strlen(s);
4154 if (len > v->length) return 1;
4155 ret = strncasecmp(s, (const char *)v->data, v->length);
4156 if (ret != 0) return ret;
4157 if (v->length > len && v->data[len] != 0) {
4165 load the UDV for a partition in v2 format
4166 The list is returned sorted, and with our local cursor added
4168 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4169 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
4171 static const char *attrs[] = { "replUpToDateVector", NULL };
4172 struct ldb_result *r;
4173 const struct ldb_val *ouv_value;
4176 uint64_t highest_usn = 0;
4177 const struct GUID *our_invocation_id;
4178 static const struct timeval tv1970;
4179 NTTIME nt1970 = timeval_to_nttime(&tv1970);
4181 ret = dsdb_search_dn(samdb, mem_ctx, &r, dn, attrs, DSDB_SEARCH_SHOW_RECYCLED|DSDB_SEARCH_SHOW_DELETED);
4182 if (ret != LDB_SUCCESS) {
4186 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
4188 enum ndr_err_code ndr_err;
4189 struct replUpToDateVectorBlob ouv;
4191 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
4192 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
4193 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
4195 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4197 if (ouv.version != 2) {
4198 /* we always store as version 2, and
4199 * replUpToDateVector is not replicated
4201 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4204 *count = ouv.ctr.ctr2.count;
4205 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
4213 our_invocation_id = samdb_ntds_invocation_id(samdb);
4214 if (!our_invocation_id) {
4215 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
4216 talloc_free(*cursors);
4217 return ldb_operr(samdb);
4220 ret = ldb_sequence_number(samdb, LDB_SEQ_HIGHEST_SEQ, &highest_usn);
4221 if (ret != LDB_SUCCESS) {
4222 /* nothing to add - this can happen after a vampire */
4223 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4227 for (i=0; i<*count; i++) {
4228 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
4229 (*cursors)[i].highest_usn = highest_usn;
4230 (*cursors)[i].last_sync_success = nt1970;
4231 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4236 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
4238 return ldb_oom(samdb);
4241 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
4242 (*cursors)[*count].highest_usn = highest_usn;
4243 (*cursors)[*count].last_sync_success = nt1970;
4246 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4252 load the UDV for a partition in version 1 format
4253 The list is returned sorted, and with our local cursor added
4255 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4256 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
4258 struct drsuapi_DsReplicaCursor2 *v2;
4262 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
4263 if (ret != LDB_SUCCESS) {
4273 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
4274 if (*cursors == NULL) {
4276 return ldb_oom(samdb);
4279 for (i=0; i<*count; i++) {
4280 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
4281 (*cursors)[i].highest_usn = v2[i].highest_usn;
4288 add a set of controls to a ldb_request structure based on a set of
4289 flags. See util.h for a list of available flags
4291 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
4294 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
4295 struct ldb_search_options_control *options;
4296 /* Using the phantom root control allows us to search all partitions */
4297 options = talloc(req, struct ldb_search_options_control);
4298 if (options == NULL) {
4299 return LDB_ERR_OPERATIONS_ERROR;
4301 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
4303 ret = ldb_request_add_control(req,
4304 LDB_CONTROL_SEARCH_OPTIONS_OID,
4306 if (ret != LDB_SUCCESS) {
4311 if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
4312 ret = ldb_request_add_control(req,
4313 DSDB_CONTROL_NO_GLOBAL_CATALOG,
4315 if (ret != LDB_SUCCESS) {
4320 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
4321 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
4322 if (ret != LDB_SUCCESS) {
4327 if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
4328 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
4329 if (ret != LDB_SUCCESS) {
4334 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
4335 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
4336 if (ret != LDB_SUCCESS) {
4341 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
4342 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
4343 if (!extended_ctrl) {
4344 return LDB_ERR_OPERATIONS_ERROR;
4346 extended_ctrl->type = 1;
4348 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
4349 if (ret != LDB_SUCCESS) {
4354 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
4355 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
4356 if (ret != LDB_SUCCESS) {
4361 if (dsdb_flags & DSDB_MODIFY_RELAX) {
4362 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
4363 if (ret != LDB_SUCCESS) {
4368 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
4369 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
4370 if (ret != LDB_SUCCESS) {
4375 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
4376 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
4377 if (ret != LDB_SUCCESS) {
4382 if (dsdb_flags & DSDB_TREE_DELETE) {
4383 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
4384 if (ret != LDB_SUCCESS) {
4389 if (dsdb_flags & DSDB_PROVISION) {
4390 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
4391 if (ret != LDB_SUCCESS) {
4396 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
4397 if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
4398 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
4399 if (ret != LDB_SUCCESS) {
4404 if (dsdb_flags & DSDB_PASSWORD_BYPASS_LAST_SET) {
4406 * This must not be critical, as it will only be
4407 * handled (and need to be handled) if the other
4408 * attributes in the request bring password_hash into
4411 ret = ldb_request_add_control(req, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID, false, NULL);
4412 if (ret != LDB_SUCCESS) {
4417 if (dsdb_flags & DSDB_REPLMD_VANISH_LINKS) {
4418 ret = ldb_request_add_control(req, DSDB_CONTROL_REPLMD_VANISH_LINKS, true, NULL);
4419 if (ret != LDB_SUCCESS) {
4424 if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
4425 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
4426 if (ret != LDB_SUCCESS) {
4431 if (dsdb_flags & DSDB_FLAG_REPLICATED_UPDATE) {
4432 ret = ldb_request_add_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID, false, NULL);
4433 if (ret != LDB_SUCCESS) {
4442 returns true if a control with the specified "oid" exists
4444 bool dsdb_request_has_control(struct ldb_request *req, const char *oid)
4446 return (ldb_request_get_control(req, oid) != NULL);
4450 an add with a set of controls
4452 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
4453 uint32_t dsdb_flags)
4455 struct ldb_request *req;
4458 ret = ldb_build_add_req(&req, ldb, ldb,
4462 ldb_op_default_callback,
4465 if (ret != LDB_SUCCESS) return ret;
4467 ret = dsdb_request_add_controls(req, dsdb_flags);
4468 if (ret != LDB_SUCCESS) {
4473 ret = dsdb_autotransaction_request(ldb, req);
4480 a modify with a set of controls
4482 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
4483 uint32_t dsdb_flags)
4485 struct ldb_request *req;
4488 ret = ldb_build_mod_req(&req, ldb, ldb,
4492 ldb_op_default_callback,
4495 if (ret != LDB_SUCCESS) return ret;
4497 ret = dsdb_request_add_controls(req, dsdb_flags);
4498 if (ret != LDB_SUCCESS) {
4503 ret = dsdb_autotransaction_request(ldb, req);
4510 a delete with a set of flags
4512 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
4513 uint32_t dsdb_flags)
4515 struct ldb_request *req;
4518 ret = ldb_build_del_req(&req, ldb, ldb,
4522 ldb_op_default_callback,
4525 if (ret != LDB_SUCCESS) return ret;
4527 ret = dsdb_request_add_controls(req, dsdb_flags);
4528 if (ret != LDB_SUCCESS) {
4533 ret = dsdb_autotransaction_request(ldb, req);
4540 like dsdb_modify() but set all the element flags to
4541 LDB_FLAG_MOD_REPLACE
4543 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
4547 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
4548 for (i=0;i<msg->num_elements;i++) {
4549 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
4552 return dsdb_modify(ldb, msg, dsdb_flags);
4557 search for attrs on one DN, allowing for dsdb_flags controls
4559 int dsdb_search_dn(struct ldb_context *ldb,
4560 TALLOC_CTX *mem_ctx,
4561 struct ldb_result **_result,
4562 struct ldb_dn *basedn,
4563 const char * const *attrs,
4564 uint32_t dsdb_flags)
4567 struct ldb_request *req;
4568 struct ldb_result *res;
4570 res = talloc_zero(mem_ctx, struct ldb_result);
4572 return ldb_oom(ldb);
4575 ret = ldb_build_search_req(&req, ldb, res,
4582 ldb_search_default_callback,
4584 if (ret != LDB_SUCCESS) {
4589 ret = dsdb_request_add_controls(req, dsdb_flags);
4590 if (ret != LDB_SUCCESS) {
4595 ret = ldb_request(ldb, req);
4596 if (ret == LDB_SUCCESS) {
4597 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4601 if (ret != LDB_SUCCESS) {
4611 search for attrs on one DN, by the GUID of the DN, allowing for
4614 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
4615 TALLOC_CTX *mem_ctx,
4616 struct ldb_result **_result,
4617 const struct GUID *guid,
4618 const char * const *attrs,
4619 uint32_t dsdb_flags)
4621 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4625 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
4627 talloc_free(tmp_ctx);
4628 return ldb_oom(ldb);
4631 ret = dsdb_search_dn(ldb, mem_ctx, _result, dn, attrs, dsdb_flags);
4632 talloc_free(tmp_ctx);
4637 general search with dsdb_flags for controls
4639 int dsdb_search(struct ldb_context *ldb,
4640 TALLOC_CTX *mem_ctx,
4641 struct ldb_result **_result,
4642 struct ldb_dn *basedn,
4643 enum ldb_scope scope,
4644 const char * const *attrs,
4645 uint32_t dsdb_flags,
4646 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4649 struct ldb_request *req;
4650 struct ldb_result *res;
4652 char *expression = NULL;
4653 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4655 /* cross-partitions searches with a basedn break multi-domain support */
4656 SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
4658 res = talloc_zero(tmp_ctx, struct ldb_result);
4660 talloc_free(tmp_ctx);
4661 return ldb_oom(ldb);
4665 va_start(ap, exp_fmt);
4666 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4670 talloc_free(tmp_ctx);
4671 return ldb_oom(ldb);
4675 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
4682 ldb_search_default_callback,
4684 if (ret != LDB_SUCCESS) {
4685 talloc_free(tmp_ctx);
4689 ret = dsdb_request_add_controls(req, dsdb_flags);
4690 if (ret != LDB_SUCCESS) {
4691 talloc_free(tmp_ctx);
4692 ldb_reset_err_string(ldb);
4696 ret = ldb_request(ldb, req);
4697 if (ret == LDB_SUCCESS) {
4698 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4701 if (ret != LDB_SUCCESS) {
4702 talloc_free(tmp_ctx);
4706 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
4707 if (res->count == 0) {
4708 talloc_free(tmp_ctx);
4709 ldb_reset_err_string(ldb);
4710 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4712 if (res->count != 1) {
4713 talloc_free(tmp_ctx);
4714 ldb_reset_err_string(ldb);
4715 return LDB_ERR_CONSTRAINT_VIOLATION;
4719 *_result = talloc_steal(mem_ctx, res);
4720 talloc_free(tmp_ctx);
4727 general search with dsdb_flags for controls
4728 returns exactly 1 record or an error
4730 int dsdb_search_one(struct ldb_context *ldb,
4731 TALLOC_CTX *mem_ctx,
4732 struct ldb_message **msg,
4733 struct ldb_dn *basedn,
4734 enum ldb_scope scope,
4735 const char * const *attrs,
4736 uint32_t dsdb_flags,
4737 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4740 struct ldb_result *res;
4742 char *expression = NULL;
4743 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4745 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4747 res = talloc_zero(tmp_ctx, struct ldb_result);
4749 talloc_free(tmp_ctx);
4750 return ldb_oom(ldb);
4754 va_start(ap, exp_fmt);
4755 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4759 talloc_free(tmp_ctx);
4760 return ldb_oom(ldb);
4762 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4763 dsdb_flags, "%s", expression);
4765 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4769 if (ret != LDB_SUCCESS) {
4770 talloc_free(tmp_ctx);
4774 *msg = talloc_steal(mem_ctx, res->msgs[0]);
4775 talloc_free(tmp_ctx);
4780 /* returns back the forest DNS name */
4781 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4783 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4784 ldb_get_root_basedn(ldb));
4787 if (forest_name == NULL) {
4791 p = strchr(forest_name, '/');
4799 /* returns back the default domain DNS name */
4800 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4802 const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4803 ldb_get_default_basedn(ldb));
4806 if (domain_name == NULL) {
4810 p = strchr(domain_name, '/');
4819 validate that an DSA GUID belongs to the specified user sid.
4820 The user SID must be a domain controller account (either RODC or
4823 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4824 const struct GUID *dsa_guid,
4825 const struct dom_sid *sid)
4828 - find DN of record with the DSA GUID in the
4829 configuration partition (objectGUID)
4830 - remove "NTDS Settings" component from DN
4831 - do a base search on that DN for serverReference with
4833 - extract objectSid from resulting serverReference
4835 - check this sid matches the sid argument
4837 struct ldb_dn *config_dn;
4838 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4839 struct ldb_message *msg;
4840 const char *attrs1[] = { NULL };
4841 const char *attrs2[] = { "serverReference", NULL };
4843 struct ldb_dn *dn, *account_dn;
4844 struct dom_sid sid2;
4847 config_dn = ldb_get_config_basedn(ldb);
4849 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4850 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4851 if (ret != LDB_SUCCESS) {
4852 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4853 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4854 talloc_free(tmp_ctx);
4855 return ldb_operr(ldb);
4859 if (!ldb_dn_remove_child_components(dn, 1)) {
4860 talloc_free(tmp_ctx);
4861 return ldb_operr(ldb);
4864 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4865 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4866 "(objectClass=server)");
4867 if (ret != LDB_SUCCESS) {
4868 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4869 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4870 talloc_free(tmp_ctx);
4871 return ldb_operr(ldb);
4874 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4875 if (account_dn == NULL) {
4876 DEBUG(1,(__location__ ": Failed to find account dn "
4877 "(serverReference) for %s, parent of DSA with "
4878 "objectGUID %s, sid %s\n",
4879 ldb_dn_get_linearized(msg->dn),
4880 GUID_string(tmp_ctx, dsa_guid),
4881 dom_sid_string(tmp_ctx, sid)));
4882 talloc_free(tmp_ctx);
4883 return ldb_operr(ldb);
4886 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4887 if (!NT_STATUS_IS_OK(status)) {
4888 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4889 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4890 talloc_free(tmp_ctx);
4891 return ldb_operr(ldb);
4894 if (!dom_sid_equal(sid, &sid2)) {
4895 /* someone is trying to spoof another account */
4896 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4897 GUID_string(tmp_ctx, dsa_guid),
4898 dom_sid_string(tmp_ctx, sid),
4899 dom_sid_string(tmp_ctx, &sid2)));
4900 talloc_free(tmp_ctx);
4901 return ldb_operr(ldb);
4904 talloc_free(tmp_ctx);
4908 static const char * const secret_attributes[] = {
4909 DSDB_SECRET_ATTRIBUTES,
4914 check if the attribute belongs to the RODC filtered attribute set
4915 Note that attributes that are in the filtered attribute set are the
4916 ones that _are_ always sent to a RODC
4918 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4920 /* they never get secret attributes */
4921 if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4925 /* they do get non-secret critical attributes */
4926 if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4930 /* they do get non-secret attributes marked as being in the FAS */
4931 if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4935 /* other attributes are denied */
4939 /* return fsmo role dn and role owner dn for a particular role*/
4940 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4941 struct ldb_context *ldb,
4943 struct ldb_dn **fsmo_role_dn,
4944 struct ldb_dn **role_owner_dn)
4948 case DREPL_NAMING_MASTER:
4949 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
4950 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4951 if (ret != LDB_SUCCESS) {
4952 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
4953 ldb_errstring(ldb)));
4954 talloc_free(tmp_ctx);
4955 return WERR_DS_DRA_INTERNAL_ERROR;
4958 case DREPL_INFRASTRUCTURE_MASTER:
4959 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
4960 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4961 if (ret != LDB_SUCCESS) {
4962 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4963 ldb_errstring(ldb)));
4964 talloc_free(tmp_ctx);
4965 return WERR_DS_DRA_INTERNAL_ERROR;
4968 case DREPL_RID_MASTER:
4969 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
4970 if (ret != LDB_SUCCESS) {
4971 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
4972 talloc_free(tmp_ctx);
4973 return WERR_DS_DRA_INTERNAL_ERROR;
4976 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4977 if (ret != LDB_SUCCESS) {
4978 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
4979 ldb_errstring(ldb)));
4980 talloc_free(tmp_ctx);
4981 return WERR_DS_DRA_INTERNAL_ERROR;
4984 case DREPL_SCHEMA_MASTER:
4985 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
4986 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4987 if (ret != LDB_SUCCESS) {
4988 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4989 ldb_errstring(ldb)));
4990 talloc_free(tmp_ctx);
4991 return WERR_DS_DRA_INTERNAL_ERROR;
4994 case DREPL_PDC_MASTER:
4995 *fsmo_role_dn = ldb_get_default_basedn(ldb);
4996 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4997 if (ret != LDB_SUCCESS) {
4998 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
4999 ldb_errstring(ldb)));
5000 talloc_free(tmp_ctx);
5001 return WERR_DS_DRA_INTERNAL_ERROR;
5005 return WERR_DS_DRA_INTERNAL_ERROR;
5010 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
5011 TALLOC_CTX *mem_ctx,
5012 struct ldb_dn *server_dn)
5015 struct ldb_result *res = NULL;
5016 const char * const attrs[] = { "dNSHostName", NULL};
5018 ldb_ret = ldb_search(ldb, mem_ctx, &res,
5022 if (ldb_ret != LDB_SUCCESS) {
5023 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
5024 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
5028 return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
5032 returns true if an attribute is in the filter,
5033 false otherwise, provided that attribute value is provided with the expression
5035 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
5039 switch (tree->operation) {
5042 for (i=0;i<tree->u.list.num_elements;i++) {
5043 if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
5049 return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
5050 case LDB_OP_EQUALITY:
5051 case LDB_OP_GREATER:
5054 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
5058 case LDB_OP_SUBSTRING:
5059 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
5063 case LDB_OP_PRESENT:
5064 /* (attrname=*) is not filtered out */
5066 case LDB_OP_EXTENDED:
5067 if (tree->u.extended.attr &&
5068 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
5076 bool is_attr_in_list(const char * const * attrs, const char *attr)
5080 for (i = 0; attrs[i]; i++) {
5081 if (ldb_attr_cmp(attrs[i], attr) == 0)
5088 int dsdb_werror_at(struct ldb_context *ldb, int ldb_ecode, WERROR werr,
5089 const char *location, const char *func,
5092 if (reason == NULL) {
5093 reason = win_errstr(werr);
5095 ldb_asprintf_errstring(ldb, "%08X: %s at %s:%s",
5096 W_ERROR_V(werr), reason, location, func);
5101 map an ldb error code to an approximate NTSTATUS code
5103 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
5107 return NT_STATUS_OK;
5109 case LDB_ERR_PROTOCOL_ERROR:
5110 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
5112 case LDB_ERR_TIME_LIMIT_EXCEEDED:
5113 return NT_STATUS_IO_TIMEOUT;
5115 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
5116 return NT_STATUS_BUFFER_TOO_SMALL;
5118 case LDB_ERR_COMPARE_FALSE:
5119 case LDB_ERR_COMPARE_TRUE:
5120 return NT_STATUS_REVISION_MISMATCH;
5122 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
5123 return NT_STATUS_NOT_SUPPORTED;
5125 case LDB_ERR_STRONG_AUTH_REQUIRED:
5126 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
5127 case LDB_ERR_SASL_BIND_IN_PROGRESS:
5128 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
5129 case LDB_ERR_INVALID_CREDENTIALS:
5130 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
5131 case LDB_ERR_UNWILLING_TO_PERFORM:
5132 return NT_STATUS_ACCESS_DENIED;
5134 case LDB_ERR_NO_SUCH_OBJECT:
5135 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5137 case LDB_ERR_REFERRAL:
5138 case LDB_ERR_NO_SUCH_ATTRIBUTE:
5139 return NT_STATUS_NOT_FOUND;
5141 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
5142 return NT_STATUS_NOT_SUPPORTED;
5144 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
5145 return NT_STATUS_BUFFER_TOO_SMALL;
5147 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
5148 case LDB_ERR_INAPPROPRIATE_MATCHING:
5149 case LDB_ERR_CONSTRAINT_VIOLATION:
5150 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
5151 case LDB_ERR_INVALID_DN_SYNTAX:
5152 case LDB_ERR_NAMING_VIOLATION:
5153 case LDB_ERR_OBJECT_CLASS_VIOLATION:
5154 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
5155 case LDB_ERR_NOT_ALLOWED_ON_RDN:
5156 return NT_STATUS_INVALID_PARAMETER;
5158 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
5159 case LDB_ERR_ENTRY_ALREADY_EXISTS:
5160 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
5163 return NT_STATUS_NETWORK_BUSY;
5165 case LDB_ERR_ALIAS_PROBLEM:
5166 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
5167 case LDB_ERR_UNAVAILABLE:
5168 case LDB_ERR_LOOP_DETECT:
5169 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
5170 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
5172 case LDB_ERR_OPERATIONS_ERROR:
5175 return NT_STATUS_UNSUCCESSFUL;
5180 create a new naming context that will hold a partial replica
5182 int dsdb_create_partial_replica_NC(struct ldb_context *ldb, struct ldb_dn *dn)
5184 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
5185 struct ldb_message *msg;
5188 msg = ldb_msg_new(tmp_ctx);
5190 talloc_free(tmp_ctx);
5191 return ldb_oom(ldb);
5195 ret = ldb_msg_add_string(msg, "objectClass", "top");
5196 if (ret != LDB_SUCCESS) {
5197 talloc_free(tmp_ctx);
5198 return ldb_oom(ldb);
5201 /* [MS-DRSR] implies that we should only add the 'top'
5202 * objectclass, but that would cause lots of problems with our
5203 * objectclass code as top is not structural, so we add
5204 * 'domainDNS' as well to keep things sane. We're expecting
5205 * this new NC to be of objectclass domainDNS after
5206 * replication anyway
5208 ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
5209 if (ret != LDB_SUCCESS) {
5210 talloc_free(tmp_ctx);
5211 return ldb_oom(ldb);
5214 ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
5215 INSTANCE_TYPE_IS_NC_HEAD|
5216 INSTANCE_TYPE_NC_ABOVE|
5217 INSTANCE_TYPE_UNINSTANT);
5218 if (ret != LDB_SUCCESS) {
5219 talloc_free(tmp_ctx);
5220 return ldb_oom(ldb);
5223 ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
5224 if (ret != LDB_SUCCESS && ret != LDB_ERR_ENTRY_ALREADY_EXISTS) {
5225 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
5226 ldb_dn_get_linearized(dn),
5227 ldb_errstring(ldb), ldb_strerror(ret)));
5228 talloc_free(tmp_ctx);
5232 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
5234 talloc_free(tmp_ctx);
5239 build a GUID from a string
5241 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
5243 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
5245 uint32_t time_mid, time_hi_and_version;
5246 uint32_t clock_seq[2];
5251 return NT_STATUS_INVALID_PARAMETER;
5254 status = parse_guid_string(s,
5257 &time_hi_and_version,
5261 if (!NT_STATUS_IS_OK(status)) {
5265 guid->time_low = time_low;
5266 guid->time_mid = time_mid;
5267 guid->time_hi_and_version = time_hi_and_version;
5268 guid->clock_seq[0] = clock_seq[0];
5269 guid->clock_seq[1] = clock_seq[1];
5271 guid->node[i] = node[i];
5274 return NT_STATUS_OK;
5277 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
5279 return talloc_asprintf(mem_ctx,
5280 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
5281 guid->time_low, guid->time_mid,
5282 guid->time_hi_and_version,
5285 guid->node[0], guid->node[1],
5286 guid->node[2], guid->node[3],
5287 guid->node[4], guid->node[5]);
5291 * Return the effective badPwdCount
5293 * This requires that the user_msg have (if present):
5297 * This also requires that the domain_msg have (if present):
5298 * - lockOutObservationWindow
5300 static int dsdb_effective_badPwdCount(const struct ldb_message *user_msg,
5301 int64_t lockOutObservationWindow,
5304 int64_t badPasswordTime;
5305 badPasswordTime = ldb_msg_find_attr_as_int64(user_msg, "badPasswordTime", 0);
5307 if (badPasswordTime - lockOutObservationWindow >= now) {
5308 return ldb_msg_find_attr_as_int(user_msg, "badPwdCount", 0);
5315 * Returns a user's PSO, or NULL if none was found
5317 static struct ldb_result *lookup_user_pso(struct ldb_context *sam_ldb,
5318 TALLOC_CTX *mem_ctx,
5319 const struct ldb_message *user_msg,
5320 const char * const *attrs)
5322 struct ldb_result *res = NULL;
5323 struct ldb_dn *pso_dn = NULL;
5326 /* if the user has a PSO that applies, then use the PSO's setting */
5327 pso_dn = ldb_msg_find_attr_as_dn(sam_ldb, mem_ctx, user_msg,
5328 "msDS-ResultantPSO");
5330 if (pso_dn != NULL) {
5332 ret = dsdb_search_dn(sam_ldb, mem_ctx, &res, pso_dn, attrs, 0);
5333 if (ret != LDB_SUCCESS) {
5336 * log the error. The caller should fallback to using
5337 * the default domain password settings
5339 DBG_ERR("Error retrieving msDS-ResultantPSO %s for %s",
5340 ldb_dn_get_linearized(pso_dn),
5341 ldb_dn_get_linearized(user_msg->dn));
5343 talloc_free(pso_dn);
5349 * Return the effective badPwdCount
5351 * This requires that the user_msg have (if present):
5354 * - msDS-ResultantPSO
5356 int samdb_result_effective_badPwdCount(struct ldb_context *sam_ldb,
5357 TALLOC_CTX *mem_ctx,
5358 struct ldb_dn *domain_dn,
5359 const struct ldb_message *user_msg)
5361 struct timeval tv_now = timeval_current();
5362 NTTIME now = timeval_to_nttime(&tv_now);
5363 int64_t lockOutObservationWindow;
5364 struct ldb_result *res = NULL;
5365 const char *attrs[] = { "msDS-LockoutObservationWindow",
5368 res = lookup_user_pso(sam_ldb, mem_ctx, user_msg, attrs);
5371 lockOutObservationWindow =
5372 ldb_msg_find_attr_as_int(res->msgs[0],
5373 "msDS-LockoutObservationWindow",
5378 /* no PSO was found, lookup the default domain setting */
5379 lockOutObservationWindow =
5380 samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn,
5381 "lockOutObservationWindow", NULL);
5384 return dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5388 * Returns the lockoutThreshold that applies. If a PSO is specified, then that
5389 * setting is used over the domain defaults
5391 static int64_t get_lockout_threshold(struct ldb_message *domain_msg,
5392 struct ldb_message *pso_msg)
5394 if (pso_msg != NULL) {
5395 return ldb_msg_find_attr_as_int(pso_msg,
5396 "msDS-LockoutThreshold", 0);
5398 return ldb_msg_find_attr_as_int(domain_msg,
5399 "lockoutThreshold", 0);
5404 * Returns the lockOutObservationWindow that applies. If a PSO is specified,
5405 * then that setting is used over the domain defaults
5407 static int64_t get_lockout_observation_window(struct ldb_message *domain_msg,
5408 struct ldb_message *pso_msg)
5410 if (pso_msg != NULL) {
5411 return ldb_msg_find_attr_as_int(pso_msg,
5412 "msDS-LockoutObservationWindow",
5415 return ldb_msg_find_attr_as_int(domain_msg,
5416 "lockOutObservationWindow", 0);
5421 * Prepare an update to the badPwdCount and associated attributes.
5423 * This requires that the user_msg have (if present):
5428 * This also requires that the domain_msg have (if present):
5430 * - lockoutThreshold
5431 * - lockOutObservationWindow
5433 * This also requires that the pso_msg have (if present):
5434 * - msDS-LockoutThreshold
5435 * - msDS-LockoutObservationWindow
5437 NTSTATUS dsdb_update_bad_pwd_count(TALLOC_CTX *mem_ctx,
5438 struct ldb_context *sam_ctx,
5439 struct ldb_message *user_msg,
5440 struct ldb_message *domain_msg,
5441 struct ldb_message *pso_msg,
5442 struct ldb_message **_mod_msg)
5444 int i, ret, badPwdCount;
5445 int64_t lockoutThreshold, lockOutObservationWindow;
5446 struct dom_sid *sid;
5447 struct timeval tv_now = timeval_current();
5448 NTTIME now = timeval_to_nttime(&tv_now);
5450 uint32_t pwdProperties, rid = 0;
5451 struct ldb_message *mod_msg;
5453 sid = samdb_result_dom_sid(mem_ctx, user_msg, "objectSid");
5455 pwdProperties = ldb_msg_find_attr_as_uint(domain_msg,
5456 "pwdProperties", -1);
5457 if (sid && !(pwdProperties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
5458 status = dom_sid_split_rid(NULL, sid, NULL, &rid);
5459 if (!NT_STATUS_IS_OK(status)) {
5461 * This can't happen anyway, but always try
5462 * and update the badPwdCount on failure
5470 * Work out if we are doing password lockout on the domain.
5471 * Also, the built in administrator account is exempt:
5472 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa375371%28v=vs.85%29.aspx
5474 lockoutThreshold = get_lockout_threshold(domain_msg, pso_msg);
5475 if (lockoutThreshold == 0 || (rid == DOMAIN_RID_ADMINISTRATOR)) {
5476 DEBUG(5, ("Not updating badPwdCount on %s after wrong password\n",
5477 ldb_dn_get_linearized(user_msg->dn)));
5478 return NT_STATUS_OK;
5481 mod_msg = ldb_msg_new(mem_ctx);
5482 if (mod_msg == NULL) {
5483 return NT_STATUS_NO_MEMORY;
5485 mod_msg->dn = ldb_dn_copy(mod_msg, user_msg->dn);
5486 if (mod_msg->dn == NULL) {
5487 TALLOC_FREE(mod_msg);
5488 return NT_STATUS_NO_MEMORY;
5491 lockOutObservationWindow = get_lockout_observation_window(domain_msg,
5494 badPwdCount = dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5498 ret = samdb_msg_add_int(sam_ctx, mod_msg, mod_msg, "badPwdCount", badPwdCount);
5499 if (ret != LDB_SUCCESS) {
5500 TALLOC_FREE(mod_msg);
5501 return NT_STATUS_NO_MEMORY;
5503 ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "badPasswordTime", now);
5504 if (ret != LDB_SUCCESS) {
5505 TALLOC_FREE(mod_msg);
5506 return NT_STATUS_NO_MEMORY;
5509 if (badPwdCount >= lockoutThreshold) {
5510 ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "lockoutTime", now);
5511 if (ret != LDB_SUCCESS) {
5512 TALLOC_FREE(mod_msg);
5513 return NT_STATUS_NO_MEMORY;
5515 DEBUGC( DBGC_AUTH, 1, ("Locked out user %s after %d wrong passwords\n",
5516 ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5518 DEBUGC( DBGC_AUTH, 5, ("Updated badPwdCount on %s after %d wrong passwords\n",
5519 ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5522 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
5523 for (i=0; i< mod_msg->num_elements; i++) {
5524 mod_msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
5527 *_mod_msg = mod_msg;
5528 return NT_STATUS_OK;
5532 * Sets defaults for a User object
5533 * List of default attributes set:
5534 * accountExpires, badPasswordTime, badPwdCount,
5535 * codePage, countryCode, lastLogoff, lastLogon
5536 * logonCount, pwdLastSet
5538 int dsdb_user_obj_set_defaults(struct ldb_context *ldb,
5539 struct ldb_message *usr_obj,
5540 struct ldb_request *req)
5543 const struct attribute_values {
5546 const char *add_value;
5547 const char *mod_value;
5548 const char *control;
5553 .name = "accountExpires",
5554 .add_value = "9223372036854775807",
5558 .name = "badPasswordTime",
5562 .name = "badPwdCount",
5570 .name = "countryCode",
5574 .name = "lastLogoff",
5578 .name = "lastLogon",
5582 .name = "logonCount",
5586 .name = "logonHours",
5587 .add_flags = DSDB_FLAG_INTERNAL_FORCE_META_DATA,
5590 .name = "pwdLastSet",
5592 .control = DSDB_CONTROL_PASSWORD_DEFAULT_LAST_SET_OID,
5595 .name = "adminCount",
5599 .name = "operatorCount",
5604 for (i = 0; i < ARRAY_SIZE(map); i++) {
5606 const char *value = NULL;
5609 if (req != NULL && req->operation == LDB_ADD) {
5610 value = map[i].add_value;
5611 flags = map[i].add_flags;
5613 value = map[i].mod_value;
5614 flags = map[i].mod_flags;
5617 if (value == NULL) {
5618 value = map[i].value;
5621 if (value != NULL) {
5622 flags |= LDB_FLAG_MOD_ADD;
5629 ret = samdb_find_or_add_attribute_ex(ldb, usr_obj,
5633 if (ret != LDB_SUCCESS) {
5637 if (req != NULL && added && map[i].control != NULL) {
5638 ret = ldb_request_add_control(req,
5641 if (ret != LDB_SUCCESS) {
5651 * Sets 'sAMAccountType on user object based on userAccountControl
5652 * @param ldb Current ldb_context
5653 * @param usr_obj ldb_message representing User object
5654 * @param user_account_control Value for userAccountControl flags
5655 * @param account_type_p Optional pointer to account_type to return
5656 * @return LDB_SUCCESS or LDB_ERR* code on failure
5658 int dsdb_user_obj_set_account_type(struct ldb_context *ldb, struct ldb_message *usr_obj,
5659 uint32_t user_account_control, uint32_t *account_type_p)
5662 uint32_t account_type;
5663 struct ldb_message_element *el;
5665 account_type = ds_uf2atype(user_account_control);
5666 if (account_type == 0) {
5667 ldb_set_errstring(ldb, "dsdb: Unrecognized account type!");
5668 return LDB_ERR_UNWILLING_TO_PERFORM;
5670 ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5673 if (ret != LDB_SUCCESS) {
5676 el = ldb_msg_find_element(usr_obj, "sAMAccountType");
5677 el->flags = LDB_FLAG_MOD_REPLACE;
5679 if (account_type_p) {
5680 *account_type_p = account_type;
5687 * Determine and set primaryGroupID based on userAccountControl value
5688 * @param ldb Current ldb_context
5689 * @param usr_obj ldb_message representing User object
5690 * @param user_account_control Value for userAccountControl flags
5691 * @param group_rid_p Optional pointer to group RID to return
5692 * @return LDB_SUCCESS or LDB_ERR* code on failure
5694 int dsdb_user_obj_set_primary_group_id(struct ldb_context *ldb, struct ldb_message *usr_obj,
5695 uint32_t user_account_control, uint32_t *group_rid_p)
5699 struct ldb_message_element *el;
5701 rid = ds_uf2prim_group_rid(user_account_control);
5703 ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5704 "primaryGroupID", rid);
5705 if (ret != LDB_SUCCESS) {
5708 el = ldb_msg_find_element(usr_obj, "primaryGroupID");
5709 el->flags = LDB_FLAG_MOD_REPLACE;
5719 * Returns True if the source and target DNs both have the same naming context,
5720 * i.e. they're both in the same partition.
5722 bool dsdb_objects_have_same_nc(struct ldb_context *ldb,
5723 TALLOC_CTX *mem_ctx,
5724 struct ldb_dn *source_dn,
5725 struct ldb_dn *target_dn)
5727 TALLOC_CTX *tmp_ctx;
5728 struct ldb_dn *source_nc;
5729 struct ldb_dn *target_nc;
5731 bool same_nc = true;
5733 tmp_ctx = talloc_new(mem_ctx);
5735 ret = dsdb_find_nc_root(ldb, tmp_ctx, source_dn, &source_nc);
5736 if (ret != LDB_SUCCESS) {
5737 DBG_ERR("Failed to find base DN for source %s\n",
5738 ldb_dn_get_linearized(source_dn));
5739 talloc_free(tmp_ctx);
5743 ret = dsdb_find_nc_root(ldb, tmp_ctx, target_dn, &target_nc);
5744 if (ret != LDB_SUCCESS) {
5745 DBG_ERR("Failed to find base DN for target %s\n",
5746 ldb_dn_get_linearized(target_dn));
5747 talloc_free(tmp_ctx);
5751 same_nc = (ldb_dn_compare(source_nc, target_nc) == 0);
5753 talloc_free(tmp_ctx);