2 Unix SMB/CIFS implementation.
3 pdb glue module for direct access to the dsdb via LDB APIs
4 Copyright (C) Volker Lendecke 2009-2011
5 Copyright (C) Andrew Bartlett 2010-2012
6 Copyright (C) Matthias Dieter Wallnöfer 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* This module, is a port of Volker's pdb_ads to ldb and DSDB APIs */
25 #include "source3/include/passdb.h"
26 #include "source4/dsdb/samdb/samdb.h"
27 #include "ldb_errors.h"
28 #include "libcli/security/dom_sid.h"
29 #include "source4/winbind/idmap.h"
30 #include "librpc/gen_ndr/ndr_security.h"
31 #include "libds/common/flag_mapping.h"
32 #include "source4/lib/events/events.h"
33 #include "source4/auth/session.h"
34 #include "source4/auth/system_session_proto.h"
35 #include "lib/param/param.h"
36 #include "source4/dsdb/common/util.h"
37 #include "source3/include/secrets.h"
39 struct pdb_samba_dsdb_state
{
40 struct tevent_context
*ev
;
41 struct ldb_context
*ldb
;
42 struct idmap_context
*idmap_ctx
;
43 struct loadparm_context
*lp_ctx
;
46 static NTSTATUS
pdb_samba_dsdb_getsampwsid(struct pdb_methods
*m
,
47 struct samu
*sam_acct
,
48 const struct dom_sid
*sid
);
49 static NTSTATUS
pdb_samba_dsdb_getsamupriv(struct pdb_samba_dsdb_state
*state
,
52 struct ldb_message
**pmsg
);
53 static bool pdb_samba_dsdb_sid_to_id(struct pdb_methods
*m
, const struct dom_sid
*sid
,
56 static bool pdb_samba_dsdb_pull_time(struct ldb_message
*msg
, const char *attr
,
60 if (! ldb_msg_find_element(msg
, attr
)) {
63 tmp
= ldb_msg_find_attr_as_uint64(msg
, attr
, 0);
64 *ptime
= nt_time_to_unix(tmp
);
68 static struct pdb_domain_info
*pdb_samba_dsdb_get_domain_info(
69 struct pdb_methods
*m
, TALLOC_CTX
*mem_ctx
)
71 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
72 m
->private_data
, struct pdb_samba_dsdb_state
);
73 struct pdb_domain_info
*info
;
74 struct dom_sid
*domain_sid
;
75 struct ldb_dn
*forest_dn
, *domain_dn
;
76 struct ldb_result
*dom_res
= NULL
;
77 const char *dom_attrs
[] = {
86 info
= talloc(mem_ctx
, struct pdb_domain_info
);
91 domain_dn
= ldb_get_default_basedn(state
->ldb
);
93 ret
= ldb_search(state
->ldb
, info
, &dom_res
,
94 domain_dn
, LDB_SCOPE_BASE
, dom_attrs
, NULL
);
95 if (ret
!= LDB_SUCCESS
) {
98 if (dom_res
->count
!= 1) {
102 info
->guid
= samdb_result_guid(dom_res
->msgs
[0], "objectGUID");
104 domain_sid
= samdb_result_dom_sid(state
, dom_res
->msgs
[0], "objectSid");
108 info
->sid
= *domain_sid
;
110 TALLOC_FREE(dom_res
);
112 info
->name
= talloc_strdup(info
, lpcfg_sam_name(state
->lp_ctx
));
113 info
->dns_domain
= ldb_dn_canonical_string(info
, domain_dn
);
115 if (!info
->dns_domain
) {
118 p
= strchr(info
->dns_domain
, '/');
123 forest_dn
= ldb_get_root_basedn(state
->ldb
);
128 info
->dns_forest
= ldb_dn_canonical_string(info
, forest_dn
);
129 if (!info
->dns_forest
) {
132 p
= strchr(info
->dns_forest
, '/');
140 TALLOC_FREE(dom_res
);
145 static struct ldb_message
*pdb_samba_dsdb_get_samu_private(
146 struct pdb_methods
*m
, struct samu
*sam
)
148 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
149 m
->private_data
, struct pdb_samba_dsdb_state
);
150 struct ldb_message
*msg
;
151 char *sidstr
, *filter
;
154 msg
= (struct ldb_message
*)
155 pdb_get_backend_private_data(sam
, m
);
158 return talloc_get_type_abort(msg
, struct ldb_message
);
161 sidstr
= dom_sid_string(talloc_tos(), pdb_get_user_sid(sam
));
162 if (sidstr
== NULL
) {
166 filter
= talloc_asprintf(
167 talloc_tos(), "(&(objectsid=%s)(objectclass=user))", sidstr
);
169 if (filter
== NULL
) {
173 status
= pdb_samba_dsdb_getsamupriv(state
, filter
, sam
, &msg
);
175 if (!NT_STATUS_IS_OK(status
)) {
182 static NTSTATUS
pdb_samba_dsdb_init_sam_from_priv(struct pdb_methods
*m
,
184 struct ldb_message
*msg
)
186 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
187 m
->private_data
, struct pdb_samba_dsdb_state
);
188 TALLOC_CTX
*frame
= talloc_stackframe();
189 NTSTATUS status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
192 struct dom_sid
*sid
, group_sid
;
194 const DATA_BLOB
*blob
;
196 str
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
198 DEBUG(10, ("no samAccountName\n"));
201 pdb_set_username(sam
, str
, PDB_SET
);
203 if (pdb_samba_dsdb_pull_time(msg
, "lastLogon", &tmp_time
)) {
204 pdb_set_logon_time(sam
, tmp_time
, PDB_SET
);
206 if (pdb_samba_dsdb_pull_time(msg
, "lastLogoff", &tmp_time
)) {
207 pdb_set_logoff_time(sam
, tmp_time
, PDB_SET
);
209 if (pdb_samba_dsdb_pull_time(msg
, "pwdLastSet", &tmp_time
)) {
210 pdb_set_pass_last_set_time(sam
, tmp_time
, PDB_SET
);
212 if (pdb_samba_dsdb_pull_time(msg
, "accountExpires", &tmp_time
)) {
213 pdb_set_kickoff_time(sam
, tmp_time
, PDB_SET
);
216 str
= ldb_msg_find_attr_as_string(msg
, "displayName",
219 pdb_set_fullname(sam
, str
, PDB_SET
);
222 str
= ldb_msg_find_attr_as_string(msg
, "homeDirectory",
225 pdb_set_homedir(sam
, str
, PDB_SET
);
228 str
= ldb_msg_find_attr_as_string(msg
, "homeDrive", NULL
);
230 pdb_set_dir_drive(sam
, str
, PDB_SET
);
233 str
= ldb_msg_find_attr_as_string(msg
, "scriptPath", NULL
);
235 pdb_set_logon_script(sam
, str
, PDB_SET
);
238 str
= ldb_msg_find_attr_as_string(msg
, "profilePath",
241 pdb_set_profile_path(sam
, str
, PDB_SET
);
244 str
= ldb_msg_find_attr_as_string(msg
, "comment",
247 pdb_set_comment(sam
, str
, PDB_SET
);
250 str
= ldb_msg_find_attr_as_string(msg
, "description",
253 pdb_set_acct_desc(sam
, str
, PDB_SET
);
256 str
= ldb_msg_find_attr_as_string(msg
, "userWorkstations",
259 pdb_set_workstations(sam
, str
, PDB_SET
);
262 str
= ldb_msg_find_attr_as_string(msg
, "userParameters",
265 pdb_set_munged_dial(sam
, str
, PDB_SET
);
268 sid
= samdb_result_dom_sid(talloc_tos(), msg
, "objectSid");
270 DEBUG(10, ("Could not pull SID\n"));
273 pdb_set_user_sid(sam
, sid
, PDB_SET
);
275 n
= samdb_result_acct_flags(msg
, "msDS-User-Account-Control-Computed");
277 DEBUG(10, ("Could not pull userAccountControl\n"));
280 pdb_set_acct_ctrl(sam
, n
, PDB_SET
);
282 blob
= ldb_msg_find_ldb_val(msg
, "unicodePwd");
284 if (blob
->length
!= NT_HASH_LEN
) {
285 DEBUG(0, ("Got NT hash of length %d, expected %d\n",
286 (int)blob
->length
, NT_HASH_LEN
));
289 pdb_set_nt_passwd(sam
, blob
->data
, PDB_SET
);
292 blob
= ldb_msg_find_ldb_val(msg
, "dBCSPwd");
294 if (blob
->length
!= LM_HASH_LEN
) {
295 DEBUG(0, ("Got LM hash of length %d, expected %d\n",
296 (int)blob
->length
, LM_HASH_LEN
));
299 pdb_set_lanman_passwd(sam
, blob
->data
, PDB_SET
);
302 n
= ldb_msg_find_attr_as_uint(msg
, "primaryGroupID", 0);
304 DEBUG(10, ("Could not pull primaryGroupID\n"));
307 sid_compose(&group_sid
, samdb_domain_sid(state
->ldb
), n
);
308 pdb_set_group_sid(sam
, &group_sid
, PDB_SET
);
310 status
= NT_STATUS_OK
;
316 static bool pdb_samba_dsdb_add_time(struct ldb_message
*msg
,
317 const char *attrib
, time_t t
)
321 unix_to_nt_time(&nt_time
, t
);
323 return ldb_msg_add_fmt(msg
, attrib
, "%llu", (unsigned long long) nt_time
);
326 static int pdb_samba_dsdb_replace_by_sam(struct pdb_samba_dsdb_state
*state
,
327 bool (*need_update
)(const struct samu
*,
332 TALLOC_CTX
*frame
= talloc_stackframe();
333 int ret
= LDB_SUCCESS
;
335 struct ldb_message
*msg
;
336 struct ldb_request
*req
;
337 uint32_t dsdb_flags
= 0;
338 /* TODO: All fields :-) */
340 msg
= ldb_msg_new(frame
);
347 /* build modify request */
348 ret
= ldb_build_mod_req(&req
, state
->ldb
, frame
, msg
, NULL
, NULL
,
349 ldb_op_default_callback
,
351 if (ret
!= LDB_SUCCESS
) {
356 /* If we set a plaintext password, the system will
357 * force the pwdLastSet to now() */
358 if (need_update(sam
, PDB_PASSLASTSET
)) {
359 dsdb_flags
= DSDB_PASSWORD_BYPASS_LAST_SET
;
361 ret
|= pdb_samba_dsdb_add_time(msg
, "pwdLastSet",
362 pdb_get_pass_last_set_time(sam
));
365 pw
= pdb_get_plaintext_passwd(sam
);
366 if (need_update(sam
, PDB_PLAINTEXT_PW
)) {
367 struct ldb_val pw_utf16
;
370 return LDB_ERR_OPERATIONS_ERROR
;
373 if (!convert_string_talloc(msg
,
376 (void *)&pw_utf16
.data
,
378 return LDB_ERR_OPERATIONS_ERROR
;
380 ret
|= ldb_msg_add_value(msg
, "clearTextPassword", &pw_utf16
, NULL
);
382 bool changed_lm_pw
= false;
383 bool changed_nt_pw
= false;
384 bool changed_history
= false;
385 if (need_update(sam
, PDB_LMPASSWD
)) {
387 val
.data
= discard_const_p(uint8_t, pdb_get_lanman_passwd(sam
));
389 samdb_msg_add_delete(state
->ldb
, msg
, msg
,
392 val
.length
= LM_HASH_LEN
;
393 ret
|= ldb_msg_add_value(msg
, "dBCSPwd", &val
, NULL
);
395 changed_lm_pw
= true;
397 if (need_update(sam
, PDB_NTPASSWD
)) {
399 val
.data
= discard_const_p(uint8_t, pdb_get_nt_passwd(sam
));
401 samdb_msg_add_delete(state
->ldb
, msg
, msg
,
404 val
.length
= NT_HASH_LEN
;
405 ret
|= ldb_msg_add_value(msg
, "unicodePwd", &val
, NULL
);
407 changed_nt_pw
= true;
410 /* Try to ensure we don't get out of sync */
411 if (changed_lm_pw
&& !changed_nt_pw
) {
412 samdb_msg_add_delete(state
->ldb
, msg
, msg
,
414 } else if (changed_nt_pw
&& !changed_lm_pw
) {
415 samdb_msg_add_delete(state
->ldb
, msg
, msg
,
418 if (changed_lm_pw
|| changed_nt_pw
) {
419 samdb_msg_add_delete(state
->ldb
, msg
, msg
,
420 "supplementalCredentials");
424 if (need_update(sam
, PDB_PWHISTORY
)) {
425 uint32_t current_hist_len
;
426 const uint8_t *history
= pdb_get_pw_history(sam
, ¤t_hist_len
);
428 bool invalid_history
= false;
429 struct samr_Password
*history_hashes
= talloc_array(talloc_tos(), struct samr_Password
,
432 invalid_history
= true;
435 static const uint8_t zeros
[16];
436 /* Parse the history into the correct format */
437 for (i
= 0; i
< current_hist_len
; i
++) {
438 if (memcmp(&history
[i
*PW_HISTORY_ENTRY_LEN
], zeros
, 16) != 0) {
439 /* If the history is in the old format, with a salted hash, then we can't migrate it to AD format */
440 invalid_history
= true;
443 /* Copy out the 2nd 16 bytes of the 32 byte password history, containing the NT hash */
444 memcpy(history_hashes
[i
].hash
,
445 &history
[(i
*PW_HISTORY_ENTRY_LEN
) + PW_HISTORY_SALT_LEN
],
446 sizeof(history_hashes
[i
].hash
));
449 if (invalid_history
) {
450 ret
|= samdb_msg_add_delete(state
->ldb
, msg
, msg
,
453 ret
|= samdb_msg_add_delete(state
->ldb
, msg
, msg
,
456 ret
|= samdb_msg_add_hashes(state
->ldb
, msg
, msg
,
461 changed_history
= true;
463 if (changed_lm_pw
|| changed_nt_pw
|| changed_history
) {
464 /* These attributes can only be modified directly by using a special control */
465 dsdb_flags
= DSDB_BYPASS_PASSWORD_HASH
;
469 /* PDB_USERSID is only allowed on ADD, handled in caller */
470 if (need_update(sam
, PDB_GROUPSID
)) {
471 const struct dom_sid
*sid
= pdb_get_group_sid(sam
);
473 NTSTATUS status
= dom_sid_split_rid(NULL
, sid
, NULL
, &rid
);
474 if (!NT_STATUS_IS_OK(status
)) {
476 return LDB_ERR_OPERATIONS_ERROR
;
478 if (!dom_sid_in_domain(samdb_domain_sid(state
->ldb
), sid
)) {
480 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
482 ret
|= samdb_msg_add_uint(state
->ldb
, msg
, msg
, "primaryGroupID", rid
);
484 if (need_update(sam
, PDB_FULLNAME
)) {
485 ret
|= ldb_msg_add_string(msg
, "displayName", pdb_get_fullname(sam
));
488 if (need_update(sam
, PDB_SMBHOME
)) {
489 ret
|= ldb_msg_add_string(msg
, "homeDirectory",
490 pdb_get_homedir(sam
));
493 if (need_update(sam
, PDB_PROFILE
)) {
494 ret
|= ldb_msg_add_string(msg
, "profilePath",
495 pdb_get_profile_path(sam
));
498 if (need_update(sam
, PDB_DRIVE
)) {
499 ret
|= ldb_msg_add_string(msg
, "homeDrive",
500 pdb_get_dir_drive(sam
));
503 if (need_update(sam
, PDB_LOGONSCRIPT
)) {
504 ret
|= ldb_msg_add_string(msg
, "scriptPath",
505 pdb_get_logon_script(sam
));
508 if (need_update(sam
, PDB_KICKOFFTIME
)) {
509 ret
|= pdb_samba_dsdb_add_time(msg
, "accountExpires",
510 pdb_get_kickoff_time(sam
));
513 if (need_update(sam
, PDB_LOGONTIME
)) {
514 ret
|= pdb_samba_dsdb_add_time(msg
, "lastLogon",
515 pdb_get_logon_time(sam
));
518 if (need_update(sam
, PDB_LOGOFFTIME
)) {
519 ret
|= pdb_samba_dsdb_add_time(msg
, "lastLogoff",
520 pdb_get_logoff_time(sam
));
523 if (need_update(sam
, PDB_USERNAME
)) {
524 ret
|= ldb_msg_add_string(msg
, "samAccountName",
525 pdb_get_username(sam
));
528 if (need_update(sam
, PDB_HOURSLEN
) || need_update(sam
, PDB_HOURS
)) {
529 struct ldb_val hours
= data_blob_const(pdb_get_hours(sam
), pdb_get_hours_len(sam
));
530 ret
|= ldb_msg_add_value(msg
, "logonHours",
534 if (need_update(sam
, PDB_ACCTCTRL
)) {
535 ret
|= samdb_msg_add_acct_flags(state
->ldb
, msg
, msg
,
536 "userAccountControl", pdb_get_acct_ctrl(sam
));
539 if (need_update(sam
, PDB_COMMENT
)) {
540 ret
|= ldb_msg_add_string(msg
, "comment",
541 pdb_get_comment(sam
));
544 if (need_update(sam
, PDB_ACCTDESC
)) {
545 ret
|= ldb_msg_add_string(msg
, "description",
546 pdb_get_acct_desc(sam
));
549 if (need_update(sam
, PDB_WORKSTATIONS
)) {
550 ret
|= ldb_msg_add_string(msg
, "userWorkstations",
551 pdb_get_workstations(sam
));
554 /* This will need work, it is actually a UTF8 'string' with internal NULLs, to handle TS parameters */
555 if (need_update(sam
, PDB_MUNGEDDIAL
)) {
556 ret
|= ldb_msg_add_string(msg
, "userParameters",
557 pdb_get_munged_dial(sam
));
560 if (need_update(sam
, PDB_COUNTRY_CODE
)) {
561 ret
|= ldb_msg_add_fmt(msg
, "countryCode",
562 "%i", (int)pdb_get_country_code(sam
));
565 if (need_update(sam
, PDB_CODE_PAGE
)) {
566 ret
|= ldb_msg_add_fmt(msg
, "codePage",
567 "%i", (int)pdb_get_code_page(sam
));
570 /* Not yet handled here or not meaningful for modifies on a Samba_Dsdb backend:
571 PDB_BAD_PASSWORD_TIME,
572 PDB_CANCHANGETIME, - these are calculated per policy, not stored
574 PDB_NTUSERNAME, - this makes no sense, and never really did
576 PDB_USERSID, - Handled in pdb_samba_dsdb_add_sam_account()
578 PDB_BAD_PASSWORD_COUNT,
581 PDB_BACKEND_PRIVATE_DATA,
584 if (ret
!= LDB_SUCCESS
) {
586 return LDB_ERR_OPERATIONS_ERROR
;
589 if (msg
->num_elements
== 0) {
591 /* Nothing to do, just return success */
595 ret
= dsdb_replace(state
->ldb
, msg
, dsdb_flags
);
597 if (ret
!= LDB_SUCCESS
) {
598 DEBUG(0,("Failed to modify account record %s to set user attributes: %s\n",
599 ldb_dn_get_linearized(msg
->dn
),
600 ldb_errstring(state
->ldb
)));
607 static NTSTATUS
pdb_samba_dsdb_getsamupriv(struct pdb_samba_dsdb_state
*state
,
610 struct ldb_message
**msg
)
612 const char * attrs
[] = {
613 "lastLogon", "lastLogoff", "pwdLastSet", "accountExpires",
614 "sAMAccountName", "displayName", "homeDirectory",
615 "homeDrive", "scriptPath", "profilePath", "description",
616 "userWorkstations", "comment", "userParameters", "objectSid",
617 "primaryGroupID", "userAccountControl",
618 "msDS-User-Account-Control-Computed", "logonHours",
619 "badPwdCount", "logonCount", "countryCode", "codePage",
620 "unicodePwd", "dBCSPwd", NULL
};
622 int rc
= dsdb_search_one(state
->ldb
, mem_ctx
, msg
, ldb_get_default_basedn(state
->ldb
), LDB_SCOPE_SUBTREE
, attrs
, 0, "%s", filter
);
623 if (rc
!= LDB_SUCCESS
) {
624 DEBUG(10, ("ldap_search failed %s\n",
625 ldb_errstring(state
->ldb
)));
626 return NT_STATUS_LDAP(rc
);
632 static NTSTATUS
pdb_samba_dsdb_getsampwfilter(struct pdb_methods
*m
,
633 struct pdb_samba_dsdb_state
*state
,
634 struct samu
*sam_acct
,
635 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(4, 5)
637 struct ldb_message
*priv
;
640 char *expression
= NULL
;
641 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
642 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
644 va_start(ap
, exp_fmt
);
645 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
649 talloc_free(tmp_ctx
);
650 return NT_STATUS_NO_MEMORY
;
653 status
= pdb_samba_dsdb_getsamupriv(state
, expression
, sam_acct
, &priv
);
654 talloc_free(tmp_ctx
);
655 if (!NT_STATUS_IS_OK(status
)) {
656 DEBUG(10, ("pdb_samba_dsdb_getsamupriv failed: %s\n",
661 status
= pdb_samba_dsdb_init_sam_from_priv(m
, sam_acct
, priv
);
662 if (!NT_STATUS_IS_OK(status
)) {
663 DEBUG(10, ("pdb_samba_dsdb_init_sam_from_priv failed: %s\n",
669 pdb_set_backend_private_data(sam_acct
, priv
, NULL
, m
, PDB_SET
);
673 static NTSTATUS
pdb_samba_dsdb_getsampwnam(struct pdb_methods
*m
,
674 struct samu
*sam_acct
,
675 const char *username
)
677 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
678 m
->private_data
, struct pdb_samba_dsdb_state
);
680 return pdb_samba_dsdb_getsampwfilter(m
, state
, sam_acct
,
681 "(&(samaccountname=%s)(objectclass=user))",
685 static NTSTATUS
pdb_samba_dsdb_getsampwsid(struct pdb_methods
*m
,
686 struct samu
*sam_acct
,
687 const struct dom_sid
*sid
)
690 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
691 m
->private_data
, struct pdb_samba_dsdb_state
);
694 sidstr
= dom_sid_string(talloc_tos(), sid
);
695 NT_STATUS_HAVE_NO_MEMORY(sidstr
);
697 status
= pdb_samba_dsdb_getsampwfilter(m
, state
, sam_acct
,
698 "(&(objectsid=%s)(objectclass=user))",
704 static NTSTATUS
pdb_samba_dsdb_create_user(struct pdb_methods
*m
,
706 const char *name
, uint32 acct_flags
,
709 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
710 m
->private_data
, struct pdb_samba_dsdb_state
);
714 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
715 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
717 /* Internally this uses transactions to ensure all the steps
718 * happen or fail as one */
719 status
= dsdb_add_user(state
->ldb
, tmp_ctx
, name
, acct_flags
, NULL
,
721 if (!NT_STATUS_IS_OK(status
)) {
722 talloc_free(tmp_ctx
);
725 sid_peek_rid(sid
, rid
);
726 talloc_free(tmp_ctx
);
730 static NTSTATUS
pdb_samba_dsdb_delete_user(struct pdb_methods
*m
,
734 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
735 m
->private_data
, struct pdb_samba_dsdb_state
);
738 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
739 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
741 dn
= ldb_dn_new_fmt(tmp_ctx
, state
->ldb
, "<SID=%s>", dom_sid_string(tmp_ctx
, pdb_get_user_sid(sam
)));
742 if (!dn
|| !ldb_dn_validate(dn
)) {
743 talloc_free(tmp_ctx
);
744 return NT_STATUS_NO_MEMORY
;
746 rc
= ldb_delete(state
->ldb
, dn
);
748 if (rc
!= LDB_SUCCESS
) {
749 DEBUG(10, ("ldb_delete for %s failed: %s\n", ldb_dn_get_linearized(dn
),
750 ldb_errstring(state
->ldb
)));
751 talloc_free(tmp_ctx
);
752 return NT_STATUS_LDAP(rc
);
754 talloc_free(tmp_ctx
);
758 /* This interface takes a fully populated struct samu and places it in
759 * the database. This is not implemented at this time as we need to
760 * be careful around the creation of arbitary SIDs (ie, we must ensrue
761 * they are not left in a RID pool */
762 static NTSTATUS
pdb_samba_dsdb_add_sam_account(struct pdb_methods
*m
,
763 struct samu
*sampass
)
768 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
769 m
->private_data
, struct pdb_samba_dsdb_state
);
770 uint32_t acb_flags
= pdb_get_acct_ctrl(sampass
);
771 const char *username
= pdb_get_username(sampass
);
772 const struct dom_sid
*user_sid
= pdb_get_user_sid(sampass
);
773 TALLOC_CTX
*tframe
= talloc_stackframe();
775 acb_flags
&= (ACB_NORMAL
|ACB_WSTRUST
|ACB_SVRTRUST
|ACB_DOMTRUST
);
777 ret
= ldb_transaction_start(state
->ldb
);
778 if (ret
!= LDB_SUCCESS
) {
780 return NT_STATUS_LOCK_NOT_GRANTED
;
783 status
= dsdb_add_user(state
->ldb
, talloc_tos(), username
,
784 acb_flags
, user_sid
, NULL
, &dn
);
785 if (!NT_STATUS_IS_OK(status
)) {
786 ldb_transaction_cancel(state
->ldb
);
791 ret
= pdb_samba_dsdb_replace_by_sam(state
, pdb_element_is_set_or_changed
,
793 if (ret
!= LDB_SUCCESS
) {
794 ldb_transaction_cancel(state
->ldb
);
796 return dsdb_ldb_err_to_ntstatus(ret
);
799 ret
= ldb_transaction_commit(state
->ldb
);
800 if (ret
!= LDB_SUCCESS
) {
801 DEBUG(0,("Failed to commit transaction to add and modify account record %s: %s\n",
802 ldb_dn_get_linearized(dn
),
803 ldb_errstring(state
->ldb
)));
805 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
812 * Update the Samba_Dsdb LDB with the changes from a struct samu.
814 * This takes care not to update elements that have not been changed
817 static NTSTATUS
pdb_samba_dsdb_update_sam_account(struct pdb_methods
*m
,
820 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
821 m
->private_data
, struct pdb_samba_dsdb_state
);
822 struct ldb_message
*msg
= pdb_samba_dsdb_get_samu_private(
826 ret
= pdb_samba_dsdb_replace_by_sam(state
, pdb_element_is_changed
, msg
->dn
,
828 return dsdb_ldb_err_to_ntstatus(ret
);
831 static NTSTATUS
pdb_samba_dsdb_delete_sam_account(struct pdb_methods
*m
,
832 struct samu
*username
)
835 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
836 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
837 status
= pdb_samba_dsdb_delete_user(m
, tmp_ctx
, username
);
838 talloc_free(tmp_ctx
);
842 static NTSTATUS
pdb_samba_dsdb_rename_sam_account(struct pdb_methods
*m
,
843 struct samu
*oldname
,
846 return NT_STATUS_NOT_IMPLEMENTED
;
849 /* This is not implemented, as this module is exptected to be used
850 * with auth_samba_dsdb, and this is responible for login counters etc
853 static NTSTATUS
pdb_samba_dsdb_update_login_attempts(struct pdb_methods
*m
,
854 struct samu
*sam_acct
,
857 return NT_STATUS_NOT_IMPLEMENTED
;
860 static NTSTATUS
pdb_samba_dsdb_getgrfilter(struct pdb_methods
*m
, GROUP_MAP
*map
,
861 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(4, 5)
863 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
864 m
->private_data
, struct pdb_samba_dsdb_state
);
865 const char *attrs
[] = { "objectSid", "description", "samAccountName", "groupType",
867 struct ldb_message
*msg
;
869 char *expression
= NULL
;
873 struct id_map id_map
;
874 struct id_map
*id_maps
[2];
875 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
876 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
878 va_start(ap
, exp_fmt
);
879 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
883 talloc_free(tmp_ctx
);
884 return NT_STATUS_NO_MEMORY
;
887 rc
= dsdb_search_one(state
->ldb
, tmp_ctx
, &msg
, ldb_get_default_basedn(state
->ldb
), LDB_SCOPE_SUBTREE
, attrs
, 0, "%s", expression
);
888 if (rc
== LDB_ERR_NO_SUCH_OBJECT
) {
889 talloc_free(tmp_ctx
);
890 return NT_STATUS_NO_SUCH_GROUP
;
891 } else if (rc
!= LDB_SUCCESS
) {
892 talloc_free(tmp_ctx
);
893 DEBUG(10, ("dsdb_search_one failed %s\n",
894 ldb_errstring(state
->ldb
)));
895 return NT_STATUS_LDAP(rc
);
898 sid
= samdb_result_dom_sid(tmp_ctx
, msg
, "objectSid");
900 talloc_free(tmp_ctx
);
901 DEBUG(10, ("Could not pull SID\n"));
902 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
907 if (samdb_find_attribute(state
->ldb
, msg
, "objectClass", "group")) {
909 uint32_t grouptype
= ldb_msg_find_attr_as_uint(msg
, "groupType", 0);
911 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
:
912 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
:
913 map
->sid_name_use
= SID_NAME_ALIAS
;
915 case GTYPE_SECURITY_GLOBAL_GROUP
:
916 map
->sid_name_use
= SID_NAME_DOM_GRP
;
919 talloc_free(tmp_ctx
);
920 DEBUG(10, ("Could not pull groupType\n"));
921 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
924 map
->sid_name_use
= SID_NAME_DOM_GRP
;
928 id_maps
[0] = &id_map
;
931 status
= idmap_sids_to_xids(state
->idmap_ctx
, tmp_ctx
, id_maps
);
932 talloc_free(tmp_ctx
);
933 if (!NT_STATUS_IS_OK(status
)) {
934 talloc_free(tmp_ctx
);
937 if (id_map
.xid
.type
== ID_TYPE_GID
|| id_map
.xid
.type
== ID_TYPE_BOTH
) {
938 map
->gid
= id_map
.xid
.id
;
940 DEBUG(1, (__location__
"Did not get GUID when mapping SID for %s", expression
));
941 talloc_free(tmp_ctx
);
942 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
944 } else if (samdb_find_attribute(state
->ldb
, msg
, "objectClass", "user")) {
945 DEBUG(1, (__location__
"Got SID_NAME_USER when searching for a group with %s", expression
));
946 talloc_free(tmp_ctx
);
947 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
950 str
= ldb_msg_find_attr_as_string(msg
, "samAccountName",
953 talloc_free(tmp_ctx
);
954 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
956 map
->nt_name
= talloc_strdup(map
, str
);
958 talloc_free(tmp_ctx
);
959 return NT_STATUS_NO_MEMORY
;
962 str
= ldb_msg_find_attr_as_string(msg
, "description",
965 map
->comment
= talloc_strdup(map
, str
);
967 map
->comment
= talloc_strdup(map
, "");
970 talloc_free(tmp_ctx
);
971 return NT_STATUS_NO_MEMORY
;
974 talloc_free(tmp_ctx
);
978 static NTSTATUS
pdb_samba_dsdb_getgrsid(struct pdb_methods
*m
, GROUP_MAP
*map
,
984 filter
= talloc_asprintf(talloc_tos(),
985 "(&(objectsid=%s)(objectclass=group))",
986 sid_string_talloc(talloc_tos(), &sid
));
987 if (filter
== NULL
) {
988 return NT_STATUS_NO_MEMORY
;
991 status
= pdb_samba_dsdb_getgrfilter(m
, map
, filter
);
996 static NTSTATUS
pdb_samba_dsdb_getgrgid(struct pdb_methods
*m
, GROUP_MAP
*map
,
999 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1000 m
->private_data
, struct pdb_samba_dsdb_state
);
1002 struct id_map id_map
;
1003 struct id_map
*id_maps
[2];
1004 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1005 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1007 id_map
.xid
.id
= gid
;
1008 id_map
.xid
.type
= ID_TYPE_GID
;
1009 id_maps
[0] = &id_map
;
1012 status
= idmap_xids_to_sids(state
->idmap_ctx
, tmp_ctx
, id_maps
);
1013 if (!NT_STATUS_IS_OK(status
)) {
1016 status
= pdb_samba_dsdb_getgrsid(m
, map
, *id_map
.sid
);
1017 talloc_free(tmp_ctx
);
1021 static NTSTATUS
pdb_samba_dsdb_getgrnam(struct pdb_methods
*m
, GROUP_MAP
*map
,
1027 filter
= talloc_asprintf(talloc_tos(),
1028 "(&(samaccountname=%s)(objectclass=group))",
1030 if (filter
== NULL
) {
1031 return NT_STATUS_NO_MEMORY
;
1034 status
= pdb_samba_dsdb_getgrfilter(m
, map
, filter
);
1035 TALLOC_FREE(filter
);
1039 static NTSTATUS
pdb_samba_dsdb_create_dom_group(struct pdb_methods
*m
,
1040 TALLOC_CTX
*mem_ctx
, const char *name
,
1043 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1044 m
->private_data
, struct pdb_samba_dsdb_state
);
1046 struct dom_sid
*sid
;
1048 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
1049 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1051 status
= dsdb_add_domain_group(state
->ldb
, tmp_ctx
, name
, &sid
, &dn
);
1052 if (!NT_STATUS_IS_OK(status
)) {
1053 talloc_free(tmp_ctx
);
1057 sid_peek_rid(sid
, rid
);
1058 talloc_free(tmp_ctx
);
1059 return NT_STATUS_OK
;
1062 static NTSTATUS
pdb_samba_dsdb_delete_dom_group(struct pdb_methods
*m
,
1063 TALLOC_CTX
*mem_ctx
, uint32 rid
)
1065 const char *attrs
[] = { NULL
};
1066 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1067 m
->private_data
, struct pdb_samba_dsdb_state
);
1069 struct ldb_message
*msg
;
1072 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
1073 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1075 sid_compose(&sid
, samdb_domain_sid(state
->ldb
), rid
);
1077 if (ldb_transaction_start(state
->ldb
) != LDB_SUCCESS
) {
1078 DEBUG(0, ("Unable to start transaction in pdb_samba_dsdb_delete_dom_group()\n"));
1079 return NT_STATUS_INTERNAL_ERROR
;
1082 dn
= ldb_dn_new_fmt(tmp_ctx
, state
->ldb
, "<SID=%s>", dom_sid_string(tmp_ctx
, &sid
));
1083 if (!dn
|| !ldb_dn_validate(dn
)) {
1084 talloc_free(tmp_ctx
);
1085 ldb_transaction_cancel(state
->ldb
);
1086 return NT_STATUS_NO_MEMORY
;
1088 rc
= dsdb_search_one(state
->ldb
, tmp_ctx
, &msg
, dn
, LDB_SCOPE_BASE
, attrs
, 0, "objectclass=group");
1089 if (rc
== LDB_ERR_NO_SUCH_OBJECT
) {
1090 talloc_free(tmp_ctx
);
1091 ldb_transaction_cancel(state
->ldb
);
1092 return NT_STATUS_NO_SUCH_GROUP
;
1094 rc
= ldb_delete(state
->ldb
, dn
);
1095 if (rc
== LDB_ERR_NO_SUCH_OBJECT
) {
1096 talloc_free(tmp_ctx
);
1097 ldb_transaction_cancel(state
->ldb
);
1098 return NT_STATUS_NO_SUCH_GROUP
;
1099 } else if (rc
!= LDB_SUCCESS
) {
1100 DEBUG(10, ("ldb_delete failed %s\n",
1101 ldb_errstring(state
->ldb
)));
1102 ldb_transaction_cancel(state
->ldb
);
1103 return NT_STATUS_LDAP(rc
);
1106 if (ldb_transaction_commit(state
->ldb
) != LDB_SUCCESS
) {
1107 DEBUG(0, ("Unable to commit transaction in pdb_samba_dsdb_delete_dom_group()\n"));
1108 return NT_STATUS_INTERNAL_ERROR
;
1110 return NT_STATUS_OK
;
1113 static NTSTATUS
pdb_samba_dsdb_add_group_mapping_entry(struct pdb_methods
*m
,
1116 return NT_STATUS_NOT_IMPLEMENTED
;
1119 static NTSTATUS
pdb_samba_dsdb_update_group_mapping_entry(struct pdb_methods
*m
,
1122 return NT_STATUS_NOT_IMPLEMENTED
;
1125 static NTSTATUS
pdb_samba_dsdb_delete_group_mapping_entry(struct pdb_methods
*m
,
1128 return NT_STATUS_NOT_IMPLEMENTED
;
1131 static NTSTATUS
pdb_samba_dsdb_enum_group_mapping(struct pdb_methods
*m
,
1132 const struct dom_sid
*sid
,
1133 enum lsa_SidType sid_name_use
,
1134 GROUP_MAP
***pp_rmap
,
1135 size_t *p_num_entries
,
1138 return NT_STATUS_NOT_IMPLEMENTED
;
1141 static NTSTATUS
pdb_samba_dsdb_enum_group_members(struct pdb_methods
*m
,
1142 TALLOC_CTX
*mem_ctx
,
1143 const struct dom_sid
*group
,
1144 uint32_t **pmembers
,
1145 size_t *pnum_members
)
1147 unsigned int i
, num_sids
, num_members
;
1148 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1149 m
->private_data
, struct pdb_samba_dsdb_state
);
1150 struct dom_sid
*members_as_sids
;
1151 struct dom_sid
*dom_sid
;
1156 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
1157 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1159 dn
= ldb_dn_new_fmt(tmp_ctx
, state
->ldb
, "<SID=%s>", dom_sid_string(tmp_ctx
, group
));
1160 if (!dn
|| !ldb_dn_validate(dn
)) {
1161 return NT_STATUS_NO_MEMORY
;
1164 status
= dsdb_enum_group_mem(state
->ldb
, tmp_ctx
, dn
, &members_as_sids
, &num_sids
);
1165 if (!NT_STATUS_IS_OK(status
)) {
1166 talloc_free(tmp_ctx
);
1169 status
= dom_sid_split_rid(tmp_ctx
, group
, &dom_sid
, NULL
);
1170 if (!NT_STATUS_IS_OK(status
)) {
1171 talloc_free(tmp_ctx
);
1175 *pmembers
= members
= talloc_array(mem_ctx
, uint32_t, num_sids
);
1176 if (*pmembers
== NULL
) {
1177 TALLOC_FREE(tmp_ctx
);
1178 return NT_STATUS_NO_MEMORY
;
1182 for (i
= 0; i
< num_sids
; i
++) {
1183 if (!dom_sid_in_domain(dom_sid
, &members_as_sids
[i
])) {
1186 status
= dom_sid_split_rid(NULL
, &members_as_sids
[i
],
1187 NULL
, &members
[num_members
]);
1188 if (!NT_STATUS_IS_OK(status
)) {
1189 talloc_free(tmp_ctx
);
1194 *pnum_members
= num_members
;
1195 return NT_STATUS_OK
;
1198 /* Just convert the primary group SID into a group */
1199 static NTSTATUS
fake_enum_group_memberships(struct pdb_samba_dsdb_state
*state
,
1200 TALLOC_CTX
*mem_ctx
,
1202 struct dom_sid
**pp_sids
,
1204 uint32_t *p_num_groups
)
1207 size_t num_groups
= 0;
1208 struct dom_sid
*group_sids
= NULL
;
1210 TALLOC_CTX
*tmp_ctx
;
1212 tmp_ctx
= talloc_new(mem_ctx
);
1213 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1215 if (user
->group_sid
) {
1216 struct id_map
*id_maps
[2];
1217 struct id_map id_map
;
1221 group_sids
= talloc_array(tmp_ctx
, struct dom_sid
, num_groups
);
1222 if (group_sids
== NULL
) {
1223 talloc_free(tmp_ctx
);
1224 return NT_STATUS_NO_MEMORY
;
1226 gids
= talloc_array(tmp_ctx
, gid_t
, num_groups
);
1228 talloc_free(tmp_ctx
);
1229 return NT_STATUS_NO_MEMORY
;
1232 group_sids
[0] = *user
->group_sid
;
1234 ZERO_STRUCT(id_map
);
1235 id_map
.sid
= &group_sids
[0];
1236 id_maps
[0] = &id_map
;
1239 status
= idmap_sids_to_xids(state
->idmap_ctx
, tmp_ctx
, id_maps
);
1240 if (!NT_STATUS_IS_OK(status
)) {
1241 talloc_free(tmp_ctx
);
1244 if (id_map
.xid
.type
== ID_TYPE_GID
|| id_map
.xid
.type
== ID_TYPE_BOTH
) {
1245 gids
[0] = id_map
.xid
.id
;
1247 DEBUG(1, (__location__
1248 "Group %s, of which %s is a member, could not be converted to a GID\n",
1249 dom_sid_string(tmp_ctx
, &group_sids
[0]),
1250 dom_sid_string(tmp_ctx
, &user
->user_sid
)));
1251 talloc_free(tmp_ctx
);
1252 /* We must error out, otherwise a user might
1253 * avoid a DENY acl based on a group they
1255 return NT_STATUS_NO_SUCH_GROUP
;
1259 *pp_sids
= talloc_steal(mem_ctx
, group_sids
);
1260 *pp_gids
= talloc_steal(mem_ctx
, gids
);
1261 *p_num_groups
= num_groups
;
1262 talloc_free(tmp_ctx
);
1263 return NT_STATUS_OK
;
1266 static NTSTATUS
pdb_samba_dsdb_enum_group_memberships(struct pdb_methods
*m
,
1267 TALLOC_CTX
*mem_ctx
,
1269 struct dom_sid
**pp_sids
,
1271 uint32_t *p_num_groups
)
1273 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1274 m
->private_data
, struct pdb_samba_dsdb_state
);
1275 struct ldb_message
*msg
= pdb_samba_dsdb_get_samu_private(
1277 const char *attrs
[] = { "tokenGroups", NULL
};
1278 struct ldb_message
*tokengroups_msg
;
1279 struct ldb_message_element
*tokengroups
;
1282 unsigned int count
= 0;
1284 struct dom_sid
*group_sids
;
1286 TALLOC_CTX
*tmp_ctx
;
1289 /* Fake up some things here */
1290 return fake_enum_group_memberships(state
,
1293 pp_gids
, p_num_groups
);
1296 tmp_ctx
= talloc_new(mem_ctx
);
1297 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1299 rc
= dsdb_search_one(state
->ldb
, tmp_ctx
, &tokengroups_msg
, msg
->dn
, LDB_SCOPE_BASE
, attrs
, 0, NULL
);
1301 if (rc
== LDB_ERR_NO_SUCH_OBJECT
) {
1302 talloc_free(tmp_ctx
);
1303 return NT_STATUS_NO_SUCH_USER
;
1304 } else if (rc
!= LDB_SUCCESS
) {
1305 DEBUG(10, ("dsdb_search_one failed %s\n",
1306 ldb_errstring(state
->ldb
)));
1307 talloc_free(tmp_ctx
);
1308 return NT_STATUS_LDAP(rc
);
1311 tokengroups
= ldb_msg_find_element(tokengroups_msg
, "tokenGroups");
1314 count
= tokengroups
->num_values
;
1317 group_sids
= talloc_array(tmp_ctx
, struct dom_sid
, count
);
1318 if (group_sids
== NULL
) {
1319 talloc_free(tmp_ctx
);
1320 return NT_STATUS_NO_MEMORY
;
1322 gids
= talloc_array(tmp_ctx
, gid_t
, count
);
1324 talloc_free(tmp_ctx
);
1325 return NT_STATUS_NO_MEMORY
;
1329 for (i
=0; i
<count
; i
++) {
1330 struct id_map
*id_maps
[2];
1331 struct id_map id_map
;
1332 struct ldb_val
*v
= &tokengroups
->values
[i
];
1333 enum ndr_err_code ndr_err
1334 = ndr_pull_struct_blob(v
, group_sids
, &group_sids
[num_groups
],
1335 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
1336 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1337 talloc_free(tmp_ctx
);
1338 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1341 ZERO_STRUCT(id_map
);
1342 id_map
.sid
= &group_sids
[num_groups
];
1343 id_maps
[0] = &id_map
;
1346 status
= idmap_sids_to_xids(state
->idmap_ctx
, tmp_ctx
, id_maps
);
1347 if (!NT_STATUS_IS_OK(status
)) {
1348 talloc_free(tmp_ctx
);
1351 if (id_map
.xid
.type
== ID_TYPE_GID
|| id_map
.xid
.type
== ID_TYPE_BOTH
) {
1352 gids
[num_groups
] = id_map
.xid
.id
;
1354 DEBUG(1, (__location__
1355 "Group %s, of which %s is a member, could not be converted to a GID\n",
1356 dom_sid_string(tmp_ctx
, &group_sids
[num_groups
]),
1357 ldb_dn_get_linearized(msg
->dn
)));
1358 talloc_free(tmp_ctx
);
1359 /* We must error out, otherwise a user might
1360 * avoid a DENY acl based on a group they
1362 return NT_STATUS_NO_SUCH_GROUP
;
1366 if (num_groups
== count
) {
1371 *pp_sids
= talloc_steal(mem_ctx
, group_sids
);
1372 *pp_gids
= talloc_steal(mem_ctx
, gids
);
1373 *p_num_groups
= num_groups
;
1374 talloc_free(tmp_ctx
);
1375 return NT_STATUS_OK
;
1378 static NTSTATUS
pdb_samba_dsdb_set_unix_primary_group(struct pdb_methods
*m
,
1379 TALLOC_CTX
*mem_ctx
,
1382 return NT_STATUS_NOT_IMPLEMENTED
;
1385 static NTSTATUS
pdb_samba_dsdb_mod_groupmem_by_sid(struct pdb_methods
*m
,
1386 TALLOC_CTX
*mem_ctx
,
1387 const struct dom_sid
*groupsid
,
1388 const struct dom_sid
*membersid
,
1391 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1392 m
->private_data
, struct pdb_samba_dsdb_state
);
1393 struct ldb_message
*msg
;
1395 struct ldb_message_element
*el
;
1396 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
1397 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1398 msg
= ldb_msg_new(tmp_ctx
);
1400 TALLOC_FREE(tmp_ctx
);
1401 return NT_STATUS_NO_MEMORY
;
1404 msg
->dn
= ldb_dn_new_fmt(msg
, state
->ldb
, "<SID=%s>", dom_sid_string(tmp_ctx
, groupsid
));
1405 if (!msg
->dn
|| !ldb_dn_validate(msg
->dn
)) {
1406 talloc_free(tmp_ctx
);
1407 return NT_STATUS_NO_MEMORY
;
1409 ret
= ldb_msg_add_fmt(msg
, "member", "<SID=%s>", dom_sid_string(tmp_ctx
, membersid
));
1410 if (ret
!= LDB_SUCCESS
) {
1411 talloc_free(tmp_ctx
);
1412 return NT_STATUS_NO_MEMORY
;
1414 el
= ldb_msg_find_element(msg
, "member");
1417 /* No need for transactions here, the ldb auto-transaction
1418 * code will handle things for the single operation */
1419 ret
= ldb_modify(state
->ldb
, msg
);
1420 talloc_free(tmp_ctx
);
1421 if (ret
!= LDB_SUCCESS
) {
1422 DEBUG(10, ("ldb_modify failed: %s\n",
1423 ldb_errstring(state
->ldb
)));
1424 if (ret
== LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
) {
1425 return NT_STATUS_MEMBER_IN_GROUP
;
1427 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
1428 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
1430 return NT_STATUS_LDAP(ret
);
1433 return NT_STATUS_OK
;
1436 static NTSTATUS
pdb_samba_dsdb_mod_groupmem(struct pdb_methods
*m
,
1437 TALLOC_CTX
*mem_ctx
,
1438 uint32 grouprid
, uint32 memberrid
,
1441 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1442 m
->private_data
, struct pdb_samba_dsdb_state
);
1443 const struct dom_sid
*dom_sid
, *groupsid
, *membersid
;
1445 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
1446 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1448 dom_sid
= samdb_domain_sid(state
->ldb
);
1450 groupsid
= dom_sid_add_rid(tmp_ctx
, dom_sid
, grouprid
);
1451 if (groupsid
== NULL
) {
1452 TALLOC_FREE(tmp_ctx
);
1453 return NT_STATUS_NO_MEMORY
;
1455 membersid
= dom_sid_add_rid(tmp_ctx
, dom_sid
, memberrid
);
1456 if (membersid
== NULL
) {
1457 TALLOC_FREE(tmp_ctx
);
1458 return NT_STATUS_NO_MEMORY
;
1460 status
= pdb_samba_dsdb_mod_groupmem_by_sid(m
, tmp_ctx
, groupsid
, membersid
, mod_op
);
1461 talloc_free(tmp_ctx
);
1465 static NTSTATUS
pdb_samba_dsdb_add_groupmem(struct pdb_methods
*m
,
1466 TALLOC_CTX
*mem_ctx
,
1467 uint32 group_rid
, uint32 member_rid
)
1469 return pdb_samba_dsdb_mod_groupmem(m
, mem_ctx
, group_rid
, member_rid
,
1473 static NTSTATUS
pdb_samba_dsdb_del_groupmem(struct pdb_methods
*m
,
1474 TALLOC_CTX
*mem_ctx
,
1475 uint32 group_rid
, uint32 member_rid
)
1477 return pdb_samba_dsdb_mod_groupmem(m
, mem_ctx
, group_rid
, member_rid
,
1478 LDB_FLAG_MOD_DELETE
);
1481 static NTSTATUS
pdb_samba_dsdb_create_alias(struct pdb_methods
*m
,
1482 const char *name
, uint32
*rid
)
1484 TALLOC_CTX
*frame
= talloc_stackframe();
1485 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1486 m
->private_data
, struct pdb_samba_dsdb_state
);
1487 struct dom_sid
*sid
;
1492 /* Internally this uses transactions to ensure all the steps
1493 * happen or fail as one */
1494 status
= dsdb_add_domain_alias(state
->ldb
, frame
, name
, &sid
, &dn
);
1495 if (!NT_STATUS_IS_OK(status
)) {
1499 sid_peek_rid(sid
, rid
);
1501 return NT_STATUS_OK
;
1504 static NTSTATUS
pdb_samba_dsdb_delete_alias(struct pdb_methods
*m
,
1505 const struct dom_sid
*sid
)
1507 const char *attrs
[] = { NULL
};
1508 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1509 m
->private_data
, struct pdb_samba_dsdb_state
);
1510 struct ldb_message
*msg
;
1513 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1514 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1516 dn
= ldb_dn_new_fmt(tmp_ctx
, state
->ldb
, "<SID=%s>", dom_sid_string(tmp_ctx
, sid
));
1517 if (!dn
|| !ldb_dn_validate(dn
)) {
1518 talloc_free(tmp_ctx
);
1519 return NT_STATUS_NO_MEMORY
;
1522 if (ldb_transaction_start(state
->ldb
) != LDB_SUCCESS
) {
1523 DEBUG(0, ("Failed to start transaction in dsdb_add_domain_alias(): %s\n", ldb_errstring(state
->ldb
)));
1524 return NT_STATUS_INTERNAL_ERROR
;
1527 rc
= dsdb_search_one(state
->ldb
, tmp_ctx
, &msg
, dn
, LDB_SCOPE_BASE
, attrs
, 0, "(objectclass=group)"
1528 "(|(grouptype=%d)(grouptype=%d)))",
1529 GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
,
1530 GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
);
1531 if (rc
== LDB_ERR_NO_SUCH_OBJECT
) {
1532 talloc_free(tmp_ctx
);
1533 ldb_transaction_cancel(state
->ldb
);
1534 return NT_STATUS_NO_SUCH_ALIAS
;
1536 rc
= ldb_delete(state
->ldb
, dn
);
1537 if (rc
== LDB_ERR_NO_SUCH_OBJECT
) {
1538 talloc_free(tmp_ctx
);
1539 ldb_transaction_cancel(state
->ldb
);
1540 return NT_STATUS_NO_SUCH_ALIAS
;
1541 } else if (rc
!= LDB_SUCCESS
) {
1542 DEBUG(10, ("ldb_delete failed %s\n",
1543 ldb_errstring(state
->ldb
)));
1544 ldb_transaction_cancel(state
->ldb
);
1545 return NT_STATUS_LDAP(rc
);
1548 if (ldb_transaction_commit(state
->ldb
) != LDB_SUCCESS
) {
1549 DEBUG(0, ("Failed to commit transaction in pdb_samba_dsdb_delete_alias(): %s\n",
1550 ldb_errstring(state
->ldb
)));
1551 return NT_STATUS_INTERNAL_ERROR
;
1554 return NT_STATUS_OK
;
1558 static NTSTATUS
pdb_samba_dsdb_set_aliasinfo(struct pdb_methods
*m
,
1559 const struct dom_sid
*sid
,
1560 struct acct_info
*info
)
1562 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1563 m
->private_data
, struct pdb_samba_dsdb_state
);
1564 struct tldap_context
*ld
;
1565 const char *attrs
[3] = { "objectSid", "description",
1567 struct ldb_message
**msg
;
1570 struct tldap_mod
*mods
;
1574 ld
= pdb_samba_dsdb_ld(state
);
1576 return NT_STATUS_LDAP(TLDAP_SERVER_DOWN
);
1579 sidstr
= sid_binstring(talloc_tos(), sid
);
1580 NT_STATUS_HAVE_NO_MEMORY(sidstr
);
1582 rc
= pdb_samba_dsdb_search_fmt(state
, state
->domaindn
, TLDAP_SCOPE_SUB
,
1583 attrs
, ARRAY_SIZE(attrs
), 0, talloc_tos(),
1584 &msg
, "(&(objectSid=%s)(objectclass=group)"
1585 "(|(grouptype=%d)(grouptype=%d)))",
1586 sidstr
, GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
,
1587 GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
);
1589 if (rc
!= LDB_SUCCESS
) {
1590 DEBUG(10, ("ldap_search failed %s\n",
1591 ldb_errstring(state
->ldb
)));
1592 return NT_STATUS_LDAP(rc
);
1594 switch talloc_array_length(msg
) {
1596 return NT_STATUS_NO_SUCH_ALIAS
;
1600 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1603 if (!tldap_entry_dn(msg
[0], &dn
)) {
1605 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1612 ok
&= tldap_make_mod_fmt(
1613 msg
[0], msg
, &num_mods
, &mods
, "description",
1614 "%s", info
->acct_desc
);
1615 ok
&= tldap_make_mod_fmt(
1616 msg
[0], msg
, &num_mods
, &mods
, "samAccountName",
1617 "%s", info
->acct_name
);
1620 return NT_STATUS_NO_MEMORY
;
1622 if (num_mods
== 0) {
1625 return NT_STATUS_OK
;
1628 rc
= tldap_modify(ld
, dn
, num_mods
, mods
, NULL
, 0, NULL
, 0);
1630 if (rc
!= LDB_SUCCESS
) {
1631 DEBUG(10, ("ldap_modify failed: %s\n",
1632 ldb_errstring(state
->ldb
)));
1633 return NT_STATUS_LDAP(rc
);
1635 return NT_STATUS_OK
;
1638 static NTSTATUS
pdb_samba_dsdb_add_aliasmem(struct pdb_methods
*m
,
1639 const struct dom_sid
*alias
,
1640 const struct dom_sid
*member
)
1643 TALLOC_CTX
*frame
= talloc_stackframe();
1644 status
= pdb_samba_dsdb_mod_groupmem_by_sid(m
, frame
, alias
, member
, LDB_FLAG_MOD_ADD
);
1649 static NTSTATUS
pdb_samba_dsdb_del_aliasmem(struct pdb_methods
*m
,
1650 const struct dom_sid
*alias
,
1651 const struct dom_sid
*member
)
1654 TALLOC_CTX
*frame
= talloc_stackframe();
1655 status
= pdb_samba_dsdb_mod_groupmem_by_sid(m
, frame
, alias
, member
, LDB_FLAG_MOD_DELETE
);
1660 static NTSTATUS
pdb_samba_dsdb_enum_aliasmem(struct pdb_methods
*m
,
1661 const struct dom_sid
*alias
,
1662 TALLOC_CTX
*mem_ctx
,
1663 struct dom_sid
**pmembers
,
1664 size_t *pnum_members
)
1666 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1667 m
->private_data
, struct pdb_samba_dsdb_state
);
1669 unsigned int num_members
;
1671 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
1672 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1674 dn
= ldb_dn_new_fmt(tmp_ctx
, state
->ldb
, "<SID=%s>", dom_sid_string(tmp_ctx
, alias
));
1675 if (!dn
|| !ldb_dn_validate(dn
)) {
1676 return NT_STATUS_NO_MEMORY
;
1679 status
= dsdb_enum_group_mem(state
->ldb
, mem_ctx
, dn
, pmembers
, &num_members
);
1680 *pnum_members
= num_members
;
1681 if (NT_STATUS_IS_OK(status
)) {
1682 talloc_steal(mem_ctx
, pmembers
);
1684 talloc_free(tmp_ctx
);
1688 static NTSTATUS
pdb_samba_dsdb_enum_alias_memberships(struct pdb_methods
*m
,
1689 TALLOC_CTX
*mem_ctx
,
1690 const struct dom_sid
*domain_sid
,
1691 const struct dom_sid
*members
,
1693 uint32_t **palias_rids
,
1694 size_t *pnum_alias_rids
)
1696 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1697 m
->private_data
, struct pdb_samba_dsdb_state
);
1698 uint32_t *alias_rids
= NULL
;
1699 size_t num_alias_rids
= 0;
1701 struct dom_sid
*groupSIDs
= NULL
;
1702 unsigned int num_groupSIDs
= 0;
1705 const char *sid_string
;
1709 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
1710 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1712 * TODO: Get the filter right so that we only get the aliases from
1713 * either the SAM or BUILTIN
1716 filter
= talloc_asprintf(tmp_ctx
, "(&(objectClass=group)(groupType:1.2.840.113556.1.4.803:=%u))",
1717 GROUP_TYPE_BUILTIN_LOCAL_GROUP
);
1718 if (filter
== NULL
) {
1719 return NT_STATUS_NO_MEMORY
;
1722 for (i
= 0; i
< num_members
; i
++) {
1723 sid_string
= dom_sid_string(tmp_ctx
, &members
[i
]);
1724 if (sid_string
== NULL
) {
1725 TALLOC_FREE(tmp_ctx
);
1726 return NT_STATUS_NO_MEMORY
;
1729 sid_dn
= talloc_asprintf(tmp_ctx
, "<SID=%s>", sid_string
);
1730 if (sid_dn
== NULL
) {
1731 TALLOC_FREE(tmp_ctx
);
1732 return NT_STATUS_NO_MEMORY
;
1735 sid_blob
= data_blob_string_const(sid_dn
);
1737 status
= dsdb_expand_nested_groups(state
->ldb
, &sid_blob
, true, filter
,
1738 tmp_ctx
, &groupSIDs
, &num_groupSIDs
);
1739 if (!NT_STATUS_IS_OK(status
)) {
1740 talloc_free(tmp_ctx
);
1745 alias_rids
= talloc_array(mem_ctx
, uint32_t, num_groupSIDs
);
1746 if (alias_rids
== NULL
) {
1747 talloc_free(tmp_ctx
);
1748 return NT_STATUS_NO_MEMORY
;
1751 for (i
=0; i
<num_groupSIDs
; i
++) {
1752 if (sid_peek_check_rid(domain_sid
, &groupSIDs
[i
],
1753 &alias_rids
[num_alias_rids
])) {
1758 *palias_rids
= alias_rids
;
1759 *pnum_alias_rids
= num_alias_rids
;
1760 return NT_STATUS_OK
;
1763 static NTSTATUS
pdb_samba_dsdb_lookup_rids(struct pdb_methods
*m
,
1764 const struct dom_sid
*domain_sid
,
1768 enum lsa_SidType
*lsa_attrs
)
1770 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1771 m
->private_data
, struct pdb_samba_dsdb_state
);
1774 TALLOC_CTX
*tmp_ctx
;
1776 if (num_rids
== 0) {
1777 return NT_STATUS_NONE_MAPPED
;
1780 tmp_ctx
= talloc_stackframe();
1781 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
1783 status
= dsdb_lookup_rids(state
->ldb
, tmp_ctx
, domain_sid
, num_rids
, rids
, names
, lsa_attrs
);
1784 talloc_free(tmp_ctx
);
1788 static NTSTATUS
pdb_samba_dsdb_lookup_names(struct pdb_methods
*m
,
1789 const struct dom_sid
*domain_sid
,
1791 const char **pp_names
,
1793 enum lsa_SidType
*attrs
)
1795 return NT_STATUS_NOT_IMPLEMENTED
;
1798 static NTSTATUS
pdb_samba_dsdb_get_account_policy(struct pdb_methods
*m
,
1799 enum pdb_policy_type type
,
1802 return account_policy_get(type
, value
)
1803 ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1806 static NTSTATUS
pdb_samba_dsdb_set_account_policy(struct pdb_methods
*m
,
1807 enum pdb_policy_type type
,
1810 return account_policy_set(type
, value
)
1811 ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1814 static NTSTATUS
pdb_samba_dsdb_get_seq_num(struct pdb_methods
*m
,
1815 time_t *seq_num_out
)
1817 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1818 m
->private_data
, struct pdb_samba_dsdb_state
);
1820 int ret
= ldb_sequence_number(state
->ldb
, LDB_SEQ_HIGHEST_SEQ
, &seq_num
);
1821 if (ret
== LDB_SUCCESS
) {
1822 *seq_num_out
= seq_num
;
1823 return NT_STATUS_OK
;
1825 return NT_STATUS_UNSUCCESSFUL
;
1829 struct pdb_samba_dsdb_search_state
{
1830 uint32_t acct_flags
;
1831 struct samr_displayentry
*entries
;
1832 uint32_t num_entries
;
1837 static bool pdb_samba_dsdb_next_entry(struct pdb_search
*search
,
1838 struct samr_displayentry
*entry
)
1840 struct pdb_samba_dsdb_search_state
*state
= talloc_get_type_abort(
1841 search
->private_data
, struct pdb_samba_dsdb_search_state
);
1843 if (state
->current
== state
->num_entries
) {
1847 entry
->idx
= state
->entries
[state
->current
].idx
;
1848 entry
->rid
= state
->entries
[state
->current
].rid
;
1849 entry
->acct_flags
= state
->entries
[state
->current
].acct_flags
;
1851 entry
->account_name
= talloc_strdup(
1852 search
, state
->entries
[state
->current
].account_name
);
1853 entry
->fullname
= talloc_strdup(
1854 search
, state
->entries
[state
->current
].fullname
);
1855 entry
->description
= talloc_strdup(
1856 search
, state
->entries
[state
->current
].description
);
1858 state
->current
+= 1;
1862 static void pdb_samba_dsdb_search_end(struct pdb_search
*search
)
1864 struct pdb_samba_dsdb_search_state
*state
= talloc_get_type_abort(
1865 search
->private_data
, struct pdb_samba_dsdb_search_state
);
1869 static bool pdb_samba_dsdb_search_filter(struct pdb_methods
*m
,
1870 struct pdb_search
*search
,
1871 struct pdb_samba_dsdb_search_state
**pstate
,
1872 const char *exp_fmt
, ...) _PRINTF_ATTRIBUTE(4, 5)
1874 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
1875 m
->private_data
, struct pdb_samba_dsdb_state
);
1876 struct pdb_samba_dsdb_search_state
*sstate
;
1877 const char * attrs
[] = { "objectSid", "sAMAccountName", "displayName",
1878 "userAccountControl", "description", NULL
};
1879 struct ldb_result
*res
;
1880 int i
, rc
, num_users
;
1883 char *expression
= NULL
;
1885 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1890 va_start(ap
, exp_fmt
);
1891 expression
= talloc_vasprintf(tmp_ctx
, exp_fmt
, ap
);
1895 talloc_free(tmp_ctx
);
1896 return LDB_ERR_OPERATIONS_ERROR
;
1899 sstate
= talloc_zero(tmp_ctx
, struct pdb_samba_dsdb_search_state
);
1900 if (sstate
== NULL
) {
1901 talloc_free(tmp_ctx
);
1905 rc
= dsdb_search(state
->ldb
, tmp_ctx
, &res
, ldb_get_default_basedn(state
->ldb
), LDB_SCOPE_SUBTREE
, attrs
, 0, "%s", expression
);
1906 if (rc
!= LDB_SUCCESS
) {
1907 talloc_free(tmp_ctx
);
1908 DEBUG(10, ("dsdb_search failed: %s\n",
1909 ldb_errstring(state
->ldb
)));
1913 num_users
= res
->count
;
1915 sstate
->entries
= talloc_array(sstate
, struct samr_displayentry
,
1917 if (sstate
->entries
== NULL
) {
1918 talloc_free(tmp_ctx
);
1919 DEBUG(10, ("talloc failed\n"));
1923 sstate
->num_entries
= 0;
1925 for (i
=0; i
<num_users
; i
++) {
1926 struct samr_displayentry
*e
;
1927 struct dom_sid
*sid
;
1929 e
= &sstate
->entries
[sstate
->num_entries
];
1931 e
->idx
= sstate
->num_entries
;
1932 sid
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[i
], "objectSid");
1934 talloc_free(tmp_ctx
);
1935 DEBUG(10, ("Could not pull SID\n"));
1938 sid_peek_rid(sid
, &e
->rid
);
1940 e
->acct_flags
= samdb_result_acct_flags(res
->msgs
[i
], "userAccountControl");
1941 e
->account_name
= ldb_msg_find_attr_as_string(
1942 res
->msgs
[i
], "samAccountName", NULL
);
1943 if (e
->account_name
== NULL
) {
1944 talloc_free(tmp_ctx
);
1947 e
->fullname
= ldb_msg_find_attr_as_string(
1948 res
->msgs
[i
], "displayName", "");
1949 e
->description
= ldb_msg_find_attr_as_string(
1950 res
->msgs
[i
], "description", "");
1952 sstate
->num_entries
+= 1;
1953 if (sstate
->num_entries
>= num_users
) {
1957 talloc_steal(sstate
->entries
, res
->msgs
);
1958 search
->private_data
= talloc_steal(search
, sstate
);
1959 search
->next_entry
= pdb_samba_dsdb_next_entry
;
1960 search
->search_end
= pdb_samba_dsdb_search_end
;
1962 talloc_free(tmp_ctx
);
1966 static bool pdb_samba_dsdb_search_users(struct pdb_methods
*m
,
1967 struct pdb_search
*search
,
1970 struct pdb_samba_dsdb_search_state
*sstate
;
1973 ret
= pdb_samba_dsdb_search_filter(m
, search
, &sstate
, "(objectclass=user)");
1977 sstate
->acct_flags
= acct_flags
;
1981 static bool pdb_samba_dsdb_search_groups(struct pdb_methods
*m
,
1982 struct pdb_search
*search
)
1984 struct pdb_samba_dsdb_search_state
*sstate
;
1987 ret
= pdb_samba_dsdb_search_filter(m
, search
, &sstate
,
1988 "(&(grouptype=%d)(objectclass=group))",
1989 GTYPE_SECURITY_GLOBAL_GROUP
);
1993 sstate
->acct_flags
= 0;
1997 static bool pdb_samba_dsdb_search_aliases(struct pdb_methods
*m
,
1998 struct pdb_search
*search
,
1999 const struct dom_sid
*sid
)
2001 struct pdb_samba_dsdb_search_state
*sstate
;
2004 ret
= pdb_samba_dsdb_search_filter(m
, search
, &sstate
,
2005 "(&(grouptype=%d)(objectclass=group))",
2006 sid_check_is_builtin(sid
)
2007 ? GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
2008 : GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
);
2012 sstate
->acct_flags
= 0;
2016 static bool pdb_samba_dsdb_uid_to_sid(struct pdb_methods
*m
, uid_t uid
,
2017 struct dom_sid
*sid
)
2019 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
2020 m
->private_data
, struct pdb_samba_dsdb_state
);
2022 struct id_map id_map
;
2023 struct id_map
*id_maps
[2];
2024 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
2029 id_map
.xid
.id
= uid
;
2030 id_map
.xid
.type
= ID_TYPE_UID
;
2031 id_maps
[0] = &id_map
;
2034 status
= idmap_xids_to_sids(state
->idmap_ctx
, tmp_ctx
, id_maps
);
2035 if (!NT_STATUS_IS_OK(status
)) {
2036 talloc_free(tmp_ctx
);
2040 talloc_free(tmp_ctx
);
2044 static bool pdb_samba_dsdb_gid_to_sid(struct pdb_methods
*m
, gid_t gid
,
2045 struct dom_sid
*sid
)
2047 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
2048 m
->private_data
, struct pdb_samba_dsdb_state
);
2050 struct id_map id_map
;
2051 struct id_map
*id_maps
[2];
2052 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
2057 id_map
.xid
.id
= gid
;
2058 id_map
.xid
.type
= ID_TYPE_GID
;
2059 id_maps
[0] = &id_map
;
2062 status
= idmap_xids_to_sids(state
->idmap_ctx
, tmp_ctx
, id_maps
);
2063 if (!NT_STATUS_IS_OK(status
)) {
2067 talloc_free(tmp_ctx
);
2071 static bool pdb_samba_dsdb_sid_to_id(struct pdb_methods
*m
, const struct dom_sid
*sid
,
2074 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
2075 m
->private_data
, struct pdb_samba_dsdb_state
);
2076 struct id_map id_map
;
2077 struct id_map
*id_maps
[2];
2079 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
2084 ZERO_STRUCT(id_map
);
2085 id_map
.sid
= discard_const_p(struct dom_sid
, sid
);
2086 id_maps
[0] = &id_map
;
2089 status
= idmap_sids_to_xids(state
->idmap_ctx
, tmp_ctx
, id_maps
);
2090 talloc_free(tmp_ctx
);
2091 if (!NT_STATUS_IS_OK(status
)) {
2094 if (id_map
.xid
.type
!= ID_TYPE_NOT_SPECIFIED
) {
2101 static uint32_t pdb_samba_dsdb_capabilities(struct pdb_methods
*m
)
2103 return PDB_CAP_STORE_RIDS
| PDB_CAP_ADS
;
2106 static bool pdb_samba_dsdb_new_rid(struct pdb_methods
*m
, uint32
*rid
)
2111 static bool pdb_samba_dsdb_get_trusteddom_pw(struct pdb_methods
*m
,
2112 const char *domain
, char** pwd
,
2113 struct dom_sid
*sid
,
2114 time_t *pass_last_set_time
)
2119 static bool pdb_samba_dsdb_set_trusteddom_pw(struct pdb_methods
*m
,
2120 const char* domain
, const char* pwd
,
2121 const struct dom_sid
*sid
)
2126 static bool pdb_samba_dsdb_del_trusteddom_pw(struct pdb_methods
*m
,
2132 static NTSTATUS
pdb_samba_dsdb_enum_trusteddoms(struct pdb_methods
*m
,
2133 TALLOC_CTX
*mem_ctx
,
2134 uint32
*num_domains
,
2135 struct trustdom_info
***domains
)
2139 return NT_STATUS_OK
;
2142 static bool pdb_samba_dsdb_is_responsible_for_wellknown(struct pdb_methods
*m
)
2147 static void pdb_samba_dsdb_init_methods(struct pdb_methods
*m
)
2149 m
->name
= "samba_dsdb";
2150 m
->get_domain_info
= pdb_samba_dsdb_get_domain_info
;
2151 m
->getsampwnam
= pdb_samba_dsdb_getsampwnam
;
2152 m
->getsampwsid
= pdb_samba_dsdb_getsampwsid
;
2153 m
->create_user
= pdb_samba_dsdb_create_user
;
2154 m
->delete_user
= pdb_samba_dsdb_delete_user
;
2155 m
->add_sam_account
= pdb_samba_dsdb_add_sam_account
;
2156 m
->update_sam_account
= pdb_samba_dsdb_update_sam_account
;
2157 m
->delete_sam_account
= pdb_samba_dsdb_delete_sam_account
;
2158 m
->rename_sam_account
= pdb_samba_dsdb_rename_sam_account
;
2159 m
->update_login_attempts
= pdb_samba_dsdb_update_login_attempts
;
2160 m
->getgrsid
= pdb_samba_dsdb_getgrsid
;
2161 m
->getgrgid
= pdb_samba_dsdb_getgrgid
;
2162 m
->getgrnam
= pdb_samba_dsdb_getgrnam
;
2163 m
->create_dom_group
= pdb_samba_dsdb_create_dom_group
;
2164 m
->delete_dom_group
= pdb_samba_dsdb_delete_dom_group
;
2165 m
->add_group_mapping_entry
= pdb_samba_dsdb_add_group_mapping_entry
;
2166 m
->update_group_mapping_entry
= pdb_samba_dsdb_update_group_mapping_entry
;
2167 m
->delete_group_mapping_entry
= pdb_samba_dsdb_delete_group_mapping_entry
;
2168 m
->enum_group_mapping
= pdb_samba_dsdb_enum_group_mapping
;
2169 m
->enum_group_members
= pdb_samba_dsdb_enum_group_members
;
2170 m
->enum_group_memberships
= pdb_samba_dsdb_enum_group_memberships
;
2171 m
->set_unix_primary_group
= pdb_samba_dsdb_set_unix_primary_group
;
2172 m
->add_groupmem
= pdb_samba_dsdb_add_groupmem
;
2173 m
->del_groupmem
= pdb_samba_dsdb_del_groupmem
;
2174 m
->create_alias
= pdb_samba_dsdb_create_alias
;
2175 m
->delete_alias
= pdb_samba_dsdb_delete_alias
;
2176 m
->get_aliasinfo
= pdb_default_get_aliasinfo
;
2177 m
->add_aliasmem
= pdb_samba_dsdb_add_aliasmem
;
2178 m
->del_aliasmem
= pdb_samba_dsdb_del_aliasmem
;
2179 m
->enum_aliasmem
= pdb_samba_dsdb_enum_aliasmem
;
2180 m
->enum_alias_memberships
= pdb_samba_dsdb_enum_alias_memberships
;
2181 m
->lookup_rids
= pdb_samba_dsdb_lookup_rids
;
2182 m
->lookup_names
= pdb_samba_dsdb_lookup_names
;
2183 m
->get_account_policy
= pdb_samba_dsdb_get_account_policy
;
2184 m
->set_account_policy
= pdb_samba_dsdb_set_account_policy
;
2185 m
->get_seq_num
= pdb_samba_dsdb_get_seq_num
;
2186 m
->search_users
= pdb_samba_dsdb_search_users
;
2187 m
->search_groups
= pdb_samba_dsdb_search_groups
;
2188 m
->search_aliases
= pdb_samba_dsdb_search_aliases
;
2189 m
->uid_to_sid
= pdb_samba_dsdb_uid_to_sid
;
2190 m
->gid_to_sid
= pdb_samba_dsdb_gid_to_sid
;
2191 m
->sid_to_id
= pdb_samba_dsdb_sid_to_id
;
2192 m
->capabilities
= pdb_samba_dsdb_capabilities
;
2193 m
->new_rid
= pdb_samba_dsdb_new_rid
;
2194 m
->get_trusteddom_pw
= pdb_samba_dsdb_get_trusteddom_pw
;
2195 m
->set_trusteddom_pw
= pdb_samba_dsdb_set_trusteddom_pw
;
2196 m
->del_trusteddom_pw
= pdb_samba_dsdb_del_trusteddom_pw
;
2197 m
->enum_trusteddoms
= pdb_samba_dsdb_enum_trusteddoms
;
2198 m
->is_responsible_for_wellknown
=
2199 pdb_samba_dsdb_is_responsible_for_wellknown
;
2202 static void free_private_data(void **vp
)
2204 struct pdb_samba_dsdb_state
*state
= talloc_get_type_abort(
2205 *vp
, struct pdb_samba_dsdb_state
);
2206 talloc_unlink(state
, state
->ldb
);
2210 static NTSTATUS
pdb_samba_dsdb_init_secrets(struct pdb_methods
*m
)
2212 struct pdb_domain_info
*dom_info
;
2215 dom_info
= pdb_samba_dsdb_get_domain_info(m
, m
);
2217 return NT_STATUS_UNSUCCESSFUL
;
2220 secrets_clear_domain_protection(dom_info
->name
);
2221 ret
= secrets_store_domain_sid(dom_info
->name
,
2226 ret
= secrets_store_domain_guid(dom_info
->name
,
2231 ret
= secrets_mark_domain_protected(dom_info
->name
);
2237 TALLOC_FREE(dom_info
);
2239 return NT_STATUS_UNSUCCESSFUL
;
2241 return NT_STATUS_OK
;
2244 static NTSTATUS
pdb_init_samba_dsdb(struct pdb_methods
**pdb_method
,
2245 const char *location
)
2247 struct pdb_methods
*m
;
2248 struct pdb_samba_dsdb_state
*state
;
2251 if ( !NT_STATUS_IS_OK(status
= make_pdb_method( &m
)) ) {
2255 state
= talloc_zero(m
, struct pdb_samba_dsdb_state
);
2256 if (state
== NULL
) {
2259 m
->private_data
= state
;
2260 m
->free_private_data
= free_private_data
;
2261 pdb_samba_dsdb_init_methods(m
);
2263 state
->ev
= s4_event_context_init(state
);
2265 DEBUG(0, ("s4_event_context_init failed\n"));
2269 state
->lp_ctx
= loadparm_init_s3(state
, loadparm_s3_helpers());
2270 if (state
->lp_ctx
== NULL
) {
2271 DEBUG(0, ("loadparm_init_s3 failed\n"));
2276 state
->ldb
= samdb_connect_url(state
,
2279 system_session(state
->lp_ctx
),
2282 state
->ldb
= samdb_connect(state
,
2285 system_session(state
->lp_ctx
), 0);
2289 DEBUG(0, ("samdb_connect failed\n"));
2290 status
= NT_STATUS_INTERNAL_ERROR
;
2294 state
->idmap_ctx
= idmap_init(state
, state
->ev
,
2296 if (!state
->idmap_ctx
) {
2297 DEBUG(0, ("idmap failed\n"));
2298 status
= NT_STATUS_INTERNAL_ERROR
;
2302 status
= pdb_samba_dsdb_init_secrets(m
);
2303 if (!NT_STATUS_IS_OK(status
)) {
2304 DEBUG(10, ("pdb_samba_dsdb_init_secrets failed!\n"));
2309 return NT_STATUS_OK
;
2311 status
= NT_STATUS_NO_MEMORY
;
2317 NTSTATUS
pdb_samba_dsdb_init(void);
2318 NTSTATUS
pdb_samba_dsdb_init(void)
2320 NTSTATUS status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "samba_dsdb",
2321 pdb_init_samba_dsdb
);
2322 if (!NT_STATUS_IS_OK(status
)) {
2325 return smb_register_passdb(PASSDB_INTERFACE_VERSION
, "samba4",
2326 pdb_init_samba_dsdb
);