s4:dsdb Add extensive tests for the behaviour of dsdb_dn
[Samba/cd1.git] / source4 / dsdb / common / util.c
blobdcbb462e710a657a91cd0809af3c3b1c2fb80b4b
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_errors.h"
28 #include "../lib/util/util_ldb.h"
29 #include "../lib/crypto/crypto.h"
30 #include "dsdb/samdb/samdb.h"
31 #include "libcli/security/security.h"
32 #include "librpc/gen_ndr/ndr_security.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "../libds/common/flags.h"
35 #include "dsdb/common/proto.h"
36 #include "libcli/ldap/ldap_ndr.h"
37 #include "param/param.h"
38 #include "libcli/auth/libcli_auth.h"
39 #include "librpc/gen_ndr/ndr_drsblobs.h"
42 search the sam for the specified attributes in a specific domain, filter on
43 objectSid being in domain_sid.
45 int samdb_search_domain(struct ldb_context *sam_ldb,
46 TALLOC_CTX *mem_ctx,
47 struct ldb_dn *basedn,
48 struct ldb_message ***res,
49 const char * const *attrs,
50 const struct dom_sid *domain_sid,
51 const char *format, ...) _PRINTF_ATTRIBUTE(7,8)
53 va_list ap;
54 int i, count;
56 va_start(ap, format);
57 count = gendb_search_v(sam_ldb, mem_ctx, basedn,
58 res, attrs, format, ap);
59 va_end(ap);
61 i=0;
63 while (i<count) {
64 struct dom_sid *entry_sid;
66 entry_sid = samdb_result_dom_sid(mem_ctx, (*res)[i], "objectSid");
68 if ((entry_sid == NULL) ||
69 (!dom_sid_in_domain(domain_sid, entry_sid))) {
70 /* Delete that entry from the result set */
71 (*res)[i] = (*res)[count-1];
72 count -= 1;
73 talloc_free(entry_sid);
74 continue;
76 talloc_free(entry_sid);
77 i += 1;
80 return count;
84 search the sam for a single string attribute in exactly 1 record
86 const char *samdb_search_string_v(struct ldb_context *sam_ldb,
87 TALLOC_CTX *mem_ctx,
88 struct ldb_dn *basedn,
89 const char *attr_name,
90 const char *format, va_list ap) _PRINTF_ATTRIBUTE(5,0)
92 int count;
93 const char *attrs[2] = { NULL, NULL };
94 struct ldb_message **res = NULL;
96 attrs[0] = attr_name;
98 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
99 if (count > 1) {
100 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
101 attr_name, format, count));
103 if (count != 1) {
104 talloc_free(res);
105 return NULL;
108 return samdb_result_string(res[0], attr_name, NULL);
112 search the sam for a single string attribute in exactly 1 record
114 const char *samdb_search_string(struct ldb_context *sam_ldb,
115 TALLOC_CTX *mem_ctx,
116 struct ldb_dn *basedn,
117 const char *attr_name,
118 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
120 va_list ap;
121 const char *str;
123 va_start(ap, format);
124 str = samdb_search_string_v(sam_ldb, mem_ctx, basedn, attr_name, format, ap);
125 va_end(ap);
127 return str;
130 struct ldb_dn *samdb_search_dn(struct ldb_context *sam_ldb,
131 TALLOC_CTX *mem_ctx,
132 struct ldb_dn *basedn,
133 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
135 va_list ap;
136 struct ldb_dn *ret;
137 struct ldb_message **res = NULL;
138 int count;
140 va_start(ap, format);
141 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, NULL, format, ap);
142 va_end(ap);
144 if (count != 1) return NULL;
146 ret = talloc_steal(mem_ctx, res[0]->dn);
147 talloc_free(res);
149 return ret;
153 search the sam for a dom_sid attribute in exactly 1 record
155 struct dom_sid *samdb_search_dom_sid(struct ldb_context *sam_ldb,
156 TALLOC_CTX *mem_ctx,
157 struct ldb_dn *basedn,
158 const char *attr_name,
159 const char *format, ...) _PRINTF_ATTRIBUTE(5,6)
161 va_list ap;
162 int count;
163 struct ldb_message **res;
164 const char *attrs[2] = { NULL, NULL };
165 struct dom_sid *sid;
167 attrs[0] = attr_name;
169 va_start(ap, format);
170 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
171 va_end(ap);
172 if (count > 1) {
173 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
174 attr_name, format, count));
176 if (count != 1) {
177 talloc_free(res);
178 return NULL;
180 sid = samdb_result_dom_sid(mem_ctx, res[0], attr_name);
181 talloc_free(res);
182 return sid;
186 return the count of the number of records in the sam matching the query
188 int samdb_search_count(struct ldb_context *sam_ldb,
189 TALLOC_CTX *mem_ctx,
190 struct ldb_dn *basedn,
191 const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
193 va_list ap;
194 struct ldb_message **res;
195 const char * const attrs[] = { NULL };
196 int ret;
198 va_start(ap, format);
199 ret = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
200 va_end(ap);
202 return ret;
207 search the sam for a single integer attribute in exactly 1 record
209 uint_t samdb_search_uint(struct ldb_context *sam_ldb,
210 TALLOC_CTX *mem_ctx,
211 uint_t default_value,
212 struct ldb_dn *basedn,
213 const char *attr_name,
214 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
216 va_list ap;
217 int count;
218 struct ldb_message **res;
219 const char *attrs[2] = { NULL, NULL };
221 attrs[0] = attr_name;
223 va_start(ap, format);
224 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
225 va_end(ap);
227 if (count != 1) {
228 return default_value;
231 return samdb_result_uint(res[0], attr_name, default_value);
235 search the sam for a single signed 64 bit integer attribute in exactly 1 record
237 int64_t samdb_search_int64(struct ldb_context *sam_ldb,
238 TALLOC_CTX *mem_ctx,
239 int64_t default_value,
240 struct ldb_dn *basedn,
241 const char *attr_name,
242 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
244 va_list ap;
245 int count;
246 struct ldb_message **res;
247 const char *attrs[2] = { NULL, NULL };
249 attrs[0] = attr_name;
251 va_start(ap, format);
252 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
253 va_end(ap);
255 if (count != 1) {
256 return default_value;
259 return samdb_result_int64(res[0], attr_name, default_value);
263 search the sam for multipe records each giving a single string attribute
264 return the number of matches, or -1 on error
266 int samdb_search_string_multiple(struct ldb_context *sam_ldb,
267 TALLOC_CTX *mem_ctx,
268 struct ldb_dn *basedn,
269 const char ***strs,
270 const char *attr_name,
271 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
273 va_list ap;
274 int count, i;
275 const char *attrs[2] = { NULL, NULL };
276 struct ldb_message **res = NULL;
278 attrs[0] = attr_name;
280 va_start(ap, format);
281 count = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
282 va_end(ap);
284 if (count <= 0) {
285 return count;
288 /* make sure its single valued */
289 for (i=0;i<count;i++) {
290 if (res[i]->num_elements != 1) {
291 DEBUG(1,("samdb: search for %s %s not single valued\n",
292 attr_name, format));
293 talloc_free(res);
294 return -1;
298 *strs = talloc_array(mem_ctx, const char *, count+1);
299 if (! *strs) {
300 talloc_free(res);
301 return -1;
304 for (i=0;i<count;i++) {
305 (*strs)[i] = samdb_result_string(res[i], attr_name, NULL);
307 (*strs)[count] = NULL;
309 return count;
313 pull a uint from a result set.
315 uint_t samdb_result_uint(const struct ldb_message *msg, const char *attr, uint_t default_value)
317 return ldb_msg_find_attr_as_uint(msg, attr, default_value);
321 pull a (signed) int64 from a result set.
323 int64_t samdb_result_int64(const struct ldb_message *msg, const char *attr, int64_t default_value)
325 return ldb_msg_find_attr_as_int64(msg, attr, default_value);
329 pull a string from a result set.
331 const char *samdb_result_string(const struct ldb_message *msg, const char *attr,
332 const char *default_value)
334 return ldb_msg_find_attr_as_string(msg, attr, default_value);
337 struct ldb_dn *samdb_result_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
338 const char *attr, struct ldb_dn *default_value)
340 struct ldb_dn *ret_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, msg, attr);
341 if (!ret_dn) {
342 return default_value;
344 return ret_dn;
348 pull a rid from a objectSid in a result set.
350 uint32_t samdb_result_rid_from_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
351 const char *attr, uint32_t default_value)
353 struct dom_sid *sid;
354 uint32_t rid;
356 sid = samdb_result_dom_sid(mem_ctx, msg, attr);
357 if (sid == NULL) {
358 return default_value;
360 rid = sid->sub_auths[sid->num_auths-1];
361 talloc_free(sid);
362 return rid;
366 pull a dom_sid structure from a objectSid in a result set.
368 struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
369 const char *attr)
371 const struct ldb_val *v;
372 struct dom_sid *sid;
373 enum ndr_err_code ndr_err;
374 v = ldb_msg_find_ldb_val(msg, attr);
375 if (v == NULL) {
376 return NULL;
378 sid = talloc(mem_ctx, struct dom_sid);
379 if (sid == NULL) {
380 return NULL;
382 ndr_err = ndr_pull_struct_blob(v, sid, NULL, sid,
383 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
384 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
385 talloc_free(sid);
386 return NULL;
388 return sid;
392 pull a guid structure from a objectGUID in a result set.
394 struct GUID samdb_result_guid(const struct ldb_message *msg, const char *attr)
396 const struct ldb_val *v;
397 enum ndr_err_code ndr_err;
398 struct GUID guid;
399 TALLOC_CTX *mem_ctx;
401 ZERO_STRUCT(guid);
403 v = ldb_msg_find_ldb_val(msg, attr);
404 if (!v) return guid;
406 mem_ctx = talloc_named_const(NULL, 0, "samdb_result_guid");
407 if (!mem_ctx) return guid;
408 ndr_err = ndr_pull_struct_blob(v, mem_ctx, NULL, &guid,
409 (ndr_pull_flags_fn_t)ndr_pull_GUID);
410 talloc_free(mem_ctx);
411 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
412 return guid;
415 return guid;
419 pull a sid prefix from a objectSid in a result set.
420 this is used to find the domain sid for a user
422 struct dom_sid *samdb_result_sid_prefix(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
423 const char *attr)
425 struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, msg, attr);
426 if (!sid || sid->num_auths < 1) return NULL;
427 sid->num_auths--;
428 return sid;
432 pull a NTTIME in a result set.
434 NTTIME samdb_result_nttime(struct ldb_message *msg, const char *attr, NTTIME default_value)
436 return ldb_msg_find_attr_as_uint64(msg, attr, default_value);
440 * Windows stores 0 for lastLogoff.
441 * But when a MS DC return the lastLogoff (as Logoff Time)
442 * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
443 * cause windows 2008 and newer version to fail for SMB requests
445 NTTIME samdb_result_last_logoff(struct ldb_message *msg)
447 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "lastLogoff",0);
449 if (ret == 0)
450 ret = 0x7FFFFFFFFFFFFFFFULL;
452 return ret;
456 * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
457 * indicate an account doesn't expire.
459 * When Windows initially creates an account, it sets
460 * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF). However,
461 * when changing from an account having a specific expiration date to
462 * that account never expiring, it sets accountExpires = 0.
464 * Consolidate that logic here to allow clearer logic for account expiry in
465 * the rest of the code.
467 NTTIME samdb_result_account_expires(struct ldb_message *msg)
469 NTTIME ret = ldb_msg_find_attr_as_uint64(msg, "accountExpires",
472 if (ret == 0)
473 ret = 0x7FFFFFFFFFFFFFFFULL;
475 return ret;
479 pull a uint64_t from a result set.
481 uint64_t samdb_result_uint64(struct ldb_message *msg, const char *attr, uint64_t default_value)
483 return ldb_msg_find_attr_as_uint64(msg, attr, default_value);
488 construct the allow_password_change field from the PwdLastSet attribute and the
489 domain password settings
491 NTTIME samdb_result_allow_password_change(struct ldb_context *sam_ldb,
492 TALLOC_CTX *mem_ctx,
493 struct ldb_dn *domain_dn,
494 struct ldb_message *msg,
495 const char *attr)
497 uint64_t attr_time = samdb_result_uint64(msg, attr, 0);
498 int64_t minPwdAge;
500 if (attr_time == 0) {
501 return 0;
504 minPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "minPwdAge", NULL);
506 /* yes, this is a -= not a += as minPwdAge is stored as the negative
507 of the number of 100-nano-seconds */
508 attr_time -= minPwdAge;
510 return attr_time;
514 construct the force_password_change field from the PwdLastSet
515 attribute, the userAccountControl and the domain password settings
517 NTTIME samdb_result_force_password_change(struct ldb_context *sam_ldb,
518 TALLOC_CTX *mem_ctx,
519 struct ldb_dn *domain_dn,
520 struct ldb_message *msg)
522 uint64_t attr_time = samdb_result_uint64(msg, "pwdLastSet", 0);
523 uint32_t userAccountControl = samdb_result_uint64(msg, "userAccountControl", 0);
524 int64_t maxPwdAge;
526 /* Machine accounts don't expire, and there is a flag for 'no expiry' */
527 if (!(userAccountControl & UF_NORMAL_ACCOUNT)
528 || (userAccountControl & UF_DONT_EXPIRE_PASSWD)) {
529 return 0x7FFFFFFFFFFFFFFFULL;
532 if (attr_time == 0) {
533 return 0;
536 maxPwdAge = samdb_search_int64(sam_ldb, mem_ctx, 0, domain_dn, "maxPwdAge", NULL);
537 if (maxPwdAge == 0) {
538 return 0x7FFFFFFFFFFFFFFFULL;
539 } else {
540 attr_time -= maxPwdAge;
543 return attr_time;
547 pull a samr_Password structutre from a result set.
549 struct samr_Password *samdb_result_hash(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, const char *attr)
551 struct samr_Password *hash = NULL;
552 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
553 if (val && (val->length >= sizeof(hash->hash))) {
554 hash = talloc(mem_ctx, struct samr_Password);
555 memcpy(hash->hash, val->data, MIN(val->length, sizeof(hash->hash)));
557 return hash;
561 pull an array of samr_Password structutres from a result set.
563 uint_t samdb_result_hashes(TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
564 const char *attr, struct samr_Password **hashes)
566 uint_t count, i;
567 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
569 *hashes = NULL;
570 if (!val) {
571 return 0;
573 count = val->length / 16;
574 if (count == 0) {
575 return 0;
578 *hashes = talloc_array(mem_ctx, struct samr_Password, count);
579 if (! *hashes) {
580 return 0;
583 for (i=0;i<count;i++) {
584 memcpy((*hashes)[i].hash, (i*16)+(char *)val->data, 16);
587 return count;
590 NTSTATUS samdb_result_passwords(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct ldb_message *msg,
591 struct samr_Password **lm_pwd, struct samr_Password **nt_pwd)
593 struct samr_Password *lmPwdHash, *ntPwdHash;
594 if (nt_pwd) {
595 int num_nt;
596 num_nt = samdb_result_hashes(mem_ctx, msg, "unicodePwd", &ntPwdHash);
597 if (num_nt == 0) {
598 *nt_pwd = NULL;
599 } else if (num_nt > 1) {
600 return NT_STATUS_INTERNAL_DB_CORRUPTION;
601 } else {
602 *nt_pwd = &ntPwdHash[0];
605 if (lm_pwd) {
606 /* Ensure that if we have turned off LM
607 * authentication, that we never use the LM hash, even
608 * if we store it */
609 if (lp_lanman_auth(lp_ctx)) {
610 int num_lm;
611 num_lm = samdb_result_hashes(mem_ctx, msg, "dBCSPwd", &lmPwdHash);
612 if (num_lm == 0) {
613 *lm_pwd = NULL;
614 } else if (num_lm > 1) {
615 return NT_STATUS_INTERNAL_DB_CORRUPTION;
616 } else {
617 *lm_pwd = &lmPwdHash[0];
619 } else {
620 *lm_pwd = NULL;
623 return NT_STATUS_OK;
627 pull a samr_LogonHours structutre from a result set.
629 struct samr_LogonHours samdb_result_logon_hours(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char *attr)
631 struct samr_LogonHours hours;
632 const int units_per_week = 168;
633 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
634 ZERO_STRUCT(hours);
635 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
636 if (!hours.bits) {
637 return hours;
639 hours.units_per_week = units_per_week;
640 memset(hours.bits, 0xFF, units_per_week);
641 if (val) {
642 memcpy(hours.bits, val->data, MIN(val->length, units_per_week));
644 return hours;
648 pull a set of account_flags from a result set.
650 This requires that the attributes:
651 pwdLastSet
652 userAccountControl
653 be included in 'msg'
655 uint32_t samdb_result_acct_flags(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
656 struct ldb_message *msg, struct ldb_dn *domain_dn)
658 uint32_t userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
659 uint32_t acct_flags = ds_uf2acb(userAccountControl);
660 NTTIME must_change_time;
661 NTTIME now;
663 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
664 domain_dn, msg);
666 /* Test account expire time */
667 unix_to_nt_time(&now, time(NULL));
668 /* check for expired password */
669 if (must_change_time < now) {
670 acct_flags |= ACB_PW_EXPIRED;
672 return acct_flags;
675 struct lsa_BinaryString samdb_result_parameters(TALLOC_CTX *mem_ctx,
676 struct ldb_message *msg,
677 const char *attr)
679 struct lsa_BinaryString s;
680 const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
682 ZERO_STRUCT(s);
684 if (!val) {
685 return s;
688 s.array = talloc_array(mem_ctx, uint16_t, val->length/2);
689 if (!s.array) {
690 return s;
692 s.length = s.size = val->length/2;
693 memcpy(s.array, val->data, val->length);
695 return s;
698 /* Find an attribute, with a particular value */
700 /* The current callers of this function expect a very specific
701 * behaviour: In particular, objectClass subclass equivilance is not
702 * wanted. This means that we should not lookup the schema for the
703 * comparison function */
704 struct ldb_message_element *samdb_find_attribute(struct ldb_context *ldb,
705 const struct ldb_message *msg,
706 const char *name, const char *value)
708 int i;
709 struct ldb_message_element *el = ldb_msg_find_element(msg, name);
711 if (!el) {
712 return NULL;
715 for (i=0;i<el->num_values;i++) {
716 if (ldb_attr_cmp(value, (char *)el->values[i].data) == 0) {
717 return el;
721 return NULL;
724 int samdb_find_or_add_value(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
726 if (samdb_find_attribute(ldb, msg, name, set_value) == NULL) {
727 return samdb_msg_add_string(ldb, msg, msg, name, set_value);
729 return LDB_SUCCESS;
732 int samdb_find_or_add_attribute(struct ldb_context *ldb, struct ldb_message *msg, const char *name, const char *set_value)
734 struct ldb_message_element *el;
736 el = ldb_msg_find_element(msg, name);
737 if (el) {
738 return LDB_SUCCESS;
741 return samdb_msg_add_string(ldb, msg, msg, name, set_value);
747 add a string element to a message
749 int samdb_msg_add_string(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
750 const char *attr_name, const char *str)
752 char *s = talloc_strdup(mem_ctx, str);
753 char *a = talloc_strdup(mem_ctx, attr_name);
754 if (s == NULL || a == NULL) {
755 return LDB_ERR_OPERATIONS_ERROR;
757 return ldb_msg_add_string(msg, a, s);
761 add a dom_sid element to a message
763 int samdb_msg_add_dom_sid(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
764 const char *attr_name, struct dom_sid *sid)
766 struct ldb_val v;
767 enum ndr_err_code ndr_err;
769 ndr_err = ndr_push_struct_blob(&v, mem_ctx,
770 lp_iconv_convenience(ldb_get_opaque(sam_ldb, "loadparm")),
771 sid,
772 (ndr_push_flags_fn_t)ndr_push_dom_sid);
773 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
774 return -1;
776 return ldb_msg_add_value(msg, attr_name, &v, NULL);
781 add a delete element operation to a message
783 int samdb_msg_add_delete(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
784 const char *attr_name)
786 /* we use an empty replace rather than a delete, as it allows for
787 samdb_replace() to be used everywhere */
788 return ldb_msg_add_empty(msg, attr_name, LDB_FLAG_MOD_REPLACE, NULL);
792 add a add attribute value to a message
794 int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
795 const char *attr_name, const char *value)
797 struct ldb_message_element *el;
798 char *a, *v;
799 int ret;
800 a = talloc_strdup(mem_ctx, attr_name);
801 if (a == NULL)
802 return -1;
803 v = talloc_strdup(mem_ctx, value);
804 if (v == NULL)
805 return -1;
806 ret = ldb_msg_add_string(msg, a, v);
807 if (ret != 0)
808 return ret;
809 el = ldb_msg_find_element(msg, a);
810 if (el == NULL)
811 return -1;
812 el->flags = LDB_FLAG_MOD_ADD;
813 return 0;
817 add a delete attribute value to a message
819 int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
820 const char *attr_name, const char *value)
822 struct ldb_message_element *el;
823 char *a, *v;
824 int ret;
825 a = talloc_strdup(mem_ctx, attr_name);
826 if (a == NULL)
827 return -1;
828 v = talloc_strdup(mem_ctx, value);
829 if (v == NULL)
830 return -1;
831 ret = ldb_msg_add_string(msg, a, v);
832 if (ret != 0)
833 return ret;
834 el = ldb_msg_find_element(msg, a);
835 if (el == NULL)
836 return -1;
837 el->flags = LDB_FLAG_MOD_DELETE;
838 return 0;
842 add a int element to a message
844 int samdb_msg_add_int(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
845 const char *attr_name, int v)
847 const char *s = talloc_asprintf(mem_ctx, "%d", v);
848 return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, s);
852 add a uint_t element to a message
854 int samdb_msg_add_uint(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
855 const char *attr_name, uint_t v)
857 const char *s = talloc_asprintf(mem_ctx, "%u", v);
858 return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, s);
862 add a (signed) int64_t element to a message
864 int samdb_msg_add_int64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
865 const char *attr_name, int64_t v)
867 const char *s = talloc_asprintf(mem_ctx, "%lld", (long long)v);
868 return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, s);
872 add a uint64_t element to a message
874 int samdb_msg_add_uint64(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
875 const char *attr_name, uint64_t v)
877 const char *s = talloc_asprintf(mem_ctx, "%llu", (unsigned long long)v);
878 return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, s);
882 add a samr_Password element to a message
884 int samdb_msg_add_hash(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
885 const char *attr_name, struct samr_Password *hash)
887 struct ldb_val val;
888 val.data = talloc_memdup(mem_ctx, hash->hash, 16);
889 if (!val.data) {
890 return -1;
892 val.length = 16;
893 return ldb_msg_add_value(msg, attr_name, &val, NULL);
897 add a samr_Password array to a message
899 int samdb_msg_add_hashes(TALLOC_CTX *mem_ctx, struct ldb_message *msg,
900 const char *attr_name, struct samr_Password *hashes, uint_t count)
902 struct ldb_val val;
903 int i;
904 val.data = talloc_array_size(mem_ctx, 16, count);
905 val.length = count*16;
906 if (!val.data) {
907 return -1;
909 for (i=0;i<count;i++) {
910 memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
912 return ldb_msg_add_value(msg, attr_name, &val, NULL);
916 add a acct_flags element to a message
918 int samdb_msg_add_acct_flags(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
919 const char *attr_name, uint32_t v)
921 return samdb_msg_add_uint(sam_ldb, mem_ctx, msg, attr_name, ds_acb2uf(v));
925 add a logon_hours element to a message
927 int samdb_msg_add_logon_hours(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
928 const char *attr_name, struct samr_LogonHours *hours)
930 struct ldb_val val;
931 val.length = hours->units_per_week / 8;
932 val.data = hours->bits;
933 return ldb_msg_add_value(msg, attr_name, &val, NULL);
937 add a parameters element to a message
939 int samdb_msg_add_parameters(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
940 const char *attr_name, struct lsa_BinaryString *parameters)
942 struct ldb_val val;
943 val.length = parameters->length * 2;
944 val.data = (uint8_t *)parameters->array;
945 return ldb_msg_add_value(msg, attr_name, &val, NULL);
948 add a general value element to a message
950 int samdb_msg_add_value(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
951 const char *attr_name, const struct ldb_val *val)
953 return ldb_msg_add_value(msg, attr_name, val, NULL);
957 sets a general value element to a message
959 int samdb_msg_set_value(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
960 const char *attr_name, const struct ldb_val *val)
962 struct ldb_message_element *el;
964 el = ldb_msg_find_element(msg, attr_name);
965 if (el) {
966 el->num_values = 0;
968 return ldb_msg_add_value(msg, attr_name, val, NULL);
972 set a string element in a message
974 int samdb_msg_set_string(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
975 const char *attr_name, const char *str)
977 struct ldb_message_element *el;
979 el = ldb_msg_find_element(msg, attr_name);
980 if (el) {
981 el->num_values = 0;
983 return samdb_msg_add_string(sam_ldb, mem_ctx, msg, attr_name, str);
987 replace elements in a record
989 int samdb_replace(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
991 int i;
993 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
994 for (i=0;i<msg->num_elements;i++) {
995 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
998 /* modify the samdb record */
999 return ldb_modify(sam_ldb, msg);
1003 return a default security descriptor
1005 struct security_descriptor *samdb_default_security_descriptor(TALLOC_CTX *mem_ctx)
1007 struct security_descriptor *sd;
1009 sd = security_descriptor_initialise(mem_ctx);
1011 return sd;
1014 struct ldb_dn *samdb_base_dn(struct ldb_context *sam_ctx)
1016 return ldb_get_default_basedn(sam_ctx);
1019 struct ldb_dn *samdb_config_dn(struct ldb_context *sam_ctx)
1021 return ldb_get_config_basedn(sam_ctx);
1024 struct ldb_dn *samdb_schema_dn(struct ldb_context *sam_ctx)
1026 return ldb_get_schema_basedn(sam_ctx);
1029 struct ldb_dn *samdb_root_dn(struct ldb_context *sam_ctx)
1031 return ldb_get_root_basedn(sam_ctx);
1034 struct ldb_dn *samdb_partitions_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1036 struct ldb_dn *new_dn;
1038 new_dn = ldb_dn_copy(mem_ctx, samdb_config_dn(sam_ctx));
1039 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Partitions")) {
1040 talloc_free(new_dn);
1041 return NULL;
1043 return new_dn;
1046 struct ldb_dn *samdb_sites_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx)
1048 struct ldb_dn *new_dn;
1050 new_dn = ldb_dn_copy(mem_ctx, samdb_config_dn(sam_ctx));
1051 if ( ! ldb_dn_add_child_fmt(new_dn, "CN=Sites")) {
1052 talloc_free(new_dn);
1053 return NULL;
1055 return new_dn;
1059 work out the domain sid for the current open ldb
1061 const struct dom_sid *samdb_domain_sid(struct ldb_context *ldb)
1063 TALLOC_CTX *tmp_ctx;
1064 const struct dom_sid *domain_sid;
1065 const char *attrs[] = {
1066 "objectSid",
1067 NULL
1069 struct ldb_result *res;
1070 int ret;
1072 /* see if we have a cached copy */
1073 domain_sid = (struct dom_sid *)ldb_get_opaque(ldb, "cache.domain_sid");
1074 if (domain_sid) {
1075 return domain_sid;
1078 tmp_ctx = talloc_new(ldb);
1079 if (tmp_ctx == NULL) {
1080 goto failed;
1083 ret = ldb_search(ldb, tmp_ctx, &res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, attrs, "objectSid=*");
1085 if (ret != LDB_SUCCESS) {
1086 goto failed;
1089 if (res->count != 1) {
1090 goto failed;
1093 domain_sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
1094 if (domain_sid == NULL) {
1095 goto failed;
1098 /* cache the domain_sid in the ldb */
1099 if (ldb_set_opaque(ldb, "cache.domain_sid", discard_const_p(struct dom_sid, domain_sid)) != LDB_SUCCESS) {
1100 goto failed;
1103 talloc_steal(ldb, domain_sid);
1104 talloc_free(tmp_ctx);
1106 return domain_sid;
1108 failed:
1109 DEBUG(1,("Failed to find domain_sid for open ldb\n"));
1110 talloc_free(tmp_ctx);
1111 return NULL;
1114 bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in)
1116 TALLOC_CTX *tmp_ctx;
1117 struct dom_sid *dom_sid_new;
1118 struct dom_sid *dom_sid_old;
1120 /* see if we have a cached copy */
1121 dom_sid_old = talloc_get_type(ldb_get_opaque(ldb,
1122 "cache.domain_sid"), struct dom_sid);
1124 tmp_ctx = talloc_new(ldb);
1125 if (tmp_ctx == NULL) {
1126 goto failed;
1129 dom_sid_new = dom_sid_dup(tmp_ctx, dom_sid_in);
1130 if (!dom_sid_new) {
1131 goto failed;
1134 /* cache the domain_sid in the ldb */
1135 if (ldb_set_opaque(ldb, "cache.domain_sid", dom_sid_new) != LDB_SUCCESS) {
1136 goto failed;
1139 talloc_steal(ldb, dom_sid_new);
1140 talloc_free(tmp_ctx);
1141 talloc_free(dom_sid_old);
1143 return true;
1145 failed:
1146 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1147 talloc_free(tmp_ctx);
1148 return false;
1151 /* Obtain the short name of the flexible single master operator
1152 * (FSMO), such as the PDC Emulator */
1153 const char *samdb_result_fsmo_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const struct ldb_message *msg,
1154 const char *attr)
1156 /* Format is cn=NTDS Settings,cn=<NETBIOS name of FSMO>,.... */
1157 struct ldb_dn *fsmo_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, msg, attr);
1158 const struct ldb_val *val = ldb_dn_get_component_val(fsmo_dn, 1);
1159 const char *name = ldb_dn_get_component_name(fsmo_dn, 1);
1161 if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
1162 /* Ensure this matches the format. This gives us a
1163 * bit more confidence that a 'cn' value will be a
1164 * ascii string */
1165 return NULL;
1167 if (val) {
1168 return (char *)val->data;
1170 return NULL;
1174 work out the ntds settings dn for the current open ldb
1176 struct ldb_dn *samdb_ntds_settings_dn(struct ldb_context *ldb)
1178 TALLOC_CTX *tmp_ctx;
1179 const char *root_attrs[] = { "dsServiceName", NULL };
1180 int ret;
1181 struct ldb_result *root_res;
1182 struct ldb_dn *settings_dn;
1184 /* see if we have a cached copy */
1185 settings_dn = (struct ldb_dn *)ldb_get_opaque(ldb, "cache.settings_dn");
1186 if (settings_dn) {
1187 return settings_dn;
1190 tmp_ctx = talloc_new(ldb);
1191 if (tmp_ctx == NULL) {
1192 goto failed;
1195 ret = ldb_search(ldb, tmp_ctx, &root_res, ldb_dn_new(tmp_ctx, ldb, ""), LDB_SCOPE_BASE, root_attrs, NULL);
1196 if (ret) {
1197 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1198 ldb_errstring(ldb)));
1199 goto failed;
1202 if (root_res->count != 1) {
1203 goto failed;
1206 settings_dn = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, root_res->msgs[0], "dsServiceName");
1208 /* cache the domain_sid in the ldb */
1209 if (ldb_set_opaque(ldb, "cache.settings_dn", settings_dn) != LDB_SUCCESS) {
1210 goto failed;
1213 talloc_steal(ldb, settings_dn);
1214 talloc_free(tmp_ctx);
1216 return settings_dn;
1218 failed:
1219 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1220 talloc_free(tmp_ctx);
1221 return NULL;
1225 work out the ntds settings invocationId for the current open ldb
1227 const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
1229 TALLOC_CTX *tmp_ctx;
1230 const char *attrs[] = { "invocationId", NULL };
1231 int ret;
1232 struct ldb_result *res;
1233 struct GUID *invocation_id;
1235 /* see if we have a cached copy */
1236 invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
1237 if (invocation_id) {
1238 return invocation_id;
1241 tmp_ctx = talloc_new(ldb);
1242 if (tmp_ctx == NULL) {
1243 goto failed;
1246 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1247 if (ret) {
1248 goto failed;
1251 if (res->count != 1) {
1252 goto failed;
1255 invocation_id = talloc(tmp_ctx, struct GUID);
1256 if (!invocation_id) {
1257 goto failed;
1260 *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
1262 /* cache the domain_sid in the ldb */
1263 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
1264 goto failed;
1267 talloc_steal(ldb, invocation_id);
1268 talloc_free(tmp_ctx);
1270 return invocation_id;
1272 failed:
1273 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1274 talloc_free(tmp_ctx);
1275 return NULL;
1278 bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
1280 TALLOC_CTX *tmp_ctx;
1281 struct GUID *invocation_id_new;
1282 struct GUID *invocation_id_old;
1284 /* see if we have a cached copy */
1285 invocation_id_old = (struct GUID *)ldb_get_opaque(ldb,
1286 "cache.invocation_id");
1288 tmp_ctx = talloc_new(ldb);
1289 if (tmp_ctx == NULL) {
1290 goto failed;
1293 invocation_id_new = talloc(tmp_ctx, struct GUID);
1294 if (!invocation_id_new) {
1295 goto failed;
1298 *invocation_id_new = *invocation_id_in;
1300 /* cache the domain_sid in the ldb */
1301 if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
1302 goto failed;
1305 talloc_steal(ldb, invocation_id_new);
1306 talloc_free(tmp_ctx);
1307 talloc_free(invocation_id_old);
1309 return true;
1311 failed:
1312 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1313 talloc_free(tmp_ctx);
1314 return false;
1318 work out the ntds settings objectGUID for the current open ldb
1320 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
1322 TALLOC_CTX *tmp_ctx;
1323 const char *attrs[] = { "objectGUID", NULL };
1324 int ret;
1325 struct ldb_result *res;
1326 struct GUID *ntds_guid;
1328 /* see if we have a cached copy */
1329 ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1330 if (ntds_guid) {
1331 return ntds_guid;
1334 tmp_ctx = talloc_new(ldb);
1335 if (tmp_ctx == NULL) {
1336 goto failed;
1339 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1340 if (ret) {
1341 goto failed;
1344 if (res->count != 1) {
1345 goto failed;
1348 ntds_guid = talloc(tmp_ctx, struct GUID);
1349 if (!ntds_guid) {
1350 goto failed;
1353 *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
1355 /* cache the domain_sid in the ldb */
1356 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
1357 goto failed;
1360 talloc_steal(ldb, ntds_guid);
1361 talloc_free(tmp_ctx);
1363 return ntds_guid;
1365 failed:
1366 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1367 talloc_free(tmp_ctx);
1368 return NULL;
1371 bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
1373 TALLOC_CTX *tmp_ctx;
1374 struct GUID *ntds_guid_new;
1375 struct GUID *ntds_guid_old;
1377 /* see if we have a cached copy */
1378 ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
1380 tmp_ctx = talloc_new(ldb);
1381 if (tmp_ctx == NULL) {
1382 goto failed;
1385 ntds_guid_new = talloc(tmp_ctx, struct GUID);
1386 if (!ntds_guid_new) {
1387 goto failed;
1390 *ntds_guid_new = *ntds_guid_in;
1392 /* cache the domain_sid in the ldb */
1393 if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
1394 goto failed;
1397 talloc_steal(ldb, ntds_guid_new);
1398 talloc_free(tmp_ctx);
1399 talloc_free(ntds_guid_old);
1401 return true;
1403 failed:
1404 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1405 talloc_free(tmp_ctx);
1406 return false;
1410 work out the server dn for the current open ldb
1412 struct ldb_dn *samdb_server_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1414 return ldb_dn_get_parent(mem_ctx, samdb_ntds_settings_dn(ldb));
1418 work out the server dn for the current open ldb
1420 struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1422 struct ldb_dn *server_dn;
1423 struct ldb_dn *server_site_dn;
1425 server_dn = samdb_server_dn(ldb, mem_ctx);
1426 if (!server_dn) return NULL;
1428 server_site_dn = ldb_dn_get_parent(mem_ctx, server_dn);
1430 talloc_free(server_dn);
1431 return server_site_dn;
1434 const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
1436 const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb, mem_ctx));
1438 if (val != NULL)
1439 return (const char *) val->data;
1440 else
1441 return NULL;
1445 work out if we are the PDC for the domain of the current open ldb
1447 bool samdb_is_pdc(struct ldb_context *ldb)
1449 const char *dom_attrs[] = { "fSMORoleOwner", NULL };
1450 int ret;
1451 struct ldb_result *dom_res;
1452 TALLOC_CTX *tmp_ctx;
1453 bool is_pdc;
1454 struct ldb_dn *pdc;
1456 tmp_ctx = talloc_new(ldb);
1457 if (tmp_ctx == NULL) {
1458 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1459 return false;
1462 ret = ldb_search(ldb, tmp_ctx, &dom_res, ldb_get_default_basedn(ldb), LDB_SCOPE_BASE, dom_attrs, NULL);
1463 if (ret) {
1464 DEBUG(1,("Searching for fSMORoleOwner in %s failed: %s\n",
1465 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)),
1466 ldb_errstring(ldb)));
1467 goto failed;
1469 if (dom_res->count != 1) {
1470 goto failed;
1473 pdc = ldb_msg_find_attr_as_dn(ldb, tmp_ctx, dom_res->msgs[0], "fSMORoleOwner");
1475 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), pdc) == 0) {
1476 is_pdc = true;
1477 } else {
1478 is_pdc = false;
1481 talloc_free(tmp_ctx);
1483 return is_pdc;
1485 failed:
1486 DEBUG(1,("Failed to find if we are the PDC for this ldb\n"));
1487 talloc_free(tmp_ctx);
1488 return false;
1492 work out if we are a Global Catalog server for the domain of the current open ldb
1494 bool samdb_is_gc(struct ldb_context *ldb)
1496 const char *attrs[] = { "options", NULL };
1497 int ret, options;
1498 struct ldb_result *res;
1499 TALLOC_CTX *tmp_ctx;
1501 tmp_ctx = talloc_new(ldb);
1502 if (tmp_ctx == NULL) {
1503 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1504 return false;
1507 /* Query cn=ntds settings,.... */
1508 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
1509 if (ret) {
1510 talloc_free(tmp_ctx);
1511 return false;
1513 if (res->count != 1) {
1514 talloc_free(tmp_ctx);
1515 return false;
1518 options = ldb_msg_find_attr_as_int(res->msgs[0], "options", 0);
1519 talloc_free(tmp_ctx);
1521 /* if options attribute has the 0x00000001 flag set, then enable the global catlog */
1522 if (options & 0x000000001) {
1523 return true;
1525 return false;
1528 /* Find a domain object in the parents of a particular DN. */
1529 int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
1530 struct ldb_dn **parent_dn, const char **errstring)
1532 TALLOC_CTX *local_ctx;
1533 struct ldb_dn *sdn = dn;
1534 struct ldb_result *res = NULL;
1535 int ret = 0;
1536 const char *attrs[] = { NULL };
1538 local_ctx = talloc_new(mem_ctx);
1539 if (local_ctx == NULL) return LDB_ERR_OPERATIONS_ERROR;
1541 while ((sdn = ldb_dn_get_parent(local_ctx, sdn))) {
1542 ret = ldb_search(ldb, local_ctx, &res, sdn, LDB_SCOPE_BASE, attrs,
1543 "(|(objectClass=domain)(objectClass=builtinDomain))");
1544 if (ret == LDB_SUCCESS) {
1545 if (res->count == 1) {
1546 break;
1548 } else {
1549 break;
1553 if (ret != LDB_SUCCESS) {
1554 *errstring = talloc_asprintf(mem_ctx, "Error searching for parent domain of %s, failed searching for %s: %s",
1555 ldb_dn_get_linearized(dn),
1556 ldb_dn_get_linearized(sdn),
1557 ldb_errstring(ldb));
1558 talloc_free(local_ctx);
1559 return ret;
1561 if (res->count != 1) {
1562 *errstring = talloc_asprintf(mem_ctx, "Invalid dn (%s), not child of a domain object",
1563 ldb_dn_get_linearized(dn));
1564 DEBUG(0,(__location__ ": %s\n", *errstring));
1565 talloc_free(local_ctx);
1566 return LDB_ERR_CONSTRAINT_VIOLATION;
1569 *parent_dn = talloc_steal(mem_ctx, res->msgs[0]->dn);
1570 talloc_free(local_ctx);
1571 return ret;
1576 * Performs checks on a user password (plaintext UNIX format - attribute
1577 * "password"). The remaining parameters have to be extracted from the domain
1578 * object in the AD.
1580 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1582 enum samr_ValidationStatus samdb_check_password(const DATA_BLOB *password,
1583 const uint32_t pwdProperties,
1584 const uint32_t minPwdLength)
1586 /* checks if the "minPwdLength" property is satisfied */
1587 if (minPwdLength > password->length)
1588 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
1590 /* checks the password complexity */
1591 if (((pwdProperties & DOMAIN_PASSWORD_COMPLEX) != 0)
1592 && (password->data != NULL)
1593 && (!check_password_quality((const char *) password->data)))
1594 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
1596 return SAMR_VALIDATION_STATUS_SUCCESS;
1600 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1601 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1602 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
1603 * gives some more informations if the changed failed.
1605 * The caller should have a LDB transaction wrapping this.
1607 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
1608 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
1609 * NT_STATUS_PASSWORD_RESTRICTION
1611 NTSTATUS samdb_set_password(struct ldb_context *ctx, TALLOC_CTX *mem_ctx,
1612 struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
1613 struct ldb_message *mod,
1614 const DATA_BLOB *new_password,
1615 struct samr_Password *param_lmNewHash,
1616 struct samr_Password *param_ntNewHash,
1617 bool user_change,
1618 enum samPwdChangeReason *reject_reason,
1619 struct samr_DomInfo1 **_dominfo)
1621 const char * const user_attrs[] = { "userAccountControl",
1622 "lmPwdHistory",
1623 "ntPwdHistory",
1624 "dBCSPwd", "unicodePwd",
1625 "objectSid",
1626 "pwdLastSet", NULL };
1627 const char * const domain_attrs[] = { "minPwdLength", "pwdProperties",
1628 "pwdHistoryLength",
1629 "maxPwdAge", "minPwdAge", NULL };
1630 NTTIME pwdLastSet;
1631 uint32_t minPwdLength, pwdProperties, pwdHistoryLength;
1632 int64_t maxPwdAge, minPwdAge;
1633 uint32_t userAccountControl;
1634 struct samr_Password *sambaLMPwdHistory, *sambaNTPwdHistory,
1635 *lmPwdHash, *ntPwdHash, *lmNewHash, *ntNewHash;
1636 struct samr_Password local_lmNewHash, local_ntNewHash;
1637 int sambaLMPwdHistory_len, sambaNTPwdHistory_len;
1638 struct dom_sid *domain_sid;
1639 struct ldb_message **res;
1640 bool restrictions;
1641 int count;
1642 time_t now = time(NULL);
1643 NTTIME now_nt;
1644 int i;
1646 /* we need to know the time to compute password age */
1647 unix_to_nt_time(&now_nt, now);
1649 /* pull all the user parameters */
1650 count = gendb_search_dn(ctx, mem_ctx, user_dn, &res, user_attrs);
1651 if (count != 1) {
1652 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1654 userAccountControl = samdb_result_uint(res[0], "userAccountControl", 0);
1655 sambaLMPwdHistory_len = samdb_result_hashes(mem_ctx, res[0],
1656 "lmPwdHistory", &sambaLMPwdHistory);
1657 sambaNTPwdHistory_len = samdb_result_hashes(mem_ctx, res[0],
1658 "ntPwdHistory", &sambaNTPwdHistory);
1659 lmPwdHash = samdb_result_hash(mem_ctx, res[0], "dBCSPwd");
1660 ntPwdHash = samdb_result_hash(mem_ctx, res[0], "unicodePwd");
1661 pwdLastSet = samdb_result_uint64(res[0], "pwdLastSet", 0);
1663 /* Copy parameters */
1664 lmNewHash = param_lmNewHash;
1665 ntNewHash = param_ntNewHash;
1667 /* Only non-trust accounts have restrictions (possibly this
1668 * test is the wrong way around, but I like to be restrictive
1669 * if possible */
1670 restrictions = !(userAccountControl & (UF_INTERDOMAIN_TRUST_ACCOUNT
1671 |UF_WORKSTATION_TRUST_ACCOUNT
1672 |UF_SERVER_TRUST_ACCOUNT));
1674 if (domain_dn != NULL) {
1675 /* pull the domain parameters */
1676 count = gendb_search_dn(ctx, mem_ctx, domain_dn, &res,
1677 domain_attrs);
1678 if (count != 1) {
1679 DEBUG(2, ("samdb_set_password: Domain DN %s is invalid, for user %s\n",
1680 ldb_dn_get_linearized(domain_dn),
1681 ldb_dn_get_linearized(user_dn)));
1682 return NT_STATUS_NO_SUCH_DOMAIN;
1684 } else {
1685 /* work out the domain sid, and pull the domain from there */
1686 domain_sid = samdb_result_sid_prefix(mem_ctx, res[0],
1687 "objectSid");
1688 if (domain_sid == NULL) {
1689 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1692 count = gendb_search(ctx, mem_ctx, NULL, &res, domain_attrs,
1693 "(objectSid=%s)",
1694 ldap_encode_ndr_dom_sid(mem_ctx, domain_sid));
1695 if (count != 1) {
1696 DEBUG(2, ("samdb_set_password: Could not find domain to match SID: %s, for user %s\n",
1697 dom_sid_string(mem_ctx, domain_sid),
1698 ldb_dn_get_linearized(user_dn)));
1699 return NT_STATUS_NO_SUCH_DOMAIN;
1703 minPwdLength = samdb_result_uint(res[0], "minPwdLength", 0);
1704 pwdProperties = samdb_result_uint(res[0], "pwdProperties", 0);
1705 pwdHistoryLength = samdb_result_uint(res[0], "pwdHistoryLength", 0);
1706 maxPwdAge = samdb_result_int64(res[0], "maxPwdAge", 0);
1707 minPwdAge = samdb_result_int64(res[0], "minPwdAge", 0);
1709 if ((userAccountControl & UF_PASSWD_NOTREQD) != 0) {
1710 /* see [MS-ADTS] 2.2.15 */
1711 minPwdLength = 0;
1714 if (_dominfo != NULL) {
1715 struct samr_DomInfo1 *dominfo;
1716 /* on failure we need to fill in the reject reasons */
1717 dominfo = talloc(mem_ctx, struct samr_DomInfo1);
1718 if (dominfo == NULL) {
1719 return NT_STATUS_NO_MEMORY;
1721 dominfo->min_password_length = minPwdLength;
1722 dominfo->password_properties = pwdProperties;
1723 dominfo->password_history_length = pwdHistoryLength;
1724 dominfo->max_password_age = maxPwdAge;
1725 dominfo->min_password_age = minPwdAge;
1726 *_dominfo = dominfo;
1729 if ((restrictions != 0) && (new_password != 0)) {
1730 char *new_pass;
1732 /* checks if the "minPwdLength" property is satisfied */
1733 if ((restrictions != 0)
1734 && (minPwdLength > utf16_len_n(
1735 new_password->data, new_password->length)/2)) {
1736 if (reject_reason) {
1737 *reject_reason = SAM_PWD_CHANGE_PASSWORD_TOO_SHORT;
1739 return NT_STATUS_PASSWORD_RESTRICTION;
1742 /* Create the NT hash */
1743 mdfour(local_ntNewHash.hash, new_password->data,
1744 new_password->length);
1746 ntNewHash = &local_ntNewHash;
1748 /* Only check complexity if we can convert it at all. Assuming unconvertable passwords are 'strong' */
1749 if (convert_string_talloc_convenience(mem_ctx,
1750 lp_iconv_convenience(ldb_get_opaque(ctx, "loadparm")),
1751 CH_UTF16, CH_UNIX,
1752 new_password->data, new_password->length,
1753 (void **)&new_pass, NULL, false)) {
1755 /* checks the password complexity */
1756 if ((restrictions != 0)
1757 && ((pwdProperties
1758 & DOMAIN_PASSWORD_COMPLEX) != 0)
1759 && (!check_password_quality(new_pass))) {
1760 if (reject_reason) {
1761 *reject_reason = SAM_PWD_CHANGE_NOT_COMPLEX;
1763 return NT_STATUS_PASSWORD_RESTRICTION;
1766 /* compute the new lm hashes (for checking history - case insenitivly!) */
1767 if (E_deshash(new_pass, local_lmNewHash.hash)) {
1768 lmNewHash = &local_lmNewHash;
1773 if ((restrictions != 0) && user_change) {
1774 /* are all password changes disallowed? */
1775 if ((pwdProperties & DOMAIN_REFUSE_PASSWORD_CHANGE) != 0) {
1776 if (reject_reason) {
1777 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
1779 return NT_STATUS_PASSWORD_RESTRICTION;
1782 /* can this user change the password? */
1783 if ((userAccountControl & UF_PASSWD_CANT_CHANGE) != 0) {
1784 if (reject_reason) {
1785 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
1787 return NT_STATUS_PASSWORD_RESTRICTION;
1790 /* Password minimum age: yes, this is a minus. The ages are in negative 100nsec units! */
1791 if (pwdLastSet - minPwdAge > now_nt) {
1792 if (reject_reason) {
1793 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
1795 return NT_STATUS_PASSWORD_RESTRICTION;
1798 /* check the immediately past password */
1799 if (pwdHistoryLength > 0) {
1800 if (lmNewHash && lmPwdHash && memcmp(lmNewHash->hash,
1801 lmPwdHash->hash, 16) == 0) {
1802 if (reject_reason) {
1803 *reject_reason = SAM_PWD_CHANGE_PWD_IN_HISTORY;
1805 return NT_STATUS_PASSWORD_RESTRICTION;
1807 if (ntNewHash && ntPwdHash && memcmp(ntNewHash->hash,
1808 ntPwdHash->hash, 16) == 0) {
1809 if (reject_reason) {
1810 *reject_reason = SAM_PWD_CHANGE_PWD_IN_HISTORY;
1812 return NT_STATUS_PASSWORD_RESTRICTION;
1816 /* check the password history */
1817 sambaLMPwdHistory_len = MIN(sambaLMPwdHistory_len,
1818 pwdHistoryLength);
1819 sambaNTPwdHistory_len = MIN(sambaNTPwdHistory_len,
1820 pwdHistoryLength);
1822 for (i=0; lmNewHash && i<sambaLMPwdHistory_len;i++) {
1823 if (memcmp(lmNewHash->hash, sambaLMPwdHistory[i].hash,
1824 16) == 0) {
1825 if (reject_reason) {
1826 *reject_reason = SAM_PWD_CHANGE_PWD_IN_HISTORY;
1828 return NT_STATUS_PASSWORD_RESTRICTION;
1831 for (i=0; ntNewHash && i<sambaNTPwdHistory_len;i++) {
1832 if (memcmp(ntNewHash->hash, sambaNTPwdHistory[i].hash,
1833 16) == 0) {
1834 if (reject_reason) {
1835 *reject_reason = SAM_PWD_CHANGE_PWD_IN_HISTORY;
1837 return NT_STATUS_PASSWORD_RESTRICTION;
1842 #define CHECK_RET(x) do { if (x != 0) return NT_STATUS_NO_MEMORY; } while(0)
1844 /* the password is acceptable. Start forming the new fields */
1845 if (new_password != NULL) {
1846 /* if we know the cleartext UTF16 password, then set it.
1847 * Modules in ldb will set all the appropriate
1848 * hashes */
1849 CHECK_RET(ldb_msg_add_value(mod, "clearTextPassword", new_password, NULL));
1850 } else {
1851 /* We don't have the cleartext, so delete the old one
1852 * and set what we have of the hashes */
1853 CHECK_RET(samdb_msg_add_delete(ctx, mem_ctx, mod, "clearTextPassword"));
1855 if (lmNewHash) {
1856 CHECK_RET(samdb_msg_add_hash(ctx, mem_ctx, mod, "dBCSPwd", lmNewHash));
1857 } else {
1858 CHECK_RET(samdb_msg_add_delete(ctx, mem_ctx, mod, "dBCSPwd"));
1861 if (ntNewHash) {
1862 CHECK_RET(samdb_msg_add_hash(ctx, mem_ctx, mod, "unicodePwd", ntNewHash));
1863 } else {
1864 CHECK_RET(samdb_msg_add_delete(ctx, mem_ctx, mod, "unicodePwd"));
1868 if (reject_reason) {
1869 *reject_reason = SAM_PWD_CHANGE_NO_ERROR;
1871 return NT_STATUS_OK;
1876 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1877 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1878 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
1879 * gives some more informations if the changed failed.
1881 * This wrapper function for "samdb_set_password" takes a SID as input rather
1882 * than a user DN.
1884 * This call encapsulates a new LDB transaction for changing the password;
1885 * therefore the user hasn't to start a new one.
1887 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
1888 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
1889 * NT_STATUS_PASSWORD_RESTRICTION
1891 NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1892 const struct dom_sid *user_sid,
1893 const DATA_BLOB *new_password,
1894 struct samr_Password *lmNewHash,
1895 struct samr_Password *ntNewHash,
1896 bool user_change,
1897 enum samPwdChangeReason *reject_reason,
1898 struct samr_DomInfo1 **_dominfo)
1900 NTSTATUS nt_status;
1901 struct ldb_dn *user_dn;
1902 struct ldb_message *msg;
1903 int ret;
1905 ret = ldb_transaction_start(ldb);
1906 if (ret != LDB_SUCCESS) {
1907 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
1908 return NT_STATUS_TRANSACTION_ABORTED;
1911 user_dn = samdb_search_dn(ldb, mem_ctx, NULL,
1912 "(&(objectSid=%s)(objectClass=user))",
1913 ldap_encode_ndr_dom_sid(mem_ctx, user_sid));
1914 if (!user_dn) {
1915 ldb_transaction_cancel(ldb);
1916 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
1917 dom_sid_string(mem_ctx, user_sid)));
1918 return NT_STATUS_NO_SUCH_USER;
1921 msg = ldb_msg_new(mem_ctx);
1922 if (msg == NULL) {
1923 ldb_transaction_cancel(ldb);
1924 return NT_STATUS_NO_MEMORY;
1927 msg->dn = ldb_dn_copy(msg, user_dn);
1928 if (!msg->dn) {
1929 ldb_transaction_cancel(ldb);
1930 return NT_STATUS_NO_MEMORY;
1933 nt_status = samdb_set_password(ldb, mem_ctx,
1934 user_dn, NULL,
1935 msg, new_password,
1936 lmNewHash, ntNewHash,
1937 user_change, /* This is a password set, not change */
1938 reject_reason, _dominfo);
1939 if (!NT_STATUS_IS_OK(nt_status)) {
1940 ldb_transaction_cancel(ldb);
1941 return nt_status;
1944 /* modify the samdb record */
1945 ret = samdb_replace(ldb, mem_ctx, msg);
1946 if (ret != LDB_SUCCESS) {
1947 ldb_transaction_cancel(ldb);
1948 return NT_STATUS_ACCESS_DENIED;
1951 ret = ldb_transaction_commit(ldb);
1952 if (ret != LDB_SUCCESS) {
1953 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
1954 ldb_dn_get_linearized(msg->dn),
1955 ldb_errstring(ldb)));
1956 return NT_STATUS_TRANSACTION_ABORTED;
1958 return NT_STATUS_OK;
1962 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1963 struct dom_sid *sid, struct ldb_dn **ret_dn)
1965 struct ldb_message *msg;
1966 struct ldb_dn *basedn;
1967 const char *sidstr;
1968 int ret;
1970 sidstr = dom_sid_string(mem_ctx, sid);
1971 NT_STATUS_HAVE_NO_MEMORY(sidstr);
1973 /* We might have to create a ForeignSecurityPrincipal, even if this user
1974 * is in our own domain */
1976 msg = ldb_msg_new(mem_ctx);
1977 if (msg == NULL) {
1978 return NT_STATUS_NO_MEMORY;
1981 /* TODO: Hmmm. This feels wrong. How do I find the base dn to
1982 * put the ForeignSecurityPrincipals? d_state->domain_dn does
1983 * not work, this is wrong for the Builtin domain, there's no
1984 * cn=For...,cn=Builtin,dc={BASEDN}. -- vl
1987 basedn = samdb_search_dn(sam_ctx, mem_ctx, NULL,
1988 "(&(objectClass=container)(cn=ForeignSecurityPrincipals))");
1990 if (basedn == NULL) {
1991 DEBUG(0, ("Failed to find DN for "
1992 "ForeignSecurityPrincipal container\n"));
1993 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1996 /* add core elements to the ldb_message for the alias */
1997 msg->dn = ldb_dn_copy(mem_ctx, basedn);
1998 if ( ! ldb_dn_add_child_fmt(msg->dn, "CN=%s", sidstr))
1999 return NT_STATUS_NO_MEMORY;
2001 samdb_msg_add_string(sam_ctx, mem_ctx, msg,
2002 "objectClass",
2003 "foreignSecurityPrincipal");
2005 /* create the alias */
2006 ret = ldb_add(sam_ctx, msg);
2007 if (ret != 0) {
2008 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2009 "record %s: %s\n",
2010 ldb_dn_get_linearized(msg->dn),
2011 ldb_errstring(sam_ctx)));
2012 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2014 *ret_dn = msg->dn;
2015 return NT_STATUS_OK;
2020 Find the DN of a domain, assuming it to be a dotted.dns name
2023 struct ldb_dn *samdb_dns_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *dns_domain)
2025 int i;
2026 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2027 const char *binary_encoded;
2028 const char **split_realm;
2029 struct ldb_dn *dn;
2031 if (!tmp_ctx) {
2032 return NULL;
2035 split_realm = (const char **)str_list_make(tmp_ctx, dns_domain, ".");
2036 if (!split_realm) {
2037 talloc_free(tmp_ctx);
2038 return NULL;
2040 dn = ldb_dn_new(mem_ctx, ldb, NULL);
2041 for (i=0; split_realm[i]; i++) {
2042 binary_encoded = ldb_binary_encode_string(tmp_ctx, split_realm[i]);
2043 if (!ldb_dn_add_base_fmt(dn, "dc=%s", binary_encoded)) {
2044 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2045 binary_encoded, ldb_dn_get_linearized(dn)));
2046 talloc_free(tmp_ctx);
2047 return NULL;
2050 if (!ldb_dn_validate(dn)) {
2051 DEBUG(2, ("Failed to validated DN %s\n",
2052 ldb_dn_get_linearized(dn)));
2053 return NULL;
2055 return dn;
2058 Find the DN of a domain, be it the netbios or DNS name
2061 struct ldb_dn *samdb_domain_to_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
2062 const char *domain_name)
2064 const char * const domain_ref_attrs[] = {
2065 "ncName", NULL
2067 const char * const domain_ref2_attrs[] = {
2068 NULL
2070 struct ldb_result *res_domain_ref;
2071 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
2072 /* find the domain's DN */
2073 int ret_domain = ldb_search(ldb, mem_ctx,
2074 &res_domain_ref,
2075 samdb_partitions_dn(ldb, mem_ctx),
2076 LDB_SCOPE_ONELEVEL,
2077 domain_ref_attrs,
2078 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2079 escaped_domain);
2080 if (ret_domain != 0) {
2081 return NULL;
2084 if (res_domain_ref->count == 0) {
2085 ret_domain = ldb_search(ldb, mem_ctx,
2086 &res_domain_ref,
2087 samdb_dns_domain_to_dn(ldb, mem_ctx, domain_name),
2088 LDB_SCOPE_BASE,
2089 domain_ref2_attrs,
2090 "(objectclass=domain)");
2091 if (ret_domain != 0) {
2092 return NULL;
2095 if (res_domain_ref->count == 1) {
2096 return res_domain_ref->msgs[0]->dn;
2098 return NULL;
2101 if (res_domain_ref->count > 1) {
2102 DEBUG(0,("Found %d records matching domain [%s]\n",
2103 ret_domain, domain_name));
2104 return NULL;
2107 return samdb_result_dn(ldb, mem_ctx, res_domain_ref->msgs[0], "nCName", NULL);
2113 use a GUID to find a DN
2115 int dsdb_find_dn_by_guid(struct ldb_context *ldb,
2116 TALLOC_CTX *mem_ctx,
2117 const char *guid_str, struct ldb_dn **dn)
2119 int ret;
2120 struct ldb_result *res;
2121 const char *attrs[] = { NULL };
2122 struct ldb_request *search_req;
2123 char *expression;
2124 struct ldb_search_options_control *options;
2126 expression = talloc_asprintf(mem_ctx, "objectGUID=%s", guid_str);
2127 if (!expression) {
2128 DEBUG(0, (__location__ ": out of memory\n"));
2129 return LDB_ERR_OPERATIONS_ERROR;
2132 res = talloc_zero(mem_ctx, struct ldb_result);
2133 if (!res) {
2134 DEBUG(0, (__location__ ": out of memory\n"));
2135 return LDB_ERR_OPERATIONS_ERROR;
2138 ret = ldb_build_search_req(&search_req, ldb, mem_ctx,
2139 ldb_get_default_basedn(ldb),
2140 LDB_SCOPE_SUBTREE,
2141 expression, attrs,
2142 NULL,
2143 res, ldb_search_default_callback,
2144 NULL);
2145 if (ret != LDB_SUCCESS) {
2146 return ret;
2149 /* we need to cope with cross-partition links, so search for
2150 the GUID over all partitions */
2151 options = talloc(search_req, struct ldb_search_options_control);
2152 if (options == NULL) {
2153 DEBUG(0, (__location__ ": out of memory\n"));
2154 return LDB_ERR_OPERATIONS_ERROR;
2156 options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
2158 ret = ldb_request_add_control(search_req, LDB_CONTROL_EXTENDED_DN_OID, true, NULL);
2159 if (ret != LDB_SUCCESS) {
2160 return ret;
2163 ret = ldb_request_add_control(search_req,
2164 LDB_CONTROL_SEARCH_OPTIONS_OID,
2165 true, options);
2166 if (ret != LDB_SUCCESS) {
2167 return ret;
2170 ret = ldb_request(ldb, search_req);
2171 if (ret != LDB_SUCCESS) {
2172 return ret;
2175 ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
2176 if (ret != LDB_SUCCESS) {
2177 return ret;
2180 /* this really should be exactly 1, but there is a bug in the
2181 partitions module that can return two here with the
2182 search_options control set */
2183 if (res->count < 1) {
2184 return LDB_ERR_NO_SUCH_OBJECT;
2187 *dn = res->msgs[0]->dn;
2189 return LDB_SUCCESS;
2193 search for attrs on one DN, allowing for deleted objects
2195 int dsdb_search_dn_with_deleted(struct ldb_context *ldb,
2196 TALLOC_CTX *mem_ctx,
2197 struct ldb_result **_res,
2198 struct ldb_dn *basedn,
2199 const char * const *attrs)
2201 int ret;
2202 struct ldb_request *req;
2203 TALLOC_CTX *tmp_ctx;
2204 struct ldb_result *res;
2206 tmp_ctx = talloc_new(mem_ctx);
2208 res = talloc_zero(tmp_ctx, struct ldb_result);
2209 if (!res) {
2210 return LDB_ERR_OPERATIONS_ERROR;
2213 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
2214 basedn,
2215 LDB_SCOPE_BASE,
2216 NULL,
2217 attrs,
2218 NULL,
2219 res,
2220 ldb_search_default_callback,
2221 NULL);
2222 if (ret != LDB_SUCCESS) {
2223 talloc_free(tmp_ctx);
2224 return ret;
2227 ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
2228 if (ret != LDB_SUCCESS) {
2229 return ret;
2232 ret = ldb_request(ldb, req);
2233 if (ret == LDB_SUCCESS) {
2234 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2237 talloc_free(req);
2238 *_res = talloc_steal(mem_ctx, res);
2239 return ret;
2244 use a DN to find a GUID
2246 int dsdb_find_guid_by_dn(struct ldb_context *ldb,
2247 struct ldb_dn *dn, struct GUID *guid)
2249 int ret;
2250 struct ldb_result *res;
2251 const char *attrs[] = { "objectGUID", NULL };
2252 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2254 ret = dsdb_search_dn_with_deleted(ldb, tmp_ctx, &res, dn, attrs);
2255 if (ret != LDB_SUCCESS) {
2256 talloc_free(tmp_ctx);
2257 return ret;
2259 if (res->count < 1) {
2260 talloc_free(tmp_ctx);
2261 return LDB_ERR_NO_SUCH_OBJECT;
2263 *guid = samdb_result_guid(res->msgs[0], "objectGUID");
2264 talloc_free(tmp_ctx);
2265 return LDB_SUCCESS;
2269 use a DN to find a SID
2271 int dsdb_find_sid_by_dn(struct ldb_context *ldb,
2272 struct ldb_dn *dn, struct dom_sid *sid)
2274 int ret;
2275 struct ldb_result *res;
2276 const char *attrs[] = { "objectSID", NULL };
2277 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2278 struct dom_sid *s;
2280 ZERO_STRUCTP(sid);
2282 ret = dsdb_search_dn_with_deleted(ldb, tmp_ctx, &res, dn, attrs);
2283 if (ret != LDB_SUCCESS) {
2284 talloc_free(tmp_ctx);
2285 return ret;
2287 if (res->count < 1) {
2288 talloc_free(tmp_ctx);
2289 return LDB_ERR_NO_SUCH_OBJECT;
2291 s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSID");
2292 if (s == NULL) {
2293 talloc_free(tmp_ctx);
2294 return LDB_ERR_NO_SUCH_OBJECT;
2296 *sid = *s;
2297 talloc_free(tmp_ctx);
2298 return LDB_SUCCESS;
2304 load a repsFromTo blob list for a given partition GUID
2305 attr must be "repsFrom" or "repsTo"
2307 WERROR dsdb_loadreps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2308 const char *attr, struct repsFromToBlob **r, uint32_t *count)
2310 const char *attrs[] = { attr, NULL };
2311 struct ldb_result *res = NULL;
2312 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2313 int i;
2314 struct ldb_message_element *el;
2316 *r = NULL;
2317 *count = 0;
2319 if (ldb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, NULL) != LDB_SUCCESS ||
2320 res->count < 1) {
2321 DEBUG(0,("dsdb_loadreps: failed to read partition object\n"));
2322 talloc_free(tmp_ctx);
2323 return WERR_DS_DRA_INTERNAL_ERROR;
2326 el = ldb_msg_find_element(res->msgs[0], attr);
2327 if (el == NULL) {
2328 /* it's OK to be empty */
2329 talloc_free(tmp_ctx);
2330 return WERR_OK;
2333 *count = el->num_values;
2334 *r = talloc_array(mem_ctx, struct repsFromToBlob, *count);
2335 if (*r == NULL) {
2336 talloc_free(tmp_ctx);
2337 return WERR_DS_DRA_INTERNAL_ERROR;
2340 for (i=0; i<(*count); i++) {
2341 enum ndr_err_code ndr_err;
2342 ndr_err = ndr_pull_struct_blob(&el->values[i],
2343 mem_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
2344 &(*r)[i],
2345 (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
2346 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2347 talloc_free(tmp_ctx);
2348 return WERR_DS_DRA_INTERNAL_ERROR;
2352 talloc_free(tmp_ctx);
2354 return WERR_OK;
2358 save the repsFromTo blob list for a given partition GUID
2359 attr must be "repsFrom" or "repsTo"
2361 WERROR dsdb_savereps(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, struct ldb_dn *dn,
2362 const char *attr, struct repsFromToBlob *r, uint32_t count)
2364 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2365 struct ldb_message *msg;
2366 struct ldb_message_element *el;
2367 int i;
2369 msg = ldb_msg_new(tmp_ctx);
2370 msg->dn = dn;
2371 if (ldb_msg_add_empty(msg, attr, LDB_FLAG_MOD_REPLACE, &el) != LDB_SUCCESS) {
2372 goto failed;
2375 el->values = talloc_array(msg, struct ldb_val, count);
2376 if (!el->values) {
2377 goto failed;
2380 for (i=0; i<count; i++) {
2381 struct ldb_val v;
2382 enum ndr_err_code ndr_err;
2384 ndr_err = ndr_push_struct_blob(&v, tmp_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
2385 &r[i],
2386 (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
2387 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2388 goto failed;
2391 el->num_values++;
2392 el->values[i] = v;
2395 if (ldb_modify(sam_ctx, msg) != LDB_SUCCESS) {
2396 DEBUG(0,("Failed to store %s - %s\n", attr, ldb_errstring(sam_ctx)));
2397 goto failed;
2400 talloc_free(tmp_ctx);
2402 return WERR_OK;
2404 failed:
2405 talloc_free(tmp_ctx);
2406 return WERR_DS_DRA_INTERNAL_ERROR;
2411 load the uSNHighest attribute from the @REPLCHANGED object for a
2412 partition
2414 int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn, uint64_t *uSN)
2416 struct ldb_request *req;
2417 int ret;
2418 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2419 struct dsdb_control_current_partition *p_ctrl;
2420 struct ldb_result *res;
2422 res = talloc_zero(tmp_ctx, struct ldb_result);
2423 if (!res) {
2424 talloc_free(tmp_ctx);
2425 return LDB_ERR_OPERATIONS_ERROR;
2428 ret = ldb_build_search_req(&req, ldb, tmp_ctx,
2429 ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
2430 LDB_SCOPE_BASE,
2431 NULL, NULL,
2432 NULL,
2433 res, ldb_search_default_callback,
2434 NULL);
2435 if (ret != LDB_SUCCESS) {
2436 talloc_free(tmp_ctx);
2437 return ret;
2440 p_ctrl = talloc(req, struct dsdb_control_current_partition);
2441 if (p_ctrl == NULL) {
2442 talloc_free(res);
2443 return LDB_ERR_OPERATIONS_ERROR;
2445 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2446 p_ctrl->dn = dn;
2449 ret = ldb_request_add_control(req,
2450 DSDB_CONTROL_CURRENT_PARTITION_OID,
2451 false, p_ctrl);
2452 if (ret != LDB_SUCCESS) {
2453 talloc_free(tmp_ctx);
2454 return ret;
2457 /* Run the new request */
2458 ret = ldb_request(ldb, req);
2460 if (ret == LDB_SUCCESS) {
2461 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2464 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2465 /* it hasn't been created yet, which means
2466 an implicit value of zero */
2467 *uSN = 0;
2468 talloc_free(tmp_ctx);
2469 return LDB_SUCCESS;
2472 if (ret != LDB_SUCCESS) {
2473 talloc_free(tmp_ctx);
2474 return ret;
2477 if (res->count < 1) {
2478 *uSN = 0;
2479 } else {
2480 *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
2483 talloc_free(tmp_ctx);
2485 return LDB_SUCCESS;
2489 save the uSNHighest attribute in the @REPLCHANGED object for a
2490 partition
2492 int dsdb_save_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn, uint64_t uSN)
2494 struct ldb_request *req;
2495 struct ldb_message *msg;
2496 struct dsdb_control_current_partition *p_ctrl;
2497 int ret;
2499 msg = ldb_msg_new(ldb);
2500 if (msg == NULL) {
2501 return LDB_ERR_OPERATIONS_ERROR;
2504 msg->dn = ldb_dn_new(msg, ldb, "@REPLCHANGED");
2505 if (msg->dn == NULL) {
2506 talloc_free(msg);
2507 return LDB_ERR_OPERATIONS_ERROR;
2510 ret = ldb_msg_add_fmt(msg, "uSNHighest", "%llu", (unsigned long long)uSN);
2511 if (ret != LDB_SUCCESS) {
2512 talloc_free(msg);
2513 return ret;
2515 msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
2518 p_ctrl = talloc(msg, struct dsdb_control_current_partition);
2519 if (p_ctrl == NULL) {
2520 talloc_free(msg);
2521 return LDB_ERR_OPERATIONS_ERROR;
2523 p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
2524 p_ctrl->dn = dn;
2526 ret = ldb_build_mod_req(&req, ldb, msg,
2527 msg,
2528 NULL,
2529 NULL, ldb_op_default_callback,
2530 NULL);
2531 again:
2532 if (ret != LDB_SUCCESS) {
2533 talloc_free(msg);
2534 return ret;
2537 ret = ldb_request_add_control(req,
2538 DSDB_CONTROL_CURRENT_PARTITION_OID,
2539 false, p_ctrl);
2540 if (ret != LDB_SUCCESS) {
2541 talloc_free(msg);
2542 return ret;
2545 /* Run the new request */
2546 ret = ldb_request(ldb, req);
2548 if (ret == LDB_SUCCESS) {
2549 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2551 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2552 ret = ldb_build_add_req(&req, ldb, msg,
2553 msg,
2554 NULL,
2555 NULL, ldb_op_default_callback,
2556 NULL);
2557 goto again;
2560 talloc_free(msg);
2562 return ret;
2565 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
2566 const struct drsuapi_DsReplicaCursor2 *c2)
2568 return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
2572 see if we are a RODC
2574 TODO: This should take a sam_ctx, and lookup the right object (with
2575 a cache)
2577 bool samdb_rodc(struct loadparm_context *lp_ctx)
2579 return lp_parm_bool(lp_ctx, NULL, "repl", "RODC", false);
2584 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
2586 flags are DS_NTDS_OPTION_*
2588 int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
2590 TALLOC_CTX *tmp_ctx;
2591 const char *attrs[] = { "options", NULL };
2592 int ret;
2593 struct ldb_result *res;
2595 tmp_ctx = talloc_new(ldb);
2596 if (tmp_ctx == NULL) {
2597 goto failed;
2600 ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
2601 if (ret) {
2602 goto failed;
2605 if (res->count != 1) {
2606 goto failed;
2609 *options = samdb_result_uint(res->msgs[0], "options", 0);
2611 talloc_free(tmp_ctx);
2613 return LDB_SUCCESS;
2615 failed:
2616 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
2617 talloc_free(tmp_ctx);
2618 return LDB_ERR_NO_SUCH_OBJECT;