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 "lib/util/tsort.h"
43 #include "dsdb/common/util.h"
44 #include "lib/socket/socket.h"
45 #include "librpc/gen_ndr/irpc.h"
46 #include "libds/common/flag_mapping.h"
49 search the sam for the specified attributes in a specific domain, filter on
50 objectSid being in domain_sid.
52 int samdb_search_domain(struct ldb_context *sam_ldb,
54 struct ldb_dn *basedn,
55 struct ldb_message ***res,
56 const char * const *attrs,
57 const struct dom_sid *domain_sid,
58 const char *format, ...) _PRINTF_ATTRIBUTE(7,8)
64 count = gendb_search_v(sam_ldb, mem_ctx, basedn,
65 res, attrs, format, ap);
71 struct dom_sid *entry_sid;
73 entry_sid = samdb_result_dom_sid(mem_ctx, (*res)[i], "objectSid");
75 if ((entry_sid == NULL) ||
76 (!dom_sid_in_domain(domain_sid, entry_sid))) {
77 /* Delete that entry from the result set */
78 (*res)[i] = (*res)[count-1];
80 talloc_free(entry_sid);
83 talloc_free(entry_sid);
91 search the sam for a single string attribute in exactly 1 record
93 const char *samdb_search_string_v(struct ldb_context *sam_ldb,
95 struct ldb_dn *basedn,
96 const char *attr_name,
97 const char *format, va_list ap) _PRINTF_ATTRIBUTE(5,0)
100 const char *attrs[2] = { NULL, NULL };
101 struct ldb_message **res = NULL;
103 attrs[0] = attr_name;
105 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
107 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
108 attr_name, format, count));
115 return ldb_msg_find_attr_as_string(res[0], attr_name, NULL);
119 search the sam for a single string attribute in exactly 1 record
121 const char *samdb_search_string(struct ldb_context *sam_ldb,
123 struct ldb_dn *basedn,
124 const char *attr_name,
125 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
130 va_start(ap, format);
131 str = samdb_search_string_v(sam_ldb, mem_ctx, basedn, attr_name, format, ap);
137 struct ldb_dn *samdb_search_dn(struct ldb_context *sam_ldb,
139 struct ldb_dn *basedn,
140 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
144 struct ldb_message **res = NULL;
147 va_start(ap, format);
148 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, NULL, format, ap);
151 if (count != 1) return NULL;
153 ret = talloc_steal(mem_ctx, res[0]->dn);
160 search the sam for a dom_sid attribute in exactly 1 record
162 struct dom_sid *samdb_search_dom_sid(struct ldb_context *sam_ldb,
164 struct ldb_dn *basedn,
165 const char *attr_name,
166 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
170 struct ldb_message **res;
171 const char *attrs[2] = { NULL, NULL };
174 attrs[0] = attr_name;
176 va_start(ap, format);
177 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
180 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
181 attr_name, format, count));
187 sid = samdb_result_dom_sid(mem_ctx, res[0], attr_name);
193 return the count of the number of records in the sam matching the query
195 int samdb_search_count(struct ldb_context *sam_ldb,
197 struct ldb_dn *basedn,
198 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
201 const char *attrs[] = { NULL };
204 va_start(ap, format);
205 ret = gendb_search_v(sam_ldb, mem_ctx, basedn, NULL, attrs, format, ap);
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 ldb_msg_find_attr_as_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 ldb_msg_find_attr_as_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] = ldb_msg_find_attr_as_string(res[i], attr_name, NULL);
313 (*strs)[count] = NULL;
318 struct ldb_dn *samdb_result_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
319 const char *attr, struct ldb_dn *default_value)
321 struct ldb_dn *ret_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, msg, attr);
323 return default_value;
329 pull a rid from a objectSid in a result set.
331 uint32_t samdb_result_rid_from_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
332 const char *attr, uint32_t default_value)
337 sid = samdb_result_dom_sid(mem_ctx, msg, attr);
339 return default_value;
341 rid = sid->sub_auths[sid->num_auths-1];
347 pull a dom_sid structure from a objectSid in a result set.
349 struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
353 const struct ldb_val *v;
355 v = ldb_msg_find_ldb_val(msg, attr);
359 sid = talloc(mem_ctx, struct dom_sid);
363 ok = sid_blob_parse(*v, sid);
372 pull a guid structure from a objectGUID in a result set.
374 struct GUID samdb_result_guid(const struct ldb_message *msg, const char *attr)
376 const struct ldb_val *v;
380 v = ldb_msg_find_ldb_val(msg, attr);
381 if (!v) return GUID_zero();
383 status = GUID_from_ndr_blob(v, &guid);
384 if (!NT_STATUS_IS_OK(status)) {
392 pull a sid prefix from a objectSid in a result set.
393 this is used to find the domain sid for a user
395 struct dom_sid *samdb_result_sid_prefix(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
398 struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, msg, attr);
399 if (!sid || sid->num_auths < 1) return NULL;
405 pull a NTTIME in a result set.
407 NTTIME samdb_result_nttime(const struct ldb_message *msg, const char *attr,
408 NTTIME default_value)
410 return ldb_msg_find_attr_as_uint64(msg, attr, default_value);
414 * Windows stores 0 for lastLogoff.
415 * But when a MS DC return the lastLogoff (as Logoff Time)
416 * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
417 * cause windows 2008 and newer version to fail for SMB requests
419 NTTIME samdb_result_last_logoff(const struct ldb_message *msg)
421 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "lastLogoff",0);
424 ret = 0x7FFFFFFFFFFFFFFFULL;
430 * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
431 * indicate an account doesn't expire.
433 * When Windows initially creates an account, it sets
434 * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF). However,
435 * when changing from an account having a specific expiration date to
436 * that account never expiring, it sets accountExpires = 0.
438 * Consolidate that logic here to allow clearer logic for account expiry in
439 * the rest of the code.
441 NTTIME samdb_result_account_expires(const struct ldb_message *msg)
443 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "accountExpires",
447 ret = 0x7FFFFFFFFFFFFFFFULL;
453 construct the allow_password_change field from the PwdLastSet attribute and the
454 domain password settings
456 NTTIME samdb_result_allow_password_change(struct ldb_context *sam_ldb,
458 struct ldb_dn *domain_dn,
459 struct ldb_message *msg,
462 uint64_t attr_time = ldb_msg_find_attr_as_uint64(msg, attr, 0);
465 if (attr_time == 0) {
469 minPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "minPwdAge", NULL);
471 /* yes, this is a -= not a += as minPwdAge is stored as the negative
472 of the number of 100-nano-seconds */
473 attr_time -= minPwdAge;
479 construct the force_password_change field from the PwdLastSet
480 attribute, the userAccountControl and the domain password settings
482 NTTIME samdb_result_force_password_change(struct ldb_context *sam_ldb,
484 struct ldb_dn *domain_dn,
485 struct ldb_message *msg)
487 int64_t attr_time = ldb_msg_find_attr_as_int64(msg, "pwdLastSet", 0);
488 uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg,
489 "userAccountControl",
493 /* Machine accounts don't expire, and there is a flag for 'no expiry' */
494 if (!(userAccountControl & UF_NORMAL_ACCOUNT)
495 || (userAccountControl & UF_DONT_EXPIRE_PASSWD)) {
496 return 0x7FFFFFFFFFFFFFFFULL;
499 if (attr_time == 0) {
502 if (attr_time == -1) {
503 return 0x7FFFFFFFFFFFFFFFULL;
506 maxPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn,
508 if (maxPwdAge == 0) {
509 return 0x7FFFFFFFFFFFFFFFULL;
511 attr_time -= maxPwdAge;
518 pull a samr_Password structutre from a result set.
520 struct samr_Password *samdb_result_hash(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, const char *attr)
522 struct samr_Password *hash = NULL;
523 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
524 if (val && (val->length >= sizeof(hash->hash))) {
525 hash = talloc(mem_ctx, struct samr_Password);
526 memcpy(hash->hash, val->data, MIN(val->length, sizeof(hash->hash)));
532 pull an array of samr_Password structures from a result set.
534 unsigned int samdb_result_hashes(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
535 const char *attr, struct samr_Password **hashes)
537 unsigned int count, i;
538 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
544 count = val->length / 16;
549 *hashes = talloc_array(mem_ctx, struct samr_Password, count);
554 for (i=0;i<count;i++) {
555 memcpy((*hashes)[i].hash, (i*16)+(char *)val->data, 16);
561 NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct ldb_message *msg,
562 struct samr_Password **lm_pwd, struct samr_Password **nt_pwd)
564 struct samr_Password *lmPwdHash, *ntPwdHash;
567 num_nt = samdb_result_hashes(mem_ctx, msg, "unicodePwd", &ntPwdHash);
570 } else if (num_nt > 1) {
571 return NT_STATUS_INTERNAL_DB_CORRUPTION;
573 *nt_pwd = &ntPwdHash[0];
577 /* Ensure that if we have turned off LM
578 * authentication, that we never use the LM hash, even
580 if (lpcfg_lanman_auth(lp_ctx)) {
582 num_lm = samdb_result_hashes(mem_ctx, msg, "dBCSPwd", &lmPwdHash);
585 } else if (num_lm > 1) {
586 return NT_STATUS_INTERNAL_DB_CORRUPTION;
588 *lm_pwd = &lmPwdHash[0];
598 pull a samr_LogonHours structutre from a result set.
600 struct samr_LogonHours samdb_result_logon_hours(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char *attr)
602 struct samr_LogonHours hours;
603 size_t units_per_week = 168;
604 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
609 units_per_week = val->length * 8;
612 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week/8);
616 hours.units_per_week = units_per_week;
617 memset(hours.bits, 0xFF, units_per_week/8);
619 memcpy(hours.bits, val->data, val->length);
626 pull a set of account_flags from a result set.
628 This requires that the attributes:
633 uint32_t samdb_result_acct_flags(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
634 struct ldb_message *msg, struct ldb_dn *domain_dn)
636 uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
637 uint32_t acct_flags = ds_uf2acb(userAccountControl);
638 NTTIME must_change_time;
641 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
644 /* Test account expire time */
645 unix_to_nt_time(&now, time(NULL));
646 /* check for expired password */
647 if (must_change_time < now) {
648 acct_flags |= ACB_PW_EXPIRED;
653 struct lsa_BinaryString samdb_result_parameters(TALLOC_CTX *mem_ctx,
654 struct ldb_message *msg,
657 struct lsa_BinaryString s;
658 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
666 s.array = talloc_array(mem_ctx, uint16_t, val->length/2);
670 s.length = s.size = val->length;
671 memcpy(s.array, val->data, val->length);
676 /* Find an attribute, with a particular value */
678 /* The current callers of this function expect a very specific
679 * behaviour: In particular, objectClass subclass equivilance is not
680 * wanted. This means that we should not lookup the schema for the
681 * comparison function */
682 struct ldb_message_element *samdb_find_attribute(struct ldb_context *ldb,
683 const struct ldb_message *msg,
684 const char *name, const char *value)
687 struct ldb_message_element *el = ldb_msg_find_element(msg, name);
693 for (i=0;i<el->num_values;i++) {
694 if (ldb_attr_cmp(value, (char *)el->values[i].data) == 0) {
702 int samdb_find_or_add_attribute(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
704 struct ldb_message_element *el;
706 el = ldb_msg_find_element(msg, name);
711 return ldb_msg_add_string(msg, name, set_value);
715 add a dom_sid element to a message
717 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
718 const char *attr_name, const struct dom_sid *sid)
721 enum ndr_err_code ndr_err;
723 ndr_err = ndr_push_struct_blob(&v, mem_ctx,
725 (ndr_push_flags_fn_t)ndr_push_dom_sid);
726 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
727 return ldb_operr(sam_ldb);
729 return ldb_msg_add_value(msg, attr_name, &v, NULL);
734 add a delete element operation to a message
736 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
737 const char *attr_name)
739 /* we use an empty replace rather than a delete, as it allows for
740 dsdb_replace() to be used everywhere */
741 return ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_REPLACE, NULL);
745 add an add attribute value to a message or enhance an existing attribute
746 which has the same name and the add flag set.
748 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
749 struct ldb_message *msg, const char *attr_name,
752 struct ldb_message_element *el;
753 struct ldb_val val, *vals;
759 v = talloc_strdup(mem_ctx, value);
761 return ldb_oom(sam_ldb);
764 val.data = (uint8_t *) v;
765 val.length = strlen(v);
767 if (val.length == 0) {
768 /* allow empty strings as non-existent attributes */
772 for (i = 0; i < msg->num_elements; i++) {
773 el = &msg->elements[i];
774 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
775 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD)) {
781 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_ADD,
783 if (ret != LDB_SUCCESS) {
788 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
791 return ldb_oom(sam_ldb);
794 el->values[el->num_values] = val;
801 add a delete attribute value to a message or enhance an existing attribute
802 which has the same name and the delete flag set.
804 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
805 struct ldb_message *msg, const char *attr_name,
808 struct ldb_message_element *el;
809 struct ldb_val val, *vals;
815 v = talloc_strdup(mem_ctx, value);
817 return ldb_oom(sam_ldb);
820 val.data = (uint8_t *) v;
821 val.length = strlen(v);
823 if (val.length == 0) {
824 /* allow empty strings as non-existent attributes */
828 for (i = 0; i < msg->num_elements; i++) {
829 el = &msg->elements[i];
830 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
831 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
837 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_DELETE,
839 if (ret != LDB_SUCCESS) {
844 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
847 return ldb_oom(sam_ldb);
850 el->values[el->num_values] = val;
857 add a int element to a message
859 int samdb_msg_add_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
860 const char *attr_name, int v)
862 const char *s = talloc_asprintf(mem_ctx, "%d", v);
864 return ldb_oom(sam_ldb);
866 return ldb_msg_add_string(msg, attr_name, s);
870 * Add an unsigned int element to a message
872 * The issue here is that we have not yet first cast to int32_t explicitly,
873 * before we cast to an signed int to printf() into the %d or cast to a
874 * int64_t before we then cast to a long long to printf into a %lld.
876 * There are *no* unsigned integers in Active Directory LDAP, even the RID
877 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
878 * (See the schema, and the syntax definitions in schema_syntax.c).
881 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
882 const char *attr_name, unsigned int v)
884 return samdb_msg_add_int(sam_ldb, mem_ctx, msg, attr_name, (int)v);
888 add a (signed) int64_t element to a message
890 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
891 const char *attr_name, int64_t v)
893 const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
895 return ldb_oom(sam_ldb);
897 return ldb_msg_add_string(msg, attr_name, s);
901 * Add an unsigned int64_t (uint64_t) element to a message
903 * The issue here is that we have not yet first cast to int32_t explicitly,
904 * before we cast to an signed int to printf() into the %d or cast to a
905 * int64_t before we then cast to a long long to printf into a %lld.
907 * There are *no* unsigned integers in Active Directory LDAP, even the RID
908 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
909 * (See the schema, and the syntax definitions in schema_syntax.c).
912 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
913 const char *attr_name, uint64_t v)
915 return samdb_msg_add_int64(sam_ldb, mem_ctx, msg, attr_name, (int64_t)v);
919 add a samr_Password element to a message
921 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
922 const char *attr_name, const struct samr_Password *hash)
925 val.data = talloc_memdup(mem_ctx, hash->hash, 16);
927 return ldb_oom(sam_ldb);
930 return ldb_msg_add_value(msg, attr_name, &val, NULL);
934 add a samr_Password array to a message
936 int samdb_msg_add_hashes(struct ldb_context *ldb,
937 TALLOC_CTX *mem_ctx, struct ldb_message *msg,
938 const char *attr_name, struct samr_Password *hashes,
943 val.data = talloc_array_size(mem_ctx, 16, count);
944 val.length = count*16;
948 for (i=0;i<count;i++) {
949 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
951 return ldb_msg_add_value(msg, attr_name, &val, NULL);
955 add a acct_flags element to a message
957 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
958 const char *attr_name, uint32_t v)
960 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
964 add a logon_hours element to a message
966 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
967 const char *attr_name, struct samr_LogonHours *hours)
970 val.length = hours->units_per_week / 8;
971 val.data = hours->bits;
972 return ldb_msg_add_value(msg, attr_name, &val, NULL);
976 add a parameters element to a message
978 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
979 const char *attr_name, struct lsa_BinaryString *parameters)
982 val.length = parameters->length;
983 val.data = (uint8_t *)parameters->array;
984 return ldb_msg_add_value(msg, attr_name, &val, NULL);
988 sets a general value element to a message
990 int samdb_msg_set_value(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
991 const char *attr_name, const struct ldb_val *val)
993 struct ldb_message_element *el;
995 el = ldb_msg_find_element(msg, attr_name);
999 return ldb_msg_add_value(msg, attr_name, val, NULL);
1003 set a string element in a message
1005 int samdb_msg_set_string(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1006 const char *attr_name, const char *str)
1008 struct ldb_message_element *el;
1010 el = ldb_msg_find_element(msg, attr_name);
1014 return ldb_msg_add_string(msg, attr_name, str);
1018 * sets a signed integer in a message
1020 int samdb_msg_set_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
1021 struct ldb_message *msg, const char *attr_name, int v)
1023 struct ldb_message_element *el;
1025 el = ldb_msg_find_element(msg, attr_name);
1029 return samdb_msg_add_int(sam_ldb, mem_ctx, msg, attr_name, v);
1033 * Sets an unsigned int element in a message
1035 * The issue here is that we have not yet first cast to int32_t explicitly,
1036 * before we cast to an signed int to printf() into the %d or cast to a
1037 * int64_t before we then cast to a long long to printf into a %lld.
1039 * There are *no* unsigned integers in Active Directory LDAP, even the RID
1040 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
1041 * (See the schema, and the syntax definitions in schema_syntax.c).
1044 int samdb_msg_set_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
1045 struct ldb_message *msg, const char *attr_name,
1048 struct ldb_message_element *el;
1050 el = ldb_msg_find_element(msg, attr_name);
1054 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, v);
1058 * Handle ldb_request in transaction
1060 static int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
1061 struct ldb_request *req)
1065 ret = ldb_transaction_start(sam_ldb);
1066 if (ret != LDB_SUCCESS) {
1070 ret = ldb_request(sam_ldb, req);
1071 if (ret == LDB_SUCCESS) {
1072 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1075 if (ret == LDB_SUCCESS) {
1076 return ldb_transaction_commit(sam_ldb);
1078 ldb_transaction_cancel(sam_ldb);
1084 return a default security descriptor
1086 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1088 struct security_descriptor *sd;
1090 sd = security_descriptor_initialise(mem_ctx);
1095 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1097 struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1098 struct ldb_dn *aggregate_dn;
1103 aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1104 if (!aggregate_dn) {
1107 if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1110 return aggregate_dn;
1113 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1115 struct ldb_dn *new_dn;
1117 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1118 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1119 talloc_free(new_dn);
1125 struct ldb_dn *samdb_infrastructure_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1127 struct ldb_dn *new_dn;
1129 new_dn = ldb_dn_copy(mem_ctx, ldb_get_default_basedn(sam_ctx));
1130 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Infrastructure")) {
1131 talloc_free(new_dn);
1137 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1139 struct ldb_dn *new_dn;
1141 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1142 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1143 talloc_free(new_dn);
1150 work out the domain sid for the current open ldb
1152 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1154 TALLOC_CTX *tmp_ctx;
1155 const struct dom_sid *domain_sid;
1156 const char *attrs[] = {
1160 struct ldb_result *res;
1163 /* see if we have a cached copy */
1164 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1169 tmp_ctx = talloc_new(ldb);
1170 if (tmp_ctx == NULL) {
1174 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1176 if (ret != LDB_SUCCESS) {
1180 if (res->count != 1) {
1184 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1185 if (domain_sid == NULL) {
1189 /* cache the domain_sid in the ldb */
1190 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1194 talloc_steal(ldb, domain_sid);
1195 talloc_free(tmp_ctx);
1200 talloc_free(tmp_ctx);
1205 get domain sid from cache
1207 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1209 return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1212 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1214 TALLOC_CTX *tmp_ctx;
1215 struct dom_sid *dom_sid_new;
1216 struct dom_sid *dom_sid_old;
1218 /* see if we have a cached copy */
1219 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1220 "cache.domain_sid"), struct dom_sid);
1222 tmp_ctx = talloc_new(ldb);
1223 if (tmp_ctx == NULL) {
1227 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1232 /* cache the domain_sid in the ldb */
1233 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1237 talloc_steal(ldb, dom_sid_new);
1238 talloc_free(tmp_ctx);
1239 talloc_free(dom_sid_old);
1244 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1245 talloc_free(tmp_ctx);
1249 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
1251 TALLOC_CTX *tmp_ctx;
1252 struct ldb_dn *ntds_settings_dn_new;
1253 struct ldb_dn *ntds_settings_dn_old;
1255 /* see if we have a forced copy from provision */
1256 ntds_settings_dn_old = talloc_get_type(ldb_get_opaque(ldb,
1257 "forced.ntds_settings_dn"), struct ldb_dn);
1259 tmp_ctx = talloc_new(ldb);
1260 if (tmp_ctx == NULL) {
1264 ntds_settings_dn_new = ldb_dn_copy(tmp_ctx, ntds_settings_dn_in);
1265 if (!ntds_settings_dn_new) {
1269 /* set the DN in the ldb to avoid lookups during provision */
1270 if (ldb_set_opaque(ldb, "forced.ntds_settings_dn", ntds_settings_dn_new) != LDB_SUCCESS) {
1274 talloc_steal(ldb, ntds_settings_dn_new);
1275 talloc_free(tmp_ctx);
1276 talloc_free(ntds_settings_dn_old);
1281 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1282 talloc_free(tmp_ctx);
1287 work out the ntds settings dn for the current open ldb
1289 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb)
1291 TALLOC_CTX *tmp_ctx;
1292 const char *root_attrs[] = { "dsServiceName", NULL };
1294 struct ldb_result *root_res;
1295 struct ldb_dn *settings_dn;
1297 /* see if we have a cached copy */
1298 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "forced.ntds_settings_dn");
1303 tmp_ctx = talloc_new(ldb);
1304 if (tmp_ctx == NULL) {
1308 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1310 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1311 ldb_errstring(ldb)));
1315 if (root_res->count != 1) {
1319 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1321 /* note that we do not cache the DN here, as that would mean
1322 * we could not handle server renames at runtime. Only
1323 * provision sets up forced.ntds_settings_dn */
1325 talloc_steal(ldb, settings_dn);
1326 talloc_free(tmp_ctx);
1331 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1332 talloc_free(tmp_ctx);
1337 work out the ntds settings invocationId for the current open ldb
1339 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1341 TALLOC_CTX *tmp_ctx;
1342 const char *attrs[] = { "invocationId", NULL };
1344 struct ldb_result *res;
1345 struct GUID *invocation_id;
1347 /* see if we have a cached copy */
1348 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1349 if (invocation_id) {
1350 return invocation_id;
1353 tmp_ctx = talloc_new(ldb);
1354 if (tmp_ctx == NULL) {
1358 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1363 if (res->count != 1) {
1367 invocation_id = talloc(tmp_ctx, struct GUID);
1368 if (!invocation_id) {
1372 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1374 /* cache the domain_sid in the ldb */
1375 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1379 talloc_steal(ldb, invocation_id);
1380 talloc_free(tmp_ctx);
1382 return invocation_id;
1385 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1386 talloc_free(tmp_ctx);
1390 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1392 TALLOC_CTX *tmp_ctx;
1393 struct GUID *invocation_id_new;
1394 struct GUID *invocation_id_old;
1396 /* see if we have a cached copy */
1397 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1398 "cache.invocation_id");
1400 tmp_ctx = talloc_new(ldb);
1401 if (tmp_ctx == NULL) {
1405 invocation_id_new = talloc(tmp_ctx, struct GUID);
1406 if (!invocation_id_new) {
1410 *invocation_id_new = *invocation_id_in;
1412 /* cache the domain_sid in the ldb */
1413 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1417 talloc_steal(ldb, invocation_id_new);
1418 talloc_free(tmp_ctx);
1419 talloc_free(invocation_id_old);
1424 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1425 talloc_free(tmp_ctx);
1430 work out the ntds settings objectGUID for the current open ldb
1432 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1434 TALLOC_CTX *tmp_ctx;
1435 const char *attrs[] = { "objectGUID", NULL };
1437 struct ldb_result *res;
1438 struct GUID *ntds_guid;
1440 /* see if we have a cached copy */
1441 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1446 tmp_ctx = talloc_new(ldb);
1447 if (tmp_ctx == NULL) {
1451 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1456 if (res->count != 1) {
1460 ntds_guid = talloc(tmp_ctx, struct GUID);
1465 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1467 /* cache the domain_sid in the ldb */
1468 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1472 talloc_steal(ldb, ntds_guid);
1473 talloc_free(tmp_ctx);
1478 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1479 talloc_free(tmp_ctx);
1483 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1485 TALLOC_CTX *tmp_ctx;
1486 struct GUID *ntds_guid_new;
1487 struct GUID *ntds_guid_old;
1489 /* see if we have a cached copy */
1490 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1492 tmp_ctx = talloc_new(ldb);
1493 if (tmp_ctx == NULL) {
1497 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1498 if (!ntds_guid_new) {
1502 *ntds_guid_new = *ntds_guid_in;
1504 /* cache the domain_sid in the ldb */
1505 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1509 talloc_steal(ldb, ntds_guid_new);
1510 talloc_free(tmp_ctx);
1511 talloc_free(ntds_guid_old);
1516 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1517 talloc_free(tmp_ctx);
1522 work out the server dn for the current open ldb
1524 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1526 return ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb));
1530 work out the server dn for the current open ldb
1532 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1534 struct ldb_dn *server_dn;
1535 struct ldb_dn *servers_dn;
1536 struct ldb_dn *server_site_dn;
1538 /* TODO: there must be a saner way to do this!! */
1539 server_dn = samdb_server_dn(ldb, mem_ctx);
1540 if (!server_dn) return NULL;
1542 servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1543 talloc_free(server_dn);
1544 if (!servers_dn) return NULL;
1546 server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1547 talloc_free(servers_dn);
1549 return server_site_dn;
1553 find the site name from a computers DN record
1555 int samdb_find_site_for_computer(struct ldb_context *ldb,
1556 TALLOC_CTX *mem_ctx, struct ldb_dn *computer_dn,
1557 const char **site_name)
1561 const struct ldb_val *rdn_val;
1565 ret = samdb_reference_dn(ldb, mem_ctx, computer_dn, "serverReferenceBL", &dn);
1566 if (ret != LDB_SUCCESS) {
1570 if (!ldb_dn_remove_child_components(dn, 2)) {
1572 return LDB_ERR_INVALID_DN_SYNTAX;
1575 rdn_val = ldb_dn_get_rdn_val(dn);
1576 if (rdn_val == NULL) {
1577 return LDB_ERR_OPERATIONS_ERROR;
1580 (*site_name) = talloc_strndup(mem_ctx, (const char *)rdn_val->data, rdn_val->length);
1583 return LDB_ERR_OPERATIONS_ERROR;
1589 find the NTDS GUID from a computers DN record
1591 int samdb_find_ntdsguid_for_computer(struct ldb_context *ldb, struct ldb_dn *computer_dn,
1592 struct GUID *ntds_guid)
1597 *ntds_guid = GUID_zero();
1599 ret = samdb_reference_dn(ldb, ldb, computer_dn, "serverReferenceBL", &dn);
1600 if (ret != LDB_SUCCESS) {
1604 if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Settings")) {
1606 return LDB_ERR_OPERATIONS_ERROR;
1609 ret = dsdb_find_guid_by_dn(ldb, dn, ntds_guid);
1615 find a 'reference' DN that points at another object
1616 (eg. serverReference, rIDManagerReference etc)
1618 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1619 const char *attribute, struct ldb_dn **dn)
1621 const char *attrs[2];
1622 struct ldb_result *res;
1625 attrs[0] = attribute;
1628 ret = dsdb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_ONE_ONLY, NULL);
1629 if (ret != LDB_SUCCESS) {
1633 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1635 if (!ldb_msg_find_element(res->msgs[0], attribute)) {
1636 ldb_asprintf_errstring(ldb, "Cannot find attribute %s of %s to calculate reference dn", attribute,
1637 ldb_dn_get_linearized(base));
1639 ldb_asprintf_errstring(ldb, "Cannot interpret attribute %s of %s as a dn", attribute,
1640 ldb_dn_get_linearized(base));
1643 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1651 find our machine account via the serverReference attribute in the
1654 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1656 struct ldb_dn *server_dn;
1659 server_dn = samdb_server_dn(ldb, mem_ctx);
1660 if (server_dn == NULL) {
1661 return LDB_ERR_NO_SUCH_OBJECT;
1664 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1665 talloc_free(server_dn);
1671 find the RID Manager$ DN via the rIDManagerReference attribute in the
1674 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1676 return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1677 "rIDManagerReference", dn);
1681 find the RID Set DN via the rIDSetReferences attribute in our
1684 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1686 struct ldb_dn *server_ref_dn;
1689 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1690 if (ret != LDB_SUCCESS) {
1693 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1694 talloc_free(server_ref_dn);
1698 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1700 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1707 return (const char *) val->data;
1711 * Finds the client site by using the client's IP address.
1712 * The "subnet_name" returns the name of the subnet if parameter != NULL
1714 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1715 const char *ip_address, char **subnet_name)
1717 const char *attrs[] = { "cn", "siteObject", NULL };
1718 struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
1719 struct ldb_result *res;
1720 const struct ldb_val *val;
1721 const char *site_name = NULL, *l_subnet_name = NULL;
1722 const char *allow_list[2] = { NULL, NULL };
1723 unsigned int i, count;
1727 * if we don't have a client ip e.g. ncalrpc
1728 * the server site is the client site
1730 if (ip_address == NULL) {
1731 return samdb_server_site_name(ldb, mem_ctx);
1734 sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1735 if (sites_container_dn == NULL) {
1739 subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1740 if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1741 talloc_free(sites_container_dn);
1742 talloc_free(subnets_dn);
1746 ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1748 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1750 } else if (ret != LDB_SUCCESS) {
1751 talloc_free(sites_container_dn);
1752 talloc_free(subnets_dn);
1758 for (i = 0; i < count; i++) {
1759 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1762 allow_list[0] = l_subnet_name;
1764 if (socket_allow_access(mem_ctx, NULL, allow_list, "", ip_address)) {
1765 sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1768 if (sites_dn == NULL) {
1769 /* No reference, maybe another subnet matches */
1773 /* "val" cannot be NULL here since "sites_dn" != NULL */
1774 val = ldb_dn_get_rdn_val(sites_dn);
1775 site_name = talloc_strdup(mem_ctx,
1776 (const char *) val->data);
1778 talloc_free(sites_dn);
1784 if (site_name == NULL) {
1785 /* This is the Windows Server fallback rule: when no subnet
1786 * exists and we have only one site available then use it (it
1787 * is for sure the same as our server site). If more sites do
1788 * exist then we don't know which one to use and set the site
1790 cnt = samdb_search_count(ldb, mem_ctx, sites_container_dn,
1791 "(objectClass=site)");
1793 site_name = samdb_server_site_name(ldb, mem_ctx);
1795 site_name = talloc_strdup(mem_ctx, "");
1797 l_subnet_name = NULL;
1800 if (subnet_name != NULL) {
1801 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1804 talloc_free(sites_container_dn);
1805 talloc_free(subnets_dn);
1812 work out if we are the PDC for the domain of the current open ldb
1814 bool samdb_is_pdc(struct ldb_context *ldb)
1816 const char *dom_attrs[] = { "fSMORoleOwner", NULL };
1818 struct ldb_result *dom_res;
1819 TALLOC_CTX *tmp_ctx;
1823 tmp_ctx = talloc_new(ldb);
1824 if (tmp_ctx == NULL) {
1825 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1829 ret = ldb_search(ldb, tmp_ctx, &dom_res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, dom_attrs, NULL);
1830 if (ret != LDB_SUCCESS) {
1831 DEBUG(1,("Searching for fSMORoleOwner in %s failed: %s\n",
1832 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1833 ldb_errstring(ldb)));
1836 if (dom_res->count != 1) {
1840 pdc = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, dom_res->msgs[0], "fSMORoleOwner");
1842 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), pdc) == 0) {
1848 talloc_free(tmp_ctx);
1853 DEBUG(1,("Failed to find if we are the PDC for this ldb\n"));
1854 talloc_free(tmp_ctx);
1859 work out if we are a Global Catalog server for the domain of the current open ldb
1861 bool samdb_is_gc(struct ldb_context *ldb)
1863 const char *attrs[] = { "options", NULL };
1866 struct ldb_result *res;
1867 TALLOC_CTX *tmp_ctx;
1869 tmp_ctx = talloc_new(ldb);
1870 if (tmp_ctx == NULL) {
1871 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1875 /* Query cn=ntds settings,.... */
1876 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1877 if (ret != LDB_SUCCESS) {
1878 talloc_free(tmp_ctx);
1881 if (res->count != 1) {
1882 talloc_free(tmp_ctx);
1886 options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
1887 talloc_free(tmp_ctx);
1889 /* if options attribute has the 0x00000001 flag set, then enable the global catlog */
1890 if (options & DS_NTDSDSA_OPT_IS_GC) {
1896 /* Find a domain object in the parents of a particular DN. */
1897 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1898 struct ldb_dn **parent_dn, const char **errstring)
1900 TALLOC_CTX *local_ctx;
1901 struct ldb_dn *sdn = dn;
1902 struct ldb_result *res = NULL;
1903 int ret = LDB_SUCCESS;
1904 const char *attrs[] = { NULL };
1906 local_ctx = talloc_new(mem_ctx);
1907 if (local_ctx == NULL) return ldb_oom(ldb);
1909 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1910 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1911 "(|(objectClass=domain)(objectClass=builtinDomain))");
1912 if (ret == LDB_SUCCESS) {
1913 if (res->count == 1) {
1921 if (ret != LDB_SUCCESS) {
1922 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1923 ldb_dn_get_linearized(dn),
1924 ldb_dn_get_linearized(sdn),
1925 ldb_errstring(ldb));
1926 talloc_free(local_ctx);
1929 if (res->count != 1) {
1930 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1931 ldb_dn_get_linearized(dn));
1932 DEBUG(0,(__location__ ": %s\n", *errstring));
1933 talloc_free(local_ctx);
1934 return LDB_ERR_CONSTRAINT_VIOLATION;
1937 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1938 talloc_free(local_ctx);
1944 * Performs checks on a user password (plaintext UNIX format - attribute
1945 * "password"). The remaining parameters have to be extracted from the domain
1948 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1950 enum samr_ValidationStatus samdb_check_password(const DATA_BLOB *password,
1951 const uint32_t pwdProperties,
1952 const uint32_t minPwdLength)
1954 /* checks if the "minPwdLength" property is satisfied */
1955 if (minPwdLength > password->length)
1956 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
1958 /* checks the password complexity */
1959 if (((pwdProperties & DOMAIN_PASSWORD_COMPLEX) != 0)
1960 && (password->data != NULL)
1961 && (!check_password_quality((const char *) password->data)))
1962 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1964 return SAMR_VALIDATION_STATUS_SUCCESS;
1968 * Callback for "samdb_set_password" password change
1970 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
1975 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1978 if (ares->error != LDB_SUCCESS) {
1980 req->context = talloc_steal(req,
1981 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1983 return ldb_request_done(req, ret);
1986 if (ares->type != LDB_REPLY_DONE) {
1988 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1991 req->context = talloc_steal(req,
1992 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1994 return ldb_request_done(req, LDB_SUCCESS);
1998 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1999 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2000 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2001 * user change or not. The "rejectReason" gives some more information if the
2004 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2005 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2007 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2008 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2009 const DATA_BLOB *new_password,
2010 const struct samr_Password *lmNewHash,
2011 const struct samr_Password *ntNewHash,
2012 const struct samr_Password *lmOldHash,
2013 const struct samr_Password *ntOldHash,
2014 enum samPwdChangeReason *reject_reason,
2015 struct samr_DomInfo1 **_dominfo)
2017 struct ldb_message *msg;
2018 struct ldb_message_element *el;
2019 struct ldb_request *req;
2020 struct dsdb_control_password_change_status *pwd_stat = NULL;
2022 NTSTATUS status = NT_STATUS_OK;
2024 #define CHECK_RET(x) \
2025 if (x != LDB_SUCCESS) { \
2027 return NT_STATUS_NO_MEMORY; \
2030 msg = ldb_msg_new(mem_ctx);
2032 return NT_STATUS_NO_MEMORY;
2035 if ((new_password != NULL)
2036 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
2037 /* we have the password as plaintext UTF16 */
2038 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
2039 new_password, NULL));
2040 el = ldb_msg_find_element(msg, "clearTextPassword");
2041 el->flags = LDB_FLAG_MOD_REPLACE;
2042 } else if ((new_password == NULL)
2043 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2044 /* we have a password as LM and/or NT hash */
2045 if (lmNewHash != NULL) {
2046 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2047 "dBCSPwd", lmNewHash));
2048 el = ldb_msg_find_element(msg, "dBCSPwd");
2049 el->flags = LDB_FLAG_MOD_REPLACE;
2051 if (ntNewHash != NULL) {
2052 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2053 "unicodePwd", ntNewHash));
2054 el = ldb_msg_find_element(msg, "unicodePwd");
2055 el->flags = LDB_FLAG_MOD_REPLACE;
2058 /* the password wasn't specified correctly */
2060 return NT_STATUS_INVALID_PARAMETER;
2063 /* build modify request */
2064 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2065 samdb_set_password_callback, NULL);
2066 if (ret != LDB_SUCCESS) {
2068 return NT_STATUS_NO_MEMORY;
2071 /* A password change operation */
2072 if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2073 struct dsdb_control_password_change *change;
2075 change = talloc(req, struct dsdb_control_password_change);
2076 if (change == NULL) {
2079 return NT_STATUS_NO_MEMORY;
2082 change->old_nt_pwd_hash = ntOldHash;
2083 change->old_lm_pwd_hash = lmOldHash;
2085 ret = ldb_request_add_control(req,
2086 DSDB_CONTROL_PASSWORD_CHANGE_OID,
2088 if (ret != LDB_SUCCESS) {
2091 return NT_STATUS_NO_MEMORY;
2094 ret = ldb_request_add_control(req,
2095 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2097 if (ret != LDB_SUCCESS) {
2100 return NT_STATUS_NO_MEMORY;
2102 ret = ldb_request_add_control(req,
2103 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2105 if (ret != LDB_SUCCESS) {
2108 return NT_STATUS_NO_MEMORY;
2111 ret = dsdb_autotransaction_request(ldb, req);
2113 if (req->context != NULL) {
2114 pwd_stat = talloc_steal(mem_ctx,
2115 ((struct ldb_control *)req->context)->data);
2121 /* Sets the domain info (if requested) */
2122 if (_dominfo != NULL) {
2123 struct samr_DomInfo1 *dominfo;
2125 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2126 if (dominfo == NULL) {
2127 return NT_STATUS_NO_MEMORY;
2130 if (pwd_stat != NULL) {
2131 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2132 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2133 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2134 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2135 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2138 *_dominfo = dominfo;
2141 if (reject_reason != NULL) {
2142 if (pwd_stat != NULL) {
2143 *reject_reason = pwd_stat->reject_reason;
2145 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2149 if (pwd_stat != NULL) {
2150 talloc_free(pwd_stat);
2153 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2154 const char *errmsg = ldb_errstring(ldb);
2155 char *endptr = NULL;
2156 WERROR werr = WERR_GENERAL_FAILURE;
2157 status = NT_STATUS_UNSUCCESSFUL;
2158 if (errmsg != NULL) {
2159 werr = W_ERROR(strtol(errmsg, &endptr, 16));
2161 if (endptr != errmsg) {
2162 if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2163 status = NT_STATUS_WRONG_PASSWORD;
2165 if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2166 status = NT_STATUS_PASSWORD_RESTRICTION;
2169 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2170 /* don't let the caller know if an account doesn't exist */
2171 status = NT_STATUS_WRONG_PASSWORD;
2172 } else if (ret != LDB_SUCCESS) {
2173 status = NT_STATUS_UNSUCCESSFUL;
2180 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2181 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2182 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2183 * user change or not. The "rejectReason" gives some more information if the
2186 * This wrapper function for "samdb_set_password" takes a SID as input rather
2189 * This call encapsulates a new LDB transaction for changing the password;
2190 * therefore the user hasn't to start a new one.
2192 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2193 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2194 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2195 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2197 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2198 const struct dom_sid *user_sid,
2199 const DATA_BLOB *new_password,
2200 const struct samr_Password *lmNewHash,
2201 const struct samr_Password *ntNewHash,
2202 const struct samr_Password *lmOldHash,
2203 const struct samr_Password *ntOldHash,
2204 enum samPwdChangeReason *reject_reason,
2205 struct samr_DomInfo1 **_dominfo)
2208 struct ldb_dn *user_dn;
2211 ret = ldb_transaction_start(ldb);
2212 if (ret != LDB_SUCCESS) {
2213 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2214 return NT_STATUS_TRANSACTION_ABORTED;
2217 user_dn = samdb_search_dn(ldb, mem_ctx, NULL,
2218 "(&(objectSid=%s)(objectClass=user))",
2219 ldap_encode_ndr_dom_sid(mem_ctx, user_sid));
2221 ldb_transaction_cancel(ldb);
2222 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2223 dom_sid_string(mem_ctx, user_sid)));
2224 return NT_STATUS_NO_SUCH_USER;
2227 nt_status = samdb_set_password(ldb, mem_ctx,
2230 lmNewHash, ntNewHash,
2231 lmOldHash, ntOldHash,
2232 reject_reason, _dominfo);
2233 if (!NT_STATUS_IS_OK(nt_status)) {
2234 ldb_transaction_cancel(ldb);
2235 talloc_free(user_dn);
2239 ret = ldb_transaction_commit(ldb);
2240 if (ret != LDB_SUCCESS) {
2241 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2242 ldb_dn_get_linearized(user_dn),
2243 ldb_errstring(ldb)));
2244 talloc_free(user_dn);
2245 return NT_STATUS_TRANSACTION_ABORTED;
2248 talloc_free(user_dn);
2249 return NT_STATUS_OK;
2253 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2254 struct dom_sid *sid, struct ldb_dn **ret_dn)
2256 struct ldb_message *msg;
2257 struct ldb_dn *basedn;
2261 sidstr = dom_sid_string(mem_ctx, sid);
2262 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2264 /* We might have to create a ForeignSecurityPrincipal, even if this user
2265 * is in our own domain */
2267 msg = ldb_msg_new(sidstr);
2269 talloc_free(sidstr);
2270 return NT_STATUS_NO_MEMORY;
2273 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2274 ldb_get_default_basedn(sam_ctx),
2275 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2277 if (ret != LDB_SUCCESS) {
2278 DEBUG(0, ("Failed to find DN for "
2279 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2280 talloc_free(sidstr);
2281 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2284 /* add core elements to the ldb_message for the alias */
2286 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2287 talloc_free(sidstr);
2288 return NT_STATUS_NO_MEMORY;
2291 ret = ldb_msg_add_string(msg, "objectClass",
2292 "foreignSecurityPrincipal");
2293 if (ret != LDB_SUCCESS) {
2294 talloc_free(sidstr);
2295 return NT_STATUS_NO_MEMORY;
2298 /* create the alias */
2299 ret = ldb_add(sam_ctx, msg);
2300 if (ret != LDB_SUCCESS) {
2301 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2303 ldb_dn_get_linearized(msg->dn),
2304 ldb_errstring(sam_ctx)));
2305 talloc_free(sidstr);
2306 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2309 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2310 talloc_free(sidstr);
2312 return NT_STATUS_OK;
2317 Find the DN of a domain, assuming it to be a dotted.dns name
2320 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2323 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2324 const char *binary_encoded;
2325 const char **split_realm;
2332 split_realm = (const char **)str_list_make(tmp_ctx, dns_domain, ".");
2334 talloc_free(tmp_ctx);
2337 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2338 for (i=0; split_realm[i]; i++) {
2339 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2340 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2341 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2342 binary_encoded, ldb_dn_get_linearized(dn)));
2343 talloc_free(tmp_ctx);
2347 if (!ldb_dn_validate(dn)) {
2348 DEBUG(2, ("Failed to validated DN %s\n",
2349 ldb_dn_get_linearized(dn)));
2350 talloc_free(tmp_ctx);
2353 talloc_free(tmp_ctx);
2359 Find the DNS equivalent of a DN, in dotted DNS form
2361 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2363 int i, num_components = ldb_dn_get_comp_num(dn);
2364 char *dns_name = talloc_strdup(mem_ctx, "");
2365 if (dns_name == NULL) {
2369 for (i=0; i<num_components; i++) {
2370 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2373 talloc_free(dns_name);
2376 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2377 (int)v->length, (int)v->length, (char *)v->data);
2379 talloc_free(dns_name);
2385 /* remove the last '.' */
2386 if (dns_name[0] != 0) {
2387 dns_name[strlen(dns_name)-1] = 0;
2394 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2395 name is based on the forest DNS name
2397 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
2398 TALLOC_CTX *mem_ctx,
2399 const struct GUID *ntds_guid)
2401 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2402 const char *guid_str;
2403 struct ldb_dn *forest_dn;
2404 const char *dnsforest;
2407 guid_str = GUID_string(tmp_ctx, ntds_guid);
2408 if (guid_str == NULL) {
2409 talloc_free(tmp_ctx);
2412 forest_dn = ldb_get_root_basedn(samdb);
2413 if (forest_dn == NULL) {
2414 talloc_free(tmp_ctx);
2417 dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
2418 if (dnsforest == NULL) {
2419 talloc_free(tmp_ctx);
2422 ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
2423 talloc_free(tmp_ctx);
2429 Find the DN of a domain, be it the netbios or DNS name
2431 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2432 const char *domain_name)
2434 const char * const domain_ref_attrs[] = {
2437 const char * const domain_ref2_attrs[] = {
2440 struct ldb_result *res_domain_ref;
2441 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2442 /* find the domain's DN */
2443 int ret_domain = ldb_search(ldb, mem_ctx,
2445 samdb_partitions_dn(ldb, mem_ctx),
2448 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2450 if (ret_domain != LDB_SUCCESS) {
2454 if (res_domain_ref->count == 0) {
2455 ret_domain = ldb_search(ldb, mem_ctx,
2457 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2460 "(objectclass=domain)");
2461 if (ret_domain != LDB_SUCCESS) {
2465 if (res_domain_ref->count == 1) {
2466 return res_domain_ref->msgs[0]->dn;
2471 if (res_domain_ref->count > 1) {
2472 DEBUG(0,("Found %d records matching domain [%s]\n",
2473 ret_domain, domain_name));
2477 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
2483 use a GUID to find a DN
2485 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
2486 TALLOC_CTX *mem_ctx,
2487 const struct GUID *guid, struct ldb_dn **dn)
2490 struct ldb_result *res;
2491 const char *attrs[] = { NULL };
2492 char *guid_str = GUID_string(mem_ctx, guid);
2495 return ldb_operr(ldb);
2498 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2499 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2500 DSDB_SEARCH_SHOW_EXTENDED_DN |
2501 DSDB_SEARCH_ONE_ONLY,
2502 "objectGUID=%s", guid_str);
2503 talloc_free(guid_str);
2504 if (ret != LDB_SUCCESS) {
2508 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2515 use a DN to find a GUID with a given attribute name
2517 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
2518 struct ldb_dn *dn, const char *attribute,
2522 struct ldb_result *res;
2523 const char *attrs[2];
2524 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2526 attrs[0] = attribute;
2529 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2530 DSDB_SEARCH_SHOW_DELETED |
2531 DSDB_SEARCH_SHOW_RECYCLED);
2532 if (ret != LDB_SUCCESS) {
2533 talloc_free(tmp_ctx);
2536 if (res->count < 1) {
2537 talloc_free(tmp_ctx);
2538 return LDB_ERR_NO_SUCH_OBJECT;
2540 *guid = samdb_result_guid(res->msgs[0], attribute);
2541 talloc_free(tmp_ctx);
2546 use a DN to find a GUID
2548 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
2549 struct ldb_dn *dn, struct GUID *guid)
2551 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
2557 adds the given GUID to the given ldb_message. This value is added
2558 for the given attr_name (may be either "objectGUID" or "parentGUID").
2560 int dsdb_msg_add_guid(struct ldb_message *msg,
2562 const char *attr_name)
2567 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
2569 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
2570 if (!NT_STATUS_IS_OK(status)) {
2571 ret = LDB_ERR_OPERATIONS_ERROR;
2575 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
2576 if (ret != LDB_SUCCESS) {
2577 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
2585 talloc_free(tmp_ctx);
2592 use a DN to find a SID
2594 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
2595 struct ldb_dn *dn, struct dom_sid *sid)
2598 struct ldb_result *res;
2599 const char *attrs[] = { "objectSid", NULL };
2600 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2605 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2606 DSDB_SEARCH_SHOW_DELETED |
2607 DSDB_SEARCH_SHOW_RECYCLED);
2608 if (ret != LDB_SUCCESS) {
2609 talloc_free(tmp_ctx);
2612 if (res->count < 1) {
2613 talloc_free(tmp_ctx);
2614 return LDB_ERR_NO_SUCH_OBJECT;
2616 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
2618 talloc_free(tmp_ctx);
2619 return LDB_ERR_NO_SUCH_OBJECT;
2622 talloc_free(tmp_ctx);
2627 use a SID to find a DN
2629 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
2630 TALLOC_CTX *mem_ctx,
2631 struct dom_sid *sid, struct ldb_dn **dn)
2634 struct ldb_result *res;
2635 const char *attrs[] = { NULL };
2636 char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
2639 return ldb_operr(ldb);
2642 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2643 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2644 DSDB_SEARCH_SHOW_EXTENDED_DN |
2645 DSDB_SEARCH_ONE_ONLY,
2646 "objectSid=%s", sid_str);
2647 talloc_free(sid_str);
2648 if (ret != LDB_SUCCESS) {
2652 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2659 load a repsFromTo blob list for a given partition GUID
2660 attr must be "repsFrom" or "repsTo"
2662 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2663 const char *attr, struct repsFromToBlob **r, uint32_t *count)
2665 const char *attrs[] = { attr, NULL };
2666 struct ldb_result *res = NULL;
2667 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2669 struct ldb_message_element *el;
2675 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
2676 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2677 /* partition hasn't been replicated yet */
2680 if (ret != LDB_SUCCESS) {
2681 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
2682 talloc_free(tmp_ctx);
2683 return WERR_DS_DRA_INTERNAL_ERROR;
2686 el = ldb_msg_find_element(res->msgs[0], attr);
2688 /* it's OK to be empty */
2689 talloc_free(tmp_ctx);
2693 *count = el->num_values;
2694 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
2696 talloc_free(tmp_ctx);
2697 return WERR_DS_DRA_INTERNAL_ERROR;
2700 for (i=0; i<(*count); i++) {
2701 enum ndr_err_code ndr_err;
2702 ndr_err = ndr_pull_struct_blob(&el->values[i],
2705 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
2706 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2707 talloc_free(tmp_ctx);
2708 return WERR_DS_DRA_INTERNAL_ERROR;
2712 talloc_free(tmp_ctx);
2718 save the repsFromTo blob list for a given partition GUID
2719 attr must be "repsFrom" or "repsTo"
2721 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2722 const char *attr, struct repsFromToBlob *r, uint32_t count)
2724 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2725 struct ldb_message *msg;
2726 struct ldb_message_element *el;
2729 msg = ldb_msg_new(tmp_ctx);
2731 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
2735 el->values = talloc_array(msg, struct ldb_val, count);
2740 for (i=0; i<count; i++) {
2742 enum ndr_err_code ndr_err;
2744 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
2746 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
2747 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2755 if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
2756 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
2760 talloc_free(tmp_ctx);
2765 talloc_free(tmp_ctx);
2766 return WERR_DS_DRA_INTERNAL_ERROR;
2771 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2772 object for a partition
2774 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
2775 uint64_t *uSN, uint64_t *urgent_uSN)
2777 struct ldb_request *req;
2779 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2780 struct dsdb_control_current_partition *p_ctrl;
2781 struct ldb_result *res;
2783 res = talloc_zero(tmp_ctx, struct ldb_result);
2785 talloc_free(tmp_ctx);
2786 return ldb_oom(ldb);
2789 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
2790 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
2794 res, ldb_search_default_callback,
2796 if (ret != LDB_SUCCESS) {
2797 talloc_free(tmp_ctx);
2801 p_ctrl = talloc(req, struct dsdb_control_current_partition);
2802 if (p_ctrl == NULL) {
2803 talloc_free(tmp_ctx);
2804 return ldb_oom(ldb);
2806 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2809 ret = ldb_request_add_control(req,
2810 DSDB_CONTROL_CURRENT_PARTITION_OID,
2812 if (ret != LDB_SUCCESS) {
2813 talloc_free(tmp_ctx);
2817 /* Run the new request */
2818 ret = ldb_request(ldb, req);
2820 if (ret == LDB_SUCCESS) {
2821 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2824 if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
2825 /* it hasn't been created yet, which means
2826 an implicit value of zero */
2828 talloc_free(tmp_ctx);
2832 if (ret != LDB_SUCCESS) {
2833 talloc_free(tmp_ctx);
2837 if (res->count < 1) {
2843 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
2845 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
2849 talloc_free(tmp_ctx);
2854 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
2855 const struct drsuapi_DsReplicaCursor2 *c2)
2857 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2860 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
2861 const struct drsuapi_DsReplicaCursor *c2)
2863 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2868 see if a computer identified by its invocationId is a RODC
2870 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
2872 /* 1) find the DN for this servers NTDSDSA object
2873 2) search for the msDS-isRODC attribute
2874 3) if not present then not a RODC
2875 4) if present and TRUE then is a RODC
2877 struct ldb_dn *config_dn;
2878 const char *attrs[] = { "msDS-isRODC", NULL };
2880 struct ldb_result *res;
2881 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
2883 config_dn = ldb_get_config_basedn(sam_ctx);
2885 talloc_free(tmp_ctx);
2886 return ldb_operr(sam_ctx);
2889 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
2890 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
2892 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2894 talloc_free(tmp_ctx);
2898 if (ret != LDB_SUCCESS) {
2899 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
2900 GUID_string(tmp_ctx, objectGUID)));
2902 talloc_free(tmp_ctx);
2906 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
2907 *is_rodc = (ret == 1);
2909 talloc_free(tmp_ctx);
2915 see if we are a RODC
2917 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
2919 const struct GUID *objectGUID;
2923 /* see if we have a cached copy */
2924 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
2930 objectGUID = samdb_ntds_objectGUID(sam_ctx);
2932 return ldb_operr(sam_ctx);
2935 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
2936 if (ret != LDB_SUCCESS) {
2940 cached = talloc(sam_ctx, bool);
2941 if (cached == NULL) {
2942 return ldb_oom(sam_ctx);
2946 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
2947 if (ret != LDB_SUCCESS) {
2948 talloc_free(cached);
2949 return ldb_operr(sam_ctx);
2955 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
2957 TALLOC_CTX *tmp_ctx;
2960 tmp_ctx = talloc_new(ldb);
2961 if (tmp_ctx == NULL) {
2965 cached = talloc(tmp_ctx, bool);
2971 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
2975 talloc_steal(ldb, cached);
2976 talloc_free(tmp_ctx);
2980 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
2981 talloc_free(tmp_ctx);
2987 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
2988 * flags are DS_NTDSSETTINGS_OPT_*
2990 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
2994 TALLOC_CTX *tmp_ctx;
2995 struct ldb_result *res;
2996 struct ldb_dn *site_dn;
2997 const char *attrs[] = { "options", NULL };
2999 tmp_ctx = talloc_new(ldb_ctx);
3000 if (tmp_ctx == NULL)
3003 /* Retrieve the site dn for the ldb that we
3004 * have open. This is our local site.
3006 site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
3007 if (site_dn == NULL)
3010 /* Perform a one level (child) search from the local
3011 * site distinguided name. We're looking for the
3012 * "options" attribute within the nTDSSiteSettings
3015 rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
3016 LDB_SCOPE_ONELEVEL, attrs,
3017 "objectClass=nTDSSiteSettings");
3019 if (rc != LDB_SUCCESS || res->count != 1)
3022 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3024 talloc_free(tmp_ctx);
3029 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3030 talloc_free(tmp_ctx);
3031 return LDB_ERR_NO_SUCH_OBJECT;
3035 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3037 flags are DS_NTDS_OPTION_*
3039 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
3041 TALLOC_CTX *tmp_ctx;
3042 const char *attrs[] = { "options", NULL };
3044 struct ldb_result *res;
3046 tmp_ctx = talloc_new(ldb);
3047 if (tmp_ctx == NULL) {
3051 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
3052 if (ret != LDB_SUCCESS) {
3056 if (res->count != 1) {
3060 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3062 talloc_free(tmp_ctx);
3067 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3068 talloc_free(tmp_ctx);
3069 return LDB_ERR_NO_SUCH_OBJECT;
3072 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3074 const char *attrs[] = { "objectCategory", NULL };
3076 struct ldb_result *res;
3078 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
3079 if (ret != LDB_SUCCESS) {
3083 if (res->count != 1) {
3087 return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3090 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3095 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3096 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3098 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3100 char **tokens, *ret;
3103 tokens = str_list_make(mem_ctx, cn, " -_");
3107 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3108 tokens[0][0] = tolower(tokens[0][0]);
3109 for (i = 1; i < str_list_length((const char **)tokens); i++)
3110 tokens[i][0] = toupper(tokens[i][0]);
3112 ret = talloc_strdup(mem_ctx, tokens[0]);
3113 for (i = 1; i < str_list_length((const char **)tokens); i++)
3114 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3116 talloc_free(tokens);
3122 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3124 int dsdb_functional_level(struct ldb_context *ldb)
3126 int *domainFunctionality =
3127 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3128 if (!domainFunctionality) {
3129 /* this is expected during initial provision */
3130 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3131 return DS_DOMAIN_FUNCTION_2000;
3133 return *domainFunctionality;
3137 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3139 int dsdb_forest_functional_level(struct ldb_context *ldb)
3141 int *forestFunctionality =
3142 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3143 if (!forestFunctionality) {
3144 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3145 return DS_DOMAIN_FUNCTION_2000;
3147 return *forestFunctionality;
3151 set a GUID in an extended DN structure
3153 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3159 status = GUID_to_ndr_blob(guid, dn, &v);
3160 if (!NT_STATUS_IS_OK(status)) {
3161 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3164 ret = ldb_dn_set_extended_component(dn, component_name, &v);
3170 return a GUID from a extended DN structure
3172 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3174 const struct ldb_val *v;
3176 v = ldb_dn_get_extended_component(dn, component_name);
3178 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3181 return GUID_from_ndr_blob(v, guid);
3185 return a uint64_t from a extended DN structure
3187 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3189 const struct ldb_val *v;
3192 v = ldb_dn_get_extended_component(dn, component_name);
3194 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3196 s = talloc_strndup(dn, (const char *)v->data, v->length);
3197 NT_STATUS_HAVE_NO_MEMORY(s);
3199 *val = strtoull(s, NULL, 0);
3202 return NT_STATUS_OK;
3206 return a NTTIME from a extended DN structure
3208 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3210 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3214 return a uint32_t from a extended DN structure
3216 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3218 const struct ldb_val *v;
3221 v = ldb_dn_get_extended_component(dn, component_name);
3223 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3226 s = talloc_strndup(dn, (const char *)v->data, v->length);
3227 NT_STATUS_HAVE_NO_MEMORY(s);
3229 *val = strtoul(s, NULL, 0);
3232 return NT_STATUS_OK;
3236 return a dom_sid from a extended DN structure
3238 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3240 const struct ldb_val *sid_blob;
3241 struct TALLOC_CTX *tmp_ctx;
3242 enum ndr_err_code ndr_err;
3244 sid_blob = ldb_dn_get_extended_component(dn, component_name);
3246 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3249 tmp_ctx = talloc_new(NULL);
3251 ndr_err = ndr_pull_struct_blob_all(sid_blob, tmp_ctx, sid,
3252 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3253 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3254 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3255 talloc_free(tmp_ctx);
3259 talloc_free(tmp_ctx);
3260 return NT_STATUS_OK;
3265 return RMD_FLAGS directly from a ldb_dn
3266 returns 0 if not found
3268 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3270 const struct ldb_val *v;
3272 v = ldb_dn_get_extended_component(dn, "RMD_FLAGS");
3273 if (!v || v->length > sizeof(buf)-1) return 0;
3274 strncpy(buf, (const char *)v->data, v->length);
3276 return strtoul(buf, NULL, 10);
3280 return RMD_FLAGS directly from a ldb_val for a DN
3281 returns 0 if RMD_FLAGS is not found
3283 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3289 if (val->length < 13) {
3292 p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3296 flags = strtoul(p+11, &end, 10);
3297 if (!end || *end != '>') {
3298 /* it must end in a > */
3305 return true if a ldb_val containing a DN in storage form is deleted
3307 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3309 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3313 return true if a ldb_val containing a DN in storage form is
3314 in the upgraded w2k3 linked attribute format
3316 bool dsdb_dn_is_upgraded_link_val(struct ldb_val *val)
3318 return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
3322 return a DN for a wellknown GUID
3324 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3325 struct ldb_dn *nc_root, const char *wk_guid,
3326 struct ldb_dn **wkguid_dn)
3328 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3329 const char *attrs[] = { NULL };
3332 struct ldb_result *res;
3334 /* construct the magic WKGUID DN */
3335 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3336 wk_guid, ldb_dn_get_linearized(nc_root));
3338 talloc_free(tmp_ctx);
3339 return ldb_operr(samdb);
3342 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
3343 DSDB_SEARCH_SHOW_DELETED |
3344 DSDB_SEARCH_SHOW_RECYCLED);
3345 if (ret != LDB_SUCCESS) {
3346 talloc_free(tmp_ctx);
3350 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
3351 talloc_free(tmp_ctx);
3356 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
3358 return ldb_dn_compare(*dn1, *dn2);
3362 find a NC root given a DN within the NC
3364 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3365 struct ldb_dn **nc_root)
3367 const char *root_attrs[] = { "namingContexts", NULL };
3368 TALLOC_CTX *tmp_ctx;
3370 struct ldb_message_element *el;
3371 struct ldb_result *root_res;
3373 struct ldb_dn **nc_dns;
3375 tmp_ctx = talloc_new(samdb);
3376 if (tmp_ctx == NULL) {
3377 return ldb_oom(samdb);
3380 ret = ldb_search(samdb, tmp_ctx, &root_res,
3381 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
3382 if (ret != LDB_SUCCESS) {
3383 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
3384 talloc_free(tmp_ctx);
3388 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
3389 if ((el == NULL) || (el->num_values < 3)) {
3390 struct ldb_message *tmp_msg;
3392 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list."));
3394 /* This generates a temporary list of NCs in order to let the
3395 * provisioning work. */
3396 tmp_msg = ldb_msg_new(tmp_ctx);
3397 if (tmp_msg == NULL) {
3398 talloc_free(tmp_ctx);
3399 return ldb_oom(samdb);
3401 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3402 ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
3403 if (ret != LDB_SUCCESS) {
3404 talloc_free(tmp_ctx);
3407 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3408 ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
3409 if (ret != LDB_SUCCESS) {
3410 talloc_free(tmp_ctx);
3413 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3414 ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
3415 if (ret != LDB_SUCCESS) {
3416 talloc_free(tmp_ctx);
3419 el = &tmp_msg->elements[0];
3422 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
3424 talloc_free(tmp_ctx);
3425 return ldb_oom(samdb);
3428 for (i=0; i<el->num_values; i++) {
3429 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
3430 if (nc_dns[i] == NULL) {
3431 talloc_free(tmp_ctx);
3432 return ldb_operr(samdb);
3436 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
3438 for (i=0; i<el->num_values; i++) {
3439 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
3440 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
3441 talloc_free(tmp_ctx);
3446 talloc_free(tmp_ctx);
3447 return LDB_ERR_NO_SUCH_OBJECT;
3452 find the deleted objects DN for any object, by looking for the NC
3453 root, then looking up the wellknown GUID
3455 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
3456 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
3457 struct ldb_dn **do_dn)
3459 struct ldb_dn *nc_root;
3462 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
3463 if (ret != LDB_SUCCESS) {
3467 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
3468 talloc_free(nc_root);
3473 return the tombstoneLifetime, in days
3475 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
3478 dn = ldb_get_config_basedn(ldb);
3480 return LDB_ERR_NO_SUCH_OBJECT;
3482 dn = ldb_dn_copy(ldb, dn);
3484 return ldb_operr(ldb);
3486 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3487 be a wellknown GUID for this */
3488 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3490 return ldb_operr(ldb);
3493 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
3499 compare a ldb_val to a string case insensitively
3501 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
3503 size_t len = strlen(s);
3505 if (len > v->length) return 1;
3506 ret = strncasecmp(s, (const char *)v->data, v->length);
3507 if (ret != 0) return ret;
3508 if (v->length > len && v->data[len] != 0) {
3516 load the UDV for a partition in v2 format
3517 The list is returned sorted, and with our local cursor added
3519 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3520 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
3522 static const char *attrs[] = { "replUpToDateVector", NULL };
3523 struct ldb_result *r;
3524 const struct ldb_val *ouv_value;
3527 uint64_t highest_usn;
3528 const struct GUID *our_invocation_id;
3529 struct timeval now = timeval_current();
3531 ret = ldb_search(samdb, mem_ctx, &r, dn, LDB_SCOPE_BASE, attrs, NULL);
3532 if (ret != LDB_SUCCESS) {
3536 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
3538 enum ndr_err_code ndr_err;
3539 struct replUpToDateVectorBlob ouv;
3541 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
3542 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
3543 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3545 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3547 if (ouv.version != 2) {
3548 /* we always store as version 2, and
3549 * replUpToDateVector is not replicated
3551 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3554 *count = ouv.ctr.ctr2.count;
3555 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
3563 our_invocation_id = samdb_ntds_invocation_id(samdb);
3564 if (!our_invocation_id) {
3565 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
3566 talloc_free(*cursors);
3567 return ldb_operr(samdb);
3570 ret = dsdb_load_partition_usn(samdb, dn, &highest_usn, NULL);
3571 if (ret != LDB_SUCCESS) {
3572 /* nothing to add - this can happen after a vampire */
3573 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3577 for (i=0; i<*count; i++) {
3578 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
3579 (*cursors)[i].highest_usn = highest_usn;
3580 (*cursors)[i].last_sync_success = timeval_to_nttime(&now);
3581 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3586 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
3588 return ldb_oom(samdb);
3591 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
3592 (*cursors)[*count].highest_usn = highest_usn;
3593 (*cursors)[*count].last_sync_success = timeval_to_nttime(&now);
3596 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3602 load the UDV for a partition in version 1 format
3603 The list is returned sorted, and with our local cursor added
3605 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3606 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
3608 struct drsuapi_DsReplicaCursor2 *v2;
3612 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
3613 if (ret != LDB_SUCCESS) {
3623 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
3624 if (*cursors == NULL) {
3626 return ldb_oom(samdb);
3629 for (i=0; i<*count; i++) {
3630 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
3631 (*cursors)[i].highest_usn = v2[i].highest_usn;
3638 add a set of controls to a ldb_request structure based on a set of
3639 flags. See util.h for a list of available flags
3641 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
3644 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
3645 struct ldb_search_options_control *options;
3646 /* Using the phantom root control allows us to search all partitions */
3647 options = talloc(req, struct ldb_search_options_control);
3648 if (options == NULL) {
3649 return LDB_ERR_OPERATIONS_ERROR;
3651 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
3653 ret = ldb_request_add_control(req,
3654 LDB_CONTROL_SEARCH_OPTIONS_OID,
3656 if (ret != LDB_SUCCESS) {
3661 if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
3662 ret = ldb_request_add_control(req,
3663 DSDB_CONTROL_NO_GLOBAL_CATALOG,
3665 if (ret != LDB_SUCCESS) {
3670 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
3671 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
3672 if (ret != LDB_SUCCESS) {
3677 if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
3678 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
3679 if (ret != LDB_SUCCESS) {
3684 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
3685 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
3686 if (ret != LDB_SUCCESS) {
3691 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
3692 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
3693 if (!extended_ctrl) {
3694 return LDB_ERR_OPERATIONS_ERROR;
3696 extended_ctrl->type = 1;
3698 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
3699 if (ret != LDB_SUCCESS) {
3704 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
3705 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
3706 if (ret != LDB_SUCCESS) {
3711 if (dsdb_flags & DSDB_MODIFY_RELAX) {
3712 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
3713 if (ret != LDB_SUCCESS) {
3718 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
3719 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
3720 if (ret != LDB_SUCCESS) {
3725 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
3726 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
3727 if (ret != LDB_SUCCESS) {
3732 if (dsdb_flags & DSDB_TREE_DELETE) {
3733 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
3734 if (ret != LDB_SUCCESS) {
3739 if (dsdb_flags & DSDB_PROVISION) {
3740 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
3741 if (ret != LDB_SUCCESS) {
3746 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
3747 if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
3748 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
3749 if (ret != LDB_SUCCESS) {
3754 if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
3755 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
3756 if (ret != LDB_SUCCESS) {
3765 an add with a set of controls
3767 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
3768 uint32_t dsdb_flags)
3770 struct ldb_request *req;
3773 ret = ldb_build_add_req(&req, ldb, ldb,
3777 ldb_op_default_callback,
3780 if (ret != LDB_SUCCESS) return ret;
3782 ret = dsdb_request_add_controls(req, dsdb_flags);
3783 if (ret != LDB_SUCCESS) {
3788 ret = dsdb_autotransaction_request(ldb, req);
3795 a modify with a set of controls
3797 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
3798 uint32_t dsdb_flags)
3800 struct ldb_request *req;
3803 ret = ldb_build_mod_req(&req, ldb, ldb,
3807 ldb_op_default_callback,
3810 if (ret != LDB_SUCCESS) return ret;
3812 ret = dsdb_request_add_controls(req, dsdb_flags);
3813 if (ret != LDB_SUCCESS) {
3818 ret = dsdb_autotransaction_request(ldb, req);
3825 a delete with a set of flags
3827 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
3828 uint32_t dsdb_flags)
3830 struct ldb_request *req;
3833 ret = ldb_build_del_req(&req, ldb, ldb,
3837 ldb_op_default_callback,
3840 if (ret != LDB_SUCCESS) return ret;
3842 ret = dsdb_request_add_controls(req, dsdb_flags);
3843 if (ret != LDB_SUCCESS) {
3848 ret = dsdb_autotransaction_request(ldb, req);
3855 like dsdb_modify() but set all the element flags to
3856 LDB_FLAG_MOD_REPLACE
3858 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
3862 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3863 for (i=0;i<msg->num_elements;i++) {
3864 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3867 return dsdb_modify(ldb, msg, dsdb_flags);
3872 search for attrs on one DN, allowing for dsdb_flags controls
3874 int dsdb_search_dn(struct ldb_context *ldb,
3875 TALLOC_CTX *mem_ctx,
3876 struct ldb_result **_res,
3877 struct ldb_dn *basedn,
3878 const char * const *attrs,
3879 uint32_t dsdb_flags)
3882 struct ldb_request *req;
3883 struct ldb_result *res;
3885 res = talloc_zero(mem_ctx, struct ldb_result);
3887 return ldb_oom(ldb);
3890 ret = ldb_build_search_req(&req, ldb, res,
3897 ldb_search_default_callback,
3899 if (ret != LDB_SUCCESS) {
3904 ret = dsdb_request_add_controls(req, dsdb_flags);
3905 if (ret != LDB_SUCCESS) {
3910 ret = ldb_request(ldb, req);
3911 if (ret == LDB_SUCCESS) {
3912 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3916 if (ret != LDB_SUCCESS) {
3926 search for attrs on one DN, by the GUID of the DN, allowing for
3929 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
3930 TALLOC_CTX *mem_ctx,
3931 struct ldb_result **_res,
3932 const struct GUID *guid,
3933 const char * const *attrs,
3934 uint32_t dsdb_flags)
3936 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3940 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
3942 talloc_free(tmp_ctx);
3943 return ldb_oom(ldb);
3946 ret = dsdb_search_dn(ldb, mem_ctx, _res, dn, attrs, dsdb_flags);
3947 talloc_free(tmp_ctx);
3952 general search with dsdb_flags for controls
3954 int dsdb_search(struct ldb_context *ldb,
3955 TALLOC_CTX *mem_ctx,
3956 struct ldb_result **_res,
3957 struct ldb_dn *basedn,
3958 enum ldb_scope scope,
3959 const char * const *attrs,
3960 uint32_t dsdb_flags,
3961 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
3964 struct ldb_request *req;
3965 struct ldb_result *res;
3967 char *expression = NULL;
3968 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3970 /* cross-partitions searches with a basedn break multi-domain support */
3971 SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
3973 res = talloc_zero(tmp_ctx, struct ldb_result);
3975 talloc_free(tmp_ctx);
3976 return ldb_oom(ldb);
3980 va_start(ap, exp_fmt);
3981 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
3985 talloc_free(tmp_ctx);
3986 return ldb_oom(ldb);
3990 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3997 ldb_search_default_callback,
3999 if (ret != LDB_SUCCESS) {
4000 talloc_free(tmp_ctx);
4004 ret = dsdb_request_add_controls(req, dsdb_flags);
4005 if (ret != LDB_SUCCESS) {
4006 talloc_free(tmp_ctx);
4007 ldb_reset_err_string(ldb);
4011 ret = ldb_request(ldb, req);
4012 if (ret == LDB_SUCCESS) {
4013 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4016 if (ret != LDB_SUCCESS) {
4017 talloc_free(tmp_ctx);
4021 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
4022 if (res->count == 0) {
4023 talloc_free(tmp_ctx);
4024 ldb_reset_err_string(ldb);
4025 return LDB_ERR_NO_SUCH_OBJECT;
4027 if (res->count != 1) {
4028 talloc_free(tmp_ctx);
4029 ldb_reset_err_string(ldb);
4030 return LDB_ERR_CONSTRAINT_VIOLATION;
4034 *_res = talloc_steal(mem_ctx, res);
4035 talloc_free(tmp_ctx);
4042 general search with dsdb_flags for controls
4043 returns exactly 1 record or an error
4045 int dsdb_search_one(struct ldb_context *ldb,
4046 TALLOC_CTX *mem_ctx,
4047 struct ldb_message **msg,
4048 struct ldb_dn *basedn,
4049 enum ldb_scope scope,
4050 const char * const *attrs,
4051 uint32_t dsdb_flags,
4052 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4055 struct ldb_result *res;
4057 char *expression = NULL;
4058 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4060 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4062 res = talloc_zero(tmp_ctx, struct ldb_result);
4064 talloc_free(tmp_ctx);
4065 return ldb_oom(ldb);
4069 va_start(ap, exp_fmt);
4070 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4074 talloc_free(tmp_ctx);
4075 return ldb_oom(ldb);
4077 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4078 dsdb_flags, "%s", expression);
4080 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4084 if (ret != LDB_SUCCESS) {
4085 talloc_free(tmp_ctx);
4089 *msg = talloc_steal(mem_ctx, res->msgs[0]);
4090 talloc_free(tmp_ctx);
4095 /* returns back the forest DNS name */
4096 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4098 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4099 ldb_get_root_basedn(ldb));
4102 if (forest_name == NULL) {
4106 p = strchr(forest_name, '/');
4114 /* returns back the default domain DNS name */
4115 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4117 const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4118 ldb_get_default_basedn(ldb));
4121 if (domain_name == NULL) {
4125 p = strchr(domain_name, '/');
4134 validate that an DSA GUID belongs to the specified user sid.
4135 The user SID must be a domain controller account (either RODC or
4138 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4139 const struct GUID *dsa_guid,
4140 const struct dom_sid *sid)
4143 - find DN of record with the DSA GUID in the
4144 configuration partition (objectGUID)
4145 - remove "NTDS Settings" component from DN
4146 - do a base search on that DN for serverReference with
4148 - extract objectSid from resulting serverReference
4150 - check this sid matches the sid argument
4152 struct ldb_dn *config_dn;
4153 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4154 struct ldb_message *msg;
4155 const char *attrs1[] = { NULL };
4156 const char *attrs2[] = { "serverReference", NULL };
4158 struct ldb_dn *dn, *account_dn;
4159 struct dom_sid sid2;
4162 config_dn = ldb_get_config_basedn(ldb);
4164 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4165 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4166 if (ret != LDB_SUCCESS) {
4167 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4168 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4169 talloc_free(tmp_ctx);
4170 return ldb_operr(ldb);
4174 if (!ldb_dn_remove_child_components(dn, 1)) {
4175 talloc_free(tmp_ctx);
4176 return ldb_operr(ldb);
4179 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4180 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4181 "(objectClass=server)");
4182 if (ret != LDB_SUCCESS) {
4183 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4184 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4185 talloc_free(tmp_ctx);
4186 return ldb_operr(ldb);
4189 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4190 if (account_dn == NULL) {
4191 DEBUG(1,(__location__ ": Failed to find account_dn for DSA with objectGUID %s, sid %s\n",
4192 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4193 talloc_free(tmp_ctx);
4194 return ldb_operr(ldb);
4197 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4198 if (!NT_STATUS_IS_OK(status)) {
4199 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4200 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4201 talloc_free(tmp_ctx);
4202 return ldb_operr(ldb);
4205 if (!dom_sid_equal(sid, &sid2)) {
4206 /* someone is trying to spoof another account */
4207 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4208 GUID_string(tmp_ctx, dsa_guid),
4209 dom_sid_string(tmp_ctx, sid),
4210 dom_sid_string(tmp_ctx, &sid2)));
4211 talloc_free(tmp_ctx);
4212 return ldb_operr(ldb);
4215 talloc_free(tmp_ctx);
4219 static const char * const secret_attributes[] = {
4220 DSDB_SECRET_ATTRIBUTES,
4225 check if the attribute belongs to the RODC filtered attribute set
4226 Note that attributes that are in the filtered attribute set are the
4227 ones that _are_ always sent to a RODC
4229 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4231 /* they never get secret attributes */
4232 if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4236 /* they do get non-secret critical attributes */
4237 if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4241 /* they do get non-secret attributes marked as being in the FAS */
4242 if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4246 /* other attributes are denied */
4250 /* return fsmo role dn and role owner dn for a particular role*/
4251 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4252 struct ldb_context *ldb,
4254 struct ldb_dn **fsmo_role_dn,
4255 struct ldb_dn **role_owner_dn)
4259 case DREPL_NAMING_MASTER:
4260 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
4261 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4262 if (ret != LDB_SUCCESS) {
4263 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
4264 ldb_errstring(ldb)));
4265 talloc_free(tmp_ctx);
4266 return WERR_DS_DRA_INTERNAL_ERROR;
4269 case DREPL_INFRASTRUCTURE_MASTER:
4270 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
4271 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4272 if (ret != LDB_SUCCESS) {
4273 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4274 ldb_errstring(ldb)));
4275 talloc_free(tmp_ctx);
4276 return WERR_DS_DRA_INTERNAL_ERROR;
4279 case DREPL_RID_MASTER:
4280 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
4281 if (ret != LDB_SUCCESS) {
4282 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
4283 talloc_free(tmp_ctx);
4284 return WERR_DS_DRA_INTERNAL_ERROR;
4287 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4288 if (ret != LDB_SUCCESS) {
4289 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
4290 ldb_errstring(ldb)));
4291 talloc_free(tmp_ctx);
4292 return WERR_DS_DRA_INTERNAL_ERROR;
4295 case DREPL_SCHEMA_MASTER:
4296 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
4297 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4298 if (ret != LDB_SUCCESS) {
4299 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4300 ldb_errstring(ldb)));
4301 talloc_free(tmp_ctx);
4302 return WERR_DS_DRA_INTERNAL_ERROR;
4305 case DREPL_PDC_MASTER:
4306 *fsmo_role_dn = ldb_get_default_basedn(ldb);
4307 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4308 if (ret != LDB_SUCCESS) {
4309 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
4310 ldb_errstring(ldb)));
4311 talloc_free(tmp_ctx);
4312 return WERR_DS_DRA_INTERNAL_ERROR;
4316 return WERR_DS_DRA_INTERNAL_ERROR;
4321 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
4322 TALLOC_CTX *mem_ctx,
4323 struct ldb_dn *server_dn)
4326 struct ldb_result *res = NULL;
4327 const char * const attrs[] = { "dNSHostName", NULL};
4329 ldb_ret = ldb_search(ldb, mem_ctx, &res,
4333 if (ldb_ret != LDB_SUCCESS) {
4334 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
4335 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
4339 return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
4343 returns true if an attribute is in the filter,
4344 false otherwise, provided that attribute value is provided with the expression
4346 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
4350 switch (tree->operation) {
4353 for (i=0;i<tree->u.list.num_elements;i++) {
4354 if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
4360 return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
4361 case LDB_OP_EQUALITY:
4362 case LDB_OP_GREATER:
4365 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
4369 case LDB_OP_SUBSTRING:
4370 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
4374 case LDB_OP_PRESENT:
4375 /* (attrname=*) is not filtered out */
4377 case LDB_OP_EXTENDED:
4378 if (tree->u.extended.attr &&
4379 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
4387 bool is_attr_in_list(const char * const * attrs, const char *attr)
4391 for (i = 0; attrs[i]; i++) {
4392 if (ldb_attr_cmp(attrs[i], attr) == 0)
4401 map an ldb error code to an approximate NTSTATUS code
4403 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
4407 return NT_STATUS_OK;
4409 case LDB_ERR_PROTOCOL_ERROR:
4410 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
4412 case LDB_ERR_TIME_LIMIT_EXCEEDED:
4413 return NT_STATUS_IO_TIMEOUT;
4415 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
4416 return NT_STATUS_BUFFER_TOO_SMALL;
4418 case LDB_ERR_COMPARE_FALSE:
4419 case LDB_ERR_COMPARE_TRUE:
4420 return NT_STATUS_REVISION_MISMATCH;
4422 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
4423 return NT_STATUS_NOT_SUPPORTED;
4425 case LDB_ERR_STRONG_AUTH_REQUIRED:
4426 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
4427 case LDB_ERR_SASL_BIND_IN_PROGRESS:
4428 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
4429 case LDB_ERR_INVALID_CREDENTIALS:
4430 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
4431 case LDB_ERR_UNWILLING_TO_PERFORM:
4432 return NT_STATUS_ACCESS_DENIED;
4434 case LDB_ERR_NO_SUCH_OBJECT:
4435 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4437 case LDB_ERR_REFERRAL:
4438 case LDB_ERR_NO_SUCH_ATTRIBUTE:
4439 return NT_STATUS_NOT_FOUND;
4441 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
4442 return NT_STATUS_NOT_SUPPORTED;
4444 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
4445 return NT_STATUS_BUFFER_TOO_SMALL;
4447 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
4448 case LDB_ERR_INAPPROPRIATE_MATCHING:
4449 case LDB_ERR_CONSTRAINT_VIOLATION:
4450 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
4451 case LDB_ERR_INVALID_DN_SYNTAX:
4452 case LDB_ERR_NAMING_VIOLATION:
4453 case LDB_ERR_OBJECT_CLASS_VIOLATION:
4454 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
4455 case LDB_ERR_NOT_ALLOWED_ON_RDN:
4456 return NT_STATUS_INVALID_PARAMETER;
4458 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
4459 case LDB_ERR_ENTRY_ALREADY_EXISTS:
4460 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
4463 return NT_STATUS_NETWORK_BUSY;
4465 case LDB_ERR_ALIAS_PROBLEM:
4466 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
4467 case LDB_ERR_UNAVAILABLE:
4468 case LDB_ERR_LOOP_DETECT:
4469 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
4470 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
4472 case LDB_ERR_OPERATIONS_ERROR:
4475 return NT_STATUS_UNSUCCESSFUL;
4480 create a new naming context that will hold a partial replica
4482 int dsdb_create_partial_replica_NC(struct ldb_context *ldb, struct ldb_dn *dn)
4484 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4485 struct ldb_message *msg;
4488 msg = ldb_msg_new(tmp_ctx);
4490 talloc_free(tmp_ctx);
4491 return ldb_oom(ldb);
4495 ret = ldb_msg_add_string(msg, "objectClass", "top");
4496 if (ret != LDB_SUCCESS) {
4497 talloc_free(tmp_ctx);
4498 return ldb_oom(ldb);
4501 /* [MS-DRSR] implies that we should only add the 'top'
4502 * objectclass, but that would cause lots of problems with our
4503 * objectclass code as top is not structural, so we add
4504 * 'domainDNS' as well to keep things sane. We're expecting
4505 * this new NC to be of objectclass domainDNS after
4506 * replication anyway
4508 ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
4509 if (ret != LDB_SUCCESS) {
4510 talloc_free(tmp_ctx);
4511 return ldb_oom(ldb);
4514 ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
4515 INSTANCE_TYPE_IS_NC_HEAD|
4516 INSTANCE_TYPE_NC_ABOVE|
4517 INSTANCE_TYPE_UNINSTANT);
4518 if (ret != LDB_SUCCESS) {
4519 talloc_free(tmp_ctx);
4520 return ldb_oom(ldb);
4523 ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
4524 if (ret != LDB_SUCCESS) {
4525 DEBUG(0,("Failed to create new NC for %s - %s\n",
4526 ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
4527 talloc_free(tmp_ctx);
4531 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
4533 talloc_free(tmp_ctx);