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
;
35 static struct pdb_init_function_entry
*backends
= NULL
;
37 static void lazy_initialize_passdb(void)
39 static BOOL initialized
= False
;
47 static BOOL
lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32 rid
,
49 enum lsa_SidType
*psid_name_use
,
50 union unid_t
*unix_id
);
52 NTSTATUS
smb_register_passdb(int version
, const char *name
, pdb_init_function init
)
54 struct pdb_init_function_entry
*entry
= backends
;
56 if(version
!= PASSDB_INTERFACE_VERSION
) {
57 DEBUG(0,("Can't register passdb backend!\n"
58 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
59 "while this version of samba uses version %d\n",
60 version
,PASSDB_INTERFACE_VERSION
));
61 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
65 return NT_STATUS_INVALID_PARAMETER
;
68 DEBUG(5,("Attempting to register passdb backend %s\n", name
));
70 /* Check for duplicates */
71 if (pdb_find_backend_entry(name
)) {
72 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name
));
73 return NT_STATUS_OBJECT_NAME_COLLISION
;
76 entry
= SMB_XMALLOC_P(struct pdb_init_function_entry
);
77 entry
->name
= smb_xstrdup(name
);
80 DLIST_ADD(backends
, entry
);
81 DEBUG(5,("Successfully added passdb backend '%s'\n", name
));
85 struct pdb_init_function_entry
*pdb_find_backend_entry(const char *name
)
87 struct pdb_init_function_entry
*entry
= backends
;
90 if (strcmp(entry
->name
, name
)==0) return entry
;
97 /******************************************************************
98 Make a pdb_methods from scratch
99 *******************************************************************/
101 NTSTATUS
make_pdb_method_name(struct pdb_methods
**methods
, const char *selected
)
103 char *module_name
= smb_xstrdup(selected
);
104 char *module_location
= NULL
, *p
;
105 struct pdb_init_function_entry
*entry
;
106 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
108 lazy_initialize_passdb();
110 p
= strchr(module_name
, ':');
114 module_location
= p
+1;
115 trim_char(module_location
, ' ', ' ');
118 trim_char(module_name
, ' ', ' ');
121 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected
, module_name
));
123 entry
= pdb_find_backend_entry(module_name
);
125 /* Try to find a module that contains this module */
127 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
128 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name
)) && !(entry
= pdb_find_backend_entry(module_name
))) {
129 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name
));
130 SAFE_FREE(module_name
);
131 return NT_STATUS_UNSUCCESSFUL
;
135 /* No such backend found */
137 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name
));
138 SAFE_FREE(module_name
);
139 return NT_STATUS_INVALID_PARAMETER
;
142 DEBUG(5,("Found pdb backend %s\n", module_name
));
144 if ( !NT_STATUS_IS_OK( nt_status
= entry
->init(methods
, module_location
) ) ) {
145 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
146 selected
, nt_errstr(nt_status
)));
147 SAFE_FREE(module_name
);
151 SAFE_FREE(module_name
);
153 DEBUG(5,("pdb backend %s has a valid init\n", selected
));
158 /******************************************************************
159 Return an already initialised pdn_methods structure
160 *******************************************************************/
162 static struct pdb_methods
*pdb_get_methods_reload( BOOL reload
)
164 static struct pdb_methods
*pdb
= NULL
;
166 if ( pdb
&& reload
) {
167 pdb
->free_private_data( &(pdb
->private_data
) );
168 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
170 slprintf(msg
, sizeof(msg
)-1, "pdb_get_methods_reload: failed to get pdb methods for backend %s\n",
171 lp_passdb_backend() );
177 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
179 slprintf(msg
, sizeof(msg
)-1, "pdb_get_methods_reload: failed to get pdb methods for backend %s\n",
180 lp_passdb_backend() );
188 static struct pdb_methods
*pdb_get_methods(void)
190 return pdb_get_methods_reload(False
);
193 /******************************************************************
194 Backward compatibility functions for the original passdb interface
195 *******************************************************************/
197 BOOL
pdb_setsampwent(BOOL update
, uint16 acb_mask
)
199 struct pdb_methods
*pdb
= pdb_get_methods();
200 return NT_STATUS_IS_OK(pdb
->setsampwent(pdb
, update
, acb_mask
));
203 void pdb_endsampwent(void)
205 struct pdb_methods
*pdb
= pdb_get_methods();
206 pdb
->endsampwent(pdb
);
209 BOOL
pdb_getsampwent(struct samu
*user
)
211 struct pdb_methods
*pdb
= pdb_get_methods();
213 if ( !NT_STATUS_IS_OK(pdb
->getsampwent(pdb
, user
) ) ) {
220 BOOL
pdb_getsampwnam(struct samu
*sam_acct
, const char *username
)
222 struct pdb_methods
*pdb
= pdb_get_methods();
224 if (!NT_STATUS_IS_OK(pdb
->getsampwnam(pdb
, sam_acct
, username
))) {
229 TALLOC_FREE(csamuser
);
232 csamuser
= samu_new( NULL
);
237 if (!pdb_copy_sam_account(csamuser
, sam_acct
)) {
238 TALLOC_FREE(csamuser
);
245 /**********************************************************************
246 **********************************************************************/
248 BOOL
guest_user_info( struct samu
*user
)
252 const char *guestname
= lp_guestaccount();
254 if ( !(pwd
= getpwnam_alloc( NULL
, guestname
) ) ) {
255 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
260 result
= samu_set_unix(user
, pwd
);
264 return NT_STATUS_IS_OK( result
);
267 /**********************************************************************
268 **********************************************************************/
270 BOOL
pdb_getsampwsid(struct samu
*sam_acct
, const DOM_SID
*sid
)
272 struct pdb_methods
*pdb
= pdb_get_methods();
275 /* hard code the Guest RID of 501 */
277 if ( !sid_peek_check_rid( get_global_sam_sid(), sid
, &rid
) )
280 if ( rid
== DOMAIN_USER_RID_GUEST
) {
281 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
282 return guest_user_info( sam_acct
);
285 /* check the cache first */
287 if ( csamuser
&& sid_equal(sid
, pdb_get_user_sid(csamuser
) ) )
288 return pdb_copy_sam_account(sam_acct
, csamuser
);
290 return NT_STATUS_IS_OK(pdb
->getsampwsid(pdb
, sam_acct
, sid
));
293 static NTSTATUS
pdb_default_create_user(struct pdb_methods
*methods
,
294 TALLOC_CTX
*tmp_ctx
, const char *name
,
295 uint32 acb_info
, uint32
*rid
)
297 struct samu
*sam_pass
;
301 if ((sam_pass
= samu_new(tmp_ctx
)) == NULL
) {
302 return NT_STATUS_NO_MEMORY
;
305 if ( !(pwd
= Get_Pwnam_alloc(tmp_ctx
, name
)) ) {
310 if ((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] != '$') {
311 pstrcpy(add_script
, lp_adduser_script());
313 pstrcpy(add_script
, lp_addmachine_script());
316 if (add_script
[0] == '\0') {
317 DEBUG(3, ("Could not find user %s and no add script "
319 return NT_STATUS_NO_SUCH_USER
;
322 /* lowercase the username before creating the Unix account for
323 compatibility with previous Samba releases */
324 fstrcpy( name2
, name
);
326 all_string_sub(add_script
, "%u", name2
, sizeof(add_script
));
327 add_ret
= smbrun(add_script
,NULL
);
328 DEBUG(add_ret
? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
329 add_script
, add_ret
));
331 smb_nscd_flush_user_cache();
334 #ifdef ENABLE_BUILD_FARM_HACKS
336 DEBUG(1, ("Creating a faked user %s for build farm "
337 "purposes\n", name
));
338 faked_create_user(name
);
344 pwd
= Get_Pwnam_alloc(tmp_ctx
, name
);
347 /* we have a valid SID coming out of this call */
349 status
= samu_alloc_rid_unix( sam_pass
, pwd
);
353 if (!NT_STATUS_IS_OK(status
)) {
354 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status
)));
358 if (!sid_peek_check_rid(get_global_sam_sid(),
359 pdb_get_user_sid(sam_pass
), rid
)) {
360 DEBUG(0, ("Could not get RID of fresh user\n"));
361 return NT_STATUS_INTERNAL_ERROR
;
364 /* Use the username case specified in the original request */
366 pdb_set_username( sam_pass
, name
, PDB_SET
);
368 /* Disable the account on creation, it does not have a reasonable password yet. */
370 acb_info
|= ACB_DISABLED
;
372 pdb_set_acct_ctrl(sam_pass
, acb_info
, PDB_CHANGED
);
374 status
= pdb_add_sam_account(sam_pass
);
376 TALLOC_FREE(sam_pass
);
381 NTSTATUS
pdb_create_user(TALLOC_CTX
*mem_ctx
, const char *name
, uint32 flags
,
384 struct pdb_methods
*pdb
= pdb_get_methods();
385 return pdb
->create_user(pdb
, mem_ctx
, name
, flags
, rid
);
388 /****************************************************************************
389 Delete a UNIX user on demand.
390 ****************************************************************************/
392 static int smb_delete_user(const char *unix_user
)
399 if ( strequal( unix_user
, "root" ) ) {
400 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
404 pstrcpy(del_script
, lp_deluser_script());
407 all_string_sub(del_script
, "%u", unix_user
, sizeof(del_script
));
408 ret
= smbrun(del_script
,NULL
);
411 smb_nscd_flush_user_cache();
413 DEBUG(ret
? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script
,ret
));
418 static NTSTATUS
pdb_default_delete_user(struct pdb_methods
*methods
,
420 struct samu
*sam_acct
)
425 status
= pdb_delete_sam_account(sam_acct
);
426 if (!NT_STATUS_IS_OK(status
)) {
431 * Now delete the unix side ....
432 * note: we don't check if the delete really happened as the script is
433 * not necessary present and maybe the sysadmin doesn't want to delete
437 /* always lower case the username before handing it off to
440 fstrcpy( username
, pdb_get_username(sam_acct
) );
441 strlower_m( username
);
443 smb_delete_user( username
);
448 NTSTATUS
pdb_delete_user(TALLOC_CTX
*mem_ctx
, struct samu
*sam_acct
)
450 struct pdb_methods
*pdb
= pdb_get_methods();
453 /* sanity check to make sure we don't delete root */
455 if ( !sid_to_uid( pdb_get_user_sid(sam_acct
), &uid
) ) {
456 return NT_STATUS_NO_SUCH_USER
;
460 return NT_STATUS_ACCESS_DENIED
;
463 return pdb
->delete_user(pdb
, mem_ctx
, sam_acct
);
466 NTSTATUS
pdb_add_sam_account(struct samu
*sam_acct
)
468 struct pdb_methods
*pdb
= pdb_get_methods();
469 return pdb
->add_sam_account(pdb
, sam_acct
);
472 NTSTATUS
pdb_update_sam_account(struct samu
*sam_acct
)
474 struct pdb_methods
*pdb
= pdb_get_methods();
476 if (csamuser
!= NULL
) {
477 TALLOC_FREE(csamuser
);
481 return pdb
->update_sam_account(pdb
, sam_acct
);
484 NTSTATUS
pdb_delete_sam_account(struct samu
*sam_acct
)
486 struct pdb_methods
*pdb
= pdb_get_methods();
488 if (csamuser
!= NULL
) {
489 TALLOC_FREE(csamuser
);
493 return pdb
->delete_sam_account(pdb
, sam_acct
);
496 NTSTATUS
pdb_rename_sam_account(struct samu
*oldname
, const char *newname
)
498 struct pdb_methods
*pdb
= pdb_get_methods();
502 if (csamuser
!= NULL
) {
503 TALLOC_FREE(csamuser
);
507 /* sanity check to make sure we don't rename root */
509 if ( !sid_to_uid( pdb_get_user_sid(oldname
), &uid
) ) {
510 return NT_STATUS_NO_SUCH_USER
;
514 return NT_STATUS_ACCESS_DENIED
;
517 status
= pdb
->rename_sam_account(pdb
, oldname
, newname
);
519 /* always flush the cache here just to be safe */
525 NTSTATUS
pdb_update_login_attempts(struct samu
*sam_acct
, BOOL success
)
527 struct pdb_methods
*pdb
= pdb_get_methods();
528 return pdb
->update_login_attempts(pdb
, sam_acct
, success
);
531 BOOL
pdb_getgrsid(GROUP_MAP
*map
, DOM_SID sid
)
533 struct pdb_methods
*pdb
= pdb_get_methods();
534 return NT_STATUS_IS_OK(pdb
->getgrsid(pdb
, map
, sid
));
537 BOOL
pdb_getgrgid(GROUP_MAP
*map
, gid_t gid
)
539 struct pdb_methods
*pdb
= pdb_get_methods();
540 return NT_STATUS_IS_OK(pdb
->getgrgid(pdb
, map
, gid
));
543 BOOL
pdb_getgrnam(GROUP_MAP
*map
, const char *name
)
545 struct pdb_methods
*pdb
= pdb_get_methods();
546 return NT_STATUS_IS_OK(pdb
->getgrnam(pdb
, map
, name
));
549 static NTSTATUS
pdb_default_create_dom_group(struct pdb_methods
*methods
,
557 grp
= getgrnam(name
);
562 if (smb_create_group(name
, &gid
) != 0) {
563 return NT_STATUS_ACCESS_DENIED
;
570 return NT_STATUS_ACCESS_DENIED
;
573 if (pdb_rid_algorithm()) {
574 *rid
= algorithmic_pdb_gid_to_group_rid( grp
->gr_gid
);
576 if (!pdb_new_rid(rid
)) {
577 return NT_STATUS_ACCESS_DENIED
;
581 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
583 return add_initial_entry(grp
->gr_gid
, sid_string_static(&group_sid
),
584 SID_NAME_DOM_GRP
, name
, NULL
);
587 NTSTATUS
pdb_create_dom_group(TALLOC_CTX
*mem_ctx
, const char *name
,
590 struct pdb_methods
*pdb
= pdb_get_methods();
591 return pdb
->create_dom_group(pdb
, mem_ctx
, name
, rid
);
594 static NTSTATUS
pdb_default_delete_dom_group(struct pdb_methods
*methods
,
602 const char *grp_name
;
604 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
606 if (!get_domain_group_from_sid(group_sid
, &map
)) {
607 DEBUG(10, ("Could not find group for rid %d\n", rid
));
608 return NT_STATUS_NO_SUCH_GROUP
;
611 /* We need the group name for the smb_delete_group later on */
613 if (map
.gid
== (gid_t
)-1) {
614 return NT_STATUS_NO_SUCH_GROUP
;
617 grp
= getgrgid(map
.gid
);
619 return NT_STATUS_NO_SUCH_GROUP
;
622 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
624 grp_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
625 if (grp_name
== NULL
) {
626 return NT_STATUS_NO_MEMORY
;
629 status
= pdb_delete_group_mapping_entry(group_sid
);
631 if (!NT_STATUS_IS_OK(status
)) {
635 /* Don't check the result of smb_delete_group */
637 smb_delete_group(grp_name
);
642 NTSTATUS
pdb_delete_dom_group(TALLOC_CTX
*mem_ctx
, uint32 rid
)
644 struct pdb_methods
*pdb
= pdb_get_methods();
645 return pdb
->delete_dom_group(pdb
, mem_ctx
, rid
);
648 NTSTATUS
pdb_add_group_mapping_entry(GROUP_MAP
*map
)
650 struct pdb_methods
*pdb
= pdb_get_methods();
651 return pdb
->add_group_mapping_entry(pdb
, map
);
654 NTSTATUS
pdb_update_group_mapping_entry(GROUP_MAP
*map
)
656 struct pdb_methods
*pdb
= pdb_get_methods();
657 return pdb
->update_group_mapping_entry(pdb
, map
);
660 NTSTATUS
pdb_delete_group_mapping_entry(DOM_SID sid
)
662 struct pdb_methods
*pdb
= pdb_get_methods();
663 return pdb
->delete_group_mapping_entry(pdb
, sid
);
666 BOOL
pdb_enum_group_mapping(const DOM_SID
*sid
, enum lsa_SidType sid_name_use
, GROUP_MAP
**pp_rmap
,
667 size_t *p_num_entries
, BOOL unix_only
)
669 struct pdb_methods
*pdb
= pdb_get_methods();
670 return NT_STATUS_IS_OK(pdb
-> enum_group_mapping(pdb
, sid
, sid_name_use
,
671 pp_rmap
, p_num_entries
, unix_only
));
674 NTSTATUS
pdb_enum_group_members(TALLOC_CTX
*mem_ctx
,
676 uint32
**pp_member_rids
,
677 size_t *p_num_members
)
679 struct pdb_methods
*pdb
= pdb_get_methods();
682 result
= pdb
->enum_group_members(pdb
, mem_ctx
,
683 sid
, pp_member_rids
, p_num_members
);
685 /* special check for rid 513 */
687 if ( !NT_STATUS_IS_OK( result
) ) {
690 sid_peek_rid( sid
, &rid
);
692 if ( rid
== DOMAIN_GROUP_RID_USERS
) {
694 *pp_member_rids
= NULL
;
703 NTSTATUS
pdb_enum_group_memberships(TALLOC_CTX
*mem_ctx
, struct samu
*user
,
704 DOM_SID
**pp_sids
, gid_t
**pp_gids
,
705 size_t *p_num_groups
)
707 struct pdb_methods
*pdb
= pdb_get_methods();
708 return pdb
->enum_group_memberships(
710 pp_sids
, pp_gids
, p_num_groups
);
713 static NTSTATUS
pdb_default_set_unix_primary_group(struct pdb_methods
*methods
,
715 struct samu
*sampass
)
720 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
) ||
721 (grp
= getgrgid(gid
)) == NULL
) {
722 return NT_STATUS_INVALID_PRIMARY_GROUP
;
725 if (smb_set_primary_group(grp
->gr_name
,
726 pdb_get_username(sampass
)) != 0) {
727 return NT_STATUS_ACCESS_DENIED
;
733 NTSTATUS
pdb_set_unix_primary_group(TALLOC_CTX
*mem_ctx
, struct samu
*user
)
735 struct pdb_methods
*pdb
= pdb_get_methods();
736 return pdb
->set_unix_primary_group(pdb
, mem_ctx
, user
);
740 * Helper function to see whether a user is in a group. We can't use
741 * user_in_group_sid here because this creates dependencies only smbd can
745 static BOOL
pdb_user_in_group(TALLOC_CTX
*mem_ctx
, struct samu
*account
,
746 const DOM_SID
*group_sid
)
750 size_t i
, num_groups
;
752 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx
, account
,
758 for (i
=0; i
<num_groups
; i
++) {
759 if (sid_equal(group_sid
, &sids
[i
])) {
766 static NTSTATUS
pdb_default_add_groupmem(struct pdb_methods
*methods
,
771 DOM_SID group_sid
, member_sid
;
772 struct samu
*account
= NULL
;
776 const char *group_name
;
779 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
780 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
782 if (!get_domain_group_from_sid(group_sid
, &map
) ||
783 (map
.gid
== (gid_t
)-1) ||
784 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
785 return NT_STATUS_NO_SUCH_GROUP
;
788 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
789 if (group_name
== NULL
) {
790 return NT_STATUS_NO_MEMORY
;
793 if ( !(account
= samu_new( NULL
)) ) {
794 return NT_STATUS_NO_MEMORY
;
797 if (!pdb_getsampwsid(account
, &member_sid
) ||
798 !sid_to_uid(&member_sid
, &uid
) ||
799 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
800 return NT_STATUS_NO_SUCH_USER
;
803 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
804 return NT_STATUS_MEMBER_IN_GROUP
;
808 * ok, the group exist, the user exist, the user is not in the group,
809 * we can (finally) add it to the group !
812 smb_add_user_group(group_name
, pwd
->pw_name
);
814 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
815 return NT_STATUS_ACCESS_DENIED
;
821 NTSTATUS
pdb_add_groupmem(TALLOC_CTX
*mem_ctx
, uint32 group_rid
,
824 struct pdb_methods
*pdb
= pdb_get_methods();
825 return pdb
->add_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
828 static NTSTATUS
pdb_default_del_groupmem(struct pdb_methods
*methods
,
833 DOM_SID group_sid
, member_sid
;
834 struct samu
*account
= NULL
;
838 const char *group_name
;
841 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
842 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
844 if (!get_domain_group_from_sid(group_sid
, &map
) ||
845 (map
.gid
== (gid_t
)-1) ||
846 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
847 return NT_STATUS_NO_SUCH_GROUP
;
850 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
851 if (group_name
== NULL
) {
852 return NT_STATUS_NO_MEMORY
;
855 if ( !(account
= samu_new( NULL
)) ) {
856 return NT_STATUS_NO_MEMORY
;
859 if (!pdb_getsampwsid(account
, &member_sid
) ||
860 !sid_to_uid(&member_sid
, &uid
) ||
861 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
862 return NT_STATUS_NO_SUCH_USER
;
865 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
866 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
870 * ok, the group exist, the user exist, the user is in the group,
871 * we can (finally) delete it from the group!
874 smb_delete_user_group(group_name
, pwd
->pw_name
);
876 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
877 return NT_STATUS_ACCESS_DENIED
;
883 NTSTATUS
pdb_del_groupmem(TALLOC_CTX
*mem_ctx
, uint32 group_rid
,
886 struct pdb_methods
*pdb
= pdb_get_methods();
887 return pdb
->del_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
890 BOOL
pdb_find_alias(const char *name
, DOM_SID
*sid
)
892 struct pdb_methods
*pdb
= pdb_get_methods();
893 return NT_STATUS_IS_OK(pdb
->find_alias(pdb
, name
, sid
));
896 NTSTATUS
pdb_create_alias(const char *name
, uint32
*rid
)
898 struct pdb_methods
*pdb
= pdb_get_methods();
899 return pdb
->create_alias(pdb
, name
, rid
);
902 BOOL
pdb_delete_alias(const DOM_SID
*sid
)
904 struct pdb_methods
*pdb
= pdb_get_methods();
905 return NT_STATUS_IS_OK(pdb
->delete_alias(pdb
, sid
));
909 BOOL
pdb_get_aliasinfo(const DOM_SID
*sid
, struct acct_info
*info
)
911 struct pdb_methods
*pdb
= pdb_get_methods();
912 return NT_STATUS_IS_OK(pdb
->get_aliasinfo(pdb
, sid
, info
));
915 BOOL
pdb_set_aliasinfo(const DOM_SID
*sid
, struct acct_info
*info
)
917 struct pdb_methods
*pdb
= pdb_get_methods();
918 return NT_STATUS_IS_OK(pdb
->set_aliasinfo(pdb
, sid
, info
));
921 NTSTATUS
pdb_add_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
923 struct pdb_methods
*pdb
= pdb_get_methods();
924 return pdb
->add_aliasmem(pdb
, alias
, member
);
927 NTSTATUS
pdb_del_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
929 struct pdb_methods
*pdb
= pdb_get_methods();
930 return pdb
->del_aliasmem(pdb
, alias
, member
);
933 NTSTATUS
pdb_enum_aliasmem(const DOM_SID
*alias
,
934 DOM_SID
**pp_members
, size_t *p_num_members
)
936 struct pdb_methods
*pdb
= pdb_get_methods();
937 return pdb
->enum_aliasmem(pdb
, alias
, pp_members
, p_num_members
);
940 NTSTATUS
pdb_enum_alias_memberships(TALLOC_CTX
*mem_ctx
,
941 const DOM_SID
*domain_sid
,
942 const DOM_SID
*members
, size_t num_members
,
943 uint32
**pp_alias_rids
,
944 size_t *p_num_alias_rids
)
946 struct pdb_methods
*pdb
= pdb_get_methods();
947 return pdb
->enum_alias_memberships(pdb
, mem_ctx
,
949 members
, num_members
,
954 NTSTATUS
pdb_lookup_rids(const DOM_SID
*domain_sid
,
958 enum lsa_SidType
*attrs
)
960 struct pdb_methods
*pdb
= pdb_get_methods();
961 return pdb
->lookup_rids(pdb
, domain_sid
, num_rids
, rids
, names
, attrs
);
965 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
967 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
968 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
969 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
970 * down to are pdb_getsampwnam and pdb_getgrnam instead of
972 * But in principle, it the call belongs to the API and might get
973 * used in this context some day.
976 NTSTATUS
pdb_lookup_names(const DOM_SID
*domain_sid
,
980 enum lsa_SidType
*attrs
)
982 struct pdb_methods
*pdb
= pdb_get_methods();
983 return pdb
->lookup_names(pdb
, domain_sid
, num_names
, names
, rids
, attrs
);
987 BOOL
pdb_get_account_policy(int policy_index
, uint32
*value
)
989 struct pdb_methods
*pdb
= pdb_get_methods();
993 status
= pdb
->get_account_policy(pdb
, policy_index
, value
);
996 return NT_STATUS_IS_OK(status
);
999 BOOL
pdb_set_account_policy(int policy_index
, uint32 value
)
1001 struct pdb_methods
*pdb
= pdb_get_methods();
1005 status
= pdb
->set_account_policy(pdb
, policy_index
, value
);
1008 return NT_STATUS_IS_OK(status
);
1011 BOOL
pdb_get_seq_num(time_t *seq_num
)
1013 struct pdb_methods
*pdb
= pdb_get_methods();
1014 return NT_STATUS_IS_OK(pdb
->get_seq_num(pdb
, seq_num
));
1017 BOOL
pdb_uid_to_rid(uid_t uid
, uint32
*rid
)
1019 struct pdb_methods
*pdb
= pdb_get_methods();
1020 return pdb
->uid_to_rid(pdb
, uid
, rid
);
1023 BOOL
pdb_uid_to_sid(uid_t uid
, DOM_SID
*sid
)
1025 struct pdb_methods
*pdb
= pdb_get_methods();
1026 return pdb
->uid_to_sid(pdb
, uid
, sid
);
1029 BOOL
pdb_gid_to_sid(gid_t gid
, DOM_SID
*sid
)
1031 struct pdb_methods
*pdb
= pdb_get_methods();
1032 return pdb
->gid_to_sid(pdb
, gid
, sid
);
1035 BOOL
pdb_sid_to_id(const DOM_SID
*sid
, union unid_t
*id
,
1036 enum lsa_SidType
*type
)
1038 struct pdb_methods
*pdb
= pdb_get_methods();
1039 return pdb
->sid_to_id(pdb
, sid
, id
, type
);
1042 BOOL
pdb_rid_algorithm(void)
1044 struct pdb_methods
*pdb
= pdb_get_methods();
1045 return pdb
->rid_algorithm(pdb
);
1048 /********************************************************************
1049 Allocate a new RID from the passdb backend. Verify that it is free
1050 by calling lookup_global_sam_rid() to verify that the RID is not
1051 in use. This handles servers that have existing users or groups
1052 with add RIDs (assigned from previous algorithmic mappings)
1053 ********************************************************************/
1055 BOOL
pdb_new_rid(uint32
*rid
)
1057 struct pdb_methods
*pdb
= pdb_get_methods();
1058 const char *name
= NULL
;
1059 enum lsa_SidType type
;
1060 uint32 allocated_rid
= 0;
1064 if (pdb_rid_algorithm()) {
1065 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1070 if (algorithmic_rid_base() != BASE_RID
) {
1071 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1072 "without algorithmic RIDs is chosen.\n"));
1073 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1074 "add', set the maximum used RID using\n"));
1075 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1079 if ( (ctx
= talloc_init("pdb_new_rid")) == NULL
) {
1080 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1084 /* Attempt to get an unused RID (max tires is 250...yes that it is
1085 and arbitrary number I pulkled out of my head). -- jerry */
1087 for ( i
=0; allocated_rid
==0 && i
<250; i
++ ) {
1090 if ( !pdb
->new_rid(pdb
, &allocated_rid
) ) {
1094 /* validate that the RID is not in use */
1096 if ( lookup_global_sam_rid( ctx
, allocated_rid
, &name
, &type
, NULL
) ) {
1103 if ( allocated_rid
== 0 ) {
1104 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1108 *rid
= allocated_rid
;
1113 /***************************************************************
1114 Initialize the static context (at smbd startup etc).
1116 If uninitialised, context will auto-init on first use.
1117 ***************************************************************/
1119 BOOL
initialize_password_db(BOOL reload
)
1121 return (pdb_get_methods_reload(reload
) != NULL
);
1125 /***************************************************************************
1126 Default implementations of some functions.
1127 ****************************************************************************/
1129 static NTSTATUS
pdb_default_getsampwnam (struct pdb_methods
*methods
, struct samu
*user
, const char *sname
)
1131 return NT_STATUS_NO_SUCH_USER
;
1134 static NTSTATUS
pdb_default_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1136 return NT_STATUS_NO_SUCH_USER
;
1139 static NTSTATUS
pdb_default_add_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1141 return NT_STATUS_NOT_IMPLEMENTED
;
1144 static NTSTATUS
pdb_default_update_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1146 return NT_STATUS_NOT_IMPLEMENTED
;
1149 static NTSTATUS
pdb_default_delete_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
)
1151 return NT_STATUS_NOT_IMPLEMENTED
;
1154 static NTSTATUS
pdb_default_rename_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
, const char *newname
)
1156 return NT_STATUS_NOT_IMPLEMENTED
;
1159 static NTSTATUS
pdb_default_update_login_attempts (struct pdb_methods
*methods
, struct samu
*newpwd
, BOOL success
)
1161 return NT_STATUS_OK
;
1164 static NTSTATUS
pdb_default_setsampwent(struct pdb_methods
*methods
, BOOL update
, uint32 acb_mask
)
1166 return NT_STATUS_NOT_IMPLEMENTED
;
1169 static NTSTATUS
pdb_default_getsampwent(struct pdb_methods
*methods
, struct samu
*user
)
1171 return NT_STATUS_NOT_IMPLEMENTED
;
1174 static void pdb_default_endsampwent(struct pdb_methods
*methods
)
1176 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1179 static NTSTATUS
pdb_default_get_account_policy(struct pdb_methods
*methods
, int policy_index
, uint32
*value
)
1181 return account_policy_get(policy_index
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1184 static NTSTATUS
pdb_default_set_account_policy(struct pdb_methods
*methods
, int policy_index
, uint32 value
)
1186 return account_policy_set(policy_index
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1189 static NTSTATUS
pdb_default_get_seq_num(struct pdb_methods
*methods
, time_t *seq_num
)
1191 *seq_num
= time(NULL
);
1192 return NT_STATUS_OK
;
1195 static BOOL
pdb_default_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
1198 struct samu
*sampw
= NULL
;
1199 struct passwd
*unix_pw
;
1202 unix_pw
= sys_getpwuid( uid
);
1205 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1206 "%lu\n", (unsigned long)uid
));
1210 if ( !(sampw
= samu_new( NULL
)) ) {
1211 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1216 ret
= NT_STATUS_IS_OK(
1217 methods
->getsampwnam(methods
, sampw
, unix_pw
->pw_name
));
1221 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1222 "%s (%d)\n", unix_pw
->pw_name
, uid
));
1227 sid_copy(sid
, pdb_get_user_sid(sampw
));
1234 static BOOL
pdb_default_uid_to_rid(struct pdb_methods
*methods
, uid_t uid
,
1240 ret
= pdb_default_uid_to_sid(methods
, uid
, &sid
);
1245 ret
= sid_peek_check_rid(get_global_sam_sid(), &sid
, rid
);
1248 DEBUG(1, ("Could not peek rid out of sid %s\n",
1249 sid_string_static(&sid
)));
1255 static BOOL
pdb_default_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
1260 if (!NT_STATUS_IS_OK(methods
->getgrgid(methods
, &map
, gid
))) {
1264 sid_copy(sid
, &map
.sid
);
1268 static BOOL
pdb_default_sid_to_id(struct pdb_methods
*methods
,
1270 union unid_t
*id
, enum lsa_SidType
*type
)
1272 TALLOC_CTX
*mem_ctx
;
1277 mem_ctx
= talloc_new(NULL
);
1279 if (mem_ctx
== NULL
) {
1280 DEBUG(0, ("talloc_new failed\n"));
1284 if (sid_peek_check_rid(get_global_sam_sid(), sid
, &rid
)) {
1285 /* Here we might have users as well as groups and aliases */
1286 ret
= lookup_global_sam_rid(mem_ctx
, rid
, &name
, type
, id
);
1290 if (sid_peek_check_rid(&global_sid_Builtin
, sid
, &rid
)) {
1291 /* Here we only have aliases */
1293 if (!NT_STATUS_IS_OK(methods
->getgrsid(methods
, &map
, *sid
))) {
1294 DEBUG(10, ("Could not find map for sid %s\n",
1295 sid_string_static(sid
)));
1298 if ((map
.sid_name_use
!= SID_NAME_ALIAS
) &&
1299 (map
.sid_name_use
!= SID_NAME_WKN_GRP
)) {
1300 DEBUG(10, ("Map for sid %s is a %s, expected an "
1301 "alias\n", sid_string_static(sid
),
1302 sid_type_lookup(map
.sid_name_use
)));
1307 *type
= SID_NAME_ALIAS
;
1312 DEBUG(5, ("Sid %s is neither ours nor builtin, don't know it\n",
1313 sid_string_static(sid
)));
1317 TALLOC_FREE(mem_ctx
);
1321 static BOOL
add_uid_to_array_unique(TALLOC_CTX
*mem_ctx
,
1322 uid_t uid
, uid_t
**pp_uids
, size_t *p_num
)
1326 for (i
=0; i
<*p_num
; i
++) {
1327 if ((*pp_uids
)[i
] == uid
)
1331 *pp_uids
= TALLOC_REALLOC_ARRAY(mem_ctx
, *pp_uids
, uid_t
, *p_num
+1);
1333 if (*pp_uids
== NULL
)
1336 (*pp_uids
)[*p_num
] = uid
;
1341 static BOOL
get_memberuids(TALLOC_CTX
*mem_ctx
, gid_t gid
, uid_t
**pp_uids
, size_t *p_num
)
1352 /* We only look at our own sam, so don't care about imported stuff */
1353 winbind_env
= winbind_env_set();
1356 if ((grp
= getgrgid(gid
)) == NULL
) {
1357 /* allow winbindd lookups, but only if they weren't already disabled */
1361 /* Primary group members */
1363 while ((pwd
= getpwent()) != NULL
) {
1364 if (pwd
->pw_gid
== gid
) {
1365 if (!add_uid_to_array_unique(mem_ctx
, pwd
->pw_uid
,
1373 /* Secondary group members */
1374 for (gr
= grp
->gr_mem
; (*gr
!= NULL
) && ((*gr
)[0] != '\0'); gr
+= 1) {
1375 struct passwd
*pw
= getpwnam(*gr
);
1379 if (!add_uid_to_array_unique(mem_ctx
, pw
->pw_uid
, pp_uids
, p_num
)) {
1388 /* allow winbindd lookups, but only if they weren't already disabled */
1396 static NTSTATUS
pdb_default_enum_group_members(struct pdb_methods
*methods
,
1397 TALLOC_CTX
*mem_ctx
,
1398 const DOM_SID
*group
,
1399 uint32
**pp_member_rids
,
1400 size_t *p_num_members
)
1406 *pp_member_rids
= NULL
;
1409 if (!sid_to_gid(group
, &gid
))
1410 return NT_STATUS_NO_SUCH_GROUP
;
1412 if(!get_memberuids(mem_ctx
, gid
, &uids
, &num_uids
))
1413 return NT_STATUS_NO_SUCH_GROUP
;
1416 return NT_STATUS_OK
;
1418 *pp_member_rids
= TALLOC_ZERO_ARRAY(mem_ctx
, uint32
, num_uids
);
1420 for (i
=0; i
<num_uids
; i
++) {
1423 uid_to_sid(&sid
, uids
[i
]);
1425 if (!sid_check_is_in_our_domain(&sid
)) {
1426 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1427 "in our domain\n"));
1431 sid_peek_rid(&sid
, &(*pp_member_rids
)[*p_num_members
]);
1432 *p_num_members
+= 1;
1435 return NT_STATUS_OK
;
1438 static NTSTATUS
pdb_default_enum_group_memberships(struct pdb_methods
*methods
,
1439 TALLOC_CTX
*mem_ctx
,
1443 size_t *p_num_groups
)
1448 const char *username
= pdb_get_username(user
);
1451 /* Ignore the primary group SID. Honor the real Unix primary group.
1452 The primary group SID is only of real use to Windows clients */
1454 if ( !(pw
= getpwnam_alloc(mem_ctx
, username
)) ) {
1455 return NT_STATUS_NO_SUCH_USER
;
1462 if (!getgroups_unix_user(mem_ctx
, username
, gid
, pp_gids
, p_num_groups
)) {
1463 return NT_STATUS_NO_SUCH_USER
;
1466 if (*p_num_groups
== 0) {
1467 smb_panic("primary group missing");
1470 *pp_sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, *p_num_groups
);
1472 if (*pp_sids
== NULL
) {
1473 TALLOC_FREE(*pp_gids
);
1474 return NT_STATUS_NO_MEMORY
;
1477 for (i
=0; i
<*p_num_groups
; i
++) {
1478 gid_to_sid(&(*pp_sids
)[i
], (*pp_gids
)[i
]);
1481 return NT_STATUS_OK
;
1484 /*******************************************************************
1485 Look up a rid in the SAM we're responsible for (i.e. passdb)
1486 ********************************************************************/
1488 static BOOL
lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32 rid
,
1490 enum lsa_SidType
*psid_name_use
,
1491 union unid_t
*unix_id
)
1493 struct samu
*sam_account
= NULL
;
1498 *psid_name_use
= SID_NAME_UNKNOWN
;
1500 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1501 (unsigned int)rid
));
1503 sid_copy(&sid
, get_global_sam_sid());
1504 sid_append_rid(&sid
, rid
);
1506 /* see if the passdb can help us with the name of the user */
1508 if ( !(sam_account
= samu_new( NULL
)) ) {
1512 /* BEING ROOT BLLOCK */
1514 if (pdb_getsampwsid(sam_account
, &sid
)) {
1517 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1518 *name
= talloc_strdup(mem_ctx
, pdb_get_username(sam_account
));
1520 TALLOC_FREE(sam_account
);
1524 *psid_name_use
= SID_NAME_USER
;
1526 TALLOC_FREE(sam_account
);
1528 if (unix_id
== NULL
) {
1532 pw
= Get_Pwnam(*name
);
1536 unix_id
->uid
= pw
->pw_uid
;
1539 TALLOC_FREE(sam_account
);
1541 ret
= pdb_getgrsid(&map
, sid
);
1543 /* END BECOME_ROOT BLOCK */
1545 /* do not resolve SIDs to a name unless there is a valid
1546 gid associated with it */
1548 if ( ret
&& (map
.gid
!= (gid_t
)-1) ) {
1549 *name
= talloc_strdup(mem_ctx
, map
.nt_name
);
1550 *psid_name_use
= map
.sid_name_use
;
1553 unix_id
->gid
= map
.gid
;
1559 /* Windows will always map RID 513 to something. On a non-domain
1560 controller, this gets mapped to SERVER\None. */
1563 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1567 if ( rid
== DOMAIN_GROUP_RID_USERS
) {
1568 *name
= talloc_strdup(mem_ctx
, "None" );
1569 *psid_name_use
= SID_NAME_DOM_GRP
;
1577 static NTSTATUS
pdb_default_lookup_rids(struct pdb_methods
*methods
,
1578 const DOM_SID
*domain_sid
,
1582 enum lsa_SidType
*attrs
)
1586 BOOL have_mapped
= False
;
1587 BOOL have_unmapped
= False
;
1589 if (sid_check_is_builtin(domain_sid
)) {
1591 for (i
=0; i
<num_rids
; i
++) {
1594 if (lookup_builtin_rid(names
, rids
[i
], &name
)) {
1595 attrs
[i
] = SID_NAME_ALIAS
;
1597 DEBUG(5,("lookup_rids: %s:%d\n",
1598 names
[i
], attrs
[i
]));
1601 have_unmapped
= True
;
1602 attrs
[i
] = SID_NAME_UNKNOWN
;
1608 /* Should not happen, but better check once too many */
1609 if (!sid_check_is_domain(domain_sid
)) {
1610 return NT_STATUS_INVALID_HANDLE
;
1613 for (i
= 0; i
< num_rids
; i
++) {
1616 if (lookup_global_sam_rid(names
, rids
[i
], &name
, &attrs
[i
],
1619 return NT_STATUS_NO_MEMORY
;
1622 DEBUG(5,("lookup_rids: %s:%d\n", names
[i
], attrs
[i
]));
1625 have_unmapped
= True
;
1626 attrs
[i
] = SID_NAME_UNKNOWN
;
1632 result
= NT_STATUS_NONE_MAPPED
;
1635 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1641 static NTSTATUS
pdb_default_lookup_names(struct pdb_methods
*methods
,
1642 const DOM_SID
*domain_sid
,
1646 enum lsa_SidType
*attrs
)
1650 BOOL have_mapped
= False
;
1651 BOOL have_unmapped
= False
;
1653 if (sid_check_is_builtin(domain_sid
)) {
1655 for (i
=0; i
<num_names
; i
++) {
1658 if (lookup_builtin_name(names
[i
], &rid
)) {
1659 attrs
[i
] = SID_NAME_ALIAS
;
1661 DEBUG(5,("lookup_rids: %s:%d\n",
1662 names
[i
], attrs
[i
]));
1665 have_unmapped
= True
;
1666 attrs
[i
] = SID_NAME_UNKNOWN
;
1672 /* Should not happen, but better check once too many */
1673 if (!sid_check_is_domain(domain_sid
)) {
1674 return NT_STATUS_INVALID_HANDLE
;
1677 for (i
= 0; i
< num_names
; i
++) {
1678 if (lookup_global_sam_name(names
[i
], 0, &rids
[i
], &attrs
[i
])) {
1679 DEBUG(5,("lookup_names: %s-> %d:%d\n", names
[i
],
1680 rids
[i
], attrs
[i
]));
1683 have_unmapped
= True
;
1684 attrs
[i
] = SID_NAME_UNKNOWN
;
1690 result
= NT_STATUS_NONE_MAPPED
;
1693 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1699 static struct pdb_search
*pdb_search_init(enum pdb_search_type type
)
1701 TALLOC_CTX
*mem_ctx
;
1702 struct pdb_search
*result
;
1704 mem_ctx
= talloc_init("pdb_search");
1705 if (mem_ctx
== NULL
) {
1706 DEBUG(0, ("talloc_init failed\n"));
1710 result
= TALLOC_P(mem_ctx
, struct pdb_search
);
1711 if (result
== NULL
) {
1712 DEBUG(0, ("talloc failed\n"));
1716 result
->mem_ctx
= mem_ctx
;
1717 result
->type
= type
;
1718 result
->cache
= NULL
;
1719 result
->num_entries
= 0;
1720 result
->cache_size
= 0;
1721 result
->search_ended
= False
;
1723 /* Segfault appropriately if not initialized */
1724 result
->next_entry
= NULL
;
1725 result
->search_end
= NULL
;
1730 static void fill_displayentry(TALLOC_CTX
*mem_ctx
, uint32 rid
,
1732 const char *account_name
,
1733 const char *fullname
,
1734 const char *description
,
1735 struct samr_displayentry
*entry
)
1738 entry
->acct_flags
= acct_flags
;
1740 if (account_name
!= NULL
)
1741 entry
->account_name
= talloc_strdup(mem_ctx
, account_name
);
1743 entry
->account_name
= "";
1745 if (fullname
!= NULL
)
1746 entry
->fullname
= talloc_strdup(mem_ctx
, fullname
);
1748 entry
->fullname
= "";
1750 if (description
!= NULL
)
1751 entry
->description
= talloc_strdup(mem_ctx
, description
);
1753 entry
->description
= "";
1756 static BOOL user_search_in_progress
= False
;
1757 struct user_search
{
1761 static BOOL
next_entry_users(struct pdb_search
*s
,
1762 struct samr_displayentry
*entry
)
1764 struct user_search
*state
= (struct user_search
*)s
->private_data
;
1765 struct samu
*user
= NULL
;
1768 if ( !(user
= samu_new( NULL
)) ) {
1769 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1773 if (!pdb_getsampwent(user
)) {
1778 if ((state
->acct_flags
!= 0) &&
1779 ((pdb_get_acct_ctrl(user
) & state
->acct_flags
) == 0)) {
1784 fill_displayentry(s
->mem_ctx
, pdb_get_user_rid(user
),
1785 pdb_get_acct_ctrl(user
), pdb_get_username(user
),
1786 pdb_get_fullname(user
), pdb_get_acct_desc(user
),
1793 static void search_end_users(struct pdb_search
*search
)
1796 user_search_in_progress
= False
;
1799 static BOOL
pdb_default_search_users(struct pdb_methods
*methods
,
1800 struct pdb_search
*search
,
1803 struct user_search
*state
;
1805 if (user_search_in_progress
) {
1806 DEBUG(1, ("user search in progress\n"));
1810 if (!pdb_setsampwent(False
, acct_flags
)) {
1811 DEBUG(5, ("Could not start search\n"));
1815 user_search_in_progress
= True
;
1817 state
= TALLOC_P(search
->mem_ctx
, struct user_search
);
1818 if (state
== NULL
) {
1819 DEBUG(0, ("talloc failed\n"));
1823 state
->acct_flags
= acct_flags
;
1825 search
->private_data
= state
;
1826 search
->next_entry
= next_entry_users
;
1827 search
->search_end
= search_end_users
;
1831 struct group_search
{
1833 size_t num_groups
, current_group
;
1836 static BOOL
next_entry_groups(struct pdb_search
*s
,
1837 struct samr_displayentry
*entry
)
1839 struct group_search
*state
= (struct group_search
*)s
->private_data
;
1841 GROUP_MAP
*map
= &state
->groups
[state
->current_group
];
1843 if (state
->current_group
== state
->num_groups
)
1846 sid_peek_rid(&map
->sid
, &rid
);
1848 fill_displayentry(s
->mem_ctx
, rid
, 0, map
->nt_name
, NULL
, map
->comment
,
1851 state
->current_group
+= 1;
1855 static void search_end_groups(struct pdb_search
*search
)
1857 struct group_search
*state
=
1858 (struct group_search
*)search
->private_data
;
1859 SAFE_FREE(state
->groups
);
1862 static BOOL
pdb_search_grouptype(struct pdb_search
*search
,
1863 const DOM_SID
*sid
, enum lsa_SidType type
)
1865 struct group_search
*state
;
1867 state
= TALLOC_P(search
->mem_ctx
, struct group_search
);
1868 if (state
== NULL
) {
1869 DEBUG(0, ("talloc failed\n"));
1873 if (!pdb_enum_group_mapping(sid
, type
, &state
->groups
, &state
->num_groups
,
1875 DEBUG(0, ("Could not enum groups\n"));
1879 state
->current_group
= 0;
1880 search
->private_data
= state
;
1881 search
->next_entry
= next_entry_groups
;
1882 search
->search_end
= search_end_groups
;
1886 static BOOL
pdb_default_search_groups(struct pdb_methods
*methods
,
1887 struct pdb_search
*search
)
1889 return pdb_search_grouptype(search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
1892 static BOOL
pdb_default_search_aliases(struct pdb_methods
*methods
,
1893 struct pdb_search
*search
,
1897 return pdb_search_grouptype(search
, sid
, SID_NAME_ALIAS
);
1900 static struct samr_displayentry
*pdb_search_getentry(struct pdb_search
*search
,
1903 if (idx
< search
->num_entries
)
1904 return &search
->cache
[idx
];
1906 if (search
->search_ended
)
1909 while (idx
>= search
->num_entries
) {
1910 struct samr_displayentry entry
;
1912 if (!search
->next_entry(search
, &entry
)) {
1913 search
->search_end(search
);
1914 search
->search_ended
= True
;
1918 ADD_TO_LARGE_ARRAY(search
->mem_ctx
, struct samr_displayentry
,
1919 entry
, &search
->cache
, &search
->num_entries
,
1920 &search
->cache_size
);
1923 return (search
->num_entries
> idx
) ? &search
->cache
[idx
] : NULL
;
1926 struct pdb_search
*pdb_search_users(uint32 acct_flags
)
1928 struct pdb_methods
*pdb
= pdb_get_methods();
1929 struct pdb_search
*result
;
1931 result
= pdb_search_init(PDB_USER_SEARCH
);
1932 if (result
== NULL
) {
1936 if (!pdb
->search_users(pdb
, result
, acct_flags
)) {
1937 talloc_destroy(result
->mem_ctx
);
1943 struct pdb_search
*pdb_search_groups(void)
1945 struct pdb_methods
*pdb
= pdb_get_methods();
1946 struct pdb_search
*result
;
1948 result
= pdb_search_init(PDB_GROUP_SEARCH
);
1949 if (result
== NULL
) {
1953 if (!pdb
->search_groups(pdb
, result
)) {
1954 talloc_destroy(result
->mem_ctx
);
1960 struct pdb_search
*pdb_search_aliases(const DOM_SID
*sid
)
1962 struct pdb_methods
*pdb
= pdb_get_methods();
1963 struct pdb_search
*result
;
1965 if (pdb
== NULL
) return NULL
;
1967 result
= pdb_search_init(PDB_ALIAS_SEARCH
);
1968 if (result
== NULL
) return NULL
;
1970 if (!pdb
->search_aliases(pdb
, result
, sid
)) {
1971 talloc_destroy(result
->mem_ctx
);
1977 uint32
pdb_search_entries(struct pdb_search
*search
,
1978 uint32 start_idx
, uint32 max_entries
,
1979 struct samr_displayentry
**result
)
1981 struct samr_displayentry
*end_entry
;
1982 uint32 end_idx
= start_idx
+max_entries
-1;
1984 /* The first entry needs to be searched after the last. Otherwise the
1985 * first entry might have moved due to a realloc during the search for
1986 * the last entry. */
1988 end_entry
= pdb_search_getentry(search
, end_idx
);
1989 *result
= pdb_search_getentry(search
, start_idx
);
1991 if (end_entry
!= NULL
)
1994 if (start_idx
>= search
->num_entries
)
1997 return search
->num_entries
- start_idx
;
2000 void pdb_search_destroy(struct pdb_search
*search
)
2005 if (!search
->search_ended
)
2006 search
->search_end(search
);
2008 talloc_destroy(search
->mem_ctx
);
2011 /*******************************************************************
2012 Create a pdb_methods structure and initialize it with the default
2013 operations. In this way a passdb module can simply implement
2014 the functionality it cares about. However, normally this is done
2015 in groups of related functions.
2016 *******************************************************************/
2018 NTSTATUS
make_pdb_method( struct pdb_methods
**methods
)
2020 /* allocate memory for the structure as its own talloc CTX */
2022 if ( !(*methods
= TALLOC_ZERO_P(NULL
, struct pdb_methods
) ) ) {
2023 return NT_STATUS_NO_MEMORY
;
2026 (*methods
)->setsampwent
= pdb_default_setsampwent
;
2027 (*methods
)->endsampwent
= pdb_default_endsampwent
;
2028 (*methods
)->getsampwent
= pdb_default_getsampwent
;
2029 (*methods
)->getsampwnam
= pdb_default_getsampwnam
;
2030 (*methods
)->getsampwsid
= pdb_default_getsampwsid
;
2031 (*methods
)->create_user
= pdb_default_create_user
;
2032 (*methods
)->delete_user
= pdb_default_delete_user
;
2033 (*methods
)->add_sam_account
= pdb_default_add_sam_account
;
2034 (*methods
)->update_sam_account
= pdb_default_update_sam_account
;
2035 (*methods
)->delete_sam_account
= pdb_default_delete_sam_account
;
2036 (*methods
)->rename_sam_account
= pdb_default_rename_sam_account
;
2037 (*methods
)->update_login_attempts
= pdb_default_update_login_attempts
;
2039 (*methods
)->getgrsid
= pdb_default_getgrsid
;
2040 (*methods
)->getgrgid
= pdb_default_getgrgid
;
2041 (*methods
)->getgrnam
= pdb_default_getgrnam
;
2042 (*methods
)->create_dom_group
= pdb_default_create_dom_group
;
2043 (*methods
)->delete_dom_group
= pdb_default_delete_dom_group
;
2044 (*methods
)->add_group_mapping_entry
= pdb_default_add_group_mapping_entry
;
2045 (*methods
)->update_group_mapping_entry
= pdb_default_update_group_mapping_entry
;
2046 (*methods
)->delete_group_mapping_entry
= pdb_default_delete_group_mapping_entry
;
2047 (*methods
)->enum_group_mapping
= pdb_default_enum_group_mapping
;
2048 (*methods
)->enum_group_members
= pdb_default_enum_group_members
;
2049 (*methods
)->enum_group_memberships
= pdb_default_enum_group_memberships
;
2050 (*methods
)->set_unix_primary_group
= pdb_default_set_unix_primary_group
;
2051 (*methods
)->add_groupmem
= pdb_default_add_groupmem
;
2052 (*methods
)->del_groupmem
= pdb_default_del_groupmem
;
2053 (*methods
)->find_alias
= pdb_default_find_alias
;
2054 (*methods
)->create_alias
= pdb_default_create_alias
;
2055 (*methods
)->delete_alias
= pdb_default_delete_alias
;
2056 (*methods
)->get_aliasinfo
= pdb_default_get_aliasinfo
;
2057 (*methods
)->set_aliasinfo
= pdb_default_set_aliasinfo
;
2058 (*methods
)->add_aliasmem
= pdb_default_add_aliasmem
;
2059 (*methods
)->del_aliasmem
= pdb_default_del_aliasmem
;
2060 (*methods
)->enum_aliasmem
= pdb_default_enum_aliasmem
;
2061 (*methods
)->enum_alias_memberships
= pdb_default_alias_memberships
;
2062 (*methods
)->lookup_rids
= pdb_default_lookup_rids
;
2063 (*methods
)->get_account_policy
= pdb_default_get_account_policy
;
2064 (*methods
)->set_account_policy
= pdb_default_set_account_policy
;
2065 (*methods
)->get_seq_num
= pdb_default_get_seq_num
;
2066 (*methods
)->uid_to_rid
= pdb_default_uid_to_rid
;
2067 (*methods
)->uid_to_sid
= pdb_default_uid_to_sid
;
2068 (*methods
)->gid_to_sid
= pdb_default_gid_to_sid
;
2069 (*methods
)->sid_to_id
= pdb_default_sid_to_id
;
2071 (*methods
)->search_users
= pdb_default_search_users
;
2072 (*methods
)->search_groups
= pdb_default_search_groups
;
2073 (*methods
)->search_aliases
= pdb_default_search_aliases
;
2075 return NT_STATUS_OK
;