python/drs: Ensure to pass in the local invocationID during the domain join
[Samba.git] / source4 / dsdb / common / util.c
blob55bd73e424996043b84228146918c2bd43ebe156
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(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct ldb_message *msg,
562 struct samr_Password **lm_pwd, struct samr_Password **nt_pwd)
564 struct samr_Password *lmPwdHash, *ntPwdHash;
565 if (nt_pwd) {
566 unsigned int num_nt;
567 num_nt = samdb_result_hashes(mem_ctx, msg, "unicodePwd", &ntPwdHash);
568 if (num_nt == 0) {
569 *nt_pwd = NULL;
570 } else if (num_nt > 1) {
571 return NT_STATUS_INTERNAL_DB_CORRUPTION;
572 } else {
573 *nt_pwd = &ntPwdHash[0];
576 if (lm_pwd) {
577 /* Ensure that if we have turned off LM
578 * authentication, that we never use the LM hash, even
579 * if we store it */
580 if (lpcfg_lanman_auth(lp_ctx)) {
581 unsigned int num_lm;
582 num_lm = samdb_result_hashes(mem_ctx, msg, "dBCSPwd", &lmPwdHash);
583 if (num_lm == 0) {
584 *lm_pwd = NULL;
585 } else if (num_lm > 1) {
586 return NT_STATUS_INTERNAL_DB_CORRUPTION;
587 } else {
588 *lm_pwd = &lmPwdHash[0];
590 } else {
591 *lm_pwd = NULL;
594 return NT_STATUS_OK;
598 pull a samr_LogonHours structutre from a result set.
600 struct samr_LogonHours samdb_result_logon_hours(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char *attr)
602 struct samr_LogonHours hours;
603 size_t units_per_week = 168;
604 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
606 ZERO_STRUCT(hours);
608 if (val) {
609 units_per_week = val->length * 8;
612 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week/8);
613 if (!hours.bits) {
614 return hours;
616 hours.units_per_week = units_per_week;
617 memset(hours.bits, 0xFF, units_per_week/8);
618 if (val) {
619 memcpy(hours.bits, val->data, val->length);
622 return hours;
626 pull a set of account_flags from a result set.
628 This requires that the attributes:
629 pwdLastSet
630 userAccountControl
631 be included in 'msg'
633 uint32_t samdb_result_acct_flags(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
634 struct ldb_message *msg, struct ldb_dn *domain_dn)
636 uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
637 uint32_t acct_flags = ds_uf2acb(userAccountControl);
638 NTTIME must_change_time;
639 NTTIME now;
641 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
642 domain_dn, msg);
644 /* Test account expire time */
645 unix_to_nt_time(&now, time(NULL));
646 /* check for expired password */
647 if (must_change_time < now) {
648 acct_flags |= ACB_PW_EXPIRED;
650 return acct_flags;
653 struct lsa_BinaryString samdb_result_parameters(TALLOC_CTX *mem_ctx,
654 struct ldb_message *msg,
655 const char *attr)
657 struct lsa_BinaryString s;
658 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
660 ZERO_STRUCT(s);
662 if (!val) {
663 return s;
666 s.array = talloc_array(mem_ctx, uint16_t, val->length/2);
667 if (!s.array) {
668 return s;
670 s.length = s.size = val->length;
671 memcpy(s.array, val->data, val->length);
673 return s;
676 /* Find an attribute, with a particular value */
678 /* The current callers of this function expect a very specific
679 * behaviour: In particular, objectClass subclass equivilance is not
680 * wanted. This means that we should not lookup the schema for the
681 * comparison function */
682 struct ldb_message_element *samdb_find_attribute(struct ldb_context *ldb,
683 const struct ldb_message *msg,
684 const char *name, const char *value)
686 unsigned int i;
687 struct ldb_message_element *el = ldb_msg_find_element(msg, name);
689 if (!el) {
690 return NULL;
693 for (i=0;i<el->num_values;i++) {
694 if (ldb_attr_cmp(value, (char *)el->values[i].data) == 0) {
695 return el;
699 return NULL;
702 int samdb_find_or_add_attribute(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
704 struct ldb_message_element *el;
706 el = ldb_msg_find_element(msg, name);
707 if (el) {
708 return LDB_SUCCESS;
711 return ldb_msg_add_string(msg, name, set_value);
715 add a dom_sid element to a message
717 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
718 const char *attr_name, const struct dom_sid *sid)
720 struct ldb_val v;
721 enum ndr_err_code ndr_err;
723 ndr_err = ndr_push_struct_blob(&v, mem_ctx,
724 sid,
725 (ndr_push_flags_fn_t)ndr_push_dom_sid);
726 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
727 return ldb_operr(sam_ldb);
729 return ldb_msg_add_value(msg, attr_name, &v, NULL);
734 add a delete element operation to a message
736 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
737 const char *attr_name)
739 /* we use an empty replace rather than a delete, as it allows for
740 dsdb_replace() to be used everywhere */
741 return ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_REPLACE, NULL);
745 add an add attribute value to a message or enhance an existing attribute
746 which has the same name and the add flag set.
748 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
749 struct ldb_message *msg, const char *attr_name,
750 const char *value)
752 struct ldb_message_element *el;
753 struct ldb_val val, *vals;
754 char *v;
755 unsigned int i;
756 bool found = false;
757 int ret;
759 v = talloc_strdup(mem_ctx, value);
760 if (v == NULL) {
761 return ldb_oom(sam_ldb);
764 val.data = (uint8_t *) v;
765 val.length = strlen(v);
767 if (val.length == 0) {
768 /* allow empty strings as non-existent attributes */
769 return LDB_SUCCESS;
772 for (i = 0; i < msg->num_elements; i++) {
773 el = &msg->elements[i];
774 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
775 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD)) {
776 found = true;
777 break;
780 if (!found) {
781 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_ADD,
782 &el);
783 if (ret != LDB_SUCCESS) {
784 return ret;
788 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
789 el->num_values + 1);
790 if (vals == NULL) {
791 return ldb_oom(sam_ldb);
793 el->values = vals;
794 el->values[el->num_values] = val;
795 ++(el->num_values);
797 return LDB_SUCCESS;
801 add a delete attribute value to a message or enhance an existing attribute
802 which has the same name and the delete flag set.
804 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
805 struct ldb_message *msg, const char *attr_name,
806 const char *value)
808 struct ldb_message_element *el;
809 struct ldb_val val, *vals;
810 char *v;
811 unsigned int i;
812 bool found = false;
813 int ret;
815 v = talloc_strdup(mem_ctx, value);
816 if (v == NULL) {
817 return ldb_oom(sam_ldb);
820 val.data = (uint8_t *) v;
821 val.length = strlen(v);
823 if (val.length == 0) {
824 /* allow empty strings as non-existent attributes */
825 return LDB_SUCCESS;
828 for (i = 0; i < msg->num_elements; i++) {
829 el = &msg->elements[i];
830 if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
831 (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
832 found = true;
833 break;
836 if (!found) {
837 ret = ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_DELETE,
838 &el);
839 if (ret != LDB_SUCCESS) {
840 return ret;
844 vals = talloc_realloc(msg->elements, el->values, struct ldb_val,
845 el->num_values + 1);
846 if (vals == NULL) {
847 return ldb_oom(sam_ldb);
849 el->values = vals;
850 el->values[el->num_values] = val;
851 ++(el->num_values);
853 return LDB_SUCCESS;
857 add a int element to a message
859 int samdb_msg_add_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
860 const char *attr_name, int v)
862 const char *s = talloc_asprintf(mem_ctx, "%d", v);
863 if (s == NULL) {
864 return ldb_oom(sam_ldb);
866 return ldb_msg_add_string(msg, attr_name, s);
870 * Add an unsigned int element to a message
872 * The issue here is that we have not yet first cast to int32_t explicitly,
873 * before we cast to an signed int to printf() into the %d or cast to a
874 * int64_t before we then cast to a long long to printf into a %lld.
876 * There are *no* unsigned integers in Active Directory LDAP, even the RID
877 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
878 * (See the schema, and the syntax definitions in schema_syntax.c).
881 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
882 const char *attr_name, unsigned int v)
884 return samdb_msg_add_int(sam_ldb, mem_ctx, msg, attr_name, (int)v);
888 add a (signed) int64_t element to a message
890 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
891 const char *attr_name, int64_t v)
893 const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
894 if (s == NULL) {
895 return ldb_oom(sam_ldb);
897 return ldb_msg_add_string(msg, attr_name, s);
901 * Add an unsigned int64_t (uint64_t) element to a message
903 * The issue here is that we have not yet first cast to int32_t explicitly,
904 * before we cast to an signed int to printf() into the %d or cast to a
905 * int64_t before we then cast to a long long to printf into a %lld.
907 * There are *no* unsigned integers in Active Directory LDAP, even the RID
908 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
909 * (See the schema, and the syntax definitions in schema_syntax.c).
912 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
913 const char *attr_name, uint64_t v)
915 return samdb_msg_add_int64(sam_ldb, mem_ctx, msg, attr_name, (int64_t)v);
919 add a samr_Password element to a message
921 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
922 const char *attr_name, const struct samr_Password *hash)
924 struct ldb_val val;
925 val.data = talloc_memdup(mem_ctx, hash->hash, 16);
926 if (!val.data) {
927 return ldb_oom(sam_ldb);
929 val.length = 16;
930 return ldb_msg_add_value(msg, attr_name, &val, NULL);
934 add a samr_Password array to a message
936 int samdb_msg_add_hashes(struct ldb_context *ldb,
937 TALLOC_CTX *mem_ctx, struct ldb_message *msg,
938 const char *attr_name, struct samr_Password *hashes,
939 unsigned int count)
941 struct ldb_val val;
942 unsigned int i;
943 val.data = talloc_array_size(mem_ctx, 16, count);
944 val.length = count*16;
945 if (!val.data) {
946 return ldb_oom(ldb);
948 for (i=0;i<count;i++) {
949 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
951 return ldb_msg_add_value(msg, attr_name, &val, NULL);
955 add a acct_flags element to a message
957 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
958 const char *attr_name, uint32_t v)
960 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
964 add a logon_hours element to a message
966 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
967 const char *attr_name, struct samr_LogonHours *hours)
969 struct ldb_val val;
970 val.length = hours->units_per_week / 8;
971 val.data = hours->bits;
972 return ldb_msg_add_value(msg, attr_name, &val, NULL);
976 add a parameters element to a message
978 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
979 const char *attr_name, struct lsa_BinaryString *parameters)
981 struct ldb_val val;
982 val.length = parameters->length;
983 val.data = (uint8_t *)parameters->array;
984 return ldb_msg_add_value(msg, attr_name, &val, NULL);
988 * Sets an unsigned int element in a message
990 * The issue here is that we have not yet first cast to int32_t explicitly,
991 * before we cast to an signed int to printf() into the %d or cast to a
992 * int64_t before we then cast to a long long to printf into a %lld.
994 * There are *no* unsigned integers in Active Directory LDAP, even the RID
995 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
996 * (See the schema, and the syntax definitions in schema_syntax.c).
999 int samdb_msg_set_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
1000 struct ldb_message *msg, const char *attr_name,
1001 unsigned int v)
1003 struct ldb_message_element *el;
1005 el = ldb_msg_find_element(msg, attr_name);
1006 if (el) {
1007 el->num_values = 0;
1009 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, v);
1013 * Handle ldb_request in transaction
1015 static int dsdb_autotransaction_request(struct ldb_context *sam_ldb,
1016 struct ldb_request *req)
1018 int ret;
1020 ret = ldb_transaction_start(sam_ldb);
1021 if (ret != LDB_SUCCESS) {
1022 return ret;
1025 ret = ldb_request(sam_ldb, req);
1026 if (ret == LDB_SUCCESS) {
1027 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1030 if (ret == LDB_SUCCESS) {
1031 return ldb_transaction_commit(sam_ldb);
1033 ldb_transaction_cancel(sam_ldb);
1035 return ret;
1039 return a default security descriptor
1041 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1043 struct security_descriptor *sd;
1045 sd = security_descriptor_initialise(mem_ctx);
1047 return sd;
1050 struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1052 struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
1053 struct ldb_dn *aggregate_dn;
1054 if (!schema_dn) {
1055 return NULL;
1058 aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
1059 if (!aggregate_dn) {
1060 return NULL;
1062 if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
1063 return NULL;
1065 return aggregate_dn;
1068 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1070 struct ldb_dn *new_dn;
1072 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1073 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1074 talloc_free(new_dn);
1075 return NULL;
1077 return new_dn;
1080 struct ldb_dn *samdb_infrastructure_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1082 struct ldb_dn *new_dn;
1084 new_dn = ldb_dn_copy(mem_ctx, ldb_get_default_basedn(sam_ctx));
1085 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Infrastructure")) {
1086 talloc_free(new_dn);
1087 return NULL;
1089 return new_dn;
1092 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1094 struct ldb_dn *new_dn;
1096 new_dn = ldb_dn_copy(mem_ctx, ldb_get_config_basedn(sam_ctx));
1097 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1098 talloc_free(new_dn);
1099 return NULL;
1101 return new_dn;
1105 work out the domain sid for the current open ldb
1107 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1109 TALLOC_CTX *tmp_ctx;
1110 const struct dom_sid *domain_sid;
1111 const char *attrs[] = {
1112 "objectSid",
1113 NULL
1115 struct ldb_result *res;
1116 int ret;
1118 /* see if we have a cached copy */
1119 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1120 if (domain_sid) {
1121 return domain_sid;
1124 tmp_ctx = talloc_new(ldb);
1125 if (tmp_ctx == NULL) {
1126 goto failed;
1129 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1131 if (ret != LDB_SUCCESS) {
1132 goto failed;
1135 if (res->count != 1) {
1136 goto failed;
1139 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1140 if (domain_sid == NULL) {
1141 goto failed;
1144 /* cache the domain_sid in the ldb */
1145 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1146 goto failed;
1149 talloc_steal(ldb, domain_sid);
1150 talloc_free(tmp_ctx);
1152 return domain_sid;
1154 failed:
1155 talloc_free(tmp_ctx);
1156 return NULL;
1160 get domain sid from cache
1162 const struct dom_sid *samdb_domain_sid_cache_only(struct ldb_context *ldb)
1164 return (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1167 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1169 TALLOC_CTX *tmp_ctx;
1170 struct dom_sid *dom_sid_new;
1171 struct dom_sid *dom_sid_old;
1173 /* see if we have a cached copy */
1174 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1175 "cache.domain_sid"), struct dom_sid);
1177 tmp_ctx = talloc_new(ldb);
1178 if (tmp_ctx == NULL) {
1179 goto failed;
1182 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1183 if (!dom_sid_new) {
1184 goto failed;
1187 /* cache the domain_sid in the ldb */
1188 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1189 goto failed;
1192 talloc_steal(ldb, dom_sid_new);
1193 talloc_free(tmp_ctx);
1194 talloc_free(dom_sid_old);
1196 return true;
1198 failed:
1199 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1200 talloc_free(tmp_ctx);
1201 return false;
1204 bool samdb_set_ntds_settings_dn(struct ldb_context *ldb, struct ldb_dn *ntds_settings_dn_in)
1206 TALLOC_CTX *tmp_ctx;
1207 struct ldb_dn *ntds_settings_dn_new;
1208 struct ldb_dn *ntds_settings_dn_old;
1210 /* see if we have a forced copy from provision */
1211 ntds_settings_dn_old = talloc_get_type(ldb_get_opaque(ldb,
1212 "forced.ntds_settings_dn"), struct ldb_dn);
1214 tmp_ctx = talloc_new(ldb);
1215 if (tmp_ctx == NULL) {
1216 goto failed;
1219 ntds_settings_dn_new = ldb_dn_copy(tmp_ctx, ntds_settings_dn_in);
1220 if (!ntds_settings_dn_new) {
1221 goto failed;
1224 /* set the DN in the ldb to avoid lookups during provision */
1225 if (ldb_set_opaque(ldb, "forced.ntds_settings_dn", ntds_settings_dn_new) != LDB_SUCCESS) {
1226 goto failed;
1229 talloc_steal(ldb, ntds_settings_dn_new);
1230 talloc_free(tmp_ctx);
1231 talloc_free(ntds_settings_dn_old);
1233 return true;
1235 failed:
1236 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1237 talloc_free(tmp_ctx);
1238 return false;
1242 work out the ntds settings dn for the current open ldb
1244 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1246 TALLOC_CTX *tmp_ctx;
1247 const char *root_attrs[] = { "dsServiceName", NULL };
1248 int ret;
1249 struct ldb_result *root_res;
1250 struct ldb_dn *settings_dn;
1252 /* see if we have a cached copy */
1253 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "forced.ntds_settings_dn");
1254 if (settings_dn) {
1255 return ldb_dn_copy(mem_ctx, settings_dn);
1258 tmp_ctx = talloc_new(mem_ctx);
1259 if (tmp_ctx == NULL) {
1260 goto failed;
1263 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1264 if (ret != LDB_SUCCESS) {
1265 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1266 ldb_errstring(ldb)));
1267 goto failed;
1270 if (root_res->count != 1) {
1271 goto failed;
1274 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1276 /* note that we do not cache the DN here, as that would mean
1277 * we could not handle server renames at runtime. Only
1278 * provision sets up forced.ntds_settings_dn */
1280 talloc_steal(mem_ctx, settings_dn);
1281 talloc_free(tmp_ctx);
1283 return settings_dn;
1285 failed:
1286 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1287 talloc_free(tmp_ctx);
1288 return NULL;
1292 work out the ntds settings invocationId for the current open ldb
1294 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1296 TALLOC_CTX *tmp_ctx;
1297 const char *attrs[] = { "invocationId", NULL };
1298 int ret;
1299 struct ldb_result *res;
1300 struct GUID *invocation_id;
1302 /* see if we have a cached copy */
1303 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1304 if (invocation_id) {
1305 SMB_ASSERT(!GUID_all_zero(invocation_id));
1306 return invocation_id;
1309 tmp_ctx = talloc_new(ldb);
1310 if (tmp_ctx == NULL) {
1311 goto failed;
1314 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1315 if (ret) {
1316 goto failed;
1319 if (res->count != 1) {
1320 goto failed;
1323 invocation_id = talloc(tmp_ctx, struct GUID);
1324 if (!invocation_id) {
1325 goto failed;
1328 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1330 /* cache the domain_sid in the ldb */
1331 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1332 goto failed;
1335 talloc_steal(ldb, invocation_id);
1336 talloc_free(tmp_ctx);
1338 return invocation_id;
1340 failed:
1341 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1342 talloc_free(tmp_ctx);
1343 return NULL;
1346 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1348 TALLOC_CTX *tmp_ctx;
1349 struct GUID *invocation_id_new;
1350 struct GUID *invocation_id_old;
1352 /* see if we have a cached copy */
1353 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1354 "cache.invocation_id");
1356 tmp_ctx = talloc_new(ldb);
1357 if (tmp_ctx == NULL) {
1358 goto failed;
1361 invocation_id_new = talloc(tmp_ctx, struct GUID);
1362 if (!invocation_id_new) {
1363 goto failed;
1366 SMB_ASSERT(!GUID_all_zero(invocation_id_in));
1367 *invocation_id_new = *invocation_id_in;
1369 /* cache the domain_sid in the ldb */
1370 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1371 goto failed;
1374 talloc_steal(ldb, invocation_id_new);
1375 talloc_free(tmp_ctx);
1376 talloc_free(invocation_id_old);
1378 return true;
1380 failed:
1381 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1382 talloc_free(tmp_ctx);
1383 return false;
1387 work out the ntds settings objectGUID for the current open ldb
1389 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1391 TALLOC_CTX *tmp_ctx;
1392 const char *attrs[] = { "objectGUID", NULL };
1393 int ret;
1394 struct ldb_result *res;
1395 struct GUID *ntds_guid;
1397 /* see if we have a cached copy */
1398 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1399 if (ntds_guid) {
1400 return ntds_guid;
1403 tmp_ctx = talloc_new(ldb);
1404 if (tmp_ctx == NULL) {
1405 goto failed;
1408 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1409 if (ret) {
1410 goto failed;
1413 if (res->count != 1) {
1414 goto failed;
1417 ntds_guid = talloc(tmp_ctx, struct GUID);
1418 if (!ntds_guid) {
1419 goto failed;
1422 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1424 /* cache the domain_sid in the ldb */
1425 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1426 goto failed;
1429 talloc_steal(ldb, ntds_guid);
1430 talloc_free(tmp_ctx);
1432 return ntds_guid;
1434 failed:
1435 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1436 talloc_free(tmp_ctx);
1437 return NULL;
1440 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1442 TALLOC_CTX *tmp_ctx;
1443 struct GUID *ntds_guid_new;
1444 struct GUID *ntds_guid_old;
1446 /* see if we have a cached copy */
1447 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1449 tmp_ctx = talloc_new(ldb);
1450 if (tmp_ctx == NULL) {
1451 goto failed;
1454 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1455 if (!ntds_guid_new) {
1456 goto failed;
1459 *ntds_guid_new = *ntds_guid_in;
1461 /* cache the domain_sid in the ldb */
1462 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1463 goto failed;
1466 talloc_steal(ldb, ntds_guid_new);
1467 talloc_free(tmp_ctx);
1468 talloc_free(ntds_guid_old);
1470 return true;
1472 failed:
1473 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1474 talloc_free(tmp_ctx);
1475 return false;
1479 work out the server dn for the current open ldb
1481 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1483 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1484 struct ldb_dn *dn;
1485 if (!tmp_ctx) {
1486 return NULL;
1488 dn = ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb, tmp_ctx));
1489 talloc_free(tmp_ctx);
1490 return dn;
1495 work out the server dn for the current open ldb
1497 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1499 struct ldb_dn *server_dn;
1500 struct ldb_dn *servers_dn;
1501 struct ldb_dn *server_site_dn;
1503 /* TODO: there must be a saner way to do this!! */
1504 server_dn = samdb_server_dn(ldb, mem_ctx);
1505 if (!server_dn) return NULL;
1507 servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1508 talloc_free(server_dn);
1509 if (!servers_dn) return NULL;
1511 server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1512 talloc_free(servers_dn);
1514 return server_site_dn;
1518 find the site name from a computers DN record
1520 int samdb_find_site_for_computer(struct ldb_context *ldb,
1521 TALLOC_CTX *mem_ctx, struct ldb_dn *computer_dn,
1522 const char **site_name)
1524 int ret;
1525 struct ldb_dn *dn;
1526 const struct ldb_val *rdn_val;
1528 *site_name = NULL;
1530 ret = samdb_reference_dn(ldb, mem_ctx, computer_dn, "serverReferenceBL", &dn);
1531 if (ret != LDB_SUCCESS) {
1532 return ret;
1535 if (!ldb_dn_remove_child_components(dn, 2)) {
1536 talloc_free(dn);
1537 return LDB_ERR_INVALID_DN_SYNTAX;
1540 rdn_val = ldb_dn_get_rdn_val(dn);
1541 if (rdn_val == NULL) {
1542 return LDB_ERR_OPERATIONS_ERROR;
1545 (*site_name) = talloc_strndup(mem_ctx, (const char *)rdn_val->data, rdn_val->length);
1546 talloc_free(dn);
1547 if (!*site_name) {
1548 return LDB_ERR_OPERATIONS_ERROR;
1550 return LDB_SUCCESS;
1554 find the NTDS GUID from a computers DN record
1556 int samdb_find_ntdsguid_for_computer(struct ldb_context *ldb, struct ldb_dn *computer_dn,
1557 struct GUID *ntds_guid)
1559 int ret;
1560 struct ldb_dn *dn;
1562 *ntds_guid = GUID_zero();
1564 ret = samdb_reference_dn(ldb, ldb, computer_dn, "serverReferenceBL", &dn);
1565 if (ret != LDB_SUCCESS) {
1566 return ret;
1569 if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Settings")) {
1570 talloc_free(dn);
1571 return LDB_ERR_OPERATIONS_ERROR;
1574 ret = dsdb_find_guid_by_dn(ldb, dn, ntds_guid);
1575 talloc_free(dn);
1576 return ret;
1580 find a 'reference' DN that points at another object
1581 (eg. serverReference, rIDManagerReference etc)
1583 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1584 const char *attribute, struct ldb_dn **dn)
1586 const char *attrs[2];
1587 struct ldb_result *res;
1588 int ret;
1590 attrs[0] = attribute;
1591 attrs[1] = NULL;
1593 ret = dsdb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_ONE_ONLY|DSDB_SEARCH_SHOW_EXTENDED_DN, NULL);
1594 if (ret != LDB_SUCCESS) {
1595 ldb_asprintf_errstring(ldb, "Cannot find DN %s to get attribute %s for reference dn: %s",
1596 ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb));
1597 return ret;
1600 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1601 if (!*dn) {
1602 if (!ldb_msg_find_element(res->msgs[0], attribute)) {
1603 ldb_asprintf_errstring(ldb, "Cannot find attribute %s of %s to calculate reference dn", attribute,
1604 ldb_dn_get_linearized(base));
1605 } else {
1606 ldb_asprintf_errstring(ldb, "Cannot interpret attribute %s of %s as a dn", attribute,
1607 ldb_dn_get_linearized(base));
1609 talloc_free(res);
1610 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1613 talloc_free(res);
1614 return LDB_SUCCESS;
1618 find if a DN (must have GUID component!) is our ntdsDsa
1620 int samdb_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *dn, bool *is_ntdsa)
1622 NTSTATUS status;
1623 struct GUID dn_guid;
1624 const struct GUID *our_ntds_guid;
1625 status = dsdb_get_extended_dn_guid(dn, &dn_guid, "GUID");
1626 if (!NT_STATUS_IS_OK(status)) {
1627 return LDB_ERR_OPERATIONS_ERROR;
1630 our_ntds_guid = samdb_ntds_objectGUID(ldb);
1631 if (!our_ntds_guid) {
1632 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
1633 return LDB_ERR_OPERATIONS_ERROR;
1636 *is_ntdsa = GUID_equal(&dn_guid, our_ntds_guid);
1637 return LDB_SUCCESS;
1641 find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1643 int samdb_reference_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *base,
1644 const char *attribute, bool *is_ntdsa)
1646 int ret;
1647 struct ldb_dn *referenced_dn;
1648 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1649 if (tmp_ctx == NULL) {
1650 return LDB_ERR_OPERATIONS_ERROR;
1652 ret = samdb_reference_dn(ldb, tmp_ctx, base, attribute, &referenced_dn);
1653 if (ret != LDB_SUCCESS) {
1654 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb)));
1655 return ret;
1658 ret = samdb_dn_is_our_ntdsa(ldb, referenced_dn, is_ntdsa);
1660 talloc_free(tmp_ctx);
1661 return ret;
1665 find our machine account via the serverReference attribute in the
1666 server DN
1668 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1670 struct ldb_dn *server_dn;
1671 int ret;
1673 server_dn = samdb_server_dn(ldb, mem_ctx);
1674 if (server_dn == NULL) {
1675 return LDB_ERR_NO_SUCH_OBJECT;
1678 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1679 talloc_free(server_dn);
1681 return ret;
1685 find the RID Manager$ DN via the rIDManagerReference attribute in the
1686 base DN
1688 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1690 return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1691 "rIDManagerReference", dn);
1695 find the RID Set DN via the rIDSetReferences attribute in our
1696 machine account DN
1698 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1700 struct ldb_dn *server_ref_dn;
1701 int ret;
1703 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1704 if (ret != LDB_SUCCESS) {
1705 return ret;
1707 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1708 talloc_free(server_ref_dn);
1709 return ret;
1712 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1714 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1715 mem_ctx));
1717 if (val == NULL) {
1718 return NULL;
1721 return (const char *) val->data;
1725 * Finds the client site by using the client's IP address.
1726 * The "subnet_name" returns the name of the subnet if parameter != NULL
1728 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1729 const char *ip_address, char **subnet_name)
1731 const char *attrs[] = { "cn", "siteObject", NULL };
1732 struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
1733 struct ldb_result *res;
1734 const struct ldb_val *val;
1735 const char *site_name = NULL, *l_subnet_name = NULL;
1736 const char *allow_list[2] = { NULL, NULL };
1737 unsigned int i, count;
1738 int cnt, ret;
1741 * if we don't have a client ip e.g. ncalrpc
1742 * the server site is the client site
1744 if (ip_address == NULL) {
1745 return samdb_server_site_name(ldb, mem_ctx);
1748 sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1749 if (sites_container_dn == NULL) {
1750 return NULL;
1753 subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1754 if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1755 talloc_free(sites_container_dn);
1756 talloc_free(subnets_dn);
1757 return NULL;
1760 ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1761 attrs, NULL);
1762 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1763 count = 0;
1764 } else if (ret != LDB_SUCCESS) {
1765 talloc_free(sites_container_dn);
1766 talloc_free(subnets_dn);
1767 return NULL;
1768 } else {
1769 count = res->count;
1772 for (i = 0; i < count; i++) {
1773 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1774 NULL);
1776 allow_list[0] = l_subnet_name;
1778 if (socket_allow_access(mem_ctx, NULL, allow_list, "", ip_address)) {
1779 sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1780 res->msgs[i],
1781 "siteObject");
1782 if (sites_dn == NULL) {
1783 /* No reference, maybe another subnet matches */
1784 continue;
1787 /* "val" cannot be NULL here since "sites_dn" != NULL */
1788 val = ldb_dn_get_rdn_val(sites_dn);
1789 site_name = talloc_strdup(mem_ctx,
1790 (const char *) val->data);
1792 talloc_free(sites_dn);
1794 break;
1798 if (site_name == NULL) {
1799 /* This is the Windows Server fallback rule: when no subnet
1800 * exists and we have only one site available then use it (it
1801 * is for sure the same as our server site). If more sites do
1802 * exist then we don't know which one to use and set the site
1803 * name to "". */
1804 cnt = samdb_search_count(ldb, mem_ctx, sites_container_dn,
1805 "(objectClass=site)");
1806 if (cnt == 1) {
1807 site_name = samdb_server_site_name(ldb, mem_ctx);
1808 } else {
1809 site_name = talloc_strdup(mem_ctx, "");
1811 l_subnet_name = NULL;
1814 if (subnet_name != NULL) {
1815 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1818 talloc_free(sites_container_dn);
1819 talloc_free(subnets_dn);
1820 talloc_free(res);
1822 return site_name;
1826 work out if we are the PDC for the domain of the current open ldb
1828 bool samdb_is_pdc(struct ldb_context *ldb)
1830 int ret;
1831 bool is_pdc;
1833 ret = samdb_reference_dn_is_our_ntdsa(ldb, ldb_get_default_basedn(ldb), "fsmoRoleOwner",
1834 &is_pdc);
1835 if (ret != LDB_SUCCESS) {
1836 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n",
1837 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1838 ldb_errstring(ldb)));
1839 return false;
1842 return is_pdc;
1846 work out if we are a Global Catalog server for the domain of the current open ldb
1848 bool samdb_is_gc(struct ldb_context *ldb)
1850 uint32_t options;
1851 if (samdb_ntds_options(ldb, &options) != LDB_SUCCESS) {
1852 return false;
1854 return (options & DS_NTDSDSA_OPT_IS_GC) != 0;
1857 /* Find a domain object in the parents of a particular DN. */
1858 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1859 struct ldb_dn **parent_dn, const char **errstring)
1861 TALLOC_CTX *local_ctx;
1862 struct ldb_dn *sdn = dn;
1863 struct ldb_result *res = NULL;
1864 int ret = LDB_SUCCESS;
1865 const char *attrs[] = { NULL };
1867 local_ctx = talloc_new(mem_ctx);
1868 if (local_ctx == NULL) return ldb_oom(ldb);
1870 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1871 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1872 "(|(objectClass=domain)(objectClass=builtinDomain))");
1873 if (ret == LDB_SUCCESS) {
1874 if (res->count == 1) {
1875 break;
1877 } else {
1878 break;
1882 if (ret != LDB_SUCCESS) {
1883 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1884 ldb_dn_get_linearized(dn),
1885 ldb_dn_get_linearized(sdn),
1886 ldb_errstring(ldb));
1887 talloc_free(local_ctx);
1888 return ret;
1890 if (res->count != 1) {
1891 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1892 ldb_dn_get_linearized(dn));
1893 DEBUG(0,(__location__ ": %s\n", *errstring));
1894 talloc_free(local_ctx);
1895 return LDB_ERR_CONSTRAINT_VIOLATION;
1898 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1899 talloc_free(local_ctx);
1900 return ret;
1905 * Performs checks on a user password (plaintext UNIX format - attribute
1906 * "password"). The remaining parameters have to be extracted from the domain
1907 * object in the AD.
1909 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1911 enum samr_ValidationStatus samdb_check_password(const DATA_BLOB *utf8_blob,
1912 const uint32_t pwdProperties,
1913 const uint32_t minPwdLength)
1915 const char *utf8_pw = (const char *)utf8_blob->data;
1916 size_t utf8_len = strlen_m(utf8_pw);
1918 /* checks if the "minPwdLength" property is satisfied */
1919 if (minPwdLength > utf8_len) {
1920 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
1923 /* checks the password complexity */
1924 if (!(pwdProperties & DOMAIN_PASSWORD_COMPLEX)) {
1925 return SAMR_VALIDATION_STATUS_SUCCESS;
1928 if (utf8_len == 0) {
1929 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1932 if (!check_password_quality(utf8_pw)) {
1933 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1936 return SAMR_VALIDATION_STATUS_SUCCESS;
1940 * Callback for "samdb_set_password" password change
1942 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
1944 int ret;
1946 if (!ares) {
1947 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1950 if (ares->error != LDB_SUCCESS) {
1951 ret = ares->error;
1952 req->context = talloc_steal(req,
1953 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1954 talloc_free(ares);
1955 return ldb_request_done(req, ret);
1958 if (ares->type != LDB_REPLY_DONE) {
1959 talloc_free(ares);
1960 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1963 req->context = talloc_steal(req,
1964 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1965 talloc_free(ares);
1966 return ldb_request_done(req, LDB_SUCCESS);
1970 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1971 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1972 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
1973 * user change or not. The "rejectReason" gives some more information if the
1974 * change failed.
1976 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
1977 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
1979 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1980 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
1981 const DATA_BLOB *new_password,
1982 const struct samr_Password *lmNewHash,
1983 const struct samr_Password *ntNewHash,
1984 const struct samr_Password *lmOldHash,
1985 const struct samr_Password *ntOldHash,
1986 enum samPwdChangeReason *reject_reason,
1987 struct samr_DomInfo1 **_dominfo)
1989 struct ldb_message *msg;
1990 struct ldb_message_element *el;
1991 struct ldb_request *req;
1992 struct dsdb_control_password_change_status *pwd_stat = NULL;
1993 int ret;
1994 bool hash_values = false;
1995 NTSTATUS status = NT_STATUS_OK;
1997 #define CHECK_RET(x) \
1998 if (x != LDB_SUCCESS) { \
1999 talloc_free(msg); \
2000 return NT_STATUS_NO_MEMORY; \
2003 msg = ldb_msg_new(mem_ctx);
2004 if (msg == NULL) {
2005 return NT_STATUS_NO_MEMORY;
2007 msg->dn = user_dn;
2008 if ((new_password != NULL)
2009 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
2010 /* we have the password as plaintext UTF16 */
2011 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
2012 new_password, NULL));
2013 el = ldb_msg_find_element(msg, "clearTextPassword");
2014 el->flags = LDB_FLAG_MOD_REPLACE;
2015 } else if ((new_password == NULL)
2016 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2017 /* we have a password as LM and/or NT hash */
2018 if (lmNewHash != NULL) {
2019 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2020 "dBCSPwd", lmNewHash));
2021 el = ldb_msg_find_element(msg, "dBCSPwd");
2022 el->flags = LDB_FLAG_MOD_REPLACE;
2024 if (ntNewHash != NULL) {
2025 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2026 "unicodePwd", ntNewHash));
2027 el = ldb_msg_find_element(msg, "unicodePwd");
2028 el->flags = LDB_FLAG_MOD_REPLACE;
2030 hash_values = true;
2031 } else {
2032 /* the password wasn't specified correctly */
2033 talloc_free(msg);
2034 return NT_STATUS_INVALID_PARAMETER;
2037 /* build modify request */
2038 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2039 samdb_set_password_callback, NULL);
2040 if (ret != LDB_SUCCESS) {
2041 talloc_free(msg);
2042 return NT_STATUS_NO_MEMORY;
2045 /* A password change operation */
2046 if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2047 struct dsdb_control_password_change *change;
2049 change = talloc(req, struct dsdb_control_password_change);
2050 if (change == NULL) {
2051 talloc_free(req);
2052 talloc_free(msg);
2053 return NT_STATUS_NO_MEMORY;
2056 change->old_nt_pwd_hash = ntOldHash;
2057 change->old_lm_pwd_hash = lmOldHash;
2059 ret = ldb_request_add_control(req,
2060 DSDB_CONTROL_PASSWORD_CHANGE_OID,
2061 true, change);
2062 if (ret != LDB_SUCCESS) {
2063 talloc_free(req);
2064 talloc_free(msg);
2065 return NT_STATUS_NO_MEMORY;
2068 if (hash_values) {
2069 ret = ldb_request_add_control(req,
2070 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2071 true, NULL);
2072 if (ret != LDB_SUCCESS) {
2073 talloc_free(req);
2074 talloc_free(msg);
2075 return NT_STATUS_NO_MEMORY;
2078 ret = ldb_request_add_control(req,
2079 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2080 true, NULL);
2081 if (ret != LDB_SUCCESS) {
2082 talloc_free(req);
2083 talloc_free(msg);
2084 return NT_STATUS_NO_MEMORY;
2087 ret = dsdb_autotransaction_request(ldb, req);
2089 if (req->context != NULL) {
2090 pwd_stat = talloc_steal(mem_ctx,
2091 ((struct ldb_control *)req->context)->data);
2094 talloc_free(req);
2095 talloc_free(msg);
2097 /* Sets the domain info (if requested) */
2098 if (_dominfo != NULL) {
2099 struct samr_DomInfo1 *dominfo;
2101 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2102 if (dominfo == NULL) {
2103 return NT_STATUS_NO_MEMORY;
2106 if (pwd_stat != NULL) {
2107 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2108 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2109 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2110 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2111 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2114 *_dominfo = dominfo;
2117 if (reject_reason != NULL) {
2118 if (pwd_stat != NULL) {
2119 *reject_reason = pwd_stat->reject_reason;
2120 } else {
2121 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2125 if (pwd_stat != NULL) {
2126 talloc_free(pwd_stat);
2129 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2130 const char *errmsg = ldb_errstring(ldb);
2131 char *endptr = NULL;
2132 WERROR werr = WERR_GENERAL_FAILURE;
2133 status = NT_STATUS_UNSUCCESSFUL;
2134 if (errmsg != NULL) {
2135 werr = W_ERROR(strtol(errmsg, &endptr, 16));
2137 if (endptr != errmsg) {
2138 if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2139 status = NT_STATUS_WRONG_PASSWORD;
2141 if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2142 status = NT_STATUS_PASSWORD_RESTRICTION;
2145 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2146 /* don't let the caller know if an account doesn't exist */
2147 status = NT_STATUS_WRONG_PASSWORD;
2148 } else if (ret != LDB_SUCCESS) {
2149 status = NT_STATUS_UNSUCCESSFUL;
2152 return status;
2156 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2157 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2158 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2159 * user change or not. The "rejectReason" gives some more information if the
2160 * change failed.
2162 * This wrapper function for "samdb_set_password" takes a SID as input rather
2163 * than a user DN.
2165 * This call encapsulates a new LDB transaction for changing the password;
2166 * therefore the user hasn't to start a new one.
2168 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2169 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2170 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2171 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2173 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2174 const struct dom_sid *user_sid,
2175 const DATA_BLOB *new_password,
2176 const struct samr_Password *lmNewHash,
2177 const struct samr_Password *ntNewHash,
2178 const struct samr_Password *lmOldHash,
2179 const struct samr_Password *ntOldHash,
2180 enum samPwdChangeReason *reject_reason,
2181 struct samr_DomInfo1 **_dominfo)
2183 NTSTATUS nt_status;
2184 struct ldb_dn *user_dn;
2185 int ret;
2187 ret = ldb_transaction_start(ldb);
2188 if (ret != LDB_SUCCESS) {
2189 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2190 return NT_STATUS_TRANSACTION_ABORTED;
2193 user_dn = samdb_search_dn(ldb, mem_ctx, NULL,
2194 "(&(objectSid=%s)(objectClass=user))",
2195 ldap_encode_ndr_dom_sid(mem_ctx, user_sid));
2196 if (!user_dn) {
2197 ldb_transaction_cancel(ldb);
2198 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2199 dom_sid_string(mem_ctx, user_sid)));
2200 return NT_STATUS_NO_SUCH_USER;
2203 nt_status = samdb_set_password(ldb, mem_ctx,
2204 user_dn, NULL,
2205 new_password,
2206 lmNewHash, ntNewHash,
2207 lmOldHash, ntOldHash,
2208 reject_reason, _dominfo);
2209 if (!NT_STATUS_IS_OK(nt_status)) {
2210 ldb_transaction_cancel(ldb);
2211 talloc_free(user_dn);
2212 return nt_status;
2215 ret = ldb_transaction_commit(ldb);
2216 if (ret != LDB_SUCCESS) {
2217 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2218 ldb_dn_get_linearized(user_dn),
2219 ldb_errstring(ldb)));
2220 talloc_free(user_dn);
2221 return NT_STATUS_TRANSACTION_ABORTED;
2224 talloc_free(user_dn);
2225 return NT_STATUS_OK;
2229 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2230 struct dom_sid *sid, struct ldb_dn **ret_dn)
2232 struct ldb_message *msg;
2233 struct ldb_dn *basedn;
2234 char *sidstr;
2235 int ret;
2237 sidstr = dom_sid_string(mem_ctx, sid);
2238 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2240 /* We might have to create a ForeignSecurityPrincipal, even if this user
2241 * is in our own domain */
2243 msg = ldb_msg_new(sidstr);
2244 if (msg == NULL) {
2245 talloc_free(sidstr);
2246 return NT_STATUS_NO_MEMORY;
2249 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2250 ldb_get_default_basedn(sam_ctx),
2251 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2252 &basedn);
2253 if (ret != LDB_SUCCESS) {
2254 DEBUG(0, ("Failed to find DN for "
2255 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2256 talloc_free(sidstr);
2257 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2260 /* add core elements to the ldb_message for the alias */
2261 msg->dn = basedn;
2262 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2263 talloc_free(sidstr);
2264 return NT_STATUS_NO_MEMORY;
2267 ret = ldb_msg_add_string(msg, "objectClass",
2268 "foreignSecurityPrincipal");
2269 if (ret != LDB_SUCCESS) {
2270 talloc_free(sidstr);
2271 return NT_STATUS_NO_MEMORY;
2274 /* create the alias */
2275 ret = ldb_add(sam_ctx, msg);
2276 if (ret != LDB_SUCCESS) {
2277 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2278 "record %s: %s\n",
2279 ldb_dn_get_linearized(msg->dn),
2280 ldb_errstring(sam_ctx)));
2281 talloc_free(sidstr);
2282 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2285 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2286 talloc_free(sidstr);
2288 return NT_STATUS_OK;
2293 Find the DN of a domain, assuming it to be a dotted.dns name
2296 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2298 unsigned int i;
2299 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2300 const char *binary_encoded;
2301 const char * const *split_realm;
2302 struct ldb_dn *dn;
2304 if (!tmp_ctx) {
2305 return NULL;
2308 split_realm = (const char * const *)str_list_make(tmp_ctx, dns_domain, ".");
2309 if (!split_realm) {
2310 talloc_free(tmp_ctx);
2311 return NULL;
2313 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2314 for (i=0; split_realm[i]; i++) {
2315 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2316 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2317 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2318 binary_encoded, ldb_dn_get_linearized(dn)));
2319 talloc_free(tmp_ctx);
2320 return NULL;
2323 if (!ldb_dn_validate(dn)) {
2324 DEBUG(2, ("Failed to validated DN %s\n",
2325 ldb_dn_get_linearized(dn)));
2326 talloc_free(tmp_ctx);
2327 return NULL;
2329 talloc_free(tmp_ctx);
2330 return dn;
2335 Find the DNS equivalent of a DN, in dotted DNS form
2337 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2339 int i, num_components = ldb_dn_get_comp_num(dn);
2340 char *dns_name = talloc_strdup(mem_ctx, "");
2341 if (dns_name == NULL) {
2342 return NULL;
2345 for (i=0; i<num_components; i++) {
2346 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2347 char *s;
2348 if (v == NULL) {
2349 talloc_free(dns_name);
2350 return NULL;
2352 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2353 (int)v->length, (int)v->length, (char *)v->data);
2354 if (s == NULL) {
2355 talloc_free(dns_name);
2356 return NULL;
2358 dns_name = s;
2361 /* remove the last '.' */
2362 if (dns_name[0] != 0) {
2363 dns_name[strlen(dns_name)-1] = 0;
2366 return dns_name;
2370 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2371 name is based on the forest DNS name
2373 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
2374 TALLOC_CTX *mem_ctx,
2375 const struct GUID *ntds_guid)
2377 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2378 const char *guid_str;
2379 struct ldb_dn *forest_dn;
2380 const char *dnsforest;
2381 char *ret;
2383 guid_str = GUID_string(tmp_ctx, ntds_guid);
2384 if (guid_str == NULL) {
2385 talloc_free(tmp_ctx);
2386 return NULL;
2388 forest_dn = ldb_get_root_basedn(samdb);
2389 if (forest_dn == NULL) {
2390 talloc_free(tmp_ctx);
2391 return NULL;
2393 dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
2394 if (dnsforest == NULL) {
2395 talloc_free(tmp_ctx);
2396 return NULL;
2398 ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
2399 talloc_free(tmp_ctx);
2400 return ret;
2405 Find the DN of a domain, be it the netbios or DNS name
2407 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2408 const char *domain_name)
2410 const char * const domain_ref_attrs[] = {
2411 "ncName", NULL
2413 const char * const domain_ref2_attrs[] = {
2414 NULL
2416 struct ldb_result *res_domain_ref;
2417 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2418 /* find the domain's DN */
2419 int ret_domain = ldb_search(ldb, mem_ctx,
2420 &res_domain_ref,
2421 samdb_partitions_dn(ldb, mem_ctx),
2422 LDB_SCOPE_ONELEVEL,
2423 domain_ref_attrs,
2424 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2425 escaped_domain);
2426 if (ret_domain != LDB_SUCCESS) {
2427 return NULL;
2430 if (res_domain_ref->count == 0) {
2431 ret_domain = ldb_search(ldb, mem_ctx,
2432 &res_domain_ref,
2433 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2434 LDB_SCOPE_BASE,
2435 domain_ref2_attrs,
2436 "(objectclass=domain)");
2437 if (ret_domain != LDB_SUCCESS) {
2438 return NULL;
2441 if (res_domain_ref->count == 1) {
2442 return res_domain_ref->msgs[0]->dn;
2444 return NULL;
2447 if (res_domain_ref->count > 1) {
2448 DEBUG(0,("Found %d records matching domain [%s]\n",
2449 ret_domain, domain_name));
2450 return NULL;
2453 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
2459 use a GUID to find a DN
2461 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
2462 TALLOC_CTX *mem_ctx,
2463 const struct GUID *guid,
2464 uint32_t dsdb_flags,
2465 struct ldb_dn **dn)
2467 int ret;
2468 struct ldb_result *res;
2469 const char *attrs[] = { NULL };
2470 char *guid_str = GUID_string(mem_ctx, guid);
2472 if (!guid_str) {
2473 return ldb_operr(ldb);
2476 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2477 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2478 DSDB_SEARCH_SHOW_EXTENDED_DN |
2479 DSDB_SEARCH_ONE_ONLY | dsdb_flags,
2480 "objectGUID=%s", guid_str);
2481 talloc_free(guid_str);
2482 if (ret != LDB_SUCCESS) {
2483 return ret;
2486 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2487 talloc_free(res);
2489 return LDB_SUCCESS;
2493 use a DN to find a GUID with a given attribute name
2495 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
2496 struct ldb_dn *dn, const char *attribute,
2497 struct GUID *guid)
2499 int ret;
2500 struct ldb_result *res;
2501 const char *attrs[2];
2502 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2504 attrs[0] = attribute;
2505 attrs[1] = NULL;
2507 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2508 DSDB_SEARCH_SHOW_DELETED |
2509 DSDB_SEARCH_SHOW_RECYCLED);
2510 if (ret != LDB_SUCCESS) {
2511 talloc_free(tmp_ctx);
2512 return ret;
2514 if (res->count < 1) {
2515 talloc_free(tmp_ctx);
2516 return LDB_ERR_NO_SUCH_OBJECT;
2518 *guid = samdb_result_guid(res->msgs[0], attribute);
2519 talloc_free(tmp_ctx);
2520 return LDB_SUCCESS;
2524 use a DN to find a GUID
2526 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
2527 struct ldb_dn *dn, struct GUID *guid)
2529 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
2535 adds the given GUID to the given ldb_message. This value is added
2536 for the given attr_name (may be either "objectGUID" or "parentGUID").
2538 int dsdb_msg_add_guid(struct ldb_message *msg,
2539 struct GUID *guid,
2540 const char *attr_name)
2542 int ret;
2543 struct ldb_val v;
2544 NTSTATUS status;
2545 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
2547 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
2548 if (!NT_STATUS_IS_OK(status)) {
2549 ret = LDB_ERR_OPERATIONS_ERROR;
2550 goto done;
2553 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
2554 if (ret != LDB_SUCCESS) {
2555 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
2556 attr_name));
2557 goto done;
2560 ret = LDB_SUCCESS;
2562 done:
2563 talloc_free(tmp_ctx);
2564 return ret;
2570 use a DN to find a SID
2572 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
2573 struct ldb_dn *dn, struct dom_sid *sid)
2575 int ret;
2576 struct ldb_result *res;
2577 const char *attrs[] = { "objectSid", NULL };
2578 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2579 struct dom_sid *s;
2581 ZERO_STRUCTP(sid);
2583 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2584 DSDB_SEARCH_SHOW_DELETED |
2585 DSDB_SEARCH_SHOW_RECYCLED);
2586 if (ret != LDB_SUCCESS) {
2587 talloc_free(tmp_ctx);
2588 return ret;
2590 if (res->count < 1) {
2591 talloc_free(tmp_ctx);
2592 return LDB_ERR_NO_SUCH_OBJECT;
2594 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
2595 if (s == NULL) {
2596 talloc_free(tmp_ctx);
2597 return LDB_ERR_NO_SUCH_OBJECT;
2599 *sid = *s;
2600 talloc_free(tmp_ctx);
2601 return LDB_SUCCESS;
2605 use a SID to find a DN
2607 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
2608 TALLOC_CTX *mem_ctx,
2609 struct dom_sid *sid, struct ldb_dn **dn)
2611 int ret;
2612 struct ldb_result *res;
2613 const char *attrs[] = { NULL };
2614 char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
2616 if (!sid_str) {
2617 return ldb_operr(ldb);
2620 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2621 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2622 DSDB_SEARCH_SHOW_EXTENDED_DN |
2623 DSDB_SEARCH_ONE_ONLY,
2624 "objectSid=%s", sid_str);
2625 talloc_free(sid_str);
2626 if (ret != LDB_SUCCESS) {
2627 return ret;
2630 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2631 talloc_free(res);
2633 return LDB_SUCCESS;
2637 load a repsFromTo blob list for a given partition GUID
2638 attr must be "repsFrom" or "repsTo"
2640 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2641 const char *attr, struct repsFromToBlob **r, uint32_t *count)
2643 const char *attrs[] = { attr, NULL };
2644 struct ldb_result *res = NULL;
2645 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2646 unsigned int i;
2647 struct ldb_message_element *el;
2648 int ret;
2650 *r = NULL;
2651 *count = 0;
2653 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
2654 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2655 /* partition hasn't been replicated yet */
2656 return WERR_OK;
2658 if (ret != LDB_SUCCESS) {
2659 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
2660 talloc_free(tmp_ctx);
2661 return WERR_DS_DRA_INTERNAL_ERROR;
2664 el = ldb_msg_find_element(res->msgs[0], attr);
2665 if (el == NULL) {
2666 /* it's OK to be empty */
2667 talloc_free(tmp_ctx);
2668 return WERR_OK;
2671 *count = el->num_values;
2672 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
2673 if (*r == NULL) {
2674 talloc_free(tmp_ctx);
2675 return WERR_DS_DRA_INTERNAL_ERROR;
2678 for (i=0; i<(*count); i++) {
2679 enum ndr_err_code ndr_err;
2680 ndr_err = ndr_pull_struct_blob(&el->values[i],
2681 mem_ctx,
2682 &(*r)[i],
2683 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
2684 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2685 talloc_free(tmp_ctx);
2686 return WERR_DS_DRA_INTERNAL_ERROR;
2690 talloc_free(tmp_ctx);
2692 return WERR_OK;
2696 save the repsFromTo blob list for a given partition GUID
2697 attr must be "repsFrom" or "repsTo"
2699 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2700 const char *attr, struct repsFromToBlob *r, uint32_t count)
2702 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2703 struct ldb_message *msg;
2704 struct ldb_message_element *el;
2705 unsigned int i;
2707 msg = ldb_msg_new(tmp_ctx);
2708 msg->dn = dn;
2709 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
2710 goto failed;
2713 el->values = talloc_array(msg, struct ldb_val, count);
2714 if (!el->values) {
2715 goto failed;
2718 for (i=0; i<count; i++) {
2719 struct ldb_val v;
2720 enum ndr_err_code ndr_err;
2722 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
2723 &r[i],
2724 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
2725 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2726 goto failed;
2729 el->num_values++;
2730 el->values[i] = v;
2733 if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
2734 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
2735 goto failed;
2738 talloc_free(tmp_ctx);
2740 return WERR_OK;
2742 failed:
2743 talloc_free(tmp_ctx);
2744 return WERR_DS_DRA_INTERNAL_ERROR;
2749 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2750 object for a partition
2752 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
2753 uint64_t *uSN, uint64_t *urgent_uSN)
2755 struct ldb_request *req;
2756 int ret;
2757 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2758 struct dsdb_control_current_partition *p_ctrl;
2759 struct ldb_result *res;
2761 res = talloc_zero(tmp_ctx, struct ldb_result);
2762 if (!res) {
2763 talloc_free(tmp_ctx);
2764 return ldb_oom(ldb);
2767 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
2768 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
2769 LDB_SCOPE_BASE,
2770 NULL, NULL,
2771 NULL,
2772 res, ldb_search_default_callback,
2773 NULL);
2774 if (ret != LDB_SUCCESS) {
2775 talloc_free(tmp_ctx);
2776 return ret;
2779 p_ctrl = talloc(req, struct dsdb_control_current_partition);
2780 if (p_ctrl == NULL) {
2781 talloc_free(tmp_ctx);
2782 return ldb_oom(ldb);
2784 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2785 p_ctrl->dn = dn;
2787 ret = ldb_request_add_control(req,
2788 DSDB_CONTROL_CURRENT_PARTITION_OID,
2789 false, p_ctrl);
2790 if (ret != LDB_SUCCESS) {
2791 talloc_free(tmp_ctx);
2792 return ret;
2795 /* Run the new request */
2796 ret = ldb_request(ldb, req);
2798 if (ret == LDB_SUCCESS) {
2799 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2802 if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
2803 /* it hasn't been created yet, which means
2804 an implicit value of zero */
2805 *uSN = 0;
2806 talloc_free(tmp_ctx);
2807 return LDB_SUCCESS;
2810 if (ret != LDB_SUCCESS) {
2811 talloc_free(tmp_ctx);
2812 return ret;
2815 if (res->count < 1) {
2816 *uSN = 0;
2817 if (urgent_uSN) {
2818 *urgent_uSN = 0;
2820 } else {
2821 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
2822 if (urgent_uSN) {
2823 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
2827 talloc_free(tmp_ctx);
2829 return LDB_SUCCESS;
2832 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
2833 const struct drsuapi_DsReplicaCursor2 *c2)
2835 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2838 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
2839 const struct drsuapi_DsReplicaCursor *c2)
2841 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2846 see if a computer identified by its invocationId is a RODC
2848 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
2850 /* 1) find the DN for this servers NTDSDSA object
2851 2) search for the msDS-isRODC attribute
2852 3) if not present then not a RODC
2853 4) if present and TRUE then is a RODC
2855 struct ldb_dn *config_dn;
2856 const char *attrs[] = { "msDS-isRODC", NULL };
2857 int ret;
2858 struct ldb_result *res;
2859 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
2861 config_dn = ldb_get_config_basedn(sam_ctx);
2862 if (!config_dn) {
2863 talloc_free(tmp_ctx);
2864 return ldb_operr(sam_ctx);
2867 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
2868 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
2870 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2871 *is_rodc = false;
2872 talloc_free(tmp_ctx);
2873 return LDB_SUCCESS;
2876 if (ret != LDB_SUCCESS) {
2877 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
2878 GUID_string(tmp_ctx, objectGUID)));
2879 *is_rodc = false;
2880 talloc_free(tmp_ctx);
2881 return ret;
2884 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
2885 *is_rodc = (ret == 1);
2887 talloc_free(tmp_ctx);
2888 return LDB_SUCCESS;
2893 see if we are a RODC
2895 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
2897 const struct GUID *objectGUID;
2898 int ret;
2899 bool *cached;
2901 /* see if we have a cached copy */
2902 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
2903 if (cached) {
2904 *am_rodc = *cached;
2905 return LDB_SUCCESS;
2908 objectGUID = samdb_ntds_objectGUID(sam_ctx);
2909 if (!objectGUID) {
2910 return ldb_operr(sam_ctx);
2913 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
2914 if (ret != LDB_SUCCESS) {
2915 return ret;
2918 cached = talloc(sam_ctx, bool);
2919 if (cached == NULL) {
2920 return ldb_oom(sam_ctx);
2922 *cached = *am_rodc;
2924 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
2925 if (ret != LDB_SUCCESS) {
2926 talloc_free(cached);
2927 return ldb_operr(sam_ctx);
2930 return LDB_SUCCESS;
2933 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
2935 TALLOC_CTX *tmp_ctx;
2936 bool *cached;
2938 tmp_ctx = talloc_new(ldb);
2939 if (tmp_ctx == NULL) {
2940 goto failed;
2943 cached = talloc(tmp_ctx, bool);
2944 if (!cached) {
2945 goto failed;
2948 *cached = am_rodc;
2949 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
2950 goto failed;
2953 talloc_steal(ldb, cached);
2954 talloc_free(tmp_ctx);
2955 return true;
2957 failed:
2958 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
2959 talloc_free(tmp_ctx);
2960 return false;
2965 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
2966 * flags are DS_NTDSSETTINGS_OPT_*
2968 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
2969 uint32_t *options)
2971 int rc;
2972 TALLOC_CTX *tmp_ctx;
2973 struct ldb_result *res;
2974 struct ldb_dn *site_dn;
2975 const char *attrs[] = { "options", NULL };
2977 tmp_ctx = talloc_new(ldb_ctx);
2978 if (tmp_ctx == NULL)
2979 goto failed;
2981 /* Retrieve the site dn for the ldb that we
2982 * have open. This is our local site.
2984 site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
2985 if (site_dn == NULL)
2986 goto failed;
2988 /* Perform a one level (child) search from the local
2989 * site distinguided name. We're looking for the
2990 * "options" attribute within the nTDSSiteSettings
2991 * object
2993 rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
2994 LDB_SCOPE_ONELEVEL, attrs,
2995 "objectClass=nTDSSiteSettings");
2997 if (rc != LDB_SUCCESS || res->count != 1)
2998 goto failed;
3000 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3002 talloc_free(tmp_ctx);
3004 return LDB_SUCCESS;
3006 failed:
3007 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3008 talloc_free(tmp_ctx);
3009 return LDB_ERR_NO_SUCH_OBJECT;
3013 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3015 flags are DS_NTDS_OPTION_*
3017 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
3019 TALLOC_CTX *tmp_ctx;
3020 const char *attrs[] = { "options", NULL };
3021 int ret;
3022 struct ldb_result *res;
3024 tmp_ctx = talloc_new(ldb);
3025 if (tmp_ctx == NULL) {
3026 goto failed;
3029 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3030 if (ret != LDB_SUCCESS) {
3031 goto failed;
3034 if (res->count != 1) {
3035 goto failed;
3038 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3040 talloc_free(tmp_ctx);
3042 return LDB_SUCCESS;
3044 failed:
3045 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3046 talloc_free(tmp_ctx);
3047 return LDB_ERR_NO_SUCH_OBJECT;
3050 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3052 const char *attrs[] = { "objectCategory", NULL };
3053 int ret;
3054 struct ldb_result *res;
3056 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3057 if (ret != LDB_SUCCESS) {
3058 goto failed;
3061 if (res->count != 1) {
3062 goto failed;
3065 return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3067 failed:
3068 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3069 return NULL;
3073 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3074 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3076 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3078 char **tokens, *ret;
3079 size_t i;
3081 tokens = str_list_make(mem_ctx, cn, " -_");
3082 if (tokens == NULL)
3083 return NULL;
3085 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3086 tokens[0][0] = tolower(tokens[0][0]);
3087 for (i = 1; i < str_list_length((const char * const *)tokens); i++)
3088 tokens[i][0] = toupper(tokens[i][0]);
3090 ret = talloc_strdup(mem_ctx, tokens[0]);
3091 for (i = 1; i < str_list_length((const char * const *)tokens); i++)
3092 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3094 talloc_free(tokens);
3096 return ret;
3100 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3102 int dsdb_functional_level(struct ldb_context *ldb)
3104 int *domainFunctionality =
3105 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3106 if (!domainFunctionality) {
3107 /* this is expected during initial provision */
3108 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3109 return DS_DOMAIN_FUNCTION_2000;
3111 return *domainFunctionality;
3115 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3117 int dsdb_forest_functional_level(struct ldb_context *ldb)
3119 int *forestFunctionality =
3120 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3121 if (!forestFunctionality) {
3122 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3123 return DS_DOMAIN_FUNCTION_2000;
3125 return *forestFunctionality;
3129 set a GUID in an extended DN structure
3131 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3133 struct ldb_val v;
3134 NTSTATUS status;
3135 int ret;
3137 status = GUID_to_ndr_blob(guid, dn, &v);
3138 if (!NT_STATUS_IS_OK(status)) {
3139 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3142 ret = ldb_dn_set_extended_component(dn, component_name, &v);
3143 data_blob_free(&v);
3144 return ret;
3148 return a GUID from a extended DN structure
3150 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3152 const struct ldb_val *v;
3154 v = ldb_dn_get_extended_component(dn, component_name);
3155 if (v == NULL) {
3156 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3159 return GUID_from_ndr_blob(v, guid);
3163 return a uint64_t from a extended DN structure
3165 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3167 const struct ldb_val *v;
3168 char *s;
3170 v = ldb_dn_get_extended_component(dn, component_name);
3171 if (v == NULL) {
3172 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3174 s = talloc_strndup(dn, (const char *)v->data, v->length);
3175 NT_STATUS_HAVE_NO_MEMORY(s);
3177 *val = strtoull(s, NULL, 0);
3179 talloc_free(s);
3180 return NT_STATUS_OK;
3184 return a NTTIME from a extended DN structure
3186 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3188 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3192 return a uint32_t from a extended DN structure
3194 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3196 const struct ldb_val *v;
3197 char *s;
3199 v = ldb_dn_get_extended_component(dn, component_name);
3200 if (v == NULL) {
3201 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3204 s = talloc_strndup(dn, (const char *)v->data, v->length);
3205 NT_STATUS_HAVE_NO_MEMORY(s);
3207 *val = strtoul(s, NULL, 0);
3209 talloc_free(s);
3210 return NT_STATUS_OK;
3214 return a dom_sid from a extended DN structure
3216 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3218 const struct ldb_val *sid_blob;
3219 struct TALLOC_CTX *tmp_ctx;
3220 enum ndr_err_code ndr_err;
3222 sid_blob = ldb_dn_get_extended_component(dn, component_name);
3223 if (!sid_blob) {
3224 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3227 tmp_ctx = talloc_new(NULL);
3229 ndr_err = ndr_pull_struct_blob_all(sid_blob, tmp_ctx, sid,
3230 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3231 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3232 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3233 talloc_free(tmp_ctx);
3234 return status;
3237 talloc_free(tmp_ctx);
3238 return NT_STATUS_OK;
3243 return RMD_FLAGS directly from a ldb_dn
3244 returns 0 if not found
3246 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3248 const struct ldb_val *v;
3249 char buf[32];
3250 v = ldb_dn_get_extended_component(dn, "RMD_FLAGS");
3251 if (!v || v->length > sizeof(buf)-1) return 0;
3252 strncpy(buf, (const char *)v->data, v->length);
3253 buf[v->length] = 0;
3254 return strtoul(buf, NULL, 10);
3258 return RMD_FLAGS directly from a ldb_val for a DN
3259 returns 0 if RMD_FLAGS is not found
3261 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3263 const char *p;
3264 uint32_t flags;
3265 char *end;
3267 if (val->length < 13) {
3268 return 0;
3270 p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3271 if (!p) {
3272 return 0;
3274 flags = strtoul(p+11, &end, 10);
3275 if (!end || *end != '>') {
3276 /* it must end in a > */
3277 return 0;
3279 return flags;
3283 return true if a ldb_val containing a DN in storage form is deleted
3285 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3287 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3291 return true if a ldb_val containing a DN in storage form is
3292 in the upgraded w2k3 linked attribute format
3294 bool dsdb_dn_is_upgraded_link_val(struct ldb_val *val)
3296 return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
3300 return a DN for a wellknown GUID
3302 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3303 struct ldb_dn *nc_root, const char *wk_guid,
3304 struct ldb_dn **wkguid_dn)
3306 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3307 const char *attrs[] = { NULL };
3308 int ret;
3309 struct ldb_dn *dn;
3310 struct ldb_result *res;
3312 /* construct the magic WKGUID DN */
3313 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3314 wk_guid, ldb_dn_get_linearized(nc_root));
3315 if (!wkguid_dn) {
3316 talloc_free(tmp_ctx);
3317 return ldb_operr(samdb);
3320 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
3321 DSDB_SEARCH_SHOW_DELETED |
3322 DSDB_SEARCH_SHOW_RECYCLED);
3323 if (ret != LDB_SUCCESS) {
3324 talloc_free(tmp_ctx);
3325 return ret;
3328 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
3329 talloc_free(tmp_ctx);
3330 return LDB_SUCCESS;
3334 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
3336 return ldb_dn_compare(*dn1, *dn2);
3340 find a NC root given a DN within the NC
3342 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3343 struct ldb_dn **nc_root)
3345 const char *root_attrs[] = { "namingContexts", NULL };
3346 TALLOC_CTX *tmp_ctx;
3347 int ret;
3348 struct ldb_message_element *el;
3349 struct ldb_result *root_res;
3350 unsigned int i;
3351 struct ldb_dn **nc_dns;
3353 tmp_ctx = talloc_new(samdb);
3354 if (tmp_ctx == NULL) {
3355 return ldb_oom(samdb);
3358 ret = ldb_search(samdb, tmp_ctx, &root_res,
3359 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
3360 if (ret != LDB_SUCCESS) {
3361 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
3362 talloc_free(tmp_ctx);
3363 return ret;
3366 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
3367 if ((el == NULL) || (el->num_values < 3)) {
3368 struct ldb_message *tmp_msg;
3370 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list."));
3372 /* This generates a temporary list of NCs in order to let the
3373 * provisioning work. */
3374 tmp_msg = ldb_msg_new(tmp_ctx);
3375 if (tmp_msg == NULL) {
3376 talloc_free(tmp_ctx);
3377 return ldb_oom(samdb);
3379 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3380 ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
3381 if (ret != LDB_SUCCESS) {
3382 talloc_free(tmp_ctx);
3383 return ret;
3385 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3386 ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
3387 if (ret != LDB_SUCCESS) {
3388 talloc_free(tmp_ctx);
3389 return ret;
3391 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3392 ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
3393 if (ret != LDB_SUCCESS) {
3394 talloc_free(tmp_ctx);
3395 return ret;
3397 el = &tmp_msg->elements[0];
3400 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
3401 if (!nc_dns) {
3402 talloc_free(tmp_ctx);
3403 return ldb_oom(samdb);
3406 for (i=0; i<el->num_values; i++) {
3407 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
3408 if (nc_dns[i] == NULL) {
3409 talloc_free(tmp_ctx);
3410 return ldb_operr(samdb);
3414 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
3416 for (i=0; i<el->num_values; i++) {
3417 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
3418 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
3419 talloc_free(tmp_ctx);
3420 return LDB_SUCCESS;
3424 talloc_free(tmp_ctx);
3425 return LDB_ERR_NO_SUCH_OBJECT;
3430 find the deleted objects DN for any object, by looking for the NC
3431 root, then looking up the wellknown GUID
3433 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
3434 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
3435 struct ldb_dn **do_dn)
3437 struct ldb_dn *nc_root;
3438 int ret;
3440 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
3441 if (ret != LDB_SUCCESS) {
3442 return ret;
3445 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
3446 talloc_free(nc_root);
3447 return ret;
3451 return the tombstoneLifetime, in days
3453 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
3455 struct ldb_dn *dn;
3456 dn = ldb_get_config_basedn(ldb);
3457 if (!dn) {
3458 return LDB_ERR_NO_SUCH_OBJECT;
3460 dn = ldb_dn_copy(ldb, dn);
3461 if (!dn) {
3462 return ldb_operr(ldb);
3464 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3465 be a wellknown GUID for this */
3466 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3467 talloc_free(dn);
3468 return ldb_operr(ldb);
3471 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
3472 talloc_free(dn);
3473 return LDB_SUCCESS;
3477 compare a ldb_val to a string case insensitively
3479 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
3481 size_t len = strlen(s);
3482 int ret;
3483 if (len > v->length) return 1;
3484 ret = strncasecmp(s, (const char *)v->data, v->length);
3485 if (ret != 0) return ret;
3486 if (v->length > len && v->data[len] != 0) {
3487 return -1;
3489 return 0;
3494 load the UDV for a partition in v2 format
3495 The list is returned sorted, and with our local cursor added
3497 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3498 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
3500 static const char *attrs[] = { "replUpToDateVector", NULL };
3501 struct ldb_result *r;
3502 const struct ldb_val *ouv_value;
3503 unsigned int i;
3504 int ret;
3505 uint64_t highest_usn = 0;
3506 const struct GUID *our_invocation_id;
3507 static const struct timeval tv1970;
3508 NTTIME nt1970 = timeval_to_nttime(&tv1970);
3510 ret = ldb_search(samdb, mem_ctx, &r, dn, LDB_SCOPE_BASE, attrs, NULL);
3511 if (ret != LDB_SUCCESS) {
3512 return ret;
3515 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
3516 if (ouv_value) {
3517 enum ndr_err_code ndr_err;
3518 struct replUpToDateVectorBlob ouv;
3520 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
3521 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
3522 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3523 talloc_free(r);
3524 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3526 if (ouv.version != 2) {
3527 /* we always store as version 2, and
3528 * replUpToDateVector is not replicated
3530 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3533 *count = ouv.ctr.ctr2.count;
3534 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
3535 } else {
3536 *count = 0;
3537 *cursors = NULL;
3540 talloc_free(r);
3542 our_invocation_id = samdb_ntds_invocation_id(samdb);
3543 if (!our_invocation_id) {
3544 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
3545 talloc_free(*cursors);
3546 return ldb_operr(samdb);
3549 ret = ldb_sequence_number(samdb, LDB_SEQ_HIGHEST_SEQ, &highest_usn);
3550 if (ret != LDB_SUCCESS) {
3551 /* nothing to add - this can happen after a vampire */
3552 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3553 return LDB_SUCCESS;
3556 for (i=0; i<*count; i++) {
3557 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
3558 (*cursors)[i].highest_usn = highest_usn;
3559 (*cursors)[i].last_sync_success = nt1970;
3560 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3561 return LDB_SUCCESS;
3565 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
3566 if (! *cursors) {
3567 return ldb_oom(samdb);
3570 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
3571 (*cursors)[*count].highest_usn = highest_usn;
3572 (*cursors)[*count].last_sync_success = nt1970;
3573 (*count)++;
3575 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3577 return LDB_SUCCESS;
3581 load the UDV for a partition in version 1 format
3582 The list is returned sorted, and with our local cursor added
3584 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3585 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
3587 struct drsuapi_DsReplicaCursor2 *v2;
3588 uint32_t i;
3589 int ret;
3591 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
3592 if (ret != LDB_SUCCESS) {
3593 return ret;
3596 if (*count == 0) {
3597 talloc_free(v2);
3598 *cursors = NULL;
3599 return LDB_SUCCESS;
3602 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
3603 if (*cursors == NULL) {
3604 talloc_free(v2);
3605 return ldb_oom(samdb);
3608 for (i=0; i<*count; i++) {
3609 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
3610 (*cursors)[i].highest_usn = v2[i].highest_usn;
3612 talloc_free(v2);
3613 return LDB_SUCCESS;
3617 add a set of controls to a ldb_request structure based on a set of
3618 flags. See util.h for a list of available flags
3620 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
3622 int ret;
3623 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
3624 struct ldb_search_options_control *options;
3625 /* Using the phantom root control allows us to search all partitions */
3626 options = talloc(req, struct ldb_search_options_control);
3627 if (options == NULL) {
3628 return LDB_ERR_OPERATIONS_ERROR;
3630 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
3632 ret = ldb_request_add_control(req,
3633 LDB_CONTROL_SEARCH_OPTIONS_OID,
3634 true, options);
3635 if (ret != LDB_SUCCESS) {
3636 return ret;
3640 if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
3641 ret = ldb_request_add_control(req,
3642 DSDB_CONTROL_NO_GLOBAL_CATALOG,
3643 false, NULL);
3644 if (ret != LDB_SUCCESS) {
3645 return ret;
3649 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
3650 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
3651 if (ret != LDB_SUCCESS) {
3652 return ret;
3656 if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
3657 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
3658 if (ret != LDB_SUCCESS) {
3659 return ret;
3663 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
3664 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
3665 if (ret != LDB_SUCCESS) {
3666 return ret;
3670 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
3671 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
3672 if (!extended_ctrl) {
3673 return LDB_ERR_OPERATIONS_ERROR;
3675 extended_ctrl->type = 1;
3677 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
3678 if (ret != LDB_SUCCESS) {
3679 return ret;
3683 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
3684 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
3685 if (ret != LDB_SUCCESS) {
3686 return ret;
3690 if (dsdb_flags & DSDB_MODIFY_RELAX) {
3691 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
3692 if (ret != LDB_SUCCESS) {
3693 return ret;
3697 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
3698 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
3699 if (ret != LDB_SUCCESS) {
3700 return ret;
3704 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
3705 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
3706 if (ret != LDB_SUCCESS) {
3707 return ret;
3711 if (dsdb_flags & DSDB_TREE_DELETE) {
3712 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
3713 if (ret != LDB_SUCCESS) {
3714 return ret;
3718 if (dsdb_flags & DSDB_PROVISION) {
3719 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
3720 if (ret != LDB_SUCCESS) {
3721 return ret;
3725 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
3726 if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
3727 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
3728 if (ret != LDB_SUCCESS) {
3729 return ret;
3733 if (dsdb_flags & DSDB_PASSWORD_BYPASS_LAST_SET) {
3735 * This must not be critical, as it will only be
3736 * handled (and need to be handled) if the other
3737 * attributes in the request bring password_hash into
3738 * action
3740 ret = ldb_request_add_control(req, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID, false, NULL);
3741 if (ret != LDB_SUCCESS) {
3742 return ret;
3746 if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
3747 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
3748 if (ret != LDB_SUCCESS) {
3749 return ret;
3753 return LDB_SUCCESS;
3757 an add with a set of controls
3759 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
3760 uint32_t dsdb_flags)
3762 struct ldb_request *req;
3763 int ret;
3765 ret = ldb_build_add_req(&req, ldb, ldb,
3766 message,
3767 NULL,
3768 NULL,
3769 ldb_op_default_callback,
3770 NULL);
3772 if (ret != LDB_SUCCESS) return ret;
3774 ret = dsdb_request_add_controls(req, dsdb_flags);
3775 if (ret != LDB_SUCCESS) {
3776 talloc_free(req);
3777 return ret;
3780 ret = dsdb_autotransaction_request(ldb, req);
3782 talloc_free(req);
3783 return ret;
3787 a modify with a set of controls
3789 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
3790 uint32_t dsdb_flags)
3792 struct ldb_request *req;
3793 int ret;
3795 ret = ldb_build_mod_req(&req, ldb, ldb,
3796 message,
3797 NULL,
3798 NULL,
3799 ldb_op_default_callback,
3800 NULL);
3802 if (ret != LDB_SUCCESS) return ret;
3804 ret = dsdb_request_add_controls(req, dsdb_flags);
3805 if (ret != LDB_SUCCESS) {
3806 talloc_free(req);
3807 return ret;
3810 ret = dsdb_autotransaction_request(ldb, req);
3812 talloc_free(req);
3813 return ret;
3817 a delete with a set of flags
3819 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
3820 uint32_t dsdb_flags)
3822 struct ldb_request *req;
3823 int ret;
3825 ret = ldb_build_del_req(&req, ldb, ldb,
3827 NULL,
3828 NULL,
3829 ldb_op_default_callback,
3830 NULL);
3832 if (ret != LDB_SUCCESS) return ret;
3834 ret = dsdb_request_add_controls(req, dsdb_flags);
3835 if (ret != LDB_SUCCESS) {
3836 talloc_free(req);
3837 return ret;
3840 ret = dsdb_autotransaction_request(ldb, req);
3842 talloc_free(req);
3843 return ret;
3847 like dsdb_modify() but set all the element flags to
3848 LDB_FLAG_MOD_REPLACE
3850 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
3852 unsigned int i;
3854 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3855 for (i=0;i<msg->num_elements;i++) {
3856 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3859 return dsdb_modify(ldb, msg, dsdb_flags);
3864 search for attrs on one DN, allowing for dsdb_flags controls
3866 int dsdb_search_dn(struct ldb_context *ldb,
3867 TALLOC_CTX *mem_ctx,
3868 struct ldb_result **_result,
3869 struct ldb_dn *basedn,
3870 const char * const *attrs,
3871 uint32_t dsdb_flags)
3873 int ret;
3874 struct ldb_request *req;
3875 struct ldb_result *res;
3877 res = talloc_zero(mem_ctx, struct ldb_result);
3878 if (!res) {
3879 return ldb_oom(ldb);
3882 ret = ldb_build_search_req(&req, ldb, res,
3883 basedn,
3884 LDB_SCOPE_BASE,
3885 NULL,
3886 attrs,
3887 NULL,
3888 res,
3889 ldb_search_default_callback,
3890 NULL);
3891 if (ret != LDB_SUCCESS) {
3892 talloc_free(res);
3893 return ret;
3896 ret = dsdb_request_add_controls(req, dsdb_flags);
3897 if (ret != LDB_SUCCESS) {
3898 talloc_free(res);
3899 return ret;
3902 ret = ldb_request(ldb, req);
3903 if (ret == LDB_SUCCESS) {
3904 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3907 talloc_free(req);
3908 if (ret != LDB_SUCCESS) {
3909 talloc_free(res);
3910 return ret;
3913 *_result = res;
3914 return LDB_SUCCESS;
3918 search for attrs on one DN, by the GUID of the DN, allowing for
3919 dsdb_flags controls
3921 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
3922 TALLOC_CTX *mem_ctx,
3923 struct ldb_result **_result,
3924 const struct GUID *guid,
3925 const char * const *attrs,
3926 uint32_t dsdb_flags)
3928 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3929 struct ldb_dn *dn;
3930 int ret;
3932 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
3933 if (dn == NULL) {
3934 talloc_free(tmp_ctx);
3935 return ldb_oom(ldb);
3938 ret = dsdb_search_dn(ldb, mem_ctx, _result, dn, attrs, dsdb_flags);
3939 talloc_free(tmp_ctx);
3940 return ret;
3944 general search with dsdb_flags for controls
3946 int dsdb_search(struct ldb_context *ldb,
3947 TALLOC_CTX *mem_ctx,
3948 struct ldb_result **_result,
3949 struct ldb_dn *basedn,
3950 enum ldb_scope scope,
3951 const char * const *attrs,
3952 uint32_t dsdb_flags,
3953 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
3955 int ret;
3956 struct ldb_request *req;
3957 struct ldb_result *res;
3958 va_list ap;
3959 char *expression = NULL;
3960 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3962 /* cross-partitions searches with a basedn break multi-domain support */
3963 SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
3965 res = talloc_zero(tmp_ctx, struct ldb_result);
3966 if (!res) {
3967 talloc_free(tmp_ctx);
3968 return ldb_oom(ldb);
3971 if (exp_fmt) {
3972 va_start(ap, exp_fmt);
3973 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
3974 va_end(ap);
3976 if (!expression) {
3977 talloc_free(tmp_ctx);
3978 return ldb_oom(ldb);
3982 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3983 basedn,
3984 scope,
3985 expression,
3986 attrs,
3987 NULL,
3988 res,
3989 ldb_search_default_callback,
3990 NULL);
3991 if (ret != LDB_SUCCESS) {
3992 talloc_free(tmp_ctx);
3993 return ret;
3996 ret = dsdb_request_add_controls(req, dsdb_flags);
3997 if (ret != LDB_SUCCESS) {
3998 talloc_free(tmp_ctx);
3999 ldb_reset_err_string(ldb);
4000 return ret;
4003 ret = ldb_request(ldb, req);
4004 if (ret == LDB_SUCCESS) {
4005 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4008 if (ret != LDB_SUCCESS) {
4009 talloc_free(tmp_ctx);
4010 return ret;
4013 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
4014 if (res->count == 0) {
4015 talloc_free(tmp_ctx);
4016 ldb_reset_err_string(ldb);
4017 return LDB_ERR_NO_SUCH_OBJECT;
4019 if (res->count != 1) {
4020 talloc_free(tmp_ctx);
4021 ldb_reset_err_string(ldb);
4022 return LDB_ERR_CONSTRAINT_VIOLATION;
4026 *_result = talloc_steal(mem_ctx, res);
4027 talloc_free(tmp_ctx);
4029 return LDB_SUCCESS;
4034 general search with dsdb_flags for controls
4035 returns exactly 1 record or an error
4037 int dsdb_search_one(struct ldb_context *ldb,
4038 TALLOC_CTX *mem_ctx,
4039 struct ldb_message **msg,
4040 struct ldb_dn *basedn,
4041 enum ldb_scope scope,
4042 const char * const *attrs,
4043 uint32_t dsdb_flags,
4044 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4046 int ret;
4047 struct ldb_result *res;
4048 va_list ap;
4049 char *expression = NULL;
4050 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4052 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4054 res = talloc_zero(tmp_ctx, struct ldb_result);
4055 if (!res) {
4056 talloc_free(tmp_ctx);
4057 return ldb_oom(ldb);
4060 if (exp_fmt) {
4061 va_start(ap, exp_fmt);
4062 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4063 va_end(ap);
4065 if (!expression) {
4066 talloc_free(tmp_ctx);
4067 return ldb_oom(ldb);
4069 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4070 dsdb_flags, "%s", expression);
4071 } else {
4072 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4073 dsdb_flags, NULL);
4076 if (ret != LDB_SUCCESS) {
4077 talloc_free(tmp_ctx);
4078 return ret;
4081 *msg = talloc_steal(mem_ctx, res->msgs[0]);
4082 talloc_free(tmp_ctx);
4084 return LDB_SUCCESS;
4087 /* returns back the forest DNS name */
4088 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4090 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4091 ldb_get_root_basedn(ldb));
4092 char *p;
4094 if (forest_name == NULL) {
4095 return NULL;
4098 p = strchr(forest_name, '/');
4099 if (p) {
4100 *p = '\0';
4103 return forest_name;
4106 /* returns back the default domain DNS name */
4107 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4109 const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4110 ldb_get_default_basedn(ldb));
4111 char *p;
4113 if (domain_name == NULL) {
4114 return NULL;
4117 p = strchr(domain_name, '/');
4118 if (p) {
4119 *p = '\0';
4122 return domain_name;
4126 validate that an DSA GUID belongs to the specified user sid.
4127 The user SID must be a domain controller account (either RODC or
4128 RWDC)
4130 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4131 const struct GUID *dsa_guid,
4132 const struct dom_sid *sid)
4134 /* strategy:
4135 - find DN of record with the DSA GUID in the
4136 configuration partition (objectGUID)
4137 - remove "NTDS Settings" component from DN
4138 - do a base search on that DN for serverReference with
4139 extended-dn enabled
4140 - extract objectSid from resulting serverReference
4141 attribute
4142 - check this sid matches the sid argument
4144 struct ldb_dn *config_dn;
4145 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4146 struct ldb_message *msg;
4147 const char *attrs1[] = { NULL };
4148 const char *attrs2[] = { "serverReference", NULL };
4149 int ret;
4150 struct ldb_dn *dn, *account_dn;
4151 struct dom_sid sid2;
4152 NTSTATUS status;
4154 config_dn = ldb_get_config_basedn(ldb);
4156 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4157 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4158 if (ret != LDB_SUCCESS) {
4159 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4160 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4161 talloc_free(tmp_ctx);
4162 return ldb_operr(ldb);
4164 dn = msg->dn;
4166 if (!ldb_dn_remove_child_components(dn, 1)) {
4167 talloc_free(tmp_ctx);
4168 return ldb_operr(ldb);
4171 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4172 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4173 "(objectClass=server)");
4174 if (ret != LDB_SUCCESS) {
4175 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4176 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4177 talloc_free(tmp_ctx);
4178 return ldb_operr(ldb);
4181 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4182 if (account_dn == NULL) {
4183 DEBUG(1,(__location__ ": Failed to find account_dn for DSA with objectGUID %s, sid %s\n",
4184 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4185 talloc_free(tmp_ctx);
4186 return ldb_operr(ldb);
4189 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4190 if (!NT_STATUS_IS_OK(status)) {
4191 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4192 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4193 talloc_free(tmp_ctx);
4194 return ldb_operr(ldb);
4197 if (!dom_sid_equal(sid, &sid2)) {
4198 /* someone is trying to spoof another account */
4199 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4200 GUID_string(tmp_ctx, dsa_guid),
4201 dom_sid_string(tmp_ctx, sid),
4202 dom_sid_string(tmp_ctx, &sid2)));
4203 talloc_free(tmp_ctx);
4204 return ldb_operr(ldb);
4207 talloc_free(tmp_ctx);
4208 return LDB_SUCCESS;
4211 static const char * const secret_attributes[] = {
4212 DSDB_SECRET_ATTRIBUTES,
4213 NULL
4217 check if the attribute belongs to the RODC filtered attribute set
4218 Note that attributes that are in the filtered attribute set are the
4219 ones that _are_ always sent to a RODC
4221 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4223 /* they never get secret attributes */
4224 if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4225 return false;
4228 /* they do get non-secret critical attributes */
4229 if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4230 return true;
4233 /* they do get non-secret attributes marked as being in the FAS */
4234 if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4235 return true;
4238 /* other attributes are denied */
4239 return false;
4242 /* return fsmo role dn and role owner dn for a particular role*/
4243 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4244 struct ldb_context *ldb,
4245 uint32_t role,
4246 struct ldb_dn **fsmo_role_dn,
4247 struct ldb_dn **role_owner_dn)
4249 int ret;
4250 switch (role) {
4251 case DREPL_NAMING_MASTER:
4252 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
4253 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4254 if (ret != LDB_SUCCESS) {
4255 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
4256 ldb_errstring(ldb)));
4257 talloc_free(tmp_ctx);
4258 return WERR_DS_DRA_INTERNAL_ERROR;
4260 break;
4261 case DREPL_INFRASTRUCTURE_MASTER:
4262 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
4263 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4264 if (ret != LDB_SUCCESS) {
4265 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4266 ldb_errstring(ldb)));
4267 talloc_free(tmp_ctx);
4268 return WERR_DS_DRA_INTERNAL_ERROR;
4270 break;
4271 case DREPL_RID_MASTER:
4272 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
4273 if (ret != LDB_SUCCESS) {
4274 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
4275 talloc_free(tmp_ctx);
4276 return WERR_DS_DRA_INTERNAL_ERROR;
4279 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4280 if (ret != LDB_SUCCESS) {
4281 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
4282 ldb_errstring(ldb)));
4283 talloc_free(tmp_ctx);
4284 return WERR_DS_DRA_INTERNAL_ERROR;
4286 break;
4287 case DREPL_SCHEMA_MASTER:
4288 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
4289 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4290 if (ret != LDB_SUCCESS) {
4291 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4292 ldb_errstring(ldb)));
4293 talloc_free(tmp_ctx);
4294 return WERR_DS_DRA_INTERNAL_ERROR;
4296 break;
4297 case DREPL_PDC_MASTER:
4298 *fsmo_role_dn = ldb_get_default_basedn(ldb);
4299 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4300 if (ret != LDB_SUCCESS) {
4301 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
4302 ldb_errstring(ldb)));
4303 talloc_free(tmp_ctx);
4304 return WERR_DS_DRA_INTERNAL_ERROR;
4306 break;
4307 default:
4308 return WERR_DS_DRA_INTERNAL_ERROR;
4310 return WERR_OK;
4313 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
4314 TALLOC_CTX *mem_ctx,
4315 struct ldb_dn *server_dn)
4317 int ldb_ret;
4318 struct ldb_result *res = NULL;
4319 const char * const attrs[] = { "dNSHostName", NULL};
4321 ldb_ret = ldb_search(ldb, mem_ctx, &res,
4322 server_dn,
4323 LDB_SCOPE_BASE,
4324 attrs, NULL);
4325 if (ldb_ret != LDB_SUCCESS) {
4326 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
4327 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
4328 return NULL;
4331 return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
4335 returns true if an attribute is in the filter,
4336 false otherwise, provided that attribute value is provided with the expression
4338 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
4339 const char *attr)
4341 unsigned int i;
4342 switch (tree->operation) {
4343 case LDB_OP_AND:
4344 case LDB_OP_OR:
4345 for (i=0;i<tree->u.list.num_elements;i++) {
4346 if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
4347 attr))
4348 return true;
4350 return false;
4351 case LDB_OP_NOT:
4352 return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
4353 case LDB_OP_EQUALITY:
4354 case LDB_OP_GREATER:
4355 case LDB_OP_LESS:
4356 case LDB_OP_APPROX:
4357 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
4358 return true;
4360 return false;
4361 case LDB_OP_SUBSTRING:
4362 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
4363 return true;
4365 return false;
4366 case LDB_OP_PRESENT:
4367 /* (attrname=*) is not filtered out */
4368 return false;
4369 case LDB_OP_EXTENDED:
4370 if (tree->u.extended.attr &&
4371 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
4372 return true;
4374 return false;
4376 return false;
4379 bool is_attr_in_list(const char * const * attrs, const char *attr)
4381 unsigned int i;
4383 for (i = 0; attrs[i]; i++) {
4384 if (ldb_attr_cmp(attrs[i], attr) == 0)
4385 return true;
4388 return false;
4393 map an ldb error code to an approximate NTSTATUS code
4395 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
4397 switch (err) {
4398 case LDB_SUCCESS:
4399 return NT_STATUS_OK;
4401 case LDB_ERR_PROTOCOL_ERROR:
4402 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
4404 case LDB_ERR_TIME_LIMIT_EXCEEDED:
4405 return NT_STATUS_IO_TIMEOUT;
4407 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
4408 return NT_STATUS_BUFFER_TOO_SMALL;
4410 case LDB_ERR_COMPARE_FALSE:
4411 case LDB_ERR_COMPARE_TRUE:
4412 return NT_STATUS_REVISION_MISMATCH;
4414 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
4415 return NT_STATUS_NOT_SUPPORTED;
4417 case LDB_ERR_STRONG_AUTH_REQUIRED:
4418 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
4419 case LDB_ERR_SASL_BIND_IN_PROGRESS:
4420 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
4421 case LDB_ERR_INVALID_CREDENTIALS:
4422 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
4423 case LDB_ERR_UNWILLING_TO_PERFORM:
4424 return NT_STATUS_ACCESS_DENIED;
4426 case LDB_ERR_NO_SUCH_OBJECT:
4427 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4429 case LDB_ERR_REFERRAL:
4430 case LDB_ERR_NO_SUCH_ATTRIBUTE:
4431 return NT_STATUS_NOT_FOUND;
4433 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
4434 return NT_STATUS_NOT_SUPPORTED;
4436 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
4437 return NT_STATUS_BUFFER_TOO_SMALL;
4439 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
4440 case LDB_ERR_INAPPROPRIATE_MATCHING:
4441 case LDB_ERR_CONSTRAINT_VIOLATION:
4442 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
4443 case LDB_ERR_INVALID_DN_SYNTAX:
4444 case LDB_ERR_NAMING_VIOLATION:
4445 case LDB_ERR_OBJECT_CLASS_VIOLATION:
4446 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
4447 case LDB_ERR_NOT_ALLOWED_ON_RDN:
4448 return NT_STATUS_INVALID_PARAMETER;
4450 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
4451 case LDB_ERR_ENTRY_ALREADY_EXISTS:
4452 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
4454 case LDB_ERR_BUSY:
4455 return NT_STATUS_NETWORK_BUSY;
4457 case LDB_ERR_ALIAS_PROBLEM:
4458 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
4459 case LDB_ERR_UNAVAILABLE:
4460 case LDB_ERR_LOOP_DETECT:
4461 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
4462 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
4463 case LDB_ERR_OTHER:
4464 case LDB_ERR_OPERATIONS_ERROR:
4465 break;
4467 return NT_STATUS_UNSUCCESSFUL;
4472 create a new naming context that will hold a partial replica
4474 int dsdb_create_partial_replica_NC(struct ldb_context *ldb, struct ldb_dn *dn)
4476 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4477 struct ldb_message *msg;
4478 int ret;
4480 msg = ldb_msg_new(tmp_ctx);
4481 if (msg == NULL) {
4482 talloc_free(tmp_ctx);
4483 return ldb_oom(ldb);
4486 msg->dn = dn;
4487 ret = ldb_msg_add_string(msg, "objectClass", "top");
4488 if (ret != LDB_SUCCESS) {
4489 talloc_free(tmp_ctx);
4490 return ldb_oom(ldb);
4493 /* [MS-DRSR] implies that we should only add the 'top'
4494 * objectclass, but that would cause lots of problems with our
4495 * objectclass code as top is not structural, so we add
4496 * 'domainDNS' as well to keep things sane. We're expecting
4497 * this new NC to be of objectclass domainDNS after
4498 * replication anyway
4500 ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
4501 if (ret != LDB_SUCCESS) {
4502 talloc_free(tmp_ctx);
4503 return ldb_oom(ldb);
4506 ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
4507 INSTANCE_TYPE_IS_NC_HEAD|
4508 INSTANCE_TYPE_NC_ABOVE|
4509 INSTANCE_TYPE_UNINSTANT);
4510 if (ret != LDB_SUCCESS) {
4511 talloc_free(tmp_ctx);
4512 return ldb_oom(ldb);
4515 ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
4516 if (ret != LDB_SUCCESS && ret != LDB_ERR_ENTRY_ALREADY_EXISTS) {
4517 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
4518 ldb_dn_get_linearized(dn),
4519 ldb_errstring(ldb), ldb_strerror(ret)));
4520 talloc_free(tmp_ctx);
4521 return ret;
4524 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
4526 talloc_free(tmp_ctx);
4527 return LDB_SUCCESS;
4531 build a GUID from a string
4533 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
4535 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
4536 uint32_t time_low;
4537 uint32_t time_mid, time_hi_and_version;
4538 uint32_t clock_seq[2];
4539 uint32_t node[6];
4540 int i;
4542 if (s == NULL) {
4543 return NT_STATUS_INVALID_PARAMETER;
4546 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
4547 &time_low, &time_mid, &time_hi_and_version,
4548 &clock_seq[0], &clock_seq[1],
4549 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
4550 status = NT_STATUS_OK;
4553 if (!NT_STATUS_IS_OK(status)) {
4554 return status;
4557 guid->time_low = time_low;
4558 guid->time_mid = time_mid;
4559 guid->time_hi_and_version = time_hi_and_version;
4560 guid->clock_seq[0] = clock_seq[0];
4561 guid->clock_seq[1] = clock_seq[1];
4562 for (i=0;i<6;i++) {
4563 guid->node[i] = node[i];
4566 return NT_STATUS_OK;
4569 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
4571 return talloc_asprintf(mem_ctx,
4572 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
4573 guid->time_low, guid->time_mid,
4574 guid->time_hi_and_version,
4575 guid->clock_seq[0],
4576 guid->clock_seq[1],
4577 guid->node[0], guid->node[1],
4578 guid->node[2], guid->node[3],
4579 guid->node[4], guid->node[5]);