dsdb: give a better error message and return code on failed password change
[Samba/wip.git] / source4 / dsdb / common / util.c
blob0ad0ea37ed70a8cd0d47050fa05a48c3c1d2f87e
1 /*
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/>.
24 #include "includes.h"
25 #include "events/events.h"
26 #include "ldb.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,
53 TALLOC_CTX *mem_ctx,
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)
60 va_list ap;
61 int i, count;
63 va_start(ap, format);
64 count = gendb_search_v(sam_ldb, mem_ctx, basedn,
65 res, attrs, format, ap);
66 va_end(ap);
68 i=0;
70 while (i<count) {
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];
79 count -= 1;
80 talloc_free(entry_sid);
81 continue;
83 talloc_free(entry_sid);
84 i += 1;
87 return count;
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,
94 TALLOC_CTX *mem_ctx,
95 struct ldb_dn *basedn,
96 const char *attr_name,
97 const char *format, va_list ap) _PRINTF_ATTRIBUTE(5,0)
99 int count;
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);
106 if (count > 1) {
107 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
108 attr_name, format, count));
110 if (count != 1) {
111 talloc_free(res);
112 return NULL;
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,
122 TALLOC_CTX *mem_ctx,
123 struct ldb_dn *basedn,
124 const char *attr_name,
125 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
127 va_list ap;
128 const char *str;
130 va_start(ap, format);
131 str = samdb_search_string_v(sam_ldb, mem_ctx, basedn, attr_name, format, ap);
132 va_end(ap);
134 return str;
137 struct ldb_dn *samdb_search_dn(struct ldb_context *sam_ldb,
138 TALLOC_CTX *mem_ctx,
139 struct ldb_dn *basedn,
140 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
142 va_list ap;
143 struct ldb_dn *ret;
144 struct ldb_message **res = NULL;
145 int count;
147 va_start(ap, format);
148 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, NULL, format, ap);
149 va_end(ap);
151 if (count != 1) return NULL;
153 ret = talloc_steal(mem_ctx, res[0]->dn);
154 talloc_free(res);
156 return ret;
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,
163 TALLOC_CTX *mem_ctx,
164 struct ldb_dn *basedn,
165 const char *attr_name,
166 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
168 va_list ap;
169 int count;
170 struct ldb_message **res;
171 const char *attrs[2] = { NULL, NULL };
172 struct dom_sid *sid;
174 attrs[0] = attr_name;
176 va_start(ap, format);
177 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
178 va_end(ap);
179 if (count > 1) {
180 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
181 attr_name, format, count));
183 if (count != 1) {
184 talloc_free(res);
185 return NULL;
187 sid = samdb_result_dom_sid(mem_ctx, res[0], attr_name);
188 talloc_free(res);
189 return sid;
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,
196 TALLOC_CTX *mem_ctx,
197 struct ldb_dn *basedn,
198 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
200 va_list ap;
201 const char *attrs[] = { NULL };
202 int ret;
204 va_start(ap, format);
205 ret = gendb_search_v(sam_ldb, mem_ctx, basedn, NULL, attrs, format, ap);
206 va_end(ap);
208 return ret;
213 search the sam for a single integer attribute in exactly 1 record
215 unsigned int samdb_search_uint(struct ldb_context *sam_ldb,
216 TALLOC_CTX *mem_ctx,
217 unsigned int default_value,
218 struct ldb_dn *basedn,
219 const char *attr_name,
220 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
222 va_list ap;
223 int count;
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);
231 va_end(ap);
233 if (count != 1) {
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,
244 TALLOC_CTX *mem_ctx,
245 int64_t default_value,
246 struct ldb_dn *basedn,
247 const char *attr_name,
248 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
250 va_list ap;
251 int count;
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);
259 va_end(ap);
261 if (count != 1) {
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,
273 TALLOC_CTX *mem_ctx,
274 struct ldb_dn *basedn,
275 const char ***strs,
276 const char *attr_name,
277 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
279 va_list ap;
280 int count, i;
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);
288 va_end(ap);
290 if (count <= 0) {
291 return count;
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",
298 attr_name, format));
299 talloc_free(res);
300 return -1;
304 *strs = talloc_array(mem_ctx, const char *, count+1);
305 if (! *strs) {
306 talloc_free(res);
307 return -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;
315 return count;
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);
322 if (!ret_dn) {
323 return default_value;
325 return ret_dn;
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)
334 struct dom_sid *sid;
335 uint32_t rid;
337 sid = samdb_result_dom_sid(mem_ctx, msg, attr);
338 if (sid == NULL) {
339 return default_value;
341 rid = sid->sub_auths[sid->num_auths-1];
342 talloc_free(sid);
343 return rid;
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,
350 const char *attr)
352 bool ok;
353 const struct ldb_val *v;
354 struct dom_sid *sid;
355 v = ldb_msg_find_ldb_val(msg, attr);
356 if (v == NULL) {
357 return NULL;
359 sid = talloc(mem_ctx, struct dom_sid);
360 if (sid == NULL) {
361 return NULL;
363 ok = sid_blob_parse(*v, sid);
364 if (!ok) {
365 talloc_free(sid);
366 return NULL;
368 return 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;
377 struct GUID guid;
378 NTSTATUS status;
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)) {
385 return GUID_zero();
388 return guid;
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,
396 const char *attr)
398 struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, msg, attr);
399 if (!sid || sid->num_auths < 1) return NULL;
400 sid->num_auths--;
401 return sid;
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);
423 if (ret == 0)
424 ret = 0x7FFFFFFFFFFFFFFFULL;
426 return ret;
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",
446 if (ret == 0)
447 ret = 0x7FFFFFFFFFFFFFFFULL;
449 return ret;
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,
457 TALLOC_CTX *mem_ctx,
458 struct ldb_dn *domain_dn,
459 struct ldb_message *msg,
460 const char *attr)
462 uint64_t attr_time = ldb_msg_find_attr_as_uint64(msg, attr, 0);
463 int64_t minPwdAge;
465 if (attr_time == 0) {
466 return 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;
475 return attr_time;
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,
483 TALLOC_CTX *mem_ctx,
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",
491 int64_t maxPwdAge;
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) {
500 return 0;
502 if (attr_time == -1) {
503 return 0x7FFFFFFFFFFFFFFFULL;
506 maxPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn,
507 "maxPwdAge", NULL);
508 if (maxPwdAge == 0 || maxPwdAge == -0x8000000000000000ULL) {
509 return 0x7FFFFFFFFFFFFFFFULL;
510 } else {
511 attr_time -= maxPwdAge;
514 return attr_time;
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)));
528 return 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);
540 *hashes = NULL;
541 if (!val) {
542 return 0;
544 count = val->length / 16;
545 if (count == 0) {
546 return 0;
549 *hashes = talloc_array(mem_ctx, struct samr_Password, count);
550 if (! *hashes) {
551 return 0;
554 for (i=0;i<count;i++) {
555 memcpy((*hashes)[i].hash, (i*16)+(char *)val->data, 16);
558 return count;
561 NTSTATUS samdb_result_passwords_no_lockout(TALLOC_CTX *mem_ctx,
562 struct loadparm_context *lp_ctx,
563 struct ldb_message *msg,
564 struct samr_Password **lm_pwd,
565 struct samr_Password **nt_pwd)
567 struct samr_Password *lmPwdHash, *ntPwdHash;
569 if (nt_pwd) {
570 unsigned int num_nt;
571 num_nt = samdb_result_hashes(mem_ctx, msg, "unicodePwd", &ntPwdHash);
572 if (num_nt == 0) {
573 *nt_pwd = NULL;
574 } else if (num_nt > 1) {
575 return NT_STATUS_INTERNAL_DB_CORRUPTION;
576 } else {
577 *nt_pwd = &ntPwdHash[0];
580 if (lm_pwd) {
581 /* Ensure that if we have turned off LM
582 * authentication, that we never use the LM hash, even
583 * if we store it */
584 if (lpcfg_lanman_auth(lp_ctx)) {
585 unsigned int num_lm;
586 num_lm = samdb_result_hashes(mem_ctx, msg, "dBCSPwd", &lmPwdHash);
587 if (num_lm == 0) {
588 *lm_pwd = NULL;
589 } else if (num_lm > 1) {
590 return NT_STATUS_INTERNAL_DB_CORRUPTION;
591 } else {
592 *lm_pwd = &lmPwdHash[0];
594 } else {
595 *lm_pwd = NULL;
598 return NT_STATUS_OK;
601 NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx,
602 struct loadparm_context *lp_ctx,
603 struct ldb_message *msg,
604 struct samr_Password **lm_pwd,
605 struct samr_Password **nt_pwd)
607 uint16_t acct_flags;
609 acct_flags = samdb_result_acct_flags(msg,
610 "msDS-User-Account-Control-Computed");
611 /* Quit if the account was locked out. */
612 if (acct_flags & ACB_AUTOLOCK) {
613 DEBUG(3,("samdb_result_passwords: Account for user %s was locked out.\n",
614 ldb_dn_get_linearized(msg->dn)));
615 return NT_STATUS_ACCOUNT_LOCKED_OUT;
618 return samdb_result_passwords_no_lockout(mem_ctx, lp_ctx, msg,
619 lm_pwd, nt_pwd);
623 pull a samr_LogonHours structutre from a result set.
625 struct samr_LogonHours samdb_result_logon_hours(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char *attr)
627 struct samr_LogonHours hours;
628 size_t units_per_week = 168;
629 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
631 ZERO_STRUCT(hours);
633 if (val) {
634 units_per_week = val->length * 8;
637 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week/8);
638 if (!hours.bits) {
639 return hours;
641 hours.units_per_week = units_per_week;
642 memset(hours.bits, 0xFF, units_per_week/8);
643 if (val) {
644 memcpy(hours.bits, val->data, val->length);
647 return hours;
651 pull a set of account_flags from a result set.
653 Naturally, this requires that userAccountControl and
654 (if not null) the attributes 'attr' be already
655 included in msg
657 uint32_t samdb_result_acct_flags(struct ldb_message *msg, const char *attr)
659 uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
660 uint32_t attr_flags = 0;
661 uint32_t acct_flags = ds_uf2acb(userAccountControl);
662 if (attr) {
663 attr_flags = ldb_msg_find_attr_as_uint(msg, attr, UF_ACCOUNTDISABLE);
664 if (attr_flags == UF_ACCOUNTDISABLE) {
665 DEBUG(0, ("Attribute %s not found, disabling account %s!\n", attr,
666 ldb_dn_get_linearized(msg->dn)));
668 acct_flags |= ds_uf2acb(attr_flags);
671 return acct_flags;
674 struct lsa_BinaryString samdb_result_parameters(TALLOC_CTX *mem_ctx,
675 struct ldb_message *msg,
676 const char *attr)
678 struct lsa_BinaryString s;
679 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
681 ZERO_STRUCT(s);
683 if (!val) {
684 return s;
687 s.array = talloc_array(mem_ctx, uint16_t, val->length/2);
688 if (!s.array) {
689 return s;
691 s.length = s.size = val->length;
692 memcpy(s.array, val->data, val->length);
694 return s;
697 /* Find an attribute, with a particular value */
699 /* The current callers of this function expect a very specific
700 * behaviour: In particular, objectClass subclass equivilance is not
701 * wanted. This means that we should not lookup the schema for the
702 * comparison function */
703 struct ldb_message_element *samdb_find_attribute(struct ldb_context *ldb,
704 const struct ldb_message *msg,
705 const char *name, const char *value)
707 unsigned int i;
708 struct ldb_message_element *el = ldb_msg_find_element(msg, name);
710 if (!el) {
711 return NULL;
714 for (i=0;i<el->num_values;i++) {
715 if (ldb_attr_cmp(value, (char *)el->values[i].data) == 0) {
716 return el;
720 return NULL;
723 int samdb_find_or_add_attribute(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
725 struct ldb_message_element *el;
727 el = ldb_msg_find_element(msg, name);
728 if (el) {
729 return LDB_SUCCESS;
732 return ldb_msg_add_string(msg, name, set_value);
736 add a dom_sid element to a message
738 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
739 const char *attr_name, const struct dom_sid *sid)
741 struct ldb_val v;
742 enum ndr_err_code ndr_err;
744 ndr_err = ndr_push_struct_blob(&v, mem_ctx,
745 sid,
746 (ndr_push_flags_fn_t)ndr_push_dom_sid);
747 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
748 return ldb_operr(sam_ldb);
750 return ldb_msg_add_value(msg, attr_name, &v, NULL);
755 add a delete element operation to a message
757 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
758 const char *attr_name)
760 /* we use an empty replace rather than a delete, as it allows for
761 dsdb_replace() to be used everywhere */
762 return ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_REPLACE, NULL);
766 add an add attribute value to a message or enhance an existing attribute
767 which has the same name and the add flag set.
769 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
770 struct ldb_message *msg, const char *attr_name,
771 const char *value)
773 struct ldb_message_element *el;
774 struct ldb_val val, *vals;
775 char *v;
776 unsigned int i;
777 bool found = false;
778 int ret;
780 v = talloc_strdup(mem_ctx, value);
781 if (v == NULL) {
782 return ldb_oom(sam_ldb);
785 val.data = (uint8_t *) v;
786 val.length = strlen(v);
788 if (val.length == 0) {
789 /* allow empty strings as non-existent attributes */
790 return LDB_SUCCESS;
793 for (i = 0; i < msg->num_elements; i++) {
794 el = &msg->elements[i];
795 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
796 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD)) {
797 found = true;
798 break;
801 if (!found) {
802 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_ADD,
803 &el);
804 if (ret != LDB_SUCCESS) {
805 return ret;
809 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
810 el->num_values + 1);
811 if (vals == NULL) {
812 return ldb_oom(sam_ldb);
814 el->values = vals;
815 el->values[el->num_values] = val;
816 ++(el->num_values);
818 return LDB_SUCCESS;
822 add a delete attribute value to a message or enhance an existing attribute
823 which has the same name and the delete flag set.
825 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
826 struct ldb_message *msg, const char *attr_name,
827 const char *value)
829 struct ldb_message_element *el;
830 struct ldb_val val, *vals;
831 char *v;
832 unsigned int i;
833 bool found = false;
834 int ret;
836 v = talloc_strdup(mem_ctx, value);
837 if (v == NULL) {
838 return ldb_oom(sam_ldb);
841 val.data = (uint8_t *) v;
842 val.length = strlen(v);
844 if (val.length == 0) {
845 /* allow empty strings as non-existent attributes */
846 return LDB_SUCCESS;
849 for (i = 0; i < msg->num_elements; i++) {
850 el = &msg->elements[i];
851 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
852 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
853 found = true;
854 break;
857 if (!found) {
858 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_DELETE,
859 &el);
860 if (ret != LDB_SUCCESS) {
861 return ret;
865 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
866 el->num_values + 1);
867 if (vals == NULL) {
868 return ldb_oom(sam_ldb);
870 el->values = vals;
871 el->values[el->num_values] = val;
872 ++(el->num_values);
874 return LDB_SUCCESS;
878 add a int element to a message
880 int samdb_msg_add_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
881 const char *attr_name, int v)
883 const char *s = talloc_asprintf(mem_ctx, "%d", v);
884 if (s == NULL) {
885 return ldb_oom(sam_ldb);
887 return ldb_msg_add_string(msg, attr_name, s);
891 * Add an unsigned int element to a message
893 * The issue here is that we have not yet first cast to int32_t explicitly,
894 * before we cast to an signed int to printf() into the %d or cast to a
895 * int64_t before we then cast to a long long to printf into a %lld.
897 * There are *no* unsigned integers in Active Directory LDAP, even the RID
898 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
899 * (See the schema, and the syntax definitions in schema_syntax.c).
902 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
903 const char *attr_name, unsigned int v)
905 return samdb_msg_add_int(sam_ldb, mem_ctx, msg, attr_name, (int)v);
909 add a (signed) int64_t element to a message
911 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
912 const char *attr_name, int64_t v)
914 const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
915 if (s == NULL) {
916 return ldb_oom(sam_ldb);
918 return ldb_msg_add_string(msg, attr_name, s);
922 * Add an unsigned int64_t (uint64_t) element to a message
924 * The issue here is that we have not yet first cast to int32_t explicitly,
925 * before we cast to an signed int to printf() into the %d or cast to a
926 * int64_t before we then cast to a long long to printf into a %lld.
928 * There are *no* unsigned integers in Active Directory LDAP, even the RID
929 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
930 * (See the schema, and the syntax definitions in schema_syntax.c).
933 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
934 const char *attr_name, uint64_t v)
936 return samdb_msg_add_int64(sam_ldb, mem_ctx, msg, attr_name, (int64_t)v);
940 add a samr_Password element to a message
942 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
943 const char *attr_name, const struct samr_Password *hash)
945 struct ldb_val val;
946 val.data = talloc_memdup(mem_ctx, hash->hash, 16);
947 if (!val.data) {
948 return ldb_oom(sam_ldb);
950 val.length = 16;
951 return ldb_msg_add_value(msg, attr_name, &val, NULL);
955 add a samr_Password array to a message
957 int samdb_msg_add_hashes(struct ldb_context *ldb,
958 TALLOC_CTX *mem_ctx, struct ldb_message *msg,
959 const char *attr_name, struct samr_Password *hashes,
960 unsigned int count)
962 struct ldb_val val;
963 unsigned int i;
964 val.data = talloc_array_size(mem_ctx, 16, count);
965 val.length = count*16;
966 if (!val.data) {
967 return ldb_oom(ldb);
969 for (i=0;i<count;i++) {
970 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
972 return ldb_msg_add_value(msg, attr_name, &val, NULL);
976 add a acct_flags element to a message
978 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
979 const char *attr_name, uint32_t v)
981 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
985 add a logon_hours element to a message
987 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
988 const char *attr_name, struct samr_LogonHours *hours)
990 struct ldb_val val;
991 val.length = hours->units_per_week / 8;
992 val.data = hours->bits;
993 return ldb_msg_add_value(msg, attr_name, &val, NULL);
997 add a parameters element to a message
999 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
1000 const char *attr_name, struct lsa_BinaryString *parameters)
1002 struct ldb_val val;
1003 val.length = parameters->length;
1004 val.data = (uint8_t *)parameters->array;
1005 return ldb_msg_add_value(msg, attr_name, &val, NULL);
1009 * Sets an unsigned int element in a message
1011 * The issue here is that we have not yet first cast to int32_t explicitly,
1012 * before we cast to an signed int to printf() into the %d or cast to a
1013 * int64_t before we then cast to a long long to printf into a %lld.
1015 * There are *no* unsigned integers in Active Directory LDAP, even the RID
1016 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
1017 * (See the schema, and the syntax definitions in schema_syntax.c).
1020 int samdb_msg_set_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
1021 struct ldb_message *msg, const char *attr_name,
1022 unsigned int v)
1024 struct ldb_message_element *el;
1026 el = ldb_msg_find_element(msg, attr_name);
1027 if (el) {
1028 el->num_values = 0;
1030 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, v);
1034 * Handle ldb_request in transaction
1036 static int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
1037 struct ldb_request *req)
1039 int ret;
1041 ret = ldb_transaction_start(sam_ldb);
1042 if (ret != LDB_SUCCESS) {
1043 return ret;
1046 ret = ldb_request(sam_ldb, req);
1047 if (ret == LDB_SUCCESS) {
1048 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1051 if (ret == LDB_SUCCESS) {
1052 return ldb_transaction_commit(sam_ldb);
1054 ldb_transaction_cancel(sam_ldb);
1056 return ret;
1060 return a default security descriptor
1062 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1064 struct security_descriptor *sd;
1066 sd = security_descriptor_initialise(mem_ctx);
1068 return sd;
1071 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1073 struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1074 struct ldb_dn *aggregate_dn;
1075 if (!schema_dn) {
1076 return NULL;
1079 aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1080 if (!aggregate_dn) {
1081 return NULL;
1083 if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1084 return NULL;
1086 return aggregate_dn;
1089 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1091 struct ldb_dn *new_dn;
1093 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1094 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1095 talloc_free(new_dn);
1096 return NULL;
1098 return new_dn;
1101 struct ldb_dn *samdb_infrastructure_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1103 struct ldb_dn *new_dn;
1105 new_dn = ldb_dn_copy(mem_ctx, ldb_get_default_basedn(sam_ctx));
1106 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Infrastructure")) {
1107 talloc_free(new_dn);
1108 return NULL;
1110 return new_dn;
1113 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1115 struct ldb_dn *new_dn;
1117 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1118 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1119 talloc_free(new_dn);
1120 return NULL;
1122 return new_dn;
1126 work out the domain sid for the current open ldb
1128 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1130 TALLOC_CTX *tmp_ctx;
1131 const struct dom_sid *domain_sid;
1132 const char *attrs[] = {
1133 "objectSid",
1134 NULL
1136 struct ldb_result *res;
1137 int ret;
1139 /* see if we have a cached copy */
1140 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1141 if (domain_sid) {
1142 return domain_sid;
1145 tmp_ctx = talloc_new(ldb);
1146 if (tmp_ctx == NULL) {
1147 goto failed;
1150 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1152 if (ret != LDB_SUCCESS) {
1153 goto failed;
1156 if (res->count != 1) {
1157 goto failed;
1160 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1161 if (domain_sid == NULL) {
1162 goto failed;
1165 /* cache the domain_sid in the ldb */
1166 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1167 goto failed;
1170 talloc_steal(ldb, domain_sid);
1171 talloc_free(tmp_ctx);
1173 return domain_sid;
1175 failed:
1176 talloc_free(tmp_ctx);
1177 return NULL;
1181 get domain sid from cache
1183 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1185 return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1188 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1190 TALLOC_CTX *tmp_ctx;
1191 struct dom_sid *dom_sid_new;
1192 struct dom_sid *dom_sid_old;
1194 /* see if we have a cached copy */
1195 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1196 "cache.domain_sid"), struct dom_sid);
1198 tmp_ctx = talloc_new(ldb);
1199 if (tmp_ctx == NULL) {
1200 goto failed;
1203 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1204 if (!dom_sid_new) {
1205 goto failed;
1208 /* cache the domain_sid in the ldb */
1209 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1210 goto failed;
1213 talloc_steal(ldb, dom_sid_new);
1214 talloc_free(tmp_ctx);
1215 talloc_free(dom_sid_old);
1217 return true;
1219 failed:
1220 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1221 talloc_free(tmp_ctx);
1222 return false;
1225 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
1227 TALLOC_CTX *tmp_ctx;
1228 struct ldb_dn *ntds_settings_dn_new;
1229 struct ldb_dn *ntds_settings_dn_old;
1231 /* see if we have a forced copy from provision */
1232 ntds_settings_dn_old = talloc_get_type(ldb_get_opaque(ldb,
1233 "forced.ntds_settings_dn"), struct ldb_dn);
1235 tmp_ctx = talloc_new(ldb);
1236 if (tmp_ctx == NULL) {
1237 goto failed;
1240 ntds_settings_dn_new = ldb_dn_copy(tmp_ctx, ntds_settings_dn_in);
1241 if (!ntds_settings_dn_new) {
1242 goto failed;
1245 /* set the DN in the ldb to avoid lookups during provision */
1246 if (ldb_set_opaque(ldb, "forced.ntds_settings_dn", ntds_settings_dn_new) != LDB_SUCCESS) {
1247 goto failed;
1250 talloc_steal(ldb, ntds_settings_dn_new);
1251 talloc_free(tmp_ctx);
1252 talloc_free(ntds_settings_dn_old);
1254 return true;
1256 failed:
1257 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1258 talloc_free(tmp_ctx);
1259 return false;
1263 work out the ntds settings dn for the current open ldb
1265 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1267 TALLOC_CTX *tmp_ctx;
1268 const char *root_attrs[] = { "dsServiceName", NULL };
1269 int ret;
1270 struct ldb_result *root_res;
1271 struct ldb_dn *settings_dn;
1273 /* see if we have a cached copy */
1274 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "forced.ntds_settings_dn");
1275 if (settings_dn) {
1276 return ldb_dn_copy(mem_ctx, settings_dn);
1279 tmp_ctx = talloc_new(mem_ctx);
1280 if (tmp_ctx == NULL) {
1281 goto failed;
1284 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1285 if (ret != LDB_SUCCESS) {
1286 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1287 ldb_errstring(ldb)));
1288 goto failed;
1291 if (root_res->count != 1) {
1292 goto failed;
1295 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1297 /* note that we do not cache the DN here, as that would mean
1298 * we could not handle server renames at runtime. Only
1299 * provision sets up forced.ntds_settings_dn */
1301 talloc_steal(mem_ctx, settings_dn);
1302 talloc_free(tmp_ctx);
1304 return settings_dn;
1306 failed:
1307 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1308 talloc_free(tmp_ctx);
1309 return NULL;
1313 work out the ntds settings invocationId for the current open ldb
1315 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1317 TALLOC_CTX *tmp_ctx;
1318 const char *attrs[] = { "invocationId", NULL };
1319 int ret;
1320 struct ldb_result *res;
1321 struct GUID *invocation_id;
1323 /* see if we have a cached copy */
1324 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1325 if (invocation_id) {
1326 SMB_ASSERT(!GUID_all_zero(invocation_id));
1327 return invocation_id;
1330 tmp_ctx = talloc_new(ldb);
1331 if (tmp_ctx == NULL) {
1332 goto failed;
1335 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1336 if (ret) {
1337 goto failed;
1340 if (res->count != 1) {
1341 goto failed;
1344 invocation_id = talloc(tmp_ctx, struct GUID);
1345 if (!invocation_id) {
1346 goto failed;
1349 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1350 if (GUID_all_zero(invocation_id)) {
1351 if (ldb_msg_find_ldb_val(res->msgs[0], "invocationId")) {
1352 DEBUG(0, ("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1353 } else {
1354 DEBUG(0, ("Failed to find parse own NTDS Settings invocationId from the ldb!\n"));
1356 goto failed;
1359 /* cache the domain_sid in the ldb */
1360 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1361 goto failed;
1364 talloc_steal(ldb, invocation_id);
1365 talloc_free(tmp_ctx);
1367 return invocation_id;
1369 failed:
1370 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1371 talloc_free(tmp_ctx);
1372 return NULL;
1375 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1377 TALLOC_CTX *tmp_ctx;
1378 struct GUID *invocation_id_new;
1379 struct GUID *invocation_id_old;
1381 /* see if we have a cached copy */
1382 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1383 "cache.invocation_id");
1385 tmp_ctx = talloc_new(ldb);
1386 if (tmp_ctx == NULL) {
1387 goto failed;
1390 invocation_id_new = talloc(tmp_ctx, struct GUID);
1391 if (!invocation_id_new) {
1392 goto failed;
1395 SMB_ASSERT(!GUID_all_zero(invocation_id_in));
1396 *invocation_id_new = *invocation_id_in;
1398 /* cache the domain_sid in the ldb */
1399 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1400 goto failed;
1403 talloc_steal(ldb, invocation_id_new);
1404 talloc_free(tmp_ctx);
1405 talloc_free(invocation_id_old);
1407 return true;
1409 failed:
1410 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1411 talloc_free(tmp_ctx);
1412 return false;
1416 work out the ntds settings objectGUID for the current open ldb
1418 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1420 TALLOC_CTX *tmp_ctx;
1421 const char *attrs[] = { "objectGUID", NULL };
1422 int ret;
1423 struct ldb_result *res;
1424 struct GUID *ntds_guid;
1426 /* see if we have a cached copy */
1427 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1428 if (ntds_guid) {
1429 return ntds_guid;
1432 tmp_ctx = talloc_new(ldb);
1433 if (tmp_ctx == NULL) {
1434 goto failed;
1437 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1438 if (ret) {
1439 goto failed;
1442 if (res->count != 1) {
1443 goto failed;
1446 ntds_guid = talloc(tmp_ctx, struct GUID);
1447 if (!ntds_guid) {
1448 goto failed;
1451 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1453 /* cache the domain_sid in the ldb */
1454 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1455 goto failed;
1458 talloc_steal(ldb, ntds_guid);
1459 talloc_free(tmp_ctx);
1461 return ntds_guid;
1463 failed:
1464 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1465 talloc_free(tmp_ctx);
1466 return NULL;
1469 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1471 TALLOC_CTX *tmp_ctx;
1472 struct GUID *ntds_guid_new;
1473 struct GUID *ntds_guid_old;
1475 /* see if we have a cached copy */
1476 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1478 tmp_ctx = talloc_new(ldb);
1479 if (tmp_ctx == NULL) {
1480 goto failed;
1483 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1484 if (!ntds_guid_new) {
1485 goto failed;
1488 *ntds_guid_new = *ntds_guid_in;
1490 /* cache the domain_sid in the ldb */
1491 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1492 goto failed;
1495 talloc_steal(ldb, ntds_guid_new);
1496 talloc_free(tmp_ctx);
1497 talloc_free(ntds_guid_old);
1499 return true;
1501 failed:
1502 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1503 talloc_free(tmp_ctx);
1504 return false;
1508 work out the server dn for the current open ldb
1510 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1512 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1513 struct ldb_dn *dn;
1514 if (!tmp_ctx) {
1515 return NULL;
1517 dn = ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb, tmp_ctx));
1518 talloc_free(tmp_ctx);
1519 return dn;
1524 work out the server dn for the current open ldb
1526 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1528 struct ldb_dn *server_dn;
1529 struct ldb_dn *servers_dn;
1530 struct ldb_dn *server_site_dn;
1532 /* TODO: there must be a saner way to do this!! */
1533 server_dn = samdb_server_dn(ldb, mem_ctx);
1534 if (!server_dn) return NULL;
1536 servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1537 talloc_free(server_dn);
1538 if (!servers_dn) return NULL;
1540 server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1541 talloc_free(servers_dn);
1543 return server_site_dn;
1547 find the site name from a computers DN record
1549 int samdb_find_site_for_computer(struct ldb_context *ldb,
1550 TALLOC_CTX *mem_ctx, struct ldb_dn *computer_dn,
1551 const char **site_name)
1553 int ret;
1554 struct ldb_dn *dn;
1555 const struct ldb_val *rdn_val;
1557 *site_name = NULL;
1559 ret = samdb_reference_dn(ldb, mem_ctx, computer_dn, "serverReferenceBL", &dn);
1560 if (ret != LDB_SUCCESS) {
1561 return ret;
1564 if (!ldb_dn_remove_child_components(dn, 2)) {
1565 talloc_free(dn);
1566 return LDB_ERR_INVALID_DN_SYNTAX;
1569 rdn_val = ldb_dn_get_rdn_val(dn);
1570 if (rdn_val == NULL) {
1571 return LDB_ERR_OPERATIONS_ERROR;
1574 (*site_name) = talloc_strndup(mem_ctx, (const char *)rdn_val->data, rdn_val->length);
1575 talloc_free(dn);
1576 if (!*site_name) {
1577 return LDB_ERR_OPERATIONS_ERROR;
1579 return LDB_SUCCESS;
1583 find the NTDS GUID from a computers DN record
1585 int samdb_find_ntdsguid_for_computer(struct ldb_context *ldb, struct ldb_dn *computer_dn,
1586 struct GUID *ntds_guid)
1588 int ret;
1589 struct ldb_dn *dn;
1591 *ntds_guid = GUID_zero();
1593 ret = samdb_reference_dn(ldb, ldb, computer_dn, "serverReferenceBL", &dn);
1594 if (ret != LDB_SUCCESS) {
1595 return ret;
1598 if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Settings")) {
1599 talloc_free(dn);
1600 return LDB_ERR_OPERATIONS_ERROR;
1603 ret = dsdb_find_guid_by_dn(ldb, dn, ntds_guid);
1604 talloc_free(dn);
1605 return ret;
1609 find a 'reference' DN that points at another object
1610 (eg. serverReference, rIDManagerReference etc)
1612 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1613 const char *attribute, struct ldb_dn **dn)
1615 const char *attrs[2];
1616 struct ldb_result *res;
1617 int ret;
1619 attrs[0] = attribute;
1620 attrs[1] = NULL;
1622 ret = dsdb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_ONE_ONLY|DSDB_SEARCH_SHOW_EXTENDED_DN, NULL);
1623 if (ret != LDB_SUCCESS) {
1624 ldb_asprintf_errstring(ldb, "Cannot find DN %s to get attribute %s for reference dn: %s",
1625 ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb));
1626 return ret;
1629 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1630 if (!*dn) {
1631 if (!ldb_msg_find_element(res->msgs[0], attribute)) {
1632 ldb_asprintf_errstring(ldb, "Cannot find attribute %s of %s to calculate reference dn", attribute,
1633 ldb_dn_get_linearized(base));
1634 } else {
1635 ldb_asprintf_errstring(ldb, "Cannot interpret attribute %s of %s as a dn", attribute,
1636 ldb_dn_get_linearized(base));
1638 talloc_free(res);
1639 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1642 talloc_free(res);
1643 return LDB_SUCCESS;
1647 find if a DN (must have GUID component!) is our ntdsDsa
1649 int samdb_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *dn, bool *is_ntdsa)
1651 NTSTATUS status;
1652 struct GUID dn_guid;
1653 const struct GUID *our_ntds_guid;
1654 status = dsdb_get_extended_dn_guid(dn, &dn_guid, "GUID");
1655 if (!NT_STATUS_IS_OK(status)) {
1656 return LDB_ERR_OPERATIONS_ERROR;
1659 our_ntds_guid = samdb_ntds_objectGUID(ldb);
1660 if (!our_ntds_guid) {
1661 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
1662 return LDB_ERR_OPERATIONS_ERROR;
1665 *is_ntdsa = GUID_equal(&dn_guid, our_ntds_guid);
1666 return LDB_SUCCESS;
1670 find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1672 int samdb_reference_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *base,
1673 const char *attribute, bool *is_ntdsa)
1675 int ret;
1676 struct ldb_dn *referenced_dn;
1677 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1678 if (tmp_ctx == NULL) {
1679 return LDB_ERR_OPERATIONS_ERROR;
1681 ret = samdb_reference_dn(ldb, tmp_ctx, base, attribute, &referenced_dn);
1682 if (ret != LDB_SUCCESS) {
1683 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb)));
1684 return ret;
1687 ret = samdb_dn_is_our_ntdsa(ldb, referenced_dn, is_ntdsa);
1689 talloc_free(tmp_ctx);
1690 return ret;
1694 find our machine account via the serverReference attribute in the
1695 server DN
1697 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1699 struct ldb_dn *server_dn;
1700 int ret;
1702 server_dn = samdb_server_dn(ldb, mem_ctx);
1703 if (server_dn == NULL) {
1704 return LDB_ERR_NO_SUCH_OBJECT;
1707 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1708 talloc_free(server_dn);
1710 return ret;
1714 find the RID Manager$ DN via the rIDManagerReference attribute in the
1715 base DN
1717 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1719 return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1720 "rIDManagerReference", dn);
1724 find the RID Set DN via the rIDSetReferences attribute in our
1725 machine account DN
1727 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1729 struct ldb_dn *server_ref_dn;
1730 int ret;
1732 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1733 if (ret != LDB_SUCCESS) {
1734 return ret;
1736 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1737 talloc_free(server_ref_dn);
1738 return ret;
1741 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1743 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1744 mem_ctx));
1746 if (val == NULL) {
1747 return NULL;
1750 return (const char *) val->data;
1754 * Finds the client site by using the client's IP address.
1755 * The "subnet_name" returns the name of the subnet if parameter != NULL
1757 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1758 const char *ip_address, char **subnet_name)
1760 const char *attrs[] = { "cn", "siteObject", NULL };
1761 struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
1762 struct ldb_result *res;
1763 const struct ldb_val *val;
1764 const char *site_name = NULL, *l_subnet_name = NULL;
1765 const char *allow_list[2] = { NULL, NULL };
1766 unsigned int i, count;
1767 int cnt, ret;
1770 * if we don't have a client ip e.g. ncalrpc
1771 * the server site is the client site
1773 if (ip_address == NULL) {
1774 return samdb_server_site_name(ldb, mem_ctx);
1777 sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1778 if (sites_container_dn == NULL) {
1779 return NULL;
1782 subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1783 if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1784 talloc_free(sites_container_dn);
1785 talloc_free(subnets_dn);
1786 return NULL;
1789 ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1790 attrs, NULL);
1791 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1792 count = 0;
1793 } else if (ret != LDB_SUCCESS) {
1794 talloc_free(sites_container_dn);
1795 talloc_free(subnets_dn);
1796 return NULL;
1797 } else {
1798 count = res->count;
1801 for (i = 0; i < count; i++) {
1802 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1803 NULL);
1805 allow_list[0] = l_subnet_name;
1807 if (socket_allow_access(mem_ctx, NULL, allow_list, "", ip_address)) {
1808 sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1809 res->msgs[i],
1810 "siteObject");
1811 if (sites_dn == NULL) {
1812 /* No reference, maybe another subnet matches */
1813 continue;
1816 /* "val" cannot be NULL here since "sites_dn" != NULL */
1817 val = ldb_dn_get_rdn_val(sites_dn);
1818 site_name = talloc_strdup(mem_ctx,
1819 (const char *) val->data);
1821 talloc_free(sites_dn);
1823 break;
1827 if (site_name == NULL) {
1828 /* This is the Windows Server fallback rule: when no subnet
1829 * exists and we have only one site available then use it (it
1830 * is for sure the same as our server site). If more sites do
1831 * exist then we don't know which one to use and set the site
1832 * name to "". */
1833 cnt = samdb_search_count(ldb, mem_ctx, sites_container_dn,
1834 "(objectClass=site)");
1835 if (cnt == 1) {
1836 site_name = samdb_server_site_name(ldb, mem_ctx);
1837 } else {
1838 site_name = talloc_strdup(mem_ctx, "");
1840 l_subnet_name = NULL;
1843 if (subnet_name != NULL) {
1844 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1847 talloc_free(sites_container_dn);
1848 talloc_free(subnets_dn);
1849 talloc_free(res);
1851 return site_name;
1855 work out if we are the PDC for the domain of the current open ldb
1857 bool samdb_is_pdc(struct ldb_context *ldb)
1859 int ret;
1860 bool is_pdc;
1862 ret = samdb_reference_dn_is_our_ntdsa(ldb, ldb_get_default_basedn(ldb), "fsmoRoleOwner",
1863 &is_pdc);
1864 if (ret != LDB_SUCCESS) {
1865 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n",
1866 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1867 ldb_errstring(ldb)));
1868 return false;
1871 return is_pdc;
1875 work out if we are a Global Catalog server for the domain of the current open ldb
1877 bool samdb_is_gc(struct ldb_context *ldb)
1879 uint32_t options;
1880 if (samdb_ntds_options(ldb, &options) != LDB_SUCCESS) {
1881 return false;
1883 return (options & DS_NTDSDSA_OPT_IS_GC) != 0;
1886 /* Find a domain object in the parents of a particular DN. */
1887 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1888 struct ldb_dn **parent_dn, const char **errstring)
1890 TALLOC_CTX *local_ctx;
1891 struct ldb_dn *sdn = dn;
1892 struct ldb_result *res = NULL;
1893 int ret = LDB_SUCCESS;
1894 const char *attrs[] = { NULL };
1896 local_ctx = talloc_new(mem_ctx);
1897 if (local_ctx == NULL) return ldb_oom(ldb);
1899 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1900 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1901 "(|(objectClass=domain)(objectClass=builtinDomain))");
1902 if (ret == LDB_SUCCESS) {
1903 if (res->count == 1) {
1904 break;
1906 } else {
1907 break;
1911 if (ret != LDB_SUCCESS) {
1912 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1913 ldb_dn_get_linearized(dn),
1914 ldb_dn_get_linearized(sdn),
1915 ldb_errstring(ldb));
1916 talloc_free(local_ctx);
1917 return ret;
1919 if (res->count != 1) {
1920 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1921 ldb_dn_get_linearized(dn));
1922 DEBUG(0,(__location__ ": %s\n", *errstring));
1923 talloc_free(local_ctx);
1924 return LDB_ERR_CONSTRAINT_VIOLATION;
1927 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1928 talloc_free(local_ctx);
1929 return ret;
1934 * Performs checks on a user password (plaintext UNIX format - attribute
1935 * "password"). The remaining parameters have to be extracted from the domain
1936 * object in the AD.
1938 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1940 enum samr_ValidationStatus samdb_check_password(const DATA_BLOB *utf8_blob,
1941 const uint32_t pwdProperties,
1942 const uint32_t minPwdLength)
1944 const char *utf8_pw = (const char *)utf8_blob->data;
1945 size_t utf8_len = strlen_m(utf8_pw);
1947 /* checks if the "minPwdLength" property is satisfied */
1948 if (minPwdLength > utf8_len) {
1949 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
1952 /* checks the password complexity */
1953 if (!(pwdProperties & DOMAIN_PASSWORD_COMPLEX)) {
1954 return SAMR_VALIDATION_STATUS_SUCCESS;
1957 if (utf8_len == 0) {
1958 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1961 if (!check_password_quality(utf8_pw)) {
1962 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1965 return SAMR_VALIDATION_STATUS_SUCCESS;
1969 * Callback for "samdb_set_password" password change
1971 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
1973 int ret;
1975 if (!ares) {
1976 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1979 if (ares->error != LDB_SUCCESS) {
1980 ret = ares->error;
1981 req->context = talloc_steal(req,
1982 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1983 talloc_free(ares);
1984 return ldb_request_done(req, ret);
1987 if (ares->type != LDB_REPLY_DONE) {
1988 talloc_free(ares);
1989 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1992 req->context = talloc_steal(req,
1993 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1994 talloc_free(ares);
1995 return ldb_request_done(req, LDB_SUCCESS);
1999 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2000 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2001 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2002 * user change or not. The "rejectReason" gives some more information if the
2003 * change failed.
2005 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2006 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2008 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2009 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
2010 const DATA_BLOB *new_password,
2011 const struct samr_Password *lmNewHash,
2012 const struct samr_Password *ntNewHash,
2013 const struct samr_Password *lmOldHash,
2014 const struct samr_Password *ntOldHash,
2015 enum samPwdChangeReason *reject_reason,
2016 struct samr_DomInfo1 **_dominfo)
2018 struct ldb_message *msg;
2019 struct ldb_message_element *el;
2020 struct ldb_request *req;
2021 struct dsdb_control_password_change_status *pwd_stat = NULL;
2022 int ret;
2023 bool hash_values = false;
2024 NTSTATUS status = NT_STATUS_OK;
2026 #define CHECK_RET(x) \
2027 if (x != LDB_SUCCESS) { \
2028 talloc_free(msg); \
2029 return NT_STATUS_NO_MEMORY; \
2032 msg = ldb_msg_new(mem_ctx);
2033 if (msg == NULL) {
2034 return NT_STATUS_NO_MEMORY;
2036 msg->dn = user_dn;
2037 if ((new_password != NULL)
2038 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
2039 /* we have the password as plaintext UTF16 */
2040 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
2041 new_password, NULL));
2042 el = ldb_msg_find_element(msg, "clearTextPassword");
2043 el->flags = LDB_FLAG_MOD_REPLACE;
2044 } else if ((new_password == NULL)
2045 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2046 /* we have a password as LM and/or NT hash */
2047 if (lmNewHash != NULL) {
2048 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2049 "dBCSPwd", lmNewHash));
2050 el = ldb_msg_find_element(msg, "dBCSPwd");
2051 el->flags = LDB_FLAG_MOD_REPLACE;
2053 if (ntNewHash != NULL) {
2054 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2055 "unicodePwd", ntNewHash));
2056 el = ldb_msg_find_element(msg, "unicodePwd");
2057 el->flags = LDB_FLAG_MOD_REPLACE;
2059 hash_values = true;
2060 } else {
2061 /* the password wasn't specified correctly */
2062 talloc_free(msg);
2063 return NT_STATUS_INVALID_PARAMETER;
2066 /* build modify request */
2067 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2068 samdb_set_password_callback, NULL);
2069 if (ret != LDB_SUCCESS) {
2070 talloc_free(msg);
2071 return NT_STATUS_NO_MEMORY;
2074 /* A password change operation */
2075 if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2076 struct dsdb_control_password_change *change;
2078 change = talloc(req, struct dsdb_control_password_change);
2079 if (change == NULL) {
2080 talloc_free(req);
2081 talloc_free(msg);
2082 return NT_STATUS_NO_MEMORY;
2085 change->old_nt_pwd_hash = ntOldHash;
2086 change->old_lm_pwd_hash = lmOldHash;
2088 ret = ldb_request_add_control(req,
2089 DSDB_CONTROL_PASSWORD_CHANGE_OID,
2090 true, change);
2091 if (ret != LDB_SUCCESS) {
2092 talloc_free(req);
2093 talloc_free(msg);
2094 return NT_STATUS_NO_MEMORY;
2097 if (hash_values) {
2098 ret = ldb_request_add_control(req,
2099 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2100 true, NULL);
2101 if (ret != LDB_SUCCESS) {
2102 talloc_free(req);
2103 talloc_free(msg);
2104 return NT_STATUS_NO_MEMORY;
2107 ret = ldb_request_add_control(req,
2108 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2109 true, NULL);
2110 if (ret != LDB_SUCCESS) {
2111 talloc_free(req);
2112 talloc_free(msg);
2113 return NT_STATUS_NO_MEMORY;
2116 ret = dsdb_autotransaction_request(ldb, req);
2118 if (req->context != NULL) {
2119 pwd_stat = talloc_steal(mem_ctx,
2120 ((struct ldb_control *)req->context)->data);
2123 talloc_free(req);
2124 talloc_free(msg);
2126 /* Sets the domain info (if requested) */
2127 if (_dominfo != NULL) {
2128 struct samr_DomInfo1 *dominfo;
2130 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2131 if (dominfo == NULL) {
2132 return NT_STATUS_NO_MEMORY;
2135 if (pwd_stat != NULL) {
2136 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2137 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2138 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2139 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2140 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2143 *_dominfo = dominfo;
2146 if (reject_reason != NULL) {
2147 if (pwd_stat != NULL) {
2148 *reject_reason = pwd_stat->reject_reason;
2149 } else {
2150 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2154 if (pwd_stat != NULL) {
2155 talloc_free(pwd_stat);
2158 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2159 const char *errmsg = ldb_errstring(ldb);
2160 char *endptr = NULL;
2161 WERROR werr = WERR_GENERAL_FAILURE;
2162 status = NT_STATUS_UNSUCCESSFUL;
2163 if (errmsg != NULL) {
2164 werr = W_ERROR(strtol(errmsg, &endptr, 16));
2166 if (endptr != errmsg) {
2167 if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2168 status = NT_STATUS_WRONG_PASSWORD;
2170 if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2171 status = NT_STATUS_PASSWORD_RESTRICTION;
2174 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2175 /* don't let the caller know if an account doesn't exist */
2176 status = NT_STATUS_WRONG_PASSWORD;
2177 } else if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
2178 status = NT_STATUS_ACCESS_DENIED;
2179 } else if (ret != LDB_SUCCESS) {
2180 DEBUG(1, ("Failed to set password on %s: %s\n",
2181 ldb_dn_get_linearized(msg->dn),
2182 ldb_errstring(ldb)));
2183 status = NT_STATUS_UNSUCCESSFUL;
2186 return status;
2190 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2191 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2192 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2193 * user change or not. The "rejectReason" gives some more information if the
2194 * change failed.
2196 * This wrapper function for "samdb_set_password" takes a SID as input rather
2197 * than a user DN.
2199 * This call encapsulates a new LDB transaction for changing the password;
2200 * therefore the user hasn't to start a new one.
2202 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2203 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2204 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2205 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2207 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2208 const struct dom_sid *user_sid,
2209 const DATA_BLOB *new_password,
2210 const struct samr_Password *lmNewHash,
2211 const struct samr_Password *ntNewHash,
2212 const struct samr_Password *lmOldHash,
2213 const struct samr_Password *ntOldHash,
2214 enum samPwdChangeReason *reject_reason,
2215 struct samr_DomInfo1 **_dominfo)
2217 NTSTATUS nt_status;
2218 struct ldb_dn *user_dn;
2219 int ret;
2221 ret = ldb_transaction_start(ldb);
2222 if (ret != LDB_SUCCESS) {
2223 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2224 return NT_STATUS_TRANSACTION_ABORTED;
2227 user_dn = samdb_search_dn(ldb, mem_ctx, NULL,
2228 "(&(objectSid=%s)(objectClass=user))",
2229 ldap_encode_ndr_dom_sid(mem_ctx, user_sid));
2230 if (!user_dn) {
2231 ldb_transaction_cancel(ldb);
2232 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2233 dom_sid_string(mem_ctx, user_sid)));
2234 return NT_STATUS_NO_SUCH_USER;
2237 nt_status = samdb_set_password(ldb, mem_ctx,
2238 user_dn, NULL,
2239 new_password,
2240 lmNewHash, ntNewHash,
2241 lmOldHash, ntOldHash,
2242 reject_reason, _dominfo);
2243 if (!NT_STATUS_IS_OK(nt_status)) {
2244 ldb_transaction_cancel(ldb);
2245 talloc_free(user_dn);
2246 return nt_status;
2249 ret = ldb_transaction_commit(ldb);
2250 if (ret != LDB_SUCCESS) {
2251 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2252 ldb_dn_get_linearized(user_dn),
2253 ldb_errstring(ldb)));
2254 talloc_free(user_dn);
2255 return NT_STATUS_TRANSACTION_ABORTED;
2258 talloc_free(user_dn);
2259 return NT_STATUS_OK;
2263 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2264 struct dom_sid *sid, struct ldb_dn **ret_dn)
2266 struct ldb_message *msg;
2267 struct ldb_dn *basedn;
2268 char *sidstr;
2269 int ret;
2271 sidstr = dom_sid_string(mem_ctx, sid);
2272 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2274 /* We might have to create a ForeignSecurityPrincipal, even if this user
2275 * is in our own domain */
2277 msg = ldb_msg_new(sidstr);
2278 if (msg == NULL) {
2279 talloc_free(sidstr);
2280 return NT_STATUS_NO_MEMORY;
2283 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2284 ldb_get_default_basedn(sam_ctx),
2285 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2286 &basedn);
2287 if (ret != LDB_SUCCESS) {
2288 DEBUG(0, ("Failed to find DN for "
2289 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2290 talloc_free(sidstr);
2291 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2294 /* add core elements to the ldb_message for the alias */
2295 msg->dn = basedn;
2296 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2297 talloc_free(sidstr);
2298 return NT_STATUS_NO_MEMORY;
2301 ret = ldb_msg_add_string(msg, "objectClass",
2302 "foreignSecurityPrincipal");
2303 if (ret != LDB_SUCCESS) {
2304 talloc_free(sidstr);
2305 return NT_STATUS_NO_MEMORY;
2308 /* create the alias */
2309 ret = ldb_add(sam_ctx, msg);
2310 if (ret != LDB_SUCCESS) {
2311 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2312 "record %s: %s\n",
2313 ldb_dn_get_linearized(msg->dn),
2314 ldb_errstring(sam_ctx)));
2315 talloc_free(sidstr);
2316 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2319 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2320 talloc_free(sidstr);
2322 return NT_STATUS_OK;
2327 Find the DN of a domain, assuming it to be a dotted.dns name
2330 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2332 unsigned int i;
2333 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2334 const char *binary_encoded;
2335 const char * const *split_realm;
2336 struct ldb_dn *dn;
2338 if (!tmp_ctx) {
2339 return NULL;
2342 split_realm = (const char * const *)str_list_make(tmp_ctx, dns_domain, ".");
2343 if (!split_realm) {
2344 talloc_free(tmp_ctx);
2345 return NULL;
2347 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2348 for (i=0; split_realm[i]; i++) {
2349 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2350 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2351 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2352 binary_encoded, ldb_dn_get_linearized(dn)));
2353 talloc_free(tmp_ctx);
2354 return NULL;
2357 if (!ldb_dn_validate(dn)) {
2358 DEBUG(2, ("Failed to validated DN %s\n",
2359 ldb_dn_get_linearized(dn)));
2360 talloc_free(tmp_ctx);
2361 return NULL;
2363 talloc_free(tmp_ctx);
2364 return dn;
2369 Find the DNS equivalent of a DN, in dotted DNS form
2371 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2373 int i, num_components = ldb_dn_get_comp_num(dn);
2374 char *dns_name = talloc_strdup(mem_ctx, "");
2375 if (dns_name == NULL) {
2376 return NULL;
2379 for (i=0; i<num_components; i++) {
2380 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2381 char *s;
2382 if (v == NULL) {
2383 talloc_free(dns_name);
2384 return NULL;
2386 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2387 (int)v->length, (int)v->length, (char *)v->data);
2388 if (s == NULL) {
2389 talloc_free(dns_name);
2390 return NULL;
2392 dns_name = s;
2395 /* remove the last '.' */
2396 if (dns_name[0] != 0) {
2397 dns_name[strlen(dns_name)-1] = 0;
2400 return dns_name;
2404 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2405 name is based on the forest DNS name
2407 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
2408 TALLOC_CTX *mem_ctx,
2409 const struct GUID *ntds_guid)
2411 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2412 const char *guid_str;
2413 struct ldb_dn *forest_dn;
2414 const char *dnsforest;
2415 char *ret;
2417 guid_str = GUID_string(tmp_ctx, ntds_guid);
2418 if (guid_str == NULL) {
2419 talloc_free(tmp_ctx);
2420 return NULL;
2422 forest_dn = ldb_get_root_basedn(samdb);
2423 if (forest_dn == NULL) {
2424 talloc_free(tmp_ctx);
2425 return NULL;
2427 dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
2428 if (dnsforest == NULL) {
2429 talloc_free(tmp_ctx);
2430 return NULL;
2432 ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
2433 talloc_free(tmp_ctx);
2434 return ret;
2439 Find the DN of a domain, be it the netbios or DNS name
2441 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2442 const char *domain_name)
2444 const char * const domain_ref_attrs[] = {
2445 "ncName", NULL
2447 const char * const domain_ref2_attrs[] = {
2448 NULL
2450 struct ldb_result *res_domain_ref;
2451 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2452 /* find the domain's DN */
2453 int ret_domain = ldb_search(ldb, mem_ctx,
2454 &res_domain_ref,
2455 samdb_partitions_dn(ldb, mem_ctx),
2456 LDB_SCOPE_ONELEVEL,
2457 domain_ref_attrs,
2458 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2459 escaped_domain);
2460 if (ret_domain != LDB_SUCCESS) {
2461 return NULL;
2464 if (res_domain_ref->count == 0) {
2465 ret_domain = ldb_search(ldb, mem_ctx,
2466 &res_domain_ref,
2467 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2468 LDB_SCOPE_BASE,
2469 domain_ref2_attrs,
2470 "(objectclass=domain)");
2471 if (ret_domain != LDB_SUCCESS) {
2472 return NULL;
2475 if (res_domain_ref->count == 1) {
2476 return res_domain_ref->msgs[0]->dn;
2478 return NULL;
2481 if (res_domain_ref->count > 1) {
2482 DEBUG(0,("Found %d records matching domain [%s]\n",
2483 ret_domain, domain_name));
2484 return NULL;
2487 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
2493 use a GUID to find a DN
2495 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
2496 TALLOC_CTX *mem_ctx,
2497 const struct GUID *guid,
2498 uint32_t dsdb_flags,
2499 struct ldb_dn **dn)
2501 int ret;
2502 struct ldb_result *res;
2503 const char *attrs[] = { NULL };
2504 char *guid_str = GUID_string(mem_ctx, guid);
2506 if (!guid_str) {
2507 return ldb_operr(ldb);
2510 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2511 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2512 DSDB_SEARCH_SHOW_EXTENDED_DN |
2513 DSDB_SEARCH_ONE_ONLY | dsdb_flags,
2514 "objectGUID=%s", guid_str);
2515 talloc_free(guid_str);
2516 if (ret != LDB_SUCCESS) {
2517 return ret;
2520 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2521 talloc_free(res);
2523 return LDB_SUCCESS;
2527 use a DN to find a GUID with a given attribute name
2529 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
2530 struct ldb_dn *dn, const char *attribute,
2531 struct GUID *guid)
2533 int ret;
2534 struct ldb_result *res;
2535 const char *attrs[2];
2536 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2538 attrs[0] = attribute;
2539 attrs[1] = NULL;
2541 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2542 DSDB_SEARCH_SHOW_DELETED |
2543 DSDB_SEARCH_SHOW_RECYCLED);
2544 if (ret != LDB_SUCCESS) {
2545 talloc_free(tmp_ctx);
2546 return ret;
2548 if (res->count < 1) {
2549 talloc_free(tmp_ctx);
2550 return LDB_ERR_NO_SUCH_OBJECT;
2552 *guid = samdb_result_guid(res->msgs[0], attribute);
2553 talloc_free(tmp_ctx);
2554 return LDB_SUCCESS;
2558 use a DN to find a GUID
2560 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
2561 struct ldb_dn *dn, struct GUID *guid)
2563 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
2569 adds the given GUID to the given ldb_message. This value is added
2570 for the given attr_name (may be either "objectGUID" or "parentGUID").
2572 int dsdb_msg_add_guid(struct ldb_message *msg,
2573 struct GUID *guid,
2574 const char *attr_name)
2576 int ret;
2577 struct ldb_val v;
2578 NTSTATUS status;
2579 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
2581 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
2582 if (!NT_STATUS_IS_OK(status)) {
2583 ret = LDB_ERR_OPERATIONS_ERROR;
2584 goto done;
2587 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
2588 if (ret != LDB_SUCCESS) {
2589 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
2590 attr_name));
2591 goto done;
2594 ret = LDB_SUCCESS;
2596 done:
2597 talloc_free(tmp_ctx);
2598 return ret;
2604 use a DN to find a SID
2606 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
2607 struct ldb_dn *dn, struct dom_sid *sid)
2609 int ret;
2610 struct ldb_result *res;
2611 const char *attrs[] = { "objectSid", NULL };
2612 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2613 struct dom_sid *s;
2615 ZERO_STRUCTP(sid);
2617 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2618 DSDB_SEARCH_SHOW_DELETED |
2619 DSDB_SEARCH_SHOW_RECYCLED);
2620 if (ret != LDB_SUCCESS) {
2621 talloc_free(tmp_ctx);
2622 return ret;
2624 if (res->count < 1) {
2625 talloc_free(tmp_ctx);
2626 return LDB_ERR_NO_SUCH_OBJECT;
2628 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
2629 if (s == NULL) {
2630 talloc_free(tmp_ctx);
2631 return LDB_ERR_NO_SUCH_OBJECT;
2633 *sid = *s;
2634 talloc_free(tmp_ctx);
2635 return LDB_SUCCESS;
2639 use a SID to find a DN
2641 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
2642 TALLOC_CTX *mem_ctx,
2643 struct dom_sid *sid, struct ldb_dn **dn)
2645 int ret;
2646 struct ldb_result *res;
2647 const char *attrs[] = { NULL };
2648 char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
2650 if (!sid_str) {
2651 return ldb_operr(ldb);
2654 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2655 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2656 DSDB_SEARCH_SHOW_EXTENDED_DN |
2657 DSDB_SEARCH_ONE_ONLY,
2658 "objectSid=%s", sid_str);
2659 talloc_free(sid_str);
2660 if (ret != LDB_SUCCESS) {
2661 return ret;
2664 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2665 talloc_free(res);
2667 return LDB_SUCCESS;
2671 load a repsFromTo blob list for a given partition GUID
2672 attr must be "repsFrom" or "repsTo"
2674 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2675 const char *attr, struct repsFromToBlob **r, uint32_t *count)
2677 const char *attrs[] = { attr, NULL };
2678 struct ldb_result *res = NULL;
2679 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2680 unsigned int i;
2681 struct ldb_message_element *el;
2682 int ret;
2684 *r = NULL;
2685 *count = 0;
2687 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
2688 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2689 /* partition hasn't been replicated yet */
2690 return WERR_OK;
2692 if (ret != LDB_SUCCESS) {
2693 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
2694 talloc_free(tmp_ctx);
2695 return WERR_DS_DRA_INTERNAL_ERROR;
2698 el = ldb_msg_find_element(res->msgs[0], attr);
2699 if (el == NULL) {
2700 /* it's OK to be empty */
2701 talloc_free(tmp_ctx);
2702 return WERR_OK;
2705 *count = el->num_values;
2706 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
2707 if (*r == NULL) {
2708 talloc_free(tmp_ctx);
2709 return WERR_DS_DRA_INTERNAL_ERROR;
2712 for (i=0; i<(*count); i++) {
2713 enum ndr_err_code ndr_err;
2714 ndr_err = ndr_pull_struct_blob(&el->values[i],
2715 mem_ctx,
2716 &(*r)[i],
2717 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
2718 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2719 talloc_free(tmp_ctx);
2720 return WERR_DS_DRA_INTERNAL_ERROR;
2724 talloc_free(tmp_ctx);
2726 return WERR_OK;
2730 save the repsFromTo blob list for a given partition GUID
2731 attr must be "repsFrom" or "repsTo"
2733 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2734 const char *attr, struct repsFromToBlob *r, uint32_t count)
2736 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2737 struct ldb_message *msg;
2738 struct ldb_message_element *el;
2739 unsigned int i;
2741 msg = ldb_msg_new(tmp_ctx);
2742 msg->dn = dn;
2743 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
2744 goto failed;
2747 el->values = talloc_array(msg, struct ldb_val, count);
2748 if (!el->values) {
2749 goto failed;
2752 for (i=0; i<count; i++) {
2753 struct ldb_val v;
2754 enum ndr_err_code ndr_err;
2756 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
2757 &r[i],
2758 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
2759 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2760 goto failed;
2763 el->num_values++;
2764 el->values[i] = v;
2767 if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
2768 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
2769 goto failed;
2772 talloc_free(tmp_ctx);
2774 return WERR_OK;
2776 failed:
2777 talloc_free(tmp_ctx);
2778 return WERR_DS_DRA_INTERNAL_ERROR;
2783 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2784 object for a partition
2786 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
2787 uint64_t *uSN, uint64_t *urgent_uSN)
2789 struct ldb_request *req;
2790 int ret;
2791 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2792 struct dsdb_control_current_partition *p_ctrl;
2793 struct ldb_result *res;
2795 res = talloc_zero(tmp_ctx, struct ldb_result);
2796 if (!res) {
2797 talloc_free(tmp_ctx);
2798 return ldb_oom(ldb);
2801 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
2802 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
2803 LDB_SCOPE_BASE,
2804 NULL, NULL,
2805 NULL,
2806 res, ldb_search_default_callback,
2807 NULL);
2808 if (ret != LDB_SUCCESS) {
2809 talloc_free(tmp_ctx);
2810 return ret;
2813 p_ctrl = talloc(req, struct dsdb_control_current_partition);
2814 if (p_ctrl == NULL) {
2815 talloc_free(tmp_ctx);
2816 return ldb_oom(ldb);
2818 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2819 p_ctrl->dn = dn;
2821 ret = ldb_request_add_control(req,
2822 DSDB_CONTROL_CURRENT_PARTITION_OID,
2823 false, p_ctrl);
2824 if (ret != LDB_SUCCESS) {
2825 talloc_free(tmp_ctx);
2826 return ret;
2829 /* Run the new request */
2830 ret = ldb_request(ldb, req);
2832 if (ret == LDB_SUCCESS) {
2833 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2836 if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
2837 /* it hasn't been created yet, which means
2838 an implicit value of zero */
2839 *uSN = 0;
2840 talloc_free(tmp_ctx);
2841 return LDB_SUCCESS;
2844 if (ret != LDB_SUCCESS) {
2845 talloc_free(tmp_ctx);
2846 return ret;
2849 if (res->count < 1) {
2850 *uSN = 0;
2851 if (urgent_uSN) {
2852 *urgent_uSN = 0;
2854 } else {
2855 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
2856 if (urgent_uSN) {
2857 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
2861 talloc_free(tmp_ctx);
2863 return LDB_SUCCESS;
2866 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
2867 const struct drsuapi_DsReplicaCursor2 *c2)
2869 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2872 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
2873 const struct drsuapi_DsReplicaCursor *c2)
2875 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2880 see if a computer identified by its invocationId is a RODC
2882 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
2884 /* 1) find the DN for this servers NTDSDSA object
2885 2) search for the msDS-isRODC attribute
2886 3) if not present then not a RODC
2887 4) if present and TRUE then is a RODC
2889 struct ldb_dn *config_dn;
2890 const char *attrs[] = { "msDS-isRODC", NULL };
2891 int ret;
2892 struct ldb_result *res;
2893 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
2895 config_dn = ldb_get_config_basedn(sam_ctx);
2896 if (!config_dn) {
2897 talloc_free(tmp_ctx);
2898 return ldb_operr(sam_ctx);
2901 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
2902 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
2904 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2905 *is_rodc = false;
2906 talloc_free(tmp_ctx);
2907 return LDB_SUCCESS;
2910 if (ret != LDB_SUCCESS) {
2911 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
2912 GUID_string(tmp_ctx, objectGUID)));
2913 *is_rodc = false;
2914 talloc_free(tmp_ctx);
2915 return ret;
2918 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
2919 *is_rodc = (ret == 1);
2921 talloc_free(tmp_ctx);
2922 return LDB_SUCCESS;
2927 see if we are a RODC
2929 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
2931 const struct GUID *objectGUID;
2932 int ret;
2933 bool *cached;
2935 /* see if we have a cached copy */
2936 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
2937 if (cached) {
2938 *am_rodc = *cached;
2939 return LDB_SUCCESS;
2942 objectGUID = samdb_ntds_objectGUID(sam_ctx);
2943 if (!objectGUID) {
2944 return ldb_operr(sam_ctx);
2947 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
2948 if (ret != LDB_SUCCESS) {
2949 return ret;
2952 cached = talloc(sam_ctx, bool);
2953 if (cached == NULL) {
2954 return ldb_oom(sam_ctx);
2956 *cached = *am_rodc;
2958 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
2959 if (ret != LDB_SUCCESS) {
2960 talloc_free(cached);
2961 return ldb_operr(sam_ctx);
2964 return LDB_SUCCESS;
2967 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
2969 TALLOC_CTX *tmp_ctx;
2970 bool *cached;
2972 tmp_ctx = talloc_new(ldb);
2973 if (tmp_ctx == NULL) {
2974 goto failed;
2977 cached = talloc(tmp_ctx, bool);
2978 if (!cached) {
2979 goto failed;
2982 *cached = am_rodc;
2983 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
2984 goto failed;
2987 talloc_steal(ldb, cached);
2988 talloc_free(tmp_ctx);
2989 return true;
2991 failed:
2992 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
2993 talloc_free(tmp_ctx);
2994 return false;
2999 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
3000 * flags are DS_NTDSSETTINGS_OPT_*
3002 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
3003 uint32_t *options)
3005 int rc;
3006 TALLOC_CTX *tmp_ctx;
3007 struct ldb_result *res;
3008 struct ldb_dn *site_dn;
3009 const char *attrs[] = { "options", NULL };
3011 tmp_ctx = talloc_new(ldb_ctx);
3012 if (tmp_ctx == NULL)
3013 goto failed;
3015 /* Retrieve the site dn for the ldb that we
3016 * have open. This is our local site.
3018 site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
3019 if (site_dn == NULL)
3020 goto failed;
3022 /* Perform a one level (child) search from the local
3023 * site distinguided name. We're looking for the
3024 * "options" attribute within the nTDSSiteSettings
3025 * object
3027 rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
3028 LDB_SCOPE_ONELEVEL, attrs,
3029 "objectClass=nTDSSiteSettings");
3031 if (rc != LDB_SUCCESS || res->count != 1)
3032 goto failed;
3034 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3036 talloc_free(tmp_ctx);
3038 return LDB_SUCCESS;
3040 failed:
3041 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3042 talloc_free(tmp_ctx);
3043 return LDB_ERR_NO_SUCH_OBJECT;
3047 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3049 flags are DS_NTDS_OPTION_*
3051 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
3053 TALLOC_CTX *tmp_ctx;
3054 const char *attrs[] = { "options", NULL };
3055 int ret;
3056 struct ldb_result *res;
3058 tmp_ctx = talloc_new(ldb);
3059 if (tmp_ctx == NULL) {
3060 goto failed;
3063 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3064 if (ret != LDB_SUCCESS) {
3065 goto failed;
3068 if (res->count != 1) {
3069 goto failed;
3072 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3074 talloc_free(tmp_ctx);
3076 return LDB_SUCCESS;
3078 failed:
3079 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3080 talloc_free(tmp_ctx);
3081 return LDB_ERR_NO_SUCH_OBJECT;
3084 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3086 const char *attrs[] = { "objectCategory", NULL };
3087 int ret;
3088 struct ldb_result *res;
3090 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3091 if (ret != LDB_SUCCESS) {
3092 goto failed;
3095 if (res->count != 1) {
3096 goto failed;
3099 return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3101 failed:
3102 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3103 return NULL;
3107 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3108 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3110 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3112 char **tokens, *ret;
3113 size_t i;
3115 tokens = str_list_make(mem_ctx, cn, " -_");
3116 if (tokens == NULL)
3117 return NULL;
3119 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3120 tokens[0][0] = tolower(tokens[0][0]);
3121 for (i = 1; i < str_list_length((const char * const *)tokens); i++)
3122 tokens[i][0] = toupper(tokens[i][0]);
3124 ret = talloc_strdup(mem_ctx, tokens[0]);
3125 for (i = 1; i < str_list_length((const char * const *)tokens); i++)
3126 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3128 talloc_free(tokens);
3130 return ret;
3134 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3136 int dsdb_functional_level(struct ldb_context *ldb)
3138 int *domainFunctionality =
3139 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3140 if (!domainFunctionality) {
3141 /* this is expected during initial provision */
3142 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3143 return DS_DOMAIN_FUNCTION_2000;
3145 return *domainFunctionality;
3149 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3151 int dsdb_forest_functional_level(struct ldb_context *ldb)
3153 int *forestFunctionality =
3154 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3155 if (!forestFunctionality) {
3156 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3157 return DS_DOMAIN_FUNCTION_2000;
3159 return *forestFunctionality;
3163 set a GUID in an extended DN structure
3165 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3167 struct ldb_val v;
3168 NTSTATUS status;
3169 int ret;
3171 status = GUID_to_ndr_blob(guid, dn, &v);
3172 if (!NT_STATUS_IS_OK(status)) {
3173 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3176 ret = ldb_dn_set_extended_component(dn, component_name, &v);
3177 data_blob_free(&v);
3178 return ret;
3182 return a GUID from a extended DN structure
3184 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3186 const struct ldb_val *v;
3188 v = ldb_dn_get_extended_component(dn, component_name);
3189 if (v == NULL) {
3190 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3193 return GUID_from_ndr_blob(v, guid);
3197 return a uint64_t from a extended DN structure
3199 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3201 const struct ldb_val *v;
3202 char *s;
3204 v = ldb_dn_get_extended_component(dn, component_name);
3205 if (v == NULL) {
3206 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3208 s = talloc_strndup(dn, (const char *)v->data, v->length);
3209 NT_STATUS_HAVE_NO_MEMORY(s);
3211 *val = strtoull(s, NULL, 0);
3213 talloc_free(s);
3214 return NT_STATUS_OK;
3218 return a NTTIME from a extended DN structure
3220 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3222 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3226 return a uint32_t from a extended DN structure
3228 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3230 const struct ldb_val *v;
3231 char *s;
3233 v = ldb_dn_get_extended_component(dn, component_name);
3234 if (v == NULL) {
3235 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3238 s = talloc_strndup(dn, (const char *)v->data, v->length);
3239 NT_STATUS_HAVE_NO_MEMORY(s);
3241 *val = strtoul(s, NULL, 0);
3243 talloc_free(s);
3244 return NT_STATUS_OK;
3248 return a dom_sid from a extended DN structure
3250 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3252 const struct ldb_val *sid_blob;
3253 struct TALLOC_CTX *tmp_ctx;
3254 enum ndr_err_code ndr_err;
3256 sid_blob = ldb_dn_get_extended_component(dn, component_name);
3257 if (!sid_blob) {
3258 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3261 tmp_ctx = talloc_new(NULL);
3263 ndr_err = ndr_pull_struct_blob_all(sid_blob, tmp_ctx, sid,
3264 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3265 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3266 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3267 talloc_free(tmp_ctx);
3268 return status;
3271 talloc_free(tmp_ctx);
3272 return NT_STATUS_OK;
3277 return RMD_FLAGS directly from a ldb_dn
3278 returns 0 if not found
3280 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3282 const struct ldb_val *v;
3283 char buf[32];
3284 v = ldb_dn_get_extended_component(dn, "RMD_FLAGS");
3285 if (!v || v->length > sizeof(buf)-1) return 0;
3286 strncpy(buf, (const char *)v->data, v->length);
3287 buf[v->length] = 0;
3288 return strtoul(buf, NULL, 10);
3292 return RMD_FLAGS directly from a ldb_val for a DN
3293 returns 0 if RMD_FLAGS is not found
3295 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3297 const char *p;
3298 uint32_t flags;
3299 char *end;
3301 if (val->length < 13) {
3302 return 0;
3304 p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3305 if (!p) {
3306 return 0;
3308 flags = strtoul(p+11, &end, 10);
3309 if (!end || *end != '>') {
3310 /* it must end in a > */
3311 return 0;
3313 return flags;
3317 return true if a ldb_val containing a DN in storage form is deleted
3319 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3321 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3325 return true if a ldb_val containing a DN in storage form is
3326 in the upgraded w2k3 linked attribute format
3328 bool dsdb_dn_is_upgraded_link_val(struct ldb_val *val)
3330 return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
3334 return a DN for a wellknown GUID
3336 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3337 struct ldb_dn *nc_root, const char *wk_guid,
3338 struct ldb_dn **wkguid_dn)
3340 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3341 const char *attrs[] = { NULL };
3342 int ret;
3343 struct ldb_dn *dn;
3344 struct ldb_result *res;
3346 /* construct the magic WKGUID DN */
3347 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3348 wk_guid, ldb_dn_get_linearized(nc_root));
3349 if (!wkguid_dn) {
3350 talloc_free(tmp_ctx);
3351 return ldb_operr(samdb);
3354 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
3355 DSDB_SEARCH_SHOW_DELETED |
3356 DSDB_SEARCH_SHOW_RECYCLED);
3357 if (ret != LDB_SUCCESS) {
3358 talloc_free(tmp_ctx);
3359 return ret;
3362 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
3363 talloc_free(tmp_ctx);
3364 return LDB_SUCCESS;
3368 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
3370 return ldb_dn_compare(*dn1, *dn2);
3374 find a NC root given a DN within the NC
3376 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3377 struct ldb_dn **nc_root)
3379 const char *root_attrs[] = { "namingContexts", NULL };
3380 TALLOC_CTX *tmp_ctx;
3381 int ret;
3382 struct ldb_message_element *el;
3383 struct ldb_result *root_res;
3384 unsigned int i;
3385 struct ldb_dn **nc_dns;
3387 tmp_ctx = talloc_new(samdb);
3388 if (tmp_ctx == NULL) {
3389 return ldb_oom(samdb);
3392 ret = ldb_search(samdb, tmp_ctx, &root_res,
3393 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
3394 if (ret != LDB_SUCCESS) {
3395 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
3396 talloc_free(tmp_ctx);
3397 return ret;
3400 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
3401 if ((el == NULL) || (el->num_values < 3)) {
3402 struct ldb_message *tmp_msg;
3404 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list."));
3406 /* This generates a temporary list of NCs in order to let the
3407 * provisioning work. */
3408 tmp_msg = ldb_msg_new(tmp_ctx);
3409 if (tmp_msg == NULL) {
3410 talloc_free(tmp_ctx);
3411 return ldb_oom(samdb);
3413 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3414 ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
3415 if (ret != LDB_SUCCESS) {
3416 talloc_free(tmp_ctx);
3417 return ret;
3419 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3420 ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
3421 if (ret != LDB_SUCCESS) {
3422 talloc_free(tmp_ctx);
3423 return ret;
3425 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3426 ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
3427 if (ret != LDB_SUCCESS) {
3428 talloc_free(tmp_ctx);
3429 return ret;
3431 el = &tmp_msg->elements[0];
3434 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
3435 if (!nc_dns) {
3436 talloc_free(tmp_ctx);
3437 return ldb_oom(samdb);
3440 for (i=0; i<el->num_values; i++) {
3441 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
3442 if (nc_dns[i] == NULL) {
3443 talloc_free(tmp_ctx);
3444 return ldb_operr(samdb);
3448 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
3450 for (i=0; i<el->num_values; i++) {
3451 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
3452 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
3453 talloc_free(tmp_ctx);
3454 return LDB_SUCCESS;
3458 talloc_free(tmp_ctx);
3459 return LDB_ERR_NO_SUCH_OBJECT;
3464 find the deleted objects DN for any object, by looking for the NC
3465 root, then looking up the wellknown GUID
3467 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
3468 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
3469 struct ldb_dn **do_dn)
3471 struct ldb_dn *nc_root;
3472 int ret;
3474 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
3475 if (ret != LDB_SUCCESS) {
3476 return ret;
3479 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
3480 talloc_free(nc_root);
3481 return ret;
3485 return the tombstoneLifetime, in days
3487 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
3489 struct ldb_dn *dn;
3490 dn = ldb_get_config_basedn(ldb);
3491 if (!dn) {
3492 return LDB_ERR_NO_SUCH_OBJECT;
3494 dn = ldb_dn_copy(ldb, dn);
3495 if (!dn) {
3496 return ldb_operr(ldb);
3498 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3499 be a wellknown GUID for this */
3500 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3501 talloc_free(dn);
3502 return ldb_operr(ldb);
3505 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
3506 talloc_free(dn);
3507 return LDB_SUCCESS;
3511 compare a ldb_val to a string case insensitively
3513 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
3515 size_t len = strlen(s);
3516 int ret;
3517 if (len > v->length) return 1;
3518 ret = strncasecmp(s, (const char *)v->data, v->length);
3519 if (ret != 0) return ret;
3520 if (v->length > len && v->data[len] != 0) {
3521 return -1;
3523 return 0;
3528 load the UDV for a partition in v2 format
3529 The list is returned sorted, and with our local cursor added
3531 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3532 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
3534 static const char *attrs[] = { "replUpToDateVector", NULL };
3535 struct ldb_result *r;
3536 const struct ldb_val *ouv_value;
3537 unsigned int i;
3538 int ret;
3539 uint64_t highest_usn = 0;
3540 const struct GUID *our_invocation_id;
3541 static const struct timeval tv1970;
3542 NTTIME nt1970 = timeval_to_nttime(&tv1970);
3544 ret = ldb_search(samdb, mem_ctx, &r, dn, LDB_SCOPE_BASE, attrs, NULL);
3545 if (ret != LDB_SUCCESS) {
3546 return ret;
3549 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
3550 if (ouv_value) {
3551 enum ndr_err_code ndr_err;
3552 struct replUpToDateVectorBlob ouv;
3554 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
3555 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
3556 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3557 talloc_free(r);
3558 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3560 if (ouv.version != 2) {
3561 /* we always store as version 2, and
3562 * replUpToDateVector is not replicated
3564 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3567 *count = ouv.ctr.ctr2.count;
3568 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
3569 } else {
3570 *count = 0;
3571 *cursors = NULL;
3574 talloc_free(r);
3576 our_invocation_id = samdb_ntds_invocation_id(samdb);
3577 if (!our_invocation_id) {
3578 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
3579 talloc_free(*cursors);
3580 return ldb_operr(samdb);
3583 ret = ldb_sequence_number(samdb, LDB_SEQ_HIGHEST_SEQ, &highest_usn);
3584 if (ret != LDB_SUCCESS) {
3585 /* nothing to add - this can happen after a vampire */
3586 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3587 return LDB_SUCCESS;
3590 for (i=0; i<*count; i++) {
3591 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
3592 (*cursors)[i].highest_usn = highest_usn;
3593 (*cursors)[i].last_sync_success = nt1970;
3594 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3595 return LDB_SUCCESS;
3599 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
3600 if (! *cursors) {
3601 return ldb_oom(samdb);
3604 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
3605 (*cursors)[*count].highest_usn = highest_usn;
3606 (*cursors)[*count].last_sync_success = nt1970;
3607 (*count)++;
3609 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3611 return LDB_SUCCESS;
3615 load the UDV for a partition in version 1 format
3616 The list is returned sorted, and with our local cursor added
3618 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3619 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
3621 struct drsuapi_DsReplicaCursor2 *v2;
3622 uint32_t i;
3623 int ret;
3625 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
3626 if (ret != LDB_SUCCESS) {
3627 return ret;
3630 if (*count == 0) {
3631 talloc_free(v2);
3632 *cursors = NULL;
3633 return LDB_SUCCESS;
3636 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
3637 if (*cursors == NULL) {
3638 talloc_free(v2);
3639 return ldb_oom(samdb);
3642 for (i=0; i<*count; i++) {
3643 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
3644 (*cursors)[i].highest_usn = v2[i].highest_usn;
3646 talloc_free(v2);
3647 return LDB_SUCCESS;
3651 add a set of controls to a ldb_request structure based on a set of
3652 flags. See util.h for a list of available flags
3654 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
3656 int ret;
3657 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
3658 struct ldb_search_options_control *options;
3659 /* Using the phantom root control allows us to search all partitions */
3660 options = talloc(req, struct ldb_search_options_control);
3661 if (options == NULL) {
3662 return LDB_ERR_OPERATIONS_ERROR;
3664 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
3666 ret = ldb_request_add_control(req,
3667 LDB_CONTROL_SEARCH_OPTIONS_OID,
3668 true, options);
3669 if (ret != LDB_SUCCESS) {
3670 return ret;
3674 if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
3675 ret = ldb_request_add_control(req,
3676 DSDB_CONTROL_NO_GLOBAL_CATALOG,
3677 false, NULL);
3678 if (ret != LDB_SUCCESS) {
3679 return ret;
3683 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
3684 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
3685 if (ret != LDB_SUCCESS) {
3686 return ret;
3690 if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
3691 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
3692 if (ret != LDB_SUCCESS) {
3693 return ret;
3697 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
3698 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
3699 if (ret != LDB_SUCCESS) {
3700 return ret;
3704 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
3705 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
3706 if (!extended_ctrl) {
3707 return LDB_ERR_OPERATIONS_ERROR;
3709 extended_ctrl->type = 1;
3711 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
3712 if (ret != LDB_SUCCESS) {
3713 return ret;
3717 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
3718 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
3719 if (ret != LDB_SUCCESS) {
3720 return ret;
3724 if (dsdb_flags & DSDB_MODIFY_RELAX) {
3725 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
3726 if (ret != LDB_SUCCESS) {
3727 return ret;
3731 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
3732 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
3733 if (ret != LDB_SUCCESS) {
3734 return ret;
3738 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
3739 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
3740 if (ret != LDB_SUCCESS) {
3741 return ret;
3745 if (dsdb_flags & DSDB_TREE_DELETE) {
3746 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
3747 if (ret != LDB_SUCCESS) {
3748 return ret;
3752 if (dsdb_flags & DSDB_PROVISION) {
3753 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
3754 if (ret != LDB_SUCCESS) {
3755 return ret;
3759 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
3760 if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
3761 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
3762 if (ret != LDB_SUCCESS) {
3763 return ret;
3767 if (dsdb_flags & DSDB_PASSWORD_BYPASS_LAST_SET) {
3769 * This must not be critical, as it will only be
3770 * handled (and need to be handled) if the other
3771 * attributes in the request bring password_hash into
3772 * action
3774 ret = ldb_request_add_control(req, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID, false, NULL);
3775 if (ret != LDB_SUCCESS) {
3776 return ret;
3780 if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
3781 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
3782 if (ret != LDB_SUCCESS) {
3783 return ret;
3787 return LDB_SUCCESS;
3791 an add with a set of controls
3793 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
3794 uint32_t dsdb_flags)
3796 struct ldb_request *req;
3797 int ret;
3799 ret = ldb_build_add_req(&req, ldb, ldb,
3800 message,
3801 NULL,
3802 NULL,
3803 ldb_op_default_callback,
3804 NULL);
3806 if (ret != LDB_SUCCESS) return ret;
3808 ret = dsdb_request_add_controls(req, dsdb_flags);
3809 if (ret != LDB_SUCCESS) {
3810 talloc_free(req);
3811 return ret;
3814 ret = dsdb_autotransaction_request(ldb, req);
3816 talloc_free(req);
3817 return ret;
3821 a modify with a set of controls
3823 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
3824 uint32_t dsdb_flags)
3826 struct ldb_request *req;
3827 int ret;
3829 ret = ldb_build_mod_req(&req, ldb, ldb,
3830 message,
3831 NULL,
3832 NULL,
3833 ldb_op_default_callback,
3834 NULL);
3836 if (ret != LDB_SUCCESS) return ret;
3838 ret = dsdb_request_add_controls(req, dsdb_flags);
3839 if (ret != LDB_SUCCESS) {
3840 talloc_free(req);
3841 return ret;
3844 ret = dsdb_autotransaction_request(ldb, req);
3846 talloc_free(req);
3847 return ret;
3851 a delete with a set of flags
3853 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
3854 uint32_t dsdb_flags)
3856 struct ldb_request *req;
3857 int ret;
3859 ret = ldb_build_del_req(&req, ldb, ldb,
3861 NULL,
3862 NULL,
3863 ldb_op_default_callback,
3864 NULL);
3866 if (ret != LDB_SUCCESS) return ret;
3868 ret = dsdb_request_add_controls(req, dsdb_flags);
3869 if (ret != LDB_SUCCESS) {
3870 talloc_free(req);
3871 return ret;
3874 ret = dsdb_autotransaction_request(ldb, req);
3876 talloc_free(req);
3877 return ret;
3881 like dsdb_modify() but set all the element flags to
3882 LDB_FLAG_MOD_REPLACE
3884 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
3886 unsigned int i;
3888 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3889 for (i=0;i<msg->num_elements;i++) {
3890 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3893 return dsdb_modify(ldb, msg, dsdb_flags);
3898 search for attrs on one DN, allowing for dsdb_flags controls
3900 int dsdb_search_dn(struct ldb_context *ldb,
3901 TALLOC_CTX *mem_ctx,
3902 struct ldb_result **_result,
3903 struct ldb_dn *basedn,
3904 const char * const *attrs,
3905 uint32_t dsdb_flags)
3907 int ret;
3908 struct ldb_request *req;
3909 struct ldb_result *res;
3911 res = talloc_zero(mem_ctx, struct ldb_result);
3912 if (!res) {
3913 return ldb_oom(ldb);
3916 ret = ldb_build_search_req(&req, ldb, res,
3917 basedn,
3918 LDB_SCOPE_BASE,
3919 NULL,
3920 attrs,
3921 NULL,
3922 res,
3923 ldb_search_default_callback,
3924 NULL);
3925 if (ret != LDB_SUCCESS) {
3926 talloc_free(res);
3927 return ret;
3930 ret = dsdb_request_add_controls(req, dsdb_flags);
3931 if (ret != LDB_SUCCESS) {
3932 talloc_free(res);
3933 return ret;
3936 ret = ldb_request(ldb, req);
3937 if (ret == LDB_SUCCESS) {
3938 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3941 talloc_free(req);
3942 if (ret != LDB_SUCCESS) {
3943 talloc_free(res);
3944 return ret;
3947 *_result = res;
3948 return LDB_SUCCESS;
3952 search for attrs on one DN, by the GUID of the DN, allowing for
3953 dsdb_flags controls
3955 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
3956 TALLOC_CTX *mem_ctx,
3957 struct ldb_result **_result,
3958 const struct GUID *guid,
3959 const char * const *attrs,
3960 uint32_t dsdb_flags)
3962 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3963 struct ldb_dn *dn;
3964 int ret;
3966 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
3967 if (dn == NULL) {
3968 talloc_free(tmp_ctx);
3969 return ldb_oom(ldb);
3972 ret = dsdb_search_dn(ldb, mem_ctx, _result, dn, attrs, dsdb_flags);
3973 talloc_free(tmp_ctx);
3974 return ret;
3978 general search with dsdb_flags for controls
3980 int dsdb_search(struct ldb_context *ldb,
3981 TALLOC_CTX *mem_ctx,
3982 struct ldb_result **_result,
3983 struct ldb_dn *basedn,
3984 enum ldb_scope scope,
3985 const char * const *attrs,
3986 uint32_t dsdb_flags,
3987 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
3989 int ret;
3990 struct ldb_request *req;
3991 struct ldb_result *res;
3992 va_list ap;
3993 char *expression = NULL;
3994 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3996 /* cross-partitions searches with a basedn break multi-domain support */
3997 SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
3999 res = talloc_zero(tmp_ctx, struct ldb_result);
4000 if (!res) {
4001 talloc_free(tmp_ctx);
4002 return ldb_oom(ldb);
4005 if (exp_fmt) {
4006 va_start(ap, exp_fmt);
4007 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4008 va_end(ap);
4010 if (!expression) {
4011 talloc_free(tmp_ctx);
4012 return ldb_oom(ldb);
4016 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
4017 basedn,
4018 scope,
4019 expression,
4020 attrs,
4021 NULL,
4022 res,
4023 ldb_search_default_callback,
4024 NULL);
4025 if (ret != LDB_SUCCESS) {
4026 talloc_free(tmp_ctx);
4027 return ret;
4030 ret = dsdb_request_add_controls(req, dsdb_flags);
4031 if (ret != LDB_SUCCESS) {
4032 talloc_free(tmp_ctx);
4033 ldb_reset_err_string(ldb);
4034 return ret;
4037 ret = ldb_request(ldb, req);
4038 if (ret == LDB_SUCCESS) {
4039 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4042 if (ret != LDB_SUCCESS) {
4043 talloc_free(tmp_ctx);
4044 return ret;
4047 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
4048 if (res->count == 0) {
4049 talloc_free(tmp_ctx);
4050 ldb_reset_err_string(ldb);
4051 return LDB_ERR_NO_SUCH_OBJECT;
4053 if (res->count != 1) {
4054 talloc_free(tmp_ctx);
4055 ldb_reset_err_string(ldb);
4056 return LDB_ERR_CONSTRAINT_VIOLATION;
4060 *_result = talloc_steal(mem_ctx, res);
4061 talloc_free(tmp_ctx);
4063 return LDB_SUCCESS;
4068 general search with dsdb_flags for controls
4069 returns exactly 1 record or an error
4071 int dsdb_search_one(struct ldb_context *ldb,
4072 TALLOC_CTX *mem_ctx,
4073 struct ldb_message **msg,
4074 struct ldb_dn *basedn,
4075 enum ldb_scope scope,
4076 const char * const *attrs,
4077 uint32_t dsdb_flags,
4078 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4080 int ret;
4081 struct ldb_result *res;
4082 va_list ap;
4083 char *expression = NULL;
4084 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4086 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4088 res = talloc_zero(tmp_ctx, struct ldb_result);
4089 if (!res) {
4090 talloc_free(tmp_ctx);
4091 return ldb_oom(ldb);
4094 if (exp_fmt) {
4095 va_start(ap, exp_fmt);
4096 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4097 va_end(ap);
4099 if (!expression) {
4100 talloc_free(tmp_ctx);
4101 return ldb_oom(ldb);
4103 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4104 dsdb_flags, "%s", expression);
4105 } else {
4106 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4107 dsdb_flags, NULL);
4110 if (ret != LDB_SUCCESS) {
4111 talloc_free(tmp_ctx);
4112 return ret;
4115 *msg = talloc_steal(mem_ctx, res->msgs[0]);
4116 talloc_free(tmp_ctx);
4118 return LDB_SUCCESS;
4121 /* returns back the forest DNS name */
4122 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4124 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4125 ldb_get_root_basedn(ldb));
4126 char *p;
4128 if (forest_name == NULL) {
4129 return NULL;
4132 p = strchr(forest_name, '/');
4133 if (p) {
4134 *p = '\0';
4137 return forest_name;
4140 /* returns back the default domain DNS name */
4141 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4143 const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4144 ldb_get_default_basedn(ldb));
4145 char *p;
4147 if (domain_name == NULL) {
4148 return NULL;
4151 p = strchr(domain_name, '/');
4152 if (p) {
4153 *p = '\0';
4156 return domain_name;
4160 validate that an DSA GUID belongs to the specified user sid.
4161 The user SID must be a domain controller account (either RODC or
4162 RWDC)
4164 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4165 const struct GUID *dsa_guid,
4166 const struct dom_sid *sid)
4168 /* strategy:
4169 - find DN of record with the DSA GUID in the
4170 configuration partition (objectGUID)
4171 - remove "NTDS Settings" component from DN
4172 - do a base search on that DN for serverReference with
4173 extended-dn enabled
4174 - extract objectSid from resulting serverReference
4175 attribute
4176 - check this sid matches the sid argument
4178 struct ldb_dn *config_dn;
4179 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4180 struct ldb_message *msg;
4181 const char *attrs1[] = { NULL };
4182 const char *attrs2[] = { "serverReference", NULL };
4183 int ret;
4184 struct ldb_dn *dn, *account_dn;
4185 struct dom_sid sid2;
4186 NTSTATUS status;
4188 config_dn = ldb_get_config_basedn(ldb);
4190 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4191 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4192 if (ret != LDB_SUCCESS) {
4193 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4194 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4195 talloc_free(tmp_ctx);
4196 return ldb_operr(ldb);
4198 dn = msg->dn;
4200 if (!ldb_dn_remove_child_components(dn, 1)) {
4201 talloc_free(tmp_ctx);
4202 return ldb_operr(ldb);
4205 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4206 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4207 "(objectClass=server)");
4208 if (ret != LDB_SUCCESS) {
4209 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4210 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4211 talloc_free(tmp_ctx);
4212 return ldb_operr(ldb);
4215 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4216 if (account_dn == NULL) {
4217 DEBUG(1,(__location__ ": Failed to find account_dn for DSA with objectGUID %s, sid %s\n",
4218 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4219 talloc_free(tmp_ctx);
4220 return ldb_operr(ldb);
4223 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4224 if (!NT_STATUS_IS_OK(status)) {
4225 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4226 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4227 talloc_free(tmp_ctx);
4228 return ldb_operr(ldb);
4231 if (!dom_sid_equal(sid, &sid2)) {
4232 /* someone is trying to spoof another account */
4233 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4234 GUID_string(tmp_ctx, dsa_guid),
4235 dom_sid_string(tmp_ctx, sid),
4236 dom_sid_string(tmp_ctx, &sid2)));
4237 talloc_free(tmp_ctx);
4238 return ldb_operr(ldb);
4241 talloc_free(tmp_ctx);
4242 return LDB_SUCCESS;
4245 static const char * const secret_attributes[] = {
4246 DSDB_SECRET_ATTRIBUTES,
4247 NULL
4251 check if the attribute belongs to the RODC filtered attribute set
4252 Note that attributes that are in the filtered attribute set are the
4253 ones that _are_ always sent to a RODC
4255 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4257 /* they never get secret attributes */
4258 if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4259 return false;
4262 /* they do get non-secret critical attributes */
4263 if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4264 return true;
4267 /* they do get non-secret attributes marked as being in the FAS */
4268 if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4269 return true;
4272 /* other attributes are denied */
4273 return false;
4276 /* return fsmo role dn and role owner dn for a particular role*/
4277 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4278 struct ldb_context *ldb,
4279 uint32_t role,
4280 struct ldb_dn **fsmo_role_dn,
4281 struct ldb_dn **role_owner_dn)
4283 int ret;
4284 switch (role) {
4285 case DREPL_NAMING_MASTER:
4286 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
4287 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4288 if (ret != LDB_SUCCESS) {
4289 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
4290 ldb_errstring(ldb)));
4291 talloc_free(tmp_ctx);
4292 return WERR_DS_DRA_INTERNAL_ERROR;
4294 break;
4295 case DREPL_INFRASTRUCTURE_MASTER:
4296 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
4297 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4298 if (ret != LDB_SUCCESS) {
4299 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4300 ldb_errstring(ldb)));
4301 talloc_free(tmp_ctx);
4302 return WERR_DS_DRA_INTERNAL_ERROR;
4304 break;
4305 case DREPL_RID_MASTER:
4306 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
4307 if (ret != LDB_SUCCESS) {
4308 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
4309 talloc_free(tmp_ctx);
4310 return WERR_DS_DRA_INTERNAL_ERROR;
4313 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4314 if (ret != LDB_SUCCESS) {
4315 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
4316 ldb_errstring(ldb)));
4317 talloc_free(tmp_ctx);
4318 return WERR_DS_DRA_INTERNAL_ERROR;
4320 break;
4321 case DREPL_SCHEMA_MASTER:
4322 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
4323 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4324 if (ret != LDB_SUCCESS) {
4325 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4326 ldb_errstring(ldb)));
4327 talloc_free(tmp_ctx);
4328 return WERR_DS_DRA_INTERNAL_ERROR;
4330 break;
4331 case DREPL_PDC_MASTER:
4332 *fsmo_role_dn = ldb_get_default_basedn(ldb);
4333 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4334 if (ret != LDB_SUCCESS) {
4335 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
4336 ldb_errstring(ldb)));
4337 talloc_free(tmp_ctx);
4338 return WERR_DS_DRA_INTERNAL_ERROR;
4340 break;
4341 default:
4342 return WERR_DS_DRA_INTERNAL_ERROR;
4344 return WERR_OK;
4347 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
4348 TALLOC_CTX *mem_ctx,
4349 struct ldb_dn *server_dn)
4351 int ldb_ret;
4352 struct ldb_result *res = NULL;
4353 const char * const attrs[] = { "dNSHostName", NULL};
4355 ldb_ret = ldb_search(ldb, mem_ctx, &res,
4356 server_dn,
4357 LDB_SCOPE_BASE,
4358 attrs, NULL);
4359 if (ldb_ret != LDB_SUCCESS) {
4360 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
4361 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
4362 return NULL;
4365 return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
4369 returns true if an attribute is in the filter,
4370 false otherwise, provided that attribute value is provided with the expression
4372 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
4373 const char *attr)
4375 unsigned int i;
4376 switch (tree->operation) {
4377 case LDB_OP_AND:
4378 case LDB_OP_OR:
4379 for (i=0;i<tree->u.list.num_elements;i++) {
4380 if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
4381 attr))
4382 return true;
4384 return false;
4385 case LDB_OP_NOT:
4386 return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
4387 case LDB_OP_EQUALITY:
4388 case LDB_OP_GREATER:
4389 case LDB_OP_LESS:
4390 case LDB_OP_APPROX:
4391 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
4392 return true;
4394 return false;
4395 case LDB_OP_SUBSTRING:
4396 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
4397 return true;
4399 return false;
4400 case LDB_OP_PRESENT:
4401 /* (attrname=*) is not filtered out */
4402 return false;
4403 case LDB_OP_EXTENDED:
4404 if (tree->u.extended.attr &&
4405 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
4406 return true;
4408 return false;
4410 return false;
4413 bool is_attr_in_list(const char * const * attrs, const char *attr)
4415 unsigned int i;
4417 for (i = 0; attrs[i]; i++) {
4418 if (ldb_attr_cmp(attrs[i], attr) == 0)
4419 return true;
4422 return false;
4427 map an ldb error code to an approximate NTSTATUS code
4429 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
4431 switch (err) {
4432 case LDB_SUCCESS:
4433 return NT_STATUS_OK;
4435 case LDB_ERR_PROTOCOL_ERROR:
4436 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
4438 case LDB_ERR_TIME_LIMIT_EXCEEDED:
4439 return NT_STATUS_IO_TIMEOUT;
4441 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
4442 return NT_STATUS_BUFFER_TOO_SMALL;
4444 case LDB_ERR_COMPARE_FALSE:
4445 case LDB_ERR_COMPARE_TRUE:
4446 return NT_STATUS_REVISION_MISMATCH;
4448 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
4449 return NT_STATUS_NOT_SUPPORTED;
4451 case LDB_ERR_STRONG_AUTH_REQUIRED:
4452 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
4453 case LDB_ERR_SASL_BIND_IN_PROGRESS:
4454 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
4455 case LDB_ERR_INVALID_CREDENTIALS:
4456 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
4457 case LDB_ERR_UNWILLING_TO_PERFORM:
4458 return NT_STATUS_ACCESS_DENIED;
4460 case LDB_ERR_NO_SUCH_OBJECT:
4461 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4463 case LDB_ERR_REFERRAL:
4464 case LDB_ERR_NO_SUCH_ATTRIBUTE:
4465 return NT_STATUS_NOT_FOUND;
4467 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
4468 return NT_STATUS_NOT_SUPPORTED;
4470 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
4471 return NT_STATUS_BUFFER_TOO_SMALL;
4473 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
4474 case LDB_ERR_INAPPROPRIATE_MATCHING:
4475 case LDB_ERR_CONSTRAINT_VIOLATION:
4476 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
4477 case LDB_ERR_INVALID_DN_SYNTAX:
4478 case LDB_ERR_NAMING_VIOLATION:
4479 case LDB_ERR_OBJECT_CLASS_VIOLATION:
4480 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
4481 case LDB_ERR_NOT_ALLOWED_ON_RDN:
4482 return NT_STATUS_INVALID_PARAMETER;
4484 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
4485 case LDB_ERR_ENTRY_ALREADY_EXISTS:
4486 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
4488 case LDB_ERR_BUSY:
4489 return NT_STATUS_NETWORK_BUSY;
4491 case LDB_ERR_ALIAS_PROBLEM:
4492 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
4493 case LDB_ERR_UNAVAILABLE:
4494 case LDB_ERR_LOOP_DETECT:
4495 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
4496 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
4497 case LDB_ERR_OTHER:
4498 case LDB_ERR_OPERATIONS_ERROR:
4499 break;
4501 return NT_STATUS_UNSUCCESSFUL;
4506 create a new naming context that will hold a partial replica
4508 int dsdb_create_partial_replica_NC(struct ldb_context *ldb, struct ldb_dn *dn)
4510 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4511 struct ldb_message *msg;
4512 int ret;
4514 msg = ldb_msg_new(tmp_ctx);
4515 if (msg == NULL) {
4516 talloc_free(tmp_ctx);
4517 return ldb_oom(ldb);
4520 msg->dn = dn;
4521 ret = ldb_msg_add_string(msg, "objectClass", "top");
4522 if (ret != LDB_SUCCESS) {
4523 talloc_free(tmp_ctx);
4524 return ldb_oom(ldb);
4527 /* [MS-DRSR] implies that we should only add the 'top'
4528 * objectclass, but that would cause lots of problems with our
4529 * objectclass code as top is not structural, so we add
4530 * 'domainDNS' as well to keep things sane. We're expecting
4531 * this new NC to be of objectclass domainDNS after
4532 * replication anyway
4534 ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
4535 if (ret != LDB_SUCCESS) {
4536 talloc_free(tmp_ctx);
4537 return ldb_oom(ldb);
4540 ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
4541 INSTANCE_TYPE_IS_NC_HEAD|
4542 INSTANCE_TYPE_NC_ABOVE|
4543 INSTANCE_TYPE_UNINSTANT);
4544 if (ret != LDB_SUCCESS) {
4545 talloc_free(tmp_ctx);
4546 return ldb_oom(ldb);
4549 ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
4550 if (ret != LDB_SUCCESS && ret != LDB_ERR_ENTRY_ALREADY_EXISTS) {
4551 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
4552 ldb_dn_get_linearized(dn),
4553 ldb_errstring(ldb), ldb_strerror(ret)));
4554 talloc_free(tmp_ctx);
4555 return ret;
4558 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
4560 talloc_free(tmp_ctx);
4561 return LDB_SUCCESS;
4565 build a GUID from a string
4567 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
4569 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
4570 uint32_t time_low;
4571 uint32_t time_mid, time_hi_and_version;
4572 uint32_t clock_seq[2];
4573 uint32_t node[6];
4574 int i;
4576 if (s == NULL) {
4577 return NT_STATUS_INVALID_PARAMETER;
4580 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
4581 &time_low, &time_mid, &time_hi_and_version,
4582 &clock_seq[0], &clock_seq[1],
4583 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
4584 status = NT_STATUS_OK;
4587 if (!NT_STATUS_IS_OK(status)) {
4588 return status;
4591 guid->time_low = time_low;
4592 guid->time_mid = time_mid;
4593 guid->time_hi_and_version = time_hi_and_version;
4594 guid->clock_seq[0] = clock_seq[0];
4595 guid->clock_seq[1] = clock_seq[1];
4596 for (i=0;i<6;i++) {
4597 guid->node[i] = node[i];
4600 return NT_STATUS_OK;
4603 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
4605 return talloc_asprintf(mem_ctx,
4606 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
4607 guid->time_low, guid->time_mid,
4608 guid->time_hi_and_version,
4609 guid->clock_seq[0],
4610 guid->clock_seq[1],
4611 guid->node[0], guid->node[1],
4612 guid->node[2], guid->node[3],
4613 guid->node[4], guid->node[5]);