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 SAM_ACCOUNT *csamuser = NULL;
33 static struct pdb_init_function_entry *backends = NULL;
35 static void lazy_initialize_passdb(void)
37 static BOOL initialized = False;
38 if(initialized)return;
43 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name);
44 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
46 enum SID_NAME_USE *psid_name_use,
47 union unid_t *unix_id);
48 /*******************************************************************
49 Clean up uninitialised passwords. The only way to tell
50 that these values are not 'real' is that they do not
51 have a valid last set time. Instead, the value is fixed at 0.
52 Therefore we use that as the key for 'is this a valid password'.
53 However, it is perfectly valid to have a 'default' last change
54 time, such LDAP with a missing attribute would produce.
55 ********************************************************************/
57 static void pdb_force_pw_initialization(SAM_ACCOUNT *pass)
59 const uint8 *lm_pwd, *nt_pwd;
61 /* only reset a password if the last set time has been
62 explicitly been set to zero. A default last set time
65 if ( (pdb_get_init_flags(pass, PDB_PASSLASTSET) != PDB_DEFAULT)
66 && (pdb_get_pass_last_set_time(pass) == 0) )
69 if (pdb_get_init_flags(pass, PDB_LMPASSWD) != PDB_DEFAULT)
71 lm_pwd = pdb_get_lanman_passwd(pass);
73 pdb_set_lanman_passwd(pass, NULL, PDB_CHANGED);
75 if (pdb_get_init_flags(pass, PDB_NTPASSWD) != PDB_DEFAULT)
77 nt_pwd = pdb_get_nt_passwd(pass);
79 pdb_set_nt_passwd(pass, NULL, PDB_CHANGED);
86 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
88 struct pdb_init_function_entry *entry = backends;
90 if(version != PASSDB_INTERFACE_VERSION) {
91 DEBUG(0,("Can't register passdb backend!\n"
92 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
93 "while this version of samba uses version %d\n",
94 version,PASSDB_INTERFACE_VERSION));
95 return NT_STATUS_OBJECT_TYPE_MISMATCH;
99 return NT_STATUS_INVALID_PARAMETER;
102 DEBUG(5,("Attempting to register passdb backend %s\n", name));
104 /* Check for duplicates */
105 if (pdb_find_backend_entry(name)) {
106 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
107 return NT_STATUS_OBJECT_NAME_COLLISION;
110 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
111 entry->name = smb_xstrdup(name);
114 DLIST_ADD(backends, entry);
115 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
119 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
121 struct pdb_init_function_entry *entry = backends;
124 if (strcmp(entry->name, name)==0) return entry;
131 /******************************************************************
132 Make a pdb_methods from scratch
133 *******************************************************************/
135 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
137 char *module_name = smb_xstrdup(selected);
138 char *module_location = NULL, *p;
139 struct pdb_init_function_entry *entry;
140 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
142 lazy_initialize_passdb();
144 p = strchr(module_name, ':');
148 module_location = p+1;
149 trim_char(module_location, ' ', ' ');
152 trim_char(module_name, ' ', ' ');
155 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
157 entry = pdb_find_backend_entry(module_name);
159 /* Try to find a module that contains this module */
161 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
162 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
163 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
164 SAFE_FREE(module_name);
165 return NT_STATUS_UNSUCCESSFUL;
169 /* No such backend found */
171 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
172 SAFE_FREE(module_name);
173 return NT_STATUS_INVALID_PARAMETER;
176 DEBUG(5,("Found pdb backend %s\n", module_name));
178 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
179 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
180 selected, nt_errstr(nt_status)));
181 SAFE_FREE(module_name);
185 SAFE_FREE(module_name);
187 DEBUG(5,("pdb backend %s has a valid init\n", selected));
192 /******************************************************************
193 Return an already initialised pdn_methods structure
194 *******************************************************************/
196 static struct pdb_methods *pdb_get_methods( BOOL reload )
198 static struct pdb_methods *pdb = NULL;
200 if ( pdb && reload ) {
201 pdb->free_private_data( &(pdb->private_data) );
202 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
208 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
216 /******************************************************************
217 Backward compatibility functions for the original passdb interface
218 *******************************************************************/
220 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask)
222 struct pdb_methods *pdb = pdb_get_methods(False);
228 return NT_STATUS_IS_OK(pdb->setsampwent(pdb, update, acb_mask));
231 void pdb_endsampwent(void)
233 struct pdb_methods *pdb = pdb_get_methods(False);
239 pdb->endsampwent(pdb);
242 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
244 struct pdb_methods *pdb = pdb_get_methods(False);
250 if ( !NT_STATUS_IS_OK(pdb->getsampwent(pdb, user) ) ) {
254 pdb_force_pw_initialization( user );
259 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username)
261 struct pdb_methods *pdb = pdb_get_methods(False);
267 if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
271 if (csamuser != NULL) {
272 pdb_free_sam(&csamuser);
276 pdb_force_pw_initialization( sam_acct );
277 pdb_copy_sam_account(sam_acct, &csamuser);
282 /**********************************************************************
283 **********************************************************************/
285 BOOL guest_user_info( SAM_ACCOUNT *user )
289 const char *guestname = lp_guestaccount();
291 if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
292 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
297 /* fill in from the users information */
299 ntstatus = pdb_fill_sam_pw( user, pwd );
301 return NT_STATUS_IS_OK(ntstatus);
305 /**********************************************************************
306 **********************************************************************/
308 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid)
310 struct pdb_methods *pdb;
313 if ( !(pdb = pdb_get_methods(False)) ) {
317 /* hard code the Guest RID of 501 */
319 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
322 if ( rid == DOMAIN_USER_RID_GUEST ) {
323 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
324 return guest_user_info( sam_acct );
327 /* check the cache first */
329 if ( csamuser && sid_equal(sid, pdb_get_user_sid(csamuser) ) )
330 return pdb_copy_sam_account(csamuser, &sam_acct);
332 return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
335 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
336 TALLOC_CTX *tmp_ctx, const char *name,
337 uint32 acb_info, uint32 *rid)
339 SAM_ACCOUNT *sam_pass = NULL;
342 if (Get_Pwnam_alloc(tmp_ctx, name) == NULL) {
346 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
347 pstrcpy(add_script, lp_adduser_script());
349 pstrcpy(add_script, lp_addmachine_script());
352 if (add_script[0] == '\0') {
353 DEBUG(3, ("Could not find user %s and no add script "
355 return NT_STATUS_NO_SUCH_USER;
358 all_string_sub(add_script, "%u", name, sizeof(add_script));
359 add_ret = smbrun(add_script,NULL);
360 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the "
361 "command `%s' gave %d\n",
362 add_script, add_ret));
365 /* implicit call to getpwnam() next. we have a valid SID coming out
369 status = pdb_init_sam_new(&sam_pass, name);
371 if (!NT_STATUS_IS_OK(status)) {
372 DEBUG(3, ("pdb_init_sam_new failed: %s\n", nt_errstr(status)));
376 if (!sid_peek_check_rid(get_global_sam_sid(),
377 pdb_get_user_sid(sam_pass), rid)) {
378 DEBUG(0, ("Could not get RID of fresh user\n"));
379 return NT_STATUS_INTERNAL_ERROR;
382 /* Disable the account on creation, it does not have a reasonable
385 acb_info |= ACB_DISABLED;
387 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
389 status = pdb_add_sam_account(sam_pass);
391 pdb_free_sam(&sam_pass);
396 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
399 struct pdb_methods *pdb = pdb_get_methods(False);
402 return NT_STATUS_UNSUCCESSFUL;
405 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
408 /****************************************************************************
409 Delete a UNIX user on demand.
410 ****************************************************************************/
412 static int smb_delete_user(const char *unix_user)
417 pstrcpy(del_script, lp_deluser_script());
420 all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
421 ret = smbrun(del_script,NULL);
423 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
428 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
430 SAM_ACCOUNT *sam_acct)
434 status = pdb_delete_sam_account(sam_acct);
435 if (!NT_STATUS_IS_OK(status)) {
440 * Now delete the unix side ....
441 * note: we don't check if the delete really happened as the script is
442 * not necessary present and maybe the sysadmin doesn't want to delete
445 smb_delete_user( pdb_get_username(sam_acct) );
450 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, SAM_ACCOUNT *sam_acct)
452 struct pdb_methods *pdb = pdb_get_methods(False);
455 return NT_STATUS_UNSUCCESSFUL;
458 return pdb->delete_user(pdb, mem_ctx, sam_acct);
461 NTSTATUS pdb_add_sam_account(SAM_ACCOUNT *sam_acct)
463 struct pdb_methods *pdb = pdb_get_methods(False);
466 return NT_STATUS_UNSUCCESSFUL;
469 return pdb->add_sam_account(pdb, sam_acct);
472 NTSTATUS pdb_update_sam_account(SAM_ACCOUNT *sam_acct)
474 struct pdb_methods *pdb = pdb_get_methods(False);
477 return NT_STATUS_UNSUCCESSFUL;
480 if (csamuser != NULL) {
481 pdb_free_sam(&csamuser);
485 return pdb->update_sam_account(pdb, sam_acct);
488 NTSTATUS pdb_delete_sam_account(SAM_ACCOUNT *sam_acct)
490 struct pdb_methods *pdb = pdb_get_methods(False);
493 return NT_STATUS_UNSUCCESSFUL;
496 if (csamuser != NULL) {
497 pdb_free_sam(&csamuser);
501 return pdb->delete_sam_account(pdb, sam_acct);
504 NTSTATUS pdb_rename_sam_account(SAM_ACCOUNT *oldname, const char *newname)
506 struct pdb_methods *pdb = pdb_get_methods(False);
509 return NT_STATUS_NOT_IMPLEMENTED;
512 if (csamuser != NULL) {
513 pdb_free_sam(&csamuser);
517 return pdb->rename_sam_account(pdb, oldname, newname);
520 NTSTATUS pdb_update_login_attempts(SAM_ACCOUNT *sam_acct, BOOL success)
522 struct pdb_methods *pdb = pdb_get_methods(False);
525 return NT_STATUS_NOT_IMPLEMENTED;
528 return pdb->update_login_attempts(pdb, sam_acct, success);
531 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
533 struct pdb_methods *pdb = pdb_get_methods(False);
539 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
542 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
544 struct pdb_methods *pdb = pdb_get_methods(False);
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(False);
561 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
564 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
572 grp = getgrnam(name);
577 if (smb_create_group(name, &gid) != 0) {
578 return NT_STATUS_ACCESS_DENIED;
585 return NT_STATUS_ACCESS_DENIED;
588 if (pdb_rid_algorithm()) {
589 *rid = pdb_gid_to_group_rid( grp->gr_gid );
591 if (!pdb_new_rid(rid)) {
592 return NT_STATUS_ACCESS_DENIED;
596 sid_compose(&group_sid, get_global_sam_sid(), *rid);
598 return add_initial_entry(grp->gr_gid, sid_string_static(&group_sid),
599 SID_NAME_DOM_GRP, name, NULL);
602 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
605 struct pdb_methods *pdb = pdb_get_methods(False);
608 return NT_STATUS_UNSUCCESSFUL;
611 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
614 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
622 const char *grp_name;
624 sid_compose(&group_sid, get_global_sam_sid(), rid);
626 if (!get_domain_group_from_sid(group_sid, &map)) {
627 DEBUG(10, ("Could not find group for rid %d\n", rid));
628 return NT_STATUS_NO_SUCH_GROUP;
631 /* We need the group name for the smb_delete_group later on */
633 if (map.gid == (gid_t)-1) {
634 return NT_STATUS_NO_SUCH_GROUP;
637 grp = getgrgid(map.gid);
639 return NT_STATUS_NO_SUCH_GROUP;
642 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
644 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
645 if (grp_name == NULL) {
646 return NT_STATUS_NO_MEMORY;
649 status = pdb_delete_group_mapping_entry(group_sid);
651 if (!NT_STATUS_IS_OK(status)) {
655 /* Don't check the result of smb_delete_group */
657 smb_delete_group(grp_name);
662 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
664 struct pdb_methods *pdb = pdb_get_methods(False);
667 return NT_STATUS_UNSUCCESSFUL;
670 return pdb->delete_dom_group(pdb, mem_ctx, rid);
673 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
675 struct pdb_methods *pdb = pdb_get_methods(False);
678 return NT_STATUS_UNSUCCESSFUL;
681 return pdb->add_group_mapping_entry(pdb, map);
684 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
686 struct pdb_methods *pdb = pdb_get_methods(False);
689 return NT_STATUS_UNSUCCESSFUL;
692 return pdb->update_group_mapping_entry(pdb, map);
695 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
697 struct pdb_methods *pdb = pdb_get_methods(False);
700 return NT_STATUS_UNSUCCESSFUL;
703 return pdb->delete_group_mapping_entry(pdb, sid);
706 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
707 size_t *p_num_entries, BOOL unix_only)
709 struct pdb_methods *pdb = pdb_get_methods(False);
715 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid_name_use,
716 pp_rmap, p_num_entries, unix_only));
719 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
721 uint32 **pp_member_rids,
722 size_t *p_num_members)
724 struct pdb_methods *pdb = pdb_get_methods(False);
727 return NT_STATUS_UNSUCCESSFUL;
730 return pdb->enum_group_members(pdb, mem_ctx, sid,
731 pp_member_rids, p_num_members);
734 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, SAM_ACCOUNT *user,
735 DOM_SID **pp_sids, gid_t **pp_gids,
736 size_t *p_num_groups)
738 struct pdb_methods *pdb = pdb_get_methods(False);
741 return NT_STATUS_UNSUCCESSFUL;
744 return pdb->enum_group_memberships(
746 pp_sids, pp_gids, p_num_groups);
749 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
751 SAM_ACCOUNT *sampass)
756 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
757 (grp = getgrgid(gid)) == NULL) {
758 return NT_STATUS_INVALID_PRIMARY_GROUP;
761 if (smb_set_primary_group(grp->gr_name,
762 pdb_get_username(sampass)) != 0) {
763 return NT_STATUS_ACCESS_DENIED;
769 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, SAM_ACCOUNT *user)
771 struct pdb_methods *pdb = pdb_get_methods(False);
774 return NT_STATUS_UNSUCCESSFUL;
777 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
781 * Helper function to see whether a user is in a group. We can't use
782 * user_in_group_sid here because this creates dependencies only smbd can
786 static BOOL pdb_user_in_group(TALLOC_CTX *mem_ctx, SAM_ACCOUNT *account,
787 const DOM_SID *group_sid)
791 size_t i, num_groups;
793 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
799 for (i=0; i<num_groups; i++) {
800 if (sid_equal(group_sid, &sids[i])) {
807 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
812 DOM_SID group_sid, member_sid;
813 SAM_ACCOUNT *account = NULL;
817 const char *group_name;
821 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
822 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
824 if (!get_domain_group_from_sid(group_sid, &map) ||
825 (map.gid == (gid_t)-1) ||
826 ((grp = getgrgid(map.gid)) == NULL)) {
827 return NT_STATUS_NO_SUCH_GROUP;
830 group_name = talloc_strdup(mem_ctx, grp->gr_name);
831 if (group_name == NULL) {
832 return NT_STATUS_NO_MEMORY;
835 if (!NT_STATUS_IS_OK(status = pdb_init_sam(&account))) {
839 if (!pdb_getsampwsid(account, &member_sid) ||
840 !sid_to_uid(&member_sid, &uid) ||
841 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
842 return NT_STATUS_NO_SUCH_USER;
845 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
846 return NT_STATUS_MEMBER_IN_GROUP;
850 * ok, the group exist, the user exist, the user is not in the group,
851 * we can (finally) add it to the group !
854 smb_add_user_group(group_name, pwd->pw_name);
856 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
857 return NT_STATUS_ACCESS_DENIED;
863 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
866 struct pdb_methods *pdb = pdb_get_methods(False);
869 return NT_STATUS_UNSUCCESSFUL;
872 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
875 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
880 DOM_SID group_sid, member_sid;
881 SAM_ACCOUNT *account = NULL;
885 const char *group_name;
889 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
890 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
892 if (!get_domain_group_from_sid(group_sid, &map) ||
893 (map.gid == (gid_t)-1) ||
894 ((grp = getgrgid(map.gid)) == NULL)) {
895 return NT_STATUS_NO_SUCH_GROUP;
898 group_name = talloc_strdup(mem_ctx, grp->gr_name);
899 if (group_name == NULL) {
900 return NT_STATUS_NO_MEMORY;
903 if (!NT_STATUS_IS_OK(status = pdb_init_sam(&account))) {
907 if (!pdb_getsampwsid(account, &member_sid) ||
908 !sid_to_uid(&member_sid, &uid) ||
909 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
910 return NT_STATUS_NO_SUCH_USER;
913 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
914 return NT_STATUS_MEMBER_NOT_IN_GROUP;
918 * ok, the group exist, the user exist, the user is in the group,
919 * we can (finally) delete it from the group!
922 smb_delete_user_group(group_name, pwd->pw_name);
924 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
925 return NT_STATUS_ACCESS_DENIED;
931 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
934 struct pdb_methods *pdb = pdb_get_methods(False);
937 return NT_STATUS_UNSUCCESSFUL;
940 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
943 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
945 struct pdb_methods *pdb = pdb_get_methods(False);
951 return NT_STATUS_IS_OK(pdb->find_alias(pdb,
955 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
957 struct pdb_methods *pdb = pdb_get_methods(False);
960 return NT_STATUS_NOT_IMPLEMENTED;
963 return pdb->create_alias(pdb, name, rid);
966 BOOL pdb_delete_alias(const DOM_SID *sid)
968 struct pdb_methods *pdb = pdb_get_methods(False);
974 return NT_STATUS_IS_OK(pdb->delete_alias(pdb,
979 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
981 struct pdb_methods *pdb = pdb_get_methods(False);
987 return NT_STATUS_IS_OK(pdb->get_aliasinfo(pdb, sid,
991 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
993 struct pdb_methods *pdb = pdb_get_methods(False);
999 return NT_STATUS_IS_OK(pdb->set_aliasinfo(pdb, sid,
1003 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1005 struct pdb_methods *pdb = pdb_get_methods(False);
1008 return NT_STATUS_UNSUCCESSFUL;
1011 return pdb->add_aliasmem(pdb, alias, member);
1014 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1016 struct pdb_methods *pdb = pdb_get_methods(False);
1019 return NT_STATUS_UNSUCCESSFUL;
1022 return pdb->del_aliasmem(pdb, alias, member);
1025 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
1026 DOM_SID **pp_members, size_t *p_num_members)
1028 struct pdb_methods *pdb = pdb_get_methods(False);
1031 return NT_STATUS_UNSUCCESSFUL;
1034 return pdb->enum_aliasmem(pdb, alias,
1035 pp_members, p_num_members);
1038 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1039 const DOM_SID *domain_sid,
1040 const DOM_SID *members, size_t num_members,
1041 uint32 **pp_alias_rids,
1042 size_t *p_num_alias_rids)
1044 struct pdb_methods *pdb = pdb_get_methods(False);
1047 return NT_STATUS_NOT_IMPLEMENTED;
1050 return pdb->enum_alias_memberships(pdb, mem_ctx,
1052 members, num_members,
1057 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
1063 struct pdb_methods *pdb = pdb_get_methods(False);
1066 return NT_STATUS_NOT_IMPLEMENTED;
1069 return pdb->lookup_rids(pdb, domain_sid,
1070 num_rids, rids, names, attrs);
1073 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
1079 struct pdb_methods *pdb = pdb_get_methods(False);
1082 return NT_STATUS_NOT_IMPLEMENTED;
1085 return pdb->lookup_names(pdb, domain_sid,
1086 num_names, names, rids, attrs);
1089 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
1091 struct pdb_methods *pdb = pdb_get_methods(False);
1097 return NT_STATUS_IS_OK(pdb->get_account_policy(pdb, policy_index, value));
1100 BOOL pdb_set_account_policy(int policy_index, uint32 value)
1102 struct pdb_methods *pdb = pdb_get_methods(False);
1108 return NT_STATUS_IS_OK(pdb->set_account_policy(pdb, policy_index, value));
1111 BOOL pdb_get_seq_num(time_t *seq_num)
1113 struct pdb_methods *pdb = pdb_get_methods(False);
1119 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1122 BOOL pdb_uid_to_rid(uid_t uid, uint32 *rid)
1124 struct pdb_methods *pdb = pdb_get_methods(False);
1130 return pdb->uid_to_rid(pdb, uid, rid);
1133 BOOL pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1135 struct pdb_methods *pdb = pdb_get_methods(False);
1141 return pdb->gid_to_sid(pdb, gid, sid);
1144 BOOL pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1145 enum SID_NAME_USE *type)
1147 struct pdb_methods *pdb = pdb_get_methods(False);
1153 return pdb->sid_to_id(pdb, sid, id, type);
1156 BOOL pdb_rid_algorithm(void)
1158 struct pdb_methods *pdb = pdb_get_methods(False);
1164 return pdb->rid_algorithm(pdb);
1167 BOOL pdb_new_rid(uint32 *rid)
1169 struct pdb_methods *pdb = pdb_get_methods(False);
1175 if (pdb_rid_algorithm()) {
1176 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1181 if (algorithmic_rid_base() != BASE_RID) {
1182 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1183 "without algorithmic RIDs is chosen.\n"));
1184 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1185 "add', set the maximum used RID using\n"));
1186 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1190 return pdb->new_rid(pdb, rid);
1193 /***************************************************************
1194 Initialize the static context (at smbd startup etc).
1196 If uninitialised, context will auto-init on first use.
1197 ***************************************************************/
1199 BOOL initialize_password_db(BOOL reload)
1201 return (pdb_get_methods(reload) != NULL);
1205 /***************************************************************************
1206 Default implementations of some functions.
1207 ****************************************************************************/
1209 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
1211 return NT_STATUS_NO_SUCH_USER;
1214 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1216 return NT_STATUS_NO_SUCH_USER;
1219 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1221 DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
1222 return NT_STATUS_NOT_IMPLEMENTED;
1225 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1227 return NT_STATUS_NOT_IMPLEMENTED;
1230 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
1232 return NT_STATUS_NOT_IMPLEMENTED;
1235 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd, const char *newname)
1237 return NT_STATUS_NOT_IMPLEMENTED;
1240 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, SAM_ACCOUNT *newpwd, BOOL success)
1242 return NT_STATUS_OK;
1245 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint16 acb_mask)
1247 return NT_STATUS_NOT_IMPLEMENTED;
1250 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1252 return NT_STATUS_NOT_IMPLEMENTED;
1255 static void pdb_default_endsampwent(struct pdb_methods *methods)
1257 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1260 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1262 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1265 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1267 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1270 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1272 *seq_num = time(NULL);
1273 return NT_STATUS_OK;
1276 static BOOL pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1279 SAM_ACCOUNT *sampw = NULL;
1280 struct passwd *unix_pw;
1283 unix_pw = sys_getpwuid( uid );
1286 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1287 "%lu\n", (unsigned long)uid));
1291 if ( !NT_STATUS_IS_OK(pdb_init_sam(&sampw)) ) {
1292 DEBUG(0,("pdb_default_uid_to_rid: failed to allocate "
1293 "SAM_ACCOUNT object\n"));
1298 ret = NT_STATUS_IS_OK(
1299 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1303 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1304 "%s (%d)\n", unix_pw->pw_name, uid));
1305 pdb_free_sam(&sampw);
1309 ret = sid_peek_check_rid(get_global_sam_sid(),
1310 pdb_get_user_sid(sampw), rid);
1313 DEBUG(1, ("Could not peek rid out of sid %s\n",
1314 sid_string_static(pdb_get_user_sid(sampw))));
1317 pdb_free_sam(&sampw);
1321 static BOOL pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1326 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1330 sid_copy(sid, &map.sid);
1334 static BOOL pdb_default_sid_to_id(struct pdb_methods *methods,
1336 union unid_t *id, enum SID_NAME_USE *type)
1338 TALLOC_CTX *mem_ctx;
1343 mem_ctx = talloc_new(NULL);
1345 if (mem_ctx == NULL) {
1346 DEBUG(0, ("talloc_new failed\n"));
1350 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1351 /* Here we might have users as well as groups and aliases */
1352 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1356 if (sid_peek_check_rid(&global_sid_Builtin, sid, &rid)) {
1357 /* Here we only have aliases */
1359 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1360 DEBUG(10, ("Could not find map for sid %s\n",
1361 sid_string_static(sid)));
1364 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1365 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1366 DEBUG(10, ("Map for sid %s is a %s, expected an "
1367 "alias\n", sid_string_static(sid),
1368 sid_type_lookup(map.sid_name_use)));
1373 *type = SID_NAME_ALIAS;
1378 DEBUG(5, ("Sid %s is neither ours nor builtin, don't know it\n",
1379 sid_string_static(sid)));
1383 TALLOC_FREE(mem_ctx);
1387 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1388 uid_t uid, uid_t **pp_uids, size_t *p_num)
1392 for (i=0; i<*p_num; i++) {
1393 if ((*pp_uids)[i] == uid)
1397 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1399 if (*pp_uids == NULL)
1402 (*pp_uids)[*p_num] = uid;
1406 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1415 /* We only look at our own sam, so don't care about imported stuff */
1419 if ((grp = getgrgid(gid)) == NULL) {
1424 /* Primary group members */
1427 while ((pwd = getpwent()) != NULL) {
1428 if (pwd->pw_gid == gid) {
1429 add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1435 /* Secondary group members */
1437 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1438 struct passwd *pw = getpwnam(*gr);
1442 add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num);
1450 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1451 TALLOC_CTX *mem_ctx,
1452 const DOM_SID *group,
1453 uint32 **pp_member_rids,
1454 size_t *p_num_members)
1460 *pp_member_rids = NULL;
1463 if (!sid_to_gid(group, &gid))
1464 return NT_STATUS_NO_SUCH_GROUP;
1466 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1467 return NT_STATUS_NO_SUCH_GROUP;
1470 return NT_STATUS_OK;
1472 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1474 for (i=0; i<num_uids; i++) {
1477 uid_to_sid(&sid, uids[i]);
1479 if (!sid_check_is_in_our_domain(&sid)) {
1480 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1481 "in our domain\n"));
1485 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1486 *p_num_members += 1;
1489 return NT_STATUS_OK;
1492 NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1493 TALLOC_CTX *mem_ctx,
1497 size_t *p_num_groups)
1502 const char *username = pdb_get_username(user);
1506 /* Ignore the primary group SID. Honor the real Unix primary group.
1507 The primary group SID is only of real use to Windows clients */
1509 if (!sid_to_gid(pdb_get_group_sid(user), &gid)) {
1510 DEBUG(10, ("sid_to_gid failed\n"));
1511 return NT_STATUS_NO_SUCH_USER;
1514 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1515 return NT_STATUS_NO_SUCH_USER;
1523 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1524 return NT_STATUS_NO_SUCH_USER;
1527 if (*p_num_groups == 0) {
1528 smb_panic("primary group missing");
1531 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1533 if (*pp_sids == NULL) {
1534 TALLOC_FREE(*pp_gids);
1535 return NT_STATUS_NO_MEMORY;
1538 for (i=0; i<*p_num_groups; i++) {
1539 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1542 return NT_STATUS_OK;
1545 /*******************************************************************
1546 Look up a rid in the SAM we're responsible for (i.e. passdb)
1547 ********************************************************************/
1549 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1551 enum SID_NAME_USE *psid_name_use,
1552 union unid_t *unix_id)
1554 SAM_ACCOUNT *sam_account = NULL;
1559 *psid_name_use = SID_NAME_UNKNOWN;
1561 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1562 (unsigned int)rid));
1564 sid_copy(&sid, get_global_sam_sid());
1565 sid_append_rid(&sid, rid);
1567 /* see if the passdb can help us with the name of the user */
1568 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
1572 /* BEING ROOT BLLOCK */
1574 if (pdb_getsampwsid(sam_account, &sid)) {
1577 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1578 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1579 *psid_name_use = SID_NAME_USER;
1581 pdb_free_sam(&sam_account);
1583 if (unix_id == NULL) {
1587 pw = Get_Pwnam(*name);
1591 unix_id->uid = pw->pw_uid;
1594 pdb_free_sam(&sam_account);
1596 ret = pdb_getgrsid(&map, sid);
1598 /* END BECOME_ROOT BLOCK */
1600 /* do not resolve SIDs to a name unless there is a valid
1601 gid associated with it */
1603 if ( ret && (map.gid != (gid_t)-1) ) {
1604 *name = talloc_strdup(mem_ctx, map.nt_name);
1605 *psid_name_use = map.sid_name_use;
1608 unix_id->gid = map.gid;
1614 /* Windows will always map RID 513 to something. On a non-domain
1615 controller, this gets mapped to SERVER\None. */
1618 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1622 if ( rid == DOMAIN_GROUP_RID_USERS ) {
1623 *name = talloc_strdup(mem_ctx, "None" );
1624 *psid_name_use = IS_DC ? SID_NAME_DOM_GRP : SID_NAME_ALIAS;
1632 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1633 const DOM_SID *domain_sid,
1641 BOOL have_mapped = False;
1642 BOOL have_unmapped = False;
1644 if (sid_check_is_builtin(domain_sid)) {
1646 for (i=0; i<num_rids; i++) {
1649 if (lookup_builtin_rid(names, rids[i], &name)) {
1650 attrs[i] = SID_NAME_ALIAS;
1652 DEBUG(5,("lookup_rids: %s:%d\n",
1653 names[i], attrs[i]));
1656 have_unmapped = True;
1657 attrs[i] = SID_NAME_UNKNOWN;
1663 /* Should not happen, but better check once too many */
1664 if (!sid_check_is_domain(domain_sid)) {
1665 return NT_STATUS_INVALID_HANDLE;
1668 for (i = 0; i < num_rids; i++) {
1671 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1674 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1677 have_unmapped = True;
1678 attrs[i] = SID_NAME_UNKNOWN;
1684 result = NT_STATUS_NONE_MAPPED;
1687 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1692 NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1693 const DOM_SID *domain_sid,
1701 BOOL have_mapped = False;
1702 BOOL have_unmapped = False;
1704 if (sid_check_is_builtin(domain_sid)) {
1706 for (i=0; i<num_names; i++) {
1709 if (lookup_builtin_name(names[i], &rid)) {
1710 attrs[i] = SID_NAME_ALIAS;
1712 DEBUG(5,("lookup_rids: %s:%d\n",
1713 names[i], attrs[i]));
1716 have_unmapped = True;
1717 attrs[i] = SID_NAME_UNKNOWN;
1723 /* Should not happen, but better check once too many */
1724 if (!sid_check_is_domain(domain_sid)) {
1725 return NT_STATUS_INVALID_HANDLE;
1728 for (i = 0; i < num_names; i++) {
1729 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1730 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1731 rids[i], attrs[i]));
1734 have_unmapped = True;
1735 attrs[i] = SID_NAME_UNKNOWN;
1741 result = NT_STATUS_NONE_MAPPED;
1744 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1749 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1751 TALLOC_CTX *mem_ctx;
1752 struct pdb_search *result;
1754 mem_ctx = talloc_init("pdb_search");
1755 if (mem_ctx == NULL) {
1756 DEBUG(0, ("talloc_init failed\n"));
1760 result = TALLOC_P(mem_ctx, struct pdb_search);
1761 if (result == NULL) {
1762 DEBUG(0, ("talloc failed\n"));
1766 result->mem_ctx = mem_ctx;
1767 result->type = type;
1768 result->cache = NULL;
1769 result->num_entries = 0;
1770 result->cache_size = 0;
1771 result->search_ended = False;
1773 /* Segfault appropriately if not initialized */
1774 result->next_entry = NULL;
1775 result->search_end = NULL;
1780 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1782 const char *account_name,
1783 const char *fullname,
1784 const char *description,
1785 struct samr_displayentry *entry)
1788 entry->acct_flags = acct_flags;
1790 if (account_name != NULL)
1791 entry->account_name = talloc_strdup(mem_ctx, account_name);
1793 entry->account_name = "";
1795 if (fullname != NULL)
1796 entry->fullname = talloc_strdup(mem_ctx, fullname);
1798 entry->fullname = "";
1800 if (description != NULL)
1801 entry->description = talloc_strdup(mem_ctx, description);
1803 entry->description = "";
1806 static BOOL user_search_in_progress = False;
1807 struct user_search {
1811 static BOOL next_entry_users(struct pdb_search *s,
1812 struct samr_displayentry *entry)
1814 struct user_search *state = s->private_data;
1815 SAM_ACCOUNT *user = NULL;
1819 status = pdb_init_sam(&user);
1820 if (!NT_STATUS_IS_OK(status)) {
1821 DEBUG(0, ("Could not pdb_init_sam\n"));
1825 if (!pdb_getsampwent(user)) {
1826 pdb_free_sam(&user);
1830 if ((state->acct_flags != 0) &&
1831 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1832 pdb_free_sam(&user);
1836 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1837 pdb_get_acct_ctrl(user), pdb_get_username(user),
1838 pdb_get_fullname(user), pdb_get_acct_desc(user),
1841 pdb_free_sam(&user);
1845 static void search_end_users(struct pdb_search *search)
1848 user_search_in_progress = False;
1851 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1852 struct pdb_search *search,
1855 struct user_search *state;
1857 if (user_search_in_progress) {
1858 DEBUG(1, ("user search in progress\n"));
1862 if (!pdb_setsampwent(False, acct_flags)) {
1863 DEBUG(5, ("Could not start search\n"));
1867 user_search_in_progress = True;
1869 state = TALLOC_P(search->mem_ctx, struct user_search);
1870 if (state == NULL) {
1871 DEBUG(0, ("talloc failed\n"));
1875 state->acct_flags = acct_flags;
1877 search->private_data = state;
1878 search->next_entry = next_entry_users;
1879 search->search_end = search_end_users;
1883 struct group_search {
1885 size_t num_groups, current_group;
1888 static BOOL next_entry_groups(struct pdb_search *s,
1889 struct samr_displayentry *entry)
1891 struct group_search *state = s->private_data;
1893 GROUP_MAP *map = &state->groups[state->current_group];
1895 if (state->current_group == state->num_groups)
1898 sid_peek_rid(&map->sid, &rid);
1900 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1903 state->current_group += 1;
1907 static void search_end_groups(struct pdb_search *search)
1909 struct group_search *state = search->private_data;
1910 SAFE_FREE(state->groups);
1913 static BOOL pdb_search_grouptype(struct pdb_search *search,
1914 enum SID_NAME_USE type)
1916 struct group_search *state;
1918 state = TALLOC_P(search->mem_ctx, struct group_search);
1919 if (state == NULL) {
1920 DEBUG(0, ("talloc failed\n"));
1924 if (!pdb_enum_group_mapping(type, &state->groups, &state->num_groups,
1926 DEBUG(0, ("Could not enum groups\n"));
1930 state->current_group = 0;
1931 search->private_data = state;
1932 search->next_entry = next_entry_groups;
1933 search->search_end = search_end_groups;
1937 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1938 struct pdb_search *search)
1940 return pdb_search_grouptype(search, SID_NAME_DOM_GRP);
1943 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1944 struct pdb_search *search,
1948 if (sid_equal(sid, get_global_sam_sid()))
1949 return pdb_search_grouptype(search, SID_NAME_ALIAS);
1951 if (sid_equal(sid, &global_sid_Builtin))
1952 return pdb_search_grouptype(search, SID_NAME_WKN_GRP);
1954 DEBUG(3, ("unknown domain sid: %s\n", sid_string_static(sid)));
1958 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1961 if (idx < search->num_entries)
1962 return &search->cache[idx];
1964 if (search->search_ended)
1967 while (idx >= search->num_entries) {
1968 struct samr_displayentry entry;
1970 if (!search->next_entry(search, &entry)) {
1971 search->search_end(search);
1972 search->search_ended = True;
1976 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1977 entry, &search->cache, &search->num_entries,
1978 &search->cache_size);
1981 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1984 struct pdb_search *pdb_search_users(uint16 acct_flags)
1986 struct pdb_methods *pdb = pdb_get_methods(False);
1987 struct pdb_search *result;
1989 if (pdb == NULL) return NULL;
1991 result = pdb_search_init(PDB_USER_SEARCH);
1992 if (result == NULL) return NULL;
1994 if (!pdb->search_users(pdb, result, acct_flags)) {
1995 talloc_destroy(result->mem_ctx);
2001 struct pdb_search *pdb_search_groups(void)
2003 struct pdb_methods *pdb = pdb_get_methods(False);
2004 struct pdb_search *result;
2006 if (pdb == NULL) return NULL;
2008 result = pdb_search_init(PDB_GROUP_SEARCH);
2009 if (result == NULL) return NULL;
2011 if (!pdb->search_groups(pdb, result)) {
2012 talloc_destroy(result->mem_ctx);
2018 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
2020 struct pdb_methods *pdb = pdb_get_methods(False);
2021 struct pdb_search *result;
2023 if (pdb == NULL) return NULL;
2025 result = pdb_search_init(PDB_ALIAS_SEARCH);
2026 if (result == NULL) return NULL;
2028 if (!pdb->search_aliases(pdb, result, sid)) {
2029 talloc_destroy(result->mem_ctx);
2035 uint32 pdb_search_entries(struct pdb_search *search,
2036 uint32 start_idx, uint32 max_entries,
2037 struct samr_displayentry **result)
2039 struct samr_displayentry *end_entry;
2040 uint32 end_idx = start_idx+max_entries-1;
2042 /* The first entry needs to be searched after the last. Otherwise the
2043 * first entry might have moved due to a realloc during the search for
2044 * the last entry. */
2046 end_entry = pdb_search_getentry(search, end_idx);
2047 *result = pdb_search_getentry(search, start_idx);
2049 if (end_entry != NULL)
2052 if (start_idx >= search->num_entries)
2055 return search->num_entries - start_idx;
2058 void pdb_search_destroy(struct pdb_search *search)
2063 if (!search->search_ended)
2064 search->search_end(search);
2066 talloc_destroy(search->mem_ctx);
2069 /*******************************************************************
2070 Create a pdb_methods structure and initialize it with the default
2071 operations. In this way a passdb module can simply implement
2072 the functionality it cares about. However, normally this is done
2073 in groups of related functions.
2074 *******************************************************************/
2076 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2078 /* allocate memory for the structure as its own talloc CTX */
2080 if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
2081 return NT_STATUS_NO_MEMORY;
2084 (*methods)->setsampwent = pdb_default_setsampwent;
2085 (*methods)->endsampwent = pdb_default_endsampwent;
2086 (*methods)->getsampwent = pdb_default_getsampwent;
2087 (*methods)->getsampwnam = pdb_default_getsampwnam;
2088 (*methods)->getsampwsid = pdb_default_getsampwsid;
2089 (*methods)->create_user = pdb_default_create_user;
2090 (*methods)->delete_user = pdb_default_delete_user;
2091 (*methods)->add_sam_account = pdb_default_add_sam_account;
2092 (*methods)->update_sam_account = pdb_default_update_sam_account;
2093 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2094 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2095 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2097 (*methods)->getgrsid = pdb_default_getgrsid;
2098 (*methods)->getgrgid = pdb_default_getgrgid;
2099 (*methods)->getgrnam = pdb_default_getgrnam;
2100 (*methods)->create_dom_group = pdb_default_create_dom_group;
2101 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2102 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2103 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2104 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2105 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2106 (*methods)->enum_group_members = pdb_default_enum_group_members;
2107 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2108 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2109 (*methods)->add_groupmem = pdb_default_add_groupmem;
2110 (*methods)->del_groupmem = pdb_default_del_groupmem;
2111 (*methods)->find_alias = pdb_default_find_alias;
2112 (*methods)->create_alias = pdb_default_create_alias;
2113 (*methods)->delete_alias = pdb_default_delete_alias;
2114 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2115 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2116 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2117 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2118 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2119 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2120 (*methods)->lookup_rids = pdb_default_lookup_rids;
2121 (*methods)->get_account_policy = pdb_default_get_account_policy;
2122 (*methods)->set_account_policy = pdb_default_set_account_policy;
2123 (*methods)->get_seq_num = pdb_default_get_seq_num;
2124 (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2125 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2126 (*methods)->sid_to_id = pdb_default_sid_to_id;
2128 (*methods)->search_users = pdb_default_search_users;
2129 (*methods)->search_groups = pdb_default_search_groups;
2130 (*methods)->search_aliases = pdb_default_search_aliases;
2132 return NT_STATUS_OK;