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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_PASSDB
29 /* Cache of latest SAM lookup query */
31 static struct samu
*csamuser
= NULL
;
33 static struct pdb_init_function_entry
*backends
= NULL
;
35 static void lazy_initialize_passdb(void)
37 static BOOL initialized
= False
;
38 if(initialized
)return;
43 static struct pdb_init_function_entry
*pdb_find_backend_entry(const char *name
);
44 static BOOL
lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32 rid
,
46 enum SID_NAME_USE
*psid_name_use
,
47 union unid_t
*unix_id
);
48 /*******************************************************************
49 Clean up uninitialised passwords. The only way to tell
50 that these values are not 'real' is that they do not
51 have a valid last set time. Instead, the value is fixed at 0.
52 Therefore we use that as the key for 'is this a valid password'.
53 However, it is perfectly valid to have a 'default' last change
54 time, such LDAP with a missing attribute would produce.
55 ********************************************************************/
57 static void pdb_force_pw_initialization(struct samu
*pass
)
59 const uint8
*lm_pwd
, *nt_pwd
;
61 /* only reset a password if the last set time has been
62 explicitly been set to zero. A default last set time
65 if ( (pdb_get_init_flags(pass
, PDB_PASSLASTSET
) != PDB_DEFAULT
)
66 && (pdb_get_pass_last_set_time(pass
) == 0) )
69 if (pdb_get_init_flags(pass
, PDB_LMPASSWD
) != PDB_DEFAULT
)
71 lm_pwd
= pdb_get_lanman_passwd(pass
);
73 pdb_set_lanman_passwd(pass
, NULL
, PDB_CHANGED
);
75 if (pdb_get_init_flags(pass
, PDB_NTPASSWD
) != PDB_DEFAULT
)
77 nt_pwd
= pdb_get_nt_passwd(pass
);
79 pdb_set_nt_passwd(pass
, NULL
, PDB_CHANGED
);
86 NTSTATUS
smb_register_passdb(int version
, const char *name
, pdb_init_function init
)
88 struct pdb_init_function_entry
*entry
= backends
;
90 if(version
!= PASSDB_INTERFACE_VERSION
) {
91 DEBUG(0,("Can't register passdb backend!\n"
92 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
93 "while this version of samba uses version %d\n",
94 version
,PASSDB_INTERFACE_VERSION
));
95 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
99 return NT_STATUS_INVALID_PARAMETER
;
102 DEBUG(5,("Attempting to register passdb backend %s\n", name
));
104 /* Check for duplicates */
105 if (pdb_find_backend_entry(name
)) {
106 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name
));
107 return NT_STATUS_OBJECT_NAME_COLLISION
;
110 entry
= SMB_XMALLOC_P(struct pdb_init_function_entry
);
111 entry
->name
= smb_xstrdup(name
);
114 DLIST_ADD(backends
, entry
);
115 DEBUG(5,("Successfully added passdb backend '%s'\n", name
));
119 static struct pdb_init_function_entry
*pdb_find_backend_entry(const char *name
)
121 struct pdb_init_function_entry
*entry
= backends
;
124 if (strcmp(entry
->name
, name
)==0) return entry
;
131 /******************************************************************
132 Make a pdb_methods from scratch
133 *******************************************************************/
135 NTSTATUS
make_pdb_method_name(struct pdb_methods
**methods
, const char *selected
)
137 char *module_name
= smb_xstrdup(selected
);
138 char *module_location
= NULL
, *p
;
139 struct pdb_init_function_entry
*entry
;
140 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
142 lazy_initialize_passdb();
144 p
= strchr(module_name
, ':');
148 module_location
= p
+1;
149 trim_char(module_location
, ' ', ' ');
152 trim_char(module_name
, ' ', ' ');
155 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected
, module_name
));
157 entry
= pdb_find_backend_entry(module_name
);
159 /* Try to find a module that contains this module */
161 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
162 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name
)) && !(entry
= pdb_find_backend_entry(module_name
))) {
163 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name
));
164 SAFE_FREE(module_name
);
165 return NT_STATUS_UNSUCCESSFUL
;
169 /* No such backend found */
171 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name
));
172 SAFE_FREE(module_name
);
173 return NT_STATUS_INVALID_PARAMETER
;
176 DEBUG(5,("Found pdb backend %s\n", module_name
));
178 if ( !NT_STATUS_IS_OK( nt_status
= entry
->init(methods
, module_location
) ) ) {
179 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
180 selected
, nt_errstr(nt_status
)));
181 SAFE_FREE(module_name
);
185 SAFE_FREE(module_name
);
187 DEBUG(5,("pdb backend %s has a valid init\n", selected
));
192 /******************************************************************
193 Return an already initialised pdn_methods structure
194 *******************************************************************/
196 static struct pdb_methods
*pdb_get_methods_reload( BOOL reload
)
198 static struct pdb_methods
*pdb
= NULL
;
200 if ( pdb
&& reload
) {
201 pdb
->free_private_data( &(pdb
->private_data
) );
202 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
208 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
216 static struct pdb_methods
*pdb_get_methods(void)
218 return pdb_get_methods_reload(False
);
221 /******************************************************************
222 Backward compatibility functions for the original passdb interface
223 *******************************************************************/
225 BOOL
pdb_setsampwent(BOOL update
, uint16 acb_mask
)
227 struct pdb_methods
*pdb
= pdb_get_methods();
233 return NT_STATUS_IS_OK(pdb
->setsampwent(pdb
, update
, acb_mask
));
236 void pdb_endsampwent(void)
238 struct pdb_methods
*pdb
= pdb_get_methods();
244 pdb
->endsampwent(pdb
);
247 BOOL
pdb_getsampwent(struct samu
*user
)
249 struct pdb_methods
*pdb
= pdb_get_methods();
255 if ( !NT_STATUS_IS_OK(pdb
->getsampwent(pdb
, user
) ) ) {
259 pdb_force_pw_initialization( user
);
264 BOOL
pdb_getsampwnam(struct samu
*sam_acct
, const char *username
)
266 struct pdb_methods
*pdb
= pdb_get_methods();
272 if (!NT_STATUS_IS_OK(pdb
->getsampwnam(pdb
, sam_acct
, username
))) {
277 TALLOC_FREE(csamuser
);
280 pdb_force_pw_initialization( sam_acct
);
282 if ( (csamuser
= samu_new( NULL
)) != NULL
) {
283 pdb_copy_sam_account(csamuser
, sam_acct
);
289 /**********************************************************************
290 **********************************************************************/
292 BOOL
guest_user_info( struct samu
*user
)
296 const char *guestname
= lp_guestaccount();
298 if ( !(pwd
= getpwnam_alloc( NULL
, guestname
) ) ) {
299 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
304 result
= samu_set_unix(user
, pwd
);
308 return NT_STATUS_IS_OK( result
);
311 /**********************************************************************
312 **********************************************************************/
314 BOOL
pdb_getsampwsid(struct samu
*sam_acct
, const DOM_SID
*sid
)
316 struct pdb_methods
*pdb
;
319 if ( !(pdb
= pdb_get_methods()) ) {
323 /* hard code the Guest RID of 501 */
325 if ( !sid_peek_check_rid( get_global_sam_sid(), sid
, &rid
) )
328 if ( rid
== DOMAIN_USER_RID_GUEST
) {
329 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
330 return guest_user_info( sam_acct
);
333 /* check the cache first */
335 if ( csamuser
&& sid_equal(sid
, pdb_get_user_sid(csamuser
) ) )
336 return pdb_copy_sam_account(sam_acct
, csamuser
);
338 return NT_STATUS_IS_OK(pdb
->getsampwsid(pdb
, sam_acct
, sid
));
341 static NTSTATUS
pdb_default_create_user(struct pdb_methods
*methods
,
342 TALLOC_CTX
*tmp_ctx
, const char *name
,
343 uint32 acb_info
, uint32
*rid
)
345 struct samu
*sam_pass
;
349 if ((sam_pass
= samu_new(tmp_ctx
)) == NULL
) {
350 return NT_STATUS_NO_MEMORY
;
353 if ( !(pwd
= Get_Pwnam_alloc(tmp_ctx
, name
)) ) {
357 if ((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] != '$') {
358 pstrcpy(add_script
, lp_adduser_script());
360 pstrcpy(add_script
, lp_addmachine_script());
363 if (add_script
[0] == '\0') {
364 DEBUG(3, ("Could not find user %s and no add script "
366 return NT_STATUS_NO_SUCH_USER
;
369 all_string_sub(add_script
, "%u", name
, sizeof(add_script
));
370 add_ret
= smbrun(add_script
,NULL
);
371 DEBUG(add_ret
? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
372 add_script
, add_ret
));
375 pwd
= Get_Pwnam_alloc(tmp_ctx
, name
);
378 /* we have a valid SID coming out of this call */
380 status
= samu_alloc_rid_unix( sam_pass
, pwd
);
384 if (!NT_STATUS_IS_OK(status
)) {
385 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status
)));
389 if (!sid_peek_check_rid(get_global_sam_sid(),
390 pdb_get_user_sid(sam_pass
), rid
)) {
391 DEBUG(0, ("Could not get RID of fresh user\n"));
392 return NT_STATUS_INTERNAL_ERROR
;
395 /* Disable the account on creation, it does not have a reasonable password yet. */
397 acb_info
|= ACB_DISABLED
;
399 pdb_set_acct_ctrl(sam_pass
, acb_info
, PDB_CHANGED
);
401 status
= pdb_add_sam_account(sam_pass
);
403 TALLOC_FREE(sam_pass
);
408 NTSTATUS
pdb_create_user(TALLOC_CTX
*mem_ctx
, const char *name
, uint32 flags
,
411 struct pdb_methods
*pdb
= pdb_get_methods();
414 return NT_STATUS_UNSUCCESSFUL
;
417 return pdb
->create_user(pdb
, mem_ctx
, name
, flags
, rid
);
420 /****************************************************************************
421 Delete a UNIX user on demand.
422 ****************************************************************************/
424 static int smb_delete_user(const char *unix_user
)
429 pstrcpy(del_script
, lp_deluser_script());
432 all_string_sub(del_script
, "%u", unix_user
, sizeof(del_script
));
433 ret
= smbrun(del_script
,NULL
);
435 DEBUG(ret
? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script
,ret
));
440 static NTSTATUS
pdb_default_delete_user(struct pdb_methods
*methods
,
442 struct samu
*sam_acct
)
446 status
= pdb_delete_sam_account(sam_acct
);
447 if (!NT_STATUS_IS_OK(status
)) {
452 * Now delete the unix side ....
453 * note: we don't check if the delete really happened as the script is
454 * not necessary present and maybe the sysadmin doesn't want to delete
457 smb_delete_user( pdb_get_username(sam_acct
) );
462 NTSTATUS
pdb_delete_user(TALLOC_CTX
*mem_ctx
, struct samu
*sam_acct
)
464 struct pdb_methods
*pdb
= pdb_get_methods();
467 return NT_STATUS_UNSUCCESSFUL
;
470 return pdb
->delete_user(pdb
, mem_ctx
, sam_acct
);
473 NTSTATUS
pdb_add_sam_account(struct samu
*sam_acct
)
475 struct pdb_methods
*pdb
= pdb_get_methods();
478 return NT_STATUS_UNSUCCESSFUL
;
481 return pdb
->add_sam_account(pdb
, sam_acct
);
484 NTSTATUS
pdb_update_sam_account(struct samu
*sam_acct
)
486 struct pdb_methods
*pdb
= pdb_get_methods();
489 return NT_STATUS_UNSUCCESSFUL
;
492 if (csamuser
!= NULL
) {
493 TALLOC_FREE(csamuser
);
497 return pdb
->update_sam_account(pdb
, sam_acct
);
500 NTSTATUS
pdb_delete_sam_account(struct samu
*sam_acct
)
502 struct pdb_methods
*pdb
= pdb_get_methods();
505 return NT_STATUS_UNSUCCESSFUL
;
508 if (csamuser
!= NULL
) {
509 TALLOC_FREE(csamuser
);
513 return pdb
->delete_sam_account(pdb
, sam_acct
);
516 NTSTATUS
pdb_rename_sam_account(struct samu
*oldname
, const char *newname
)
518 struct pdb_methods
*pdb
= pdb_get_methods();
521 return NT_STATUS_NOT_IMPLEMENTED
;
524 if (csamuser
!= NULL
) {
525 TALLOC_FREE(csamuser
);
529 return pdb
->rename_sam_account(pdb
, oldname
, newname
);
532 NTSTATUS
pdb_update_login_attempts(struct samu
*sam_acct
, BOOL success
)
534 struct pdb_methods
*pdb
= pdb_get_methods();
537 return NT_STATUS_NOT_IMPLEMENTED
;
540 return pdb
->update_login_attempts(pdb
, sam_acct
, success
);
543 BOOL
pdb_getgrsid(GROUP_MAP
*map
, DOM_SID sid
)
545 struct pdb_methods
*pdb
= pdb_get_methods();
551 return NT_STATUS_IS_OK(pdb
->getgrsid(pdb
, map
, sid
));
554 BOOL
pdb_getgrgid(GROUP_MAP
*map
, gid_t gid
)
556 struct pdb_methods
*pdb
= pdb_get_methods();
562 return NT_STATUS_IS_OK(pdb
->getgrgid(pdb
, map
, gid
));
565 BOOL
pdb_getgrnam(GROUP_MAP
*map
, const char *name
)
567 struct pdb_methods
*pdb
= pdb_get_methods();
573 return NT_STATUS_IS_OK(pdb
->getgrnam(pdb
, map
, name
));
576 static NTSTATUS
pdb_default_create_dom_group(struct pdb_methods
*methods
,
584 grp
= getgrnam(name
);
589 if (smb_create_group(name
, &gid
) != 0) {
590 return NT_STATUS_ACCESS_DENIED
;
597 return NT_STATUS_ACCESS_DENIED
;
600 if (pdb_rid_algorithm()) {
601 *rid
= pdb_gid_to_group_rid( grp
->gr_gid
);
603 if (!pdb_new_rid(rid
)) {
604 return NT_STATUS_ACCESS_DENIED
;
608 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
610 return add_initial_entry(grp
->gr_gid
, sid_string_static(&group_sid
),
611 SID_NAME_DOM_GRP
, name
, NULL
);
614 NTSTATUS
pdb_create_dom_group(TALLOC_CTX
*mem_ctx
, const char *name
,
617 struct pdb_methods
*pdb
= pdb_get_methods();
620 return NT_STATUS_UNSUCCESSFUL
;
623 return pdb
->create_dom_group(pdb
, mem_ctx
, name
, rid
);
626 static NTSTATUS
pdb_default_delete_dom_group(struct pdb_methods
*methods
,
634 const char *grp_name
;
636 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
638 if (!get_domain_group_from_sid(group_sid
, &map
)) {
639 DEBUG(10, ("Could not find group for rid %d\n", rid
));
640 return NT_STATUS_NO_SUCH_GROUP
;
643 /* We need the group name for the smb_delete_group later on */
645 if (map
.gid
== (gid_t
)-1) {
646 return NT_STATUS_NO_SUCH_GROUP
;
649 grp
= getgrgid(map
.gid
);
651 return NT_STATUS_NO_SUCH_GROUP
;
654 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
656 grp_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
657 if (grp_name
== NULL
) {
658 return NT_STATUS_NO_MEMORY
;
661 status
= pdb_delete_group_mapping_entry(group_sid
);
663 if (!NT_STATUS_IS_OK(status
)) {
667 /* Don't check the result of smb_delete_group */
669 smb_delete_group(grp_name
);
674 NTSTATUS
pdb_delete_dom_group(TALLOC_CTX
*mem_ctx
, uint32 rid
)
676 struct pdb_methods
*pdb
= pdb_get_methods();
679 return NT_STATUS_UNSUCCESSFUL
;
682 return pdb
->delete_dom_group(pdb
, mem_ctx
, rid
);
685 NTSTATUS
pdb_add_group_mapping_entry(GROUP_MAP
*map
)
687 struct pdb_methods
*pdb
= pdb_get_methods();
690 return NT_STATUS_UNSUCCESSFUL
;
693 return pdb
->add_group_mapping_entry(pdb
, map
);
696 NTSTATUS
pdb_update_group_mapping_entry(GROUP_MAP
*map
)
698 struct pdb_methods
*pdb
= pdb_get_methods();
701 return NT_STATUS_UNSUCCESSFUL
;
704 return pdb
->update_group_mapping_entry(pdb
, map
);
707 NTSTATUS
pdb_delete_group_mapping_entry(DOM_SID sid
)
709 struct pdb_methods
*pdb
= pdb_get_methods();
712 return NT_STATUS_UNSUCCESSFUL
;
715 return pdb
->delete_group_mapping_entry(pdb
, sid
);
718 BOOL
pdb_enum_group_mapping(const DOM_SID
*sid
, enum SID_NAME_USE sid_name_use
, GROUP_MAP
**pp_rmap
,
719 size_t *p_num_entries
, BOOL unix_only
)
721 struct pdb_methods
*pdb
= pdb_get_methods();
727 return NT_STATUS_IS_OK(pdb
-> enum_group_mapping(pdb
, sid
, sid_name_use
,
728 pp_rmap
, p_num_entries
, unix_only
));
731 NTSTATUS
pdb_enum_group_members(TALLOC_CTX
*mem_ctx
,
733 uint32
**pp_member_rids
,
734 size_t *p_num_members
)
736 struct pdb_methods
*pdb
= pdb_get_methods();
740 return NT_STATUS_UNSUCCESSFUL
;
743 result
= pdb
->enum_group_members(pdb
, mem_ctx
,
744 sid
, pp_member_rids
, p_num_members
);
746 /* special check for rid 513 */
748 if ( !NT_STATUS_IS_OK( result
) ) {
751 sid_peek_rid( sid
, &rid
);
753 if ( rid
== DOMAIN_GROUP_RID_USERS
) {
755 *pp_member_rids
= NULL
;
764 NTSTATUS
pdb_enum_group_memberships(TALLOC_CTX
*mem_ctx
, struct samu
*user
,
765 DOM_SID
**pp_sids
, gid_t
**pp_gids
,
766 size_t *p_num_groups
)
768 struct pdb_methods
*pdb
= pdb_get_methods();
771 return NT_STATUS_UNSUCCESSFUL
;
774 return pdb
->enum_group_memberships(
776 pp_sids
, pp_gids
, p_num_groups
);
779 static NTSTATUS
pdb_default_set_unix_primary_group(struct pdb_methods
*methods
,
781 struct samu
*sampass
)
786 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
) ||
787 (grp
= getgrgid(gid
)) == NULL
) {
788 return NT_STATUS_INVALID_PRIMARY_GROUP
;
791 if (smb_set_primary_group(grp
->gr_name
,
792 pdb_get_username(sampass
)) != 0) {
793 return NT_STATUS_ACCESS_DENIED
;
799 NTSTATUS
pdb_set_unix_primary_group(TALLOC_CTX
*mem_ctx
, struct samu
*user
)
801 struct pdb_methods
*pdb
= pdb_get_methods();
804 return NT_STATUS_UNSUCCESSFUL
;
807 return pdb
->set_unix_primary_group(pdb
, mem_ctx
, user
);
811 * Helper function to see whether a user is in a group. We can't use
812 * user_in_group_sid here because this creates dependencies only smbd can
816 static BOOL
pdb_user_in_group(TALLOC_CTX
*mem_ctx
, struct samu
*account
,
817 const DOM_SID
*group_sid
)
821 size_t i
, num_groups
;
823 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx
, account
,
829 for (i
=0; i
<num_groups
; i
++) {
830 if (sid_equal(group_sid
, &sids
[i
])) {
837 static NTSTATUS
pdb_default_add_groupmem(struct pdb_methods
*methods
,
842 DOM_SID group_sid
, member_sid
;
843 struct samu
*account
= NULL
;
847 const char *group_name
;
850 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
851 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
853 if (!get_domain_group_from_sid(group_sid
, &map
) ||
854 (map
.gid
== (gid_t
)-1) ||
855 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
856 return NT_STATUS_NO_SUCH_GROUP
;
859 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
860 if (group_name
== NULL
) {
861 return NT_STATUS_NO_MEMORY
;
864 if ( !(account
= samu_new( NULL
)) ) {
865 return NT_STATUS_NO_MEMORY
;
868 if (!pdb_getsampwsid(account
, &member_sid
) ||
869 !sid_to_uid(&member_sid
, &uid
) ||
870 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
871 return NT_STATUS_NO_SUCH_USER
;
874 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
875 return NT_STATUS_MEMBER_IN_GROUP
;
879 * ok, the group exist, the user exist, the user is not in the group,
880 * we can (finally) add it to the group !
883 smb_add_user_group(group_name
, pwd
->pw_name
);
885 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
886 return NT_STATUS_ACCESS_DENIED
;
892 NTSTATUS
pdb_add_groupmem(TALLOC_CTX
*mem_ctx
, uint32 group_rid
,
895 struct pdb_methods
*pdb
= pdb_get_methods();
898 return NT_STATUS_UNSUCCESSFUL
;
901 return pdb
->add_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
904 static NTSTATUS
pdb_default_del_groupmem(struct pdb_methods
*methods
,
909 DOM_SID group_sid
, member_sid
;
910 struct samu
*account
= NULL
;
914 const char *group_name
;
917 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
918 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
920 if (!get_domain_group_from_sid(group_sid
, &map
) ||
921 (map
.gid
== (gid_t
)-1) ||
922 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
923 return NT_STATUS_NO_SUCH_GROUP
;
926 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
927 if (group_name
== NULL
) {
928 return NT_STATUS_NO_MEMORY
;
931 if ( !(account
= samu_new( NULL
)) ) {
932 return NT_STATUS_NO_MEMORY
;
935 if (!pdb_getsampwsid(account
, &member_sid
) ||
936 !sid_to_uid(&member_sid
, &uid
) ||
937 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
938 return NT_STATUS_NO_SUCH_USER
;
941 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
942 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
946 * ok, the group exist, the user exist, the user is in the group,
947 * we can (finally) delete it from the group!
950 smb_delete_user_group(group_name
, pwd
->pw_name
);
952 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
953 return NT_STATUS_ACCESS_DENIED
;
959 NTSTATUS
pdb_del_groupmem(TALLOC_CTX
*mem_ctx
, uint32 group_rid
,
962 struct pdb_methods
*pdb
= pdb_get_methods();
965 return NT_STATUS_UNSUCCESSFUL
;
968 return pdb
->del_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
971 BOOL
pdb_find_alias(const char *name
, DOM_SID
*sid
)
973 struct pdb_methods
*pdb
= pdb_get_methods();
979 return NT_STATUS_IS_OK(pdb
->find_alias(pdb
,
983 NTSTATUS
pdb_create_alias(const char *name
, uint32
*rid
)
985 struct pdb_methods
*pdb
= pdb_get_methods();
988 return NT_STATUS_NOT_IMPLEMENTED
;
991 return pdb
->create_alias(pdb
, name
, rid
);
994 BOOL
pdb_delete_alias(const DOM_SID
*sid
)
996 struct pdb_methods
*pdb
= pdb_get_methods();
1002 return NT_STATUS_IS_OK(pdb
->delete_alias(pdb
,
1007 BOOL
pdb_get_aliasinfo(const DOM_SID
*sid
, struct acct_info
*info
)
1009 struct pdb_methods
*pdb
= pdb_get_methods();
1015 return NT_STATUS_IS_OK(pdb
->get_aliasinfo(pdb
, sid
,
1019 BOOL
pdb_set_aliasinfo(const DOM_SID
*sid
, struct acct_info
*info
)
1021 struct pdb_methods
*pdb
= pdb_get_methods();
1027 return NT_STATUS_IS_OK(pdb
->set_aliasinfo(pdb
, sid
,
1031 NTSTATUS
pdb_add_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
1033 struct pdb_methods
*pdb
= pdb_get_methods();
1036 return NT_STATUS_UNSUCCESSFUL
;
1039 return pdb
->add_aliasmem(pdb
, alias
, member
);
1042 NTSTATUS
pdb_del_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
1044 struct pdb_methods
*pdb
= pdb_get_methods();
1047 return NT_STATUS_UNSUCCESSFUL
;
1050 return pdb
->del_aliasmem(pdb
, alias
, member
);
1053 NTSTATUS
pdb_enum_aliasmem(const DOM_SID
*alias
,
1054 DOM_SID
**pp_members
, size_t *p_num_members
)
1056 struct pdb_methods
*pdb
= pdb_get_methods();
1059 return NT_STATUS_UNSUCCESSFUL
;
1062 return pdb
->enum_aliasmem(pdb
, alias
,
1063 pp_members
, p_num_members
);
1066 NTSTATUS
pdb_enum_alias_memberships(TALLOC_CTX
*mem_ctx
,
1067 const DOM_SID
*domain_sid
,
1068 const DOM_SID
*members
, size_t num_members
,
1069 uint32
**pp_alias_rids
,
1070 size_t *p_num_alias_rids
)
1072 struct pdb_methods
*pdb
= pdb_get_methods();
1075 return NT_STATUS_NOT_IMPLEMENTED
;
1078 return pdb
->enum_alias_memberships(pdb
, mem_ctx
,
1080 members
, num_members
,
1085 NTSTATUS
pdb_lookup_rids(const DOM_SID
*domain_sid
,
1091 struct pdb_methods
*pdb
= pdb_get_methods();
1094 return NT_STATUS_NOT_IMPLEMENTED
;
1097 return pdb
->lookup_rids(pdb
, domain_sid
,
1098 num_rids
, rids
, names
, attrs
);
1101 NTSTATUS
pdb_lookup_names(const DOM_SID
*domain_sid
,
1107 struct pdb_methods
*pdb
= pdb_get_methods();
1110 return NT_STATUS_NOT_IMPLEMENTED
;
1113 return pdb
->lookup_names(pdb
, domain_sid
,
1114 num_names
, names
, rids
, attrs
);
1117 BOOL
pdb_get_account_policy(int policy_index
, uint32
*value
)
1119 struct pdb_methods
*pdb
= pdb_get_methods();
1125 return NT_STATUS_IS_OK(pdb
->get_account_policy(pdb
, policy_index
, value
));
1128 BOOL
pdb_set_account_policy(int policy_index
, uint32 value
)
1130 struct pdb_methods
*pdb
= pdb_get_methods();
1136 return NT_STATUS_IS_OK(pdb
->set_account_policy(pdb
, policy_index
, value
));
1139 BOOL
pdb_get_seq_num(time_t *seq_num
)
1141 struct pdb_methods
*pdb
= pdb_get_methods();
1147 return NT_STATUS_IS_OK(pdb
->get_seq_num(pdb
, seq_num
));
1150 BOOL
pdb_uid_to_rid(uid_t uid
, uint32
*rid
)
1152 struct pdb_methods
*pdb
= pdb_get_methods();
1158 return pdb
->uid_to_rid(pdb
, uid
, rid
);
1161 BOOL
pdb_gid_to_sid(gid_t gid
, DOM_SID
*sid
)
1163 struct pdb_methods
*pdb
= pdb_get_methods();
1169 return pdb
->gid_to_sid(pdb
, gid
, sid
);
1172 BOOL
pdb_sid_to_id(const DOM_SID
*sid
, union unid_t
*id
,
1173 enum SID_NAME_USE
*type
)
1175 struct pdb_methods
*pdb
= pdb_get_methods();
1181 return pdb
->sid_to_id(pdb
, sid
, id
, type
);
1184 BOOL
pdb_rid_algorithm(void)
1186 struct pdb_methods
*pdb
= pdb_get_methods();
1192 return pdb
->rid_algorithm(pdb
);
1195 BOOL
pdb_new_rid(uint32
*rid
)
1197 struct pdb_methods
*pdb
= pdb_get_methods();
1203 if (pdb_rid_algorithm()) {
1204 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1209 if (algorithmic_rid_base() != BASE_RID
) {
1210 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1211 "without algorithmic RIDs is chosen.\n"));
1212 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1213 "add', set the maximum used RID using\n"));
1214 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1218 return pdb
->new_rid(pdb
, rid
);
1221 /***************************************************************
1222 Initialize the static context (at smbd startup etc).
1224 If uninitialised, context will auto-init on first use.
1225 ***************************************************************/
1227 BOOL
initialize_password_db(BOOL reload
)
1229 return (pdb_get_methods_reload(reload
) != NULL
);
1233 /***************************************************************************
1234 Default implementations of some functions.
1235 ****************************************************************************/
1237 static NTSTATUS
pdb_default_getsampwnam (struct pdb_methods
*methods
, struct samu
*user
, const char *sname
)
1239 return NT_STATUS_NO_SUCH_USER
;
1242 static NTSTATUS
pdb_default_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1244 return NT_STATUS_NO_SUCH_USER
;
1247 static NTSTATUS
pdb_default_add_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1249 DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods
->name
));
1250 return NT_STATUS_NOT_IMPLEMENTED
;
1253 static NTSTATUS
pdb_default_update_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1255 return NT_STATUS_NOT_IMPLEMENTED
;
1258 static NTSTATUS
pdb_default_delete_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
)
1260 return NT_STATUS_NOT_IMPLEMENTED
;
1263 static NTSTATUS
pdb_default_rename_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
, const char *newname
)
1265 return NT_STATUS_NOT_IMPLEMENTED
;
1268 static NTSTATUS
pdb_default_update_login_attempts (struct pdb_methods
*methods
, struct samu
*newpwd
, BOOL success
)
1270 return NT_STATUS_OK
;
1273 static NTSTATUS
pdb_default_setsampwent(struct pdb_methods
*methods
, BOOL update
, uint32 acb_mask
)
1275 return NT_STATUS_NOT_IMPLEMENTED
;
1278 static NTSTATUS
pdb_default_getsampwent(struct pdb_methods
*methods
, struct samu
*user
)
1280 return NT_STATUS_NOT_IMPLEMENTED
;
1283 static void pdb_default_endsampwent(struct pdb_methods
*methods
)
1285 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1288 static NTSTATUS
pdb_default_get_account_policy(struct pdb_methods
*methods
, int policy_index
, uint32
*value
)
1290 return account_policy_get(policy_index
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1293 static NTSTATUS
pdb_default_set_account_policy(struct pdb_methods
*methods
, int policy_index
, uint32 value
)
1295 return account_policy_set(policy_index
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1298 static NTSTATUS
pdb_default_get_seq_num(struct pdb_methods
*methods
, time_t *seq_num
)
1300 *seq_num
= time(NULL
);
1301 return NT_STATUS_OK
;
1304 static BOOL
pdb_default_uid_to_rid(struct pdb_methods
*methods
, uid_t uid
,
1307 struct samu
*sampw
= NULL
;
1308 struct passwd
*unix_pw
;
1311 unix_pw
= sys_getpwuid( uid
);
1314 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1315 "%lu\n", (unsigned long)uid
));
1319 if ( !(sampw
= samu_new( NULL
)) ) {
1320 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1325 ret
= NT_STATUS_IS_OK(
1326 methods
->getsampwnam(methods
, sampw
, unix_pw
->pw_name
));
1330 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1331 "%s (%d)\n", unix_pw
->pw_name
, uid
));
1336 ret
= sid_peek_check_rid(get_global_sam_sid(),
1337 pdb_get_user_sid(sampw
), rid
);
1340 DEBUG(1, ("Could not peek rid out of sid %s\n",
1341 sid_string_static(pdb_get_user_sid(sampw
))));
1348 static BOOL
pdb_default_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
1353 if (!NT_STATUS_IS_OK(methods
->getgrgid(methods
, &map
, gid
))) {
1357 sid_copy(sid
, &map
.sid
);
1361 static BOOL
pdb_default_sid_to_id(struct pdb_methods
*methods
,
1363 union unid_t
*id
, enum SID_NAME_USE
*type
)
1365 TALLOC_CTX
*mem_ctx
;
1370 mem_ctx
= talloc_new(NULL
);
1372 if (mem_ctx
== NULL
) {
1373 DEBUG(0, ("talloc_new failed\n"));
1377 if (sid_peek_check_rid(get_global_sam_sid(), sid
, &rid
)) {
1378 /* Here we might have users as well as groups and aliases */
1379 ret
= lookup_global_sam_rid(mem_ctx
, rid
, &name
, type
, id
);
1383 if (sid_peek_check_rid(&global_sid_Builtin
, sid
, &rid
)) {
1384 /* Here we only have aliases */
1386 if (!NT_STATUS_IS_OK(methods
->getgrsid(methods
, &map
, *sid
))) {
1387 DEBUG(10, ("Could not find map for sid %s\n",
1388 sid_string_static(sid
)));
1391 if ((map
.sid_name_use
!= SID_NAME_ALIAS
) &&
1392 (map
.sid_name_use
!= SID_NAME_WKN_GRP
)) {
1393 DEBUG(10, ("Map for sid %s is a %s, expected an "
1394 "alias\n", sid_string_static(sid
),
1395 sid_type_lookup(map
.sid_name_use
)));
1400 *type
= SID_NAME_ALIAS
;
1405 DEBUG(5, ("Sid %s is neither ours nor builtin, don't know it\n",
1406 sid_string_static(sid
)));
1410 TALLOC_FREE(mem_ctx
);
1414 static void add_uid_to_array_unique(TALLOC_CTX
*mem_ctx
,
1415 uid_t uid
, uid_t
**pp_uids
, size_t *p_num
)
1419 for (i
=0; i
<*p_num
; i
++) {
1420 if ((*pp_uids
)[i
] == uid
)
1424 *pp_uids
= TALLOC_REALLOC_ARRAY(mem_ctx
, *pp_uids
, uid_t
, *p_num
+1);
1426 if (*pp_uids
== NULL
)
1429 (*pp_uids
)[*p_num
] = uid
;
1433 static BOOL
get_memberuids(TALLOC_CTX
*mem_ctx
, gid_t gid
, uid_t
**pp_uids
, size_t *p_num
)
1442 /* We only look at our own sam, so don't care about imported stuff */
1446 if ((grp
= getgrgid(gid
)) == NULL
) {
1451 /* Primary group members */
1454 while ((pwd
= getpwent()) != NULL
) {
1455 if (pwd
->pw_gid
== gid
) {
1456 add_uid_to_array_unique(mem_ctx
, pwd
->pw_uid
,
1462 /* Secondary group members */
1464 for (gr
= grp
->gr_mem
; (*gr
!= NULL
) && ((*gr
)[0] != '\0'); gr
+= 1) {
1465 struct passwd
*pw
= getpwnam(*gr
);
1469 add_uid_to_array_unique(mem_ctx
, pw
->pw_uid
, pp_uids
, p_num
);
1477 NTSTATUS
pdb_default_enum_group_members(struct pdb_methods
*methods
,
1478 TALLOC_CTX
*mem_ctx
,
1479 const DOM_SID
*group
,
1480 uint32
**pp_member_rids
,
1481 size_t *p_num_members
)
1487 *pp_member_rids
= NULL
;
1490 if (!sid_to_gid(group
, &gid
))
1491 return NT_STATUS_NO_SUCH_GROUP
;
1493 if(!get_memberuids(mem_ctx
, gid
, &uids
, &num_uids
))
1494 return NT_STATUS_NO_SUCH_GROUP
;
1497 return NT_STATUS_OK
;
1499 *pp_member_rids
= TALLOC_ZERO_ARRAY(mem_ctx
, uint32
, num_uids
);
1501 for (i
=0; i
<num_uids
; i
++) {
1504 uid_to_sid(&sid
, uids
[i
]);
1506 if (!sid_check_is_in_our_domain(&sid
)) {
1507 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1508 "in our domain\n"));
1512 sid_peek_rid(&sid
, &(*pp_member_rids
)[*p_num_members
]);
1513 *p_num_members
+= 1;
1516 return NT_STATUS_OK
;
1519 NTSTATUS
pdb_default_enum_group_memberships(struct pdb_methods
*methods
,
1520 TALLOC_CTX
*mem_ctx
,
1524 size_t *p_num_groups
)
1529 const char *username
= pdb_get_username(user
);
1533 /* Ignore the primary group SID. Honor the real Unix primary group.
1534 The primary group SID is only of real use to Windows clients */
1536 if (!sid_to_gid(pdb_get_group_sid(user
), &gid
)) {
1537 DEBUG(10, ("sid_to_gid failed\n"));
1538 return NT_STATUS_NO_SUCH_USER
;
1541 if ( !(pw
= getpwnam_alloc(mem_ctx
, username
)) ) {
1542 return NT_STATUS_NO_SUCH_USER
;
1550 if (!getgroups_unix_user(mem_ctx
, username
, gid
, pp_gids
, p_num_groups
)) {
1551 return NT_STATUS_NO_SUCH_USER
;
1554 if (*p_num_groups
== 0) {
1555 smb_panic("primary group missing");
1558 *pp_sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, *p_num_groups
);
1560 if (*pp_sids
== NULL
) {
1561 TALLOC_FREE(*pp_gids
);
1562 return NT_STATUS_NO_MEMORY
;
1565 for (i
=0; i
<*p_num_groups
; i
++) {
1566 gid_to_sid(&(*pp_sids
)[i
], (*pp_gids
)[i
]);
1569 return NT_STATUS_OK
;
1572 /*******************************************************************
1573 Look up a rid in the SAM we're responsible for (i.e. passdb)
1574 ********************************************************************/
1576 static BOOL
lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32 rid
,
1578 enum SID_NAME_USE
*psid_name_use
,
1579 union unid_t
*unix_id
)
1581 struct samu
*sam_account
= NULL
;
1586 *psid_name_use
= SID_NAME_UNKNOWN
;
1588 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1589 (unsigned int)rid
));
1591 sid_copy(&sid
, get_global_sam_sid());
1592 sid_append_rid(&sid
, rid
);
1594 /* see if the passdb can help us with the name of the user */
1596 if ( !(sam_account
= samu_new( NULL
)) ) {
1600 /* BEING ROOT BLLOCK */
1602 if (pdb_getsampwsid(sam_account
, &sid
)) {
1605 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1606 *name
= talloc_strdup(mem_ctx
, pdb_get_username(sam_account
));
1607 *psid_name_use
= SID_NAME_USER
;
1609 TALLOC_FREE(sam_account
);
1611 if (unix_id
== NULL
) {
1615 pw
= Get_Pwnam(*name
);
1619 unix_id
->uid
= pw
->pw_uid
;
1622 TALLOC_FREE(sam_account
);
1624 ret
= pdb_getgrsid(&map
, sid
);
1626 /* END BECOME_ROOT BLOCK */
1628 /* do not resolve SIDs to a name unless there is a valid
1629 gid associated with it */
1631 if ( ret
&& (map
.gid
!= (gid_t
)-1) ) {
1632 *name
= talloc_strdup(mem_ctx
, map
.nt_name
);
1633 *psid_name_use
= map
.sid_name_use
;
1636 unix_id
->gid
= map
.gid
;
1642 /* Windows will always map RID 513 to something. On a non-domain
1643 controller, this gets mapped to SERVER\None. */
1646 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1650 if ( rid
== DOMAIN_GROUP_RID_USERS
) {
1651 *name
= talloc_strdup(mem_ctx
, "None" );
1652 *psid_name_use
= SID_NAME_DOM_GRP
;
1660 NTSTATUS
pdb_default_lookup_rids(struct pdb_methods
*methods
,
1661 const DOM_SID
*domain_sid
,
1669 BOOL have_mapped
= False
;
1670 BOOL have_unmapped
= False
;
1672 if (sid_check_is_builtin(domain_sid
)) {
1674 for (i
=0; i
<num_rids
; i
++) {
1677 if (lookup_builtin_rid(names
, rids
[i
], &name
)) {
1678 attrs
[i
] = SID_NAME_ALIAS
;
1680 DEBUG(5,("lookup_rids: %s:%d\n",
1681 names
[i
], attrs
[i
]));
1684 have_unmapped
= True
;
1685 attrs
[i
] = SID_NAME_UNKNOWN
;
1691 /* Should not happen, but better check once too many */
1692 if (!sid_check_is_domain(domain_sid
)) {
1693 return NT_STATUS_INVALID_HANDLE
;
1696 for (i
= 0; i
< num_rids
; i
++) {
1699 if (lookup_global_sam_rid(names
, rids
[i
], &name
, &attrs
[i
],
1702 DEBUG(5,("lookup_rids: %s:%d\n", names
[i
], attrs
[i
]));
1705 have_unmapped
= True
;
1706 attrs
[i
] = SID_NAME_UNKNOWN
;
1712 result
= NT_STATUS_NONE_MAPPED
;
1715 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1720 NTSTATUS
pdb_default_lookup_names(struct pdb_methods
*methods
,
1721 const DOM_SID
*domain_sid
,
1729 BOOL have_mapped
= False
;
1730 BOOL have_unmapped
= False
;
1732 if (sid_check_is_builtin(domain_sid
)) {
1734 for (i
=0; i
<num_names
; i
++) {
1737 if (lookup_builtin_name(names
[i
], &rid
)) {
1738 attrs
[i
] = SID_NAME_ALIAS
;
1740 DEBUG(5,("lookup_rids: %s:%d\n",
1741 names
[i
], attrs
[i
]));
1744 have_unmapped
= True
;
1745 attrs
[i
] = SID_NAME_UNKNOWN
;
1751 /* Should not happen, but better check once too many */
1752 if (!sid_check_is_domain(domain_sid
)) {
1753 return NT_STATUS_INVALID_HANDLE
;
1756 for (i
= 0; i
< num_names
; i
++) {
1757 if (lookup_global_sam_name(names
[i
], 0, &rids
[i
], &attrs
[i
])) {
1758 DEBUG(5,("lookup_names: %s-> %d:%d\n", names
[i
],
1759 rids
[i
], attrs
[i
]));
1762 have_unmapped
= True
;
1763 attrs
[i
] = SID_NAME_UNKNOWN
;
1769 result
= NT_STATUS_NONE_MAPPED
;
1772 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1777 static struct pdb_search
*pdb_search_init(enum pdb_search_type type
)
1779 TALLOC_CTX
*mem_ctx
;
1780 struct pdb_search
*result
;
1782 mem_ctx
= talloc_init("pdb_search");
1783 if (mem_ctx
== NULL
) {
1784 DEBUG(0, ("talloc_init failed\n"));
1788 result
= TALLOC_P(mem_ctx
, struct pdb_search
);
1789 if (result
== NULL
) {
1790 DEBUG(0, ("talloc failed\n"));
1794 result
->mem_ctx
= mem_ctx
;
1795 result
->type
= type
;
1796 result
->cache
= NULL
;
1797 result
->num_entries
= 0;
1798 result
->cache_size
= 0;
1799 result
->search_ended
= False
;
1801 /* Segfault appropriately if not initialized */
1802 result
->next_entry
= NULL
;
1803 result
->search_end
= NULL
;
1808 static void fill_displayentry(TALLOC_CTX
*mem_ctx
, uint32 rid
,
1810 const char *account_name
,
1811 const char *fullname
,
1812 const char *description
,
1813 struct samr_displayentry
*entry
)
1816 entry
->acct_flags
= acct_flags
;
1818 if (account_name
!= NULL
)
1819 entry
->account_name
= talloc_strdup(mem_ctx
, account_name
);
1821 entry
->account_name
= "";
1823 if (fullname
!= NULL
)
1824 entry
->fullname
= talloc_strdup(mem_ctx
, fullname
);
1826 entry
->fullname
= "";
1828 if (description
!= NULL
)
1829 entry
->description
= talloc_strdup(mem_ctx
, description
);
1831 entry
->description
= "";
1834 static BOOL user_search_in_progress
= False
;
1835 struct user_search
{
1839 static BOOL
next_entry_users(struct pdb_search
*s
,
1840 struct samr_displayentry
*entry
)
1842 struct user_search
*state
= s
->private_data
;
1843 struct samu
*user
= NULL
;
1846 if ( !(user
= samu_new( NULL
)) ) {
1847 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1851 if (!pdb_getsampwent(user
)) {
1856 if ((state
->acct_flags
!= 0) &&
1857 ((pdb_get_acct_ctrl(user
) & state
->acct_flags
) == 0)) {
1862 fill_displayentry(s
->mem_ctx
, pdb_get_user_rid(user
),
1863 pdb_get_acct_ctrl(user
), pdb_get_username(user
),
1864 pdb_get_fullname(user
), pdb_get_acct_desc(user
),
1871 static void search_end_users(struct pdb_search
*search
)
1874 user_search_in_progress
= False
;
1877 static BOOL
pdb_default_search_users(struct pdb_methods
*methods
,
1878 struct pdb_search
*search
,
1881 struct user_search
*state
;
1883 if (user_search_in_progress
) {
1884 DEBUG(1, ("user search in progress\n"));
1888 if (!pdb_setsampwent(False
, acct_flags
)) {
1889 DEBUG(5, ("Could not start search\n"));
1893 user_search_in_progress
= True
;
1895 state
= TALLOC_P(search
->mem_ctx
, struct user_search
);
1896 if (state
== NULL
) {
1897 DEBUG(0, ("talloc failed\n"));
1901 state
->acct_flags
= acct_flags
;
1903 search
->private_data
= state
;
1904 search
->next_entry
= next_entry_users
;
1905 search
->search_end
= search_end_users
;
1909 struct group_search
{
1911 size_t num_groups
, current_group
;
1914 static BOOL
next_entry_groups(struct pdb_search
*s
,
1915 struct samr_displayentry
*entry
)
1917 struct group_search
*state
= s
->private_data
;
1919 GROUP_MAP
*map
= &state
->groups
[state
->current_group
];
1921 if (state
->current_group
== state
->num_groups
)
1924 sid_peek_rid(&map
->sid
, &rid
);
1926 fill_displayentry(s
->mem_ctx
, rid
, 0, map
->nt_name
, NULL
, map
->comment
,
1929 state
->current_group
+= 1;
1933 static void search_end_groups(struct pdb_search
*search
)
1935 struct group_search
*state
= search
->private_data
;
1936 SAFE_FREE(state
->groups
);
1939 static BOOL
pdb_search_grouptype(struct pdb_search
*search
,
1940 const DOM_SID
*sid
, enum SID_NAME_USE type
)
1942 struct group_search
*state
;
1944 state
= TALLOC_P(search
->mem_ctx
, struct group_search
);
1945 if (state
== NULL
) {
1946 DEBUG(0, ("talloc failed\n"));
1950 if (!pdb_enum_group_mapping(sid
, type
, &state
->groups
, &state
->num_groups
,
1952 DEBUG(0, ("Could not enum groups\n"));
1956 state
->current_group
= 0;
1957 search
->private_data
= state
;
1958 search
->next_entry
= next_entry_groups
;
1959 search
->search_end
= search_end_groups
;
1963 static BOOL
pdb_default_search_groups(struct pdb_methods
*methods
,
1964 struct pdb_search
*search
)
1966 return pdb_search_grouptype(search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
1969 static BOOL
pdb_default_search_aliases(struct pdb_methods
*methods
,
1970 struct pdb_search
*search
,
1974 return pdb_search_grouptype(search
, sid
, SID_NAME_ALIAS
);
1977 static struct samr_displayentry
*pdb_search_getentry(struct pdb_search
*search
,
1980 if (idx
< search
->num_entries
)
1981 return &search
->cache
[idx
];
1983 if (search
->search_ended
)
1986 while (idx
>= search
->num_entries
) {
1987 struct samr_displayentry entry
;
1989 if (!search
->next_entry(search
, &entry
)) {
1990 search
->search_end(search
);
1991 search
->search_ended
= True
;
1995 ADD_TO_LARGE_ARRAY(search
->mem_ctx
, struct samr_displayentry
,
1996 entry
, &search
->cache
, &search
->num_entries
,
1997 &search
->cache_size
);
2000 return (search
->num_entries
> idx
) ? &search
->cache
[idx
] : NULL
;
2003 struct pdb_search
*pdb_search_users(uint32 acct_flags
)
2005 struct pdb_methods
*pdb
= pdb_get_methods();
2006 struct pdb_search
*result
;
2008 if (pdb
== NULL
) return NULL
;
2010 result
= pdb_search_init(PDB_USER_SEARCH
);
2011 if (result
== NULL
) return NULL
;
2013 if (!pdb
->search_users(pdb
, result
, acct_flags
)) {
2014 talloc_destroy(result
->mem_ctx
);
2020 struct pdb_search
*pdb_search_groups(void)
2022 struct pdb_methods
*pdb
= pdb_get_methods();
2023 struct pdb_search
*result
;
2025 if (pdb
== NULL
) return NULL
;
2027 result
= pdb_search_init(PDB_GROUP_SEARCH
);
2028 if (result
== NULL
) return NULL
;
2030 if (!pdb
->search_groups(pdb
, result
)) {
2031 talloc_destroy(result
->mem_ctx
);
2037 struct pdb_search
*pdb_search_aliases(const DOM_SID
*sid
)
2039 struct pdb_methods
*pdb
= pdb_get_methods();
2040 struct pdb_search
*result
;
2042 if (pdb
== NULL
) return NULL
;
2044 result
= pdb_search_init(PDB_ALIAS_SEARCH
);
2045 if (result
== NULL
) return NULL
;
2047 if (!pdb
->search_aliases(pdb
, result
, sid
)) {
2048 talloc_destroy(result
->mem_ctx
);
2054 uint32
pdb_search_entries(struct pdb_search
*search
,
2055 uint32 start_idx
, uint32 max_entries
,
2056 struct samr_displayentry
**result
)
2058 struct samr_displayentry
*end_entry
;
2059 uint32 end_idx
= start_idx
+max_entries
-1;
2061 /* The first entry needs to be searched after the last. Otherwise the
2062 * first entry might have moved due to a realloc during the search for
2063 * the last entry. */
2065 end_entry
= pdb_search_getentry(search
, end_idx
);
2066 *result
= pdb_search_getentry(search
, start_idx
);
2068 if (end_entry
!= NULL
)
2071 if (start_idx
>= search
->num_entries
)
2074 return search
->num_entries
- start_idx
;
2077 void pdb_search_destroy(struct pdb_search
*search
)
2082 if (!search
->search_ended
)
2083 search
->search_end(search
);
2085 talloc_destroy(search
->mem_ctx
);
2088 /*******************************************************************
2089 Create a pdb_methods structure and initialize it with the default
2090 operations. In this way a passdb module can simply implement
2091 the functionality it cares about. However, normally this is done
2092 in groups of related functions.
2093 *******************************************************************/
2095 NTSTATUS
make_pdb_method( struct pdb_methods
**methods
)
2097 /* allocate memory for the structure as its own talloc CTX */
2099 if ( !(*methods
= TALLOC_ZERO_P(NULL
, struct pdb_methods
) ) ) {
2100 return NT_STATUS_NO_MEMORY
;
2103 (*methods
)->setsampwent
= pdb_default_setsampwent
;
2104 (*methods
)->endsampwent
= pdb_default_endsampwent
;
2105 (*methods
)->getsampwent
= pdb_default_getsampwent
;
2106 (*methods
)->getsampwnam
= pdb_default_getsampwnam
;
2107 (*methods
)->getsampwsid
= pdb_default_getsampwsid
;
2108 (*methods
)->create_user
= pdb_default_create_user
;
2109 (*methods
)->delete_user
= pdb_default_delete_user
;
2110 (*methods
)->add_sam_account
= pdb_default_add_sam_account
;
2111 (*methods
)->update_sam_account
= pdb_default_update_sam_account
;
2112 (*methods
)->delete_sam_account
= pdb_default_delete_sam_account
;
2113 (*methods
)->rename_sam_account
= pdb_default_rename_sam_account
;
2114 (*methods
)->update_login_attempts
= pdb_default_update_login_attempts
;
2116 (*methods
)->getgrsid
= pdb_default_getgrsid
;
2117 (*methods
)->getgrgid
= pdb_default_getgrgid
;
2118 (*methods
)->getgrnam
= pdb_default_getgrnam
;
2119 (*methods
)->create_dom_group
= pdb_default_create_dom_group
;
2120 (*methods
)->delete_dom_group
= pdb_default_delete_dom_group
;
2121 (*methods
)->add_group_mapping_entry
= pdb_default_add_group_mapping_entry
;
2122 (*methods
)->update_group_mapping_entry
= pdb_default_update_group_mapping_entry
;
2123 (*methods
)->delete_group_mapping_entry
= pdb_default_delete_group_mapping_entry
;
2124 (*methods
)->enum_group_mapping
= pdb_default_enum_group_mapping
;
2125 (*methods
)->enum_group_members
= pdb_default_enum_group_members
;
2126 (*methods
)->enum_group_memberships
= pdb_default_enum_group_memberships
;
2127 (*methods
)->set_unix_primary_group
= pdb_default_set_unix_primary_group
;
2128 (*methods
)->add_groupmem
= pdb_default_add_groupmem
;
2129 (*methods
)->del_groupmem
= pdb_default_del_groupmem
;
2130 (*methods
)->find_alias
= pdb_default_find_alias
;
2131 (*methods
)->create_alias
= pdb_default_create_alias
;
2132 (*methods
)->delete_alias
= pdb_default_delete_alias
;
2133 (*methods
)->get_aliasinfo
= pdb_default_get_aliasinfo
;
2134 (*methods
)->set_aliasinfo
= pdb_default_set_aliasinfo
;
2135 (*methods
)->add_aliasmem
= pdb_default_add_aliasmem
;
2136 (*methods
)->del_aliasmem
= pdb_default_del_aliasmem
;
2137 (*methods
)->enum_aliasmem
= pdb_default_enum_aliasmem
;
2138 (*methods
)->enum_alias_memberships
= pdb_default_alias_memberships
;
2139 (*methods
)->lookup_rids
= pdb_default_lookup_rids
;
2140 (*methods
)->get_account_policy
= pdb_default_get_account_policy
;
2141 (*methods
)->set_account_policy
= pdb_default_set_account_policy
;
2142 (*methods
)->get_seq_num
= pdb_default_get_seq_num
;
2143 (*methods
)->uid_to_rid
= pdb_default_uid_to_rid
;
2144 (*methods
)->gid_to_sid
= pdb_default_gid_to_sid
;
2145 (*methods
)->sid_to_id
= pdb_default_sid_to_id
;
2147 (*methods
)->search_users
= pdb_default_search_users
;
2148 (*methods
)->search_groups
= pdb_default_search_groups
;
2149 (*methods
)->search_aliases
= pdb_default_search_aliases
;
2151 return NT_STATUS_OK
;