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));
365 #ifdef ENABLE_BUILD_FARM_HACKS
367 DEBUG(1, ("Creating a faked user %s for build farm "
369 faked_create_user(name);
375 pwd = Get_Pwnam_alloc(tmp_ctx, name);
378 /* we have a valid SID coming out of this call */
380 status = samu_alloc_rid_unix( sam_pass, pwd );
384 if (!NT_STATUS_IS_OK(status)) {
385 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
389 if (!sid_peek_check_rid(get_global_sam_sid(),
390 pdb_get_user_sid(sam_pass), rid)) {
391 DEBUG(0, ("Could not get RID of fresh user\n"));
392 return NT_STATUS_INTERNAL_ERROR;
395 /* Disable the account on creation, it does not have a reasonable password yet. */
397 acb_info |= ACB_DISABLED;
399 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
401 status = pdb_add_sam_account(sam_pass);
403 TALLOC_FREE(sam_pass);
408 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
411 struct pdb_methods *pdb = pdb_get_methods();
412 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
415 /****************************************************************************
416 Delete a UNIX user on demand.
417 ****************************************************************************/
419 static int smb_delete_user(const char *unix_user)
426 if ( strequal( unix_user, "root" ) ) {
427 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
431 pstrcpy(del_script, lp_deluser_script());
434 all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
435 ret = smbrun(del_script,NULL);
437 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
442 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
444 struct samu *sam_acct)
448 status = pdb_delete_sam_account(sam_acct);
449 if (!NT_STATUS_IS_OK(status)) {
454 * Now delete the unix side ....
455 * note: we don't check if the delete really happened as the script is
456 * not necessary present and maybe the sysadmin doesn't want to delete
459 smb_delete_user( pdb_get_username(sam_acct) );
464 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
466 struct pdb_methods *pdb = pdb_get_methods();
469 /* sanity check to make sure we don't delete root */
471 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
472 return NT_STATUS_NO_SUCH_USER;
476 return NT_STATUS_ACCESS_DENIED;
479 return pdb->delete_user(pdb, mem_ctx, sam_acct);
482 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
484 struct pdb_methods *pdb = pdb_get_methods();
485 return pdb->add_sam_account(pdb, sam_acct);
488 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
490 struct pdb_methods *pdb = pdb_get_methods();
492 if (csamuser != NULL) {
493 TALLOC_FREE(csamuser);
497 return pdb->update_sam_account(pdb, sam_acct);
500 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
502 struct pdb_methods *pdb = pdb_get_methods();
504 if (csamuser != NULL) {
505 TALLOC_FREE(csamuser);
509 return pdb->delete_sam_account(pdb, sam_acct);
512 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
514 struct pdb_methods *pdb = pdb_get_methods();
517 if (csamuser != NULL) {
518 TALLOC_FREE(csamuser);
522 /* sanity check to make sure we don't rename root */
524 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
525 return NT_STATUS_NO_SUCH_USER;
529 return NT_STATUS_ACCESS_DENIED;
532 return pdb->rename_sam_account(pdb, oldname, newname);
535 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, BOOL success)
537 struct pdb_methods *pdb = pdb_get_methods();
538 return pdb->update_login_attempts(pdb, sam_acct, success);
541 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
543 struct pdb_methods *pdb = pdb_get_methods();
544 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
547 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
549 struct pdb_methods *pdb = pdb_get_methods();
550 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
553 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
555 struct pdb_methods *pdb = pdb_get_methods();
556 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
559 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
567 grp = getgrnam(name);
572 if (smb_create_group(name, &gid) != 0) {
573 return NT_STATUS_ACCESS_DENIED;
580 return NT_STATUS_ACCESS_DENIED;
583 if (pdb_rid_algorithm()) {
584 *rid = pdb_gid_to_group_rid( grp->gr_gid );
586 if (!pdb_new_rid(rid)) {
587 return NT_STATUS_ACCESS_DENIED;
591 sid_compose(&group_sid, get_global_sam_sid(), *rid);
593 return add_initial_entry(grp->gr_gid, sid_string_static(&group_sid),
594 SID_NAME_DOM_GRP, name, NULL);
597 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
600 struct pdb_methods *pdb = pdb_get_methods();
601 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
604 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
612 const char *grp_name;
614 sid_compose(&group_sid, get_global_sam_sid(), rid);
616 if (!get_domain_group_from_sid(group_sid, &map)) {
617 DEBUG(10, ("Could not find group for rid %d\n", rid));
618 return NT_STATUS_NO_SUCH_GROUP;
621 /* We need the group name for the smb_delete_group later on */
623 if (map.gid == (gid_t)-1) {
624 return NT_STATUS_NO_SUCH_GROUP;
627 grp = getgrgid(map.gid);
629 return NT_STATUS_NO_SUCH_GROUP;
632 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
634 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
635 if (grp_name == NULL) {
636 return NT_STATUS_NO_MEMORY;
639 status = pdb_delete_group_mapping_entry(group_sid);
641 if (!NT_STATUS_IS_OK(status)) {
645 /* Don't check the result of smb_delete_group */
647 smb_delete_group(grp_name);
652 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
654 struct pdb_methods *pdb = pdb_get_methods();
655 return pdb->delete_dom_group(pdb, mem_ctx, rid);
658 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
660 struct pdb_methods *pdb = pdb_get_methods();
661 return pdb->add_group_mapping_entry(pdb, map);
664 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
666 struct pdb_methods *pdb = pdb_get_methods();
667 return pdb->update_group_mapping_entry(pdb, map);
670 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
672 struct pdb_methods *pdb = pdb_get_methods();
673 return pdb->delete_group_mapping_entry(pdb, sid);
676 BOOL pdb_enum_group_mapping(const DOM_SID *sid, enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
677 size_t *p_num_entries, BOOL unix_only)
679 struct pdb_methods *pdb = pdb_get_methods();
680 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
681 pp_rmap, p_num_entries, unix_only));
684 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
686 uint32 **pp_member_rids,
687 size_t *p_num_members)
689 struct pdb_methods *pdb = pdb_get_methods();
692 result = pdb->enum_group_members(pdb, mem_ctx,
693 sid, pp_member_rids, p_num_members);
695 /* special check for rid 513 */
697 if ( !NT_STATUS_IS_OK( result ) ) {
700 sid_peek_rid( sid, &rid );
702 if ( rid == DOMAIN_GROUP_RID_USERS ) {
704 *pp_member_rids = NULL;
713 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
714 DOM_SID **pp_sids, gid_t **pp_gids,
715 size_t *p_num_groups)
717 struct pdb_methods *pdb = pdb_get_methods();
718 return pdb->enum_group_memberships(
720 pp_sids, pp_gids, p_num_groups);
723 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
725 struct samu *sampass)
730 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
731 (grp = getgrgid(gid)) == NULL) {
732 return NT_STATUS_INVALID_PRIMARY_GROUP;
735 if (smb_set_primary_group(grp->gr_name,
736 pdb_get_username(sampass)) != 0) {
737 return NT_STATUS_ACCESS_DENIED;
743 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
745 struct pdb_methods *pdb = pdb_get_methods();
746 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
750 * Helper function to see whether a user is in a group. We can't use
751 * user_in_group_sid here because this creates dependencies only smbd can
755 static BOOL pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
756 const DOM_SID *group_sid)
760 size_t i, num_groups;
762 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
768 for (i=0; i<num_groups; i++) {
769 if (sid_equal(group_sid, &sids[i])) {
776 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
781 DOM_SID group_sid, member_sid;
782 struct samu *account = NULL;
786 const char *group_name;
789 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
790 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
792 if (!get_domain_group_from_sid(group_sid, &map) ||
793 (map.gid == (gid_t)-1) ||
794 ((grp = getgrgid(map.gid)) == NULL)) {
795 return NT_STATUS_NO_SUCH_GROUP;
798 group_name = talloc_strdup(mem_ctx, grp->gr_name);
799 if (group_name == NULL) {
800 return NT_STATUS_NO_MEMORY;
803 if ( !(account = samu_new( NULL )) ) {
804 return NT_STATUS_NO_MEMORY;
807 if (!pdb_getsampwsid(account, &member_sid) ||
808 !sid_to_uid(&member_sid, &uid) ||
809 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
810 return NT_STATUS_NO_SUCH_USER;
813 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
814 return NT_STATUS_MEMBER_IN_GROUP;
818 * ok, the group exist, the user exist, the user is not in the group,
819 * we can (finally) add it to the group !
822 smb_add_user_group(group_name, pwd->pw_name);
824 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
825 return NT_STATUS_ACCESS_DENIED;
831 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
834 struct pdb_methods *pdb = pdb_get_methods();
835 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
838 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
843 DOM_SID group_sid, member_sid;
844 struct samu *account = NULL;
848 const char *group_name;
851 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
852 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
854 if (!get_domain_group_from_sid(group_sid, &map) ||
855 (map.gid == (gid_t)-1) ||
856 ((grp = getgrgid(map.gid)) == NULL)) {
857 return NT_STATUS_NO_SUCH_GROUP;
860 group_name = talloc_strdup(mem_ctx, grp->gr_name);
861 if (group_name == NULL) {
862 return NT_STATUS_NO_MEMORY;
865 if ( !(account = samu_new( NULL )) ) {
866 return NT_STATUS_NO_MEMORY;
869 if (!pdb_getsampwsid(account, &member_sid) ||
870 !sid_to_uid(&member_sid, &uid) ||
871 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
872 return NT_STATUS_NO_SUCH_USER;
875 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
876 return NT_STATUS_MEMBER_NOT_IN_GROUP;
880 * ok, the group exist, the user exist, the user is in the group,
881 * we can (finally) delete it from the group!
884 smb_delete_user_group(group_name, pwd->pw_name);
886 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
887 return NT_STATUS_ACCESS_DENIED;
893 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
896 struct pdb_methods *pdb = pdb_get_methods();
897 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
900 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
902 struct pdb_methods *pdb = pdb_get_methods();
903 return NT_STATUS_IS_OK(pdb->find_alias(pdb, name, sid));
906 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
908 struct pdb_methods *pdb = pdb_get_methods();
909 return pdb->create_alias(pdb, name, rid);
912 BOOL pdb_delete_alias(const DOM_SID *sid)
914 struct pdb_methods *pdb = pdb_get_methods();
915 return NT_STATUS_IS_OK(pdb->delete_alias(pdb, sid));
919 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
921 struct pdb_methods *pdb = pdb_get_methods();
922 return NT_STATUS_IS_OK(pdb->get_aliasinfo(pdb, sid, info));
925 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
927 struct pdb_methods *pdb = pdb_get_methods();
928 return NT_STATUS_IS_OK(pdb->set_aliasinfo(pdb, sid, info));
931 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
933 struct pdb_methods *pdb = pdb_get_methods();
934 return pdb->add_aliasmem(pdb, alias, member);
937 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
939 struct pdb_methods *pdb = pdb_get_methods();
940 return pdb->del_aliasmem(pdb, alias, member);
943 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
944 DOM_SID **pp_members, size_t *p_num_members)
946 struct pdb_methods *pdb = pdb_get_methods();
947 return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
950 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
951 const DOM_SID *domain_sid,
952 const DOM_SID *members, size_t num_members,
953 uint32 **pp_alias_rids,
954 size_t *p_num_alias_rids)
956 struct pdb_methods *pdb = pdb_get_methods();
957 return pdb->enum_alias_memberships(pdb, mem_ctx,
959 members, num_members,
964 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
968 enum SID_NAME_USE *attrs)
970 struct pdb_methods *pdb = pdb_get_methods();
971 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
974 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
978 enum SID_NAME_USE *attrs)
980 struct pdb_methods *pdb = pdb_get_methods();
981 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
984 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
986 struct pdb_methods *pdb = pdb_get_methods();
987 return NT_STATUS_IS_OK(pdb->get_account_policy(pdb, policy_index, value));
990 BOOL pdb_set_account_policy(int policy_index, uint32 value)
992 struct pdb_methods *pdb = pdb_get_methods();
993 return NT_STATUS_IS_OK(pdb->set_account_policy(pdb, policy_index, value));
996 BOOL pdb_get_seq_num(time_t *seq_num)
998 struct pdb_methods *pdb = pdb_get_methods();
999 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1002 BOOL pdb_uid_to_rid(uid_t uid, uint32 *rid)
1004 struct pdb_methods *pdb = pdb_get_methods();
1005 return pdb->uid_to_rid(pdb, uid, rid);
1008 BOOL pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1010 struct pdb_methods *pdb = pdb_get_methods();
1011 return pdb->gid_to_sid(pdb, gid, sid);
1014 BOOL pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1015 enum SID_NAME_USE *type)
1017 struct pdb_methods *pdb = pdb_get_methods();
1018 return pdb->sid_to_id(pdb, sid, id, type);
1021 BOOL pdb_rid_algorithm(void)
1023 struct pdb_methods *pdb = pdb_get_methods();
1024 return pdb->rid_algorithm(pdb);
1027 /********************************************************************
1028 Allocate a new RID from the passdb backend. Verify that it is free
1029 by calling lookup_global_sam_rid() to verify that the RID is not
1030 in use. This handles servers that have existing users or groups
1031 with add RIDs (assigned from previous algorithmic mappings)
1032 ********************************************************************/
1034 BOOL pdb_new_rid(uint32 *rid)
1036 struct pdb_methods *pdb = pdb_get_methods();
1037 const char *name = NULL;
1038 enum SID_NAME_USE type;
1039 uint32 allocated_rid = 0;
1043 if (pdb_rid_algorithm()) {
1044 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1049 if (algorithmic_rid_base() != BASE_RID) {
1050 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1051 "without algorithmic RIDs is chosen.\n"));
1052 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1053 "add', set the maximum used RID using\n"));
1054 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1058 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1059 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1063 /* Attempt to get an unused RID (max tires is 250...yes that it is
1064 and arbitrary number I pulkled out of my head). -- jerry */
1066 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1069 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1073 /* validate that the RID is not in use */
1075 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1082 if ( allocated_rid == 0 ) {
1083 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1087 *rid = allocated_rid;
1092 /***************************************************************
1093 Initialize the static context (at smbd startup etc).
1095 If uninitialised, context will auto-init on first use.
1096 ***************************************************************/
1098 BOOL initialize_password_db(BOOL reload)
1100 return (pdb_get_methods_reload(reload) != NULL);
1104 /***************************************************************************
1105 Default implementations of some functions.
1106 ****************************************************************************/
1108 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1110 return NT_STATUS_NO_SUCH_USER;
1113 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1115 return NT_STATUS_NO_SUCH_USER;
1118 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1120 return NT_STATUS_NOT_IMPLEMENTED;
1123 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1125 return NT_STATUS_NOT_IMPLEMENTED;
1128 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1130 return NT_STATUS_NOT_IMPLEMENTED;
1133 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1135 return NT_STATUS_NOT_IMPLEMENTED;
1138 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, BOOL success)
1140 return NT_STATUS_OK;
1143 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint32 acb_mask)
1145 return NT_STATUS_NOT_IMPLEMENTED;
1148 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, struct samu *user)
1150 return NT_STATUS_NOT_IMPLEMENTED;
1153 static void pdb_default_endsampwent(struct pdb_methods *methods)
1155 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1158 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1160 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1163 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1165 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1168 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1170 *seq_num = time(NULL);
1171 return NT_STATUS_OK;
1174 static BOOL pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1177 struct samu *sampw = NULL;
1178 struct passwd *unix_pw;
1181 unix_pw = sys_getpwuid( uid );
1184 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1185 "%lu\n", (unsigned long)uid));
1189 if ( !(sampw = samu_new( NULL )) ) {
1190 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1195 ret = NT_STATUS_IS_OK(
1196 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1200 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1201 "%s (%d)\n", unix_pw->pw_name, uid));
1206 ret = sid_peek_check_rid(get_global_sam_sid(),
1207 pdb_get_user_sid(sampw), rid);
1210 DEBUG(1, ("Could not peek rid out of sid %s\n",
1211 sid_string_static(pdb_get_user_sid(sampw))));
1218 static BOOL pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1223 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1227 sid_copy(sid, &map.sid);
1231 static BOOL pdb_default_sid_to_id(struct pdb_methods *methods,
1233 union unid_t *id, enum SID_NAME_USE *type)
1235 TALLOC_CTX *mem_ctx;
1240 mem_ctx = talloc_new(NULL);
1242 if (mem_ctx == NULL) {
1243 DEBUG(0, ("talloc_new failed\n"));
1247 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1248 /* Here we might have users as well as groups and aliases */
1249 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1253 if (sid_peek_check_rid(&global_sid_Builtin, sid, &rid)) {
1254 /* Here we only have aliases */
1256 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1257 DEBUG(10, ("Could not find map for sid %s\n",
1258 sid_string_static(sid)));
1261 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1262 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1263 DEBUG(10, ("Map for sid %s is a %s, expected an "
1264 "alias\n", sid_string_static(sid),
1265 sid_type_lookup(map.sid_name_use)));
1270 *type = SID_NAME_ALIAS;
1275 DEBUG(5, ("Sid %s is neither ours nor builtin, don't know it\n",
1276 sid_string_static(sid)));
1280 TALLOC_FREE(mem_ctx);
1284 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1285 uid_t uid, uid_t **pp_uids, size_t *p_num)
1289 for (i=0; i<*p_num; i++) {
1290 if ((*pp_uids)[i] == uid)
1294 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1296 if (*pp_uids == NULL)
1299 (*pp_uids)[*p_num] = uid;
1303 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1313 /* We only look at our own sam, so don't care about imported stuff */
1315 winbindd_env = getenv(WINBINDD_DONT_ENV);
1318 if ((grp = getgrgid(gid)) == NULL) {
1319 /* allow winbindd lookups, but only if they weren't already disabled */
1320 if ( !(winbindd_env && strequal(winbindd_env, "1")) ) {
1327 /* Primary group members */
1330 while ((pwd = getpwent()) != NULL) {
1331 if (pwd->pw_gid == gid) {
1332 add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1338 /* Secondary group members */
1340 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1341 struct passwd *pw = getpwnam(*gr);
1345 add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num);
1348 /* allow winbindd lookups, but only if they weren't already disabled */
1350 if ( !(winbindd_env && strequal(winbindd_env, "1")) ) {
1357 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1358 TALLOC_CTX *mem_ctx,
1359 const DOM_SID *group,
1360 uint32 **pp_member_rids,
1361 size_t *p_num_members)
1367 *pp_member_rids = NULL;
1370 if (!sid_to_gid(group, &gid))
1371 return NT_STATUS_NO_SUCH_GROUP;
1373 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1374 return NT_STATUS_NO_SUCH_GROUP;
1377 return NT_STATUS_OK;
1379 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1381 for (i=0; i<num_uids; i++) {
1384 uid_to_sid(&sid, uids[i]);
1386 if (!sid_check_is_in_our_domain(&sid)) {
1387 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1388 "in our domain\n"));
1392 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1393 *p_num_members += 1;
1396 return NT_STATUS_OK;
1399 NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1400 TALLOC_CTX *mem_ctx,
1404 size_t *p_num_groups)
1409 const char *username = pdb_get_username(user);
1412 /* Ignore the primary group SID. Honor the real Unix primary group.
1413 The primary group SID is only of real use to Windows clients */
1415 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1416 return NT_STATUS_NO_SUCH_USER;
1423 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1424 return NT_STATUS_NO_SUCH_USER;
1427 if (*p_num_groups == 0) {
1428 smb_panic("primary group missing");
1431 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1433 if (*pp_sids == NULL) {
1434 TALLOC_FREE(*pp_gids);
1435 return NT_STATUS_NO_MEMORY;
1438 for (i=0; i<*p_num_groups; i++) {
1439 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1442 return NT_STATUS_OK;
1445 /*******************************************************************
1446 Look up a rid in the SAM we're responsible for (i.e. passdb)
1447 ********************************************************************/
1449 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1451 enum SID_NAME_USE *psid_name_use,
1452 union unid_t *unix_id)
1454 struct samu *sam_account = NULL;
1459 *psid_name_use = SID_NAME_UNKNOWN;
1461 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1462 (unsigned int)rid));
1464 sid_copy(&sid, get_global_sam_sid());
1465 sid_append_rid(&sid, rid);
1467 /* see if the passdb can help us with the name of the user */
1469 if ( !(sam_account = samu_new( NULL )) ) {
1473 /* BEING ROOT BLLOCK */
1475 if (pdb_getsampwsid(sam_account, &sid)) {
1478 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1479 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1481 TALLOC_FREE(sam_account);
1485 *psid_name_use = SID_NAME_USER;
1487 TALLOC_FREE(sam_account);
1489 if (unix_id == NULL) {
1493 pw = Get_Pwnam(*name);
1497 unix_id->uid = pw->pw_uid;
1500 TALLOC_FREE(sam_account);
1502 ret = pdb_getgrsid(&map, sid);
1504 /* END BECOME_ROOT BLOCK */
1506 /* do not resolve SIDs to a name unless there is a valid
1507 gid associated with it */
1509 if ( ret && (map.gid != (gid_t)-1) ) {
1510 *name = talloc_strdup(mem_ctx, map.nt_name);
1511 *psid_name_use = map.sid_name_use;
1514 unix_id->gid = map.gid;
1520 /* Windows will always map RID 513 to something. On a non-domain
1521 controller, this gets mapped to SERVER\None. */
1524 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1528 if ( rid == DOMAIN_GROUP_RID_USERS ) {
1529 *name = talloc_strdup(mem_ctx, "None" );
1530 *psid_name_use = SID_NAME_DOM_GRP;
1538 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1539 const DOM_SID *domain_sid,
1543 enum SID_NAME_USE *attrs)
1547 BOOL have_mapped = False;
1548 BOOL have_unmapped = False;
1550 if (sid_check_is_builtin(domain_sid)) {
1552 for (i=0; i<num_rids; i++) {
1555 if (lookup_builtin_rid(names, rids[i], &name)) {
1556 attrs[i] = SID_NAME_ALIAS;
1558 DEBUG(5,("lookup_rids: %s:%d\n",
1559 names[i], attrs[i]));
1562 have_unmapped = True;
1563 attrs[i] = SID_NAME_UNKNOWN;
1569 /* Should not happen, but better check once too many */
1570 if (!sid_check_is_domain(domain_sid)) {
1571 return NT_STATUS_INVALID_HANDLE;
1574 for (i = 0; i < num_rids; i++) {
1577 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1580 return NT_STATUS_NO_MEMORY;
1583 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1586 have_unmapped = True;
1587 attrs[i] = SID_NAME_UNKNOWN;
1593 result = NT_STATUS_NONE_MAPPED;
1596 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1601 NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1602 const DOM_SID *domain_sid,
1606 enum SID_NAME_USE *attrs)
1610 BOOL have_mapped = False;
1611 BOOL have_unmapped = False;
1613 if (sid_check_is_builtin(domain_sid)) {
1615 for (i=0; i<num_names; i++) {
1618 if (lookup_builtin_name(names[i], &rid)) {
1619 attrs[i] = SID_NAME_ALIAS;
1621 DEBUG(5,("lookup_rids: %s:%d\n",
1622 names[i], attrs[i]));
1625 have_unmapped = True;
1626 attrs[i] = SID_NAME_UNKNOWN;
1632 /* Should not happen, but better check once too many */
1633 if (!sid_check_is_domain(domain_sid)) {
1634 return NT_STATUS_INVALID_HANDLE;
1637 for (i = 0; i < num_names; i++) {
1638 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1639 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1640 rids[i], attrs[i]));
1643 have_unmapped = True;
1644 attrs[i] = SID_NAME_UNKNOWN;
1650 result = NT_STATUS_NONE_MAPPED;
1653 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1658 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1660 TALLOC_CTX *mem_ctx;
1661 struct pdb_search *result;
1663 mem_ctx = talloc_init("pdb_search");
1664 if (mem_ctx == NULL) {
1665 DEBUG(0, ("talloc_init failed\n"));
1669 result = TALLOC_P(mem_ctx, struct pdb_search);
1670 if (result == NULL) {
1671 DEBUG(0, ("talloc failed\n"));
1675 result->mem_ctx = mem_ctx;
1676 result->type = type;
1677 result->cache = NULL;
1678 result->num_entries = 0;
1679 result->cache_size = 0;
1680 result->search_ended = False;
1682 /* Segfault appropriately if not initialized */
1683 result->next_entry = NULL;
1684 result->search_end = NULL;
1689 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1691 const char *account_name,
1692 const char *fullname,
1693 const char *description,
1694 struct samr_displayentry *entry)
1697 entry->acct_flags = acct_flags;
1699 if (account_name != NULL)
1700 entry->account_name = talloc_strdup(mem_ctx, account_name);
1702 entry->account_name = "";
1704 if (fullname != NULL)
1705 entry->fullname = talloc_strdup(mem_ctx, fullname);
1707 entry->fullname = "";
1709 if (description != NULL)
1710 entry->description = talloc_strdup(mem_ctx, description);
1712 entry->description = "";
1715 static BOOL user_search_in_progress = False;
1716 struct user_search {
1720 static BOOL next_entry_users(struct pdb_search *s,
1721 struct samr_displayentry *entry)
1723 struct user_search *state = (struct user_search *)s->private_data;
1724 struct samu *user = NULL;
1727 if ( !(user = samu_new( NULL )) ) {
1728 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1732 if (!pdb_getsampwent(user)) {
1737 if ((state->acct_flags != 0) &&
1738 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1743 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1744 pdb_get_acct_ctrl(user), pdb_get_username(user),
1745 pdb_get_fullname(user), pdb_get_acct_desc(user),
1752 static void search_end_users(struct pdb_search *search)
1755 user_search_in_progress = False;
1758 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1759 struct pdb_search *search,
1762 struct user_search *state;
1764 if (user_search_in_progress) {
1765 DEBUG(1, ("user search in progress\n"));
1769 if (!pdb_setsampwent(False, acct_flags)) {
1770 DEBUG(5, ("Could not start search\n"));
1774 user_search_in_progress = True;
1776 state = TALLOC_P(search->mem_ctx, struct user_search);
1777 if (state == NULL) {
1778 DEBUG(0, ("talloc failed\n"));
1782 state->acct_flags = acct_flags;
1784 search->private_data = state;
1785 search->next_entry = next_entry_users;
1786 search->search_end = search_end_users;
1790 struct group_search {
1792 size_t num_groups, current_group;
1795 static BOOL next_entry_groups(struct pdb_search *s,
1796 struct samr_displayentry *entry)
1798 struct group_search *state = (struct group_search *)s->private_data;
1800 GROUP_MAP *map = &state->groups[state->current_group];
1802 if (state->current_group == state->num_groups)
1805 sid_peek_rid(&map->sid, &rid);
1807 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1810 state->current_group += 1;
1814 static void search_end_groups(struct pdb_search *search)
1816 struct group_search *state =
1817 (struct group_search *)search->private_data;
1818 SAFE_FREE(state->groups);
1821 static BOOL pdb_search_grouptype(struct pdb_search *search,
1822 const DOM_SID *sid, enum SID_NAME_USE type)
1824 struct group_search *state;
1826 state = TALLOC_P(search->mem_ctx, struct group_search);
1827 if (state == NULL) {
1828 DEBUG(0, ("talloc failed\n"));
1832 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1834 DEBUG(0, ("Could not enum groups\n"));
1838 state->current_group = 0;
1839 search->private_data = state;
1840 search->next_entry = next_entry_groups;
1841 search->search_end = search_end_groups;
1845 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1846 struct pdb_search *search)
1848 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1851 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1852 struct pdb_search *search,
1856 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1859 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1862 if (idx < search->num_entries)
1863 return &search->cache[idx];
1865 if (search->search_ended)
1868 while (idx >= search->num_entries) {
1869 struct samr_displayentry entry;
1871 if (!search->next_entry(search, &entry)) {
1872 search->search_end(search);
1873 search->search_ended = True;
1877 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1878 entry, &search->cache, &search->num_entries,
1879 &search->cache_size);
1882 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1885 struct pdb_search *pdb_search_users(uint32 acct_flags)
1887 struct pdb_methods *pdb = pdb_get_methods();
1888 struct pdb_search *result;
1890 result = pdb_search_init(PDB_USER_SEARCH);
1891 if (result == NULL) {
1895 if (!pdb->search_users(pdb, result, acct_flags)) {
1896 talloc_destroy(result->mem_ctx);
1902 struct pdb_search *pdb_search_groups(void)
1904 struct pdb_methods *pdb = pdb_get_methods();
1905 struct pdb_search *result;
1907 result = pdb_search_init(PDB_GROUP_SEARCH);
1908 if (result == NULL) {
1912 if (!pdb->search_groups(pdb, result)) {
1913 talloc_destroy(result->mem_ctx);
1919 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1921 struct pdb_methods *pdb = pdb_get_methods();
1922 struct pdb_search *result;
1924 if (pdb == NULL) return NULL;
1926 result = pdb_search_init(PDB_ALIAS_SEARCH);
1927 if (result == NULL) return NULL;
1929 if (!pdb->search_aliases(pdb, result, sid)) {
1930 talloc_destroy(result->mem_ctx);
1936 uint32 pdb_search_entries(struct pdb_search *search,
1937 uint32 start_idx, uint32 max_entries,
1938 struct samr_displayentry **result)
1940 struct samr_displayentry *end_entry;
1941 uint32 end_idx = start_idx+max_entries-1;
1943 /* The first entry needs to be searched after the last. Otherwise the
1944 * first entry might have moved due to a realloc during the search for
1945 * the last entry. */
1947 end_entry = pdb_search_getentry(search, end_idx);
1948 *result = pdb_search_getentry(search, start_idx);
1950 if (end_entry != NULL)
1953 if (start_idx >= search->num_entries)
1956 return search->num_entries - start_idx;
1959 void pdb_search_destroy(struct pdb_search *search)
1964 if (!search->search_ended)
1965 search->search_end(search);
1967 talloc_destroy(search->mem_ctx);
1970 /*******************************************************************
1971 Create a pdb_methods structure and initialize it with the default
1972 operations. In this way a passdb module can simply implement
1973 the functionality it cares about. However, normally this is done
1974 in groups of related functions.
1975 *******************************************************************/
1977 NTSTATUS make_pdb_method( struct pdb_methods **methods )
1979 /* allocate memory for the structure as its own talloc CTX */
1981 if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
1982 return NT_STATUS_NO_MEMORY;
1985 (*methods)->setsampwent = pdb_default_setsampwent;
1986 (*methods)->endsampwent = pdb_default_endsampwent;
1987 (*methods)->getsampwent = pdb_default_getsampwent;
1988 (*methods)->getsampwnam = pdb_default_getsampwnam;
1989 (*methods)->getsampwsid = pdb_default_getsampwsid;
1990 (*methods)->create_user = pdb_default_create_user;
1991 (*methods)->delete_user = pdb_default_delete_user;
1992 (*methods)->add_sam_account = pdb_default_add_sam_account;
1993 (*methods)->update_sam_account = pdb_default_update_sam_account;
1994 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
1995 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
1996 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
1998 (*methods)->getgrsid = pdb_default_getgrsid;
1999 (*methods)->getgrgid = pdb_default_getgrgid;
2000 (*methods)->getgrnam = pdb_default_getgrnam;
2001 (*methods)->create_dom_group = pdb_default_create_dom_group;
2002 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2003 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2004 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2005 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2006 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2007 (*methods)->enum_group_members = pdb_default_enum_group_members;
2008 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2009 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2010 (*methods)->add_groupmem = pdb_default_add_groupmem;
2011 (*methods)->del_groupmem = pdb_default_del_groupmem;
2012 (*methods)->find_alias = pdb_default_find_alias;
2013 (*methods)->create_alias = pdb_default_create_alias;
2014 (*methods)->delete_alias = pdb_default_delete_alias;
2015 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2016 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2017 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2018 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2019 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2020 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2021 (*methods)->lookup_rids = pdb_default_lookup_rids;
2022 (*methods)->get_account_policy = pdb_default_get_account_policy;
2023 (*methods)->set_account_policy = pdb_default_set_account_policy;
2024 (*methods)->get_seq_num = pdb_default_get_seq_num;
2025 (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2026 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2027 (*methods)->sid_to_id = pdb_default_sid_to_id;
2029 (*methods)->search_users = pdb_default_search_users;
2030 (*methods)->search_groups = pdb_default_search_groups;
2031 (*methods)->search_aliases = pdb_default_search_aliases;
2033 return NT_STATUS_OK;