2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 2004
6 Copyright (C) Volker Lendecke 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "events/events.h"
27 #include "ldb_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"
40 #include "system/locale.h"
41 #include "lib/util/tsort.h"
42 #include "dsdb/common/util.h"
43 #include "lib/socket/socket.h"
44 #include "dsdb/samdb/ldb_modules/util.h"
47 search the sam for the specified attributes in a specific domain, filter on
48 objectSid being in domain_sid.
50 int samdb_search_domain(struct ldb_context
*sam_ldb
,
52 struct ldb_dn
*basedn
,
53 struct ldb_message
***res
,
54 const char * const *attrs
,
55 const struct dom_sid
*domain_sid
,
56 const char *format
, ...) _PRINTF_ATTRIBUTE(7,8)
62 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
,
63 res
, attrs
, format
, ap
);
69 struct dom_sid
*entry_sid
;
71 entry_sid
= samdb_result_dom_sid(mem_ctx
, (*res
)[i
], "objectSid");
73 if ((entry_sid
== NULL
) ||
74 (!dom_sid_in_domain(domain_sid
, entry_sid
))) {
75 /* Delete that entry from the result set */
76 (*res
)[i
] = (*res
)[count
-1];
78 talloc_free(entry_sid
);
81 talloc_free(entry_sid
);
89 search the sam for a single string attribute in exactly 1 record
91 const char *samdb_search_string_v(struct ldb_context
*sam_ldb
,
93 struct ldb_dn
*basedn
,
94 const char *attr_name
,
95 const char *format
, va_list ap
) _PRINTF_ATTRIBUTE(5,0)
98 const char *attrs
[2] = { NULL
, NULL
};
99 struct ldb_message
**res
= NULL
;
101 attrs
[0] = attr_name
;
103 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
105 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
106 attr_name
, format
, count
));
113 return samdb_result_string(res
[0], attr_name
, NULL
);
117 search the sam for a single string attribute in exactly 1 record
119 const char *samdb_search_string(struct ldb_context
*sam_ldb
,
121 struct ldb_dn
*basedn
,
122 const char *attr_name
,
123 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
128 va_start(ap
, format
);
129 str
= samdb_search_string_v(sam_ldb
, mem_ctx
, basedn
, attr_name
, format
, ap
);
135 struct ldb_dn
*samdb_search_dn(struct ldb_context
*sam_ldb
,
137 struct ldb_dn
*basedn
,
138 const char *format
, ...) _PRINTF_ATTRIBUTE(4,5)
142 struct ldb_message
**res
= NULL
;
145 va_start(ap
, format
);
146 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, NULL
, format
, ap
);
149 if (count
!= 1) return NULL
;
151 ret
= talloc_steal(mem_ctx
, res
[0]->dn
);
158 search the sam for a dom_sid attribute in exactly 1 record
160 struct dom_sid
*samdb_search_dom_sid(struct ldb_context
*sam_ldb
,
162 struct ldb_dn
*basedn
,
163 const char *attr_name
,
164 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
168 struct ldb_message
**res
;
169 const char *attrs
[2] = { NULL
, NULL
};
172 attrs
[0] = attr_name
;
174 va_start(ap
, format
);
175 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
178 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
179 attr_name
, format
, count
));
185 sid
= samdb_result_dom_sid(mem_ctx
, res
[0], attr_name
);
191 return the count of the number of records in the sam matching the query
193 int samdb_search_count(struct ldb_context
*sam_ldb
,
194 struct ldb_dn
*basedn
,
195 const char *format
, ...) _PRINTF_ATTRIBUTE(3,4)
198 struct ldb_message
**res
;
199 const char *attrs
[] = { NULL
};
201 TALLOC_CTX
*tmp_ctx
= talloc_new(sam_ldb
);
203 va_start(ap
, format
);
204 ret
= gendb_search_v(sam_ldb
, tmp_ctx
, basedn
, &res
, attrs
, format
, ap
);
206 talloc_free(tmp_ctx
);
213 search the sam for a single integer attribute in exactly 1 record
215 unsigned int samdb_search_uint(struct ldb_context
*sam_ldb
,
217 unsigned int default_value
,
218 struct ldb_dn
*basedn
,
219 const char *attr_name
,
220 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
224 struct ldb_message
**res
;
225 const char *attrs
[2] = { NULL
, NULL
};
227 attrs
[0] = attr_name
;
229 va_start(ap
, format
);
230 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
234 return default_value
;
237 return samdb_result_uint(res
[0], attr_name
, default_value
);
241 search the sam for a single signed 64 bit integer attribute in exactly 1 record
243 int64_t samdb_search_int64(struct ldb_context
*sam_ldb
,
245 int64_t default_value
,
246 struct ldb_dn
*basedn
,
247 const char *attr_name
,
248 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
252 struct ldb_message
**res
;
253 const char *attrs
[2] = { NULL
, NULL
};
255 attrs
[0] = attr_name
;
257 va_start(ap
, format
);
258 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
262 return default_value
;
265 return samdb_result_int64(res
[0], attr_name
, default_value
);
269 search the sam for multipe records each giving a single string attribute
270 return the number of matches, or -1 on error
272 int samdb_search_string_multiple(struct ldb_context
*sam_ldb
,
274 struct ldb_dn
*basedn
,
276 const char *attr_name
,
277 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
281 const char *attrs
[2] = { NULL
, NULL
};
282 struct ldb_message
**res
= NULL
;
284 attrs
[0] = attr_name
;
286 va_start(ap
, format
);
287 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
294 /* make sure its single valued */
295 for (i
=0;i
<count
;i
++) {
296 if (res
[i
]->num_elements
!= 1) {
297 DEBUG(1,("samdb: search for %s %s not single valued\n",
304 *strs
= talloc_array(mem_ctx
, const char *, count
+1);
310 for (i
=0;i
<count
;i
++) {
311 (*strs
)[i
] = samdb_result_string(res
[i
], attr_name
, NULL
);
313 (*strs
)[count
] = NULL
;
319 pull a uint from a result set.
321 unsigned int samdb_result_uint(const struct ldb_message
*msg
, const char *attr
, unsigned int default_value
)
323 return ldb_msg_find_attr_as_uint(msg
, attr
, default_value
);
327 pull a (signed) int64 from a result set.
329 int64_t samdb_result_int64(const struct ldb_message
*msg
, const char *attr
, int64_t default_value
)
331 return ldb_msg_find_attr_as_int64(msg
, attr
, default_value
);
335 pull a string from a result set.
337 const char *samdb_result_string(const struct ldb_message
*msg
, const char *attr
,
338 const char *default_value
)
340 return ldb_msg_find_attr_as_string(msg
, attr
, default_value
);
343 struct ldb_dn
*samdb_result_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
344 const char *attr
, struct ldb_dn
*default_value
)
346 struct ldb_dn
*ret_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, msg
, attr
);
348 return default_value
;
354 pull a rid from a objectSid in a result set.
356 uint32_t samdb_result_rid_from_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
357 const char *attr
, uint32_t default_value
)
362 sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
364 return default_value
;
366 rid
= sid
->sub_auths
[sid
->num_auths
-1];
372 pull a dom_sid structure from a objectSid in a result set.
374 struct dom_sid
*samdb_result_dom_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
377 const struct ldb_val
*v
;
379 enum ndr_err_code ndr_err
;
380 v
= ldb_msg_find_ldb_val(msg
, attr
);
384 sid
= talloc(mem_ctx
, struct dom_sid
);
388 ndr_err
= ndr_pull_struct_blob(v
, sid
, sid
,
389 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
390 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
398 pull a guid structure from a objectGUID in a result set.
400 struct GUID
samdb_result_guid(const struct ldb_message
*msg
, const char *attr
)
402 const struct ldb_val
*v
;
406 v
= ldb_msg_find_ldb_val(msg
, attr
);
407 if (!v
) return GUID_zero();
409 status
= GUID_from_ndr_blob(v
, &guid
);
410 if (!NT_STATUS_IS_OK(status
)) {
418 pull a sid prefix from a objectSid in a result set.
419 this is used to find the domain sid for a user
421 struct dom_sid
*samdb_result_sid_prefix(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
424 struct dom_sid
*sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
425 if (!sid
|| sid
->num_auths
< 1) return NULL
;
431 pull a NTTIME in a result set.
433 NTTIME
samdb_result_nttime(const struct ldb_message
*msg
, const char *attr
,
434 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(const struct ldb_message
*msg
)
447 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "lastLogoff",0);
450 ret
= 0x7FFFFFFFFFFFFFFFULL
;
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(const struct ldb_message
*msg
)
469 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "accountExpires",
473 ret
= 0x7FFFFFFFFFFFFFFFULL
;
479 pull a uint64_t from a result set.
481 uint64_t samdb_result_uint64(const struct ldb_message
*msg
, const char *attr
,
482 uint64_t default_value
)
484 return ldb_msg_find_attr_as_uint64(msg
, attr
, default_value
);
489 construct the allow_password_change field from the PwdLastSet attribute and the
490 domain password settings
492 NTTIME
samdb_result_allow_password_change(struct ldb_context
*sam_ldb
,
494 struct ldb_dn
*domain_dn
,
495 struct ldb_message
*msg
,
498 uint64_t attr_time
= samdb_result_uint64(msg
, attr
, 0);
501 if (attr_time
== 0) {
505 minPwdAge
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
, "minPwdAge", NULL
);
507 /* yes, this is a -= not a += as minPwdAge is stored as the negative
508 of the number of 100-nano-seconds */
509 attr_time
-= minPwdAge
;
515 construct the force_password_change field from the PwdLastSet
516 attribute, the userAccountControl and the domain password settings
518 NTTIME
samdb_result_force_password_change(struct ldb_context
*sam_ldb
,
520 struct ldb_dn
*domain_dn
,
521 struct ldb_message
*msg
)
523 uint64_t attr_time
= samdb_result_uint64(msg
, "pwdLastSet", 0);
524 uint32_t userAccountControl
= samdb_result_uint64(msg
, "userAccountControl", 0);
527 /* Machine accounts don't expire, and there is a flag for 'no expiry' */
528 if (!(userAccountControl
& UF_NORMAL_ACCOUNT
)
529 || (userAccountControl
& UF_DONT_EXPIRE_PASSWD
)) {
530 return 0x7FFFFFFFFFFFFFFFULL
;
533 if (attr_time
== 0) {
537 maxPwdAge
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
, "maxPwdAge", NULL
);
538 if (maxPwdAge
== 0) {
539 return 0x7FFFFFFFFFFFFFFFULL
;
541 attr_time
-= maxPwdAge
;
548 pull a samr_Password structutre from a result set.
550 struct samr_Password
*samdb_result_hash(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
, const char *attr
)
552 struct samr_Password
*hash
= NULL
;
553 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
554 if (val
&& (val
->length
>= sizeof(hash
->hash
))) {
555 hash
= talloc(mem_ctx
, struct samr_Password
);
556 memcpy(hash
->hash
, val
->data
, MIN(val
->length
, sizeof(hash
->hash
)));
562 pull an array of samr_Password structures from a result set.
564 unsigned int samdb_result_hashes(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
565 const char *attr
, struct samr_Password
**hashes
)
567 unsigned int count
, i
;
568 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
574 count
= val
->length
/ 16;
579 *hashes
= talloc_array(mem_ctx
, struct samr_Password
, count
);
584 for (i
=0;i
<count
;i
++) {
585 memcpy((*hashes
)[i
].hash
, (i
*16)+(char *)val
->data
, 16);
591 NTSTATUS
samdb_result_passwords(TALLOC_CTX
*mem_ctx
, struct loadparm_context
*lp_ctx
, struct ldb_message
*msg
,
592 struct samr_Password
**lm_pwd
, struct samr_Password
**nt_pwd
)
594 struct samr_Password
*lmPwdHash
, *ntPwdHash
;
597 num_nt
= samdb_result_hashes(mem_ctx
, msg
, "unicodePwd", &ntPwdHash
);
600 } else if (num_nt
> 1) {
601 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
603 *nt_pwd
= &ntPwdHash
[0];
607 /* Ensure that if we have turned off LM
608 * authentication, that we never use the LM hash, even
610 if (lp_lanman_auth(lp_ctx
)) {
612 num_lm
= samdb_result_hashes(mem_ctx
, msg
, "dBCSPwd", &lmPwdHash
);
615 } else if (num_lm
> 1) {
616 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
618 *lm_pwd
= &lmPwdHash
[0];
628 pull a samr_LogonHours structutre from a result set.
630 struct samr_LogonHours
samdb_result_logon_hours(TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
, const char *attr
)
632 struct samr_LogonHours hours
;
633 size_t units_per_week
= 168;
634 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
639 units_per_week
= val
->length
* 8;
642 hours
.bits
= talloc_array(mem_ctx
, uint8_t, units_per_week
/8);
646 hours
.units_per_week
= units_per_week
;
647 memset(hours
.bits
, 0xFF, units_per_week
/8);
649 memcpy(hours
.bits
, val
->data
, val
->length
);
656 pull a set of account_flags from a result set.
658 This requires that the attributes:
663 uint32_t samdb_result_acct_flags(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
664 struct ldb_message
*msg
, struct ldb_dn
*domain_dn
)
666 uint32_t userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
667 uint32_t acct_flags
= ds_uf2acb(userAccountControl
);
668 NTTIME must_change_time
;
671 must_change_time
= samdb_result_force_password_change(sam_ctx
, mem_ctx
,
674 /* Test account expire time */
675 unix_to_nt_time(&now
, time(NULL
));
676 /* check for expired password */
677 if (must_change_time
< now
) {
678 acct_flags
|= ACB_PW_EXPIRED
;
683 struct lsa_BinaryString
samdb_result_parameters(TALLOC_CTX
*mem_ctx
,
684 struct ldb_message
*msg
,
687 struct lsa_BinaryString s
;
688 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
696 s
.array
= talloc_array(mem_ctx
, uint16_t, val
->length
/2);
700 s
.length
= s
.size
= val
->length
;
701 memcpy(s
.array
, val
->data
, val
->length
);
706 /* Find an attribute, with a particular value */
708 /* The current callers of this function expect a very specific
709 * behaviour: In particular, objectClass subclass equivilance is not
710 * wanted. This means that we should not lookup the schema for the
711 * comparison function */
712 struct ldb_message_element
*samdb_find_attribute(struct ldb_context
*ldb
,
713 const struct ldb_message
*msg
,
714 const char *name
, const char *value
)
717 struct ldb_message_element
*el
= ldb_msg_find_element(msg
, name
);
723 for (i
=0;i
<el
->num_values
;i
++) {
724 if (ldb_attr_cmp(value
, (char *)el
->values
[i
].data
) == 0) {
733 * This is intended for use by the "password hash" module since there
734 * password changes can be specified through one message element with the
735 * new password (to set) and another one with the old password (to unset).
737 * The first which sets a password (new value) can have flags
738 * (LDB_FLAG_MOD_ADD, LDB_FLAG_MOD_REPLACE) but also none (on "add" operations
739 * for entries). The latter (old value) has always specified
740 * LDB_FLAG_MOD_DELETE.
742 * Returns LDB_ERR_NO_SUCH_ATTRIBUTE if the attribute which should be deleted
743 * doesn't contain only one value (this is the Windows Server behaviour)
744 * otherwise LDB_SUCCESS.
746 int samdb_msg_find_old_and_new_ldb_val(const struct ldb_message
*msg
,
748 const struct ldb_val
**new_val
,
749 const struct ldb_val
**old_val
)
760 for (i
= 0; i
< msg
->num_elements
; i
++) {
761 if (ldb_attr_cmp(msg
->elements
[i
].name
, name
) == 0) {
762 if (msg
->elements
[i
].flags
== LDB_FLAG_MOD_DELETE
) {
763 *old_val
= &msg
->elements
[i
].values
[0];
765 *new_val
= &msg
->elements
[i
].values
[0];
773 int samdb_find_or_add_value(struct ldb_context
*ldb
, struct ldb_message
*msg
, const char *name
, const char *set_value
)
775 if (samdb_find_attribute(ldb
, msg
, name
, set_value
) == NULL
) {
776 return samdb_msg_add_string(ldb
, msg
, msg
, name
, set_value
);
781 int samdb_find_or_add_attribute(struct ldb_context
*ldb
, struct ldb_message
*msg
, const char *name
, const char *set_value
)
783 struct ldb_message_element
*el
;
785 el
= ldb_msg_find_element(msg
, name
);
790 return samdb_msg_add_string(ldb
, msg
, msg
, name
, set_value
);
796 add a string element to a message
798 int samdb_msg_add_string(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
799 const char *attr_name
, const char *str
)
801 char *s
= talloc_strdup(mem_ctx
, str
);
802 char *a
= talloc_strdup(mem_ctx
, attr_name
);
803 if (s
== NULL
|| a
== NULL
) {
804 return LDB_ERR_OPERATIONS_ERROR
;
806 return ldb_msg_add_string(msg
, a
, s
);
810 add a dom_sid element to a message
812 int samdb_msg_add_dom_sid(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
813 const char *attr_name
, struct dom_sid
*sid
)
816 enum ndr_err_code ndr_err
;
818 ndr_err
= ndr_push_struct_blob(&v
, mem_ctx
,
820 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
821 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
822 return LDB_ERR_OPERATIONS_ERROR
;
824 return ldb_msg_add_value(msg
, attr_name
, &v
, NULL
);
829 add a delete element operation to a message
831 int samdb_msg_add_delete(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
832 const char *attr_name
)
834 /* we use an empty replace rather than a delete, as it allows for
835 dsdb_replace() to be used everywhere */
836 return ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_REPLACE
, NULL
);
840 add a add attribute value to a message
842 int samdb_msg_add_addval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
843 const char *attr_name
, const char *value
)
845 struct ldb_message_element
*el
;
848 a
= talloc_strdup(mem_ctx
, attr_name
);
850 return LDB_ERR_OPERATIONS_ERROR
;
851 v
= talloc_strdup(mem_ctx
, value
);
853 return LDB_ERR_OPERATIONS_ERROR
;
854 ret
= ldb_msg_add_string(msg
, a
, v
);
857 el
= ldb_msg_find_element(msg
, a
);
859 return LDB_ERR_OPERATIONS_ERROR
;
860 el
->flags
= LDB_FLAG_MOD_ADD
;
865 add a delete attribute value to a message
867 int samdb_msg_add_delval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
868 const char *attr_name
, const char *value
)
870 struct ldb_message_element
*el
;
873 a
= talloc_strdup(mem_ctx
, attr_name
);
875 return LDB_ERR_OPERATIONS_ERROR
;
876 v
= talloc_strdup(mem_ctx
, value
);
878 return LDB_ERR_OPERATIONS_ERROR
;
879 ret
= ldb_msg_add_string(msg
, a
, v
);
882 el
= ldb_msg_find_element(msg
, a
);
884 return LDB_ERR_OPERATIONS_ERROR
;
885 el
->flags
= LDB_FLAG_MOD_DELETE
;
890 add a int element to a message
892 int samdb_msg_add_int(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
893 const char *attr_name
, int v
)
895 const char *s
= talloc_asprintf(mem_ctx
, "%d", v
);
896 return samdb_msg_add_string(sam_ldb
, mem_ctx
, msg
, attr_name
, s
);
900 add a unsigned int element to a message
902 int samdb_msg_add_uint(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
903 const char *attr_name
, unsigned int v
)
905 return samdb_msg_add_int(sam_ldb
, mem_ctx
, msg
, attr_name
, (int)v
);
909 add a (signed) int64_t element to a message
911 int samdb_msg_add_int64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
912 const char *attr_name
, int64_t v
)
914 const char *s
= talloc_asprintf(mem_ctx
, "%lld", (long long)v
);
915 return samdb_msg_add_string(sam_ldb
, mem_ctx
, msg
, attr_name
, s
);
919 add a uint64_t element to a message
921 int samdb_msg_add_uint64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
922 const char *attr_name
, uint64_t v
)
924 return samdb_msg_add_int64(sam_ldb
, mem_ctx
, msg
, attr_name
, (int64_t)v
);
928 add a samr_Password element to a message
930 int samdb_msg_add_hash(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
931 const char *attr_name
, struct samr_Password
*hash
)
934 val
.data
= talloc_memdup(mem_ctx
, hash
->hash
, 16);
936 return LDB_ERR_OPERATIONS_ERROR
;
939 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
943 add a samr_Password array to a message
945 int samdb_msg_add_hashes(TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
946 const char *attr_name
, struct samr_Password
*hashes
,
951 val
.data
= talloc_array_size(mem_ctx
, 16, count
);
952 val
.length
= count
*16;
954 return LDB_ERR_OPERATIONS_ERROR
;
956 for (i
=0;i
<count
;i
++) {
957 memcpy(i
*16 + (char *)val
.data
, hashes
[i
].hash
, 16);
959 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
963 add a acct_flags element to a message
965 int samdb_msg_add_acct_flags(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
966 const char *attr_name
, uint32_t v
)
968 return samdb_msg_add_uint(sam_ldb
, mem_ctx
, msg
, attr_name
, ds_acb2uf(v
));
972 add a logon_hours element to a message
974 int samdb_msg_add_logon_hours(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
975 const char *attr_name
, struct samr_LogonHours
*hours
)
978 val
.length
= hours
->units_per_week
/ 8;
979 val
.data
= hours
->bits
;
980 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
984 add a parameters element to a message
986 int samdb_msg_add_parameters(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
987 const char *attr_name
, struct lsa_BinaryString
*parameters
)
990 val
.length
= parameters
->length
;
991 val
.data
= (uint8_t *)parameters
->array
;
992 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
995 add a general value element to a message
997 int samdb_msg_add_value(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
998 const char *attr_name
, const struct ldb_val
*val
)
1000 return ldb_msg_add_value(msg
, attr_name
, val
, NULL
);
1004 sets a general value element to a message
1006 int samdb_msg_set_value(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1007 const char *attr_name
, const struct ldb_val
*val
)
1009 struct ldb_message_element
*el
;
1011 el
= ldb_msg_find_element(msg
, attr_name
);
1015 return ldb_msg_add_value(msg
, attr_name
, val
, NULL
);
1019 set a string element in a message
1021 int samdb_msg_set_string(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1022 const char *attr_name
, const char *str
)
1024 struct ldb_message_element
*el
;
1026 el
= ldb_msg_find_element(msg
, attr_name
);
1030 return samdb_msg_add_string(sam_ldb
, mem_ctx
, msg
, attr_name
, str
);
1034 * Handle ldb_request in transaction
1036 static int dsdb_autotransaction_request(struct ldb_context
*sam_ldb
,
1037 struct ldb_request
*req
)
1041 ret
= ldb_transaction_start(sam_ldb
);
1042 if (ret
!= LDB_SUCCESS
) {
1046 ret
= ldb_request(sam_ldb
, req
);
1047 if (ret
== LDB_SUCCESS
) {
1048 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1051 if (ret
== LDB_SUCCESS
) {
1052 return ldb_transaction_commit(sam_ldb
);
1054 ldb_transaction_cancel(sam_ldb
);
1060 return a default security descriptor
1062 struct security_descriptor
*samdb_default_security_descriptor(TALLOC_CTX
*mem_ctx
)
1064 struct security_descriptor
*sd
;
1066 sd
= security_descriptor_initialise(mem_ctx
);
1071 struct ldb_dn
*samdb_aggregate_schema_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1073 struct ldb_dn
*schema_dn
= ldb_get_schema_basedn(sam_ctx
);
1074 struct ldb_dn
*aggregate_dn
;
1079 aggregate_dn
= ldb_dn_copy(mem_ctx
, schema_dn
);
1080 if (!aggregate_dn
) {
1083 if (!ldb_dn_add_child_fmt(aggregate_dn
, "CN=Aggregate")) {
1086 return aggregate_dn
;
1089 struct ldb_dn
*samdb_partitions_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1091 struct ldb_dn
*new_dn
;
1093 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_config_basedn(sam_ctx
));
1094 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Partitions")) {
1095 talloc_free(new_dn
);
1101 struct ldb_dn
*samdb_infrastructure_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1103 struct ldb_dn
*new_dn
;
1105 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_default_basedn(sam_ctx
));
1106 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Infrastructure")) {
1107 talloc_free(new_dn
);
1113 struct ldb_dn
*samdb_sites_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1115 struct ldb_dn
*new_dn
;
1117 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_config_basedn(sam_ctx
));
1118 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Sites")) {
1119 talloc_free(new_dn
);
1126 work out the domain sid for the current open ldb
1128 const struct dom_sid
*samdb_domain_sid(struct ldb_context
*ldb
)
1130 TALLOC_CTX
*tmp_ctx
;
1131 const struct dom_sid
*domain_sid
;
1132 const char *attrs
[] = {
1136 struct ldb_result
*res
;
1139 /* see if we have a cached copy */
1140 domain_sid
= (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1145 tmp_ctx
= talloc_new(ldb
);
1146 if (tmp_ctx
== NULL
) {
1150 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_get_default_basedn(ldb
), LDB_SCOPE_BASE
, attrs
, "objectSid=*");
1152 if (ret
!= LDB_SUCCESS
) {
1156 if (res
->count
!= 1) {
1160 domain_sid
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSid");
1161 if (domain_sid
== NULL
) {
1165 /* cache the domain_sid in the ldb */
1166 if (ldb_set_opaque(ldb
, "cache.domain_sid", discard_const_p(struct dom_sid
, domain_sid
)) != LDB_SUCCESS
) {
1170 talloc_steal(ldb
, domain_sid
);
1171 talloc_free(tmp_ctx
);
1176 talloc_free(tmp_ctx
);
1181 get domain sid from cache
1183 const struct dom_sid
*samdb_domain_sid_cache_only(struct ldb_context
*ldb
)
1185 return (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1188 bool samdb_set_domain_sid(struct ldb_context
*ldb
, const struct dom_sid
*dom_sid_in
)
1190 TALLOC_CTX
*tmp_ctx
;
1191 struct dom_sid
*dom_sid_new
;
1192 struct dom_sid
*dom_sid_old
;
1194 /* see if we have a cached copy */
1195 dom_sid_old
= talloc_get_type(ldb_get_opaque(ldb
,
1196 "cache.domain_sid"), struct dom_sid
);
1198 tmp_ctx
= talloc_new(ldb
);
1199 if (tmp_ctx
== NULL
) {
1203 dom_sid_new
= dom_sid_dup(tmp_ctx
, dom_sid_in
);
1208 /* cache the domain_sid in the ldb */
1209 if (ldb_set_opaque(ldb
, "cache.domain_sid", dom_sid_new
) != LDB_SUCCESS
) {
1213 talloc_steal(ldb
, dom_sid_new
);
1214 talloc_free(tmp_ctx
);
1215 talloc_free(dom_sid_old
);
1220 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1221 talloc_free(tmp_ctx
);
1225 bool samdb_set_ntds_settings_dn(struct ldb_context
*ldb
, struct ldb_dn
*ntds_settings_dn_in
)
1227 TALLOC_CTX
*tmp_ctx
;
1228 struct ldb_dn
*ntds_settings_dn_new
;
1229 struct ldb_dn
*ntds_settings_dn_old
;
1231 /* see if we have a cached copy */
1232 ntds_settings_dn_old
= talloc_get_type(ldb_get_opaque(ldb
,
1233 "cache.ntds_settings_dn"), struct ldb_dn
);
1235 tmp_ctx
= talloc_new(ldb
);
1236 if (tmp_ctx
== NULL
) {
1240 ntds_settings_dn_new
= ldb_dn_copy(tmp_ctx
, ntds_settings_dn_in
);
1241 if (!ntds_settings_dn_new
) {
1245 /* cache the domain_sid in the ldb */
1246 if (ldb_set_opaque(ldb
, "cache.ntds_settings_dn", ntds_settings_dn_new
) != LDB_SUCCESS
) {
1250 talloc_steal(ldb
, ntds_settings_dn_new
);
1251 talloc_free(tmp_ctx
);
1252 talloc_free(ntds_settings_dn_old
);
1257 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1258 talloc_free(tmp_ctx
);
1262 /* Obtain the short name of the flexible single master operator
1263 * (FSMO), such as the PDC Emulator */
1264 const char *samdb_result_fsmo_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
1267 /* Format is cn=NTDS Settings,cn=<NETBIOS name of FSMO>,.... */
1268 struct ldb_dn
*fsmo_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, msg
, attr
);
1269 const struct ldb_val
*val
= ldb_dn_get_component_val(fsmo_dn
, 1);
1270 const char *name
= ldb_dn_get_component_name(fsmo_dn
, 1);
1272 if (!name
|| (ldb_attr_cmp(name
, "cn") != 0)) {
1273 /* Ensure this matches the format. This gives us a
1274 * bit more confidence that a 'cn' value will be a
1279 return (char *)val
->data
;
1285 work out the ntds settings dn for the current open ldb
1287 struct ldb_dn
*samdb_ntds_settings_dn(struct ldb_context
*ldb
)
1289 TALLOC_CTX
*tmp_ctx
;
1290 const char *root_attrs
[] = { "dsServiceName", NULL
};
1292 struct ldb_result
*root_res
;
1293 struct ldb_dn
*settings_dn
;
1295 /* see if we have a cached copy */
1296 settings_dn
= (struct ldb_dn
*)ldb_get_opaque(ldb
, "cache.ntds_settings_dn");
1301 tmp_ctx
= talloc_new(ldb
);
1302 if (tmp_ctx
== NULL
) {
1306 ret
= ldb_search(ldb
, tmp_ctx
, &root_res
, ldb_dn_new(tmp_ctx
, ldb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
1308 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1309 ldb_errstring(ldb
)));
1313 if (root_res
->count
!= 1) {
1317 settings_dn
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, root_res
->msgs
[0], "dsServiceName");
1319 /* cache the domain_sid in the ldb */
1320 if (ldb_set_opaque(ldb
, "cache.settings_dn", settings_dn
) != LDB_SUCCESS
) {
1324 talloc_steal(ldb
, settings_dn
);
1325 talloc_free(tmp_ctx
);
1330 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1331 talloc_free(tmp_ctx
);
1336 work out the ntds settings invocationId for the current open ldb
1338 const struct GUID
*samdb_ntds_invocation_id(struct ldb_context
*ldb
)
1340 TALLOC_CTX
*tmp_ctx
;
1341 const char *attrs
[] = { "invocationId", NULL
};
1343 struct ldb_result
*res
;
1344 struct GUID
*invocation_id
;
1346 /* see if we have a cached copy */
1347 invocation_id
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.invocation_id");
1348 if (invocation_id
) {
1349 return invocation_id
;
1352 tmp_ctx
= talloc_new(ldb
);
1353 if (tmp_ctx
== NULL
) {
1357 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
1362 if (res
->count
!= 1) {
1366 invocation_id
= talloc(tmp_ctx
, struct GUID
);
1367 if (!invocation_id
) {
1371 *invocation_id
= samdb_result_guid(res
->msgs
[0], "invocationId");
1373 /* cache the domain_sid in the ldb */
1374 if (ldb_set_opaque(ldb
, "cache.invocation_id", invocation_id
) != LDB_SUCCESS
) {
1378 talloc_steal(ldb
, invocation_id
);
1379 talloc_free(tmp_ctx
);
1381 return invocation_id
;
1384 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1385 talloc_free(tmp_ctx
);
1389 bool samdb_set_ntds_invocation_id(struct ldb_context
*ldb
, const struct GUID
*invocation_id_in
)
1391 TALLOC_CTX
*tmp_ctx
;
1392 struct GUID
*invocation_id_new
;
1393 struct GUID
*invocation_id_old
;
1395 /* see if we have a cached copy */
1396 invocation_id_old
= (struct GUID
*)ldb_get_opaque(ldb
,
1397 "cache.invocation_id");
1399 tmp_ctx
= talloc_new(ldb
);
1400 if (tmp_ctx
== NULL
) {
1404 invocation_id_new
= talloc(tmp_ctx
, struct GUID
);
1405 if (!invocation_id_new
) {
1409 *invocation_id_new
= *invocation_id_in
;
1411 /* cache the domain_sid in the ldb */
1412 if (ldb_set_opaque(ldb
, "cache.invocation_id", invocation_id_new
) != LDB_SUCCESS
) {
1416 talloc_steal(ldb
, invocation_id_new
);
1417 talloc_free(tmp_ctx
);
1418 talloc_free(invocation_id_old
);
1423 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1424 talloc_free(tmp_ctx
);
1429 work out the ntds settings objectGUID for the current open ldb
1431 const struct GUID
*samdb_ntds_objectGUID(struct ldb_context
*ldb
)
1433 TALLOC_CTX
*tmp_ctx
;
1434 const char *attrs
[] = { "objectGUID", NULL
};
1436 struct ldb_result
*res
;
1437 struct GUID
*ntds_guid
;
1439 /* see if we have a cached copy */
1440 ntds_guid
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.ntds_guid");
1445 tmp_ctx
= talloc_new(ldb
);
1446 if (tmp_ctx
== NULL
) {
1450 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
1455 if (res
->count
!= 1) {
1459 ntds_guid
= talloc(tmp_ctx
, struct GUID
);
1464 *ntds_guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
1466 /* cache the domain_sid in the ldb */
1467 if (ldb_set_opaque(ldb
, "cache.ntds_guid", ntds_guid
) != LDB_SUCCESS
) {
1471 talloc_steal(ldb
, ntds_guid
);
1472 talloc_free(tmp_ctx
);
1477 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1478 talloc_free(tmp_ctx
);
1482 bool samdb_set_ntds_objectGUID(struct ldb_context
*ldb
, const struct GUID
*ntds_guid_in
)
1484 TALLOC_CTX
*tmp_ctx
;
1485 struct GUID
*ntds_guid_new
;
1486 struct GUID
*ntds_guid_old
;
1488 /* see if we have a cached copy */
1489 ntds_guid_old
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.ntds_guid");
1491 tmp_ctx
= talloc_new(ldb
);
1492 if (tmp_ctx
== NULL
) {
1496 ntds_guid_new
= talloc(tmp_ctx
, struct GUID
);
1497 if (!ntds_guid_new
) {
1501 *ntds_guid_new
= *ntds_guid_in
;
1503 /* cache the domain_sid in the ldb */
1504 if (ldb_set_opaque(ldb
, "cache.ntds_guid", ntds_guid_new
) != LDB_SUCCESS
) {
1508 talloc_steal(ldb
, ntds_guid_new
);
1509 talloc_free(tmp_ctx
);
1510 talloc_free(ntds_guid_old
);
1515 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1516 talloc_free(tmp_ctx
);
1521 work out the server dn for the current open ldb
1523 struct ldb_dn
*samdb_server_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1525 return ldb_dn_get_parent(mem_ctx
, samdb_ntds_settings_dn(ldb
));
1529 work out the server dn for the current open ldb
1531 struct ldb_dn
*samdb_server_site_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1533 struct ldb_dn
*server_dn
;
1534 struct ldb_dn
*servers_dn
;
1535 struct ldb_dn
*server_site_dn
;
1537 /* TODO: there must be a saner way to do this!! */
1538 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1539 if (!server_dn
) return NULL
;
1541 servers_dn
= ldb_dn_get_parent(mem_ctx
, server_dn
);
1542 talloc_free(server_dn
);
1543 if (!servers_dn
) return NULL
;
1545 server_site_dn
= ldb_dn_get_parent(mem_ctx
, servers_dn
);
1546 talloc_free(servers_dn
);
1548 return server_site_dn
;
1552 find a 'reference' DN that points at another object
1553 (eg. serverReference, rIDManagerReference etc)
1555 int samdb_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*base
,
1556 const char *attribute
, struct ldb_dn
**dn
)
1558 const char *attrs
[2];
1559 struct ldb_result
*res
;
1562 attrs
[0] = attribute
;
1565 ret
= ldb_search(ldb
, mem_ctx
, &res
, base
, LDB_SCOPE_BASE
, attrs
, NULL
);
1566 if (ret
!= LDB_SUCCESS
) {
1569 if (res
->count
!= 1) {
1571 return LDB_ERR_NO_SUCH_OBJECT
;
1574 *dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, res
->msgs
[0], attribute
);
1577 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
1585 find our machine account via the serverReference attribute in the
1588 int samdb_server_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1590 struct ldb_dn
*server_dn
;
1593 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1594 if (server_dn
== NULL
) {
1595 return LDB_ERR_NO_SUCH_OBJECT
;
1598 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_dn
, "serverReference", dn
);
1599 talloc_free(server_dn
);
1605 find the RID Manager$ DN via the rIDManagerReference attribute in the
1608 int samdb_rid_manager_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1610 return samdb_reference_dn(ldb
, mem_ctx
, ldb_get_default_basedn(ldb
),
1611 "rIDManagerReference", dn
);
1615 find the RID Set DN via the rIDSetReferences attribute in our
1618 int samdb_rid_set_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1620 struct ldb_dn
*server_ref_dn
;
1623 ret
= samdb_server_reference_dn(ldb
, mem_ctx
, &server_ref_dn
);
1624 if (ret
!= LDB_SUCCESS
) {
1627 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_ref_dn
, "rIDSetReferences", dn
);
1628 talloc_free(server_ref_dn
);
1632 const char *samdb_server_site_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1634 const struct ldb_val
*val
= ldb_dn_get_rdn_val(samdb_server_site_dn(ldb
,
1641 return (const char *) val
->data
;
1645 * Finds the client site by using the client's IP address.
1646 * The "subnet_name" returns the name of the subnet if parameter != NULL
1648 const char *samdb_client_site_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1649 const char *ip_address
, char **subnet_name
)
1651 const char *attrs
[] = { "cn", "siteObject", NULL
};
1652 struct ldb_dn
*sites_container_dn
, *subnets_dn
, *sites_dn
;
1653 struct ldb_result
*res
;
1654 const struct ldb_val
*val
;
1655 const char *site_name
= NULL
, *l_subnet_name
= NULL
;
1656 const char *allow_list
[2] = { NULL
, NULL
};
1657 unsigned int i
, count
;
1661 * if we don't have a client ip e.g. ncalrpc
1662 * the server site is the client site
1664 if (ip_address
== NULL
) {
1665 return samdb_server_site_name(ldb
, mem_ctx
);
1668 sites_container_dn
= samdb_sites_dn(ldb
, mem_ctx
);
1669 if (sites_container_dn
== NULL
) {
1673 subnets_dn
= ldb_dn_copy(mem_ctx
, sites_container_dn
);
1674 if ( ! ldb_dn_add_child_fmt(subnets_dn
, "CN=Subnets")) {
1675 talloc_free(sites_container_dn
);
1676 talloc_free(subnets_dn
);
1680 ret
= ldb_search(ldb
, mem_ctx
, &res
, subnets_dn
, LDB_SCOPE_ONELEVEL
,
1682 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1684 } else if (ret
!= LDB_SUCCESS
) {
1685 talloc_free(sites_container_dn
);
1686 talloc_free(subnets_dn
);
1692 for (i
= 0; i
< count
; i
++) {
1693 l_subnet_name
= ldb_msg_find_attr_as_string(res
->msgs
[i
], "cn",
1696 allow_list
[0] = l_subnet_name
;
1698 if (allow_access(mem_ctx
, NULL
, allow_list
, "", ip_address
)) {
1699 sites_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
,
1702 if (sites_dn
== NULL
) {
1703 /* No reference, maybe another subnet matches */
1707 /* "val" cannot be NULL here since "sites_dn" != NULL */
1708 val
= ldb_dn_get_rdn_val(sites_dn
);
1709 site_name
= talloc_strdup(mem_ctx
,
1710 (const char *) val
->data
);
1712 talloc_free(sites_dn
);
1718 if (site_name
== NULL
) {
1719 /* This is the Windows Server fallback rule: when no subnet
1720 * exists and we have only one site available then use it (it
1721 * is for sure the same as our server site). If more sites do
1722 * exist then we don't know which one to use and set the site
1724 cnt
= samdb_search_count(ldb
, sites_container_dn
,
1725 "(objectClass=site)");
1727 site_name
= samdb_server_site_name(ldb
, mem_ctx
);
1729 site_name
= talloc_strdup(mem_ctx
, "");
1731 l_subnet_name
= NULL
;
1734 if (subnet_name
!= NULL
) {
1735 *subnet_name
= talloc_strdup(mem_ctx
, l_subnet_name
);
1738 talloc_free(sites_container_dn
);
1739 talloc_free(subnets_dn
);
1746 work out if we are the PDC for the domain of the current open ldb
1748 bool samdb_is_pdc(struct ldb_context
*ldb
)
1750 const char *dom_attrs
[] = { "fSMORoleOwner", NULL
};
1752 struct ldb_result
*dom_res
;
1753 TALLOC_CTX
*tmp_ctx
;
1757 tmp_ctx
= talloc_new(ldb
);
1758 if (tmp_ctx
== NULL
) {
1759 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1763 ret
= ldb_search(ldb
, tmp_ctx
, &dom_res
, ldb_get_default_basedn(ldb
), LDB_SCOPE_BASE
, dom_attrs
, NULL
);
1765 DEBUG(1,("Searching for fSMORoleOwner in %s failed: %s\n",
1766 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)),
1767 ldb_errstring(ldb
)));
1770 if (dom_res
->count
!= 1) {
1774 pdc
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, dom_res
->msgs
[0], "fSMORoleOwner");
1776 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb
), pdc
) == 0) {
1782 talloc_free(tmp_ctx
);
1787 DEBUG(1,("Failed to find if we are the PDC for this ldb\n"));
1788 talloc_free(tmp_ctx
);
1793 work out if we are a Global Catalog server for the domain of the current open ldb
1795 bool samdb_is_gc(struct ldb_context
*ldb
)
1797 const char *attrs
[] = { "options", NULL
};
1799 struct ldb_result
*res
;
1800 TALLOC_CTX
*tmp_ctx
;
1802 tmp_ctx
= talloc_new(ldb
);
1803 if (tmp_ctx
== NULL
) {
1804 DEBUG(1, ("talloc_new failed in samdb_is_pdc"));
1808 /* Query cn=ntds settings,.... */
1809 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
1811 talloc_free(tmp_ctx
);
1814 if (res
->count
!= 1) {
1815 talloc_free(tmp_ctx
);
1819 options
= ldb_msg_find_attr_as_int(res
->msgs
[0], "options", 0);
1820 talloc_free(tmp_ctx
);
1822 /* if options attribute has the 0x00000001 flag set, then enable the global catlog */
1823 if (options
& 0x000000001) {
1829 /* Find a domain object in the parents of a particular DN. */
1830 int samdb_search_for_parent_domain(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
1831 struct ldb_dn
**parent_dn
, const char **errstring
)
1833 TALLOC_CTX
*local_ctx
;
1834 struct ldb_dn
*sdn
= dn
;
1835 struct ldb_result
*res
= NULL
;
1837 const char *attrs
[] = { NULL
};
1839 local_ctx
= talloc_new(mem_ctx
);
1840 if (local_ctx
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
1842 while ((sdn
= ldb_dn_get_parent(local_ctx
, sdn
))) {
1843 ret
= ldb_search(ldb
, local_ctx
, &res
, sdn
, LDB_SCOPE_BASE
, attrs
,
1844 "(|(objectClass=domain)(objectClass=builtinDomain))");
1845 if (ret
== LDB_SUCCESS
) {
1846 if (res
->count
== 1) {
1854 if (ret
!= LDB_SUCCESS
) {
1855 *errstring
= talloc_asprintf(mem_ctx
, "Error searching for parent domain of %s, failed searching for %s: %s",
1856 ldb_dn_get_linearized(dn
),
1857 ldb_dn_get_linearized(sdn
),
1858 ldb_errstring(ldb
));
1859 talloc_free(local_ctx
);
1862 if (res
->count
!= 1) {
1863 *errstring
= talloc_asprintf(mem_ctx
, "Invalid dn (%s), not child of a domain object",
1864 ldb_dn_get_linearized(dn
));
1865 DEBUG(0,(__location__
": %s\n", *errstring
));
1866 talloc_free(local_ctx
);
1867 return LDB_ERR_CONSTRAINT_VIOLATION
;
1870 *parent_dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
1871 talloc_free(local_ctx
);
1877 * Performs checks on a user password (plaintext UNIX format - attribute
1878 * "password"). The remaining parameters have to be extracted from the domain
1881 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
1883 enum samr_ValidationStatus
samdb_check_password(const DATA_BLOB
*password
,
1884 const uint32_t pwdProperties
,
1885 const uint32_t minPwdLength
)
1887 /* checks if the "minPwdLength" property is satisfied */
1888 if (minPwdLength
> password
->length
)
1889 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT
;
1891 /* checks the password complexity */
1892 if (((pwdProperties
& DOMAIN_PASSWORD_COMPLEX
) != 0)
1893 && (password
->data
!= NULL
)
1894 && (!check_password_quality((const char *) password
->data
)))
1895 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH
;
1897 return SAMR_VALIDATION_STATUS_SUCCESS
;
1901 * Callback for "samdb_set_password" password change
1903 int samdb_set_password_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1908 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1911 if (ares
->error
!= LDB_SUCCESS
) {
1913 req
->context
= talloc_steal(req
,
1914 ldb_reply_get_control(ares
, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
));
1916 return ldb_request_done(req
, ret
);
1919 if (ares
->type
!= LDB_REPLY_DONE
) {
1921 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1924 req
->context
= talloc_steal(req
,
1925 ldb_reply_get_control(ares
, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
));
1927 return ldb_request_done(req
, LDB_SUCCESS
);
1931 * Sets the user password using plaintext UTF16 (attribute "new_password") or
1932 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
1933 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
1934 * gives some more informations if the changed failed.
1936 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
1937 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
1938 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
1940 NTSTATUS
samdb_set_password(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1941 struct ldb_dn
*user_dn
, struct ldb_dn
*domain_dn
,
1942 const DATA_BLOB
*new_password
,
1943 struct samr_Password
*lmNewHash
,
1944 struct samr_Password
*ntNewHash
,
1946 enum samPwdChangeReason
*reject_reason
,
1947 struct samr_DomInfo1
**_dominfo
)
1949 struct ldb_message
*msg
;
1950 struct ldb_message_element
*el
;
1951 struct ldb_request
*req
;
1952 struct dsdb_control_password_change_status
*pwd_stat
= NULL
;
1956 #define CHECK_RET(x) \
1957 if (x != LDB_SUCCESS) { \
1959 return NT_STATUS_NO_MEMORY; \
1962 msg
= ldb_msg_new(mem_ctx
);
1964 return NT_STATUS_NO_MEMORY
;
1967 if ((new_password
!= NULL
)
1968 && ((lmNewHash
== NULL
) && (ntNewHash
== NULL
))) {
1969 /* we have the password as plaintext UTF16 */
1970 CHECK_RET(samdb_msg_add_value(ldb
, mem_ctx
, msg
,
1971 "clearTextPassword", new_password
));
1972 el
= ldb_msg_find_element(msg
, "clearTextPassword");
1973 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1974 } else if ((new_password
== NULL
)
1975 && ((lmNewHash
!= NULL
) || (ntNewHash
!= NULL
))) {
1976 /* we have a password as LM and/or NT hash */
1977 if (lmNewHash
!= NULL
) {
1978 CHECK_RET(samdb_msg_add_hash(ldb
, mem_ctx
, msg
,
1979 "dBCSPwd", lmNewHash
));
1980 el
= ldb_msg_find_element(msg
, "dBCSPwd");
1981 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1983 if (ntNewHash
!= NULL
) {
1984 CHECK_RET(samdb_msg_add_hash(ldb
, mem_ctx
, msg
,
1985 "unicodePwd", ntNewHash
));
1986 el
= ldb_msg_find_element(msg
, "unicodePwd");
1987 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1990 /* the password wasn't specified correctly */
1992 return NT_STATUS_INVALID_PARAMETER
;
1995 /* build modify request */
1996 ret
= ldb_build_mod_req(&req
, ldb
, mem_ctx
, msg
, NULL
, NULL
,
1997 samdb_set_password_callback
, NULL
);
1998 if (ret
!= LDB_SUCCESS
) {
2000 return NT_STATUS_NO_MEMORY
;
2004 /* a user password change and we've checked already the old
2005 * password somewhere else (callers responsability) */
2006 ret
= ldb_request_add_control(req
,
2007 DSDB_CONTROL_PASSWORD_CHANGE_OLD_PW_CHECKED_OID
,
2009 if (ret
!= LDB_SUCCESS
) {
2012 return NT_STATUS_NO_MEMORY
;
2015 ret
= ldb_request_add_control(req
,
2016 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID
,
2018 if (ret
!= LDB_SUCCESS
) {
2021 return NT_STATUS_NO_MEMORY
;
2023 ret
= ldb_request_add_control(req
,
2024 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
,
2026 if (ret
!= LDB_SUCCESS
) {
2029 return NT_STATUS_NO_MEMORY
;
2032 ret
= dsdb_autotransaction_request(ldb
, req
);
2034 if (req
->context
!= NULL
) {
2035 pwd_stat
= talloc_steal(mem_ctx
,
2036 ((struct ldb_control
*)req
->context
)->data
);
2042 /* Sets the domain info (if requested) */
2043 if (_dominfo
!= NULL
) {
2044 struct samr_DomInfo1
*dominfo
;
2046 dominfo
= talloc_zero(mem_ctx
, struct samr_DomInfo1
);
2047 if (dominfo
== NULL
) {
2048 return NT_STATUS_NO_MEMORY
;
2051 if (pwd_stat
!= NULL
) {
2052 dominfo
->min_password_length
= pwd_stat
->domain_data
.minPwdLength
;
2053 dominfo
->password_properties
= pwd_stat
->domain_data
.pwdProperties
;
2054 dominfo
->password_history_length
= pwd_stat
->domain_data
.pwdHistoryLength
;
2055 dominfo
->max_password_age
= pwd_stat
->domain_data
.maxPwdAge
;
2056 dominfo
->min_password_age
= pwd_stat
->domain_data
.minPwdAge
;
2059 *_dominfo
= dominfo
;
2062 if (reject_reason
!= NULL
) {
2063 if (pwd_stat
!= NULL
) {
2064 *reject_reason
= pwd_stat
->reject_reason
;
2066 *reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
2070 if (pwd_stat
!= NULL
) {
2071 talloc_free(pwd_stat
);
2074 /* TODO: Error results taken from "password_hash" module. Are they
2076 if (ret
== LDB_ERR_UNWILLING_TO_PERFORM
) {
2077 status
= NT_STATUS_WRONG_PASSWORD
;
2078 } else if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
2079 status
= NT_STATUS_PASSWORD_RESTRICTION
;
2080 } else if (ret
!= LDB_SUCCESS
) {
2081 status
= NT_STATUS_UNSUCCESSFUL
;
2083 status
= NT_STATUS_OK
;
2090 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2091 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2092 * as parameter if it's a user change or not ("userChange"). The "rejectReason"
2093 * gives some more informations if the changed failed.
2095 * This wrapper function for "samdb_set_password" takes a SID as input rather
2098 * This call encapsulates a new LDB transaction for changing the password;
2099 * therefore the user hasn't to start a new one.
2101 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2102 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2103 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2105 NTSTATUS
samdb_set_password_sid(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2106 const struct dom_sid
*user_sid
,
2107 const DATA_BLOB
*new_password
,
2108 struct samr_Password
*lmNewHash
,
2109 struct samr_Password
*ntNewHash
,
2111 enum samPwdChangeReason
*reject_reason
,
2112 struct samr_DomInfo1
**_dominfo
)
2115 struct ldb_dn
*user_dn
;
2118 ret
= ldb_transaction_start(ldb
);
2119 if (ret
!= LDB_SUCCESS
) {
2120 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb
)));
2121 return NT_STATUS_TRANSACTION_ABORTED
;
2124 user_dn
= samdb_search_dn(ldb
, mem_ctx
, NULL
,
2125 "(&(objectSid=%s)(objectClass=user))",
2126 ldap_encode_ndr_dom_sid(mem_ctx
, user_sid
));
2128 ldb_transaction_cancel(ldb
);
2129 DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
2130 dom_sid_string(mem_ctx
, user_sid
)));
2131 return NT_STATUS_NO_SUCH_USER
;
2134 nt_status
= samdb_set_password(ldb
, mem_ctx
,
2137 lmNewHash
, ntNewHash
,
2139 reject_reason
, _dominfo
);
2140 if (!NT_STATUS_IS_OK(nt_status
)) {
2141 ldb_transaction_cancel(ldb
);
2142 talloc_free(user_dn
);
2146 ret
= ldb_transaction_commit(ldb
);
2147 if (ret
!= LDB_SUCCESS
) {
2148 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2149 ldb_dn_get_linearized(user_dn
),
2150 ldb_errstring(ldb
)));
2151 talloc_free(user_dn
);
2152 return NT_STATUS_TRANSACTION_ABORTED
;
2155 talloc_free(user_dn
);
2156 return NT_STATUS_OK
;
2160 NTSTATUS
samdb_create_foreign_security_principal(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
2161 struct dom_sid
*sid
, struct ldb_dn
**ret_dn
)
2163 struct ldb_message
*msg
;
2164 struct ldb_dn
*basedn
;
2168 sidstr
= dom_sid_string(mem_ctx
, sid
);
2169 NT_STATUS_HAVE_NO_MEMORY(sidstr
);
2171 /* We might have to create a ForeignSecurityPrincipal, even if this user
2172 * is in our own domain */
2174 msg
= ldb_msg_new(sidstr
);
2176 talloc_free(sidstr
);
2177 return NT_STATUS_NO_MEMORY
;
2180 ret
= dsdb_wellknown_dn(sam_ctx
, sidstr
,
2181 ldb_get_default_basedn(sam_ctx
),
2182 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER
,
2184 if (ret
!= LDB_SUCCESS
) {
2185 DEBUG(0, ("Failed to find DN for "
2186 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx
)));
2187 talloc_free(sidstr
);
2188 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2191 /* add core elements to the ldb_message for the alias */
2193 if ( ! ldb_dn_add_child_fmt(msg
->dn
, "CN=%s", sidstr
)) {
2194 talloc_free(sidstr
);
2195 return NT_STATUS_NO_MEMORY
;
2198 samdb_msg_add_string(sam_ctx
, msg
, msg
,
2200 "foreignSecurityPrincipal");
2202 /* create the alias */
2203 ret
= ldb_add(sam_ctx
, msg
);
2204 if (ret
!= LDB_SUCCESS
) {
2205 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2207 ldb_dn_get_linearized(msg
->dn
),
2208 ldb_errstring(sam_ctx
)));
2209 talloc_free(sidstr
);
2210 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2213 *ret_dn
= talloc_steal(mem_ctx
, msg
->dn
);
2214 talloc_free(sidstr
);
2216 return NT_STATUS_OK
;
2221 Find the DN of a domain, assuming it to be a dotted.dns name
2224 struct ldb_dn
*samdb_dns_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char *dns_domain
)
2227 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2228 const char *binary_encoded
;
2229 const char **split_realm
;
2236 split_realm
= (const char **)str_list_make(tmp_ctx
, dns_domain
, ".");
2238 talloc_free(tmp_ctx
);
2241 dn
= ldb_dn_new(mem_ctx
, ldb
, NULL
);
2242 for (i
=0; split_realm
[i
]; i
++) {
2243 binary_encoded
= ldb_binary_encode_string(tmp_ctx
, split_realm
[i
]);
2244 if (!ldb_dn_add_base_fmt(dn
, "dc=%s", binary_encoded
)) {
2245 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2246 binary_encoded
, ldb_dn_get_linearized(dn
)));
2247 talloc_free(tmp_ctx
);
2251 if (!ldb_dn_validate(dn
)) {
2252 DEBUG(2, ("Failed to validated DN %s\n",
2253 ldb_dn_get_linearized(dn
)));
2254 talloc_free(tmp_ctx
);
2257 talloc_free(tmp_ctx
);
2262 Find the DN of a domain, be it the netbios or DNS name
2264 struct ldb_dn
*samdb_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2265 const char *domain_name
)
2267 const char * const domain_ref_attrs
[] = {
2270 const char * const domain_ref2_attrs
[] = {
2273 struct ldb_result
*res_domain_ref
;
2274 char *escaped_domain
= ldb_binary_encode_string(mem_ctx
, domain_name
);
2275 /* find the domain's DN */
2276 int ret_domain
= ldb_search(ldb
, mem_ctx
,
2278 samdb_partitions_dn(ldb
, mem_ctx
),
2281 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2283 if (ret_domain
!= 0) {
2287 if (res_domain_ref
->count
== 0) {
2288 ret_domain
= ldb_search(ldb
, mem_ctx
,
2290 samdb_dns_domain_to_dn(ldb
, mem_ctx
, domain_name
),
2293 "(objectclass=domain)");
2294 if (ret_domain
!= 0) {
2298 if (res_domain_ref
->count
== 1) {
2299 return res_domain_ref
->msgs
[0]->dn
;
2304 if (res_domain_ref
->count
> 1) {
2305 DEBUG(0,("Found %d records matching domain [%s]\n",
2306 ret_domain
, domain_name
));
2310 return samdb_result_dn(ldb
, mem_ctx
, res_domain_ref
->msgs
[0], "nCName", NULL
);
2316 use a GUID to find a DN
2318 int dsdb_find_dn_by_guid(struct ldb_context
*ldb
,
2319 TALLOC_CTX
*mem_ctx
,
2320 const struct GUID
*guid
, struct ldb_dn
**dn
)
2323 struct ldb_result
*res
;
2324 const char *attrs
[] = { NULL
};
2325 char *guid_str
= GUID_string(mem_ctx
, guid
);
2328 return LDB_ERR_OPERATIONS_ERROR
;
2331 ret
= dsdb_search(ldb
, mem_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, attrs
,
2332 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
|
2333 DSDB_SEARCH_SHOW_EXTENDED_DN
|
2334 DSDB_SEARCH_ONE_ONLY
,
2335 "objectGUID=%s", guid_str
);
2336 talloc_free(guid_str
);
2337 if (ret
!= LDB_SUCCESS
) {
2341 *dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
2348 use a DN to find a GUID with a given attribute name
2350 int dsdb_find_guid_attr_by_dn(struct ldb_context
*ldb
,
2351 struct ldb_dn
*dn
, const char *attribute
,
2355 struct ldb_result
*res
;
2356 const char *attrs
[2];
2357 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2359 attrs
[0] = attribute
;
2362 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
, DSDB_SEARCH_SHOW_DELETED
);
2363 if (ret
!= LDB_SUCCESS
) {
2364 talloc_free(tmp_ctx
);
2367 if (res
->count
< 1) {
2368 talloc_free(tmp_ctx
);
2369 return LDB_ERR_NO_SUCH_OBJECT
;
2371 *guid
= samdb_result_guid(res
->msgs
[0], attribute
);
2372 talloc_free(tmp_ctx
);
2377 use a DN to find a GUID
2379 int dsdb_find_guid_by_dn(struct ldb_context
*ldb
,
2380 struct ldb_dn
*dn
, struct GUID
*guid
)
2382 return dsdb_find_guid_attr_by_dn(ldb
, dn
, "objectGUID", guid
);
2388 adds the given GUID to the given ldb_message. This value is added
2389 for the given attr_name (may be either "objectGUID" or "parentGUID").
2391 int dsdb_msg_add_guid(struct ldb_message
*msg
,
2393 const char *attr_name
)
2398 TALLOC_CTX
*tmp_ctx
= talloc_init("dsdb_msg_add_guid");
2400 status
= GUID_to_ndr_blob(guid
, tmp_ctx
, &v
);
2401 if (!NT_STATUS_IS_OK(status
)) {
2402 ret
= LDB_ERR_OPERATIONS_ERROR
;
2406 ret
= ldb_msg_add_steal_value(msg
, attr_name
, &v
);
2407 if (ret
!= LDB_SUCCESS
) {
2408 DEBUG(4,(__location__
": Failed to add %s to the message\n",
2416 talloc_free(tmp_ctx
);
2423 use a DN to find a SID
2425 int dsdb_find_sid_by_dn(struct ldb_context
*ldb
,
2426 struct ldb_dn
*dn
, struct dom_sid
*sid
)
2429 struct ldb_result
*res
;
2430 const char *attrs
[] = { "objectSID", NULL
};
2431 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2436 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
, DSDB_SEARCH_SHOW_DELETED
);
2437 if (ret
!= LDB_SUCCESS
) {
2438 talloc_free(tmp_ctx
);
2441 if (res
->count
< 1) {
2442 talloc_free(tmp_ctx
);
2443 return LDB_ERR_NO_SUCH_OBJECT
;
2445 s
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSID");
2447 talloc_free(tmp_ctx
);
2448 return LDB_ERR_NO_SUCH_OBJECT
;
2451 talloc_free(tmp_ctx
);
2457 load a repsFromTo blob list for a given partition GUID
2458 attr must be "repsFrom" or "repsTo"
2460 WERROR
dsdb_loadreps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
2461 const char *attr
, struct repsFromToBlob
**r
, uint32_t *count
)
2463 const char *attrs
[] = { attr
, NULL
};
2464 struct ldb_result
*res
= NULL
;
2465 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2467 struct ldb_message_element
*el
;
2472 if (ldb_search(sam_ctx
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, NULL
) != LDB_SUCCESS
||
2474 DEBUG(0,("dsdb_loadreps: failed to read partition object\n"));
2475 talloc_free(tmp_ctx
);
2476 return WERR_DS_DRA_INTERNAL_ERROR
;
2479 el
= ldb_msg_find_element(res
->msgs
[0], attr
);
2481 /* it's OK to be empty */
2482 talloc_free(tmp_ctx
);
2486 *count
= el
->num_values
;
2487 *r
= talloc_array(mem_ctx
, struct repsFromToBlob
, *count
);
2489 talloc_free(tmp_ctx
);
2490 return WERR_DS_DRA_INTERNAL_ERROR
;
2493 for (i
=0; i
<(*count
); i
++) {
2494 enum ndr_err_code ndr_err
;
2495 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
],
2498 (ndr_pull_flags_fn_t
)ndr_pull_repsFromToBlob
);
2499 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2500 talloc_free(tmp_ctx
);
2501 return WERR_DS_DRA_INTERNAL_ERROR
;
2505 talloc_free(tmp_ctx
);
2511 save the repsFromTo blob list for a given partition GUID
2512 attr must be "repsFrom" or "repsTo"
2514 WERROR
dsdb_savereps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
2515 const char *attr
, struct repsFromToBlob
*r
, uint32_t count
)
2517 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2518 struct ldb_message
*msg
;
2519 struct ldb_message_element
*el
;
2522 msg
= ldb_msg_new(tmp_ctx
);
2524 if (ldb_msg_add_empty(msg
, attr
, LDB_FLAG_MOD_REPLACE
, &el
) != LDB_SUCCESS
) {
2528 el
->values
= talloc_array(msg
, struct ldb_val
, count
);
2533 for (i
=0; i
<count
; i
++) {
2535 enum ndr_err_code ndr_err
;
2537 ndr_err
= ndr_push_struct_blob(&v
, tmp_ctx
,
2539 (ndr_push_flags_fn_t
)ndr_push_repsFromToBlob
);
2540 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2548 if (ldb_modify(sam_ctx
, msg
) != LDB_SUCCESS
) {
2549 DEBUG(0,("Failed to store %s - %s\n", attr
, ldb_errstring(sam_ctx
)));
2553 talloc_free(tmp_ctx
);
2558 talloc_free(tmp_ctx
);
2559 return WERR_DS_DRA_INTERNAL_ERROR
;
2564 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
2565 object for a partition
2567 int dsdb_load_partition_usn(struct ldb_context
*ldb
, struct ldb_dn
*dn
,
2568 uint64_t *uSN
, uint64_t *urgent_uSN
)
2570 struct ldb_request
*req
;
2572 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2573 struct dsdb_control_current_partition
*p_ctrl
;
2574 struct ldb_result
*res
;
2576 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
2578 talloc_free(tmp_ctx
);
2579 return LDB_ERR_OPERATIONS_ERROR
;
2582 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
2583 ldb_dn_new(tmp_ctx
, ldb
, "@REPLCHANGED"),
2587 res
, ldb_search_default_callback
,
2589 if (ret
!= LDB_SUCCESS
) {
2590 talloc_free(tmp_ctx
);
2594 p_ctrl
= talloc(req
, struct dsdb_control_current_partition
);
2595 if (p_ctrl
== NULL
) {
2597 return LDB_ERR_OPERATIONS_ERROR
;
2599 p_ctrl
->version
= DSDB_CONTROL_CURRENT_PARTITION_VERSION
;
2603 ret
= ldb_request_add_control(req
,
2604 DSDB_CONTROL_CURRENT_PARTITION_OID
,
2606 if (ret
!= LDB_SUCCESS
) {
2607 talloc_free(tmp_ctx
);
2611 /* Run the new request */
2612 ret
= ldb_request(ldb
, req
);
2614 if (ret
== LDB_SUCCESS
) {
2615 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
2618 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
2619 /* it hasn't been created yet, which means
2620 an implicit value of zero */
2622 talloc_free(tmp_ctx
);
2626 if (ret
!= LDB_SUCCESS
) {
2627 talloc_free(tmp_ctx
);
2631 if (res
->count
< 1) {
2637 *uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNHighest", 0);
2639 *urgent_uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNUrgent", 0);
2643 talloc_free(tmp_ctx
);
2648 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2
*c1
,
2649 const struct drsuapi_DsReplicaCursor2
*c2
)
2651 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
2654 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor
*c1
,
2655 const struct drsuapi_DsReplicaCursor
*c2
)
2657 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
2662 see if a computer identified by its invocationId is a RODC
2664 int samdb_is_rodc(struct ldb_context
*sam_ctx
, const struct GUID
*objectGUID
, bool *is_rodc
)
2666 /* 1) find the DN for this servers NTDSDSA object
2667 2) search for the msDS-isRODC attribute
2668 3) if not present then not a RODC
2669 4) if present and TRUE then is a RODC
2671 struct ldb_dn
*config_dn
;
2672 const char *attrs
[] = { "msDS-isRODC", NULL
};
2674 struct ldb_result
*res
;
2675 TALLOC_CTX
*tmp_ctx
= talloc_new(sam_ctx
);
2677 config_dn
= ldb_get_config_basedn(sam_ctx
);
2679 talloc_free(tmp_ctx
);
2680 return LDB_ERR_OPERATIONS_ERROR
;
2683 ret
= dsdb_search(sam_ctx
, tmp_ctx
, &res
, config_dn
, LDB_SCOPE_SUBTREE
, attrs
,
2684 DSDB_SEARCH_ONE_ONLY
, "objectGUID=%s", GUID_string(tmp_ctx
, objectGUID
));
2686 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
2688 talloc_free(tmp_ctx
);
2692 if (ret
!= LDB_SUCCESS
) {
2693 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
2694 GUID_string(tmp_ctx
, objectGUID
)));
2696 talloc_free(tmp_ctx
);
2700 ret
= ldb_msg_find_attr_as_bool(res
->msgs
[0], "msDS-isRODC", 0);
2701 *is_rodc
= (ret
== 1);
2703 talloc_free(tmp_ctx
);
2709 see if we are a RODC
2711 int samdb_rodc(struct ldb_context
*sam_ctx
, bool *am_rodc
)
2713 const struct GUID
*objectGUID
;
2717 /* see if we have a cached copy */
2718 cached
= (bool *)ldb_get_opaque(sam_ctx
, "cache.am_rodc");
2724 objectGUID
= samdb_ntds_objectGUID(sam_ctx
);
2726 return LDB_ERR_OPERATIONS_ERROR
;
2729 ret
= samdb_is_rodc(sam_ctx
, objectGUID
, am_rodc
);
2730 if (ret
!= LDB_SUCCESS
) {
2734 cached
= talloc(sam_ctx
, bool);
2735 if (cached
== NULL
) {
2736 return LDB_ERR_OPERATIONS_ERROR
;
2740 ret
= ldb_set_opaque(sam_ctx
, "cache.am_rodc", cached
);
2741 if (ret
!= LDB_SUCCESS
) {
2742 talloc_free(cached
);
2743 return LDB_ERR_OPERATIONS_ERROR
;
2749 bool samdb_set_am_rodc(struct ldb_context
*ldb
, bool am_rodc
)
2751 TALLOC_CTX
*tmp_ctx
;
2754 tmp_ctx
= talloc_new(ldb
);
2755 if (tmp_ctx
== NULL
) {
2759 cached
= talloc(tmp_ctx
, bool);
2765 if (ldb_set_opaque(ldb
, "cache.am_rodc", cached
) != LDB_SUCCESS
) {
2769 talloc_steal(ldb
, cached
);
2770 talloc_free(tmp_ctx
);
2774 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
2775 talloc_free(tmp_ctx
);
2781 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
2783 flags are DS_NTDS_OPTION_*
2785 int samdb_ntds_options(struct ldb_context
*ldb
, uint32_t *options
)
2787 TALLOC_CTX
*tmp_ctx
;
2788 const char *attrs
[] = { "options", NULL
};
2790 struct ldb_result
*res
;
2792 tmp_ctx
= talloc_new(ldb
);
2793 if (tmp_ctx
== NULL
) {
2797 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
2802 if (res
->count
!= 1) {
2806 *options
= samdb_result_uint(res
->msgs
[0], "options", 0);
2808 talloc_free(tmp_ctx
);
2813 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
2814 talloc_free(tmp_ctx
);
2815 return LDB_ERR_NO_SUCH_OBJECT
;
2818 const char* samdb_ntds_object_category(TALLOC_CTX
*tmp_ctx
, struct ldb_context
*ldb
)
2820 const char *attrs
[] = { "objectCategory", NULL
};
2822 struct ldb_result
*res
;
2824 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
), LDB_SCOPE_BASE
, attrs
, NULL
);
2829 if (res
->count
!= 1) {
2833 return samdb_result_string(res
->msgs
[0], "objectCategory", NULL
);
2836 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
2841 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
2842 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
2844 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX
*mem_ctx
, const char *cn
)
2846 char **tokens
, *ret
;
2849 tokens
= str_list_make(mem_ctx
, cn
, " -_");
2853 /* "tolower()" and "toupper()" should also work properly on 0x00 */
2854 tokens
[0][0] = tolower(tokens
[0][0]);
2855 for (i
= 1; i
< str_list_length((const char **)tokens
); i
++)
2856 tokens
[i
][0] = toupper(tokens
[i
][0]);
2858 ret
= talloc_strdup(mem_ctx
, tokens
[0]);
2859 for (i
= 1; i
< str_list_length((const char **)tokens
); i
++)
2860 ret
= talloc_asprintf_append_buffer(ret
, "%s", tokens
[i
]);
2862 talloc_free(tokens
);
2868 return domain functional level
2869 returns DS_DOMAIN_FUNCTION_*
2871 int dsdb_functional_level(struct ldb_context
*ldb
)
2873 int *domainFunctionality
=
2874 talloc_get_type(ldb_get_opaque(ldb
, "domainFunctionality"), int);
2875 if (!domainFunctionality
) {
2876 DEBUG(0,(__location__
": WARNING: domainFunctionality not setup\n"));
2877 return DS_DOMAIN_FUNCTION_2000
;
2879 return *domainFunctionality
;
2883 set a GUID in an extended DN structure
2885 int dsdb_set_extended_dn_guid(struct ldb_dn
*dn
, const struct GUID
*guid
, const char *component_name
)
2891 status
= GUID_to_ndr_blob(guid
, dn
, &v
);
2892 if (!NT_STATUS_IS_OK(status
)) {
2893 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
2896 ret
= ldb_dn_set_extended_component(dn
, component_name
, &v
);
2902 return a GUID from a extended DN structure
2904 NTSTATUS
dsdb_get_extended_dn_guid(struct ldb_dn
*dn
, struct GUID
*guid
, const char *component_name
)
2906 const struct ldb_val
*v
;
2908 v
= ldb_dn_get_extended_component(dn
, component_name
);
2910 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2913 return GUID_from_ndr_blob(v
, guid
);
2917 return a uint64_t from a extended DN structure
2919 NTSTATUS
dsdb_get_extended_dn_uint64(struct ldb_dn
*dn
, uint64_t *val
, const char *component_name
)
2921 const struct ldb_val
*v
;
2924 v
= ldb_dn_get_extended_component(dn
, component_name
);
2926 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2928 s
= talloc_strndup(dn
, (const char *)v
->data
, v
->length
);
2929 NT_STATUS_HAVE_NO_MEMORY(s
);
2931 *val
= strtoull(s
, NULL
, 0);
2934 return NT_STATUS_OK
;
2938 return a NTTIME from a extended DN structure
2940 NTSTATUS
dsdb_get_extended_dn_nttime(struct ldb_dn
*dn
, NTTIME
*nttime
, const char *component_name
)
2942 return dsdb_get_extended_dn_uint64(dn
, nttime
, component_name
);
2946 return a uint32_t from a extended DN structure
2948 NTSTATUS
dsdb_get_extended_dn_uint32(struct ldb_dn
*dn
, uint32_t *val
, const char *component_name
)
2950 const struct ldb_val
*v
;
2953 v
= ldb_dn_get_extended_component(dn
, component_name
);
2955 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2958 s
= talloc_strndup(dn
, (const char *)v
->data
, v
->length
);
2959 NT_STATUS_HAVE_NO_MEMORY(s
);
2961 *val
= strtoul(s
, NULL
, 0);
2964 return NT_STATUS_OK
;
2968 return a dom_sid from a extended DN structure
2970 NTSTATUS
dsdb_get_extended_dn_sid(struct ldb_dn
*dn
, struct dom_sid
*sid
, const char *component_name
)
2972 const struct ldb_val
*sid_blob
;
2973 struct TALLOC_CTX
*tmp_ctx
;
2974 enum ndr_err_code ndr_err
;
2976 sid_blob
= ldb_dn_get_extended_component(dn
, "SID");
2978 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2981 tmp_ctx
= talloc_new(NULL
);
2983 ndr_err
= ndr_pull_struct_blob_all(sid_blob
, tmp_ctx
, sid
,
2984 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
2985 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2986 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
2987 talloc_free(tmp_ctx
);
2991 talloc_free(tmp_ctx
);
2992 return NT_STATUS_OK
;
2997 return RMD_FLAGS directly from a ldb_dn
2998 returns 0 if not found
3000 uint32_t dsdb_dn_rmd_flags(struct ldb_dn
*dn
)
3002 const struct ldb_val
*v
;
3004 v
= ldb_dn_get_extended_component(dn
, "RMD_FLAGS");
3005 if (!v
|| v
->length
> sizeof(buf
)-1) return 0;
3006 strncpy(buf
, (const char *)v
->data
, v
->length
);
3008 return strtoul(buf
, NULL
, 10);
3012 return RMD_FLAGS directly from a ldb_val for a DN
3013 returns 0 if RMD_FLAGS is not found
3015 uint32_t dsdb_dn_val_rmd_flags(struct ldb_val
*val
)
3021 if (val
->length
< 13) {
3024 p
= memmem(val
->data
, val
->length
-2, "<RMD_FLAGS=", 11);
3028 flags
= strtoul(p
+11, &end
, 10);
3029 if (!end
|| *end
!= '>') {
3030 /* it must end in a > */
3037 return true if a ldb_val containing a DN in storage form is deleted
3039 bool dsdb_dn_is_deleted_val(struct ldb_val
*val
)
3041 return (dsdb_dn_val_rmd_flags(val
) & DSDB_RMD_FLAG_DELETED
) != 0;
3045 return true if a ldb_val containing a DN in storage form is
3046 in the upgraded w2k3 linked attribute format
3048 bool dsdb_dn_is_upgraded_link_val(struct ldb_val
*val
)
3050 return memmem(val
->data
, val
->length
, "<RMD_ADDTIME=", 13) != NULL
;
3054 return a DN for a wellknown GUID
3056 int dsdb_wellknown_dn(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
,
3057 struct ldb_dn
*nc_root
, const char *wk_guid
,
3058 struct ldb_dn
**wkguid_dn
)
3060 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3061 const char *attrs
[] = { NULL
};
3064 struct ldb_result
*res
;
3066 /* construct the magic WKGUID DN */
3067 dn
= ldb_dn_new_fmt(tmp_ctx
, samdb
, "<WKGUID=%s,%s>",
3068 wk_guid
, ldb_dn_get_linearized(nc_root
));
3070 talloc_free(tmp_ctx
);
3071 return LDB_ERR_OPERATIONS_ERROR
;
3074 ret
= dsdb_search_dn(samdb
, tmp_ctx
, &res
, dn
, attrs
, DSDB_SEARCH_SHOW_DELETED
);
3075 if (ret
!= LDB_SUCCESS
) {
3076 talloc_free(tmp_ctx
);
3080 (*wkguid_dn
) = talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
3081 talloc_free(tmp_ctx
);
3086 static int dsdb_dn_compare_ptrs(struct ldb_dn
**dn1
, struct ldb_dn
**dn2
)
3088 return ldb_dn_compare(*dn1
, *dn2
);
3092 find a NC root given a DN within the NC
3094 int dsdb_find_nc_root(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
3095 struct ldb_dn
**nc_root
)
3097 const char *root_attrs
[] = { "namingContexts", NULL
};
3098 TALLOC_CTX
*tmp_ctx
;
3100 struct ldb_message_element
*el
;
3101 struct ldb_result
*root_res
;
3103 struct ldb_dn
**nc_dns
;
3105 tmp_ctx
= talloc_new(samdb
);
3106 if (tmp_ctx
== NULL
) {
3107 return LDB_ERR_OPERATIONS_ERROR
;
3110 ret
= ldb_search(samdb
, tmp_ctx
, &root_res
,
3111 ldb_dn_new(tmp_ctx
, samdb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
3113 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb
)));
3114 talloc_free(tmp_ctx
);
3118 el
= ldb_msg_find_element(root_res
->msgs
[0], "namingContexts");
3120 DEBUG(1,("Finding namingContexts element in root_res failed: %s\n",
3121 ldb_errstring(samdb
)));
3122 talloc_free(tmp_ctx
);
3123 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
3126 nc_dns
= talloc_array(tmp_ctx
, struct ldb_dn
*, el
->num_values
);
3128 talloc_free(tmp_ctx
);
3129 return LDB_ERR_OPERATIONS_ERROR
;
3132 for (i
=0; i
<el
->num_values
; i
++) {
3133 nc_dns
[i
] = ldb_dn_from_ldb_val(nc_dns
, samdb
, &el
->values
[i
]);
3134 if (nc_dns
[i
] == NULL
) {
3135 talloc_free(tmp_ctx
);
3136 return LDB_ERR_OPERATIONS_ERROR
;
3140 TYPESAFE_QSORT(nc_dns
, el
->num_values
, dsdb_dn_compare_ptrs
);
3142 for (i
=0; i
<el
->num_values
; i
++) {
3143 if (ldb_dn_compare_base(nc_dns
[i
], dn
) == 0) {
3144 (*nc_root
) = talloc_steal(mem_ctx
, nc_dns
[i
]);
3145 talloc_free(tmp_ctx
);
3150 talloc_free(tmp_ctx
);
3151 return LDB_ERR_NO_SUCH_OBJECT
;
3156 find the deleted objects DN for any object, by looking for the NC
3157 root, then looking up the wellknown GUID
3159 int dsdb_get_deleted_objects_dn(struct ldb_context
*ldb
,
3160 TALLOC_CTX
*mem_ctx
, struct ldb_dn
*obj_dn
,
3161 struct ldb_dn
**do_dn
)
3163 struct ldb_dn
*nc_root
;
3166 ret
= dsdb_find_nc_root(ldb
, mem_ctx
, obj_dn
, &nc_root
);
3167 if (ret
!= LDB_SUCCESS
) {
3171 ret
= dsdb_wellknown_dn(ldb
, mem_ctx
, nc_root
, DS_GUID_DELETED_OBJECTS_CONTAINER
, do_dn
);
3172 talloc_free(nc_root
);
3177 return the tombstoneLifetime, in days
3179 int dsdb_tombstone_lifetime(struct ldb_context
*ldb
, uint32_t *lifetime
)
3182 dn
= ldb_get_config_basedn(ldb
);
3184 return LDB_ERR_NO_SUCH_OBJECT
;
3186 dn
= ldb_dn_copy(ldb
, dn
);
3188 return LDB_ERR_OPERATIONS_ERROR
;
3190 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3191 be a wellknown GUID for this */
3192 if (!ldb_dn_add_child_fmt(dn
, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3194 return LDB_ERR_OPERATIONS_ERROR
;
3197 *lifetime
= samdb_search_uint(ldb
, dn
, 180, dn
, "tombstoneLifetime", "objectClass=nTDSService");
3203 compare a ldb_val to a string case insensitively
3205 int samdb_ldb_val_case_cmp(const char *s
, struct ldb_val
*v
)
3207 size_t len
= strlen(s
);
3209 if (len
> v
->length
) return 1;
3210 ret
= strncasecmp(s
, (const char *)v
->data
, v
->length
);
3211 if (ret
!= 0) return ret
;
3212 if (v
->length
> len
&& v
->data
[len
] != 0) {
3220 load the UDV for a partition in v2 format
3221 The list is returned sorted, and with our local cursor added
3223 int dsdb_load_udv_v2(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
3224 struct drsuapi_DsReplicaCursor2
**cursors
, uint32_t *count
)
3226 static const char *attrs
[] = { "replUpToDateVector", NULL
};
3227 struct ldb_result
*r
;
3228 const struct ldb_val
*ouv_value
;
3231 uint64_t highest_usn
;
3232 const struct GUID
*our_invocation_id
;
3233 struct timeval now
= timeval_current();
3235 ret
= ldb_search(samdb
, mem_ctx
, &r
, dn
, LDB_SCOPE_BASE
, attrs
, NULL
);
3236 if (ret
!= LDB_SUCCESS
) {
3240 ouv_value
= ldb_msg_find_ldb_val(r
->msgs
[0], "replUpToDateVector");
3242 enum ndr_err_code ndr_err
;
3243 struct replUpToDateVectorBlob ouv
;
3245 ndr_err
= ndr_pull_struct_blob(ouv_value
, r
, &ouv
,
3246 (ndr_pull_flags_fn_t
)ndr_pull_replUpToDateVectorBlob
);
3247 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3249 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
3251 if (ouv
.version
!= 2) {
3252 /* we always store as version 2, and
3253 * replUpToDateVector is not replicated
3255 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
3258 *count
= ouv
.ctr
.ctr2
.count
;
3259 *cursors
= talloc_steal(mem_ctx
, ouv
.ctr
.ctr2
.cursors
);
3267 our_invocation_id
= samdb_ntds_invocation_id(samdb
);
3268 if (!our_invocation_id
) {
3269 DEBUG(0,(__location__
": No invocationID on samdb - %s\n", ldb_errstring(samdb
)));
3270 talloc_free(*cursors
);
3271 return LDB_ERR_OPERATIONS_ERROR
;
3274 ret
= dsdb_load_partition_usn(samdb
, dn
, &highest_usn
, NULL
);
3275 if (ret
!= LDB_SUCCESS
) {
3276 /* nothing to add - this can happen after a vampire */
3277 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
3281 for (i
=0; i
<*count
; i
++) {
3282 if (GUID_equal(our_invocation_id
, &(*cursors
)[i
].source_dsa_invocation_id
)) {
3283 (*cursors
)[i
].highest_usn
= highest_usn
;
3284 (*cursors
)[i
].last_sync_success
= timeval_to_nttime(&now
);
3285 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
3290 (*cursors
) = talloc_realloc(mem_ctx
, *cursors
, struct drsuapi_DsReplicaCursor2
, (*count
)+1);
3292 return LDB_ERR_OPERATIONS_ERROR
;
3295 (*cursors
)[*count
].source_dsa_invocation_id
= *our_invocation_id
;
3296 (*cursors
)[*count
].highest_usn
= highest_usn
;
3297 (*cursors
)[*count
].last_sync_success
= timeval_to_nttime(&now
);
3300 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
3306 load the UDV for a partition in version 1 format
3307 The list is returned sorted, and with our local cursor added
3309 int dsdb_load_udv_v1(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
3310 struct drsuapi_DsReplicaCursor
**cursors
, uint32_t *count
)
3312 struct drsuapi_DsReplicaCursor2
*v2
;
3316 ret
= dsdb_load_udv_v2(samdb
, dn
, mem_ctx
, &v2
, count
);
3317 if (ret
!= LDB_SUCCESS
) {
3327 *cursors
= talloc_array(mem_ctx
, struct drsuapi_DsReplicaCursor
, *count
);
3328 if (*cursors
== NULL
) {
3330 return LDB_ERR_OPERATIONS_ERROR
;
3333 for (i
=0; i
<*count
; i
++) {
3334 (*cursors
)[i
].source_dsa_invocation_id
= v2
[i
].source_dsa_invocation_id
;
3335 (*cursors
)[i
].highest_usn
= v2
[i
].highest_usn
;
3342 add a set of controls to a ldb_request structure based on a set of
3343 flags. See util.h for a list of available flags
3345 int dsdb_request_add_controls(struct ldb_request
*req
, uint32_t dsdb_flags
)
3348 if (dsdb_flags
& DSDB_SEARCH_SEARCH_ALL_PARTITIONS
) {
3349 struct ldb_search_options_control
*options
;
3350 /* Using the phantom root control allows us to search all partitions */
3351 options
= talloc(req
, struct ldb_search_options_control
);
3352 if (options
== NULL
) {
3353 return LDB_ERR_OPERATIONS_ERROR
;
3355 options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
3357 ret
= ldb_request_add_control(req
,
3358 LDB_CONTROL_SEARCH_OPTIONS_OID
,
3360 if (ret
!= LDB_SUCCESS
) {
3365 if (dsdb_flags
& DSDB_SEARCH_SHOW_DELETED
) {
3366 ret
= ldb_request_add_control(req
, LDB_CONTROL_SHOW_DELETED_OID
, true, NULL
);
3367 if (ret
!= LDB_SUCCESS
) {
3372 if (dsdb_flags
& DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
) {
3373 ret
= ldb_request_add_control(req
, DSDB_CONTROL_DN_STORAGE_FORMAT_OID
, true, NULL
);
3374 if (ret
!= LDB_SUCCESS
) {
3379 if (dsdb_flags
& DSDB_SEARCH_SHOW_EXTENDED_DN
) {
3380 struct ldb_extended_dn_control
*extended_ctrl
= talloc(req
, struct ldb_extended_dn_control
);
3381 if (!extended_ctrl
) {
3382 return LDB_ERR_OPERATIONS_ERROR
;
3384 extended_ctrl
->type
= 1;
3386 ret
= ldb_request_add_control(req
, LDB_CONTROL_EXTENDED_DN_OID
, true, extended_ctrl
);
3387 if (ret
!= LDB_SUCCESS
) {
3392 if (dsdb_flags
& DSDB_SEARCH_REVEAL_INTERNALS
) {
3393 ret
= ldb_request_add_control(req
, LDB_CONTROL_REVEAL_INTERNALS
, false, NULL
);
3394 if (ret
!= LDB_SUCCESS
) {
3399 if (dsdb_flags
& DSDB_MODIFY_RELAX
) {
3400 ret
= ldb_request_add_control(req
, LDB_CONTROL_RELAX_OID
, false, NULL
);
3401 if (ret
!= LDB_SUCCESS
) {
3406 if (dsdb_flags
& DSDB_MODIFY_PERMISSIVE
) {
3407 ret
= ldb_request_add_control(req
, LDB_CONTROL_PERMISSIVE_MODIFY_OID
, false, NULL
);
3408 if (ret
!= LDB_SUCCESS
) {
3413 if (dsdb_flags
& DSDB_FLAG_AS_SYSTEM
) {
3414 ret
= ldb_request_add_control(req
, LDB_CONTROL_AS_SYSTEM_OID
, false, NULL
);
3415 if (ret
!= LDB_SUCCESS
) {
3424 a modify with a set of controls
3426 int dsdb_modify(struct ldb_context
*ldb
, const struct ldb_message
*message
,
3427 uint32_t dsdb_flags
)
3429 struct ldb_request
*req
;
3432 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
3436 ldb_op_default_callback
,
3439 if (ret
!= LDB_SUCCESS
) return ret
;
3441 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3442 if (ret
!= LDB_SUCCESS
) {
3447 ret
= dsdb_autotransaction_request(ldb
, req
);
3454 like dsdb_modify() but set all the element flags to
3455 LDB_FLAG_MOD_REPLACE
3457 int dsdb_replace(struct ldb_context
*ldb
, struct ldb_message
*msg
, uint32_t dsdb_flags
)
3461 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
3462 for (i
=0;i
<msg
->num_elements
;i
++) {
3463 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
3466 return dsdb_modify(ldb
, msg
, dsdb_flags
);
3471 search for attrs on one DN, allowing for dsdb_flags controls
3473 int dsdb_search_dn(struct ldb_context
*ldb
,
3474 TALLOC_CTX
*mem_ctx
,
3475 struct ldb_result
**_res
,
3476 struct ldb_dn
*basedn
,
3477 const char * const *attrs
,
3478 uint32_t dsdb_flags
)
3481 struct ldb_request
*req
;
3482 struct ldb_result
*res
;
3484 res
= talloc_zero(mem_ctx
, struct ldb_result
);
3486 return LDB_ERR_OPERATIONS_ERROR
;
3489 ret
= ldb_build_search_req(&req
, ldb
, res
,
3496 ldb_search_default_callback
,
3498 if (ret
!= LDB_SUCCESS
) {
3503 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3504 if (ret
!= LDB_SUCCESS
) {
3509 ret
= ldb_request(ldb
, req
);
3510 if (ret
== LDB_SUCCESS
) {
3511 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
3515 if (ret
!= LDB_SUCCESS
) {
3525 general search with dsdb_flags for controls
3527 int dsdb_search(struct ldb_context
*ldb
,
3528 TALLOC_CTX
*mem_ctx
,
3529 struct ldb_result
**_res
,
3530 struct ldb_dn
*basedn
,
3531 enum ldb_scope scope
,
3532 const char * const *attrs
,
3533 uint32_t dsdb_flags
,
3534 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
3537 struct ldb_request
*req
;
3538 struct ldb_result
*res
;
3540 char *expression
= NULL
;
3541 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3543 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
3545 talloc_free(tmp_ctx
);
3546 return LDB_ERR_OPERATIONS_ERROR
;
3550 va_start(ap
, exp_fmt
);
3551 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
3555 talloc_free(tmp_ctx
);
3556 return LDB_ERR_OPERATIONS_ERROR
;
3560 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
3567 ldb_search_default_callback
,
3569 if (ret
!= LDB_SUCCESS
) {
3570 talloc_free(tmp_ctx
);
3574 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
3575 if (ret
!= LDB_SUCCESS
) {
3576 talloc_free(tmp_ctx
);
3580 ret
= ldb_request(ldb
, req
);
3581 if (ret
== LDB_SUCCESS
) {
3582 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
3585 if (ret
!= LDB_SUCCESS
) {
3586 talloc_free(tmp_ctx
);
3590 if (dsdb_flags
& DSDB_SEARCH_ONE_ONLY
) {
3591 if (res
->count
== 0) {
3592 talloc_free(tmp_ctx
);
3593 return LDB_ERR_NO_SUCH_OBJECT
;
3595 if (res
->count
!= 1) {
3596 talloc_free(tmp_ctx
);
3597 return LDB_ERR_CONSTRAINT_VIOLATION
;
3601 *_res
= talloc_steal(mem_ctx
, res
);
3602 talloc_free(tmp_ctx
);
3609 general search with dsdb_flags for controls
3610 returns exactly 1 record or an error
3612 int dsdb_search_one(struct ldb_context
*ldb
,
3613 TALLOC_CTX
*mem_ctx
,
3614 struct ldb_message
**msg
,
3615 struct ldb_dn
*basedn
,
3616 enum ldb_scope scope
,
3617 const char * const *attrs
,
3618 uint32_t dsdb_flags
,
3619 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
3622 struct ldb_result
*res
;
3624 char *expression
= NULL
;
3625 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3627 dsdb_flags
|= DSDB_SEARCH_ONE_ONLY
;
3629 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
3631 talloc_free(tmp_ctx
);
3632 return LDB_ERR_OPERATIONS_ERROR
;
3636 va_start(ap
, exp_fmt
);
3637 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
3641 talloc_free(tmp_ctx
);
3642 return LDB_ERR_OPERATIONS_ERROR
;
3644 ret
= dsdb_search(ldb
, tmp_ctx
, &res
, basedn
, scope
, attrs
,
3645 dsdb_flags
, "%s", expression
);
3647 ret
= dsdb_search(ldb
, tmp_ctx
, &res
, basedn
, scope
, attrs
,
3651 if (ret
!= LDB_SUCCESS
) {
3652 talloc_free(tmp_ctx
);
3656 *msg
= talloc_steal(mem_ctx
, res
->msgs
[0]);
3657 talloc_free(tmp_ctx
);
3662 /* returns back the forest DNS name */
3663 const char *samdb_forest_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
3665 const char *forest_name
= ldb_dn_canonical_string(mem_ctx
,
3666 ldb_get_root_basedn(ldb
));
3669 if (forest_name
== NULL
) {
3673 p
= strchr(forest_name
, '/');
3682 validate that an DSA GUID belongs to the specified user sid.
3683 The user SID must be a domain controller account (either RODC or
3686 int dsdb_validate_dsa_guid(struct ldb_context
*ldb
,
3687 const struct GUID
*dsa_guid
,
3688 const struct dom_sid
*sid
)
3691 - find DN of record with the DSA GUID in the
3692 configuration partition (objectGUID)
3693 - remove "NTDS Settings" component from DN
3694 - do a base search on that DN for serverReference with
3696 - extract objectSID from resulting serverReference
3698 - check this sid matches the sid argument
3700 struct ldb_dn
*config_dn
;
3701 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
3702 struct ldb_message
*msg
;
3703 const char *attrs1
[] = { NULL
};
3704 const char *attrs2
[] = { "serverReference", NULL
};
3706 struct ldb_dn
*dn
, *account_dn
;
3707 struct dom_sid sid2
;
3710 config_dn
= ldb_get_config_basedn(ldb
);
3712 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, config_dn
, LDB_SCOPE_SUBTREE
,
3713 attrs1
, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx
, dsa_guid
));
3714 if (ret
!= LDB_SUCCESS
) {
3715 DEBUG(1,(__location__
": Failed to find DSA objectGUID %s for sid %s\n",
3716 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
3717 talloc_free(tmp_ctx
);
3718 return LDB_ERR_OPERATIONS_ERROR
;
3722 if (!ldb_dn_remove_child_components(dn
, 1)) {
3723 talloc_free(tmp_ctx
);
3724 return LDB_ERR_OPERATIONS_ERROR
;
3727 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, dn
, LDB_SCOPE_BASE
,
3728 attrs2
, DSDB_SEARCH_SHOW_EXTENDED_DN
,
3729 "(objectClass=server)");
3730 if (ret
!= LDB_SUCCESS
) {
3731 DEBUG(1,(__location__
": Failed to find server record for DSA with objectGUID %s, sid %s\n",
3732 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
3733 talloc_free(tmp_ctx
);
3734 return LDB_ERR_OPERATIONS_ERROR
;
3737 account_dn
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, msg
, "serverReference");
3738 if (account_dn
== NULL
) {
3739 DEBUG(1,(__location__
": Failed to find account_dn for DSA with objectGUID %s, sid %s\n",
3740 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
3741 talloc_free(tmp_ctx
);
3742 return LDB_ERR_OPERATIONS_ERROR
;
3745 status
= dsdb_get_extended_dn_sid(account_dn
, &sid2
, "SID");
3746 if (!NT_STATUS_IS_OK(status
)) {
3747 DEBUG(1,(__location__
": Failed to find SID for DSA with objectGUID %s, sid %s\n",
3748 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
3749 talloc_free(tmp_ctx
);
3750 return LDB_ERR_OPERATIONS_ERROR
;
3753 if (!dom_sid_equal(sid
, &sid2
)) {
3754 /* someone is trying to spoof another account */
3755 DEBUG(0,(__location__
": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
3756 GUID_string(tmp_ctx
, dsa_guid
),
3757 dom_sid_string(tmp_ctx
, sid
),
3758 dom_sid_string(tmp_ctx
, &sid2
)));
3759 talloc_free(tmp_ctx
);
3760 return LDB_ERR_OPERATIONS_ERROR
;
3763 talloc_free(tmp_ctx
);
3767 const char *rodc_fas_list
[] = {"ms-PKI-DPAPIMasterKeys",
3768 "ms-PKI-AccountCredentials",
3769 "ms-PKI-RoamingTimeStamp",
3770 "ms-FVE-KeyPackage",
3771 "ms-FVE-RecoveryGuid",
3772 "ms-FVE-RecoveryInformation",
3773 "ms-FVE-RecoveryPassword",
3774 "ms-FVE-VolumeGuid",
3775 "ms-TPM-OwnerInformation",
3778 check if the attribute belongs to the RODC filtered attribute set
3780 bool dsdb_attr_in_rodc_fas(uint32_t replica_flags
, const struct dsdb_attribute
*sa
)
3782 int rodc_filtered_flags
= SEARCH_FLAG_RODC_ATTRIBUTE
| SEARCH_FLAG_CONFIDENTIAL
;
3783 bool drs_write_replica
= ((replica_flags
& DRSUAPI_DRS_WRIT_REP
) == 0);
3785 if (drs_write_replica
&& (sa
->searchFlags
& rodc_filtered_flags
)) {
3788 if (drs_write_replica
&& is_attr_in_list(rodc_fas_list
, sa
->cn
)) {