2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Jelmer Vernooij 2002
6 Copyright (C) Simo Sorce 2003
7 Copyright (C) Volker Lendecke 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_PASSDB
29 /* Cache of latest SAM lookup query */
31 static struct samu *csamuser = NULL;
33 static struct pdb_init_function_entry *backends = NULL;
35 static void lazy_initialize_passdb(void)
37 static BOOL initialized = False;
38 if(initialized)return;
43 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
45 enum SID_NAME_USE *psid_name_use,
46 union unid_t *unix_id);
47 /*******************************************************************
48 Clean up uninitialised passwords. The only way to tell
49 that these values are not 'real' is that they do not
50 have a valid last set time. Instead, the value is fixed at 0.
51 Therefore we use that as the key for 'is this a valid password'.
52 However, it is perfectly valid to have a 'default' last change
53 time, such LDAP with a missing attribute would produce.
54 ********************************************************************/
56 static void pdb_force_pw_initialization(struct samu *pass)
58 const uint8 *lm_pwd, *nt_pwd;
60 /* only reset a password if the last set time has been
61 explicitly been set to zero. A default last set time
64 if ( (pdb_get_init_flags(pass, PDB_PASSLASTSET) != PDB_DEFAULT)
65 && (pdb_get_pass_last_set_time(pass) == 0) )
68 if (pdb_get_init_flags(pass, PDB_LMPASSWD) != PDB_DEFAULT)
70 lm_pwd = pdb_get_lanman_passwd(pass);
72 pdb_set_lanman_passwd(pass, NULL, PDB_CHANGED);
74 if (pdb_get_init_flags(pass, PDB_NTPASSWD) != PDB_DEFAULT)
76 nt_pwd = pdb_get_nt_passwd(pass);
78 pdb_set_nt_passwd(pass, NULL, PDB_CHANGED);
85 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
87 struct pdb_init_function_entry *entry = backends;
89 if(version != PASSDB_INTERFACE_VERSION) {
90 DEBUG(0,("Can't register passdb backend!\n"
91 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
92 "while this version of samba uses version %d\n",
93 version,PASSDB_INTERFACE_VERSION));
94 return NT_STATUS_OBJECT_TYPE_MISMATCH;
98 return NT_STATUS_INVALID_PARAMETER;
101 DEBUG(5,("Attempting to register passdb backend %s\n", name));
103 /* Check for duplicates */
104 if (pdb_find_backend_entry(name)) {
105 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
106 return NT_STATUS_OBJECT_NAME_COLLISION;
109 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
110 entry->name = smb_xstrdup(name);
113 DLIST_ADD(backends, entry);
114 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
118 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
120 struct pdb_init_function_entry *entry = backends;
123 if (strcmp(entry->name, name)==0) return entry;
130 /******************************************************************
131 Make a pdb_methods from scratch
132 *******************************************************************/
134 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
136 char *module_name = smb_xstrdup(selected);
137 char *module_location = NULL, *p;
138 struct pdb_init_function_entry *entry;
139 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
141 lazy_initialize_passdb();
143 p = strchr(module_name, ':');
147 module_location = p+1;
148 trim_char(module_location, ' ', ' ');
151 trim_char(module_name, ' ', ' ');
154 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
156 entry = pdb_find_backend_entry(module_name);
158 /* Try to find a module that contains this module */
160 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
161 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
162 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
163 SAFE_FREE(module_name);
164 return NT_STATUS_UNSUCCESSFUL;
168 /* No such backend found */
170 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
171 SAFE_FREE(module_name);
172 return NT_STATUS_INVALID_PARAMETER;
175 DEBUG(5,("Found pdb backend %s\n", module_name));
177 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
178 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
179 selected, nt_errstr(nt_status)));
180 SAFE_FREE(module_name);
184 SAFE_FREE(module_name);
186 DEBUG(5,("pdb backend %s has a valid init\n", selected));
191 /******************************************************************
192 Return an already initialised pdn_methods structure
193 *******************************************************************/
195 static struct pdb_methods *pdb_get_methods_reload( BOOL reload )
197 static struct pdb_methods *pdb = NULL;
199 if ( pdb && reload ) {
200 pdb->free_private_data( &(pdb->private_data) );
201 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
207 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
215 static struct pdb_methods *pdb_get_methods(void)
217 return pdb_get_methods_reload(False);
220 /******************************************************************
221 Backward compatibility functions for the original passdb interface
222 *******************************************************************/
224 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask)
226 struct pdb_methods *pdb = pdb_get_methods();
232 return NT_STATUS_IS_OK(pdb->setsampwent(pdb, update, acb_mask));
235 void pdb_endsampwent(void)
237 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();
254 if ( !NT_STATUS_IS_OK(pdb->getsampwent(pdb, user) ) ) {
258 pdb_force_pw_initialization( user );
263 BOOL pdb_getsampwnam(struct samu *sam_acct, const char *username)
265 struct pdb_methods *pdb = pdb_get_methods();
271 if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
276 TALLOC_FREE(csamuser);
279 pdb_force_pw_initialization( sam_acct );
281 if ( (csamuser = samu_new( NULL )) != NULL ) {
282 pdb_copy_sam_account(csamuser, sam_acct);
288 /**********************************************************************
289 **********************************************************************/
291 BOOL guest_user_info( struct samu *user )
295 const char *guestname = lp_guestaccount();
297 if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
298 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
303 result = samu_set_unix(user, pwd );
307 return NT_STATUS_IS_OK( result );
310 /**********************************************************************
311 **********************************************************************/
313 BOOL pdb_getsampwsid(struct samu *sam_acct, const DOM_SID *sid)
315 struct pdb_methods *pdb;
318 if ( !(pdb = pdb_get_methods()) ) {
322 /* hard code the Guest RID of 501 */
324 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
327 if ( rid == DOMAIN_USER_RID_GUEST ) {
328 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
329 return guest_user_info( sam_acct );
332 /* check the cache first */
334 if ( csamuser && sid_equal(sid, pdb_get_user_sid(csamuser) ) )
335 return pdb_copy_sam_account(sam_acct, csamuser);
337 return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
340 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
341 TALLOC_CTX *tmp_ctx, const char *name,
342 uint32 acb_info, uint32 *rid)
344 struct samu *sam_pass;
348 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
349 return NT_STATUS_NO_MEMORY;
352 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
356 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
357 pstrcpy(add_script, lp_adduser_script());
359 pstrcpy(add_script, lp_addmachine_script());
362 if (add_script[0] == '\0') {
363 DEBUG(3, ("Could not find user %s and no add script "
365 return NT_STATUS_NO_SUCH_USER;
368 all_string_sub(add_script, "%u", name, sizeof(add_script));
369 add_ret = smbrun(add_script,NULL);
370 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
371 add_script, add_ret));
374 pwd = Get_Pwnam_alloc(tmp_ctx, name);
377 /* we have a valid SID coming out of this call */
379 status = samu_alloc_rid_unix( sam_pass, pwd );
383 if (!NT_STATUS_IS_OK(status)) {
384 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
388 if (!sid_peek_check_rid(get_global_sam_sid(),
389 pdb_get_user_sid(sam_pass), rid)) {
390 DEBUG(0, ("Could not get RID of fresh user\n"));
391 return NT_STATUS_INTERNAL_ERROR;
394 /* Disable the account on creation, it does not have a reasonable password yet. */
396 acb_info |= ACB_DISABLED;
398 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
400 status = pdb_add_sam_account(sam_pass);
402 TALLOC_FREE(sam_pass);
407 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
410 struct pdb_methods *pdb = pdb_get_methods();
413 return NT_STATUS_UNSUCCESSFUL;
416 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
419 /****************************************************************************
420 Delete a UNIX user on demand.
421 ****************************************************************************/
423 static int smb_delete_user(const char *unix_user)
430 if ( strequal( unix_user, "root" ) ) {
431 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
435 pstrcpy(del_script, lp_deluser_script());
438 all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
439 ret = smbrun(del_script,NULL);
441 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
446 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
448 struct samu *sam_acct)
452 status = pdb_delete_sam_account(sam_acct);
453 if (!NT_STATUS_IS_OK(status)) {
458 * Now delete the unix side ....
459 * note: we don't check if the delete really happened as the script is
460 * not necessary present and maybe the sysadmin doesn't want to delete
463 smb_delete_user( pdb_get_username(sam_acct) );
468 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
470 struct pdb_methods *pdb = pdb_get_methods();
474 return NT_STATUS_UNSUCCESSFUL;
477 /* sanity check to make sure we don't delete root */
479 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
480 return NT_STATUS_NO_SUCH_USER;
484 return NT_STATUS_ACCESS_DENIED;
487 return pdb->delete_user(pdb, mem_ctx, sam_acct);
490 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
492 struct pdb_methods *pdb = pdb_get_methods();
495 return NT_STATUS_UNSUCCESSFUL;
498 return pdb->add_sam_account(pdb, sam_acct);
501 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
503 struct pdb_methods *pdb = pdb_get_methods();
506 return NT_STATUS_UNSUCCESSFUL;
509 if (csamuser != NULL) {
510 TALLOC_FREE(csamuser);
514 return pdb->update_sam_account(pdb, sam_acct);
517 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
519 struct pdb_methods *pdb = pdb_get_methods();
522 return NT_STATUS_UNSUCCESSFUL;
525 if (csamuser != NULL) {
526 TALLOC_FREE(csamuser);
530 return pdb->delete_sam_account(pdb, sam_acct);
533 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
535 struct pdb_methods *pdb = pdb_get_methods();
539 return NT_STATUS_NOT_IMPLEMENTED;
542 if (csamuser != NULL) {
543 TALLOC_FREE(csamuser);
547 /* sanity check to make sure we don't rename root */
549 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
550 return NT_STATUS_NO_SUCH_USER;
554 return NT_STATUS_ACCESS_DENIED;
557 return pdb->rename_sam_account(pdb, oldname, newname);
560 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, BOOL success)
562 struct pdb_methods *pdb = pdb_get_methods();
565 return NT_STATUS_NOT_IMPLEMENTED;
568 return pdb->update_login_attempts(pdb, sam_acct, success);
571 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
573 struct pdb_methods *pdb = pdb_get_methods();
579 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
582 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
584 struct pdb_methods *pdb = pdb_get_methods();
590 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
593 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
595 struct pdb_methods *pdb = pdb_get_methods();
601 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
604 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
612 grp = getgrnam(name);
617 if (smb_create_group(name, &gid) != 0) {
618 return NT_STATUS_ACCESS_DENIED;
625 return NT_STATUS_ACCESS_DENIED;
628 if (pdb_rid_algorithm()) {
629 *rid = pdb_gid_to_group_rid( grp->gr_gid );
631 if (!pdb_new_rid(rid)) {
632 return NT_STATUS_ACCESS_DENIED;
636 sid_compose(&group_sid, get_global_sam_sid(), *rid);
638 return add_initial_entry(grp->gr_gid, sid_string_static(&group_sid),
639 SID_NAME_DOM_GRP, name, NULL);
642 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
645 struct pdb_methods *pdb = pdb_get_methods();
648 return NT_STATUS_UNSUCCESSFUL;
651 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
654 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
662 const char *grp_name;
664 sid_compose(&group_sid, get_global_sam_sid(), rid);
666 if (!get_domain_group_from_sid(group_sid, &map)) {
667 DEBUG(10, ("Could not find group for rid %d\n", rid));
668 return NT_STATUS_NO_SUCH_GROUP;
671 /* We need the group name for the smb_delete_group later on */
673 if (map.gid == (gid_t)-1) {
674 return NT_STATUS_NO_SUCH_GROUP;
677 grp = getgrgid(map.gid);
679 return NT_STATUS_NO_SUCH_GROUP;
682 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
684 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
685 if (grp_name == NULL) {
686 return NT_STATUS_NO_MEMORY;
689 status = pdb_delete_group_mapping_entry(group_sid);
691 if (!NT_STATUS_IS_OK(status)) {
695 /* Don't check the result of smb_delete_group */
697 smb_delete_group(grp_name);
702 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
704 struct pdb_methods *pdb = pdb_get_methods();
707 return NT_STATUS_UNSUCCESSFUL;
710 return pdb->delete_dom_group(pdb, mem_ctx, rid);
713 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
715 struct pdb_methods *pdb = pdb_get_methods();
718 return NT_STATUS_UNSUCCESSFUL;
721 return pdb->add_group_mapping_entry(pdb, map);
724 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
726 struct pdb_methods *pdb = pdb_get_methods();
729 return NT_STATUS_UNSUCCESSFUL;
732 return pdb->update_group_mapping_entry(pdb, map);
735 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
737 struct pdb_methods *pdb = pdb_get_methods();
740 return NT_STATUS_UNSUCCESSFUL;
743 return pdb->delete_group_mapping_entry(pdb, sid);
746 BOOL pdb_enum_group_mapping(const DOM_SID *sid, enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
747 size_t *p_num_entries, BOOL unix_only)
749 struct pdb_methods *pdb = pdb_get_methods();
755 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
756 pp_rmap, p_num_entries, unix_only));
759 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
761 uint32 **pp_member_rids,
762 size_t *p_num_members)
764 struct pdb_methods *pdb = pdb_get_methods();
768 return NT_STATUS_UNSUCCESSFUL;
771 result = pdb->enum_group_members(pdb, mem_ctx,
772 sid, pp_member_rids, p_num_members);
774 /* special check for rid 513 */
776 if ( !NT_STATUS_IS_OK( result ) ) {
779 sid_peek_rid( sid, &rid );
781 if ( rid == DOMAIN_GROUP_RID_USERS ) {
783 *pp_member_rids = NULL;
792 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
793 DOM_SID **pp_sids, gid_t **pp_gids,
794 size_t *p_num_groups)
796 struct pdb_methods *pdb = pdb_get_methods();
799 return NT_STATUS_UNSUCCESSFUL;
802 return pdb->enum_group_memberships(
804 pp_sids, pp_gids, p_num_groups);
807 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
809 struct samu *sampass)
814 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
815 (grp = getgrgid(gid)) == NULL) {
816 return NT_STATUS_INVALID_PRIMARY_GROUP;
819 if (smb_set_primary_group(grp->gr_name,
820 pdb_get_username(sampass)) != 0) {
821 return NT_STATUS_ACCESS_DENIED;
827 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
829 struct pdb_methods *pdb = pdb_get_methods();
832 return NT_STATUS_UNSUCCESSFUL;
835 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
839 * Helper function to see whether a user is in a group. We can't use
840 * user_in_group_sid here because this creates dependencies only smbd can
844 static BOOL pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
845 const DOM_SID *group_sid)
849 size_t i, num_groups;
851 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
857 for (i=0; i<num_groups; i++) {
858 if (sid_equal(group_sid, &sids[i])) {
865 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
870 DOM_SID group_sid, member_sid;
871 struct samu *account = NULL;
875 const char *group_name;
878 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
879 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
881 if (!get_domain_group_from_sid(group_sid, &map) ||
882 (map.gid == (gid_t)-1) ||
883 ((grp = getgrgid(map.gid)) == NULL)) {
884 return NT_STATUS_NO_SUCH_GROUP;
887 group_name = talloc_strdup(mem_ctx, grp->gr_name);
888 if (group_name == NULL) {
889 return NT_STATUS_NO_MEMORY;
892 if ( !(account = samu_new( NULL )) ) {
893 return NT_STATUS_NO_MEMORY;
896 if (!pdb_getsampwsid(account, &member_sid) ||
897 !sid_to_uid(&member_sid, &uid) ||
898 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
899 return NT_STATUS_NO_SUCH_USER;
902 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
903 return NT_STATUS_MEMBER_IN_GROUP;
907 * ok, the group exist, the user exist, the user is not in the group,
908 * we can (finally) add it to the group !
911 smb_add_user_group(group_name, pwd->pw_name);
913 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
914 return NT_STATUS_ACCESS_DENIED;
920 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
923 struct pdb_methods *pdb = pdb_get_methods();
926 return NT_STATUS_UNSUCCESSFUL;
929 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
932 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
937 DOM_SID group_sid, member_sid;
938 struct samu *account = NULL;
942 const char *group_name;
945 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
946 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
948 if (!get_domain_group_from_sid(group_sid, &map) ||
949 (map.gid == (gid_t)-1) ||
950 ((grp = getgrgid(map.gid)) == NULL)) {
951 return NT_STATUS_NO_SUCH_GROUP;
954 group_name = talloc_strdup(mem_ctx, grp->gr_name);
955 if (group_name == NULL) {
956 return NT_STATUS_NO_MEMORY;
959 if ( !(account = samu_new( NULL )) ) {
960 return NT_STATUS_NO_MEMORY;
963 if (!pdb_getsampwsid(account, &member_sid) ||
964 !sid_to_uid(&member_sid, &uid) ||
965 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
966 return NT_STATUS_NO_SUCH_USER;
969 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
970 return NT_STATUS_MEMBER_NOT_IN_GROUP;
974 * ok, the group exist, the user exist, the user is in the group,
975 * we can (finally) delete it from the group!
978 smb_delete_user_group(group_name, pwd->pw_name);
980 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
981 return NT_STATUS_ACCESS_DENIED;
987 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
990 struct pdb_methods *pdb = pdb_get_methods();
993 return NT_STATUS_UNSUCCESSFUL;
996 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
999 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
1001 struct pdb_methods *pdb = pdb_get_methods();
1007 return NT_STATUS_IS_OK(pdb->find_alias(pdb, name, sid));
1010 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
1012 struct pdb_methods *pdb = pdb_get_methods();
1015 return NT_STATUS_NOT_IMPLEMENTED;
1018 return pdb->create_alias(pdb, name, rid);
1021 BOOL pdb_delete_alias(const DOM_SID *sid)
1023 struct pdb_methods *pdb = pdb_get_methods();
1029 return NT_STATUS_IS_OK(pdb->delete_alias(pdb, sid));
1033 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1035 struct pdb_methods *pdb = pdb_get_methods();
1041 return NT_STATUS_IS_OK(pdb->get_aliasinfo(pdb, sid, info));
1044 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1046 struct pdb_methods *pdb = pdb_get_methods();
1052 return NT_STATUS_IS_OK(pdb->set_aliasinfo(pdb, sid, info));
1055 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1057 struct pdb_methods *pdb = pdb_get_methods();
1060 return NT_STATUS_UNSUCCESSFUL;
1063 return pdb->add_aliasmem(pdb, alias, member);
1066 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1068 struct pdb_methods *pdb = pdb_get_methods();
1071 return NT_STATUS_UNSUCCESSFUL;
1074 return pdb->del_aliasmem(pdb, alias, member);
1077 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
1078 DOM_SID **pp_members, size_t *p_num_members)
1080 struct pdb_methods *pdb = pdb_get_methods();
1083 return NT_STATUS_UNSUCCESSFUL;
1086 return pdb->enum_aliasmem(pdb, alias,
1087 pp_members, p_num_members);
1090 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1091 const DOM_SID *domain_sid,
1092 const DOM_SID *members, size_t num_members,
1093 uint32 **pp_alias_rids,
1094 size_t *p_num_alias_rids)
1096 struct pdb_methods *pdb = pdb_get_methods();
1099 return NT_STATUS_NOT_IMPLEMENTED;
1102 return pdb->enum_alias_memberships(pdb, mem_ctx,
1104 members, num_members,
1109 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
1115 struct pdb_methods *pdb = pdb_get_methods();
1118 return NT_STATUS_NOT_IMPLEMENTED;
1121 return pdb->lookup_rids(pdb, domain_sid,
1122 num_rids, rids, names, attrs);
1125 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
1131 struct pdb_methods *pdb = pdb_get_methods();
1134 return NT_STATUS_NOT_IMPLEMENTED;
1137 return pdb->lookup_names(pdb, domain_sid,
1138 num_names, names, rids, attrs);
1141 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
1143 struct pdb_methods *pdb = pdb_get_methods();
1149 return NT_STATUS_IS_OK(pdb->get_account_policy(pdb, policy_index, value));
1152 BOOL pdb_set_account_policy(int policy_index, uint32 value)
1154 struct pdb_methods *pdb = pdb_get_methods();
1160 return NT_STATUS_IS_OK(pdb->set_account_policy(pdb, policy_index, value));
1163 BOOL pdb_get_seq_num(time_t *seq_num)
1165 struct pdb_methods *pdb = pdb_get_methods();
1171 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1174 BOOL pdb_uid_to_rid(uid_t uid, uint32 *rid)
1176 struct pdb_methods *pdb = pdb_get_methods();
1182 return pdb->uid_to_rid(pdb, uid, rid);
1185 BOOL pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1187 struct pdb_methods *pdb = pdb_get_methods();
1193 return pdb->gid_to_sid(pdb, gid, sid);
1196 BOOL pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1197 enum SID_NAME_USE *type)
1199 struct pdb_methods *pdb = pdb_get_methods();
1205 return pdb->sid_to_id(pdb, sid, id, type);
1208 BOOL pdb_rid_algorithm(void)
1210 struct pdb_methods *pdb = pdb_get_methods();
1216 return pdb->rid_algorithm(pdb);
1219 /********************************************************************
1220 Allocate a new RID from the passdb backend. Verify that it is free
1221 by calling lookup_global_sam_rid() to verify that the RID is not
1222 in use. This handles servers that have existing users or groups
1223 with add RIDs (assigned from previous algorithmic mappings)
1224 ********************************************************************/
1226 BOOL pdb_new_rid(uint32 *rid)
1228 struct pdb_methods *pdb = pdb_get_methods();
1229 const char *name = NULL;
1230 enum SID_NAME_USE type;
1231 uint32 allocated_rid = 0;
1239 if (pdb_rid_algorithm()) {
1240 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1245 if (algorithmic_rid_base() != BASE_RID) {
1246 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1247 "without algorithmic RIDs is chosen.\n"));
1248 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1249 "add', set the maximum used RID using\n"));
1250 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1254 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1255 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1259 /* Attempt to get an unused RID (max tires is 250...yes that it is
1260 and arbitrary number I pulkled out of my head). -- jerry */
1262 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1265 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1269 /* validate that the RID is not in use */
1271 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1278 if ( allocated_rid == 0 ) {
1279 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1283 *rid = allocated_rid;
1288 /***************************************************************
1289 Initialize the static context (at smbd startup etc).
1291 If uninitialised, context will auto-init on first use.
1292 ***************************************************************/
1294 BOOL initialize_password_db(BOOL reload)
1296 return (pdb_get_methods_reload(reload) != NULL);
1300 /***************************************************************************
1301 Default implementations of some functions.
1302 ****************************************************************************/
1304 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1306 return NT_STATUS_NO_SUCH_USER;
1309 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1311 return NT_STATUS_NO_SUCH_USER;
1314 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1316 return NT_STATUS_NOT_IMPLEMENTED;
1319 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1321 return NT_STATUS_NOT_IMPLEMENTED;
1324 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1326 return NT_STATUS_NOT_IMPLEMENTED;
1329 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1331 return NT_STATUS_NOT_IMPLEMENTED;
1334 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, BOOL success)
1336 return NT_STATUS_OK;
1339 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint32 acb_mask)
1341 return NT_STATUS_NOT_IMPLEMENTED;
1344 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, struct samu *user)
1346 return NT_STATUS_NOT_IMPLEMENTED;
1349 static void pdb_default_endsampwent(struct pdb_methods *methods)
1351 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1354 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1356 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1359 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1361 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1364 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1366 *seq_num = time(NULL);
1367 return NT_STATUS_OK;
1370 static BOOL pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1373 struct samu *sampw = NULL;
1374 struct passwd *unix_pw;
1377 unix_pw = sys_getpwuid( uid );
1380 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1381 "%lu\n", (unsigned long)uid));
1385 if ( !(sampw = samu_new( NULL )) ) {
1386 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1391 ret = NT_STATUS_IS_OK(
1392 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1396 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1397 "%s (%d)\n", unix_pw->pw_name, uid));
1402 ret = sid_peek_check_rid(get_global_sam_sid(),
1403 pdb_get_user_sid(sampw), rid);
1406 DEBUG(1, ("Could not peek rid out of sid %s\n",
1407 sid_string_static(pdb_get_user_sid(sampw))));
1414 static BOOL pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1419 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1423 sid_copy(sid, &map.sid);
1427 static BOOL pdb_default_sid_to_id(struct pdb_methods *methods,
1429 union unid_t *id, enum SID_NAME_USE *type)
1431 TALLOC_CTX *mem_ctx;
1436 mem_ctx = talloc_new(NULL);
1438 if (mem_ctx == NULL) {
1439 DEBUG(0, ("talloc_new failed\n"));
1443 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1444 /* Here we might have users as well as groups and aliases */
1445 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1449 if (sid_peek_check_rid(&global_sid_Builtin, sid, &rid)) {
1450 /* Here we only have aliases */
1452 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1453 DEBUG(10, ("Could not find map for sid %s\n",
1454 sid_string_static(sid)));
1457 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1458 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1459 DEBUG(10, ("Map for sid %s is a %s, expected an "
1460 "alias\n", sid_string_static(sid),
1461 sid_type_lookup(map.sid_name_use)));
1466 *type = SID_NAME_ALIAS;
1471 DEBUG(5, ("Sid %s is neither ours nor builtin, don't know it\n",
1472 sid_string_static(sid)));
1476 TALLOC_FREE(mem_ctx);
1480 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1481 uid_t uid, uid_t **pp_uids, size_t *p_num)
1485 for (i=0; i<*p_num; i++) {
1486 if ((*pp_uids)[i] == uid)
1490 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1492 if (*pp_uids == NULL)
1495 (*pp_uids)[*p_num] = uid;
1499 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1509 /* We only look at our own sam, so don't care about imported stuff */
1511 winbindd_env = getenv(WINBINDD_DONT_ENV);
1514 if ((grp = getgrgid(gid)) == NULL) {
1515 /* allow winbindd lookups, but only if they weren't already disabled */
1516 if ( !(winbindd_env && strequal(winbindd_env, "1")) ) {
1523 /* Primary group members */
1526 while ((pwd = getpwent()) != NULL) {
1527 if (pwd->pw_gid == gid) {
1528 add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1534 /* Secondary group members */
1536 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1537 struct passwd *pw = getpwnam(*gr);
1541 add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num);
1544 /* allow winbindd lookups, but only if they weren't already disabled */
1546 if ( !(winbindd_env && strequal(winbindd_env, "1")) ) {
1553 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1554 TALLOC_CTX *mem_ctx,
1555 const DOM_SID *group,
1556 uint32 **pp_member_rids,
1557 size_t *p_num_members)
1563 *pp_member_rids = NULL;
1566 if (!sid_to_gid(group, &gid))
1567 return NT_STATUS_NO_SUCH_GROUP;
1569 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1570 return NT_STATUS_NO_SUCH_GROUP;
1573 return NT_STATUS_OK;
1575 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1577 for (i=0; i<num_uids; i++) {
1580 uid_to_sid(&sid, uids[i]);
1582 if (!sid_check_is_in_our_domain(&sid)) {
1583 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1584 "in our domain\n"));
1588 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1589 *p_num_members += 1;
1592 return NT_STATUS_OK;
1595 NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1596 TALLOC_CTX *mem_ctx,
1600 size_t *p_num_groups)
1605 const char *username = pdb_get_username(user);
1608 /* Ignore the primary group SID. Honor the real Unix primary group.
1609 The primary group SID is only of real use to Windows clients */
1611 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1612 return NT_STATUS_NO_SUCH_USER;
1619 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1620 return NT_STATUS_NO_SUCH_USER;
1623 if (*p_num_groups == 0) {
1624 smb_panic("primary group missing");
1627 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1629 if (*pp_sids == NULL) {
1630 TALLOC_FREE(*pp_gids);
1631 return NT_STATUS_NO_MEMORY;
1634 for (i=0; i<*p_num_groups; i++) {
1635 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1638 return NT_STATUS_OK;
1641 /*******************************************************************
1642 Look up a rid in the SAM we're responsible for (i.e. passdb)
1643 ********************************************************************/
1645 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1647 enum SID_NAME_USE *psid_name_use,
1648 union unid_t *unix_id)
1650 struct samu *sam_account = NULL;
1655 *psid_name_use = SID_NAME_UNKNOWN;
1657 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1658 (unsigned int)rid));
1660 sid_copy(&sid, get_global_sam_sid());
1661 sid_append_rid(&sid, rid);
1663 /* see if the passdb can help us with the name of the user */
1665 if ( !(sam_account = samu_new( NULL )) ) {
1669 /* BEING ROOT BLLOCK */
1671 if (pdb_getsampwsid(sam_account, &sid)) {
1674 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1675 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1676 *psid_name_use = SID_NAME_USER;
1678 TALLOC_FREE(sam_account);
1680 if (unix_id == NULL) {
1684 pw = Get_Pwnam(*name);
1688 unix_id->uid = pw->pw_uid;
1691 TALLOC_FREE(sam_account);
1693 ret = pdb_getgrsid(&map, sid);
1695 /* END BECOME_ROOT BLOCK */
1697 /* do not resolve SIDs to a name unless there is a valid
1698 gid associated with it */
1700 if ( ret && (map.gid != (gid_t)-1) ) {
1701 *name = talloc_strdup(mem_ctx, map.nt_name);
1702 *psid_name_use = map.sid_name_use;
1705 unix_id->gid = map.gid;
1711 /* Windows will always map RID 513 to something. On a non-domain
1712 controller, this gets mapped to SERVER\None. */
1715 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1719 if ( rid == DOMAIN_GROUP_RID_USERS ) {
1720 *name = talloc_strdup(mem_ctx, "None" );
1721 *psid_name_use = SID_NAME_DOM_GRP;
1729 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1730 const DOM_SID *domain_sid,
1738 BOOL have_mapped = False;
1739 BOOL have_unmapped = False;
1741 if (sid_check_is_builtin(domain_sid)) {
1743 for (i=0; i<num_rids; i++) {
1746 if (lookup_builtin_rid(names, rids[i], &name)) {
1747 attrs[i] = SID_NAME_ALIAS;
1749 DEBUG(5,("lookup_rids: %s:%d\n",
1750 names[i], attrs[i]));
1753 have_unmapped = True;
1754 attrs[i] = SID_NAME_UNKNOWN;
1760 /* Should not happen, but better check once too many */
1761 if (!sid_check_is_domain(domain_sid)) {
1762 return NT_STATUS_INVALID_HANDLE;
1765 for (i = 0; i < num_rids; i++) {
1768 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1771 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1774 have_unmapped = True;
1775 attrs[i] = SID_NAME_UNKNOWN;
1781 result = NT_STATUS_NONE_MAPPED;
1784 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1789 NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1790 const DOM_SID *domain_sid,
1798 BOOL have_mapped = False;
1799 BOOL have_unmapped = False;
1801 if (sid_check_is_builtin(domain_sid)) {
1803 for (i=0; i<num_names; i++) {
1806 if (lookup_builtin_name(names[i], &rid)) {
1807 attrs[i] = SID_NAME_ALIAS;
1809 DEBUG(5,("lookup_rids: %s:%d\n",
1810 names[i], attrs[i]));
1813 have_unmapped = True;
1814 attrs[i] = SID_NAME_UNKNOWN;
1820 /* Should not happen, but better check once too many */
1821 if (!sid_check_is_domain(domain_sid)) {
1822 return NT_STATUS_INVALID_HANDLE;
1825 for (i = 0; i < num_names; i++) {
1826 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1827 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1828 rids[i], attrs[i]));
1831 have_unmapped = True;
1832 attrs[i] = SID_NAME_UNKNOWN;
1838 result = NT_STATUS_NONE_MAPPED;
1841 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1846 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1848 TALLOC_CTX *mem_ctx;
1849 struct pdb_search *result;
1851 mem_ctx = talloc_init("pdb_search");
1852 if (mem_ctx == NULL) {
1853 DEBUG(0, ("talloc_init failed\n"));
1857 result = TALLOC_P(mem_ctx, struct pdb_search);
1858 if (result == NULL) {
1859 DEBUG(0, ("talloc failed\n"));
1863 result->mem_ctx = mem_ctx;
1864 result->type = type;
1865 result->cache = NULL;
1866 result->num_entries = 0;
1867 result->cache_size = 0;
1868 result->search_ended = False;
1870 /* Segfault appropriately if not initialized */
1871 result->next_entry = NULL;
1872 result->search_end = NULL;
1877 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1879 const char *account_name,
1880 const char *fullname,
1881 const char *description,
1882 struct samr_displayentry *entry)
1885 entry->acct_flags = acct_flags;
1887 if (account_name != NULL)
1888 entry->account_name = talloc_strdup(mem_ctx, account_name);
1890 entry->account_name = "";
1892 if (fullname != NULL)
1893 entry->fullname = talloc_strdup(mem_ctx, fullname);
1895 entry->fullname = "";
1897 if (description != NULL)
1898 entry->description = talloc_strdup(mem_ctx, description);
1900 entry->description = "";
1903 static BOOL user_search_in_progress = False;
1904 struct user_search {
1908 static BOOL next_entry_users(struct pdb_search *s,
1909 struct samr_displayentry *entry)
1911 struct user_search *state = s->private_data;
1912 struct samu *user = NULL;
1915 if ( !(user = samu_new( NULL )) ) {
1916 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1920 if (!pdb_getsampwent(user)) {
1925 if ((state->acct_flags != 0) &&
1926 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1931 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1932 pdb_get_acct_ctrl(user), pdb_get_username(user),
1933 pdb_get_fullname(user), pdb_get_acct_desc(user),
1940 static void search_end_users(struct pdb_search *search)
1943 user_search_in_progress = False;
1946 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1947 struct pdb_search *search,
1950 struct user_search *state;
1952 if (user_search_in_progress) {
1953 DEBUG(1, ("user search in progress\n"));
1957 if (!pdb_setsampwent(False, acct_flags)) {
1958 DEBUG(5, ("Could not start search\n"));
1962 user_search_in_progress = True;
1964 state = TALLOC_P(search->mem_ctx, struct user_search);
1965 if (state == NULL) {
1966 DEBUG(0, ("talloc failed\n"));
1970 state->acct_flags = acct_flags;
1972 search->private_data = state;
1973 search->next_entry = next_entry_users;
1974 search->search_end = search_end_users;
1978 struct group_search {
1980 size_t num_groups, current_group;
1983 static BOOL next_entry_groups(struct pdb_search *s,
1984 struct samr_displayentry *entry)
1986 struct group_search *state = s->private_data;
1988 GROUP_MAP *map = &state->groups[state->current_group];
1990 if (state->current_group == state->num_groups)
1993 sid_peek_rid(&map->sid, &rid);
1995 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1998 state->current_group += 1;
2002 static void search_end_groups(struct pdb_search *search)
2004 struct group_search *state = search->private_data;
2005 SAFE_FREE(state->groups);
2008 static BOOL pdb_search_grouptype(struct pdb_search *search,
2009 const DOM_SID *sid, enum SID_NAME_USE type)
2011 struct group_search *state;
2013 state = TALLOC_P(search->mem_ctx, struct group_search);
2014 if (state == NULL) {
2015 DEBUG(0, ("talloc failed\n"));
2019 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
2021 DEBUG(0, ("Could not enum groups\n"));
2025 state->current_group = 0;
2026 search->private_data = state;
2027 search->next_entry = next_entry_groups;
2028 search->search_end = search_end_groups;
2032 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
2033 struct pdb_search *search)
2035 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
2038 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
2039 struct pdb_search *search,
2043 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
2046 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
2049 if (idx < search->num_entries)
2050 return &search->cache[idx];
2052 if (search->search_ended)
2055 while (idx >= search->num_entries) {
2056 struct samr_displayentry entry;
2058 if (!search->next_entry(search, &entry)) {
2059 search->search_end(search);
2060 search->search_ended = True;
2064 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
2065 entry, &search->cache, &search->num_entries,
2066 &search->cache_size);
2069 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
2072 struct pdb_search *pdb_search_users(uint32 acct_flags)
2074 struct pdb_methods *pdb = pdb_get_methods();
2075 struct pdb_search *result;
2077 if (pdb == NULL) return NULL;
2079 result = pdb_search_init(PDB_USER_SEARCH);
2080 if (result == NULL) return NULL;
2082 if (!pdb->search_users(pdb, result, acct_flags)) {
2083 talloc_destroy(result->mem_ctx);
2089 struct pdb_search *pdb_search_groups(void)
2091 struct pdb_methods *pdb = pdb_get_methods();
2092 struct pdb_search *result;
2094 if (pdb == NULL) return NULL;
2096 result = pdb_search_init(PDB_GROUP_SEARCH);
2097 if (result == NULL) return NULL;
2099 if (!pdb->search_groups(pdb, result)) {
2100 talloc_destroy(result->mem_ctx);
2106 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
2108 struct pdb_methods *pdb = pdb_get_methods();
2109 struct pdb_search *result;
2111 if (pdb == NULL) return NULL;
2113 result = pdb_search_init(PDB_ALIAS_SEARCH);
2114 if (result == NULL) return NULL;
2116 if (!pdb->search_aliases(pdb, result, sid)) {
2117 talloc_destroy(result->mem_ctx);
2123 uint32 pdb_search_entries(struct pdb_search *search,
2124 uint32 start_idx, uint32 max_entries,
2125 struct samr_displayentry **result)
2127 struct samr_displayentry *end_entry;
2128 uint32 end_idx = start_idx+max_entries-1;
2130 /* The first entry needs to be searched after the last. Otherwise the
2131 * first entry might have moved due to a realloc during the search for
2132 * the last entry. */
2134 end_entry = pdb_search_getentry(search, end_idx);
2135 *result = pdb_search_getentry(search, start_idx);
2137 if (end_entry != NULL)
2140 if (start_idx >= search->num_entries)
2143 return search->num_entries - start_idx;
2146 void pdb_search_destroy(struct pdb_search *search)
2151 if (!search->search_ended)
2152 search->search_end(search);
2154 talloc_destroy(search->mem_ctx);
2157 /*******************************************************************
2158 Create a pdb_methods structure and initialize it with the default
2159 operations. In this way a passdb module can simply implement
2160 the functionality it cares about. However, normally this is done
2161 in groups of related functions.
2162 *******************************************************************/
2164 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2166 /* allocate memory for the structure as its own talloc CTX */
2168 if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
2169 return NT_STATUS_NO_MEMORY;
2172 (*methods)->setsampwent = pdb_default_setsampwent;
2173 (*methods)->endsampwent = pdb_default_endsampwent;
2174 (*methods)->getsampwent = pdb_default_getsampwent;
2175 (*methods)->getsampwnam = pdb_default_getsampwnam;
2176 (*methods)->getsampwsid = pdb_default_getsampwsid;
2177 (*methods)->create_user = pdb_default_create_user;
2178 (*methods)->delete_user = pdb_default_delete_user;
2179 (*methods)->add_sam_account = pdb_default_add_sam_account;
2180 (*methods)->update_sam_account = pdb_default_update_sam_account;
2181 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2182 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2183 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2185 (*methods)->getgrsid = pdb_default_getgrsid;
2186 (*methods)->getgrgid = pdb_default_getgrgid;
2187 (*methods)->getgrnam = pdb_default_getgrnam;
2188 (*methods)->create_dom_group = pdb_default_create_dom_group;
2189 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2190 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2191 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2192 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2193 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2194 (*methods)->enum_group_members = pdb_default_enum_group_members;
2195 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2196 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2197 (*methods)->add_groupmem = pdb_default_add_groupmem;
2198 (*methods)->del_groupmem = pdb_default_del_groupmem;
2199 (*methods)->find_alias = pdb_default_find_alias;
2200 (*methods)->create_alias = pdb_default_create_alias;
2201 (*methods)->delete_alias = pdb_default_delete_alias;
2202 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2203 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2204 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2205 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2206 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2207 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2208 (*methods)->lookup_rids = pdb_default_lookup_rids;
2209 (*methods)->get_account_policy = pdb_default_get_account_policy;
2210 (*methods)->set_account_policy = pdb_default_set_account_policy;
2211 (*methods)->get_seq_num = pdb_default_get_seq_num;
2212 (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2213 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2214 (*methods)->sid_to_id = pdb_default_sid_to_id;
2216 (*methods)->search_users = pdb_default_search_users;
2217 (*methods)->search_groups = pdb_default_search_groups;
2218 (*methods)->search_aliases = pdb_default_search_aliases;
2220 return NT_STATUS_OK;