dsdb-acl: remove unused variable
[Samba/gebeck_regimport.git] / source4 / dsdb / common / util.c
blob2b96bd431e72608996057bdf0db2a7811303aea2
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 return invocation_id;
1308 tmp_ctx = talloc_new(ldb);
1309 if (tmp_ctx == NULL) {
1310 goto failed;
1313 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1314 if (ret) {
1315 goto failed;
1318 if (res->count != 1) {
1319 goto failed;
1322 invocation_id = talloc(tmp_ctx, struct GUID);
1323 if (!invocation_id) {
1324 goto failed;
1327 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1329 /* cache the domain_sid in the ldb */
1330 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1331 goto failed;
1334 talloc_steal(ldb, invocation_id);
1335 talloc_free(tmp_ctx);
1337 return invocation_id;
1339 failed:
1340 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1341 talloc_free(tmp_ctx);
1342 return NULL;
1345 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1347 TALLOC_CTX *tmp_ctx;
1348 struct GUID *invocation_id_new;
1349 struct GUID *invocation_id_old;
1351 /* see if we have a cached copy */
1352 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1353 "cache.invocation_id");
1355 tmp_ctx = talloc_new(ldb);
1356 if (tmp_ctx == NULL) {
1357 goto failed;
1360 invocation_id_new = talloc(tmp_ctx, struct GUID);
1361 if (!invocation_id_new) {
1362 goto failed;
1365 *invocation_id_new = *invocation_id_in;
1367 /* cache the domain_sid in the ldb */
1368 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1369 goto failed;
1372 talloc_steal(ldb, invocation_id_new);
1373 talloc_free(tmp_ctx);
1374 talloc_free(invocation_id_old);
1376 return true;
1378 failed:
1379 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1380 talloc_free(tmp_ctx);
1381 return false;
1385 work out the ntds settings objectGUID for the current open ldb
1387 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1389 TALLOC_CTX *tmp_ctx;
1390 const char *attrs[] = { "objectGUID", NULL };
1391 int ret;
1392 struct ldb_result *res;
1393 struct GUID *ntds_guid;
1395 /* see if we have a cached copy */
1396 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1397 if (ntds_guid) {
1398 return ntds_guid;
1401 tmp_ctx = talloc_new(ldb);
1402 if (tmp_ctx == NULL) {
1403 goto failed;
1406 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
1407 if (ret) {
1408 goto failed;
1411 if (res->count != 1) {
1412 goto failed;
1415 ntds_guid = talloc(tmp_ctx, struct GUID);
1416 if (!ntds_guid) {
1417 goto failed;
1420 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1422 /* cache the domain_sid in the ldb */
1423 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1424 goto failed;
1427 talloc_steal(ldb, ntds_guid);
1428 talloc_free(tmp_ctx);
1430 return ntds_guid;
1432 failed:
1433 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1434 talloc_free(tmp_ctx);
1435 return NULL;
1438 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1440 TALLOC_CTX *tmp_ctx;
1441 struct GUID *ntds_guid_new;
1442 struct GUID *ntds_guid_old;
1444 /* see if we have a cached copy */
1445 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1447 tmp_ctx = talloc_new(ldb);
1448 if (tmp_ctx == NULL) {
1449 goto failed;
1452 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1453 if (!ntds_guid_new) {
1454 goto failed;
1457 *ntds_guid_new = *ntds_guid_in;
1459 /* cache the domain_sid in the ldb */
1460 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1461 goto failed;
1464 talloc_steal(ldb, ntds_guid_new);
1465 talloc_free(tmp_ctx);
1466 talloc_free(ntds_guid_old);
1468 return true;
1470 failed:
1471 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1472 talloc_free(tmp_ctx);
1473 return false;
1477 work out the server dn for the current open ldb
1479 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1481 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1482 struct ldb_dn *dn;
1483 if (!tmp_ctx) {
1484 return NULL;
1486 dn = ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb, tmp_ctx));
1487 talloc_free(tmp_ctx);
1488 return dn;
1493 work out the server dn for the current open ldb
1495 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1497 struct ldb_dn *server_dn;
1498 struct ldb_dn *servers_dn;
1499 struct ldb_dn *server_site_dn;
1501 /* TODO: there must be a saner way to do this!! */
1502 server_dn = samdb_server_dn(ldb, mem_ctx);
1503 if (!server_dn) return NULL;
1505 servers_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1506 talloc_free(server_dn);
1507 if (!servers_dn) return NULL;
1509 server_site_dn = ldb_dn_get_parent(mem_ctx, servers_dn);
1510 talloc_free(servers_dn);
1512 return server_site_dn;
1516 find the site name from a computers DN record
1518 int samdb_find_site_for_computer(struct ldb_context *ldb,
1519 TALLOC_CTX *mem_ctx, struct ldb_dn *computer_dn,
1520 const char **site_name)
1522 int ret;
1523 struct ldb_dn *dn;
1524 const struct ldb_val *rdn_val;
1526 *site_name = NULL;
1528 ret = samdb_reference_dn(ldb, mem_ctx, computer_dn, "serverReferenceBL", &dn);
1529 if (ret != LDB_SUCCESS) {
1530 return ret;
1533 if (!ldb_dn_remove_child_components(dn, 2)) {
1534 talloc_free(dn);
1535 return LDB_ERR_INVALID_DN_SYNTAX;
1538 rdn_val = ldb_dn_get_rdn_val(dn);
1539 if (rdn_val == NULL) {
1540 return LDB_ERR_OPERATIONS_ERROR;
1543 (*site_name) = talloc_strndup(mem_ctx, (const char *)rdn_val->data, rdn_val->length);
1544 talloc_free(dn);
1545 if (!*site_name) {
1546 return LDB_ERR_OPERATIONS_ERROR;
1548 return LDB_SUCCESS;
1552 find the NTDS GUID from a computers DN record
1554 int samdb_find_ntdsguid_for_computer(struct ldb_context *ldb, struct ldb_dn *computer_dn,
1555 struct GUID *ntds_guid)
1557 int ret;
1558 struct ldb_dn *dn;
1560 *ntds_guid = GUID_zero();
1562 ret = samdb_reference_dn(ldb, ldb, computer_dn, "serverReferenceBL", &dn);
1563 if (ret != LDB_SUCCESS) {
1564 return ret;
1567 if (!ldb_dn_add_child_fmt(dn, "CN=NTDS Settings")) {
1568 talloc_free(dn);
1569 return LDB_ERR_OPERATIONS_ERROR;
1572 ret = dsdb_find_guid_by_dn(ldb, dn, ntds_guid);
1573 talloc_free(dn);
1574 return ret;
1578 find a 'reference' DN that points at another object
1579 (eg. serverReference, rIDManagerReference etc)
1581 int samdb_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *base,
1582 const char *attribute, struct ldb_dn **dn)
1584 const char *attrs[2];
1585 struct ldb_result *res;
1586 int ret;
1588 attrs[0] = attribute;
1589 attrs[1] = NULL;
1591 ret = dsdb_search(ldb, mem_ctx, &res, base, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_ONE_ONLY|DSDB_SEARCH_SHOW_EXTENDED_DN, NULL);
1592 if (ret != LDB_SUCCESS) {
1593 ldb_asprintf_errstring(ldb, "Cannot find DN %s to get attribute %s for reference dn: %s",
1594 ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb));
1595 return ret;
1598 *dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0], attribute);
1599 if (!*dn) {
1600 if (!ldb_msg_find_element(res->msgs[0], attribute)) {
1601 ldb_asprintf_errstring(ldb, "Cannot find attribute %s of %s to calculate reference dn", attribute,
1602 ldb_dn_get_linearized(base));
1603 } else {
1604 ldb_asprintf_errstring(ldb, "Cannot interpret attribute %s of %s as a dn", attribute,
1605 ldb_dn_get_linearized(base));
1607 talloc_free(res);
1608 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1611 talloc_free(res);
1612 return LDB_SUCCESS;
1616 find if a DN (must have GUID component!) is our ntdsDsa
1618 int samdb_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *dn, bool *is_ntdsa)
1620 NTSTATUS status;
1621 struct GUID dn_guid;
1622 const struct GUID *our_ntds_guid;
1623 status = dsdb_get_extended_dn_guid(dn, &dn_guid, "GUID");
1624 if (!NT_STATUS_IS_OK(status)) {
1625 return LDB_ERR_OPERATIONS_ERROR;
1628 our_ntds_guid = samdb_ntds_objectGUID(ldb);
1629 if (!our_ntds_guid) {
1630 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
1631 return LDB_ERR_OPERATIONS_ERROR;
1634 *is_ntdsa = GUID_equal(&dn_guid, our_ntds_guid);
1635 return LDB_SUCCESS;
1639 find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1641 int samdb_reference_dn_is_our_ntdsa(struct ldb_context *ldb, struct ldb_dn *base,
1642 const char *attribute, bool *is_ntdsa)
1644 int ret;
1645 struct ldb_dn *referenced_dn;
1646 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1647 if (tmp_ctx == NULL) {
1648 return LDB_ERR_OPERATIONS_ERROR;
1650 ret = samdb_reference_dn(ldb, tmp_ctx, base, attribute, &referenced_dn);
1651 if (ret != LDB_SUCCESS) {
1652 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base), attribute, ldb_errstring(ldb)));
1653 return ret;
1656 ret = samdb_dn_is_our_ntdsa(ldb, referenced_dn, is_ntdsa);
1658 talloc_free(tmp_ctx);
1659 return ret;
1663 find our machine account via the serverReference attribute in the
1664 server DN
1666 int samdb_server_reference_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1668 struct ldb_dn *server_dn;
1669 int ret;
1671 server_dn = samdb_server_dn(ldb, mem_ctx);
1672 if (server_dn == NULL) {
1673 return LDB_ERR_NO_SUCH_OBJECT;
1676 ret = samdb_reference_dn(ldb, mem_ctx, server_dn, "serverReference", dn);
1677 talloc_free(server_dn);
1679 return ret;
1683 find the RID Manager$ DN via the rIDManagerReference attribute in the
1684 base DN
1686 int samdb_rid_manager_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1688 return samdb_reference_dn(ldb, mem_ctx, ldb_get_default_basedn(ldb),
1689 "rIDManagerReference", dn);
1693 find the RID Set DN via the rIDSetReferences attribute in our
1694 machine account DN
1696 int samdb_rid_set_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn **dn)
1698 struct ldb_dn *server_ref_dn;
1699 int ret;
1701 ret = samdb_server_reference_dn(ldb, mem_ctx, &server_ref_dn);
1702 if (ret != LDB_SUCCESS) {
1703 return ret;
1705 ret = samdb_reference_dn(ldb, mem_ctx, server_ref_dn, "rIDSetReferences", dn);
1706 talloc_free(server_ref_dn);
1707 return ret;
1710 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1712 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb,
1713 mem_ctx));
1715 if (val == NULL) {
1716 return NULL;
1719 return (const char *) val->data;
1723 * Finds the client site by using the client's IP address.
1724 * The "subnet_name" returns the name of the subnet if parameter != NULL
1726 const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1727 const char *ip_address, char **subnet_name)
1729 const char *attrs[] = { "cn", "siteObject", NULL };
1730 struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
1731 struct ldb_result *res;
1732 const struct ldb_val *val;
1733 const char *site_name = NULL, *l_subnet_name = NULL;
1734 const char *allow_list[2] = { NULL, NULL };
1735 unsigned int i, count;
1736 int cnt, ret;
1739 * if we don't have a client ip e.g. ncalrpc
1740 * the server site is the client site
1742 if (ip_address == NULL) {
1743 return samdb_server_site_name(ldb, mem_ctx);
1746 sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
1747 if (sites_container_dn == NULL) {
1748 return NULL;
1751 subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
1752 if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
1753 talloc_free(sites_container_dn);
1754 talloc_free(subnets_dn);
1755 return NULL;
1758 ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
1759 attrs, NULL);
1760 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1761 count = 0;
1762 } else if (ret != LDB_SUCCESS) {
1763 talloc_free(sites_container_dn);
1764 talloc_free(subnets_dn);
1765 return NULL;
1766 } else {
1767 count = res->count;
1770 for (i = 0; i < count; i++) {
1771 l_subnet_name = ldb_msg_find_attr_as_string(res->msgs[i], "cn",
1772 NULL);
1774 allow_list[0] = l_subnet_name;
1776 if (socket_allow_access(mem_ctx, NULL, allow_list, "", ip_address)) {
1777 sites_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx,
1778 res->msgs[i],
1779 "siteObject");
1780 if (sites_dn == NULL) {
1781 /* No reference, maybe another subnet matches */
1782 continue;
1785 /* "val" cannot be NULL here since "sites_dn" != NULL */
1786 val = ldb_dn_get_rdn_val(sites_dn);
1787 site_name = talloc_strdup(mem_ctx,
1788 (const char *) val->data);
1790 talloc_free(sites_dn);
1792 break;
1796 if (site_name == NULL) {
1797 /* This is the Windows Server fallback rule: when no subnet
1798 * exists and we have only one site available then use it (it
1799 * is for sure the same as our server site). If more sites do
1800 * exist then we don't know which one to use and set the site
1801 * name to "". */
1802 cnt = samdb_search_count(ldb, mem_ctx, sites_container_dn,
1803 "(objectClass=site)");
1804 if (cnt == 1) {
1805 site_name = samdb_server_site_name(ldb, mem_ctx);
1806 } else {
1807 site_name = talloc_strdup(mem_ctx, "");
1809 l_subnet_name = NULL;
1812 if (subnet_name != NULL) {
1813 *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
1816 talloc_free(sites_container_dn);
1817 talloc_free(subnets_dn);
1818 talloc_free(res);
1820 return site_name;
1824 work out if we are the PDC for the domain of the current open ldb
1826 bool samdb_is_pdc(struct ldb_context *ldb)
1828 int ret;
1829 bool is_pdc;
1831 ret = samdb_reference_dn_is_our_ntdsa(ldb, ldb_get_default_basedn(ldb), "fsmoRoleOwner",
1832 &is_pdc);
1833 if (ret != LDB_SUCCESS) {
1834 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n",
1835 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1836 ldb_errstring(ldb)));
1837 return false;
1840 return is_pdc;
1844 work out if we are a Global Catalog server for the domain of the current open ldb
1846 bool samdb_is_gc(struct ldb_context *ldb)
1848 uint32_t options;
1849 if (samdb_ntds_options(ldb, &options) != LDB_SUCCESS) {
1850 return false;
1852 return (options & DS_NTDSDSA_OPT_IS_GC) != 0;
1855 /* Find a domain object in the parents of a particular DN. */
1856 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1857 struct ldb_dn **parent_dn, const char **errstring)
1859 TALLOC_CTX *local_ctx;
1860 struct ldb_dn *sdn = dn;
1861 struct ldb_result *res = NULL;
1862 int ret = LDB_SUCCESS;
1863 const char *attrs[] = { NULL };
1865 local_ctx = talloc_new(mem_ctx);
1866 if (local_ctx == NULL) return ldb_oom(ldb);
1868 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1869 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1870 "(|(objectClass=domain)(objectClass=builtinDomain))");
1871 if (ret == LDB_SUCCESS) {
1872 if (res->count == 1) {
1873 break;
1875 } else {
1876 break;
1880 if (ret != LDB_SUCCESS) {
1881 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1882 ldb_dn_get_linearized(dn),
1883 ldb_dn_get_linearized(sdn),
1884 ldb_errstring(ldb));
1885 talloc_free(local_ctx);
1886 return ret;
1888 if (res->count != 1) {
1889 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1890 ldb_dn_get_linearized(dn));
1891 DEBUG(0,(__location__ ": %s\n", *errstring));
1892 talloc_free(local_ctx);
1893 return LDB_ERR_CONSTRAINT_VIOLATION;
1896 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1897 talloc_free(local_ctx);
1898 return ret;
1903 * Performs checks on a user password (plaintext UNIX format - attribute
1904 * "password"). The remaining parameters have to be extracted from the domain
1905 * object in the AD.
1907 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1909 enum samr_ValidationStatus samdb_check_password(const DATA_BLOB *password,
1910 const uint32_t pwdProperties,
1911 const uint32_t minPwdLength)
1913 /* checks if the "minPwdLength" property is satisfied */
1914 if (minPwdLength > password->length)
1915 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
1917 /* checks the password complexity */
1918 if (((pwdProperties & DOMAIN_PASSWORD_COMPLEX) != 0)
1919 && (password->data != NULL)
1920 && (!check_password_quality((const char *) password->data)))
1921 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1923 return SAMR_VALIDATION_STATUS_SUCCESS;
1927 * Callback for "samdb_set_password" password change
1929 int samdb_set_password_callback(struct ldb_request *req, struct ldb_reply *ares)
1931 int ret;
1933 if (!ares) {
1934 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1937 if (ares->error != LDB_SUCCESS) {
1938 ret = ares->error;
1939 req->context = talloc_steal(req,
1940 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1941 talloc_free(ares);
1942 return ldb_request_done(req, ret);
1945 if (ares->type != LDB_REPLY_DONE) {
1946 talloc_free(ares);
1947 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1950 req->context = talloc_steal(req,
1951 ldb_reply_get_control(ares, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID));
1952 talloc_free(ares);
1953 return ldb_request_done(req, LDB_SUCCESS);
1957 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1958 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1959 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
1960 * user change or not. The "rejectReason" gives some more information if the
1961 * change failed.
1963 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
1964 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
1966 NTSTATUS samdb_set_password(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1967 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
1968 const DATA_BLOB *new_password,
1969 const struct samr_Password *lmNewHash,
1970 const struct samr_Password *ntNewHash,
1971 const struct samr_Password *lmOldHash,
1972 const struct samr_Password *ntOldHash,
1973 enum samPwdChangeReason *reject_reason,
1974 struct samr_DomInfo1 **_dominfo)
1976 struct ldb_message *msg;
1977 struct ldb_message_element *el;
1978 struct ldb_request *req;
1979 struct dsdb_control_password_change_status *pwd_stat = NULL;
1980 int ret;
1981 bool hash_values = false;
1982 NTSTATUS status = NT_STATUS_OK;
1984 #define CHECK_RET(x) \
1985 if (x != LDB_SUCCESS) { \
1986 talloc_free(msg); \
1987 return NT_STATUS_NO_MEMORY; \
1990 msg = ldb_msg_new(mem_ctx);
1991 if (msg == NULL) {
1992 return NT_STATUS_NO_MEMORY;
1994 msg->dn = user_dn;
1995 if ((new_password != NULL)
1996 && ((lmNewHash == NULL) && (ntNewHash == NULL))) {
1997 /* we have the password as plaintext UTF16 */
1998 CHECK_RET(ldb_msg_add_value(msg, "clearTextPassword",
1999 new_password, NULL));
2000 el = ldb_msg_find_element(msg, "clearTextPassword");
2001 el->flags = LDB_FLAG_MOD_REPLACE;
2002 } else if ((new_password == NULL)
2003 && ((lmNewHash != NULL) || (ntNewHash != NULL))) {
2004 /* we have a password as LM and/or NT hash */
2005 if (lmNewHash != NULL) {
2006 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2007 "dBCSPwd", lmNewHash));
2008 el = ldb_msg_find_element(msg, "dBCSPwd");
2009 el->flags = LDB_FLAG_MOD_REPLACE;
2011 if (ntNewHash != NULL) {
2012 CHECK_RET(samdb_msg_add_hash(ldb, mem_ctx, msg,
2013 "unicodePwd", ntNewHash));
2014 el = ldb_msg_find_element(msg, "unicodePwd");
2015 el->flags = LDB_FLAG_MOD_REPLACE;
2017 hash_values = true;
2018 } else {
2019 /* the password wasn't specified correctly */
2020 talloc_free(msg);
2021 return NT_STATUS_INVALID_PARAMETER;
2024 /* build modify request */
2025 ret = ldb_build_mod_req(&req, ldb, mem_ctx, msg, NULL, NULL,
2026 samdb_set_password_callback, NULL);
2027 if (ret != LDB_SUCCESS) {
2028 talloc_free(msg);
2029 return NT_STATUS_NO_MEMORY;
2032 /* A password change operation */
2033 if ((ntOldHash != NULL) || (lmOldHash != NULL)) {
2034 struct dsdb_control_password_change *change;
2036 change = talloc(req, struct dsdb_control_password_change);
2037 if (change == NULL) {
2038 talloc_free(req);
2039 talloc_free(msg);
2040 return NT_STATUS_NO_MEMORY;
2043 change->old_nt_pwd_hash = ntOldHash;
2044 change->old_lm_pwd_hash = lmOldHash;
2046 ret = ldb_request_add_control(req,
2047 DSDB_CONTROL_PASSWORD_CHANGE_OID,
2048 true, change);
2049 if (ret != LDB_SUCCESS) {
2050 talloc_free(req);
2051 talloc_free(msg);
2052 return NT_STATUS_NO_MEMORY;
2055 if (hash_values) {
2056 ret = ldb_request_add_control(req,
2057 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID,
2058 true, NULL);
2059 if (ret != LDB_SUCCESS) {
2060 talloc_free(req);
2061 talloc_free(msg);
2062 return NT_STATUS_NO_MEMORY;
2065 ret = ldb_request_add_control(req,
2066 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID,
2067 true, NULL);
2068 if (ret != LDB_SUCCESS) {
2069 talloc_free(req);
2070 talloc_free(msg);
2071 return NT_STATUS_NO_MEMORY;
2074 ret = dsdb_autotransaction_request(ldb, req);
2076 if (req->context != NULL) {
2077 pwd_stat = talloc_steal(mem_ctx,
2078 ((struct ldb_control *)req->context)->data);
2081 talloc_free(req);
2082 talloc_free(msg);
2084 /* Sets the domain info (if requested) */
2085 if (_dominfo != NULL) {
2086 struct samr_DomInfo1 *dominfo;
2088 dominfo = talloc_zero(mem_ctx, struct samr_DomInfo1);
2089 if (dominfo == NULL) {
2090 return NT_STATUS_NO_MEMORY;
2093 if (pwd_stat != NULL) {
2094 dominfo->min_password_length = pwd_stat->domain_data.minPwdLength;
2095 dominfo->password_properties = pwd_stat->domain_data.pwdProperties;
2096 dominfo->password_history_length = pwd_stat->domain_data.pwdHistoryLength;
2097 dominfo->max_password_age = pwd_stat->domain_data.maxPwdAge;
2098 dominfo->min_password_age = pwd_stat->domain_data.minPwdAge;
2101 *_dominfo = dominfo;
2104 if (reject_reason != NULL) {
2105 if (pwd_stat != NULL) {
2106 *reject_reason = pwd_stat->reject_reason;
2107 } else {
2108 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
2112 if (pwd_stat != NULL) {
2113 talloc_free(pwd_stat);
2116 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2117 const char *errmsg = ldb_errstring(ldb);
2118 char *endptr = NULL;
2119 WERROR werr = WERR_GENERAL_FAILURE;
2120 status = NT_STATUS_UNSUCCESSFUL;
2121 if (errmsg != NULL) {
2122 werr = W_ERROR(strtol(errmsg, &endptr, 16));
2124 if (endptr != errmsg) {
2125 if (W_ERROR_EQUAL(werr, WERR_INVALID_PASSWORD)) {
2126 status = NT_STATUS_WRONG_PASSWORD;
2128 if (W_ERROR_EQUAL(werr, WERR_PASSWORD_RESTRICTION)) {
2129 status = NT_STATUS_PASSWORD_RESTRICTION;
2132 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2133 /* don't let the caller know if an account doesn't exist */
2134 status = NT_STATUS_WRONG_PASSWORD;
2135 } else if (ret != LDB_SUCCESS) {
2136 status = NT_STATUS_UNSUCCESSFUL;
2139 return status;
2143 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2144 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2145 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2146 * user change or not. The "rejectReason" gives some more information if the
2147 * change failed.
2149 * This wrapper function for "samdb_set_password" takes a SID as input rather
2150 * than a user DN.
2152 * This call encapsulates a new LDB transaction for changing the password;
2153 * therefore the user hasn't to start a new one.
2155 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2156 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2157 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2158 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2160 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2161 const struct dom_sid *user_sid,
2162 const DATA_BLOB *new_password,
2163 const struct samr_Password *lmNewHash,
2164 const struct samr_Password *ntNewHash,
2165 const struct samr_Password *lmOldHash,
2166 const struct samr_Password *ntOldHash,
2167 enum samPwdChangeReason *reject_reason,
2168 struct samr_DomInfo1 **_dominfo)
2170 NTSTATUS nt_status;
2171 struct ldb_dn *user_dn;
2172 int ret;
2174 ret = ldb_transaction_start(ldb);
2175 if (ret != LDB_SUCCESS) {
2176 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
2177 return NT_STATUS_TRANSACTION_ABORTED;
2180 user_dn = samdb_search_dn(ldb, mem_ctx, NULL,
2181 "(&(objectSid=%s)(objectClass=user))",
2182 ldap_encode_ndr_dom_sid(mem_ctx, user_sid));
2183 if (!user_dn) {
2184 ldb_transaction_cancel(ldb);
2185 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2186 dom_sid_string(mem_ctx, user_sid)));
2187 return NT_STATUS_NO_SUCH_USER;
2190 nt_status = samdb_set_password(ldb, mem_ctx,
2191 user_dn, NULL,
2192 new_password,
2193 lmNewHash, ntNewHash,
2194 lmOldHash, ntOldHash,
2195 reject_reason, _dominfo);
2196 if (!NT_STATUS_IS_OK(nt_status)) {
2197 ldb_transaction_cancel(ldb);
2198 talloc_free(user_dn);
2199 return nt_status;
2202 ret = ldb_transaction_commit(ldb);
2203 if (ret != LDB_SUCCESS) {
2204 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2205 ldb_dn_get_linearized(user_dn),
2206 ldb_errstring(ldb)));
2207 talloc_free(user_dn);
2208 return NT_STATUS_TRANSACTION_ABORTED;
2211 talloc_free(user_dn);
2212 return NT_STATUS_OK;
2216 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
2217 struct dom_sid *sid, struct ldb_dn **ret_dn)
2219 struct ldb_message *msg;
2220 struct ldb_dn *basedn;
2221 char *sidstr;
2222 int ret;
2224 sidstr = dom_sid_string(mem_ctx, sid);
2225 NT_STATUS_HAVE_NO_MEMORY(sidstr);
2227 /* We might have to create a ForeignSecurityPrincipal, even if this user
2228 * is in our own domain */
2230 msg = ldb_msg_new(sidstr);
2231 if (msg == NULL) {
2232 talloc_free(sidstr);
2233 return NT_STATUS_NO_MEMORY;
2236 ret = dsdb_wellknown_dn(sam_ctx, sidstr,
2237 ldb_get_default_basedn(sam_ctx),
2238 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER,
2239 &basedn);
2240 if (ret != LDB_SUCCESS) {
2241 DEBUG(0, ("Failed to find DN for "
2242 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx)));
2243 talloc_free(sidstr);
2244 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2247 /* add core elements to the ldb_message for the alias */
2248 msg->dn = basedn;
2249 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr)) {
2250 talloc_free(sidstr);
2251 return NT_STATUS_NO_MEMORY;
2254 ret = ldb_msg_add_string(msg, "objectClass",
2255 "foreignSecurityPrincipal");
2256 if (ret != LDB_SUCCESS) {
2257 talloc_free(sidstr);
2258 return NT_STATUS_NO_MEMORY;
2261 /* create the alias */
2262 ret = ldb_add(sam_ctx, msg);
2263 if (ret != LDB_SUCCESS) {
2264 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2265 "record %s: %s\n",
2266 ldb_dn_get_linearized(msg->dn),
2267 ldb_errstring(sam_ctx)));
2268 talloc_free(sidstr);
2269 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2272 *ret_dn = talloc_steal(mem_ctx, msg->dn);
2273 talloc_free(sidstr);
2275 return NT_STATUS_OK;
2280 Find the DN of a domain, assuming it to be a dotted.dns name
2283 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2285 unsigned int i;
2286 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2287 const char *binary_encoded;
2288 const char * const *split_realm;
2289 struct ldb_dn *dn;
2291 if (!tmp_ctx) {
2292 return NULL;
2295 split_realm = (const char * const *)str_list_make(tmp_ctx, dns_domain, ".");
2296 if (!split_realm) {
2297 talloc_free(tmp_ctx);
2298 return NULL;
2300 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2301 for (i=0; split_realm[i]; i++) {
2302 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2303 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2304 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2305 binary_encoded, ldb_dn_get_linearized(dn)));
2306 talloc_free(tmp_ctx);
2307 return NULL;
2310 if (!ldb_dn_validate(dn)) {
2311 DEBUG(2, ("Failed to validated DN %s\n",
2312 ldb_dn_get_linearized(dn)));
2313 talloc_free(tmp_ctx);
2314 return NULL;
2316 talloc_free(tmp_ctx);
2317 return dn;
2322 Find the DNS equivalent of a DN, in dotted DNS form
2324 char *samdb_dn_to_dns_domain(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
2326 int i, num_components = ldb_dn_get_comp_num(dn);
2327 char *dns_name = talloc_strdup(mem_ctx, "");
2328 if (dns_name == NULL) {
2329 return NULL;
2332 for (i=0; i<num_components; i++) {
2333 const struct ldb_val *v = ldb_dn_get_component_val(dn, i);
2334 char *s;
2335 if (v == NULL) {
2336 talloc_free(dns_name);
2337 return NULL;
2339 s = talloc_asprintf_append_buffer(dns_name, "%*.*s.",
2340 (int)v->length, (int)v->length, (char *)v->data);
2341 if (s == NULL) {
2342 talloc_free(dns_name);
2343 return NULL;
2345 dns_name = s;
2348 /* remove the last '.' */
2349 if (dns_name[0] != 0) {
2350 dns_name[strlen(dns_name)-1] = 0;
2353 return dns_name;
2357 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2358 name is based on the forest DNS name
2360 char *samdb_ntds_msdcs_dns_name(struct ldb_context *samdb,
2361 TALLOC_CTX *mem_ctx,
2362 const struct GUID *ntds_guid)
2364 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2365 const char *guid_str;
2366 struct ldb_dn *forest_dn;
2367 const char *dnsforest;
2368 char *ret;
2370 guid_str = GUID_string(tmp_ctx, ntds_guid);
2371 if (guid_str == NULL) {
2372 talloc_free(tmp_ctx);
2373 return NULL;
2375 forest_dn = ldb_get_root_basedn(samdb);
2376 if (forest_dn == NULL) {
2377 talloc_free(tmp_ctx);
2378 return NULL;
2380 dnsforest = samdb_dn_to_dns_domain(tmp_ctx, forest_dn);
2381 if (dnsforest == NULL) {
2382 talloc_free(tmp_ctx);
2383 return NULL;
2385 ret = talloc_asprintf(mem_ctx, "%s._msdcs.%s", guid_str, dnsforest);
2386 talloc_free(tmp_ctx);
2387 return ret;
2392 Find the DN of a domain, be it the netbios or DNS name
2394 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2395 const char *domain_name)
2397 const char * const domain_ref_attrs[] = {
2398 "ncName", NULL
2400 const char * const domain_ref2_attrs[] = {
2401 NULL
2403 struct ldb_result *res_domain_ref;
2404 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2405 /* find the domain's DN */
2406 int ret_domain = ldb_search(ldb, mem_ctx,
2407 &res_domain_ref,
2408 samdb_partitions_dn(ldb, mem_ctx),
2409 LDB_SCOPE_ONELEVEL,
2410 domain_ref_attrs,
2411 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2412 escaped_domain);
2413 if (ret_domain != LDB_SUCCESS) {
2414 return NULL;
2417 if (res_domain_ref->count == 0) {
2418 ret_domain = ldb_search(ldb, mem_ctx,
2419 &res_domain_ref,
2420 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2421 LDB_SCOPE_BASE,
2422 domain_ref2_attrs,
2423 "(objectclass=domain)");
2424 if (ret_domain != LDB_SUCCESS) {
2425 return NULL;
2428 if (res_domain_ref->count == 1) {
2429 return res_domain_ref->msgs[0]->dn;
2431 return NULL;
2434 if (res_domain_ref->count > 1) {
2435 DEBUG(0,("Found %d records matching domain [%s]\n",
2436 ret_domain, domain_name));
2437 return NULL;
2440 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
2446 use a GUID to find a DN
2448 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
2449 TALLOC_CTX *mem_ctx,
2450 const struct GUID *guid, struct ldb_dn **dn)
2452 int ret;
2453 struct ldb_result *res;
2454 const char *attrs[] = { NULL };
2455 char *guid_str = GUID_string(mem_ctx, guid);
2457 if (!guid_str) {
2458 return ldb_operr(ldb);
2461 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2462 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2463 DSDB_SEARCH_SHOW_EXTENDED_DN |
2464 DSDB_SEARCH_ONE_ONLY,
2465 "objectGUID=%s", guid_str);
2466 talloc_free(guid_str);
2467 if (ret != LDB_SUCCESS) {
2468 return ret;
2471 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2472 talloc_free(res);
2474 return LDB_SUCCESS;
2478 use a DN to find a GUID with a given attribute name
2480 int dsdb_find_guid_attr_by_dn(struct ldb_context *ldb,
2481 struct ldb_dn *dn, const char *attribute,
2482 struct GUID *guid)
2484 int ret;
2485 struct ldb_result *res;
2486 const char *attrs[2];
2487 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2489 attrs[0] = attribute;
2490 attrs[1] = NULL;
2492 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2493 DSDB_SEARCH_SHOW_DELETED |
2494 DSDB_SEARCH_SHOW_RECYCLED);
2495 if (ret != LDB_SUCCESS) {
2496 talloc_free(tmp_ctx);
2497 return ret;
2499 if (res->count < 1) {
2500 talloc_free(tmp_ctx);
2501 return LDB_ERR_NO_SUCH_OBJECT;
2503 *guid = samdb_result_guid(res->msgs[0], attribute);
2504 talloc_free(tmp_ctx);
2505 return LDB_SUCCESS;
2509 use a DN to find a GUID
2511 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
2512 struct ldb_dn *dn, struct GUID *guid)
2514 return dsdb_find_guid_attr_by_dn(ldb, dn, "objectGUID", guid);
2520 adds the given GUID to the given ldb_message. This value is added
2521 for the given attr_name (may be either "objectGUID" or "parentGUID").
2523 int dsdb_msg_add_guid(struct ldb_message *msg,
2524 struct GUID *guid,
2525 const char *attr_name)
2527 int ret;
2528 struct ldb_val v;
2529 NTSTATUS status;
2530 TALLOC_CTX *tmp_ctx = talloc_init("dsdb_msg_add_guid");
2532 status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
2533 if (!NT_STATUS_IS_OK(status)) {
2534 ret = LDB_ERR_OPERATIONS_ERROR;
2535 goto done;
2538 ret = ldb_msg_add_steal_value(msg, attr_name, &v);
2539 if (ret != LDB_SUCCESS) {
2540 DEBUG(4,(__location__ ": Failed to add %s to the message\n",
2541 attr_name));
2542 goto done;
2545 ret = LDB_SUCCESS;
2547 done:
2548 talloc_free(tmp_ctx);
2549 return ret;
2555 use a DN to find a SID
2557 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
2558 struct ldb_dn *dn, struct dom_sid *sid)
2560 int ret;
2561 struct ldb_result *res;
2562 const char *attrs[] = { "objectSid", NULL };
2563 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2564 struct dom_sid *s;
2566 ZERO_STRUCTP(sid);
2568 ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
2569 DSDB_SEARCH_SHOW_DELETED |
2570 DSDB_SEARCH_SHOW_RECYCLED);
2571 if (ret != LDB_SUCCESS) {
2572 talloc_free(tmp_ctx);
2573 return ret;
2575 if (res->count < 1) {
2576 talloc_free(tmp_ctx);
2577 return LDB_ERR_NO_SUCH_OBJECT;
2579 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
2580 if (s == NULL) {
2581 talloc_free(tmp_ctx);
2582 return LDB_ERR_NO_SUCH_OBJECT;
2584 *sid = *s;
2585 talloc_free(tmp_ctx);
2586 return LDB_SUCCESS;
2590 use a SID to find a DN
2592 int dsdb_find_dn_by_sid(struct ldb_context *ldb,
2593 TALLOC_CTX *mem_ctx,
2594 struct dom_sid *sid, struct ldb_dn **dn)
2596 int ret;
2597 struct ldb_result *res;
2598 const char *attrs[] = { NULL };
2599 char *sid_str = ldap_encode_ndr_dom_sid(mem_ctx, sid);
2601 if (!sid_str) {
2602 return ldb_operr(ldb);
2605 ret = dsdb_search(ldb, mem_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
2606 DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
2607 DSDB_SEARCH_SHOW_EXTENDED_DN |
2608 DSDB_SEARCH_ONE_ONLY,
2609 "objectSid=%s", sid_str);
2610 talloc_free(sid_str);
2611 if (ret != LDB_SUCCESS) {
2612 return ret;
2615 *dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
2616 talloc_free(res);
2618 return LDB_SUCCESS;
2622 load a repsFromTo blob list for a given partition GUID
2623 attr must be "repsFrom" or "repsTo"
2625 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2626 const char *attr, struct repsFromToBlob **r, uint32_t *count)
2628 const char *attrs[] = { attr, NULL };
2629 struct ldb_result *res = NULL;
2630 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2631 unsigned int i;
2632 struct ldb_message_element *el;
2633 int ret;
2635 *r = NULL;
2636 *count = 0;
2638 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs, 0);
2639 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2640 /* partition hasn't been replicated yet */
2641 return WERR_OK;
2643 if (ret != LDB_SUCCESS) {
2644 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx)));
2645 talloc_free(tmp_ctx);
2646 return WERR_DS_DRA_INTERNAL_ERROR;
2649 el = ldb_msg_find_element(res->msgs[0], attr);
2650 if (el == NULL) {
2651 /* it's OK to be empty */
2652 talloc_free(tmp_ctx);
2653 return WERR_OK;
2656 *count = el->num_values;
2657 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
2658 if (*r == NULL) {
2659 talloc_free(tmp_ctx);
2660 return WERR_DS_DRA_INTERNAL_ERROR;
2663 for (i=0; i<(*count); i++) {
2664 enum ndr_err_code ndr_err;
2665 ndr_err = ndr_pull_struct_blob(&el->values[i],
2666 mem_ctx,
2667 &(*r)[i],
2668 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
2669 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2670 talloc_free(tmp_ctx);
2671 return WERR_DS_DRA_INTERNAL_ERROR;
2675 talloc_free(tmp_ctx);
2677 return WERR_OK;
2681 save the repsFromTo blob list for a given partition GUID
2682 attr must be "repsFrom" or "repsTo"
2684 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2685 const char *attr, struct repsFromToBlob *r, uint32_t count)
2687 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2688 struct ldb_message *msg;
2689 struct ldb_message_element *el;
2690 unsigned int i;
2692 msg = ldb_msg_new(tmp_ctx);
2693 msg->dn = dn;
2694 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
2695 goto failed;
2698 el->values = talloc_array(msg, struct ldb_val, count);
2699 if (!el->values) {
2700 goto failed;
2703 for (i=0; i<count; i++) {
2704 struct ldb_val v;
2705 enum ndr_err_code ndr_err;
2707 ndr_err = ndr_push_struct_blob(&v, tmp_ctx,
2708 &r[i],
2709 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
2710 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2711 goto failed;
2714 el->num_values++;
2715 el->values[i] = v;
2718 if (dsdb_modify(sam_ctx, msg, 0) != LDB_SUCCESS) {
2719 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
2720 goto failed;
2723 talloc_free(tmp_ctx);
2725 return WERR_OK;
2727 failed:
2728 talloc_free(tmp_ctx);
2729 return WERR_DS_DRA_INTERNAL_ERROR;
2734 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2735 object for a partition
2737 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
2738 uint64_t *uSN, uint64_t *urgent_uSN)
2740 struct ldb_request *req;
2741 int ret;
2742 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2743 struct dsdb_control_current_partition *p_ctrl;
2744 struct ldb_result *res;
2746 res = talloc_zero(tmp_ctx, struct ldb_result);
2747 if (!res) {
2748 talloc_free(tmp_ctx);
2749 return ldb_oom(ldb);
2752 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
2753 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
2754 LDB_SCOPE_BASE,
2755 NULL, NULL,
2756 NULL,
2757 res, ldb_search_default_callback,
2758 NULL);
2759 if (ret != LDB_SUCCESS) {
2760 talloc_free(tmp_ctx);
2761 return ret;
2764 p_ctrl = talloc(req, struct dsdb_control_current_partition);
2765 if (p_ctrl == NULL) {
2766 talloc_free(tmp_ctx);
2767 return ldb_oom(ldb);
2769 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2770 p_ctrl->dn = dn;
2772 ret = ldb_request_add_control(req,
2773 DSDB_CONTROL_CURRENT_PARTITION_OID,
2774 false, p_ctrl);
2775 if (ret != LDB_SUCCESS) {
2776 talloc_free(tmp_ctx);
2777 return ret;
2780 /* Run the new request */
2781 ret = ldb_request(ldb, req);
2783 if (ret == LDB_SUCCESS) {
2784 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2787 if (ret == LDB_ERR_NO_SUCH_OBJECT || ret == LDB_ERR_INVALID_DN_SYNTAX) {
2788 /* it hasn't been created yet, which means
2789 an implicit value of zero */
2790 *uSN = 0;
2791 talloc_free(tmp_ctx);
2792 return LDB_SUCCESS;
2795 if (ret != LDB_SUCCESS) {
2796 talloc_free(tmp_ctx);
2797 return ret;
2800 if (res->count < 1) {
2801 *uSN = 0;
2802 if (urgent_uSN) {
2803 *urgent_uSN = 0;
2805 } else {
2806 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
2807 if (urgent_uSN) {
2808 *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
2812 talloc_free(tmp_ctx);
2814 return LDB_SUCCESS;
2817 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
2818 const struct drsuapi_DsReplicaCursor2 *c2)
2820 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2823 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor *c1,
2824 const struct drsuapi_DsReplicaCursor *c2)
2826 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2831 see if a computer identified by its invocationId is a RODC
2833 int samdb_is_rodc(struct ldb_context *sam_ctx, const struct GUID *objectGUID, bool *is_rodc)
2835 /* 1) find the DN for this servers NTDSDSA object
2836 2) search for the msDS-isRODC attribute
2837 3) if not present then not a RODC
2838 4) if present and TRUE then is a RODC
2840 struct ldb_dn *config_dn;
2841 const char *attrs[] = { "msDS-isRODC", NULL };
2842 int ret;
2843 struct ldb_result *res;
2844 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
2846 config_dn = ldb_get_config_basedn(sam_ctx);
2847 if (!config_dn) {
2848 talloc_free(tmp_ctx);
2849 return ldb_operr(sam_ctx);
2852 ret = dsdb_search(sam_ctx, tmp_ctx, &res, config_dn, LDB_SCOPE_SUBTREE, attrs,
2853 DSDB_SEARCH_ONE_ONLY, "objectGUID=%s", GUID_string(tmp_ctx, objectGUID));
2855 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2856 *is_rodc = false;
2857 talloc_free(tmp_ctx);
2858 return LDB_SUCCESS;
2861 if (ret != LDB_SUCCESS) {
2862 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
2863 GUID_string(tmp_ctx, objectGUID)));
2864 *is_rodc = false;
2865 talloc_free(tmp_ctx);
2866 return ret;
2869 ret = ldb_msg_find_attr_as_bool(res->msgs[0], "msDS-isRODC", 0);
2870 *is_rodc = (ret == 1);
2872 talloc_free(tmp_ctx);
2873 return LDB_SUCCESS;
2878 see if we are a RODC
2880 int samdb_rodc(struct ldb_context *sam_ctx, bool *am_rodc)
2882 const struct GUID *objectGUID;
2883 int ret;
2884 bool *cached;
2886 /* see if we have a cached copy */
2887 cached = (bool *)ldb_get_opaque(sam_ctx, "cache.am_rodc");
2888 if (cached) {
2889 *am_rodc = *cached;
2890 return LDB_SUCCESS;
2893 objectGUID = samdb_ntds_objectGUID(sam_ctx);
2894 if (!objectGUID) {
2895 return ldb_operr(sam_ctx);
2898 ret = samdb_is_rodc(sam_ctx, objectGUID, am_rodc);
2899 if (ret != LDB_SUCCESS) {
2900 return ret;
2903 cached = talloc(sam_ctx, bool);
2904 if (cached == NULL) {
2905 return ldb_oom(sam_ctx);
2907 *cached = *am_rodc;
2909 ret = ldb_set_opaque(sam_ctx, "cache.am_rodc", cached);
2910 if (ret != LDB_SUCCESS) {
2911 talloc_free(cached);
2912 return ldb_operr(sam_ctx);
2915 return LDB_SUCCESS;
2918 bool samdb_set_am_rodc(struct ldb_context *ldb, bool am_rodc)
2920 TALLOC_CTX *tmp_ctx;
2921 bool *cached;
2923 tmp_ctx = talloc_new(ldb);
2924 if (tmp_ctx == NULL) {
2925 goto failed;
2928 cached = talloc(tmp_ctx, bool);
2929 if (!cached) {
2930 goto failed;
2933 *cached = am_rodc;
2934 if (ldb_set_opaque(ldb, "cache.am_rodc", cached) != LDB_SUCCESS) {
2935 goto failed;
2938 talloc_steal(ldb, cached);
2939 talloc_free(tmp_ctx);
2940 return true;
2942 failed:
2943 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
2944 talloc_free(tmp_ctx);
2945 return false;
2950 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
2951 * flags are DS_NTDSSETTINGS_OPT_*
2953 int samdb_ntds_site_settings_options(struct ldb_context *ldb_ctx,
2954 uint32_t *options)
2956 int rc;
2957 TALLOC_CTX *tmp_ctx;
2958 struct ldb_result *res;
2959 struct ldb_dn *site_dn;
2960 const char *attrs[] = { "options", NULL };
2962 tmp_ctx = talloc_new(ldb_ctx);
2963 if (tmp_ctx == NULL)
2964 goto failed;
2966 /* Retrieve the site dn for the ldb that we
2967 * have open. This is our local site.
2969 site_dn = samdb_server_site_dn(ldb_ctx, tmp_ctx);
2970 if (site_dn == NULL)
2971 goto failed;
2973 /* Perform a one level (child) search from the local
2974 * site distinguided name. We're looking for the
2975 * "options" attribute within the nTDSSiteSettings
2976 * object
2978 rc = ldb_search(ldb_ctx, tmp_ctx, &res, site_dn,
2979 LDB_SCOPE_ONELEVEL, attrs,
2980 "objectClass=nTDSSiteSettings");
2982 if (rc != LDB_SUCCESS || res->count != 1)
2983 goto failed;
2985 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
2987 talloc_free(tmp_ctx);
2989 return LDB_SUCCESS;
2991 failed:
2992 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
2993 talloc_free(tmp_ctx);
2994 return LDB_ERR_NO_SUCH_OBJECT;
2998 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3000 flags are DS_NTDS_OPTION_*
3002 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
3004 TALLOC_CTX *tmp_ctx;
3005 const char *attrs[] = { "options", NULL };
3006 int ret;
3007 struct ldb_result *res;
3009 tmp_ctx = talloc_new(ldb);
3010 if (tmp_ctx == NULL) {
3011 goto failed;
3014 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3015 if (ret != LDB_SUCCESS) {
3016 goto failed;
3019 if (res->count != 1) {
3020 goto failed;
3023 *options = ldb_msg_find_attr_as_uint(res->msgs[0], "options", 0);
3025 talloc_free(tmp_ctx);
3027 return LDB_SUCCESS;
3029 failed:
3030 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3031 talloc_free(tmp_ctx);
3032 return LDB_ERR_NO_SUCH_OBJECT;
3035 const char* samdb_ntds_object_category(TALLOC_CTX *tmp_ctx, struct ldb_context *ldb)
3037 const char *attrs[] = { "objectCategory", NULL };
3038 int ret;
3039 struct ldb_result *res;
3041 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
3042 if (ret != LDB_SUCCESS) {
3043 goto failed;
3046 if (res->count != 1) {
3047 goto failed;
3050 return ldb_msg_find_attr_as_string(res->msgs[0], "objectCategory", NULL);
3052 failed:
3053 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3054 return NULL;
3058 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3059 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3061 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
3063 char **tokens, *ret;
3064 size_t i;
3066 tokens = str_list_make(mem_ctx, cn, " -_");
3067 if (tokens == NULL)
3068 return NULL;
3070 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3071 tokens[0][0] = tolower(tokens[0][0]);
3072 for (i = 1; i < str_list_length((const char * const *)tokens); i++)
3073 tokens[i][0] = toupper(tokens[i][0]);
3075 ret = talloc_strdup(mem_ctx, tokens[0]);
3076 for (i = 1; i < str_list_length((const char * const *)tokens); i++)
3077 ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
3079 talloc_free(tokens);
3081 return ret;
3085 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3087 int dsdb_functional_level(struct ldb_context *ldb)
3089 int *domainFunctionality =
3090 talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
3091 if (!domainFunctionality) {
3092 /* this is expected during initial provision */
3093 DEBUG(4,(__location__ ": WARNING: domainFunctionality not setup\n"));
3094 return DS_DOMAIN_FUNCTION_2000;
3096 return *domainFunctionality;
3100 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3102 int dsdb_forest_functional_level(struct ldb_context *ldb)
3104 int *forestFunctionality =
3105 talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int);
3106 if (!forestFunctionality) {
3107 DEBUG(0,(__location__ ": WARNING: forestFunctionality not setup\n"));
3108 return DS_DOMAIN_FUNCTION_2000;
3110 return *forestFunctionality;
3114 set a GUID in an extended DN structure
3116 int dsdb_set_extended_dn_guid(struct ldb_dn *dn, const struct GUID *guid, const char *component_name)
3118 struct ldb_val v;
3119 NTSTATUS status;
3120 int ret;
3122 status = GUID_to_ndr_blob(guid, dn, &v);
3123 if (!NT_STATUS_IS_OK(status)) {
3124 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3127 ret = ldb_dn_set_extended_component(dn, component_name, &v);
3128 data_blob_free(&v);
3129 return ret;
3133 return a GUID from a extended DN structure
3135 NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const char *component_name)
3137 const struct ldb_val *v;
3139 v = ldb_dn_get_extended_component(dn, component_name);
3140 if (v == NULL) {
3141 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3144 return GUID_from_ndr_blob(v, guid);
3148 return a uint64_t from a extended DN structure
3150 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
3152 const struct ldb_val *v;
3153 char *s;
3155 v = ldb_dn_get_extended_component(dn, component_name);
3156 if (v == NULL) {
3157 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3159 s = talloc_strndup(dn, (const char *)v->data, v->length);
3160 NT_STATUS_HAVE_NO_MEMORY(s);
3162 *val = strtoull(s, NULL, 0);
3164 talloc_free(s);
3165 return NT_STATUS_OK;
3169 return a NTTIME from a extended DN structure
3171 NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const char *component_name)
3173 return dsdb_get_extended_dn_uint64(dn, nttime, component_name);
3177 return a uint32_t from a extended DN structure
3179 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
3181 const struct ldb_val *v;
3182 char *s;
3184 v = ldb_dn_get_extended_component(dn, component_name);
3185 if (v == NULL) {
3186 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3189 s = talloc_strndup(dn, (const char *)v->data, v->length);
3190 NT_STATUS_HAVE_NO_MEMORY(s);
3192 *val = strtoul(s, NULL, 0);
3194 talloc_free(s);
3195 return NT_STATUS_OK;
3199 return a dom_sid from a extended DN structure
3201 NTSTATUS dsdb_get_extended_dn_sid(struct ldb_dn *dn, struct dom_sid *sid, const char *component_name)
3203 const struct ldb_val *sid_blob;
3204 struct TALLOC_CTX *tmp_ctx;
3205 enum ndr_err_code ndr_err;
3207 sid_blob = ldb_dn_get_extended_component(dn, component_name);
3208 if (!sid_blob) {
3209 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3212 tmp_ctx = talloc_new(NULL);
3214 ndr_err = ndr_pull_struct_blob_all(sid_blob, tmp_ctx, sid,
3215 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
3216 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3217 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
3218 talloc_free(tmp_ctx);
3219 return status;
3222 talloc_free(tmp_ctx);
3223 return NT_STATUS_OK;
3228 return RMD_FLAGS directly from a ldb_dn
3229 returns 0 if not found
3231 uint32_t dsdb_dn_rmd_flags(struct ldb_dn *dn)
3233 const struct ldb_val *v;
3234 char buf[32];
3235 v = ldb_dn_get_extended_component(dn, "RMD_FLAGS");
3236 if (!v || v->length > sizeof(buf)-1) return 0;
3237 strncpy(buf, (const char *)v->data, v->length);
3238 buf[v->length] = 0;
3239 return strtoul(buf, NULL, 10);
3243 return RMD_FLAGS directly from a ldb_val for a DN
3244 returns 0 if RMD_FLAGS is not found
3246 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
3248 const char *p;
3249 uint32_t flags;
3250 char *end;
3252 if (val->length < 13) {
3253 return 0;
3255 p = memmem(val->data, val->length, "<RMD_FLAGS=", 11);
3256 if (!p) {
3257 return 0;
3259 flags = strtoul(p+11, &end, 10);
3260 if (!end || *end != '>') {
3261 /* it must end in a > */
3262 return 0;
3264 return flags;
3268 return true if a ldb_val containing a DN in storage form is deleted
3270 bool dsdb_dn_is_deleted_val(const struct ldb_val *val)
3272 return (dsdb_dn_val_rmd_flags(val) & DSDB_RMD_FLAG_DELETED) != 0;
3276 return true if a ldb_val containing a DN in storage form is
3277 in the upgraded w2k3 linked attribute format
3279 bool dsdb_dn_is_upgraded_link_val(struct ldb_val *val)
3281 return memmem(val->data, val->length, "<RMD_VERSION=", 13) != NULL;
3285 return a DN for a wellknown GUID
3287 int dsdb_wellknown_dn(struct ldb_context *samdb, TALLOC_CTX *mem_ctx,
3288 struct ldb_dn *nc_root, const char *wk_guid,
3289 struct ldb_dn **wkguid_dn)
3291 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3292 const char *attrs[] = { NULL };
3293 int ret;
3294 struct ldb_dn *dn;
3295 struct ldb_result *res;
3297 /* construct the magic WKGUID DN */
3298 dn = ldb_dn_new_fmt(tmp_ctx, samdb, "<WKGUID=%s,%s>",
3299 wk_guid, ldb_dn_get_linearized(nc_root));
3300 if (!wkguid_dn) {
3301 talloc_free(tmp_ctx);
3302 return ldb_operr(samdb);
3305 ret = dsdb_search_dn(samdb, tmp_ctx, &res, dn, attrs,
3306 DSDB_SEARCH_SHOW_DELETED |
3307 DSDB_SEARCH_SHOW_RECYCLED);
3308 if (ret != LDB_SUCCESS) {
3309 talloc_free(tmp_ctx);
3310 return ret;
3313 (*wkguid_dn) = talloc_steal(mem_ctx, res->msgs[0]->dn);
3314 talloc_free(tmp_ctx);
3315 return LDB_SUCCESS;
3319 static int dsdb_dn_compare_ptrs(struct ldb_dn **dn1, struct ldb_dn **dn2)
3321 return ldb_dn_compare(*dn1, *dn2);
3325 find a NC root given a DN within the NC
3327 int dsdb_find_nc_root(struct ldb_context *samdb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
3328 struct ldb_dn **nc_root)
3330 const char *root_attrs[] = { "namingContexts", NULL };
3331 TALLOC_CTX *tmp_ctx;
3332 int ret;
3333 struct ldb_message_element *el;
3334 struct ldb_result *root_res;
3335 unsigned int i;
3336 struct ldb_dn **nc_dns;
3338 tmp_ctx = talloc_new(samdb);
3339 if (tmp_ctx == NULL) {
3340 return ldb_oom(samdb);
3343 ret = ldb_search(samdb, tmp_ctx, &root_res,
3344 ldb_dn_new(tmp_ctx, samdb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
3345 if (ret != LDB_SUCCESS) {
3346 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb)));
3347 talloc_free(tmp_ctx);
3348 return ret;
3351 el = ldb_msg_find_element(root_res->msgs[0], "namingContexts");
3352 if ((el == NULL) || (el->num_values < 3)) {
3353 struct ldb_message *tmp_msg;
3355 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list."));
3357 /* This generates a temporary list of NCs in order to let the
3358 * provisioning work. */
3359 tmp_msg = ldb_msg_new(tmp_ctx);
3360 if (tmp_msg == NULL) {
3361 talloc_free(tmp_ctx);
3362 return ldb_oom(samdb);
3364 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3365 ldb_dn_alloc_linearized(tmp_msg, ldb_get_schema_basedn(samdb)));
3366 if (ret != LDB_SUCCESS) {
3367 talloc_free(tmp_ctx);
3368 return ret;
3370 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3371 ldb_dn_alloc_linearized(tmp_msg, ldb_get_config_basedn(samdb)));
3372 if (ret != LDB_SUCCESS) {
3373 talloc_free(tmp_ctx);
3374 return ret;
3376 ret = ldb_msg_add_steal_string(tmp_msg, "namingContexts",
3377 ldb_dn_alloc_linearized(tmp_msg, ldb_get_default_basedn(samdb)));
3378 if (ret != LDB_SUCCESS) {
3379 talloc_free(tmp_ctx);
3380 return ret;
3382 el = &tmp_msg->elements[0];
3385 nc_dns = talloc_array(tmp_ctx, struct ldb_dn *, el->num_values);
3386 if (!nc_dns) {
3387 talloc_free(tmp_ctx);
3388 return ldb_oom(samdb);
3391 for (i=0; i<el->num_values; i++) {
3392 nc_dns[i] = ldb_dn_from_ldb_val(nc_dns, samdb, &el->values[i]);
3393 if (nc_dns[i] == NULL) {
3394 talloc_free(tmp_ctx);
3395 return ldb_operr(samdb);
3399 TYPESAFE_QSORT(nc_dns, el->num_values, dsdb_dn_compare_ptrs);
3401 for (i=0; i<el->num_values; i++) {
3402 if (ldb_dn_compare_base(nc_dns[i], dn) == 0) {
3403 (*nc_root) = talloc_steal(mem_ctx, nc_dns[i]);
3404 talloc_free(tmp_ctx);
3405 return LDB_SUCCESS;
3409 talloc_free(tmp_ctx);
3410 return LDB_ERR_NO_SUCH_OBJECT;
3415 find the deleted objects DN for any object, by looking for the NC
3416 root, then looking up the wellknown GUID
3418 int dsdb_get_deleted_objects_dn(struct ldb_context *ldb,
3419 TALLOC_CTX *mem_ctx, struct ldb_dn *obj_dn,
3420 struct ldb_dn **do_dn)
3422 struct ldb_dn *nc_root;
3423 int ret;
3425 ret = dsdb_find_nc_root(ldb, mem_ctx, obj_dn, &nc_root);
3426 if (ret != LDB_SUCCESS) {
3427 return ret;
3430 ret = dsdb_wellknown_dn(ldb, mem_ctx, nc_root, DS_GUID_DELETED_OBJECTS_CONTAINER, do_dn);
3431 talloc_free(nc_root);
3432 return ret;
3436 return the tombstoneLifetime, in days
3438 int dsdb_tombstone_lifetime(struct ldb_context *ldb, uint32_t *lifetime)
3440 struct ldb_dn *dn;
3441 dn = ldb_get_config_basedn(ldb);
3442 if (!dn) {
3443 return LDB_ERR_NO_SUCH_OBJECT;
3445 dn = ldb_dn_copy(ldb, dn);
3446 if (!dn) {
3447 return ldb_operr(ldb);
3449 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3450 be a wellknown GUID for this */
3451 if (!ldb_dn_add_child_fmt(dn, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3452 talloc_free(dn);
3453 return ldb_operr(ldb);
3456 *lifetime = samdb_search_uint(ldb, dn, 180, dn, "tombstoneLifetime", "objectClass=nTDSService");
3457 talloc_free(dn);
3458 return LDB_SUCCESS;
3462 compare a ldb_val to a string case insensitively
3464 int samdb_ldb_val_case_cmp(const char *s, struct ldb_val *v)
3466 size_t len = strlen(s);
3467 int ret;
3468 if (len > v->length) return 1;
3469 ret = strncasecmp(s, (const char *)v->data, v->length);
3470 if (ret != 0) return ret;
3471 if (v->length > len && v->data[len] != 0) {
3472 return -1;
3474 return 0;
3479 load the UDV for a partition in v2 format
3480 The list is returned sorted, and with our local cursor added
3482 int dsdb_load_udv_v2(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3483 struct drsuapi_DsReplicaCursor2 **cursors, uint32_t *count)
3485 static const char *attrs[] = { "replUpToDateVector", NULL };
3486 struct ldb_result *r;
3487 const struct ldb_val *ouv_value;
3488 unsigned int i;
3489 int ret;
3490 uint64_t highest_usn = 0;
3491 const struct GUID *our_invocation_id;
3492 static const struct timeval tv1970;
3493 NTTIME nt1970 = timeval_to_nttime(&tv1970);
3495 ret = ldb_search(samdb, mem_ctx, &r, dn, LDB_SCOPE_BASE, attrs, NULL);
3496 if (ret != LDB_SUCCESS) {
3497 return ret;
3500 ouv_value = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
3501 if (ouv_value) {
3502 enum ndr_err_code ndr_err;
3503 struct replUpToDateVectorBlob ouv;
3505 ndr_err = ndr_pull_struct_blob(ouv_value, r, &ouv,
3506 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
3507 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3508 talloc_free(r);
3509 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3511 if (ouv.version != 2) {
3512 /* we always store as version 2, and
3513 * replUpToDateVector is not replicated
3515 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
3518 *count = ouv.ctr.ctr2.count;
3519 *cursors = talloc_steal(mem_ctx, ouv.ctr.ctr2.cursors);
3520 } else {
3521 *count = 0;
3522 *cursors = NULL;
3525 talloc_free(r);
3527 our_invocation_id = samdb_ntds_invocation_id(samdb);
3528 if (!our_invocation_id) {
3529 DEBUG(0,(__location__ ": No invocationID on samdb - %s\n", ldb_errstring(samdb)));
3530 talloc_free(*cursors);
3531 return ldb_operr(samdb);
3534 ret = ldb_sequence_number(samdb, LDB_SEQ_HIGHEST_SEQ, &highest_usn);
3535 if (ret != LDB_SUCCESS) {
3536 /* nothing to add - this can happen after a vampire */
3537 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3538 return LDB_SUCCESS;
3541 for (i=0; i<*count; i++) {
3542 if (GUID_equal(our_invocation_id, &(*cursors)[i].source_dsa_invocation_id)) {
3543 (*cursors)[i].highest_usn = highest_usn;
3544 (*cursors)[i].last_sync_success = nt1970;
3545 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3546 return LDB_SUCCESS;
3550 (*cursors) = talloc_realloc(mem_ctx, *cursors, struct drsuapi_DsReplicaCursor2, (*count)+1);
3551 if (! *cursors) {
3552 return ldb_oom(samdb);
3555 (*cursors)[*count].source_dsa_invocation_id = *our_invocation_id;
3556 (*cursors)[*count].highest_usn = highest_usn;
3557 (*cursors)[*count].last_sync_success = nt1970;
3558 (*count)++;
3560 TYPESAFE_QSORT(*cursors, *count, drsuapi_DsReplicaCursor2_compare);
3562 return LDB_SUCCESS;
3566 load the UDV for a partition in version 1 format
3567 The list is returned sorted, and with our local cursor added
3569 int dsdb_load_udv_v1(struct ldb_context *samdb, struct ldb_dn *dn, TALLOC_CTX *mem_ctx,
3570 struct drsuapi_DsReplicaCursor **cursors, uint32_t *count)
3572 struct drsuapi_DsReplicaCursor2 *v2;
3573 uint32_t i;
3574 int ret;
3576 ret = dsdb_load_udv_v2(samdb, dn, mem_ctx, &v2, count);
3577 if (ret != LDB_SUCCESS) {
3578 return ret;
3581 if (*count == 0) {
3582 talloc_free(v2);
3583 *cursors = NULL;
3584 return LDB_SUCCESS;
3587 *cursors = talloc_array(mem_ctx, struct drsuapi_DsReplicaCursor, *count);
3588 if (*cursors == NULL) {
3589 talloc_free(v2);
3590 return ldb_oom(samdb);
3593 for (i=0; i<*count; i++) {
3594 (*cursors)[i].source_dsa_invocation_id = v2[i].source_dsa_invocation_id;
3595 (*cursors)[i].highest_usn = v2[i].highest_usn;
3597 talloc_free(v2);
3598 return LDB_SUCCESS;
3602 add a set of controls to a ldb_request structure based on a set of
3603 flags. See util.h for a list of available flags
3605 int dsdb_request_add_controls(struct ldb_request *req, uint32_t dsdb_flags)
3607 int ret;
3608 if (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) {
3609 struct ldb_search_options_control *options;
3610 /* Using the phantom root control allows us to search all partitions */
3611 options = talloc(req, struct ldb_search_options_control);
3612 if (options == NULL) {
3613 return LDB_ERR_OPERATIONS_ERROR;
3615 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
3617 ret = ldb_request_add_control(req,
3618 LDB_CONTROL_SEARCH_OPTIONS_OID,
3619 true, options);
3620 if (ret != LDB_SUCCESS) {
3621 return ret;
3625 if (dsdb_flags & DSDB_SEARCH_NO_GLOBAL_CATALOG) {
3626 ret = ldb_request_add_control(req,
3627 DSDB_CONTROL_NO_GLOBAL_CATALOG,
3628 false, NULL);
3629 if (ret != LDB_SUCCESS) {
3630 return ret;
3634 if (dsdb_flags & DSDB_SEARCH_SHOW_DELETED) {
3635 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
3636 if (ret != LDB_SUCCESS) {
3637 return ret;
3641 if (dsdb_flags & DSDB_SEARCH_SHOW_RECYCLED) {
3642 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, false, NULL);
3643 if (ret != LDB_SUCCESS) {
3644 return ret;
3648 if (dsdb_flags & DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT) {
3649 ret = ldb_request_add_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID, false, NULL);
3650 if (ret != LDB_SUCCESS) {
3651 return ret;
3655 if (dsdb_flags & DSDB_SEARCH_SHOW_EXTENDED_DN) {
3656 struct ldb_extended_dn_control *extended_ctrl = talloc(req, struct ldb_extended_dn_control);
3657 if (!extended_ctrl) {
3658 return LDB_ERR_OPERATIONS_ERROR;
3660 extended_ctrl->type = 1;
3662 ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_ctrl);
3663 if (ret != LDB_SUCCESS) {
3664 return ret;
3668 if (dsdb_flags & DSDB_SEARCH_REVEAL_INTERNALS) {
3669 ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
3670 if (ret != LDB_SUCCESS) {
3671 return ret;
3675 if (dsdb_flags & DSDB_MODIFY_RELAX) {
3676 ret = ldb_request_add_control(req, LDB_CONTROL_RELAX_OID, false, NULL);
3677 if (ret != LDB_SUCCESS) {
3678 return ret;
3682 if (dsdb_flags & DSDB_MODIFY_PERMISSIVE) {
3683 ret = ldb_request_add_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID, false, NULL);
3684 if (ret != LDB_SUCCESS) {
3685 return ret;
3689 if (dsdb_flags & DSDB_FLAG_AS_SYSTEM) {
3690 ret = ldb_request_add_control(req, LDB_CONTROL_AS_SYSTEM_OID, false, NULL);
3691 if (ret != LDB_SUCCESS) {
3692 return ret;
3696 if (dsdb_flags & DSDB_TREE_DELETE) {
3697 ret = ldb_request_add_control(req, LDB_CONTROL_TREE_DELETE_OID, false, NULL);
3698 if (ret != LDB_SUCCESS) {
3699 return ret;
3703 if (dsdb_flags & DSDB_PROVISION) {
3704 ret = ldb_request_add_control(req, LDB_CONTROL_PROVISION_OID, false, NULL);
3705 if (ret != LDB_SUCCESS) {
3706 return ret;
3710 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
3711 if (dsdb_flags & DSDB_BYPASS_PASSWORD_HASH) {
3712 ret = ldb_request_add_control(req, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, true, NULL);
3713 if (ret != LDB_SUCCESS) {
3714 return ret;
3718 if (dsdb_flags & DSDB_PASSWORD_BYPASS_LAST_SET) {
3720 * This must not be critical, as it will only be
3721 * handled (and need to be handled) if the other
3722 * attributes in the request bring password_hash into
3723 * action
3725 ret = ldb_request_add_control(req, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID, false, NULL);
3726 if (ret != LDB_SUCCESS) {
3727 return ret;
3731 if (dsdb_flags & DSDB_MODIFY_PARTIAL_REPLICA) {
3732 ret = ldb_request_add_control(req, DSDB_CONTROL_PARTIAL_REPLICA, false, NULL);
3733 if (ret != LDB_SUCCESS) {
3734 return ret;
3738 return LDB_SUCCESS;
3742 an add with a set of controls
3744 int dsdb_add(struct ldb_context *ldb, const struct ldb_message *message,
3745 uint32_t dsdb_flags)
3747 struct ldb_request *req;
3748 int ret;
3750 ret = ldb_build_add_req(&req, ldb, ldb,
3751 message,
3752 NULL,
3753 NULL,
3754 ldb_op_default_callback,
3755 NULL);
3757 if (ret != LDB_SUCCESS) return ret;
3759 ret = dsdb_request_add_controls(req, dsdb_flags);
3760 if (ret != LDB_SUCCESS) {
3761 talloc_free(req);
3762 return ret;
3765 ret = dsdb_autotransaction_request(ldb, req);
3767 talloc_free(req);
3768 return ret;
3772 a modify with a set of controls
3774 int dsdb_modify(struct ldb_context *ldb, const struct ldb_message *message,
3775 uint32_t dsdb_flags)
3777 struct ldb_request *req;
3778 int ret;
3780 ret = ldb_build_mod_req(&req, ldb, ldb,
3781 message,
3782 NULL,
3783 NULL,
3784 ldb_op_default_callback,
3785 NULL);
3787 if (ret != LDB_SUCCESS) return ret;
3789 ret = dsdb_request_add_controls(req, dsdb_flags);
3790 if (ret != LDB_SUCCESS) {
3791 talloc_free(req);
3792 return ret;
3795 ret = dsdb_autotransaction_request(ldb, req);
3797 talloc_free(req);
3798 return ret;
3802 a delete with a set of flags
3804 int dsdb_delete(struct ldb_context *ldb, struct ldb_dn *dn,
3805 uint32_t dsdb_flags)
3807 struct ldb_request *req;
3808 int ret;
3810 ret = ldb_build_del_req(&req, ldb, ldb,
3812 NULL,
3813 NULL,
3814 ldb_op_default_callback,
3815 NULL);
3817 if (ret != LDB_SUCCESS) return ret;
3819 ret = dsdb_request_add_controls(req, dsdb_flags);
3820 if (ret != LDB_SUCCESS) {
3821 talloc_free(req);
3822 return ret;
3825 ret = dsdb_autotransaction_request(ldb, req);
3827 talloc_free(req);
3828 return ret;
3832 like dsdb_modify() but set all the element flags to
3833 LDB_FLAG_MOD_REPLACE
3835 int dsdb_replace(struct ldb_context *ldb, struct ldb_message *msg, uint32_t dsdb_flags)
3837 unsigned int i;
3839 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3840 for (i=0;i<msg->num_elements;i++) {
3841 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
3844 return dsdb_modify(ldb, msg, dsdb_flags);
3849 search for attrs on one DN, allowing for dsdb_flags controls
3851 int dsdb_search_dn(struct ldb_context *ldb,
3852 TALLOC_CTX *mem_ctx,
3853 struct ldb_result **_result,
3854 struct ldb_dn *basedn,
3855 const char * const *attrs,
3856 uint32_t dsdb_flags)
3858 int ret;
3859 struct ldb_request *req;
3860 struct ldb_result *res;
3862 res = talloc_zero(mem_ctx, struct ldb_result);
3863 if (!res) {
3864 return ldb_oom(ldb);
3867 ret = ldb_build_search_req(&req, ldb, res,
3868 basedn,
3869 LDB_SCOPE_BASE,
3870 NULL,
3871 attrs,
3872 NULL,
3873 res,
3874 ldb_search_default_callback,
3875 NULL);
3876 if (ret != LDB_SUCCESS) {
3877 talloc_free(res);
3878 return ret;
3881 ret = dsdb_request_add_controls(req, dsdb_flags);
3882 if (ret != LDB_SUCCESS) {
3883 talloc_free(res);
3884 return ret;
3887 ret = ldb_request(ldb, req);
3888 if (ret == LDB_SUCCESS) {
3889 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3892 talloc_free(req);
3893 if (ret != LDB_SUCCESS) {
3894 talloc_free(res);
3895 return ret;
3898 *_result = res;
3899 return LDB_SUCCESS;
3903 search for attrs on one DN, by the GUID of the DN, allowing for
3904 dsdb_flags controls
3906 int dsdb_search_by_dn_guid(struct ldb_context *ldb,
3907 TALLOC_CTX *mem_ctx,
3908 struct ldb_result **_result,
3909 const struct GUID *guid,
3910 const char * const *attrs,
3911 uint32_t dsdb_flags)
3913 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3914 struct ldb_dn *dn;
3915 int ret;
3917 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "<GUID=%s>", GUID_string(tmp_ctx, guid));
3918 if (dn == NULL) {
3919 talloc_free(tmp_ctx);
3920 return ldb_oom(ldb);
3923 ret = dsdb_search_dn(ldb, mem_ctx, _result, dn, attrs, dsdb_flags);
3924 talloc_free(tmp_ctx);
3925 return ret;
3929 general search with dsdb_flags for controls
3931 int dsdb_search(struct ldb_context *ldb,
3932 TALLOC_CTX *mem_ctx,
3933 struct ldb_result **_result,
3934 struct ldb_dn *basedn,
3935 enum ldb_scope scope,
3936 const char * const *attrs,
3937 uint32_t dsdb_flags,
3938 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
3940 int ret;
3941 struct ldb_request *req;
3942 struct ldb_result *res;
3943 va_list ap;
3944 char *expression = NULL;
3945 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3947 /* cross-partitions searches with a basedn break multi-domain support */
3948 SMB_ASSERT(basedn == NULL || (dsdb_flags & DSDB_SEARCH_SEARCH_ALL_PARTITIONS) == 0);
3950 res = talloc_zero(tmp_ctx, struct ldb_result);
3951 if (!res) {
3952 talloc_free(tmp_ctx);
3953 return ldb_oom(ldb);
3956 if (exp_fmt) {
3957 va_start(ap, exp_fmt);
3958 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
3959 va_end(ap);
3961 if (!expression) {
3962 talloc_free(tmp_ctx);
3963 return ldb_oom(ldb);
3967 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
3968 basedn,
3969 scope,
3970 expression,
3971 attrs,
3972 NULL,
3973 res,
3974 ldb_search_default_callback,
3975 NULL);
3976 if (ret != LDB_SUCCESS) {
3977 talloc_free(tmp_ctx);
3978 return ret;
3981 ret = dsdb_request_add_controls(req, dsdb_flags);
3982 if (ret != LDB_SUCCESS) {
3983 talloc_free(tmp_ctx);
3984 ldb_reset_err_string(ldb);
3985 return ret;
3988 ret = ldb_request(ldb, req);
3989 if (ret == LDB_SUCCESS) {
3990 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
3993 if (ret != LDB_SUCCESS) {
3994 talloc_free(tmp_ctx);
3995 return ret;
3998 if (dsdb_flags & DSDB_SEARCH_ONE_ONLY) {
3999 if (res->count == 0) {
4000 talloc_free(tmp_ctx);
4001 ldb_reset_err_string(ldb);
4002 return LDB_ERR_NO_SUCH_OBJECT;
4004 if (res->count != 1) {
4005 talloc_free(tmp_ctx);
4006 ldb_reset_err_string(ldb);
4007 return LDB_ERR_CONSTRAINT_VIOLATION;
4011 *_result = talloc_steal(mem_ctx, res);
4012 talloc_free(tmp_ctx);
4014 return LDB_SUCCESS;
4019 general search with dsdb_flags for controls
4020 returns exactly 1 record or an error
4022 int dsdb_search_one(struct ldb_context *ldb,
4023 TALLOC_CTX *mem_ctx,
4024 struct ldb_message **msg,
4025 struct ldb_dn *basedn,
4026 enum ldb_scope scope,
4027 const char * const *attrs,
4028 uint32_t dsdb_flags,
4029 const char *exp_fmt, ...) _PRINTF_ATTRIBUTE(8, 9)
4031 int ret;
4032 struct ldb_result *res;
4033 va_list ap;
4034 char *expression = NULL;
4035 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4037 dsdb_flags |= DSDB_SEARCH_ONE_ONLY;
4039 res = talloc_zero(tmp_ctx, struct ldb_result);
4040 if (!res) {
4041 talloc_free(tmp_ctx);
4042 return ldb_oom(ldb);
4045 if (exp_fmt) {
4046 va_start(ap, exp_fmt);
4047 expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
4048 va_end(ap);
4050 if (!expression) {
4051 talloc_free(tmp_ctx);
4052 return ldb_oom(ldb);
4054 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4055 dsdb_flags, "%s", expression);
4056 } else {
4057 ret = dsdb_search(ldb, tmp_ctx, &res, basedn, scope, attrs,
4058 dsdb_flags, NULL);
4061 if (ret != LDB_SUCCESS) {
4062 talloc_free(tmp_ctx);
4063 return ret;
4066 *msg = talloc_steal(mem_ctx, res->msgs[0]);
4067 talloc_free(tmp_ctx);
4069 return LDB_SUCCESS;
4072 /* returns back the forest DNS name */
4073 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4075 const char *forest_name = ldb_dn_canonical_string(mem_ctx,
4076 ldb_get_root_basedn(ldb));
4077 char *p;
4079 if (forest_name == NULL) {
4080 return NULL;
4083 p = strchr(forest_name, '/');
4084 if (p) {
4085 *p = '\0';
4088 return forest_name;
4091 /* returns back the default domain DNS name */
4092 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
4094 const char *domain_name = ldb_dn_canonical_string(mem_ctx,
4095 ldb_get_default_basedn(ldb));
4096 char *p;
4098 if (domain_name == NULL) {
4099 return NULL;
4102 p = strchr(domain_name, '/');
4103 if (p) {
4104 *p = '\0';
4107 return domain_name;
4111 validate that an DSA GUID belongs to the specified user sid.
4112 The user SID must be a domain controller account (either RODC or
4113 RWDC)
4115 int dsdb_validate_dsa_guid(struct ldb_context *ldb,
4116 const struct GUID *dsa_guid,
4117 const struct dom_sid *sid)
4119 /* strategy:
4120 - find DN of record with the DSA GUID in the
4121 configuration partition (objectGUID)
4122 - remove "NTDS Settings" component from DN
4123 - do a base search on that DN for serverReference with
4124 extended-dn enabled
4125 - extract objectSid from resulting serverReference
4126 attribute
4127 - check this sid matches the sid argument
4129 struct ldb_dn *config_dn;
4130 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4131 struct ldb_message *msg;
4132 const char *attrs1[] = { NULL };
4133 const char *attrs2[] = { "serverReference", NULL };
4134 int ret;
4135 struct ldb_dn *dn, *account_dn;
4136 struct dom_sid sid2;
4137 NTSTATUS status;
4139 config_dn = ldb_get_config_basedn(ldb);
4141 ret = dsdb_search_one(ldb, tmp_ctx, &msg, config_dn, LDB_SCOPE_SUBTREE,
4142 attrs1, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx, dsa_guid));
4143 if (ret != LDB_SUCCESS) {
4144 DEBUG(1,(__location__ ": Failed to find DSA objectGUID %s for sid %s\n",
4145 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4146 talloc_free(tmp_ctx);
4147 return ldb_operr(ldb);
4149 dn = msg->dn;
4151 if (!ldb_dn_remove_child_components(dn, 1)) {
4152 talloc_free(tmp_ctx);
4153 return ldb_operr(ldb);
4156 ret = dsdb_search_one(ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE,
4157 attrs2, DSDB_SEARCH_SHOW_EXTENDED_DN,
4158 "(objectClass=server)");
4159 if (ret != LDB_SUCCESS) {
4160 DEBUG(1,(__location__ ": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4161 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4162 talloc_free(tmp_ctx);
4163 return ldb_operr(ldb);
4166 account_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, msg, "serverReference");
4167 if (account_dn == NULL) {
4168 DEBUG(1,(__location__ ": Failed to find account_dn for DSA with objectGUID %s, sid %s\n",
4169 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4170 talloc_free(tmp_ctx);
4171 return ldb_operr(ldb);
4174 status = dsdb_get_extended_dn_sid(account_dn, &sid2, "SID");
4175 if (!NT_STATUS_IS_OK(status)) {
4176 DEBUG(1,(__location__ ": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4177 GUID_string(tmp_ctx, dsa_guid), dom_sid_string(tmp_ctx, sid)));
4178 talloc_free(tmp_ctx);
4179 return ldb_operr(ldb);
4182 if (!dom_sid_equal(sid, &sid2)) {
4183 /* someone is trying to spoof another account */
4184 DEBUG(0,(__location__ ": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4185 GUID_string(tmp_ctx, dsa_guid),
4186 dom_sid_string(tmp_ctx, sid),
4187 dom_sid_string(tmp_ctx, &sid2)));
4188 talloc_free(tmp_ctx);
4189 return ldb_operr(ldb);
4192 talloc_free(tmp_ctx);
4193 return LDB_SUCCESS;
4196 static const char * const secret_attributes[] = {
4197 DSDB_SECRET_ATTRIBUTES,
4198 NULL
4202 check if the attribute belongs to the RODC filtered attribute set
4203 Note that attributes that are in the filtered attribute set are the
4204 ones that _are_ always sent to a RODC
4206 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute *sa)
4208 /* they never get secret attributes */
4209 if (is_attr_in_list(secret_attributes, sa->lDAPDisplayName)) {
4210 return false;
4213 /* they do get non-secret critical attributes */
4214 if (sa->schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) {
4215 return true;
4218 /* they do get non-secret attributes marked as being in the FAS */
4219 if (sa->searchFlags & SEARCH_FLAG_RODC_ATTRIBUTE) {
4220 return true;
4223 /* other attributes are denied */
4224 return false;
4227 /* return fsmo role dn and role owner dn for a particular role*/
4228 WERROR dsdb_get_fsmo_role_info(TALLOC_CTX *tmp_ctx,
4229 struct ldb_context *ldb,
4230 uint32_t role,
4231 struct ldb_dn **fsmo_role_dn,
4232 struct ldb_dn **role_owner_dn)
4234 int ret;
4235 switch (role) {
4236 case DREPL_NAMING_MASTER:
4237 *fsmo_role_dn = samdb_partitions_dn(ldb, tmp_ctx);
4238 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4239 if (ret != LDB_SUCCESS) {
4240 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Naming Master object - %s",
4241 ldb_errstring(ldb)));
4242 talloc_free(tmp_ctx);
4243 return WERR_DS_DRA_INTERNAL_ERROR;
4245 break;
4246 case DREPL_INFRASTRUCTURE_MASTER:
4247 *fsmo_role_dn = samdb_infrastructure_dn(ldb, tmp_ctx);
4248 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4249 if (ret != LDB_SUCCESS) {
4250 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4251 ldb_errstring(ldb)));
4252 talloc_free(tmp_ctx);
4253 return WERR_DS_DRA_INTERNAL_ERROR;
4255 break;
4256 case DREPL_RID_MASTER:
4257 ret = samdb_rid_manager_dn(ldb, tmp_ctx, fsmo_role_dn);
4258 if (ret != LDB_SUCCESS) {
4259 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s", ldb_errstring(ldb)));
4260 talloc_free(tmp_ctx);
4261 return WERR_DS_DRA_INTERNAL_ERROR;
4264 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4265 if (ret != LDB_SUCCESS) {
4266 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s",
4267 ldb_errstring(ldb)));
4268 talloc_free(tmp_ctx);
4269 return WERR_DS_DRA_INTERNAL_ERROR;
4271 break;
4272 case DREPL_SCHEMA_MASTER:
4273 *fsmo_role_dn = ldb_get_schema_basedn(ldb);
4274 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4275 if (ret != LDB_SUCCESS) {
4276 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Schema Master object - %s",
4277 ldb_errstring(ldb)));
4278 talloc_free(tmp_ctx);
4279 return WERR_DS_DRA_INTERNAL_ERROR;
4281 break;
4282 case DREPL_PDC_MASTER:
4283 *fsmo_role_dn = ldb_get_default_basedn(ldb);
4284 ret = samdb_reference_dn(ldb, tmp_ctx, *fsmo_role_dn, "fSMORoleOwner", role_owner_dn);
4285 if (ret != LDB_SUCCESS) {
4286 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in Pd Master object - %s",
4287 ldb_errstring(ldb)));
4288 talloc_free(tmp_ctx);
4289 return WERR_DS_DRA_INTERNAL_ERROR;
4291 break;
4292 default:
4293 return WERR_DS_DRA_INTERNAL_ERROR;
4295 return WERR_OK;
4298 const char *samdb_dn_to_dnshostname(struct ldb_context *ldb,
4299 TALLOC_CTX *mem_ctx,
4300 struct ldb_dn *server_dn)
4302 int ldb_ret;
4303 struct ldb_result *res = NULL;
4304 const char * const attrs[] = { "dNSHostName", NULL};
4306 ldb_ret = ldb_search(ldb, mem_ctx, &res,
4307 server_dn,
4308 LDB_SCOPE_BASE,
4309 attrs, NULL);
4310 if (ldb_ret != LDB_SUCCESS) {
4311 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
4312 ldb_dn_get_linearized(server_dn), ldb_errstring(ldb)));
4313 return NULL;
4316 return ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
4320 returns true if an attribute is in the filter,
4321 false otherwise, provided that attribute value is provided with the expression
4323 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree *tree,
4324 const char *attr)
4326 unsigned int i;
4327 switch (tree->operation) {
4328 case LDB_OP_AND:
4329 case LDB_OP_OR:
4330 for (i=0;i<tree->u.list.num_elements;i++) {
4331 if (dsdb_attr_in_parse_tree(tree->u.list.elements[i],
4332 attr))
4333 return true;
4335 return false;
4336 case LDB_OP_NOT:
4337 return dsdb_attr_in_parse_tree(tree->u.isnot.child, attr);
4338 case LDB_OP_EQUALITY:
4339 case LDB_OP_GREATER:
4340 case LDB_OP_LESS:
4341 case LDB_OP_APPROX:
4342 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
4343 return true;
4345 return false;
4346 case LDB_OP_SUBSTRING:
4347 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
4348 return true;
4350 return false;
4351 case LDB_OP_PRESENT:
4352 /* (attrname=*) is not filtered out */
4353 return false;
4354 case LDB_OP_EXTENDED:
4355 if (tree->u.extended.attr &&
4356 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
4357 return true;
4359 return false;
4361 return false;
4364 bool is_attr_in_list(const char * const * attrs, const char *attr)
4366 unsigned int i;
4368 for (i = 0; attrs[i]; i++) {
4369 if (ldb_attr_cmp(attrs[i], attr) == 0)
4370 return true;
4373 return false;
4378 map an ldb error code to an approximate NTSTATUS code
4380 NTSTATUS dsdb_ldb_err_to_ntstatus(int err)
4382 switch (err) {
4383 case LDB_SUCCESS:
4384 return NT_STATUS_OK;
4386 case LDB_ERR_PROTOCOL_ERROR:
4387 return NT_STATUS_DEVICE_PROTOCOL_ERROR;
4389 case LDB_ERR_TIME_LIMIT_EXCEEDED:
4390 return NT_STATUS_IO_TIMEOUT;
4392 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
4393 return NT_STATUS_BUFFER_TOO_SMALL;
4395 case LDB_ERR_COMPARE_FALSE:
4396 case LDB_ERR_COMPARE_TRUE:
4397 return NT_STATUS_REVISION_MISMATCH;
4399 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
4400 return NT_STATUS_NOT_SUPPORTED;
4402 case LDB_ERR_STRONG_AUTH_REQUIRED:
4403 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
4404 case LDB_ERR_SASL_BIND_IN_PROGRESS:
4405 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
4406 case LDB_ERR_INVALID_CREDENTIALS:
4407 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
4408 case LDB_ERR_UNWILLING_TO_PERFORM:
4409 return NT_STATUS_ACCESS_DENIED;
4411 case LDB_ERR_NO_SUCH_OBJECT:
4412 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4414 case LDB_ERR_REFERRAL:
4415 case LDB_ERR_NO_SUCH_ATTRIBUTE:
4416 return NT_STATUS_NOT_FOUND;
4418 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
4419 return NT_STATUS_NOT_SUPPORTED;
4421 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
4422 return NT_STATUS_BUFFER_TOO_SMALL;
4424 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
4425 case LDB_ERR_INAPPROPRIATE_MATCHING:
4426 case LDB_ERR_CONSTRAINT_VIOLATION:
4427 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
4428 case LDB_ERR_INVALID_DN_SYNTAX:
4429 case LDB_ERR_NAMING_VIOLATION:
4430 case LDB_ERR_OBJECT_CLASS_VIOLATION:
4431 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
4432 case LDB_ERR_NOT_ALLOWED_ON_RDN:
4433 return NT_STATUS_INVALID_PARAMETER;
4435 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
4436 case LDB_ERR_ENTRY_ALREADY_EXISTS:
4437 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS;
4439 case LDB_ERR_BUSY:
4440 return NT_STATUS_NETWORK_BUSY;
4442 case LDB_ERR_ALIAS_PROBLEM:
4443 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
4444 case LDB_ERR_UNAVAILABLE:
4445 case LDB_ERR_LOOP_DETECT:
4446 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
4447 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
4448 case LDB_ERR_OTHER:
4449 case LDB_ERR_OPERATIONS_ERROR:
4450 break;
4452 return NT_STATUS_UNSUCCESSFUL;
4457 create a new naming context that will hold a partial replica
4459 int dsdb_create_partial_replica_NC(struct ldb_context *ldb, struct ldb_dn *dn)
4461 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
4462 struct ldb_message *msg;
4463 int ret;
4465 msg = ldb_msg_new(tmp_ctx);
4466 if (msg == NULL) {
4467 talloc_free(tmp_ctx);
4468 return ldb_oom(ldb);
4471 msg->dn = dn;
4472 ret = ldb_msg_add_string(msg, "objectClass", "top");
4473 if (ret != LDB_SUCCESS) {
4474 talloc_free(tmp_ctx);
4475 return ldb_oom(ldb);
4478 /* [MS-DRSR] implies that we should only add the 'top'
4479 * objectclass, but that would cause lots of problems with our
4480 * objectclass code as top is not structural, so we add
4481 * 'domainDNS' as well to keep things sane. We're expecting
4482 * this new NC to be of objectclass domainDNS after
4483 * replication anyway
4485 ret = ldb_msg_add_string(msg, "objectClass", "domainDNS");
4486 if (ret != LDB_SUCCESS) {
4487 talloc_free(tmp_ctx);
4488 return ldb_oom(ldb);
4491 ret = ldb_msg_add_fmt(msg, "instanceType", "%u",
4492 INSTANCE_TYPE_IS_NC_HEAD|
4493 INSTANCE_TYPE_NC_ABOVE|
4494 INSTANCE_TYPE_UNINSTANT);
4495 if (ret != LDB_SUCCESS) {
4496 talloc_free(tmp_ctx);
4497 return ldb_oom(ldb);
4500 ret = dsdb_add(ldb, msg, DSDB_MODIFY_PARTIAL_REPLICA);
4501 if (ret != LDB_SUCCESS && ret != LDB_ERR_ENTRY_ALREADY_EXISTS) {
4502 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
4503 ldb_dn_get_linearized(dn),
4504 ldb_errstring(ldb), ldb_strerror(ret)));
4505 talloc_free(tmp_ctx);
4506 return ret;
4509 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn)));
4511 talloc_free(tmp_ctx);
4512 return LDB_SUCCESS;
4516 build a GUID from a string
4518 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
4520 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
4521 uint32_t time_low;
4522 uint32_t time_mid, time_hi_and_version;
4523 uint32_t clock_seq[2];
4524 uint32_t node[6];
4525 int i;
4527 if (s == NULL) {
4528 return NT_STATUS_INVALID_PARAMETER;
4531 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
4532 &time_low, &time_mid, &time_hi_and_version,
4533 &clock_seq[0], &clock_seq[1],
4534 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
4535 status = NT_STATUS_OK;
4538 if (!NT_STATUS_IS_OK(status)) {
4539 return status;
4542 guid->time_low = time_low;
4543 guid->time_mid = time_mid;
4544 guid->time_hi_and_version = time_hi_and_version;
4545 guid->clock_seq[0] = clock_seq[0];
4546 guid->clock_seq[1] = clock_seq[1];
4547 for (i=0;i<6;i++) {
4548 guid->node[i] = node[i];
4551 return NT_STATUS_OK;
4554 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
4556 return talloc_asprintf(mem_ctx,
4557 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
4558 guid->time_low, guid->time_mid,
4559 guid->time_hi_and_version,
4560 guid->clock_seq[0],
4561 guid->clock_seq[1],
4562 guid->node[0], guid->node[1],
4563 guid->node[2], guid->node[3],
4564 guid->node[4], guid->node[5]);