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 SID_NAME_USE *psid_name_use,
50 union unid_t *unix_id);
51 /*******************************************************************
52 Clean up uninitialised passwords. The only way to tell
53 that these values are not 'real' is that they do not
54 have a valid last set time. Instead, the value is fixed at 0.
55 Therefore we use that as the key for 'is this a valid password'.
56 However, it is perfectly valid to have a 'default' last change
57 time, such LDAP with a missing attribute would produce.
58 ********************************************************************/
60 static void pdb_force_pw_initialization(struct samu *pass)
62 const uint8 *lm_pwd, *nt_pwd;
64 /* only reset a password if the last set time has been
65 explicitly been set to zero. A default last set time
68 if ( (pdb_get_init_flags(pass, PDB_PASSLASTSET) != PDB_DEFAULT)
69 && (pdb_get_pass_last_set_time(pass) == 0) )
72 if (pdb_get_init_flags(pass, PDB_LMPASSWD) != PDB_DEFAULT)
74 lm_pwd = pdb_get_lanman_passwd(pass);
76 pdb_set_lanman_passwd(pass, NULL, PDB_CHANGED);
78 if (pdb_get_init_flags(pass, PDB_NTPASSWD) != PDB_DEFAULT)
80 nt_pwd = pdb_get_nt_passwd(pass);
82 pdb_set_nt_passwd(pass, NULL, PDB_CHANGED);
89 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
91 struct pdb_init_function_entry *entry = backends;
93 if(version != PASSDB_INTERFACE_VERSION) {
94 DEBUG(0,("Can't register passdb backend!\n"
95 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
96 "while this version of samba uses version %d\n",
97 version,PASSDB_INTERFACE_VERSION));
98 return NT_STATUS_OBJECT_TYPE_MISMATCH;
101 if (!name || !init) {
102 return NT_STATUS_INVALID_PARAMETER;
105 DEBUG(5,("Attempting to register passdb backend %s\n", name));
107 /* Check for duplicates */
108 if (pdb_find_backend_entry(name)) {
109 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
110 return NT_STATUS_OBJECT_NAME_COLLISION;
113 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
114 entry->name = smb_xstrdup(name);
117 DLIST_ADD(backends, entry);
118 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
122 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
124 struct pdb_init_function_entry *entry = backends;
127 if (strcmp(entry->name, name)==0) return entry;
134 /******************************************************************
135 Make a pdb_methods from scratch
136 *******************************************************************/
138 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
140 char *module_name = smb_xstrdup(selected);
141 char *module_location = NULL, *p;
142 struct pdb_init_function_entry *entry;
143 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
145 lazy_initialize_passdb();
147 p = strchr(module_name, ':');
151 module_location = p+1;
152 trim_char(module_location, ' ', ' ');
155 trim_char(module_name, ' ', ' ');
158 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
160 entry = pdb_find_backend_entry(module_name);
162 /* Try to find a module that contains this module */
164 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
165 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
166 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
167 SAFE_FREE(module_name);
168 return NT_STATUS_UNSUCCESSFUL;
172 /* No such backend found */
174 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
175 SAFE_FREE(module_name);
176 return NT_STATUS_INVALID_PARAMETER;
179 DEBUG(5,("Found pdb backend %s\n", module_name));
181 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
182 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
183 selected, nt_errstr(nt_status)));
184 SAFE_FREE(module_name);
188 SAFE_FREE(module_name);
190 DEBUG(5,("pdb backend %s has a valid init\n", selected));
195 /******************************************************************
196 Return an already initialised pdn_methods structure
197 *******************************************************************/
199 static struct pdb_methods *pdb_get_methods_reload( BOOL reload )
201 static struct pdb_methods *pdb = NULL;
203 if ( pdb && reload ) {
204 pdb->free_private_data( &(pdb->private_data) );
205 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
207 slprintf(msg, sizeof(msg)-1, "pdb_get_methods_reload: failed to get pdb methods for backend %s\n",
208 lp_passdb_backend() );
214 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
216 slprintf(msg, sizeof(msg)-1, "pdb_get_methods_reload: failed to get pdb methods for backend %s\n",
217 lp_passdb_backend() );
225 static struct pdb_methods *pdb_get_methods(void)
227 return pdb_get_methods_reload(False);
230 /******************************************************************
231 Backward compatibility functions for the original passdb interface
232 *******************************************************************/
234 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask)
236 struct pdb_methods *pdb = pdb_get_methods();
237 return NT_STATUS_IS_OK(pdb->setsampwent(pdb, update, acb_mask));
240 void pdb_endsampwent(void)
242 struct pdb_methods *pdb = pdb_get_methods();
243 pdb->endsampwent(pdb);
246 BOOL pdb_getsampwent(struct samu *user)
248 struct pdb_methods *pdb = pdb_get_methods();
250 if ( !NT_STATUS_IS_OK(pdb->getsampwent(pdb, user) ) ) {
253 pdb_force_pw_initialization( user );
257 BOOL pdb_getsampwnam(struct samu *sam_acct, const char *username)
259 struct pdb_methods *pdb = pdb_get_methods();
261 if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
266 TALLOC_FREE(csamuser);
269 pdb_force_pw_initialization( sam_acct );
271 csamuser = samu_new( NULL );
276 if (!pdb_copy_sam_account(csamuser, sam_acct)) {
277 TALLOC_FREE(csamuser);
284 /**********************************************************************
285 **********************************************************************/
287 BOOL guest_user_info( struct samu *user )
291 const char *guestname = lp_guestaccount();
293 if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
294 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
299 result = samu_set_unix(user, pwd );
303 return NT_STATUS_IS_OK( result );
306 /**********************************************************************
307 **********************************************************************/
309 BOOL pdb_getsampwsid(struct samu *sam_acct, const DOM_SID *sid)
311 struct pdb_methods *pdb = pdb_get_methods();
314 /* hard code the Guest RID of 501 */
316 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
319 if ( rid == DOMAIN_USER_RID_GUEST ) {
320 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
321 return guest_user_info( sam_acct );
324 /* check the cache first */
326 if ( csamuser && sid_equal(sid, pdb_get_user_sid(csamuser) ) )
327 return pdb_copy_sam_account(sam_acct, csamuser);
329 return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
332 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
333 TALLOC_CTX *tmp_ctx, const char *name,
334 uint32 acb_info, uint32 *rid)
336 struct samu *sam_pass;
340 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
341 return NT_STATUS_NO_MEMORY;
344 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
348 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
349 pstrcpy(add_script, lp_adduser_script());
351 pstrcpy(add_script, lp_addmachine_script());
354 if (add_script[0] == '\0') {
355 DEBUG(3, ("Could not find user %s and no add script "
357 return NT_STATUS_NO_SUCH_USER;
360 all_string_sub(add_script, "%u", name, sizeof(add_script));
361 add_ret = smbrun(add_script,NULL);
362 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
363 add_script, add_ret));
366 pwd = Get_Pwnam_alloc(tmp_ctx, name);
369 /* we have a valid SID coming out of this call */
371 status = samu_alloc_rid_unix( sam_pass, pwd );
375 if (!NT_STATUS_IS_OK(status)) {
376 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
380 if (!sid_peek_check_rid(get_global_sam_sid(),
381 pdb_get_user_sid(sam_pass), rid)) {
382 DEBUG(0, ("Could not get RID of fresh user\n"));
383 return NT_STATUS_INTERNAL_ERROR;
386 /* Disable the account on creation, it does not have a reasonable password yet. */
388 acb_info |= ACB_DISABLED;
390 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
392 status = pdb_add_sam_account(sam_pass);
394 TALLOC_FREE(sam_pass);
399 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
402 struct pdb_methods *pdb = pdb_get_methods();
403 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
406 /****************************************************************************
407 Delete a UNIX user on demand.
408 ****************************************************************************/
410 static int smb_delete_user(const char *unix_user)
417 if ( strequal( unix_user, "root" ) ) {
418 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
422 pstrcpy(del_script, lp_deluser_script());
425 all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
426 ret = smbrun(del_script,NULL);
428 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
433 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
435 struct samu *sam_acct)
439 status = pdb_delete_sam_account(sam_acct);
440 if (!NT_STATUS_IS_OK(status)) {
445 * Now delete the unix side ....
446 * note: we don't check if the delete really happened as the script is
447 * not necessary present and maybe the sysadmin doesn't want to delete
450 smb_delete_user( pdb_get_username(sam_acct) );
455 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
457 struct pdb_methods *pdb = pdb_get_methods();
460 /* sanity check to make sure we don't delete root */
462 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
463 return NT_STATUS_NO_SUCH_USER;
467 return NT_STATUS_ACCESS_DENIED;
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();
476 return pdb->add_sam_account(pdb, sam_acct);
479 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
481 struct pdb_methods *pdb = pdb_get_methods();
483 if (csamuser != NULL) {
484 TALLOC_FREE(csamuser);
488 return pdb->update_sam_account(pdb, sam_acct);
491 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
493 struct pdb_methods *pdb = pdb_get_methods();
495 if (csamuser != NULL) {
496 TALLOC_FREE(csamuser);
500 return pdb->delete_sam_account(pdb, sam_acct);
503 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
505 struct pdb_methods *pdb = pdb_get_methods();
508 if (csamuser != NULL) {
509 TALLOC_FREE(csamuser);
513 /* sanity check to make sure we don't rename root */
515 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
516 return NT_STATUS_NO_SUCH_USER;
520 return NT_STATUS_ACCESS_DENIED;
523 return pdb->rename_sam_account(pdb, oldname, newname);
526 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, BOOL success)
528 struct pdb_methods *pdb = pdb_get_methods();
529 return pdb->update_login_attempts(pdb, sam_acct, success);
532 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
534 struct pdb_methods *pdb = pdb_get_methods();
535 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
538 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
540 struct pdb_methods *pdb = pdb_get_methods();
541 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
544 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
546 struct pdb_methods *pdb = pdb_get_methods();
547 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
550 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
558 grp = getgrnam(name);
563 if (smb_create_group(name, &gid) != 0) {
564 return NT_STATUS_ACCESS_DENIED;
571 return NT_STATUS_ACCESS_DENIED;
574 if (pdb_rid_algorithm()) {
575 *rid = pdb_gid_to_group_rid( grp->gr_gid );
577 if (!pdb_new_rid(rid)) {
578 return NT_STATUS_ACCESS_DENIED;
582 sid_compose(&group_sid, get_global_sam_sid(), *rid);
584 return add_initial_entry(grp->gr_gid, sid_string_static(&group_sid),
585 SID_NAME_DOM_GRP, name, NULL);
588 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
591 struct pdb_methods *pdb = pdb_get_methods();
592 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
595 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
603 const char *grp_name;
605 sid_compose(&group_sid, get_global_sam_sid(), rid);
607 if (!get_domain_group_from_sid(group_sid, &map)) {
608 DEBUG(10, ("Could not find group for rid %d\n", rid));
609 return NT_STATUS_NO_SUCH_GROUP;
612 /* We need the group name for the smb_delete_group later on */
614 if (map.gid == (gid_t)-1) {
615 return NT_STATUS_NO_SUCH_GROUP;
618 grp = getgrgid(map.gid);
620 return NT_STATUS_NO_SUCH_GROUP;
623 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
625 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
626 if (grp_name == NULL) {
627 return NT_STATUS_NO_MEMORY;
630 status = pdb_delete_group_mapping_entry(group_sid);
632 if (!NT_STATUS_IS_OK(status)) {
636 /* Don't check the result of smb_delete_group */
638 smb_delete_group(grp_name);
643 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
645 struct pdb_methods *pdb = pdb_get_methods();
646 return pdb->delete_dom_group(pdb, mem_ctx, rid);
649 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
651 struct pdb_methods *pdb = pdb_get_methods();
652 return pdb->add_group_mapping_entry(pdb, map);
655 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
657 struct pdb_methods *pdb = pdb_get_methods();
658 return pdb->update_group_mapping_entry(pdb, map);
661 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
663 struct pdb_methods *pdb = pdb_get_methods();
664 return pdb->delete_group_mapping_entry(pdb, sid);
667 BOOL pdb_enum_group_mapping(const DOM_SID *sid, enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
668 size_t *p_num_entries, BOOL unix_only)
670 struct pdb_methods *pdb = pdb_get_methods();
671 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
672 pp_rmap, p_num_entries, unix_only));
675 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
677 uint32 **pp_member_rids,
678 size_t *p_num_members)
680 struct pdb_methods *pdb = pdb_get_methods();
683 result = pdb->enum_group_members(pdb, mem_ctx,
684 sid, pp_member_rids, p_num_members);
686 /* special check for rid 513 */
688 if ( !NT_STATUS_IS_OK( result ) ) {
691 sid_peek_rid( sid, &rid );
693 if ( rid == DOMAIN_GROUP_RID_USERS ) {
695 *pp_member_rids = NULL;
704 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
705 DOM_SID **pp_sids, gid_t **pp_gids,
706 size_t *p_num_groups)
708 struct pdb_methods *pdb = pdb_get_methods();
709 return pdb->enum_group_memberships(
711 pp_sids, pp_gids, p_num_groups);
714 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
716 struct samu *sampass)
721 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
722 (grp = getgrgid(gid)) == NULL) {
723 return NT_STATUS_INVALID_PRIMARY_GROUP;
726 if (smb_set_primary_group(grp->gr_name,
727 pdb_get_username(sampass)) != 0) {
728 return NT_STATUS_ACCESS_DENIED;
734 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
736 struct pdb_methods *pdb = pdb_get_methods();
737 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
741 * Helper function to see whether a user is in a group. We can't use
742 * user_in_group_sid here because this creates dependencies only smbd can
746 static BOOL pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
747 const DOM_SID *group_sid)
751 size_t i, num_groups;
753 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
759 for (i=0; i<num_groups; i++) {
760 if (sid_equal(group_sid, &sids[i])) {
767 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
772 DOM_SID group_sid, member_sid;
773 struct samu *account = NULL;
777 const char *group_name;
780 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
781 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
783 if (!get_domain_group_from_sid(group_sid, &map) ||
784 (map.gid == (gid_t)-1) ||
785 ((grp = getgrgid(map.gid)) == NULL)) {
786 return NT_STATUS_NO_SUCH_GROUP;
789 group_name = talloc_strdup(mem_ctx, grp->gr_name);
790 if (group_name == NULL) {
791 return NT_STATUS_NO_MEMORY;
794 if ( !(account = samu_new( NULL )) ) {
795 return NT_STATUS_NO_MEMORY;
798 if (!pdb_getsampwsid(account, &member_sid) ||
799 !sid_to_uid(&member_sid, &uid) ||
800 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
801 return NT_STATUS_NO_SUCH_USER;
804 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
805 return NT_STATUS_MEMBER_IN_GROUP;
809 * ok, the group exist, the user exist, the user is not in the group,
810 * we can (finally) add it to the group !
813 smb_add_user_group(group_name, pwd->pw_name);
815 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
816 return NT_STATUS_ACCESS_DENIED;
822 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
825 struct pdb_methods *pdb = pdb_get_methods();
826 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
829 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
834 DOM_SID group_sid, member_sid;
835 struct samu *account = NULL;
839 const char *group_name;
842 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
843 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
845 if (!get_domain_group_from_sid(group_sid, &map) ||
846 (map.gid == (gid_t)-1) ||
847 ((grp = getgrgid(map.gid)) == NULL)) {
848 return NT_STATUS_NO_SUCH_GROUP;
851 group_name = talloc_strdup(mem_ctx, grp->gr_name);
852 if (group_name == NULL) {
853 return NT_STATUS_NO_MEMORY;
856 if ( !(account = samu_new( NULL )) ) {
857 return NT_STATUS_NO_MEMORY;
860 if (!pdb_getsampwsid(account, &member_sid) ||
861 !sid_to_uid(&member_sid, &uid) ||
862 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
863 return NT_STATUS_NO_SUCH_USER;
866 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
867 return NT_STATUS_MEMBER_NOT_IN_GROUP;
871 * ok, the group exist, the user exist, the user is in the group,
872 * we can (finally) delete it from the group!
875 smb_delete_user_group(group_name, pwd->pw_name);
877 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
878 return NT_STATUS_ACCESS_DENIED;
884 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
887 struct pdb_methods *pdb = pdb_get_methods();
888 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
891 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
893 struct pdb_methods *pdb = pdb_get_methods();
894 return NT_STATUS_IS_OK(pdb->find_alias(pdb, name, sid));
897 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
899 struct pdb_methods *pdb = pdb_get_methods();
900 return pdb->create_alias(pdb, name, rid);
903 BOOL pdb_delete_alias(const DOM_SID *sid)
905 struct pdb_methods *pdb = pdb_get_methods();
906 return NT_STATUS_IS_OK(pdb->delete_alias(pdb, sid));
910 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
912 struct pdb_methods *pdb = pdb_get_methods();
913 return NT_STATUS_IS_OK(pdb->get_aliasinfo(pdb, sid, info));
916 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
918 struct pdb_methods *pdb = pdb_get_methods();
919 return NT_STATUS_IS_OK(pdb->set_aliasinfo(pdb, sid, info));
922 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
924 struct pdb_methods *pdb = pdb_get_methods();
925 return pdb->add_aliasmem(pdb, alias, member);
928 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
930 struct pdb_methods *pdb = pdb_get_methods();
931 return pdb->del_aliasmem(pdb, alias, member);
934 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
935 DOM_SID **pp_members, size_t *p_num_members)
937 struct pdb_methods *pdb = pdb_get_methods();
938 return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
941 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
942 const DOM_SID *domain_sid,
943 const DOM_SID *members, size_t num_members,
944 uint32 **pp_alias_rids,
945 size_t *p_num_alias_rids)
947 struct pdb_methods *pdb = pdb_get_methods();
948 return pdb->enum_alias_memberships(pdb, mem_ctx,
950 members, num_members,
955 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
959 enum SID_NAME_USE *attrs)
961 struct pdb_methods *pdb = pdb_get_methods();
962 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
965 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
969 enum SID_NAME_USE *attrs)
971 struct pdb_methods *pdb = pdb_get_methods();
972 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
975 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
977 struct pdb_methods *pdb = pdb_get_methods();
978 return NT_STATUS_IS_OK(pdb->get_account_policy(pdb, policy_index, value));
981 BOOL pdb_set_account_policy(int policy_index, uint32 value)
983 struct pdb_methods *pdb = pdb_get_methods();
984 return NT_STATUS_IS_OK(pdb->set_account_policy(pdb, policy_index, value));
987 BOOL pdb_get_seq_num(time_t *seq_num)
989 struct pdb_methods *pdb = pdb_get_methods();
990 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
993 BOOL pdb_uid_to_rid(uid_t uid, uint32 *rid)
995 struct pdb_methods *pdb = pdb_get_methods();
996 return pdb->uid_to_rid(pdb, uid, rid);
999 BOOL pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1001 struct pdb_methods *pdb = pdb_get_methods();
1002 return pdb->gid_to_sid(pdb, gid, sid);
1005 BOOL pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1006 enum SID_NAME_USE *type)
1008 struct pdb_methods *pdb = pdb_get_methods();
1009 return pdb->sid_to_id(pdb, sid, id, type);
1012 BOOL pdb_rid_algorithm(void)
1014 struct pdb_methods *pdb = pdb_get_methods();
1015 return pdb->rid_algorithm(pdb);
1018 /********************************************************************
1019 Allocate a new RID from the passdb backend. Verify that it is free
1020 by calling lookup_global_sam_rid() to verify that the RID is not
1021 in use. This handles servers that have existing users or groups
1022 with add RIDs (assigned from previous algorithmic mappings)
1023 ********************************************************************/
1025 BOOL pdb_new_rid(uint32 *rid)
1027 struct pdb_methods *pdb = pdb_get_methods();
1028 const char *name = NULL;
1029 enum SID_NAME_USE type;
1030 uint32 allocated_rid = 0;
1034 if (pdb_rid_algorithm()) {
1035 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1040 if (algorithmic_rid_base() != BASE_RID) {
1041 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1042 "without algorithmic RIDs is chosen.\n"));
1043 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1044 "add', set the maximum used RID using\n"));
1045 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1049 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1050 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1054 /* Attempt to get an unused RID (max tires is 250...yes that it is
1055 and arbitrary number I pulkled out of my head). -- jerry */
1057 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1060 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1064 /* validate that the RID is not in use */
1066 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1073 if ( allocated_rid == 0 ) {
1074 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1078 *rid = allocated_rid;
1083 /***************************************************************
1084 Initialize the static context (at smbd startup etc).
1086 If uninitialised, context will auto-init on first use.
1087 ***************************************************************/
1089 BOOL initialize_password_db(BOOL reload)
1091 return (pdb_get_methods_reload(reload) != NULL);
1095 /***************************************************************************
1096 Default implementations of some functions.
1097 ****************************************************************************/
1099 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1101 return NT_STATUS_NO_SUCH_USER;
1104 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1106 return NT_STATUS_NO_SUCH_USER;
1109 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1111 return NT_STATUS_NOT_IMPLEMENTED;
1114 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1116 return NT_STATUS_NOT_IMPLEMENTED;
1119 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1121 return NT_STATUS_NOT_IMPLEMENTED;
1124 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1126 return NT_STATUS_NOT_IMPLEMENTED;
1129 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, BOOL success)
1131 return NT_STATUS_OK;
1134 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint32 acb_mask)
1136 return NT_STATUS_NOT_IMPLEMENTED;
1139 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, struct samu *user)
1141 return NT_STATUS_NOT_IMPLEMENTED;
1144 static void pdb_default_endsampwent(struct pdb_methods *methods)
1146 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1149 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1151 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1154 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1156 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1159 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1161 *seq_num = time(NULL);
1162 return NT_STATUS_OK;
1165 static BOOL pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1168 struct samu *sampw = NULL;
1169 struct passwd *unix_pw;
1172 unix_pw = sys_getpwuid( uid );
1175 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1176 "%lu\n", (unsigned long)uid));
1180 if ( !(sampw = samu_new( NULL )) ) {
1181 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1186 ret = NT_STATUS_IS_OK(
1187 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1191 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1192 "%s (%d)\n", unix_pw->pw_name, uid));
1197 ret = sid_peek_check_rid(get_global_sam_sid(),
1198 pdb_get_user_sid(sampw), rid);
1201 DEBUG(1, ("Could not peek rid out of sid %s\n",
1202 sid_string_static(pdb_get_user_sid(sampw))));
1209 static BOOL pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1214 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1218 sid_copy(sid, &map.sid);
1222 static BOOL pdb_default_sid_to_id(struct pdb_methods *methods,
1224 union unid_t *id, enum SID_NAME_USE *type)
1226 TALLOC_CTX *mem_ctx;
1231 mem_ctx = talloc_new(NULL);
1233 if (mem_ctx == NULL) {
1234 DEBUG(0, ("talloc_new failed\n"));
1238 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1239 /* Here we might have users as well as groups and aliases */
1240 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1244 if (sid_peek_check_rid(&global_sid_Builtin, sid, &rid)) {
1245 /* Here we only have aliases */
1247 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1248 DEBUG(10, ("Could not find map for sid %s\n",
1249 sid_string_static(sid)));
1252 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1253 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1254 DEBUG(10, ("Map for sid %s is a %s, expected an "
1255 "alias\n", sid_string_static(sid),
1256 sid_type_lookup(map.sid_name_use)));
1261 *type = SID_NAME_ALIAS;
1266 DEBUG(5, ("Sid %s is neither ours nor builtin, don't know it\n",
1267 sid_string_static(sid)));
1271 TALLOC_FREE(mem_ctx);
1275 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1276 uid_t uid, uid_t **pp_uids, size_t *p_num)
1280 for (i=0; i<*p_num; i++) {
1281 if ((*pp_uids)[i] == uid)
1285 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1287 if (*pp_uids == NULL)
1290 (*pp_uids)[*p_num] = uid;
1294 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1304 /* We only look at our own sam, so don't care about imported stuff */
1306 winbindd_env = getenv(WINBINDD_DONT_ENV);
1309 if ((grp = getgrgid(gid)) == NULL) {
1310 /* allow winbindd lookups, but only if they weren't already disabled */
1311 if ( !(winbindd_env && strequal(winbindd_env, "1")) ) {
1318 /* Primary group members */
1321 while ((pwd = getpwent()) != NULL) {
1322 if (pwd->pw_gid == gid) {
1323 add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1329 /* Secondary group members */
1331 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1332 struct passwd *pw = getpwnam(*gr);
1336 add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num);
1339 /* allow winbindd lookups, but only if they weren't already disabled */
1341 if ( !(winbindd_env && strequal(winbindd_env, "1")) ) {
1348 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1349 TALLOC_CTX *mem_ctx,
1350 const DOM_SID *group,
1351 uint32 **pp_member_rids,
1352 size_t *p_num_members)
1358 *pp_member_rids = NULL;
1361 if (!sid_to_gid(group, &gid))
1362 return NT_STATUS_NO_SUCH_GROUP;
1364 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1365 return NT_STATUS_NO_SUCH_GROUP;
1368 return NT_STATUS_OK;
1370 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1372 for (i=0; i<num_uids; i++) {
1375 uid_to_sid(&sid, uids[i]);
1377 if (!sid_check_is_in_our_domain(&sid)) {
1378 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1379 "in our domain\n"));
1383 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1384 *p_num_members += 1;
1387 return NT_STATUS_OK;
1390 NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1391 TALLOC_CTX *mem_ctx,
1395 size_t *p_num_groups)
1400 const char *username = pdb_get_username(user);
1403 /* Ignore the primary group SID. Honor the real Unix primary group.
1404 The primary group SID is only of real use to Windows clients */
1406 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1407 return NT_STATUS_NO_SUCH_USER;
1414 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1415 return NT_STATUS_NO_SUCH_USER;
1418 if (*p_num_groups == 0) {
1419 smb_panic("primary group missing");
1422 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1424 if (*pp_sids == NULL) {
1425 TALLOC_FREE(*pp_gids);
1426 return NT_STATUS_NO_MEMORY;
1429 for (i=0; i<*p_num_groups; i++) {
1430 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1433 return NT_STATUS_OK;
1436 /*******************************************************************
1437 Look up a rid in the SAM we're responsible for (i.e. passdb)
1438 ********************************************************************/
1440 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1442 enum SID_NAME_USE *psid_name_use,
1443 union unid_t *unix_id)
1445 struct samu *sam_account = NULL;
1450 *psid_name_use = SID_NAME_UNKNOWN;
1452 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1453 (unsigned int)rid));
1455 sid_copy(&sid, get_global_sam_sid());
1456 sid_append_rid(&sid, rid);
1458 /* see if the passdb can help us with the name of the user */
1460 if ( !(sam_account = samu_new( NULL )) ) {
1464 /* BEING ROOT BLLOCK */
1466 if (pdb_getsampwsid(sam_account, &sid)) {
1469 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1470 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1472 TALLOC_FREE(sam_account);
1476 *psid_name_use = SID_NAME_USER;
1478 TALLOC_FREE(sam_account);
1480 if (unix_id == NULL) {
1484 pw = Get_Pwnam(*name);
1488 unix_id->uid = pw->pw_uid;
1491 TALLOC_FREE(sam_account);
1493 ret = pdb_getgrsid(&map, sid);
1495 /* END BECOME_ROOT BLOCK */
1497 /* do not resolve SIDs to a name unless there is a valid
1498 gid associated with it */
1500 if ( ret && (map.gid != (gid_t)-1) ) {
1501 *name = talloc_strdup(mem_ctx, map.nt_name);
1502 *psid_name_use = map.sid_name_use;
1505 unix_id->gid = map.gid;
1511 /* Windows will always map RID 513 to something. On a non-domain
1512 controller, this gets mapped to SERVER\None. */
1515 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1519 if ( rid == DOMAIN_GROUP_RID_USERS ) {
1520 *name = talloc_strdup(mem_ctx, "None" );
1521 *psid_name_use = SID_NAME_DOM_GRP;
1529 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1530 const DOM_SID *domain_sid,
1534 enum SID_NAME_USE *attrs)
1538 BOOL have_mapped = False;
1539 BOOL have_unmapped = False;
1541 if (sid_check_is_builtin(domain_sid)) {
1543 for (i=0; i<num_rids; i++) {
1546 if (lookup_builtin_rid(names, rids[i], &name)) {
1547 attrs[i] = SID_NAME_ALIAS;
1549 DEBUG(5,("lookup_rids: %s:%d\n",
1550 names[i], attrs[i]));
1553 have_unmapped = True;
1554 attrs[i] = SID_NAME_UNKNOWN;
1560 /* Should not happen, but better check once too many */
1561 if (!sid_check_is_domain(domain_sid)) {
1562 return NT_STATUS_INVALID_HANDLE;
1565 for (i = 0; i < num_rids; i++) {
1568 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1571 return NT_STATUS_NO_MEMORY;
1574 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1577 have_unmapped = True;
1578 attrs[i] = SID_NAME_UNKNOWN;
1584 result = NT_STATUS_NONE_MAPPED;
1587 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1592 NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1593 const DOM_SID *domain_sid,
1597 enum SID_NAME_USE *attrs)
1601 BOOL have_mapped = False;
1602 BOOL have_unmapped = False;
1604 if (sid_check_is_builtin(domain_sid)) {
1606 for (i=0; i<num_names; i++) {
1609 if (lookup_builtin_name(names[i], &rid)) {
1610 attrs[i] = SID_NAME_ALIAS;
1612 DEBUG(5,("lookup_rids: %s:%d\n",
1613 names[i], attrs[i]));
1616 have_unmapped = True;
1617 attrs[i] = SID_NAME_UNKNOWN;
1623 /* Should not happen, but better check once too many */
1624 if (!sid_check_is_domain(domain_sid)) {
1625 return NT_STATUS_INVALID_HANDLE;
1628 for (i = 0; i < num_names; i++) {
1629 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1630 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1631 rids[i], attrs[i]));
1634 have_unmapped = True;
1635 attrs[i] = SID_NAME_UNKNOWN;
1641 result = NT_STATUS_NONE_MAPPED;
1644 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1649 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1651 TALLOC_CTX *mem_ctx;
1652 struct pdb_search *result;
1654 mem_ctx = talloc_init("pdb_search");
1655 if (mem_ctx == NULL) {
1656 DEBUG(0, ("talloc_init failed\n"));
1660 result = TALLOC_P(mem_ctx, struct pdb_search);
1661 if (result == NULL) {
1662 DEBUG(0, ("talloc failed\n"));
1666 result->mem_ctx = mem_ctx;
1667 result->type = type;
1668 result->cache = NULL;
1669 result->num_entries = 0;
1670 result->cache_size = 0;
1671 result->search_ended = False;
1673 /* Segfault appropriately if not initialized */
1674 result->next_entry = NULL;
1675 result->search_end = NULL;
1680 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1682 const char *account_name,
1683 const char *fullname,
1684 const char *description,
1685 struct samr_displayentry *entry)
1688 entry->acct_flags = acct_flags;
1690 if (account_name != NULL)
1691 entry->account_name = talloc_strdup(mem_ctx, account_name);
1693 entry->account_name = "";
1695 if (fullname != NULL)
1696 entry->fullname = talloc_strdup(mem_ctx, fullname);
1698 entry->fullname = "";
1700 if (description != NULL)
1701 entry->description = talloc_strdup(mem_ctx, description);
1703 entry->description = "";
1706 static BOOL user_search_in_progress = False;
1707 struct user_search {
1711 static BOOL next_entry_users(struct pdb_search *s,
1712 struct samr_displayentry *entry)
1714 struct user_search *state = s->private_data;
1715 struct samu *user = NULL;
1718 if ( !(user = samu_new( NULL )) ) {
1719 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1723 if (!pdb_getsampwent(user)) {
1728 if ((state->acct_flags != 0) &&
1729 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1734 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1735 pdb_get_acct_ctrl(user), pdb_get_username(user),
1736 pdb_get_fullname(user), pdb_get_acct_desc(user),
1743 static void search_end_users(struct pdb_search *search)
1746 user_search_in_progress = False;
1749 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1750 struct pdb_search *search,
1753 struct user_search *state;
1755 if (user_search_in_progress) {
1756 DEBUG(1, ("user search in progress\n"));
1760 if (!pdb_setsampwent(False, acct_flags)) {
1761 DEBUG(5, ("Could not start search\n"));
1765 user_search_in_progress = True;
1767 state = TALLOC_P(search->mem_ctx, struct user_search);
1768 if (state == NULL) {
1769 DEBUG(0, ("talloc failed\n"));
1773 state->acct_flags = acct_flags;
1775 search->private_data = state;
1776 search->next_entry = next_entry_users;
1777 search->search_end = search_end_users;
1781 struct group_search {
1783 size_t num_groups, current_group;
1786 static BOOL next_entry_groups(struct pdb_search *s,
1787 struct samr_displayentry *entry)
1789 struct group_search *state = s->private_data;
1791 GROUP_MAP *map = &state->groups[state->current_group];
1793 if (state->current_group == state->num_groups)
1796 sid_peek_rid(&map->sid, &rid);
1798 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1801 state->current_group += 1;
1805 static void search_end_groups(struct pdb_search *search)
1807 struct group_search *state = search->private_data;
1808 SAFE_FREE(state->groups);
1811 static BOOL pdb_search_grouptype(struct pdb_search *search,
1812 const DOM_SID *sid, enum SID_NAME_USE type)
1814 struct group_search *state;
1816 state = TALLOC_P(search->mem_ctx, struct group_search);
1817 if (state == NULL) {
1818 DEBUG(0, ("talloc failed\n"));
1822 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1824 DEBUG(0, ("Could not enum groups\n"));
1828 state->current_group = 0;
1829 search->private_data = state;
1830 search->next_entry = next_entry_groups;
1831 search->search_end = search_end_groups;
1835 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1836 struct pdb_search *search)
1838 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1841 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1842 struct pdb_search *search,
1846 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1849 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1852 if (idx < search->num_entries)
1853 return &search->cache[idx];
1855 if (search->search_ended)
1858 while (idx >= search->num_entries) {
1859 struct samr_displayentry entry;
1861 if (!search->next_entry(search, &entry)) {
1862 search->search_end(search);
1863 search->search_ended = True;
1867 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1868 entry, &search->cache, &search->num_entries,
1869 &search->cache_size);
1872 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1875 struct pdb_search *pdb_search_users(uint32 acct_flags)
1877 struct pdb_methods *pdb = pdb_get_methods();
1878 struct pdb_search *result;
1880 result = pdb_search_init(PDB_USER_SEARCH);
1881 if (result == NULL) {
1885 if (!pdb->search_users(pdb, result, acct_flags)) {
1886 talloc_destroy(result->mem_ctx);
1892 struct pdb_search *pdb_search_groups(void)
1894 struct pdb_methods *pdb = pdb_get_methods();
1895 struct pdb_search *result;
1897 result = pdb_search_init(PDB_GROUP_SEARCH);
1898 if (result == NULL) {
1902 if (!pdb->search_groups(pdb, result)) {
1903 talloc_destroy(result->mem_ctx);
1909 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1911 struct pdb_methods *pdb = pdb_get_methods();
1912 struct pdb_search *result;
1914 if (pdb == NULL) return NULL;
1916 result = pdb_search_init(PDB_ALIAS_SEARCH);
1917 if (result == NULL) return NULL;
1919 if (!pdb->search_aliases(pdb, result, sid)) {
1920 talloc_destroy(result->mem_ctx);
1926 uint32 pdb_search_entries(struct pdb_search *search,
1927 uint32 start_idx, uint32 max_entries,
1928 struct samr_displayentry **result)
1930 struct samr_displayentry *end_entry;
1931 uint32 end_idx = start_idx+max_entries-1;
1933 /* The first entry needs to be searched after the last. Otherwise the
1934 * first entry might have moved due to a realloc during the search for
1935 * the last entry. */
1937 end_entry = pdb_search_getentry(search, end_idx);
1938 *result = pdb_search_getentry(search, start_idx);
1940 if (end_entry != NULL)
1943 if (start_idx >= search->num_entries)
1946 return search->num_entries - start_idx;
1949 void pdb_search_destroy(struct pdb_search *search)
1954 if (!search->search_ended)
1955 search->search_end(search);
1957 talloc_destroy(search->mem_ctx);
1960 /*******************************************************************
1961 Create a pdb_methods structure and initialize it with the default
1962 operations. In this way a passdb module can simply implement
1963 the functionality it cares about. However, normally this is done
1964 in groups of related functions.
1965 *******************************************************************/
1967 NTSTATUS make_pdb_method( struct pdb_methods **methods )
1969 /* allocate memory for the structure as its own talloc CTX */
1971 if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
1972 return NT_STATUS_NO_MEMORY;
1975 (*methods)->setsampwent = pdb_default_setsampwent;
1976 (*methods)->endsampwent = pdb_default_endsampwent;
1977 (*methods)->getsampwent = pdb_default_getsampwent;
1978 (*methods)->getsampwnam = pdb_default_getsampwnam;
1979 (*methods)->getsampwsid = pdb_default_getsampwsid;
1980 (*methods)->create_user = pdb_default_create_user;
1981 (*methods)->delete_user = pdb_default_delete_user;
1982 (*methods)->add_sam_account = pdb_default_add_sam_account;
1983 (*methods)->update_sam_account = pdb_default_update_sam_account;
1984 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
1985 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
1986 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
1988 (*methods)->getgrsid = pdb_default_getgrsid;
1989 (*methods)->getgrgid = pdb_default_getgrgid;
1990 (*methods)->getgrnam = pdb_default_getgrnam;
1991 (*methods)->create_dom_group = pdb_default_create_dom_group;
1992 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
1993 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
1994 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
1995 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
1996 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
1997 (*methods)->enum_group_members = pdb_default_enum_group_members;
1998 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
1999 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2000 (*methods)->add_groupmem = pdb_default_add_groupmem;
2001 (*methods)->del_groupmem = pdb_default_del_groupmem;
2002 (*methods)->find_alias = pdb_default_find_alias;
2003 (*methods)->create_alias = pdb_default_create_alias;
2004 (*methods)->delete_alias = pdb_default_delete_alias;
2005 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2006 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2007 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2008 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2009 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2010 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2011 (*methods)->lookup_rids = pdb_default_lookup_rids;
2012 (*methods)->get_account_policy = pdb_default_get_account_policy;
2013 (*methods)->set_account_policy = pdb_default_set_account_policy;
2014 (*methods)->get_seq_num = pdb_default_get_seq_num;
2015 (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2016 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2017 (*methods)->sid_to_id = pdb_default_sid_to_id;
2019 (*methods)->search_users = pdb_default_search_users;
2020 (*methods)->search_groups = pdb_default_search_groups;
2021 (*methods)->search_aliases = pdb_default_search_aliases;
2023 return NT_STATUS_OK;