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 || maxPwdAge == -0x8000000000000000ULL) {
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 an unsigned int element in a message
990 * The issue here is that we have not yet first cast to int32_t explicitly,
991 * before we cast to an signed int to printf() into the %d or cast to a
992 * int64_t before we then cast to a long long to printf into a %lld.
994 * There are *no* unsigned integers in Active Directory LDAP, even the RID
995 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
996 * (See the schema, and the syntax definitions in schema_syntax.c).
999 int samdb_msg_set_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
1000 struct ldb_message *msg, const char *attr_name,
1003 struct ldb_message_element *el;
1005 el = ldb_msg_find_element(msg, attr_name);
1009 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, v);
1013 * Handle ldb_request in transaction
1015 static int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
1016 struct ldb_request *req)
1020 ret = ldb_transaction_start(sam_ldb);
1021 if (ret != LDB_SUCCESS) {
1025 ret = ldb_request(sam_ldb, req);
1026 if (ret == LDB_SUCCESS) {
1027 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1030 if (ret == LDB_SUCCESS) {
1031 return ldb_transaction_commit(sam_ldb);
1033 ldb_transaction_cancel(sam_ldb);
1039 return a default security descriptor
1041 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1043 struct security_descriptor *sd;
1045 sd = security_descriptor_initialise(mem_ctx);
1050 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1052 struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1053 struct ldb_dn *aggregate_dn;
1058 aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1059 if (!aggregate_dn) {
1062 if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1065 return aggregate_dn;
1068 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1070 struct ldb_dn *new_dn;
1072 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1073 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1074 talloc_free(new_dn);
1080 struct ldb_dn *samdb_infrastructure_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1082 struct ldb_dn *new_dn;
1084 new_dn = ldb_dn_copy(mem_ctx, ldb_get_default_basedn(sam_ctx));
1085 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Infrastructure")) {
1086 talloc_free(new_dn);
1092 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1094 struct ldb_dn *new_dn;
1096 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1097 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1098 talloc_free(new_dn);
1105 work out the domain sid for the current open ldb
1107 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1109 TALLOC_CTX *tmp_ctx;
1110 const struct dom_sid *domain_sid;
1111 const char *attrs[] = {
1115 struct ldb_result *res;
1118 /* see if we have a cached copy */
1119 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1124 tmp_ctx = talloc_new(ldb);
1125 if (tmp_ctx == NULL) {
1129 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1131 if (ret != LDB_SUCCESS) {
1135 if (res->count != 1) {
1139 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1140 if (domain_sid == NULL) {
1144 /* cache the domain_sid in the ldb */
1145 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1149 talloc_steal(ldb, domain_sid);
1150 talloc_free(tmp_ctx);
1155 talloc_free(tmp_ctx);
1160 get domain sid from cache
1162 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1164 return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1167 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1169 TALLOC_CTX *tmp_ctx;
1170 struct dom_sid *dom_sid_new;
1171 struct dom_sid *dom_sid_old;
1173 /* see if we have a cached copy */
1174 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1175 "cache.domain_sid"), struct dom_sid);
1177 tmp_ctx = talloc_new(ldb);
1178 if (tmp_ctx == NULL) {
1182 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1187 /* cache the domain_sid in the ldb */
1188 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1192 talloc_steal(ldb, dom_sid_new);
1193 talloc_free(tmp_ctx);
1194 talloc_free(dom_sid_old);
1199 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1200 talloc_free(tmp_ctx);
1204 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
1206 TALLOC_CTX *tmp_ctx;
1207 struct ldb_dn *ntds_settings_dn_new;
1208 struct ldb_dn *ntds_settings_dn_old;
1210 /* see if we have a forced copy from provision */
1211 ntds_settings_dn_old = talloc_get_type(ldb_get_opaque(ldb,
1212 "forced.ntds_settings_dn"), struct ldb_dn);
1214 tmp_ctx = talloc_new(ldb);
1215 if (tmp_ctx == NULL) {
1219 ntds_settings_dn_new = ldb_dn_copy(tmp_ctx, ntds_settings_dn_in);
1220 if (!ntds_settings_dn_new) {
1224 /* set the DN in the ldb to avoid lookups during provision */
1225 if (ldb_set_opaque(ldb, "forced.ntds_settings_dn", ntds_settings_dn_new) != LDB_SUCCESS) {
1229 talloc_steal(ldb, ntds_settings_dn_new);
1230 talloc_free(tmp_ctx);
1231 talloc_free(ntds_settings_dn_old);
1236 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1237 talloc_free(tmp_ctx);
1242 work out the ntds settings dn for the current open ldb
1244 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1246 TALLOC_CTX *tmp_ctx;
1247 const char *root_attrs[] = { "dsServiceName", NULL };
1249 struct ldb_result *root_res;
1250 struct ldb_dn *settings_dn;
1252 /* see if we have a cached copy */
1253 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "forced.ntds_settings_dn");
1255 return ldb_dn_copy(mem_ctx, settings_dn);
1258 tmp_ctx = talloc_new(mem_ctx);
1259 if (tmp_ctx == NULL) {
1263 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1264 if (ret != LDB_SUCCESS) {
1265 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1266 ldb_errstring(ldb)));
1270 if (root_res->count != 1) {
1274 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1276 /* note that we do not cache the DN here, as that would mean
1277 * we could not handle server renames at runtime. Only
1278 * provision sets up forced.ntds_settings_dn */
1280 talloc_steal(mem_ctx, settings_dn);
1281 talloc_free(tmp_ctx);
1286 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1287 talloc_free(tmp_ctx);
1292 work out the ntds settings invocationId for the current open ldb
1294 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1296 TALLOC_CTX *tmp_ctx;
1297 const char *attrs[] = { "invocationId", NULL };
1299 struct ldb_result *res;
1300 struct GUID *invocation_id;
1302 /* see if we have a cached copy */
1303 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1304 if (invocation_id) {
1305 return invocation_id;
1308 tmp_ctx = talloc_new(ldb);
1309 if (tmp_ctx == NULL) {
1313 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1318 if (res->count != 1) {
1322 invocation_id = talloc(tmp_ctx, struct GUID);
1323 if (!invocation_id) {
1327 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1329 /* cache the domain_sid in the ldb */
1330 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1334 talloc_steal(ldb, invocation_id);
1335 talloc_free(tmp_ctx);
1337 return invocation_id;
1340 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1341 talloc_free(tmp_ctx);
1345 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1347 TALLOC_CTX *tmp_ctx;
1348 struct GUID *invocation_id_new;
1349 struct GUID *invocation_id_old;
1351 /* see if we have a cached copy */
1352 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1353 "cache.invocation_id");
1355 tmp_ctx = talloc_new(ldb);
1356 if (tmp_ctx == NULL) {
1360 invocation_id_new = talloc(tmp_ctx, struct GUID);
1361 if (!invocation_id_new) {
1365 *invocation_id_new = *invocation_id_in;
1367 /* cache the domain_sid in the ldb */
1368 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1372 talloc_steal(ldb, invocation_id_new);
1373 talloc_free(tmp_ctx);
1374 talloc_free(invocation_id_old);
1379 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1380 talloc_free(tmp_ctx);
1385 work out the ntds settings objectGUID for the current open ldb
1387 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1389 TALLOC_CTX *tmp_ctx;
1390 const char *attrs[] = { "objectGUID", NULL };
1392 struct ldb_result *res;
1393 struct GUID *ntds_guid;
1395 /* see if we have a cached copy */
1396 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1401 tmp_ctx = talloc_new(ldb);
1402 if (tmp_ctx == NULL) {
1406 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1411 if (res->count != 1) {
1415 ntds_guid = talloc(tmp_ctx, struct GUID);
1420 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1422 /* cache the domain_sid in the ldb */
1423 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1427 talloc_steal(ldb, ntds_guid);
1428 talloc_free(tmp_ctx);
1433 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1434 talloc_free(tmp_ctx);
1438 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1440 TALLOC_CTX *tmp_ctx;
1441 struct GUID *ntds_guid_new;
1442 struct GUID *ntds_guid_old;
1444 /* see if we have a cached copy */
1445 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1447 tmp_ctx = talloc_new(ldb);
1448 if (tmp_ctx == NULL) {
1452 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1453 if (!ntds_guid_new) {
1457 *ntds_guid_new = *ntds_guid_in;
1459 /* cache the domain_sid in the ldb */
1460 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1464 talloc_steal(ldb, ntds_guid_new);
1465 talloc_free(tmp_ctx);
1466 talloc_free(ntds_guid_old);
1471 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1472 talloc_free(tmp_ctx);
1477 work out the server dn for the current open ldb
1479 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1481 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1486 dn = ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb, tmp_ctx));
1487 talloc_free(tmp_ctx);
1493 work out the server dn for the current open ldb
1495 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1497 struct ldb_dn *server_dn;
1498 struct ldb_dn *servers_dn;
1499 struct ldb_dn *server_site_dn;
1501 /* TODO: there must be a saner way to do this!! */
1502 server_dn = samdb_server_dn(ldb, mem_ctx);
1503 if (!server_dn) return NULL;
1505 servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1506 talloc_free(server_dn);
1507 if (!servers_dn) return NULL;
1509 server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1510 talloc_free(servers_dn);
1512 return server_site_dn;
1516 find the site name from a computers DN record
1518 int samdb_find_site_for_computer(struct ldb_context *ldb,
1519 TALLOC_CTX *mem_ctx, struct ldb_dn *computer_dn,
1520 const char **site_name)
1524 const struct ldb_val *rdn_val;
1528 ret = samdb_reference_dn(ldb, mem_ctx, computer_dn, "serverReferenceBL", &dn);
1529 if (ret != LDB_SUCCESS) {
1533 if (!ldb_dn_remove_child_components(dn, 2)) {
1535 return LDB_ERR_INVALID_DN_SYNTAX;
1538 rdn_val = ldb_dn_get_rdn_val(dn);
1539 if (rdn_val == NULL) {
1540 return LDB_ERR_OPERATIONS_ERROR;
1543 (*site_name) = talloc_strndup(mem_ctx, (const char *)rdn_val->data, rdn_val->length);
1546 return LDB_ERR_OPERATIONS_ERROR;
1552 find the NTDS GUID from a computers DN record
1554 int samdb_find_ntdsguid_for_computer(struct ldb_context *ldb, struct ldb_dn *computer_dn,
1555 struct GUID *ntds_guid)
1560 *ntds_guid = GUID_zero();
1562 ret = samdb_reference_dn(ldb, ldb, computer_dn, "serverReferenceBL", &dn);
1563 if (ret != LDB_SUCCESS) {
1567 if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Settings")) {
1569 return LDB_ERR_OPERATIONS_ERROR;
1572 ret = dsdb_find_guid_by_dn(ldb, dn, ntds_guid);
1578 find a 'reference' DN that points at another object
1579 (eg. serverReference, rIDManagerReference etc)
1581 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1582 const char *attribute, struct ldb_dn **dn)
1584 const char *attrs[2];
1585 struct ldb_result *res;
1588 attrs[0] = attribute;
1591 ret = dsdb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_ONE_ONLY|DSDB_SEARCH_SHOW_EXTENDED_DN, NULL);
1592 if (ret != LDB_SUCCESS) {
1593 ldb_asprintf_errstring(ldb, "Cannot find DN %s to get attribute %s for reference dn: %s",
1594 ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb));
1598 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1600 if (!ldb_msg_find_element(res->msgs[0], attribute)) {
1601 ldb_asprintf_errstring(ldb, "Cannot find attribute %s of %s to calculate reference dn", attribute,
1602 ldb_dn_get_linearized(base));
1604 ldb_asprintf_errstring(ldb, "Cannot interpret attribute %s of %s as a dn", attribute,
1605 ldb_dn_get_linearized(base));
1608 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1616 find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1618 int samdb_reference_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *base,
1619 const char *attribute, bool *is_ntdsa)
1622 struct ldb_dn *referenced_dn;
1624 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1625 struct GUID referenced_guid;
1626 const struct GUID *our_ntds_guid;
1627 if (tmp_ctx == NULL) {
1628 return LDB_ERR_OPERATIONS_ERROR;
1630 ret = samdb_reference_dn(ldb, tmp_ctx, base, attribute, &referenced_dn);
1631 if (ret != LDB_SUCCESS) {
1632 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb)));
1636 status = dsdb_get_extended_dn_guid(referenced_dn, &referenced_guid, "GUID");
1637 talloc_free(tmp_ctx);
1638 if (!NT_STATUS_IS_OK(status)) {
1639 return LDB_ERR_OPERATIONS_ERROR;
1643 our_ntds_guid = samdb_ntds_objectGUID(ldb);
1644 if (!our_ntds_guid) {
1645 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s on %s - %s\n", attribute, ldb_dn_get_linearized(base), ldb_errstring(ldb)));
1646 return LDB_ERR_OPERATIONS_ERROR;
1649 *is_ntdsa = GUID_equal(&referenced_guid, our_ntds_guid);
1654 find our machine account via the serverReference attribute in the
1657 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1659 struct ldb_dn *server_dn;
1662 server_dn = samdb_server_dn(ldb, mem_ctx);
1663 if (server_dn == NULL) {
1664 return LDB_ERR_NO_SUCH_OBJECT;
1667 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1668 talloc_free(server_dn);
1674 find the RID Manager$ DN via the rIDManagerReference attribute in the
1677 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1679 return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1680 "rIDManagerReference", dn);
1684 find the RID Set DN via the rIDSetReferences attribute in our
1687 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1689 struct ldb_dn *server_ref_dn;
1692 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1693 if (ret != LDB_SUCCESS) {
1696 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1697 talloc_free(server_ref_dn);
1701 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1703 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1710 return (const char *) val->data;
1714 * Finds the client site by using the client's IP address.
1715 * The "subnet_name" returns the name of the subnet if parameter != NULL
1717 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1718 const char *ip_address, char **subnet_name)
1720 const char *attrs[] = { "cn", "siteObject", NULL };
1721 struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
1722 struct ldb_result *res;
1723 const struct ldb_val *val;
1724 const char *site_name = NULL, *l_subnet_name = NULL;
1725 const char *allow_list[2] = { NULL, NULL };
1726 unsigned int i, count;
1730 * if we don't have a client ip e.g. ncalrpc
1731 * the server site is the client site
1733 if (ip_address == NULL) {
1734 return samdb_server_site_name(ldb, mem_ctx);
1737 sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1738 if (sites_container_dn == NULL) {
1742 subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1743 if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1744 talloc_free(sites_container_dn);
1745 talloc_free(subnets_dn);
1749 ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1751 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1753 } else if (ret != LDB_SUCCESS) {
1754 talloc_free(sites_container_dn);
1755 talloc_free(subnets_dn);
1761 for (i = 0; i < count; i++) {
1762 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1765 allow_list[0] = l_subnet_name;
1767 if (socket_allow_access(mem_ctx, NULL, allow_list, "", ip_address)) {
1768 sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1771 if (sites_dn == NULL) {
1772 /* No reference, maybe another subnet matches */
1776 /* "val" cannot be NULL here since "sites_dn" != NULL */
1777 val = ldb_dn_get_rdn_val(sites_dn);
1778 site_name = talloc_strdup(mem_ctx,
1779 (const char *) val->data);
1781 talloc_free(sites_dn);
1787 if (site_name == NULL) {
1788 /* This is the Windows Server fallback rule: when no subnet
1789 * exists and we have only one site available then use it (it
1790 * is for sure the same as our server site). If more sites do
1791 * exist then we don't know which one to use and set the site
1793 cnt = samdb_search_count(ldb, mem_ctx, sites_container_dn,
1794 "(objectClass=site)");
1796 site_name = samdb_server_site_name(ldb, mem_ctx);
1798 site_name = talloc_strdup(mem_ctx, "");
1800 l_subnet_name = NULL;
1803 if (subnet_name != NULL) {
1804 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1807 talloc_free(sites_container_dn);
1808 talloc_free(subnets_dn);
1815 work out if we are the PDC for the domain of the current open ldb
1817 bool samdb_is_pdc(struct ldb_context *ldb)
1822 ret = samdb_reference_dn_is_our_ntdsa(ldb, ldb_get_default_basedn(ldb), "fsmoRoleOwner",
1824 if (ret != LDB_SUCCESS) {
1825 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n",
1826 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1827 ldb_errstring(ldb)));
1835 work out if we are a Global Catalog server for the domain of the current open ldb
1837 bool samdb_is_gc(struct ldb_context *ldb)
1840 if (samdb_ntds_options(ldb, &options) != LDB_SUCCESS) {
1843 return (options & DS_NTDSDSA_OPT_IS_GC) != 0;
1846 /* Find a domain object in the parents of a particular DN. */
1847 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1848 struct ldb_dn **parent_dn, const char **errstring)
1850 TALLOC_CTX *local_ctx;
1851 struct ldb_dn *sdn = dn;
1852 struct ldb_result *res = NULL;
1853 int ret = LDB_SUCCESS;
1854 const char *attrs[] = { NULL };
1856 local_ctx = talloc_new(mem_ctx);
1857 if (local_ctx == NULL) return ldb_oom(ldb);
1859 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1860 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1861 "(|(objectClass=domain)(objectClass=builtinDomain))");
1862 if (ret == LDB_SUCCESS) {
1863 if (res->count == 1) {
1871 if (ret != LDB_SUCCESS) {
1872 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1873 ldb_dn_get_linearized(dn),
1874 ldb_dn_get_linearized(sdn),
1875 ldb_errstring(ldb));
1876 talloc_free(local_ctx);
1879 if (res->count != 1) {
1880 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1881 ldb_dn_get_linearized(dn));
1882 DEBUG(0,(__location__ ": %s\n", *errstring));
1883 talloc_free(local_ctx);
1884 return LDB_ERR_CONSTRAINT_VIOLATION;
1887 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1888 talloc_free(local_ctx);
1894 * Performs checks on a user password (plaintext UNIX format - attribute
1895 * "password"). The remaining parameters have to be extracted from the domain
1898 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1900 enum samr_ValidationStatus samdb_check_password(const DATA_BLOB *password,
1901 const uint32_t pwdProperties,
1902 const uint32_t minPwdLength)
1904 /* checks if the "minPwdLength" property is satisfied */
1905 if (minPwdLength > password->length)
1906 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
1908 /* checks the password complexity */
1909 if (((pwdProperties & DOMAIN_PASSWORD_COMPLEX) != 0)
1910 && (password->data != NULL)
1911 && (!check_password_quality((const char *) password->data)))
1912 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1914 return SAMR_VALIDATION_STATUS_SUCCESS;
1918 * Callback for "samdb_set_password" password change
1920 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
1925 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1928 if (ares->error != LDB_SUCCESS) {
1930 req->context = talloc_steal(req,
1931 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1933 return ldb_request_done(req, ret);
1936 if (ares->type != LDB_REPLY_DONE) {
1938 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1941 req->context = talloc_steal(req,
1942 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1944 return ldb_request_done(req, LDB_SUCCESS);
1948 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1949 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1950 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
1951 * user change or not. The "rejectReason" gives some more information if the
1954 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
1955 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
1957 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1958 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
1959 const DATA_BLOB *new_password,
1960 const struct samr_Password *lmNewHash,
1961 const struct samr_Password *ntNewHash,
1962 const struct samr_Password *lmOldHash,
1963 const struct samr_Password *ntOldHash,
1964 enum samPwdChangeReason *reject_reason,
1965 struct samr_DomInfo1 **_dominfo)
1967 struct ldb_message *msg;
1968 struct ldb_message_element *el;
1969 struct ldb_request *req;
1970 struct dsdb_control_password_change_status *pwd_stat = NULL;
1972 NTSTATUS status = NT_STATUS_OK;
1974 #define CHECK_RET(x) \
1975 if (x != LDB_SUCCESS) { \
1977 return NT_STATUS_NO_MEMORY; \
1980 msg = ldb_msg_new(mem_ctx);
1982 return NT_STATUS_NO_MEMORY;
1985 if ((new_password != NULL)
1986 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
1987 /* we have the password as plaintext UTF16 */
1988 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
1989 new_password, NULL));
1990 el = ldb_msg_find_element(msg, "clearTextPassword");
1991 el->flags = LDB_FLAG_MOD_REPLACE;
1992 } else if ((new_password == NULL)
1993 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
1994 /* we have a password as LM and/or NT hash */
1995 if (lmNewHash != NULL) {
1996 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
1997 "dBCSPwd", lmNewHash));
1998 el = ldb_msg_find_element(msg, "dBCSPwd");
1999 el->flags = LDB_FLAG_MOD_REPLACE;
2001 if (ntNewHash != NULL) {
2002 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2003 "unicodePwd", ntNewHash));
2004 el = ldb_msg_find_element(msg, "unicodePwd");
2005 el->flags = LDB_FLAG_MOD_REPLACE;
2008 /* the password wasn't specified correctly */
2010 return NT_STATUS_INVALID_PARAMETER;
2013 /* build modify request */
2014 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2015 samdb_set_password_callback, NULL);
2016 if (ret != LDB_SUCCESS) {
2018 return NT_STATUS_NO_MEMORY;
2021 /* A password change operation */
2022 if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2023 struct dsdb_control_password_change *change;
2025 change = talloc(req, struct dsdb_control_password_change);
2026 if (change == NULL) {
2029 return NT_STATUS_NO_MEMORY;
2032 change->old_nt_pwd_hash = ntOldHash;
2033 change->old_lm_pwd_hash = lmOldHash;
2035 ret = ldb_request_add_control(req,
2036 DSDB_CONTROL_PASSWORD_CHANGE_OID,
2038 if (ret != LDB_SUCCESS) {
2041 return NT_STATUS_NO_MEMORY;
2044 ret = ldb_request_add_control(req,
2045 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2047 if (ret != LDB_SUCCESS) {
2050 return NT_STATUS_NO_MEMORY;
2052 ret = ldb_request_add_control(req,
2053 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2055 if (ret != LDB_SUCCESS) {
2058 return NT_STATUS_NO_MEMORY;
2061 ret = dsdb_autotransaction_request(ldb, req);
2063 if (req->context != NULL) {
2064 pwd_stat = talloc_steal(mem_ctx,
2065 ((struct ldb_control *)req->context)->data);
2071 /* Sets the domain info (if requested) */
2072 if (_dominfo != NULL) {
2073 struct samr_DomInfo1 *dominfo;
2075 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2076 if (dominfo == NULL) {
2077 return NT_STATUS_NO_MEMORY;
2080 if (pwd_stat != NULL) {
2081 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2082 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2083 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2084 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2085 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2088 *_dominfo = dominfo;
2091 if (reject_reason != NULL) {
2092 if (pwd_stat != NULL) {
2093 *reject_reason = pwd_stat->reject_reason;
2095 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2099 if (pwd_stat != NULL) {
2100 talloc_free(pwd_stat);
2103 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2104 const char *errmsg = ldb_errstring(ldb);
2105 char *endptr = NULL;
2106 WERROR werr = WERR_GENERAL_FAILURE;
2107 status = NT_STATUS_UNSUCCESSFUL;
2108 if (errmsg != NULL) {
2109 werr = W_ERROR(strtol(errmsg, &endptr, 16));
2111 if (endptr != errmsg) {
2112 if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2113 status = NT_STATUS_WRONG_PASSWORD;
2115 if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2116 status = NT_STATUS_PASSWORD_RESTRICTION;
2119 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2120 /* don't let the caller know if an account doesn't exist */
2121 status = NT_STATUS_WRONG_PASSWORD;
2122 } else if (ret != LDB_SUCCESS) {
2123 status = NT_STATUS_UNSUCCESSFUL;
2130 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2131 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2132 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2133 * user change or not. The "rejectReason" gives some more information if the
2136 * This wrapper function for "samdb_set_password" takes a SID as input rather
2139 * This call encapsulates a new LDB transaction for changing the password;
2140 * therefore the user hasn't to start a new one.
2142 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2143 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2144 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2145 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2147 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2148 const struct dom_sid *user_sid,
2149 const DATA_BLOB *new_password,
2150 const struct samr_Password *lmNewHash,
2151 const struct samr_Password *ntNewHash,
2152 const struct samr_Password *lmOldHash,
2153 const struct samr_Password *ntOldHash,
2154 enum samPwdChangeReason *reject_reason,
2155 struct samr_DomInfo1 **_dominfo)
2158 struct ldb_dn *user_dn;
2161 ret = ldb_transaction_start(ldb);
2162 if (ret != LDB_SUCCESS) {
2163 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2164 return NT_STATUS_TRANSACTION_ABORTED;
2167 user_dn = samdb_search_dn(ldb, mem_ctx, NULL,
2168 "(&(objectSid=%s)(objectClass=user))",
2169 ldap_encode_ndr_dom_sid(mem_ctx, user_sid));
2171 ldb_transaction_cancel(ldb);
2172 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2173 dom_sid_string(mem_ctx, user_sid)));
2174 return NT_STATUS_NO_SUCH_USER;
2177 nt_status = samdb_set_password(ldb, mem_ctx,
2180 lmNewHash, ntNewHash,
2181 lmOldHash, ntOldHash,
2182 reject_reason, _dominfo);
2183 if (!NT_STATUS_IS_OK(nt_status)) {
2184 ldb_transaction_cancel(ldb);
2185 talloc_free(user_dn);
2189 ret = ldb_transaction_commit(ldb);
2190 if (ret != LDB_SUCCESS) {
2191 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2192 ldb_dn_get_linearized(user_dn),
2193 ldb_errstring(ldb)));
2194 talloc_free(user_dn);
2195 return NT_STATUS_TRANSACTION_ABORTED;
2198 talloc_free(user_dn);
2199 return NT_STATUS_OK;
2203 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2204 struct dom_sid *sid, struct ldb_dn **ret_dn)
2206 struct ldb_message *msg;
2207 struct ldb_dn *basedn;
2211 sidstr = dom_sid_string(mem_ctx, sid);
2212 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2214 /* We might have to create a ForeignSecurityPrincipal, even if this user
2215 * is in our own domain */
2217 msg = ldb_msg_new(sidstr);
2219 talloc_free(sidstr);
2220 return NT_STATUS_NO_MEMORY;
2223 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2224 ldb_get_default_basedn(sam_ctx),
2225 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2227 if (ret != LDB_SUCCESS) {
2228 DEBUG(0, ("Failed to find DN for "
2229 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2230 talloc_free(sidstr);
2231 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2234 /* add core elements to the ldb_message for the alias */
2236 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2237 talloc_free(sidstr);
2238 return NT_STATUS_NO_MEMORY;
2241 ret = ldb_msg_add_string(msg, "objectClass",
2242 "foreignSecurityPrincipal");
2243 if (ret != LDB_SUCCESS) {
2244 talloc_free(sidstr);
2245 return NT_STATUS_NO_MEMORY;
2248 /* create the alias */
2249 ret = ldb_add(sam_ctx, msg);
2250 if (ret != LDB_SUCCESS) {
2251 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2253 ldb_dn_get_linearized(msg->dn),
2254 ldb_errstring(sam_ctx)));
2255 talloc_free(sidstr);
2256 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2259 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2260 talloc_free(sidstr);
2262 return NT_STATUS_OK;
2267 Find the DN of a domain, assuming it to be a dotted.dns name
2270 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2273 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2274 const char *binary_encoded;
2275 const char * const *split_realm;
2282 split_realm = (const char * const *)str_list_make(tmp_ctx, dns_domain, ".");
2284 talloc_free(tmp_ctx);
2287 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2288 for (i=0; split_realm[i]; i++) {
2289 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2290 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2291 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2292 binary_encoded, ldb_dn_get_linearized(dn)));
2293 talloc_free(tmp_ctx);
2297 if (!ldb_dn_validate(dn)) {
2298 DEBUG(2, ("Failed to validated DN %s\n",
2299 ldb_dn_get_linearized(dn)));
2300 talloc_free(tmp_ctx);
2303 talloc_free(tmp_ctx);
2309 Find the DNS equivalent of a DN, in dotted DNS form
2311 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2313 int i, num_components = ldb_dn_get_comp_num(dn);
2314 char *dns_name = talloc_strdup(mem_ctx, "");
2315 if (dns_name == NULL) {
2319 for (i=0; i<num_components; i++) {
2320 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2323 talloc_free(dns_name);
2326 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2327 (int)v->length, (int)v->length, (char *)v->data);
2329 talloc_free(dns_name);
2335 /* remove the last '.' */
2336 if (dns_name[0] != 0) {
2337 dns_name[strlen(dns_name)-1] = 0;
2344 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2345 name is based on the forest DNS name
2347 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
2348 TALLOC_CTX *mem_ctx,
2349 const struct GUID *ntds_guid)
2351 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2352 const char *guid_str;
2353 struct ldb_dn *forest_dn;
2354 const char *dnsforest;
2357 guid_str = GUID_string(tmp_ctx, ntds_guid);
2358 if (guid_str == NULL) {
2359 talloc_free(tmp_ctx);
2362 forest_dn = ldb_get_root_basedn(samdb);
2363 if (forest_dn == NULL) {
2364 talloc_free(tmp_ctx);
2367 dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
2368 if (dnsforest == NULL) {
2369 talloc_free(tmp_ctx);
2372 ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
2373 talloc_free(tmp_ctx);
2379 Find the DN of a domain, be it the netbios or DNS name
2381 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2382 const char *domain_name)
2384 const char * const domain_ref_attrs[] = {
2387 const char * const domain_ref2_attrs[] = {
2390 struct ldb_result *res_domain_ref;
2391 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2392 /* find the domain's DN */
2393 int ret_domain = ldb_search(ldb, mem_ctx,
2395 samdb_partitions_dn(ldb, mem_ctx),
2398 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2400 if (ret_domain != LDB_SUCCESS) {
2404 if (res_domain_ref->count == 0) {
2405 ret_domain = ldb_search(ldb, mem_ctx,
2407 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2410 "(objectclass=domain)");
2411 if (ret_domain != LDB_SUCCESS) {
2415 if (res_domain_ref->count == 1) {
2416 return res_domain_ref->msgs[0]->dn;
2421 if (res_domain_ref->count > 1) {
2422 DEBUG(0,("Found %d records matching domain [%s]\n",
2423 ret_domain, domain_name));
2427 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
2433 use a GUID to find a DN
2435 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
2436 TALLOC_CTX *mem_ctx,
2437 const struct GUID *guid, struct ldb_dn **dn)
2440 struct ldb_result *res;
2441 const char *attrs[] = { NULL };
2442 char *guid_str = GUID_string(mem_ctx, guid);
2445 return ldb_operr(ldb);
2448 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2449 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2450 DSDB_SEARCH_SHOW_EXTENDED_DN |
2451 DSDB_SEARCH_ONE_ONLY,
2452 "objectGUID=%s", guid_str);
2453 talloc_free(guid_str);
2454 if (ret != LDB_SUCCESS) {
2458 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2465 use a DN to find a GUID with a given attribute name
2467 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
2468 struct ldb_dn *dn, const char *attribute,
2472 struct ldb_result *res;
2473 const char *attrs[2];
2474 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2476 attrs[0] = attribute;
2479 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2480 DSDB_SEARCH_SHOW_DELETED |
2481 DSDB_SEARCH_SHOW_RECYCLED);
2482 if (ret != LDB_SUCCESS) {
2483 talloc_free(tmp_ctx);
2486 if (res->count < 1) {
2487 talloc_free(tmp_ctx);
2488 return LDB_ERR_NO_SUCH_OBJECT;
2490 *guid = samdb_result_guid(res->msgs[0], attribute);
2491 talloc_free(tmp_ctx);
2496 use a DN to find a GUID
2498 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
2499 struct ldb_dn *dn, struct GUID *guid)
2501 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
2507 adds the given GUID to the given ldb_message. This value is added
2508 for the given attr_name (may be either "objectGUID" or "parentGUID").
2510 int dsdb_msg_add_guid(struct ldb_message *msg,
2512 const char *attr_name)
2517 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
2519 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
2520 if (!NT_STATUS_IS_OK(status)) {
2521 ret = LDB_ERR_OPERATIONS_ERROR;
2525 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
2526 if (ret != LDB_SUCCESS) {
2527 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
2535 talloc_free(tmp_ctx);
2542 use a DN to find a SID
2544 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
2545 struct ldb_dn *dn, struct dom_sid *sid)
2548 struct ldb_result *res;
2549 const char *attrs[] = { "objectSid", NULL };
2550 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2555 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2556 DSDB_SEARCH_SHOW_DELETED |
2557 DSDB_SEARCH_SHOW_RECYCLED);
2558 if (ret != LDB_SUCCESS) {
2559 talloc_free(tmp_ctx);
2562 if (res->count < 1) {
2563 talloc_free(tmp_ctx);
2564 return LDB_ERR_NO_SUCH_OBJECT;
2566 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
2568 talloc_free(tmp_ctx);
2569 return LDB_ERR_NO_SUCH_OBJECT;
2572 talloc_free(tmp_ctx);
2577 use a SID to find a DN
2579 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
2580 TALLOC_CTX *mem_ctx,
2581 struct dom_sid *sid, struct ldb_dn **dn)
2584 struct ldb_result *res;
2585 const char *attrs[] = { NULL };
2586 char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
2589 return ldb_operr(ldb);
2592 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2593 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2594 DSDB_SEARCH_SHOW_EXTENDED_DN |
2595 DSDB_SEARCH_ONE_ONLY,
2596 "objectSid=%s", sid_str);
2597 talloc_free(sid_str);
2598 if (ret != LDB_SUCCESS) {
2602 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2609 load a repsFromTo blob list for a given partition GUID
2610 attr must be "repsFrom" or "repsTo"
2612 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2613 const char *attr, struct repsFromToBlob **r, uint32_t *count)
2615 const char *attrs[] = { attr, NULL };
2616 struct ldb_result *res = NULL;
2617 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2619 struct ldb_message_element *el;
2625 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
2626 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2627 /* partition hasn't been replicated yet */
2630 if (ret != LDB_SUCCESS) {
2631 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
2632 talloc_free(tmp_ctx);
2633 return WERR_DS_DRA_INTERNAL_ERROR;
2636 el = ldb_msg_find_element(res->msgs[0], attr);
2638 /* it's OK to be empty */
2639 talloc_free(tmp_ctx);
2643 *count = el->num_values;
2644 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
2646 talloc_free(tmp_ctx);
2647 return WERR_DS_DRA_INTERNAL_ERROR;
2650 for (i=0; i<(*count); i++) {
2651 enum ndr_err_code ndr_err;
2652 ndr_err = ndr_pull_struct_blob(&el->values[i],
2655 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
2656 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2657 talloc_free(tmp_ctx);
2658 return WERR_DS_DRA_INTERNAL_ERROR;
2662 talloc_free(tmp_ctx);
2668 save the repsFromTo blob list for a given partition GUID
2669 attr must be "repsFrom" or "repsTo"
2671 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2672 const char *attr, struct repsFromToBlob *r, uint32_t count)
2674 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2675 struct ldb_message *msg;
2676 struct ldb_message_element *el;
2679 msg = ldb_msg_new(tmp_ctx);
2681 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
2685 el->values = talloc_array(msg, struct ldb_val, count);
2690 for (i=0; i<count; i++) {
2692 enum ndr_err_code ndr_err;
2694 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
2696 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
2697 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2705 if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
2706 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
2710 talloc_free(tmp_ctx);
2715 talloc_free(tmp_ctx);
2716 return WERR_DS_DRA_INTERNAL_ERROR;
2721 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2722 object for a partition
2724 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
2725 uint64_t *uSN, uint64_t *urgent_uSN)
2727 struct ldb_request *req;
2729 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2730 struct dsdb_control_current_partition *p_ctrl;
2731 struct ldb_result *res;
2733 res = talloc_zero(tmp_ctx, struct ldb_result);
2735 talloc_free(tmp_ctx);
2736 return ldb_oom(ldb);
2739 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
2740 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
2744 res, ldb_search_default_callback,
2746 if (ret != LDB_SUCCESS) {
2747 talloc_free(tmp_ctx);
2751 p_ctrl = talloc(req, struct dsdb_control_current_partition);
2752 if (p_ctrl == NULL) {
2753 talloc_free(tmp_ctx);
2754 return ldb_oom(ldb);
2756 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2759 ret = ldb_request_add_control(req,
2760 DSDB_CONTROL_CURRENT_PARTITION_OID,
2762 if (ret != LDB_SUCCESS) {
2763 talloc_free(tmp_ctx);
2767 /* Run the new request */
2768 ret = ldb_request(ldb, req);
2770 if (ret == LDB_SUCCESS) {
2771 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2774 if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
2775 /* it hasn't been created yet, which means
2776 an implicit value of zero */
2778 talloc_free(tmp_ctx);
2782 if (ret != LDB_SUCCESS) {
2783 talloc_free(tmp_ctx);
2787 if (res->count < 1) {
2793 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
2795 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
2799 talloc_free(tmp_ctx);
2804 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
2805 const struct drsuapi_DsReplicaCursor2 *c2)
2807 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2810 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
2811 const struct drsuapi_DsReplicaCursor *c2)
2813 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2818 see if a computer identified by its invocationId is a RODC
2820 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
2822 /* 1) find the DN for this servers NTDSDSA object
2823 2) search for the msDS-isRODC attribute
2824 3) if not present then not a RODC
2825 4) if present and TRUE then is a RODC
2827 struct ldb_dn *config_dn;
2828 const char *attrs[] = { "msDS-isRODC", NULL };
2830 struct ldb_result *res;
2831 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
2833 config_dn = ldb_get_config_basedn(sam_ctx);
2835 talloc_free(tmp_ctx);
2836 return ldb_operr(sam_ctx);
2839 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
2840 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
2842 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2844 talloc_free(tmp_ctx);
2848 if (ret != LDB_SUCCESS) {
2849 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
2850 GUID_string(tmp_ctx, objectGUID)));
2852 talloc_free(tmp_ctx);
2856 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
2857 *is_rodc = (ret == 1);
2859 talloc_free(tmp_ctx);
2865 see if we are a RODC
2867 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
2869 const struct GUID *objectGUID;
2873 /* see if we have a cached copy */
2874 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
2880 objectGUID = samdb_ntds_objectGUID(sam_ctx);
2882 return ldb_operr(sam_ctx);
2885 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
2886 if (ret != LDB_SUCCESS) {
2890 cached = talloc(sam_ctx, bool);
2891 if (cached == NULL) {
2892 return ldb_oom(sam_ctx);
2896 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
2897 if (ret != LDB_SUCCESS) {
2898 talloc_free(cached);
2899 return ldb_operr(sam_ctx);
2905 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
2907 TALLOC_CTX *tmp_ctx;
2910 tmp_ctx = talloc_new(ldb);
2911 if (tmp_ctx == NULL) {
2915 cached = talloc(tmp_ctx, bool);
2921 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
2925 talloc_steal(ldb, cached);
2926 talloc_free(tmp_ctx);
2930 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
2931 talloc_free(tmp_ctx);
2937 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
2938 * flags are DS_NTDSSETTINGS_OPT_*
2940 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
2944 TALLOC_CTX *tmp_ctx;
2945 struct ldb_result *res;
2946 struct ldb_dn *site_dn;
2947 const char *attrs[] = { "options", NULL };
2949 tmp_ctx = talloc_new(ldb_ctx);
2950 if (tmp_ctx == NULL)
2953 /* Retrieve the site dn for the ldb that we
2954 * have open. This is our local site.
2956 site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
2957 if (site_dn == NULL)
2960 /* Perform a one level (child) search from the local
2961 * site distinguided name. We're looking for the
2962 * "options" attribute within the nTDSSiteSettings
2965 rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
2966 LDB_SCOPE_ONELEVEL, attrs,
2967 "objectClass=nTDSSiteSettings");
2969 if (rc != LDB_SUCCESS || res->count != 1)
2972 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
2974 talloc_free(tmp_ctx);
2979 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
2980 talloc_free(tmp_ctx);
2981 return LDB_ERR_NO_SUCH_OBJECT;
2985 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
2987 flags are DS_NTDS_OPTION_*
2989 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
2991 TALLOC_CTX *tmp_ctx;
2992 const char *attrs[] = { "options", NULL };
2994 struct ldb_result *res;
2996 tmp_ctx = talloc_new(ldb);
2997 if (tmp_ctx == NULL) {
3001 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3002 if (ret != LDB_SUCCESS) {
3006 if (res->count != 1) {
3010 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3012 talloc_free(tmp_ctx);
3017 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3018 talloc_free(tmp_ctx);
3019 return LDB_ERR_NO_SUCH_OBJECT;
3022 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3024 const char *attrs[] = { "objectCategory", NULL };
3026 struct ldb_result *res;
3028 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3029 if (ret != LDB_SUCCESS) {
3033 if (res->count != 1) {
3037 return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3040 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3045 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3046 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3048 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3050 char **tokens, *ret;
3053 tokens = str_list_make(mem_ctx, cn, " -_");
3057 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3058 tokens[0][0] = tolower(tokens[0][0]);
3059 for (i = 1; i < str_list_length((const char * const *)tokens); i++)
3060 tokens[i][0] = toupper(tokens[i][0]);
3062 ret = talloc_strdup(mem_ctx, tokens[0]);
3063 for (i = 1; i < str_list_length((const char * const *)tokens); i++)
3064 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3066 talloc_free(tokens);
3072 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3074 int dsdb_functional_level(struct ldb_context *ldb)
3076 int *domainFunctionality =
3077 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3078 if (!domainFunctionality) {
3079 /* this is expected during initial provision */
3080 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3081 return DS_DOMAIN_FUNCTION_2000;
3083 return *domainFunctionality;
3087 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3089 int dsdb_forest_functional_level(struct ldb_context *ldb)
3091 int *forestFunctionality =
3092 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3093 if (!forestFunctionality) {
3094 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3095 return DS_DOMAIN_FUNCTION_2000;
3097 return *forestFunctionality;
3101 set a GUID in an extended DN structure
3103 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3109 status = GUID_to_ndr_blob(guid, dn, &v);
3110 if (!NT_STATUS_IS_OK(status)) {
3111 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3114 ret = ldb_dn_set_extended_component(dn, component_name, &v);
3120 return a GUID from a extended DN structure
3122 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3124 const struct ldb_val *v;
3126 v = ldb_dn_get_extended_component(dn, component_name);
3128 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3131 return GUID_from_ndr_blob(v, guid);
3135 return a uint64_t from a extended DN structure
3137 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3139 const struct ldb_val *v;
3142 v = ldb_dn_get_extended_component(dn, component_name);
3144 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3146 s = talloc_strndup(dn, (const char *)v->data, v->length);
3147 NT_STATUS_HAVE_NO_MEMORY(s);
3149 *val = strtoull(s, NULL, 0);
3152 return NT_STATUS_OK;
3156 return a NTTIME from a extended DN structure
3158 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3160 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3164 return a uint32_t from a extended DN structure
3166 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3168 const struct ldb_val *v;
3171 v = ldb_dn_get_extended_component(dn, component_name);
3173 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3176 s = talloc_strndup(dn, (const char *)v->data, v->length);
3177 NT_STATUS_HAVE_NO_MEMORY(s);
3179 *val = strtoul(s, NULL, 0);
3182 return NT_STATUS_OK;
3186 return a dom_sid from a extended DN structure
3188 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3190 const struct ldb_val *sid_blob;
3191 struct TALLOC_CTX *tmp_ctx;
3192 enum ndr_err_code ndr_err;
3194 sid_blob = ldb_dn_get_extended_component(dn, component_name);
3196 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3199 tmp_ctx = talloc_new(NULL);
3201 ndr_err = ndr_pull_struct_blob_all(sid_blob, tmp_ctx, sid,
3202 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3203 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3204 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3205 talloc_free(tmp_ctx);
3209 talloc_free(tmp_ctx);
3210 return NT_STATUS_OK;
3215 return RMD_FLAGS directly from a ldb_dn
3216 returns 0 if not found
3218 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3220 const struct ldb_val *v;
3222 v = ldb_dn_get_extended_component(dn, "RMD_FLAGS");
3223 if (!v || v->length > sizeof(buf)-1) return 0;
3224 strncpy(buf, (const char *)v->data, v->length);
3226 return strtoul(buf, NULL, 10);
3230 return RMD_FLAGS directly from a ldb_val for a DN
3231 returns 0 if RMD_FLAGS is not found
3233 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3239 if (val->length < 13) {
3242 p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3246 flags = strtoul(p+11, &end, 10);
3247 if (!end || *end != '>') {
3248 /* it must end in a > */
3255 return true if a ldb_val containing a DN in storage form is deleted
3257 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3259 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3263 return true if a ldb_val containing a DN in storage form is
3264 in the upgraded w2k3 linked attribute format
3266 bool dsdb_dn_is_upgraded_link_val(struct ldb_val *val)
3268 return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
3272 return a DN for a wellknown GUID
3274 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3275 struct ldb_dn *nc_root, const char *wk_guid,
3276 struct ldb_dn **wkguid_dn)
3278 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3279 const char *attrs[] = { NULL };
3282 struct ldb_result *res;
3284 /* construct the magic WKGUID DN */
3285 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3286 wk_guid, ldb_dn_get_linearized(nc_root));
3288 talloc_free(tmp_ctx);
3289 return ldb_operr(samdb);
3292 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
3293 DSDB_SEARCH_SHOW_DELETED |
3294 DSDB_SEARCH_SHOW_RECYCLED);
3295 if (ret != LDB_SUCCESS) {
3296 talloc_free(tmp_ctx);
3300 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
3301 talloc_free(tmp_ctx);
3306 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
3308 return ldb_dn_compare(*dn1, *dn2);
3312 find a NC root given a DN within the NC
3314 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3315 struct ldb_dn **nc_root)
3317 const char *root_attrs[] = { "namingContexts", NULL };
3318 TALLOC_CTX *tmp_ctx;
3320 struct ldb_message_element *el;
3321 struct ldb_result *root_res;
3323 struct ldb_dn **nc_dns;
3325 tmp_ctx = talloc_new(samdb);
3326 if (tmp_ctx == NULL) {
3327 return ldb_oom(samdb);
3330 ret = ldb_search(samdb, tmp_ctx, &root_res,
3331 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
3332 if (ret != LDB_SUCCESS) {
3333 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
3334 talloc_free(tmp_ctx);
3338 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
3339 if ((el == NULL) || (el->num_values < 3)) {
3340 struct ldb_message *tmp_msg;
3342 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list."));
3344 /* This generates a temporary list of NCs in order to let the
3345 * provisioning work. */
3346 tmp_msg = ldb_msg_new(tmp_ctx);
3347 if (tmp_msg == NULL) {
3348 talloc_free(tmp_ctx);
3349 return ldb_oom(samdb);
3351 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3352 ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
3353 if (ret != LDB_SUCCESS) {
3354 talloc_free(tmp_ctx);
3357 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3358 ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
3359 if (ret != LDB_SUCCESS) {
3360 talloc_free(tmp_ctx);
3363 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3364 ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
3365 if (ret != LDB_SUCCESS) {
3366 talloc_free(tmp_ctx);
3369 el = &tmp_msg->elements[0];
3372 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
3374 talloc_free(tmp_ctx);
3375 return ldb_oom(samdb);
3378 for (i=0; i<el->num_values; i++) {
3379 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
3380 if (nc_dns[i] == NULL) {
3381 talloc_free(tmp_ctx);
3382 return ldb_operr(samdb);
3386 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
3388 for (i=0; i<el->num_values; i++) {
3389 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
3390 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
3391 talloc_free(tmp_ctx);
3396 talloc_free(tmp_ctx);
3397 return LDB_ERR_NO_SUCH_OBJECT;
3402 find the deleted objects DN for any object, by looking for the NC
3403 root, then looking up the wellknown GUID
3405 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
3406 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
3407 struct ldb_dn **do_dn)
3409 struct ldb_dn *nc_root;
3412 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
3413 if (ret != LDB_SUCCESS) {
3417 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
3418 talloc_free(nc_root);
3423 return the tombstoneLifetime, in days
3425 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
3428 dn = ldb_get_config_basedn(ldb);
3430 return LDB_ERR_NO_SUCH_OBJECT;
3432 dn = ldb_dn_copy(ldb, dn);
3434 return ldb_operr(ldb);
3436 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3437 be a wellknown GUID for this */
3438 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3440 return ldb_operr(ldb);
3443 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
3449 compare a ldb_val to a string case insensitively
3451 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
3453 size_t len = strlen(s);
3455 if (len > v->length) return 1;
3456 ret = strncasecmp(s, (const char *)v->data, v->length);
3457 if (ret != 0) return ret;
3458 if (v->length > len && v->data[len] != 0) {
3466 load the UDV for a partition in v2 format
3467 The list is returned sorted, and with our local cursor added
3469 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3470 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
3472 static const char *attrs[] = { "replUpToDateVector", NULL };
3473 struct ldb_result *r;
3474 const struct ldb_val *ouv_value;
3477 uint64_t highest_usn;
3478 const struct GUID *our_invocation_id;
3479 struct timeval now = timeval_current();
3481 ret = ldb_search(samdb, mem_ctx, &r, dn, LDB_SCOPE_BASE, attrs, NULL);
3482 if (ret != LDB_SUCCESS) {
3486 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
3488 enum ndr_err_code ndr_err;
3489 struct replUpToDateVectorBlob ouv;
3491 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
3492 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
3493 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3495 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3497 if (ouv.version != 2) {
3498 /* we always store as version 2, and
3499 * replUpToDateVector is not replicated
3501 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3504 *count = ouv.ctr.ctr2.count;
3505 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
3513 our_invocation_id = samdb_ntds_invocation_id(samdb);
3514 if (!our_invocation_id) {
3515 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
3516 talloc_free(*cursors);
3517 return ldb_operr(samdb);
3520 ret = dsdb_load_partition_usn(samdb, dn, &highest_usn, NULL);
3521 if (ret != LDB_SUCCESS) {
3522 /* nothing to add - this can happen after a vampire */
3523 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3527 for (i=0; i<*count; i++) {
3528 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
3529 (*cursors)[i].highest_usn = highest_usn;
3530 (*cursors)[i].last_sync_success = timeval_to_nttime(&now);
3531 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3536 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
3538 return ldb_oom(samdb);
3541 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
3542 (*cursors)[*count].highest_usn = highest_usn;
3543 (*cursors)[*count].last_sync_success = timeval_to_nttime(&now);
3546 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3552 load the UDV for a partition in version 1 format
3553 The list is returned sorted, and with our local cursor added
3555 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3556 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
3558 struct drsuapi_DsReplicaCursor2 *v2;
3562 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
3563 if (ret != LDB_SUCCESS) {
3573 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
3574 if (*cursors == NULL) {
3576 return ldb_oom(samdb);
3579 for (i=0; i<*count; i++) {
3580 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
3581 (*cursors)[i].highest_usn = v2[i].highest_usn;
3588 add a set of controls to a ldb_request structure based on a set of
3589 flags. See util.h for a list of available flags
3591 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
3594 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
3595 struct ldb_search_options_control *options;
3596 /* Using the phantom root control allows us to search all partitions */
3597 options = talloc(req, struct ldb_search_options_control);
3598 if (options == NULL) {
3599 return LDB_ERR_OPERATIONS_ERROR;
3601 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
3603 ret = ldb_request_add_control(req,
3604 LDB_CONTROL_SEARCH_OPTIONS_OID,
3606 if (ret != LDB_SUCCESS) {
3611 if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
3612 ret = ldb_request_add_control(req,
3613 DSDB_CONTROL_NO_GLOBAL_CATALOG,
3615 if (ret != LDB_SUCCESS) {
3620 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
3621 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
3622 if (ret != LDB_SUCCESS) {
3627 if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
3628 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
3629 if (ret != LDB_SUCCESS) {
3634 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
3635 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
3636 if (ret != LDB_SUCCESS) {
3641 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
3642 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
3643 if (!extended_ctrl) {
3644 return LDB_ERR_OPERATIONS_ERROR;
3646 extended_ctrl->type = 1;
3648 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
3649 if (ret != LDB_SUCCESS) {
3654 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
3655 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
3656 if (ret != LDB_SUCCESS) {
3661 if (dsdb_flags & DSDB_MODIFY_RELAX) {
3662 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
3663 if (ret != LDB_SUCCESS) {
3668 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
3669 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
3670 if (ret != LDB_SUCCESS) {
3675 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
3676 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
3677 if (ret != LDB_SUCCESS) {
3682 if (dsdb_flags & DSDB_TREE_DELETE) {
3683 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
3684 if (ret != LDB_SUCCESS) {
3689 if (dsdb_flags & DSDB_PROVISION) {
3690 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
3691 if (ret != LDB_SUCCESS) {
3696 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
3697 if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
3698 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
3699 if (ret != LDB_SUCCESS) {
3704 if (dsdb_flags & DSDB_PASSWORD_BYPASS_LAST_SET) {
3706 * This must not be critical, as it will only be
3707 * handled (and need to be handled) if the other
3708 * attributes in the request bring password_hash into
3711 ret = ldb_request_add_control(req, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID, false, NULL);
3712 if (ret != LDB_SUCCESS) {
3717 if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
3718 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
3719 if (ret != LDB_SUCCESS) {
3728 an add with a set of controls
3730 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
3731 uint32_t dsdb_flags)
3733 struct ldb_request *req;
3736 ret = ldb_build_add_req(&req, ldb, ldb,
3740 ldb_op_default_callback,
3743 if (ret != LDB_SUCCESS) return ret;
3745 ret = dsdb_request_add_controls(req, dsdb_flags);
3746 if (ret != LDB_SUCCESS) {
3751 ret = dsdb_autotransaction_request(ldb, req);
3758 a modify with a set of controls
3760 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
3761 uint32_t dsdb_flags)
3763 struct ldb_request *req;
3766 ret = ldb_build_mod_req(&req, ldb, ldb,
3770 ldb_op_default_callback,
3773 if (ret != LDB_SUCCESS) return ret;
3775 ret = dsdb_request_add_controls(req, dsdb_flags);
3776 if (ret != LDB_SUCCESS) {
3781 ret = dsdb_autotransaction_request(ldb, req);
3788 a delete with a set of flags
3790 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
3791 uint32_t dsdb_flags)
3793 struct ldb_request *req;
3796 ret = ldb_build_del_req(&req, ldb, ldb,
3800 ldb_op_default_callback,
3803 if (ret != LDB_SUCCESS) return ret;
3805 ret = dsdb_request_add_controls(req, dsdb_flags);
3806 if (ret != LDB_SUCCESS) {
3811 ret = dsdb_autotransaction_request(ldb, req);
3818 like dsdb_modify() but set all the element flags to
3819 LDB_FLAG_MOD_REPLACE
3821 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
3825 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3826 for (i=0;i<msg->num_elements;i++) {
3827 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3830 return dsdb_modify(ldb, msg, dsdb_flags);
3835 search for attrs on one DN, allowing for dsdb_flags controls
3837 int dsdb_search_dn(struct ldb_context *ldb,
3838 TALLOC_CTX *mem_ctx,
3839 struct ldb_result **_res,
3840 struct ldb_dn *basedn,
3841 const char * const *attrs,
3842 uint32_t dsdb_flags)
3845 struct ldb_request *req;
3846 struct ldb_result *res;
3848 res = talloc_zero(mem_ctx, struct ldb_result);
3850 return ldb_oom(ldb);
3853 ret = ldb_build_search_req(&req, ldb, res,
3860 ldb_search_default_callback,
3862 if (ret != LDB_SUCCESS) {
3867 ret = dsdb_request_add_controls(req, dsdb_flags);
3868 if (ret != LDB_SUCCESS) {
3873 ret = ldb_request(ldb, req);
3874 if (ret == LDB_SUCCESS) {
3875 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3879 if (ret != LDB_SUCCESS) {
3889 search for attrs on one DN, by the GUID of the DN, allowing for
3892 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
3893 TALLOC_CTX *mem_ctx,
3894 struct ldb_result **_res,
3895 const struct GUID *guid,
3896 const char * const *attrs,
3897 uint32_t dsdb_flags)
3899 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3903 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
3905 talloc_free(tmp_ctx);
3906 return ldb_oom(ldb);
3909 ret = dsdb_search_dn(ldb, mem_ctx, _res, dn, attrs, dsdb_flags);
3910 talloc_free(tmp_ctx);
3915 general search with dsdb_flags for controls
3917 int dsdb_search(struct ldb_context *ldb,
3918 TALLOC_CTX *mem_ctx,
3919 struct ldb_result **_res,
3920 struct ldb_dn *basedn,
3921 enum ldb_scope scope,
3922 const char * const *attrs,
3923 uint32_t dsdb_flags,
3924 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
3927 struct ldb_request *req;
3928 struct ldb_result *res;
3930 char *expression = NULL;
3931 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3933 /* cross-partitions searches with a basedn break multi-domain support */
3934 SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
3936 res = talloc_zero(tmp_ctx, struct ldb_result);
3938 talloc_free(tmp_ctx);
3939 return ldb_oom(ldb);
3943 va_start(ap, exp_fmt);
3944 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
3948 talloc_free(tmp_ctx);
3949 return ldb_oom(ldb);
3953 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3960 ldb_search_default_callback,
3962 if (ret != LDB_SUCCESS) {
3963 talloc_free(tmp_ctx);
3967 ret = dsdb_request_add_controls(req, dsdb_flags);
3968 if (ret != LDB_SUCCESS) {
3969 talloc_free(tmp_ctx);
3970 ldb_reset_err_string(ldb);
3974 ret = ldb_request(ldb, req);
3975 if (ret == LDB_SUCCESS) {
3976 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3979 if (ret != LDB_SUCCESS) {
3980 talloc_free(tmp_ctx);
3984 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
3985 if (res->count == 0) {
3986 talloc_free(tmp_ctx);
3987 ldb_reset_err_string(ldb);
3988 return LDB_ERR_NO_SUCH_OBJECT;
3990 if (res->count != 1) {
3991 talloc_free(tmp_ctx);
3992 ldb_reset_err_string(ldb);
3993 return LDB_ERR_CONSTRAINT_VIOLATION;
3997 *_res = talloc_steal(mem_ctx, res);
3998 talloc_free(tmp_ctx);
4005 general search with dsdb_flags for controls
4006 returns exactly 1 record or an error
4008 int dsdb_search_one(struct ldb_context *ldb,
4009 TALLOC_CTX *mem_ctx,
4010 struct ldb_message **msg,
4011 struct ldb_dn *basedn,
4012 enum ldb_scope scope,
4013 const char * const *attrs,
4014 uint32_t dsdb_flags,
4015 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4018 struct ldb_result *res;
4020 char *expression = NULL;
4021 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4023 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4025 res = talloc_zero(tmp_ctx, struct ldb_result);
4027 talloc_free(tmp_ctx);
4028 return ldb_oom(ldb);
4032 va_start(ap, exp_fmt);
4033 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4037 talloc_free(tmp_ctx);
4038 return ldb_oom(ldb);
4040 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4041 dsdb_flags, "%s", expression);
4043 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4047 if (ret != LDB_SUCCESS) {
4048 talloc_free(tmp_ctx);
4052 *msg = talloc_steal(mem_ctx, res->msgs[0]);
4053 talloc_free(tmp_ctx);
4058 /* returns back the forest DNS name */
4059 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4061 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4062 ldb_get_root_basedn(ldb));
4065 if (forest_name == NULL) {
4069 p = strchr(forest_name, '/');
4077 /* returns back the default domain DNS name */
4078 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4080 const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4081 ldb_get_default_basedn(ldb));
4084 if (domain_name == NULL) {
4088 p = strchr(domain_name, '/');
4097 validate that an DSA GUID belongs to the specified user sid.
4098 The user SID must be a domain controller account (either RODC or
4101 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4102 const struct GUID *dsa_guid,
4103 const struct dom_sid *sid)
4106 - find DN of record with the DSA GUID in the
4107 configuration partition (objectGUID)
4108 - remove "NTDS Settings" component from DN
4109 - do a base search on that DN for serverReference with
4111 - extract objectSid from resulting serverReference
4113 - check this sid matches the sid argument
4115 struct ldb_dn *config_dn;
4116 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4117 struct ldb_message *msg;
4118 const char *attrs1[] = { NULL };
4119 const char *attrs2[] = { "serverReference", NULL };
4121 struct ldb_dn *dn, *account_dn;
4122 struct dom_sid sid2;
4125 config_dn = ldb_get_config_basedn(ldb);
4127 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4128 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4129 if (ret != LDB_SUCCESS) {
4130 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4131 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4132 talloc_free(tmp_ctx);
4133 return ldb_operr(ldb);
4137 if (!ldb_dn_remove_child_components(dn, 1)) {
4138 talloc_free(tmp_ctx);
4139 return ldb_operr(ldb);
4142 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4143 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4144 "(objectClass=server)");
4145 if (ret != LDB_SUCCESS) {
4146 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4147 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4148 talloc_free(tmp_ctx);
4149 return ldb_operr(ldb);
4152 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4153 if (account_dn == NULL) {
4154 DEBUG(1,(__location__ ": Failed to find account_dn for DSA with objectGUID %s, sid %s\n",
4155 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4156 talloc_free(tmp_ctx);
4157 return ldb_operr(ldb);
4160 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4161 if (!NT_STATUS_IS_OK(status)) {
4162 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4163 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4164 talloc_free(tmp_ctx);
4165 return ldb_operr(ldb);
4168 if (!dom_sid_equal(sid, &sid2)) {
4169 /* someone is trying to spoof another account */
4170 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4171 GUID_string(tmp_ctx, dsa_guid),
4172 dom_sid_string(tmp_ctx, sid),
4173 dom_sid_string(tmp_ctx, &sid2)));
4174 talloc_free(tmp_ctx);
4175 return ldb_operr(ldb);
4178 talloc_free(tmp_ctx);
4182 static const char * const secret_attributes[] = {
4183 DSDB_SECRET_ATTRIBUTES,
4188 check if the attribute belongs to the RODC filtered attribute set
4189 Note that attributes that are in the filtered attribute set are the
4190 ones that _are_ always sent to a RODC
4192 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4194 /* they never get secret attributes */
4195 if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4199 /* they do get non-secret critical attributes */
4200 if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4204 /* they do get non-secret attributes marked as being in the FAS */
4205 if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4209 /* other attributes are denied */
4213 /* return fsmo role dn and role owner dn for a particular role*/
4214 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4215 struct ldb_context *ldb,
4217 struct ldb_dn **fsmo_role_dn,
4218 struct ldb_dn **role_owner_dn)
4222 case DREPL_NAMING_MASTER:
4223 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
4224 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4225 if (ret != LDB_SUCCESS) {
4226 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
4227 ldb_errstring(ldb)));
4228 talloc_free(tmp_ctx);
4229 return WERR_DS_DRA_INTERNAL_ERROR;
4232 case DREPL_INFRASTRUCTURE_MASTER:
4233 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
4234 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4235 if (ret != LDB_SUCCESS) {
4236 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4237 ldb_errstring(ldb)));
4238 talloc_free(tmp_ctx);
4239 return WERR_DS_DRA_INTERNAL_ERROR;
4242 case DREPL_RID_MASTER:
4243 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
4244 if (ret != LDB_SUCCESS) {
4245 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
4246 talloc_free(tmp_ctx);
4247 return WERR_DS_DRA_INTERNAL_ERROR;
4250 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4251 if (ret != LDB_SUCCESS) {
4252 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
4253 ldb_errstring(ldb)));
4254 talloc_free(tmp_ctx);
4255 return WERR_DS_DRA_INTERNAL_ERROR;
4258 case DREPL_SCHEMA_MASTER:
4259 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
4260 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4261 if (ret != LDB_SUCCESS) {
4262 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4263 ldb_errstring(ldb)));
4264 talloc_free(tmp_ctx);
4265 return WERR_DS_DRA_INTERNAL_ERROR;
4268 case DREPL_PDC_MASTER:
4269 *fsmo_role_dn = ldb_get_default_basedn(ldb);
4270 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4271 if (ret != LDB_SUCCESS) {
4272 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
4273 ldb_errstring(ldb)));
4274 talloc_free(tmp_ctx);
4275 return WERR_DS_DRA_INTERNAL_ERROR;
4279 return WERR_DS_DRA_INTERNAL_ERROR;
4284 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
4285 TALLOC_CTX *mem_ctx,
4286 struct ldb_dn *server_dn)
4289 struct ldb_result *res = NULL;
4290 const char * const attrs[] = { "dNSHostName", NULL};
4292 ldb_ret = ldb_search(ldb, mem_ctx, &res,
4296 if (ldb_ret != LDB_SUCCESS) {
4297 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
4298 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
4302 return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
4306 returns true if an attribute is in the filter,
4307 false otherwise, provided that attribute value is provided with the expression
4309 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
4313 switch (tree->operation) {
4316 for (i=0;i<tree->u.list.num_elements;i++) {
4317 if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
4323 return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
4324 case LDB_OP_EQUALITY:
4325 case LDB_OP_GREATER:
4328 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
4332 case LDB_OP_SUBSTRING:
4333 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
4337 case LDB_OP_PRESENT:
4338 /* (attrname=*) is not filtered out */
4340 case LDB_OP_EXTENDED:
4341 if (tree->u.extended.attr &&
4342 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
4350 bool is_attr_in_list(const char * const * attrs, const char *attr)
4354 for (i = 0; attrs[i]; i++) {
4355 if (ldb_attr_cmp(attrs[i], attr) == 0)
4364 map an ldb error code to an approximate NTSTATUS code
4366 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
4370 return NT_STATUS_OK;
4372 case LDB_ERR_PROTOCOL_ERROR:
4373 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
4375 case LDB_ERR_TIME_LIMIT_EXCEEDED:
4376 return NT_STATUS_IO_TIMEOUT;
4378 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
4379 return NT_STATUS_BUFFER_TOO_SMALL;
4381 case LDB_ERR_COMPARE_FALSE:
4382 case LDB_ERR_COMPARE_TRUE:
4383 return NT_STATUS_REVISION_MISMATCH;
4385 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
4386 return NT_STATUS_NOT_SUPPORTED;
4388 case LDB_ERR_STRONG_AUTH_REQUIRED:
4389 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
4390 case LDB_ERR_SASL_BIND_IN_PROGRESS:
4391 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
4392 case LDB_ERR_INVALID_CREDENTIALS:
4393 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
4394 case LDB_ERR_UNWILLING_TO_PERFORM:
4395 return NT_STATUS_ACCESS_DENIED;
4397 case LDB_ERR_NO_SUCH_OBJECT:
4398 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4400 case LDB_ERR_REFERRAL:
4401 case LDB_ERR_NO_SUCH_ATTRIBUTE:
4402 return NT_STATUS_NOT_FOUND;
4404 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
4405 return NT_STATUS_NOT_SUPPORTED;
4407 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
4408 return NT_STATUS_BUFFER_TOO_SMALL;
4410 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
4411 case LDB_ERR_INAPPROPRIATE_MATCHING:
4412 case LDB_ERR_CONSTRAINT_VIOLATION:
4413 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
4414 case LDB_ERR_INVALID_DN_SYNTAX:
4415 case LDB_ERR_NAMING_VIOLATION:
4416 case LDB_ERR_OBJECT_CLASS_VIOLATION:
4417 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
4418 case LDB_ERR_NOT_ALLOWED_ON_RDN:
4419 return NT_STATUS_INVALID_PARAMETER;
4421 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
4422 case LDB_ERR_ENTRY_ALREADY_EXISTS:
4423 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
4426 return NT_STATUS_NETWORK_BUSY;
4428 case LDB_ERR_ALIAS_PROBLEM:
4429 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
4430 case LDB_ERR_UNAVAILABLE:
4431 case LDB_ERR_LOOP_DETECT:
4432 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
4433 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
4435 case LDB_ERR_OPERATIONS_ERROR:
4438 return NT_STATUS_UNSUCCESSFUL;
4443 create a new naming context that will hold a partial replica
4445 int dsdb_create_partial_replica_NC(struct ldb_context *ldb, struct ldb_dn *dn)
4447 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4448 struct ldb_message *msg;
4451 msg = ldb_msg_new(tmp_ctx);
4453 talloc_free(tmp_ctx);
4454 return ldb_oom(ldb);
4458 ret = ldb_msg_add_string(msg, "objectClass", "top");
4459 if (ret != LDB_SUCCESS) {
4460 talloc_free(tmp_ctx);
4461 return ldb_oom(ldb);
4464 /* [MS-DRSR] implies that we should only add the 'top'
4465 * objectclass, but that would cause lots of problems with our
4466 * objectclass code as top is not structural, so we add
4467 * 'domainDNS' as well to keep things sane. We're expecting
4468 * this new NC to be of objectclass domainDNS after
4469 * replication anyway
4471 ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
4472 if (ret != LDB_SUCCESS) {
4473 talloc_free(tmp_ctx);
4474 return ldb_oom(ldb);
4477 ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
4478 INSTANCE_TYPE_IS_NC_HEAD|
4479 INSTANCE_TYPE_NC_ABOVE|
4480 INSTANCE_TYPE_UNINSTANT);
4481 if (ret != LDB_SUCCESS) {
4482 talloc_free(tmp_ctx);
4483 return ldb_oom(ldb);
4486 ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
4487 if (ret != LDB_SUCCESS && ret != LDB_ERR_ENTRY_ALREADY_EXISTS) {
4488 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
4489 ldb_dn_get_linearized(dn),
4490 ldb_errstring(ldb), ldb_strerror(ret)));
4491 talloc_free(tmp_ctx);
4495 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
4497 talloc_free(tmp_ctx);
4502 build a GUID from a string
4504 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
4506 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
4508 uint32_t time_mid, time_hi_and_version;
4509 uint32_t clock_seq[2];
4514 return NT_STATUS_INVALID_PARAMETER;
4517 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
4518 &time_low, &time_mid, &time_hi_and_version,
4519 &clock_seq[0], &clock_seq[1],
4520 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
4521 status = NT_STATUS_OK;
4524 if (!NT_STATUS_IS_OK(status)) {
4528 guid->time_low = time_low;
4529 guid->time_mid = time_mid;
4530 guid->time_hi_and_version = time_hi_and_version;
4531 guid->clock_seq[0] = clock_seq[0];
4532 guid->clock_seq[1] = clock_seq[1];
4534 guid->node[i] = node[i];
4537 return NT_STATUS_OK;
4540 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
4542 return talloc_asprintf(mem_ctx,
4543 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
4544 guid->time_low, guid->time_mid,
4545 guid->time_hi_and_version,
4548 guid->node[0], guid->node[1],
4549 guid->node[2], guid->node[3],
4550 guid->node[4], guid->node[5]);