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 "lib/util/tsort.h"
43 #include "dsdb/common/util.h"
44 #include "lib/socket/socket.h"
45 #include "librpc/gen_ndr/irpc.h"
46 #include "libds/common/flag_mapping.h"
49 search the sam for the specified attributes in a specific domain, filter on
50 objectSid being in domain_sid.
52 int samdb_search_domain(struct ldb_context
*sam_ldb
,
54 struct ldb_dn
*basedn
,
55 struct ldb_message
***res
,
56 const char * const *attrs
,
57 const struct dom_sid
*domain_sid
,
58 const char *format
, ...) _PRINTF_ATTRIBUTE(7,8)
64 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
,
65 res
, attrs
, format
, ap
);
71 struct dom_sid
*entry_sid
;
73 entry_sid
= samdb_result_dom_sid(mem_ctx
, (*res
)[i
], "objectSid");
75 if ((entry_sid
== NULL
) ||
76 (!dom_sid_in_domain(domain_sid
, entry_sid
))) {
77 /* Delete that entry from the result set */
78 (*res
)[i
] = (*res
)[count
-1];
80 talloc_free(entry_sid
);
83 talloc_free(entry_sid
);
91 search the sam for a single string attribute in exactly 1 record
93 const char *samdb_search_string_v(struct ldb_context
*sam_ldb
,
95 struct ldb_dn
*basedn
,
96 const char *attr_name
,
97 const char *format
, va_list ap
) _PRINTF_ATTRIBUTE(5,0)
100 const char *attrs
[2] = { NULL
, NULL
};
101 struct ldb_message
**res
= NULL
;
103 attrs
[0] = attr_name
;
105 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
107 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
108 attr_name
, format
, count
));
115 return ldb_msg_find_attr_as_string(res
[0], attr_name
, NULL
);
119 search the sam for a single string attribute in exactly 1 record
121 const char *samdb_search_string(struct ldb_context
*sam_ldb
,
123 struct ldb_dn
*basedn
,
124 const char *attr_name
,
125 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
130 va_start(ap
, format
);
131 str
= samdb_search_string_v(sam_ldb
, mem_ctx
, basedn
, attr_name
, format
, ap
);
137 struct ldb_dn
*samdb_search_dn(struct ldb_context
*sam_ldb
,
139 struct ldb_dn
*basedn
,
140 const char *format
, ...) _PRINTF_ATTRIBUTE(4,5)
144 struct ldb_message
**res
= NULL
;
147 va_start(ap
, format
);
148 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, NULL
, format
, ap
);
151 if (count
!= 1) return NULL
;
153 ret
= talloc_steal(mem_ctx
, res
[0]->dn
);
160 search the sam for a dom_sid attribute in exactly 1 record
162 struct dom_sid
*samdb_search_dom_sid(struct ldb_context
*sam_ldb
,
164 struct ldb_dn
*basedn
,
165 const char *attr_name
,
166 const char *format
, ...) _PRINTF_ATTRIBUTE(5,6)
170 struct ldb_message
**res
;
171 const char *attrs
[2] = { NULL
, NULL
};
174 attrs
[0] = attr_name
;
176 va_start(ap
, format
);
177 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
180 DEBUG(1,("samdb: search for %s %s not single valued (count=%d)\n",
181 attr_name
, format
, count
));
187 sid
= samdb_result_dom_sid(mem_ctx
, res
[0], attr_name
);
193 return the count of the number of records in the sam matching the query
195 int samdb_search_count(struct ldb_context
*sam_ldb
,
197 struct ldb_dn
*basedn
,
198 const char *format
, ...) _PRINTF_ATTRIBUTE(4,5)
201 const char *attrs
[] = { NULL
};
204 va_start(ap
, format
);
205 ret
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, NULL
, attrs
, format
, ap
);
213 search the sam for a single integer attribute in exactly 1 record
215 unsigned int samdb_search_uint(struct ldb_context
*sam_ldb
,
217 unsigned int default_value
,
218 struct ldb_dn
*basedn
,
219 const char *attr_name
,
220 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
224 struct ldb_message
**res
;
225 const char *attrs
[2] = { NULL
, NULL
};
227 attrs
[0] = attr_name
;
229 va_start(ap
, format
);
230 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
234 return default_value
;
237 return ldb_msg_find_attr_as_uint(res
[0], attr_name
, default_value
);
241 search the sam for a single signed 64 bit integer attribute in exactly 1 record
243 int64_t samdb_search_int64(struct ldb_context
*sam_ldb
,
245 int64_t default_value
,
246 struct ldb_dn
*basedn
,
247 const char *attr_name
,
248 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
252 struct ldb_message
**res
;
253 const char *attrs
[2] = { NULL
, NULL
};
255 attrs
[0] = attr_name
;
257 va_start(ap
, format
);
258 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
262 return default_value
;
265 return ldb_msg_find_attr_as_int64(res
[0], attr_name
, default_value
);
269 search the sam for multipe records each giving a single string attribute
270 return the number of matches, or -1 on error
272 int samdb_search_string_multiple(struct ldb_context
*sam_ldb
,
274 struct ldb_dn
*basedn
,
276 const char *attr_name
,
277 const char *format
, ...) _PRINTF_ATTRIBUTE(6,7)
281 const char *attrs
[2] = { NULL
, NULL
};
282 struct ldb_message
**res
= NULL
;
284 attrs
[0] = attr_name
;
286 va_start(ap
, format
);
287 count
= gendb_search_v(sam_ldb
, mem_ctx
, basedn
, &res
, attrs
, format
, ap
);
294 /* make sure its single valued */
295 for (i
=0;i
<count
;i
++) {
296 if (res
[i
]->num_elements
!= 1) {
297 DEBUG(1,("samdb: search for %s %s not single valued\n",
304 *strs
= talloc_array(mem_ctx
, const char *, count
+1);
310 for (i
=0;i
<count
;i
++) {
311 (*strs
)[i
] = ldb_msg_find_attr_as_string(res
[i
], attr_name
, NULL
);
313 (*strs
)[count
] = NULL
;
318 struct ldb_dn
*samdb_result_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
319 const char *attr
, struct ldb_dn
*default_value
)
321 struct ldb_dn
*ret_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, msg
, attr
);
323 return default_value
;
329 pull a rid from a objectSid in a result set.
331 uint32_t samdb_result_rid_from_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
332 const char *attr
, uint32_t default_value
)
337 sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
339 return default_value
;
341 rid
= sid
->sub_auths
[sid
->num_auths
-1];
347 pull a dom_sid structure from a objectSid in a result set.
349 struct dom_sid
*samdb_result_dom_sid(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
353 const struct ldb_val
*v
;
355 v
= ldb_msg_find_ldb_val(msg
, attr
);
359 sid
= talloc(mem_ctx
, struct dom_sid
);
363 ok
= sid_blob_parse(*v
, sid
);
372 pull a guid structure from a objectGUID in a result set.
374 struct GUID
samdb_result_guid(const struct ldb_message
*msg
, const char *attr
)
376 const struct ldb_val
*v
;
380 v
= ldb_msg_find_ldb_val(msg
, attr
);
381 if (!v
) return GUID_zero();
383 status
= GUID_from_ndr_blob(v
, &guid
);
384 if (!NT_STATUS_IS_OK(status
)) {
392 pull a sid prefix from a objectSid in a result set.
393 this is used to find the domain sid for a user
395 struct dom_sid
*samdb_result_sid_prefix(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
398 struct dom_sid
*sid
= samdb_result_dom_sid(mem_ctx
, msg
, attr
);
399 if (!sid
|| sid
->num_auths
< 1) return NULL
;
405 pull a NTTIME in a result set.
407 NTTIME
samdb_result_nttime(const struct ldb_message
*msg
, const char *attr
,
408 NTTIME default_value
)
410 return ldb_msg_find_attr_as_uint64(msg
, attr
, default_value
);
414 * Windows stores 0 for lastLogoff.
415 * But when a MS DC return the lastLogoff (as Logoff Time)
416 * it returns 0x7FFFFFFFFFFFFFFF, not returning this value in this case
417 * cause windows 2008 and newer version to fail for SMB requests
419 NTTIME
samdb_result_last_logoff(const struct ldb_message
*msg
)
421 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "lastLogoff",0);
424 ret
= 0x7FFFFFFFFFFFFFFFULL
;
430 * Windows uses both 0 and 9223372036854775807 (0x7FFFFFFFFFFFFFFFULL) to
431 * indicate an account doesn't expire.
433 * When Windows initially creates an account, it sets
434 * accountExpires = 9223372036854775807 (0x7FFFFFFFFFFFFFFF). However,
435 * when changing from an account having a specific expiration date to
436 * that account never expiring, it sets accountExpires = 0.
438 * Consolidate that logic here to allow clearer logic for account expiry in
439 * the rest of the code.
441 NTTIME
samdb_result_account_expires(const struct ldb_message
*msg
)
443 NTTIME ret
= ldb_msg_find_attr_as_uint64(msg
, "accountExpires",
447 ret
= 0x7FFFFFFFFFFFFFFFULL
;
453 construct the allow_password_change field from the PwdLastSet attribute and the
454 domain password settings
456 NTTIME
samdb_result_allow_password_change(struct ldb_context
*sam_ldb
,
458 struct ldb_dn
*domain_dn
,
459 struct ldb_message
*msg
,
462 uint64_t attr_time
= ldb_msg_find_attr_as_uint64(msg
, attr
, 0);
465 if (attr_time
== 0) {
469 minPwdAge
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
, "minPwdAge", NULL
);
471 /* yes, this is a -= not a += as minPwdAge is stored as the negative
472 of the number of 100-nano-seconds */
473 attr_time
-= minPwdAge
;
479 construct the force_password_change field from the PwdLastSet
480 attribute, the userAccountControl and the domain password settings
482 NTTIME
samdb_result_force_password_change(struct ldb_context
*sam_ldb
,
484 struct ldb_dn
*domain_dn
,
485 struct ldb_message
*msg
)
487 int64_t attr_time
= ldb_msg_find_attr_as_int64(msg
, "pwdLastSet", 0);
488 uint32_t userAccountControl
= ldb_msg_find_attr_as_uint(msg
,
489 "userAccountControl",
493 /* Machine accounts don't expire, and there is a flag for 'no expiry' */
494 if (!(userAccountControl
& UF_NORMAL_ACCOUNT
)
495 || (userAccountControl
& UF_DONT_EXPIRE_PASSWD
)) {
496 return 0x7FFFFFFFFFFFFFFFULL
;
499 if (attr_time
== 0) {
502 if (attr_time
== -1) {
503 return 0x7FFFFFFFFFFFFFFFULL
;
506 maxPwdAge
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
,
508 if (maxPwdAge
== 0 || maxPwdAge
== -0x8000000000000000ULL
) {
509 return 0x7FFFFFFFFFFFFFFFULL
;
511 attr_time
-= maxPwdAge
;
518 pull a samr_Password structutre from a result set.
520 struct samr_Password
*samdb_result_hash(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
, const char *attr
)
522 struct samr_Password
*hash
= NULL
;
523 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
524 if (val
&& (val
->length
>= sizeof(hash
->hash
))) {
525 hash
= talloc(mem_ctx
, struct samr_Password
);
526 memcpy(hash
->hash
, val
->data
, MIN(val
->length
, sizeof(hash
->hash
)));
532 pull an array of samr_Password structures from a result set.
534 unsigned int samdb_result_hashes(TALLOC_CTX
*mem_ctx
, const struct ldb_message
*msg
,
535 const char *attr
, struct samr_Password
**hashes
)
537 unsigned int count
, i
;
538 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
544 count
= val
->length
/ 16;
549 *hashes
= talloc_array(mem_ctx
, struct samr_Password
, count
);
554 for (i
=0;i
<count
;i
++) {
555 memcpy((*hashes
)[i
].hash
, (i
*16)+(char *)val
->data
, 16);
561 NTSTATUS
samdb_result_passwords_from_history(TALLOC_CTX
*mem_ctx
,
562 struct loadparm_context
*lp_ctx
,
563 struct ldb_message
*msg
,
565 struct samr_Password
**lm_pwd
,
566 struct samr_Password
**nt_pwd
)
568 struct samr_Password
*lmPwdHash
, *ntPwdHash
;
572 num_nt
= samdb_result_hashes(mem_ctx
, msg
, "ntPwdHistory", &ntPwdHash
);
576 *nt_pwd
= &ntPwdHash
[idx
];
580 /* Ensure that if we have turned off LM
581 * authentication, that we never use the LM hash, even
583 if (lpcfg_lanman_auth(lp_ctx
)) {
585 num_lm
= samdb_result_hashes(mem_ctx
, msg
, "lmPwdHistory", &lmPwdHash
);
589 *lm_pwd
= &lmPwdHash
[idx
];
598 NTSTATUS
samdb_result_passwords_no_lockout(TALLOC_CTX
*mem_ctx
,
599 struct loadparm_context
*lp_ctx
,
600 struct ldb_message
*msg
,
601 struct samr_Password
**lm_pwd
,
602 struct samr_Password
**nt_pwd
)
604 struct samr_Password
*lmPwdHash
, *ntPwdHash
;
608 num_nt
= samdb_result_hashes(mem_ctx
, msg
, "unicodePwd", &ntPwdHash
);
611 } else if (num_nt
> 1) {
612 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
614 *nt_pwd
= &ntPwdHash
[0];
618 /* Ensure that if we have turned off LM
619 * authentication, that we never use the LM hash, even
621 if (lpcfg_lanman_auth(lp_ctx
)) {
623 num_lm
= samdb_result_hashes(mem_ctx
, msg
, "dBCSPwd", &lmPwdHash
);
626 } else if (num_lm
> 1) {
627 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
629 *lm_pwd
= &lmPwdHash
[0];
638 NTSTATUS
samdb_result_passwords(TALLOC_CTX
*mem_ctx
,
639 struct loadparm_context
*lp_ctx
,
640 struct ldb_message
*msg
,
641 struct samr_Password
**lm_pwd
,
642 struct samr_Password
**nt_pwd
)
646 acct_flags
= samdb_result_acct_flags(msg
,
647 "msDS-User-Account-Control-Computed");
648 /* Quit if the account was locked out. */
649 if (acct_flags
& ACB_AUTOLOCK
) {
650 DEBUG(3,("samdb_result_passwords: Account for user %s was locked out.\n",
651 ldb_dn_get_linearized(msg
->dn
)));
652 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
655 return samdb_result_passwords_no_lockout(mem_ctx
, lp_ctx
, msg
,
660 pull a samr_LogonHours structutre from a result set.
662 struct samr_LogonHours
samdb_result_logon_hours(TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
, const char *attr
)
664 struct samr_LogonHours hours
;
665 size_t units_per_week
= 168;
666 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
671 units_per_week
= val
->length
* 8;
674 hours
.bits
= talloc_array(mem_ctx
, uint8_t, units_per_week
/8);
678 hours
.units_per_week
= units_per_week
;
679 memset(hours
.bits
, 0xFF, units_per_week
/8);
681 memcpy(hours
.bits
, val
->data
, val
->length
);
688 pull a set of account_flags from a result set.
690 Naturally, this requires that userAccountControl and
691 (if not null) the attributes 'attr' be already
694 uint32_t samdb_result_acct_flags(struct ldb_message
*msg
, const char *attr
)
696 uint32_t userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
697 uint32_t attr_flags
= 0;
698 uint32_t acct_flags
= ds_uf2acb(userAccountControl
);
700 attr_flags
= ldb_msg_find_attr_as_uint(msg
, attr
, UF_ACCOUNTDISABLE
);
701 if (attr_flags
== UF_ACCOUNTDISABLE
) {
702 DEBUG(0, ("Attribute %s not found, disabling account %s!\n", attr
,
703 ldb_dn_get_linearized(msg
->dn
)));
705 acct_flags
|= ds_uf2acb(attr_flags
);
711 NTSTATUS
samdb_result_parameters(TALLOC_CTX
*mem_ctx
,
712 struct ldb_message
*msg
,
714 struct lsa_BinaryString
*s
)
717 const struct ldb_val
*val
= ldb_msg_find_ldb_val(msg
, attr
);
725 if ((val
->length
% 2) != 0) {
727 * If the on-disk data is not even in length, we know
728 * it is corrupt, and can not be safely pushed. We
729 * would either truncate, send either a un-initilaised
730 * byte or send a forced zero byte
732 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
735 s
->array
= talloc_array(mem_ctx
, uint16_t, val
->length
/2);
737 return NT_STATUS_NO_MEMORY
;
739 s
->length
= s
->size
= val
->length
;
741 /* The on-disk format is the 'network' format, being UTF16LE (sort of) */
742 for (i
= 0; i
< s
->length
/ 2; i
++) {
743 s
->array
[i
] = SVAL(val
->data
, i
* 2);
749 /* Find an attribute, with a particular value */
751 /* The current callers of this function expect a very specific
752 * behaviour: In particular, objectClass subclass equivilance is not
753 * wanted. This means that we should not lookup the schema for the
754 * comparison function */
755 struct ldb_message_element
*samdb_find_attribute(struct ldb_context
*ldb
,
756 const struct ldb_message
*msg
,
757 const char *name
, const char *value
)
760 struct ldb_message_element
*el
= ldb_msg_find_element(msg
, name
);
766 for (i
=0;i
<el
->num_values
;i
++) {
767 if (ldb_attr_cmp(value
, (char *)el
->values
[i
].data
) == 0) {
775 int samdb_find_or_add_attribute(struct ldb_context
*ldb
, struct ldb_message
*msg
, const char *name
, const char *set_value
)
778 struct ldb_message_element
*el
;
780 el
= ldb_msg_find_element(msg
, name
);
785 ret
= ldb_msg_add_string(msg
, name
, set_value
);
786 if (ret
!= LDB_SUCCESS
) {
789 msg
->elements
[msg
->num_elements
- 1].flags
= LDB_FLAG_MOD_ADD
;
794 add a dom_sid element to a message
796 int samdb_msg_add_dom_sid(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
797 const char *attr_name
, const struct dom_sid
*sid
)
800 enum ndr_err_code ndr_err
;
802 ndr_err
= ndr_push_struct_blob(&v
, mem_ctx
,
804 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
805 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
806 return ldb_operr(sam_ldb
);
808 return ldb_msg_add_value(msg
, attr_name
, &v
, NULL
);
813 add a delete element operation to a message
815 int samdb_msg_add_delete(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
816 const char *attr_name
)
818 /* we use an empty replace rather than a delete, as it allows for
819 dsdb_replace() to be used everywhere */
820 return ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_REPLACE
, NULL
);
824 add an add attribute value to a message or enhance an existing attribute
825 which has the same name and the add flag set.
827 int samdb_msg_add_addval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
828 struct ldb_message
*msg
, const char *attr_name
,
831 struct ldb_message_element
*el
;
832 struct ldb_val val
, *vals
;
838 v
= talloc_strdup(mem_ctx
, value
);
840 return ldb_oom(sam_ldb
);
843 val
.data
= (uint8_t *) v
;
844 val
.length
= strlen(v
);
846 if (val
.length
== 0) {
847 /* allow empty strings as non-existent attributes */
851 for (i
= 0; i
< msg
->num_elements
; i
++) {
852 el
= &msg
->elements
[i
];
853 if ((ldb_attr_cmp(el
->name
, attr_name
) == 0) &&
854 (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_ADD
)) {
860 ret
= ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_ADD
,
862 if (ret
!= LDB_SUCCESS
) {
867 vals
= talloc_realloc(msg
->elements
, el
->values
, struct ldb_val
,
870 return ldb_oom(sam_ldb
);
873 el
->values
[el
->num_values
] = val
;
880 add a delete attribute value to a message or enhance an existing attribute
881 which has the same name and the delete flag set.
883 int samdb_msg_add_delval(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
884 struct ldb_message
*msg
, const char *attr_name
,
887 struct ldb_message_element
*el
;
888 struct ldb_val val
, *vals
;
894 v
= talloc_strdup(mem_ctx
, value
);
896 return ldb_oom(sam_ldb
);
899 val
.data
= (uint8_t *) v
;
900 val
.length
= strlen(v
);
902 if (val
.length
== 0) {
903 /* allow empty strings as non-existent attributes */
907 for (i
= 0; i
< msg
->num_elements
; i
++) {
908 el
= &msg
->elements
[i
];
909 if ((ldb_attr_cmp(el
->name
, attr_name
) == 0) &&
910 (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
)) {
916 ret
= ldb_msg_add_empty(msg
, attr_name
, LDB_FLAG_MOD_DELETE
,
918 if (ret
!= LDB_SUCCESS
) {
923 vals
= talloc_realloc(msg
->elements
, el
->values
, struct ldb_val
,
926 return ldb_oom(sam_ldb
);
929 el
->values
[el
->num_values
] = val
;
936 add a int element to a message
938 int samdb_msg_add_int(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
939 const char *attr_name
, int v
)
941 const char *s
= talloc_asprintf(mem_ctx
, "%d", v
);
943 return ldb_oom(sam_ldb
);
945 return ldb_msg_add_string(msg
, attr_name
, s
);
949 * Add an unsigned int element to a message
951 * The issue here is that we have not yet first cast to int32_t explicitly,
952 * before we cast to an signed int to printf() into the %d or cast to a
953 * int64_t before we then cast to a long long to printf into a %lld.
955 * There are *no* unsigned integers in Active Directory LDAP, even the RID
956 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
957 * (See the schema, and the syntax definitions in schema_syntax.c).
960 int samdb_msg_add_uint(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
961 const char *attr_name
, unsigned int v
)
963 return samdb_msg_add_int(sam_ldb
, mem_ctx
, msg
, attr_name
, (int)v
);
967 add a (signed) int64_t element to a message
969 int samdb_msg_add_int64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
970 const char *attr_name
, int64_t v
)
972 const char *s
= talloc_asprintf(mem_ctx
, "%lld", (long long)v
);
974 return ldb_oom(sam_ldb
);
976 return ldb_msg_add_string(msg
, attr_name
, s
);
980 * Add an unsigned int64_t (uint64_t) element to a message
982 * The issue here is that we have not yet first cast to int32_t explicitly,
983 * before we cast to an signed int to printf() into the %d or cast to a
984 * int64_t before we then cast to a long long to printf into a %lld.
986 * There are *no* unsigned integers in Active Directory LDAP, even the RID
987 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
988 * (See the schema, and the syntax definitions in schema_syntax.c).
991 int samdb_msg_add_uint64(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
992 const char *attr_name
, uint64_t v
)
994 return samdb_msg_add_int64(sam_ldb
, mem_ctx
, msg
, attr_name
, (int64_t)v
);
998 add a samr_Password element to a message
1000 int samdb_msg_add_hash(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1001 const char *attr_name
, const struct samr_Password
*hash
)
1004 val
.data
= talloc_memdup(mem_ctx
, hash
->hash
, 16);
1006 return ldb_oom(sam_ldb
);
1009 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
1013 add a samr_Password array to a message
1015 int samdb_msg_add_hashes(struct ldb_context
*ldb
,
1016 TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1017 const char *attr_name
, struct samr_Password
*hashes
,
1022 val
.data
= talloc_array_size(mem_ctx
, 16, count
);
1023 val
.length
= count
*16;
1025 return ldb_oom(ldb
);
1027 for (i
=0;i
<count
;i
++) {
1028 memcpy(i
*16 + (char *)val
.data
, hashes
[i
].hash
, 16);
1030 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
1034 add a acct_flags element to a message
1036 int samdb_msg_add_acct_flags(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1037 const char *attr_name
, uint32_t v
)
1039 return samdb_msg_add_uint(sam_ldb
, mem_ctx
, msg
, attr_name
, ds_acb2uf(v
));
1043 add a logon_hours element to a message
1045 int samdb_msg_add_logon_hours(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1046 const char *attr_name
, struct samr_LogonHours
*hours
)
1049 val
.length
= hours
->units_per_week
/ 8;
1050 val
.data
= hours
->bits
;
1051 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
1055 add a parameters element to a message
1057 int samdb_msg_add_parameters(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_message
*msg
,
1058 const char *attr_name
, struct lsa_BinaryString
*parameters
)
1062 if ((parameters
->length
% 2) != 0) {
1063 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
1066 val
.data
= talloc_array(mem_ctx
, uint8_t, parameters
->length
);
1067 if (val
.data
== NULL
) {
1068 return LDB_ERR_OPERATIONS_ERROR
;
1070 val
.length
= parameters
->length
;
1071 for (i
= 0; i
< parameters
->length
/ 2; i
++) {
1073 * The on-disk format needs to be in the 'network'
1074 * format, parmeters->array is a uint16_t array of
1075 * length parameters->length / 2
1077 SSVAL(val
.data
, i
* 2, parameters
->array
[i
]);
1079 return ldb_msg_add_steal_value(msg
, attr_name
, &val
);
1083 * Sets an unsigned int element in a message
1085 * The issue here is that we have not yet first cast to int32_t explicitly,
1086 * before we cast to an signed int to printf() into the %d or cast to a
1087 * int64_t before we then cast to a long long to printf into a %lld.
1089 * There are *no* unsigned integers in Active Directory LDAP, even the RID
1090 * allocations and ms-DS-Secondary-KrbTgt-Number are *signed* quantities.
1091 * (See the schema, and the syntax definitions in schema_syntax.c).
1094 int samdb_msg_set_uint(struct ldb_context
*sam_ldb
, TALLOC_CTX
*mem_ctx
,
1095 struct ldb_message
*msg
, const char *attr_name
,
1098 struct ldb_message_element
*el
;
1100 el
= ldb_msg_find_element(msg
, attr_name
);
1104 return samdb_msg_add_uint(sam_ldb
, mem_ctx
, msg
, attr_name
, v
);
1108 * Handle ldb_request in transaction
1110 int dsdb_autotransaction_request(struct ldb_context
*sam_ldb
,
1111 struct ldb_request
*req
)
1115 ret
= ldb_transaction_start(sam_ldb
);
1116 if (ret
!= LDB_SUCCESS
) {
1120 ret
= ldb_request(sam_ldb
, req
);
1121 if (ret
== LDB_SUCCESS
) {
1122 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1125 if (ret
== LDB_SUCCESS
) {
1126 return ldb_transaction_commit(sam_ldb
);
1128 ldb_transaction_cancel(sam_ldb
);
1134 return a default security descriptor
1136 struct security_descriptor
*samdb_default_security_descriptor(TALLOC_CTX
*mem_ctx
)
1138 struct security_descriptor
*sd
;
1140 sd
= security_descriptor_initialise(mem_ctx
);
1145 struct ldb_dn
*samdb_aggregate_schema_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1147 struct ldb_dn
*schema_dn
= ldb_get_schema_basedn(sam_ctx
);
1148 struct ldb_dn
*aggregate_dn
;
1153 aggregate_dn
= ldb_dn_copy(mem_ctx
, schema_dn
);
1154 if (!aggregate_dn
) {
1157 if (!ldb_dn_add_child_fmt(aggregate_dn
, "CN=Aggregate")) {
1160 return aggregate_dn
;
1163 struct ldb_dn
*samdb_partitions_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1165 struct ldb_dn
*new_dn
;
1167 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_config_basedn(sam_ctx
));
1168 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Partitions")) {
1169 talloc_free(new_dn
);
1175 struct ldb_dn
*samdb_infrastructure_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1177 struct ldb_dn
*new_dn
;
1179 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_default_basedn(sam_ctx
));
1180 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Infrastructure")) {
1181 talloc_free(new_dn
);
1187 struct ldb_dn
*samdb_sites_dn(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
)
1189 struct ldb_dn
*new_dn
;
1191 new_dn
= ldb_dn_copy(mem_ctx
, ldb_get_config_basedn(sam_ctx
));
1192 if ( ! ldb_dn_add_child_fmt(new_dn
, "CN=Sites")) {
1193 talloc_free(new_dn
);
1200 work out the domain sid for the current open ldb
1202 const struct dom_sid
*samdb_domain_sid(struct ldb_context
*ldb
)
1204 TALLOC_CTX
*tmp_ctx
;
1205 const struct dom_sid
*domain_sid
;
1206 const char *attrs
[] = {
1210 struct ldb_result
*res
;
1213 /* see if we have a cached copy */
1214 domain_sid
= (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1219 tmp_ctx
= talloc_new(ldb
);
1220 if (tmp_ctx
== NULL
) {
1224 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_get_default_basedn(ldb
), LDB_SCOPE_BASE
, attrs
, "objectSid=*");
1226 if (ret
!= LDB_SUCCESS
) {
1230 if (res
->count
!= 1) {
1234 domain_sid
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSid");
1235 if (domain_sid
== NULL
) {
1239 /* cache the domain_sid in the ldb */
1240 if (ldb_set_opaque(ldb
, "cache.domain_sid", discard_const_p(struct dom_sid
, domain_sid
)) != LDB_SUCCESS
) {
1244 talloc_steal(ldb
, domain_sid
);
1245 talloc_free(tmp_ctx
);
1250 talloc_free(tmp_ctx
);
1255 get domain sid from cache
1257 const struct dom_sid
*samdb_domain_sid_cache_only(struct ldb_context
*ldb
)
1259 return (struct dom_sid
*)ldb_get_opaque(ldb
, "cache.domain_sid");
1262 bool samdb_set_domain_sid(struct ldb_context
*ldb
, const struct dom_sid
*dom_sid_in
)
1264 TALLOC_CTX
*tmp_ctx
;
1265 struct dom_sid
*dom_sid_new
;
1266 struct dom_sid
*dom_sid_old
;
1268 /* see if we have a cached copy */
1269 dom_sid_old
= talloc_get_type(ldb_get_opaque(ldb
,
1270 "cache.domain_sid"), struct dom_sid
);
1272 tmp_ctx
= talloc_new(ldb
);
1273 if (tmp_ctx
== NULL
) {
1277 dom_sid_new
= dom_sid_dup(tmp_ctx
, dom_sid_in
);
1282 /* cache the domain_sid in the ldb */
1283 if (ldb_set_opaque(ldb
, "cache.domain_sid", dom_sid_new
) != LDB_SUCCESS
) {
1287 talloc_steal(ldb
, dom_sid_new
);
1288 talloc_free(tmp_ctx
);
1289 talloc_free(dom_sid_old
);
1294 DEBUG(1,("Failed to set our own cached domain SID in the ldb!\n"));
1295 talloc_free(tmp_ctx
);
1299 bool samdb_set_ntds_settings_dn(struct ldb_context
*ldb
, struct ldb_dn
*ntds_settings_dn_in
)
1301 TALLOC_CTX
*tmp_ctx
;
1302 struct ldb_dn
*ntds_settings_dn_new
;
1303 struct ldb_dn
*ntds_settings_dn_old
;
1305 /* see if we have a forced copy from provision */
1306 ntds_settings_dn_old
= talloc_get_type(ldb_get_opaque(ldb
,
1307 "forced.ntds_settings_dn"), struct ldb_dn
);
1309 tmp_ctx
= talloc_new(ldb
);
1310 if (tmp_ctx
== NULL
) {
1314 ntds_settings_dn_new
= ldb_dn_copy(tmp_ctx
, ntds_settings_dn_in
);
1315 if (!ntds_settings_dn_new
) {
1319 /* set the DN in the ldb to avoid lookups during provision */
1320 if (ldb_set_opaque(ldb
, "forced.ntds_settings_dn", ntds_settings_dn_new
) != LDB_SUCCESS
) {
1324 talloc_steal(ldb
, ntds_settings_dn_new
);
1325 talloc_free(tmp_ctx
);
1326 talloc_free(ntds_settings_dn_old
);
1331 DEBUG(1,("Failed to set our NTDS Settings DN in the ldb!\n"));
1332 talloc_free(tmp_ctx
);
1337 work out the ntds settings dn for the current open ldb
1339 struct ldb_dn
*samdb_ntds_settings_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1341 TALLOC_CTX
*tmp_ctx
;
1342 const char *root_attrs
[] = { "dsServiceName", NULL
};
1344 struct ldb_result
*root_res
;
1345 struct ldb_dn
*settings_dn
;
1347 /* see if we have a cached copy */
1348 settings_dn
= (struct ldb_dn
*)ldb_get_opaque(ldb
, "forced.ntds_settings_dn");
1350 return ldb_dn_copy(mem_ctx
, settings_dn
);
1353 tmp_ctx
= talloc_new(mem_ctx
);
1354 if (tmp_ctx
== NULL
) {
1358 ret
= ldb_search(ldb
, tmp_ctx
, &root_res
, ldb_dn_new(tmp_ctx
, ldb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
1359 if (ret
!= LDB_SUCCESS
) {
1360 DEBUG(1,("Searching for dsServiceName in rootDSE failed: %s\n",
1361 ldb_errstring(ldb
)));
1365 if (root_res
->count
!= 1) {
1369 settings_dn
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, root_res
->msgs
[0], "dsServiceName");
1371 /* note that we do not cache the DN here, as that would mean
1372 * we could not handle server renames at runtime. Only
1373 * provision sets up forced.ntds_settings_dn */
1375 talloc_steal(mem_ctx
, settings_dn
);
1376 talloc_free(tmp_ctx
);
1381 DEBUG(1,("Failed to find our own NTDS Settings DN in the ldb!\n"));
1382 talloc_free(tmp_ctx
);
1387 work out the ntds settings invocationId for the current open ldb
1389 const struct GUID
*samdb_ntds_invocation_id(struct ldb_context
*ldb
)
1391 TALLOC_CTX
*tmp_ctx
;
1392 const char *attrs
[] = { "invocationId", NULL
};
1394 struct ldb_result
*res
;
1395 struct GUID
*invocation_id
;
1397 /* see if we have a cached copy */
1398 invocation_id
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.invocation_id");
1399 if (invocation_id
) {
1400 SMB_ASSERT(!GUID_all_zero(invocation_id
));
1401 return invocation_id
;
1404 tmp_ctx
= talloc_new(ldb
);
1405 if (tmp_ctx
== NULL
) {
1409 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
, tmp_ctx
), LDB_SCOPE_BASE
, attrs
, NULL
);
1414 if (res
->count
!= 1) {
1418 invocation_id
= talloc(tmp_ctx
, struct GUID
);
1419 if (!invocation_id
) {
1423 *invocation_id
= samdb_result_guid(res
->msgs
[0], "invocationId");
1424 if (GUID_all_zero(invocation_id
)) {
1425 if (ldb_msg_find_ldb_val(res
->msgs
[0], "invocationId")) {
1426 DEBUG(0, ("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1428 DEBUG(0, ("Failed to find parse own NTDS Settings invocationId from the ldb!\n"));
1433 /* cache the domain_sid in the ldb */
1434 if (ldb_set_opaque(ldb
, "cache.invocation_id", invocation_id
) != LDB_SUCCESS
) {
1438 talloc_steal(ldb
, invocation_id
);
1439 talloc_free(tmp_ctx
);
1441 return invocation_id
;
1444 DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
1445 talloc_free(tmp_ctx
);
1449 bool samdb_set_ntds_invocation_id(struct ldb_context
*ldb
, const struct GUID
*invocation_id_in
)
1451 TALLOC_CTX
*tmp_ctx
;
1452 struct GUID
*invocation_id_new
;
1453 struct GUID
*invocation_id_old
;
1455 /* see if we have a cached copy */
1456 invocation_id_old
= (struct GUID
*)ldb_get_opaque(ldb
,
1457 "cache.invocation_id");
1459 tmp_ctx
= talloc_new(ldb
);
1460 if (tmp_ctx
== NULL
) {
1464 invocation_id_new
= talloc(tmp_ctx
, struct GUID
);
1465 if (!invocation_id_new
) {
1469 SMB_ASSERT(!GUID_all_zero(invocation_id_in
));
1470 *invocation_id_new
= *invocation_id_in
;
1472 /* cache the domain_sid in the ldb */
1473 if (ldb_set_opaque(ldb
, "cache.invocation_id", invocation_id_new
) != LDB_SUCCESS
) {
1477 talloc_steal(ldb
, invocation_id_new
);
1478 talloc_free(tmp_ctx
);
1479 talloc_free(invocation_id_old
);
1484 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1485 talloc_free(tmp_ctx
);
1490 work out the ntds settings objectGUID for the current open ldb
1492 const struct GUID
*samdb_ntds_objectGUID(struct ldb_context
*ldb
)
1494 TALLOC_CTX
*tmp_ctx
;
1495 const char *attrs
[] = { "objectGUID", NULL
};
1497 struct ldb_result
*res
;
1498 struct GUID
*ntds_guid
;
1500 /* see if we have a cached copy */
1501 ntds_guid
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.ntds_guid");
1506 tmp_ctx
= talloc_new(ldb
);
1507 if (tmp_ctx
== NULL
) {
1511 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
, tmp_ctx
), LDB_SCOPE_BASE
, attrs
, NULL
);
1516 if (res
->count
!= 1) {
1520 ntds_guid
= talloc(tmp_ctx
, struct GUID
);
1525 *ntds_guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
1527 /* cache the domain_sid in the ldb */
1528 if (ldb_set_opaque(ldb
, "cache.ntds_guid", ntds_guid
) != LDB_SUCCESS
) {
1532 talloc_steal(ldb
, ntds_guid
);
1533 talloc_free(tmp_ctx
);
1538 DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
1539 talloc_free(tmp_ctx
);
1543 bool samdb_set_ntds_objectGUID(struct ldb_context
*ldb
, const struct GUID
*ntds_guid_in
)
1545 TALLOC_CTX
*tmp_ctx
;
1546 struct GUID
*ntds_guid_new
;
1547 struct GUID
*ntds_guid_old
;
1549 /* see if we have a cached copy */
1550 ntds_guid_old
= (struct GUID
*)ldb_get_opaque(ldb
, "cache.ntds_guid");
1552 tmp_ctx
= talloc_new(ldb
);
1553 if (tmp_ctx
== NULL
) {
1557 ntds_guid_new
= talloc(tmp_ctx
, struct GUID
);
1558 if (!ntds_guid_new
) {
1562 *ntds_guid_new
= *ntds_guid_in
;
1564 /* cache the domain_sid in the ldb */
1565 if (ldb_set_opaque(ldb
, "cache.ntds_guid", ntds_guid_new
) != LDB_SUCCESS
) {
1569 talloc_steal(ldb
, ntds_guid_new
);
1570 talloc_free(tmp_ctx
);
1571 talloc_free(ntds_guid_old
);
1576 DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
1577 talloc_free(tmp_ctx
);
1582 work out the server dn for the current open ldb
1584 struct ldb_dn
*samdb_server_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1586 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
1591 dn
= ldb_dn_get_parent(mem_ctx
, samdb_ntds_settings_dn(ldb
, tmp_ctx
));
1592 talloc_free(tmp_ctx
);
1598 work out the server dn for the current open ldb
1600 struct ldb_dn
*samdb_server_site_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1602 struct ldb_dn
*server_dn
;
1603 struct ldb_dn
*servers_dn
;
1604 struct ldb_dn
*server_site_dn
;
1606 /* TODO: there must be a saner way to do this!! */
1607 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1608 if (!server_dn
) return NULL
;
1610 servers_dn
= ldb_dn_get_parent(mem_ctx
, server_dn
);
1611 talloc_free(server_dn
);
1612 if (!servers_dn
) return NULL
;
1614 server_site_dn
= ldb_dn_get_parent(mem_ctx
, servers_dn
);
1615 talloc_free(servers_dn
);
1617 return server_site_dn
;
1621 find the site name from a computers DN record
1623 int samdb_find_site_for_computer(struct ldb_context
*ldb
,
1624 TALLOC_CTX
*mem_ctx
, struct ldb_dn
*computer_dn
,
1625 const char **site_name
)
1629 const struct ldb_val
*rdn_val
;
1633 ret
= samdb_reference_dn(ldb
, mem_ctx
, computer_dn
, "serverReferenceBL", &dn
);
1634 if (ret
!= LDB_SUCCESS
) {
1638 if (!ldb_dn_remove_child_components(dn
, 2)) {
1640 return LDB_ERR_INVALID_DN_SYNTAX
;
1643 rdn_val
= ldb_dn_get_rdn_val(dn
);
1644 if (rdn_val
== NULL
) {
1645 return LDB_ERR_OPERATIONS_ERROR
;
1648 (*site_name
) = talloc_strndup(mem_ctx
, (const char *)rdn_val
->data
, rdn_val
->length
);
1651 return LDB_ERR_OPERATIONS_ERROR
;
1657 find the NTDS GUID from a computers DN record
1659 int samdb_find_ntdsguid_for_computer(struct ldb_context
*ldb
, struct ldb_dn
*computer_dn
,
1660 struct GUID
*ntds_guid
)
1665 *ntds_guid
= GUID_zero();
1667 ret
= samdb_reference_dn(ldb
, ldb
, computer_dn
, "serverReferenceBL", &dn
);
1668 if (ret
!= LDB_SUCCESS
) {
1672 if (!ldb_dn_add_child_fmt(dn
, "CN=NTDS Settings")) {
1674 return LDB_ERR_OPERATIONS_ERROR
;
1677 ret
= dsdb_find_guid_by_dn(ldb
, dn
, ntds_guid
);
1683 find a 'reference' DN that points at another object
1684 (eg. serverReference, rIDManagerReference etc)
1686 int samdb_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*base
,
1687 const char *attribute
, struct ldb_dn
**dn
)
1689 const char *attrs
[2];
1690 struct ldb_result
*res
;
1693 attrs
[0] = attribute
;
1696 ret
= dsdb_search(ldb
, mem_ctx
, &res
, base
, LDB_SCOPE_BASE
, attrs
, DSDB_SEARCH_ONE_ONLY
|DSDB_SEARCH_SHOW_EXTENDED_DN
, NULL
);
1697 if (ret
!= LDB_SUCCESS
) {
1698 ldb_asprintf_errstring(ldb
, "Cannot find DN %s to get attribute %s for reference dn: %s",
1699 ldb_dn_get_linearized(base
), attribute
, ldb_errstring(ldb
));
1703 *dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, res
->msgs
[0], attribute
);
1705 if (!ldb_msg_find_element(res
->msgs
[0], attribute
)) {
1706 ldb_asprintf_errstring(ldb
, "Cannot find attribute %s of %s to calculate reference dn", attribute
,
1707 ldb_dn_get_linearized(base
));
1709 ldb_asprintf_errstring(ldb
, "Cannot interpret attribute %s of %s as a dn", attribute
,
1710 ldb_dn_get_linearized(base
));
1713 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
1721 find if a DN (must have GUID component!) is our ntdsDsa
1723 int samdb_dn_is_our_ntdsa(struct ldb_context
*ldb
, struct ldb_dn
*dn
, bool *is_ntdsa
)
1726 struct GUID dn_guid
;
1727 const struct GUID
*our_ntds_guid
;
1728 status
= dsdb_get_extended_dn_guid(dn
, &dn_guid
, "GUID");
1729 if (!NT_STATUS_IS_OK(status
)) {
1730 return LDB_ERR_OPERATIONS_ERROR
;
1733 our_ntds_guid
= samdb_ntds_objectGUID(ldb
);
1734 if (!our_ntds_guid
) {
1735 DEBUG(0, ("Failed to find our NTDS Settings GUID for comparison with %s - %s\n", ldb_dn_get_linearized(dn
), ldb_errstring(ldb
)));
1736 return LDB_ERR_OPERATIONS_ERROR
;
1739 *is_ntdsa
= GUID_equal(&dn_guid
, our_ntds_guid
);
1744 find a 'reference' DN that points at another object and indicate if it is our ntdsDsa
1746 int samdb_reference_dn_is_our_ntdsa(struct ldb_context
*ldb
, struct ldb_dn
*base
,
1747 const char *attribute
, bool *is_ntdsa
)
1750 struct ldb_dn
*referenced_dn
;
1751 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
1752 if (tmp_ctx
== NULL
) {
1753 return LDB_ERR_OPERATIONS_ERROR
;
1755 ret
= samdb_reference_dn(ldb
, tmp_ctx
, base
, attribute
, &referenced_dn
);
1756 if (ret
!= LDB_SUCCESS
) {
1757 DEBUG(0, ("Failed to find object %s for attribute %s - %s\n", ldb_dn_get_linearized(base
), attribute
, ldb_errstring(ldb
)));
1761 ret
= samdb_dn_is_our_ntdsa(ldb
, referenced_dn
, is_ntdsa
);
1763 talloc_free(tmp_ctx
);
1768 find our machine account via the serverReference attribute in the
1771 int samdb_server_reference_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1773 struct ldb_dn
*server_dn
;
1776 server_dn
= samdb_server_dn(ldb
, mem_ctx
);
1777 if (server_dn
== NULL
) {
1778 return LDB_ERR_NO_SUCH_OBJECT
;
1781 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_dn
, "serverReference", dn
);
1782 talloc_free(server_dn
);
1788 find the RID Manager$ DN via the rIDManagerReference attribute in the
1791 int samdb_rid_manager_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1793 return samdb_reference_dn(ldb
, mem_ctx
, ldb_get_default_basedn(ldb
),
1794 "rIDManagerReference", dn
);
1798 find the RID Set DN via the rIDSetReferences attribute in our
1801 int samdb_rid_set_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
**dn
)
1803 struct ldb_dn
*server_ref_dn
;
1806 ret
= samdb_server_reference_dn(ldb
, mem_ctx
, &server_ref_dn
);
1807 if (ret
!= LDB_SUCCESS
) {
1810 ret
= samdb_reference_dn(ldb
, mem_ctx
, server_ref_dn
, "rIDSetReferences", dn
);
1811 talloc_free(server_ref_dn
);
1815 const char *samdb_server_site_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
1817 const struct ldb_val
*val
= ldb_dn_get_rdn_val(samdb_server_site_dn(ldb
,
1824 return (const char *) val
->data
;
1828 * Finds the client site by using the client's IP address.
1829 * The "subnet_name" returns the name of the subnet if parameter != NULL
1831 const char *samdb_client_site_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1832 const char *ip_address
, char **subnet_name
)
1834 const char *attrs
[] = { "cn", "siteObject", NULL
};
1835 struct ldb_dn
*sites_container_dn
, *subnets_dn
, *sites_dn
;
1836 struct ldb_result
*res
;
1837 const struct ldb_val
*val
;
1838 const char *site_name
= NULL
, *l_subnet_name
= NULL
;
1839 const char *allow_list
[2] = { NULL
, NULL
};
1840 unsigned int i
, count
;
1844 * if we don't have a client ip e.g. ncalrpc
1845 * the server site is the client site
1847 if (ip_address
== NULL
) {
1848 return samdb_server_site_name(ldb
, mem_ctx
);
1851 sites_container_dn
= samdb_sites_dn(ldb
, mem_ctx
);
1852 if (sites_container_dn
== NULL
) {
1856 subnets_dn
= ldb_dn_copy(mem_ctx
, sites_container_dn
);
1857 if ( ! ldb_dn_add_child_fmt(subnets_dn
, "CN=Subnets")) {
1858 talloc_free(sites_container_dn
);
1859 talloc_free(subnets_dn
);
1863 ret
= ldb_search(ldb
, mem_ctx
, &res
, subnets_dn
, LDB_SCOPE_ONELEVEL
,
1865 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1867 } else if (ret
!= LDB_SUCCESS
) {
1868 talloc_free(sites_container_dn
);
1869 talloc_free(subnets_dn
);
1875 for (i
= 0; i
< count
; i
++) {
1876 l_subnet_name
= ldb_msg_find_attr_as_string(res
->msgs
[i
], "cn",
1879 allow_list
[0] = l_subnet_name
;
1881 if (socket_allow_access(mem_ctx
, NULL
, allow_list
, "", ip_address
)) {
1882 sites_dn
= ldb_msg_find_attr_as_dn(ldb
, mem_ctx
,
1885 if (sites_dn
== NULL
) {
1886 /* No reference, maybe another subnet matches */
1890 /* "val" cannot be NULL here since "sites_dn" != NULL */
1891 val
= ldb_dn_get_rdn_val(sites_dn
);
1892 site_name
= talloc_strdup(mem_ctx
,
1893 (const char *) val
->data
);
1895 talloc_free(sites_dn
);
1901 if (site_name
== NULL
) {
1902 /* This is the Windows Server fallback rule: when no subnet
1903 * exists and we have only one site available then use it (it
1904 * is for sure the same as our server site). If more sites do
1905 * exist then we don't know which one to use and set the site
1907 cnt
= samdb_search_count(ldb
, mem_ctx
, sites_container_dn
,
1908 "(objectClass=site)");
1910 site_name
= samdb_server_site_name(ldb
, mem_ctx
);
1912 site_name
= talloc_strdup(mem_ctx
, "");
1914 l_subnet_name
= NULL
;
1917 if (subnet_name
!= NULL
) {
1918 *subnet_name
= talloc_strdup(mem_ctx
, l_subnet_name
);
1921 talloc_free(sites_container_dn
);
1922 talloc_free(subnets_dn
);
1929 work out if we are the PDC for the domain of the current open ldb
1931 bool samdb_is_pdc(struct ldb_context
*ldb
)
1936 ret
= samdb_reference_dn_is_our_ntdsa(ldb
, ldb_get_default_basedn(ldb
), "fsmoRoleOwner",
1938 if (ret
!= LDB_SUCCESS
) {
1939 DEBUG(1,("Failed to find if we are the PDC for this ldb: Searching for fSMORoleOwner in %s failed: %s\n",
1940 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)),
1941 ldb_errstring(ldb
)));
1949 work out if we are a Global Catalog server for the domain of the current open ldb
1951 bool samdb_is_gc(struct ldb_context
*ldb
)
1954 if (samdb_ntds_options(ldb
, &options
) != LDB_SUCCESS
) {
1957 return (options
& DS_NTDSDSA_OPT_IS_GC
) != 0;
1960 /* Find a domain object in the parents of a particular DN. */
1961 int samdb_search_for_parent_domain(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
1962 struct ldb_dn
**parent_dn
, const char **errstring
)
1964 TALLOC_CTX
*local_ctx
;
1965 struct ldb_dn
*sdn
= dn
;
1966 struct ldb_result
*res
= NULL
;
1967 int ret
= LDB_SUCCESS
;
1968 const char *attrs
[] = { NULL
};
1970 local_ctx
= talloc_new(mem_ctx
);
1971 if (local_ctx
== NULL
) return ldb_oom(ldb
);
1973 while ((sdn
= ldb_dn_get_parent(local_ctx
, sdn
))) {
1974 ret
= ldb_search(ldb
, local_ctx
, &res
, sdn
, LDB_SCOPE_BASE
, attrs
,
1975 "(|(objectClass=domain)(objectClass=builtinDomain))");
1976 if (ret
== LDB_SUCCESS
) {
1977 if (res
->count
== 1) {
1985 if (ret
!= LDB_SUCCESS
) {
1986 *errstring
= talloc_asprintf(mem_ctx
, "Error searching for parent domain of %s, failed searching for %s: %s",
1987 ldb_dn_get_linearized(dn
),
1988 ldb_dn_get_linearized(sdn
),
1989 ldb_errstring(ldb
));
1990 talloc_free(local_ctx
);
1993 if (res
->count
!= 1) {
1994 *errstring
= talloc_asprintf(mem_ctx
, "Invalid dn (%s), not child of a domain object",
1995 ldb_dn_get_linearized(dn
));
1996 DEBUG(0,(__location__
": %s\n", *errstring
));
1997 talloc_free(local_ctx
);
1998 return LDB_ERR_CONSTRAINT_VIOLATION
;
2001 *parent_dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
2002 talloc_free(local_ctx
);
2008 * Performs checks on a user password (plaintext UNIX format - attribute
2009 * "password"). The remaining parameters have to be extracted from the domain
2012 * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
2014 enum samr_ValidationStatus
samdb_check_password(const DATA_BLOB
*utf8_blob
,
2015 const uint32_t pwdProperties
,
2016 const uint32_t minPwdLength
)
2018 const char *utf8_pw
= (const char *)utf8_blob
->data
;
2019 size_t utf8_len
= strlen_m(utf8_pw
);
2021 /* checks if the "minPwdLength" property is satisfied */
2022 if (minPwdLength
> utf8_len
) {
2023 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT
;
2026 /* checks the password complexity */
2027 if (!(pwdProperties
& DOMAIN_PASSWORD_COMPLEX
)) {
2028 return SAMR_VALIDATION_STATUS_SUCCESS
;
2031 if (utf8_len
== 0) {
2032 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH
;
2035 if (!check_password_quality(utf8_pw
)) {
2036 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH
;
2039 return SAMR_VALIDATION_STATUS_SUCCESS
;
2043 * Callback for "samdb_set_password" password change
2045 int samdb_set_password_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
2050 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
2053 if (ares
->error
!= LDB_SUCCESS
) {
2055 req
->context
= talloc_steal(req
,
2056 ldb_reply_get_control(ares
, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
));
2058 return ldb_request_done(req
, ret
);
2061 if (ares
->type
!= LDB_REPLY_DONE
) {
2063 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
2066 req
->context
= talloc_steal(req
,
2067 ldb_reply_get_control(ares
, DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
));
2069 return ldb_request_done(req
, LDB_SUCCESS
);
2073 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2074 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2075 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2076 * user change or not. The "rejectReason" gives some more information if the
2079 * Results: NT_STATUS_OK, NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2080 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION
2082 static NTSTATUS
samdb_set_password_internal(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2083 struct ldb_dn
*user_dn
, struct ldb_dn
*domain_dn
,
2084 const DATA_BLOB
*new_password
,
2085 const struct samr_Password
*lmNewHash
,
2086 const struct samr_Password
*ntNewHash
,
2087 const struct samr_Password
*lmOldHash
,
2088 const struct samr_Password
*ntOldHash
,
2089 enum samPwdChangeReason
*reject_reason
,
2090 struct samr_DomInfo1
**_dominfo
,
2091 bool permit_interdomain_trust
)
2093 struct ldb_message
*msg
;
2094 struct ldb_message_element
*el
;
2095 struct ldb_request
*req
;
2096 struct dsdb_control_password_change_status
*pwd_stat
= NULL
;
2098 bool hash_values
= false;
2099 NTSTATUS status
= NT_STATUS_OK
;
2101 #define CHECK_RET(x) \
2102 if (x != LDB_SUCCESS) { \
2104 return NT_STATUS_NO_MEMORY; \
2107 msg
= ldb_msg_new(mem_ctx
);
2109 return NT_STATUS_NO_MEMORY
;
2112 if ((new_password
!= NULL
)
2113 && ((lmNewHash
== NULL
) && (ntNewHash
== NULL
))) {
2114 /* we have the password as plaintext UTF16 */
2115 CHECK_RET(ldb_msg_add_value(msg
, "clearTextPassword",
2116 new_password
, NULL
));
2117 el
= ldb_msg_find_element(msg
, "clearTextPassword");
2118 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2119 } else if ((new_password
== NULL
)
2120 && ((lmNewHash
!= NULL
) || (ntNewHash
!= NULL
))) {
2121 /* we have a password as LM and/or NT hash */
2122 if (lmNewHash
!= NULL
) {
2123 CHECK_RET(samdb_msg_add_hash(ldb
, mem_ctx
, msg
,
2124 "dBCSPwd", lmNewHash
));
2125 el
= ldb_msg_find_element(msg
, "dBCSPwd");
2126 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2128 if (ntNewHash
!= NULL
) {
2129 CHECK_RET(samdb_msg_add_hash(ldb
, mem_ctx
, msg
,
2130 "unicodePwd", ntNewHash
));
2131 el
= ldb_msg_find_element(msg
, "unicodePwd");
2132 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2136 /* the password wasn't specified correctly */
2138 return NT_STATUS_INVALID_PARAMETER
;
2141 /* build modify request */
2142 ret
= ldb_build_mod_req(&req
, ldb
, mem_ctx
, msg
, NULL
, NULL
,
2143 samdb_set_password_callback
, NULL
);
2144 if (ret
!= LDB_SUCCESS
) {
2146 return NT_STATUS_NO_MEMORY
;
2149 /* A password change operation */
2150 if ((ntOldHash
!= NULL
) || (lmOldHash
!= NULL
)) {
2151 struct dsdb_control_password_change
*change
;
2153 change
= talloc(req
, struct dsdb_control_password_change
);
2154 if (change
== NULL
) {
2157 return NT_STATUS_NO_MEMORY
;
2160 change
->old_nt_pwd_hash
= ntOldHash
;
2161 change
->old_lm_pwd_hash
= lmOldHash
;
2163 ret
= ldb_request_add_control(req
,
2164 DSDB_CONTROL_PASSWORD_CHANGE_OID
,
2166 if (ret
!= LDB_SUCCESS
) {
2169 return NT_STATUS_NO_MEMORY
;
2173 ret
= ldb_request_add_control(req
,
2174 DSDB_CONTROL_PASSWORD_HASH_VALUES_OID
,
2176 if (ret
!= LDB_SUCCESS
) {
2179 return NT_STATUS_NO_MEMORY
;
2182 if (permit_interdomain_trust
) {
2183 ret
= ldb_request_add_control(req
,
2184 DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID
,
2186 if (ret
!= LDB_SUCCESS
) {
2189 return NT_STATUS_NO_MEMORY
;
2192 ret
= ldb_request_add_control(req
,
2193 DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID
,
2195 if (ret
!= LDB_SUCCESS
) {
2198 return NT_STATUS_NO_MEMORY
;
2201 ret
= dsdb_autotransaction_request(ldb
, req
);
2203 if (req
->context
!= NULL
) {
2204 struct ldb_control
*control
= talloc_get_type_abort(req
->context
,
2205 struct ldb_control
);
2206 pwd_stat
= talloc_get_type_abort(control
->data
,
2207 struct dsdb_control_password_change_status
);
2208 talloc_steal(mem_ctx
, pwd_stat
);
2214 /* Sets the domain info (if requested) */
2215 if (_dominfo
!= NULL
) {
2216 struct samr_DomInfo1
*dominfo
;
2218 dominfo
= talloc_zero(mem_ctx
, struct samr_DomInfo1
);
2219 if (dominfo
== NULL
) {
2220 return NT_STATUS_NO_MEMORY
;
2223 if (pwd_stat
!= NULL
) {
2224 dominfo
->min_password_length
= pwd_stat
->domain_data
.minPwdLength
;
2225 dominfo
->password_properties
= pwd_stat
->domain_data
.pwdProperties
;
2226 dominfo
->password_history_length
= pwd_stat
->domain_data
.pwdHistoryLength
;
2227 dominfo
->max_password_age
= pwd_stat
->domain_data
.maxPwdAge
;
2228 dominfo
->min_password_age
= pwd_stat
->domain_data
.minPwdAge
;
2231 *_dominfo
= dominfo
;
2234 if (reject_reason
!= NULL
) {
2235 if (pwd_stat
!= NULL
) {
2236 *reject_reason
= pwd_stat
->reject_reason
;
2238 *reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
2242 if (pwd_stat
!= NULL
) {
2243 talloc_free(pwd_stat
);
2246 if (ret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
2247 const char *errmsg
= ldb_errstring(ldb
);
2248 char *endptr
= NULL
;
2249 WERROR werr
= WERR_GENERAL_FAILURE
;
2250 status
= NT_STATUS_UNSUCCESSFUL
;
2251 if (errmsg
!= NULL
) {
2252 werr
= W_ERROR(strtol(errmsg
, &endptr
, 16));
2254 if (endptr
!= errmsg
) {
2255 if (W_ERROR_EQUAL(werr
, WERR_INVALID_PASSWORD
)) {
2256 status
= NT_STATUS_WRONG_PASSWORD
;
2258 if (W_ERROR_EQUAL(werr
, WERR_PASSWORD_RESTRICTION
)) {
2259 status
= NT_STATUS_PASSWORD_RESTRICTION
;
2262 } else if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
2263 /* don't let the caller know if an account doesn't exist */
2264 status
= NT_STATUS_WRONG_PASSWORD
;
2265 } else if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
2266 status
= NT_STATUS_ACCESS_DENIED
;
2267 } else if (ret
!= LDB_SUCCESS
) {
2268 DEBUG(1, ("Failed to set password on %s: %s\n",
2269 ldb_dn_get_linearized(msg
->dn
),
2270 ldb_errstring(ldb
)));
2271 status
= NT_STATUS_UNSUCCESSFUL
;
2277 NTSTATUS
samdb_set_password(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2278 struct ldb_dn
*user_dn
, struct ldb_dn
*domain_dn
,
2279 const DATA_BLOB
*new_password
,
2280 const struct samr_Password
*lmNewHash
,
2281 const struct samr_Password
*ntNewHash
,
2282 const struct samr_Password
*lmOldHash
,
2283 const struct samr_Password
*ntOldHash
,
2284 enum samPwdChangeReason
*reject_reason
,
2285 struct samr_DomInfo1
**_dominfo
)
2287 return samdb_set_password_internal(ldb
, mem_ctx
,
2290 lmNewHash
, ntNewHash
,
2291 lmOldHash
, ntOldHash
,
2292 reject_reason
, _dominfo
,
2293 false); /* reject trusts */
2297 * Sets the user password using plaintext UTF16 (attribute "new_password") or
2298 * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
2299 * the old LM and/or NT hash (attributes "lmOldHash"/"ntOldHash") if it is a
2300 * user change or not. The "rejectReason" gives some more information if the
2303 * This wrapper function for "samdb_set_password" takes a SID as input rather
2306 * This call encapsulates a new LDB transaction for changing the password;
2307 * therefore the user hasn't to start a new one.
2309 * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
2310 * NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
2311 * NT_STATUS_WRONG_PASSWORD, NT_STATUS_PASSWORD_RESTRICTION,
2312 * NT_STATUS_TRANSACTION_ABORTED, NT_STATUS_NO_SUCH_USER
2314 NTSTATUS
samdb_set_password_sid(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2315 const struct dom_sid
*user_sid
,
2316 const uint32_t *new_version
, /* optional for trusts */
2317 const DATA_BLOB
*new_password
,
2318 const struct samr_Password
*lmNewHash
,
2319 const struct samr_Password
*ntNewHash
,
2320 const struct samr_Password
*lmOldHash
,
2321 const struct samr_Password
*ntOldHash
,
2322 enum samPwdChangeReason
*reject_reason
,
2323 struct samr_DomInfo1
**_dominfo
)
2325 TALLOC_CTX
*frame
= talloc_stackframe();
2327 const char * const user_attrs
[] = {
2328 "userAccountControl",
2332 struct ldb_message
*user_msg
= NULL
;
2336 ret
= ldb_transaction_start(ldb
);
2337 if (ret
!= LDB_SUCCESS
) {
2338 DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb
)));
2340 return NT_STATUS_TRANSACTION_ABORTED
;
2343 ret
= dsdb_search_one(ldb
, frame
, &user_msg
, ldb_get_default_basedn(ldb
),
2344 LDB_SCOPE_SUBTREE
, user_attrs
, 0,
2345 "(&(objectSid=%s)(objectClass=user))",
2346 ldap_encode_ndr_dom_sid(frame
, user_sid
));
2347 if (ret
!= LDB_SUCCESS
) {
2348 ldb_transaction_cancel(ldb
);
2349 DEBUG(3, ("samdb_set_password_sid: SID[%s] not found in samdb %s - %s, "
2350 "returning NO_SUCH_USER\n",
2351 dom_sid_string(frame
, user_sid
),
2352 ldb_strerror(ret
), ldb_errstring(ldb
)));
2354 return NT_STATUS_NO_SUCH_USER
;
2357 uac
= ldb_msg_find_attr_as_uint(user_msg
, "userAccountControl", 0);
2358 if (!(uac
& UF_ACCOUNT_TYPE_MASK
)) {
2359 ldb_transaction_cancel(ldb
);
2360 DEBUG(1, ("samdb_set_password_sid: invalid "
2361 "userAccountControl[0x%08X] for SID[%s] DN[%s], "
2362 "returning NO_SUCH_USER\n",
2363 (unsigned)uac
, dom_sid_string(frame
, user_sid
),
2364 ldb_dn_get_linearized(user_msg
->dn
)));
2366 return NT_STATUS_NO_SUCH_USER
;
2369 if (uac
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
2370 const char * const tdo_attrs
[] = {
2371 "trustAuthIncoming",
2375 struct ldb_message
*tdo_msg
= NULL
;
2376 const char *account_name
= NULL
;
2377 uint32_t trust_direction
;
2379 const struct ldb_val
*old_val
= NULL
;
2380 struct trustAuthInOutBlob old_blob
= {};
2381 uint32_t old_version
= 0;
2382 struct AuthenticationInformation
*old_version_a
= NULL
;
2383 uint32_t _new_version
= 0;
2384 struct trustAuthInOutBlob new_blob
= {};
2385 struct ldb_val new_val
= {};
2386 struct timeval tv
= timeval_current();
2387 NTTIME now
= timeval_to_nttime(&tv
);
2388 enum ndr_err_code ndr_err
;
2390 if (new_password
== NULL
&& ntNewHash
== NULL
) {
2391 ldb_transaction_cancel(ldb
);
2392 DEBUG(1, ("samdb_set_password_sid: "
2393 "no new password provided "
2394 "sAMAccountName for SID[%s] DN[%s], "
2395 "returning INVALID_PARAMETER\n",
2396 dom_sid_string(frame
, user_sid
),
2397 ldb_dn_get_linearized(user_msg
->dn
)));
2399 return NT_STATUS_INVALID_PARAMETER
;
2402 if (new_password
!= NULL
&& ntNewHash
!= NULL
) {
2403 ldb_transaction_cancel(ldb
);
2404 DEBUG(1, ("samdb_set_password_sid: "
2405 "two new passwords provided "
2406 "sAMAccountName for SID[%s] DN[%s], "
2407 "returning INVALID_PARAMETER\n",
2408 dom_sid_string(frame
, user_sid
),
2409 ldb_dn_get_linearized(user_msg
->dn
)));
2411 return NT_STATUS_INVALID_PARAMETER
;
2414 if (new_password
!= NULL
&& (new_password
->length
% 2)) {
2415 ldb_transaction_cancel(ldb
);
2416 DEBUG(2, ("samdb_set_password_sid: "
2417 "invalid utf16 length (%zu) "
2418 "sAMAccountName for SID[%s] DN[%s], "
2419 "returning WRONG_PASSWORD\n",
2420 new_password
->length
,
2421 dom_sid_string(frame
, user_sid
),
2422 ldb_dn_get_linearized(user_msg
->dn
)));
2424 return NT_STATUS_WRONG_PASSWORD
;
2427 if (new_password
!= NULL
&& new_password
->length
>= 500) {
2428 ldb_transaction_cancel(ldb
);
2429 DEBUG(2, ("samdb_set_password_sid: "
2430 "utf16 password too long (%zu) "
2431 "sAMAccountName for SID[%s] DN[%s], "
2432 "returning WRONG_PASSWORD\n",
2433 new_password
->length
,
2434 dom_sid_string(frame
, user_sid
),
2435 ldb_dn_get_linearized(user_msg
->dn
)));
2437 return NT_STATUS_WRONG_PASSWORD
;
2440 account_name
= ldb_msg_find_attr_as_string(user_msg
,
2441 "sAMAccountName", NULL
);
2442 if (account_name
== NULL
) {
2443 ldb_transaction_cancel(ldb
);
2444 DEBUG(1, ("samdb_set_password_sid: missing "
2445 "sAMAccountName for SID[%s] DN[%s], "
2446 "returning NO_SUCH_USER\n",
2447 dom_sid_string(frame
, user_sid
),
2448 ldb_dn_get_linearized(user_msg
->dn
)));
2450 return NT_STATUS_NO_SUCH_USER
;
2453 nt_status
= dsdb_trust_search_tdo_by_type(ldb
,
2458 if (!NT_STATUS_IS_OK(nt_status
)) {
2459 ldb_transaction_cancel(ldb
);
2460 DEBUG(1, ("samdb_set_password_sid: dsdb_trust_search_tdo "
2461 "failed(%s) for sAMAccountName[%s] SID[%s] DN[%s], "
2462 "returning INTERNAL_DB_CORRUPTION\n",
2463 nt_errstr(nt_status
), account_name
,
2464 dom_sid_string(frame
, user_sid
),
2465 ldb_dn_get_linearized(user_msg
->dn
)));
2467 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2470 trust_direction
= ldb_msg_find_attr_as_int(tdo_msg
,
2471 "trustDirection", 0);
2472 if (!(trust_direction
& LSA_TRUST_DIRECTION_INBOUND
)) {
2473 ldb_transaction_cancel(ldb
);
2474 DEBUG(1, ("samdb_set_password_sid: direction[0x%08X] is "
2475 "not inbound for sAMAccountName[%s] "
2477 "returning INTERNAL_DB_CORRUPTION\n",
2478 (unsigned)trust_direction
,
2480 ldb_dn_get_linearized(user_msg
->dn
),
2481 ldb_dn_get_linearized(tdo_msg
->dn
)));
2483 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2486 old_val
= ldb_msg_find_ldb_val(tdo_msg
, "trustAuthIncoming");
2487 if (old_val
!= NULL
) {
2488 ndr_err
= ndr_pull_struct_blob(old_val
, frame
, &old_blob
,
2489 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
2490 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2491 ldb_transaction_cancel(ldb
);
2492 DEBUG(1, ("samdb_set_password_sid: "
2493 "failed(%s) to parse "
2494 "trustAuthOutgoing sAMAccountName[%s] "
2496 "returning INTERNAL_DB_CORRUPTION\n",
2497 ndr_map_error2string(ndr_err
),
2499 ldb_dn_get_linearized(user_msg
->dn
),
2500 ldb_dn_get_linearized(tdo_msg
->dn
)));
2503 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2507 for (i
= old_blob
.current
.count
; i
> 0; i
--) {
2508 struct AuthenticationInformation
*a
=
2509 &old_blob
.current
.array
[i
- 1];
2511 switch (a
->AuthType
) {
2512 case TRUST_AUTH_TYPE_NONE
:
2513 if (i
== old_blob
.current
.count
) {
2515 * remove TRUST_AUTH_TYPE_NONE at the
2518 old_blob
.current
.count
--;
2522 case TRUST_AUTH_TYPE_VERSION
:
2524 old_version
= a
->AuthInfo
.version
.version
;
2527 case TRUST_AUTH_TYPE_CLEAR
:
2530 case TRUST_AUTH_TYPE_NT4OWF
:
2535 if (new_version
== NULL
) {
2537 new_version
= &_new_version
;
2540 if (old_version_a
!= NULL
&& *new_version
!= (old_version
+ 1)) {
2541 old_version_a
->LastUpdateTime
= now
;
2542 old_version_a
->AuthType
= TRUST_AUTH_TYPE_NONE
;
2545 new_blob
.count
= MAX(old_blob
.current
.count
, 2);
2546 new_blob
.current
.array
= talloc_zero_array(frame
,
2547 struct AuthenticationInformation
,
2549 if (new_blob
.current
.array
== NULL
) {
2550 ldb_transaction_cancel(ldb
);
2552 return NT_STATUS_NO_MEMORY
;
2554 new_blob
.previous
.array
= talloc_zero_array(frame
,
2555 struct AuthenticationInformation
,
2557 if (new_blob
.current
.array
== NULL
) {
2558 ldb_transaction_cancel(ldb
);
2560 return NT_STATUS_NO_MEMORY
;
2563 for (i
= 0; i
< old_blob
.current
.count
; i
++) {
2564 struct AuthenticationInformation
*o
=
2565 &old_blob
.current
.array
[i
];
2566 struct AuthenticationInformation
*p
=
2567 &new_blob
.previous
.array
[i
];
2570 new_blob
.previous
.count
++;
2572 for (; i
< new_blob
.count
; i
++) {
2573 struct AuthenticationInformation
*pi
=
2574 &new_blob
.previous
.array
[i
];
2578 * new_blob.previous is still empty so
2579 * we'll do new_blob.previous = new_blob.current
2585 pi
->LastUpdateTime
= now
;
2586 pi
->AuthType
= TRUST_AUTH_TYPE_NONE
;
2587 new_blob
.previous
.count
++;
2590 for (i
= 0; i
< new_blob
.count
; i
++) {
2591 struct AuthenticationInformation
*ci
=
2592 &new_blob
.current
.array
[i
];
2594 ci
->LastUpdateTime
= now
;
2597 if (ntNewHash
!= NULL
) {
2598 ci
->AuthType
= TRUST_AUTH_TYPE_NT4OWF
;
2599 ci
->AuthInfo
.nt4owf
.password
= *ntNewHash
;
2603 ci
->AuthType
= TRUST_AUTH_TYPE_CLEAR
;
2604 ci
->AuthInfo
.clear
.size
= new_password
->length
;
2605 ci
->AuthInfo
.clear
.password
= new_password
->data
;
2608 ci
->AuthType
= TRUST_AUTH_TYPE_VERSION
;
2609 ci
->AuthInfo
.version
.version
= *new_version
;
2612 ci
->AuthType
= TRUST_AUTH_TYPE_NONE
;
2616 new_blob
.current
.count
++;
2619 if (new_blob
.previous
.count
== 0) {
2620 TALLOC_FREE(new_blob
.previous
.array
);
2621 new_blob
.previous
= new_blob
.current
;
2624 ndr_err
= ndr_push_struct_blob(&new_val
, frame
, &new_blob
,
2625 (ndr_push_flags_fn_t
)ndr_push_trustAuthInOutBlob
);
2626 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2627 ldb_transaction_cancel(ldb
);
2628 DEBUG(1, ("samdb_set_password_sid: "
2629 "failed(%s) to generate "
2630 "trustAuthOutgoing sAMAccountName[%s] "
2632 "returning UNSUCCESSFUL\n",
2633 ndr_map_error2string(ndr_err
),
2635 ldb_dn_get_linearized(user_msg
->dn
),
2636 ldb_dn_get_linearized(tdo_msg
->dn
)));
2638 return NT_STATUS_UNSUCCESSFUL
;
2641 tdo_msg
->num_elements
= 0;
2642 TALLOC_FREE(tdo_msg
->elements
);
2644 ret
= ldb_msg_add_empty(tdo_msg
, "trustAuthIncoming",
2645 LDB_FLAG_MOD_REPLACE
, NULL
);
2646 if (ret
!= LDB_SUCCESS
) {
2647 ldb_transaction_cancel(ldb
);
2649 return NT_STATUS_NO_MEMORY
;
2651 ret
= ldb_msg_add_value(tdo_msg
, "trustAuthIncoming",
2653 if (ret
!= LDB_SUCCESS
) {
2654 ldb_transaction_cancel(ldb
);
2656 return NT_STATUS_NO_MEMORY
;
2659 ret
= ldb_modify(ldb
, tdo_msg
);
2660 if (ret
!= LDB_SUCCESS
) {
2661 nt_status
= dsdb_ldb_err_to_ntstatus(ret
);
2662 ldb_transaction_cancel(ldb
);
2663 DEBUG(1, ("samdb_set_password_sid: "
2664 "failed to replace "
2665 "trustAuthOutgoing sAMAccountName[%s] "
2669 ldb_dn_get_linearized(user_msg
->dn
),
2670 ldb_dn_get_linearized(tdo_msg
->dn
),
2671 nt_errstr(nt_status
), ldb_errstring(ldb
)));
2677 nt_status
= samdb_set_password_internal(ldb
, mem_ctx
,
2680 lmNewHash
, ntNewHash
,
2681 lmOldHash
, ntOldHash
,
2682 reject_reason
, _dominfo
,
2683 true); /* permit trusts */
2684 if (!NT_STATUS_IS_OK(nt_status
)) {
2685 ldb_transaction_cancel(ldb
);
2690 ret
= ldb_transaction_commit(ldb
);
2691 if (ret
!= LDB_SUCCESS
) {
2692 DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
2693 ldb_dn_get_linearized(user_msg
->dn
),
2694 ldb_errstring(ldb
)));
2696 return NT_STATUS_TRANSACTION_ABORTED
;
2700 return NT_STATUS_OK
;
2704 NTSTATUS
samdb_create_foreign_security_principal(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
,
2705 struct dom_sid
*sid
, struct ldb_dn
**ret_dn
)
2707 struct ldb_message
*msg
;
2708 struct ldb_dn
*basedn
;
2712 sidstr
= dom_sid_string(mem_ctx
, sid
);
2713 NT_STATUS_HAVE_NO_MEMORY(sidstr
);
2715 /* We might have to create a ForeignSecurityPrincipal, even if this user
2716 * is in our own domain */
2718 msg
= ldb_msg_new(sidstr
);
2720 talloc_free(sidstr
);
2721 return NT_STATUS_NO_MEMORY
;
2724 ret
= dsdb_wellknown_dn(sam_ctx
, sidstr
,
2725 ldb_get_default_basedn(sam_ctx
),
2726 DS_GUID_FOREIGNSECURITYPRINCIPALS_CONTAINER
,
2728 if (ret
!= LDB_SUCCESS
) {
2729 DEBUG(0, ("Failed to find DN for "
2730 "ForeignSecurityPrincipal container - %s\n", ldb_errstring(sam_ctx
)));
2731 talloc_free(sidstr
);
2732 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2735 /* add core elements to the ldb_message for the alias */
2737 if ( ! ldb_dn_add_child_fmt(msg
->dn
, "CN=%s", sidstr
)) {
2738 talloc_free(sidstr
);
2739 return NT_STATUS_NO_MEMORY
;
2742 ret
= ldb_msg_add_string(msg
, "objectClass",
2743 "foreignSecurityPrincipal");
2744 if (ret
!= LDB_SUCCESS
) {
2745 talloc_free(sidstr
);
2746 return NT_STATUS_NO_MEMORY
;
2749 /* create the alias */
2750 ret
= ldb_add(sam_ctx
, msg
);
2751 if (ret
!= LDB_SUCCESS
) {
2752 DEBUG(0,("Failed to create foreignSecurityPrincipal "
2754 ldb_dn_get_linearized(msg
->dn
),
2755 ldb_errstring(sam_ctx
)));
2756 talloc_free(sidstr
);
2757 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2760 *ret_dn
= talloc_steal(mem_ctx
, msg
->dn
);
2761 talloc_free(sidstr
);
2763 return NT_STATUS_OK
;
2768 Find the DN of a domain, assuming it to be a dotted.dns name
2771 struct ldb_dn
*samdb_dns_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char *dns_domain
)
2774 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2775 const char *binary_encoded
;
2776 const char * const *split_realm
;
2783 split_realm
= (const char * const *)str_list_make(tmp_ctx
, dns_domain
, ".");
2785 talloc_free(tmp_ctx
);
2788 dn
= ldb_dn_new(mem_ctx
, ldb
, NULL
);
2789 for (i
=0; split_realm
[i
]; i
++) {
2790 binary_encoded
= ldb_binary_encode_string(tmp_ctx
, split_realm
[i
]);
2791 if (!ldb_dn_add_base_fmt(dn
, "dc=%s", binary_encoded
)) {
2792 DEBUG(2, ("Failed to add dc=%s element to DN %s\n",
2793 binary_encoded
, ldb_dn_get_linearized(dn
)));
2794 talloc_free(tmp_ctx
);
2798 if (!ldb_dn_validate(dn
)) {
2799 DEBUG(2, ("Failed to validated DN %s\n",
2800 ldb_dn_get_linearized(dn
)));
2801 talloc_free(tmp_ctx
);
2804 talloc_free(tmp_ctx
);
2810 Find the DNS equivalent of a DN, in dotted DNS form
2812 char *samdb_dn_to_dns_domain(TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
)
2814 int i
, num_components
= ldb_dn_get_comp_num(dn
);
2815 char *dns_name
= talloc_strdup(mem_ctx
, "");
2816 if (dns_name
== NULL
) {
2820 for (i
=0; i
<num_components
; i
++) {
2821 const struct ldb_val
*v
= ldb_dn_get_component_val(dn
, i
);
2824 talloc_free(dns_name
);
2827 s
= talloc_asprintf_append_buffer(dns_name
, "%*.*s.",
2828 (int)v
->length
, (int)v
->length
, (char *)v
->data
);
2830 talloc_free(dns_name
);
2836 /* remove the last '.' */
2837 if (dns_name
[0] != 0) {
2838 dns_name
[strlen(dns_name
)-1] = 0;
2845 Find the DNS _msdcs name for a given NTDS GUID. The resulting DNS
2846 name is based on the forest DNS name
2848 char *samdb_ntds_msdcs_dns_name(struct ldb_context
*samdb
,
2849 TALLOC_CTX
*mem_ctx
,
2850 const struct GUID
*ntds_guid
)
2852 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
2853 const char *guid_str
;
2854 struct ldb_dn
*forest_dn
;
2855 const char *dnsforest
;
2858 guid_str
= GUID_string(tmp_ctx
, ntds_guid
);
2859 if (guid_str
== NULL
) {
2860 talloc_free(tmp_ctx
);
2863 forest_dn
= ldb_get_root_basedn(samdb
);
2864 if (forest_dn
== NULL
) {
2865 talloc_free(tmp_ctx
);
2868 dnsforest
= samdb_dn_to_dns_domain(tmp_ctx
, forest_dn
);
2869 if (dnsforest
== NULL
) {
2870 talloc_free(tmp_ctx
);
2873 ret
= talloc_asprintf(mem_ctx
, "%s._msdcs.%s", guid_str
, dnsforest
);
2874 talloc_free(tmp_ctx
);
2880 Find the DN of a domain, be it the netbios or DNS name
2882 struct ldb_dn
*samdb_domain_to_dn(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
2883 const char *domain_name
)
2885 const char * const domain_ref_attrs
[] = {
2888 const char * const domain_ref2_attrs
[] = {
2891 struct ldb_result
*res_domain_ref
;
2892 char *escaped_domain
= ldb_binary_encode_string(mem_ctx
, domain_name
);
2893 /* find the domain's DN */
2894 int ret_domain
= ldb_search(ldb
, mem_ctx
,
2896 samdb_partitions_dn(ldb
, mem_ctx
),
2899 "(&(nETBIOSName=%s)(objectclass=crossRef))",
2901 if (ret_domain
!= LDB_SUCCESS
) {
2905 if (res_domain_ref
->count
== 0) {
2906 ret_domain
= ldb_search(ldb
, mem_ctx
,
2908 samdb_dns_domain_to_dn(ldb
, mem_ctx
, domain_name
),
2911 "(objectclass=domain)");
2912 if (ret_domain
!= LDB_SUCCESS
) {
2916 if (res_domain_ref
->count
== 1) {
2917 return res_domain_ref
->msgs
[0]->dn
;
2922 if (res_domain_ref
->count
> 1) {
2923 DEBUG(0,("Found %d records matching domain [%s]\n",
2924 ret_domain
, domain_name
));
2928 return samdb_result_dn(ldb
, mem_ctx
, res_domain_ref
->msgs
[0], "nCName", NULL
);
2934 use a GUID to find a DN
2936 int dsdb_find_dn_by_guid(struct ldb_context
*ldb
,
2937 TALLOC_CTX
*mem_ctx
,
2938 const struct GUID
*guid
,
2939 uint32_t dsdb_flags
,
2943 struct ldb_result
*res
;
2944 const char *attrs
[] = { NULL
};
2945 char *guid_str
= GUID_string(mem_ctx
, guid
);
2948 return ldb_operr(ldb
);
2951 ret
= dsdb_search(ldb
, mem_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, attrs
,
2952 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
|
2953 DSDB_SEARCH_SHOW_EXTENDED_DN
|
2954 DSDB_SEARCH_ONE_ONLY
| dsdb_flags
,
2955 "objectGUID=%s", guid_str
);
2956 talloc_free(guid_str
);
2957 if (ret
!= LDB_SUCCESS
) {
2961 *dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
2968 use a DN to find a GUID with a given attribute name
2970 int dsdb_find_guid_attr_by_dn(struct ldb_context
*ldb
,
2971 struct ldb_dn
*dn
, const char *attribute
,
2975 struct ldb_result
*res
;
2976 const char *attrs
[2];
2977 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
2979 attrs
[0] = attribute
;
2982 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
,
2983 DSDB_SEARCH_SHOW_DELETED
|
2984 DSDB_SEARCH_SHOW_RECYCLED
);
2985 if (ret
!= LDB_SUCCESS
) {
2986 talloc_free(tmp_ctx
);
2989 if (res
->count
< 1) {
2990 talloc_free(tmp_ctx
);
2991 return LDB_ERR_NO_SUCH_OBJECT
;
2993 *guid
= samdb_result_guid(res
->msgs
[0], attribute
);
2994 talloc_free(tmp_ctx
);
2999 use a DN to find a GUID
3001 int dsdb_find_guid_by_dn(struct ldb_context
*ldb
,
3002 struct ldb_dn
*dn
, struct GUID
*guid
)
3004 return dsdb_find_guid_attr_by_dn(ldb
, dn
, "objectGUID", guid
);
3010 adds the given GUID to the given ldb_message. This value is added
3011 for the given attr_name (may be either "objectGUID" or "parentGUID").
3013 int dsdb_msg_add_guid(struct ldb_message
*msg
,
3015 const char *attr_name
)
3020 TALLOC_CTX
*tmp_ctx
= talloc_init("dsdb_msg_add_guid");
3022 status
= GUID_to_ndr_blob(guid
, tmp_ctx
, &v
);
3023 if (!NT_STATUS_IS_OK(status
)) {
3024 ret
= LDB_ERR_OPERATIONS_ERROR
;
3028 ret
= ldb_msg_add_steal_value(msg
, attr_name
, &v
);
3029 if (ret
!= LDB_SUCCESS
) {
3030 DEBUG(4,(__location__
": Failed to add %s to the message\n",
3038 talloc_free(tmp_ctx
);
3045 use a DN to find a SID
3047 int dsdb_find_sid_by_dn(struct ldb_context
*ldb
,
3048 struct ldb_dn
*dn
, struct dom_sid
*sid
)
3051 struct ldb_result
*res
;
3052 const char *attrs
[] = { "objectSid", NULL
};
3053 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
3058 ret
= dsdb_search_dn(ldb
, tmp_ctx
, &res
, dn
, attrs
,
3059 DSDB_SEARCH_SHOW_DELETED
|
3060 DSDB_SEARCH_SHOW_RECYCLED
);
3061 if (ret
!= LDB_SUCCESS
) {
3062 talloc_free(tmp_ctx
);
3065 if (res
->count
< 1) {
3066 talloc_free(tmp_ctx
);
3067 return LDB_ERR_NO_SUCH_OBJECT
;
3069 s
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSid");
3071 talloc_free(tmp_ctx
);
3072 return LDB_ERR_NO_SUCH_OBJECT
;
3075 talloc_free(tmp_ctx
);
3080 use a SID to find a DN
3082 int dsdb_find_dn_by_sid(struct ldb_context
*ldb
,
3083 TALLOC_CTX
*mem_ctx
,
3084 struct dom_sid
*sid
, struct ldb_dn
**dn
)
3087 struct ldb_result
*res
;
3088 const char *attrs
[] = { NULL
};
3089 char *sid_str
= ldap_encode_ndr_dom_sid(mem_ctx
, sid
);
3092 return ldb_operr(ldb
);
3095 ret
= dsdb_search(ldb
, mem_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, attrs
,
3096 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
|
3097 DSDB_SEARCH_SHOW_EXTENDED_DN
|
3098 DSDB_SEARCH_ONE_ONLY
,
3099 "objectSid=%s", sid_str
);
3100 talloc_free(sid_str
);
3101 if (ret
!= LDB_SUCCESS
) {
3105 *dn
= talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
3112 load a repsFromTo blob list for a given partition GUID
3113 attr must be "repsFrom" or "repsTo"
3115 WERROR
dsdb_loadreps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
3116 const char *attr
, struct repsFromToBlob
**r
, uint32_t *count
)
3118 const char *attrs
[] = { attr
, NULL
};
3119 struct ldb_result
*res
= NULL
;
3120 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3122 struct ldb_message_element
*el
;
3128 ret
= dsdb_search_dn(sam_ctx
, tmp_ctx
, &res
, dn
, attrs
, 0);
3129 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
3130 /* partition hasn't been replicated yet */
3133 if (ret
!= LDB_SUCCESS
) {
3134 DEBUG(0,("dsdb_loadreps: failed to read partition object: %s\n", ldb_errstring(sam_ctx
)));
3135 talloc_free(tmp_ctx
);
3136 return WERR_DS_DRA_INTERNAL_ERROR
;
3139 el
= ldb_msg_find_element(res
->msgs
[0], attr
);
3141 /* it's OK to be empty */
3142 talloc_free(tmp_ctx
);
3146 *count
= el
->num_values
;
3147 *r
= talloc_array(mem_ctx
, struct repsFromToBlob
, *count
);
3149 talloc_free(tmp_ctx
);
3150 return WERR_DS_DRA_INTERNAL_ERROR
;
3153 for (i
=0; i
<(*count
); i
++) {
3154 enum ndr_err_code ndr_err
;
3155 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
],
3158 (ndr_pull_flags_fn_t
)ndr_pull_repsFromToBlob
);
3159 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3160 talloc_free(tmp_ctx
);
3161 return WERR_DS_DRA_INTERNAL_ERROR
;
3165 talloc_free(tmp_ctx
);
3171 save the repsFromTo blob list for a given partition GUID
3172 attr must be "repsFrom" or "repsTo"
3174 WERROR
dsdb_savereps(struct ldb_context
*sam_ctx
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
3175 const char *attr
, struct repsFromToBlob
*r
, uint32_t count
)
3177 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3178 struct ldb_message
*msg
;
3179 struct ldb_message_element
*el
;
3182 msg
= ldb_msg_new(tmp_ctx
);
3184 if (ldb_msg_add_empty(msg
, attr
, LDB_FLAG_MOD_REPLACE
, &el
) != LDB_SUCCESS
) {
3188 el
->values
= talloc_array(msg
, struct ldb_val
, count
);
3193 for (i
=0; i
<count
; i
++) {
3195 enum ndr_err_code ndr_err
;
3197 ndr_err
= ndr_push_struct_blob(&v
, tmp_ctx
,
3199 (ndr_push_flags_fn_t
)ndr_push_repsFromToBlob
);
3200 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3208 if (dsdb_modify(sam_ctx
, msg
, 0) != LDB_SUCCESS
) {
3209 DEBUG(0,("Failed to store %s - %s\n", attr
, ldb_errstring(sam_ctx
)));
3213 talloc_free(tmp_ctx
);
3218 talloc_free(tmp_ctx
);
3219 return WERR_DS_DRA_INTERNAL_ERROR
;
3224 load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
3225 object for a partition
3227 int dsdb_load_partition_usn(struct ldb_context
*ldb
, struct ldb_dn
*dn
,
3228 uint64_t *uSN
, uint64_t *urgent_uSN
)
3230 struct ldb_request
*req
;
3232 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
3233 struct dsdb_control_current_partition
*p_ctrl
;
3234 struct ldb_result
*res
;
3236 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
3238 talloc_free(tmp_ctx
);
3239 return ldb_oom(ldb
);
3242 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
3243 ldb_dn_new(tmp_ctx
, ldb
, "@REPLCHANGED"),
3247 res
, ldb_search_default_callback
,
3249 if (ret
!= LDB_SUCCESS
) {
3250 talloc_free(tmp_ctx
);
3254 p_ctrl
= talloc(req
, struct dsdb_control_current_partition
);
3255 if (p_ctrl
== NULL
) {
3256 talloc_free(tmp_ctx
);
3257 return ldb_oom(ldb
);
3259 p_ctrl
->version
= DSDB_CONTROL_CURRENT_PARTITION_VERSION
;
3262 ret
= ldb_request_add_control(req
,
3263 DSDB_CONTROL_CURRENT_PARTITION_OID
,
3265 if (ret
!= LDB_SUCCESS
) {
3266 talloc_free(tmp_ctx
);
3270 /* Run the new request */
3271 ret
= ldb_request(ldb
, req
);
3273 if (ret
== LDB_SUCCESS
) {
3274 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
3277 if (ret
== LDB_ERR_NO_SUCH_OBJECT
|| ret
== LDB_ERR_INVALID_DN_SYNTAX
) {
3278 /* it hasn't been created yet, which means
3279 an implicit value of zero */
3281 talloc_free(tmp_ctx
);
3285 if (ret
!= LDB_SUCCESS
) {
3286 talloc_free(tmp_ctx
);
3290 if (res
->count
< 1) {
3296 *uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNHighest", 0);
3298 *urgent_uSN
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "uSNUrgent", 0);
3302 talloc_free(tmp_ctx
);
3307 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2
*c1
,
3308 const struct drsuapi_DsReplicaCursor2
*c2
)
3310 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
3313 int drsuapi_DsReplicaCursor_compare(const struct drsuapi_DsReplicaCursor
*c1
,
3314 const struct drsuapi_DsReplicaCursor
*c2
)
3316 return GUID_compare(&c1
->source_dsa_invocation_id
, &c2
->source_dsa_invocation_id
);
3321 see if a computer identified by its invocationId is a RODC
3323 int samdb_is_rodc(struct ldb_context
*sam_ctx
, const struct GUID
*objectGUID
, bool *is_rodc
)
3325 /* 1) find the DN for this servers NTDSDSA object
3326 2) search for the msDS-isRODC attribute
3327 3) if not present then not a RODC
3328 4) if present and TRUE then is a RODC
3330 struct ldb_dn
*config_dn
;
3331 const char *attrs
[] = { "msDS-isRODC", NULL
};
3333 struct ldb_result
*res
;
3334 TALLOC_CTX
*tmp_ctx
= talloc_new(sam_ctx
);
3336 config_dn
= ldb_get_config_basedn(sam_ctx
);
3338 talloc_free(tmp_ctx
);
3339 return ldb_operr(sam_ctx
);
3342 ret
= dsdb_search(sam_ctx
, tmp_ctx
, &res
, config_dn
, LDB_SCOPE_SUBTREE
, attrs
,
3343 DSDB_SEARCH_ONE_ONLY
, "objectGUID=%s", GUID_string(tmp_ctx
, objectGUID
));
3345 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
3347 talloc_free(tmp_ctx
);
3351 if (ret
!= LDB_SUCCESS
) {
3352 DEBUG(1,(("Failed to find our own NTDS Settings object by objectGUID=%s!\n"),
3353 GUID_string(tmp_ctx
, objectGUID
)));
3355 talloc_free(tmp_ctx
);
3359 ret
= ldb_msg_find_attr_as_bool(res
->msgs
[0], "msDS-isRODC", 0);
3360 *is_rodc
= (ret
== 1);
3362 talloc_free(tmp_ctx
);
3368 see if we are a RODC
3370 int samdb_rodc(struct ldb_context
*sam_ctx
, bool *am_rodc
)
3372 const struct GUID
*objectGUID
;
3376 /* see if we have a cached copy */
3377 cached
= (bool *)ldb_get_opaque(sam_ctx
, "cache.am_rodc");
3383 objectGUID
= samdb_ntds_objectGUID(sam_ctx
);
3385 return ldb_operr(sam_ctx
);
3388 ret
= samdb_is_rodc(sam_ctx
, objectGUID
, am_rodc
);
3389 if (ret
!= LDB_SUCCESS
) {
3393 cached
= talloc(sam_ctx
, bool);
3394 if (cached
== NULL
) {
3395 return ldb_oom(sam_ctx
);
3399 ret
= ldb_set_opaque(sam_ctx
, "cache.am_rodc", cached
);
3400 if (ret
!= LDB_SUCCESS
) {
3401 talloc_free(cached
);
3402 return ldb_operr(sam_ctx
);
3408 bool samdb_set_am_rodc(struct ldb_context
*ldb
, bool am_rodc
)
3410 TALLOC_CTX
*tmp_ctx
;
3413 tmp_ctx
= talloc_new(ldb
);
3414 if (tmp_ctx
== NULL
) {
3418 cached
= talloc(tmp_ctx
, bool);
3424 if (ldb_set_opaque(ldb
, "cache.am_rodc", cached
) != LDB_SUCCESS
) {
3428 talloc_steal(ldb
, cached
);
3429 talloc_free(tmp_ctx
);
3433 DEBUG(1,("Failed to set our own cached am_rodc in the ldb!\n"));
3434 talloc_free(tmp_ctx
);
3440 * return NTDSSiteSettings options. See MS-ADTS 7.1.1.2.2.1.1
3441 * flags are DS_NTDSSETTINGS_OPT_*
3443 int samdb_ntds_site_settings_options(struct ldb_context
*ldb_ctx
,
3447 TALLOC_CTX
*tmp_ctx
;
3448 struct ldb_result
*res
;
3449 struct ldb_dn
*site_dn
;
3450 const char *attrs
[] = { "options", NULL
};
3452 tmp_ctx
= talloc_new(ldb_ctx
);
3453 if (tmp_ctx
== NULL
)
3456 /* Retrieve the site dn for the ldb that we
3457 * have open. This is our local site.
3459 site_dn
= samdb_server_site_dn(ldb_ctx
, tmp_ctx
);
3460 if (site_dn
== NULL
)
3463 /* Perform a one level (child) search from the local
3464 * site distinguided name. We're looking for the
3465 * "options" attribute within the nTDSSiteSettings
3468 rc
= ldb_search(ldb_ctx
, tmp_ctx
, &res
, site_dn
,
3469 LDB_SCOPE_ONELEVEL
, attrs
,
3470 "objectClass=nTDSSiteSettings");
3472 if (rc
!= LDB_SUCCESS
|| res
->count
!= 1)
3475 *options
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "options", 0);
3477 talloc_free(tmp_ctx
);
3482 DEBUG(1,("Failed to find our NTDS Site Settings options in ldb!\n"));
3483 talloc_free(tmp_ctx
);
3484 return LDB_ERR_NO_SUCH_OBJECT
;
3488 return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1
3490 flags are DS_NTDS_OPTION_*
3492 int samdb_ntds_options(struct ldb_context
*ldb
, uint32_t *options
)
3494 TALLOC_CTX
*tmp_ctx
;
3495 const char *attrs
[] = { "options", NULL
};
3497 struct ldb_result
*res
;
3499 tmp_ctx
= talloc_new(ldb
);
3500 if (tmp_ctx
== NULL
) {
3504 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
, tmp_ctx
), LDB_SCOPE_BASE
, attrs
, NULL
);
3505 if (ret
!= LDB_SUCCESS
) {
3509 if (res
->count
!= 1) {
3513 *options
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "options", 0);
3515 talloc_free(tmp_ctx
);
3520 DEBUG(1,("Failed to find our own NTDS Settings options in the ldb!\n"));
3521 talloc_free(tmp_ctx
);
3522 return LDB_ERR_NO_SUCH_OBJECT
;
3525 const char* samdb_ntds_object_category(TALLOC_CTX
*tmp_ctx
, struct ldb_context
*ldb
)
3527 const char *attrs
[] = { "objectCategory", NULL
};
3529 struct ldb_result
*res
;
3531 ret
= ldb_search(ldb
, tmp_ctx
, &res
, samdb_ntds_settings_dn(ldb
, tmp_ctx
), LDB_SCOPE_BASE
, attrs
, NULL
);
3532 if (ret
!= LDB_SUCCESS
) {
3536 if (res
->count
!= 1) {
3540 return ldb_msg_find_attr_as_string(res
->msgs
[0], "objectCategory", NULL
);
3543 DEBUG(1,("Failed to find our own NTDS Settings objectCategory in the ldb!\n"));
3548 * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
3549 * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
3551 const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX
*mem_ctx
, const char *cn
)
3553 char **tokens
, *ret
;
3556 tokens
= str_list_make(mem_ctx
, cn
, " -_");
3560 /* "tolower()" and "toupper()" should also work properly on 0x00 */
3561 tokens
[0][0] = tolower(tokens
[0][0]);
3562 for (i
= 1; i
< str_list_length((const char * const *)tokens
); i
++)
3563 tokens
[i
][0] = toupper(tokens
[i
][0]);
3565 ret
= talloc_strdup(mem_ctx
, tokens
[0]);
3566 for (i
= 1; i
< str_list_length((const char * const *)tokens
); i
++)
3567 ret
= talloc_asprintf_append_buffer(ret
, "%s", tokens
[i
]);
3569 talloc_free(tokens
);
3575 * This detects and returns the domain functional level (DS_DOMAIN_FUNCTION_*)
3577 int dsdb_functional_level(struct ldb_context
*ldb
)
3579 int *domainFunctionality
=
3580 talloc_get_type(ldb_get_opaque(ldb
, "domainFunctionality"), int);
3581 if (!domainFunctionality
) {
3582 /* this is expected during initial provision */
3583 DEBUG(4,(__location__
": WARNING: domainFunctionality not setup\n"));
3584 return DS_DOMAIN_FUNCTION_2000
;
3586 return *domainFunctionality
;
3590 * This detects and returns the forest functional level (DS_DOMAIN_FUNCTION_*)
3592 int dsdb_forest_functional_level(struct ldb_context
*ldb
)
3594 int *forestFunctionality
=
3595 talloc_get_type(ldb_get_opaque(ldb
, "forestFunctionality"), int);
3596 if (!forestFunctionality
) {
3597 DEBUG(0,(__location__
": WARNING: forestFunctionality not setup\n"));
3598 return DS_DOMAIN_FUNCTION_2000
;
3600 return *forestFunctionality
;
3604 set a GUID in an extended DN structure
3606 int dsdb_set_extended_dn_guid(struct ldb_dn
*dn
, const struct GUID
*guid
, const char *component_name
)
3612 status
= GUID_to_ndr_blob(guid
, dn
, &v
);
3613 if (!NT_STATUS_IS_OK(status
)) {
3614 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
3617 ret
= ldb_dn_set_extended_component(dn
, component_name
, &v
);
3623 return a GUID from a extended DN structure
3625 NTSTATUS
dsdb_get_extended_dn_guid(struct ldb_dn
*dn
, struct GUID
*guid
, const char *component_name
)
3627 const struct ldb_val
*v
;
3629 v
= ldb_dn_get_extended_component(dn
, component_name
);
3631 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3634 return GUID_from_ndr_blob(v
, guid
);
3638 return a uint64_t from a extended DN structure
3640 NTSTATUS
dsdb_get_extended_dn_uint64(struct ldb_dn
*dn
, uint64_t *val
, const char *component_name
)
3642 const struct ldb_val
*v
;
3645 v
= ldb_dn_get_extended_component(dn
, component_name
);
3647 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3649 s
= talloc_strndup(dn
, (const char *)v
->data
, v
->length
);
3650 NT_STATUS_HAVE_NO_MEMORY(s
);
3652 *val
= strtoull(s
, NULL
, 0);
3655 return NT_STATUS_OK
;
3659 return a NTTIME from a extended DN structure
3661 NTSTATUS
dsdb_get_extended_dn_nttime(struct ldb_dn
*dn
, NTTIME
*nttime
, const char *component_name
)
3663 return dsdb_get_extended_dn_uint64(dn
, nttime
, component_name
);
3667 return a uint32_t from a extended DN structure
3669 NTSTATUS
dsdb_get_extended_dn_uint32(struct ldb_dn
*dn
, uint32_t *val
, const char *component_name
)
3671 const struct ldb_val
*v
;
3674 v
= ldb_dn_get_extended_component(dn
, component_name
);
3676 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3679 s
= talloc_strndup(dn
, (const char *)v
->data
, v
->length
);
3680 NT_STATUS_HAVE_NO_MEMORY(s
);
3682 *val
= strtoul(s
, NULL
, 0);
3685 return NT_STATUS_OK
;
3689 return a dom_sid from a extended DN structure
3691 NTSTATUS
dsdb_get_extended_dn_sid(struct ldb_dn
*dn
, struct dom_sid
*sid
, const char *component_name
)
3693 const struct ldb_val
*sid_blob
;
3694 struct TALLOC_CTX
*tmp_ctx
;
3695 enum ndr_err_code ndr_err
;
3697 sid_blob
= ldb_dn_get_extended_component(dn
, component_name
);
3699 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3702 tmp_ctx
= talloc_new(NULL
);
3704 ndr_err
= ndr_pull_struct_blob_all(sid_blob
, tmp_ctx
, sid
,
3705 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
3706 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3707 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
3708 talloc_free(tmp_ctx
);
3712 talloc_free(tmp_ctx
);
3713 return NT_STATUS_OK
;
3718 return RMD_FLAGS directly from a ldb_dn
3719 returns 0 if not found
3721 uint32_t dsdb_dn_rmd_flags(struct ldb_dn
*dn
)
3723 const struct ldb_val
*v
;
3725 v
= ldb_dn_get_extended_component(dn
, "RMD_FLAGS");
3726 if (!v
|| v
->length
> sizeof(buf
)-1) return 0;
3727 strncpy(buf
, (const char *)v
->data
, v
->length
);
3729 return strtoul(buf
, NULL
, 10);
3733 return RMD_FLAGS directly from a ldb_val for a DN
3734 returns 0 if RMD_FLAGS is not found
3736 uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val
*val
)
3742 if (val
->length
< 13) {
3745 p
= memmem(val
->data
, val
->length
, "<RMD_FLAGS=", 11);
3749 flags
= strtoul(p
+11, &end
, 10);
3750 if (!end
|| *end
!= '>') {
3751 /* it must end in a > */
3758 return true if a ldb_val containing a DN in storage form is deleted
3760 bool dsdb_dn_is_deleted_val(const struct ldb_val
*val
)
3762 return (dsdb_dn_val_rmd_flags(val
) & DSDB_RMD_FLAG_DELETED
) != 0;
3766 return true if a ldb_val containing a DN in storage form is
3767 in the upgraded w2k3 linked attribute format
3769 bool dsdb_dn_is_upgraded_link_val(struct ldb_val
*val
)
3771 return memmem(val
->data
, val
->length
, "<RMD_VERSION=", 13) != NULL
;
3775 return a DN for a wellknown GUID
3777 int dsdb_wellknown_dn(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
,
3778 struct ldb_dn
*nc_root
, const char *wk_guid
,
3779 struct ldb_dn
**wkguid_dn
)
3781 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
3782 const char *attrs
[] = { NULL
};
3785 struct ldb_result
*res
;
3787 /* construct the magic WKGUID DN */
3788 dn
= ldb_dn_new_fmt(tmp_ctx
, samdb
, "<WKGUID=%s,%s>",
3789 wk_guid
, ldb_dn_get_linearized(nc_root
));
3791 talloc_free(tmp_ctx
);
3792 return ldb_operr(samdb
);
3795 ret
= dsdb_search_dn(samdb
, tmp_ctx
, &res
, dn
, attrs
,
3796 DSDB_SEARCH_SHOW_DELETED
|
3797 DSDB_SEARCH_SHOW_RECYCLED
);
3798 if (ret
!= LDB_SUCCESS
) {
3799 talloc_free(tmp_ctx
);
3803 (*wkguid_dn
) = talloc_steal(mem_ctx
, res
->msgs
[0]->dn
);
3804 talloc_free(tmp_ctx
);
3809 static int dsdb_dn_compare_ptrs(struct ldb_dn
**dn1
, struct ldb_dn
**dn2
)
3811 return ldb_dn_compare(*dn1
, *dn2
);
3815 find a NC root given a DN within the NC
3817 int dsdb_find_nc_root(struct ldb_context
*samdb
, TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
,
3818 struct ldb_dn
**nc_root
)
3820 const char *root_attrs
[] = { "namingContexts", NULL
};
3821 TALLOC_CTX
*tmp_ctx
;
3823 struct ldb_message_element
*el
;
3824 struct ldb_result
*root_res
;
3826 struct ldb_dn
**nc_dns
;
3828 tmp_ctx
= talloc_new(samdb
);
3829 if (tmp_ctx
== NULL
) {
3830 return ldb_oom(samdb
);
3833 ret
= ldb_search(samdb
, tmp_ctx
, &root_res
,
3834 ldb_dn_new(tmp_ctx
, samdb
, ""), LDB_SCOPE_BASE
, root_attrs
, NULL
);
3835 if (ret
!= LDB_SUCCESS
|| root_res
->count
== 0) {
3836 DEBUG(1,("Searching for namingContexts in rootDSE failed: %s\n", ldb_errstring(samdb
)));
3837 talloc_free(tmp_ctx
);
3841 el
= ldb_msg_find_element(root_res
->msgs
[0], "namingContexts");
3842 if ((el
== NULL
) || (el
->num_values
< 3)) {
3843 struct ldb_message
*tmp_msg
;
3845 DEBUG(5,("dsdb_find_nc_root: Finding a valid 'namingContexts' element in the RootDSE failed. Using a temporary list."));
3847 /* This generates a temporary list of NCs in order to let the
3848 * provisioning work. */
3849 tmp_msg
= ldb_msg_new(tmp_ctx
);
3850 if (tmp_msg
== NULL
) {
3851 talloc_free(tmp_ctx
);
3852 return ldb_oom(samdb
);
3854 ret
= ldb_msg_add_steal_string(tmp_msg
, "namingContexts",
3855 ldb_dn_alloc_linearized(tmp_msg
, ldb_get_schema_basedn(samdb
)));
3856 if (ret
!= LDB_SUCCESS
) {
3857 talloc_free(tmp_ctx
);
3860 ret
= ldb_msg_add_steal_string(tmp_msg
, "namingContexts",
3861 ldb_dn_alloc_linearized(tmp_msg
, ldb_get_config_basedn(samdb
)));
3862 if (ret
!= LDB_SUCCESS
) {
3863 talloc_free(tmp_ctx
);
3866 ret
= ldb_msg_add_steal_string(tmp_msg
, "namingContexts",
3867 ldb_dn_alloc_linearized(tmp_msg
, ldb_get_default_basedn(samdb
)));
3868 if (ret
!= LDB_SUCCESS
) {
3869 talloc_free(tmp_ctx
);
3872 el
= &tmp_msg
->elements
[0];
3875 nc_dns
= talloc_array(tmp_ctx
, struct ldb_dn
*, el
->num_values
);
3877 talloc_free(tmp_ctx
);
3878 return ldb_oom(samdb
);
3881 for (i
=0; i
<el
->num_values
; i
++) {
3882 nc_dns
[i
] = ldb_dn_from_ldb_val(nc_dns
, samdb
, &el
->values
[i
]);
3883 if (nc_dns
[i
] == NULL
) {
3884 talloc_free(tmp_ctx
);
3885 return ldb_operr(samdb
);
3889 TYPESAFE_QSORT(nc_dns
, el
->num_values
, dsdb_dn_compare_ptrs
);
3891 for (i
=0; i
<el
->num_values
; i
++) {
3892 if (ldb_dn_compare_base(nc_dns
[i
], dn
) == 0) {
3893 (*nc_root
) = talloc_steal(mem_ctx
, nc_dns
[i
]);
3894 talloc_free(tmp_ctx
);
3899 talloc_free(tmp_ctx
);
3900 return LDB_ERR_NO_SUCH_OBJECT
;
3905 find the deleted objects DN for any object, by looking for the NC
3906 root, then looking up the wellknown GUID
3908 int dsdb_get_deleted_objects_dn(struct ldb_context
*ldb
,
3909 TALLOC_CTX
*mem_ctx
, struct ldb_dn
*obj_dn
,
3910 struct ldb_dn
**do_dn
)
3912 struct ldb_dn
*nc_root
;
3915 ret
= dsdb_find_nc_root(ldb
, mem_ctx
, obj_dn
, &nc_root
);
3916 if (ret
!= LDB_SUCCESS
) {
3920 ret
= dsdb_wellknown_dn(ldb
, mem_ctx
, nc_root
, DS_GUID_DELETED_OBJECTS_CONTAINER
, do_dn
);
3921 talloc_free(nc_root
);
3926 return the tombstoneLifetime, in days
3928 int dsdb_tombstone_lifetime(struct ldb_context
*ldb
, uint32_t *lifetime
)
3931 dn
= ldb_get_config_basedn(ldb
);
3933 return LDB_ERR_NO_SUCH_OBJECT
;
3935 dn
= ldb_dn_copy(ldb
, dn
);
3937 return ldb_operr(ldb
);
3939 /* see MS-ADTS section 7.1.1.2.4.1.1. There doesn't appear to
3940 be a wellknown GUID for this */
3941 if (!ldb_dn_add_child_fmt(dn
, "CN=Directory Service,CN=Windows NT,CN=Services")) {
3943 return ldb_operr(ldb
);
3946 *lifetime
= samdb_search_uint(ldb
, dn
, 180, dn
, "tombstoneLifetime", "objectClass=nTDSService");
3952 compare a ldb_val to a string case insensitively
3954 int samdb_ldb_val_case_cmp(const char *s
, struct ldb_val
*v
)
3956 size_t len
= strlen(s
);
3958 if (len
> v
->length
) return 1;
3959 ret
= strncasecmp(s
, (const char *)v
->data
, v
->length
);
3960 if (ret
!= 0) return ret
;
3961 if (v
->length
> len
&& v
->data
[len
] != 0) {
3969 load the UDV for a partition in v2 format
3970 The list is returned sorted, and with our local cursor added
3972 int dsdb_load_udv_v2(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
3973 struct drsuapi_DsReplicaCursor2
**cursors
, uint32_t *count
)
3975 static const char *attrs
[] = { "replUpToDateVector", NULL
};
3976 struct ldb_result
*r
;
3977 const struct ldb_val
*ouv_value
;
3980 uint64_t highest_usn
= 0;
3981 const struct GUID
*our_invocation_id
;
3982 static const struct timeval tv1970
;
3983 NTTIME nt1970
= timeval_to_nttime(&tv1970
);
3985 ret
= ldb_search(samdb
, mem_ctx
, &r
, dn
, LDB_SCOPE_BASE
, attrs
, NULL
);
3986 if (ret
!= LDB_SUCCESS
) {
3990 ouv_value
= ldb_msg_find_ldb_val(r
->msgs
[0], "replUpToDateVector");
3992 enum ndr_err_code ndr_err
;
3993 struct replUpToDateVectorBlob ouv
;
3995 ndr_err
= ndr_pull_struct_blob(ouv_value
, r
, &ouv
,
3996 (ndr_pull_flags_fn_t
)ndr_pull_replUpToDateVectorBlob
);
3997 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3999 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
4001 if (ouv
.version
!= 2) {
4002 /* we always store as version 2, and
4003 * replUpToDateVector is not replicated
4005 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
4008 *count
= ouv
.ctr
.ctr2
.count
;
4009 *cursors
= talloc_steal(mem_ctx
, ouv
.ctr
.ctr2
.cursors
);
4017 our_invocation_id
= samdb_ntds_invocation_id(samdb
);
4018 if (!our_invocation_id
) {
4019 DEBUG(0,(__location__
": No invocationID on samdb - %s\n", ldb_errstring(samdb
)));
4020 talloc_free(*cursors
);
4021 return ldb_operr(samdb
);
4024 ret
= ldb_sequence_number(samdb
, LDB_SEQ_HIGHEST_SEQ
, &highest_usn
);
4025 if (ret
!= LDB_SUCCESS
) {
4026 /* nothing to add - this can happen after a vampire */
4027 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
4031 for (i
=0; i
<*count
; i
++) {
4032 if (GUID_equal(our_invocation_id
, &(*cursors
)[i
].source_dsa_invocation_id
)) {
4033 (*cursors
)[i
].highest_usn
= highest_usn
;
4034 (*cursors
)[i
].last_sync_success
= nt1970
;
4035 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
4040 (*cursors
) = talloc_realloc(mem_ctx
, *cursors
, struct drsuapi_DsReplicaCursor2
, (*count
)+1);
4042 return ldb_oom(samdb
);
4045 (*cursors
)[*count
].source_dsa_invocation_id
= *our_invocation_id
;
4046 (*cursors
)[*count
].highest_usn
= highest_usn
;
4047 (*cursors
)[*count
].last_sync_success
= nt1970
;
4050 TYPESAFE_QSORT(*cursors
, *count
, drsuapi_DsReplicaCursor2_compare
);
4056 load the UDV for a partition in version 1 format
4057 The list is returned sorted, and with our local cursor added
4059 int dsdb_load_udv_v1(struct ldb_context
*samdb
, struct ldb_dn
*dn
, TALLOC_CTX
*mem_ctx
,
4060 struct drsuapi_DsReplicaCursor
**cursors
, uint32_t *count
)
4062 struct drsuapi_DsReplicaCursor2
*v2
;
4066 ret
= dsdb_load_udv_v2(samdb
, dn
, mem_ctx
, &v2
, count
);
4067 if (ret
!= LDB_SUCCESS
) {
4077 *cursors
= talloc_array(mem_ctx
, struct drsuapi_DsReplicaCursor
, *count
);
4078 if (*cursors
== NULL
) {
4080 return ldb_oom(samdb
);
4083 for (i
=0; i
<*count
; i
++) {
4084 (*cursors
)[i
].source_dsa_invocation_id
= v2
[i
].source_dsa_invocation_id
;
4085 (*cursors
)[i
].highest_usn
= v2
[i
].highest_usn
;
4092 add a set of controls to a ldb_request structure based on a set of
4093 flags. See util.h for a list of available flags
4095 int dsdb_request_add_controls(struct ldb_request
*req
, uint32_t dsdb_flags
)
4098 if (dsdb_flags
& DSDB_SEARCH_SEARCH_ALL_PARTITIONS
) {
4099 struct ldb_search_options_control
*options
;
4100 /* Using the phantom root control allows us to search all partitions */
4101 options
= talloc(req
, struct ldb_search_options_control
);
4102 if (options
== NULL
) {
4103 return LDB_ERR_OPERATIONS_ERROR
;
4105 options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
4107 ret
= ldb_request_add_control(req
,
4108 LDB_CONTROL_SEARCH_OPTIONS_OID
,
4110 if (ret
!= LDB_SUCCESS
) {
4115 if (dsdb_flags
& DSDB_SEARCH_NO_GLOBAL_CATALOG
) {
4116 ret
= ldb_request_add_control(req
,
4117 DSDB_CONTROL_NO_GLOBAL_CATALOG
,
4119 if (ret
!= LDB_SUCCESS
) {
4124 if (dsdb_flags
& DSDB_SEARCH_SHOW_DELETED
) {
4125 ret
= ldb_request_add_control(req
, LDB_CONTROL_SHOW_DELETED_OID
, true, NULL
);
4126 if (ret
!= LDB_SUCCESS
) {
4131 if (dsdb_flags
& DSDB_SEARCH_SHOW_RECYCLED
) {
4132 ret
= ldb_request_add_control(req
, LDB_CONTROL_SHOW_RECYCLED_OID
, false, NULL
);
4133 if (ret
!= LDB_SUCCESS
) {
4138 if (dsdb_flags
& DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
) {
4139 ret
= ldb_request_add_control(req
, DSDB_CONTROL_DN_STORAGE_FORMAT_OID
, false, NULL
);
4140 if (ret
!= LDB_SUCCESS
) {
4145 if (dsdb_flags
& DSDB_SEARCH_SHOW_EXTENDED_DN
) {
4146 struct ldb_extended_dn_control
*extended_ctrl
= talloc(req
, struct ldb_extended_dn_control
);
4147 if (!extended_ctrl
) {
4148 return LDB_ERR_OPERATIONS_ERROR
;
4150 extended_ctrl
->type
= 1;
4152 ret
= ldb_request_add_control(req
, LDB_CONTROL_EXTENDED_DN_OID
, true, extended_ctrl
);
4153 if (ret
!= LDB_SUCCESS
) {
4158 if (dsdb_flags
& DSDB_SEARCH_REVEAL_INTERNALS
) {
4159 ret
= ldb_request_add_control(req
, LDB_CONTROL_REVEAL_INTERNALS
, false, NULL
);
4160 if (ret
!= LDB_SUCCESS
) {
4165 if (dsdb_flags
& DSDB_MODIFY_RELAX
) {
4166 ret
= ldb_request_add_control(req
, LDB_CONTROL_RELAX_OID
, false, NULL
);
4167 if (ret
!= LDB_SUCCESS
) {
4172 if (dsdb_flags
& DSDB_MODIFY_PERMISSIVE
) {
4173 ret
= ldb_request_add_control(req
, LDB_CONTROL_PERMISSIVE_MODIFY_OID
, false, NULL
);
4174 if (ret
!= LDB_SUCCESS
) {
4179 if (dsdb_flags
& DSDB_FLAG_AS_SYSTEM
) {
4180 ret
= ldb_request_add_control(req
, LDB_CONTROL_AS_SYSTEM_OID
, false, NULL
);
4181 if (ret
!= LDB_SUCCESS
) {
4186 if (dsdb_flags
& DSDB_TREE_DELETE
) {
4187 ret
= ldb_request_add_control(req
, LDB_CONTROL_TREE_DELETE_OID
, false, NULL
);
4188 if (ret
!= LDB_SUCCESS
) {
4193 if (dsdb_flags
& DSDB_PROVISION
) {
4194 ret
= ldb_request_add_control(req
, LDB_CONTROL_PROVISION_OID
, false, NULL
);
4195 if (ret
!= LDB_SUCCESS
) {
4200 /* This is a special control to bypass the password_hash module for use in pdb_samba4 for Samba3 upgrades */
4201 if (dsdb_flags
& DSDB_BYPASS_PASSWORD_HASH
) {
4202 ret
= ldb_request_add_control(req
, DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID
, true, NULL
);
4203 if (ret
!= LDB_SUCCESS
) {
4208 if (dsdb_flags
& DSDB_PASSWORD_BYPASS_LAST_SET
) {
4210 * This must not be critical, as it will only be
4211 * handled (and need to be handled) if the other
4212 * attributes in the request bring password_hash into
4215 ret
= ldb_request_add_control(req
, DSDB_CONTROL_PASSWORD_BYPASS_LAST_SET_OID
, false, NULL
);
4216 if (ret
!= LDB_SUCCESS
) {
4221 if (dsdb_flags
& DSDB_MODIFY_PARTIAL_REPLICA
) {
4222 ret
= ldb_request_add_control(req
, DSDB_CONTROL_PARTIAL_REPLICA
, false, NULL
);
4223 if (ret
!= LDB_SUCCESS
) {
4232 an add with a set of controls
4234 int dsdb_add(struct ldb_context
*ldb
, const struct ldb_message
*message
,
4235 uint32_t dsdb_flags
)
4237 struct ldb_request
*req
;
4240 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
4244 ldb_op_default_callback
,
4247 if (ret
!= LDB_SUCCESS
) return ret
;
4249 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4250 if (ret
!= LDB_SUCCESS
) {
4255 ret
= dsdb_autotransaction_request(ldb
, req
);
4262 a modify with a set of controls
4264 int dsdb_modify(struct ldb_context
*ldb
, const struct ldb_message
*message
,
4265 uint32_t dsdb_flags
)
4267 struct ldb_request
*req
;
4270 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
4274 ldb_op_default_callback
,
4277 if (ret
!= LDB_SUCCESS
) return ret
;
4279 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4280 if (ret
!= LDB_SUCCESS
) {
4285 ret
= dsdb_autotransaction_request(ldb
, req
);
4292 a delete with a set of flags
4294 int dsdb_delete(struct ldb_context
*ldb
, struct ldb_dn
*dn
,
4295 uint32_t dsdb_flags
)
4297 struct ldb_request
*req
;
4300 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
4304 ldb_op_default_callback
,
4307 if (ret
!= LDB_SUCCESS
) return ret
;
4309 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4310 if (ret
!= LDB_SUCCESS
) {
4315 ret
= dsdb_autotransaction_request(ldb
, req
);
4322 like dsdb_modify() but set all the element flags to
4323 LDB_FLAG_MOD_REPLACE
4325 int dsdb_replace(struct ldb_context
*ldb
, struct ldb_message
*msg
, uint32_t dsdb_flags
)
4329 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
4330 for (i
=0;i
<msg
->num_elements
;i
++) {
4331 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
4334 return dsdb_modify(ldb
, msg
, dsdb_flags
);
4339 search for attrs on one DN, allowing for dsdb_flags controls
4341 int dsdb_search_dn(struct ldb_context
*ldb
,
4342 TALLOC_CTX
*mem_ctx
,
4343 struct ldb_result
**_result
,
4344 struct ldb_dn
*basedn
,
4345 const char * const *attrs
,
4346 uint32_t dsdb_flags
)
4349 struct ldb_request
*req
;
4350 struct ldb_result
*res
;
4352 res
= talloc_zero(mem_ctx
, struct ldb_result
);
4354 return ldb_oom(ldb
);
4357 ret
= ldb_build_search_req(&req
, ldb
, res
,
4364 ldb_search_default_callback
,
4366 if (ret
!= LDB_SUCCESS
) {
4371 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4372 if (ret
!= LDB_SUCCESS
) {
4377 ret
= ldb_request(ldb
, req
);
4378 if (ret
== LDB_SUCCESS
) {
4379 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
4383 if (ret
!= LDB_SUCCESS
) {
4393 search for attrs on one DN, by the GUID of the DN, allowing for
4396 int dsdb_search_by_dn_guid(struct ldb_context
*ldb
,
4397 TALLOC_CTX
*mem_ctx
,
4398 struct ldb_result
**_result
,
4399 const struct GUID
*guid
,
4400 const char * const *attrs
,
4401 uint32_t dsdb_flags
)
4403 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
4407 dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "<GUID=%s>", GUID_string(tmp_ctx
, guid
));
4409 talloc_free(tmp_ctx
);
4410 return ldb_oom(ldb
);
4413 ret
= dsdb_search_dn(ldb
, mem_ctx
, _result
, dn
, attrs
, dsdb_flags
);
4414 talloc_free(tmp_ctx
);
4419 general search with dsdb_flags for controls
4421 int dsdb_search(struct ldb_context
*ldb
,
4422 TALLOC_CTX
*mem_ctx
,
4423 struct ldb_result
**_result
,
4424 struct ldb_dn
*basedn
,
4425 enum ldb_scope scope
,
4426 const char * const *attrs
,
4427 uint32_t dsdb_flags
,
4428 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
4431 struct ldb_request
*req
;
4432 struct ldb_result
*res
;
4434 char *expression
= NULL
;
4435 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
4437 /* cross-partitions searches with a basedn break multi-domain support */
4438 SMB_ASSERT(basedn
== NULL
|| (dsdb_flags
& DSDB_SEARCH_SEARCH_ALL_PARTITIONS
) == 0);
4440 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
4442 talloc_free(tmp_ctx
);
4443 return ldb_oom(ldb
);
4447 va_start(ap
, exp_fmt
);
4448 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
4452 talloc_free(tmp_ctx
);
4453 return ldb_oom(ldb
);
4457 ret
= ldb_build_search_req(&req
, ldb
, tmp_ctx
,
4464 ldb_search_default_callback
,
4466 if (ret
!= LDB_SUCCESS
) {
4467 talloc_free(tmp_ctx
);
4471 ret
= dsdb_request_add_controls(req
, dsdb_flags
);
4472 if (ret
!= LDB_SUCCESS
) {
4473 talloc_free(tmp_ctx
);
4474 ldb_reset_err_string(ldb
);
4478 ret
= ldb_request(ldb
, req
);
4479 if (ret
== LDB_SUCCESS
) {
4480 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
4483 if (ret
!= LDB_SUCCESS
) {
4484 talloc_free(tmp_ctx
);
4488 if (dsdb_flags
& DSDB_SEARCH_ONE_ONLY
) {
4489 if (res
->count
== 0) {
4490 talloc_free(tmp_ctx
);
4491 ldb_reset_err_string(ldb
);
4492 return LDB_ERR_NO_SUCH_OBJECT
;
4494 if (res
->count
!= 1) {
4495 talloc_free(tmp_ctx
);
4496 ldb_reset_err_string(ldb
);
4497 return LDB_ERR_CONSTRAINT_VIOLATION
;
4501 *_result
= talloc_steal(mem_ctx
, res
);
4502 talloc_free(tmp_ctx
);
4509 general search with dsdb_flags for controls
4510 returns exactly 1 record or an error
4512 int dsdb_search_one(struct ldb_context
*ldb
,
4513 TALLOC_CTX
*mem_ctx
,
4514 struct ldb_message
**msg
,
4515 struct ldb_dn
*basedn
,
4516 enum ldb_scope scope
,
4517 const char * const *attrs
,
4518 uint32_t dsdb_flags
,
4519 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(8, 9)
4522 struct ldb_result
*res
;
4524 char *expression
= NULL
;
4525 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
4527 dsdb_flags
|= DSDB_SEARCH_ONE_ONLY
;
4529 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
4531 talloc_free(tmp_ctx
);
4532 return ldb_oom(ldb
);
4536 va_start(ap
, exp_fmt
);
4537 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
4541 talloc_free(tmp_ctx
);
4542 return ldb_oom(ldb
);
4544 ret
= dsdb_search(ldb
, tmp_ctx
, &res
, basedn
, scope
, attrs
,
4545 dsdb_flags
, "%s", expression
);
4547 ret
= dsdb_search(ldb
, tmp_ctx
, &res
, basedn
, scope
, attrs
,
4551 if (ret
!= LDB_SUCCESS
) {
4552 talloc_free(tmp_ctx
);
4556 *msg
= talloc_steal(mem_ctx
, res
->msgs
[0]);
4557 talloc_free(tmp_ctx
);
4562 /* returns back the forest DNS name */
4563 const char *samdb_forest_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
4565 const char *forest_name
= ldb_dn_canonical_string(mem_ctx
,
4566 ldb_get_root_basedn(ldb
));
4569 if (forest_name
== NULL
) {
4573 p
= strchr(forest_name
, '/');
4581 /* returns back the default domain DNS name */
4582 const char *samdb_default_domain_name(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
)
4584 const char *domain_name
= ldb_dn_canonical_string(mem_ctx
,
4585 ldb_get_default_basedn(ldb
));
4588 if (domain_name
== NULL
) {
4592 p
= strchr(domain_name
, '/');
4601 validate that an DSA GUID belongs to the specified user sid.
4602 The user SID must be a domain controller account (either RODC or
4605 int dsdb_validate_dsa_guid(struct ldb_context
*ldb
,
4606 const struct GUID
*dsa_guid
,
4607 const struct dom_sid
*sid
)
4610 - find DN of record with the DSA GUID in the
4611 configuration partition (objectGUID)
4612 - remove "NTDS Settings" component from DN
4613 - do a base search on that DN for serverReference with
4615 - extract objectSid from resulting serverReference
4617 - check this sid matches the sid argument
4619 struct ldb_dn
*config_dn
;
4620 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
4621 struct ldb_message
*msg
;
4622 const char *attrs1
[] = { NULL
};
4623 const char *attrs2
[] = { "serverReference", NULL
};
4625 struct ldb_dn
*dn
, *account_dn
;
4626 struct dom_sid sid2
;
4629 config_dn
= ldb_get_config_basedn(ldb
);
4631 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, config_dn
, LDB_SCOPE_SUBTREE
,
4632 attrs1
, 0, "(&(objectGUID=%s)(objectClass=nTDSDSA))", GUID_string(tmp_ctx
, dsa_guid
));
4633 if (ret
!= LDB_SUCCESS
) {
4634 DEBUG(1,(__location__
": Failed to find DSA objectGUID %s for sid %s\n",
4635 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
4636 talloc_free(tmp_ctx
);
4637 return ldb_operr(ldb
);
4641 if (!ldb_dn_remove_child_components(dn
, 1)) {
4642 talloc_free(tmp_ctx
);
4643 return ldb_operr(ldb
);
4646 ret
= dsdb_search_one(ldb
, tmp_ctx
, &msg
, dn
, LDB_SCOPE_BASE
,
4647 attrs2
, DSDB_SEARCH_SHOW_EXTENDED_DN
,
4648 "(objectClass=server)");
4649 if (ret
!= LDB_SUCCESS
) {
4650 DEBUG(1,(__location__
": Failed to find server record for DSA with objectGUID %s, sid %s\n",
4651 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
4652 talloc_free(tmp_ctx
);
4653 return ldb_operr(ldb
);
4656 account_dn
= ldb_msg_find_attr_as_dn(ldb
, tmp_ctx
, msg
, "serverReference");
4657 if (account_dn
== NULL
) {
4658 DEBUG(1,(__location__
": Failed to find account dn "
4659 "(serverReference) for %s, parent of DSA with "
4660 "objectGUID %s, sid %s\n",
4661 ldb_dn_get_linearized(msg
->dn
),
4662 GUID_string(tmp_ctx
, dsa_guid
),
4663 dom_sid_string(tmp_ctx
, sid
)));
4664 talloc_free(tmp_ctx
);
4665 return ldb_operr(ldb
);
4668 status
= dsdb_get_extended_dn_sid(account_dn
, &sid2
, "SID");
4669 if (!NT_STATUS_IS_OK(status
)) {
4670 DEBUG(1,(__location__
": Failed to find SID for DSA with objectGUID %s, sid %s\n",
4671 GUID_string(tmp_ctx
, dsa_guid
), dom_sid_string(tmp_ctx
, sid
)));
4672 talloc_free(tmp_ctx
);
4673 return ldb_operr(ldb
);
4676 if (!dom_sid_equal(sid
, &sid2
)) {
4677 /* someone is trying to spoof another account */
4678 DEBUG(0,(__location__
": Bad DSA objectGUID %s for sid %s - expected sid %s\n",
4679 GUID_string(tmp_ctx
, dsa_guid
),
4680 dom_sid_string(tmp_ctx
, sid
),
4681 dom_sid_string(tmp_ctx
, &sid2
)));
4682 talloc_free(tmp_ctx
);
4683 return ldb_operr(ldb
);
4686 talloc_free(tmp_ctx
);
4690 static const char * const secret_attributes
[] = {
4691 DSDB_SECRET_ATTRIBUTES
,
4696 check if the attribute belongs to the RODC filtered attribute set
4697 Note that attributes that are in the filtered attribute set are the
4698 ones that _are_ always sent to a RODC
4700 bool dsdb_attr_in_rodc_fas(const struct dsdb_attribute
*sa
)
4702 /* they never get secret attributes */
4703 if (is_attr_in_list(secret_attributes
, sa
->lDAPDisplayName
)) {
4707 /* they do get non-secret critical attributes */
4708 if (sa
->schemaFlagsEx
& SCHEMA_FLAG_ATTR_IS_CRITICAL
) {
4712 /* they do get non-secret attributes marked as being in the FAS */
4713 if (sa
->searchFlags
& SEARCH_FLAG_RODC_ATTRIBUTE
) {
4717 /* other attributes are denied */
4721 /* return fsmo role dn and role owner dn for a particular role*/
4722 WERROR
dsdb_get_fsmo_role_info(TALLOC_CTX
*tmp_ctx
,
4723 struct ldb_context
*ldb
,
4725 struct ldb_dn
**fsmo_role_dn
,
4726 struct ldb_dn
**role_owner_dn
)
4730 case DREPL_NAMING_MASTER
:
4731 *fsmo_role_dn
= samdb_partitions_dn(ldb
, tmp_ctx
);
4732 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4733 if (ret
!= LDB_SUCCESS
) {
4734 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Naming Master object - %s",
4735 ldb_errstring(ldb
)));
4736 talloc_free(tmp_ctx
);
4737 return WERR_DS_DRA_INTERNAL_ERROR
;
4740 case DREPL_INFRASTRUCTURE_MASTER
:
4741 *fsmo_role_dn
= samdb_infrastructure_dn(ldb
, tmp_ctx
);
4742 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4743 if (ret
!= LDB_SUCCESS
) {
4744 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Schema Master object - %s",
4745 ldb_errstring(ldb
)));
4746 talloc_free(tmp_ctx
);
4747 return WERR_DS_DRA_INTERNAL_ERROR
;
4750 case DREPL_RID_MASTER
:
4751 ret
= samdb_rid_manager_dn(ldb
, tmp_ctx
, fsmo_role_dn
);
4752 if (ret
!= LDB_SUCCESS
) {
4753 DEBUG(0, (__location__
": Failed to find RID Manager object - %s", ldb_errstring(ldb
)));
4754 talloc_free(tmp_ctx
);
4755 return WERR_DS_DRA_INTERNAL_ERROR
;
4758 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4759 if (ret
!= LDB_SUCCESS
) {
4760 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in RID Manager object - %s",
4761 ldb_errstring(ldb
)));
4762 talloc_free(tmp_ctx
);
4763 return WERR_DS_DRA_INTERNAL_ERROR
;
4766 case DREPL_SCHEMA_MASTER
:
4767 *fsmo_role_dn
= ldb_get_schema_basedn(ldb
);
4768 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4769 if (ret
!= LDB_SUCCESS
) {
4770 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Schema Master object - %s",
4771 ldb_errstring(ldb
)));
4772 talloc_free(tmp_ctx
);
4773 return WERR_DS_DRA_INTERNAL_ERROR
;
4776 case DREPL_PDC_MASTER
:
4777 *fsmo_role_dn
= ldb_get_default_basedn(ldb
);
4778 ret
= samdb_reference_dn(ldb
, tmp_ctx
, *fsmo_role_dn
, "fSMORoleOwner", role_owner_dn
);
4779 if (ret
!= LDB_SUCCESS
) {
4780 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in Pd Master object - %s",
4781 ldb_errstring(ldb
)));
4782 talloc_free(tmp_ctx
);
4783 return WERR_DS_DRA_INTERNAL_ERROR
;
4787 return WERR_DS_DRA_INTERNAL_ERROR
;
4792 const char *samdb_dn_to_dnshostname(struct ldb_context
*ldb
,
4793 TALLOC_CTX
*mem_ctx
,
4794 struct ldb_dn
*server_dn
)
4797 struct ldb_result
*res
= NULL
;
4798 const char * const attrs
[] = { "dNSHostName", NULL
};
4800 ldb_ret
= ldb_search(ldb
, mem_ctx
, &res
,
4804 if (ldb_ret
!= LDB_SUCCESS
) {
4805 DEBUG(4, ("Failed to find dNSHostName for dn %s, ldb error: %s",
4806 ldb_dn_get_linearized(server_dn
), ldb_errstring(ldb
)));
4810 return ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
4814 returns true if an attribute is in the filter,
4815 false otherwise, provided that attribute value is provided with the expression
4817 bool dsdb_attr_in_parse_tree(struct ldb_parse_tree
*tree
,
4821 switch (tree
->operation
) {
4824 for (i
=0;i
<tree
->u
.list
.num_elements
;i
++) {
4825 if (dsdb_attr_in_parse_tree(tree
->u
.list
.elements
[i
],
4831 return dsdb_attr_in_parse_tree(tree
->u
.isnot
.child
, attr
);
4832 case LDB_OP_EQUALITY
:
4833 case LDB_OP_GREATER
:
4836 if (ldb_attr_cmp(tree
->u
.equality
.attr
, attr
) == 0) {
4840 case LDB_OP_SUBSTRING
:
4841 if (ldb_attr_cmp(tree
->u
.substring
.attr
, attr
) == 0) {
4845 case LDB_OP_PRESENT
:
4846 /* (attrname=*) is not filtered out */
4848 case LDB_OP_EXTENDED
:
4849 if (tree
->u
.extended
.attr
&&
4850 ldb_attr_cmp(tree
->u
.extended
.attr
, attr
) == 0) {
4858 bool is_attr_in_list(const char * const * attrs
, const char *attr
)
4862 for (i
= 0; attrs
[i
]; i
++) {
4863 if (ldb_attr_cmp(attrs
[i
], attr
) == 0)
4872 map an ldb error code to an approximate NTSTATUS code
4874 NTSTATUS
dsdb_ldb_err_to_ntstatus(int err
)
4878 return NT_STATUS_OK
;
4880 case LDB_ERR_PROTOCOL_ERROR
:
4881 return NT_STATUS_DEVICE_PROTOCOL_ERROR
;
4883 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
4884 return NT_STATUS_IO_TIMEOUT
;
4886 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
4887 return NT_STATUS_BUFFER_TOO_SMALL
;
4889 case LDB_ERR_COMPARE_FALSE
:
4890 case LDB_ERR_COMPARE_TRUE
:
4891 return NT_STATUS_REVISION_MISMATCH
;
4893 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
4894 return NT_STATUS_NOT_SUPPORTED
;
4896 case LDB_ERR_STRONG_AUTH_REQUIRED
:
4897 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
4898 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
4899 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
4900 case LDB_ERR_INVALID_CREDENTIALS
:
4901 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
4902 case LDB_ERR_UNWILLING_TO_PERFORM
:
4903 return NT_STATUS_ACCESS_DENIED
;
4905 case LDB_ERR_NO_SUCH_OBJECT
:
4906 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4908 case LDB_ERR_REFERRAL
:
4909 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
4910 return NT_STATUS_NOT_FOUND
;
4912 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
4913 return NT_STATUS_NOT_SUPPORTED
;
4915 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
4916 return NT_STATUS_BUFFER_TOO_SMALL
;
4918 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
4919 case LDB_ERR_INAPPROPRIATE_MATCHING
:
4920 case LDB_ERR_CONSTRAINT_VIOLATION
:
4921 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
4922 case LDB_ERR_INVALID_DN_SYNTAX
:
4923 case LDB_ERR_NAMING_VIOLATION
:
4924 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
4925 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
4926 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
4927 return NT_STATUS_INVALID_PARAMETER
;
4929 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
4930 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
4931 return NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS
;
4934 return NT_STATUS_NETWORK_BUSY
;
4936 case LDB_ERR_ALIAS_PROBLEM
:
4937 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
4938 case LDB_ERR_UNAVAILABLE
:
4939 case LDB_ERR_LOOP_DETECT
:
4940 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
4941 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
4943 case LDB_ERR_OPERATIONS_ERROR
:
4946 return NT_STATUS_UNSUCCESSFUL
;
4951 create a new naming context that will hold a partial replica
4953 int dsdb_create_partial_replica_NC(struct ldb_context
*ldb
, struct ldb_dn
*dn
)
4955 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
4956 struct ldb_message
*msg
;
4959 msg
= ldb_msg_new(tmp_ctx
);
4961 talloc_free(tmp_ctx
);
4962 return ldb_oom(ldb
);
4966 ret
= ldb_msg_add_string(msg
, "objectClass", "top");
4967 if (ret
!= LDB_SUCCESS
) {
4968 talloc_free(tmp_ctx
);
4969 return ldb_oom(ldb
);
4972 /* [MS-DRSR] implies that we should only add the 'top'
4973 * objectclass, but that would cause lots of problems with our
4974 * objectclass code as top is not structural, so we add
4975 * 'domainDNS' as well to keep things sane. We're expecting
4976 * this new NC to be of objectclass domainDNS after
4977 * replication anyway
4979 ret
= ldb_msg_add_string(msg
, "objectClass", "domainDNS");
4980 if (ret
!= LDB_SUCCESS
) {
4981 talloc_free(tmp_ctx
);
4982 return ldb_oom(ldb
);
4985 ret
= ldb_msg_add_fmt(msg
, "instanceType", "%u",
4986 INSTANCE_TYPE_IS_NC_HEAD
|
4987 INSTANCE_TYPE_NC_ABOVE
|
4988 INSTANCE_TYPE_UNINSTANT
);
4989 if (ret
!= LDB_SUCCESS
) {
4990 talloc_free(tmp_ctx
);
4991 return ldb_oom(ldb
);
4994 ret
= dsdb_add(ldb
, msg
, DSDB_MODIFY_PARTIAL_REPLICA
);
4995 if (ret
!= LDB_SUCCESS
&& ret
!= LDB_ERR_ENTRY_ALREADY_EXISTS
) {
4996 DEBUG(0,("Failed to create new NC for %s - %s (%s)\n",
4997 ldb_dn_get_linearized(dn
),
4998 ldb_errstring(ldb
), ldb_strerror(ret
)));
4999 talloc_free(tmp_ctx
);
5003 DEBUG(1,("Created new NC for %s\n", ldb_dn_get_linearized(dn
)));
5005 talloc_free(tmp_ctx
);
5010 build a GUID from a string
5012 _PUBLIC_ NTSTATUS
NS_GUID_from_string(const char *s
, struct GUID
*guid
)
5014 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
5016 uint32_t time_mid
, time_hi_and_version
;
5017 uint32_t clock_seq
[2];
5022 return NT_STATUS_INVALID_PARAMETER
;
5025 if (11 == sscanf(s
, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
5026 &time_low
, &time_mid
, &time_hi_and_version
,
5027 &clock_seq
[0], &clock_seq
[1],
5028 &node
[0], &node
[1], &node
[2], &node
[3], &node
[4], &node
[5])) {
5029 status
= NT_STATUS_OK
;
5032 if (!NT_STATUS_IS_OK(status
)) {
5036 guid
->time_low
= time_low
;
5037 guid
->time_mid
= time_mid
;
5038 guid
->time_hi_and_version
= time_hi_and_version
;
5039 guid
->clock_seq
[0] = clock_seq
[0];
5040 guid
->clock_seq
[1] = clock_seq
[1];
5042 guid
->node
[i
] = node
[i
];
5045 return NT_STATUS_OK
;
5048 _PUBLIC_
char *NS_GUID_string(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
5050 return talloc_asprintf(mem_ctx
,
5051 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
5052 guid
->time_low
, guid
->time_mid
,
5053 guid
->time_hi_and_version
,
5056 guid
->node
[0], guid
->node
[1],
5057 guid
->node
[2], guid
->node
[3],
5058 guid
->node
[4], guid
->node
[5]);
5062 * Return the effective badPwdCount
5064 * This requires that the user_msg have (if present):
5068 * This also requires that the domain_msg have (if present):
5069 * - lockOutObservationWindow
5071 static int dsdb_effective_badPwdCount(struct ldb_message
*user_msg
,
5072 int64_t lockOutObservationWindow
,
5075 int64_t badPasswordTime
;
5076 badPasswordTime
= ldb_msg_find_attr_as_int64(user_msg
, "badPasswordTime", 0);
5078 if (badPasswordTime
- lockOutObservationWindow
>= now
) {
5079 return ldb_msg_find_attr_as_int(user_msg
, "badPwdCount", 0);
5086 * Return the effective badPwdCount
5088 * This requires that the user_msg have (if present):
5093 int samdb_result_effective_badPwdCount(struct ldb_context
*sam_ldb
,
5094 TALLOC_CTX
*mem_ctx
,
5095 struct ldb_dn
*domain_dn
,
5096 struct ldb_message
*user_msg
)
5098 struct timeval tv_now
= timeval_current();
5099 NTTIME now
= timeval_to_nttime(&tv_now
);
5100 int64_t lockOutObservationWindow
= samdb_search_int64(sam_ldb
, mem_ctx
, 0, domain_dn
,
5101 "lockOutObservationWindow", NULL
);
5102 return dsdb_effective_badPwdCount(user_msg
, lockOutObservationWindow
, now
);
5106 * Prepare an update to the badPwdCount and associated attributes.
5108 * This requires that the user_msg have (if present):
5113 * This also requires that the domain_msg have (if present):
5115 * - lockoutThreshold
5116 * - lockOutObservationWindow
5118 NTSTATUS
dsdb_update_bad_pwd_count(TALLOC_CTX
*mem_ctx
,
5119 struct ldb_context
*sam_ctx
,
5120 struct ldb_message
*user_msg
,
5121 struct ldb_message
*domain_msg
,
5122 struct ldb_message
**_mod_msg
)
5124 int i
, ret
, badPwdCount
;
5125 int64_t lockoutThreshold
, lockOutObservationWindow
;
5126 struct dom_sid
*sid
;
5127 struct timeval tv_now
= timeval_current();
5128 NTTIME now
= timeval_to_nttime(&tv_now
);
5130 uint32_t pwdProperties
, rid
= 0;
5131 struct ldb_message
*mod_msg
;
5133 sid
= samdb_result_dom_sid(mem_ctx
, user_msg
, "objectSid");
5135 pwdProperties
= ldb_msg_find_attr_as_uint(domain_msg
,
5136 "pwdProperties", -1);
5137 if (sid
&& !(pwdProperties
& DOMAIN_PASSWORD_LOCKOUT_ADMINS
)) {
5138 status
= dom_sid_split_rid(NULL
, sid
, NULL
, &rid
);
5139 if (!NT_STATUS_IS_OK(status
)) {
5141 * This can't happen anyway, but always try
5142 * and update the badPwdCount on failure
5150 * Work out if we are doing password lockout on the domain.
5151 * Also, the built in administrator account is exempt:
5152 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa375371%28v=vs.85%29.aspx
5154 lockoutThreshold
= ldb_msg_find_attr_as_int(domain_msg
,
5155 "lockoutThreshold", 0);
5156 if (lockoutThreshold
== 0 || (rid
== DOMAIN_RID_ADMINISTRATOR
)) {
5157 DEBUG(5, ("Not updating badPwdCount on %s after wrong password\n",
5158 ldb_dn_get_linearized(user_msg
->dn
)));
5159 return NT_STATUS_OK
;
5162 mod_msg
= ldb_msg_new(mem_ctx
);
5163 if (mod_msg
== NULL
) {
5164 return NT_STATUS_NO_MEMORY
;
5166 mod_msg
->dn
= ldb_dn_copy(mod_msg
, user_msg
->dn
);
5167 if (mod_msg
->dn
== NULL
) {
5168 TALLOC_FREE(mod_msg
);
5169 return NT_STATUS_NO_MEMORY
;
5172 lockOutObservationWindow
= ldb_msg_find_attr_as_int64(domain_msg
,
5173 "lockOutObservationWindow", 0);
5175 badPwdCount
= dsdb_effective_badPwdCount(user_msg
, lockOutObservationWindow
, now
);
5179 ret
= samdb_msg_add_int(sam_ctx
, mod_msg
, mod_msg
, "badPwdCount", badPwdCount
);
5180 if (ret
!= LDB_SUCCESS
) {
5181 TALLOC_FREE(mod_msg
);
5182 return NT_STATUS_NO_MEMORY
;
5184 ret
= samdb_msg_add_int64(sam_ctx
, mod_msg
, mod_msg
, "badPasswordTime", now
);
5185 if (ret
!= LDB_SUCCESS
) {
5186 TALLOC_FREE(mod_msg
);
5187 return NT_STATUS_NO_MEMORY
;
5190 if (badPwdCount
>= lockoutThreshold
) {
5191 ret
= samdb_msg_add_int64(sam_ctx
, mod_msg
, mod_msg
, "lockoutTime", now
);
5192 if (ret
!= LDB_SUCCESS
) {
5193 TALLOC_FREE(mod_msg
);
5194 return NT_STATUS_NO_MEMORY
;
5196 DEBUG(5, ("Locked out user %s after %d wrong passwords\n",
5197 ldb_dn_get_linearized(user_msg
->dn
), badPwdCount
));
5199 DEBUG(5, ("Updated badPwdCount on %s after %d wrong passwords\n",
5200 ldb_dn_get_linearized(user_msg
->dn
), badPwdCount
));
5203 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
5204 for (i
=0; i
< mod_msg
->num_elements
; i
++) {
5205 mod_msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
5208 *_mod_msg
= mod_msg
;
5209 return NT_STATUS_OK
;
5213 * Sets defaults for a User object
5214 * List of default attributes set:
5215 * accountExpires, badPasswordTime, badPwdCount,
5216 * codePage, countryCode, lastLogoff, lastLogon
5217 * logonCount, pwdLastSet
5219 int dsdb_user_obj_set_defaults(struct ldb_context
*ldb
, struct ldb_message
*usr_obj
)
5222 const struct attribute_values
{
5227 .name
= "accountExpires",
5228 .value
= "9223372036854775807"
5231 .name
= "badPasswordTime",
5235 .name
= "badPwdCount",
5243 .name
= "countryCode",
5247 .name
= "lastLogoff",
5251 .name
= "lastLogon",
5255 .name
= "logonCount",
5259 .name
= "pwdLastSet",
5264 for (i
= 0; i
< ARRAY_SIZE(map
); i
++) {
5265 ret
= samdb_find_or_add_attribute(ldb
, usr_obj
,
5266 map
[i
].name
, map
[i
].value
);
5267 if (ret
!= LDB_SUCCESS
) {
5276 * Sets 'sAMAccountType on user object based on userAccountControl
5277 * @param ldb Current ldb_context
5278 * @param usr_obj ldb_message representing User object
5279 * @param user_account_control Value for userAccountControl flags
5280 * @param account_type_p Optional pointer to account_type to return
5281 * @return LDB_SUCCESS or LDB_ERR* code on failure
5283 int dsdb_user_obj_set_account_type(struct ldb_context
*ldb
, struct ldb_message
*usr_obj
,
5284 uint32_t user_account_control
, uint32_t *account_type_p
)
5287 uint32_t account_type
;
5288 struct ldb_message_element
*el
;
5290 account_type
= ds_uf2atype(user_account_control
);
5291 if (account_type
== 0) {
5292 ldb_set_errstring(ldb
, "dsdb: Unrecognized account type!");
5293 return LDB_ERR_UNWILLING_TO_PERFORM
;
5295 ret
= samdb_msg_add_uint(ldb
, usr_obj
, usr_obj
,
5298 if (ret
!= LDB_SUCCESS
) {
5301 el
= ldb_msg_find_element(usr_obj
, "sAMAccountType");
5302 el
->flags
= LDB_FLAG_MOD_REPLACE
;
5304 if (account_type_p
) {
5305 *account_type_p
= account_type
;
5312 * Determine and set primaryGroupID based on userAccountControl value
5313 * @param ldb Current ldb_context
5314 * @param usr_obj ldb_message representing User object
5315 * @param user_account_control Value for userAccountControl flags
5316 * @param group_rid_p Optional pointer to group RID to return
5317 * @return LDB_SUCCESS or LDB_ERR* code on failure
5319 int dsdb_user_obj_set_primary_group_id(struct ldb_context
*ldb
, struct ldb_message
*usr_obj
,
5320 uint32_t user_account_control
, uint32_t *group_rid_p
)
5324 struct ldb_message_element
*el
;
5326 rid
= ds_uf2prim_group_rid(user_account_control
);
5328 ret
= samdb_msg_add_uint(ldb
, usr_obj
, usr_obj
,
5329 "primaryGroupID", rid
);
5330 if (ret
!= LDB_SUCCESS
) {
5333 el
= ldb_msg_find_element(usr_obj
, "primaryGroupID");
5334 el
->flags
= LDB_FLAG_MOD_REPLACE
;