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_module.h"
28 #include "ldb_errors.h"
29 #include "../lib/util/util_ldb.h"
30 #include "../lib/crypto/crypto.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "libcli/security/security.h"
33 #include "librpc/gen_ndr/ndr_security.h"
34 #include "librpc/gen_ndr/ndr_misc.h"
35 #include "../libds/common/flags.h"
36 #include "dsdb/common/proto.h"
37 #include "libcli/ldap/ldap_ndr.h"
38 #include "param/param.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "librpc/gen_ndr/ndr_drsblobs.h"
41 #include "system/locale.h"
42 #include "system/filesys.h"
43 #include "lib/util/tsort.h"
44 #include "dsdb/common/util.h"
45 #include "lib/socket/socket.h"
46 #include "librpc/gen_ndr/irpc.h"
47 #include "libds/common/flag_mapping.h"
48 #include "lib/util/access.h"
49 #include "lib/util/sys_rw_data.h"
50 #include "libcli/util/ntstatus.h"
51 #include "lib/util/smb_strtox.h"
57 * This included to allow us to handle DSDB_FLAG_REPLICATED_UPDATE in
58 * dsdb_request_add_controls()
60 #include "dsdb/samdb/ldb_modules/util.h"
62 /* default is 30 minutes: -1e7 * 30 * 60 */
63 #define DEFAULT_OBSERVATION_WINDOW -18000000000
66 search the sam for the specified attributes in a specific domain, filter on
67 objectSid being in domain_sid.
69 int samdb_search_domain(struct ldb_context
*sam_ldb
,
71 struct ldb_dn
*basedn
,
72 struct ldb_message
***res
,
73 const char * const *attrs
,
74 const struct dom_sid
*domain_sid
,
75 const char *format
, ...) _PRINTF_ATTRIBUTE(7,8)
81 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
,
82 res
, attrs
, format
, ap
);
88 struct dom_sid
*entry_sid
;
90 entry_sid
= samdb_result_dom_sid(mem_ctx
, (*res
)[i
], "objectSid");
92 if ((entry_sid
== NULL
) ||
93 (!dom_sid_in_domain(domain_sid
, entry_sid
))) {
94 /* Delete that entry from the result set */
95 (*res
)[i
] = (*res
)[count
-1];
97 talloc_free(entry_sid
);
100 talloc_free(entry_sid
);
108 search the sam for a single string attribute in exactly 1 record
110 const char *samdb_search_string_v(struct ldb_context
*sam_ldb
,
112 struct ldb_dn
*basedn
,
113 const char *attr_name
,
114 const char *format
, va_list ap
) _PRINTF_ATTRIBUTE(5,0)
117 const char *attrs
[2] = { NULL
, NULL
};
118 struct ldb_message
**res
= NULL
;
120 attrs
[0] = attr_name
;
122 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
124 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
125 attr_name
, format
, count
));
132 return ldb_msg_find_attr_as_string(res
[0], attr_name
, NULL
);
136 search the sam for a single string attribute in exactly 1 record
138 const char *samdb_search_string(struct ldb_context
*sam_ldb
,
140 struct ldb_dn
*basedn
,
141 const char *attr_name
,
142 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
147 va_start(ap
, format
);
148 str
= samdb_search_string_v(sam_ldb
, mem_ctx
, basedn
, attr_name
, format
, ap
);
154 struct ldb_dn
*samdb_search_dn(struct ldb_context
*sam_ldb
,
156 struct ldb_dn
*basedn
,
157 const char *format
, ...) _PRINTF_ATTRIBUTE(4,5)
161 struct ldb_message
**res
= NULL
;
164 va_start(ap
, format
);
165 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, NULL
, format
, ap
);
168 if (count
!= 1) return NULL
;
170 ret
= talloc_steal(mem_ctx
, res
[0]->dn
);
177 search the sam for a dom_sid attribute in exactly 1 record
179 struct dom_sid
*samdb_search_dom_sid(struct ldb_context
*sam_ldb
,
181 struct ldb_dn
*basedn
,
182 const char *attr_name
,
183 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
187 struct ldb_message
**res
;
188 const char *attrs
[2] = { NULL
, NULL
};
191 attrs
[0] = attr_name
;
193 va_start(ap
, format
);
194 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
197 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
198 attr_name
, format
, count
));
204 sid
= samdb_result_dom_sid(mem_ctx
, res
[0], attr_name
);
210 search the sam for a single integer attribute in exactly 1 record
212 unsigned int samdb_search_uint(struct ldb_context
*sam_ldb
,
214 unsigned int default_value
,
215 struct ldb_dn
*basedn
,
216 const char *attr_name
,
217 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
221 struct ldb_message
**res
;
222 const char *attrs
[2] = { NULL
, NULL
};
224 attrs
[0] = attr_name
;
226 va_start(ap
, format
);
227 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
231 return default_value
;
234 return ldb_msg_find_attr_as_uint(res
[0], attr_name
, default_value
);
238 search the sam for a single signed 64 bit integer attribute in exactly 1 record
240 int64_t samdb_search_int64(struct ldb_context
*sam_ldb
,
242 int64_t default_value
,
243 struct ldb_dn
*basedn
,
244 const char *attr_name
,
245 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
249 struct ldb_message
**res
;
250 const char *attrs
[2] = { NULL
, NULL
};
252 attrs
[0] = attr_name
;
254 va_start(ap
, format
);
255 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
259 return default_value
;
262 return ldb_msg_find_attr_as_int64(res
[0], attr_name
, default_value
);
266 search the sam for multipe records each giving a single string attribute
267 return the number of matches, or -1 on error
269 int samdb_search_string_multiple(struct ldb_context
*sam_ldb
,
271 struct ldb_dn
*basedn
,
273 const char *attr_name
,
274 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
278 const char *attrs
[2] = { NULL
, NULL
};
279 struct ldb_message
**res
= NULL
;
281 attrs
[0] = attr_name
;
283 va_start(ap
, format
);
284 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
291 /* make sure its single valued */
292 for (i
=0;i
<count
;i
++) {
293 if (res
[i
]->num_elements
!= 1) {
294 DEBUG(1,("samdb: search for %s %s not single valued\n",
301 *strs
= talloc_array(mem_ctx
, const char *, count
+1);
307 for (i
=0;i
<count
;i
++) {
308 (*strs
)[i
] = ldb_msg_find_attr_as_string(res
[i
], attr_name
, NULL
);
310 (*strs
)[count
] = NULL
;
315 struct ldb_dn
*samdb_result_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
316 const char *attr
, struct ldb_dn
*default_value
)
318 struct ldb_dn
*ret_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, msg
, attr
);
320 return default_value
;
326 pull a rid from a objectSid in a result set.
328 uint32_t samdb_result_rid_from_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
329 const char *attr
, uint32_t default_value
)
334 sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
336 return default_value
;
338 rid
= sid
->sub_auths
[sid
->num_auths
-1];
344 pull a dom_sid structure from a objectSid in a result set.
346 struct dom_sid
*samdb_result_dom_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
350 const struct ldb_val
*v
;
352 v
= ldb_msg_find_ldb_val(msg
, attr
);
356 sid
= talloc(mem_ctx
, struct dom_sid
);
360 ret
= sid_parse(v
->data
, v
->length
, sid
);
369 pull a guid structure from a objectGUID in a result set.
371 struct GUID
samdb_result_guid(const struct ldb_message
*msg
, const char *attr
)
373 const struct ldb_val
*v
;
377 v
= ldb_msg_find_ldb_val(msg
, attr
);
378 if (!v
) return GUID_zero();
380 status
= GUID_from_ndr_blob(v
, &guid
);
381 if (!NT_STATUS_IS_OK(status
)) {
389 pull a sid prefix from a objectSid in a result set.
390 this is used to find the domain sid for a user
392 struct dom_sid
*samdb_result_sid_prefix(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
395 struct dom_sid
*sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
396 if (!sid
|| sid
->num_auths
< 1) return NULL
;
402 pull a NTTIME in a result set.
404 NTTIME
samdb_result_nttime(const struct ldb_message
*msg
, const char *attr
,
405 NTTIME default_value
)
407 return ldb_msg_find_attr_as_uint64(msg
, attr
, default_value
);
411 * Windows stores 0 for lastLogoff.
412 * But when a MS DC return the lastLogoff (as Logoff Time)
413 * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
414 * cause windows 2008 and newer version to fail for SMB requests
416 NTTIME
samdb_result_last_logoff(const struct ldb_message
*msg
)
418 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "lastLogoff",0);
421 ret
= 0x7FFFFFFFFFFFFFFFULL
;
427 * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
428 * indicate an account doesn't expire.
430 * When Windows initially creates an account, it sets
431 * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF). However,
432 * when changing from an account having a specific expiration date to
433 * that account never expiring, it sets accountExpires = 0.
435 * Consolidate that logic here to allow clearer logic for account expiry in
436 * the rest of the code.
438 NTTIME
samdb_result_account_expires(const struct ldb_message
*msg
)
440 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "accountExpires",
444 ret
= 0x7FFFFFFFFFFFFFFFULL
;
450 construct the allow_password_change field from the PwdLastSet attribute and the
451 domain password settings
453 NTTIME
samdb_result_allow_password_change(struct ldb_context
*sam_ldb
,
455 struct ldb_dn
*domain_dn
,
456 struct ldb_message
*msg
,
459 uint64_t attr_time
= ldb_msg_find_attr_as_uint64(msg
, attr
, 0);
462 if (attr_time
== 0) {
466 minPwdAge
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
, "minPwdAge", NULL
);
468 /* yes, this is a -= not a += as minPwdAge is stored as the negative
469 of the number of 100-nano-seconds */
470 attr_time
-= minPwdAge
;
476 pull a samr_Password structutre from a result set.
478 struct samr_Password
*samdb_result_hash(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
, const char *attr
)
480 struct samr_Password
*hash
= NULL
;
481 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
482 if (val
&& (val
->length
>= sizeof(hash
->hash
))) {
483 hash
= talloc(mem_ctx
, struct samr_Password
);
484 memcpy(hash
->hash
, val
->data
, MIN(val
->length
, sizeof(hash
->hash
)));
490 pull an array of samr_Password structures from a result set.
492 unsigned int samdb_result_hashes(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
493 const char *attr
, struct samr_Password
**hashes
)
495 unsigned int count
, i
;
496 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
502 count
= val
->length
/ 16;
507 *hashes
= talloc_array(mem_ctx
, struct samr_Password
, count
);
512 for (i
=0;i
<count
;i
++) {
513 memcpy((*hashes
)[i
].hash
, (i
*16)+(char *)val
->data
, 16);
519 NTSTATUS
samdb_result_passwords_from_history(TALLOC_CTX
*mem_ctx
,
520 struct loadparm_context
*lp_ctx
,
521 struct ldb_message
*msg
,
523 struct samr_Password
**lm_pwd
,
524 struct samr_Password
**nt_pwd
)
526 struct samr_Password
*lmPwdHash
, *ntPwdHash
;
530 num_nt
= samdb_result_hashes(mem_ctx
, msg
, "ntPwdHistory", &ntPwdHash
);
534 *nt_pwd
= &ntPwdHash
[idx
];
538 /* Ensure that if we have turned off LM
539 * authentication, that we never use the LM hash, even
541 if (lpcfg_lanman_auth(lp_ctx
)) {
543 num_lm
= samdb_result_hashes(mem_ctx
, msg
, "lmPwdHistory", &lmPwdHash
);
547 *lm_pwd
= &lmPwdHash
[idx
];
556 NTSTATUS
samdb_result_passwords_no_lockout(TALLOC_CTX
*mem_ctx
,
557 struct loadparm_context
*lp_ctx
,
558 const struct ldb_message
*msg
,
559 struct samr_Password
**lm_pwd
,
560 struct samr_Password
**nt_pwd
)
562 struct samr_Password
*lmPwdHash
, *ntPwdHash
;
566 num_nt
= samdb_result_hashes(mem_ctx
, msg
, "unicodePwd", &ntPwdHash
);
569 } else if (num_nt
> 1) {
570 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
572 *nt_pwd
= &ntPwdHash
[0];
576 /* Ensure that if we have turned off LM
577 * authentication, that we never use the LM hash, even
579 if (lpcfg_lanman_auth(lp_ctx
)) {
581 num_lm
= samdb_result_hashes(mem_ctx
, msg
, "dBCSPwd", &lmPwdHash
);
584 } else if (num_lm
> 1) {
585 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
587 *lm_pwd
= &lmPwdHash
[0];
596 NTSTATUS
samdb_result_passwords(TALLOC_CTX
*mem_ctx
,
597 struct loadparm_context
*lp_ctx
,
598 const struct ldb_message
*msg
,
599 struct samr_Password
**lm_pwd
,
600 struct samr_Password
**nt_pwd
)
604 acct_flags
= samdb_result_acct_flags(msg
,
605 "msDS-User-Account-Control-Computed");
606 /* Quit if the account was locked out. */
607 if (acct_flags
& ACB_AUTOLOCK
) {
608 DEBUG(3,("samdb_result_passwords: Account for user %s was locked out.\n",
609 ldb_dn_get_linearized(msg
->dn
)));
610 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
613 return samdb_result_passwords_no_lockout(mem_ctx
, lp_ctx
, msg
,
618 pull a samr_LogonHours structutre from a result set.
620 struct samr_LogonHours
samdb_result_logon_hours(TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
, const char *attr
)
622 struct samr_LogonHours hours
;
623 size_t units_per_week
= 168;
624 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
629 units_per_week
= val
->length
* 8;
632 hours
.bits
= talloc_array(mem_ctx
, uint8_t, units_per_week
/8);
636 hours
.units_per_week
= units_per_week
;
637 memset(hours
.bits
, 0xFF, units_per_week
/8);
639 memcpy(hours
.bits
, val
->data
, val
->length
);
646 pull a set of account_flags from a result set.
648 Naturally, this requires that userAccountControl and
649 (if not null) the attributes 'attr' be already
652 uint32_t samdb_result_acct_flags(const struct ldb_message
*msg
, const char *attr
)
654 uint32_t userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
655 uint32_t attr_flags
= 0;
656 uint32_t acct_flags
= ds_uf2acb(userAccountControl
);
658 attr_flags
= ldb_msg_find_attr_as_uint(msg
, attr
, UF_ACCOUNTDISABLE
);
659 if (attr_flags
== UF_ACCOUNTDISABLE
) {
660 DEBUG(0, ("Attribute %s not found, disabling account %s!\n", attr
,
661 ldb_dn_get_linearized(msg
->dn
)));
663 acct_flags
|= ds_uf2acb(attr_flags
);
669 NTSTATUS
samdb_result_parameters(TALLOC_CTX
*mem_ctx
,
670 struct ldb_message
*msg
,
672 struct lsa_BinaryString
*s
)
675 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
683 if ((val
->length
% 2) != 0) {
685 * If the on-disk data is not even in length, we know
686 * it is corrupt, and can not be safely pushed. We
687 * would either truncate, send either a un-initilaised
688 * byte or send a forced zero byte
690 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
693 s
->array
= talloc_array(mem_ctx
, uint16_t, val
->length
/2);
695 return NT_STATUS_NO_MEMORY
;
697 s
->length
= s
->size
= val
->length
;
699 /* The on-disk format is the 'network' format, being UTF16LE (sort of) */
700 for (i
= 0; i
< s
->length
/ 2; i
++) {
701 s
->array
[i
] = SVAL(val
->data
, i
* 2);
707 /* Find an attribute, with a particular value */
709 /* The current callers of this function expect a very specific
710 * behaviour: In particular, objectClass subclass equivalence is not
711 * wanted. This means that we should not lookup the schema for the
712 * comparison function */
713 struct ldb_message_element
*samdb_find_attribute(struct ldb_context
*ldb
,
714 const struct ldb_message
*msg
,
715 const char *name
, const char *value
)
718 struct ldb_message_element
*el
= ldb_msg_find_element(msg
, name
);
724 for (i
=0;i
<el
->num_values
;i
++) {
725 if (ldb_attr_cmp(value
, (char *)el
->values
[i
].data
) == 0) {
733 static int samdb_find_or_add_attribute_ex(struct ldb_context
*ldb
,
734 struct ldb_message
*msg
,
736 const char *set_value
,
741 struct ldb_message_element
*el
;
743 SMB_ASSERT(attr_flags
!= 0);
745 el
= ldb_msg_find_element(msg
, name
);
754 ret
= ldb_msg_add_empty(msg
, name
,
757 if (ret
!= LDB_SUCCESS
) {
761 if (set_value
!= NULL
) {
762 ret
= ldb_msg_add_string(msg
, name
, set_value
);
763 if (ret
!= LDB_SUCCESS
) {
774 int samdb_find_or_add_attribute(struct ldb_context
*ldb
, struct ldb_message
*msg
, const char *name
, const char *set_value
)
776 return samdb_find_or_add_attribute_ex(ldb
, msg
, name
, set_value
, LDB_FLAG_MOD_ADD
, NULL
);
780 add a dom_sid element to a message
782 int samdb_msg_add_dom_sid(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
783 const char *attr_name
, const struct dom_sid
*sid
)
786 enum ndr_err_code ndr_err
;
788 ndr_err
= ndr_push_struct_blob(&v
, mem_ctx
,
790 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
791 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
792 return ldb_operr(sam_ldb
);
794 return ldb_msg_add_value(msg
, attr_name
, &v
, NULL
);
799 add a delete element operation to a message
801 int samdb_msg_add_delete(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
802 const char *attr_name
)
804 /* we use an empty replace rather than a delete, as it allows for
805 dsdb_replace() to be used everywhere */
806 return ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_REPLACE
, NULL
);
810 add an add attribute value to a message or enhance an existing attribute
811 which has the same name and the add flag set.
813 int samdb_msg_add_addval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
814 struct ldb_message
*msg
, const char *attr_name
,
817 struct ldb_message_element
*el
;
818 struct ldb_val val
, *vals
;
824 v
= talloc_strdup(mem_ctx
, value
);
826 return ldb_oom(sam_ldb
);
829 val
.data
= (uint8_t *) v
;
830 val
.length
= strlen(v
);
832 if (val
.length
== 0) {
833 /* allow empty strings as non-existent attributes */
837 for (i
= 0; i
< msg
->num_elements
; i
++) {
838 el
= &msg
->elements
[i
];
839 if ((ldb_attr_cmp(el
->name
, attr_name
) == 0) &&
840 (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_ADD
)) {
846 ret
= ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_ADD
,
848 if (ret
!= LDB_SUCCESS
) {
853 vals
= talloc_realloc(msg
->elements
, el
->values
, struct ldb_val
,
856 return ldb_oom(sam_ldb
);
859 el
->values
[el
->num_values
] = val
;
866 add a delete attribute value to a message or enhance an existing attribute
867 which has the same name and the delete flag set.
869 int samdb_msg_add_delval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
870 struct ldb_message
*msg
, const char *attr_name
,
873 struct ldb_message_element
*el
;
874 struct ldb_val val
, *vals
;
880 v
= talloc_strdup(mem_ctx
, value
);
882 return ldb_oom(sam_ldb
);
885 val
.data
= (uint8_t *) v
;
886 val
.length
= strlen(v
);
888 if (val
.length
== 0) {
889 /* allow empty strings as non-existent attributes */
893 for (i
= 0; i
< msg
->num_elements
; i
++) {
894 el
= &msg
->elements
[i
];
895 if ((ldb_attr_cmp(el
->name
, attr_name
) == 0) &&
896 (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
)) {
902 ret
= ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_DELETE
,
904 if (ret
!= LDB_SUCCESS
) {
909 vals
= talloc_realloc(msg
->elements
, el
->values
, struct ldb_val
,
912 return ldb_oom(sam_ldb
);
915 el
->values
[el
->num_values
] = val
;
922 add a int element to a message
924 int samdb_msg_add_int(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
925 const char *attr_name
, int v
)
927 const char *s
= talloc_asprintf(mem_ctx
, "%d", v
);
929 return ldb_oom(sam_ldb
);
931 return ldb_msg_add_string(msg
, attr_name
, s
);
935 * Add an unsigned int element to a message
937 * The issue here is that we have not yet first cast to int32_t explicitly,
938 * before we cast to an signed int to printf() into the %d or cast to a
939 * int64_t before we then cast to a long long to printf into a %lld.
941 * There are *no* unsigned integers in Active Directory LDAP, even the RID
942 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
943 * (See the schema, and the syntax definitions in schema_syntax.c).
946 int samdb_msg_add_uint(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
947 const char *attr_name
, unsigned int v
)
949 return samdb_msg_add_int(sam_ldb
, mem_ctx
, msg
, attr_name
, (int)v
);
953 add a (signed) int64_t element to a message
955 int samdb_msg_add_int64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
956 const char *attr_name
, int64_t v
)
958 const char *s
= talloc_asprintf(mem_ctx
, "%lld", (long long)v
);
960 return ldb_oom(sam_ldb
);
962 return ldb_msg_add_string(msg
, attr_name
, s
);
966 * Add an unsigned int64_t (uint64_t) element to a message
968 * The issue here is that we have not yet first cast to int32_t explicitly,
969 * before we cast to an signed int to printf() into the %d or cast to a
970 * int64_t before we then cast to a long long to printf into a %lld.
972 * There are *no* unsigned integers in Active Directory LDAP, even the RID
973 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
974 * (See the schema, and the syntax definitions in schema_syntax.c).
977 int samdb_msg_add_uint64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
978 const char *attr_name
, uint64_t v
)
980 return samdb_msg_add_int64(sam_ldb
, mem_ctx
, msg
, attr_name
, (int64_t)v
);
984 add a samr_Password element to a message
986 int samdb_msg_add_hash(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
987 const char *attr_name
, const struct samr_Password
*hash
)
990 val
.data
= talloc_memdup(mem_ctx
, hash
->hash
, 16);
992 return ldb_oom(sam_ldb
);
995 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
999 add a samr_Password array to a message
1001 int samdb_msg_add_hashes(struct ldb_context
*ldb
,
1002 TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1003 const char *attr_name
, struct samr_Password
*hashes
,
1008 val
.data
= talloc_array_size(mem_ctx
, 16, count
);
1009 val
.length
= count
*16;
1011 return ldb_oom(ldb
);
1013 for (i
=0;i
<count
;i
++) {
1014 memcpy(i
*16 + (char *)val
.data
, hashes
[i
].hash
, 16);
1016 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
1020 add a acct_flags element to a message
1022 int samdb_msg_add_acct_flags(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1023 const char *attr_name
, uint32_t v
)
1025 return samdb_msg_add_uint(sam_ldb
, mem_ctx
, msg
, attr_name
, ds_acb2uf(v
));
1029 add a logon_hours element to a message
1031 int samdb_msg_add_logon_hours(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1032 const char *attr_name
, struct samr_LogonHours
*hours
)
1035 val
.length
= hours
->units_per_week
/ 8;
1036 val
.data
= hours
->bits
;
1037 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
1041 add a parameters element to a message
1043 int samdb_msg_add_parameters(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1044 const char *attr_name
, struct lsa_BinaryString
*parameters
)
1048 if ((parameters
->length
% 2) != 0) {
1049 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
1052 val
.data
= talloc_array(mem_ctx
, uint8_t, parameters
->length
);
1053 if (val
.data
== NULL
) {
1054 return LDB_ERR_OPERATIONS_ERROR
;
1056 val
.length
= parameters
->length
;
1057 for (i
= 0; i
< parameters
->length
/ 2; i
++) {
1059 * The on-disk format needs to be in the 'network'
1060 * format, parmeters->array is a uint16_t array of
1061 * length parameters->length / 2
1063 SSVAL(val
.data
, i
* 2, parameters
->array
[i
]);
1065 return ldb_msg_add_steal_value(msg
, attr_name
, &val
);
1069 * Sets an unsigned int element in a message
1071 * The issue here is that we have not yet first cast to int32_t explicitly,
1072 * before we cast to an signed int to printf() into the %d or cast to a
1073 * int64_t before we then cast to a long long to printf into a %lld.
1075 * There are *no* unsigned integers in Active Directory LDAP, even the RID
1076 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
1077 * (See the schema, and the syntax definitions in schema_syntax.c).
1080 int samdb_msg_set_uint(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
1081 struct ldb_message
*msg
, const char *attr_name
,
1084 struct ldb_message_element
*el
;
1086 el
= ldb_msg_find_element(msg
, attr_name
);
1090 return samdb_msg_add_uint(sam_ldb
, mem_ctx
, msg
, attr_name
, v
);
1094 * Handle ldb_request in transaction
1096 int dsdb_autotransaction_request(struct ldb_context
*sam_ldb
,
1097 struct ldb_request
*req
)
1101 ret
= ldb_transaction_start(sam_ldb
);
1102 if (ret
!= LDB_SUCCESS
) {
1106 ret
= ldb_request(sam_ldb
, req
);
1107 if (ret
== LDB_SUCCESS
) {
1108 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1111 if (ret
== LDB_SUCCESS
) {
1112 return ldb_transaction_commit(sam_ldb
);
1114 ldb_transaction_cancel(sam_ldb
);
1120 return a default security descriptor
1122 struct security_descriptor
*samdb_default_security_descriptor(TALLOC_CTX
*mem_ctx
)
1124 struct security_descriptor
*sd
;
1126 sd
= security_descriptor_initialise(mem_ctx
);
1131 struct ldb_dn
*samdb_aggregate_schema_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1133 struct ldb_dn
*schema_dn
= ldb_get_schema_basedn(sam_ctx
);
1134 struct ldb_dn
*aggregate_dn
;
1139 aggregate_dn
= ldb_dn_copy(mem_ctx
, schema_dn
);
1140 if (!aggregate_dn
) {
1143 if (!ldb_dn_add_child_fmt(aggregate_dn
, "CN=Aggregate")) {
1146 return aggregate_dn
;
1149 struct ldb_dn
*samdb_partitions_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1151 struct ldb_dn
*new_dn
;
1153 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_config_basedn(sam_ctx
));
1154 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Partitions")) {
1155 talloc_free(new_dn
);
1161 struct ldb_dn
*samdb_infrastructure_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1163 struct ldb_dn
*new_dn
;
1165 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_default_basedn(sam_ctx
));
1166 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Infrastructure")) {
1167 talloc_free(new_dn
);
1173 struct ldb_dn
*samdb_sites_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1175 struct ldb_dn
*new_dn
;
1177 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_config_basedn(sam_ctx
));
1178 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Sites")) {
1179 talloc_free(new_dn
);
1185 struct ldb_dn
*samdb_extended_rights_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1187 struct ldb_dn
*new_dn
;
1189 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_config_basedn(sam_ctx
));
1190 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Extended-Rights")) {
1191 talloc_free(new_dn
);
1197 work out the domain sid for the current open ldb
1199 const struct dom_sid
*samdb_domain_sid(struct ldb_context
*ldb
)
1201 TALLOC_CTX
*tmp_ctx
;
1202 const struct dom_sid
*domain_sid
;
1203 const char *attrs
[] = {
1207 struct ldb_result
*res
;
1210 /* see if we have a cached copy */
1211 domain_sid
= (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1216 tmp_ctx
= talloc_new(ldb
);
1217 if (tmp_ctx
== NULL
) {
1221 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_get_default_basedn(ldb
), LDB_SCOPE_BASE
, attrs
, "objectSid=*");
1223 if (ret
!= LDB_SUCCESS
) {
1227 if (res
->count
!= 1) {
1231 domain_sid
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSid");
1232 if (domain_sid
== NULL
) {
1236 /* cache the domain_sid in the ldb */
1237 if (ldb_set_opaque(ldb
, "cache.domain_sid", discard_const_p(struct dom_sid
, domain_sid
)) != LDB_SUCCESS
) {
1241 talloc_steal(ldb
, domain_sid
);
1242 talloc_free(tmp_ctx
);
1247 talloc_free(tmp_ctx
);
1252 get domain sid from cache
1254 const struct dom_sid
*samdb_domain_sid_cache_only(struct ldb_context
*ldb
)
1256 return (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1259 bool samdb_set_domain_sid(struct ldb_context
*ldb
, const struct dom_sid
*dom_sid_in
)
1261 TALLOC_CTX
*tmp_ctx
;
1262 struct dom_sid
*dom_sid_new
;
1263 struct dom_sid
*dom_sid_old
;
1265 /* see if we have a cached copy */
1266 dom_sid_old
= talloc_get_type(ldb_get_opaque(ldb
,
1267 "cache.domain_sid"), struct dom_sid
);
1269 tmp_ctx
= talloc_new(ldb
);
1270 if (tmp_ctx
== NULL
) {
1274 dom_sid_new
= dom_sid_dup(tmp_ctx
, dom_sid_in
);
1279 /* cache the domain_sid in the ldb */
1280 if (ldb_set_opaque(ldb
, "cache.domain_sid", dom_sid_new
) != LDB_SUCCESS
) {
1284 talloc_steal(ldb
, dom_sid_new
);
1285 talloc_free(tmp_ctx
);
1286 talloc_free(dom_sid_old
);
1291 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1292 talloc_free(tmp_ctx
);
1297 work out the domain guid for the current open ldb
1299 const struct GUID
*samdb_domain_guid(struct ldb_context
*ldb
)
1301 TALLOC_CTX
*tmp_ctx
= NULL
;
1302 struct GUID
*domain_guid
= NULL
;
1303 const char *attrs
[] = {
1307 struct ldb_result
*res
= NULL
;
1310 /* see if we have a cached copy */
1311 domain_guid
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.domain_guid");
1316 tmp_ctx
= talloc_new(ldb
);
1317 if (tmp_ctx
== NULL
) {
1321 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_get_default_basedn(ldb
), LDB_SCOPE_BASE
, attrs
, "objectGUID=*");
1322 if (ret
!= LDB_SUCCESS
) {
1326 if (res
->count
!= 1) {
1330 domain_guid
= talloc(tmp_ctx
, struct GUID
);
1331 if (domain_guid
== NULL
) {
1334 *domain_guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
1336 /* cache the domain_sid in the ldb */
1337 if (ldb_set_opaque(ldb
, "cache.domain_guid", domain_guid
) != LDB_SUCCESS
) {
1341 talloc_steal(ldb
, domain_guid
);
1342 talloc_free(tmp_ctx
);
1347 talloc_free(tmp_ctx
);
1351 bool samdb_set_ntds_settings_dn(struct ldb_context
*ldb
, struct ldb_dn
*ntds_settings_dn_in
)
1353 TALLOC_CTX
*tmp_ctx
;
1354 struct ldb_dn
*ntds_settings_dn_new
;
1355 struct ldb_dn
*ntds_settings_dn_old
;
1357 /* see if we have a forced copy from provision */
1358 ntds_settings_dn_old
= talloc_get_type(ldb_get_opaque(ldb
,
1359 "forced.ntds_settings_dn"), struct ldb_dn
);
1361 tmp_ctx
= talloc_new(ldb
);
1362 if (tmp_ctx
== NULL
) {
1366 ntds_settings_dn_new
= ldb_dn_copy(tmp_ctx
, ntds_settings_dn_in
);
1367 if (!ntds_settings_dn_new
) {
1371 /* set the DN in the ldb to avoid lookups during provision */
1372 if (ldb_set_opaque(ldb
, "forced.ntds_settings_dn", ntds_settings_dn_new
) != LDB_SUCCESS
) {
1376 talloc_steal(ldb
, ntds_settings_dn_new
);
1377 talloc_free(tmp_ctx
);
1378 talloc_free(ntds_settings_dn_old
);
1383 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1384 talloc_free(tmp_ctx
);
1389 work out the ntds settings dn for the current open ldb
1391 struct ldb_dn
*samdb_ntds_settings_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1393 TALLOC_CTX
*tmp_ctx
;
1394 const char *root_attrs
[] = { "dsServiceName", NULL
};
1396 struct ldb_result
*root_res
;
1397 struct ldb_dn
*settings_dn
;
1399 /* see if we have a cached copy */
1400 settings_dn
= (struct ldb_dn
*)ldb_get_opaque(ldb
, "forced.ntds_settings_dn");
1402 return ldb_dn_copy(mem_ctx
, settings_dn
);
1405 tmp_ctx
= talloc_new(mem_ctx
);
1406 if (tmp_ctx
== NULL
) {
1410 ret
= ldb_search(ldb
, tmp_ctx
, &root_res
, ldb_dn_new(tmp_ctx
, ldb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
1411 if (ret
!= LDB_SUCCESS
) {
1412 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1413 ldb_errstring(ldb
)));
1417 if (root_res
->count
!= 1) {
1421 settings_dn
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, root_res
->msgs
[0], "dsServiceName");
1423 /* note that we do not cache the DN here, as that would mean
1424 * we could not handle server renames at runtime. Only
1425 * provision sets up forced.ntds_settings_dn */
1427 talloc_steal(mem_ctx
, settings_dn
);
1428 talloc_free(tmp_ctx
);
1433 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1434 talloc_free(tmp_ctx
);
1439 work out the ntds settings invocationID/objectGUID for the current open ldb
1441 static const struct GUID
*samdb_ntds_GUID(struct ldb_context
*ldb
,
1442 const char *attribute
,
1443 const char *cache_name
)
1445 TALLOC_CTX
*tmp_ctx
;
1446 const char *attrs
[] = { attribute
, NULL
};
1448 struct ldb_result
*res
;
1449 struct GUID
*ntds_guid
;
1450 struct ldb_dn
*ntds_settings_dn
= NULL
;
1451 const char *errstr
= NULL
;
1453 /* see if we have a cached copy */
1454 ntds_guid
= (struct GUID
*)ldb_get_opaque(ldb
, cache_name
);
1455 if (ntds_guid
!= NULL
) {
1459 tmp_ctx
= talloc_new(ldb
);
1460 if (tmp_ctx
== NULL
) {
1464 ntds_settings_dn
= samdb_ntds_settings_dn(ldb
, tmp_ctx
);
1465 if (ntds_settings_dn
== NULL
) {
1466 errstr
= "samdb_ntds_settings_dn() returned NULL";
1470 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ntds_settings_dn
,
1471 LDB_SCOPE_BASE
, attrs
, NULL
);
1473 errstr
= ldb_errstring(ldb
);
1477 if (res
->count
!= 1) {
1478 errstr
= "incorrect number of results from base search";
1482 ntds_guid
= talloc(tmp_ctx
, struct GUID
);
1483 if (ntds_guid
== NULL
) {
1487 *ntds_guid
= samdb_result_guid(res
->msgs
[0], attribute
);
1489 if (GUID_all_zero(ntds_guid
)) {
1490 if (ldb_msg_find_ldb_val(res
->msgs
[0], attribute
)) {
1491 errstr
= "failed to find the GUID attribute";
1493 errstr
= "failed to parse the GUID";
1498 /* cache the domain_sid in the ldb */
1499 if (ldb_set_opaque(ldb
, cache_name
, ntds_guid
) != LDB_SUCCESS
) {
1500 errstr
= "ldb_set_opaque() failed";
1504 talloc_steal(ldb
, ntds_guid
);
1505 talloc_free(tmp_ctx
);
1510 DBG_WARNING("Failed to find our own NTDS Settings %s in the ldb: %s!\n",
1512 talloc_free(tmp_ctx
);
1517 work out the ntds settings objectGUID for the current open ldb
1519 const struct GUID
*samdb_ntds_objectGUID(struct ldb_context
*ldb
)
1521 return samdb_ntds_GUID(ldb
, "objectGUID", "cache.ntds_guid");
1525 work out the ntds settings invocationId for the current open ldb
1527 const struct GUID
*samdb_ntds_invocation_id(struct ldb_context
*ldb
)
1529 return samdb_ntds_GUID(ldb
, "invocationId", "cache.invocation_id");
1532 static bool samdb_set_ntds_GUID(struct ldb_context
*ldb
,
1533 const struct GUID
*ntds_guid_in
,
1534 const char *attribute
,
1535 const char *cache_name
)
1537 TALLOC_CTX
*tmp_ctx
;
1538 struct GUID
*ntds_guid_new
;
1539 struct GUID
*ntds_guid_old
;
1541 /* see if we have a cached copy */
1542 ntds_guid_old
= (struct GUID
*)ldb_get_opaque(ldb
, cache_name
);
1544 tmp_ctx
= talloc_new(ldb
);
1545 if (tmp_ctx
== NULL
) {
1549 ntds_guid_new
= talloc(tmp_ctx
, struct GUID
);
1550 if (!ntds_guid_new
) {
1554 *ntds_guid_new
= *ntds_guid_in
;
1556 /* cache the domain_sid in the ldb */
1557 if (ldb_set_opaque(ldb
, cache_name
, ntds_guid_new
) != LDB_SUCCESS
) {
1561 talloc_steal(ldb
, ntds_guid_new
);
1562 talloc_free(tmp_ctx
);
1563 talloc_free(ntds_guid_old
);
1568 DBG_WARNING("Failed to set our own cached %s in the ldb!\n",
1570 talloc_free(tmp_ctx
);
1574 bool samdb_set_ntds_objectGUID(struct ldb_context
*ldb
, const struct GUID
*ntds_guid_in
)
1576 return samdb_set_ntds_GUID(ldb
,
1582 bool samdb_set_ntds_invocation_id(struct ldb_context
*ldb
, const struct GUID
*invocation_id_in
)
1584 return samdb_set_ntds_GUID(ldb
,
1587 "cache.invocation_id");
1591 work out the server dn for the current open ldb
1593 struct ldb_dn
*samdb_server_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1595 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
1600 dn
= ldb_dn_get_parent(mem_ctx
, samdb_ntds_settings_dn(ldb
, tmp_ctx
));
1601 talloc_free(tmp_ctx
);
1607 work out the server dn for the current open ldb
1609 struct ldb_dn
*samdb_server_site_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1611 struct ldb_dn
*server_dn
;
1612 struct ldb_dn
*servers_dn
;
1613 struct ldb_dn
*server_site_dn
;
1615 /* TODO: there must be a saner way to do this!! */
1616 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1617 if (!server_dn
) return NULL
;
1619 servers_dn
= ldb_dn_get_parent(mem_ctx
, server_dn
);
1620 talloc_free(server_dn
);
1621 if (!servers_dn
) return NULL
;
1623 server_site_dn
= ldb_dn_get_parent(mem_ctx
, servers_dn
);
1624 talloc_free(servers_dn
);
1626 return server_site_dn
;
1630 find the site name from a computers DN record
1632 int samdb_find_site_for_computer(struct ldb_context
*ldb
,
1633 TALLOC_CTX
*mem_ctx
, struct ldb_dn
*computer_dn
,
1634 const char **site_name
)
1638 const struct ldb_val
*rdn_val
;
1642 ret
= samdb_reference_dn(ldb
, mem_ctx
, computer_dn
, "serverReferenceBL", &dn
);
1643 if (ret
!= LDB_SUCCESS
) {
1647 if (!ldb_dn_remove_child_components(dn
, 2)) {
1649 return LDB_ERR_INVALID_DN_SYNTAX
;
1652 rdn_val
= ldb_dn_get_rdn_val(dn
);
1653 if (rdn_val
== NULL
) {
1654 return LDB_ERR_OPERATIONS_ERROR
;
1657 (*site_name
) = talloc_strndup(mem_ctx
, (const char *)rdn_val
->data
, rdn_val
->length
);
1660 return LDB_ERR_OPERATIONS_ERROR
;
1666 find the NTDS GUID from a computers DN record
1668 int samdb_find_ntdsguid_for_computer(struct ldb_context
*ldb
, struct ldb_dn
*computer_dn
,
1669 struct GUID
*ntds_guid
)
1674 *ntds_guid
= GUID_zero();
1676 ret
= samdb_reference_dn(ldb
, ldb
, computer_dn
, "serverReferenceBL", &dn
);
1677 if (ret
!= LDB_SUCCESS
) {
1681 if (!ldb_dn_add_child_fmt(dn
, "CN=NTDS Settings")) {
1683 return LDB_ERR_OPERATIONS_ERROR
;
1686 ret
= dsdb_find_guid_by_dn(ldb
, dn
, ntds_guid
);
1692 find a 'reference' DN that points at another object
1693 (eg. serverReference, rIDManagerReference etc)
1695 int samdb_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*base
,
1696 const char *attribute
, struct ldb_dn
**dn
)
1698 const char *attrs
[2];
1699 struct ldb_result
*res
;
1702 attrs
[0] = attribute
;
1705 ret
= dsdb_search(ldb
, mem_ctx
, &res
, base
, LDB_SCOPE_BASE
, attrs
, DSDB_SEARCH_ONE_ONLY
|DSDB_SEARCH_SHOW_EXTENDED_DN
, NULL
);
1706 if (ret
!= LDB_SUCCESS
) {
1707 ldb_asprintf_errstring(ldb
, "Cannot find DN %s to get attribute %s for reference dn: %s",
1708 ldb_dn_get_linearized(base
), attribute
, ldb_errstring(ldb
));
1712 *dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, res
->msgs
[0], attribute
);
1714 if (!ldb_msg_find_element(res
->msgs
[0], attribute
)) {
1715 ldb_asprintf_errstring(ldb
, "Cannot find attribute %s of %s to calculate reference dn", attribute
,
1716 ldb_dn_get_linearized(base
));
1718 ldb_asprintf_errstring(ldb
, "Cannot interpret attribute %s of %s as a dn", attribute
,
1719 ldb_dn_get_linearized(base
));
1722 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
1730 find if a DN (must have GUID component!) is our ntdsDsa
1732 int samdb_dn_is_our_ntdsa(struct ldb_context
*ldb
, struct ldb_dn
*dn
, bool *is_ntdsa
)
1735 struct GUID dn_guid
;
1736 const struct GUID
*our_ntds_guid
;
1737 status
= dsdb_get_extended_dn_guid(dn
, &dn_guid
, "GUID");
1738 if (!NT_STATUS_IS_OK(status
)) {
1739 return LDB_ERR_OPERATIONS_ERROR
;
1742 our_ntds_guid
= samdb_ntds_objectGUID(ldb
);
1743 if (!our_ntds_guid
) {
1744 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s - %s\n", ldb_dn_get_linearized(dn
), ldb_errstring(ldb
)));
1745 return LDB_ERR_OPERATIONS_ERROR
;
1748 *is_ntdsa
= GUID_equal(&dn_guid
, our_ntds_guid
);
1753 find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1755 int samdb_reference_dn_is_our_ntdsa(struct ldb_context
*ldb
, struct ldb_dn
*base
,
1756 const char *attribute
, bool *is_ntdsa
)
1759 struct ldb_dn
*referenced_dn
;
1760 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
1761 if (tmp_ctx
== NULL
) {
1762 return LDB_ERR_OPERATIONS_ERROR
;
1764 ret
= samdb_reference_dn(ldb
, tmp_ctx
, base
, attribute
, &referenced_dn
);
1765 if (ret
!= LDB_SUCCESS
) {
1766 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base
), attribute
, ldb_errstring(ldb
)));
1770 ret
= samdb_dn_is_our_ntdsa(ldb
, referenced_dn
, is_ntdsa
);
1772 talloc_free(tmp_ctx
);
1777 find our machine account via the serverReference attribute in the
1780 int samdb_server_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1782 struct ldb_dn
*server_dn
;
1785 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1786 if (server_dn
== NULL
) {
1787 return ldb_error(ldb
, LDB_ERR_NO_SUCH_OBJECT
, __func__
);
1790 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_dn
, "serverReference", dn
);
1791 talloc_free(server_dn
);
1797 find the RID Manager$ DN via the rIDManagerReference attribute in the
1800 int samdb_rid_manager_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1802 return samdb_reference_dn(ldb
, mem_ctx
, ldb_get_default_basedn(ldb
),
1803 "rIDManagerReference", dn
);
1807 find the RID Set DN via the rIDSetReferences attribute in our
1810 int samdb_rid_set_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1812 struct ldb_dn
*server_ref_dn
= NULL
;
1815 ret
= samdb_server_reference_dn(ldb
, mem_ctx
, &server_ref_dn
);
1816 if (ret
!= LDB_SUCCESS
) {
1819 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_ref_dn
, "rIDSetReferences", dn
);
1820 talloc_free(server_ref_dn
);
1824 const char *samdb_server_site_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1826 const struct ldb_val
*val
= ldb_dn_get_rdn_val(samdb_server_site_dn(ldb
,
1833 return (const char *) val
->data
;
1837 * Finds the client site by using the client's IP address.
1838 * The "subnet_name" returns the name of the subnet if parameter != NULL
1840 * Has a Windows-based fallback to provide the only site available, or an empty
1841 * string if there are multiple sites.
1843 const char *samdb_client_site_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1844 const char *ip_address
, char **subnet_name
,
1847 const char *attrs
[] = { "cn", "siteObject", NULL
};
1848 struct ldb_dn
*sites_container_dn
= NULL
;
1849 struct ldb_dn
*subnets_dn
= NULL
;
1850 struct ldb_dn
*sites_dn
= NULL
;
1851 struct ldb_result
*res
= NULL
;
1852 const struct ldb_val
*val
= NULL
;
1853 const char *site_name
= NULL
;
1854 const char *l_subnet_name
= NULL
;
1855 const char *allow_list
[2] = { NULL
, NULL
};
1856 unsigned int i
, count
;
1860 * if we don't have a client ip e.g. ncalrpc
1861 * the server site is the client site
1863 if (ip_address
== NULL
) {
1864 return samdb_server_site_name(ldb
, mem_ctx
);
1867 sites_container_dn
= samdb_sites_dn(ldb
, mem_ctx
);
1868 if (sites_container_dn
== NULL
) {
1872 subnets_dn
= ldb_dn_copy(mem_ctx
, sites_container_dn
);
1873 if ( ! ldb_dn_add_child_fmt(subnets_dn
, "CN=Subnets")) {
1877 ret
= ldb_search(ldb
, mem_ctx
, &res
, subnets_dn
, LDB_SCOPE_ONELEVEL
,
1879 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1881 } else if (ret
!= LDB_SUCCESS
) {
1887 for (i
= 0; i
< count
; i
++) {
1888 l_subnet_name
= ldb_msg_find_attr_as_string(res
->msgs
[i
], "cn",
1891 allow_list
[0] = l_subnet_name
;
1893 if (allow_access_nolog(NULL
, allow_list
, "", ip_address
)) {
1894 sites_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
,
1897 if (sites_dn
== NULL
) {
1898 /* No reference, maybe another subnet matches */
1902 /* "val" cannot be NULL here since "sites_dn" != NULL */
1903 val
= ldb_dn_get_rdn_val(sites_dn
);
1904 site_name
= talloc_strdup(mem_ctx
,
1905 (const char *) val
->data
);
1907 TALLOC_FREE(sites_dn
);
1913 if (site_name
== NULL
&& fallback
) {
1914 /* This is the Windows Server fallback rule: when no subnet
1915 * exists and we have only one site available then use it (it
1916 * is for sure the same as our server site). If more sites do
1917 * exist then we don't know which one to use and set the site
1920 ret
= dsdb_domain_count(
1926 "(objectClass=site)");
1927 if (ret
!= LDB_SUCCESS
) {
1931 site_name
= samdb_server_site_name(ldb
, mem_ctx
);
1933 site_name
= talloc_strdup(mem_ctx
, "");
1935 l_subnet_name
= NULL
;
1938 if (subnet_name
!= NULL
) {
1939 *subnet_name
= talloc_strdup(mem_ctx
, l_subnet_name
);
1943 TALLOC_FREE(sites_container_dn
);
1944 TALLOC_FREE(subnets_dn
);
1951 work out if we are the PDC for the domain of the current open ldb
1953 bool samdb_is_pdc(struct ldb_context
*ldb
)
1958 ret
= samdb_reference_dn_is_our_ntdsa(ldb
, ldb_get_default_basedn(ldb
), "fsmoRoleOwner",
1960 if (ret
!= LDB_SUCCESS
) {
1961 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n",
1962 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)),
1963 ldb_errstring(ldb
)));
1971 work out if we are a Global Catalog server for the domain of the current open ldb
1973 bool samdb_is_gc(struct ldb_context
*ldb
)
1975 uint32_t options
= 0;
1976 if (samdb_ntds_options(ldb
, &options
) != LDB_SUCCESS
) {
1979 return (options
& DS_NTDSDSA_OPT_IS_GC
) != 0;
1982 /* Find a domain object in the parents of a particular DN. */
1983 int samdb_search_for_parent_domain(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
1984 struct ldb_dn
**parent_dn
, const char **errstring
)
1986 TALLOC_CTX
*local_ctx
;
1987 struct ldb_dn
*sdn
= dn
;
1988 struct ldb_result
*res
= NULL
;
1989 int ret
= LDB_SUCCESS
;
1990 const char *attrs
[] = { NULL
};
1992 local_ctx
= talloc_new(mem_ctx
);
1993 if (local_ctx
== NULL
) return ldb_oom(ldb
);
1995 while ((sdn
= ldb_dn_get_parent(local_ctx
, sdn
))) {
1996 ret
= ldb_search(ldb
, local_ctx
, &res
, sdn
, LDB_SCOPE_BASE
, attrs
,
1997 "(|(objectClass=domain)(objectClass=builtinDomain))");
1998 if (ret
== LDB_SUCCESS
) {
1999 if (res
->count
== 1) {
2007 if (ret
!= LDB_SUCCESS
) {
2008 *errstring
= talloc_asprintf(mem_ctx
, "Error searching for parent domain of %s, failed searching for %s: %s",
2009 ldb_dn_get_linearized(dn
),
2010 ldb_dn_get_linearized(sdn
),
2011 ldb_errstring(ldb
));
2012 talloc_free(local_ctx
);
2015 /* should never be true with 'ret=LDB_SUCCESS', here to satisfy clang */
2017 talloc_free(local_ctx
);
2018 return LDB_ERR_OTHER
;
2020 if (res
->count
!= 1) {
2021 *errstring
= talloc_asprintf(mem_ctx
, "Invalid dn (%s), not child of a domain object",
2022 ldb_dn_get_linearized(dn
));
2023 DEBUG(0,(__location__
": %s\n", *errstring
));
2024 talloc_free(local_ctx
);
2025 return LDB_ERR_CONSTRAINT_VIOLATION
;
2028 *parent_dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
2029 talloc_free(local_ctx
);
2033 static void pwd_timeout_debug(struct tevent_context
*unused1
,
2034 struct tevent_timer
*unused2
,
2035 struct timeval unused3
,
2038 DEBUG(0, ("WARNING: check_password_complexity: password script "
2039 "took more than 1 second to run\n"));
2044 * Performs checks on a user password (plaintext UNIX format - attribute
2045 * "password"). The remaining parameters have to be extracted from the domain
2048 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
2050 enum samr_ValidationStatus
samdb_check_password(TALLOC_CTX
*mem_ctx
,
2051 struct loadparm_context
*lp_ctx
,
2052 const char *account_name
,
2053 const char *user_principal_name
,
2054 const char *full_name
,
2055 const DATA_BLOB
*utf8_blob
,
2056 const uint32_t pwdProperties
,
2057 const uint32_t minPwdLength
)
2059 const struct loadparm_substitution
*lp_sub
=
2060 lpcfg_noop_substitution();
2061 char *password_script
= NULL
;
2062 const char *utf8_pw
= (const char *)utf8_blob
->data
;
2065 * This looks strange because it is.
2067 * The check for the number of characters in the password
2068 * should clearly not be against the byte length, or else a
2069 * single UTF8 character would count for more than one.
2071 * We have chosen to use the number of 16-bit units that the
2072 * password encodes to as the measure of length. This is not
2073 * the same as the number of codepoints, if a password
2074 * contains a character beyond the Basic Multilingual Plane
2075 * (above 65535) it will count for more than one "character".
2078 size_t password_characters_roughly
= strlen_m(utf8_pw
);
2080 /* checks if the "minPwdLength" property is satisfied */
2081 if (minPwdLength
> password_characters_roughly
) {
2082 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT
;
2085 /* We might not be asked to check the password complexity */
2086 if (!(pwdProperties
& DOMAIN_PASSWORD_COMPLEX
)) {
2087 return SAMR_VALIDATION_STATUS_SUCCESS
;
2090 if (password_characters_roughly
== 0) {
2091 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH
;
2094 password_script
= lpcfg_check_password_script(lp_ctx
, lp_sub
, mem_ctx
);
2095 if (password_script
!= NULL
&& *password_script
!= '\0') {
2098 ssize_t nwritten
= 0;
2099 struct tevent_context
*event_ctx
= NULL
;
2100 struct tevent_req
*req
= NULL
;
2102 const char * const cmd
[4] = {
2108 event_ctx
= tevent_context_init(mem_ctx
);
2109 if (event_ctx
== NULL
) {
2110 TALLOC_FREE(password_script
);
2111 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR
;
2114 /* Gives a warning after 1 second, terminates after 10 */
2115 tevent_add_timer(event_ctx
, event_ctx
,
2116 tevent_timeval_current_ofs(1, 0),
2117 pwd_timeout_debug
, NULL
);
2119 check_ret
= setenv("SAMBA_CPS_ACCOUNT_NAME", account_name
, 1);
2120 if (check_ret
!= 0) {
2121 TALLOC_FREE(password_script
);
2122 TALLOC_FREE(event_ctx
);
2123 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR
;
2125 if (user_principal_name
!= NULL
) {
2126 check_ret
= setenv("SAMBA_CPS_USER_PRINCIPAL_NAME",
2127 user_principal_name
, 1);
2129 unsetenv("SAMBA_CPS_USER_PRINCIPAL_NAME");
2131 if (check_ret
!= 0) {
2132 TALLOC_FREE(password_script
);
2133 TALLOC_FREE(event_ctx
);
2134 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR
;
2136 if (full_name
!= NULL
) {
2137 check_ret
= setenv("SAMBA_CPS_FULL_NAME", full_name
, 1);
2139 unsetenv("SAMBA_CPS_FULL_NAME");
2141 if (check_ret
!= 0) {
2142 TALLOC_FREE(password_script
);
2143 TALLOC_FREE(event_ctx
);
2144 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR
;
2147 req
= samba_runcmd_send(event_ctx
, event_ctx
,
2148 tevent_timeval_current_ofs(10, 0),
2149 100, 100, cmd
, NULL
);
2150 unsetenv("SAMBA_CPS_ACCOUNT_NAME");
2151 unsetenv("SAMBA_CPS_USER_PRINCIPAL_NAME");
2152 unsetenv("SAMBA_CPS_FULL_NAME");
2154 TALLOC_FREE(password_script
);
2155 TALLOC_FREE(event_ctx
);
2156 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR
;
2159 cps_stdin
= samba_runcmd_export_stdin(req
);
2161 nwritten
= write_data(
2162 cps_stdin
, utf8_blob
->data
, utf8_blob
->length
);
2163 if (nwritten
== -1) {
2165 TALLOC_FREE(password_script
);
2166 TALLOC_FREE(event_ctx
);
2167 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR
;
2172 if (!tevent_req_poll(req
, event_ctx
)) {
2173 TALLOC_FREE(password_script
);
2174 TALLOC_FREE(event_ctx
);
2175 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR
;
2178 check_ret
= samba_runcmd_recv(req
, &error
);
2179 TALLOC_FREE(event_ctx
);
2181 if (error
== ETIMEDOUT
) {
2182 DEBUG(0, ("check_password_complexity: check password script took too long!\n"));
2183 TALLOC_FREE(password_script
);
2184 return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR
;
2186 DEBUG(5,("check_password_complexity: check password script (%s) "
2187 "returned [%d]\n", password_script
, check_ret
));
2189 if (check_ret
!= 0) {
2190 DEBUG(1,("check_password_complexity: "
2191 "check password script said new password is not good "
2193 TALLOC_FREE(password_script
);
2194 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH
;
2197 TALLOC_FREE(password_script
);
2198 return SAMR_VALIDATION_STATUS_SUCCESS
;
2201 TALLOC_FREE(password_script
);
2204 * Here are the standard AD password quality rules, which we
2205 * run after the script.
2208 if (!check_password_quality(utf8_pw
)) {
2209 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH
;
2212 return SAMR_VALIDATION_STATUS_SUCCESS
;
2216 * Callback for "samdb_set_password" password change
2218 int samdb_set_password_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
2223 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
2226 if (ares
->error
!= LDB_SUCCESS
) {
2228 req
->context
= talloc_steal(req
,
2229 ldb_reply_get_control(ares
, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
));
2231 return ldb_request_done(req
, ret
);
2234 if (ares
->type
!= LDB_REPLY_DONE
) {
2236 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
2239 req
->context
= talloc_steal(req
,
2240 ldb_reply_get_control(ares
, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
));
2242 return ldb_request_done(req
, LDB_SUCCESS
);
2246 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2247 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2248 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2249 * user change or not. The "rejectReason" gives some more information if the
2252 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2253 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2255 static NTSTATUS
samdb_set_password_internal(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2256 struct ldb_dn
*user_dn
, struct ldb_dn
*domain_dn
,
2257 const DATA_BLOB
*new_password
,
2258 const struct samr_Password
*lmNewHash
,
2259 const struct samr_Password
*ntNewHash
,
2260 const struct samr_Password
*lmOldHash
,
2261 const struct samr_Password
*ntOldHash
,
2262 enum samPwdChangeReason
*reject_reason
,
2263 struct samr_DomInfo1
**_dominfo
,
2264 bool permit_interdomain_trust
)
2266 struct ldb_message
*msg
;
2267 struct ldb_message_element
*el
;
2268 struct ldb_request
*req
;
2269 struct dsdb_control_password_change_status
*pwd_stat
= NULL
;
2271 bool hash_values
= false;
2272 NTSTATUS status
= NT_STATUS_OK
;
2274 #define CHECK_RET(x) \
2275 if (x != LDB_SUCCESS) { \
2277 return NT_STATUS_NO_MEMORY; \
2280 msg
= ldb_msg_new(mem_ctx
);
2282 return NT_STATUS_NO_MEMORY
;
2285 if ((new_password
!= NULL
)
2286 && ((lmNewHash
== NULL
) && (ntNewHash
== NULL
))) {
2287 /* we have the password as plaintext UTF16 */
2288 CHECK_RET(ldb_msg_add_value(msg
, "clearTextPassword",
2289 new_password
, NULL
));
2290 el
= ldb_msg_find_element(msg
, "clearTextPassword");
2291 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2292 } else if ((new_password
== NULL
)
2293 && ((lmNewHash
!= NULL
) || (ntNewHash
!= NULL
))) {
2294 /* we have a password as LM and/or NT hash */
2295 if (lmNewHash
!= NULL
) {
2296 CHECK_RET(samdb_msg_add_hash(ldb
, mem_ctx
, msg
,
2297 "dBCSPwd", lmNewHash
));
2298 el
= ldb_msg_find_element(msg
, "dBCSPwd");
2299 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2301 if (ntNewHash
!= NULL
) {
2302 CHECK_RET(samdb_msg_add_hash(ldb
, mem_ctx
, msg
,
2303 "unicodePwd", ntNewHash
));
2304 el
= ldb_msg_find_element(msg
, "unicodePwd");
2305 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2309 /* the password wasn't specified correctly */
2311 return NT_STATUS_INVALID_PARAMETER
;
2314 /* build modify request */
2315 ret
= ldb_build_mod_req(&req
, ldb
, mem_ctx
, msg
, NULL
, NULL
,
2316 samdb_set_password_callback
, NULL
);
2317 if (ret
!= LDB_SUCCESS
) {
2319 return NT_STATUS_NO_MEMORY
;
2322 /* A password change operation */
2323 if ((ntOldHash
!= NULL
) || (lmOldHash
!= NULL
)) {
2324 struct dsdb_control_password_change
*change
;
2326 change
= talloc(req
, struct dsdb_control_password_change
);
2327 if (change
== NULL
) {
2330 return NT_STATUS_NO_MEMORY
;
2333 change
->old_nt_pwd_hash
= ntOldHash
;
2334 change
->old_lm_pwd_hash
= lmOldHash
;
2336 ret
= ldb_request_add_control(req
,
2337 DSDB_CONTROL_PASSWORD_CHANGE_OID
,
2339 if (ret
!= LDB_SUCCESS
) {
2342 return NT_STATUS_NO_MEMORY
;
2346 ret
= ldb_request_add_control(req
,
2347 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID
,
2349 if (ret
!= LDB_SUCCESS
) {
2352 return NT_STATUS_NO_MEMORY
;
2355 if (permit_interdomain_trust
) {
2356 ret
= ldb_request_add_control(req
,
2357 DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID
,
2359 if (ret
!= LDB_SUCCESS
) {
2362 return NT_STATUS_NO_MEMORY
;
2365 ret
= ldb_request_add_control(req
,
2366 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
,
2368 if (ret
!= LDB_SUCCESS
) {
2371 return NT_STATUS_NO_MEMORY
;
2374 ret
= dsdb_autotransaction_request(ldb
, req
);
2376 if (req
->context
!= NULL
) {
2377 struct ldb_control
*control
= talloc_get_type_abort(req
->context
,
2378 struct ldb_control
);
2379 pwd_stat
= talloc_get_type_abort(control
->data
,
2380 struct dsdb_control_password_change_status
);
2381 talloc_steal(mem_ctx
, pwd_stat
);
2387 /* Sets the domain info (if requested) */
2388 if (_dominfo
!= NULL
) {
2389 struct samr_DomInfo1
*dominfo
;
2391 dominfo
= talloc_zero(mem_ctx
, struct samr_DomInfo1
);
2392 if (dominfo
== NULL
) {
2393 return NT_STATUS_NO_MEMORY
;
2396 if (pwd_stat
!= NULL
) {
2397 dominfo
->min_password_length
= pwd_stat
->domain_data
.minPwdLength
;
2398 dominfo
->password_properties
= pwd_stat
->domain_data
.pwdProperties
;
2399 dominfo
->password_history_length
= pwd_stat
->domain_data
.pwdHistoryLength
;
2400 dominfo
->max_password_age
= pwd_stat
->domain_data
.maxPwdAge
;
2401 dominfo
->min_password_age
= pwd_stat
->domain_data
.minPwdAge
;
2404 *_dominfo
= dominfo
;
2407 if (reject_reason
!= NULL
) {
2408 if (pwd_stat
!= NULL
) {
2409 *reject_reason
= pwd_stat
->reject_reason
;
2411 *reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
2415 if (pwd_stat
!= NULL
) {
2416 talloc_free(pwd_stat
);
2419 if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
2420 const char *errmsg
= ldb_errstring(ldb
);
2421 char *endptr
= NULL
;
2422 WERROR werr
= WERR_GEN_FAILURE
;
2423 status
= NT_STATUS_UNSUCCESSFUL
;
2424 if (errmsg
!= NULL
) {
2425 werr
= W_ERROR(strtol(errmsg
, &endptr
, 16));
2426 DBG_WARNING("%s\n", errmsg
);
2428 if (endptr
!= errmsg
) {
2429 if (W_ERROR_EQUAL(werr
, WERR_INVALID_PASSWORD
)) {
2430 status
= NT_STATUS_WRONG_PASSWORD
;
2432 if (W_ERROR_EQUAL(werr
, WERR_PASSWORD_RESTRICTION
)) {
2433 status
= NT_STATUS_PASSWORD_RESTRICTION
;
2436 } else if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
2437 /* don't let the caller know if an account doesn't exist */
2438 status
= NT_STATUS_WRONG_PASSWORD
;
2439 } else if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
2440 status
= NT_STATUS_ACCESS_DENIED
;
2441 } else if (ret
!= LDB_SUCCESS
) {
2442 DEBUG(1, ("Failed to set password on %s: %s\n",
2443 ldb_dn_get_linearized(user_dn
),
2444 ldb_errstring(ldb
)));
2445 status
= NT_STATUS_UNSUCCESSFUL
;
2451 NTSTATUS
samdb_set_password(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2452 struct ldb_dn
*user_dn
, struct ldb_dn
*domain_dn
,
2453 const DATA_BLOB
*new_password
,
2454 const struct samr_Password
*lmNewHash
,
2455 const struct samr_Password
*ntNewHash
,
2456 const struct samr_Password
*lmOldHash
,
2457 const struct samr_Password
*ntOldHash
,
2458 enum samPwdChangeReason
*reject_reason
,
2459 struct samr_DomInfo1
**_dominfo
)
2461 return samdb_set_password_internal(ldb
, mem_ctx
,
2464 lmNewHash
, ntNewHash
,
2465 lmOldHash
, ntOldHash
,
2466 reject_reason
, _dominfo
,
2467 false); /* reject trusts */
2471 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2472 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2473 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2474 * user change or not. The "rejectReason" gives some more information if the
2477 * This wrapper function for "samdb_set_password" takes a SID as input rather
2480 * This call encapsulates a new LDB transaction for changing the password;
2481 * therefore the user hasn't to start a new one.
2483 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2484 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2485 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2486 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2488 NTSTATUS
samdb_set_password_sid(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2489 const struct dom_sid
*user_sid
,
2490 const uint32_t *new_version
, /* optional for trusts */
2491 const DATA_BLOB
*new_password
,
2492 const struct samr_Password
*lmNewHash
,
2493 const struct samr_Password
*ntNewHash
,
2494 const struct samr_Password
*lmOldHash
,
2495 const struct samr_Password
*ntOldHash
,
2496 enum samPwdChangeReason
*reject_reason
,
2497 struct samr_DomInfo1
**_dominfo
)
2499 TALLOC_CTX
*frame
= talloc_stackframe();
2501 const char * const user_attrs
[] = {
2502 "userAccountControl",
2506 struct ldb_message
*user_msg
= NULL
;
2510 ret
= ldb_transaction_start(ldb
);
2511 if (ret
!= LDB_SUCCESS
) {
2512 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb
)));
2514 return NT_STATUS_TRANSACTION_ABORTED
;
2517 ret
= dsdb_search_one(ldb
, frame
, &user_msg
, ldb_get_default_basedn(ldb
),
2518 LDB_SCOPE_SUBTREE
, user_attrs
, 0,
2519 "(&(objectSid=%s)(objectClass=user))",
2520 ldap_encode_ndr_dom_sid(frame
, user_sid
));
2521 if (ret
!= LDB_SUCCESS
) {
2522 ldb_transaction_cancel(ldb
);
2523 DEBUG(3, ("samdb_set_password_sid: SID[%s] not found in samdb %s - %s, "
2524 "returning NO_SUCH_USER\n",
2525 dom_sid_string(frame
, user_sid
),
2526 ldb_strerror(ret
), ldb_errstring(ldb
)));
2528 return NT_STATUS_NO_SUCH_USER
;
2531 uac
= ldb_msg_find_attr_as_uint(user_msg
, "userAccountControl", 0);
2532 if (!(uac
& UF_ACCOUNT_TYPE_MASK
)) {
2533 ldb_transaction_cancel(ldb
);
2534 DEBUG(1, ("samdb_set_password_sid: invalid "
2535 "userAccountControl[0x%08X] for SID[%s] DN[%s], "
2536 "returning NO_SUCH_USER\n",
2537 (unsigned)uac
, dom_sid_string(frame
, user_sid
),
2538 ldb_dn_get_linearized(user_msg
->dn
)));
2540 return NT_STATUS_NO_SUCH_USER
;
2543 if (uac
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
2544 const char * const tdo_attrs
[] = {
2545 "trustAuthIncoming",
2549 struct ldb_message
*tdo_msg
= NULL
;
2550 const char *account_name
= NULL
;
2551 uint32_t trust_direction
;
2553 const struct ldb_val
*old_val
= NULL
;
2554 struct trustAuthInOutBlob old_blob
= {
2557 uint32_t old_version
= 0;
2558 struct AuthenticationInformation
*old_version_a
= NULL
;
2559 uint32_t _new_version
= 0;
2560 struct trustAuthInOutBlob new_blob
= {
2563 struct ldb_val new_val
= {
2566 struct timeval tv
= timeval_current();
2567 NTTIME now
= timeval_to_nttime(&tv
);
2568 enum ndr_err_code ndr_err
;
2570 if (new_password
== NULL
&& ntNewHash
== NULL
) {
2571 ldb_transaction_cancel(ldb
);
2572 DEBUG(1, ("samdb_set_password_sid: "
2573 "no new password provided "
2574 "sAMAccountName for SID[%s] DN[%s], "
2575 "returning INVALID_PARAMETER\n",
2576 dom_sid_string(frame
, user_sid
),
2577 ldb_dn_get_linearized(user_msg
->dn
)));
2579 return NT_STATUS_INVALID_PARAMETER
;
2582 if (new_password
!= NULL
&& ntNewHash
!= NULL
) {
2583 ldb_transaction_cancel(ldb
);
2584 DEBUG(1, ("samdb_set_password_sid: "
2585 "two new passwords provided "
2586 "sAMAccountName for SID[%s] DN[%s], "
2587 "returning INVALID_PARAMETER\n",
2588 dom_sid_string(frame
, user_sid
),
2589 ldb_dn_get_linearized(user_msg
->dn
)));
2591 return NT_STATUS_INVALID_PARAMETER
;
2594 if (new_password
!= NULL
&& (new_password
->length
% 2)) {
2595 ldb_transaction_cancel(ldb
);
2596 DEBUG(2, ("samdb_set_password_sid: "
2597 "invalid utf16 length (%zu) "
2598 "sAMAccountName for SID[%s] DN[%s], "
2599 "returning WRONG_PASSWORD\n",
2600 new_password
->length
,
2601 dom_sid_string(frame
, user_sid
),
2602 ldb_dn_get_linearized(user_msg
->dn
)));
2604 return NT_STATUS_WRONG_PASSWORD
;
2607 if (new_password
!= NULL
&& new_password
->length
>= 500) {
2608 ldb_transaction_cancel(ldb
);
2609 DEBUG(2, ("samdb_set_password_sid: "
2610 "utf16 password too long (%zu) "
2611 "sAMAccountName for SID[%s] DN[%s], "
2612 "returning WRONG_PASSWORD\n",
2613 new_password
->length
,
2614 dom_sid_string(frame
, user_sid
),
2615 ldb_dn_get_linearized(user_msg
->dn
)));
2617 return NT_STATUS_WRONG_PASSWORD
;
2620 account_name
= ldb_msg_find_attr_as_string(user_msg
,
2621 "sAMAccountName", NULL
);
2622 if (account_name
== NULL
) {
2623 ldb_transaction_cancel(ldb
);
2624 DEBUG(1, ("samdb_set_password_sid: missing "
2625 "sAMAccountName for SID[%s] DN[%s], "
2626 "returning NO_SUCH_USER\n",
2627 dom_sid_string(frame
, user_sid
),
2628 ldb_dn_get_linearized(user_msg
->dn
)));
2630 return NT_STATUS_NO_SUCH_USER
;
2633 nt_status
= dsdb_trust_search_tdo_by_type(ldb
,
2638 if (!NT_STATUS_IS_OK(nt_status
)) {
2639 ldb_transaction_cancel(ldb
);
2640 DEBUG(1, ("samdb_set_password_sid: dsdb_trust_search_tdo "
2641 "failed(%s) for sAMAccountName[%s] SID[%s] DN[%s], "
2642 "returning INTERNAL_DB_CORRUPTION\n",
2643 nt_errstr(nt_status
), account_name
,
2644 dom_sid_string(frame
, user_sid
),
2645 ldb_dn_get_linearized(user_msg
->dn
)));
2647 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2650 trust_direction
= ldb_msg_find_attr_as_int(tdo_msg
,
2651 "trustDirection", 0);
2652 if (!(trust_direction
& LSA_TRUST_DIRECTION_INBOUND
)) {
2653 ldb_transaction_cancel(ldb
);
2654 DEBUG(1, ("samdb_set_password_sid: direction[0x%08X] is "
2655 "not inbound for sAMAccountName[%s] "
2657 "returning INTERNAL_DB_CORRUPTION\n",
2658 (unsigned)trust_direction
,
2660 ldb_dn_get_linearized(user_msg
->dn
),
2661 ldb_dn_get_linearized(tdo_msg
->dn
)));
2663 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2666 old_val
= ldb_msg_find_ldb_val(tdo_msg
, "trustAuthIncoming");
2667 if (old_val
!= NULL
) {
2668 ndr_err
= ndr_pull_struct_blob(old_val
, frame
, &old_blob
,
2669 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
2670 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2671 ldb_transaction_cancel(ldb
);
2672 DEBUG(1, ("samdb_set_password_sid: "
2673 "failed(%s) to parse "
2674 "trustAuthOutgoing sAMAccountName[%s] "
2676 "returning INTERNAL_DB_CORRUPTION\n",
2677 ndr_map_error2string(ndr_err
),
2679 ldb_dn_get_linearized(user_msg
->dn
),
2680 ldb_dn_get_linearized(tdo_msg
->dn
)));
2683 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2687 for (i
= old_blob
.current
.count
; i
> 0; i
--) {
2688 struct AuthenticationInformation
*a
=
2689 &old_blob
.current
.array
[i
- 1];
2691 switch (a
->AuthType
) {
2692 case TRUST_AUTH_TYPE_NONE
:
2693 if (i
== old_blob
.current
.count
) {
2695 * remove TRUST_AUTH_TYPE_NONE at the
2698 old_blob
.current
.count
--;
2702 case TRUST_AUTH_TYPE_VERSION
:
2704 old_version
= a
->AuthInfo
.version
.version
;
2707 case TRUST_AUTH_TYPE_CLEAR
:
2710 case TRUST_AUTH_TYPE_NT4OWF
:
2715 if (new_version
== NULL
) {
2717 new_version
= &_new_version
;
2720 if (old_version_a
!= NULL
&& *new_version
!= (old_version
+ 1)) {
2721 old_version_a
->LastUpdateTime
= now
;
2722 old_version_a
->AuthType
= TRUST_AUTH_TYPE_NONE
;
2725 new_blob
.count
= MAX(old_blob
.current
.count
, 2);
2726 new_blob
.current
.array
= talloc_zero_array(frame
,
2727 struct AuthenticationInformation
,
2729 if (new_blob
.current
.array
== NULL
) {
2730 ldb_transaction_cancel(ldb
);
2732 return NT_STATUS_NO_MEMORY
;
2734 new_blob
.previous
.array
= talloc_zero_array(frame
,
2735 struct AuthenticationInformation
,
2737 if (new_blob
.current
.array
== NULL
) {
2738 ldb_transaction_cancel(ldb
);
2740 return NT_STATUS_NO_MEMORY
;
2743 for (i
= 0; i
< old_blob
.current
.count
; i
++) {
2744 struct AuthenticationInformation
*o
=
2745 &old_blob
.current
.array
[i
];
2746 struct AuthenticationInformation
*p
=
2747 &new_blob
.previous
.array
[i
];
2750 new_blob
.previous
.count
++;
2752 for (; i
< new_blob
.count
; i
++) {
2753 struct AuthenticationInformation
*pi
=
2754 &new_blob
.previous
.array
[i
];
2758 * new_blob.previous is still empty so
2759 * we'll do new_blob.previous = new_blob.current
2765 pi
->LastUpdateTime
= now
;
2766 pi
->AuthType
= TRUST_AUTH_TYPE_NONE
;
2767 new_blob
.previous
.count
++;
2770 for (i
= 0; i
< new_blob
.count
; i
++) {
2771 struct AuthenticationInformation
*ci
=
2772 &new_blob
.current
.array
[i
];
2774 ci
->LastUpdateTime
= now
;
2777 if (ntNewHash
!= NULL
) {
2778 ci
->AuthType
= TRUST_AUTH_TYPE_NT4OWF
;
2779 ci
->AuthInfo
.nt4owf
.password
= *ntNewHash
;
2783 ci
->AuthType
= TRUST_AUTH_TYPE_CLEAR
;
2784 ci
->AuthInfo
.clear
.size
= new_password
->length
;
2785 ci
->AuthInfo
.clear
.password
= new_password
->data
;
2788 ci
->AuthType
= TRUST_AUTH_TYPE_VERSION
;
2789 ci
->AuthInfo
.version
.version
= *new_version
;
2792 ci
->AuthType
= TRUST_AUTH_TYPE_NONE
;
2796 new_blob
.current
.count
++;
2799 if (new_blob
.previous
.count
== 0) {
2800 TALLOC_FREE(new_blob
.previous
.array
);
2801 new_blob
.previous
= new_blob
.current
;
2804 ndr_err
= ndr_push_struct_blob(&new_val
, frame
, &new_blob
,
2805 (ndr_push_flags_fn_t
)ndr_push_trustAuthInOutBlob
);
2806 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2807 ldb_transaction_cancel(ldb
);
2808 DEBUG(1, ("samdb_set_password_sid: "
2809 "failed(%s) to generate "
2810 "trustAuthOutgoing sAMAccountName[%s] "
2812 "returning UNSUCCESSFUL\n",
2813 ndr_map_error2string(ndr_err
),
2815 ldb_dn_get_linearized(user_msg
->dn
),
2816 ldb_dn_get_linearized(tdo_msg
->dn
)));
2818 return NT_STATUS_UNSUCCESSFUL
;
2821 tdo_msg
->num_elements
= 0;
2822 TALLOC_FREE(tdo_msg
->elements
);
2824 ret
= ldb_msg_add_empty(tdo_msg
, "trustAuthIncoming",
2825 LDB_FLAG_MOD_REPLACE
, NULL
);
2826 if (ret
!= LDB_SUCCESS
) {
2827 ldb_transaction_cancel(ldb
);
2829 return NT_STATUS_NO_MEMORY
;
2831 ret
= ldb_msg_add_value(tdo_msg
, "trustAuthIncoming",
2833 if (ret
!= LDB_SUCCESS
) {
2834 ldb_transaction_cancel(ldb
);
2836 return NT_STATUS_NO_MEMORY
;
2839 ret
= ldb_modify(ldb
, tdo_msg
);
2840 if (ret
!= LDB_SUCCESS
) {
2841 nt_status
= dsdb_ldb_err_to_ntstatus(ret
);
2842 ldb_transaction_cancel(ldb
);
2843 DEBUG(1, ("samdb_set_password_sid: "
2844 "failed to replace "
2845 "trustAuthOutgoing sAMAccountName[%s] "
2849 ldb_dn_get_linearized(user_msg
->dn
),
2850 ldb_dn_get_linearized(tdo_msg
->dn
),
2851 nt_errstr(nt_status
), ldb_errstring(ldb
)));
2857 nt_status
= samdb_set_password_internal(ldb
, mem_ctx
,
2860 lmNewHash
, ntNewHash
,
2861 lmOldHash
, ntOldHash
,
2862 reject_reason
, _dominfo
,
2863 true); /* permit trusts */
2864 if (!NT_STATUS_IS_OK(nt_status
)) {
2865 ldb_transaction_cancel(ldb
);
2870 ret
= ldb_transaction_commit(ldb
);
2871 if (ret
!= LDB_SUCCESS
) {
2872 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2873 ldb_dn_get_linearized(user_msg
->dn
),
2874 ldb_errstring(ldb
)));
2876 return NT_STATUS_TRANSACTION_ABORTED
;
2880 return NT_STATUS_OK
;
2884 NTSTATUS
samdb_create_foreign_security_principal(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
2885 struct dom_sid
*sid
, struct ldb_dn
**ret_dn
)
2887 struct ldb_message
*msg
;
2888 struct ldb_dn
*basedn
= NULL
;
2892 sidstr
= dom_sid_string(mem_ctx
, sid
);
2893 NT_STATUS_HAVE_NO_MEMORY(sidstr
);
2895 /* We might have to create a ForeignSecurityPrincipal, even if this user
2896 * is in our own domain */
2898 msg
= ldb_msg_new(sidstr
);
2900 talloc_free(sidstr
);
2901 return NT_STATUS_NO_MEMORY
;
2904 ret
= dsdb_wellknown_dn(sam_ctx
, sidstr
,
2905 ldb_get_default_basedn(sam_ctx
),
2906 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER
,
2908 if (ret
!= LDB_SUCCESS
) {
2909 DEBUG(0, ("Failed to find DN for "
2910 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx
)));
2911 talloc_free(sidstr
);
2912 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2915 /* add core elements to the ldb_message for the alias */
2917 if ( ! ldb_dn_add_child_fmt(msg
->dn
, "CN=%s", sidstr
)) {
2918 talloc_free(sidstr
);
2919 return NT_STATUS_NO_MEMORY
;
2922 ret
= ldb_msg_add_string(msg
, "objectClass",
2923 "foreignSecurityPrincipal");
2924 if (ret
!= LDB_SUCCESS
) {
2925 talloc_free(sidstr
);
2926 return NT_STATUS_NO_MEMORY
;
2929 /* create the alias */
2930 ret
= ldb_add(sam_ctx
, msg
);
2931 if (ret
!= LDB_SUCCESS
) {
2932 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2934 ldb_dn_get_linearized(msg
->dn
),
2935 ldb_errstring(sam_ctx
)));
2936 talloc_free(sidstr
);
2937 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2940 *ret_dn
= talloc_steal(mem_ctx
, msg
->dn
);
2941 talloc_free(sidstr
);
2943 return NT_STATUS_OK
;
2948 Find the DN of a domain, assuming it to be a dotted.dns name
2951 struct ldb_dn
*samdb_dns_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char *dns_domain
)
2954 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2955 const char *binary_encoded
;
2956 const char * const *split_realm
;
2963 split_realm
= (const char * const *)str_list_make(tmp_ctx
, dns_domain
, ".");
2965 talloc_free(tmp_ctx
);
2968 dn
= ldb_dn_new(mem_ctx
, ldb
, NULL
);
2969 for (i
=0; split_realm
[i
]; i
++) {
2970 binary_encoded
= ldb_binary_encode_string(tmp_ctx
, split_realm
[i
]);
2971 if (!ldb_dn_add_base_fmt(dn
, "dc=%s", binary_encoded
)) {
2972 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2973 binary_encoded
, ldb_dn_get_linearized(dn
)));
2974 talloc_free(tmp_ctx
);
2978 if (!ldb_dn_validate(dn
)) {
2979 DEBUG(2, ("Failed to validated DN %s\n",
2980 ldb_dn_get_linearized(dn
)));
2981 talloc_free(tmp_ctx
);
2984 talloc_free(tmp_ctx
);
2990 Find the DNS equivalent of a DN, in dotted DNS form
2992 char *samdb_dn_to_dns_domain(TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
)
2994 int i
, num_components
= ldb_dn_get_comp_num(dn
);
2995 char *dns_name
= talloc_strdup(mem_ctx
, "");
2996 if (dns_name
== NULL
) {
3000 for (i
=0; i
<num_components
; i
++) {
3001 const struct ldb_val
*v
= ldb_dn_get_component_val(dn
, i
);
3004 talloc_free(dns_name
);
3007 s
= talloc_asprintf_append_buffer(dns_name
, "%*.*s.",
3008 (int)v
->length
, (int)v
->length
, (char *)v
->data
);
3010 talloc_free(dns_name
);
3016 /* remove the last '.' */
3017 if (dns_name
[0] != 0) {
3018 dns_name
[strlen(dns_name
)-1] = 0;
3025 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
3026 name is based on the forest DNS name
3028 char *samdb_ntds_msdcs_dns_name(struct ldb_context
*samdb
,
3029 TALLOC_CTX
*mem_ctx
,
3030 const struct GUID
*ntds_guid
)
3032 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3033 const char *guid_str
;
3034 struct ldb_dn
*forest_dn
;
3035 const char *dnsforest
;
3038 guid_str
= GUID_string(tmp_ctx
, ntds_guid
);
3039 if (guid_str
== NULL
) {
3040 talloc_free(tmp_ctx
);
3043 forest_dn
= ldb_get_root_basedn(samdb
);
3044 if (forest_dn
== NULL
) {
3045 talloc_free(tmp_ctx
);
3048 dnsforest
= samdb_dn_to_dns_domain(tmp_ctx
, forest_dn
);
3049 if (dnsforest
== NULL
) {
3050 talloc_free(tmp_ctx
);
3053 ret
= talloc_asprintf(mem_ctx
, "%s._msdcs.%s", guid_str
, dnsforest
);
3054 talloc_free(tmp_ctx
);
3060 Find the DN of a domain, be it the netbios or DNS name
3062 struct ldb_dn
*samdb_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
3063 const char *domain_name
)
3065 const char * const domain_ref_attrs
[] = {
3068 const char * const domain_ref2_attrs
[] = {
3071 struct ldb_result
*res_domain_ref
;
3072 char *escaped_domain
= ldb_binary_encode_string(mem_ctx
, domain_name
);
3073 /* find the domain's DN */
3074 int ret_domain
= ldb_search(ldb
, mem_ctx
,
3076 samdb_partitions_dn(ldb
, mem_ctx
),
3079 "(&(nETBIOSName=%s)(objectclass=crossRef))",
3081 if (ret_domain
!= LDB_SUCCESS
) {
3085 if (res_domain_ref
->count
== 0) {
3086 ret_domain
= ldb_search(ldb
, mem_ctx
,
3088 samdb_dns_domain_to_dn(ldb
, mem_ctx
, domain_name
),
3091 "(objectclass=domain)");
3092 if (ret_domain
!= LDB_SUCCESS
) {
3096 if (res_domain_ref
->count
== 1) {
3097 return res_domain_ref
->msgs
[0]->dn
;
3102 if (res_domain_ref
->count
> 1) {
3103 DEBUG(0,("Found %d records matching domain [%s]\n",
3104 ret_domain
, domain_name
));
3108 return samdb_result_dn(ldb
, mem_ctx
, res_domain_ref
->msgs
[0], "nCName", NULL
);
3114 use a GUID to find a DN
3116 int dsdb_find_dn_by_guid(struct ldb_context
*ldb
,
3117 TALLOC_CTX
*mem_ctx
,
3118 const struct GUID
*guid
,
3119 uint32_t dsdb_flags
,
3123 struct ldb_result
*res
;
3124 const char *attrs
[] = { NULL
};
3125 char *guid_str
= GUID_string(mem_ctx
, guid
);
3128 return ldb_operr(ldb
);
3131 ret
= dsdb_search(ldb
, mem_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, attrs
,
3132 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
|
3133 DSDB_SEARCH_SHOW_EXTENDED_DN
|
3134 DSDB_SEARCH_ONE_ONLY
| dsdb_flags
,
3135 "objectGUID=%s", guid_str
);
3136 talloc_free(guid_str
);
3137 if (ret
!= LDB_SUCCESS
) {
3141 *dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
3148 use a DN to find a GUID with a given attribute name
3150 int dsdb_find_guid_attr_by_dn(struct ldb_context
*ldb
,
3151 struct ldb_dn
*dn
, const char *attribute
,
3155 struct ldb_result
*res
= NULL
;
3156 const char *attrs
[2];
3157 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
3159 attrs
[0] = attribute
;
3162 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
,
3163 DSDB_SEARCH_SHOW_DELETED
|
3164 DSDB_SEARCH_SHOW_RECYCLED
);
3165 if (ret
!= LDB_SUCCESS
) {
3166 talloc_free(tmp_ctx
);
3171 talloc_free(tmp_ctx
);
3172 return LDB_ERR_OTHER
;
3174 if (res
->count
< 1) {
3175 talloc_free(tmp_ctx
);
3176 return ldb_error(ldb
, LDB_ERR_NO_SUCH_OBJECT
, __func__
);
3178 *guid
= samdb_result_guid(res
->msgs
[0], attribute
);
3179 talloc_free(tmp_ctx
);
3184 use a DN to find a GUID
3186 int dsdb_find_guid_by_dn(struct ldb_context
*ldb
,
3187 struct ldb_dn
*dn
, struct GUID
*guid
)
3189 return dsdb_find_guid_attr_by_dn(ldb
, dn
, "objectGUID", guid
);
3195 adds the given GUID to the given ldb_message. This value is added
3196 for the given attr_name (may be either "objectGUID" or "parentGUID").
3198 int dsdb_msg_add_guid(struct ldb_message
*msg
,
3200 const char *attr_name
)
3205 TALLOC_CTX
*tmp_ctx
= talloc_init("dsdb_msg_add_guid");
3207 status
= GUID_to_ndr_blob(guid
, tmp_ctx
, &v
);
3208 if (!NT_STATUS_IS_OK(status
)) {
3209 ret
= LDB_ERR_OPERATIONS_ERROR
;
3213 ret
= ldb_msg_add_steal_value(msg
, attr_name
, &v
);
3214 if (ret
!= LDB_SUCCESS
) {
3215 DEBUG(4,(__location__
": Failed to add %s to the message\n",
3223 talloc_free(tmp_ctx
);
3230 use a DN to find a SID
3232 int dsdb_find_sid_by_dn(struct ldb_context
*ldb
,
3233 struct ldb_dn
*dn
, struct dom_sid
*sid
)
3236 struct ldb_result
*res
= NULL
;
3237 const char *attrs
[] = { "objectSid", NULL
};
3238 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
3243 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
,
3244 DSDB_SEARCH_SHOW_DELETED
|
3245 DSDB_SEARCH_SHOW_RECYCLED
);
3246 if (ret
!= LDB_SUCCESS
) {
3247 talloc_free(tmp_ctx
);
3251 talloc_free(tmp_ctx
);
3252 return LDB_ERR_OTHER
;
3254 if (res
->count
< 1) {
3255 talloc_free(tmp_ctx
);
3256 return ldb_error(ldb
, LDB_ERR_NO_SUCH_OBJECT
, __func__
);
3258 s
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSid");
3260 talloc_free(tmp_ctx
);
3261 return ldb_error(ldb
, LDB_ERR_NO_SUCH_OBJECT
, __func__
);
3264 talloc_free(tmp_ctx
);
3269 use a SID to find a DN
3271 int dsdb_find_dn_by_sid(struct ldb_context
*ldb
,
3272 TALLOC_CTX
*mem_ctx
,
3273 struct dom_sid
*sid
, struct ldb_dn
**dn
)
3276 struct ldb_result
*res
;
3277 const char *attrs
[] = { NULL
};
3278 char *sid_str
= ldap_encode_ndr_dom_sid(mem_ctx
, sid
);
3281 return ldb_operr(ldb
);
3284 ret
= dsdb_search(ldb
, mem_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, attrs
,
3285 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
|
3286 DSDB_SEARCH_SHOW_EXTENDED_DN
|
3287 DSDB_SEARCH_ONE_ONLY
,
3288 "objectSid=%s", sid_str
);
3289 talloc_free(sid_str
);
3290 if (ret
!= LDB_SUCCESS
) {
3294 *dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
3301 load a repsFromTo blob list for a given partition GUID
3302 attr must be "repsFrom" or "repsTo"
3304 WERROR
dsdb_loadreps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
3305 const char *attr
, struct repsFromToBlob
**r
, uint32_t *count
)
3307 const char *attrs
[] = { attr
, NULL
};
3308 struct ldb_result
*res
= NULL
;
3309 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3311 struct ldb_message_element
*el
;
3317 ret
= dsdb_search_dn(sam_ctx
, tmp_ctx
, &res
, dn
, attrs
, 0);
3318 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
3319 /* partition hasn't been replicated yet */
3322 if (ret
!= LDB_SUCCESS
) {
3323 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx
)));
3324 talloc_free(tmp_ctx
);
3325 return WERR_DS_DRA_INTERNAL_ERROR
;
3330 talloc_free(tmp_ctx
);
3331 return WERR_DS_DRA_INTERNAL_ERROR
;
3333 el
= ldb_msg_find_element(res
->msgs
[0], attr
);
3335 /* it's OK to be empty */
3336 talloc_free(tmp_ctx
);
3340 *count
= el
->num_values
;
3341 *r
= talloc_array(mem_ctx
, struct repsFromToBlob
, *count
);
3343 talloc_free(tmp_ctx
);
3344 return WERR_DS_DRA_INTERNAL_ERROR
;
3347 for (i
=0; i
<(*count
); i
++) {
3348 enum ndr_err_code ndr_err
;
3349 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
],
3352 (ndr_pull_flags_fn_t
)ndr_pull_repsFromToBlob
);
3353 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3354 talloc_free(tmp_ctx
);
3355 return WERR_DS_DRA_INTERNAL_ERROR
;
3359 talloc_free(tmp_ctx
);
3365 save the repsFromTo blob list for a given partition GUID
3366 attr must be "repsFrom" or "repsTo"
3368 WERROR
dsdb_savereps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
3369 const char *attr
, struct repsFromToBlob
*r
, uint32_t count
)
3371 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3372 struct ldb_message
*msg
;
3373 struct ldb_message_element
*el
;
3376 msg
= ldb_msg_new(tmp_ctx
);
3378 if (ldb_msg_add_empty(msg
, attr
, LDB_FLAG_MOD_REPLACE
, &el
) != LDB_SUCCESS
) {
3382 el
->values
= talloc_array(msg
, struct ldb_val
, count
);
3387 for (i
=0; i
<count
; i
++) {
3389 enum ndr_err_code ndr_err
;
3391 ndr_err
= ndr_push_struct_blob(&v
, tmp_ctx
,
3393 (ndr_push_flags_fn_t
)ndr_push_repsFromToBlob
);
3394 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3402 if (dsdb_modify(sam_ctx
, msg
, 0) != LDB_SUCCESS
) {
3403 DEBUG(0,("Failed to store %s - %s\n", attr
, ldb_errstring(sam_ctx
)));
3407 talloc_free(tmp_ctx
);
3412 talloc_free(tmp_ctx
);
3413 return WERR_DS_DRA_INTERNAL_ERROR
;
3418 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
3419 object for a partition
3421 int dsdb_load_partition_usn(struct ldb_context
*ldb
, struct ldb_dn
*dn
,
3422 uint64_t *uSN
, uint64_t *urgent_uSN
)
3424 struct ldb_request
*req
;
3426 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
3427 struct dsdb_control_current_partition
*p_ctrl
;
3428 struct ldb_result
*res
;
3430 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
3432 talloc_free(tmp_ctx
);
3433 return ldb_oom(ldb
);
3436 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
3437 ldb_dn_new(tmp_ctx
, ldb
, "@REPLCHANGED"),
3441 res
, ldb_search_default_callback
,
3443 if (ret
!= LDB_SUCCESS
) {
3444 talloc_free(tmp_ctx
);
3448 p_ctrl
= talloc(req
, struct dsdb_control_current_partition
);
3449 if (p_ctrl
== NULL
) {
3450 talloc_free(tmp_ctx
);
3451 return ldb_oom(ldb
);
3453 p_ctrl
->version
= DSDB_CONTROL_CURRENT_PARTITION_VERSION
;
3456 ret
= ldb_request_add_control(req
,
3457 DSDB_CONTROL_CURRENT_PARTITION_OID
,
3459 if (ret
!= LDB_SUCCESS
) {
3460 talloc_free(tmp_ctx
);
3464 /* Run the new request */
3465 ret
= ldb_request(ldb
, req
);
3467 if (ret
== LDB_SUCCESS
) {
3468 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
3471 if (ret
== LDB_ERR_NO_SUCH_OBJECT
|| ret
== LDB_ERR_INVALID_DN_SYNTAX
) {
3472 /* it hasn't been created yet, which means
3473 an implicit value of zero */
3475 talloc_free(tmp_ctx
);
3479 if (ret
!= LDB_SUCCESS
) {
3480 talloc_free(tmp_ctx
);
3484 if (res
->count
< 1) {
3490 *uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNHighest", 0);
3492 *urgent_uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNUrgent", 0);
3496 talloc_free(tmp_ctx
);
3501 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2
*c1
,
3502 const struct drsuapi_DsReplicaCursor2
*c2
)
3504 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
3507 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor
*c1
,
3508 const struct drsuapi_DsReplicaCursor
*c2
)
3510 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
3515 see if a computer identified by its invocationId is a RODC
3517 int samdb_is_rodc(struct ldb_context
*sam_ctx
, const struct GUID
*objectGUID
, bool *is_rodc
)
3519 /* 1) find the DN for this servers NTDSDSA object
3520 2) search for the msDS-isRODC attribute
3521 3) if not present then not a RODC
3522 4) if present and TRUE then is a RODC
3524 struct ldb_dn
*config_dn
;
3525 const char *attrs
[] = { "msDS-isRODC", NULL
};
3527 struct ldb_result
*res
;
3528 TALLOC_CTX
*tmp_ctx
= talloc_new(sam_ctx
);
3530 config_dn
= ldb_get_config_basedn(sam_ctx
);
3532 talloc_free(tmp_ctx
);
3533 return ldb_operr(sam_ctx
);
3536 ret
= dsdb_search(sam_ctx
, tmp_ctx
, &res
, config_dn
, LDB_SCOPE_SUBTREE
, attrs
,
3537 DSDB_SEARCH_ONE_ONLY
, "objectGUID=%s", GUID_string(tmp_ctx
, objectGUID
));
3539 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
3541 talloc_free(tmp_ctx
);
3545 if (ret
!= LDB_SUCCESS
) {
3546 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
3547 GUID_string(tmp_ctx
, objectGUID
)));
3549 talloc_free(tmp_ctx
);
3553 ret
= ldb_msg_find_attr_as_bool(res
->msgs
[0], "msDS-isRODC", 0);
3554 *is_rodc
= (ret
== 1);
3556 talloc_free(tmp_ctx
);
3562 see if we are a RODC
3564 int samdb_rodc(struct ldb_context
*sam_ctx
, bool *am_rodc
)
3566 const struct GUID
*objectGUID
;
3570 /* see if we have a cached copy */
3571 cached
= (bool *)ldb_get_opaque(sam_ctx
, "cache.am_rodc");
3577 objectGUID
= samdb_ntds_objectGUID(sam_ctx
);
3579 return ldb_operr(sam_ctx
);
3582 ret
= samdb_is_rodc(sam_ctx
, objectGUID
, am_rodc
);
3583 if (ret
!= LDB_SUCCESS
) {
3587 cached
= talloc(sam_ctx
, bool);
3588 if (cached
== NULL
) {
3589 return ldb_oom(sam_ctx
);
3593 ret
= ldb_set_opaque(sam_ctx
, "cache.am_rodc", cached
);
3594 if (ret
!= LDB_SUCCESS
) {
3595 talloc_free(cached
);
3596 return ldb_operr(sam_ctx
);
3602 int samdb_dns_host_name(struct ldb_context
*sam_ctx
, const char **host_name
)
3604 const char *_host_name
= NULL
;
3605 const char *attrs
[] = { "dnsHostName", NULL
};
3606 TALLOC_CTX
*tmp_ctx
= NULL
;
3608 struct ldb_result
*res
= NULL
;
3610 _host_name
= (const char *)ldb_get_opaque(sam_ctx
, "cache.dns_host_name");
3611 if (_host_name
!= NULL
) {
3612 *host_name
= _host_name
;
3616 tmp_ctx
= talloc_new(sam_ctx
);
3618 ret
= dsdb_search_dn(sam_ctx
, tmp_ctx
, &res
, NULL
, attrs
, 0);
3620 if (res
== NULL
|| res
->count
!= 1 || ret
!= LDB_SUCCESS
) {
3621 DEBUG(0, ("Failed to get rootDSE for dnsHostName: %s",
3622 ldb_errstring(sam_ctx
)));
3623 TALLOC_FREE(tmp_ctx
);
3627 _host_name
= ldb_msg_find_attr_as_string(res
->msgs
[0],
3630 if (_host_name
== NULL
) {
3631 DEBUG(0, ("Failed to get dnsHostName from rootDSE"));
3632 TALLOC_FREE(tmp_ctx
);
3633 return LDB_ERR_OPERATIONS_ERROR
;
3635 ret
= ldb_set_opaque(sam_ctx
, "cache.dns_host_name",
3636 discard_const_p(char *, _host_name
));
3637 if (ret
!= LDB_SUCCESS
) {
3638 TALLOC_FREE(tmp_ctx
);
3639 return ldb_operr(sam_ctx
);
3642 *host_name
= talloc_steal(sam_ctx
, _host_name
);
3644 TALLOC_FREE(tmp_ctx
);
3648 bool samdb_set_am_rodc(struct ldb_context
*ldb
, bool am_rodc
)
3650 TALLOC_CTX
*tmp_ctx
;
3653 tmp_ctx
= talloc_new(ldb
);
3654 if (tmp_ctx
== NULL
) {
3658 cached
= talloc(tmp_ctx
, bool);
3664 if (ldb_set_opaque(ldb
, "cache.am_rodc", cached
) != LDB_SUCCESS
) {
3668 talloc_steal(ldb
, cached
);
3669 talloc_free(tmp_ctx
);
3673 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
3674 talloc_free(tmp_ctx
);
3680 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
3681 * flags are DS_NTDSSETTINGS_OPT_*
3683 int samdb_ntds_site_settings_options(struct ldb_context
*ldb_ctx
,
3687 TALLOC_CTX
*tmp_ctx
;
3688 struct ldb_result
*res
;
3689 struct ldb_dn
*site_dn
;
3690 const char *attrs
[] = { "options", NULL
};
3692 tmp_ctx
= talloc_new(ldb_ctx
);
3693 if (tmp_ctx
== NULL
)
3696 /* Retrieve the site dn for the ldb that we
3697 * have open. This is our local site.
3699 site_dn
= samdb_server_site_dn(ldb_ctx
, tmp_ctx
);
3700 if (site_dn
== NULL
)
3703 /* Perform a one level (child) search from the local
3704 * site distinguided name. We're looking for the
3705 * "options" attribute within the nTDSSiteSettings
3708 rc
= ldb_search(ldb_ctx
, tmp_ctx
, &res
, site_dn
,
3709 LDB_SCOPE_ONELEVEL
, attrs
,
3710 "objectClass=nTDSSiteSettings");
3712 if (rc
!= LDB_SUCCESS
|| res
->count
!= 1)
3715 *options
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "options", 0);
3717 talloc_free(tmp_ctx
);
3722 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3723 talloc_free(tmp_ctx
);
3724 return ldb_error(ldb_ctx
, LDB_ERR_NO_SUCH_OBJECT
, __func__
);
3728 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3730 flags are DS_NTDS_OPTION_*
3732 int samdb_ntds_options(struct ldb_context
*ldb
, uint32_t *options
)
3734 TALLOC_CTX
*tmp_ctx
;
3735 const char *attrs
[] = { "options", NULL
};
3737 struct ldb_result
*res
;
3739 tmp_ctx
= talloc_new(ldb
);
3740 if (tmp_ctx
== NULL
) {
3744 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
, tmp_ctx
), LDB_SCOPE_BASE
, attrs
, NULL
);
3745 if (ret
!= LDB_SUCCESS
) {
3749 if (res
->count
!= 1) {
3753 *options
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "options", 0);
3755 talloc_free(tmp_ctx
);
3760 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3761 talloc_free(tmp_ctx
);
3762 return ldb_error(ldb
, LDB_ERR_NO_SUCH_OBJECT
, __func__
);
3765 const char* samdb_ntds_object_category(TALLOC_CTX
*tmp_ctx
, struct ldb_context
*ldb
)
3767 const char *attrs
[] = { "objectCategory", NULL
};
3769 struct ldb_result
*res
;
3771 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
, tmp_ctx
), LDB_SCOPE_BASE
, attrs
, NULL
);
3772 if (ret
!= LDB_SUCCESS
) {
3776 if (res
->count
!= 1) {
3780 return ldb_msg_find_attr_as_string(res
->msgs
[0], "objectCategory", NULL
);
3783 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3788 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3789 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3791 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX
*mem_ctx
, const char *cn
)
3793 char **tokens
, *ret
;
3796 tokens
= str_list_make(mem_ctx
, cn
, " -_");
3797 if (tokens
== NULL
|| tokens
[0] == NULL
) {
3801 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3802 tokens
[0][0] = tolower(tokens
[0][0]);
3803 for (i
= 1; tokens
[i
] != NULL
; i
++)
3804 tokens
[i
][0] = toupper(tokens
[i
][0]);
3806 ret
= talloc_strdup(mem_ctx
, tokens
[0]);
3807 for (i
= 1; tokens
[i
] != NULL
; i
++)
3808 ret
= talloc_asprintf_append_buffer(ret
, "%s", tokens
[i
]);
3810 talloc_free(tokens
);
3816 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3818 int dsdb_functional_level(struct ldb_context
*ldb
)
3820 int *domainFunctionality
=
3821 talloc_get_type(ldb_get_opaque(ldb
, "domainFunctionality"), int);
3822 if (!domainFunctionality
) {
3823 /* this is expected during initial provision */
3824 DEBUG(4,(__location__
": WARNING: domainFunctionality not setup\n"));
3825 return DS_DOMAIN_FUNCTION_2000
;
3827 return *domainFunctionality
;
3831 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3833 int dsdb_forest_functional_level(struct ldb_context
*ldb
)
3835 int *forestFunctionality
=
3836 talloc_get_type(ldb_get_opaque(ldb
, "forestFunctionality"), int);
3837 if (!forestFunctionality
) {
3838 DEBUG(0,(__location__
": WARNING: forestFunctionality not setup\n"));
3839 return DS_DOMAIN_FUNCTION_2000
;
3841 return *forestFunctionality
;
3845 * This detects and returns the DC functional level (DS_DOMAIN_FUNCTION_*)
3847 int dsdb_dc_functional_level(struct ldb_context
*ldb
)
3849 int *dcFunctionality
=
3850 talloc_get_type(ldb_get_opaque(ldb
, "domainFunctionality"), int);
3851 if (!dcFunctionality
) {
3852 /* this is expected during initial provision */
3853 DEBUG(4,(__location__
": WARNING: domainControllerFunctionality not setup\n"));
3854 return DS_DOMAIN_FUNCTION_2008_R2
;
3856 return *dcFunctionality
;
3860 set a GUID in an extended DN structure
3862 int dsdb_set_extended_dn_guid(struct ldb_dn
*dn
, const struct GUID
*guid
, const char *component_name
)
3868 status
= GUID_to_ndr_blob(guid
, dn
, &v
);
3869 if (!NT_STATUS_IS_OK(status
)) {
3870 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
3873 ret
= ldb_dn_set_extended_component(dn
, component_name
, &v
);
3879 return a GUID from a extended DN structure
3881 NTSTATUS
dsdb_get_extended_dn_guid(struct ldb_dn
*dn
, struct GUID
*guid
, const char *component_name
)
3883 const struct ldb_val
*v
;
3885 v
= ldb_dn_get_extended_component(dn
, component_name
);
3887 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3890 return GUID_from_ndr_blob(v
, guid
);
3894 return a uint64_t from a extended DN structure
3896 NTSTATUS
dsdb_get_extended_dn_uint64(struct ldb_dn
*dn
, uint64_t *val
, const char *component_name
)
3898 const struct ldb_val
*v
;
3901 v
= ldb_dn_get_extended_component(dn
, component_name
);
3903 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3906 /* Just check we don't allow the caller to fill our stack */
3907 if (v
->length
>= 64) {
3908 return NT_STATUS_INVALID_PARAMETER
;
3910 char s
[v
->length
+1];
3911 memcpy(s
, v
->data
, v
->length
);
3914 *val
= smb_strtoull(s
, NULL
, 0, &error
, SMB_STR_STANDARD
);
3916 return NT_STATUS_INVALID_PARAMETER
;
3919 return NT_STATUS_OK
;
3923 return a NTTIME from a extended DN structure
3925 NTSTATUS
dsdb_get_extended_dn_nttime(struct ldb_dn
*dn
, NTTIME
*nttime
, const char *component_name
)
3927 return dsdb_get_extended_dn_uint64(dn
, nttime
, component_name
);
3931 return a uint32_t from a extended DN structure
3933 NTSTATUS
dsdb_get_extended_dn_uint32(struct ldb_dn
*dn
, uint32_t *val
, const char *component_name
)
3935 const struct ldb_val
*v
;
3938 v
= ldb_dn_get_extended_component(dn
, component_name
);
3940 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3943 /* Just check we don't allow the caller to fill our stack */
3944 if (v
->length
>= 32) {
3945 return NT_STATUS_INVALID_PARAMETER
;
3947 char s
[v
->length
+ 1];
3948 memcpy(s
, v
->data
, v
->length
);
3950 *val
= smb_strtoul(s
, NULL
, 0, &error
, SMB_STR_STANDARD
);
3952 return NT_STATUS_INVALID_PARAMETER
;
3956 return NT_STATUS_OK
;
3960 return a dom_sid from a extended DN structure
3962 NTSTATUS
dsdb_get_extended_dn_sid(struct ldb_dn
*dn
, struct dom_sid
*sid
, const char *component_name
)
3964 const struct ldb_val
*sid_blob
;
3965 enum ndr_err_code ndr_err
;
3967 sid_blob
= ldb_dn_get_extended_component(dn
, component_name
);
3969 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3972 ndr_err
= ndr_pull_struct_blob_all_noalloc(sid_blob
, sid
,
3973 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
3974 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3975 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
3979 return NT_STATUS_OK
;
3984 return RMD_FLAGS directly from a ldb_dn
3985 returns 0 if not found
3987 uint32_t dsdb_dn_rmd_flags(struct ldb_dn
*dn
)
3989 uint32_t rmd_flags
= 0;
3990 NTSTATUS status
= dsdb_get_extended_dn_uint32(dn
, &rmd_flags
,
3992 if (NT_STATUS_IS_OK(status
)) {
3999 return RMD_FLAGS directly from a ldb_val for a DN
4000 returns 0 if RMD_FLAGS is not found
4002 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val
*val
)
4009 if (val
->length
< 13) {
4012 p
= memmem(val
->data
, val
->length
, "<RMD_FLAGS=", 11);
4016 flags
= smb_strtoul(p
+11, &end
, 10, &error
, SMB_STR_STANDARD
);
4017 if (!end
|| *end
!= '>' || error
!= 0) {
4018 /* it must end in a > */
4025 return true if a ldb_val containing a DN in storage form is deleted
4027 bool dsdb_dn_is_deleted_val(const struct ldb_val
*val
)
4029 return (dsdb_dn_val_rmd_flags(val
) & DSDB_RMD_FLAG_DELETED
) != 0;
4033 return true if a ldb_val containing a DN in storage form is
4034 in the upgraded w2k3 linked attribute format
4036 bool dsdb_dn_is_upgraded_link_val(const struct ldb_val
*val
)
4038 return memmem(val
->data
, val
->length
, "<RMD_VERSION=", 13) != NULL
;
4042 return a DN for a wellknown GUID
4044 int dsdb_wellknown_dn(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
,
4045 struct ldb_dn
*nc_root
, const char *wk_guid
,
4046 struct ldb_dn
**wkguid_dn
)
4048 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
4049 const char *attrs
[] = { NULL
};
4052 struct ldb_result
*res
= NULL
;
4054 /* construct the magic WKGUID DN */
4055 dn
= ldb_dn_new_fmt(tmp_ctx
, samdb
, "<WKGUID=%s,%s>",
4056 wk_guid
, ldb_dn_get_linearized(nc_root
));
4058 talloc_free(tmp_ctx
);
4059 return ldb_operr(samdb
);
4062 ret
= dsdb_search_dn(samdb
, tmp_ctx
, &res
, dn
, attrs
,
4063 DSDB_SEARCH_SHOW_DELETED
|
4064 DSDB_SEARCH_SHOW_RECYCLED
);
4065 if (ret
!= LDB_SUCCESS
) {
4066 talloc_free(tmp_ctx
);
4069 /* fix clang warning */
4071 talloc_free(tmp_ctx
);
4072 return LDB_ERR_OTHER
;
4075 (*wkguid_dn
) = talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
4076 talloc_free(tmp_ctx
);
4081 static int dsdb_dn_compare_ptrs(struct ldb_dn
**dn1
, struct ldb_dn
**dn2
)
4083 return ldb_dn_compare(*dn1
, *dn2
);
4087 find a NC root given a DN within the NC
4089 int dsdb_find_nc_root(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
4090 struct ldb_dn
**nc_root
)
4092 const char *root_attrs
[] = { "namingContexts", NULL
};
4093 TALLOC_CTX
*tmp_ctx
;
4095 struct ldb_message_element
*el
;
4096 struct ldb_result
*root_res
;
4098 struct ldb_dn
**nc_dns
;
4100 tmp_ctx
= talloc_new(samdb
);
4101 if (tmp_ctx
== NULL
) {
4102 return ldb_oom(samdb
);
4105 ret
= ldb_search(samdb
, tmp_ctx
, &root_res
,
4106 ldb_dn_new(tmp_ctx
, samdb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
4107 if (ret
!= LDB_SUCCESS
|| root_res
->count
== 0) {
4108 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb
)));
4109 talloc_free(tmp_ctx
);
4113 el
= ldb_msg_find_element(root_res
->msgs
[0], "namingContexts");
4114 if ((el
== NULL
) || (el
->num_values
< 3)) {
4115 struct ldb_message
*tmp_msg
;
4117 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list.\n"));
4119 /* This generates a temporary list of NCs in order to let the
4120 * provisioning work. */
4121 tmp_msg
= ldb_msg_new(tmp_ctx
);
4122 if (tmp_msg
== NULL
) {
4123 talloc_free(tmp_ctx
);
4124 return ldb_oom(samdb
);
4126 ret
= ldb_msg_add_steal_string(tmp_msg
, "namingContexts",
4127 ldb_dn_alloc_linearized(tmp_msg
, ldb_get_schema_basedn(samdb
)));
4128 if (ret
!= LDB_SUCCESS
) {
4129 talloc_free(tmp_ctx
);
4132 ret
= ldb_msg_add_steal_string(tmp_msg
, "namingContexts",
4133 ldb_dn_alloc_linearized(tmp_msg
, ldb_get_config_basedn(samdb
)));
4134 if (ret
!= LDB_SUCCESS
) {
4135 talloc_free(tmp_ctx
);
4138 ret
= ldb_msg_add_steal_string(tmp_msg
, "namingContexts",
4139 ldb_dn_alloc_linearized(tmp_msg
, ldb_get_default_basedn(samdb
)));
4140 if (ret
!= LDB_SUCCESS
) {
4141 talloc_free(tmp_ctx
);
4144 el
= &tmp_msg
->elements
[0];
4147 nc_dns
= talloc_array(tmp_ctx
, struct ldb_dn
*, el
->num_values
);
4149 talloc_free(tmp_ctx
);
4150 return ldb_oom(samdb
);
4153 for (i
=0; i
<el
->num_values
; i
++) {
4154 nc_dns
[i
] = ldb_dn_from_ldb_val(nc_dns
, samdb
, &el
->values
[i
]);
4155 if (nc_dns
[i
] == NULL
) {
4156 talloc_free(tmp_ctx
);
4157 return ldb_operr(samdb
);
4161 TYPESAFE_QSORT(nc_dns
, el
->num_values
, dsdb_dn_compare_ptrs
);
4163 for (i
=0; i
<el
->num_values
; i
++) {
4164 if (ldb_dn_compare_base(nc_dns
[i
], dn
) == 0) {
4165 (*nc_root
) = talloc_steal(mem_ctx
, nc_dns
[i
]);
4166 talloc_free(tmp_ctx
);
4171 talloc_free(tmp_ctx
);
4172 return ldb_error(samdb
, LDB_ERR_NO_SUCH_OBJECT
, __func__
);
4177 find the deleted objects DN for any object, by looking for the NC
4178 root, then looking up the wellknown GUID
4180 int dsdb_get_deleted_objects_dn(struct ldb_context
*ldb
,
4181 TALLOC_CTX
*mem_ctx
, struct ldb_dn
*obj_dn
,
4182 struct ldb_dn
**do_dn
)
4184 struct ldb_dn
*nc_root
;
4187 ret
= dsdb_find_nc_root(ldb
, mem_ctx
, obj_dn
, &nc_root
);
4188 if (ret
!= LDB_SUCCESS
) {
4192 ret
= dsdb_wellknown_dn(ldb
, mem_ctx
, nc_root
, DS_GUID_DELETED_OBJECTS_CONTAINER
, do_dn
);
4193 talloc_free(nc_root
);
4198 return the tombstoneLifetime, in days
4200 int dsdb_tombstone_lifetime(struct ldb_context
*ldb
, uint32_t *lifetime
)
4203 dn
= ldb_get_config_basedn(ldb
);
4205 return ldb_error(ldb
, LDB_ERR_NO_SUCH_OBJECT
, __func__
);
4207 dn
= ldb_dn_copy(ldb
, dn
);
4209 return ldb_operr(ldb
);
4211 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
4212 be a wellknown GUID for this */
4213 if (!ldb_dn_add_child_fmt(dn
, "CN=Directory Service,CN=Windows NT,CN=Services")) {
4215 return ldb_operr(ldb
);
4218 *lifetime
= samdb_search_uint(ldb
, dn
, 180, dn
, "tombstoneLifetime", "objectClass=nTDSService");
4224 compare a ldb_val to a string case insensitively
4226 int samdb_ldb_val_case_cmp(const char *s
, struct ldb_val
*v
)
4228 size_t len
= strlen(s
);
4230 if (len
> v
->length
) return 1;
4231 ret
= strncasecmp(s
, (const char *)v
->data
, v
->length
);
4232 if (ret
!= 0) return ret
;
4233 if (v
->length
> len
&& v
->data
[len
] != 0) {
4241 load the UDV for a partition in v2 format
4242 The list is returned sorted, and with our local cursor added
4244 int dsdb_load_udv_v2(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
4245 struct drsuapi_DsReplicaCursor2
**cursors
, uint32_t *count
)
4247 static const char *attrs
[] = { "replUpToDateVector", NULL
};
4248 struct ldb_result
*r
= NULL
;
4249 const struct ldb_val
*ouv_value
;
4252 uint64_t highest_usn
= 0;
4253 const struct GUID
*our_invocation_id
;
4254 static const struct timeval tv1970
;
4255 NTTIME nt1970
= timeval_to_nttime(&tv1970
);
4257 ret
= dsdb_search_dn(samdb
, mem_ctx
, &r
, dn
, attrs
, DSDB_SEARCH_SHOW_RECYCLED
|DSDB_SEARCH_SHOW_DELETED
);
4258 if (ret
!= LDB_SUCCESS
) {
4261 /* fix clang warning */
4263 return LDB_ERR_OTHER
;
4265 ouv_value
= ldb_msg_find_ldb_val(r
->msgs
[0], "replUpToDateVector");
4267 enum ndr_err_code ndr_err
;
4268 struct replUpToDateVectorBlob ouv
;
4270 ndr_err
= ndr_pull_struct_blob(ouv_value
, r
, &ouv
,
4271 (ndr_pull_flags_fn_t
)ndr_pull_replUpToDateVectorBlob
);
4272 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
4274 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
4276 if (ouv
.version
!= 2) {
4277 /* we always store as version 2, and
4278 * replUpToDateVector is not replicated
4280 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
4283 *count
= ouv
.ctr
.ctr2
.count
;
4284 *cursors
= talloc_steal(mem_ctx
, ouv
.ctr
.ctr2
.cursors
);
4292 our_invocation_id
= samdb_ntds_invocation_id(samdb
);
4293 if (!our_invocation_id
) {
4294 DEBUG(0,(__location__
": No invocationID on samdb - %s\n", ldb_errstring(samdb
)));
4295 talloc_free(*cursors
);
4296 return ldb_operr(samdb
);
4299 ret
= ldb_sequence_number(samdb
, LDB_SEQ_HIGHEST_SEQ
, &highest_usn
);
4300 if (ret
!= LDB_SUCCESS
) {
4301 /* nothing to add - this can happen after a vampire */
4302 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
4306 for (i
=0; i
<*count
; i
++) {
4307 if (GUID_equal(our_invocation_id
, &(*cursors
)[i
].source_dsa_invocation_id
)) {
4308 (*cursors
)[i
].highest_usn
= highest_usn
;
4309 (*cursors
)[i
].last_sync_success
= nt1970
;
4310 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
4315 (*cursors
) = talloc_realloc(mem_ctx
, *cursors
, struct drsuapi_DsReplicaCursor2
, (*count
)+1);
4317 return ldb_oom(samdb
);
4320 (*cursors
)[*count
].source_dsa_invocation_id
= *our_invocation_id
;
4321 (*cursors
)[*count
].highest_usn
= highest_usn
;
4322 (*cursors
)[*count
].last_sync_success
= nt1970
;
4325 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
4331 load the UDV for a partition in version 1 format
4332 The list is returned sorted, and with our local cursor added
4334 int dsdb_load_udv_v1(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
4335 struct drsuapi_DsReplicaCursor
**cursors
, uint32_t *count
)
4337 struct drsuapi_DsReplicaCursor2
*v2
= NULL
;
4341 ret
= dsdb_load_udv_v2(samdb
, dn
, mem_ctx
, &v2
, count
);
4342 if (ret
!= LDB_SUCCESS
) {
4352 *cursors
= talloc_array(mem_ctx
, struct drsuapi_DsReplicaCursor
, *count
);
4353 if (*cursors
== NULL
) {
4355 return ldb_oom(samdb
);
4358 for (i
=0; i
<*count
; i
++) {
4359 (*cursors
)[i
].source_dsa_invocation_id
= v2
[i
].source_dsa_invocation_id
;
4360 (*cursors
)[i
].highest_usn
= v2
[i
].highest_usn
;
4367 add a set of controls to a ldb_request structure based on a set of
4368 flags. See util.h for a list of available flags
4370 int dsdb_request_add_controls(struct ldb_request
*req
, uint32_t dsdb_flags
)
4373 if (dsdb_flags
& DSDB_SEARCH_SEARCH_ALL_PARTITIONS
) {
4374 struct ldb_search_options_control
*options
;
4375 /* Using the phantom root control allows us to search all partitions */
4376 options
= talloc(req
, struct ldb_search_options_control
);
4377 if (options
== NULL
) {
4378 return LDB_ERR_OPERATIONS_ERROR
;
4380 options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
4382 ret
= ldb_request_add_control(req
,
4383 LDB_CONTROL_SEARCH_OPTIONS_OID
,
4385 if (ret
!= LDB_SUCCESS
) {
4390 if (dsdb_flags
& DSDB_SEARCH_NO_GLOBAL_CATALOG
) {
4391 ret
= ldb_request_add_control(req
,
4392 DSDB_CONTROL_NO_GLOBAL_CATALOG
,
4394 if (ret
!= LDB_SUCCESS
) {
4399 if (dsdb_flags
& DSDB_SEARCH_SHOW_DELETED
) {
4400 ret
= ldb_request_add_control(req
, LDB_CONTROL_SHOW_DELETED_OID
, true, NULL
);
4401 if (ret
!= LDB_SUCCESS
) {
4406 if (dsdb_flags
& DSDB_SEARCH_SHOW_RECYCLED
) {
4407 ret
= ldb_request_add_control(req
, LDB_CONTROL_SHOW_RECYCLED_OID
, false, NULL
);
4408 if (ret
!= LDB_SUCCESS
) {
4413 if (dsdb_flags
& DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
) {
4414 ret
= ldb_request_add_control(req
, DSDB_CONTROL_DN_STORAGE_FORMAT_OID
, false, NULL
);
4415 if (ret
!= LDB_SUCCESS
) {
4420 if (dsdb_flags
& DSDB_SEARCH_SHOW_EXTENDED_DN
) {
4421 struct ldb_extended_dn_control
*extended_ctrl
= talloc(req
, struct ldb_extended_dn_control
);
4422 if (!extended_ctrl
) {
4423 return LDB_ERR_OPERATIONS_ERROR
;
4425 extended_ctrl
->type
= 1;
4427 ret
= ldb_request_add_control(req
, LDB_CONTROL_EXTENDED_DN_OID
, true, extended_ctrl
);
4428 if (ret
!= LDB_SUCCESS
) {
4433 if (dsdb_flags
& DSDB_SEARCH_REVEAL_INTERNALS
) {
4434 ret
= ldb_request_add_control(req
, LDB_CONTROL_REVEAL_INTERNALS
, false, NULL
);
4435 if (ret
!= LDB_SUCCESS
) {
4440 if (dsdb_flags
& DSDB_MODIFY_RELAX
) {
4441 ret
= ldb_request_add_control(req
, LDB_CONTROL_RELAX_OID
, false, NULL
);
4442 if (ret
!= LDB_SUCCESS
) {
4447 if (dsdb_flags
& DSDB_MODIFY_PERMISSIVE
) {
4448 ret
= ldb_request_add_control(req
, LDB_CONTROL_PERMISSIVE_MODIFY_OID
, false, NULL
);
4449 if (ret
!= LDB_SUCCESS
) {
4454 if (dsdb_flags
& DSDB_FLAG_AS_SYSTEM
) {
4455 ret
= ldb_request_add_control(req
, LDB_CONTROL_AS_SYSTEM_OID
, false, NULL
);
4456 if (ret
!= LDB_SUCCESS
) {
4461 if (dsdb_flags
& DSDB_TREE_DELETE
) {
4462 ret
= ldb_request_add_control(req
, LDB_CONTROL_TREE_DELETE_OID
, false, NULL
);
4463 if (ret
!= LDB_SUCCESS
) {
4468 if (dsdb_flags
& DSDB_PROVISION
) {
4469 ret
= ldb_request_add_control(req
, LDB_CONTROL_PROVISION_OID
, false, NULL
);
4470 if (ret
!= LDB_SUCCESS
) {
4475 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
4476 if (dsdb_flags
& DSDB_BYPASS_PASSWORD_HASH
) {
4477 ret
= ldb_request_add_control(req
, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID
, true, NULL
);
4478 if (ret
!= LDB_SUCCESS
) {
4483 if (dsdb_flags
& DSDB_PASSWORD_BYPASS_LAST_SET
) {
4485 * This must not be critical, as it will only be
4486 * handled (and need to be handled) if the other
4487 * attributes in the request bring password_hash into
4490 ret
= ldb_request_add_control(req
, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID
, false, NULL
);
4491 if (ret
!= LDB_SUCCESS
) {
4496 if (dsdb_flags
& DSDB_REPLMD_VANISH_LINKS
) {
4497 ret
= ldb_request_add_control(req
, DSDB_CONTROL_REPLMD_VANISH_LINKS
, true, NULL
);
4498 if (ret
!= LDB_SUCCESS
) {
4503 if (dsdb_flags
& DSDB_MODIFY_PARTIAL_REPLICA
) {
4504 ret
= ldb_request_add_control(req
, DSDB_CONTROL_PARTIAL_REPLICA
, false, NULL
);
4505 if (ret
!= LDB_SUCCESS
) {
4510 if (dsdb_flags
& DSDB_FLAG_REPLICATED_UPDATE
) {
4511 ret
= ldb_request_add_control(req
, DSDB_CONTROL_REPLICATED_UPDATE_OID
, false, NULL
);
4512 if (ret
!= LDB_SUCCESS
) {
4521 returns true if a control with the specified "oid" exists
4523 bool dsdb_request_has_control(struct ldb_request
*req
, const char *oid
)
4525 return (ldb_request_get_control(req
, oid
) != NULL
);
4529 an add with a set of controls
4531 int dsdb_add(struct ldb_context
*ldb
, const struct ldb_message
*message
,
4532 uint32_t dsdb_flags
)
4534 struct ldb_request
*req
;
4537 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
4541 ldb_op_default_callback
,
4544 if (ret
!= LDB_SUCCESS
) return ret
;
4546 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4547 if (ret
!= LDB_SUCCESS
) {
4552 ret
= dsdb_autotransaction_request(ldb
, req
);
4559 a modify with a set of controls
4561 int dsdb_modify(struct ldb_context
*ldb
, const struct ldb_message
*message
,
4562 uint32_t dsdb_flags
)
4564 struct ldb_request
*req
;
4567 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
4571 ldb_op_default_callback
,
4574 if (ret
!= LDB_SUCCESS
) return ret
;
4576 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4577 if (ret
!= LDB_SUCCESS
) {
4582 ret
= dsdb_autotransaction_request(ldb
, req
);
4589 a delete with a set of flags
4591 int dsdb_delete(struct ldb_context
*ldb
, struct ldb_dn
*dn
,
4592 uint32_t dsdb_flags
)
4594 struct ldb_request
*req
;
4597 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
4601 ldb_op_default_callback
,
4604 if (ret
!= LDB_SUCCESS
) return ret
;
4606 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4607 if (ret
!= LDB_SUCCESS
) {
4612 ret
= dsdb_autotransaction_request(ldb
, req
);
4619 like dsdb_modify() but set all the element flags to
4620 LDB_FLAG_MOD_REPLACE
4622 int dsdb_replace(struct ldb_context
*ldb
, struct ldb_message
*msg
, uint32_t dsdb_flags
)
4626 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
4627 for (i
=0;i
<msg
->num_elements
;i
++) {
4628 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
4631 return dsdb_modify(ldb
, msg
, dsdb_flags
);
4636 search for attrs on one DN, allowing for dsdb_flags controls
4638 int dsdb_search_dn(struct ldb_context
*ldb
,
4639 TALLOC_CTX
*mem_ctx
,
4640 struct ldb_result
**_result
,
4641 struct ldb_dn
*basedn
,
4642 const char * const *attrs
,
4643 uint32_t dsdb_flags
)
4646 struct ldb_request
*req
;
4647 struct ldb_result
*res
;
4649 res
= talloc_zero(mem_ctx
, struct ldb_result
);
4651 return ldb_oom(ldb
);
4654 ret
= ldb_build_search_req(&req
, ldb
, res
,
4661 ldb_search_default_callback
,
4663 if (ret
!= LDB_SUCCESS
) {
4668 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4669 if (ret
!= LDB_SUCCESS
) {
4674 ret
= ldb_request(ldb
, req
);
4675 if (ret
== LDB_SUCCESS
) {
4676 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
4680 if (ret
!= LDB_SUCCESS
) {
4690 search for attrs on one DN, by the GUID of the DN, allowing for
4693 int dsdb_search_by_dn_guid(struct ldb_context
*ldb
,
4694 TALLOC_CTX
*mem_ctx
,
4695 struct ldb_result
**_result
,
4696 const struct GUID
*guid
,
4697 const char * const *attrs
,
4698 uint32_t dsdb_flags
)
4700 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
4704 dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "<GUID=%s>", GUID_string(tmp_ctx
, guid
));
4706 talloc_free(tmp_ctx
);
4707 return ldb_oom(ldb
);
4710 ret
= dsdb_search_dn(ldb
, mem_ctx
, _result
, dn
, attrs
, dsdb_flags
);
4711 talloc_free(tmp_ctx
);
4716 general search with dsdb_flags for controls
4718 int dsdb_search(struct ldb_context
*ldb
,
4719 TALLOC_CTX
*mem_ctx
,
4720 struct ldb_result
**_result
,
4721 struct ldb_dn
*basedn
,
4722 enum ldb_scope scope
,
4723 const char * const *attrs
,
4724 uint32_t dsdb_flags
,
4725 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
4728 struct ldb_request
*req
;
4729 struct ldb_result
*res
;
4731 char *expression
= NULL
;
4732 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
4734 /* cross-partitions searches with a basedn break multi-domain support */
4735 SMB_ASSERT(basedn
== NULL
|| (dsdb_flags
& DSDB_SEARCH_SEARCH_ALL_PARTITIONS
) == 0);
4737 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
4739 talloc_free(tmp_ctx
);
4740 return ldb_oom(ldb
);
4744 va_start(ap
, exp_fmt
);
4745 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
4749 talloc_free(tmp_ctx
);
4750 return ldb_oom(ldb
);
4754 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
4761 ldb_search_default_callback
,
4763 if (ret
!= LDB_SUCCESS
) {
4764 talloc_free(tmp_ctx
);
4768 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4769 if (ret
!= LDB_SUCCESS
) {
4770 talloc_free(tmp_ctx
);
4771 ldb_reset_err_string(ldb
);
4775 ret
= ldb_request(ldb
, req
);
4776 if (ret
== LDB_SUCCESS
) {
4777 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
4780 if (ret
!= LDB_SUCCESS
) {
4781 talloc_free(tmp_ctx
);
4785 if (dsdb_flags
& DSDB_SEARCH_ONE_ONLY
) {
4786 if (res
->count
== 0) {
4787 talloc_free(tmp_ctx
);
4788 ldb_reset_err_string(ldb
);
4789 return ldb_error(ldb
, LDB_ERR_NO_SUCH_OBJECT
, __func__
);
4791 if (res
->count
!= 1) {
4792 talloc_free(tmp_ctx
);
4793 ldb_reset_err_string(ldb
);
4794 return LDB_ERR_CONSTRAINT_VIOLATION
;
4798 *_result
= talloc_steal(mem_ctx
, res
);
4799 talloc_free(tmp_ctx
);
4806 general search with dsdb_flags for controls
4807 returns exactly 1 record or an error
4809 int dsdb_search_one(struct ldb_context
*ldb
,
4810 TALLOC_CTX
*mem_ctx
,
4811 struct ldb_message
**msg
,
4812 struct ldb_dn
*basedn
,
4813 enum ldb_scope scope
,
4814 const char * const *attrs
,
4815 uint32_t dsdb_flags
,
4816 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
4819 struct ldb_result
*res
;
4821 char *expression
= NULL
;
4822 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
4824 dsdb_flags
|= DSDB_SEARCH_ONE_ONLY
;
4826 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
4828 talloc_free(tmp_ctx
);
4829 return ldb_oom(ldb
);
4833 va_start(ap
, exp_fmt
);
4834 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
4838 talloc_free(tmp_ctx
);
4839 return ldb_oom(ldb
);
4841 ret
= dsdb_search(ldb
, tmp_ctx
, &res
, basedn
, scope
, attrs
,
4842 dsdb_flags
, "%s", expression
);
4844 ret
= dsdb_search(ldb
, tmp_ctx
, &res
, basedn
, scope
, attrs
,
4848 if (ret
!= LDB_SUCCESS
) {
4849 talloc_free(tmp_ctx
);
4853 *msg
= talloc_steal(mem_ctx
, res
->msgs
[0]);
4854 talloc_free(tmp_ctx
);
4859 /* returns back the forest DNS name */
4860 const char *samdb_forest_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
4862 const char *forest_name
= ldb_dn_canonical_string(mem_ctx
,
4863 ldb_get_root_basedn(ldb
));
4866 if (forest_name
== NULL
) {
4870 p
= strchr(forest_name
, '/');
4878 /* returns back the default domain DNS name */
4879 const char *samdb_default_domain_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
4881 const char *domain_name
= ldb_dn_canonical_string(mem_ctx
,
4882 ldb_get_default_basedn(ldb
));
4885 if (domain_name
== NULL
) {
4889 p
= strchr(domain_name
, '/');
4898 validate that an DSA GUID belongs to the specified user sid.
4899 The user SID must be a domain controller account (either RODC or
4902 int dsdb_validate_dsa_guid(struct ldb_context
*ldb
,
4903 const struct GUID
*dsa_guid
,
4904 const struct dom_sid
*sid
)
4907 - find DN of record with the DSA GUID in the
4908 configuration partition (objectGUID)
4909 - remove "NTDS Settings" component from DN
4910 - do a base search on that DN for serverReference with
4912 - extract objectSid from resulting serverReference
4914 - check this sid matches the sid argument
4916 struct ldb_dn
*config_dn
;
4917 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
4918 struct ldb_message
*msg
;
4919 const char *attrs1
[] = { NULL
};
4920 const char *attrs2
[] = { "serverReference", NULL
};
4922 struct ldb_dn
*dn
, *account_dn
;
4923 struct dom_sid sid2
;
4926 config_dn
= ldb_get_config_basedn(ldb
);
4928 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, config_dn
, LDB_SCOPE_SUBTREE
,
4929 attrs1
, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx
, dsa_guid
));
4930 if (ret
!= LDB_SUCCESS
) {
4931 DEBUG(1,(__location__
": Failed to find DSA objectGUID %s for sid %s\n",
4932 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
4933 talloc_free(tmp_ctx
);
4934 return ldb_operr(ldb
);
4938 if (!ldb_dn_remove_child_components(dn
, 1)) {
4939 talloc_free(tmp_ctx
);
4940 return ldb_operr(ldb
);
4943 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, dn
, LDB_SCOPE_BASE
,
4944 attrs2
, DSDB_SEARCH_SHOW_EXTENDED_DN
,
4945 "(objectClass=server)");
4946 if (ret
!= LDB_SUCCESS
) {
4947 DEBUG(1,(__location__
": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4948 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
4949 talloc_free(tmp_ctx
);
4950 return ldb_operr(ldb
);
4953 account_dn
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, msg
, "serverReference");
4954 if (account_dn
== NULL
) {
4955 DEBUG(1,(__location__
": Failed to find account dn "
4956 "(serverReference) for %s, parent of DSA with "
4957 "objectGUID %s, sid %s\n",
4958 ldb_dn_get_linearized(msg
->dn
),
4959 GUID_string(tmp_ctx
, dsa_guid
),
4960 dom_sid_string(tmp_ctx
, sid
)));
4961 talloc_free(tmp_ctx
);
4962 return ldb_operr(ldb
);
4965 status
= dsdb_get_extended_dn_sid(account_dn
, &sid2
, "SID");
4966 if (!NT_STATUS_IS_OK(status
)) {
4967 DEBUG(1,(__location__
": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4968 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
4969 talloc_free(tmp_ctx
);
4970 return ldb_operr(ldb
);
4973 if (!dom_sid_equal(sid
, &sid2
)) {
4974 /* someone is trying to spoof another account */
4975 DEBUG(0,(__location__
": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4976 GUID_string(tmp_ctx
, dsa_guid
),
4977 dom_sid_string(tmp_ctx
, sid
),
4978 dom_sid_string(tmp_ctx
, &sid2
)));
4979 talloc_free(tmp_ctx
);
4980 return ldb_operr(ldb
);
4983 talloc_free(tmp_ctx
);
4987 static const char * const secret_attributes
[] = {
4988 DSDB_SECRET_ATTRIBUTES
,
4993 check if the attribute belongs to the RODC filtered attribute set
4994 Note that attributes that are in the filtered attribute set are the
4995 ones that _are_ always sent to a RODC
4997 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute
*sa
)
4999 /* they never get secret attributes */
5000 if (is_attr_in_list(secret_attributes
, sa
->lDAPDisplayName
)) {
5004 /* they do get non-secret critical attributes */
5005 if (sa
->schemaFlagsEx
& SCHEMA_FLAG_ATTR_IS_CRITICAL
) {
5009 /* they do get non-secret attributes marked as being in the FAS */
5010 if (sa
->searchFlags
& SEARCH_FLAG_RODC_ATTRIBUTE
) {
5014 /* other attributes are denied */
5018 /* return fsmo role dn and role owner dn for a particular role*/
5019 WERROR
dsdb_get_fsmo_role_info(TALLOC_CTX
*tmp_ctx
,
5020 struct ldb_context
*ldb
,
5022 struct ldb_dn
**fsmo_role_dn
,
5023 struct ldb_dn
**role_owner_dn
)
5027 case DREPL_NAMING_MASTER
:
5028 *fsmo_role_dn
= samdb_partitions_dn(ldb
, tmp_ctx
);
5029 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
5030 if (ret
!= LDB_SUCCESS
) {
5031 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Naming Master object - %s",
5032 ldb_errstring(ldb
)));
5033 talloc_free(tmp_ctx
);
5034 return WERR_DS_DRA_INTERNAL_ERROR
;
5037 case DREPL_INFRASTRUCTURE_MASTER
:
5038 *fsmo_role_dn
= samdb_infrastructure_dn(ldb
, tmp_ctx
);
5039 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
5040 if (ret
!= LDB_SUCCESS
) {
5041 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Schema Master object - %s",
5042 ldb_errstring(ldb
)));
5043 talloc_free(tmp_ctx
);
5044 return WERR_DS_DRA_INTERNAL_ERROR
;
5047 case DREPL_RID_MASTER
:
5048 ret
= samdb_rid_manager_dn(ldb
, tmp_ctx
, fsmo_role_dn
);
5049 if (ret
!= LDB_SUCCESS
) {
5050 DEBUG(0, (__location__
": Failed to find RID Manager object - %s", ldb_errstring(ldb
)));
5051 talloc_free(tmp_ctx
);
5052 return WERR_DS_DRA_INTERNAL_ERROR
;
5055 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
5056 if (ret
!= LDB_SUCCESS
) {
5057 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in RID Manager object - %s",
5058 ldb_errstring(ldb
)));
5059 talloc_free(tmp_ctx
);
5060 return WERR_DS_DRA_INTERNAL_ERROR
;
5063 case DREPL_SCHEMA_MASTER
:
5064 *fsmo_role_dn
= ldb_get_schema_basedn(ldb
);
5065 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
5066 if (ret
!= LDB_SUCCESS
) {
5067 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Schema Master object - %s",
5068 ldb_errstring(ldb
)));
5069 talloc_free(tmp_ctx
);
5070 return WERR_DS_DRA_INTERNAL_ERROR
;
5073 case DREPL_PDC_MASTER
:
5074 *fsmo_role_dn
= ldb_get_default_basedn(ldb
);
5075 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
5076 if (ret
!= LDB_SUCCESS
) {
5077 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Pd Master object - %s",
5078 ldb_errstring(ldb
)));
5079 talloc_free(tmp_ctx
);
5080 return WERR_DS_DRA_INTERNAL_ERROR
;
5084 return WERR_DS_DRA_INTERNAL_ERROR
;
5089 const char *samdb_dn_to_dnshostname(struct ldb_context
*ldb
,
5090 TALLOC_CTX
*mem_ctx
,
5091 struct ldb_dn
*server_dn
)
5094 struct ldb_result
*res
= NULL
;
5095 const char * const attrs
[] = { "dNSHostName", NULL
};
5097 ldb_ret
= ldb_search(ldb
, mem_ctx
, &res
,
5101 if (ldb_ret
!= LDB_SUCCESS
) {
5102 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
5103 ldb_dn_get_linearized(server_dn
), ldb_errstring(ldb
)));
5107 return ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
5111 returns true if an attribute is in the filter,
5112 false otherwise, provided that attribute value is provided with the expression
5114 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree
*tree
,
5118 switch (tree
->operation
) {
5121 for (i
=0;i
<tree
->u
.list
.num_elements
;i
++) {
5122 if (dsdb_attr_in_parse_tree(tree
->u
.list
.elements
[i
],
5128 return dsdb_attr_in_parse_tree(tree
->u
.isnot
.child
, attr
);
5129 case LDB_OP_EQUALITY
:
5130 case LDB_OP_GREATER
:
5133 if (ldb_attr_cmp(tree
->u
.equality
.attr
, attr
) == 0) {
5137 case LDB_OP_SUBSTRING
:
5138 if (ldb_attr_cmp(tree
->u
.substring
.attr
, attr
) == 0) {
5142 case LDB_OP_PRESENT
:
5143 /* (attrname=*) is not filtered out */
5145 case LDB_OP_EXTENDED
:
5146 if (tree
->u
.extended
.attr
&&
5147 ldb_attr_cmp(tree
->u
.extended
.attr
, attr
) == 0) {
5155 bool is_attr_in_list(const char * const * attrs
, const char *attr
)
5159 for (i
= 0; attrs
[i
]; i
++) {
5160 if (ldb_attr_cmp(attrs
[i
], attr
) == 0)
5167 int dsdb_werror_at(struct ldb_context
*ldb
, int ldb_ecode
, WERROR werr
,
5168 const char *location
, const char *func
,
5171 if (reason
== NULL
) {
5172 reason
= win_errstr(werr
);
5174 ldb_asprintf_errstring(ldb
, "%08X: %s at %s:%s",
5175 W_ERROR_V(werr
), reason
, location
, func
);
5180 map an ldb error code to an approximate NTSTATUS code
5182 NTSTATUS
dsdb_ldb_err_to_ntstatus(int err
)
5186 return NT_STATUS_OK
;
5188 case LDB_ERR_PROTOCOL_ERROR
:
5189 return NT_STATUS_DEVICE_PROTOCOL_ERROR
;
5191 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
5192 return NT_STATUS_IO_TIMEOUT
;
5194 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
5195 return NT_STATUS_BUFFER_TOO_SMALL
;
5197 case LDB_ERR_COMPARE_FALSE
:
5198 case LDB_ERR_COMPARE_TRUE
:
5199 return NT_STATUS_REVISION_MISMATCH
;
5201 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
5202 return NT_STATUS_NOT_SUPPORTED
;
5204 case LDB_ERR_STRONG_AUTH_REQUIRED
:
5205 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
5206 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
5207 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
5208 case LDB_ERR_INVALID_CREDENTIALS
:
5209 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
5210 case LDB_ERR_UNWILLING_TO_PERFORM
:
5211 return NT_STATUS_ACCESS_DENIED
;
5213 case LDB_ERR_NO_SUCH_OBJECT
:
5214 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5216 case LDB_ERR_REFERRAL
:
5217 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
5218 return NT_STATUS_NOT_FOUND
;
5220 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
5221 return NT_STATUS_NOT_SUPPORTED
;
5223 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
5224 return NT_STATUS_BUFFER_TOO_SMALL
;
5226 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
5227 case LDB_ERR_INAPPROPRIATE_MATCHING
:
5228 case LDB_ERR_CONSTRAINT_VIOLATION
:
5229 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
5230 case LDB_ERR_INVALID_DN_SYNTAX
:
5231 case LDB_ERR_NAMING_VIOLATION
:
5232 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
5233 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
5234 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
5235 return NT_STATUS_INVALID_PARAMETER
;
5237 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
5238 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
5239 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS
;
5242 return NT_STATUS_NETWORK_BUSY
;
5244 case LDB_ERR_ALIAS_PROBLEM
:
5245 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
5246 case LDB_ERR_UNAVAILABLE
:
5247 case LDB_ERR_LOOP_DETECT
:
5248 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
5249 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
5251 case LDB_ERR_OPERATIONS_ERROR
:
5254 return NT_STATUS_UNSUCCESSFUL
;
5259 create a new naming context that will hold a partial replica
5261 int dsdb_create_partial_replica_NC(struct ldb_context
*ldb
, struct ldb_dn
*dn
)
5263 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
5264 struct ldb_message
*msg
;
5267 msg
= ldb_msg_new(tmp_ctx
);
5269 talloc_free(tmp_ctx
);
5270 return ldb_oom(ldb
);
5274 ret
= ldb_msg_add_string(msg
, "objectClass", "top");
5275 if (ret
!= LDB_SUCCESS
) {
5276 talloc_free(tmp_ctx
);
5277 return ldb_oom(ldb
);
5280 /* [MS-DRSR] implies that we should only add the 'top'
5281 * objectclass, but that would cause lots of problems with our
5282 * objectclass code as top is not structural, so we add
5283 * 'domainDNS' as well to keep things sane. We're expecting
5284 * this new NC to be of objectclass domainDNS after
5285 * replication anyway
5287 ret
= ldb_msg_add_string(msg
, "objectClass", "domainDNS");
5288 if (ret
!= LDB_SUCCESS
) {
5289 talloc_free(tmp_ctx
);
5290 return ldb_oom(ldb
);
5293 ret
= ldb_msg_add_fmt(msg
, "instanceType", "%u",
5294 INSTANCE_TYPE_IS_NC_HEAD
|
5295 INSTANCE_TYPE_NC_ABOVE
|
5296 INSTANCE_TYPE_UNINSTANT
);
5297 if (ret
!= LDB_SUCCESS
) {
5298 talloc_free(tmp_ctx
);
5299 return ldb_oom(ldb
);
5302 ret
= dsdb_add(ldb
, msg
, DSDB_MODIFY_PARTIAL_REPLICA
);
5303 if (ret
!= LDB_SUCCESS
&& ret
!= LDB_ERR_ENTRY_ALREADY_EXISTS
) {
5304 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
5305 ldb_dn_get_linearized(dn
),
5306 ldb_errstring(ldb
), ldb_strerror(ret
)));
5307 talloc_free(tmp_ctx
);
5311 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn
)));
5313 talloc_free(tmp_ctx
);
5318 * Return the effective badPwdCount
5320 * This requires that the user_msg have (if present):
5324 * This also requires that the domain_msg have (if present):
5325 * - lockOutObservationWindow
5327 static int dsdb_effective_badPwdCount(const struct ldb_message
*user_msg
,
5328 int64_t lockOutObservationWindow
,
5331 int64_t badPasswordTime
;
5332 badPasswordTime
= ldb_msg_find_attr_as_int64(user_msg
, "badPasswordTime", 0);
5334 if (badPasswordTime
- lockOutObservationWindow
>= now
) {
5335 return ldb_msg_find_attr_as_int(user_msg
, "badPwdCount", 0);
5342 * Returns a user's PSO, or NULL if none was found
5344 static struct ldb_result
*lookup_user_pso(struct ldb_context
*sam_ldb
,
5345 TALLOC_CTX
*mem_ctx
,
5346 const struct ldb_message
*user_msg
,
5347 const char * const *attrs
)
5349 struct ldb_result
*res
= NULL
;
5350 struct ldb_dn
*pso_dn
= NULL
;
5353 /* if the user has a PSO that applies, then use the PSO's setting */
5354 pso_dn
= ldb_msg_find_attr_as_dn(sam_ldb
, mem_ctx
, user_msg
,
5355 "msDS-ResultantPSO");
5357 if (pso_dn
!= NULL
) {
5359 ret
= dsdb_search_dn(sam_ldb
, mem_ctx
, &res
, pso_dn
, attrs
, 0);
5360 if (ret
!= LDB_SUCCESS
) {
5363 * log the error. The caller should fallback to using
5364 * the default domain password settings
5366 DBG_ERR("Error retrieving msDS-ResultantPSO %s for %s",
5367 ldb_dn_get_linearized(pso_dn
),
5368 ldb_dn_get_linearized(user_msg
->dn
));
5370 talloc_free(pso_dn
);
5376 * Return the effective badPwdCount
5378 * This requires that the user_msg have (if present):
5381 * - msDS-ResultantPSO
5383 int samdb_result_effective_badPwdCount(struct ldb_context
*sam_ldb
,
5384 TALLOC_CTX
*mem_ctx
,
5385 struct ldb_dn
*domain_dn
,
5386 const struct ldb_message
*user_msg
)
5388 struct timeval tv_now
= timeval_current();
5389 NTTIME now
= timeval_to_nttime(&tv_now
);
5390 int64_t lockOutObservationWindow
;
5391 struct ldb_result
*res
= NULL
;
5392 const char *attrs
[] = { "msDS-LockoutObservationWindow",
5395 res
= lookup_user_pso(sam_ldb
, mem_ctx
, user_msg
, attrs
);
5398 lockOutObservationWindow
=
5399 ldb_msg_find_attr_as_int64(res
->msgs
[0],
5400 "msDS-LockoutObservationWindow",
5401 DEFAULT_OBSERVATION_WINDOW
);
5405 /* no PSO was found, lookup the default domain setting */
5406 lockOutObservationWindow
=
5407 samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
,
5408 "lockOutObservationWindow", NULL
);
5411 return dsdb_effective_badPwdCount(user_msg
, lockOutObservationWindow
, now
);
5415 * Returns the lockoutThreshold that applies. If a PSO is specified, then that
5416 * setting is used over the domain defaults
5418 static int64_t get_lockout_threshold(struct ldb_message
*domain_msg
,
5419 struct ldb_message
*pso_msg
)
5421 if (pso_msg
!= NULL
) {
5422 return ldb_msg_find_attr_as_int(pso_msg
,
5423 "msDS-LockoutThreshold", 0);
5425 return ldb_msg_find_attr_as_int(domain_msg
,
5426 "lockoutThreshold", 0);
5431 * Returns the lockOutObservationWindow that applies. If a PSO is specified,
5432 * then that setting is used over the domain defaults
5434 static int64_t get_lockout_observation_window(struct ldb_message
*domain_msg
,
5435 struct ldb_message
*pso_msg
)
5437 if (pso_msg
!= NULL
) {
5438 return ldb_msg_find_attr_as_int64(pso_msg
,
5439 "msDS-LockoutObservationWindow",
5440 DEFAULT_OBSERVATION_WINDOW
);
5442 return ldb_msg_find_attr_as_int64(domain_msg
,
5443 "lockOutObservationWindow",
5444 DEFAULT_OBSERVATION_WINDOW
);
5449 * Prepare an update to the badPwdCount and associated attributes.
5451 * This requires that the user_msg have (if present):
5456 * This also requires that the domain_msg have (if present):
5458 * - lockoutThreshold
5459 * - lockOutObservationWindow
5461 * This also requires that the pso_msg have (if present):
5462 * - msDS-LockoutThreshold
5463 * - msDS-LockoutObservationWindow
5465 NTSTATUS
dsdb_update_bad_pwd_count(TALLOC_CTX
*mem_ctx
,
5466 struct ldb_context
*sam_ctx
,
5467 struct ldb_message
*user_msg
,
5468 struct ldb_message
*domain_msg
,
5469 struct ldb_message
*pso_msg
,
5470 struct ldb_message
**_mod_msg
)
5472 int ret
, badPwdCount
;
5474 int64_t lockoutThreshold
, lockOutObservationWindow
;
5475 struct dom_sid
*sid
;
5476 struct timeval tv_now
= timeval_current();
5477 NTTIME now
= timeval_to_nttime(&tv_now
);
5479 uint32_t pwdProperties
, rid
= 0;
5480 struct ldb_message
*mod_msg
;
5482 sid
= samdb_result_dom_sid(mem_ctx
, user_msg
, "objectSid");
5484 pwdProperties
= ldb_msg_find_attr_as_uint(domain_msg
,
5485 "pwdProperties", -1);
5486 if (sid
&& !(pwdProperties
& DOMAIN_PASSWORD_LOCKOUT_ADMINS
)) {
5487 status
= dom_sid_split_rid(NULL
, sid
, NULL
, &rid
);
5488 if (!NT_STATUS_IS_OK(status
)) {
5490 * This can't happen anyway, but always try
5491 * and update the badPwdCount on failure
5499 * Work out if we are doing password lockout on the domain.
5500 * Also, the built in administrator account is exempt:
5501 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa375371%28v=vs.85%29.aspx
5503 lockoutThreshold
= get_lockout_threshold(domain_msg
, pso_msg
);
5504 if (lockoutThreshold
== 0 || (rid
== DOMAIN_RID_ADMINISTRATOR
)) {
5505 DEBUG(5, ("Not updating badPwdCount on %s after wrong password\n",
5506 ldb_dn_get_linearized(user_msg
->dn
)));
5507 return NT_STATUS_OK
;
5510 mod_msg
= ldb_msg_new(mem_ctx
);
5511 if (mod_msg
== NULL
) {
5512 return NT_STATUS_NO_MEMORY
;
5514 mod_msg
->dn
= ldb_dn_copy(mod_msg
, user_msg
->dn
);
5515 if (mod_msg
->dn
== NULL
) {
5516 TALLOC_FREE(mod_msg
);
5517 return NT_STATUS_NO_MEMORY
;
5520 lockOutObservationWindow
= get_lockout_observation_window(domain_msg
,
5523 badPwdCount
= dsdb_effective_badPwdCount(user_msg
, lockOutObservationWindow
, now
);
5527 ret
= samdb_msg_add_int(sam_ctx
, mod_msg
, mod_msg
, "badPwdCount", badPwdCount
);
5528 if (ret
!= LDB_SUCCESS
) {
5529 TALLOC_FREE(mod_msg
);
5530 return NT_STATUS_NO_MEMORY
;
5532 ret
= samdb_msg_add_int64(sam_ctx
, mod_msg
, mod_msg
, "badPasswordTime", now
);
5533 if (ret
!= LDB_SUCCESS
) {
5534 TALLOC_FREE(mod_msg
);
5535 return NT_STATUS_NO_MEMORY
;
5538 if (badPwdCount
>= lockoutThreshold
) {
5539 ret
= samdb_msg_add_int64(sam_ctx
, mod_msg
, mod_msg
, "lockoutTime", now
);
5540 if (ret
!= LDB_SUCCESS
) {
5541 TALLOC_FREE(mod_msg
);
5542 return NT_STATUS_NO_MEMORY
;
5544 DEBUGC( DBGC_AUTH
, 1, ("Locked out user %s after %d wrong passwords\n",
5545 ldb_dn_get_linearized(user_msg
->dn
), badPwdCount
));
5547 DEBUGC( DBGC_AUTH
, 5, ("Updated badPwdCount on %s after %d wrong passwords\n",
5548 ldb_dn_get_linearized(user_msg
->dn
), badPwdCount
));
5551 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
5552 for (i
=0; i
< mod_msg
->num_elements
; i
++) {
5553 mod_msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
5556 *_mod_msg
= mod_msg
;
5557 return NT_STATUS_OK
;
5561 * Sets defaults for a User object
5562 * List of default attributes set:
5563 * accountExpires, badPasswordTime, badPwdCount,
5564 * codePage, countryCode, lastLogoff, lastLogon
5565 * logonCount, pwdLastSet
5567 int dsdb_user_obj_set_defaults(struct ldb_context
*ldb
,
5568 struct ldb_message
*usr_obj
,
5569 struct ldb_request
*req
)
5573 const struct attribute_values
{
5576 const char *add_value
;
5577 const char *mod_value
;
5578 const char *control
;
5583 .name
= "accountExpires",
5584 .add_value
= "9223372036854775807",
5588 .name
= "badPasswordTime",
5592 .name
= "badPwdCount",
5600 .name
= "countryCode",
5604 .name
= "lastLogoff",
5608 .name
= "lastLogon",
5612 .name
= "logonCount",
5616 .name
= "logonHours",
5617 .add_flags
= DSDB_FLAG_INTERNAL_FORCE_META_DATA
,
5620 .name
= "pwdLastSet",
5622 .control
= DSDB_CONTROL_PASSWORD_DEFAULT_LAST_SET_OID
,
5625 .name
= "adminCount",
5629 .name
= "operatorCount",
5634 for (i
= 0; i
< ARRAY_SIZE(map
); i
++) {
5636 const char *value
= NULL
;
5639 if (req
!= NULL
&& req
->operation
== LDB_ADD
) {
5640 value
= map
[i
].add_value
;
5641 flags
= map
[i
].add_flags
;
5643 value
= map
[i
].mod_value
;
5644 flags
= map
[i
].mod_flags
;
5647 if (value
== NULL
) {
5648 value
= map
[i
].value
;
5651 if (value
!= NULL
) {
5652 flags
|= LDB_FLAG_MOD_ADD
;
5659 ret
= samdb_find_or_add_attribute_ex(ldb
, usr_obj
,
5663 if (ret
!= LDB_SUCCESS
) {
5667 if (req
!= NULL
&& added
&& map
[i
].control
!= NULL
) {
5668 ret
= ldb_request_add_control(req
,
5671 if (ret
!= LDB_SUCCESS
) {
5681 * Sets 'sAMAccountType on user object based on userAccountControl
5682 * @param ldb Current ldb_context
5683 * @param usr_obj ldb_message representing User object
5684 * @param user_account_control Value for userAccountControl flags
5685 * @param account_type_p Optional pointer to account_type to return
5686 * @return LDB_SUCCESS or LDB_ERR* code on failure
5688 int dsdb_user_obj_set_account_type(struct ldb_context
*ldb
, struct ldb_message
*usr_obj
,
5689 uint32_t user_account_control
, uint32_t *account_type_p
)
5692 uint32_t account_type
;
5693 struct ldb_message_element
*el
;
5695 account_type
= ds_uf2atype(user_account_control
);
5696 if (account_type
== 0) {
5697 ldb_set_errstring(ldb
, "dsdb: Unrecognized account type!");
5698 return LDB_ERR_UNWILLING_TO_PERFORM
;
5700 ret
= samdb_msg_add_uint(ldb
, usr_obj
, usr_obj
,
5703 if (ret
!= LDB_SUCCESS
) {
5706 el
= ldb_msg_find_element(usr_obj
, "sAMAccountType");
5707 el
->flags
= LDB_FLAG_MOD_REPLACE
;
5709 if (account_type_p
) {
5710 *account_type_p
= account_type
;
5717 * Determine and set primaryGroupID based on userAccountControl value
5718 * @param ldb Current ldb_context
5719 * @param usr_obj ldb_message representing User object
5720 * @param user_account_control Value for userAccountControl flags
5721 * @param group_rid_p Optional pointer to group RID to return
5722 * @return LDB_SUCCESS or LDB_ERR* code on failure
5724 int dsdb_user_obj_set_primary_group_id(struct ldb_context
*ldb
, struct ldb_message
*usr_obj
,
5725 uint32_t user_account_control
, uint32_t *group_rid_p
)
5729 struct ldb_message_element
*el
;
5731 rid
= ds_uf2prim_group_rid(user_account_control
);
5733 ret
= samdb_msg_add_uint(ldb
, usr_obj
, usr_obj
,
5734 "primaryGroupID", rid
);
5735 if (ret
!= LDB_SUCCESS
) {
5738 el
= ldb_msg_find_element(usr_obj
, "primaryGroupID");
5739 el
->flags
= LDB_FLAG_MOD_REPLACE
;
5749 * Returns True if the source and target DNs both have the same naming context,
5750 * i.e. they're both in the same partition.
5752 bool dsdb_objects_have_same_nc(struct ldb_context
*ldb
,
5753 TALLOC_CTX
*mem_ctx
,
5754 struct ldb_dn
*source_dn
,
5755 struct ldb_dn
*target_dn
)
5757 TALLOC_CTX
*tmp_ctx
;
5758 struct ldb_dn
*source_nc
= NULL
;
5759 struct ldb_dn
*target_nc
= NULL
;
5761 bool same_nc
= true;
5763 tmp_ctx
= talloc_new(mem_ctx
);
5765 ret
= dsdb_find_nc_root(ldb
, tmp_ctx
, source_dn
, &source_nc
);
5766 /* fix clang warning */
5767 if (source_nc
== NULL
) {
5768 ret
= LDB_ERR_OTHER
;
5770 if (ret
!= LDB_SUCCESS
) {
5771 DBG_ERR("Failed to find base DN for source %s\n",
5772 ldb_dn_get_linearized(source_dn
));
5773 talloc_free(tmp_ctx
);
5777 ret
= dsdb_find_nc_root(ldb
, tmp_ctx
, target_dn
, &target_nc
);
5778 /* fix clang warning */
5779 if (target_nc
== NULL
) {
5780 ret
= LDB_ERR_OTHER
;
5782 if (ret
!= LDB_SUCCESS
) {
5783 DBG_ERR("Failed to find base DN for target %s\n",
5784 ldb_dn_get_linearized(target_dn
));
5785 talloc_free(tmp_ctx
);
5789 same_nc
= (ldb_dn_compare(source_nc
, target_nc
) == 0);
5791 talloc_free(tmp_ctx
);
5796 * Context for dsdb_count_domain_callback
5798 struct dsdb_count_domain_context
{
5800 * Number of matching records
5804 * sid of the domain that the records must belong to.
5805 * if NULL records can belong to any domain.
5807 struct dom_sid
*dom_sid
;
5811 * @brief ldb aysnc callback for dsdb_domain_count.
5813 * count the number of records in the database matching an LDAP query,
5814 * optionally filtering for domain membership.
5816 * @param [in,out] req the ldb request being processed
5817 * req->context contains:
5818 * count The number of matching records
5819 * dom_sid The domain sid, if present records must belong
5820 * to the domain to be counted.
5821 *@param [in,out] ares The query result.
5823 * @return an LDB error code
5826 static int dsdb_count_domain_callback(
5827 struct ldb_request
*req
,
5828 struct ldb_reply
*ares
)
5832 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
5834 if (ares
->error
!= LDB_SUCCESS
) {
5835 int error
= ares
->error
;
5837 return ldb_request_done(req
, error
);
5840 switch (ares
->type
) {
5841 case LDB_REPLY_ENTRY
:
5843 struct dsdb_count_domain_context
*context
= NULL
;
5847 const struct ldb_val
*v
;
5849 context
= req
->context
;
5850 if (context
->dom_sid
== NULL
) {
5855 v
= ldb_msg_find_ldb_val(ares
->message
, "objectSid");
5860 ret
= sid_parse(v
->data
, v
->length
, &sid
);
5865 in_domain
= dom_sid_in_domain(context
->dom_sid
, &sid
);
5873 case LDB_REPLY_REFERRAL
:
5876 case LDB_REPLY_DONE
:
5878 return ldb_request_done(req
, LDB_SUCCESS
);
5887 * @brief Count the number of records matching a query.
5889 * Count the number of entries in the database matching the supplied query,
5890 * optionally filtering only those entries belonging to the supplied domain.
5892 * @param ldb [in] Current ldb context
5893 * @param count [out] Pointer to the count
5894 * @param base [in] The base dn for the quey
5895 * @param dom_sid [in] The domain sid, if non NULL records that are not a member
5896 * of the domain are ignored.
5897 * @param scope [in] Search scope.
5898 * @param exp_fmt [in] format string for the query.
5900 * @return LDB_STATUS code.
5902 int PRINTF_ATTRIBUTE(6, 7) dsdb_domain_count(
5903 struct ldb_context
*ldb
,
5905 struct ldb_dn
*base
,
5906 struct dom_sid
*dom_sid
,
5907 enum ldb_scope scope
,
5908 const char *exp_fmt
, ...)
5910 TALLOC_CTX
*tmp_ctx
= NULL
;
5911 struct ldb_request
*req
= NULL
;
5912 struct dsdb_count_domain_context
*context
= NULL
;
5913 char *expression
= NULL
;
5914 const char *object_sid
[] = {"objectSid", NULL
};
5915 const char *none
[] = {NULL
};
5920 tmp_ctx
= talloc_new(ldb
);
5922 context
= talloc_zero(tmp_ctx
, struct dsdb_count_domain_context
);
5923 if (context
== NULL
) {
5924 return LDB_ERR_OPERATIONS_ERROR
;
5926 context
->dom_sid
= dom_sid
;
5929 va_start(ap
, exp_fmt
);
5930 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
5933 if (expression
== NULL
) {
5934 TALLOC_FREE(context
);
5935 TALLOC_FREE(tmp_ctx
);
5936 return LDB_ERR_OPERATIONS_ERROR
;
5940 ret
= ldb_build_search_req(
5947 (dom_sid
== NULL
) ? none
: object_sid
,
5950 dsdb_count_domain_callback
,
5952 ldb_req_set_location(req
, "dsdb_domain_count");
5954 if (ret
!= LDB_SUCCESS
) goto done
;
5956 ret
= ldb_request(ldb
, req
);
5958 if (ret
== LDB_SUCCESS
) {
5959 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
5960 if (ret
== LDB_SUCCESS
) {
5961 *count
= context
->count
;
5967 TALLOC_FREE(expression
);
5969 TALLOC_FREE(context
);
5970 TALLOC_FREE(tmp_ctx
);