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,
745 struct ldb_message_element *el;
747 el = ldb_msg_find_element(msg, name);
756 ret = ldb_msg_add_string(msg, name, set_value);
757 if (ret != LDB_SUCCESS) {
760 msg->elements[msg->num_elements - 1].flags = LDB_FLAG_MOD_ADD;
767 int samdb_find_or_add_attribute(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
769 return samdb_find_or_add_attribute_ex(ldb, msg, name, set_value, NULL);
773 add a dom_sid element to a message
775 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
776 const char *attr_name, const struct dom_sid *sid)
779 enum ndr_err_code ndr_err;
781 ndr_err = ndr_push_struct_blob(&v, mem_ctx,
783 (ndr_push_flags_fn_t)ndr_push_dom_sid);
784 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
785 return ldb_operr(sam_ldb);
787 return ldb_msg_add_value(msg, attr_name, &v, NULL);
792 add a delete element operation to a message
794 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
795 const char *attr_name)
797 /* we use an empty replace rather than a delete, as it allows for
798 dsdb_replace() to be used everywhere */
799 return ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_REPLACE, NULL);
803 add an add attribute value to a message or enhance an existing attribute
804 which has the same name and the add flag set.
806 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
807 struct ldb_message *msg, const char *attr_name,
810 struct ldb_message_element *el;
811 struct ldb_val val, *vals;
817 v = talloc_strdup(mem_ctx, value);
819 return ldb_oom(sam_ldb);
822 val.data = (uint8_t *) v;
823 val.length = strlen(v);
825 if (val.length == 0) {
826 /* allow empty strings as non-existent attributes */
830 for (i = 0; i < msg->num_elements; i++) {
831 el = &msg->elements[i];
832 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
833 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD)) {
839 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_ADD,
841 if (ret != LDB_SUCCESS) {
846 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
849 return ldb_oom(sam_ldb);
852 el->values[el->num_values] = val;
859 add a delete attribute value to a message or enhance an existing attribute
860 which has the same name and the delete flag set.
862 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
863 struct ldb_message *msg, const char *attr_name,
866 struct ldb_message_element *el;
867 struct ldb_val val, *vals;
873 v = talloc_strdup(mem_ctx, value);
875 return ldb_oom(sam_ldb);
878 val.data = (uint8_t *) v;
879 val.length = strlen(v);
881 if (val.length == 0) {
882 /* allow empty strings as non-existent attributes */
886 for (i = 0; i < msg->num_elements; i++) {
887 el = &msg->elements[i];
888 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
889 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
895 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_DELETE,
897 if (ret != LDB_SUCCESS) {
902 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
905 return ldb_oom(sam_ldb);
908 el->values[el->num_values] = val;
915 add a int element to a message
917 int samdb_msg_add_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
918 const char *attr_name, int v)
920 const char *s = talloc_asprintf(mem_ctx, "%d", v);
922 return ldb_oom(sam_ldb);
924 return ldb_msg_add_string(msg, attr_name, s);
928 * Add an unsigned int element to a message
930 * The issue here is that we have not yet first cast to int32_t explicitly,
931 * before we cast to an signed int to printf() into the %d or cast to a
932 * int64_t before we then cast to a long long to printf into a %lld.
934 * There are *no* unsigned integers in Active Directory LDAP, even the RID
935 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
936 * (See the schema, and the syntax definitions in schema_syntax.c).
939 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
940 const char *attr_name, unsigned int v)
942 return samdb_msg_add_int(sam_ldb, mem_ctx, msg, attr_name, (int)v);
946 add a (signed) int64_t element to a message
948 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
949 const char *attr_name, int64_t v)
951 const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
953 return ldb_oom(sam_ldb);
955 return ldb_msg_add_string(msg, attr_name, s);
959 * Add an unsigned int64_t (uint64_t) element to a message
961 * The issue here is that we have not yet first cast to int32_t explicitly,
962 * before we cast to an signed int to printf() into the %d or cast to a
963 * int64_t before we then cast to a long long to printf into a %lld.
965 * There are *no* unsigned integers in Active Directory LDAP, even the RID
966 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
967 * (See the schema, and the syntax definitions in schema_syntax.c).
970 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
971 const char *attr_name, uint64_t v)
973 return samdb_msg_add_int64(sam_ldb, mem_ctx, msg, attr_name, (int64_t)v);
977 add a samr_Password element to a message
979 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
980 const char *attr_name, const struct samr_Password *hash)
983 val.data = talloc_memdup(mem_ctx, hash->hash, 16);
985 return ldb_oom(sam_ldb);
988 return ldb_msg_add_value(msg, attr_name, &val, NULL);
992 add a samr_Password array to a message
994 int samdb_msg_add_hashes(struct ldb_context *ldb,
995 TALLOC_CTX *mem_ctx, struct ldb_message *msg,
996 const char *attr_name, struct samr_Password *hashes,
1001 val.data = talloc_array_size(mem_ctx, 16, count);
1002 val.length = count*16;
1004 return ldb_oom(ldb);
1006 for (i=0;i<count;i++) {
1007 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
1009 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1013 add a acct_flags element to a message
1015 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1016 const char *attr_name, uint32_t v)
1018 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
1022 add a logon_hours element to a message
1024 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1025 const char *attr_name, struct samr_LogonHours *hours)
1028 val.length = hours->units_per_week / 8;
1029 val.data = hours->bits;
1030 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1034 add a parameters element to a message
1036 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1037 const char *attr_name, struct lsa_BinaryString *parameters)
1041 if ((parameters->length % 2) != 0) {
1042 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
1045 val.data = talloc_array(mem_ctx, uint8_t, parameters->length);
1046 if (val.data == NULL) {
1047 return LDB_ERR_OPERATIONS_ERROR;
1049 val.length = parameters->length;
1050 for (i = 0; i < parameters->length / 2; i++) {
1052 * The on-disk format needs to be in the 'network'
1053 * format, parmeters->array is a uint16_t array of
1054 * length parameters->length / 2
1056 SSVAL(val.data, i * 2, parameters->array[i]);
1058 return ldb_msg_add_steal_value(msg, attr_name, &val);
1062 * Sets an unsigned int element in a message
1064 * The issue here is that we have not yet first cast to int32_t explicitly,
1065 * before we cast to an signed int to printf() into the %d or cast to a
1066 * int64_t before we then cast to a long long to printf into a %lld.
1068 * There are *no* unsigned integers in Active Directory LDAP, even the RID
1069 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
1070 * (See the schema, and the syntax definitions in schema_syntax.c).
1073 int samdb_msg_set_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
1074 struct ldb_message *msg, const char *attr_name,
1077 struct ldb_message_element *el;
1079 el = ldb_msg_find_element(msg, attr_name);
1083 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, v);
1087 * Handle ldb_request in transaction
1089 int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
1090 struct ldb_request *req)
1094 ret = ldb_transaction_start(sam_ldb);
1095 if (ret != LDB_SUCCESS) {
1099 ret = ldb_request(sam_ldb, req);
1100 if (ret == LDB_SUCCESS) {
1101 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1104 if (ret == LDB_SUCCESS) {
1105 return ldb_transaction_commit(sam_ldb);
1107 ldb_transaction_cancel(sam_ldb);
1113 return a default security descriptor
1115 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1117 struct security_descriptor *sd;
1119 sd = security_descriptor_initialise(mem_ctx);
1124 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1126 struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1127 struct ldb_dn *aggregate_dn;
1132 aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1133 if (!aggregate_dn) {
1136 if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1139 return aggregate_dn;
1142 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1144 struct ldb_dn *new_dn;
1146 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1147 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1148 talloc_free(new_dn);
1154 struct ldb_dn *samdb_infrastructure_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_default_basedn(sam_ctx));
1159 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Infrastructure")) {
1160 talloc_free(new_dn);
1166 struct ldb_dn *samdb_sites_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_config_basedn(sam_ctx));
1171 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1172 talloc_free(new_dn);
1179 work out the domain sid for the current open ldb
1181 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1183 TALLOC_CTX *tmp_ctx;
1184 const struct dom_sid *domain_sid;
1185 const char *attrs[] = {
1189 struct ldb_result *res;
1192 /* see if we have a cached copy */
1193 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1198 tmp_ctx = talloc_new(ldb);
1199 if (tmp_ctx == NULL) {
1203 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1205 if (ret != LDB_SUCCESS) {
1209 if (res->count != 1) {
1213 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1214 if (domain_sid == NULL) {
1218 /* cache the domain_sid in the ldb */
1219 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1223 talloc_steal(ldb, domain_sid);
1224 talloc_free(tmp_ctx);
1229 talloc_free(tmp_ctx);
1234 get domain sid from cache
1236 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1238 return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1241 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1243 TALLOC_CTX *tmp_ctx;
1244 struct dom_sid *dom_sid_new;
1245 struct dom_sid *dom_sid_old;
1247 /* see if we have a cached copy */
1248 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1249 "cache.domain_sid"), struct dom_sid);
1251 tmp_ctx = talloc_new(ldb);
1252 if (tmp_ctx == NULL) {
1256 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1261 /* cache the domain_sid in the ldb */
1262 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1266 talloc_steal(ldb, dom_sid_new);
1267 talloc_free(tmp_ctx);
1268 talloc_free(dom_sid_old);
1273 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1274 talloc_free(tmp_ctx);
1278 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
1280 TALLOC_CTX *tmp_ctx;
1281 struct ldb_dn *ntds_settings_dn_new;
1282 struct ldb_dn *ntds_settings_dn_old;
1284 /* see if we have a forced copy from provision */
1285 ntds_settings_dn_old = talloc_get_type(ldb_get_opaque(ldb,
1286 "forced.ntds_settings_dn"), struct ldb_dn);
1288 tmp_ctx = talloc_new(ldb);
1289 if (tmp_ctx == NULL) {
1293 ntds_settings_dn_new = ldb_dn_copy(tmp_ctx, ntds_settings_dn_in);
1294 if (!ntds_settings_dn_new) {
1298 /* set the DN in the ldb to avoid lookups during provision */
1299 if (ldb_set_opaque(ldb, "forced.ntds_settings_dn", ntds_settings_dn_new) != LDB_SUCCESS) {
1303 talloc_steal(ldb, ntds_settings_dn_new);
1304 talloc_free(tmp_ctx);
1305 talloc_free(ntds_settings_dn_old);
1310 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1311 talloc_free(tmp_ctx);
1316 work out the ntds settings dn for the current open ldb
1318 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1320 TALLOC_CTX *tmp_ctx;
1321 const char *root_attrs[] = { "dsServiceName", NULL };
1323 struct ldb_result *root_res;
1324 struct ldb_dn *settings_dn;
1326 /* see if we have a cached copy */
1327 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "forced.ntds_settings_dn");
1329 return ldb_dn_copy(mem_ctx, settings_dn);
1332 tmp_ctx = talloc_new(mem_ctx);
1333 if (tmp_ctx == NULL) {
1337 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1338 if (ret != LDB_SUCCESS) {
1339 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1340 ldb_errstring(ldb)));
1344 if (root_res->count != 1) {
1348 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1350 /* note that we do not cache the DN here, as that would mean
1351 * we could not handle server renames at runtime. Only
1352 * provision sets up forced.ntds_settings_dn */
1354 talloc_steal(mem_ctx, settings_dn);
1355 talloc_free(tmp_ctx);
1360 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1361 talloc_free(tmp_ctx);
1366 work out the ntds settings invocationId for the current open ldb
1368 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1370 TALLOC_CTX *tmp_ctx;
1371 const char *attrs[] = { "invocationId", NULL };
1373 struct ldb_result *res;
1374 struct GUID *invocation_id;
1376 /* see if we have a cached copy */
1377 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1378 if (invocation_id) {
1379 SMB_ASSERT(!GUID_all_zero(invocation_id));
1380 return invocation_id;
1383 tmp_ctx = talloc_new(ldb);
1384 if (tmp_ctx == NULL) {
1388 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1393 if (res->count != 1) {
1397 invocation_id = talloc(tmp_ctx, struct GUID);
1398 if (!invocation_id) {
1402 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1403 if (GUID_all_zero(invocation_id)) {
1404 if (ldb_msg_find_ldb_val(res->msgs[0], "invocationId")) {
1405 DEBUG(0, ("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1407 DEBUG(0, ("Failed to find parse own NTDS Settings invocationId from the ldb!\n"));
1412 /* cache the domain_sid in the ldb */
1413 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1417 talloc_steal(ldb, invocation_id);
1418 talloc_free(tmp_ctx);
1420 return invocation_id;
1423 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1424 talloc_free(tmp_ctx);
1428 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1430 TALLOC_CTX *tmp_ctx;
1431 struct GUID *invocation_id_new;
1432 struct GUID *invocation_id_old;
1434 /* see if we have a cached copy */
1435 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1436 "cache.invocation_id");
1438 tmp_ctx = talloc_new(ldb);
1439 if (tmp_ctx == NULL) {
1443 invocation_id_new = talloc(tmp_ctx, struct GUID);
1444 if (!invocation_id_new) {
1448 SMB_ASSERT(!GUID_all_zero(invocation_id_in));
1449 *invocation_id_new = *invocation_id_in;
1451 /* cache the domain_sid in the ldb */
1452 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1456 talloc_steal(ldb, invocation_id_new);
1457 talloc_free(tmp_ctx);
1458 talloc_free(invocation_id_old);
1463 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1464 talloc_free(tmp_ctx);
1469 work out the ntds settings objectGUID for the current open ldb
1471 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1473 TALLOC_CTX *tmp_ctx;
1474 const char *attrs[] = { "objectGUID", NULL };
1476 struct ldb_result *res;
1477 struct GUID *ntds_guid;
1479 /* see if we have a cached copy */
1480 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1485 tmp_ctx = talloc_new(ldb);
1486 if (tmp_ctx == NULL) {
1490 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1495 if (res->count != 1) {
1499 ntds_guid = talloc(tmp_ctx, struct GUID);
1504 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1506 /* cache the domain_sid in the ldb */
1507 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1511 talloc_steal(ldb, ntds_guid);
1512 talloc_free(tmp_ctx);
1517 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1518 talloc_free(tmp_ctx);
1522 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1524 TALLOC_CTX *tmp_ctx;
1525 struct GUID *ntds_guid_new;
1526 struct GUID *ntds_guid_old;
1528 /* see if we have a cached copy */
1529 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1531 tmp_ctx = talloc_new(ldb);
1532 if (tmp_ctx == NULL) {
1536 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1537 if (!ntds_guid_new) {
1541 *ntds_guid_new = *ntds_guid_in;
1543 /* cache the domain_sid in the ldb */
1544 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1548 talloc_steal(ldb, ntds_guid_new);
1549 talloc_free(tmp_ctx);
1550 talloc_free(ntds_guid_old);
1555 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1556 talloc_free(tmp_ctx);
1561 work out the server dn for the current open ldb
1563 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1565 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1570 dn = ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb, tmp_ctx));
1571 talloc_free(tmp_ctx);
1577 work out the server dn for the current open ldb
1579 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1581 struct ldb_dn *server_dn;
1582 struct ldb_dn *servers_dn;
1583 struct ldb_dn *server_site_dn;
1585 /* TODO: there must be a saner way to do this!! */
1586 server_dn = samdb_server_dn(ldb, mem_ctx);
1587 if (!server_dn) return NULL;
1589 servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1590 talloc_free(server_dn);
1591 if (!servers_dn) return NULL;
1593 server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1594 talloc_free(servers_dn);
1596 return server_site_dn;
1600 find the site name from a computers DN record
1602 int samdb_find_site_for_computer(struct ldb_context *ldb,
1603 TALLOC_CTX *mem_ctx, struct ldb_dn *computer_dn,
1604 const char **site_name)
1608 const struct ldb_val *rdn_val;
1612 ret = samdb_reference_dn(ldb, mem_ctx, computer_dn, "serverReferenceBL", &dn);
1613 if (ret != LDB_SUCCESS) {
1617 if (!ldb_dn_remove_child_components(dn, 2)) {
1619 return LDB_ERR_INVALID_DN_SYNTAX;
1622 rdn_val = ldb_dn_get_rdn_val(dn);
1623 if (rdn_val == NULL) {
1624 return LDB_ERR_OPERATIONS_ERROR;
1627 (*site_name) = talloc_strndup(mem_ctx, (const char *)rdn_val->data, rdn_val->length);
1630 return LDB_ERR_OPERATIONS_ERROR;
1636 find the NTDS GUID from a computers DN record
1638 int samdb_find_ntdsguid_for_computer(struct ldb_context *ldb, struct ldb_dn *computer_dn,
1639 struct GUID *ntds_guid)
1644 *ntds_guid = GUID_zero();
1646 ret = samdb_reference_dn(ldb, ldb, computer_dn, "serverReferenceBL", &dn);
1647 if (ret != LDB_SUCCESS) {
1651 if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Settings")) {
1653 return LDB_ERR_OPERATIONS_ERROR;
1656 ret = dsdb_find_guid_by_dn(ldb, dn, ntds_guid);
1662 find a 'reference' DN that points at another object
1663 (eg. serverReference, rIDManagerReference etc)
1665 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1666 const char *attribute, struct ldb_dn **dn)
1668 const char *attrs[2];
1669 struct ldb_result *res;
1672 attrs[0] = attribute;
1675 ret = dsdb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_ONE_ONLY|DSDB_SEARCH_SHOW_EXTENDED_DN, NULL);
1676 if (ret != LDB_SUCCESS) {
1677 ldb_asprintf_errstring(ldb, "Cannot find DN %s to get attribute %s for reference dn: %s",
1678 ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb));
1682 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1684 if (!ldb_msg_find_element(res->msgs[0], attribute)) {
1685 ldb_asprintf_errstring(ldb, "Cannot find attribute %s of %s to calculate reference dn", attribute,
1686 ldb_dn_get_linearized(base));
1688 ldb_asprintf_errstring(ldb, "Cannot interpret attribute %s of %s as a dn", attribute,
1689 ldb_dn_get_linearized(base));
1692 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1700 find if a DN (must have GUID component!) is our ntdsDsa
1702 int samdb_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *dn, bool *is_ntdsa)
1705 struct GUID dn_guid;
1706 const struct GUID *our_ntds_guid;
1707 status = dsdb_get_extended_dn_guid(dn, &dn_guid, "GUID");
1708 if (!NT_STATUS_IS_OK(status)) {
1709 return LDB_ERR_OPERATIONS_ERROR;
1712 our_ntds_guid = samdb_ntds_objectGUID(ldb);
1713 if (!our_ntds_guid) {
1714 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
1715 return LDB_ERR_OPERATIONS_ERROR;
1718 *is_ntdsa = GUID_equal(&dn_guid, our_ntds_guid);
1723 find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1725 int samdb_reference_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *base,
1726 const char *attribute, bool *is_ntdsa)
1729 struct ldb_dn *referenced_dn;
1730 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1731 if (tmp_ctx == NULL) {
1732 return LDB_ERR_OPERATIONS_ERROR;
1734 ret = samdb_reference_dn(ldb, tmp_ctx, base, attribute, &referenced_dn);
1735 if (ret != LDB_SUCCESS) {
1736 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb)));
1740 ret = samdb_dn_is_our_ntdsa(ldb, referenced_dn, is_ntdsa);
1742 talloc_free(tmp_ctx);
1747 find our machine account via the serverReference attribute in the
1750 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1752 struct ldb_dn *server_dn;
1755 server_dn = samdb_server_dn(ldb, mem_ctx);
1756 if (server_dn == NULL) {
1757 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
1760 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1761 talloc_free(server_dn);
1767 find the RID Manager$ DN via the rIDManagerReference attribute in the
1770 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1772 return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1773 "rIDManagerReference", dn);
1777 find the RID Set DN via the rIDSetReferences attribute in our
1780 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1782 struct ldb_dn *server_ref_dn;
1785 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1786 if (ret != LDB_SUCCESS) {
1789 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1790 talloc_free(server_ref_dn);
1794 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1796 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1803 return (const char *) val->data;
1807 * Finds the client site by using the client's IP address.
1808 * The "subnet_name" returns the name of the subnet if parameter != NULL
1810 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1811 const char *ip_address, char **subnet_name)
1813 const char *attrs[] = { "cn", "siteObject", NULL };
1814 struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
1815 struct ldb_result *res;
1816 const struct ldb_val *val;
1817 const char *site_name = NULL, *l_subnet_name = NULL;
1818 const char *allow_list[2] = { NULL, NULL };
1819 unsigned int i, count;
1823 * if we don't have a client ip e.g. ncalrpc
1824 * the server site is the client site
1826 if (ip_address == NULL) {
1827 return samdb_server_site_name(ldb, mem_ctx);
1830 sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1831 if (sites_container_dn == NULL) {
1835 subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1836 if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1837 talloc_free(sites_container_dn);
1838 talloc_free(subnets_dn);
1842 ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1844 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1846 } else if (ret != LDB_SUCCESS) {
1847 talloc_free(sites_container_dn);
1848 talloc_free(subnets_dn);
1854 for (i = 0; i < count; i++) {
1855 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1858 allow_list[0] = l_subnet_name;
1860 if (socket_allow_access(mem_ctx, NULL, allow_list, "", ip_address)) {
1861 sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1864 if (sites_dn == NULL) {
1865 /* No reference, maybe another subnet matches */
1869 /* "val" cannot be NULL here since "sites_dn" != NULL */
1870 val = ldb_dn_get_rdn_val(sites_dn);
1871 site_name = talloc_strdup(mem_ctx,
1872 (const char *) val->data);
1874 talloc_free(sites_dn);
1880 if (site_name == NULL) {
1881 /* This is the Windows Server fallback rule: when no subnet
1882 * exists and we have only one site available then use it (it
1883 * is for sure the same as our server site). If more sites do
1884 * exist then we don't know which one to use and set the site
1886 cnt = samdb_search_count(ldb, mem_ctx, sites_container_dn,
1887 "(objectClass=site)");
1889 site_name = samdb_server_site_name(ldb, mem_ctx);
1891 site_name = talloc_strdup(mem_ctx, "");
1893 l_subnet_name = NULL;
1896 if (subnet_name != NULL) {
1897 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1900 talloc_free(sites_container_dn);
1901 talloc_free(subnets_dn);
1908 work out if we are the PDC for the domain of the current open ldb
1910 bool samdb_is_pdc(struct ldb_context *ldb)
1915 ret = samdb_reference_dn_is_our_ntdsa(ldb, ldb_get_default_basedn(ldb), "fsmoRoleOwner",
1917 if (ret != LDB_SUCCESS) {
1918 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n",
1919 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1920 ldb_errstring(ldb)));
1928 work out if we are a Global Catalog server for the domain of the current open ldb
1930 bool samdb_is_gc(struct ldb_context *ldb)
1933 if (samdb_ntds_options(ldb, &options) != LDB_SUCCESS) {
1936 return (options & DS_NTDSDSA_OPT_IS_GC) != 0;
1939 /* Find a domain object in the parents of a particular DN. */
1940 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1941 struct ldb_dn **parent_dn, const char **errstring)
1943 TALLOC_CTX *local_ctx;
1944 struct ldb_dn *sdn = dn;
1945 struct ldb_result *res = NULL;
1946 int ret = LDB_SUCCESS;
1947 const char *attrs[] = { NULL };
1949 local_ctx = talloc_new(mem_ctx);
1950 if (local_ctx == NULL) return ldb_oom(ldb);
1952 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1953 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1954 "(|(objectClass=domain)(objectClass=builtinDomain))");
1955 if (ret == LDB_SUCCESS) {
1956 if (res->count == 1) {
1964 if (ret != LDB_SUCCESS) {
1965 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1966 ldb_dn_get_linearized(dn),
1967 ldb_dn_get_linearized(sdn),
1968 ldb_errstring(ldb));
1969 talloc_free(local_ctx);
1972 if (res->count != 1) {
1973 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1974 ldb_dn_get_linearized(dn));
1975 DEBUG(0,(__location__ ": %s\n", *errstring));
1976 talloc_free(local_ctx);
1977 return LDB_ERR_CONSTRAINT_VIOLATION;
1980 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1981 talloc_free(local_ctx);
1985 static void pwd_timeout_debug(struct tevent_context *unused1,
1986 struct tevent_timer *unused2,
1987 struct timeval unused3,
1990 DEBUG(0, ("WARNING: check_password_complexity: password script "
1991 "took more than 1 second to run\n"));
1996 * Performs checks on a user password (plaintext UNIX format - attribute
1997 * "password"). The remaining parameters have to be extracted from the domain
2000 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
2002 enum samr_ValidationStatus samdb_check_password(TALLOC_CTX *mem_ctx,
2003 struct loadparm_context *lp_ctx,
2004 const DATA_BLOB *utf8_blob,
2005 const uint32_t pwdProperties,
2006 const uint32_t minPwdLength)
2008 const char *utf8_pw = (const char *)utf8_blob->data;
2009 size_t utf8_len = strlen_m(utf8_pw);
2010 char *password_script = NULL;
2012 /* checks if the "minPwdLength" property is satisfied */
2013 if (minPwdLength > utf8_len) {
2014 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
2017 /* checks the password complexity */
2018 if (!(pwdProperties & DOMAIN_PASSWORD_COMPLEX)) {
2019 return SAMR_VALIDATION_STATUS_SUCCESS;
2022 if (utf8_len == 0) {
2023 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2026 password_script = lpcfg_check_password_script(lp_ctx, mem_ctx);
2027 if (password_script != NULL && *password_script != '\0') {
2030 struct tevent_context *event_ctx = NULL;
2031 struct tevent_req *req = NULL;
2032 struct samba_runcmd_state *run_cmd = NULL;
2033 const char * const cmd[4] = {
2039 event_ctx = tevent_context_init(mem_ctx);
2040 if (event_ctx == NULL) {
2041 TALLOC_FREE(password_script);
2042 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2045 /* Gives a warning after 1 second, terminates after 10 */
2046 tevent_add_timer(event_ctx, event_ctx,
2047 tevent_timeval_current_ofs(1, 0),
2048 pwd_timeout_debug, NULL);
2050 req = samba_runcmd_send(event_ctx, event_ctx,
2051 tevent_timeval_current_ofs(10, 0),
2052 100, 100, cmd, NULL);
2053 run_cmd = tevent_req_data(req, struct samba_runcmd_state);
2054 if (write(run_cmd->fd_stdin, utf8_pw, utf8_len) != utf8_len) {
2055 TALLOC_FREE(password_script);
2056 TALLOC_FREE(event_ctx);
2057 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2060 close(run_cmd->fd_stdin);
2061 run_cmd->fd_stdin = -1;
2063 if (!tevent_req_poll(req, event_ctx)) {
2064 TALLOC_FREE(password_script);
2065 TALLOC_FREE(event_ctx);
2066 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2069 check_ret = samba_runcmd_recv(req, &error);
2070 TALLOC_FREE(event_ctx);
2072 if (error == ETIMEDOUT) {
2073 DEBUG(0, ("check_password_complexity: check password script took too long!\n"));
2074 TALLOC_FREE(password_script);
2075 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
2077 DEBUG(5,("check_password_complexity: check password script (%s) "
2078 "returned [%d]\n", password_script, check_ret));
2080 if (check_ret != 0) {
2081 DEBUG(1,("check_password_complexity: "
2082 "check password script said new password is not good "
2084 TALLOC_FREE(password_script);
2085 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2089 TALLOC_FREE(password_script);
2090 return SAMR_VALIDATION_STATUS_SUCCESS;
2093 TALLOC_FREE(password_script);
2095 if (!check_password_quality(utf8_pw)) {
2096 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
2099 return SAMR_VALIDATION_STATUS_SUCCESS;
2103 * Callback for "samdb_set_password" password change
2105 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
2110 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2113 if (ares->error != LDB_SUCCESS) {
2115 req->context = talloc_steal(req,
2116 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2118 return ldb_request_done(req, ret);
2121 if (ares->type != LDB_REPLY_DONE) {
2123 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
2126 req->context = talloc_steal(req,
2127 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
2129 return ldb_request_done(req, LDB_SUCCESS);
2133 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2134 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2135 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2136 * user change or not. The "rejectReason" gives some more information if the
2139 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2140 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2142 static NTSTATUS samdb_set_password_internal(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2143 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2144 const DATA_BLOB *new_password,
2145 const struct samr_Password *lmNewHash,
2146 const struct samr_Password *ntNewHash,
2147 const struct samr_Password *lmOldHash,
2148 const struct samr_Password *ntOldHash,
2149 enum samPwdChangeReason *reject_reason,
2150 struct samr_DomInfo1 **_dominfo,
2151 bool permit_interdomain_trust)
2153 struct ldb_message *msg;
2154 struct ldb_message_element *el;
2155 struct ldb_request *req;
2156 struct dsdb_control_password_change_status *pwd_stat = NULL;
2158 bool hash_values = false;
2159 NTSTATUS status = NT_STATUS_OK;
2161 #define CHECK_RET(x) \
2162 if (x != LDB_SUCCESS) { \
2164 return NT_STATUS_NO_MEMORY; \
2167 msg = ldb_msg_new(mem_ctx);
2169 return NT_STATUS_NO_MEMORY;
2172 if ((new_password != NULL)
2173 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
2174 /* we have the password as plaintext UTF16 */
2175 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
2176 new_password, NULL));
2177 el = ldb_msg_find_element(msg, "clearTextPassword");
2178 el->flags = LDB_FLAG_MOD_REPLACE;
2179 } else if ((new_password == NULL)
2180 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2181 /* we have a password as LM and/or NT hash */
2182 if (lmNewHash != NULL) {
2183 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2184 "dBCSPwd", lmNewHash));
2185 el = ldb_msg_find_element(msg, "dBCSPwd");
2186 el->flags = LDB_FLAG_MOD_REPLACE;
2188 if (ntNewHash != NULL) {
2189 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2190 "unicodePwd", ntNewHash));
2191 el = ldb_msg_find_element(msg, "unicodePwd");
2192 el->flags = LDB_FLAG_MOD_REPLACE;
2196 /* the password wasn't specified correctly */
2198 return NT_STATUS_INVALID_PARAMETER;
2201 /* build modify request */
2202 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2203 samdb_set_password_callback, NULL);
2204 if (ret != LDB_SUCCESS) {
2206 return NT_STATUS_NO_MEMORY;
2209 /* A password change operation */
2210 if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2211 struct dsdb_control_password_change *change;
2213 change = talloc(req, struct dsdb_control_password_change);
2214 if (change == NULL) {
2217 return NT_STATUS_NO_MEMORY;
2220 change->old_nt_pwd_hash = ntOldHash;
2221 change->old_lm_pwd_hash = lmOldHash;
2223 ret = ldb_request_add_control(req,
2224 DSDB_CONTROL_PASSWORD_CHANGE_OID,
2226 if (ret != LDB_SUCCESS) {
2229 return NT_STATUS_NO_MEMORY;
2233 ret = ldb_request_add_control(req,
2234 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2236 if (ret != LDB_SUCCESS) {
2239 return NT_STATUS_NO_MEMORY;
2242 if (permit_interdomain_trust) {
2243 ret = ldb_request_add_control(req,
2244 DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID,
2246 if (ret != LDB_SUCCESS) {
2249 return NT_STATUS_NO_MEMORY;
2252 ret = ldb_request_add_control(req,
2253 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2255 if (ret != LDB_SUCCESS) {
2258 return NT_STATUS_NO_MEMORY;
2261 ret = dsdb_autotransaction_request(ldb, req);
2263 if (req->context != NULL) {
2264 struct ldb_control *control = talloc_get_type_abort(req->context,
2265 struct ldb_control);
2266 pwd_stat = talloc_get_type_abort(control->data,
2267 struct dsdb_control_password_change_status);
2268 talloc_steal(mem_ctx, pwd_stat);
2274 /* Sets the domain info (if requested) */
2275 if (_dominfo != NULL) {
2276 struct samr_DomInfo1 *dominfo;
2278 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2279 if (dominfo == NULL) {
2280 return NT_STATUS_NO_MEMORY;
2283 if (pwd_stat != NULL) {
2284 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2285 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2286 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2287 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2288 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2291 *_dominfo = dominfo;
2294 if (reject_reason != NULL) {
2295 if (pwd_stat != NULL) {
2296 *reject_reason = pwd_stat->reject_reason;
2298 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2302 if (pwd_stat != NULL) {
2303 talloc_free(pwd_stat);
2306 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2307 const char *errmsg = ldb_errstring(ldb);
2308 char *endptr = NULL;
2309 WERROR werr = WERR_GENERAL_FAILURE;
2310 status = NT_STATUS_UNSUCCESSFUL;
2311 if (errmsg != NULL) {
2312 werr = W_ERROR(strtol(errmsg, &endptr, 16));
2313 DBG_WARNING("%s\n", errmsg);
2315 if (endptr != errmsg) {
2316 if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2317 status = NT_STATUS_WRONG_PASSWORD;
2319 if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2320 status = NT_STATUS_PASSWORD_RESTRICTION;
2323 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2324 /* don't let the caller know if an account doesn't exist */
2325 status = NT_STATUS_WRONG_PASSWORD;
2326 } else if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
2327 status = NT_STATUS_ACCESS_DENIED;
2328 } else if (ret != LDB_SUCCESS) {
2329 DEBUG(1, ("Failed to set password on %s: %s\n",
2330 ldb_dn_get_linearized(msg->dn),
2331 ldb_errstring(ldb)));
2332 status = NT_STATUS_UNSUCCESSFUL;
2338 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2339 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2340 const DATA_BLOB *new_password,
2341 const struct samr_Password *lmNewHash,
2342 const struct samr_Password *ntNewHash,
2343 const struct samr_Password *lmOldHash,
2344 const struct samr_Password *ntOldHash,
2345 enum samPwdChangeReason *reject_reason,
2346 struct samr_DomInfo1 **_dominfo)
2348 return samdb_set_password_internal(ldb, mem_ctx,
2351 lmNewHash, ntNewHash,
2352 lmOldHash, ntOldHash,
2353 reject_reason, _dominfo,
2354 false); /* reject trusts */
2358 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2359 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2360 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2361 * user change or not. The "rejectReason" gives some more information if the
2364 * This wrapper function for "samdb_set_password" takes a SID as input rather
2367 * This call encapsulates a new LDB transaction for changing the password;
2368 * therefore the user hasn't to start a new one.
2370 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2371 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2372 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2373 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2375 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2376 const struct dom_sid *user_sid,
2377 const uint32_t *new_version, /* optional for trusts */
2378 const DATA_BLOB *new_password,
2379 const struct samr_Password *lmNewHash,
2380 const struct samr_Password *ntNewHash,
2381 const struct samr_Password *lmOldHash,
2382 const struct samr_Password *ntOldHash,
2383 enum samPwdChangeReason *reject_reason,
2384 struct samr_DomInfo1 **_dominfo)
2386 TALLOC_CTX *frame = talloc_stackframe();
2388 const char * const user_attrs[] = {
2389 "userAccountControl",
2393 struct ldb_message *user_msg = NULL;
2397 ret = ldb_transaction_start(ldb);
2398 if (ret != LDB_SUCCESS) {
2399 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2401 return NT_STATUS_TRANSACTION_ABORTED;
2404 ret = dsdb_search_one(ldb, frame, &user_msg, ldb_get_default_basedn(ldb),
2405 LDB_SCOPE_SUBTREE, user_attrs, 0,
2406 "(&(objectSid=%s)(objectClass=user))",
2407 ldap_encode_ndr_dom_sid(frame, user_sid));
2408 if (ret != LDB_SUCCESS) {
2409 ldb_transaction_cancel(ldb);
2410 DEBUG(3, ("samdb_set_password_sid: SID[%s] not found in samdb %s - %s, "
2411 "returning NO_SUCH_USER\n",
2412 dom_sid_string(frame, user_sid),
2413 ldb_strerror(ret), ldb_errstring(ldb)));
2415 return NT_STATUS_NO_SUCH_USER;
2418 uac = ldb_msg_find_attr_as_uint(user_msg, "userAccountControl", 0);
2419 if (!(uac & UF_ACCOUNT_TYPE_MASK)) {
2420 ldb_transaction_cancel(ldb);
2421 DEBUG(1, ("samdb_set_password_sid: invalid "
2422 "userAccountControl[0x%08X] for SID[%s] DN[%s], "
2423 "returning NO_SUCH_USER\n",
2424 (unsigned)uac, dom_sid_string(frame, user_sid),
2425 ldb_dn_get_linearized(user_msg->dn)));
2427 return NT_STATUS_NO_SUCH_USER;
2430 if (uac & UF_INTERDOMAIN_TRUST_ACCOUNT) {
2431 const char * const tdo_attrs[] = {
2432 "trustAuthIncoming",
2436 struct ldb_message *tdo_msg = NULL;
2437 const char *account_name = NULL;
2438 uint32_t trust_direction;
2440 const struct ldb_val *old_val = NULL;
2441 struct trustAuthInOutBlob old_blob = {};
2442 uint32_t old_version = 0;
2443 struct AuthenticationInformation *old_version_a = NULL;
2444 uint32_t _new_version = 0;
2445 struct trustAuthInOutBlob new_blob = {};
2446 struct ldb_val new_val = {};
2447 struct timeval tv = timeval_current();
2448 NTTIME now = timeval_to_nttime(&tv);
2449 enum ndr_err_code ndr_err;
2451 if (new_password == NULL && ntNewHash == NULL) {
2452 ldb_transaction_cancel(ldb);
2453 DEBUG(1, ("samdb_set_password_sid: "
2454 "no new password provided "
2455 "sAMAccountName for SID[%s] DN[%s], "
2456 "returning INVALID_PARAMETER\n",
2457 dom_sid_string(frame, user_sid),
2458 ldb_dn_get_linearized(user_msg->dn)));
2460 return NT_STATUS_INVALID_PARAMETER;
2463 if (new_password != NULL && ntNewHash != NULL) {
2464 ldb_transaction_cancel(ldb);
2465 DEBUG(1, ("samdb_set_password_sid: "
2466 "two new passwords 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 && (new_password->length % 2)) {
2476 ldb_transaction_cancel(ldb);
2477 DEBUG(2, ("samdb_set_password_sid: "
2478 "invalid utf16 length (%zu) "
2479 "sAMAccountName for SID[%s] DN[%s], "
2480 "returning WRONG_PASSWORD\n",
2481 new_password->length,
2482 dom_sid_string(frame, user_sid),
2483 ldb_dn_get_linearized(user_msg->dn)));
2485 return NT_STATUS_WRONG_PASSWORD;
2488 if (new_password != NULL && new_password->length >= 500) {
2489 ldb_transaction_cancel(ldb);
2490 DEBUG(2, ("samdb_set_password_sid: "
2491 "utf16 password too long (%zu) "
2492 "sAMAccountName for SID[%s] DN[%s], "
2493 "returning WRONG_PASSWORD\n",
2494 new_password->length,
2495 dom_sid_string(frame, user_sid),
2496 ldb_dn_get_linearized(user_msg->dn)));
2498 return NT_STATUS_WRONG_PASSWORD;
2501 account_name = ldb_msg_find_attr_as_string(user_msg,
2502 "sAMAccountName", NULL);
2503 if (account_name == NULL) {
2504 ldb_transaction_cancel(ldb);
2505 DEBUG(1, ("samdb_set_password_sid: missing "
2506 "sAMAccountName for SID[%s] DN[%s], "
2507 "returning NO_SUCH_USER\n",
2508 dom_sid_string(frame, user_sid),
2509 ldb_dn_get_linearized(user_msg->dn)));
2511 return NT_STATUS_NO_SUCH_USER;
2514 nt_status = dsdb_trust_search_tdo_by_type(ldb,
2519 if (!NT_STATUS_IS_OK(nt_status)) {
2520 ldb_transaction_cancel(ldb);
2521 DEBUG(1, ("samdb_set_password_sid: dsdb_trust_search_tdo "
2522 "failed(%s) for sAMAccountName[%s] SID[%s] DN[%s], "
2523 "returning INTERNAL_DB_CORRUPTION\n",
2524 nt_errstr(nt_status), account_name,
2525 dom_sid_string(frame, user_sid),
2526 ldb_dn_get_linearized(user_msg->dn)));
2528 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2531 trust_direction = ldb_msg_find_attr_as_int(tdo_msg,
2532 "trustDirection", 0);
2533 if (!(trust_direction & LSA_TRUST_DIRECTION_INBOUND)) {
2534 ldb_transaction_cancel(ldb);
2535 DEBUG(1, ("samdb_set_password_sid: direction[0x%08X] is "
2536 "not inbound for sAMAccountName[%s] "
2538 "returning INTERNAL_DB_CORRUPTION\n",
2539 (unsigned)trust_direction,
2541 ldb_dn_get_linearized(user_msg->dn),
2542 ldb_dn_get_linearized(tdo_msg->dn)));
2544 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2547 old_val = ldb_msg_find_ldb_val(tdo_msg, "trustAuthIncoming");
2548 if (old_val != NULL) {
2549 ndr_err = ndr_pull_struct_blob(old_val, frame, &old_blob,
2550 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2551 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2552 ldb_transaction_cancel(ldb);
2553 DEBUG(1, ("samdb_set_password_sid: "
2554 "failed(%s) to parse "
2555 "trustAuthOutgoing sAMAccountName[%s] "
2557 "returning INTERNAL_DB_CORRUPTION\n",
2558 ndr_map_error2string(ndr_err),
2560 ldb_dn_get_linearized(user_msg->dn),
2561 ldb_dn_get_linearized(tdo_msg->dn)));
2564 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2568 for (i = old_blob.current.count; i > 0; i--) {
2569 struct AuthenticationInformation *a =
2570 &old_blob.current.array[i - 1];
2572 switch (a->AuthType) {
2573 case TRUST_AUTH_TYPE_NONE:
2574 if (i == old_blob.current.count) {
2576 * remove TRUST_AUTH_TYPE_NONE at the
2579 old_blob.current.count--;
2583 case TRUST_AUTH_TYPE_VERSION:
2585 old_version = a->AuthInfo.version.version;
2588 case TRUST_AUTH_TYPE_CLEAR:
2591 case TRUST_AUTH_TYPE_NT4OWF:
2596 if (new_version == NULL) {
2598 new_version = &_new_version;
2601 if (old_version_a != NULL && *new_version != (old_version + 1)) {
2602 old_version_a->LastUpdateTime = now;
2603 old_version_a->AuthType = TRUST_AUTH_TYPE_NONE;
2606 new_blob.count = MAX(old_blob.current.count, 2);
2607 new_blob.current.array = talloc_zero_array(frame,
2608 struct AuthenticationInformation,
2610 if (new_blob.current.array == NULL) {
2611 ldb_transaction_cancel(ldb);
2613 return NT_STATUS_NO_MEMORY;
2615 new_blob.previous.array = talloc_zero_array(frame,
2616 struct AuthenticationInformation,
2618 if (new_blob.current.array == NULL) {
2619 ldb_transaction_cancel(ldb);
2621 return NT_STATUS_NO_MEMORY;
2624 for (i = 0; i < old_blob.current.count; i++) {
2625 struct AuthenticationInformation *o =
2626 &old_blob.current.array[i];
2627 struct AuthenticationInformation *p =
2628 &new_blob.previous.array[i];
2631 new_blob.previous.count++;
2633 for (; i < new_blob.count; i++) {
2634 struct AuthenticationInformation *pi =
2635 &new_blob.previous.array[i];
2639 * new_blob.previous is still empty so
2640 * we'll do new_blob.previous = new_blob.current
2646 pi->LastUpdateTime = now;
2647 pi->AuthType = TRUST_AUTH_TYPE_NONE;
2648 new_blob.previous.count++;
2651 for (i = 0; i < new_blob.count; i++) {
2652 struct AuthenticationInformation *ci =
2653 &new_blob.current.array[i];
2655 ci->LastUpdateTime = now;
2658 if (ntNewHash != NULL) {
2659 ci->AuthType = TRUST_AUTH_TYPE_NT4OWF;
2660 ci->AuthInfo.nt4owf.password = *ntNewHash;
2664 ci->AuthType = TRUST_AUTH_TYPE_CLEAR;
2665 ci->AuthInfo.clear.size = new_password->length;
2666 ci->AuthInfo.clear.password = new_password->data;
2669 ci->AuthType = TRUST_AUTH_TYPE_VERSION;
2670 ci->AuthInfo.version.version = *new_version;
2673 ci->AuthType = TRUST_AUTH_TYPE_NONE;
2677 new_blob.current.count++;
2680 if (new_blob.previous.count == 0) {
2681 TALLOC_FREE(new_blob.previous.array);
2682 new_blob.previous = new_blob.current;
2685 ndr_err = ndr_push_struct_blob(&new_val, frame, &new_blob,
2686 (ndr_push_flags_fn_t)ndr_push_trustAuthInOutBlob);
2687 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2688 ldb_transaction_cancel(ldb);
2689 DEBUG(1, ("samdb_set_password_sid: "
2690 "failed(%s) to generate "
2691 "trustAuthOutgoing sAMAccountName[%s] "
2693 "returning UNSUCCESSFUL\n",
2694 ndr_map_error2string(ndr_err),
2696 ldb_dn_get_linearized(user_msg->dn),
2697 ldb_dn_get_linearized(tdo_msg->dn)));
2699 return NT_STATUS_UNSUCCESSFUL;
2702 tdo_msg->num_elements = 0;
2703 TALLOC_FREE(tdo_msg->elements);
2705 ret = ldb_msg_add_empty(tdo_msg, "trustAuthIncoming",
2706 LDB_FLAG_MOD_REPLACE, NULL);
2707 if (ret != LDB_SUCCESS) {
2708 ldb_transaction_cancel(ldb);
2710 return NT_STATUS_NO_MEMORY;
2712 ret = ldb_msg_add_value(tdo_msg, "trustAuthIncoming",
2714 if (ret != LDB_SUCCESS) {
2715 ldb_transaction_cancel(ldb);
2717 return NT_STATUS_NO_MEMORY;
2720 ret = ldb_modify(ldb, tdo_msg);
2721 if (ret != LDB_SUCCESS) {
2722 nt_status = dsdb_ldb_err_to_ntstatus(ret);
2723 ldb_transaction_cancel(ldb);
2724 DEBUG(1, ("samdb_set_password_sid: "
2725 "failed to replace "
2726 "trustAuthOutgoing sAMAccountName[%s] "
2730 ldb_dn_get_linearized(user_msg->dn),
2731 ldb_dn_get_linearized(tdo_msg->dn),
2732 nt_errstr(nt_status), ldb_errstring(ldb)));
2738 nt_status = samdb_set_password_internal(ldb, mem_ctx,
2741 lmNewHash, ntNewHash,
2742 lmOldHash, ntOldHash,
2743 reject_reason, _dominfo,
2744 true); /* permit trusts */
2745 if (!NT_STATUS_IS_OK(nt_status)) {
2746 ldb_transaction_cancel(ldb);
2751 ret = ldb_transaction_commit(ldb);
2752 if (ret != LDB_SUCCESS) {
2753 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2754 ldb_dn_get_linearized(user_msg->dn),
2755 ldb_errstring(ldb)));
2757 return NT_STATUS_TRANSACTION_ABORTED;
2761 return NT_STATUS_OK;
2765 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2766 struct dom_sid *sid, struct ldb_dn **ret_dn)
2768 struct ldb_message *msg;
2769 struct ldb_dn *basedn;
2773 sidstr = dom_sid_string(mem_ctx, sid);
2774 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2776 /* We might have to create a ForeignSecurityPrincipal, even if this user
2777 * is in our own domain */
2779 msg = ldb_msg_new(sidstr);
2781 talloc_free(sidstr);
2782 return NT_STATUS_NO_MEMORY;
2785 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2786 ldb_get_default_basedn(sam_ctx),
2787 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2789 if (ret != LDB_SUCCESS) {
2790 DEBUG(0, ("Failed to find DN for "
2791 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2792 talloc_free(sidstr);
2793 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2796 /* add core elements to the ldb_message for the alias */
2798 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2799 talloc_free(sidstr);
2800 return NT_STATUS_NO_MEMORY;
2803 ret = ldb_msg_add_string(msg, "objectClass",
2804 "foreignSecurityPrincipal");
2805 if (ret != LDB_SUCCESS) {
2806 talloc_free(sidstr);
2807 return NT_STATUS_NO_MEMORY;
2810 /* create the alias */
2811 ret = ldb_add(sam_ctx, msg);
2812 if (ret != LDB_SUCCESS) {
2813 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2815 ldb_dn_get_linearized(msg->dn),
2816 ldb_errstring(sam_ctx)));
2817 talloc_free(sidstr);
2818 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2821 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2822 talloc_free(sidstr);
2824 return NT_STATUS_OK;
2829 Find the DN of a domain, assuming it to be a dotted.dns name
2832 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2835 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2836 const char *binary_encoded;
2837 const char * const *split_realm;
2844 split_realm = (const char * const *)str_list_make(tmp_ctx, dns_domain, ".");
2846 talloc_free(tmp_ctx);
2849 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2850 for (i=0; split_realm[i]; i++) {
2851 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2852 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2853 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2854 binary_encoded, ldb_dn_get_linearized(dn)));
2855 talloc_free(tmp_ctx);
2859 if (!ldb_dn_validate(dn)) {
2860 DEBUG(2, ("Failed to validated DN %s\n",
2861 ldb_dn_get_linearized(dn)));
2862 talloc_free(tmp_ctx);
2865 talloc_free(tmp_ctx);
2871 Find the DNS equivalent of a DN, in dotted DNS form
2873 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2875 int i, num_components = ldb_dn_get_comp_num(dn);
2876 char *dns_name = talloc_strdup(mem_ctx, "");
2877 if (dns_name == NULL) {
2881 for (i=0; i<num_components; i++) {
2882 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2885 talloc_free(dns_name);
2888 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2889 (int)v->length, (int)v->length, (char *)v->data);
2891 talloc_free(dns_name);
2897 /* remove the last '.' */
2898 if (dns_name[0] != 0) {
2899 dns_name[strlen(dns_name)-1] = 0;
2906 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2907 name is based on the forest DNS name
2909 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
2910 TALLOC_CTX *mem_ctx,
2911 const struct GUID *ntds_guid)
2913 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2914 const char *guid_str;
2915 struct ldb_dn *forest_dn;
2916 const char *dnsforest;
2919 guid_str = GUID_string(tmp_ctx, ntds_guid);
2920 if (guid_str == NULL) {
2921 talloc_free(tmp_ctx);
2924 forest_dn = ldb_get_root_basedn(samdb);
2925 if (forest_dn == NULL) {
2926 talloc_free(tmp_ctx);
2929 dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
2930 if (dnsforest == NULL) {
2931 talloc_free(tmp_ctx);
2934 ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
2935 talloc_free(tmp_ctx);
2941 Find the DN of a domain, be it the netbios or DNS name
2943 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2944 const char *domain_name)
2946 const char * const domain_ref_attrs[] = {
2949 const char * const domain_ref2_attrs[] = {
2952 struct ldb_result *res_domain_ref;
2953 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2954 /* find the domain's DN */
2955 int ret_domain = ldb_search(ldb, mem_ctx,
2957 samdb_partitions_dn(ldb, mem_ctx),
2960 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2962 if (ret_domain != LDB_SUCCESS) {
2966 if (res_domain_ref->count == 0) {
2967 ret_domain = ldb_search(ldb, mem_ctx,
2969 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2972 "(objectclass=domain)");
2973 if (ret_domain != LDB_SUCCESS) {
2977 if (res_domain_ref->count == 1) {
2978 return res_domain_ref->msgs[0]->dn;
2983 if (res_domain_ref->count > 1) {
2984 DEBUG(0,("Found %d records matching domain [%s]\n",
2985 ret_domain, domain_name));
2989 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
2995 use a GUID to find a DN
2997 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
2998 TALLOC_CTX *mem_ctx,
2999 const struct GUID *guid,
3000 uint32_t dsdb_flags,
3004 struct ldb_result *res;
3005 const char *attrs[] = { NULL };
3006 char *guid_str = GUID_string(mem_ctx, guid);
3009 return ldb_operr(ldb);
3012 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3013 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3014 DSDB_SEARCH_SHOW_EXTENDED_DN |
3015 DSDB_SEARCH_ONE_ONLY | dsdb_flags,
3016 "objectGUID=%s", guid_str);
3017 talloc_free(guid_str);
3018 if (ret != LDB_SUCCESS) {
3022 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3029 use a DN to find a GUID with a given attribute name
3031 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
3032 struct ldb_dn *dn, const char *attribute,
3036 struct ldb_result *res;
3037 const char *attrs[2];
3038 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3040 attrs[0] = attribute;
3043 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3044 DSDB_SEARCH_SHOW_DELETED |
3045 DSDB_SEARCH_SHOW_RECYCLED);
3046 if (ret != LDB_SUCCESS) {
3047 talloc_free(tmp_ctx);
3050 if (res->count < 1) {
3051 talloc_free(tmp_ctx);
3052 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3054 *guid = samdb_result_guid(res->msgs[0], attribute);
3055 talloc_free(tmp_ctx);
3060 use a DN to find a GUID
3062 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
3063 struct ldb_dn *dn, struct GUID *guid)
3065 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
3071 adds the given GUID to the given ldb_message. This value is added
3072 for the given attr_name (may be either "objectGUID" or "parentGUID").
3074 int dsdb_msg_add_guid(struct ldb_message *msg,
3076 const char *attr_name)
3081 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
3083 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
3084 if (!NT_STATUS_IS_OK(status)) {
3085 ret = LDB_ERR_OPERATIONS_ERROR;
3089 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
3090 if (ret != LDB_SUCCESS) {
3091 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
3099 talloc_free(tmp_ctx);
3106 use a DN to find a SID
3108 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
3109 struct ldb_dn *dn, struct dom_sid *sid)
3112 struct ldb_result *res;
3113 const char *attrs[] = { "objectSid", NULL };
3114 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3119 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
3120 DSDB_SEARCH_SHOW_DELETED |
3121 DSDB_SEARCH_SHOW_RECYCLED);
3122 if (ret != LDB_SUCCESS) {
3123 talloc_free(tmp_ctx);
3126 if (res->count < 1) {
3127 talloc_free(tmp_ctx);
3128 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3130 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
3132 talloc_free(tmp_ctx);
3133 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3136 talloc_free(tmp_ctx);
3141 use a SID to find a DN
3143 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
3144 TALLOC_CTX *mem_ctx,
3145 struct dom_sid *sid, struct ldb_dn **dn)
3148 struct ldb_result *res;
3149 const char *attrs[] = { NULL };
3150 char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
3153 return ldb_operr(ldb);
3156 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3157 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3158 DSDB_SEARCH_SHOW_EXTENDED_DN |
3159 DSDB_SEARCH_ONE_ONLY,
3160 "objectSid=%s", sid_str);
3161 talloc_free(sid_str);
3162 if (ret != LDB_SUCCESS) {
3166 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
3173 load a repsFromTo blob list for a given partition GUID
3174 attr must be "repsFrom" or "repsTo"
3176 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3177 const char *attr, struct repsFromToBlob **r, uint32_t *count)
3179 const char *attrs[] = { attr, NULL };
3180 struct ldb_result *res = NULL;
3181 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3183 struct ldb_message_element *el;
3189 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
3190 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3191 /* partition hasn't been replicated yet */
3194 if (ret != LDB_SUCCESS) {
3195 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
3196 talloc_free(tmp_ctx);
3197 return WERR_DS_DRA_INTERNAL_ERROR;
3200 el = ldb_msg_find_element(res->msgs[0], attr);
3202 /* it's OK to be empty */
3203 talloc_free(tmp_ctx);
3207 *count = el->num_values;
3208 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
3210 talloc_free(tmp_ctx);
3211 return WERR_DS_DRA_INTERNAL_ERROR;
3214 for (i=0; i<(*count); i++) {
3215 enum ndr_err_code ndr_err;
3216 ndr_err = ndr_pull_struct_blob(&el->values[i],
3219 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
3220 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3221 talloc_free(tmp_ctx);
3222 return WERR_DS_DRA_INTERNAL_ERROR;
3226 talloc_free(tmp_ctx);
3232 save the repsFromTo blob list for a given partition GUID
3233 attr must be "repsFrom" or "repsTo"
3235 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3236 const char *attr, struct repsFromToBlob *r, uint32_t count)
3238 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3239 struct ldb_message *msg;
3240 struct ldb_message_element *el;
3243 msg = ldb_msg_new(tmp_ctx);
3245 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
3249 el->values = talloc_array(msg, struct ldb_val, count);
3254 for (i=0; i<count; i++) {
3256 enum ndr_err_code ndr_err;
3258 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
3260 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
3261 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3269 if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
3270 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
3274 talloc_free(tmp_ctx);
3279 talloc_free(tmp_ctx);
3280 return WERR_DS_DRA_INTERNAL_ERROR;
3285 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
3286 object for a partition
3288 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
3289 uint64_t *uSN, uint64_t *urgent_uSN)
3291 struct ldb_request *req;
3293 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3294 struct dsdb_control_current_partition *p_ctrl;
3295 struct ldb_result *res;
3297 res = talloc_zero(tmp_ctx, struct ldb_result);
3299 talloc_free(tmp_ctx);
3300 return ldb_oom(ldb);
3303 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3304 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
3308 res, ldb_search_default_callback,
3310 if (ret != LDB_SUCCESS) {
3311 talloc_free(tmp_ctx);
3315 p_ctrl = talloc(req, struct dsdb_control_current_partition);
3316 if (p_ctrl == NULL) {
3317 talloc_free(tmp_ctx);
3318 return ldb_oom(ldb);
3320 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
3323 ret = ldb_request_add_control(req,
3324 DSDB_CONTROL_CURRENT_PARTITION_OID,
3326 if (ret != LDB_SUCCESS) {
3327 talloc_free(tmp_ctx);
3331 /* Run the new request */
3332 ret = ldb_request(ldb, req);
3334 if (ret == LDB_SUCCESS) {
3335 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3338 if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
3339 /* it hasn't been created yet, which means
3340 an implicit value of zero */
3342 talloc_free(tmp_ctx);
3346 if (ret != LDB_SUCCESS) {
3347 talloc_free(tmp_ctx);
3351 if (res->count < 1) {
3357 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
3359 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
3363 talloc_free(tmp_ctx);
3368 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
3369 const struct drsuapi_DsReplicaCursor2 *c2)
3371 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3374 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
3375 const struct drsuapi_DsReplicaCursor *c2)
3377 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
3382 see if a computer identified by its invocationId is a RODC
3384 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
3386 /* 1) find the DN for this servers NTDSDSA object
3387 2) search for the msDS-isRODC attribute
3388 3) if not present then not a RODC
3389 4) if present and TRUE then is a RODC
3391 struct ldb_dn *config_dn;
3392 const char *attrs[] = { "msDS-isRODC", NULL };
3394 struct ldb_result *res;
3395 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
3397 config_dn = ldb_get_config_basedn(sam_ctx);
3399 talloc_free(tmp_ctx);
3400 return ldb_operr(sam_ctx);
3403 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
3404 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
3406 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3408 talloc_free(tmp_ctx);
3412 if (ret != LDB_SUCCESS) {
3413 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
3414 GUID_string(tmp_ctx, objectGUID)));
3416 talloc_free(tmp_ctx);
3420 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
3421 *is_rodc = (ret == 1);
3423 talloc_free(tmp_ctx);
3429 see if we are a RODC
3431 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
3433 const struct GUID *objectGUID;
3437 /* see if we have a cached copy */
3438 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
3444 objectGUID = samdb_ntds_objectGUID(sam_ctx);
3446 return ldb_operr(sam_ctx);
3449 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
3450 if (ret != LDB_SUCCESS) {
3454 cached = talloc(sam_ctx, bool);
3455 if (cached == NULL) {
3456 return ldb_oom(sam_ctx);
3460 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
3461 if (ret != LDB_SUCCESS) {
3462 talloc_free(cached);
3463 return ldb_operr(sam_ctx);
3469 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
3471 TALLOC_CTX *tmp_ctx;
3474 tmp_ctx = talloc_new(ldb);
3475 if (tmp_ctx == NULL) {
3479 cached = talloc(tmp_ctx, bool);
3485 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
3489 talloc_steal(ldb, cached);
3490 talloc_free(tmp_ctx);
3494 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
3495 talloc_free(tmp_ctx);
3501 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
3502 * flags are DS_NTDSSETTINGS_OPT_*
3504 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
3508 TALLOC_CTX *tmp_ctx;
3509 struct ldb_result *res;
3510 struct ldb_dn *site_dn;
3511 const char *attrs[] = { "options", NULL };
3513 tmp_ctx = talloc_new(ldb_ctx);
3514 if (tmp_ctx == NULL)
3517 /* Retrieve the site dn for the ldb that we
3518 * have open. This is our local site.
3520 site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
3521 if (site_dn == NULL)
3524 /* Perform a one level (child) search from the local
3525 * site distinguided name. We're looking for the
3526 * "options" attribute within the nTDSSiteSettings
3529 rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
3530 LDB_SCOPE_ONELEVEL, attrs,
3531 "objectClass=nTDSSiteSettings");
3533 if (rc != LDB_SUCCESS || res->count != 1)
3536 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3538 talloc_free(tmp_ctx);
3543 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3544 talloc_free(tmp_ctx);
3545 return ldb_error(ldb_ctx, LDB_ERR_NO_SUCH_OBJECT, __func__);
3549 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3551 flags are DS_NTDS_OPTION_*
3553 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
3555 TALLOC_CTX *tmp_ctx;
3556 const char *attrs[] = { "options", NULL };
3558 struct ldb_result *res;
3560 tmp_ctx = talloc_new(ldb);
3561 if (tmp_ctx == NULL) {
3565 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3566 if (ret != LDB_SUCCESS) {
3570 if (res->count != 1) {
3574 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3576 talloc_free(tmp_ctx);
3581 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3582 talloc_free(tmp_ctx);
3583 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3586 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3588 const char *attrs[] = { "objectCategory", NULL };
3590 struct ldb_result *res;
3592 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3593 if (ret != LDB_SUCCESS) {
3597 if (res->count != 1) {
3601 return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3604 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3609 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3610 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3612 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3614 char **tokens, *ret;
3617 tokens = str_list_make(mem_ctx, cn, " -_");
3618 if (tokens == NULL || tokens[0] == NULL) {
3622 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3623 tokens[0][0] = tolower(tokens[0][0]);
3624 for (i = 1; tokens[i] != NULL; i++)
3625 tokens[i][0] = toupper(tokens[i][0]);
3627 ret = talloc_strdup(mem_ctx, tokens[0]);
3628 for (i = 1; tokens[i] != NULL; i++)
3629 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3631 talloc_free(tokens);
3637 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3639 int dsdb_functional_level(struct ldb_context *ldb)
3641 int *domainFunctionality =
3642 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3643 if (!domainFunctionality) {
3644 /* this is expected during initial provision */
3645 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3646 return DS_DOMAIN_FUNCTION_2000;
3648 return *domainFunctionality;
3652 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3654 int dsdb_forest_functional_level(struct ldb_context *ldb)
3656 int *forestFunctionality =
3657 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3658 if (!forestFunctionality) {
3659 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3660 return DS_DOMAIN_FUNCTION_2000;
3662 return *forestFunctionality;
3666 set a GUID in an extended DN structure
3668 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3674 status = GUID_to_ndr_blob(guid, dn, &v);
3675 if (!NT_STATUS_IS_OK(status)) {
3676 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3679 ret = ldb_dn_set_extended_component(dn, component_name, &v);
3685 return a GUID from a extended DN structure
3687 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3689 const struct ldb_val *v;
3691 v = ldb_dn_get_extended_component(dn, component_name);
3693 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3696 return GUID_from_ndr_blob(v, guid);
3700 return a uint64_t from a extended DN structure
3702 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3704 const struct ldb_val *v;
3706 v = ldb_dn_get_extended_component(dn, component_name);
3708 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3712 char s[v->length+1];
3713 memcpy(s, v->data, v->length);
3716 *val = strtoull(s, NULL, 0);
3718 return NT_STATUS_OK;
3722 return a NTTIME from a extended DN structure
3724 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3726 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3730 return a uint32_t from a extended DN structure
3732 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3734 const struct ldb_val *v;
3736 v = ldb_dn_get_extended_component(dn, component_name);
3738 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3742 char s[v->length + 1];
3743 memcpy(s, v->data, v->length);
3745 *val = strtoul(s, NULL, 0);
3748 return NT_STATUS_OK;
3752 return a dom_sid from a extended DN structure
3754 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3756 const struct ldb_val *sid_blob;
3757 enum ndr_err_code ndr_err;
3759 sid_blob = ldb_dn_get_extended_component(dn, component_name);
3761 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3764 ndr_err = ndr_pull_struct_blob_all_noalloc(sid_blob, sid,
3765 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3766 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3767 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3771 return NT_STATUS_OK;
3776 return RMD_FLAGS directly from a ldb_dn
3777 returns 0 if not found
3779 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3781 const struct ldb_val *v;
3783 v = ldb_dn_get_extended_component(dn, "RMD_FLAGS");
3784 if (!v || v->length > sizeof(buf)-1) return 0;
3785 strncpy(buf, (const char *)v->data, v->length);
3787 return strtoul(buf, NULL, 10);
3791 return RMD_FLAGS directly from a ldb_val for a DN
3792 returns 0 if RMD_FLAGS is not found
3794 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3800 if (val->length < 13) {
3803 p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3807 flags = strtoul(p+11, &end, 10);
3808 if (!end || *end != '>') {
3809 /* it must end in a > */
3816 return true if a ldb_val containing a DN in storage form is deleted
3818 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3820 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3824 return true if a ldb_val containing a DN in storage form is
3825 in the upgraded w2k3 linked attribute format
3827 bool dsdb_dn_is_upgraded_link_val(const struct ldb_val *val)
3829 return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
3833 return a DN for a wellknown GUID
3835 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3836 struct ldb_dn *nc_root, const char *wk_guid,
3837 struct ldb_dn **wkguid_dn)
3839 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3840 const char *attrs[] = { NULL };
3843 struct ldb_result *res;
3845 /* construct the magic WKGUID DN */
3846 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3847 wk_guid, ldb_dn_get_linearized(nc_root));
3849 talloc_free(tmp_ctx);
3850 return ldb_operr(samdb);
3853 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
3854 DSDB_SEARCH_SHOW_DELETED |
3855 DSDB_SEARCH_SHOW_RECYCLED);
3856 if (ret != LDB_SUCCESS) {
3857 talloc_free(tmp_ctx);
3861 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
3862 talloc_free(tmp_ctx);
3867 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
3869 return ldb_dn_compare(*dn1, *dn2);
3873 find a NC root given a DN within the NC
3875 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3876 struct ldb_dn **nc_root)
3878 const char *root_attrs[] = { "namingContexts", NULL };
3879 TALLOC_CTX *tmp_ctx;
3881 struct ldb_message_element *el;
3882 struct ldb_result *root_res;
3884 struct ldb_dn **nc_dns;
3886 tmp_ctx = talloc_new(samdb);
3887 if (tmp_ctx == NULL) {
3888 return ldb_oom(samdb);
3891 ret = ldb_search(samdb, tmp_ctx, &root_res,
3892 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
3893 if (ret != LDB_SUCCESS || root_res->count == 0) {
3894 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
3895 talloc_free(tmp_ctx);
3899 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
3900 if ((el == NULL) || (el->num_values < 3)) {
3901 struct ldb_message *tmp_msg;
3903 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list."));
3905 /* This generates a temporary list of NCs in order to let the
3906 * provisioning work. */
3907 tmp_msg = ldb_msg_new(tmp_ctx);
3908 if (tmp_msg == NULL) {
3909 talloc_free(tmp_ctx);
3910 return ldb_oom(samdb);
3912 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3913 ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
3914 if (ret != LDB_SUCCESS) {
3915 talloc_free(tmp_ctx);
3918 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3919 ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
3920 if (ret != LDB_SUCCESS) {
3921 talloc_free(tmp_ctx);
3924 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3925 ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
3926 if (ret != LDB_SUCCESS) {
3927 talloc_free(tmp_ctx);
3930 el = &tmp_msg->elements[0];
3933 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
3935 talloc_free(tmp_ctx);
3936 return ldb_oom(samdb);
3939 for (i=0; i<el->num_values; i++) {
3940 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
3941 if (nc_dns[i] == NULL) {
3942 talloc_free(tmp_ctx);
3943 return ldb_operr(samdb);
3947 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
3949 for (i=0; i<el->num_values; i++) {
3950 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
3951 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
3952 talloc_free(tmp_ctx);
3957 talloc_free(tmp_ctx);
3958 return ldb_error(samdb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3963 find the deleted objects DN for any object, by looking for the NC
3964 root, then looking up the wellknown GUID
3966 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
3967 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
3968 struct ldb_dn **do_dn)
3970 struct ldb_dn *nc_root;
3973 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
3974 if (ret != LDB_SUCCESS) {
3978 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
3979 talloc_free(nc_root);
3984 return the tombstoneLifetime, in days
3986 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
3989 dn = ldb_get_config_basedn(ldb);
3991 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
3993 dn = ldb_dn_copy(ldb, dn);
3995 return ldb_operr(ldb);
3997 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3998 be a wellknown GUID for this */
3999 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
4001 return ldb_operr(ldb);
4004 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
4010 compare a ldb_val to a string case insensitively
4012 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
4014 size_t len = strlen(s);
4016 if (len > v->length) return 1;
4017 ret = strncasecmp(s, (const char *)v->data, v->length);
4018 if (ret != 0) return ret;
4019 if (v->length > len && v->data[len] != 0) {
4027 load the UDV for a partition in v2 format
4028 The list is returned sorted, and with our local cursor added
4030 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4031 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
4033 static const char *attrs[] = { "replUpToDateVector", NULL };
4034 struct ldb_result *r;
4035 const struct ldb_val *ouv_value;
4038 uint64_t highest_usn = 0;
4039 const struct GUID *our_invocation_id;
4040 static const struct timeval tv1970;
4041 NTTIME nt1970 = timeval_to_nttime(&tv1970);
4043 ret = dsdb_search_dn(samdb, mem_ctx, &r, dn, attrs, DSDB_SEARCH_SHOW_RECYCLED|DSDB_SEARCH_SHOW_DELETED);
4044 if (ret != LDB_SUCCESS) {
4048 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
4050 enum ndr_err_code ndr_err;
4051 struct replUpToDateVectorBlob ouv;
4053 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
4054 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
4055 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
4057 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4059 if (ouv.version != 2) {
4060 /* we always store as version 2, and
4061 * replUpToDateVector is not replicated
4063 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
4066 *count = ouv.ctr.ctr2.count;
4067 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
4075 our_invocation_id = samdb_ntds_invocation_id(samdb);
4076 if (!our_invocation_id) {
4077 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
4078 talloc_free(*cursors);
4079 return ldb_operr(samdb);
4082 ret = ldb_sequence_number(samdb, LDB_SEQ_HIGHEST_SEQ, &highest_usn);
4083 if (ret != LDB_SUCCESS) {
4084 /* nothing to add - this can happen after a vampire */
4085 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4089 for (i=0; i<*count; i++) {
4090 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
4091 (*cursors)[i].highest_usn = highest_usn;
4092 (*cursors)[i].last_sync_success = nt1970;
4093 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4098 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
4100 return ldb_oom(samdb);
4103 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
4104 (*cursors)[*count].highest_usn = highest_usn;
4105 (*cursors)[*count].last_sync_success = nt1970;
4108 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
4114 load the UDV for a partition in version 1 format
4115 The list is returned sorted, and with our local cursor added
4117 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
4118 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
4120 struct drsuapi_DsReplicaCursor2 *v2;
4124 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
4125 if (ret != LDB_SUCCESS) {
4135 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
4136 if (*cursors == NULL) {
4138 return ldb_oom(samdb);
4141 for (i=0; i<*count; i++) {
4142 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
4143 (*cursors)[i].highest_usn = v2[i].highest_usn;
4150 add a set of controls to a ldb_request structure based on a set of
4151 flags. See util.h for a list of available flags
4153 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
4156 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
4157 struct ldb_search_options_control *options;
4158 /* Using the phantom root control allows us to search all partitions */
4159 options = talloc(req, struct ldb_search_options_control);
4160 if (options == NULL) {
4161 return LDB_ERR_OPERATIONS_ERROR;
4163 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
4165 ret = ldb_request_add_control(req,
4166 LDB_CONTROL_SEARCH_OPTIONS_OID,
4168 if (ret != LDB_SUCCESS) {
4173 if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
4174 ret = ldb_request_add_control(req,
4175 DSDB_CONTROL_NO_GLOBAL_CATALOG,
4177 if (ret != LDB_SUCCESS) {
4182 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
4183 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
4184 if (ret != LDB_SUCCESS) {
4189 if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
4190 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
4191 if (ret != LDB_SUCCESS) {
4196 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
4197 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
4198 if (ret != LDB_SUCCESS) {
4203 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
4204 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
4205 if (!extended_ctrl) {
4206 return LDB_ERR_OPERATIONS_ERROR;
4208 extended_ctrl->type = 1;
4210 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
4211 if (ret != LDB_SUCCESS) {
4216 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
4217 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
4218 if (ret != LDB_SUCCESS) {
4223 if (dsdb_flags & DSDB_MODIFY_RELAX) {
4224 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
4225 if (ret != LDB_SUCCESS) {
4230 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
4231 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
4232 if (ret != LDB_SUCCESS) {
4237 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
4238 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
4239 if (ret != LDB_SUCCESS) {
4244 if (dsdb_flags & DSDB_TREE_DELETE) {
4245 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
4246 if (ret != LDB_SUCCESS) {
4251 if (dsdb_flags & DSDB_PROVISION) {
4252 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
4253 if (ret != LDB_SUCCESS) {
4258 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
4259 if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
4260 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
4261 if (ret != LDB_SUCCESS) {
4266 if (dsdb_flags & DSDB_PASSWORD_BYPASS_LAST_SET) {
4268 * This must not be critical, as it will only be
4269 * handled (and need to be handled) if the other
4270 * attributes in the request bring password_hash into
4273 ret = ldb_request_add_control(req, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID, false, NULL);
4274 if (ret != LDB_SUCCESS) {
4279 if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
4280 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
4281 if (ret != LDB_SUCCESS) {
4290 an add with a set of controls
4292 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
4293 uint32_t dsdb_flags)
4295 struct ldb_request *req;
4298 ret = ldb_build_add_req(&req, ldb, ldb,
4302 ldb_op_default_callback,
4305 if (ret != LDB_SUCCESS) return ret;
4307 ret = dsdb_request_add_controls(req, dsdb_flags);
4308 if (ret != LDB_SUCCESS) {
4313 ret = dsdb_autotransaction_request(ldb, req);
4320 a modify with a set of controls
4322 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
4323 uint32_t dsdb_flags)
4325 struct ldb_request *req;
4328 ret = ldb_build_mod_req(&req, ldb, ldb,
4332 ldb_op_default_callback,
4335 if (ret != LDB_SUCCESS) return ret;
4337 ret = dsdb_request_add_controls(req, dsdb_flags);
4338 if (ret != LDB_SUCCESS) {
4343 ret = dsdb_autotransaction_request(ldb, req);
4350 a delete with a set of flags
4352 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
4353 uint32_t dsdb_flags)
4355 struct ldb_request *req;
4358 ret = ldb_build_del_req(&req, ldb, ldb,
4362 ldb_op_default_callback,
4365 if (ret != LDB_SUCCESS) return ret;
4367 ret = dsdb_request_add_controls(req, dsdb_flags);
4368 if (ret != LDB_SUCCESS) {
4373 ret = dsdb_autotransaction_request(ldb, req);
4380 like dsdb_modify() but set all the element flags to
4381 LDB_FLAG_MOD_REPLACE
4383 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
4387 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
4388 for (i=0;i<msg->num_elements;i++) {
4389 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
4392 return dsdb_modify(ldb, msg, dsdb_flags);
4397 search for attrs on one DN, allowing for dsdb_flags controls
4399 int dsdb_search_dn(struct ldb_context *ldb,
4400 TALLOC_CTX *mem_ctx,
4401 struct ldb_result **_result,
4402 struct ldb_dn *basedn,
4403 const char * const *attrs,
4404 uint32_t dsdb_flags)
4407 struct ldb_request *req;
4408 struct ldb_result *res;
4410 res = talloc_zero(mem_ctx, struct ldb_result);
4412 return ldb_oom(ldb);
4415 ret = ldb_build_search_req(&req, ldb, res,
4422 ldb_search_default_callback,
4424 if (ret != LDB_SUCCESS) {
4429 ret = dsdb_request_add_controls(req, dsdb_flags);
4430 if (ret != LDB_SUCCESS) {
4435 ret = ldb_request(ldb, req);
4436 if (ret == LDB_SUCCESS) {
4437 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4441 if (ret != LDB_SUCCESS) {
4451 search for attrs on one DN, by the GUID of the DN, allowing for
4454 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
4455 TALLOC_CTX *mem_ctx,
4456 struct ldb_result **_result,
4457 const struct GUID *guid,
4458 const char * const *attrs,
4459 uint32_t dsdb_flags)
4461 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4465 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
4467 talloc_free(tmp_ctx);
4468 return ldb_oom(ldb);
4471 ret = dsdb_search_dn(ldb, mem_ctx, _result, dn, attrs, dsdb_flags);
4472 talloc_free(tmp_ctx);
4477 general search with dsdb_flags for controls
4479 int dsdb_search(struct ldb_context *ldb,
4480 TALLOC_CTX *mem_ctx,
4481 struct ldb_result **_result,
4482 struct ldb_dn *basedn,
4483 enum ldb_scope scope,
4484 const char * const *attrs,
4485 uint32_t dsdb_flags,
4486 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4489 struct ldb_request *req;
4490 struct ldb_result *res;
4492 char *expression = NULL;
4493 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4495 /* cross-partitions searches with a basedn break multi-domain support */
4496 SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
4498 res = talloc_zero(tmp_ctx, struct ldb_result);
4500 talloc_free(tmp_ctx);
4501 return ldb_oom(ldb);
4505 va_start(ap, exp_fmt);
4506 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4510 talloc_free(tmp_ctx);
4511 return ldb_oom(ldb);
4515 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
4522 ldb_search_default_callback,
4524 if (ret != LDB_SUCCESS) {
4525 talloc_free(tmp_ctx);
4529 ret = dsdb_request_add_controls(req, dsdb_flags);
4530 if (ret != LDB_SUCCESS) {
4531 talloc_free(tmp_ctx);
4532 ldb_reset_err_string(ldb);
4536 ret = ldb_request(ldb, req);
4537 if (ret == LDB_SUCCESS) {
4538 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4541 if (ret != LDB_SUCCESS) {
4542 talloc_free(tmp_ctx);
4546 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
4547 if (res->count == 0) {
4548 talloc_free(tmp_ctx);
4549 ldb_reset_err_string(ldb);
4550 return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
4552 if (res->count != 1) {
4553 talloc_free(tmp_ctx);
4554 ldb_reset_err_string(ldb);
4555 return LDB_ERR_CONSTRAINT_VIOLATION;
4559 *_result = talloc_steal(mem_ctx, res);
4560 talloc_free(tmp_ctx);
4567 general search with dsdb_flags for controls
4568 returns exactly 1 record or an error
4570 int dsdb_search_one(struct ldb_context *ldb,
4571 TALLOC_CTX *mem_ctx,
4572 struct ldb_message **msg,
4573 struct ldb_dn *basedn,
4574 enum ldb_scope scope,
4575 const char * const *attrs,
4576 uint32_t dsdb_flags,
4577 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4580 struct ldb_result *res;
4582 char *expression = NULL;
4583 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4585 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4587 res = talloc_zero(tmp_ctx, struct ldb_result);
4589 talloc_free(tmp_ctx);
4590 return ldb_oom(ldb);
4594 va_start(ap, exp_fmt);
4595 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4599 talloc_free(tmp_ctx);
4600 return ldb_oom(ldb);
4602 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4603 dsdb_flags, "%s", expression);
4605 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4609 if (ret != LDB_SUCCESS) {
4610 talloc_free(tmp_ctx);
4614 *msg = talloc_steal(mem_ctx, res->msgs[0]);
4615 talloc_free(tmp_ctx);
4620 /* returns back the forest DNS name */
4621 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4623 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4624 ldb_get_root_basedn(ldb));
4627 if (forest_name == NULL) {
4631 p = strchr(forest_name, '/');
4639 /* returns back the default domain DNS name */
4640 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4642 const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4643 ldb_get_default_basedn(ldb));
4646 if (domain_name == NULL) {
4650 p = strchr(domain_name, '/');
4659 validate that an DSA GUID belongs to the specified user sid.
4660 The user SID must be a domain controller account (either RODC or
4663 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4664 const struct GUID *dsa_guid,
4665 const struct dom_sid *sid)
4668 - find DN of record with the DSA GUID in the
4669 configuration partition (objectGUID)
4670 - remove "NTDS Settings" component from DN
4671 - do a base search on that DN for serverReference with
4673 - extract objectSid from resulting serverReference
4675 - check this sid matches the sid argument
4677 struct ldb_dn *config_dn;
4678 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4679 struct ldb_message *msg;
4680 const char *attrs1[] = { NULL };
4681 const char *attrs2[] = { "serverReference", NULL };
4683 struct ldb_dn *dn, *account_dn;
4684 struct dom_sid sid2;
4687 config_dn = ldb_get_config_basedn(ldb);
4689 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4690 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4691 if (ret != LDB_SUCCESS) {
4692 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4693 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4694 talloc_free(tmp_ctx);
4695 return ldb_operr(ldb);
4699 if (!ldb_dn_remove_child_components(dn, 1)) {
4700 talloc_free(tmp_ctx);
4701 return ldb_operr(ldb);
4704 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4705 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4706 "(objectClass=server)");
4707 if (ret != LDB_SUCCESS) {
4708 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4709 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4710 talloc_free(tmp_ctx);
4711 return ldb_operr(ldb);
4714 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4715 if (account_dn == NULL) {
4716 DEBUG(1,(__location__ ": Failed to find account dn "
4717 "(serverReference) for %s, parent of DSA with "
4718 "objectGUID %s, sid %s\n",
4719 ldb_dn_get_linearized(msg->dn),
4720 GUID_string(tmp_ctx, dsa_guid),
4721 dom_sid_string(tmp_ctx, sid)));
4722 talloc_free(tmp_ctx);
4723 return ldb_operr(ldb);
4726 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4727 if (!NT_STATUS_IS_OK(status)) {
4728 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4729 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4730 talloc_free(tmp_ctx);
4731 return ldb_operr(ldb);
4734 if (!dom_sid_equal(sid, &sid2)) {
4735 /* someone is trying to spoof another account */
4736 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4737 GUID_string(tmp_ctx, dsa_guid),
4738 dom_sid_string(tmp_ctx, sid),
4739 dom_sid_string(tmp_ctx, &sid2)));
4740 talloc_free(tmp_ctx);
4741 return ldb_operr(ldb);
4744 talloc_free(tmp_ctx);
4748 static const char * const secret_attributes[] = {
4749 DSDB_SECRET_ATTRIBUTES,
4754 check if the attribute belongs to the RODC filtered attribute set
4755 Note that attributes that are in the filtered attribute set are the
4756 ones that _are_ always sent to a RODC
4758 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4760 /* they never get secret attributes */
4761 if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4765 /* they do get non-secret critical attributes */
4766 if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4770 /* they do get non-secret attributes marked as being in the FAS */
4771 if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4775 /* other attributes are denied */
4779 /* return fsmo role dn and role owner dn for a particular role*/
4780 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4781 struct ldb_context *ldb,
4783 struct ldb_dn **fsmo_role_dn,
4784 struct ldb_dn **role_owner_dn)
4788 case DREPL_NAMING_MASTER:
4789 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
4790 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4791 if (ret != LDB_SUCCESS) {
4792 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
4793 ldb_errstring(ldb)));
4794 talloc_free(tmp_ctx);
4795 return WERR_DS_DRA_INTERNAL_ERROR;
4798 case DREPL_INFRASTRUCTURE_MASTER:
4799 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
4800 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4801 if (ret != LDB_SUCCESS) {
4802 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4803 ldb_errstring(ldb)));
4804 talloc_free(tmp_ctx);
4805 return WERR_DS_DRA_INTERNAL_ERROR;
4808 case DREPL_RID_MASTER:
4809 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
4810 if (ret != LDB_SUCCESS) {
4811 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
4812 talloc_free(tmp_ctx);
4813 return WERR_DS_DRA_INTERNAL_ERROR;
4816 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4817 if (ret != LDB_SUCCESS) {
4818 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
4819 ldb_errstring(ldb)));
4820 talloc_free(tmp_ctx);
4821 return WERR_DS_DRA_INTERNAL_ERROR;
4824 case DREPL_SCHEMA_MASTER:
4825 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
4826 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4827 if (ret != LDB_SUCCESS) {
4828 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4829 ldb_errstring(ldb)));
4830 talloc_free(tmp_ctx);
4831 return WERR_DS_DRA_INTERNAL_ERROR;
4834 case DREPL_PDC_MASTER:
4835 *fsmo_role_dn = ldb_get_default_basedn(ldb);
4836 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4837 if (ret != LDB_SUCCESS) {
4838 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
4839 ldb_errstring(ldb)));
4840 talloc_free(tmp_ctx);
4841 return WERR_DS_DRA_INTERNAL_ERROR;
4845 return WERR_DS_DRA_INTERNAL_ERROR;
4850 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
4851 TALLOC_CTX *mem_ctx,
4852 struct ldb_dn *server_dn)
4855 struct ldb_result *res = NULL;
4856 const char * const attrs[] = { "dNSHostName", NULL};
4858 ldb_ret = ldb_search(ldb, mem_ctx, &res,
4862 if (ldb_ret != LDB_SUCCESS) {
4863 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
4864 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
4868 return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
4872 returns true if an attribute is in the filter,
4873 false otherwise, provided that attribute value is provided with the expression
4875 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
4879 switch (tree->operation) {
4882 for (i=0;i<tree->u.list.num_elements;i++) {
4883 if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
4889 return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
4890 case LDB_OP_EQUALITY:
4891 case LDB_OP_GREATER:
4894 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
4898 case LDB_OP_SUBSTRING:
4899 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
4903 case LDB_OP_PRESENT:
4904 /* (attrname=*) is not filtered out */
4906 case LDB_OP_EXTENDED:
4907 if (tree->u.extended.attr &&
4908 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
4916 bool is_attr_in_list(const char * const * attrs, const char *attr)
4920 for (i = 0; attrs[i]; i++) {
4921 if (ldb_attr_cmp(attrs[i], attr) == 0)
4928 int dsdb_werror_at(struct ldb_context *ldb, int ldb_ecode, WERROR werr,
4929 const char *location, const char *func,
4932 if (reason == NULL) {
4933 reason = win_errstr(werr);
4935 ldb_asprintf_errstring(ldb, "%08X: %s at %s:%s",
4936 W_ERROR_V(werr), reason, location, func);
4941 map an ldb error code to an approximate NTSTATUS code
4943 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
4947 return NT_STATUS_OK;
4949 case LDB_ERR_PROTOCOL_ERROR:
4950 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
4952 case LDB_ERR_TIME_LIMIT_EXCEEDED:
4953 return NT_STATUS_IO_TIMEOUT;
4955 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
4956 return NT_STATUS_BUFFER_TOO_SMALL;
4958 case LDB_ERR_COMPARE_FALSE:
4959 case LDB_ERR_COMPARE_TRUE:
4960 return NT_STATUS_REVISION_MISMATCH;
4962 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
4963 return NT_STATUS_NOT_SUPPORTED;
4965 case LDB_ERR_STRONG_AUTH_REQUIRED:
4966 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
4967 case LDB_ERR_SASL_BIND_IN_PROGRESS:
4968 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
4969 case LDB_ERR_INVALID_CREDENTIALS:
4970 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
4971 case LDB_ERR_UNWILLING_TO_PERFORM:
4972 return NT_STATUS_ACCESS_DENIED;
4974 case LDB_ERR_NO_SUCH_OBJECT:
4975 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4977 case LDB_ERR_REFERRAL:
4978 case LDB_ERR_NO_SUCH_ATTRIBUTE:
4979 return NT_STATUS_NOT_FOUND;
4981 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
4982 return NT_STATUS_NOT_SUPPORTED;
4984 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
4985 return NT_STATUS_BUFFER_TOO_SMALL;
4987 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
4988 case LDB_ERR_INAPPROPRIATE_MATCHING:
4989 case LDB_ERR_CONSTRAINT_VIOLATION:
4990 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
4991 case LDB_ERR_INVALID_DN_SYNTAX:
4992 case LDB_ERR_NAMING_VIOLATION:
4993 case LDB_ERR_OBJECT_CLASS_VIOLATION:
4994 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
4995 case LDB_ERR_NOT_ALLOWED_ON_RDN:
4996 return NT_STATUS_INVALID_PARAMETER;
4998 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
4999 case LDB_ERR_ENTRY_ALREADY_EXISTS:
5000 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
5003 return NT_STATUS_NETWORK_BUSY;
5005 case LDB_ERR_ALIAS_PROBLEM:
5006 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
5007 case LDB_ERR_UNAVAILABLE:
5008 case LDB_ERR_LOOP_DETECT:
5009 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
5010 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
5012 case LDB_ERR_OPERATIONS_ERROR:
5015 return NT_STATUS_UNSUCCESSFUL;
5020 create a new naming context that will hold a partial replica
5022 int dsdb_create_partial_replica_NC(struct ldb_context *ldb, struct ldb_dn *dn)
5024 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
5025 struct ldb_message *msg;
5028 msg = ldb_msg_new(tmp_ctx);
5030 talloc_free(tmp_ctx);
5031 return ldb_oom(ldb);
5035 ret = ldb_msg_add_string(msg, "objectClass", "top");
5036 if (ret != LDB_SUCCESS) {
5037 talloc_free(tmp_ctx);
5038 return ldb_oom(ldb);
5041 /* [MS-DRSR] implies that we should only add the 'top'
5042 * objectclass, but that would cause lots of problems with our
5043 * objectclass code as top is not structural, so we add
5044 * 'domainDNS' as well to keep things sane. We're expecting
5045 * this new NC to be of objectclass domainDNS after
5046 * replication anyway
5048 ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
5049 if (ret != LDB_SUCCESS) {
5050 talloc_free(tmp_ctx);
5051 return ldb_oom(ldb);
5054 ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
5055 INSTANCE_TYPE_IS_NC_HEAD|
5056 INSTANCE_TYPE_NC_ABOVE|
5057 INSTANCE_TYPE_UNINSTANT);
5058 if (ret != LDB_SUCCESS) {
5059 talloc_free(tmp_ctx);
5060 return ldb_oom(ldb);
5063 ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
5064 if (ret != LDB_SUCCESS && ret != LDB_ERR_ENTRY_ALREADY_EXISTS) {
5065 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
5066 ldb_dn_get_linearized(dn),
5067 ldb_errstring(ldb), ldb_strerror(ret)));
5068 talloc_free(tmp_ctx);
5072 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
5074 talloc_free(tmp_ctx);
5079 build a GUID from a string
5081 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
5083 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
5085 uint32_t time_mid, time_hi_and_version;
5086 uint32_t clock_seq[2];
5091 return NT_STATUS_INVALID_PARAMETER;
5094 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
5095 &time_low, &time_mid, &time_hi_and_version,
5096 &clock_seq[0], &clock_seq[1],
5097 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
5098 status = NT_STATUS_OK;
5101 if (!NT_STATUS_IS_OK(status)) {
5105 guid->time_low = time_low;
5106 guid->time_mid = time_mid;
5107 guid->time_hi_and_version = time_hi_and_version;
5108 guid->clock_seq[0] = clock_seq[0];
5109 guid->clock_seq[1] = clock_seq[1];
5111 guid->node[i] = node[i];
5114 return NT_STATUS_OK;
5117 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
5119 return talloc_asprintf(mem_ctx,
5120 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
5121 guid->time_low, guid->time_mid,
5122 guid->time_hi_and_version,
5125 guid->node[0], guid->node[1],
5126 guid->node[2], guid->node[3],
5127 guid->node[4], guid->node[5]);
5131 * Return the effective badPwdCount
5133 * This requires that the user_msg have (if present):
5137 * This also requires that the domain_msg have (if present):
5138 * - lockOutObservationWindow
5140 static int dsdb_effective_badPwdCount(const struct ldb_message *user_msg,
5141 int64_t lockOutObservationWindow,
5144 int64_t badPasswordTime;
5145 badPasswordTime = ldb_msg_find_attr_as_int64(user_msg, "badPasswordTime", 0);
5147 if (badPasswordTime - lockOutObservationWindow >= now) {
5148 return ldb_msg_find_attr_as_int(user_msg, "badPwdCount", 0);
5155 * Return the effective badPwdCount
5157 * This requires that the user_msg have (if present):
5162 int samdb_result_effective_badPwdCount(struct ldb_context *sam_ldb,
5163 TALLOC_CTX *mem_ctx,
5164 struct ldb_dn *domain_dn,
5165 const struct ldb_message *user_msg)
5167 struct timeval tv_now = timeval_current();
5168 NTTIME now = timeval_to_nttime(&tv_now);
5169 int64_t lockOutObservationWindow = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn,
5170 "lockOutObservationWindow", NULL);
5171 return dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5175 * Prepare an update to the badPwdCount and associated attributes.
5177 * This requires that the user_msg have (if present):
5182 * This also requires that the domain_msg have (if present):
5184 * - lockoutThreshold
5185 * - lockOutObservationWindow
5187 NTSTATUS dsdb_update_bad_pwd_count(TALLOC_CTX *mem_ctx,
5188 struct ldb_context *sam_ctx,
5189 struct ldb_message *user_msg,
5190 struct ldb_message *domain_msg,
5191 struct ldb_message **_mod_msg)
5193 int i, ret, badPwdCount;
5194 int64_t lockoutThreshold, lockOutObservationWindow;
5195 struct dom_sid *sid;
5196 struct timeval tv_now = timeval_current();
5197 NTTIME now = timeval_to_nttime(&tv_now);
5199 uint32_t pwdProperties, rid = 0;
5200 struct ldb_message *mod_msg;
5202 sid = samdb_result_dom_sid(mem_ctx, user_msg, "objectSid");
5204 pwdProperties = ldb_msg_find_attr_as_uint(domain_msg,
5205 "pwdProperties", -1);
5206 if (sid && !(pwdProperties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
5207 status = dom_sid_split_rid(NULL, sid, NULL, &rid);
5208 if (!NT_STATUS_IS_OK(status)) {
5210 * This can't happen anyway, but always try
5211 * and update the badPwdCount on failure
5219 * Work out if we are doing password lockout on the domain.
5220 * Also, the built in administrator account is exempt:
5221 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa375371%28v=vs.85%29.aspx
5223 lockoutThreshold = ldb_msg_find_attr_as_int(domain_msg,
5224 "lockoutThreshold", 0);
5225 if (lockoutThreshold == 0 || (rid == DOMAIN_RID_ADMINISTRATOR)) {
5226 DEBUG(5, ("Not updating badPwdCount on %s after wrong password\n",
5227 ldb_dn_get_linearized(user_msg->dn)));
5228 return NT_STATUS_OK;
5231 mod_msg = ldb_msg_new(mem_ctx);
5232 if (mod_msg == NULL) {
5233 return NT_STATUS_NO_MEMORY;
5235 mod_msg->dn = ldb_dn_copy(mod_msg, user_msg->dn);
5236 if (mod_msg->dn == NULL) {
5237 TALLOC_FREE(mod_msg);
5238 return NT_STATUS_NO_MEMORY;
5241 lockOutObservationWindow = ldb_msg_find_attr_as_int64(domain_msg,
5242 "lockOutObservationWindow", 0);
5244 badPwdCount = dsdb_effective_badPwdCount(user_msg, lockOutObservationWindow, now);
5248 ret = samdb_msg_add_int(sam_ctx, mod_msg, mod_msg, "badPwdCount", badPwdCount);
5249 if (ret != LDB_SUCCESS) {
5250 TALLOC_FREE(mod_msg);
5251 return NT_STATUS_NO_MEMORY;
5253 ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "badPasswordTime", now);
5254 if (ret != LDB_SUCCESS) {
5255 TALLOC_FREE(mod_msg);
5256 return NT_STATUS_NO_MEMORY;
5259 if (badPwdCount >= lockoutThreshold) {
5260 ret = samdb_msg_add_int64(sam_ctx, mod_msg, mod_msg, "lockoutTime", now);
5261 if (ret != LDB_SUCCESS) {
5262 TALLOC_FREE(mod_msg);
5263 return NT_STATUS_NO_MEMORY;
5265 DEBUG(5, ("Locked out user %s after %d wrong passwords\n",
5266 ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5268 DEBUG(5, ("Updated badPwdCount on %s after %d wrong passwords\n",
5269 ldb_dn_get_linearized(user_msg->dn), badPwdCount));
5272 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
5273 for (i=0; i< mod_msg->num_elements; i++) {
5274 mod_msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
5277 *_mod_msg = mod_msg;
5278 return NT_STATUS_OK;
5282 * Sets defaults for a User object
5283 * List of default attributes set:
5284 * accountExpires, badPasswordTime, badPwdCount,
5285 * codePage, countryCode, lastLogoff, lastLogon
5286 * logonCount, pwdLastSet
5288 int dsdb_user_obj_set_defaults(struct ldb_context *ldb,
5289 struct ldb_message *usr_obj,
5290 struct ldb_request *req)
5293 const struct attribute_values {
5296 const char *add_control;
5299 .name = "accountExpires",
5300 .value = "9223372036854775807"
5303 .name = "badPasswordTime",
5307 .name = "badPwdCount",
5315 .name = "countryCode",
5319 .name = "lastLogoff",
5323 .name = "lastLogon",
5327 .name = "logonCount",
5331 .name = "pwdLastSet",
5333 .add_control = DSDB_CONTROL_PASSWORD_DEFAULT_LAST_SET_OID,
5337 for (i = 0; i < ARRAY_SIZE(map); i++) {
5340 ret = samdb_find_or_add_attribute_ex(ldb, usr_obj,
5344 if (ret != LDB_SUCCESS) {
5348 if (req != NULL && added && map[i].add_control != NULL) {
5349 ret = ldb_request_add_control(req,
5352 if (ret != LDB_SUCCESS) {
5362 * Sets 'sAMAccountType on user object based on userAccountControl
5363 * @param ldb Current ldb_context
5364 * @param usr_obj ldb_message representing User object
5365 * @param user_account_control Value for userAccountControl flags
5366 * @param account_type_p Optional pointer to account_type to return
5367 * @return LDB_SUCCESS or LDB_ERR* code on failure
5369 int dsdb_user_obj_set_account_type(struct ldb_context *ldb, struct ldb_message *usr_obj,
5370 uint32_t user_account_control, uint32_t *account_type_p)
5373 uint32_t account_type;
5374 struct ldb_message_element *el;
5376 account_type = ds_uf2atype(user_account_control);
5377 if (account_type == 0) {
5378 ldb_set_errstring(ldb, "dsdb: Unrecognized account type!");
5379 return LDB_ERR_UNWILLING_TO_PERFORM;
5381 ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5384 if (ret != LDB_SUCCESS) {
5387 el = ldb_msg_find_element(usr_obj, "sAMAccountType");
5388 el->flags = LDB_FLAG_MOD_REPLACE;
5390 if (account_type_p) {
5391 *account_type_p = account_type;
5398 * Determine and set primaryGroupID based on userAccountControl value
5399 * @param ldb Current ldb_context
5400 * @param usr_obj ldb_message representing User object
5401 * @param user_account_control Value for userAccountControl flags
5402 * @param group_rid_p Optional pointer to group RID to return
5403 * @return LDB_SUCCESS or LDB_ERR* code on failure
5405 int dsdb_user_obj_set_primary_group_id(struct ldb_context *ldb, struct ldb_message *usr_obj,
5406 uint32_t user_account_control, uint32_t *group_rid_p)
5410 struct ldb_message_element *el;
5412 rid = ds_uf2prim_group_rid(user_account_control);
5414 ret = samdb_msg_add_uint(ldb, usr_obj, usr_obj,
5415 "primaryGroupID", rid);
5416 if (ret != LDB_SUCCESS) {
5419 el = ldb_msg_find_element(usr_obj, "primaryGroupID");
5420 el->flags = LDB_FLAG_MOD_REPLACE;