2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Jelmer Vernooij 2002
6 Copyright (C) Simo Sorce 2003
7 Copyright (C) Volker Lendecke 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/passwd.h"
27 #include "../librpc/gen_ndr/samr.h"
29 #include "nsswitch/winbind_client.h"
30 #include "../libcli/security/security.h"
31 #include "../lib/util/util_pw.h"
34 #define DBGC_CLASS DBGC_PASSDB
38 static struct pdb_init_function_entry
*backends
= NULL
;
40 static void lazy_initialize_passdb(void)
42 static bool initialized
= False
;
50 static bool lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32 rid
,
52 enum lsa_SidType
*psid_name_use
,
53 union unid_t
*unix_id
);
55 NTSTATUS
smb_register_passdb(int version
, const char *name
, pdb_init_function init
)
57 struct pdb_init_function_entry
*entry
= backends
;
59 if(version
!= PASSDB_INTERFACE_VERSION
) {
60 DEBUG(0,("Can't register passdb backend!\n"
61 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
62 "while this version of samba uses version %d\n",
63 version
,PASSDB_INTERFACE_VERSION
));
64 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
68 return NT_STATUS_INVALID_PARAMETER
;
71 DEBUG(5,("Attempting to register passdb backend %s\n", name
));
73 /* Check for duplicates */
74 if (pdb_find_backend_entry(name
)) {
75 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name
));
76 return NT_STATUS_OBJECT_NAME_COLLISION
;
79 entry
= SMB_XMALLOC_P(struct pdb_init_function_entry
);
80 entry
->name
= smb_xstrdup(name
);
83 DLIST_ADD(backends
, entry
);
84 DEBUG(5,("Successfully added passdb backend '%s'\n", name
));
88 struct pdb_init_function_entry
*pdb_find_backend_entry(const char *name
)
90 struct pdb_init_function_entry
*entry
= backends
;
93 if (strcmp(entry
->name
, name
)==0) return entry
;
101 * The event context for the passdb backend. I know this is a bad hack and yet
102 * another static variable, but our pdb API is a global thing per
103 * definition. The first use for this is the LDAP idle function, more might be
106 * I don't feel too bad about this static variable, it replaces the
107 * smb_idle_event_list that used to exist in lib/module.c. -- VL
110 static struct event_context
*pdb_event_ctx
;
112 struct event_context
*pdb_get_event_context(void)
114 return pdb_event_ctx
;
117 /******************************************************************
118 Make a pdb_methods from scratch
119 *******************************************************************/
121 NTSTATUS
make_pdb_method_name(struct pdb_methods
**methods
, const char *selected
)
123 char *module_name
= smb_xstrdup(selected
);
124 char *module_location
= NULL
, *p
;
125 struct pdb_init_function_entry
*entry
;
126 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
128 lazy_initialize_passdb();
130 p
= strchr(module_name
, ':');
134 module_location
= p
+1;
135 trim_char(module_location
, ' ', ' ');
138 trim_char(module_name
, ' ', ' ');
141 DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected
, module_name
));
143 entry
= pdb_find_backend_entry(module_name
);
145 /* Try to find a module that contains this module */
147 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
148 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name
)) && !(entry
= pdb_find_backend_entry(module_name
))) {
149 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name
));
150 SAFE_FREE(module_name
);
151 return NT_STATUS_UNSUCCESSFUL
;
155 /* No such backend found */
157 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name
));
158 SAFE_FREE(module_name
);
159 return NT_STATUS_INVALID_PARAMETER
;
162 DEBUG(5,("Found pdb backend %s\n", module_name
));
164 if ( !NT_STATUS_IS_OK( nt_status
= entry
->init(methods
, module_location
) ) ) {
165 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
166 selected
, nt_errstr(nt_status
)));
167 SAFE_FREE(module_name
);
171 SAFE_FREE(module_name
);
173 DEBUG(5,("pdb backend %s has a valid init\n", selected
));
178 /******************************************************************
179 Return an already initialized pdb_methods structure
180 *******************************************************************/
182 static struct pdb_methods
*pdb_get_methods_reload( bool reload
)
184 static struct pdb_methods
*pdb
= NULL
;
186 if ( pdb
&& reload
) {
187 if (pdb
->free_private_data
!= NULL
) {
188 pdb
->free_private_data( &(pdb
->private_data
) );
190 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
192 if (asprintf(&msg
, "pdb_get_methods_reload: "
193 "failed to get pdb methods for backend %s\n",
194 lp_passdb_backend()) > 0) {
197 smb_panic("pdb_get_methods_reload");
203 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
205 if (asprintf(&msg
, "pdb_get_methods_reload: "
206 "failed to get pdb methods for backend %s\n",
207 lp_passdb_backend()) > 0) {
210 smb_panic("pdb_get_methods_reload");
218 static struct pdb_methods
*pdb_get_methods(void)
220 return pdb_get_methods_reload(False
);
223 struct pdb_domain_info
*pdb_get_domain_info(TALLOC_CTX
*mem_ctx
)
225 struct pdb_methods
*pdb
= pdb_get_methods();
226 return pdb
->get_domain_info(pdb
, mem_ctx
);
230 * @brief Check if the user account has been locked out and try to unlock it.
232 * If the user has been automatically locked out and a lockout duration is set,
233 * then check if we can unlock the account and reset the bad password values.
235 * @param[in] sampass The sam user to check.
237 * @return True if the function was successfull, false on an error.
239 static bool pdb_try_account_unlock(struct samu
*sampass
)
241 uint32_t acb_info
= pdb_get_acct_ctrl(sampass
);
243 if ((acb_info
& ACB_NORMAL
) && (acb_info
& ACB_AUTOLOCK
)) {
244 uint32_t lockout_duration
;
245 time_t bad_password_time
;
246 time_t now
= time(NULL
);
249 ok
= pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION
,
252 DEBUG(0, ("pdb_try_account_unlock: "
253 "pdb_get_account_policy failed.\n"));
257 if (lockout_duration
== (uint32_t) -1 ||
258 lockout_duration
== 0) {
259 DEBUG(9, ("pdb_try_account_unlock: No reset duration, "
260 "can't reset autolock\n"));
263 lockout_duration
*= 60;
265 bad_password_time
= pdb_get_bad_password_time(sampass
);
266 if (bad_password_time
== (time_t) 0) {
267 DEBUG(2, ("pdb_try_account_unlock: Account %s "
268 "administratively locked out "
269 "with no bad password "
270 "time. Leaving locked out.\n",
271 pdb_get_username(sampass
)));
275 if ((bad_password_time
+
276 convert_uint32_t_to_time_t(lockout_duration
)) < now
) {
279 pdb_set_acct_ctrl(sampass
, acb_info
& ~ACB_AUTOLOCK
,
281 pdb_set_bad_password_count(sampass
, 0, PDB_CHANGED
);
282 pdb_set_bad_password_time(sampass
, 0, PDB_CHANGED
);
285 status
= pdb_update_sam_account(sampass
);
287 if (!NT_STATUS_IS_OK(status
)) {
288 DEBUG(0, ("_samr_OpenUser: Couldn't "
289 "update account %s - %s\n",
290 pdb_get_username(sampass
),
301 * @brief Get a sam user structure by the given username.
303 * This functions also checks if the account has been automatically locked out
304 * and unlocks it if a lockout duration time has been defined and the time has
307 * @param[in] sam_acct The sam user structure to fill.
309 * @param[in] username The username to look for.
311 * @return True on success, false on error.
313 bool pdb_getsampwnam(struct samu
*sam_acct
, const char *username
)
315 struct pdb_methods
*pdb
= pdb_get_methods();
316 struct samu
*for_cache
;
317 const struct dom_sid
*user_sid
;
321 status
= pdb
->getsampwnam(pdb
, sam_acct
, username
);
322 if (!NT_STATUS_IS_OK(status
)) {
326 ok
= pdb_try_account_unlock(sam_acct
);
328 DEBUG(1, ("pdb_getsampwnam: Failed to unlock account %s\n",
332 for_cache
= samu_new(NULL
);
333 if (for_cache
== NULL
) {
337 if (!pdb_copy_sam_account(for_cache
, sam_acct
)) {
338 TALLOC_FREE(for_cache
);
342 user_sid
= pdb_get_user_sid(for_cache
);
344 memcache_add_talloc(NULL
, PDB_GETPWSID_CACHE
,
345 data_blob_const(user_sid
, sizeof(*user_sid
)),
351 /**********************************************************************
352 **********************************************************************/
354 static bool guest_user_info( struct samu
*user
)
358 const char *guestname
= lp_guestaccount();
360 pwd
= Get_Pwnam_alloc(talloc_tos(), guestname
);
362 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
367 result
= samu_set_unix(user
, pwd
);
371 return NT_STATUS_IS_OK( result
);
375 * @brief Get a sam user structure by the given username.
377 * This functions also checks if the account has been automatically locked out
378 * and unlocks it if a lockout duration time has been defined and the time has
382 * @param[in] sam_acct The sam user structure to fill.
384 * @param[in] sid The user SDI to look up.
386 * @return True on success, false on error.
388 bool pdb_getsampwsid(struct samu
*sam_acct
, const struct dom_sid
*sid
)
390 struct pdb_methods
*pdb
= pdb_get_methods();
395 /* hard code the Guest RID of 501 */
397 if ( !sid_peek_check_rid( get_global_sam_sid(), sid
, &rid
) )
400 if ( rid
== DOMAIN_RID_GUEST
) {
401 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
402 return guest_user_info( sam_acct
);
405 /* check the cache first */
407 cache_data
= memcache_lookup_talloc(
408 NULL
, PDB_GETPWSID_CACHE
, data_blob_const(sid
, sizeof(*sid
)));
410 if (cache_data
!= NULL
) {
411 struct samu
*cache_copy
= talloc_get_type_abort(
412 cache_data
, struct samu
);
414 ok
= pdb_copy_sam_account(sam_acct
, cache_copy
);
416 ok
= NT_STATUS_IS_OK(pdb
->getsampwsid(pdb
, sam_acct
, sid
));
423 ok
= pdb_try_account_unlock(sam_acct
);
425 DEBUG(1, ("pdb_getsampwsid: Failed to unlock account %s\n",
426 sam_acct
->username
));
432 static NTSTATUS
pdb_default_create_user(struct pdb_methods
*methods
,
433 TALLOC_CTX
*tmp_ctx
, const char *name
,
434 uint32_t acb_info
, uint32_t *rid
)
436 struct samu
*sam_pass
;
440 if ((sam_pass
= samu_new(tmp_ctx
)) == NULL
) {
441 return NT_STATUS_NO_MEMORY
;
444 if ( !(pwd
= Get_Pwnam_alloc(tmp_ctx
, name
)) ) {
445 char *add_script
= NULL
;
449 if ((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] != '$') {
450 add_script
= talloc_strdup(tmp_ctx
,
451 lp_adduser_script());
453 add_script
= talloc_strdup(tmp_ctx
,
454 lp_addmachine_script());
457 if (!add_script
|| add_script
[0] == '\0') {
458 DEBUG(3, ("Could not find user %s and no add script "
460 return NT_STATUS_NO_SUCH_USER
;
463 /* lowercase the username before creating the Unix account for
464 compatibility with previous Samba releases */
465 fstrcpy( name2
, name
);
467 add_script
= talloc_all_string_sub(tmp_ctx
,
472 return NT_STATUS_NO_MEMORY
;
474 add_ret
= smbrun(add_script
,NULL
);
475 DEBUG(add_ret
? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
476 add_script
, add_ret
));
478 smb_nscd_flush_user_cache();
483 pwd
= Get_Pwnam_alloc(tmp_ctx
, name
);
486 DEBUG(3, ("Could not find user %s, add script did not work\n", name
));
487 return NT_STATUS_NO_SUCH_USER
;
491 /* we have a valid SID coming out of this call */
493 status
= samu_alloc_rid_unix( sam_pass
, pwd
);
497 if (!NT_STATUS_IS_OK(status
)) {
498 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status
)));
502 if (!sid_peek_check_rid(get_global_sam_sid(),
503 pdb_get_user_sid(sam_pass
), rid
)) {
504 DEBUG(0, ("Could not get RID of fresh user\n"));
505 return NT_STATUS_INTERNAL_ERROR
;
508 /* Use the username case specified in the original request */
510 pdb_set_username( sam_pass
, name
, PDB_SET
);
512 /* Disable the account on creation, it does not have a reasonable password yet. */
514 acb_info
|= ACB_DISABLED
;
516 pdb_set_acct_ctrl(sam_pass
, acb_info
, PDB_CHANGED
);
518 status
= pdb_add_sam_account(sam_pass
);
520 TALLOC_FREE(sam_pass
);
525 NTSTATUS
pdb_create_user(TALLOC_CTX
*mem_ctx
, const char *name
, uint32_t flags
,
528 struct pdb_methods
*pdb
= pdb_get_methods();
529 return pdb
->create_user(pdb
, mem_ctx
, name
, flags
, rid
);
532 /****************************************************************************
533 Delete a UNIX user on demand.
534 ****************************************************************************/
536 static int smb_delete_user(const char *unix_user
)
538 char *del_script
= NULL
;
543 if ( strequal( unix_user
, "root" ) ) {
544 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
548 del_script
= talloc_strdup(talloc_tos(), lp_deluser_script());
549 if (!del_script
|| !*del_script
) {
552 del_script
= talloc_all_string_sub(talloc_tos(),
559 ret
= smbrun(del_script
,NULL
);
562 smb_nscd_flush_user_cache();
564 DEBUG(ret
? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script
,ret
));
569 static NTSTATUS
pdb_default_delete_user(struct pdb_methods
*methods
,
571 struct samu
*sam_acct
)
576 status
= pdb_delete_sam_account(sam_acct
);
577 if (!NT_STATUS_IS_OK(status
)) {
582 * Now delete the unix side ....
583 * note: we don't check if the delete really happened as the script is
584 * not necessary present and maybe the sysadmin doesn't want to delete
588 /* always lower case the username before handing it off to
591 fstrcpy( username
, pdb_get_username(sam_acct
) );
592 strlower_m( username
);
594 smb_delete_user( username
);
599 NTSTATUS
pdb_delete_user(TALLOC_CTX
*mem_ctx
, struct samu
*sam_acct
)
601 struct pdb_methods
*pdb
= pdb_get_methods();
604 /* sanity check to make sure we don't delete root */
606 if ( !sid_to_uid( pdb_get_user_sid(sam_acct
), &uid
) ) {
607 return NT_STATUS_NO_SUCH_USER
;
611 return NT_STATUS_ACCESS_DENIED
;
614 return pdb
->delete_user(pdb
, mem_ctx
, sam_acct
);
617 NTSTATUS
pdb_add_sam_account(struct samu
*sam_acct
)
619 struct pdb_methods
*pdb
= pdb_get_methods();
620 return pdb
->add_sam_account(pdb
, sam_acct
);
623 NTSTATUS
pdb_update_sam_account(struct samu
*sam_acct
)
625 struct pdb_methods
*pdb
= pdb_get_methods();
627 memcache_flush(NULL
, PDB_GETPWSID_CACHE
);
629 return pdb
->update_sam_account(pdb
, sam_acct
);
632 NTSTATUS
pdb_delete_sam_account(struct samu
*sam_acct
)
634 struct pdb_methods
*pdb
= pdb_get_methods();
636 memcache_flush(NULL
, PDB_GETPWSID_CACHE
);
638 return pdb
->delete_sam_account(pdb
, sam_acct
);
641 NTSTATUS
pdb_rename_sam_account(struct samu
*oldname
, const char *newname
)
643 struct pdb_methods
*pdb
= pdb_get_methods();
647 memcache_flush(NULL
, PDB_GETPWSID_CACHE
);
649 /* sanity check to make sure we don't rename root */
651 if ( !sid_to_uid( pdb_get_user_sid(oldname
), &uid
) ) {
652 return NT_STATUS_NO_SUCH_USER
;
656 return NT_STATUS_ACCESS_DENIED
;
659 status
= pdb
->rename_sam_account(pdb
, oldname
, newname
);
661 /* always flush the cache here just to be safe */
667 NTSTATUS
pdb_update_login_attempts(struct samu
*sam_acct
, bool success
)
669 struct pdb_methods
*pdb
= pdb_get_methods();
670 return pdb
->update_login_attempts(pdb
, sam_acct
, success
);
673 bool pdb_getgrsid(GROUP_MAP
*map
, struct dom_sid sid
)
675 struct pdb_methods
*pdb
= pdb_get_methods();
676 return NT_STATUS_IS_OK(pdb
->getgrsid(pdb
, map
, sid
));
679 bool pdb_getgrgid(GROUP_MAP
*map
, gid_t gid
)
681 struct pdb_methods
*pdb
= pdb_get_methods();
682 return NT_STATUS_IS_OK(pdb
->getgrgid(pdb
, map
, gid
));
685 bool pdb_getgrnam(GROUP_MAP
*map
, const char *name
)
687 struct pdb_methods
*pdb
= pdb_get_methods();
688 return NT_STATUS_IS_OK(pdb
->getgrnam(pdb
, map
, name
));
691 static NTSTATUS
pdb_default_create_dom_group(struct pdb_methods
*methods
,
696 struct dom_sid group_sid
;
700 grp
= getgrnam(name
);
705 if (smb_create_group(name
, &gid
) != 0) {
706 return NT_STATUS_ACCESS_DENIED
;
713 return NT_STATUS_ACCESS_DENIED
;
716 if (pdb_capabilities() & PDB_CAP_STORE_RIDS
) {
717 if (!pdb_new_rid(rid
)) {
718 return NT_STATUS_ACCESS_DENIED
;
721 *rid
= algorithmic_pdb_gid_to_group_rid( grp
->gr_gid
);
724 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
726 return add_initial_entry(grp
->gr_gid
, sid_to_fstring(tmp
, &group_sid
),
727 SID_NAME_DOM_GRP
, name
, NULL
);
730 NTSTATUS
pdb_create_dom_group(TALLOC_CTX
*mem_ctx
, const char *name
,
733 struct pdb_methods
*pdb
= pdb_get_methods();
734 return pdb
->create_dom_group(pdb
, mem_ctx
, name
, rid
);
737 static NTSTATUS
pdb_default_delete_dom_group(struct pdb_methods
*methods
,
741 struct dom_sid group_sid
;
745 const char *grp_name
;
748 map
.gid
= (gid_t
) -1;
750 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
752 if (!get_domain_group_from_sid(group_sid
, &map
)) {
753 DEBUG(10, ("Could not find group for rid %d\n", rid
));
754 return NT_STATUS_NO_SUCH_GROUP
;
757 /* We need the group name for the smb_delete_group later on */
759 if (map
.gid
== (gid_t
)-1) {
760 return NT_STATUS_NO_SUCH_GROUP
;
763 grp
= getgrgid(map
.gid
);
765 return NT_STATUS_NO_SUCH_GROUP
;
768 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
770 grp_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
771 if (grp_name
== NULL
) {
772 return NT_STATUS_NO_MEMORY
;
775 status
= pdb_delete_group_mapping_entry(group_sid
);
777 if (!NT_STATUS_IS_OK(status
)) {
781 /* Don't check the result of smb_delete_group */
783 smb_delete_group(grp_name
);
788 NTSTATUS
pdb_delete_dom_group(TALLOC_CTX
*mem_ctx
, uint32_t rid
)
790 struct pdb_methods
*pdb
= pdb_get_methods();
791 return pdb
->delete_dom_group(pdb
, mem_ctx
, rid
);
794 NTSTATUS
pdb_add_group_mapping_entry(GROUP_MAP
*map
)
796 struct pdb_methods
*pdb
= pdb_get_methods();
797 return pdb
->add_group_mapping_entry(pdb
, map
);
800 NTSTATUS
pdb_update_group_mapping_entry(GROUP_MAP
*map
)
802 struct pdb_methods
*pdb
= pdb_get_methods();
803 return pdb
->update_group_mapping_entry(pdb
, map
);
806 NTSTATUS
pdb_delete_group_mapping_entry(struct dom_sid sid
)
808 struct pdb_methods
*pdb
= pdb_get_methods();
809 return pdb
->delete_group_mapping_entry(pdb
, sid
);
812 bool pdb_enum_group_mapping(const struct dom_sid
*sid
, enum lsa_SidType sid_name_use
, GROUP_MAP
**pp_rmap
,
813 size_t *p_num_entries
, bool unix_only
)
815 struct pdb_methods
*pdb
= pdb_get_methods();
816 return NT_STATUS_IS_OK(pdb
-> enum_group_mapping(pdb
, sid
, sid_name_use
,
817 pp_rmap
, p_num_entries
, unix_only
));
820 NTSTATUS
pdb_enum_group_members(TALLOC_CTX
*mem_ctx
,
821 const struct dom_sid
*sid
,
822 uint32_t **pp_member_rids
,
823 size_t *p_num_members
)
825 struct pdb_methods
*pdb
= pdb_get_methods();
828 result
= pdb
->enum_group_members(pdb
, mem_ctx
,
829 sid
, pp_member_rids
, p_num_members
);
831 /* special check for rid 513 */
833 if ( !NT_STATUS_IS_OK( result
) ) {
836 sid_peek_rid( sid
, &rid
);
838 if ( rid
== DOMAIN_RID_USERS
) {
840 *pp_member_rids
= NULL
;
849 NTSTATUS
pdb_enum_group_memberships(TALLOC_CTX
*mem_ctx
, struct samu
*user
,
850 struct dom_sid
**pp_sids
, gid_t
**pp_gids
,
851 uint32_t *p_num_groups
)
853 struct pdb_methods
*pdb
= pdb_get_methods();
854 return pdb
->enum_group_memberships(
856 pp_sids
, pp_gids
, p_num_groups
);
859 static NTSTATUS
pdb_default_set_unix_primary_group(struct pdb_methods
*methods
,
861 struct samu
*sampass
)
866 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
) ||
867 (grp
= getgrgid(gid
)) == NULL
) {
868 return NT_STATUS_INVALID_PRIMARY_GROUP
;
871 if (smb_set_primary_group(grp
->gr_name
,
872 pdb_get_username(sampass
)) != 0) {
873 return NT_STATUS_ACCESS_DENIED
;
879 NTSTATUS
pdb_set_unix_primary_group(TALLOC_CTX
*mem_ctx
, struct samu
*user
)
881 struct pdb_methods
*pdb
= pdb_get_methods();
882 return pdb
->set_unix_primary_group(pdb
, mem_ctx
, user
);
886 * Helper function to see whether a user is in a group. We can't use
887 * user_in_group_sid here because this creates dependencies only smbd can
891 static bool pdb_user_in_group(TALLOC_CTX
*mem_ctx
, struct samu
*account
,
892 const struct dom_sid
*group_sid
)
894 struct dom_sid
*sids
;
896 uint32_t i
, num_groups
;
898 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx
, account
,
904 for (i
=0; i
<num_groups
; i
++) {
905 if (dom_sid_equal(group_sid
, &sids
[i
])) {
912 static NTSTATUS
pdb_default_add_groupmem(struct pdb_methods
*methods
,
917 struct dom_sid group_sid
, member_sid
;
918 struct samu
*account
= NULL
;
922 const char *group_name
;
926 map
.gid
= (gid_t
) -1;
928 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
929 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
931 if (!get_domain_group_from_sid(group_sid
, &map
) ||
932 (map
.gid
== (gid_t
)-1) ||
933 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
934 return NT_STATUS_NO_SUCH_GROUP
;
937 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
938 if (group_name
== NULL
) {
939 return NT_STATUS_NO_MEMORY
;
942 if ( !(account
= samu_new( NULL
)) ) {
943 return NT_STATUS_NO_MEMORY
;
946 if (!pdb_getsampwsid(account
, &member_sid
) ||
947 !sid_to_uid(&member_sid
, &uid
) ||
948 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
949 return NT_STATUS_NO_SUCH_USER
;
952 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
953 return NT_STATUS_MEMBER_IN_GROUP
;
957 * ok, the group exist, the user exist, the user is not in the group,
958 * we can (finally) add it to the group !
961 smb_add_user_group(group_name
, pwd
->pw_name
);
963 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
964 return NT_STATUS_ACCESS_DENIED
;
970 NTSTATUS
pdb_add_groupmem(TALLOC_CTX
*mem_ctx
, uint32_t group_rid
,
973 struct pdb_methods
*pdb
= pdb_get_methods();
974 return pdb
->add_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
977 static NTSTATUS
pdb_default_del_groupmem(struct pdb_methods
*methods
,
982 struct dom_sid group_sid
, member_sid
;
983 struct samu
*account
= NULL
;
987 const char *group_name
;
990 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
991 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
993 if (!get_domain_group_from_sid(group_sid
, &map
) ||
994 (map
.gid
== (gid_t
)-1) ||
995 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
996 return NT_STATUS_NO_SUCH_GROUP
;
999 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
1000 if (group_name
== NULL
) {
1001 return NT_STATUS_NO_MEMORY
;
1004 if ( !(account
= samu_new( NULL
)) ) {
1005 return NT_STATUS_NO_MEMORY
;
1008 if (!pdb_getsampwsid(account
, &member_sid
) ||
1009 !sid_to_uid(&member_sid
, &uid
) ||
1010 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
1011 return NT_STATUS_NO_SUCH_USER
;
1014 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
1015 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
1019 * ok, the group exist, the user exist, the user is in the group,
1020 * we can (finally) delete it from the group!
1023 smb_delete_user_group(group_name
, pwd
->pw_name
);
1025 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
1026 return NT_STATUS_ACCESS_DENIED
;
1029 return NT_STATUS_OK
;
1032 NTSTATUS
pdb_del_groupmem(TALLOC_CTX
*mem_ctx
, uint32_t group_rid
,
1033 uint32_t member_rid
)
1035 struct pdb_methods
*pdb
= pdb_get_methods();
1036 return pdb
->del_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
1039 NTSTATUS
pdb_create_alias(const char *name
, uint32_t *rid
)
1041 struct pdb_methods
*pdb
= pdb_get_methods();
1042 return pdb
->create_alias(pdb
, name
, rid
);
1045 NTSTATUS
pdb_delete_alias(const struct dom_sid
*sid
)
1047 struct pdb_methods
*pdb
= pdb_get_methods();
1048 return pdb
->delete_alias(pdb
, sid
);
1051 NTSTATUS
pdb_get_aliasinfo(const struct dom_sid
*sid
, struct acct_info
*info
)
1053 struct pdb_methods
*pdb
= pdb_get_methods();
1054 return pdb
->get_aliasinfo(pdb
, sid
, info
);
1057 NTSTATUS
pdb_set_aliasinfo(const struct dom_sid
*sid
, struct acct_info
*info
)
1059 struct pdb_methods
*pdb
= pdb_get_methods();
1060 return pdb
->set_aliasinfo(pdb
, sid
, info
);
1063 NTSTATUS
pdb_add_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
1065 struct pdb_methods
*pdb
= pdb_get_methods();
1066 return pdb
->add_aliasmem(pdb
, alias
, member
);
1069 NTSTATUS
pdb_del_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
1071 struct pdb_methods
*pdb
= pdb_get_methods();
1072 return pdb
->del_aliasmem(pdb
, alias
, member
);
1075 NTSTATUS
pdb_enum_aliasmem(const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
1076 struct dom_sid
**pp_members
, size_t *p_num_members
)
1078 struct pdb_methods
*pdb
= pdb_get_methods();
1079 return pdb
->enum_aliasmem(pdb
, alias
, mem_ctx
, pp_members
,
1083 NTSTATUS
pdb_enum_alias_memberships(TALLOC_CTX
*mem_ctx
,
1084 const struct dom_sid
*domain_sid
,
1085 const struct dom_sid
*members
, size_t num_members
,
1086 uint32_t **pp_alias_rids
,
1087 size_t *p_num_alias_rids
)
1089 struct pdb_methods
*pdb
= pdb_get_methods();
1090 return pdb
->enum_alias_memberships(pdb
, mem_ctx
,
1092 members
, num_members
,
1097 NTSTATUS
pdb_lookup_rids(const struct dom_sid
*domain_sid
,
1101 enum lsa_SidType
*attrs
)
1103 struct pdb_methods
*pdb
= pdb_get_methods();
1104 return pdb
->lookup_rids(pdb
, domain_sid
, num_rids
, rids
, names
, attrs
);
1108 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
1109 * in the samba code.
1110 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
1111 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
1112 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
1113 * down to are pdb_getsampwnam and pdb_getgrnam instead of
1115 * But in principle, it the call belongs to the API and might get
1116 * used in this context some day.
1119 NTSTATUS
pdb_lookup_names(const struct dom_sid
*domain_sid
,
1123 enum lsa_SidType
*attrs
)
1125 struct pdb_methods
*pdb
= pdb_get_methods();
1126 return pdb
->lookup_names(pdb
, domain_sid
, num_names
, names
, rids
, attrs
);
1130 bool pdb_get_account_policy(enum pdb_policy_type type
, uint32_t *value
)
1132 struct pdb_methods
*pdb
= pdb_get_methods();
1136 status
= pdb
->get_account_policy(pdb
, type
, value
);
1139 return NT_STATUS_IS_OK(status
);
1142 bool pdb_set_account_policy(enum pdb_policy_type type
, uint32_t value
)
1144 struct pdb_methods
*pdb
= pdb_get_methods();
1148 status
= pdb
->set_account_policy(pdb
, type
, value
);
1151 return NT_STATUS_IS_OK(status
);
1154 bool pdb_get_seq_num(time_t *seq_num
)
1156 struct pdb_methods
*pdb
= pdb_get_methods();
1157 return NT_STATUS_IS_OK(pdb
->get_seq_num(pdb
, seq_num
));
1160 bool pdb_uid_to_sid(uid_t uid
, struct dom_sid
*sid
)
1162 struct pdb_methods
*pdb
= pdb_get_methods();
1163 return pdb
->uid_to_sid(pdb
, uid
, sid
);
1166 bool pdb_gid_to_sid(gid_t gid
, struct dom_sid
*sid
)
1168 struct pdb_methods
*pdb
= pdb_get_methods();
1169 return pdb
->gid_to_sid(pdb
, gid
, sid
);
1172 bool pdb_sid_to_id(const struct dom_sid
*sid
, union unid_t
*id
,
1173 enum lsa_SidType
*type
)
1175 struct pdb_methods
*pdb
= pdb_get_methods();
1176 return pdb
->sid_to_id(pdb
, sid
, id
, type
);
1179 uint32_t pdb_capabilities(void)
1181 struct pdb_methods
*pdb
= pdb_get_methods();
1182 return pdb
->capabilities(pdb
);
1185 /********************************************************************
1186 Allocate a new RID from the passdb backend. Verify that it is free
1187 by calling lookup_global_sam_rid() to verify that the RID is not
1188 in use. This handles servers that have existing users or groups
1189 with add RIDs (assigned from previous algorithmic mappings)
1190 ********************************************************************/
1192 bool pdb_new_rid(uint32_t *rid
)
1194 struct pdb_methods
*pdb
= pdb_get_methods();
1195 const char *name
= NULL
;
1196 enum lsa_SidType type
;
1197 uint32_t allocated_rid
= 0;
1201 if ((pdb_capabilities() & PDB_CAP_STORE_RIDS
) == 0) {
1202 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1207 if (algorithmic_rid_base() != BASE_RID
) {
1208 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1209 "without algorithmic RIDs is chosen.\n"));
1210 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1211 "add', set the maximum used RID\n"));
1212 DEBUGADD(0, ("and remove the parameter\n"));
1216 if ( (ctx
= talloc_init("pdb_new_rid")) == NULL
) {
1217 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1221 /* Attempt to get an unused RID (max tires is 250...yes that it is
1222 and arbitrary number I pulkled out of my head). -- jerry */
1224 for ( i
=0; allocated_rid
==0 && i
<250; i
++ ) {
1227 if ( !pdb
->new_rid(pdb
, &allocated_rid
) ) {
1231 /* validate that the RID is not in use */
1233 if ( lookup_global_sam_rid( ctx
, allocated_rid
, &name
, &type
, NULL
) ) {
1240 if ( allocated_rid
== 0 ) {
1241 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1245 *rid
= allocated_rid
;
1250 /***************************************************************
1251 Initialize the static context (at smbd startup etc).
1253 If uninitialised, context will auto-init on first use.
1254 ***************************************************************/
1256 bool initialize_password_db(bool reload
, struct event_context
*event_ctx
)
1258 pdb_event_ctx
= event_ctx
;
1259 return (pdb_get_methods_reload(reload
) != NULL
);
1263 /***************************************************************************
1264 Default implementations of some functions.
1265 ****************************************************************************/
1267 static NTSTATUS
pdb_default_getsampwnam (struct pdb_methods
*methods
, struct samu
*user
, const char *sname
)
1269 return NT_STATUS_NO_SUCH_USER
;
1272 static NTSTATUS
pdb_default_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1274 return NT_STATUS_NO_SUCH_USER
;
1277 static NTSTATUS
pdb_default_add_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1279 return NT_STATUS_NOT_IMPLEMENTED
;
1282 static NTSTATUS
pdb_default_update_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1284 return NT_STATUS_NOT_IMPLEMENTED
;
1287 static NTSTATUS
pdb_default_delete_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
)
1289 return NT_STATUS_NOT_IMPLEMENTED
;
1292 static NTSTATUS
pdb_default_rename_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
, const char *newname
)
1294 return NT_STATUS_NOT_IMPLEMENTED
;
1297 static NTSTATUS
pdb_default_update_login_attempts (struct pdb_methods
*methods
, struct samu
*newpwd
, bool success
)
1299 /* Only the pdb_nds backend implements this, by
1300 * default just return ok. */
1301 return NT_STATUS_OK
;
1304 static NTSTATUS
pdb_default_get_account_policy(struct pdb_methods
*methods
, enum pdb_policy_type type
, uint32_t *value
)
1306 return account_policy_get(type
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1309 static NTSTATUS
pdb_default_set_account_policy(struct pdb_methods
*methods
, enum pdb_policy_type type
, uint32_t value
)
1311 return account_policy_set(type
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1314 static NTSTATUS
pdb_default_get_seq_num(struct pdb_methods
*methods
, time_t *seq_num
)
1316 *seq_num
= time(NULL
);
1317 return NT_STATUS_OK
;
1320 static bool pdb_default_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
1321 struct dom_sid
*sid
)
1323 struct samu
*sampw
= NULL
;
1324 struct passwd
*unix_pw
;
1327 unix_pw
= sys_getpwuid( uid
);
1330 DEBUG(4,("pdb_default_uid_to_sid: host has no idea of uid "
1331 "%lu\n", (unsigned long)uid
));
1335 if ( !(sampw
= samu_new( NULL
)) ) {
1336 DEBUG(0,("pdb_default_uid_to_sid: samu_new() failed!\n"));
1341 ret
= NT_STATUS_IS_OK(
1342 methods
->getsampwnam(methods
, sampw
, unix_pw
->pw_name
));
1346 DEBUG(5, ("pdb_default_uid_to_sid: Did not find user "
1347 "%s (%u)\n", unix_pw
->pw_name
, (unsigned int)uid
));
1352 sid_copy(sid
, pdb_get_user_sid(sampw
));
1359 static bool pdb_default_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
1360 struct dom_sid
*sid
)
1364 if (!NT_STATUS_IS_OK(methods
->getgrgid(methods
, &map
, gid
))) {
1368 sid_copy(sid
, &map
.sid
);
1372 static bool pdb_default_sid_to_id(struct pdb_methods
*methods
,
1373 const struct dom_sid
*sid
,
1374 union unid_t
*id
, enum lsa_SidType
*type
)
1376 TALLOC_CTX
*mem_ctx
;
1381 mem_ctx
= talloc_new(NULL
);
1383 if (mem_ctx
== NULL
) {
1384 DEBUG(0, ("talloc_new failed\n"));
1388 if (sid_peek_check_rid(get_global_sam_sid(), sid
, &rid
)) {
1389 /* Here we might have users as well as groups and aliases */
1390 ret
= lookup_global_sam_rid(mem_ctx
, rid
, &name
, type
, id
);
1394 /* check for "Unix User" */
1396 if ( sid_peek_check_rid(&global_sid_Unix_Users
, sid
, &rid
) ) {
1398 *type
= SID_NAME_USER
;
1403 /* check for "Unix Group" */
1405 if ( sid_peek_check_rid(&global_sid_Unix_Groups
, sid
, &rid
) ) {
1407 *type
= SID_NAME_ALIAS
;
1414 if (sid_check_is_in_builtin(sid
) ||
1415 sid_check_is_in_wellknown_domain(sid
)) {
1416 /* Here we only have aliases */
1418 if (!NT_STATUS_IS_OK(methods
->getgrsid(methods
, &map
, *sid
))) {
1419 DEBUG(10, ("Could not find map for sid %s\n",
1420 sid_string_dbg(sid
)));
1423 if ((map
.sid_name_use
!= SID_NAME_ALIAS
) &&
1424 (map
.sid_name_use
!= SID_NAME_WKN_GRP
)) {
1425 DEBUG(10, ("Map for sid %s is a %s, expected an "
1426 "alias\n", sid_string_dbg(sid
),
1427 sid_type_lookup(map
.sid_name_use
)));
1432 *type
= SID_NAME_ALIAS
;
1437 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1438 sid_string_dbg(sid
)));
1442 TALLOC_FREE(mem_ctx
);
1446 static bool get_memberuids(TALLOC_CTX
*mem_ctx
, gid_t gid
, uid_t
**pp_uids
, uint32_t *p_num
)
1457 /* We only look at our own sam, so don't care about imported stuff */
1458 winbind_env
= winbind_env_set();
1459 (void)winbind_off();
1461 if ((grp
= getgrgid(gid
)) == NULL
) {
1462 /* allow winbindd lookups, but only if they weren't already disabled */
1466 /* Primary group members */
1468 while ((pwd
= getpwent()) != NULL
) {
1469 if (pwd
->pw_gid
== gid
) {
1470 if (!add_uid_to_array_unique(mem_ctx
, pwd
->pw_uid
,
1478 /* Secondary group members */
1479 for (gr
= grp
->gr_mem
; (*gr
!= NULL
) && ((*gr
)[0] != '\0'); gr
+= 1) {
1480 struct passwd
*pw
= getpwnam(*gr
);
1484 if (!add_uid_to_array_unique(mem_ctx
, pw
->pw_uid
, pp_uids
, p_num
)) {
1493 /* allow winbindd lookups, but only if they weren't already disabled */
1501 static NTSTATUS
pdb_default_enum_group_members(struct pdb_methods
*methods
,
1502 TALLOC_CTX
*mem_ctx
,
1503 const struct dom_sid
*group
,
1504 uint32_t **pp_member_rids
,
1505 size_t *p_num_members
)
1509 uint32_t i
, num_uids
;
1511 *pp_member_rids
= NULL
;
1514 if (!sid_to_gid(group
, &gid
))
1515 return NT_STATUS_NO_SUCH_GROUP
;
1517 if(!get_memberuids(mem_ctx
, gid
, &uids
, &num_uids
))
1518 return NT_STATUS_NO_SUCH_GROUP
;
1521 return NT_STATUS_OK
;
1523 *pp_member_rids
= TALLOC_ZERO_ARRAY(mem_ctx
, uint32_t, num_uids
);
1525 for (i
=0; i
<num_uids
; i
++) {
1528 uid_to_sid(&sid
, uids
[i
]);
1530 if (!sid_check_is_in_our_domain(&sid
)) {
1531 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1532 "in our domain\n"));
1536 sid_peek_rid(&sid
, &(*pp_member_rids
)[*p_num_members
]);
1537 *p_num_members
+= 1;
1540 return NT_STATUS_OK
;
1543 static NTSTATUS
pdb_default_enum_group_memberships(struct pdb_methods
*methods
,
1544 TALLOC_CTX
*mem_ctx
,
1546 struct dom_sid
**pp_sids
,
1548 uint32_t *p_num_groups
)
1553 const char *username
= pdb_get_username(user
);
1556 /* Ignore the primary group SID. Honor the real Unix primary group.
1557 The primary group SID is only of real use to Windows clients */
1559 if ( !(pw
= Get_Pwnam_alloc(mem_ctx
, username
)) ) {
1560 return NT_STATUS_NO_SUCH_USER
;
1567 if (!getgroups_unix_user(mem_ctx
, username
, gid
, pp_gids
, p_num_groups
)) {
1568 return NT_STATUS_NO_SUCH_USER
;
1571 if (*p_num_groups
== 0) {
1572 smb_panic("primary group missing");
1575 *pp_sids
= TALLOC_ARRAY(mem_ctx
, struct dom_sid
, *p_num_groups
);
1577 if (*pp_sids
== NULL
) {
1578 TALLOC_FREE(*pp_gids
);
1579 return NT_STATUS_NO_MEMORY
;
1582 for (i
=0; i
<*p_num_groups
; i
++) {
1583 gid_to_sid(&(*pp_sids
)[i
], (*pp_gids
)[i
]);
1586 return NT_STATUS_OK
;
1589 /*******************************************************************
1590 Look up a rid in the SAM we're responsible for (i.e. passdb)
1591 ********************************************************************/
1593 static bool lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32_t rid
,
1595 enum lsa_SidType
*psid_name_use
,
1596 union unid_t
*unix_id
)
1598 struct samu
*sam_account
= NULL
;
1603 *psid_name_use
= SID_NAME_UNKNOWN
;
1605 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1606 (unsigned int)rid
));
1608 sid_compose(&sid
, get_global_sam_sid(), rid
);
1610 /* see if the passdb can help us with the name of the user */
1612 if ( !(sam_account
= samu_new( NULL
)) ) {
1616 /* BEING ROOT BLOCK */
1618 if (pdb_getsampwsid(sam_account
, &sid
)) {
1621 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1622 *name
= talloc_strdup(mem_ctx
, pdb_get_username(sam_account
));
1624 TALLOC_FREE(sam_account
);
1628 *psid_name_use
= SID_NAME_USER
;
1630 TALLOC_FREE(sam_account
);
1632 if (unix_id
== NULL
) {
1636 pw
= Get_Pwnam_alloc(talloc_tos(), *name
);
1640 unix_id
->uid
= pw
->pw_uid
;
1644 TALLOC_FREE(sam_account
);
1646 ret
= pdb_getgrsid(&map
, sid
);
1648 /* END BECOME_ROOT BLOCK */
1650 /* do not resolve SIDs to a name unless there is a valid
1651 gid associated with it */
1653 if ( ret
&& (map
.gid
!= (gid_t
)-1) ) {
1654 *name
= talloc_strdup(mem_ctx
, map
.nt_name
);
1655 *psid_name_use
= map
.sid_name_use
;
1658 unix_id
->gid
= map
.gid
;
1664 /* Windows will always map RID 513 to something. On a non-domain
1665 controller, this gets mapped to SERVER\None. */
1668 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1672 if ( rid
== DOMAIN_RID_USERS
) {
1673 *name
= talloc_strdup(mem_ctx
, "None" );
1674 *psid_name_use
= SID_NAME_DOM_GRP
;
1682 static NTSTATUS
pdb_default_lookup_rids(struct pdb_methods
*methods
,
1683 const struct dom_sid
*domain_sid
,
1687 enum lsa_SidType
*attrs
)
1691 bool have_mapped
= False
;
1692 bool have_unmapped
= False
;
1694 if (sid_check_is_builtin(domain_sid
)) {
1696 for (i
=0; i
<num_rids
; i
++) {
1699 if (lookup_builtin_rid(names
, rids
[i
], &name
)) {
1700 attrs
[i
] = SID_NAME_ALIAS
;
1702 DEBUG(5,("lookup_rids: %s:%d\n",
1703 names
[i
], attrs
[i
]));
1706 have_unmapped
= True
;
1707 attrs
[i
] = SID_NAME_UNKNOWN
;
1713 /* Should not happen, but better check once too many */
1714 if (!sid_check_is_domain(domain_sid
)) {
1715 return NT_STATUS_INVALID_HANDLE
;
1718 for (i
= 0; i
< num_rids
; i
++) {
1721 if (lookup_global_sam_rid(names
, rids
[i
], &name
, &attrs
[i
],
1724 return NT_STATUS_NO_MEMORY
;
1727 DEBUG(5,("lookup_rids: %s:%d\n", names
[i
], attrs
[i
]));
1730 have_unmapped
= True
;
1731 attrs
[i
] = SID_NAME_UNKNOWN
;
1737 result
= NT_STATUS_NONE_MAPPED
;
1740 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1746 static NTSTATUS
pdb_default_lookup_names(struct pdb_methods
*methods
,
1747 const struct dom_sid
*domain_sid
,
1751 enum lsa_SidType
*attrs
)
1755 bool have_mapped
= False
;
1756 bool have_unmapped
= False
;
1758 if (sid_check_is_builtin(domain_sid
)) {
1760 for (i
=0; i
<num_names
; i
++) {
1763 if (lookup_builtin_name(names
[i
], &rid
)) {
1764 attrs
[i
] = SID_NAME_ALIAS
;
1766 DEBUG(5,("lookup_rids: %s:%d\n",
1767 names
[i
], attrs
[i
]));
1770 have_unmapped
= True
;
1771 attrs
[i
] = SID_NAME_UNKNOWN
;
1777 /* Should not happen, but better check once too many */
1778 if (!sid_check_is_domain(domain_sid
)) {
1779 return NT_STATUS_INVALID_HANDLE
;
1782 for (i
= 0; i
< num_names
; i
++) {
1783 if (lookup_global_sam_name(names
[i
], 0, &rids
[i
], &attrs
[i
])) {
1784 DEBUG(5,("lookup_names: %s-> %d:%d\n", names
[i
],
1785 rids
[i
], attrs
[i
]));
1788 have_unmapped
= True
;
1789 attrs
[i
] = SID_NAME_UNKNOWN
;
1795 result
= NT_STATUS_NONE_MAPPED
;
1798 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1804 static int pdb_search_destructor(struct pdb_search
*search
)
1806 if ((!search
->search_ended
) && (search
->search_end
!= NULL
)) {
1807 search
->search_end(search
);
1812 struct pdb_search
*pdb_search_init(TALLOC_CTX
*mem_ctx
,
1813 enum pdb_search_type type
)
1815 struct pdb_search
*result
;
1817 result
= talloc(mem_ctx
, struct pdb_search
);
1818 if (result
== NULL
) {
1819 DEBUG(0, ("talloc failed\n"));
1823 result
->type
= type
;
1824 result
->cache
= NULL
;
1825 result
->num_entries
= 0;
1826 result
->cache_size
= 0;
1827 result
->search_ended
= False
;
1828 result
->search_end
= NULL
;
1830 /* Segfault appropriately if not initialized */
1831 result
->next_entry
= NULL
;
1832 result
->search_end
= NULL
;
1834 talloc_set_destructor(result
, pdb_search_destructor
);
1839 static void fill_displayentry(TALLOC_CTX
*mem_ctx
, uint32_t rid
,
1840 uint16_t acct_flags
,
1841 const char *account_name
,
1842 const char *fullname
,
1843 const char *description
,
1844 struct samr_displayentry
*entry
)
1847 entry
->acct_flags
= acct_flags
;
1849 if (account_name
!= NULL
)
1850 entry
->account_name
= talloc_strdup(mem_ctx
, account_name
);
1852 entry
->account_name
= "";
1854 if (fullname
!= NULL
)
1855 entry
->fullname
= talloc_strdup(mem_ctx
, fullname
);
1857 entry
->fullname
= "";
1859 if (description
!= NULL
)
1860 entry
->description
= talloc_strdup(mem_ctx
, description
);
1862 entry
->description
= "";
1865 struct group_search
{
1867 size_t num_groups
, current_group
;
1870 static bool next_entry_groups(struct pdb_search
*s
,
1871 struct samr_displayentry
*entry
)
1873 struct group_search
*state
= (struct group_search
*)s
->private_data
;
1875 GROUP_MAP
*map
= &state
->groups
[state
->current_group
];
1877 if (state
->current_group
== state
->num_groups
)
1880 sid_peek_rid(&map
->sid
, &rid
);
1882 fill_displayentry(s
, rid
, 0, map
->nt_name
, NULL
, map
->comment
, entry
);
1884 state
->current_group
+= 1;
1888 static void search_end_groups(struct pdb_search
*search
)
1890 struct group_search
*state
=
1891 (struct group_search
*)search
->private_data
;
1892 SAFE_FREE(state
->groups
);
1895 static bool pdb_search_grouptype(struct pdb_search
*search
,
1896 const struct dom_sid
*sid
, enum lsa_SidType type
)
1898 struct group_search
*state
;
1900 state
= talloc(search
, struct group_search
);
1901 if (state
== NULL
) {
1902 DEBUG(0, ("talloc failed\n"));
1906 if (!pdb_enum_group_mapping(sid
, type
, &state
->groups
, &state
->num_groups
,
1908 DEBUG(0, ("Could not enum groups\n"));
1912 state
->current_group
= 0;
1913 search
->private_data
= state
;
1914 search
->next_entry
= next_entry_groups
;
1915 search
->search_end
= search_end_groups
;
1919 static bool pdb_default_search_groups(struct pdb_methods
*methods
,
1920 struct pdb_search
*search
)
1922 return pdb_search_grouptype(search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
1925 static bool pdb_default_search_aliases(struct pdb_methods
*methods
,
1926 struct pdb_search
*search
,
1927 const struct dom_sid
*sid
)
1930 return pdb_search_grouptype(search
, sid
, SID_NAME_ALIAS
);
1933 static struct samr_displayentry
*pdb_search_getentry(struct pdb_search
*search
,
1936 if (idx
< search
->num_entries
)
1937 return &search
->cache
[idx
];
1939 if (search
->search_ended
)
1942 while (idx
>= search
->num_entries
) {
1943 struct samr_displayentry entry
;
1945 if (!search
->next_entry(search
, &entry
)) {
1946 search
->search_end(search
);
1947 search
->search_ended
= True
;
1951 ADD_TO_LARGE_ARRAY(search
, struct samr_displayentry
,
1952 entry
, &search
->cache
, &search
->num_entries
,
1953 &search
->cache_size
);
1956 return (search
->num_entries
> idx
) ? &search
->cache
[idx
] : NULL
;
1959 struct pdb_search
*pdb_search_users(TALLOC_CTX
*mem_ctx
, uint32_t acct_flags
)
1961 struct pdb_methods
*pdb
= pdb_get_methods();
1962 struct pdb_search
*result
;
1964 result
= pdb_search_init(mem_ctx
, PDB_USER_SEARCH
);
1965 if (result
== NULL
) {
1969 if (!pdb
->search_users(pdb
, result
, acct_flags
)) {
1970 TALLOC_FREE(result
);
1976 struct pdb_search
*pdb_search_groups(TALLOC_CTX
*mem_ctx
)
1978 struct pdb_methods
*pdb
= pdb_get_methods();
1979 struct pdb_search
*result
;
1981 result
= pdb_search_init(mem_ctx
, PDB_GROUP_SEARCH
);
1982 if (result
== NULL
) {
1986 if (!pdb
->search_groups(pdb
, result
)) {
1987 TALLOC_FREE(result
);
1993 struct pdb_search
*pdb_search_aliases(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
)
1995 struct pdb_methods
*pdb
= pdb_get_methods();
1996 struct pdb_search
*result
;
1998 if (pdb
== NULL
) return NULL
;
2000 result
= pdb_search_init(mem_ctx
, PDB_ALIAS_SEARCH
);
2001 if (result
== NULL
) {
2005 if (!pdb
->search_aliases(pdb
, result
, sid
)) {
2006 TALLOC_FREE(result
);
2012 uint32_t pdb_search_entries(struct pdb_search
*search
,
2013 uint32_t start_idx
, uint32_t max_entries
,
2014 struct samr_displayentry
**result
)
2016 struct samr_displayentry
*end_entry
;
2017 uint32_t end_idx
= start_idx
+max_entries
-1;
2019 /* The first entry needs to be searched after the last. Otherwise the
2020 * first entry might have moved due to a realloc during the search for
2021 * the last entry. */
2023 end_entry
= pdb_search_getentry(search
, end_idx
);
2024 *result
= pdb_search_getentry(search
, start_idx
);
2026 if (end_entry
!= NULL
)
2029 if (start_idx
>= search
->num_entries
)
2032 return search
->num_entries
- start_idx
;
2035 /*******************************************************************
2037 *******************************************************************/
2039 bool pdb_get_trusteddom_pw(const char *domain
, char** pwd
, struct dom_sid
*sid
,
2040 time_t *pass_last_set_time
)
2042 struct pdb_methods
*pdb
= pdb_get_methods();
2043 return pdb
->get_trusteddom_pw(pdb
, domain
, pwd
, sid
,
2044 pass_last_set_time
);
2047 bool pdb_set_trusteddom_pw(const char* domain
, const char* pwd
,
2048 const struct dom_sid
*sid
)
2050 struct pdb_methods
*pdb
= pdb_get_methods();
2051 return pdb
->set_trusteddom_pw(pdb
, domain
, pwd
, sid
);
2054 bool pdb_del_trusteddom_pw(const char *domain
)
2056 struct pdb_methods
*pdb
= pdb_get_methods();
2057 return pdb
->del_trusteddom_pw(pdb
, domain
);
2060 NTSTATUS
pdb_enum_trusteddoms(TALLOC_CTX
*mem_ctx
, uint32_t *num_domains
,
2061 struct trustdom_info
***domains
)
2063 struct pdb_methods
*pdb
= pdb_get_methods();
2064 return pdb
->enum_trusteddoms(pdb
, mem_ctx
, num_domains
, domains
);
2067 /*******************************************************************
2068 the defaults for trustdom methods:
2069 these simply call the original passdb/secrets.c actions,
2070 to be replaced by pdb_ldap.
2071 *******************************************************************/
2073 static bool pdb_default_get_trusteddom_pw(struct pdb_methods
*methods
,
2076 struct dom_sid
*sid
,
2077 time_t *pass_last_set_time
)
2079 return secrets_fetch_trusted_domain_password(domain
, pwd
,
2080 sid
, pass_last_set_time
);
2084 static bool pdb_default_set_trusteddom_pw(struct pdb_methods
*methods
,
2087 const struct dom_sid
*sid
)
2089 return secrets_store_trusted_domain_password(domain
, pwd
, sid
);
2092 static bool pdb_default_del_trusteddom_pw(struct pdb_methods
*methods
,
2095 return trusted_domain_password_delete(domain
);
2098 static NTSTATUS
pdb_default_enum_trusteddoms(struct pdb_methods
*methods
,
2099 TALLOC_CTX
*mem_ctx
,
2100 uint32_t *num_domains
,
2101 struct trustdom_info
***domains
)
2103 return secrets_trusted_domains(mem_ctx
, num_domains
, domains
);
2106 /*******************************************************************
2107 trusted_domain methods
2108 *******************************************************************/
2110 NTSTATUS
pdb_get_trusted_domain(TALLOC_CTX
*mem_ctx
, const char *domain
,
2111 struct pdb_trusted_domain
**td
)
2113 struct pdb_methods
*pdb
= pdb_get_methods();
2114 return pdb
->get_trusted_domain(pdb
, mem_ctx
, domain
, td
);
2117 NTSTATUS
pdb_get_trusted_domain_by_sid(TALLOC_CTX
*mem_ctx
, struct dom_sid
*sid
,
2118 struct pdb_trusted_domain
**td
)
2120 struct pdb_methods
*pdb
= pdb_get_methods();
2121 return pdb
->get_trusted_domain_by_sid(pdb
, mem_ctx
, sid
, td
);
2124 NTSTATUS
pdb_set_trusted_domain(const char* domain
,
2125 const struct pdb_trusted_domain
*td
)
2127 struct pdb_methods
*pdb
= pdb_get_methods();
2128 return pdb
->set_trusted_domain(pdb
, domain
, td
);
2131 NTSTATUS
pdb_del_trusted_domain(const char *domain
)
2133 struct pdb_methods
*pdb
= pdb_get_methods();
2134 return pdb
->del_trusted_domain(pdb
, domain
);
2137 NTSTATUS
pdb_enum_trusted_domains(TALLOC_CTX
*mem_ctx
, uint32_t *num_domains
,
2138 struct pdb_trusted_domain
***domains
)
2140 struct pdb_methods
*pdb
= pdb_get_methods();
2141 return pdb
->enum_trusted_domains(pdb
, mem_ctx
, num_domains
, domains
);
2144 static NTSTATUS
pdb_default_get_trusted_domain(struct pdb_methods
*methods
,
2145 TALLOC_CTX
*mem_ctx
,
2147 struct pdb_trusted_domain
**td
)
2149 return NT_STATUS_NOT_IMPLEMENTED
;
2152 static NTSTATUS
pdb_default_get_trusted_domain_by_sid(struct pdb_methods
*methods
,
2153 TALLOC_CTX
*mem_ctx
,
2154 struct dom_sid
*sid
,
2155 struct pdb_trusted_domain
**td
)
2157 return NT_STATUS_NOT_IMPLEMENTED
;
2160 static NTSTATUS
pdb_default_set_trusted_domain(struct pdb_methods
*methods
,
2162 const struct pdb_trusted_domain
*td
)
2164 return NT_STATUS_NOT_IMPLEMENTED
;
2167 static NTSTATUS
pdb_default_del_trusted_domain(struct pdb_methods
*methods
,
2170 return NT_STATUS_NOT_IMPLEMENTED
;
2173 static NTSTATUS
pdb_default_enum_trusted_domains(struct pdb_methods
*methods
,
2174 TALLOC_CTX
*mem_ctx
,
2175 uint32_t *num_domains
,
2176 struct pdb_trusted_domain
***domains
)
2178 return NT_STATUS_NOT_IMPLEMENTED
;
2181 static struct pdb_domain_info
*pdb_default_get_domain_info(
2182 struct pdb_methods
*m
, TALLOC_CTX
*mem_ctx
)
2187 /*******************************************************************
2188 Create a pdb_methods structure and initialize it with the default
2189 operations. In this way a passdb module can simply implement
2190 the functionality it cares about. However, normally this is done
2191 in groups of related functions.
2192 *******************************************************************/
2194 NTSTATUS
make_pdb_method( struct pdb_methods
**methods
)
2196 /* allocate memory for the structure as its own talloc CTX */
2198 *methods
= talloc_zero(NULL
, struct pdb_methods
);
2199 if (*methods
== NULL
) {
2200 return NT_STATUS_NO_MEMORY
;
2203 (*methods
)->get_domain_info
= pdb_default_get_domain_info
;
2204 (*methods
)->getsampwnam
= pdb_default_getsampwnam
;
2205 (*methods
)->getsampwsid
= pdb_default_getsampwsid
;
2206 (*methods
)->create_user
= pdb_default_create_user
;
2207 (*methods
)->delete_user
= pdb_default_delete_user
;
2208 (*methods
)->add_sam_account
= pdb_default_add_sam_account
;
2209 (*methods
)->update_sam_account
= pdb_default_update_sam_account
;
2210 (*methods
)->delete_sam_account
= pdb_default_delete_sam_account
;
2211 (*methods
)->rename_sam_account
= pdb_default_rename_sam_account
;
2212 (*methods
)->update_login_attempts
= pdb_default_update_login_attempts
;
2214 (*methods
)->getgrsid
= pdb_default_getgrsid
;
2215 (*methods
)->getgrgid
= pdb_default_getgrgid
;
2216 (*methods
)->getgrnam
= pdb_default_getgrnam
;
2217 (*methods
)->create_dom_group
= pdb_default_create_dom_group
;
2218 (*methods
)->delete_dom_group
= pdb_default_delete_dom_group
;
2219 (*methods
)->add_group_mapping_entry
= pdb_default_add_group_mapping_entry
;
2220 (*methods
)->update_group_mapping_entry
= pdb_default_update_group_mapping_entry
;
2221 (*methods
)->delete_group_mapping_entry
= pdb_default_delete_group_mapping_entry
;
2222 (*methods
)->enum_group_mapping
= pdb_default_enum_group_mapping
;
2223 (*methods
)->enum_group_members
= pdb_default_enum_group_members
;
2224 (*methods
)->enum_group_memberships
= pdb_default_enum_group_memberships
;
2225 (*methods
)->set_unix_primary_group
= pdb_default_set_unix_primary_group
;
2226 (*methods
)->add_groupmem
= pdb_default_add_groupmem
;
2227 (*methods
)->del_groupmem
= pdb_default_del_groupmem
;
2228 (*methods
)->create_alias
= pdb_default_create_alias
;
2229 (*methods
)->delete_alias
= pdb_default_delete_alias
;
2230 (*methods
)->get_aliasinfo
= pdb_default_get_aliasinfo
;
2231 (*methods
)->set_aliasinfo
= pdb_default_set_aliasinfo
;
2232 (*methods
)->add_aliasmem
= pdb_default_add_aliasmem
;
2233 (*methods
)->del_aliasmem
= pdb_default_del_aliasmem
;
2234 (*methods
)->enum_aliasmem
= pdb_default_enum_aliasmem
;
2235 (*methods
)->enum_alias_memberships
= pdb_default_alias_memberships
;
2236 (*methods
)->lookup_rids
= pdb_default_lookup_rids
;
2237 (*methods
)->get_account_policy
= pdb_default_get_account_policy
;
2238 (*methods
)->set_account_policy
= pdb_default_set_account_policy
;
2239 (*methods
)->get_seq_num
= pdb_default_get_seq_num
;
2240 (*methods
)->uid_to_sid
= pdb_default_uid_to_sid
;
2241 (*methods
)->gid_to_sid
= pdb_default_gid_to_sid
;
2242 (*methods
)->sid_to_id
= pdb_default_sid_to_id
;
2244 (*methods
)->search_groups
= pdb_default_search_groups
;
2245 (*methods
)->search_aliases
= pdb_default_search_aliases
;
2247 (*methods
)->get_trusteddom_pw
= pdb_default_get_trusteddom_pw
;
2248 (*methods
)->set_trusteddom_pw
= pdb_default_set_trusteddom_pw
;
2249 (*methods
)->del_trusteddom_pw
= pdb_default_del_trusteddom_pw
;
2250 (*methods
)->enum_trusteddoms
= pdb_default_enum_trusteddoms
;
2252 (*methods
)->get_trusted_domain
= pdb_default_get_trusted_domain
;
2253 (*methods
)->get_trusted_domain_by_sid
= pdb_default_get_trusted_domain_by_sid
;
2254 (*methods
)->set_trusted_domain
= pdb_default_set_trusted_domain
;
2255 (*methods
)->del_trusted_domain
= pdb_default_del_trusted_domain
;
2256 (*methods
)->enum_trusted_domains
= pdb_default_enum_trusted_domains
;
2258 return NT_STATUS_OK
;