2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 2004
6 Copyright (C) Volker Lendecke 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "events/events.h"
27 #include "ldb_errors.h"
28 #include "../lib/util/util_ldb.h"
29 #include "../lib/crypto/crypto.h"
30 #include "dsdb/samdb/samdb.h"
31 #include "libcli/security/security.h"
32 #include "librpc/gen_ndr/ndr_security.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "../libds/common/flags.h"
35 #include "dsdb/common/proto.h"
36 #include "libcli/ldap/ldap_ndr.h"
37 #include "param/param.h"
38 #include "libcli/auth/libcli_auth.h"
39 #include "librpc/gen_ndr/ndr_drsblobs.h"
40 #include "system/locale.h"
41 #include "lib/util/tsort.h"
42 #include "dsdb/common/util.h"
43 #include "lib/socket/socket.h"
44 #include "dsdb/samdb/ldb_modules/util.h"
47 search the sam for the specified attributes in a specific domain, filter on
48 objectSid being in domain_sid.
50 int samdb_search_domain(struct ldb_context *sam_ldb,
52 struct ldb_dn *basedn,
53 struct ldb_message ***res,
54 const char * const *attrs,
55 const struct dom_sid *domain_sid,
56 const char *format, ...) _PRINTF_ATTRIBUTE(7,8)
62 count = gendb_search_v(sam_ldb, mem_ctx, basedn,
63 res, attrs, format, ap);
69 struct dom_sid *entry_sid;
71 entry_sid = samdb_result_dom_sid(mem_ctx, (*res)[i], "objectSid");
73 if ((entry_sid == NULL) ||
74 (!dom_sid_in_domain(domain_sid, entry_sid))) {
75 /* Delete that entry from the result set */
76 (*res)[i] = (*res)[count-1];
78 talloc_free(entry_sid);
81 talloc_free(entry_sid);
89 search the sam for a single string attribute in exactly 1 record
91 const char *samdb_search_string_v(struct ldb_context *sam_ldb,
93 struct ldb_dn *basedn,
94 const char *attr_name,
95 const char *format, va_list ap) _PRINTF_ATTRIBUTE(5,0)
98 const char *attrs[2] = { NULL, NULL };
99 struct ldb_message **res = NULL;
101 attrs[0] = attr_name;
103 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
105 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
106 attr_name, format, count));
113 return samdb_result_string(res[0], attr_name, NULL);
117 search the sam for a single string attribute in exactly 1 record
119 const char *samdb_search_string(struct ldb_context *sam_ldb,
121 struct ldb_dn *basedn,
122 const char *attr_name,
123 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
128 va_start(ap, format);
129 str = samdb_search_string_v(sam_ldb, mem_ctx, basedn, attr_name, format, ap);
135 struct ldb_dn *samdb_search_dn(struct ldb_context *sam_ldb,
137 struct ldb_dn *basedn,
138 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
142 struct ldb_message **res = NULL;
145 va_start(ap, format);
146 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, NULL, format, ap);
149 if (count != 1) return NULL;
151 ret = talloc_steal(mem_ctx, res[0]->dn);
158 search the sam for a dom_sid attribute in exactly 1 record
160 struct dom_sid *samdb_search_dom_sid(struct ldb_context *sam_ldb,
162 struct ldb_dn *basedn,
163 const char *attr_name,
164 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
168 struct ldb_message **res;
169 const char *attrs[2] = { NULL, NULL };
172 attrs[0] = attr_name;
174 va_start(ap, format);
175 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
178 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
179 attr_name, format, count));
185 sid = samdb_result_dom_sid(mem_ctx, res[0], attr_name);
191 return the count of the number of records in the sam matching the query
193 int samdb_search_count(struct ldb_context *sam_ldb,
194 struct ldb_dn *basedn,
195 const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
198 struct ldb_message **res;
199 const char *attrs[] = { NULL };
201 TALLOC_CTX *tmp_ctx = talloc_new(sam_ldb);
203 va_start(ap, format);
204 ret = gendb_search_v(sam_ldb, tmp_ctx, basedn, &res, attrs, format, ap);
206 talloc_free(tmp_ctx);
213 search the sam for a single integer attribute in exactly 1 record
215 unsigned int samdb_search_uint(struct ldb_context *sam_ldb,
217 unsigned int default_value,
218 struct ldb_dn *basedn,
219 const char *attr_name,
220 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
224 struct ldb_message **res;
225 const char *attrs[2] = { NULL, NULL };
227 attrs[0] = attr_name;
229 va_start(ap, format);
230 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
234 return default_value;
237 return samdb_result_uint(res[0], attr_name, default_value);
241 search the sam for a single signed 64 bit integer attribute in exactly 1 record
243 int64_t samdb_search_int64(struct ldb_context *sam_ldb,
245 int64_t default_value,
246 struct ldb_dn *basedn,
247 const char *attr_name,
248 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
252 struct ldb_message **res;
253 const char *attrs[2] = { NULL, NULL };
255 attrs[0] = attr_name;
257 va_start(ap, format);
258 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
262 return default_value;
265 return samdb_result_int64(res[0], attr_name, default_value);
269 search the sam for multipe records each giving a single string attribute
270 return the number of matches, or -1 on error
272 int samdb_search_string_multiple(struct ldb_context *sam_ldb,
274 struct ldb_dn *basedn,
276 const char *attr_name,
277 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
281 const char *attrs[2] = { NULL, NULL };
282 struct ldb_message **res = NULL;
284 attrs[0] = attr_name;
286 va_start(ap, format);
287 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
294 /* make sure its single valued */
295 for (i=0;i<count;i++) {
296 if (res[i]->num_elements != 1) {
297 DEBUG(1,("samdb: search for %s %s not single valued\n",
304 *strs = talloc_array(mem_ctx, const char *, count+1);
310 for (i=0;i<count;i++) {
311 (*strs)[i] = samdb_result_string(res[i], attr_name, NULL);
313 (*strs)[count] = NULL;
319 pull a uint from a result set.
321 unsigned int samdb_result_uint(const struct ldb_message *msg, const char *attr, unsigned int default_value)
323 return ldb_msg_find_attr_as_uint(msg, attr, default_value);
327 pull a (signed) int64 from a result set.
329 int64_t samdb_result_int64(const struct ldb_message *msg, const char *attr, int64_t default_value)
331 return ldb_msg_find_attr_as_int64(msg, attr, default_value);
335 pull a string from a result set.
337 const char *samdb_result_string(const struct ldb_message *msg, const char *attr,
338 const char *default_value)
340 return ldb_msg_find_attr_as_string(msg, attr, default_value);
343 struct ldb_dn *samdb_result_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
344 const char *attr, struct ldb_dn *default_value)
346 struct ldb_dn *ret_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, msg, attr);
348 return default_value;
354 pull a rid from a objectSid in a result set.
356 uint32_t samdb_result_rid_from_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
357 const char *attr, uint32_t default_value)
362 sid = samdb_result_dom_sid(mem_ctx, msg, attr);
364 return default_value;
366 rid = sid->sub_auths[sid->num_auths-1];
372 pull a dom_sid structure from a objectSid in a result set.
374 struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
377 const struct ldb_val *v;
379 enum ndr_err_code ndr_err;
380 v = ldb_msg_find_ldb_val(msg, attr);
384 sid = talloc(mem_ctx, struct dom_sid);
388 ndr_err = ndr_pull_struct_blob(v, sid, sid,
389 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
390 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
398 pull a guid structure from a objectGUID in a result set.
400 struct GUID samdb_result_guid(const struct ldb_message *msg, const char *attr)
402 const struct ldb_val *v;
406 v = ldb_msg_find_ldb_val(msg, attr);
407 if (!v) return GUID_zero();
409 status = GUID_from_ndr_blob(v, &guid);
410 if (!NT_STATUS_IS_OK(status)) {
418 pull a sid prefix from a objectSid in a result set.
419 this is used to find the domain sid for a user
421 struct dom_sid *samdb_result_sid_prefix(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
424 struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, msg, attr);
425 if (!sid || sid->num_auths < 1) return NULL;
431 pull a NTTIME in a result set.
433 NTTIME samdb_result_nttime(const struct ldb_message *msg, const char *attr,
434 NTTIME default_value)
436 return ldb_msg_find_attr_as_uint64(msg, attr, default_value);
440 * Windows stores 0 for lastLogoff.
441 * But when a MS DC return the lastLogoff (as Logoff Time)
442 * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
443 * cause windows 2008 and newer version to fail for SMB requests
445 NTTIME samdb_result_last_logoff(const struct ldb_message *msg)
447 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "lastLogoff",0);
450 ret = 0x7FFFFFFFFFFFFFFFULL;
456 * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
457 * indicate an account doesn't expire.
459 * When Windows initially creates an account, it sets
460 * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF). However,
461 * when changing from an account having a specific expiration date to
462 * that account never expiring, it sets accountExpires = 0.
464 * Consolidate that logic here to allow clearer logic for account expiry in
465 * the rest of the code.
467 NTTIME samdb_result_account_expires(const struct ldb_message *msg)
469 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "accountExpires",
473 ret = 0x7FFFFFFFFFFFFFFFULL;
479 pull a uint64_t from a result set.
481 uint64_t samdb_result_uint64(const struct ldb_message *msg, const char *attr,
482 uint64_t default_value)
484 return ldb_msg_find_attr_as_uint64(msg, attr, default_value);
489 construct the allow_password_change field from the PwdLastSet attribute and the
490 domain password settings
492 NTTIME samdb_result_allow_password_change(struct ldb_context *sam_ldb,
494 struct ldb_dn *domain_dn,
495 struct ldb_message *msg,
498 uint64_t attr_time = samdb_result_uint64(msg, attr, 0);
501 if (attr_time == 0) {
505 minPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "minPwdAge", NULL);
507 /* yes, this is a -= not a += as minPwdAge is stored as the negative
508 of the number of 100-nano-seconds */
509 attr_time -= minPwdAge;
515 construct the force_password_change field from the PwdLastSet
516 attribute, the userAccountControl and the domain password settings
518 NTTIME samdb_result_force_password_change(struct ldb_context *sam_ldb,
520 struct ldb_dn *domain_dn,
521 struct ldb_message *msg)
523 uint64_t attr_time = samdb_result_uint64(msg, "pwdLastSet", 0);
524 uint32_t userAccountControl = samdb_result_uint64(msg, "userAccountControl", 0);
527 /* Machine accounts don't expire, and there is a flag for 'no expiry' */
528 if (!(userAccountControl & UF_NORMAL_ACCOUNT)
529 || (userAccountControl & UF_DONT_EXPIRE_PASSWD)) {
530 return 0x7FFFFFFFFFFFFFFFULL;
533 if (attr_time == 0) {
537 maxPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "maxPwdAge", NULL);
538 if (maxPwdAge == 0) {
539 return 0x7FFFFFFFFFFFFFFFULL;
541 attr_time -= maxPwdAge;
548 pull a samr_Password structutre from a result set.
550 struct samr_Password *samdb_result_hash(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, const char *attr)
552 struct samr_Password *hash = NULL;
553 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
554 if (val && (val->length >= sizeof(hash->hash))) {
555 hash = talloc(mem_ctx, struct samr_Password);
556 memcpy(hash->hash, val->data, MIN(val->length, sizeof(hash->hash)));
562 pull an array of samr_Password structures from a result set.
564 unsigned int samdb_result_hashes(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
565 const char *attr, struct samr_Password **hashes)
567 unsigned int count, i;
568 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
574 count = val->length / 16;
579 *hashes = talloc_array(mem_ctx, struct samr_Password, count);
584 for (i=0;i<count;i++) {
585 memcpy((*hashes)[i].hash, (i*16)+(char *)val->data, 16);
591 NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct ldb_message *msg,
592 struct samr_Password **lm_pwd, struct samr_Password **nt_pwd)
594 struct samr_Password *lmPwdHash, *ntPwdHash;
597 num_nt = samdb_result_hashes(mem_ctx, msg, "unicodePwd", &ntPwdHash);
600 } else if (num_nt > 1) {
601 return NT_STATUS_INTERNAL_DB_CORRUPTION;
603 *nt_pwd = &ntPwdHash[0];
607 /* Ensure that if we have turned off LM
608 * authentication, that we never use the LM hash, even
610 if (lp_lanman_auth(lp_ctx)) {
612 num_lm = samdb_result_hashes(mem_ctx, msg, "dBCSPwd", &lmPwdHash);
615 } else if (num_lm > 1) {
616 return NT_STATUS_INTERNAL_DB_CORRUPTION;
618 *lm_pwd = &lmPwdHash[0];
628 pull a samr_LogonHours structutre from a result set.
630 struct samr_LogonHours samdb_result_logon_hours(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char *attr)
632 struct samr_LogonHours hours;
633 size_t units_per_week = 168;
634 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
639 units_per_week = val->length * 8;
642 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week/8);
646 hours.units_per_week = units_per_week;
647 memset(hours.bits, 0xFF, units_per_week/8);
649 memcpy(hours.bits, val->data, val->length);
656 pull a set of account_flags from a result set.
658 This requires that the attributes:
663 uint32_t samdb_result_acct_flags(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
664 struct ldb_message *msg, struct ldb_dn *domain_dn)
666 uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
667 uint32_t acct_flags = ds_uf2acb(userAccountControl);
668 NTTIME must_change_time;
671 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
674 /* Test account expire time */
675 unix_to_nt_time(&now, time(NULL));
676 /* check for expired password */
677 if (must_change_time < now) {
678 acct_flags |= ACB_PW_EXPIRED;
683 struct lsa_BinaryString samdb_result_parameters(TALLOC_CTX *mem_ctx,
684 struct ldb_message *msg,
687 struct lsa_BinaryString s;
688 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
696 s.array = talloc_array(mem_ctx, uint16_t, val->length/2);
700 s.length = s.size = val->length;
701 memcpy(s.array, val->data, val->length);
706 /* Find an attribute, with a particular value */
708 /* The current callers of this function expect a very specific
709 * behaviour: In particular, objectClass subclass equivilance is not
710 * wanted. This means that we should not lookup the schema for the
711 * comparison function */
712 struct ldb_message_element *samdb_find_attribute(struct ldb_context *ldb,
713 const struct ldb_message *msg,
714 const char *name, const char *value)
717 struct ldb_message_element *el = ldb_msg_find_element(msg, name);
723 for (i=0;i<el->num_values;i++) {
724 if (ldb_attr_cmp(value, (char *)el->values[i].data) == 0) {
733 * This is intended for use by the "password hash" module since there
734 * password changes can be specified through one message element with the
735 * new password (to set) and another one with the old password (to unset).
737 * The first which sets a password (new value) can have flags
738 * (LDB_FLAG_MOD_ADD, LDB_FLAG_MOD_REPLACE) but also none (on "add" operations
739 * for entries). The latter (old value) has always specified
740 * LDB_FLAG_MOD_DELETE.
742 * Returns LDB_ERR_NO_SUCH_ATTRIBUTE if the attribute which should be deleted
743 * doesn't contain only one value (this is the Windows Server behaviour)
744 * otherwise LDB_SUCCESS.
746 int samdb_msg_find_old_and_new_ldb_val(const struct ldb_message *msg,
748 const struct ldb_val **new_val,
749 const struct ldb_val **old_val)
760 for (i = 0; i < msg->num_elements; i++) {
761 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
762 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
763 *old_val = &msg->elements[i].values[0];
765 *new_val = &msg->elements[i].values[0];
773 int samdb_find_or_add_value(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
775 if (samdb_find_attribute(ldb, msg, name, set_value) == NULL) {
776 return samdb_msg_add_string(ldb, msg, msg, name, set_value);
781 int samdb_find_or_add_attribute(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
783 struct ldb_message_element *el;
785 el = ldb_msg_find_element(msg, name);
790 return samdb_msg_add_string(ldb, msg, msg, name, set_value);
796 add a string element to a message
798 int samdb_msg_add_string(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
799 const char *attr_name, const char *str)
801 char *s = talloc_strdup(mem_ctx, str);
802 char *a = talloc_strdup(mem_ctx, attr_name);
803 if (s == NULL || a == NULL) {
804 return LDB_ERR_OPERATIONS_ERROR;
806 return ldb_msg_add_string(msg, a, s);
810 add a dom_sid element to a message
812 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
813 const char *attr_name, struct dom_sid *sid)
816 enum ndr_err_code ndr_err;
818 ndr_err = ndr_push_struct_blob(&v, mem_ctx,
820 (ndr_push_flags_fn_t)ndr_push_dom_sid);
821 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
822 return LDB_ERR_OPERATIONS_ERROR;
824 return ldb_msg_add_value(msg, attr_name, &v, NULL);
829 add a delete element operation to a message
831 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
832 const char *attr_name)
834 /* we use an empty replace rather than a delete, as it allows for
835 dsdb_replace() to be used everywhere */
836 return ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_REPLACE, NULL);
840 add an add attribute value to a message or enhance an existing attribute
841 which has the same name and the add flag set.
843 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
844 struct ldb_message *msg, const char *attr_name,
847 struct ldb_message_element *el;
848 struct ldb_val val, *vals;
854 v = talloc_strdup(mem_ctx, value);
856 return LDB_ERR_OPERATIONS_ERROR;
859 val.data = (uint8_t *) v;
860 val.length = strlen(v);
862 if (val.length == 0) {
863 /* allow empty strings as non-existent attributes */
867 for (i = 0; i < msg->num_elements; i++) {
868 el = &msg->elements[i];
869 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
870 (el->flags == LDB_FLAG_MOD_ADD)) {
876 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_ADD,
878 if (ret != LDB_SUCCESS) {
883 vals = talloc_realloc(msg, el->values, struct ldb_val,
886 return LDB_ERR_OPERATIONS_ERROR;
889 el->values[el->num_values] = val;
896 add a delete attribute value to a message or enhance an existing attribute
897 which has the same name and the delete flag set.
899 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
900 struct ldb_message *msg, const char *attr_name,
903 struct ldb_message_element *el;
904 struct ldb_val val, *vals;
910 v = talloc_strdup(mem_ctx, value);
912 return LDB_ERR_OPERATIONS_ERROR;
915 val.data = (uint8_t *) v;
916 val.length = strlen(v);
918 if (val.length == 0) {
919 /* allow empty strings as non-existent attributes */
923 for (i = 0; i < msg->num_elements; i++) {
924 el = &msg->elements[i];
925 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
926 (el->flags == LDB_FLAG_MOD_DELETE)) {
932 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_DELETE,
934 if (ret != LDB_SUCCESS) {
939 vals = talloc_realloc(msg, el->values, struct ldb_val,
942 return LDB_ERR_OPERATIONS_ERROR;
945 el->values[el->num_values] = val;
952 add a int element to a message
954 int samdb_msg_add_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
955 const char *attr_name, int v)
957 const char *s = talloc_asprintf(mem_ctx, "%d", v);
958 return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, s);
962 add a unsigned int element to a message
964 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
965 const char *attr_name, unsigned int v)
967 return samdb_msg_add_int(sam_ldb, mem_ctx, msg, attr_name, (int)v);
971 add a (signed) int64_t element to a message
973 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
974 const char *attr_name, int64_t v)
976 const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
977 return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, s);
981 add a uint64_t element to a message
983 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
984 const char *attr_name, uint64_t v)
986 return samdb_msg_add_int64(sam_ldb, mem_ctx, msg, attr_name, (int64_t)v);
990 add a samr_Password element to a message
992 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
993 const char *attr_name, struct samr_Password *hash)
996 val.data = talloc_memdup(mem_ctx, hash->hash, 16);
998 return LDB_ERR_OPERATIONS_ERROR;
1001 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1005 add a samr_Password array to a message
1007 int samdb_msg_add_hashes(TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1008 const char *attr_name, struct samr_Password *hashes,
1013 val.data = talloc_array_size(mem_ctx, 16, count);
1014 val.length = count*16;
1016 return LDB_ERR_OPERATIONS_ERROR;
1018 for (i=0;i<count;i++) {
1019 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
1021 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1025 add a acct_flags element to a message
1027 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1028 const char *attr_name, uint32_t v)
1030 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
1034 add a logon_hours element to a message
1036 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1037 const char *attr_name, struct samr_LogonHours *hours)
1040 val.length = hours->units_per_week / 8;
1041 val.data = hours->bits;
1042 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1046 add a parameters element to a message
1048 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1049 const char *attr_name, struct lsa_BinaryString *parameters)
1052 val.length = parameters->length;
1053 val.data = (uint8_t *)parameters->array;
1054 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1057 add a general value element to a message
1059 int samdb_msg_add_value(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1060 const char *attr_name, const struct ldb_val *val)
1062 return ldb_msg_add_value(msg, attr_name, val, NULL);
1066 sets a general value element to a message
1068 int samdb_msg_set_value(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1069 const char *attr_name, const struct ldb_val *val)
1071 struct ldb_message_element *el;
1073 el = ldb_msg_find_element(msg, attr_name);
1077 return ldb_msg_add_value(msg, attr_name, val, NULL);
1081 set a string element in a message
1083 int samdb_msg_set_string(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1084 const char *attr_name, const char *str)
1086 struct ldb_message_element *el;
1088 el = ldb_msg_find_element(msg, attr_name);
1092 return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, str);
1096 * Handle ldb_request in transaction
1098 static int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
1099 struct ldb_request *req)
1103 ret = ldb_transaction_start(sam_ldb);
1104 if (ret != LDB_SUCCESS) {
1108 ret = ldb_request(sam_ldb, req);
1109 if (ret == LDB_SUCCESS) {
1110 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1113 if (ret == LDB_SUCCESS) {
1114 return ldb_transaction_commit(sam_ldb);
1116 ldb_transaction_cancel(sam_ldb);
1122 return a default security descriptor
1124 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1126 struct security_descriptor *sd;
1128 sd = security_descriptor_initialise(mem_ctx);
1133 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1135 struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1136 struct ldb_dn *aggregate_dn;
1141 aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1142 if (!aggregate_dn) {
1145 if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1148 return aggregate_dn;
1151 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1153 struct ldb_dn *new_dn;
1155 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1156 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1157 talloc_free(new_dn);
1163 struct ldb_dn *samdb_infrastructure_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1165 struct ldb_dn *new_dn;
1167 new_dn = ldb_dn_copy(mem_ctx, ldb_get_default_basedn(sam_ctx));
1168 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Infrastructure")) {
1169 talloc_free(new_dn);
1175 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1177 struct ldb_dn *new_dn;
1179 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1180 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1181 talloc_free(new_dn);
1188 work out the domain sid for the current open ldb
1190 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1192 TALLOC_CTX *tmp_ctx;
1193 const struct dom_sid *domain_sid;
1194 const char *attrs[] = {
1198 struct ldb_result *res;
1201 /* see if we have a cached copy */
1202 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1207 tmp_ctx = talloc_new(ldb);
1208 if (tmp_ctx == NULL) {
1212 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1214 if (ret != LDB_SUCCESS) {
1218 if (res->count != 1) {
1222 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1223 if (domain_sid == NULL) {
1227 /* cache the domain_sid in the ldb */
1228 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1232 talloc_steal(ldb, domain_sid);
1233 talloc_free(tmp_ctx);
1238 talloc_free(tmp_ctx);
1243 get domain sid from cache
1245 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1247 return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1250 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1252 TALLOC_CTX *tmp_ctx;
1253 struct dom_sid *dom_sid_new;
1254 struct dom_sid *dom_sid_old;
1256 /* see if we have a cached copy */
1257 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1258 "cache.domain_sid"), struct dom_sid);
1260 tmp_ctx = talloc_new(ldb);
1261 if (tmp_ctx == NULL) {
1265 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1270 /* cache the domain_sid in the ldb */
1271 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1275 talloc_steal(ldb, dom_sid_new);
1276 talloc_free(tmp_ctx);
1277 talloc_free(dom_sid_old);
1282 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1283 talloc_free(tmp_ctx);
1287 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
1289 TALLOC_CTX *tmp_ctx;
1290 struct ldb_dn *ntds_settings_dn_new;
1291 struct ldb_dn *ntds_settings_dn_old;
1293 /* see if we have a cached copy */
1294 ntds_settings_dn_old = talloc_get_type(ldb_get_opaque(ldb,
1295 "cache.ntds_settings_dn"), struct ldb_dn);
1297 tmp_ctx = talloc_new(ldb);
1298 if (tmp_ctx == NULL) {
1302 ntds_settings_dn_new = ldb_dn_copy(tmp_ctx, ntds_settings_dn_in);
1303 if (!ntds_settings_dn_new) {
1307 /* cache the domain_sid in the ldb */
1308 if (ldb_set_opaque(ldb, "cache.ntds_settings_dn", ntds_settings_dn_new) != LDB_SUCCESS) {
1312 talloc_steal(ldb, ntds_settings_dn_new);
1313 talloc_free(tmp_ctx);
1314 talloc_free(ntds_settings_dn_old);
1319 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1320 talloc_free(tmp_ctx);
1324 /* Obtain the short name of the flexible single master operator
1325 * (FSMO), such as the PDC Emulator */
1326 const char *samdb_result_fsmo_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
1329 /* Format is cn=NTDS Settings,cn=<NETBIOS name of FSMO>,.... */
1330 struct ldb_dn *fsmo_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, msg, attr);
1331 const struct ldb_val *val = ldb_dn_get_component_val(fsmo_dn, 1);
1332 const char *name = ldb_dn_get_component_name(fsmo_dn, 1);
1334 if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
1335 /* Ensure this matches the format. This gives us a
1336 * bit more confidence that a 'cn' value will be a
1341 return (char *)val->data;
1347 work out the ntds settings dn for the current open ldb
1349 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb)
1351 TALLOC_CTX *tmp_ctx;
1352 const char *root_attrs[] = { "dsServiceName", NULL };
1354 struct ldb_result *root_res;
1355 struct ldb_dn *settings_dn;
1357 /* see if we have a cached copy */
1358 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "cache.ntds_settings_dn");
1363 tmp_ctx = talloc_new(ldb);
1364 if (tmp_ctx == NULL) {
1368 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1370 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1371 ldb_errstring(ldb)));
1375 if (root_res->count != 1) {
1379 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1381 /* cache the domain_sid in the ldb */
1382 if (ldb_set_opaque(ldb, "cache.settings_dn", settings_dn) != LDB_SUCCESS) {
1386 talloc_steal(ldb, settings_dn);
1387 talloc_free(tmp_ctx);
1392 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1393 talloc_free(tmp_ctx);
1398 work out the ntds settings invocationId for the current open ldb
1400 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1402 TALLOC_CTX *tmp_ctx;
1403 const char *attrs[] = { "invocationId", NULL };
1405 struct ldb_result *res;
1406 struct GUID *invocation_id;
1408 /* see if we have a cached copy */
1409 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1410 if (invocation_id) {
1411 return invocation_id;
1414 tmp_ctx = talloc_new(ldb);
1415 if (tmp_ctx == NULL) {
1419 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1424 if (res->count != 1) {
1428 invocation_id = talloc(tmp_ctx, struct GUID);
1429 if (!invocation_id) {
1433 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1435 /* cache the domain_sid in the ldb */
1436 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1440 talloc_steal(ldb, invocation_id);
1441 talloc_free(tmp_ctx);
1443 return invocation_id;
1446 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1447 talloc_free(tmp_ctx);
1451 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1453 TALLOC_CTX *tmp_ctx;
1454 struct GUID *invocation_id_new;
1455 struct GUID *invocation_id_old;
1457 /* see if we have a cached copy */
1458 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1459 "cache.invocation_id");
1461 tmp_ctx = talloc_new(ldb);
1462 if (tmp_ctx == NULL) {
1466 invocation_id_new = talloc(tmp_ctx, struct GUID);
1467 if (!invocation_id_new) {
1471 *invocation_id_new = *invocation_id_in;
1473 /* cache the domain_sid in the ldb */
1474 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1478 talloc_steal(ldb, invocation_id_new);
1479 talloc_free(tmp_ctx);
1480 talloc_free(invocation_id_old);
1485 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1486 talloc_free(tmp_ctx);
1491 work out the ntds settings objectGUID for the current open ldb
1493 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1495 TALLOC_CTX *tmp_ctx;
1496 const char *attrs[] = { "objectGUID", NULL };
1498 struct ldb_result *res;
1499 struct GUID *ntds_guid;
1501 /* see if we have a cached copy */
1502 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1507 tmp_ctx = talloc_new(ldb);
1508 if (tmp_ctx == NULL) {
1512 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1517 if (res->count != 1) {
1521 ntds_guid = talloc(tmp_ctx, struct GUID);
1526 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1528 /* cache the domain_sid in the ldb */
1529 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1533 talloc_steal(ldb, ntds_guid);
1534 talloc_free(tmp_ctx);
1539 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1540 talloc_free(tmp_ctx);
1544 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1546 TALLOC_CTX *tmp_ctx;
1547 struct GUID *ntds_guid_new;
1548 struct GUID *ntds_guid_old;
1550 /* see if we have a cached copy */
1551 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1553 tmp_ctx = talloc_new(ldb);
1554 if (tmp_ctx == NULL) {
1558 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1559 if (!ntds_guid_new) {
1563 *ntds_guid_new = *ntds_guid_in;
1565 /* cache the domain_sid in the ldb */
1566 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1570 talloc_steal(ldb, ntds_guid_new);
1571 talloc_free(tmp_ctx);
1572 talloc_free(ntds_guid_old);
1577 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1578 talloc_free(tmp_ctx);
1583 work out the server dn for the current open ldb
1585 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1587 return ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb));
1591 work out the server dn for the current open ldb
1593 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1595 struct ldb_dn *server_dn;
1596 struct ldb_dn *servers_dn;
1597 struct ldb_dn *server_site_dn;
1599 /* TODO: there must be a saner way to do this!! */
1600 server_dn = samdb_server_dn(ldb, mem_ctx);
1601 if (!server_dn) return NULL;
1603 servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1604 talloc_free(server_dn);
1605 if (!servers_dn) return NULL;
1607 server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1608 talloc_free(servers_dn);
1610 return server_site_dn;
1614 find a 'reference' DN that points at another object
1615 (eg. serverReference, rIDManagerReference etc)
1617 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1618 const char *attribute, struct ldb_dn **dn)
1620 const char *attrs[2];
1621 struct ldb_result *res;
1624 attrs[0] = attribute;
1627 ret = ldb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, NULL);
1628 if (ret != LDB_SUCCESS) {
1631 if (res->count != 1) {
1633 return LDB_ERR_NO_SUCH_OBJECT;
1636 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1639 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1647 find our machine account via the serverReference attribute in the
1650 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1652 struct ldb_dn *server_dn;
1655 server_dn = samdb_server_dn(ldb, mem_ctx);
1656 if (server_dn == NULL) {
1657 return LDB_ERR_NO_SUCH_OBJECT;
1660 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1661 talloc_free(server_dn);
1667 find the RID Manager$ DN via the rIDManagerReference attribute in the
1670 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1672 return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1673 "rIDManagerReference", dn);
1677 find the RID Set DN via the rIDSetReferences attribute in our
1680 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1682 struct ldb_dn *server_ref_dn;
1685 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1686 if (ret != LDB_SUCCESS) {
1689 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1690 talloc_free(server_ref_dn);
1694 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1696 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1703 return (const char *) val->data;
1707 * Finds the client site by using the client's IP address.
1708 * The "subnet_name" returns the name of the subnet if parameter != NULL
1710 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1711 const char *ip_address, char **subnet_name)
1713 const char *attrs[] = { "cn", "siteObject", NULL };
1714 struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
1715 struct ldb_result *res;
1716 const struct ldb_val *val;
1717 const char *site_name = NULL, *l_subnet_name = NULL;
1718 const char *allow_list[2] = { NULL, NULL };
1719 unsigned int i, count;
1723 * if we don't have a client ip e.g. ncalrpc
1724 * the server site is the client site
1726 if (ip_address == NULL) {
1727 return samdb_server_site_name(ldb, mem_ctx);
1730 sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1731 if (sites_container_dn == NULL) {
1735 subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1736 if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1737 talloc_free(sites_container_dn);
1738 talloc_free(subnets_dn);
1742 ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1744 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1746 } else if (ret != LDB_SUCCESS) {
1747 talloc_free(sites_container_dn);
1748 talloc_free(subnets_dn);
1754 for (i = 0; i < count; i++) {
1755 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1758 allow_list[0] = l_subnet_name;
1760 if (allow_access(mem_ctx, NULL, allow_list, "", ip_address)) {
1761 sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1764 if (sites_dn == NULL) {
1765 /* No reference, maybe another subnet matches */
1769 /* "val" cannot be NULL here since "sites_dn" != NULL */
1770 val = ldb_dn_get_rdn_val(sites_dn);
1771 site_name = talloc_strdup(mem_ctx,
1772 (const char *) val->data);
1774 talloc_free(sites_dn);
1780 if (site_name == NULL) {
1781 /* This is the Windows Server fallback rule: when no subnet
1782 * exists and we have only one site available then use it (it
1783 * is for sure the same as our server site). If more sites do
1784 * exist then we don't know which one to use and set the site
1786 cnt = samdb_search_count(ldb, sites_container_dn,
1787 "(objectClass=site)");
1789 site_name = samdb_server_site_name(ldb, mem_ctx);
1791 site_name = talloc_strdup(mem_ctx, "");
1793 l_subnet_name = NULL;
1796 if (subnet_name != NULL) {
1797 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1800 talloc_free(sites_container_dn);
1801 talloc_free(subnets_dn);
1808 work out if we are the PDC for the domain of the current open ldb
1810 bool samdb_is_pdc(struct ldb_context *ldb)
1812 const char *dom_attrs[] = { "fSMORoleOwner", NULL };
1814 struct ldb_result *dom_res;
1815 TALLOC_CTX *tmp_ctx;
1819 tmp_ctx = talloc_new(ldb);
1820 if (tmp_ctx == NULL) {
1821 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1825 ret = ldb_search(ldb, tmp_ctx, &dom_res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, dom_attrs, NULL);
1827 DEBUG(1,("Searching for fSMORoleOwner in %s failed: %s\n",
1828 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1829 ldb_errstring(ldb)));
1832 if (dom_res->count != 1) {
1836 pdc = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, dom_res->msgs[0], "fSMORoleOwner");
1838 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), pdc) == 0) {
1844 talloc_free(tmp_ctx);
1849 DEBUG(1,("Failed to find if we are the PDC for this ldb\n"));
1850 talloc_free(tmp_ctx);
1855 work out if we are a Global Catalog server for the domain of the current open ldb
1857 bool samdb_is_gc(struct ldb_context *ldb)
1859 const char *attrs[] = { "options", NULL };
1861 struct ldb_result *res;
1862 TALLOC_CTX *tmp_ctx;
1864 tmp_ctx = talloc_new(ldb);
1865 if (tmp_ctx == NULL) {
1866 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1870 /* Query cn=ntds settings,.... */
1871 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1873 talloc_free(tmp_ctx);
1876 if (res->count != 1) {
1877 talloc_free(tmp_ctx);
1881 options = ldb_msg_find_attr_as_int(res->msgs[0], "options", 0);
1882 talloc_free(tmp_ctx);
1884 /* if options attribute has the 0x00000001 flag set, then enable the global catlog */
1885 if (options & 0x000000001) {
1891 /* Find a domain object in the parents of a particular DN. */
1892 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1893 struct ldb_dn **parent_dn, const char **errstring)
1895 TALLOC_CTX *local_ctx;
1896 struct ldb_dn *sdn = dn;
1897 struct ldb_result *res = NULL;
1899 const char *attrs[] = { NULL };
1901 local_ctx = talloc_new(mem_ctx);
1902 if (local_ctx == NULL) return LDB_ERR_OPERATIONS_ERROR;
1904 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1905 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1906 "(|(objectClass=domain)(objectClass=builtinDomain))");
1907 if (ret == LDB_SUCCESS) {
1908 if (res->count == 1) {
1916 if (ret != LDB_SUCCESS) {
1917 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1918 ldb_dn_get_linearized(dn),
1919 ldb_dn_get_linearized(sdn),
1920 ldb_errstring(ldb));
1921 talloc_free(local_ctx);
1924 if (res->count != 1) {
1925 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1926 ldb_dn_get_linearized(dn));
1927 DEBUG(0,(__location__ ": %s\n", *errstring));
1928 talloc_free(local_ctx);
1929 return LDB_ERR_CONSTRAINT_VIOLATION;
1932 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1933 talloc_free(local_ctx);
1939 * Performs checks on a user password (plaintext UNIX format - attribute
1940 * "password"). The remaining parameters have to be extracted from the domain
1943 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1945 enum samr_ValidationStatus samdb_check_password(const DATA_BLOB *password,
1946 const uint32_t pwdProperties,
1947 const uint32_t minPwdLength)
1949 /* checks if the "minPwdLength" property is satisfied */
1950 if (minPwdLength > password->length)
1951 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
1953 /* checks the password complexity */
1954 if (((pwdProperties & DOMAIN_PASSWORD_COMPLEX) != 0)
1955 && (password->data != NULL)
1956 && (!check_password_quality((const char *) password->data)))
1957 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1959 return SAMR_VALIDATION_STATUS_SUCCESS;
1963 * Callback for "samdb_set_password" password change
1965 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
1970 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1973 if (ares->error != LDB_SUCCESS) {
1975 req->context = talloc_steal(req,
1976 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1978 return ldb_request_done(req, ret);
1981 if (ares->type != LDB_REPLY_DONE) {
1983 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1986 req->context = talloc_steal(req,
1987 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1989 return ldb_request_done(req, LDB_SUCCESS);
1993 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1994 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1995 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
1996 * gives some more informations if the changed failed.
1998 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
1999 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2000 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2002 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2003 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2004 const DATA_BLOB *new_password,
2005 struct samr_Password *lmNewHash,
2006 struct samr_Password *ntNewHash,
2008 enum samPwdChangeReason *reject_reason,
2009 struct samr_DomInfo1 **_dominfo)
2011 struct ldb_message *msg;
2012 struct ldb_message_element *el;
2013 struct ldb_request *req;
2014 struct dsdb_control_password_change_status *pwd_stat = NULL;
2018 #define CHECK_RET(x) \
2019 if (x != LDB_SUCCESS) { \
2021 return NT_STATUS_NO_MEMORY; \
2024 msg = ldb_msg_new(mem_ctx);
2026 return NT_STATUS_NO_MEMORY;
2029 if ((new_password != NULL)
2030 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
2031 /* we have the password as plaintext UTF16 */
2032 CHECK_RET(samdb_msg_add_value(ldb, mem_ctx, msg,
2033 "clearTextPassword", new_password));
2034 el = ldb_msg_find_element(msg, "clearTextPassword");
2035 el->flags = LDB_FLAG_MOD_REPLACE;
2036 } else if ((new_password == NULL)
2037 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2038 /* we have a password as LM and/or NT hash */
2039 if (lmNewHash != NULL) {
2040 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2041 "dBCSPwd", lmNewHash));
2042 el = ldb_msg_find_element(msg, "dBCSPwd");
2043 el->flags = LDB_FLAG_MOD_REPLACE;
2045 if (ntNewHash != NULL) {
2046 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2047 "unicodePwd", ntNewHash));
2048 el = ldb_msg_find_element(msg, "unicodePwd");
2049 el->flags = LDB_FLAG_MOD_REPLACE;
2052 /* the password wasn't specified correctly */
2054 return NT_STATUS_INVALID_PARAMETER;
2057 /* build modify request */
2058 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2059 samdb_set_password_callback, NULL);
2060 if (ret != LDB_SUCCESS) {
2062 return NT_STATUS_NO_MEMORY;
2066 /* a user password change and we've checked already the old
2067 * password somewhere else (callers responsability) */
2068 ret = ldb_request_add_control(req,
2069 DSDB_CONTROL_PASSWORD_CHANGE_OLD_PW_CHECKED_OID,
2071 if (ret != LDB_SUCCESS) {
2074 return NT_STATUS_NO_MEMORY;
2077 ret = ldb_request_add_control(req,
2078 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2080 if (ret != LDB_SUCCESS) {
2083 return NT_STATUS_NO_MEMORY;
2085 ret = ldb_request_add_control(req,
2086 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2088 if (ret != LDB_SUCCESS) {
2091 return NT_STATUS_NO_MEMORY;
2094 ret = dsdb_autotransaction_request(ldb, req);
2096 if (req->context != NULL) {
2097 pwd_stat = talloc_steal(mem_ctx,
2098 ((struct ldb_control *)req->context)->data);
2104 /* Sets the domain info (if requested) */
2105 if (_dominfo != NULL) {
2106 struct samr_DomInfo1 *dominfo;
2108 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2109 if (dominfo == NULL) {
2110 return NT_STATUS_NO_MEMORY;
2113 if (pwd_stat != NULL) {
2114 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2115 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2116 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2117 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2118 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2121 *_dominfo = dominfo;
2124 if (reject_reason != NULL) {
2125 if (pwd_stat != NULL) {
2126 *reject_reason = pwd_stat->reject_reason;
2128 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2132 if (pwd_stat != NULL) {
2133 talloc_free(pwd_stat);
2136 /* TODO: Error results taken from "password_hash" module. Are they
2138 if (ret == LDB_ERR_UNWILLING_TO_PERFORM) {
2139 status = NT_STATUS_WRONG_PASSWORD;
2140 } else if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2141 status = NT_STATUS_PASSWORD_RESTRICTION;
2142 } else if (ret != LDB_SUCCESS) {
2143 status = NT_STATUS_UNSUCCESSFUL;
2145 status = NT_STATUS_OK;
2152 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2153 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2154 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
2155 * gives some more informations if the changed failed.
2157 * This wrapper function for "samdb_set_password" takes a SID as input rather
2160 * This call encapsulates a new LDB transaction for changing the password;
2161 * therefore the user hasn't to start a new one.
2163 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2164 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2165 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2167 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2168 const struct dom_sid *user_sid,
2169 const DATA_BLOB *new_password,
2170 struct samr_Password *lmNewHash,
2171 struct samr_Password *ntNewHash,
2173 enum samPwdChangeReason *reject_reason,
2174 struct samr_DomInfo1 **_dominfo)
2177 struct ldb_dn *user_dn;
2180 ret = ldb_transaction_start(ldb);
2181 if (ret != LDB_SUCCESS) {
2182 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2183 return NT_STATUS_TRANSACTION_ABORTED;
2186 user_dn = samdb_search_dn(ldb, mem_ctx, NULL,
2187 "(&(objectSid=%s)(objectClass=user))",
2188 ldap_encode_ndr_dom_sid(mem_ctx, user_sid));
2190 ldb_transaction_cancel(ldb);
2191 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2192 dom_sid_string(mem_ctx, user_sid)));
2193 return NT_STATUS_NO_SUCH_USER;
2196 nt_status = samdb_set_password(ldb, mem_ctx,
2199 lmNewHash, ntNewHash,
2201 reject_reason, _dominfo);
2202 if (!NT_STATUS_IS_OK(nt_status)) {
2203 ldb_transaction_cancel(ldb);
2204 talloc_free(user_dn);
2208 ret = ldb_transaction_commit(ldb);
2209 if (ret != LDB_SUCCESS) {
2210 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2211 ldb_dn_get_linearized(user_dn),
2212 ldb_errstring(ldb)));
2213 talloc_free(user_dn);
2214 return NT_STATUS_TRANSACTION_ABORTED;
2217 talloc_free(user_dn);
2218 return NT_STATUS_OK;
2222 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2223 struct dom_sid *sid, struct ldb_dn **ret_dn)
2225 struct ldb_message *msg;
2226 struct ldb_dn *basedn;
2230 sidstr = dom_sid_string(mem_ctx, sid);
2231 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2233 /* We might have to create a ForeignSecurityPrincipal, even if this user
2234 * is in our own domain */
2236 msg = ldb_msg_new(sidstr);
2238 talloc_free(sidstr);
2239 return NT_STATUS_NO_MEMORY;
2242 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2243 ldb_get_default_basedn(sam_ctx),
2244 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2246 if (ret != LDB_SUCCESS) {
2247 DEBUG(0, ("Failed to find DN for "
2248 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2249 talloc_free(sidstr);
2250 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2253 /* add core elements to the ldb_message for the alias */
2255 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2256 talloc_free(sidstr);
2257 return NT_STATUS_NO_MEMORY;
2260 samdb_msg_add_string(sam_ctx, msg, msg,
2262 "foreignSecurityPrincipal");
2264 /* create the alias */
2265 ret = ldb_add(sam_ctx, msg);
2266 if (ret != LDB_SUCCESS) {
2267 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2269 ldb_dn_get_linearized(msg->dn),
2270 ldb_errstring(sam_ctx)));
2271 talloc_free(sidstr);
2272 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2275 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2276 talloc_free(sidstr);
2278 return NT_STATUS_OK;
2283 Find the DN of a domain, assuming it to be a dotted.dns name
2286 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2289 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2290 const char *binary_encoded;
2291 const char **split_realm;
2298 split_realm = (const char **)str_list_make(tmp_ctx, dns_domain, ".");
2300 talloc_free(tmp_ctx);
2303 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2304 for (i=0; split_realm[i]; i++) {
2305 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2306 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2307 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2308 binary_encoded, ldb_dn_get_linearized(dn)));
2309 talloc_free(tmp_ctx);
2313 if (!ldb_dn_validate(dn)) {
2314 DEBUG(2, ("Failed to validated DN %s\n",
2315 ldb_dn_get_linearized(dn)));
2316 talloc_free(tmp_ctx);
2319 talloc_free(tmp_ctx);
2324 Find the DN of a domain, be it the netbios or DNS name
2326 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2327 const char *domain_name)
2329 const char * const domain_ref_attrs[] = {
2332 const char * const domain_ref2_attrs[] = {
2335 struct ldb_result *res_domain_ref;
2336 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2337 /* find the domain's DN */
2338 int ret_domain = ldb_search(ldb, mem_ctx,
2340 samdb_partitions_dn(ldb, mem_ctx),
2343 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2345 if (ret_domain != 0) {
2349 if (res_domain_ref->count == 0) {
2350 ret_domain = ldb_search(ldb, mem_ctx,
2352 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2355 "(objectclass=domain)");
2356 if (ret_domain != 0) {
2360 if (res_domain_ref->count == 1) {
2361 return res_domain_ref->msgs[0]->dn;
2366 if (res_domain_ref->count > 1) {
2367 DEBUG(0,("Found %d records matching domain [%s]\n",
2368 ret_domain, domain_name));
2372 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
2378 use a GUID to find a DN
2380 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
2381 TALLOC_CTX *mem_ctx,
2382 const struct GUID *guid, struct ldb_dn **dn)
2385 struct ldb_result *res;
2386 const char *attrs[] = { NULL };
2387 char *guid_str = GUID_string(mem_ctx, guid);
2390 return LDB_ERR_OPERATIONS_ERROR;
2393 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2394 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2395 DSDB_SEARCH_SHOW_EXTENDED_DN |
2396 DSDB_SEARCH_ONE_ONLY,
2397 "objectGUID=%s", guid_str);
2398 talloc_free(guid_str);
2399 if (ret != LDB_SUCCESS) {
2403 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2410 use a DN to find a GUID with a given attribute name
2412 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
2413 struct ldb_dn *dn, const char *attribute,
2417 struct ldb_result *res;
2418 const char *attrs[2];
2419 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2421 attrs[0] = attribute;
2424 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs, DSDB_SEARCH_SHOW_DELETED);
2425 if (ret != LDB_SUCCESS) {
2426 talloc_free(tmp_ctx);
2429 if (res->count < 1) {
2430 talloc_free(tmp_ctx);
2431 return LDB_ERR_NO_SUCH_OBJECT;
2433 *guid = samdb_result_guid(res->msgs[0], attribute);
2434 talloc_free(tmp_ctx);
2439 use a DN to find a GUID
2441 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
2442 struct ldb_dn *dn, struct GUID *guid)
2444 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
2450 adds the given GUID to the given ldb_message. This value is added
2451 for the given attr_name (may be either "objectGUID" or "parentGUID").
2453 int dsdb_msg_add_guid(struct ldb_message *msg,
2455 const char *attr_name)
2460 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
2462 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
2463 if (!NT_STATUS_IS_OK(status)) {
2464 ret = LDB_ERR_OPERATIONS_ERROR;
2468 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
2469 if (ret != LDB_SUCCESS) {
2470 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
2478 talloc_free(tmp_ctx);
2485 use a DN to find a SID
2487 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
2488 struct ldb_dn *dn, struct dom_sid *sid)
2491 struct ldb_result *res;
2492 const char *attrs[] = { "objectSID", NULL };
2493 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2498 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs, DSDB_SEARCH_SHOW_DELETED);
2499 if (ret != LDB_SUCCESS) {
2500 talloc_free(tmp_ctx);
2503 if (res->count < 1) {
2504 talloc_free(tmp_ctx);
2505 return LDB_ERR_NO_SUCH_OBJECT;
2507 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSID");
2509 talloc_free(tmp_ctx);
2510 return LDB_ERR_NO_SUCH_OBJECT;
2513 talloc_free(tmp_ctx);
2518 use a SID to find a DN
2520 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
2521 TALLOC_CTX *mem_ctx,
2522 struct dom_sid *sid, struct ldb_dn **dn)
2525 struct ldb_result *res;
2526 const char *attrs[] = { NULL };
2527 char *sid_str = dom_sid_string(mem_ctx, sid);
2530 return LDB_ERR_OPERATIONS_ERROR;
2533 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2534 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2535 DSDB_SEARCH_SHOW_EXTENDED_DN |
2536 DSDB_SEARCH_ONE_ONLY,
2537 "objectSID=%s", sid_str);
2538 talloc_free(sid_str);
2539 if (ret != LDB_SUCCESS) {
2543 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2550 load a repsFromTo blob list for a given partition GUID
2551 attr must be "repsFrom" or "repsTo"
2553 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2554 const char *attr, struct repsFromToBlob **r, uint32_t *count)
2556 const char *attrs[] = { attr, NULL };
2557 struct ldb_result *res = NULL;
2558 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2560 struct ldb_message_element *el;
2565 if (ldb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, NULL) != LDB_SUCCESS ||
2567 DEBUG(0,("dsdb_loadreps: failed to read partition object\n"));
2568 talloc_free(tmp_ctx);
2569 return WERR_DS_DRA_INTERNAL_ERROR;
2572 el = ldb_msg_find_element(res->msgs[0], attr);
2574 /* it's OK to be empty */
2575 talloc_free(tmp_ctx);
2579 *count = el->num_values;
2580 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
2582 talloc_free(tmp_ctx);
2583 return WERR_DS_DRA_INTERNAL_ERROR;
2586 for (i=0; i<(*count); i++) {
2587 enum ndr_err_code ndr_err;
2588 ndr_err = ndr_pull_struct_blob(&el->values[i],
2591 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
2592 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2593 talloc_free(tmp_ctx);
2594 return WERR_DS_DRA_INTERNAL_ERROR;
2598 talloc_free(tmp_ctx);
2604 save the repsFromTo blob list for a given partition GUID
2605 attr must be "repsFrom" or "repsTo"
2607 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2608 const char *attr, struct repsFromToBlob *r, uint32_t count)
2610 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2611 struct ldb_message *msg;
2612 struct ldb_message_element *el;
2615 msg = ldb_msg_new(tmp_ctx);
2617 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
2621 el->values = talloc_array(msg, struct ldb_val, count);
2626 for (i=0; i<count; i++) {
2628 enum ndr_err_code ndr_err;
2630 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
2632 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
2633 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2641 if (ldb_modify(sam_ctx, msg) != LDB_SUCCESS) {
2642 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
2646 talloc_free(tmp_ctx);
2651 talloc_free(tmp_ctx);
2652 return WERR_DS_DRA_INTERNAL_ERROR;
2657 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2658 object for a partition
2660 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
2661 uint64_t *uSN, uint64_t *urgent_uSN)
2663 struct ldb_request *req;
2665 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2666 struct dsdb_control_current_partition *p_ctrl;
2667 struct ldb_result *res;
2669 res = talloc_zero(tmp_ctx, struct ldb_result);
2671 talloc_free(tmp_ctx);
2672 return LDB_ERR_OPERATIONS_ERROR;
2675 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
2676 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
2680 res, ldb_search_default_callback,
2682 if (ret != LDB_SUCCESS) {
2683 talloc_free(tmp_ctx);
2687 p_ctrl = talloc(req, struct dsdb_control_current_partition);
2688 if (p_ctrl == NULL) {
2689 talloc_free(tmp_ctx);
2690 return LDB_ERR_OPERATIONS_ERROR;
2692 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2695 ret = ldb_request_add_control(req,
2696 DSDB_CONTROL_CURRENT_PARTITION_OID,
2698 if (ret != LDB_SUCCESS) {
2699 talloc_free(tmp_ctx);
2703 /* Run the new request */
2704 ret = ldb_request(ldb, req);
2706 if (ret == LDB_SUCCESS) {
2707 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2710 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2711 /* it hasn't been created yet, which means
2712 an implicit value of zero */
2714 talloc_free(tmp_ctx);
2718 if (ret != LDB_SUCCESS) {
2719 talloc_free(tmp_ctx);
2723 if (res->count < 1) {
2729 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
2731 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
2735 talloc_free(tmp_ctx);
2740 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
2741 const struct drsuapi_DsReplicaCursor2 *c2)
2743 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2746 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
2747 const struct drsuapi_DsReplicaCursor *c2)
2749 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2754 see if a computer identified by its invocationId is a RODC
2756 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
2758 /* 1) find the DN for this servers NTDSDSA object
2759 2) search for the msDS-isRODC attribute
2760 3) if not present then not a RODC
2761 4) if present and TRUE then is a RODC
2763 struct ldb_dn *config_dn;
2764 const char *attrs[] = { "msDS-isRODC", NULL };
2766 struct ldb_result *res;
2767 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
2769 config_dn = ldb_get_config_basedn(sam_ctx);
2771 talloc_free(tmp_ctx);
2772 return LDB_ERR_OPERATIONS_ERROR;
2775 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
2776 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
2778 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2780 talloc_free(tmp_ctx);
2784 if (ret != LDB_SUCCESS) {
2785 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
2786 GUID_string(tmp_ctx, objectGUID)));
2788 talloc_free(tmp_ctx);
2792 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
2793 *is_rodc = (ret == 1);
2795 talloc_free(tmp_ctx);
2801 see if we are a RODC
2803 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
2805 const struct GUID *objectGUID;
2809 /* see if we have a cached copy */
2810 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
2816 objectGUID = samdb_ntds_objectGUID(sam_ctx);
2818 return LDB_ERR_OPERATIONS_ERROR;
2821 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
2822 if (ret != LDB_SUCCESS) {
2826 cached = talloc(sam_ctx, bool);
2827 if (cached == NULL) {
2828 return LDB_ERR_OPERATIONS_ERROR;
2832 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
2833 if (ret != LDB_SUCCESS) {
2834 talloc_free(cached);
2835 return LDB_ERR_OPERATIONS_ERROR;
2841 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
2843 TALLOC_CTX *tmp_ctx;
2846 tmp_ctx = talloc_new(ldb);
2847 if (tmp_ctx == NULL) {
2851 cached = talloc(tmp_ctx, bool);
2857 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
2861 talloc_steal(ldb, cached);
2862 talloc_free(tmp_ctx);
2866 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
2867 talloc_free(tmp_ctx);
2873 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
2875 flags are DS_NTDS_OPTION_*
2877 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
2879 TALLOC_CTX *tmp_ctx;
2880 const char *attrs[] = { "options", NULL };
2882 struct ldb_result *res;
2884 tmp_ctx = talloc_new(ldb);
2885 if (tmp_ctx == NULL) {
2889 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
2894 if (res->count != 1) {
2898 *options = samdb_result_uint(res->msgs[0], "options", 0);
2900 talloc_free(tmp_ctx);
2905 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
2906 talloc_free(tmp_ctx);
2907 return LDB_ERR_NO_SUCH_OBJECT;
2910 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
2912 const char *attrs[] = { "objectCategory", NULL };
2914 struct ldb_result *res;
2916 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
2921 if (res->count != 1) {
2925 return samdb_result_string(res->msgs[0], "objectCategory", NULL);
2928 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
2933 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
2934 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
2936 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
2938 char **tokens, *ret;
2941 tokens = str_list_make(mem_ctx, cn, " -_");
2945 /* "tolower()" and "toupper()" should also work properly on 0x00 */
2946 tokens[0][0] = tolower(tokens[0][0]);
2947 for (i = 1; i < str_list_length((const char **)tokens); i++)
2948 tokens[i][0] = toupper(tokens[i][0]);
2950 ret = talloc_strdup(mem_ctx, tokens[0]);
2951 for (i = 1; i < str_list_length((const char **)tokens); i++)
2952 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
2954 talloc_free(tokens);
2960 return domain functional level
2961 returns DS_DOMAIN_FUNCTION_*
2963 int dsdb_functional_level(struct ldb_context *ldb)
2965 int *domainFunctionality =
2966 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
2967 if (!domainFunctionality) {
2968 DEBUG(0,(__location__ ": WARNING: domainFunctionality not setup\n"));
2969 return DS_DOMAIN_FUNCTION_2000;
2971 return *domainFunctionality;
2975 set a GUID in an extended DN structure
2977 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
2983 status = GUID_to_ndr_blob(guid, dn, &v);
2984 if (!NT_STATUS_IS_OK(status)) {
2985 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
2988 ret = ldb_dn_set_extended_component(dn, component_name, &v);
2994 return a GUID from a extended DN structure
2996 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
2998 const struct ldb_val *v;
3000 v = ldb_dn_get_extended_component(dn, component_name);
3002 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3005 return GUID_from_ndr_blob(v, guid);
3009 return a uint64_t from a extended DN structure
3011 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3013 const struct ldb_val *v;
3016 v = ldb_dn_get_extended_component(dn, component_name);
3018 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3020 s = talloc_strndup(dn, (const char *)v->data, v->length);
3021 NT_STATUS_HAVE_NO_MEMORY(s);
3023 *val = strtoull(s, NULL, 0);
3026 return NT_STATUS_OK;
3030 return a NTTIME from a extended DN structure
3032 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3034 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3038 return a uint32_t from a extended DN structure
3040 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3042 const struct ldb_val *v;
3045 v = ldb_dn_get_extended_component(dn, component_name);
3047 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3050 s = talloc_strndup(dn, (const char *)v->data, v->length);
3051 NT_STATUS_HAVE_NO_MEMORY(s);
3053 *val = strtoul(s, NULL, 0);
3056 return NT_STATUS_OK;
3060 return a dom_sid from a extended DN structure
3062 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3064 const struct ldb_val *sid_blob;
3065 struct TALLOC_CTX *tmp_ctx;
3066 enum ndr_err_code ndr_err;
3068 sid_blob = ldb_dn_get_extended_component(dn, "SID");
3070 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3073 tmp_ctx = talloc_new(NULL);
3075 ndr_err = ndr_pull_struct_blob_all(sid_blob, tmp_ctx, sid,
3076 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3077 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3078 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3079 talloc_free(tmp_ctx);
3083 talloc_free(tmp_ctx);
3084 return NT_STATUS_OK;
3089 return RMD_FLAGS directly from a ldb_dn
3090 returns 0 if not found
3092 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3094 const struct ldb_val *v;
3096 v = ldb_dn_get_extended_component(dn, "RMD_FLAGS");
3097 if (!v || v->length > sizeof(buf)-1) return 0;
3098 strncpy(buf, (const char *)v->data, v->length);
3100 return strtoul(buf, NULL, 10);
3104 return RMD_FLAGS directly from a ldb_val for a DN
3105 returns 0 if RMD_FLAGS is not found
3107 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3113 if (val->length < 13) {
3116 p = memmem(val->data, val->length-2, "<RMD_FLAGS=", 11);
3120 flags = strtoul(p+11, &end, 10);
3121 if (!end || *end != '>') {
3122 /* it must end in a > */
3129 return true if a ldb_val containing a DN in storage form is deleted
3131 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3133 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3137 return true if a ldb_val containing a DN in storage form is
3138 in the upgraded w2k3 linked attribute format
3140 bool dsdb_dn_is_upgraded_link_val(struct ldb_val *val)
3142 return memmem(val->data, val->length, "<RMD_ADDTIME=", 13) != NULL;
3146 return a DN for a wellknown GUID
3148 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3149 struct ldb_dn *nc_root, const char *wk_guid,
3150 struct ldb_dn **wkguid_dn)
3152 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3153 const char *attrs[] = { NULL };
3156 struct ldb_result *res;
3158 /* construct the magic WKGUID DN */
3159 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3160 wk_guid, ldb_dn_get_linearized(nc_root));
3162 talloc_free(tmp_ctx);
3163 return LDB_ERR_OPERATIONS_ERROR;
3166 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs, DSDB_SEARCH_SHOW_DELETED);
3167 if (ret != LDB_SUCCESS) {
3168 talloc_free(tmp_ctx);
3172 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
3173 talloc_free(tmp_ctx);
3178 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
3180 return ldb_dn_compare(*dn1, *dn2);
3184 find a NC root given a DN within the NC
3186 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3187 struct ldb_dn **nc_root)
3189 const char *root_attrs[] = { "namingContexts", NULL };
3190 TALLOC_CTX *tmp_ctx;
3192 struct ldb_message_element *el;
3193 struct ldb_result *root_res;
3195 struct ldb_dn **nc_dns;
3197 tmp_ctx = talloc_new(samdb);
3198 if (tmp_ctx == NULL) {
3199 return LDB_ERR_OPERATIONS_ERROR;
3202 ret = ldb_search(samdb, tmp_ctx, &root_res,
3203 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
3205 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
3206 talloc_free(tmp_ctx);
3210 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
3212 DEBUG(1,("Finding namingContexts element in root_res failed: %s\n",
3213 ldb_errstring(samdb)));
3214 talloc_free(tmp_ctx);
3215 return LDB_ERR_NO_SUCH_ATTRIBUTE;
3218 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
3220 talloc_free(tmp_ctx);
3221 return LDB_ERR_OPERATIONS_ERROR;
3224 for (i=0; i<el->num_values; i++) {
3225 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
3226 if (nc_dns[i] == NULL) {
3227 talloc_free(tmp_ctx);
3228 return LDB_ERR_OPERATIONS_ERROR;
3232 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
3234 for (i=0; i<el->num_values; i++) {
3235 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
3236 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
3237 talloc_free(tmp_ctx);
3242 talloc_free(tmp_ctx);
3243 return LDB_ERR_NO_SUCH_OBJECT;
3248 find the deleted objects DN for any object, by looking for the NC
3249 root, then looking up the wellknown GUID
3251 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
3252 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
3253 struct ldb_dn **do_dn)
3255 struct ldb_dn *nc_root;
3258 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
3259 if (ret != LDB_SUCCESS) {
3263 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
3264 talloc_free(nc_root);
3269 return the tombstoneLifetime, in days
3271 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
3274 dn = ldb_get_config_basedn(ldb);
3276 return LDB_ERR_NO_SUCH_OBJECT;
3278 dn = ldb_dn_copy(ldb, dn);
3280 return LDB_ERR_OPERATIONS_ERROR;
3282 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3283 be a wellknown GUID for this */
3284 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3286 return LDB_ERR_OPERATIONS_ERROR;
3289 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
3295 compare a ldb_val to a string case insensitively
3297 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
3299 size_t len = strlen(s);
3301 if (len > v->length) return 1;
3302 ret = strncasecmp(s, (const char *)v->data, v->length);
3303 if (ret != 0) return ret;
3304 if (v->length > len && v->data[len] != 0) {
3312 load the UDV for a partition in v2 format
3313 The list is returned sorted, and with our local cursor added
3315 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3316 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
3318 static const char *attrs[] = { "replUpToDateVector", NULL };
3319 struct ldb_result *r;
3320 const struct ldb_val *ouv_value;
3323 uint64_t highest_usn;
3324 const struct GUID *our_invocation_id;
3325 struct timeval now = timeval_current();
3327 ret = ldb_search(samdb, mem_ctx, &r, dn, LDB_SCOPE_BASE, attrs, NULL);
3328 if (ret != LDB_SUCCESS) {
3332 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
3334 enum ndr_err_code ndr_err;
3335 struct replUpToDateVectorBlob ouv;
3337 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
3338 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
3339 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3341 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3343 if (ouv.version != 2) {
3344 /* we always store as version 2, and
3345 * replUpToDateVector is not replicated
3347 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3350 *count = ouv.ctr.ctr2.count;
3351 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
3359 our_invocation_id = samdb_ntds_invocation_id(samdb);
3360 if (!our_invocation_id) {
3361 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
3362 talloc_free(*cursors);
3363 return LDB_ERR_OPERATIONS_ERROR;
3366 ret = dsdb_load_partition_usn(samdb, dn, &highest_usn, NULL);
3367 if (ret != LDB_SUCCESS) {
3368 /* nothing to add - this can happen after a vampire */
3369 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3373 for (i=0; i<*count; i++) {
3374 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
3375 (*cursors)[i].highest_usn = highest_usn;
3376 (*cursors)[i].last_sync_success = timeval_to_nttime(&now);
3377 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3382 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
3384 return LDB_ERR_OPERATIONS_ERROR;
3387 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
3388 (*cursors)[*count].highest_usn = highest_usn;
3389 (*cursors)[*count].last_sync_success = timeval_to_nttime(&now);
3392 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3398 load the UDV for a partition in version 1 format
3399 The list is returned sorted, and with our local cursor added
3401 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3402 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
3404 struct drsuapi_DsReplicaCursor2 *v2;
3408 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
3409 if (ret != LDB_SUCCESS) {
3419 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
3420 if (*cursors == NULL) {
3422 return LDB_ERR_OPERATIONS_ERROR;
3425 for (i=0; i<*count; i++) {
3426 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
3427 (*cursors)[i].highest_usn = v2[i].highest_usn;
3434 add a set of controls to a ldb_request structure based on a set of
3435 flags. See util.h for a list of available flags
3437 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
3440 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
3441 struct ldb_search_options_control *options;
3442 /* Using the phantom root control allows us to search all partitions */
3443 options = talloc(req, struct ldb_search_options_control);
3444 if (options == NULL) {
3445 return LDB_ERR_OPERATIONS_ERROR;
3447 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
3449 ret = ldb_request_add_control(req,
3450 LDB_CONTROL_SEARCH_OPTIONS_OID,
3452 if (ret != LDB_SUCCESS) {
3457 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
3458 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
3459 if (ret != LDB_SUCCESS) {
3464 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
3465 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, true, NULL);
3466 if (ret != LDB_SUCCESS) {
3471 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
3472 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
3473 if (!extended_ctrl) {
3474 return LDB_ERR_OPERATIONS_ERROR;
3476 extended_ctrl->type = 1;
3478 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
3479 if (ret != LDB_SUCCESS) {
3484 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
3485 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
3486 if (ret != LDB_SUCCESS) {
3491 if (dsdb_flags & DSDB_MODIFY_RELAX) {
3492 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
3493 if (ret != LDB_SUCCESS) {
3498 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
3499 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
3500 if (ret != LDB_SUCCESS) {
3505 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
3506 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
3507 if (ret != LDB_SUCCESS) {
3512 if (dsdb_flags & DSDB_TREE_DELETE) {
3513 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
3514 if (ret != LDB_SUCCESS) {
3523 a modify with a set of controls
3525 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
3526 uint32_t dsdb_flags)
3528 struct ldb_request *req;
3531 ret = ldb_build_mod_req(&req, ldb, ldb,
3535 ldb_op_default_callback,
3538 if (ret != LDB_SUCCESS) return ret;
3540 ret = dsdb_request_add_controls(req, dsdb_flags);
3541 if (ret != LDB_SUCCESS) {
3546 ret = dsdb_autotransaction_request(ldb, req);
3553 like dsdb_modify() but set all the element flags to
3554 LDB_FLAG_MOD_REPLACE
3556 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
3560 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3561 for (i=0;i<msg->num_elements;i++) {
3562 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3565 return dsdb_modify(ldb, msg, dsdb_flags);
3570 search for attrs on one DN, allowing for dsdb_flags controls
3572 int dsdb_search_dn(struct ldb_context *ldb,
3573 TALLOC_CTX *mem_ctx,
3574 struct ldb_result **_res,
3575 struct ldb_dn *basedn,
3576 const char * const *attrs,
3577 uint32_t dsdb_flags)
3580 struct ldb_request *req;
3581 struct ldb_result *res;
3583 res = talloc_zero(mem_ctx, struct ldb_result);
3585 return LDB_ERR_OPERATIONS_ERROR;
3588 ret = ldb_build_search_req(&req, ldb, res,
3595 ldb_search_default_callback,
3597 if (ret != LDB_SUCCESS) {
3602 ret = dsdb_request_add_controls(req, dsdb_flags);
3603 if (ret != LDB_SUCCESS) {
3608 ret = ldb_request(ldb, req);
3609 if (ret == LDB_SUCCESS) {
3610 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3614 if (ret != LDB_SUCCESS) {
3624 general search with dsdb_flags for controls
3626 int dsdb_search(struct ldb_context *ldb,
3627 TALLOC_CTX *mem_ctx,
3628 struct ldb_result **_res,
3629 struct ldb_dn *basedn,
3630 enum ldb_scope scope,
3631 const char * const *attrs,
3632 uint32_t dsdb_flags,
3633 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
3636 struct ldb_request *req;
3637 struct ldb_result *res;
3639 char *expression = NULL;
3640 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3642 res = talloc_zero(tmp_ctx, struct ldb_result);
3644 talloc_free(tmp_ctx);
3645 return LDB_ERR_OPERATIONS_ERROR;
3649 va_start(ap, exp_fmt);
3650 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
3654 talloc_free(tmp_ctx);
3655 return LDB_ERR_OPERATIONS_ERROR;
3659 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3666 ldb_search_default_callback,
3668 if (ret != LDB_SUCCESS) {
3669 talloc_free(tmp_ctx);
3673 ret = dsdb_request_add_controls(req, dsdb_flags);
3674 if (ret != LDB_SUCCESS) {
3675 talloc_free(tmp_ctx);
3679 ret = ldb_request(ldb, req);
3680 if (ret == LDB_SUCCESS) {
3681 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3684 if (ret != LDB_SUCCESS) {
3685 talloc_free(tmp_ctx);
3689 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
3690 if (res->count == 0) {
3691 talloc_free(tmp_ctx);
3692 return LDB_ERR_NO_SUCH_OBJECT;
3694 if (res->count != 1) {
3695 talloc_free(tmp_ctx);
3696 return LDB_ERR_CONSTRAINT_VIOLATION;
3700 *_res = talloc_steal(mem_ctx, res);
3701 talloc_free(tmp_ctx);
3708 general search with dsdb_flags for controls
3709 returns exactly 1 record or an error
3711 int dsdb_search_one(struct ldb_context *ldb,
3712 TALLOC_CTX *mem_ctx,
3713 struct ldb_message **msg,
3714 struct ldb_dn *basedn,
3715 enum ldb_scope scope,
3716 const char * const *attrs,
3717 uint32_t dsdb_flags,
3718 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
3721 struct ldb_result *res;
3723 char *expression = NULL;
3724 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3726 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
3728 res = talloc_zero(tmp_ctx, struct ldb_result);
3730 talloc_free(tmp_ctx);
3731 return LDB_ERR_OPERATIONS_ERROR;
3735 va_start(ap, exp_fmt);
3736 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
3740 talloc_free(tmp_ctx);
3741 return LDB_ERR_OPERATIONS_ERROR;
3743 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
3744 dsdb_flags, "%s", expression);
3746 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
3750 if (ret != LDB_SUCCESS) {
3751 talloc_free(tmp_ctx);
3755 *msg = talloc_steal(mem_ctx, res->msgs[0]);
3756 talloc_free(tmp_ctx);
3761 /* returns back the forest DNS name */
3762 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
3764 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
3765 ldb_get_root_basedn(ldb));
3768 if (forest_name == NULL) {
3772 p = strchr(forest_name, '/');
3781 validate that an DSA GUID belongs to the specified user sid.
3782 The user SID must be a domain controller account (either RODC or
3785 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
3786 const struct GUID *dsa_guid,
3787 const struct dom_sid *sid)
3790 - find DN of record with the DSA GUID in the
3791 configuration partition (objectGUID)
3792 - remove "NTDS Settings" component from DN
3793 - do a base search on that DN for serverReference with
3795 - extract objectSID from resulting serverReference
3797 - check this sid matches the sid argument
3799 struct ldb_dn *config_dn;
3800 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
3801 struct ldb_message *msg;
3802 const char *attrs1[] = { NULL };
3803 const char *attrs2[] = { "serverReference", NULL };
3805 struct ldb_dn *dn, *account_dn;
3806 struct dom_sid sid2;
3809 config_dn = ldb_get_config_basedn(ldb);
3811 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
3812 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
3813 if (ret != LDB_SUCCESS) {
3814 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
3815 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
3816 talloc_free(tmp_ctx);
3817 return LDB_ERR_OPERATIONS_ERROR;
3821 if (!ldb_dn_remove_child_components(dn, 1)) {
3822 talloc_free(tmp_ctx);
3823 return LDB_ERR_OPERATIONS_ERROR;
3826 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
3827 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
3828 "(objectClass=server)");
3829 if (ret != LDB_SUCCESS) {
3830 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
3831 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
3832 talloc_free(tmp_ctx);
3833 return LDB_ERR_OPERATIONS_ERROR;
3836 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
3837 if (account_dn == NULL) {
3838 DEBUG(1,(__location__ ": Failed to find account_dn for DSA with objectGUID %s, sid %s\n",
3839 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
3840 talloc_free(tmp_ctx);
3841 return LDB_ERR_OPERATIONS_ERROR;
3844 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
3845 if (!NT_STATUS_IS_OK(status)) {
3846 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
3847 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
3848 talloc_free(tmp_ctx);
3849 return LDB_ERR_OPERATIONS_ERROR;
3852 if (!dom_sid_equal(sid, &sid2)) {
3853 /* someone is trying to spoof another account */
3854 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
3855 GUID_string(tmp_ctx, dsa_guid),
3856 dom_sid_string(tmp_ctx, sid),
3857 dom_sid_string(tmp_ctx, &sid2)));
3858 talloc_free(tmp_ctx);
3859 return LDB_ERR_OPERATIONS_ERROR;
3862 talloc_free(tmp_ctx);
3866 const char *rodc_fas_list[] = {"ms-PKI-DPAPIMasterKeys",
3867 "ms-PKI-AccountCredentials",
3868 "ms-PKI-RoamingTimeStamp",
3869 "ms-FVE-KeyPackage",
3870 "ms-FVE-RecoveryGuid",
3871 "ms-FVE-RecoveryInformation",
3872 "ms-FVE-RecoveryPassword",
3873 "ms-FVE-VolumeGuid",
3874 "ms-TPM-OwnerInformation",
3877 check if the attribute belongs to the RODC filtered attribute set
3879 bool dsdb_attr_in_rodc_fas(uint32_t replica_flags, const struct dsdb_attribute *sa)
3881 int rodc_filtered_flags = SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL;
3882 bool drs_write_replica = ((replica_flags & DRSUAPI_DRS_WRIT_REP) == 0);
3884 if (drs_write_replica && (sa->searchFlags & rodc_filtered_flags)) {
3887 if (drs_write_replica && is_attr_in_list(rodc_fas_list, sa->cn)) {