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"
51 search the sam for the specified attributes in a specific domain, filter on
52 objectSid being in domain_sid.
54 int samdb_search_domain(struct ldb_context *sam_ldb,
56 struct ldb_dn *basedn,
57 struct ldb_message ***res,
58 const char * const *attrs,
59 const struct dom_sid *domain_sid,
60 const char *format, ...) _PRINTF_ATTRIBUTE(7,8)
66 count = gendb_search_v(sam_ldb, mem_ctx, basedn,
67 res, attrs, format, ap);
73 struct dom_sid *entry_sid;
75 entry_sid = samdb_result_dom_sid(mem_ctx, (*res)[i], "objectSid");
77 if ((entry_sid == NULL) ||
78 (!dom_sid_in_domain(domain_sid, entry_sid))) {
79 /* Delete that entry from the result set */
80 (*res)[i] = (*res)[count-1];
82 talloc_free(entry_sid);
85 talloc_free(entry_sid);
93 search the sam for a single string attribute in exactly 1 record
95 const char *samdb_search_string_v(struct ldb_context *sam_ldb,
97 struct ldb_dn *basedn,
98 const char *attr_name,
99 const char *format, va_list ap) _PRINTF_ATTRIBUTE(5,0)
102 const char *attrs[2] = { NULL, NULL };
103 struct ldb_message **res = NULL;
105 attrs[0] = attr_name;
107 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
109 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
110 attr_name, format, count));
117 return ldb_msg_find_attr_as_string(res[0], attr_name, NULL);
121 search the sam for a single string attribute in exactly 1 record
123 const char *samdb_search_string(struct ldb_context *sam_ldb,
125 struct ldb_dn *basedn,
126 const char *attr_name,
127 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
132 va_start(ap, format);
133 str = samdb_search_string_v(sam_ldb, mem_ctx, basedn, attr_name, format, ap);
139 struct ldb_dn *samdb_search_dn(struct ldb_context *sam_ldb,
141 struct ldb_dn *basedn,
142 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
146 struct ldb_message **res = NULL;
149 va_start(ap, format);
150 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, NULL, format, ap);
153 if (count != 1) return NULL;
155 ret = talloc_steal(mem_ctx, res[0]->dn);
162 search the sam for a dom_sid attribute in exactly 1 record
164 struct dom_sid *samdb_search_dom_sid(struct ldb_context *sam_ldb,
166 struct ldb_dn *basedn,
167 const char *attr_name,
168 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
172 struct ldb_message **res;
173 const char *attrs[2] = { NULL, NULL };
176 attrs[0] = attr_name;
178 va_start(ap, format);
179 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
182 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
183 attr_name, format, count));
189 sid = samdb_result_dom_sid(mem_ctx, res[0], attr_name);
195 return the count of the number of records in the sam matching the query
197 int samdb_search_count(struct ldb_context *sam_ldb,
199 struct ldb_dn *basedn,
200 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
203 const char *attrs[] = { NULL };
206 va_start(ap, format);
207 ret = gendb_search_v(sam_ldb, mem_ctx, basedn, NULL, attrs, format, ap);
215 search the sam for a single integer attribute in exactly 1 record
217 unsigned int samdb_search_uint(struct ldb_context *sam_ldb,
219 unsigned int default_value,
220 struct ldb_dn *basedn,
221 const char *attr_name,
222 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
226 struct ldb_message **res;
227 const char *attrs[2] = { NULL, NULL };
229 attrs[0] = attr_name;
231 va_start(ap, format);
232 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
236 return default_value;
239 return ldb_msg_find_attr_as_uint(res[0], attr_name, default_value);
243 search the sam for a single signed 64 bit integer attribute in exactly 1 record
245 int64_t samdb_search_int64(struct ldb_context *sam_ldb,
247 int64_t default_value,
248 struct ldb_dn *basedn,
249 const char *attr_name,
250 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
254 struct ldb_message **res;
255 const char *attrs[2] = { NULL, NULL };
257 attrs[0] = attr_name;
259 va_start(ap, format);
260 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
264 return default_value;
267 return ldb_msg_find_attr_as_int64(res[0], attr_name, default_value);
271 search the sam for multipe records each giving a single string attribute
272 return the number of matches, or -1 on error
274 int samdb_search_string_multiple(struct ldb_context *sam_ldb,
276 struct ldb_dn *basedn,
278 const char *attr_name,
279 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
283 const char *attrs[2] = { NULL, NULL };
284 struct ldb_message **res = NULL;
286 attrs[0] = attr_name;
288 va_start(ap, format);
289 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
296 /* make sure its single valued */
297 for (i=0;i<count;i++) {
298 if (res[i]->num_elements != 1) {
299 DEBUG(1,("samdb: search for %s %s not single valued\n",
306 *strs = talloc_array(mem_ctx, const char *, count+1);
312 for (i=0;i<count;i++) {
313 (*strs)[i] = ldb_msg_find_attr_as_string(res[i], attr_name, NULL);
315 (*strs)[count] = NULL;
320 struct ldb_dn *samdb_result_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
321 const char *attr, struct ldb_dn *default_value)
323 struct ldb_dn *ret_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, msg, attr);
325 return default_value;
331 pull a rid from a objectSid in a result set.
333 uint32_t samdb_result_rid_from_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
334 const char *attr, uint32_t default_value)
339 sid = samdb_result_dom_sid(mem_ctx, msg, attr);
341 return default_value;
343 rid = sid->sub_auths[sid->num_auths-1];
349 pull a dom_sid structure from a objectSid in a result set.
351 struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
355 const struct ldb_val *v;
357 v = ldb_msg_find_ldb_val(msg, attr);
361 sid = talloc(mem_ctx, struct dom_sid);
365 ok = sid_parse(v->data, v->length, sid);
374 pull a guid structure from a objectGUID in a result set.
376 struct GUID samdb_result_guid(const struct ldb_message *msg, const char *attr)
378 const struct ldb_val *v;
382 v = ldb_msg_find_ldb_val(msg, attr);
383 if (!v) return GUID_zero();
385 status = GUID_from_ndr_blob(v, &guid);
386 if (!NT_STATUS_IS_OK(status)) {
394 pull a sid prefix from a objectSid in a result set.
395 this is used to find the domain sid for a user
397 struct dom_sid *samdb_result_sid_prefix(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
400 struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, msg, attr);
401 if (!sid || sid->num_auths < 1) return NULL;
407 pull a NTTIME in a result set.
409 NTTIME samdb_result_nttime(const struct ldb_message *msg, const char *attr,
410 NTTIME default_value)
412 return ldb_msg_find_attr_as_uint64(msg, attr, default_value);
416 * Windows stores 0 for lastLogoff.
417 * But when a MS DC return the lastLogoff (as Logoff Time)
418 * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
419 * cause windows 2008 and newer version to fail for SMB requests
421 NTTIME samdb_result_last_logoff(const struct ldb_message *msg)
423 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "lastLogoff",0);
426 ret = 0x7FFFFFFFFFFFFFFFULL;
432 * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
433 * indicate an account doesn't expire.
435 * When Windows initially creates an account, it sets
436 * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF). However,
437 * when changing from an account having a specific expiration date to
438 * that account never expiring, it sets accountExpires = 0.
440 * Consolidate that logic here to allow clearer logic for account expiry in
441 * the rest of the code.
443 NTTIME samdb_result_account_expires(const struct ldb_message *msg)
445 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "accountExpires",
449 ret = 0x7FFFFFFFFFFFFFFFULL;
455 construct the allow_password_change field from the PwdLastSet attribute and the
456 domain password settings
458 NTTIME samdb_result_allow_password_change(struct ldb_context *sam_ldb,
460 struct ldb_dn *domain_dn,
461 struct ldb_message *msg,
464 uint64_t attr_time = ldb_msg_find_attr_as_uint64(msg, attr, 0);
467 if (attr_time == 0) {
471 minPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "minPwdAge", NULL);
473 /* yes, this is a -= not a += as minPwdAge is stored as the negative
474 of the number of 100-nano-seconds */
475 attr_time -= minPwdAge;
481 pull a samr_Password structutre from a result set.
483 struct samr_Password *samdb_result_hash(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, const char *attr)
485 struct samr_Password *hash = NULL;
486 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
487 if (val && (val->length >= sizeof(hash->hash))) {
488 hash = talloc(mem_ctx, struct samr_Password);
489 memcpy(hash->hash, val->data, MIN(val->length, sizeof(hash->hash)));
495 pull an array of samr_Password structures from a result set.
497 unsigned int samdb_result_hashes(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
498 const char *attr, struct samr_Password **hashes)
500 unsigned int count, i;
501 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
507 count = val->length / 16;
512 *hashes = talloc_array(mem_ctx, struct samr_Password, count);
517 for (i=0;i<count;i++) {
518 memcpy((*hashes)[i].hash, (i*16)+(char *)val->data, 16);
524 NTSTATUS samdb_result_passwords_from_history(TALLOC_CTX *mem_ctx,
525 struct loadparm_context *lp_ctx,
526 struct ldb_message *msg,
528 struct samr_Password **lm_pwd,
529 struct samr_Password **nt_pwd)
531 struct samr_Password *lmPwdHash, *ntPwdHash;
535 num_nt = samdb_result_hashes(mem_ctx, msg, "ntPwdHistory", &ntPwdHash);
539 *nt_pwd = &ntPwdHash[idx];
543 /* Ensure that if we have turned off LM
544 * authentication, that we never use the LM hash, even
546 if (lpcfg_lanman_auth(lp_ctx)) {
548 num_lm = samdb_result_hashes(mem_ctx, msg, "lmPwdHistory", &lmPwdHash);
552 *lm_pwd = &lmPwdHash[idx];
561 NTSTATUS samdb_result_passwords_no_lockout(TALLOC_CTX *mem_ctx,
562 struct loadparm_context *lp_ctx,
563 const struct ldb_message *msg,
564 struct samr_Password **lm_pwd,
565 struct samr_Password **nt_pwd)
567 struct samr_Password *lmPwdHash, *ntPwdHash;
571 num_nt = samdb_result_hashes(mem_ctx, msg, "unicodePwd", &ntPwdHash);
574 } else if (num_nt > 1) {
575 return NT_STATUS_INTERNAL_DB_CORRUPTION;
577 *nt_pwd = &ntPwdHash[0];
581 /* Ensure that if we have turned off LM
582 * authentication, that we never use the LM hash, even
584 if (lpcfg_lanman_auth(lp_ctx)) {
586 num_lm = samdb_result_hashes(mem_ctx, msg, "dBCSPwd", &lmPwdHash);
589 } else if (num_lm > 1) {
590 return NT_STATUS_INTERNAL_DB_CORRUPTION;
592 *lm_pwd = &lmPwdHash[0];
601 NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx,
602 struct loadparm_context *lp_ctx,
603 const struct ldb_message *msg,
604 struct samr_Password **lm_pwd,
605 struct samr_Password **nt_pwd)
609 acct_flags = samdb_result_acct_flags(msg,
610 "msDS-User-Account-Control-Computed");
611 /* Quit if the account was locked out. */
612 if (acct_flags & ACB_AUTOLOCK) {
613 DEBUG(3,("samdb_result_passwords: Account for user %s was locked out.\n",
614 ldb_dn_get_linearized(msg->dn)));
615 return NT_STATUS_ACCOUNT_LOCKED_OUT;
618 return samdb_result_passwords_no_lockout(mem_ctx, lp_ctx, msg,
623 pull a samr_LogonHours structutre from a result set.
625 struct samr_LogonHours samdb_result_logon_hours(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char *attr)
627 struct samr_LogonHours hours;
628 size_t units_per_week = 168;
629 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
634 units_per_week = val->length * 8;
637 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week/8);
641 hours.units_per_week = units_per_week;
642 memset(hours.bits, 0xFF, units_per_week/8);
644 memcpy(hours.bits, val->data, val->length);
651 pull a set of account_flags from a result set.
653 Naturally, this requires that userAccountControl and
654 (if not null) the attributes 'attr' be already
657 uint32_t samdb_result_acct_flags(const struct ldb_message *msg, const char *attr)
659 uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
660 uint32_t attr_flags = 0;
661 uint32_t acct_flags = ds_uf2acb(userAccountControl);
663 attr_flags = ldb_msg_find_attr_as_uint(msg, attr, UF_ACCOUNTDISABLE);
664 if (attr_flags == UF_ACCOUNTDISABLE) {
665 DEBUG(0, ("Attribute %s not found, disabling account %s!\n", attr,
666 ldb_dn_get_linearized(msg->dn)));
668 acct_flags |= ds_uf2acb(attr_flags);
674 NTSTATUS samdb_result_parameters(TALLOC_CTX *mem_ctx,
675 struct ldb_message *msg,
677 struct lsa_BinaryString *s)
680 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
688 if ((val->length % 2) != 0) {
690 * If the on-disk data is not even in length, we know
691 * it is corrupt, and can not be safely pushed. We
692 * would either truncate, send either a un-initilaised
693 * byte or send a forced zero byte
695 return NT_STATUS_INTERNAL_DB_CORRUPTION;
698 s->array = talloc_array(mem_ctx, uint16_t, val->length/2);
700 return NT_STATUS_NO_MEMORY;
702 s->length = s->size = val->length;
704 /* The on-disk format is the 'network' format, being UTF16LE (sort of) */
705 for (i = 0; i < s->length / 2; i++) {
706 s->array[i] = SVAL(val->data, i * 2);
712 /* Find an attribute, with a particular value */
714 /* The current callers of this function expect a very specific
715 * behaviour: In particular, objectClass subclass equivilance is not
716 * wanted. This means that we should not lookup the schema for the
717 * comparison function */
718 struct ldb_message_element *samdb_find_attribute(struct ldb_context *ldb,
719 const struct ldb_message *msg,
720 const char *name, const char *value)
723 struct ldb_message_element *el = ldb_msg_find_element(msg, name);
729 for (i=0;i<el->num_values;i++) {
730 if (ldb_attr_cmp(value, (char *)el->values[i].data) == 0) {
738 static int samdb_find_or_add_attribute_ex(struct ldb_context *ldb,
739 struct ldb_message *msg,
741 const char *set_value,
746 struct ldb_message_element *el;
748 SMB_ASSERT(attr_flags != 0);
750 el = ldb_msg_find_element(msg, name);
759 ret = ldb_msg_add_empty(msg, name,
762 if (ret != LDB_SUCCESS) {
766 if (set_value != NULL) {
767 ret = ldb_msg_add_string(msg, name, set_value);
768 if (ret != LDB_SUCCESS) {
779 int samdb_find_or_add_attribute(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
781 return samdb_find_or_add_attribute_ex(ldb, msg, name, set_value, LDB_FLAG_MOD_ADD, NULL);
785 add a dom_sid element to a message
787 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
788 const char *attr_name, const struct dom_sid *sid)
791 enum ndr_err_code ndr_err;
793 ndr_err = ndr_push_struct_blob(&v, mem_ctx,
795 (ndr_push_flags_fn_t)ndr_push_dom_sid);
796 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
797 return ldb_operr(sam_ldb);
799 return ldb_msg_add_value(msg, attr_name, &v, NULL);
804 add a delete element operation to a message
806 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
807 const char *attr_name)
809 /* we use an empty replace rather than a delete, as it allows for
810 dsdb_replace() to be used everywhere */
811 return ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_REPLACE, NULL);
815 add an add attribute value to a message or enhance an existing attribute
816 which has the same name and the add flag set.
818 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
819 struct ldb_message *msg, const char *attr_name,
822 struct ldb_message_element *el;
823 struct ldb_val val, *vals;
829 v = talloc_strdup(mem_ctx, value);
831 return ldb_oom(sam_ldb);
834 val.data = (uint8_t *) v;
835 val.length = strlen(v);
837 if (val.length == 0) {
838 /* allow empty strings as non-existent attributes */
842 for (i = 0; i < msg->num_elements; i++) {
843 el = &msg->elements[i];
844 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
845 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD)) {
851 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_ADD,
853 if (ret != LDB_SUCCESS) {
858 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
861 return ldb_oom(sam_ldb);
864 el->values[el->num_values] = val;
871 add a delete attribute value to a message or enhance an existing attribute
872 which has the same name and the delete flag set.
874 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
875 struct ldb_message *msg, const char *attr_name,
878 struct ldb_message_element *el;
879 struct ldb_val val, *vals;
885 v = talloc_strdup(mem_ctx, value);
887 return ldb_oom(sam_ldb);
890 val.data = (uint8_t *) v;
891 val.length = strlen(v);
893 if (val.length == 0) {
894 /* allow empty strings as non-existent attributes */
898 for (i = 0; i < msg->num_elements; i++) {
899 el = &msg->elements[i];
900 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
901 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
907 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_DELETE,
909 if (ret != LDB_SUCCESS) {
914 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
917 return ldb_oom(sam_ldb);
920 el->values[el->num_values] = val;
927 add a int element to a message
929 int samdb_msg_add_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
930 const char *attr_name, int v)
932 const char *s = talloc_asprintf(mem_ctx, "%d", v);
934 return ldb_oom(sam_ldb);
936 return ldb_msg_add_string(msg, attr_name, s);
940 * Add an unsigned int element to a message
942 * The issue here is that we have not yet first cast to int32_t explicitly,
943 * before we cast to an signed int to printf() into the %d or cast to a
944 * int64_t before we then cast to a long long to printf into a %lld.
946 * There are *no* unsigned integers in Active Directory LDAP, even the RID
947 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
948 * (See the schema, and the syntax definitions in schema_syntax.c).
951 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
952 const char *attr_name, unsigned int v)
954 return samdb_msg_add_int(sam_ldb, mem_ctx, msg, attr_name, (int)v);
958 add a (signed) int64_t element to a message
960 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
961 const char *attr_name, int64_t v)
963 const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
965 return ldb_oom(sam_ldb);
967 return ldb_msg_add_string(msg, attr_name, s);
971 * Add an unsigned int64_t (uint64_t) element to a message
973 * The issue here is that we have not yet first cast to int32_t explicitly,
974 * before we cast to an signed int to printf() into the %d or cast to a
975 * int64_t before we then cast to a long long to printf into a %lld.
977 * There are *no* unsigned integers in Active Directory LDAP, even the RID
978 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
979 * (See the schema, and the syntax definitions in schema_syntax.c).
982 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
983 const char *attr_name, uint64_t v)
985 return samdb_msg_add_int64(sam_ldb, mem_ctx, msg, attr_name, (int64_t)v);
989 add a samr_Password element to a message
991 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
992 const char *attr_name, const struct samr_Password *hash)
995 val.data = talloc_memdup(mem_ctx, hash->hash, 16);
997 return ldb_oom(sam_ldb);
1000 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1004 add a samr_Password array to a message
1006 int samdb_msg_add_hashes(struct ldb_context *ldb,
1007 TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1008 const char *attr_name, struct samr_Password *hashes,
1013 val.data = talloc_array_size(mem_ctx, 16, count);
1014 val.length = count*16;
1016 return ldb_oom(ldb);
1018 for (i=0;i<count;i++) {
1019 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
1021 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1025 add a acct_flags element to a message
1027 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1028 const char *attr_name, uint32_t v)
1030 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
1034 add a logon_hours element to a message
1036 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1037 const char *attr_name, struct samr_LogonHours *hours)
1040 val.length = hours->units_per_week / 8;
1041 val.data = hours->bits;
1042 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1046 add a parameters element to a message
1048 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1049 const char *attr_name, struct lsa_BinaryString *parameters)
1053 if ((parameters->length % 2) != 0) {
1054 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
1057 val.data = talloc_array(mem_ctx, uint8_t, parameters->length);
1058 if (val.data == NULL) {
1059 return LDB_ERR_OPERATIONS_ERROR;
1061 val.length = parameters->length;
1062 for (i = 0; i < parameters->length / 2; i++) {
1064 * The on-disk format needs to be in the 'network'
1065 * format, parmeters->array is a uint16_t array of
1066 * length parameters->length / 2
1068 SSVAL(val.data, i * 2, parameters->array[i]);
1070 return ldb_msg_add_steal_value(msg, attr_name, &val);
1074 * Sets an unsigned int element in a message
1076 * The issue here is that we have not yet first cast to int32_t explicitly,
1077 * before we cast to an signed int to printf() into the %d or cast to a
1078 * int64_t before we then cast to a long long to printf into a %lld.
1080 * There are *no* unsigned integers in Active Directory LDAP, even the RID
1081 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
1082 * (See the schema, and the syntax definitions in schema_syntax.c).
1085 int samdb_msg_set_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
1086 struct ldb_message *msg, const char *attr_name,
1089 struct ldb_message_element *el;
1091 el = ldb_msg_find_element(msg, attr_name);
1095 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, v);
1099 * Handle ldb_request in transaction
1101 int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
1102 struct ldb_request *req)
1106 ret = ldb_transaction_start(sam_ldb);
1107 if (ret != LDB_SUCCESS) {
1111 ret = ldb_request(sam_ldb, req);
1112 if (ret == LDB_SUCCESS) {
1113 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1116 if (ret == LDB_SUCCESS) {
1117 return ldb_transaction_commit(sam_ldb);
1119 ldb_transaction_cancel(sam_ldb);
1125 return a default security descriptor
1127 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1129 struct security_descriptor *sd;
1131 sd = security_descriptor_initialise(mem_ctx);
1136 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1138 struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1139 struct ldb_dn *aggregate_dn;
1144 aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1145 if (!aggregate_dn) {
1148 if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1151 return aggregate_dn;
1154 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1156 struct ldb_dn *new_dn;
1158 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1159 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1160 talloc_free(new_dn);
1166 struct ldb_dn *samdb_infrastructure_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1168 struct ldb_dn *new_dn;
1170 new_dn = ldb_dn_copy(mem_ctx, ldb_get_default_basedn(sam_ctx));
1171 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Infrastructure")) {
1172 talloc_free(new_dn);
1178 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1180 struct ldb_dn *new_dn;
1182 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1183 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1184 talloc_free(new_dn);
1191 work out the domain sid for the current open ldb
1193 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1195 TALLOC_CTX *tmp_ctx;
1196 const struct dom_sid *domain_sid;
1197 const char *attrs[] = {
1201 struct ldb_result *res;
1204 /* see if we have a cached copy */
1205 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1210 tmp_ctx = talloc_new(ldb);
1211 if (tmp_ctx == NULL) {
1215 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1217 if (ret != LDB_SUCCESS) {
1221 if (res->count != 1) {
1225 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1226 if (domain_sid == NULL) {
1230 /* cache the domain_sid in the ldb */
1231 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1235 talloc_steal(ldb, domain_sid);
1236 talloc_free(tmp_ctx);
1241 talloc_free(tmp_ctx);
1246 get domain sid from cache
1248 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1250 return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1253 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1255 TALLOC_CTX *tmp_ctx;
1256 struct dom_sid *dom_sid_new;
1257 struct dom_sid *dom_sid_old;
1259 /* see if we have a cached copy */
1260 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1261 "cache.domain_sid"), struct dom_sid);
1263 tmp_ctx = talloc_new(ldb);
1264 if (tmp_ctx == NULL) {
1268 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1273 /* cache the domain_sid in the ldb */
1274 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1278 talloc_steal(ldb, dom_sid_new);
1279 talloc_free(tmp_ctx);
1280 talloc_free(dom_sid_old);
1285 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1286 talloc_free(tmp_ctx);
1290 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
1292 TALLOC_CTX *tmp_ctx;
1293 struct ldb_dn *ntds_settings_dn_new;
1294 struct ldb_dn *ntds_settings_dn_old;
1296 /* see if we have a forced copy from provision */
1297 ntds_settings_dn_old = talloc_get_type(ldb_get_opaque(ldb,
1298 "forced.ntds_settings_dn"), struct ldb_dn);
1300 tmp_ctx = talloc_new(ldb);
1301 if (tmp_ctx == NULL) {
1305 ntds_settings_dn_new = ldb_dn_copy(tmp_ctx, ntds_settings_dn_in);
1306 if (!ntds_settings_dn_new) {
1310 /* set the DN in the ldb to avoid lookups during provision */
1311 if (ldb_set_opaque(ldb, "forced.ntds_settings_dn", ntds_settings_dn_new) != LDB_SUCCESS) {
1315 talloc_steal(ldb, ntds_settings_dn_new);
1316 talloc_free(tmp_ctx);
1317 talloc_free(ntds_settings_dn_old);
1322 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1323 talloc_free(tmp_ctx);
1328 work out the ntds settings dn for the current open ldb
1330 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1332 TALLOC_CTX *tmp_ctx;
1333 const char *root_attrs[] = { "dsServiceName", NULL };
1335 struct ldb_result *root_res;
1336 struct ldb_dn *settings_dn;
1338 /* see if we have a cached copy */
1339 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "forced.ntds_settings_dn");
1341 return ldb_dn_copy(mem_ctx, settings_dn);
1344 tmp_ctx = talloc_new(mem_ctx);
1345 if (tmp_ctx == NULL) {
1349 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1350 if (ret != LDB_SUCCESS) {
1351 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1352 ldb_errstring(ldb)));
1356 if (root_res->count != 1) {
1360 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1362 /* note that we do not cache the DN here, as that would mean
1363 * we could not handle server renames at runtime. Only
1364 * provision sets up forced.ntds_settings_dn */
1366 talloc_steal(mem_ctx, settings_dn);
1367 talloc_free(tmp_ctx);
1372 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1373 talloc_free(tmp_ctx);
1378 work out the ntds settings invocationId for the current open ldb
1380 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1382 TALLOC_CTX *tmp_ctx;
1383 const char *attrs[] = { "invocationId", NULL };
1385 struct ldb_result *res;
1386 struct GUID *invocation_id;
1388 /* see if we have a cached copy */
1389 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1390 if (invocation_id) {
1391 SMB_ASSERT(!GUID_all_zero(invocation_id));
1392 return invocation_id;
1395 tmp_ctx = talloc_new(ldb);
1396 if (tmp_ctx == NULL) {
1400 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1405 if (res->count != 1) {
1409 invocation_id = talloc(tmp_ctx, struct GUID);
1410 if (!invocation_id) {
1414 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1415 if (GUID_all_zero(invocation_id)) {
1416 if (ldb_msg_find_ldb_val(res->msgs[0], "invocationId")) {
1417 DEBUG(0, ("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1419 DEBUG(0, ("Failed to find parse own NTDS Settings invocationId from the ldb!\n"));
1424 /* cache the domain_sid in the ldb */
1425 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1429 talloc_steal(ldb, invocation_id);
1430 talloc_free(tmp_ctx);
1432 return invocation_id;
1435 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1436 talloc_free(tmp_ctx);
1440 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1442 TALLOC_CTX *tmp_ctx;
1443 struct GUID *invocation_id_new;
1444 struct GUID *invocation_id_old;
1446 /* see if we have a cached copy */
1447 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1448 "cache.invocation_id");
1450 tmp_ctx = talloc_new(ldb);
1451 if (tmp_ctx == NULL) {
1455 invocation_id_new = talloc(tmp_ctx, struct GUID);
1456 if (!invocation_id_new) {
1460 SMB_ASSERT(!GUID_all_zero(invocation_id_in));
1461 *invocation_id_new = *invocation_id_in;
1463 /* cache the domain_sid in the ldb */
1464 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1468 talloc_steal(ldb, invocation_id_new);
1469 talloc_free(tmp_ctx);
1470 talloc_free(invocation_id_old);
1475 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1476 talloc_free(tmp_ctx);
1481 work out the ntds settings objectGUID for the current open ldb
1483 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1485 TALLOC_CTX *tmp_ctx;
1486 const char *attrs[] = { "objectGUID", NULL };
1488 struct ldb_result *res;
1489 struct GUID *ntds_guid;
1491 /* see if we have a cached copy */
1492 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1497 tmp_ctx = talloc_new(ldb);
1498 if (tmp_ctx == NULL) {
1502 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1507 if (res->count != 1) {
1511 ntds_guid = talloc(tmp_ctx, struct GUID);
1516 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1518 /* cache the domain_sid in the ldb */
1519 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1523 talloc_steal(ldb, ntds_guid);
1524 talloc_free(tmp_ctx);
1529 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1530 talloc_free(tmp_ctx);
1534 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1536 TALLOC_CTX *tmp_ctx;
1537 struct GUID *ntds_guid_new;
1538 struct GUID *ntds_guid_old;
1540 /* see if we have a cached copy */
1541 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1543 tmp_ctx = talloc_new(ldb);
1544 if (tmp_ctx == NULL) {
1548 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1549 if (!ntds_guid_new) {
1553 *ntds_guid_new = *ntds_guid_in;
1555 /* cache the domain_sid in the ldb */
1556 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1560 talloc_steal(ldb, ntds_guid_new);
1561 talloc_free(tmp_ctx);
1562 talloc_free(ntds_guid_old);
1567 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1568 talloc_free(tmp_ctx);
1573 work out the server dn for the current open ldb
1575 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1577 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1582 dn = ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb, tmp_ctx));
1583 talloc_free(tmp_ctx);
1589 work out the server dn for the current open ldb
1591 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1593 struct ldb_dn *server_dn;
1594 struct ldb_dn *servers_dn;
1595 struct ldb_dn *server_site_dn;
1597 /* TODO: there must be a saner way to do this!! */
1598 server_dn = samdb_server_dn(ldb, mem_ctx);
1599 if (!server_dn) return NULL;
1601 servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1602 talloc_free(server_dn);
1603 if (!servers_dn) return NULL;
1605 server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1606 talloc_free(servers_dn);
1608 return server_site_dn;
1612 find the site name from a computers DN record
1614 int samdb_find_site_for_computer(struct ldb_context *ldb,
1615 TALLOC_CTX *mem_ctx, struct ldb_dn *computer_dn,
1616 const char **site_name)
1620 const struct ldb_val *rdn_val;
1624 ret = samdb_reference_dn(ldb, mem_ctx, computer_dn, "serverReferenceBL", &dn);
1625 if (ret != LDB_SUCCESS) {
1629 if (!ldb_dn_remove_child_components(dn, 2)) {
1631 return LDB_ERR_INVALID_DN_SYNTAX;
1634 rdn_val = ldb_dn_get_rdn_val(dn);
1635 if (rdn_val == NULL) {
1636 return LDB_ERR_OPERATIONS_ERROR;
1639 (*site_name) = talloc_strndup(mem_ctx, (const char *)rdn_val->data, rdn_val->length);
1642 return LDB_ERR_OPERATIONS_ERROR;
1648 find the NTDS GUID from a computers DN record
1650 int samdb_find_ntdsguid_for_computer(struct ldb_context *ldb, struct ldb_dn *computer_dn,
1651 struct GUID *ntds_guid)
1656 *ntds_guid = GUID_zero();
1658 ret = samdb_reference_dn(ldb, ldb, computer_dn, "serverReferenceBL", &dn);
1659 if (ret != LDB_SUCCESS) {
1663 if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Settings")) {
1665 return LDB_ERR_OPERATIONS_ERROR;
1668 ret = dsdb_find_guid_by_dn(ldb, dn, ntds_guid);
1674 find a 'reference' DN that points at another object
1675 (eg. serverReference, rIDManagerReference etc)
1677 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1678 const char *attribute, struct ldb_dn **dn)
1680 const char *attrs[2];
1681 struct ldb_result *res;
1684 attrs[0] = attribute;
1687 ret = dsdb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_ONE_ONLY|DSDB_SEARCH_SHOW_EXTENDED_DN, NULL);
1688 if (ret != LDB_SUCCESS) {
1689 ldb_asprintf_errstring(ldb, "Cannot find DN %s to get attribute %s for reference dn: %s",
1690 ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb));
1694 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1696 if (!ldb_msg_find_element(res->msgs[0], attribute)) {
1697 ldb_asprintf_errstring(ldb, "Cannot find attribute %s of %s to calculate reference dn", attribute,
1698 ldb_dn_get_linearized(base));
1700 ldb_asprintf_errstring(ldb, "Cannot interpret attribute %s of %s as a dn", attribute,
1701 ldb_dn_get_linearized(base));
1704 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1712 find if a DN (must have GUID component!) is our ntdsDsa
1714 int samdb_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *dn, bool *is_ntdsa)
1717 struct GUID dn_guid;
1718 const struct GUID *our_ntds_guid;
1719 status = dsdb_get_extended_dn_guid(dn, &dn_guid, "GUID");
1720 if (!NT_STATUS_IS_OK(status)) {
1721 return LDB_ERR_OPERATIONS_ERROR;
1724 our_ntds_guid = samdb_ntds_objectGUID(ldb);
1725 if (!our_ntds_guid) {
1726 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
1727 return LDB_ERR_OPERATIONS_ERROR;
1730 *is_ntdsa = GUID_equal(&dn_guid, our_ntds_guid);
1735 find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1737 int samdb_reference_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *base,
1738 const char *attribute, bool *is_ntdsa)
1741 struct ldb_dn *referenced_dn;
1742 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1743 if (tmp_ctx == NULL) {
1744 return LDB_ERR_OPERATIONS_ERROR;
1746 ret = samdb_reference_dn(ldb, tmp_ctx, base, attribute, &referenced_dn);
1747 if (ret != LDB_SUCCESS) {
1748 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb)));
1752 ret = samdb_dn_is_our_ntdsa(ldb, referenced_dn, is_ntdsa);
1754 talloc_free(tmp_ctx);
1759 find our machine account via the serverReference attribute in the
1762 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1764 struct ldb_dn *server_dn;
1767 server_dn = samdb_server_dn(ldb, mem_ctx);
1768 if (server_dn == NULL) {
1769 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
1772 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1773 talloc_free(server_dn);
1779 find the RID Manager$ DN via the rIDManagerReference attribute in the
1782 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1784 return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1785 "rIDManagerReference", dn);
1789 find the RID Set DN via the rIDSetReferences attribute in our
1792 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1794 struct ldb_dn *server_ref_dn;
1797 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1798 if (ret != LDB_SUCCESS) {
1801 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1802 talloc_free(server_ref_dn);
1806 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1808 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1815 return (const char *) val->data;
1819 * Finds the client site by using the client's IP address.
1820 * The "subnet_name" returns the name of the subnet if parameter != NULL
1822 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1823 const char *ip_address, char **subnet_name)
1825 const char *attrs[] = { "cn", "siteObject", NULL };
1826 struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
1827 struct ldb_result *res;
1828 const struct ldb_val *val;
1829 const char *site_name = NULL, *l_subnet_name = NULL;
1830 const char *allow_list[2] = { NULL, NULL };
1831 unsigned int i, count;
1835 * if we don't have a client ip e.g. ncalrpc
1836 * the server site is the client site
1838 if (ip_address == NULL) {
1839 return samdb_server_site_name(ldb, mem_ctx);
1842 sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1843 if (sites_container_dn == NULL) {
1847 subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1848 if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1849 talloc_free(sites_container_dn);
1850 talloc_free(subnets_dn);
1854 ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1856 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1858 } else if (ret != LDB_SUCCESS) {
1859 talloc_free(sites_container_dn);
1860 talloc_free(subnets_dn);
1866 for (i = 0; i < count; i++) {
1867 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1870 allow_list[0] = l_subnet_name;
1872 if (socket_allow_access(mem_ctx, NULL, allow_list, "", ip_address)) {
1873 sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1876 if (sites_dn == NULL) {
1877 /* No reference, maybe another subnet matches */
1881 /* "val" cannot be NULL here since "sites_dn" != NULL */
1882 val = ldb_dn_get_rdn_val(sites_dn);
1883 site_name = talloc_strdup(mem_ctx,
1884 (const char *) val->data);
1886 talloc_free(sites_dn);
1892 if (site_name == NULL) {
1893 /* This is the Windows Server fallback rule: when no subnet
1894 * exists and we have only one site available then use it (it
1895 * is for sure the same as our server site). If more sites do
1896 * exist then we don't know which one to use and set the site
1898 cnt = samdb_search_count(ldb, mem_ctx, sites_container_dn,
1899 "(objectClass=site)");
1901 site_name = samdb_server_site_name(ldb, mem_ctx);
1903 site_name = talloc_strdup(mem_ctx, "");
1905 l_subnet_name = NULL;
1908 if (subnet_name != NULL) {
1909 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1912 talloc_free(sites_container_dn);
1913 talloc_free(subnets_dn);
1920 work out if we are the PDC for the domain of the current open ldb
1922 bool samdb_is_pdc(struct ldb_context *ldb)
1927 ret = samdb_reference_dn_is_our_ntdsa(ldb, ldb_get_default_basedn(ldb), "fsmoRoleOwner",
1929 if (ret != LDB_SUCCESS) {
1930 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n",
1931 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1932 ldb_errstring(ldb)));
1940 work out if we are a Global Catalog server for the domain of the current open ldb
1942 bool samdb_is_gc(struct ldb_context *ldb)
1945 if (samdb_ntds_options(ldb, &options) != LDB_SUCCESS) {
1948 return (options & DS_NTDSDSA_OPT_IS_GC) != 0;
1951 /* Find a domain object in the parents of a particular DN. */
1952 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1953 struct ldb_dn **parent_dn, const char **errstring)
1955 TALLOC_CTX *local_ctx;
1956 struct ldb_dn *sdn = dn;
1957 struct ldb_result *res = NULL;
1958 int ret = LDB_SUCCESS;
1959 const char *attrs[] = { NULL };
1961 local_ctx = talloc_new(mem_ctx);
1962 if (local_ctx == NULL) return ldb_oom(ldb);
1964 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1965 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1966 "(|(objectClass=domain)(objectClass=builtinDomain))");
1967 if (ret == LDB_SUCCESS) {
1968 if (res->count == 1) {
1976 if (ret != LDB_SUCCESS) {
1977 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1978 ldb_dn_get_linearized(dn),
1979 ldb_dn_get_linearized(sdn),
1980 ldb_errstring(ldb));
1981 talloc_free(local_ctx);
1984 if (res->count != 1) {
1985 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1986 ldb_dn_get_linearized(dn));
1987 DEBUG(0,(__location__ ": %s\n", *errstring));
1988 talloc_free(local_ctx);
1989 return LDB_ERR_CONSTRAINT_VIOLATION;
1992 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1993 talloc_free(local_ctx);
1997 static void pwd_timeout_debug(struct tevent_context *unused1,
1998 struct tevent_timer *unused2,
1999 struct timeval unused3,
2002 DEBUG(0, ("WARNING: check_password_complexity: password script "
2003 "took more than 1 second to run\n"));
2008 * Performs checks on a user password (plaintext UNIX format - attribute
2009 * "password"). The remaining parameters have to be extracted from the domain
2012 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
2014 enum samr_ValidationStatus samdb_check_password(TALLOC_CTX *mem_ctx,
2015 struct loadparm_context *lp_ctx,
2016 const DATA_BLOB *utf8_blob,
2017 const uint32_t pwdProperties,
2018 const uint32_t minPwdLength)
2020 const char *utf8_pw = (const char *)utf8_blob->data;
2021 size_t utf8_len = strlen_m(utf8_pw);
2022 char *password_script = NULL;
2024 /* checks if the "minPwdLength" property is satisfied */
2025 if (minPwdLength > utf8_len) {
2026 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
2029 /* checks the password complexity */
2030 if (!(pwdProperties & DOMAIN_PASSWORD_COMPLEX)) {
2031 return SAMR_VALIDATION_STATUS_SUCCESS;
2034 if (utf8_len == 0) {
2035 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2038 password_script = lpcfg_check_password_script(lp_ctx, mem_ctx);
2039 if (password_script != NULL && *password_script != '\0') {
2042 struct tevent_context *event_ctx = NULL;
2043 struct tevent_req *req = NULL;
2044 struct samba_runcmd_state *run_cmd = NULL;
2045 const char * const cmd[4] = {
2051 event_ctx = tevent_context_init(mem_ctx);
2052 if (event_ctx == NULL) {
2053 TALLOC_FREE(password_script);
2054 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2057 /* Gives a warning after 1 second, terminates after 10 */
2058 tevent_add_timer(event_ctx, event_ctx,
2059 tevent_timeval_current_ofs(1, 0),
2060 pwd_timeout_debug, NULL);
2062 req = samba_runcmd_send(event_ctx, event_ctx,
2063 tevent_timeval_current_ofs(10, 0),
2064 100, 100, cmd, NULL);
2065 run_cmd = tevent_req_data(req, struct samba_runcmd_state);
2066 if (write(run_cmd->fd_stdin, utf8_pw, utf8_len) != utf8_len) {
2067 TALLOC_FREE(password_script);
2068 TALLOC_FREE(event_ctx);
2069 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2072 close(run_cmd->fd_stdin);
2073 run_cmd->fd_stdin = -1;
2075 if (!tevent_req_poll(req, event_ctx)) {
2076 TALLOC_FREE(password_script);
2077 TALLOC_FREE(event_ctx);
2078 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2081 check_ret = samba_runcmd_recv(req, &error);
2082 TALLOC_FREE(event_ctx);
2084 if (error == ETIMEDOUT) {
2085 DEBUG(0, ("check_password_complexity: check password script took too long!\n"));
2086 TALLOC_FREE(password_script);
2087 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2089 DEBUG(5,("check_password_complexity: check password script (%s) "
2090 "returned [%d]\n", password_script, check_ret));
2092 if (check_ret != 0) {
2093 DEBUG(1,("check_password_complexity: "
2094 "check password script said new password is not good "
2096 TALLOC_FREE(password_script);
2097 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2101 TALLOC_FREE(password_script);
2102 return SAMR_VALIDATION_STATUS_SUCCESS;
2105 TALLOC_FREE(password_script);
2107 if (!check_password_quality(utf8_pw)) {
2108 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2111 return SAMR_VALIDATION_STATUS_SUCCESS;
2115 * Callback for "samdb_set_password" password change
2117 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
2122 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2125 if (ares->error != LDB_SUCCESS) {
2127 req->context = talloc_steal(req,
2128 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2130 return ldb_request_done(req, ret);
2133 if (ares->type != LDB_REPLY_DONE) {
2135 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2138 req->context = talloc_steal(req,
2139 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2141 return ldb_request_done(req, LDB_SUCCESS);
2145 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2146 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2147 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2148 * user change or not. The "rejectReason" gives some more information if the
2151 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2152 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2154 static NTSTATUS samdb_set_password_internal(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2155 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2156 const DATA_BLOB *new_password,
2157 const struct samr_Password *lmNewHash,
2158 const struct samr_Password *ntNewHash,
2159 const struct samr_Password *lmOldHash,
2160 const struct samr_Password *ntOldHash,
2161 enum samPwdChangeReason *reject_reason,
2162 struct samr_DomInfo1 **_dominfo,
2163 bool permit_interdomain_trust)
2165 struct ldb_message *msg;
2166 struct ldb_message_element *el;
2167 struct ldb_request *req;
2168 struct dsdb_control_password_change_status *pwd_stat = NULL;
2170 bool hash_values = false;
2171 NTSTATUS status = NT_STATUS_OK;
2173 #define CHECK_RET(x) \
2174 if (x != LDB_SUCCESS) { \
2176 return NT_STATUS_NO_MEMORY; \
2179 msg = ldb_msg_new(mem_ctx);
2181 return NT_STATUS_NO_MEMORY;
2184 if ((new_password != NULL)
2185 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
2186 /* we have the password as plaintext UTF16 */
2187 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
2188 new_password, NULL));
2189 el = ldb_msg_find_element(msg, "clearTextPassword");
2190 el->flags = LDB_FLAG_MOD_REPLACE;
2191 } else if ((new_password == NULL)
2192 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2193 /* we have a password as LM and/or NT hash */
2194 if (lmNewHash != NULL) {
2195 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2196 "dBCSPwd", lmNewHash));
2197 el = ldb_msg_find_element(msg, "dBCSPwd");
2198 el->flags = LDB_FLAG_MOD_REPLACE;
2200 if (ntNewHash != NULL) {
2201 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2202 "unicodePwd", ntNewHash));
2203 el = ldb_msg_find_element(msg, "unicodePwd");
2204 el->flags = LDB_FLAG_MOD_REPLACE;
2208 /* the password wasn't specified correctly */
2210 return NT_STATUS_INVALID_PARAMETER;
2213 /* build modify request */
2214 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2215 samdb_set_password_callback, NULL);
2216 if (ret != LDB_SUCCESS) {
2218 return NT_STATUS_NO_MEMORY;
2221 /* A password change operation */
2222 if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2223 struct dsdb_control_password_change *change;
2225 change = talloc(req, struct dsdb_control_password_change);
2226 if (change == NULL) {
2229 return NT_STATUS_NO_MEMORY;
2232 change->old_nt_pwd_hash = ntOldHash;
2233 change->old_lm_pwd_hash = lmOldHash;
2235 ret = ldb_request_add_control(req,
2236 DSDB_CONTROL_PASSWORD_CHANGE_OID,
2238 if (ret != LDB_SUCCESS) {
2241 return NT_STATUS_NO_MEMORY;
2245 ret = ldb_request_add_control(req,
2246 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2248 if (ret != LDB_SUCCESS) {
2251 return NT_STATUS_NO_MEMORY;
2254 if (permit_interdomain_trust) {
2255 ret = ldb_request_add_control(req,
2256 DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID,
2258 if (ret != LDB_SUCCESS) {
2261 return NT_STATUS_NO_MEMORY;
2264 ret = ldb_request_add_control(req,
2265 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2267 if (ret != LDB_SUCCESS) {
2270 return NT_STATUS_NO_MEMORY;
2273 ret = dsdb_autotransaction_request(ldb, req);
2275 if (req->context != NULL) {
2276 struct ldb_control *control = talloc_get_type_abort(req->context,
2277 struct ldb_control);
2278 pwd_stat = talloc_get_type_abort(control->data,
2279 struct dsdb_control_password_change_status);
2280 talloc_steal(mem_ctx, pwd_stat);
2286 /* Sets the domain info (if requested) */
2287 if (_dominfo != NULL) {
2288 struct samr_DomInfo1 *dominfo;
2290 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2291 if (dominfo == NULL) {
2292 return NT_STATUS_NO_MEMORY;
2295 if (pwd_stat != NULL) {
2296 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2297 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2298 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2299 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2300 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2303 *_dominfo = dominfo;
2306 if (reject_reason != NULL) {
2307 if (pwd_stat != NULL) {
2308 *reject_reason = pwd_stat->reject_reason;
2310 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2314 if (pwd_stat != NULL) {
2315 talloc_free(pwd_stat);
2318 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2319 const char *errmsg = ldb_errstring(ldb);
2320 char *endptr = NULL;
2321 WERROR werr = WERR_GENERAL_FAILURE;
2322 status = NT_STATUS_UNSUCCESSFUL;
2323 if (errmsg != NULL) {
2324 werr = W_ERROR(strtol(errmsg, &endptr, 16));
2325 DBG_WARNING("%s\n", errmsg);
2327 if (endptr != errmsg) {
2328 if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2329 status = NT_STATUS_WRONG_PASSWORD;
2331 if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2332 status = NT_STATUS_PASSWORD_RESTRICTION;
2335 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2336 /* don't let the caller know if an account doesn't exist */
2337 status = NT_STATUS_WRONG_PASSWORD;
2338 } else if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
2339 status = NT_STATUS_ACCESS_DENIED;
2340 } else if (ret != LDB_SUCCESS) {
2341 DEBUG(1, ("Failed to set password on %s: %s\n",
2342 ldb_dn_get_linearized(msg->dn),
2343 ldb_errstring(ldb)));
2344 status = NT_STATUS_UNSUCCESSFUL;
2350 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2351 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2352 const DATA_BLOB *new_password,
2353 const struct samr_Password *lmNewHash,
2354 const struct samr_Password *ntNewHash,
2355 const struct samr_Password *lmOldHash,
2356 const struct samr_Password *ntOldHash,
2357 enum samPwdChangeReason *reject_reason,
2358 struct samr_DomInfo1 **_dominfo)
2360 return samdb_set_password_internal(ldb, mem_ctx,
2363 lmNewHash, ntNewHash,
2364 lmOldHash, ntOldHash,
2365 reject_reason, _dominfo,
2366 false); /* reject trusts */
2370 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2371 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2372 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2373 * user change or not. The "rejectReason" gives some more information if the
2376 * This wrapper function for "samdb_set_password" takes a SID as input rather
2379 * This call encapsulates a new LDB transaction for changing the password;
2380 * therefore the user hasn't to start a new one.
2382 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2383 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2384 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2385 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2387 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2388 const struct dom_sid *user_sid,
2389 const uint32_t *new_version, /* optional for trusts */
2390 const DATA_BLOB *new_password,
2391 const struct samr_Password *lmNewHash,
2392 const struct samr_Password *ntNewHash,
2393 const struct samr_Password *lmOldHash,
2394 const struct samr_Password *ntOldHash,
2395 enum samPwdChangeReason *reject_reason,
2396 struct samr_DomInfo1 **_dominfo)
2398 TALLOC_CTX *frame = talloc_stackframe();
2400 const char * const user_attrs[] = {
2401 "userAccountControl",
2405 struct ldb_message *user_msg = NULL;
2409 ret = ldb_transaction_start(ldb);
2410 if (ret != LDB_SUCCESS) {
2411 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2413 return NT_STATUS_TRANSACTION_ABORTED;
2416 ret = dsdb_search_one(ldb, frame, &user_msg, ldb_get_default_basedn(ldb),
2417 LDB_SCOPE_SUBTREE, user_attrs, 0,
2418 "(&(objectSid=%s)(objectClass=user))",
2419 ldap_encode_ndr_dom_sid(frame, user_sid));
2420 if (ret != LDB_SUCCESS) {
2421 ldb_transaction_cancel(ldb);
2422 DEBUG(3, ("samdb_set_password_sid: SID[%s] not found in samdb %s - %s, "
2423 "returning NO_SUCH_USER\n",
2424 dom_sid_string(frame, user_sid),
2425 ldb_strerror(ret), ldb_errstring(ldb)));
2427 return NT_STATUS_NO_SUCH_USER;
2430 uac = ldb_msg_find_attr_as_uint(user_msg, "userAccountControl", 0);
2431 if (!(uac & UF_ACCOUNT_TYPE_MASK)) {
2432 ldb_transaction_cancel(ldb);
2433 DEBUG(1, ("samdb_set_password_sid: invalid "
2434 "userAccountControl[0x%08X] for SID[%s] DN[%s], "
2435 "returning NO_SUCH_USER\n",
2436 (unsigned)uac, dom_sid_string(frame, user_sid),
2437 ldb_dn_get_linearized(user_msg->dn)));
2439 return NT_STATUS_NO_SUCH_USER;
2442 if (uac & UF_INTERDOMAIN_TRUST_ACCOUNT) {
2443 const char * const tdo_attrs[] = {
2444 "trustAuthIncoming",
2448 struct ldb_message *tdo_msg = NULL;
2449 const char *account_name = NULL;
2450 uint32_t trust_direction;
2452 const struct ldb_val *old_val = NULL;
2453 struct trustAuthInOutBlob old_blob = {};
2454 uint32_t old_version = 0;
2455 struct AuthenticationInformation *old_version_a = NULL;
2456 uint32_t _new_version = 0;
2457 struct trustAuthInOutBlob new_blob = {};
2458 struct ldb_val new_val = {};
2459 struct timeval tv = timeval_current();
2460 NTTIME now = timeval_to_nttime(&tv);
2461 enum ndr_err_code ndr_err;
2463 if (new_password == NULL && ntNewHash == NULL) {
2464 ldb_transaction_cancel(ldb);
2465 DEBUG(1, ("samdb_set_password_sid: "
2466 "no new password provided "
2467 "sAMAccountName for SID[%s] DN[%s], "
2468 "returning INVALID_PARAMETER\n",
2469 dom_sid_string(frame, user_sid),
2470 ldb_dn_get_linearized(user_msg->dn)));
2472 return NT_STATUS_INVALID_PARAMETER;
2475 if (new_password != NULL && ntNewHash != NULL) {
2476 ldb_transaction_cancel(ldb);
2477 DEBUG(1, ("samdb_set_password_sid: "
2478 "two new passwords provided "
2479 "sAMAccountName for SID[%s] DN[%s], "
2480 "returning INVALID_PARAMETER\n",
2481 dom_sid_string(frame, user_sid),
2482 ldb_dn_get_linearized(user_msg->dn)));
2484 return NT_STATUS_INVALID_PARAMETER;
2487 if (new_password != NULL && (new_password->length % 2)) {
2488 ldb_transaction_cancel(ldb);
2489 DEBUG(2, ("samdb_set_password_sid: "
2490 "invalid utf16 length (%zu) "
2491 "sAMAccountName for SID[%s] DN[%s], "
2492 "returning WRONG_PASSWORD\n",
2493 new_password->length,
2494 dom_sid_string(frame, user_sid),
2495 ldb_dn_get_linearized(user_msg->dn)));
2497 return NT_STATUS_WRONG_PASSWORD;
2500 if (new_password != NULL && new_password->length >= 500) {
2501 ldb_transaction_cancel(ldb);
2502 DEBUG(2, ("samdb_set_password_sid: "
2503 "utf16 password too long (%zu) "
2504 "sAMAccountName for SID[%s] DN[%s], "
2505 "returning WRONG_PASSWORD\n",
2506 new_password->length,
2507 dom_sid_string(frame, user_sid),
2508 ldb_dn_get_linearized(user_msg->dn)));
2510 return NT_STATUS_WRONG_PASSWORD;
2513 account_name = ldb_msg_find_attr_as_string(user_msg,
2514 "sAMAccountName", NULL);
2515 if (account_name == NULL) {
2516 ldb_transaction_cancel(ldb);
2517 DEBUG(1, ("samdb_set_password_sid: missing "
2518 "sAMAccountName for SID[%s] DN[%s], "
2519 "returning NO_SUCH_USER\n",
2520 dom_sid_string(frame, user_sid),
2521 ldb_dn_get_linearized(user_msg->dn)));
2523 return NT_STATUS_NO_SUCH_USER;
2526 nt_status = dsdb_trust_search_tdo_by_type(ldb,
2531 if (!NT_STATUS_IS_OK(nt_status)) {
2532 ldb_transaction_cancel(ldb);
2533 DEBUG(1, ("samdb_set_password_sid: dsdb_trust_search_tdo "
2534 "failed(%s) for sAMAccountName[%s] SID[%s] DN[%s], "
2535 "returning INTERNAL_DB_CORRUPTION\n",
2536 nt_errstr(nt_status), account_name,
2537 dom_sid_string(frame, user_sid),
2538 ldb_dn_get_linearized(user_msg->dn)));
2540 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2543 trust_direction = ldb_msg_find_attr_as_int(tdo_msg,
2544 "trustDirection", 0);
2545 if (!(trust_direction & LSA_TRUST_DIRECTION_INBOUND)) {
2546 ldb_transaction_cancel(ldb);
2547 DEBUG(1, ("samdb_set_password_sid: direction[0x%08X] is "
2548 "not inbound for sAMAccountName[%s] "
2550 "returning INTERNAL_DB_CORRUPTION\n",
2551 (unsigned)trust_direction,
2553 ldb_dn_get_linearized(user_msg->dn),
2554 ldb_dn_get_linearized(tdo_msg->dn)));
2556 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2559 old_val = ldb_msg_find_ldb_val(tdo_msg, "trustAuthIncoming");
2560 if (old_val != NULL) {
2561 ndr_err = ndr_pull_struct_blob(old_val, frame, &old_blob,
2562 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2563 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2564 ldb_transaction_cancel(ldb);
2565 DEBUG(1, ("samdb_set_password_sid: "
2566 "failed(%s) to parse "
2567 "trustAuthOutgoing sAMAccountName[%s] "
2569 "returning INTERNAL_DB_CORRUPTION\n",
2570 ndr_map_error2string(ndr_err),
2572 ldb_dn_get_linearized(user_msg->dn),
2573 ldb_dn_get_linearized(tdo_msg->dn)));
2576 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2580 for (i = old_blob.current.count; i > 0; i--) {
2581 struct AuthenticationInformation *a =
2582 &old_blob.current.array[i - 1];
2584 switch (a->AuthType) {
2585 case TRUST_AUTH_TYPE_NONE:
2586 if (i == old_blob.current.count) {
2588 * remove TRUST_AUTH_TYPE_NONE at the
2591 old_blob.current.count--;
2595 case TRUST_AUTH_TYPE_VERSION:
2597 old_version = a->AuthInfo.version.version;
2600 case TRUST_AUTH_TYPE_CLEAR:
2603 case TRUST_AUTH_TYPE_NT4OWF:
2608 if (new_version == NULL) {
2610 new_version = &_new_version;
2613 if (old_version_a != NULL && *new_version != (old_version + 1)) {
2614 old_version_a->LastUpdateTime = now;
2615 old_version_a->AuthType = TRUST_AUTH_TYPE_NONE;
2618 new_blob.count = MAX(old_blob.current.count, 2);
2619 new_blob.current.array = talloc_zero_array(frame,
2620 struct AuthenticationInformation,
2622 if (new_blob.current.array == NULL) {
2623 ldb_transaction_cancel(ldb);
2625 return NT_STATUS_NO_MEMORY;
2627 new_blob.previous.array = talloc_zero_array(frame,
2628 struct AuthenticationInformation,
2630 if (new_blob.current.array == NULL) {
2631 ldb_transaction_cancel(ldb);
2633 return NT_STATUS_NO_MEMORY;
2636 for (i = 0; i < old_blob.current.count; i++) {
2637 struct AuthenticationInformation *o =
2638 &old_blob.current.array[i];
2639 struct AuthenticationInformation *p =
2640 &new_blob.previous.array[i];
2643 new_blob.previous.count++;
2645 for (; i < new_blob.count; i++) {
2646 struct AuthenticationInformation *pi =
2647 &new_blob.previous.array[i];
2651 * new_blob.previous is still empty so
2652 * we'll do new_blob.previous = new_blob.current
2658 pi->LastUpdateTime = now;
2659 pi->AuthType = TRUST_AUTH_TYPE_NONE;
2660 new_blob.previous.count++;
2663 for (i = 0; i < new_blob.count; i++) {
2664 struct AuthenticationInformation *ci =
2665 &new_blob.current.array[i];
2667 ci->LastUpdateTime = now;
2670 if (ntNewHash != NULL) {
2671 ci->AuthType = TRUST_AUTH_TYPE_NT4OWF;
2672 ci->AuthInfo.nt4owf.password = *ntNewHash;
2676 ci->AuthType = TRUST_AUTH_TYPE_CLEAR;
2677 ci->AuthInfo.clear.size = new_password->length;
2678 ci->AuthInfo.clear.password = new_password->data;
2681 ci->AuthType = TRUST_AUTH_TYPE_VERSION;
2682 ci->AuthInfo.version.version = *new_version;
2685 ci->AuthType = TRUST_AUTH_TYPE_NONE;
2689 new_blob.current.count++;
2692 if (new_blob.previous.count == 0) {
2693 TALLOC_FREE(new_blob.previous.array);
2694 new_blob.previous = new_blob.current;
2697 ndr_err = ndr_push_struct_blob(&new_val, frame, &new_blob,
2698 (ndr_push_flags_fn_t)ndr_push_trustAuthInOutBlob);
2699 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2700 ldb_transaction_cancel(ldb);
2701 DEBUG(1, ("samdb_set_password_sid: "
2702 "failed(%s) to generate "
2703 "trustAuthOutgoing sAMAccountName[%s] "
2705 "returning UNSUCCESSFUL\n",
2706 ndr_map_error2string(ndr_err),
2708 ldb_dn_get_linearized(user_msg->dn),
2709 ldb_dn_get_linearized(tdo_msg->dn)));
2711 return NT_STATUS_UNSUCCESSFUL;
2714 tdo_msg->num_elements = 0;
2715 TALLOC_FREE(tdo_msg->elements);
2717 ret = ldb_msg_add_empty(tdo_msg, "trustAuthIncoming",
2718 LDB_FLAG_MOD_REPLACE, NULL);
2719 if (ret != LDB_SUCCESS) {
2720 ldb_transaction_cancel(ldb);
2722 return NT_STATUS_NO_MEMORY;
2724 ret = ldb_msg_add_value(tdo_msg, "trustAuthIncoming",
2726 if (ret != LDB_SUCCESS) {
2727 ldb_transaction_cancel(ldb);
2729 return NT_STATUS_NO_MEMORY;
2732 ret = ldb_modify(ldb, tdo_msg);
2733 if (ret != LDB_SUCCESS) {
2734 nt_status = dsdb_ldb_err_to_ntstatus(ret);
2735 ldb_transaction_cancel(ldb);
2736 DEBUG(1, ("samdb_set_password_sid: "
2737 "failed to replace "
2738 "trustAuthOutgoing sAMAccountName[%s] "
2742 ldb_dn_get_linearized(user_msg->dn),
2743 ldb_dn_get_linearized(tdo_msg->dn),
2744 nt_errstr(nt_status), ldb_errstring(ldb)));
2750 nt_status = samdb_set_password_internal(ldb, mem_ctx,
2753 lmNewHash, ntNewHash,
2754 lmOldHash, ntOldHash,
2755 reject_reason, _dominfo,
2756 true); /* permit trusts */
2757 if (!NT_STATUS_IS_OK(nt_status)) {
2758 ldb_transaction_cancel(ldb);
2763 ret = ldb_transaction_commit(ldb);
2764 if (ret != LDB_SUCCESS) {
2765 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2766 ldb_dn_get_linearized(user_msg->dn),
2767 ldb_errstring(ldb)));
2769 return NT_STATUS_TRANSACTION_ABORTED;
2773 return NT_STATUS_OK;
2777 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2778 struct dom_sid *sid, struct ldb_dn **ret_dn)
2780 struct ldb_message *msg;
2781 struct ldb_dn *basedn;
2785 sidstr = dom_sid_string(mem_ctx, sid);
2786 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2788 /* We might have to create a ForeignSecurityPrincipal, even if this user
2789 * is in our own domain */
2791 msg = ldb_msg_new(sidstr);
2793 talloc_free(sidstr);
2794 return NT_STATUS_NO_MEMORY;
2797 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2798 ldb_get_default_basedn(sam_ctx),
2799 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2801 if (ret != LDB_SUCCESS) {
2802 DEBUG(0, ("Failed to find DN for "
2803 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2804 talloc_free(sidstr);
2805 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2808 /* add core elements to the ldb_message for the alias */
2810 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2811 talloc_free(sidstr);
2812 return NT_STATUS_NO_MEMORY;
2815 ret = ldb_msg_add_string(msg, "objectClass",
2816 "foreignSecurityPrincipal");
2817 if (ret != LDB_SUCCESS) {
2818 talloc_free(sidstr);
2819 return NT_STATUS_NO_MEMORY;
2822 /* create the alias */
2823 ret = ldb_add(sam_ctx, msg);
2824 if (ret != LDB_SUCCESS) {
2825 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2827 ldb_dn_get_linearized(msg->dn),
2828 ldb_errstring(sam_ctx)));
2829 talloc_free(sidstr);
2830 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2833 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2834 talloc_free(sidstr);
2836 return NT_STATUS_OK;
2841 Find the DN of a domain, assuming it to be a dotted.dns name
2844 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2847 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2848 const char *binary_encoded;
2849 const char * const *split_realm;
2856 split_realm = (const char * const *)str_list_make(tmp_ctx, dns_domain, ".");
2858 talloc_free(tmp_ctx);
2861 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2862 for (i=0; split_realm[i]; i++) {
2863 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2864 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2865 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2866 binary_encoded, ldb_dn_get_linearized(dn)));
2867 talloc_free(tmp_ctx);
2871 if (!ldb_dn_validate(dn)) {
2872 DEBUG(2, ("Failed to validated DN %s\n",
2873 ldb_dn_get_linearized(dn)));
2874 talloc_free(tmp_ctx);
2877 talloc_free(tmp_ctx);
2883 Find the DNS equivalent of a DN, in dotted DNS form
2885 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2887 int i, num_components = ldb_dn_get_comp_num(dn);
2888 char *dns_name = talloc_strdup(mem_ctx, "");
2889 if (dns_name == NULL) {
2893 for (i=0; i<num_components; i++) {
2894 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2897 talloc_free(dns_name);
2900 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2901 (int)v->length, (int)v->length, (char *)v->data);
2903 talloc_free(dns_name);
2909 /* remove the last '.' */
2910 if (dns_name[0] != 0) {
2911 dns_name[strlen(dns_name)-1] = 0;
2918 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2919 name is based on the forest DNS name
2921 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
2922 TALLOC_CTX *mem_ctx,
2923 const struct GUID *ntds_guid)
2925 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2926 const char *guid_str;
2927 struct ldb_dn *forest_dn;
2928 const char *dnsforest;
2931 guid_str = GUID_string(tmp_ctx, ntds_guid);
2932 if (guid_str == NULL) {
2933 talloc_free(tmp_ctx);
2936 forest_dn = ldb_get_root_basedn(samdb);
2937 if (forest_dn == NULL) {
2938 talloc_free(tmp_ctx);
2941 dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
2942 if (dnsforest == NULL) {
2943 talloc_free(tmp_ctx);
2946 ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
2947 talloc_free(tmp_ctx);
2953 Find the DN of a domain, be it the netbios or DNS name
2955 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2956 const char *domain_name)
2958 const char * const domain_ref_attrs[] = {
2961 const char * const domain_ref2_attrs[] = {
2964 struct ldb_result *res_domain_ref;
2965 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2966 /* find the domain's DN */
2967 int ret_domain = ldb_search(ldb, mem_ctx,
2969 samdb_partitions_dn(ldb, mem_ctx),
2972 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2974 if (ret_domain != LDB_SUCCESS) {
2978 if (res_domain_ref->count == 0) {
2979 ret_domain = ldb_search(ldb, mem_ctx,
2981 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2984 "(objectclass=domain)");
2985 if (ret_domain != LDB_SUCCESS) {
2989 if (res_domain_ref->count == 1) {
2990 return res_domain_ref->msgs[0]->dn;
2995 if (res_domain_ref->count > 1) {
2996 DEBUG(0,("Found %d records matching domain [%s]\n",
2997 ret_domain, domain_name));
3001 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
3007 use a GUID to find a DN
3009 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
3010 TALLOC_CTX *mem_ctx,
3011 const struct GUID *guid,
3012 uint32_t dsdb_flags,
3016 struct ldb_result *res;
3017 const char *attrs[] = { NULL };
3018 char *guid_str = GUID_string(mem_ctx, guid);
3021 return ldb_operr(ldb);
3024 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3025 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3026 DSDB_SEARCH_SHOW_EXTENDED_DN |
3027 DSDB_SEARCH_ONE_ONLY | dsdb_flags,
3028 "objectGUID=%s", guid_str);
3029 talloc_free(guid_str);
3030 if (ret != LDB_SUCCESS) {
3034 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3041 use a DN to find a GUID with a given attribute name
3043 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
3044 struct ldb_dn *dn, const char *attribute,
3048 struct ldb_result *res;
3049 const char *attrs[2];
3050 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3052 attrs[0] = attribute;
3055 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3056 DSDB_SEARCH_SHOW_DELETED |
3057 DSDB_SEARCH_SHOW_RECYCLED);
3058 if (ret != LDB_SUCCESS) {
3059 talloc_free(tmp_ctx);
3062 if (res->count < 1) {
3063 talloc_free(tmp_ctx);
3064 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3066 *guid = samdb_result_guid(res->msgs[0], attribute);
3067 talloc_free(tmp_ctx);
3072 use a DN to find a GUID
3074 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
3075 struct ldb_dn *dn, struct GUID *guid)
3077 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
3083 adds the given GUID to the given ldb_message. This value is added
3084 for the given attr_name (may be either "objectGUID" or "parentGUID").
3086 int dsdb_msg_add_guid(struct ldb_message *msg,
3088 const char *attr_name)
3093 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
3095 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
3096 if (!NT_STATUS_IS_OK(status)) {
3097 ret = LDB_ERR_OPERATIONS_ERROR;
3101 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
3102 if (ret != LDB_SUCCESS) {
3103 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
3111 talloc_free(tmp_ctx);
3118 use a DN to find a SID
3120 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
3121 struct ldb_dn *dn, struct dom_sid *sid)
3124 struct ldb_result *res;
3125 const char *attrs[] = { "objectSid", NULL };
3126 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3131 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3132 DSDB_SEARCH_SHOW_DELETED |
3133 DSDB_SEARCH_SHOW_RECYCLED);
3134 if (ret != LDB_SUCCESS) {
3135 talloc_free(tmp_ctx);
3138 if (res->count < 1) {
3139 talloc_free(tmp_ctx);
3140 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3142 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
3144 talloc_free(tmp_ctx);
3145 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3148 talloc_free(tmp_ctx);
3153 use a SID to find a DN
3155 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
3156 TALLOC_CTX *mem_ctx,
3157 struct dom_sid *sid, struct ldb_dn **dn)
3160 struct ldb_result *res;
3161 const char *attrs[] = { NULL };
3162 char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
3165 return ldb_operr(ldb);
3168 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3169 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3170 DSDB_SEARCH_SHOW_EXTENDED_DN |
3171 DSDB_SEARCH_ONE_ONLY,
3172 "objectSid=%s", sid_str);
3173 talloc_free(sid_str);
3174 if (ret != LDB_SUCCESS) {
3178 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3185 load a repsFromTo blob list for a given partition GUID
3186 attr must be "repsFrom" or "repsTo"
3188 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3189 const char *attr, struct repsFromToBlob **r, uint32_t *count)
3191 const char *attrs[] = { attr, NULL };
3192 struct ldb_result *res = NULL;
3193 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3195 struct ldb_message_element *el;
3201 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
3202 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3203 /* partition hasn't been replicated yet */
3206 if (ret != LDB_SUCCESS) {
3207 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
3208 talloc_free(tmp_ctx);
3209 return WERR_DS_DRA_INTERNAL_ERROR;
3212 el = ldb_msg_find_element(res->msgs[0], attr);
3214 /* it's OK to be empty */
3215 talloc_free(tmp_ctx);
3219 *count = el->num_values;
3220 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
3222 talloc_free(tmp_ctx);
3223 return WERR_DS_DRA_INTERNAL_ERROR;
3226 for (i=0; i<(*count); i++) {
3227 enum ndr_err_code ndr_err;
3228 ndr_err = ndr_pull_struct_blob(&el->values[i],
3231 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
3232 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3233 talloc_free(tmp_ctx);
3234 return WERR_DS_DRA_INTERNAL_ERROR;
3238 talloc_free(tmp_ctx);
3244 save the repsFromTo blob list for a given partition GUID
3245 attr must be "repsFrom" or "repsTo"
3247 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3248 const char *attr, struct repsFromToBlob *r, uint32_t count)
3250 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3251 struct ldb_message *msg;
3252 struct ldb_message_element *el;
3255 msg = ldb_msg_new(tmp_ctx);
3257 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
3261 el->values = talloc_array(msg, struct ldb_val, count);
3266 for (i=0; i<count; i++) {
3268 enum ndr_err_code ndr_err;
3270 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
3272 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
3273 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3281 if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
3282 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
3286 talloc_free(tmp_ctx);
3291 talloc_free(tmp_ctx);
3292 return WERR_DS_DRA_INTERNAL_ERROR;
3297 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
3298 object for a partition
3300 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
3301 uint64_t *uSN, uint64_t *urgent_uSN)
3303 struct ldb_request *req;
3305 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3306 struct dsdb_control_current_partition *p_ctrl;
3307 struct ldb_result *res;
3309 res = talloc_zero(tmp_ctx, struct ldb_result);
3311 talloc_free(tmp_ctx);
3312 return ldb_oom(ldb);
3315 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3316 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
3320 res, ldb_search_default_callback,
3322 if (ret != LDB_SUCCESS) {
3323 talloc_free(tmp_ctx);
3327 p_ctrl = talloc(req, struct dsdb_control_current_partition);
3328 if (p_ctrl == NULL) {
3329 talloc_free(tmp_ctx);
3330 return ldb_oom(ldb);
3332 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
3335 ret = ldb_request_add_control(req,
3336 DSDB_CONTROL_CURRENT_PARTITION_OID,
3338 if (ret != LDB_SUCCESS) {
3339 talloc_free(tmp_ctx);
3343 /* Run the new request */
3344 ret = ldb_request(ldb, req);
3346 if (ret == LDB_SUCCESS) {
3347 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3350 if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
3351 /* it hasn't been created yet, which means
3352 an implicit value of zero */
3354 talloc_free(tmp_ctx);
3358 if (ret != LDB_SUCCESS) {
3359 talloc_free(tmp_ctx);
3363 if (res->count < 1) {
3369 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
3371 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
3375 talloc_free(tmp_ctx);
3380 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
3381 const struct drsuapi_DsReplicaCursor2 *c2)
3383 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3386 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
3387 const struct drsuapi_DsReplicaCursor *c2)
3389 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3394 see if a computer identified by its invocationId is a RODC
3396 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
3398 /* 1) find the DN for this servers NTDSDSA object
3399 2) search for the msDS-isRODC attribute
3400 3) if not present then not a RODC
3401 4) if present and TRUE then is a RODC
3403 struct ldb_dn *config_dn;
3404 const char *attrs[] = { "msDS-isRODC", NULL };
3406 struct ldb_result *res;
3407 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
3409 config_dn = ldb_get_config_basedn(sam_ctx);
3411 talloc_free(tmp_ctx);
3412 return ldb_operr(sam_ctx);
3415 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
3416 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
3418 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3420 talloc_free(tmp_ctx);
3424 if (ret != LDB_SUCCESS) {
3425 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
3426 GUID_string(tmp_ctx, objectGUID)));
3428 talloc_free(tmp_ctx);
3432 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
3433 *is_rodc = (ret == 1);
3435 talloc_free(tmp_ctx);
3441 see if we are a RODC
3443 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
3445 const struct GUID *objectGUID;
3449 /* see if we have a cached copy */
3450 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
3456 objectGUID = samdb_ntds_objectGUID(sam_ctx);
3458 return ldb_operr(sam_ctx);
3461 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
3462 if (ret != LDB_SUCCESS) {
3466 cached = talloc(sam_ctx, bool);
3467 if (cached == NULL) {
3468 return ldb_oom(sam_ctx);
3472 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
3473 if (ret != LDB_SUCCESS) {
3474 talloc_free(cached);
3475 return ldb_operr(sam_ctx);
3481 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
3483 TALLOC_CTX *tmp_ctx;
3486 tmp_ctx = talloc_new(ldb);
3487 if (tmp_ctx == NULL) {
3491 cached = talloc(tmp_ctx, bool);
3497 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
3501 talloc_steal(ldb, cached);
3502 talloc_free(tmp_ctx);
3506 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
3507 talloc_free(tmp_ctx);
3513 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
3514 * flags are DS_NTDSSETTINGS_OPT_*
3516 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
3520 TALLOC_CTX *tmp_ctx;
3521 struct ldb_result *res;
3522 struct ldb_dn *site_dn;
3523 const char *attrs[] = { "options", NULL };
3525 tmp_ctx = talloc_new(ldb_ctx);
3526 if (tmp_ctx == NULL)
3529 /* Retrieve the site dn for the ldb that we
3530 * have open. This is our local site.
3532 site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
3533 if (site_dn == NULL)
3536 /* Perform a one level (child) search from the local
3537 * site distinguided name. We're looking for the
3538 * "options" attribute within the nTDSSiteSettings
3541 rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
3542 LDB_SCOPE_ONELEVEL, attrs,
3543 "objectClass=nTDSSiteSettings");
3545 if (rc != LDB_SUCCESS || res->count != 1)
3548 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3550 talloc_free(tmp_ctx);
3555 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3556 talloc_free(tmp_ctx);
3557 return ldb_error(ldb_ctx, LDB_ERR_NO_SUCH_OBJECT, __func__);
3561 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3563 flags are DS_NTDS_OPTION_*
3565 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
3567 TALLOC_CTX *tmp_ctx;
3568 const char *attrs[] = { "options", NULL };
3570 struct ldb_result *res;
3572 tmp_ctx = talloc_new(ldb);
3573 if (tmp_ctx == NULL) {
3577 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3578 if (ret != LDB_SUCCESS) {
3582 if (res->count != 1) {
3586 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3588 talloc_free(tmp_ctx);
3593 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3594 talloc_free(tmp_ctx);
3595 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3598 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3600 const char *attrs[] = { "objectCategory", NULL };
3602 struct ldb_result *res;
3604 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3605 if (ret != LDB_SUCCESS) {
3609 if (res->count != 1) {
3613 return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3616 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3621 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3622 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3624 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3626 char **tokens, *ret;
3629 tokens = str_list_make(mem_ctx, cn, " -_");
3630 if (tokens == NULL || tokens[0] == NULL) {
3634 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3635 tokens[0][0] = tolower(tokens[0][0]);
3636 for (i = 1; tokens[i] != NULL; i++)
3637 tokens[i][0] = toupper(tokens[i][0]);
3639 ret = talloc_strdup(mem_ctx, tokens[0]);
3640 for (i = 1; tokens[i] != NULL; i++)
3641 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3643 talloc_free(tokens);
3649 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3651 int dsdb_functional_level(struct ldb_context *ldb)
3653 int *domainFunctionality =
3654 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3655 if (!domainFunctionality) {
3656 /* this is expected during initial provision */
3657 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3658 return DS_DOMAIN_FUNCTION_2000;
3660 return *domainFunctionality;
3664 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3666 int dsdb_forest_functional_level(struct ldb_context *ldb)
3668 int *forestFunctionality =
3669 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3670 if (!forestFunctionality) {
3671 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3672 return DS_DOMAIN_FUNCTION_2000;
3674 return *forestFunctionality;
3678 set a GUID in an extended DN structure
3680 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3686 status = GUID_to_ndr_blob(guid, dn, &v);
3687 if (!NT_STATUS_IS_OK(status)) {
3688 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3691 ret = ldb_dn_set_extended_component(dn, component_name, &v);
3697 return a GUID from a extended DN structure
3699 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3701 const struct ldb_val *v;
3703 v = ldb_dn_get_extended_component(dn, component_name);
3705 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3708 return GUID_from_ndr_blob(v, guid);
3712 return a uint64_t from a extended DN structure
3714 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3716 const struct ldb_val *v;
3718 v = ldb_dn_get_extended_component(dn, component_name);
3720 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3724 char s[v->length+1];
3725 memcpy(s, v->data, v->length);
3728 *val = strtoull(s, NULL, 0);
3730 return NT_STATUS_OK;
3734 return a NTTIME from a extended DN structure
3736 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3738 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3742 return a uint32_t from a extended DN structure
3744 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3746 const struct ldb_val *v;
3748 v = ldb_dn_get_extended_component(dn, component_name);
3750 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3754 char s[v->length + 1];
3755 memcpy(s, v->data, v->length);
3757 *val = strtoul(s, NULL, 0);
3760 return NT_STATUS_OK;
3764 return a dom_sid from a extended DN structure
3766 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3768 const struct ldb_val *sid_blob;
3769 enum ndr_err_code ndr_err;
3771 sid_blob = ldb_dn_get_extended_component(dn, component_name);
3773 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3776 ndr_err = ndr_pull_struct_blob_all_noalloc(sid_blob, sid,
3777 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3778 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3779 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3783 return NT_STATUS_OK;
3788 return RMD_FLAGS directly from a ldb_dn
3789 returns 0 if not found
3791 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3793 const struct ldb_val *v;
3795 v = ldb_dn_get_extended_component(dn, "RMD_FLAGS");
3796 if (!v || v->length > sizeof(buf)-1) return 0;
3797 strncpy(buf, (const char *)v->data, v->length);
3799 return strtoul(buf, NULL, 10);
3803 return RMD_FLAGS directly from a ldb_val for a DN
3804 returns 0 if RMD_FLAGS is not found
3806 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3812 if (val->length < 13) {
3815 p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3819 flags = strtoul(p+11, &end, 10);
3820 if (!end || *end != '>') {
3821 /* it must end in a > */
3828 return true if a ldb_val containing a DN in storage form is deleted
3830 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3832 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3836 return true if a ldb_val containing a DN in storage form is
3837 in the upgraded w2k3 linked attribute format
3839 bool dsdb_dn_is_upgraded_link_val(const struct ldb_val *val)
3841 return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
3845 return a DN for a wellknown GUID
3847 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3848 struct ldb_dn *nc_root, const char *wk_guid,
3849 struct ldb_dn **wkguid_dn)
3851 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3852 const char *attrs[] = { NULL };
3855 struct ldb_result *res;
3857 /* construct the magic WKGUID DN */
3858 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3859 wk_guid, ldb_dn_get_linearized(nc_root));
3861 talloc_free(tmp_ctx);
3862 return ldb_operr(samdb);
3865 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
3866 DSDB_SEARCH_SHOW_DELETED |
3867 DSDB_SEARCH_SHOW_RECYCLED);
3868 if (ret != LDB_SUCCESS) {
3869 talloc_free(tmp_ctx);
3873 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
3874 talloc_free(tmp_ctx);
3879 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
3881 return ldb_dn_compare(*dn1, *dn2);
3885 find a NC root given a DN within the NC
3887 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3888 struct ldb_dn **nc_root)
3890 const char *root_attrs[] = { "namingContexts", NULL };
3891 TALLOC_CTX *tmp_ctx;
3893 struct ldb_message_element *el;
3894 struct ldb_result *root_res;
3896 struct ldb_dn **nc_dns;
3898 tmp_ctx = talloc_new(samdb);
3899 if (tmp_ctx == NULL) {
3900 return ldb_oom(samdb);
3903 ret = ldb_search(samdb, tmp_ctx, &root_res,
3904 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
3905 if (ret != LDB_SUCCESS || root_res->count == 0) {
3906 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
3907 talloc_free(tmp_ctx);
3911 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
3912 if ((el == NULL) || (el->num_values < 3)) {
3913 struct ldb_message *tmp_msg;
3915 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list."));
3917 /* This generates a temporary list of NCs in order to let the
3918 * provisioning work. */
3919 tmp_msg = ldb_msg_new(tmp_ctx);
3920 if (tmp_msg == NULL) {
3921 talloc_free(tmp_ctx);
3922 return ldb_oom(samdb);
3924 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3925 ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
3926 if (ret != LDB_SUCCESS) {
3927 talloc_free(tmp_ctx);
3930 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3931 ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
3932 if (ret != LDB_SUCCESS) {
3933 talloc_free(tmp_ctx);
3936 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3937 ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
3938 if (ret != LDB_SUCCESS) {
3939 talloc_free(tmp_ctx);
3942 el = &tmp_msg->elements[0];
3945 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
3947 talloc_free(tmp_ctx);
3948 return ldb_oom(samdb);
3951 for (i=0; i<el->num_values; i++) {
3952 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
3953 if (nc_dns[i] == NULL) {
3954 talloc_free(tmp_ctx);
3955 return ldb_operr(samdb);
3959 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
3961 for (i=0; i<el->num_values; i++) {
3962 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
3963 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
3964 talloc_free(tmp_ctx);
3969 talloc_free(tmp_ctx);
3970 return ldb_error(samdb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3975 find the deleted objects DN for any object, by looking for the NC
3976 root, then looking up the wellknown GUID
3978 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
3979 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
3980 struct ldb_dn **do_dn)
3982 struct ldb_dn *nc_root;
3985 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
3986 if (ret != LDB_SUCCESS) {
3990 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
3991 talloc_free(nc_root);
3996 return the tombstoneLifetime, in days
3998 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
4001 dn = ldb_get_config_basedn(ldb);
4003 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4005 dn = ldb_dn_copy(ldb, dn);
4007 return ldb_operr(ldb);
4009 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
4010 be a wellknown GUID for this */
4011 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
4013 return ldb_operr(ldb);
4016 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
4022 compare a ldb_val to a string case insensitively
4024 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
4026 size_t len = strlen(s);
4028 if (len > v->length) return 1;
4029 ret = strncasecmp(s, (const char *)v->data, v->length);
4030 if (ret != 0) return ret;
4031 if (v->length > len && v->data[len] != 0) {
4039 load the UDV for a partition in v2 format
4040 The list is returned sorted, and with our local cursor added
4042 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4043 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
4045 static const char *attrs[] = { "replUpToDateVector", NULL };
4046 struct ldb_result *r;
4047 const struct ldb_val *ouv_value;
4050 uint64_t highest_usn = 0;
4051 const struct GUID *our_invocation_id;
4052 static const struct timeval tv1970;
4053 NTTIME nt1970 = timeval_to_nttime(&tv1970);
4055 ret = dsdb_search_dn(samdb, mem_ctx, &r, dn, attrs, DSDB_SEARCH_SHOW_RECYCLED|DSDB_SEARCH_SHOW_DELETED);
4056 if (ret != LDB_SUCCESS) {
4060 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
4062 enum ndr_err_code ndr_err;
4063 struct replUpToDateVectorBlob ouv;
4065 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
4066 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
4067 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
4069 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4071 if (ouv.version != 2) {
4072 /* we always store as version 2, and
4073 * replUpToDateVector is not replicated
4075 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4078 *count = ouv.ctr.ctr2.count;
4079 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
4087 our_invocation_id = samdb_ntds_invocation_id(samdb);
4088 if (!our_invocation_id) {
4089 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
4090 talloc_free(*cursors);
4091 return ldb_operr(samdb);
4094 ret = ldb_sequence_number(samdb, LDB_SEQ_HIGHEST_SEQ, &highest_usn);
4095 if (ret != LDB_SUCCESS) {
4096 /* nothing to add - this can happen after a vampire */
4097 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4101 for (i=0; i<*count; i++) {
4102 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
4103 (*cursors)[i].highest_usn = highest_usn;
4104 (*cursors)[i].last_sync_success = nt1970;
4105 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4110 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
4112 return ldb_oom(samdb);
4115 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
4116 (*cursors)[*count].highest_usn = highest_usn;
4117 (*cursors)[*count].last_sync_success = nt1970;
4120 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4126 load the UDV for a partition in version 1 format
4127 The list is returned sorted, and with our local cursor added
4129 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4130 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
4132 struct drsuapi_DsReplicaCursor2 *v2;
4136 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
4137 if (ret != LDB_SUCCESS) {
4147 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
4148 if (*cursors == NULL) {
4150 return ldb_oom(samdb);
4153 for (i=0; i<*count; i++) {
4154 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
4155 (*cursors)[i].highest_usn = v2[i].highest_usn;
4162 add a set of controls to a ldb_request structure based on a set of
4163 flags. See util.h for a list of available flags
4165 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
4168 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
4169 struct ldb_search_options_control *options;
4170 /* Using the phantom root control allows us to search all partitions */
4171 options = talloc(req, struct ldb_search_options_control);
4172 if (options == NULL) {
4173 return LDB_ERR_OPERATIONS_ERROR;
4175 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
4177 ret = ldb_request_add_control(req,
4178 LDB_CONTROL_SEARCH_OPTIONS_OID,
4180 if (ret != LDB_SUCCESS) {
4185 if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
4186 ret = ldb_request_add_control(req,
4187 DSDB_CONTROL_NO_GLOBAL_CATALOG,
4189 if (ret != LDB_SUCCESS) {
4194 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
4195 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
4196 if (ret != LDB_SUCCESS) {
4201 if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
4202 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
4203 if (ret != LDB_SUCCESS) {
4208 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
4209 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
4210 if (ret != LDB_SUCCESS) {
4215 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
4216 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
4217 if (!extended_ctrl) {
4218 return LDB_ERR_OPERATIONS_ERROR;
4220 extended_ctrl->type = 1;
4222 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
4223 if (ret != LDB_SUCCESS) {
4228 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
4229 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
4230 if (ret != LDB_SUCCESS) {
4235 if (dsdb_flags & DSDB_MODIFY_RELAX) {
4236 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
4237 if (ret != LDB_SUCCESS) {
4242 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
4243 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
4244 if (ret != LDB_SUCCESS) {
4249 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
4250 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
4251 if (ret != LDB_SUCCESS) {
4256 if (dsdb_flags & DSDB_TREE_DELETE) {
4257 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
4258 if (ret != LDB_SUCCESS) {
4263 if (dsdb_flags & DSDB_PROVISION) {
4264 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
4265 if (ret != LDB_SUCCESS) {
4270 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
4271 if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
4272 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
4273 if (ret != LDB_SUCCESS) {
4278 if (dsdb_flags & DSDB_PASSWORD_BYPASS_LAST_SET) {
4280 * This must not be critical, as it will only be
4281 * handled (and need to be handled) if the other
4282 * attributes in the request bring password_hash into
4285 ret = ldb_request_add_control(req, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID, false, NULL);
4286 if (ret != LDB_SUCCESS) {
4291 if (dsdb_flags & DSDB_REPLMD_VANISH_LINKS) {
4292 ret = ldb_request_add_control(req, DSDB_CONTROL_REPLMD_VANISH_LINKS, true, NULL);
4293 if (ret != LDB_SUCCESS) {
4298 if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
4299 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
4300 if (ret != LDB_SUCCESS) {
4309 an add with a set of controls
4311 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
4312 uint32_t dsdb_flags)
4314 struct ldb_request *req;
4317 ret = ldb_build_add_req(&req, ldb, ldb,
4321 ldb_op_default_callback,
4324 if (ret != LDB_SUCCESS) return ret;
4326 ret = dsdb_request_add_controls(req, dsdb_flags);
4327 if (ret != LDB_SUCCESS) {
4332 ret = dsdb_autotransaction_request(ldb, req);
4339 a modify with a set of controls
4341 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
4342 uint32_t dsdb_flags)
4344 struct ldb_request *req;
4347 ret = ldb_build_mod_req(&req, ldb, ldb,
4351 ldb_op_default_callback,
4354 if (ret != LDB_SUCCESS) return ret;
4356 ret = dsdb_request_add_controls(req, dsdb_flags);
4357 if (ret != LDB_SUCCESS) {
4362 ret = dsdb_autotransaction_request(ldb, req);
4369 a delete with a set of flags
4371 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
4372 uint32_t dsdb_flags)
4374 struct ldb_request *req;
4377 ret = ldb_build_del_req(&req, ldb, ldb,
4381 ldb_op_default_callback,
4384 if (ret != LDB_SUCCESS) return ret;
4386 ret = dsdb_request_add_controls(req, dsdb_flags);
4387 if (ret != LDB_SUCCESS) {
4392 ret = dsdb_autotransaction_request(ldb, req);
4399 like dsdb_modify() but set all the element flags to
4400 LDB_FLAG_MOD_REPLACE
4402 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
4406 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
4407 for (i=0;i<msg->num_elements;i++) {
4408 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
4411 return dsdb_modify(ldb, msg, dsdb_flags);
4416 search for attrs on one DN, allowing for dsdb_flags controls
4418 int dsdb_search_dn(struct ldb_context *ldb,
4419 TALLOC_CTX *mem_ctx,
4420 struct ldb_result **_result,
4421 struct ldb_dn *basedn,
4422 const char * const *attrs,
4423 uint32_t dsdb_flags)
4426 struct ldb_request *req;
4427 struct ldb_result *res;
4429 res = talloc_zero(mem_ctx, struct ldb_result);
4431 return ldb_oom(ldb);
4434 ret = ldb_build_search_req(&req, ldb, res,
4441 ldb_search_default_callback,
4443 if (ret != LDB_SUCCESS) {
4448 ret = dsdb_request_add_controls(req, dsdb_flags);
4449 if (ret != LDB_SUCCESS) {
4454 ret = ldb_request(ldb, req);
4455 if (ret == LDB_SUCCESS) {
4456 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4460 if (ret != LDB_SUCCESS) {
4470 search for attrs on one DN, by the GUID of the DN, allowing for
4473 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
4474 TALLOC_CTX *mem_ctx,
4475 struct ldb_result **_result,
4476 const struct GUID *guid,
4477 const char * const *attrs,
4478 uint32_t dsdb_flags)
4480 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4484 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
4486 talloc_free(tmp_ctx);
4487 return ldb_oom(ldb);
4490 ret = dsdb_search_dn(ldb, mem_ctx, _result, dn, attrs, dsdb_flags);
4491 talloc_free(tmp_ctx);
4496 general search with dsdb_flags for controls
4498 int dsdb_search(struct ldb_context *ldb,
4499 TALLOC_CTX *mem_ctx,
4500 struct ldb_result **_result,
4501 struct ldb_dn *basedn,
4502 enum ldb_scope scope,
4503 const char * const *attrs,
4504 uint32_t dsdb_flags,
4505 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4508 struct ldb_request *req;
4509 struct ldb_result *res;
4511 char *expression = NULL;
4512 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4514 /* cross-partitions searches with a basedn break multi-domain support */
4515 SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
4517 res = talloc_zero(tmp_ctx, struct ldb_result);
4519 talloc_free(tmp_ctx);
4520 return ldb_oom(ldb);
4524 va_start(ap, exp_fmt);
4525 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4529 talloc_free(tmp_ctx);
4530 return ldb_oom(ldb);
4534 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
4541 ldb_search_default_callback,
4543 if (ret != LDB_SUCCESS) {
4544 talloc_free(tmp_ctx);
4548 ret = dsdb_request_add_controls(req, dsdb_flags);
4549 if (ret != LDB_SUCCESS) {
4550 talloc_free(tmp_ctx);
4551 ldb_reset_err_string(ldb);
4555 ret = ldb_request(ldb, req);
4556 if (ret == LDB_SUCCESS) {
4557 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4560 if (ret != LDB_SUCCESS) {
4561 talloc_free(tmp_ctx);
4565 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
4566 if (res->count == 0) {
4567 talloc_free(tmp_ctx);
4568 ldb_reset_err_string(ldb);
4569 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4571 if (res->count != 1) {
4572 talloc_free(tmp_ctx);
4573 ldb_reset_err_string(ldb);
4574 return LDB_ERR_CONSTRAINT_VIOLATION;
4578 *_result = talloc_steal(mem_ctx, res);
4579 talloc_free(tmp_ctx);
4586 general search with dsdb_flags for controls
4587 returns exactly 1 record or an error
4589 int dsdb_search_one(struct ldb_context *ldb,
4590 TALLOC_CTX *mem_ctx,
4591 struct ldb_message **msg,
4592 struct ldb_dn *basedn,
4593 enum ldb_scope scope,
4594 const char * const *attrs,
4595 uint32_t dsdb_flags,
4596 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4599 struct ldb_result *res;
4601 char *expression = NULL;
4602 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4604 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4606 res = talloc_zero(tmp_ctx, struct ldb_result);
4608 talloc_free(tmp_ctx);
4609 return ldb_oom(ldb);
4613 va_start(ap, exp_fmt);
4614 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4618 talloc_free(tmp_ctx);
4619 return ldb_oom(ldb);
4621 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4622 dsdb_flags, "%s", expression);
4624 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4628 if (ret != LDB_SUCCESS) {
4629 talloc_free(tmp_ctx);
4633 *msg = talloc_steal(mem_ctx, res->msgs[0]);
4634 talloc_free(tmp_ctx);
4639 /* returns back the forest DNS name */
4640 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4642 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4643 ldb_get_root_basedn(ldb));
4646 if (forest_name == NULL) {
4650 p = strchr(forest_name, '/');
4658 /* returns back the default domain DNS name */
4659 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4661 const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4662 ldb_get_default_basedn(ldb));
4665 if (domain_name == NULL) {
4669 p = strchr(domain_name, '/');
4678 validate that an DSA GUID belongs to the specified user sid.
4679 The user SID must be a domain controller account (either RODC or
4682 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4683 const struct GUID *dsa_guid,
4684 const struct dom_sid *sid)
4687 - find DN of record with the DSA GUID in the
4688 configuration partition (objectGUID)
4689 - remove "NTDS Settings" component from DN
4690 - do a base search on that DN for serverReference with
4692 - extract objectSid from resulting serverReference
4694 - check this sid matches the sid argument
4696 struct ldb_dn *config_dn;
4697 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4698 struct ldb_message *msg;
4699 const char *attrs1[] = { NULL };
4700 const char *attrs2[] = { "serverReference", NULL };
4702 struct ldb_dn *dn, *account_dn;
4703 struct dom_sid sid2;
4706 config_dn = ldb_get_config_basedn(ldb);
4708 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4709 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4710 if (ret != LDB_SUCCESS) {
4711 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4712 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4713 talloc_free(tmp_ctx);
4714 return ldb_operr(ldb);
4718 if (!ldb_dn_remove_child_components(dn, 1)) {
4719 talloc_free(tmp_ctx);
4720 return ldb_operr(ldb);
4723 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4724 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4725 "(objectClass=server)");
4726 if (ret != LDB_SUCCESS) {
4727 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4728 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4729 talloc_free(tmp_ctx);
4730 return ldb_operr(ldb);
4733 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4734 if (account_dn == NULL) {
4735 DEBUG(1,(__location__ ": Failed to find account dn "
4736 "(serverReference) for %s, parent of DSA with "
4737 "objectGUID %s, sid %s\n",
4738 ldb_dn_get_linearized(msg->dn),
4739 GUID_string(tmp_ctx, dsa_guid),
4740 dom_sid_string(tmp_ctx, sid)));
4741 talloc_free(tmp_ctx);
4742 return ldb_operr(ldb);
4745 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4746 if (!NT_STATUS_IS_OK(status)) {
4747 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4748 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4749 talloc_free(tmp_ctx);
4750 return ldb_operr(ldb);
4753 if (!dom_sid_equal(sid, &sid2)) {
4754 /* someone is trying to spoof another account */
4755 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4756 GUID_string(tmp_ctx, dsa_guid),
4757 dom_sid_string(tmp_ctx, sid),
4758 dom_sid_string(tmp_ctx, &sid2)));
4759 talloc_free(tmp_ctx);
4760 return ldb_operr(ldb);
4763 talloc_free(tmp_ctx);
4767 static const char * const secret_attributes[] = {
4768 DSDB_SECRET_ATTRIBUTES,
4773 check if the attribute belongs to the RODC filtered attribute set
4774 Note that attributes that are in the filtered attribute set are the
4775 ones that _are_ always sent to a RODC
4777 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4779 /* they never get secret attributes */
4780 if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4784 /* they do get non-secret critical attributes */
4785 if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4789 /* they do get non-secret attributes marked as being in the FAS */
4790 if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4794 /* other attributes are denied */
4798 /* return fsmo role dn and role owner dn for a particular role*/
4799 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4800 struct ldb_context *ldb,
4802 struct ldb_dn **fsmo_role_dn,
4803 struct ldb_dn **role_owner_dn)
4807 case DREPL_NAMING_MASTER:
4808 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
4809 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4810 if (ret != LDB_SUCCESS) {
4811 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
4812 ldb_errstring(ldb)));
4813 talloc_free(tmp_ctx);
4814 return WERR_DS_DRA_INTERNAL_ERROR;
4817 case DREPL_INFRASTRUCTURE_MASTER:
4818 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
4819 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4820 if (ret != LDB_SUCCESS) {
4821 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4822 ldb_errstring(ldb)));
4823 talloc_free(tmp_ctx);
4824 return WERR_DS_DRA_INTERNAL_ERROR;
4827 case DREPL_RID_MASTER:
4828 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
4829 if (ret != LDB_SUCCESS) {
4830 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
4831 talloc_free(tmp_ctx);
4832 return WERR_DS_DRA_INTERNAL_ERROR;
4835 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4836 if (ret != LDB_SUCCESS) {
4837 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
4838 ldb_errstring(ldb)));
4839 talloc_free(tmp_ctx);
4840 return WERR_DS_DRA_INTERNAL_ERROR;
4843 case DREPL_SCHEMA_MASTER:
4844 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
4845 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4846 if (ret != LDB_SUCCESS) {
4847 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4848 ldb_errstring(ldb)));
4849 talloc_free(tmp_ctx);
4850 return WERR_DS_DRA_INTERNAL_ERROR;
4853 case DREPL_PDC_MASTER:
4854 *fsmo_role_dn = ldb_get_default_basedn(ldb);
4855 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4856 if (ret != LDB_SUCCESS) {
4857 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
4858 ldb_errstring(ldb)));
4859 talloc_free(tmp_ctx);
4860 return WERR_DS_DRA_INTERNAL_ERROR;
4864 return WERR_DS_DRA_INTERNAL_ERROR;
4869 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
4870 TALLOC_CTX *mem_ctx,
4871 struct ldb_dn *server_dn)
4874 struct ldb_result *res = NULL;
4875 const char * const attrs[] = { "dNSHostName", NULL};
4877 ldb_ret = ldb_search(ldb, mem_ctx, &res,
4881 if (ldb_ret != LDB_SUCCESS) {
4882 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
4883 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
4887 return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
4891 returns true if an attribute is in the filter,
4892 false otherwise, provided that attribute value is provided with the expression
4894 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
4898 switch (tree->operation) {
4901 for (i=0;i<tree->u.list.num_elements;i++) {
4902 if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
4908 return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
4909 case LDB_OP_EQUALITY:
4910 case LDB_OP_GREATER:
4913 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
4917 case LDB_OP_SUBSTRING:
4918 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
4922 case LDB_OP_PRESENT:
4923 /* (attrname=*) is not filtered out */
4925 case LDB_OP_EXTENDED:
4926 if (tree->u.extended.attr &&
4927 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
4935 bool is_attr_in_list(const char * const * attrs, const char *attr)
4939 for (i = 0; attrs[i]; i++) {
4940 if (ldb_attr_cmp(attrs[i], attr) == 0)
4947 int dsdb_werror_at(struct ldb_context *ldb, int ldb_ecode, WERROR werr,
4948 const char *location, const char *func,
4951 if (reason == NULL) {
4952 reason = win_errstr(werr);
4954 ldb_asprintf_errstring(ldb, "%08X: %s at %s:%s",
4955 W_ERROR_V(werr), reason, location, func);
4960 map an ldb error code to an approximate NTSTATUS code
4962 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
4966 return NT_STATUS_OK;
4968 case LDB_ERR_PROTOCOL_ERROR:
4969 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
4971 case LDB_ERR_TIME_LIMIT_EXCEEDED:
4972 return NT_STATUS_IO_TIMEOUT;
4974 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
4975 return NT_STATUS_BUFFER_TOO_SMALL;
4977 case LDB_ERR_COMPARE_FALSE:
4978 case LDB_ERR_COMPARE_TRUE:
4979 return NT_STATUS_REVISION_MISMATCH;
4981 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
4982 return NT_STATUS_NOT_SUPPORTED;
4984 case LDB_ERR_STRONG_AUTH_REQUIRED:
4985 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
4986 case LDB_ERR_SASL_BIND_IN_PROGRESS:
4987 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
4988 case LDB_ERR_INVALID_CREDENTIALS:
4989 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
4990 case LDB_ERR_UNWILLING_TO_PERFORM:
4991 return NT_STATUS_ACCESS_DENIED;
4993 case LDB_ERR_NO_SUCH_OBJECT:
4994 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4996 case LDB_ERR_REFERRAL:
4997 case LDB_ERR_NO_SUCH_ATTRIBUTE:
4998 return NT_STATUS_NOT_FOUND;
5000 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
5001 return NT_STATUS_NOT_SUPPORTED;
5003 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
5004 return NT_STATUS_BUFFER_TOO_SMALL;
5006 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
5007 case LDB_ERR_INAPPROPRIATE_MATCHING:
5008 case LDB_ERR_CONSTRAINT_VIOLATION:
5009 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
5010 case LDB_ERR_INVALID_DN_SYNTAX:
5011 case LDB_ERR_NAMING_VIOLATION:
5012 case LDB_ERR_OBJECT_CLASS_VIOLATION:
5013 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
5014 case LDB_ERR_NOT_ALLOWED_ON_RDN:
5015 return NT_STATUS_INVALID_PARAMETER;
5017 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
5018 case LDB_ERR_ENTRY_ALREADY_EXISTS:
5019 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
5022 return NT_STATUS_NETWORK_BUSY;
5024 case LDB_ERR_ALIAS_PROBLEM:
5025 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
5026 case LDB_ERR_UNAVAILABLE:
5027 case LDB_ERR_LOOP_DETECT:
5028 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
5029 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
5031 case LDB_ERR_OPERATIONS_ERROR:
5034 return NT_STATUS_UNSUCCESSFUL;
5039 create a new naming context that will hold a partial replica
5041 int dsdb_create_partial_replica_NC(struct ldb_context *ldb, struct ldb_dn *dn)
5043 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
5044 struct ldb_message *msg;
5047 msg = ldb_msg_new(tmp_ctx);
5049 talloc_free(tmp_ctx);
5050 return ldb_oom(ldb);
5054 ret = ldb_msg_add_string(msg, "objectClass", "top");
5055 if (ret != LDB_SUCCESS) {
5056 talloc_free(tmp_ctx);
5057 return ldb_oom(ldb);
5060 /* [MS-DRSR] implies that we should only add the 'top'
5061 * objectclass, but that would cause lots of problems with our
5062 * objectclass code as top is not structural, so we add
5063 * 'domainDNS' as well to keep things sane. We're expecting
5064 * this new NC to be of objectclass domainDNS after
5065 * replication anyway
5067 ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
5068 if (ret != LDB_SUCCESS) {
5069 talloc_free(tmp_ctx);
5070 return ldb_oom(ldb);
5073 ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
5074 INSTANCE_TYPE_IS_NC_HEAD|
5075 INSTANCE_TYPE_NC_ABOVE|
5076 INSTANCE_TYPE_UNINSTANT);
5077 if (ret != LDB_SUCCESS) {
5078 talloc_free(tmp_ctx);
5079 return ldb_oom(ldb);
5082 ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
5083 if (ret != LDB_SUCCESS && ret != LDB_ERR_ENTRY_ALREADY_EXISTS) {
5084 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
5085 ldb_dn_get_linearized(dn),
5086 ldb_errstring(ldb), ldb_strerror(ret)));
5087 talloc_free(tmp_ctx);
5091 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
5093 talloc_free(tmp_ctx);
5098 build a GUID from a string
5100 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
5102 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
5104 uint32_t time_mid, time_hi_and_version;
5105 uint32_t clock_seq[2];
5110 return NT_STATUS_INVALID_PARAMETER;
5113 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
5114 &time_low, &time_mid, &time_hi_and_version,
5115 &clock_seq[0], &clock_seq[1],
5116 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
5117 status = NT_STATUS_OK;
5120 if (!NT_STATUS_IS_OK(status)) {
5124 guid->time_low = time_low;
5125 guid->time_mid = time_mid;
5126 guid->time_hi_and_version = time_hi_and_version;
5127 guid->clock_seq[0] = clock_seq[0];
5128 guid->clock_seq[1] = clock_seq[1];
5130 guid->node[i] = node[i];
5133 return NT_STATUS_OK;
5136 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
5138 return talloc_asprintf(mem_ctx,
5139 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
5140 guid->time_low, guid->time_mid,
5141 guid->time_hi_and_version,
5144 guid->node[0], guid->node[1],
5145 guid->node[2], guid->node[3],
5146 guid->node[4], guid->node[5]);
5150 * Return the effective badPwdCount
5152 * This requires that the user_msg have (if present):
5156 * This also requires that the domain_msg have (if present):
5157 * - lockOutObservationWindow
5159 static int dsdb_effective_badPwdCount(const struct ldb_message *user_msg,
5160 int64_t lockOutObservationWindow,
5163 int64_t badPasswordTime;
5164 badPasswordTime = ldb_msg_find_attr_as_int64(user_msg, "badPasswordTime", 0);
5166 if (badPasswordTime - lockOutObservationWindow >= now) {
5167 return ldb_msg_find_attr_as_int(user_msg, "badPwdCount", 0);
5174 * Return the effective badPwdCount
5176 * This requires that the user_msg have (if present):
5181 int samdb_result_effective_badPwdCount(struct ldb_context *sam_ldb,
5182 TALLOC_CTX *mem_ctx,
5183 struct ldb_dn *domain_dn,
5184 const struct ldb_message *user_msg)
5186 struct timeval tv_now = timeval_current();
5187 NTTIME now = timeval_to_nttime(&tv_now);
5188 int64_t lockOutObservationWindow = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn,
5189 "lockOutObservationWindow", NULL);
5190 return dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5194 * Prepare an update to the badPwdCount and associated attributes.
5196 * This requires that the user_msg have (if present):
5201 * This also requires that the domain_msg have (if present):
5203 * - lockoutThreshold
5204 * - lockOutObservationWindow
5206 NTSTATUS dsdb_update_bad_pwd_count(TALLOC_CTX *mem_ctx,
5207 struct ldb_context *sam_ctx,
5208 struct ldb_message *user_msg,
5209 struct ldb_message *domain_msg,
5210 struct ldb_message **_mod_msg)
5212 int i, ret, badPwdCount;
5213 int64_t lockoutThreshold, lockOutObservationWindow;
5214 struct dom_sid *sid;
5215 struct timeval tv_now = timeval_current();
5216 NTTIME now = timeval_to_nttime(&tv_now);
5218 uint32_t pwdProperties, rid = 0;
5219 struct ldb_message *mod_msg;
5221 sid = samdb_result_dom_sid(mem_ctx, user_msg, "objectSid");
5223 pwdProperties = ldb_msg_find_attr_as_uint(domain_msg,
5224 "pwdProperties", -1);
5225 if (sid && !(pwdProperties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
5226 status = dom_sid_split_rid(NULL, sid, NULL, &rid);
5227 if (!NT_STATUS_IS_OK(status)) {
5229 * This can't happen anyway, but always try
5230 * and update the badPwdCount on failure
5238 * Work out if we are doing password lockout on the domain.
5239 * Also, the built in administrator account is exempt:
5240 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa375371%28v=vs.85%29.aspx
5242 lockoutThreshold = ldb_msg_find_attr_as_int(domain_msg,
5243 "lockoutThreshold", 0);
5244 if (lockoutThreshold == 0 || (rid == DOMAIN_RID_ADMINISTRATOR)) {
5245 DEBUG(5, ("Not updating badPwdCount on %s after wrong password\n",
5246 ldb_dn_get_linearized(user_msg->dn)));
5247 return NT_STATUS_OK;
5250 mod_msg = ldb_msg_new(mem_ctx);
5251 if (mod_msg == NULL) {
5252 return NT_STATUS_NO_MEMORY;
5254 mod_msg->dn = ldb_dn_copy(mod_msg, user_msg->dn);
5255 if (mod_msg->dn == NULL) {
5256 TALLOC_FREE(mod_msg);
5257 return NT_STATUS_NO_MEMORY;
5260 lockOutObservationWindow = ldb_msg_find_attr_as_int64(domain_msg,
5261 "lockOutObservationWindow", 0);
5263 badPwdCount = dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5267 ret = samdb_msg_add_int(sam_ctx, mod_msg, mod_msg, "badPwdCount", badPwdCount);
5268 if (ret != LDB_SUCCESS) {
5269 TALLOC_FREE(mod_msg);
5270 return NT_STATUS_NO_MEMORY;
5272 ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "badPasswordTime", now);
5273 if (ret != LDB_SUCCESS) {
5274 TALLOC_FREE(mod_msg);
5275 return NT_STATUS_NO_MEMORY;
5278 if (badPwdCount >= lockoutThreshold) {
5279 ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "lockoutTime", now);
5280 if (ret != LDB_SUCCESS) {
5281 TALLOC_FREE(mod_msg);
5282 return NT_STATUS_NO_MEMORY;
5284 DEBUG(5, ("Locked out user %s after %d wrong passwords\n",
5285 ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5287 DEBUG(5, ("Updated badPwdCount on %s after %d wrong passwords\n",
5288 ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5291 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
5292 for (i=0; i< mod_msg->num_elements; i++) {
5293 mod_msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
5296 *_mod_msg = mod_msg;
5297 return NT_STATUS_OK;
5301 * Sets defaults for a User object
5302 * List of default attributes set:
5303 * accountExpires, badPasswordTime, badPwdCount,
5304 * codePage, countryCode, lastLogoff, lastLogon
5305 * logonCount, pwdLastSet
5307 int dsdb_user_obj_set_defaults(struct ldb_context *ldb,
5308 struct ldb_message *usr_obj,
5309 struct ldb_request *req)
5312 const struct attribute_values {
5315 const char *add_value;
5316 const char *mod_value;
5317 const char *control;
5322 .name = "accountExpires",
5323 .add_value = "9223372036854775807",
5327 .name = "badPasswordTime",
5331 .name = "badPwdCount",
5339 .name = "countryCode",
5343 .name = "lastLogoff",
5347 .name = "lastLogon",
5351 .name = "logonCount",
5355 .name = "logonHours",
5356 .add_flags = DSDB_FLAG_INTERNAL_FORCE_META_DATA,
5359 .name = "pwdLastSet",
5361 .control = DSDB_CONTROL_PASSWORD_DEFAULT_LAST_SET_OID,
5364 .name = "adminCount",
5368 .name = "operatorCount",
5373 for (i = 0; i < ARRAY_SIZE(map); i++) {
5375 const char *value = NULL;
5378 if (req != NULL && req->operation == LDB_ADD) {
5379 value = map[i].add_value;
5380 flags = map[i].add_flags;
5382 value = map[i].mod_value;
5383 flags = map[i].mod_flags;
5386 if (value == NULL) {
5387 value = map[i].value;
5390 if (value != NULL) {
5391 flags |= LDB_FLAG_MOD_ADD;
5398 ret = samdb_find_or_add_attribute_ex(ldb, usr_obj,
5402 if (ret != LDB_SUCCESS) {
5406 if (req != NULL && added && map[i].control != NULL) {
5407 ret = ldb_request_add_control(req,
5410 if (ret != LDB_SUCCESS) {
5420 * Sets 'sAMAccountType on user object based on userAccountControl
5421 * @param ldb Current ldb_context
5422 * @param usr_obj ldb_message representing User object
5423 * @param user_account_control Value for userAccountControl flags
5424 * @param account_type_p Optional pointer to account_type to return
5425 * @return LDB_SUCCESS or LDB_ERR* code on failure
5427 int dsdb_user_obj_set_account_type(struct ldb_context *ldb, struct ldb_message *usr_obj,
5428 uint32_t user_account_control, uint32_t *account_type_p)
5431 uint32_t account_type;
5432 struct ldb_message_element *el;
5434 account_type = ds_uf2atype(user_account_control);
5435 if (account_type == 0) {
5436 ldb_set_errstring(ldb, "dsdb: Unrecognized account type!");
5437 return LDB_ERR_UNWILLING_TO_PERFORM;
5439 ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5442 if (ret != LDB_SUCCESS) {
5445 el = ldb_msg_find_element(usr_obj, "sAMAccountType");
5446 el->flags = LDB_FLAG_MOD_REPLACE;
5448 if (account_type_p) {
5449 *account_type_p = account_type;
5456 * Determine and set primaryGroupID based on userAccountControl value
5457 * @param ldb Current ldb_context
5458 * @param usr_obj ldb_message representing User object
5459 * @param user_account_control Value for userAccountControl flags
5460 * @param group_rid_p Optional pointer to group RID to return
5461 * @return LDB_SUCCESS or LDB_ERR* code on failure
5463 int dsdb_user_obj_set_primary_group_id(struct ldb_context *ldb, struct ldb_message *usr_obj,
5464 uint32_t user_account_control, uint32_t *group_rid_p)
5468 struct ldb_message_element *el;
5470 rid = ds_uf2prim_group_rid(user_account_control);
5472 ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5473 "primaryGroupID", rid);
5474 if (ret != LDB_SUCCESS) {
5477 el = ldb_msg_find_element(usr_obj, "primaryGroupID");
5478 el->flags = LDB_FLAG_MOD_REPLACE;