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 3 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, see <http://www.gnu.org/licenses/>.
26 #define DBGC_CLASS DBGC_PASSDB
28 /* Cache of latest SAM lookup query */
30 static struct samu *csamuser = NULL;
34 static struct pdb_init_function_entry *backends = NULL;
36 static void lazy_initialize_passdb(void)
38 static bool initialized = False;
46 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
48 enum lsa_SidType *psid_name_use,
49 union unid_t *unix_id);
51 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
53 struct pdb_init_function_entry *entry = backends;
55 if(version != PASSDB_INTERFACE_VERSION) {
56 DEBUG(0,("Can't register passdb backend!\n"
57 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
58 "while this version of samba uses version %d\n",
59 version,PASSDB_INTERFACE_VERSION));
60 return NT_STATUS_OBJECT_TYPE_MISMATCH;
64 return NT_STATUS_INVALID_PARAMETER;
67 DEBUG(5,("Attempting to register passdb backend %s\n", name));
69 /* Check for duplicates */
70 if (pdb_find_backend_entry(name)) {
71 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
72 return NT_STATUS_OBJECT_NAME_COLLISION;
75 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
76 entry->name = smb_xstrdup(name);
79 DLIST_ADD(backends, entry);
80 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
84 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
86 struct pdb_init_function_entry *entry = backends;
89 if (strcmp(entry->name, name)==0) return entry;
97 * The event context for the passdb backend. I know this is a bad hack and yet
98 * another static variable, but our pdb API is a global thing per
99 * definition. The first use for this is the LDAP idle function, more might be
102 * I don't feel too bad about this static variable, it replaces the
103 * smb_idle_event_list that used to exist in lib/module.c. -- VL
106 static struct event_context *pdb_event_ctx;
108 struct event_context *pdb_get_event_context(void)
110 return pdb_event_ctx;
113 /******************************************************************
114 Make a pdb_methods from scratch
115 *******************************************************************/
117 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
119 char *module_name = smb_xstrdup(selected);
120 char *module_location = NULL, *p;
121 struct pdb_init_function_entry *entry;
122 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
124 lazy_initialize_passdb();
126 p = strchr(module_name, ':');
130 module_location = p+1;
131 trim_char(module_location, ' ', ' ');
134 trim_char(module_name, ' ', ' ');
137 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
139 entry = pdb_find_backend_entry(module_name);
141 /* Try to find a module that contains this module */
143 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
144 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
145 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
146 SAFE_FREE(module_name);
147 return NT_STATUS_UNSUCCESSFUL;
151 /* No such backend found */
153 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
154 SAFE_FREE(module_name);
155 return NT_STATUS_INVALID_PARAMETER;
158 DEBUG(5,("Found pdb backend %s\n", module_name));
160 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
161 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
162 selected, nt_errstr(nt_status)));
163 SAFE_FREE(module_name);
167 SAFE_FREE(module_name);
169 DEBUG(5,("pdb backend %s has a valid init\n", selected));
174 /******************************************************************
175 Return an already initialised pdn_methods structure
176 *******************************************************************/
178 static struct pdb_methods *pdb_get_methods_reload( bool reload )
180 static struct pdb_methods *pdb = NULL;
182 if ( pdb && reload ) {
183 pdb->free_private_data( &(pdb->private_data) );
184 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
186 asprintf(&msg, "pdb_get_methods_reload: "
187 "failed to get pdb methods for backend %s\n",
188 lp_passdb_backend());
194 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
196 asprintf(&msg, "pdb_get_methods_reload: "
197 "failed to get pdb methods for backend %s\n",
198 lp_passdb_backend());
206 static struct pdb_methods *pdb_get_methods(void)
208 return pdb_get_methods_reload(False);
211 /******************************************************************
212 Backward compatibility functions for the original passdb interface
213 *******************************************************************/
215 bool pdb_setsampwent(bool update, uint16 acb_mask)
217 struct pdb_methods *pdb = pdb_get_methods();
218 return NT_STATUS_IS_OK(pdb->setsampwent(pdb, update, acb_mask));
221 void pdb_endsampwent(void)
223 struct pdb_methods *pdb = pdb_get_methods();
224 pdb->endsampwent(pdb);
227 bool pdb_getsampwent(struct samu *user)
229 struct pdb_methods *pdb = pdb_get_methods();
231 if ( !NT_STATUS_IS_OK(pdb->getsampwent(pdb, user) ) ) {
238 bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
240 struct pdb_methods *pdb = pdb_get_methods();
242 if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
247 TALLOC_FREE(csamuser);
250 csamuser = samu_new( NULL );
255 if (!pdb_copy_sam_account(csamuser, sam_acct)) {
256 TALLOC_FREE(csamuser);
263 /**********************************************************************
264 **********************************************************************/
266 bool guest_user_info( struct samu *user )
270 const char *guestname = lp_guestaccount();
272 if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
273 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
278 result = samu_set_unix(user, pwd );
282 return NT_STATUS_IS_OK( result );
285 /**********************************************************************
286 **********************************************************************/
288 bool pdb_getsampwsid(struct samu *sam_acct, const DOM_SID *sid)
290 struct pdb_methods *pdb = pdb_get_methods();
293 /* hard code the Guest RID of 501 */
295 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
298 if ( rid == DOMAIN_USER_RID_GUEST ) {
299 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
300 return guest_user_info( sam_acct );
303 /* check the cache first */
305 if ( csamuser && sid_equal(sid, pdb_get_user_sid(csamuser) ) )
306 return pdb_copy_sam_account(sam_acct, csamuser);
308 return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
311 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
312 TALLOC_CTX *tmp_ctx, const char *name,
313 uint32 acb_info, uint32 *rid)
315 struct samu *sam_pass;
319 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
320 return NT_STATUS_NO_MEMORY;
323 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
324 char *add_script = NULL;
328 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
329 add_script = talloc_strdup(tmp_ctx,
330 lp_adduser_script());
332 add_script = talloc_strdup(tmp_ctx,
333 lp_addmachine_script());
336 if (!add_script || add_script[0] == '\0') {
337 DEBUG(3, ("Could not find user %s and no add script "
339 return NT_STATUS_NO_SUCH_USER;
342 /* lowercase the username before creating the Unix account for
343 compatibility with previous Samba releases */
344 fstrcpy( name2, name );
346 add_script = talloc_all_string_sub(tmp_ctx,
351 return NT_STATUS_NO_MEMORY;
353 add_ret = smbrun(add_script,NULL);
354 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
355 add_script, add_ret));
357 smb_nscd_flush_user_cache();
362 pwd = Get_Pwnam_alloc(tmp_ctx, name);
365 /* we have a valid SID coming out of this call */
367 status = samu_alloc_rid_unix( sam_pass, pwd );
371 if (!NT_STATUS_IS_OK(status)) {
372 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %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 /* Use the username case specified in the original request */
384 pdb_set_username( sam_pass, name, PDB_SET );
386 /* Disable the account on creation, it does not have a reasonable password yet. */
388 acb_info |= ACB_DISABLED;
390 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
392 status = pdb_add_sam_account(sam_pass);
394 TALLOC_FREE(sam_pass);
399 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
402 struct pdb_methods *pdb = pdb_get_methods();
403 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
406 /****************************************************************************
407 Delete a UNIX user on demand.
408 ****************************************************************************/
410 static int smb_delete_user(const char *unix_user)
412 char *del_script = NULL;
417 if ( strequal( unix_user, "root" ) ) {
418 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
422 del_script = talloc_strdup(talloc_tos(), lp_deluser_script());
423 if (!del_script || !*del_script) {
426 del_script = talloc_all_string_sub(talloc_tos(),
433 ret = smbrun(del_script,NULL);
436 smb_nscd_flush_user_cache();
438 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
443 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
445 struct samu *sam_acct)
450 status = pdb_delete_sam_account(sam_acct);
451 if (!NT_STATUS_IS_OK(status)) {
456 * Now delete the unix side ....
457 * note: we don't check if the delete really happened as the script is
458 * not necessary present and maybe the sysadmin doesn't want to delete
462 /* always lower case the username before handing it off to
465 fstrcpy( username, pdb_get_username(sam_acct) );
466 strlower_m( username );
468 smb_delete_user( username );
473 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
475 struct pdb_methods *pdb = pdb_get_methods();
478 /* sanity check to make sure we don't delete root */
480 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
481 return NT_STATUS_NO_SUCH_USER;
485 return NT_STATUS_ACCESS_DENIED;
488 return pdb->delete_user(pdb, mem_ctx, sam_acct);
491 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
493 struct pdb_methods *pdb = pdb_get_methods();
494 return pdb->add_sam_account(pdb, sam_acct);
497 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
499 struct pdb_methods *pdb = pdb_get_methods();
501 if (csamuser != NULL) {
502 TALLOC_FREE(csamuser);
506 return pdb->update_sam_account(pdb, sam_acct);
509 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
511 struct pdb_methods *pdb = pdb_get_methods();
513 if (csamuser != NULL) {
514 TALLOC_FREE(csamuser);
518 return pdb->delete_sam_account(pdb, sam_acct);
521 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
523 struct pdb_methods *pdb = pdb_get_methods();
527 if (csamuser != NULL) {
528 TALLOC_FREE(csamuser);
532 /* sanity check to make sure we don't rename root */
534 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
535 return NT_STATUS_NO_SUCH_USER;
539 return NT_STATUS_ACCESS_DENIED;
542 status = pdb->rename_sam_account(pdb, oldname, newname);
544 /* always flush the cache here just to be safe */
550 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
552 struct pdb_methods *pdb = pdb_get_methods();
553 return pdb->update_login_attempts(pdb, sam_acct, success);
556 bool pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
558 struct pdb_methods *pdb = pdb_get_methods();
559 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
562 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
564 struct pdb_methods *pdb = pdb_get_methods();
565 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
568 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
570 struct pdb_methods *pdb = pdb_get_methods();
571 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
574 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
583 grp = getgrnam(name);
588 if (smb_create_group(name, &gid) != 0) {
589 return NT_STATUS_ACCESS_DENIED;
596 return NT_STATUS_ACCESS_DENIED;
599 if (pdb_rid_algorithm()) {
600 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
602 if (!pdb_new_rid(rid)) {
603 return NT_STATUS_ACCESS_DENIED;
607 sid_compose(&group_sid, get_global_sam_sid(), *rid);
609 return add_initial_entry(grp->gr_gid, sid_to_string(tmp, &group_sid),
610 SID_NAME_DOM_GRP, name, NULL);
613 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
616 struct pdb_methods *pdb = pdb_get_methods();
617 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
620 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
628 const char *grp_name;
630 sid_compose(&group_sid, get_global_sam_sid(), rid);
632 if (!get_domain_group_from_sid(group_sid, &map)) {
633 DEBUG(10, ("Could not find group for rid %d\n", rid));
634 return NT_STATUS_NO_SUCH_GROUP;
637 /* We need the group name for the smb_delete_group later on */
639 if (map.gid == (gid_t)-1) {
640 return NT_STATUS_NO_SUCH_GROUP;
643 grp = getgrgid(map.gid);
645 return NT_STATUS_NO_SUCH_GROUP;
648 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
650 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
651 if (grp_name == NULL) {
652 return NT_STATUS_NO_MEMORY;
655 status = pdb_delete_group_mapping_entry(group_sid);
657 if (!NT_STATUS_IS_OK(status)) {
661 /* Don't check the result of smb_delete_group */
663 smb_delete_group(grp_name);
668 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
670 struct pdb_methods *pdb = pdb_get_methods();
671 return pdb->delete_dom_group(pdb, mem_ctx, rid);
674 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
676 struct pdb_methods *pdb = pdb_get_methods();
677 return pdb->add_group_mapping_entry(pdb, map);
680 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
682 struct pdb_methods *pdb = pdb_get_methods();
683 return pdb->update_group_mapping_entry(pdb, map);
686 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
688 struct pdb_methods *pdb = pdb_get_methods();
689 return pdb->delete_group_mapping_entry(pdb, sid);
692 bool pdb_enum_group_mapping(const DOM_SID *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
693 size_t *p_num_entries, bool unix_only)
695 struct pdb_methods *pdb = pdb_get_methods();
696 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
697 pp_rmap, p_num_entries, unix_only));
700 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
702 uint32 **pp_member_rids,
703 size_t *p_num_members)
705 struct pdb_methods *pdb = pdb_get_methods();
708 result = pdb->enum_group_members(pdb, mem_ctx,
709 sid, pp_member_rids, p_num_members);
711 /* special check for rid 513 */
713 if ( !NT_STATUS_IS_OK( result ) ) {
716 sid_peek_rid( sid, &rid );
718 if ( rid == DOMAIN_GROUP_RID_USERS ) {
720 *pp_member_rids = NULL;
729 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
730 DOM_SID **pp_sids, gid_t **pp_gids,
731 size_t *p_num_groups)
733 struct pdb_methods *pdb = pdb_get_methods();
734 return pdb->enum_group_memberships(
736 pp_sids, pp_gids, p_num_groups);
739 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
741 struct samu *sampass)
746 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
747 (grp = getgrgid(gid)) == NULL) {
748 return NT_STATUS_INVALID_PRIMARY_GROUP;
751 if (smb_set_primary_group(grp->gr_name,
752 pdb_get_username(sampass)) != 0) {
753 return NT_STATUS_ACCESS_DENIED;
759 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
761 struct pdb_methods *pdb = pdb_get_methods();
762 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
766 * Helper function to see whether a user is in a group. We can't use
767 * user_in_group_sid here because this creates dependencies only smbd can
771 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
772 const DOM_SID *group_sid)
776 size_t i, num_groups;
778 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
784 for (i=0; i<num_groups; i++) {
785 if (sid_equal(group_sid, &sids[i])) {
792 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
797 DOM_SID group_sid, member_sid;
798 struct samu *account = NULL;
802 const char *group_name;
805 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
806 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
808 if (!get_domain_group_from_sid(group_sid, &map) ||
809 (map.gid == (gid_t)-1) ||
810 ((grp = getgrgid(map.gid)) == NULL)) {
811 return NT_STATUS_NO_SUCH_GROUP;
814 group_name = talloc_strdup(mem_ctx, grp->gr_name);
815 if (group_name == NULL) {
816 return NT_STATUS_NO_MEMORY;
819 if ( !(account = samu_new( NULL )) ) {
820 return NT_STATUS_NO_MEMORY;
823 if (!pdb_getsampwsid(account, &member_sid) ||
824 !sid_to_uid(&member_sid, &uid) ||
825 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
826 return NT_STATUS_NO_SUCH_USER;
829 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
830 return NT_STATUS_MEMBER_IN_GROUP;
834 * ok, the group exist, the user exist, the user is not in the group,
835 * we can (finally) add it to the group !
838 smb_add_user_group(group_name, pwd->pw_name);
840 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
841 return NT_STATUS_ACCESS_DENIED;
847 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
850 struct pdb_methods *pdb = pdb_get_methods();
851 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
854 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
859 DOM_SID group_sid, member_sid;
860 struct samu *account = NULL;
864 const char *group_name;
867 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
868 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
870 if (!get_domain_group_from_sid(group_sid, &map) ||
871 (map.gid == (gid_t)-1) ||
872 ((grp = getgrgid(map.gid)) == NULL)) {
873 return NT_STATUS_NO_SUCH_GROUP;
876 group_name = talloc_strdup(mem_ctx, grp->gr_name);
877 if (group_name == NULL) {
878 return NT_STATUS_NO_MEMORY;
881 if ( !(account = samu_new( NULL )) ) {
882 return NT_STATUS_NO_MEMORY;
885 if (!pdb_getsampwsid(account, &member_sid) ||
886 !sid_to_uid(&member_sid, &uid) ||
887 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
888 return NT_STATUS_NO_SUCH_USER;
891 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
892 return NT_STATUS_MEMBER_NOT_IN_GROUP;
896 * ok, the group exist, the user exist, the user is in the group,
897 * we can (finally) delete it from the group!
900 smb_delete_user_group(group_name, pwd->pw_name);
902 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
903 return NT_STATUS_ACCESS_DENIED;
909 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
912 struct pdb_methods *pdb = pdb_get_methods();
913 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
916 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
918 struct pdb_methods *pdb = pdb_get_methods();
919 return pdb->create_alias(pdb, name, rid);
922 NTSTATUS pdb_delete_alias(const DOM_SID *sid)
924 struct pdb_methods *pdb = pdb_get_methods();
925 return pdb->delete_alias(pdb, sid);
928 NTSTATUS pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
930 struct pdb_methods *pdb = pdb_get_methods();
931 return pdb->get_aliasinfo(pdb, sid, info);
934 NTSTATUS pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
936 struct pdb_methods *pdb = pdb_get_methods();
937 return pdb->set_aliasinfo(pdb, sid, info);
940 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
942 struct pdb_methods *pdb = pdb_get_methods();
943 return pdb->add_aliasmem(pdb, alias, member);
946 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
948 struct pdb_methods *pdb = pdb_get_methods();
949 return pdb->del_aliasmem(pdb, alias, member);
952 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
953 DOM_SID **pp_members, size_t *p_num_members)
955 struct pdb_methods *pdb = pdb_get_methods();
956 return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
959 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
960 const DOM_SID *domain_sid,
961 const DOM_SID *members, size_t num_members,
962 uint32 **pp_alias_rids,
963 size_t *p_num_alias_rids)
965 struct pdb_methods *pdb = pdb_get_methods();
966 return pdb->enum_alias_memberships(pdb, mem_ctx,
968 members, num_members,
973 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
977 enum lsa_SidType *attrs)
979 struct pdb_methods *pdb = pdb_get_methods();
980 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
984 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
986 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
987 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
988 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
989 * down to are pdb_getsampwnam and pdb_getgrnam instead of
991 * But in principle, it the call belongs to the API and might get
992 * used in this context some day.
995 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
999 enum lsa_SidType *attrs)
1001 struct pdb_methods *pdb = pdb_get_methods();
1002 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
1006 bool pdb_get_account_policy(int policy_index, uint32 *value)
1008 struct pdb_methods *pdb = pdb_get_methods();
1012 status = pdb->get_account_policy(pdb, policy_index, value);
1015 return NT_STATUS_IS_OK(status);
1018 bool pdb_set_account_policy(int policy_index, uint32 value)
1020 struct pdb_methods *pdb = pdb_get_methods();
1024 status = pdb->set_account_policy(pdb, policy_index, value);
1027 return NT_STATUS_IS_OK(status);
1030 bool pdb_get_seq_num(time_t *seq_num)
1032 struct pdb_methods *pdb = pdb_get_methods();
1033 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1036 bool pdb_uid_to_rid(uid_t uid, uint32 *rid)
1038 struct pdb_methods *pdb = pdb_get_methods();
1039 return pdb->uid_to_rid(pdb, uid, rid);
1042 bool pdb_uid_to_sid(uid_t uid, DOM_SID *sid)
1044 struct pdb_methods *pdb = pdb_get_methods();
1045 return pdb->uid_to_sid(pdb, uid, sid);
1048 bool pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1050 struct pdb_methods *pdb = pdb_get_methods();
1051 return pdb->gid_to_sid(pdb, gid, sid);
1054 bool pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1055 enum lsa_SidType *type)
1057 struct pdb_methods *pdb = pdb_get_methods();
1058 return pdb->sid_to_id(pdb, sid, id, type);
1061 bool pdb_rid_algorithm(void)
1063 struct pdb_methods *pdb = pdb_get_methods();
1064 return pdb->rid_algorithm(pdb);
1067 /********************************************************************
1068 Allocate a new RID from the passdb backend. Verify that it is free
1069 by calling lookup_global_sam_rid() to verify that the RID is not
1070 in use. This handles servers that have existing users or groups
1071 with add RIDs (assigned from previous algorithmic mappings)
1072 ********************************************************************/
1074 bool pdb_new_rid(uint32 *rid)
1076 struct pdb_methods *pdb = pdb_get_methods();
1077 const char *name = NULL;
1078 enum lsa_SidType type;
1079 uint32 allocated_rid = 0;
1083 if (pdb_rid_algorithm()) {
1084 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1089 if (algorithmic_rid_base() != BASE_RID) {
1090 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1091 "without algorithmic RIDs is chosen.\n"));
1092 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1093 "add', set the maximum used RID using\n"));
1094 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1098 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1099 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1103 /* Attempt to get an unused RID (max tires is 250...yes that it is
1104 and arbitrary number I pulkled out of my head). -- jerry */
1106 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1109 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1113 /* validate that the RID is not in use */
1115 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1122 if ( allocated_rid == 0 ) {
1123 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1127 *rid = allocated_rid;
1132 /***************************************************************
1133 Initialize the static context (at smbd startup etc).
1135 If uninitialised, context will auto-init on first use.
1136 ***************************************************************/
1138 bool initialize_password_db(bool reload, struct event_context *event_ctx)
1140 pdb_event_ctx = event_ctx;
1141 return (pdb_get_methods_reload(reload) != NULL);
1145 /***************************************************************************
1146 Default implementations of some functions.
1147 ****************************************************************************/
1149 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1151 return NT_STATUS_NO_SUCH_USER;
1154 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1156 return NT_STATUS_NO_SUCH_USER;
1159 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1161 return NT_STATUS_NOT_IMPLEMENTED;
1164 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1166 return NT_STATUS_NOT_IMPLEMENTED;
1169 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1171 return NT_STATUS_NOT_IMPLEMENTED;
1174 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1176 return NT_STATUS_NOT_IMPLEMENTED;
1179 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1181 return NT_STATUS_NOT_IMPLEMENTED;
1184 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, bool update, uint32 acb_mask)
1186 return NT_STATUS_NOT_IMPLEMENTED;
1189 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, struct samu *user)
1191 return NT_STATUS_NOT_IMPLEMENTED;
1194 static void pdb_default_endsampwent(struct pdb_methods *methods)
1196 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1199 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1201 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1204 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1206 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1209 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1211 *seq_num = time(NULL);
1212 return NT_STATUS_OK;
1215 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1218 struct samu *sampw = NULL;
1219 struct passwd *unix_pw;
1222 unix_pw = sys_getpwuid( uid );
1225 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1226 "%lu\n", (unsigned long)uid));
1230 if ( !(sampw = samu_new( NULL )) ) {
1231 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1236 ret = NT_STATUS_IS_OK(
1237 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1241 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1242 "%s (%d)\n", unix_pw->pw_name, uid));
1247 sid_copy(sid, pdb_get_user_sid(sampw));
1254 static bool pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1260 ret = pdb_default_uid_to_sid(methods, uid, &sid);
1265 ret = sid_peek_check_rid(get_global_sam_sid(), &sid, rid);
1268 DEBUG(1, ("Could not peek rid out of sid %s\n",
1269 sid_string_dbg(&sid)));
1275 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1280 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1284 sid_copy(sid, &map.sid);
1288 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1290 union unid_t *id, enum lsa_SidType *type)
1292 TALLOC_CTX *mem_ctx;
1297 mem_ctx = talloc_new(NULL);
1299 if (mem_ctx == NULL) {
1300 DEBUG(0, ("talloc_new failed\n"));
1304 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1305 /* Here we might have users as well as groups and aliases */
1306 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1310 /* check for "Unix User" */
1312 if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1314 *type = SID_NAME_USER;
1319 /* check for "Unix Group" */
1321 if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1323 *type = SID_NAME_ALIAS;
1330 if (sid_check_is_in_builtin(sid) ||
1331 sid_check_is_in_wellknown_domain(sid)) {
1332 /* Here we only have aliases */
1334 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1335 DEBUG(10, ("Could not find map for sid %s\n",
1336 sid_string_dbg(sid)));
1339 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1340 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1341 DEBUG(10, ("Map for sid %s is a %s, expected an "
1342 "alias\n", sid_string_dbg(sid),
1343 sid_type_lookup(map.sid_name_use)));
1348 *type = SID_NAME_ALIAS;
1353 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1354 sid_string_dbg(sid)));
1358 TALLOC_FREE(mem_ctx);
1362 static bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1363 uid_t uid, uid_t **pp_uids, size_t *p_num)
1367 for (i=0; i<*p_num; i++) {
1368 if ((*pp_uids)[i] == uid)
1372 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1374 if (*pp_uids == NULL)
1377 (*pp_uids)[*p_num] = uid;
1382 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1393 /* We only look at our own sam, so don't care about imported stuff */
1394 winbind_env = winbind_env_set();
1397 if ((grp = getgrgid(gid)) == NULL) {
1398 /* allow winbindd lookups, but only if they weren't already disabled */
1402 /* Primary group members */
1404 while ((pwd = getpwent()) != NULL) {
1405 if (pwd->pw_gid == gid) {
1406 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1414 /* Secondary group members */
1415 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1416 struct passwd *pw = getpwnam(*gr);
1420 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1429 /* allow winbindd lookups, but only if they weren't already disabled */
1437 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1438 TALLOC_CTX *mem_ctx,
1439 const DOM_SID *group,
1440 uint32 **pp_member_rids,
1441 size_t *p_num_members)
1447 *pp_member_rids = NULL;
1450 if (!sid_to_gid(group, &gid))
1451 return NT_STATUS_NO_SUCH_GROUP;
1453 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1454 return NT_STATUS_NO_SUCH_GROUP;
1457 return NT_STATUS_OK;
1459 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1461 for (i=0; i<num_uids; i++) {
1464 uid_to_sid(&sid, uids[i]);
1466 if (!sid_check_is_in_our_domain(&sid)) {
1467 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1468 "in our domain\n"));
1472 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1473 *p_num_members += 1;
1476 return NT_STATUS_OK;
1479 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1480 TALLOC_CTX *mem_ctx,
1484 size_t *p_num_groups)
1489 const char *username = pdb_get_username(user);
1492 /* Ignore the primary group SID. Honor the real Unix primary group.
1493 The primary group SID is only of real use to Windows clients */
1495 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1496 return NT_STATUS_NO_SUCH_USER;
1503 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1504 return NT_STATUS_NO_SUCH_USER;
1507 if (*p_num_groups == 0) {
1508 smb_panic("primary group missing");
1511 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1513 if (*pp_sids == NULL) {
1514 TALLOC_FREE(*pp_gids);
1515 return NT_STATUS_NO_MEMORY;
1518 for (i=0; i<*p_num_groups; i++) {
1519 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1522 return NT_STATUS_OK;
1525 /*******************************************************************
1526 Look up a rid in the SAM we're responsible for (i.e. passdb)
1527 ********************************************************************/
1529 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1531 enum lsa_SidType *psid_name_use,
1532 union unid_t *unix_id)
1534 struct samu *sam_account = NULL;
1539 *psid_name_use = SID_NAME_UNKNOWN;
1541 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1542 (unsigned int)rid));
1544 sid_copy(&sid, get_global_sam_sid());
1545 sid_append_rid(&sid, rid);
1547 /* see if the passdb can help us with the name of the user */
1549 if ( !(sam_account = samu_new( NULL )) ) {
1553 /* BEING ROOT BLLOCK */
1555 if (pdb_getsampwsid(sam_account, &sid)) {
1558 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1559 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1561 TALLOC_FREE(sam_account);
1565 *psid_name_use = SID_NAME_USER;
1567 TALLOC_FREE(sam_account);
1569 if (unix_id == NULL) {
1573 pw = Get_Pwnam(*name);
1577 unix_id->uid = pw->pw_uid;
1580 TALLOC_FREE(sam_account);
1582 ret = pdb_getgrsid(&map, sid);
1584 /* END BECOME_ROOT BLOCK */
1586 /* do not resolve SIDs to a name unless there is a valid
1587 gid associated with it */
1589 if ( ret && (map.gid != (gid_t)-1) ) {
1590 *name = talloc_strdup(mem_ctx, map.nt_name);
1591 *psid_name_use = map.sid_name_use;
1594 unix_id->gid = map.gid;
1600 /* Windows will always map RID 513 to something. On a non-domain
1601 controller, this gets mapped to SERVER\None. */
1604 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1608 if ( rid == DOMAIN_GROUP_RID_USERS ) {
1609 *name = talloc_strdup(mem_ctx, "None" );
1610 *psid_name_use = SID_NAME_DOM_GRP;
1618 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1619 const DOM_SID *domain_sid,
1623 enum lsa_SidType *attrs)
1627 bool have_mapped = False;
1628 bool have_unmapped = False;
1630 if (sid_check_is_builtin(domain_sid)) {
1632 for (i=0; i<num_rids; i++) {
1635 if (lookup_builtin_rid(names, rids[i], &name)) {
1636 attrs[i] = SID_NAME_ALIAS;
1638 DEBUG(5,("lookup_rids: %s:%d\n",
1639 names[i], attrs[i]));
1642 have_unmapped = True;
1643 attrs[i] = SID_NAME_UNKNOWN;
1649 /* Should not happen, but better check once too many */
1650 if (!sid_check_is_domain(domain_sid)) {
1651 return NT_STATUS_INVALID_HANDLE;
1654 for (i = 0; i < num_rids; i++) {
1657 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1660 return NT_STATUS_NO_MEMORY;
1663 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1666 have_unmapped = True;
1667 attrs[i] = SID_NAME_UNKNOWN;
1673 result = NT_STATUS_NONE_MAPPED;
1676 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1682 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1683 const DOM_SID *domain_sid,
1687 enum lsa_SidType *attrs)
1691 bool have_mapped = False;
1692 bool have_unmapped = False;
1694 if (sid_check_is_builtin(domain_sid)) {
1696 for (i=0; i<num_names; i++) {
1699 if (lookup_builtin_name(names[i], &rid)) {
1700 attrs[i] = SID_NAME_ALIAS;
1702 DEBUG(5,("lookup_rids: %s:%d\n",
1703 names[i], attrs[i]));
1706 have_unmapped = True;
1707 attrs[i] = SID_NAME_UNKNOWN;
1713 /* Should not happen, but better check once too many */
1714 if (!sid_check_is_domain(domain_sid)) {
1715 return NT_STATUS_INVALID_HANDLE;
1718 for (i = 0; i < num_names; i++) {
1719 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1720 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1721 rids[i], attrs[i]));
1724 have_unmapped = True;
1725 attrs[i] = SID_NAME_UNKNOWN;
1731 result = NT_STATUS_NONE_MAPPED;
1734 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1740 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1742 TALLOC_CTX *mem_ctx;
1743 struct pdb_search *result;
1745 mem_ctx = talloc_init("pdb_search");
1746 if (mem_ctx == NULL) {
1747 DEBUG(0, ("talloc_init failed\n"));
1751 result = TALLOC_P(mem_ctx, struct pdb_search);
1752 if (result == NULL) {
1753 DEBUG(0, ("talloc failed\n"));
1757 result->mem_ctx = mem_ctx;
1758 result->type = type;
1759 result->cache = NULL;
1760 result->num_entries = 0;
1761 result->cache_size = 0;
1762 result->search_ended = False;
1764 /* Segfault appropriately if not initialized */
1765 result->next_entry = NULL;
1766 result->search_end = NULL;
1771 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1773 const char *account_name,
1774 const char *fullname,
1775 const char *description,
1776 struct samr_displayentry *entry)
1779 entry->acct_flags = acct_flags;
1781 if (account_name != NULL)
1782 entry->account_name = talloc_strdup(mem_ctx, account_name);
1784 entry->account_name = "";
1786 if (fullname != NULL)
1787 entry->fullname = talloc_strdup(mem_ctx, fullname);
1789 entry->fullname = "";
1791 if (description != NULL)
1792 entry->description = talloc_strdup(mem_ctx, description);
1794 entry->description = "";
1797 static bool user_search_in_progress = False;
1798 struct user_search {
1802 static bool next_entry_users(struct pdb_search *s,
1803 struct samr_displayentry *entry)
1805 struct user_search *state = (struct user_search *)s->private_data;
1806 struct samu *user = NULL;
1809 if ( !(user = samu_new( NULL )) ) {
1810 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1814 if (!pdb_getsampwent(user)) {
1819 if ((state->acct_flags != 0) &&
1820 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1825 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1826 pdb_get_acct_ctrl(user), pdb_get_username(user),
1827 pdb_get_fullname(user), pdb_get_acct_desc(user),
1834 static void search_end_users(struct pdb_search *search)
1837 user_search_in_progress = False;
1840 static bool pdb_default_search_users(struct pdb_methods *methods,
1841 struct pdb_search *search,
1844 struct user_search *state;
1846 if (user_search_in_progress) {
1847 DEBUG(1, ("user search in progress\n"));
1851 if (!pdb_setsampwent(False, acct_flags)) {
1852 DEBUG(5, ("Could not start search\n"));
1856 user_search_in_progress = True;
1858 state = TALLOC_P(search->mem_ctx, struct user_search);
1859 if (state == NULL) {
1860 DEBUG(0, ("talloc failed\n"));
1864 state->acct_flags = acct_flags;
1866 search->private_data = state;
1867 search->next_entry = next_entry_users;
1868 search->search_end = search_end_users;
1872 struct group_search {
1874 size_t num_groups, current_group;
1877 static bool next_entry_groups(struct pdb_search *s,
1878 struct samr_displayentry *entry)
1880 struct group_search *state = (struct group_search *)s->private_data;
1882 GROUP_MAP *map = &state->groups[state->current_group];
1884 if (state->current_group == state->num_groups)
1887 sid_peek_rid(&map->sid, &rid);
1889 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1892 state->current_group += 1;
1896 static void search_end_groups(struct pdb_search *search)
1898 struct group_search *state =
1899 (struct group_search *)search->private_data;
1900 SAFE_FREE(state->groups);
1903 static bool pdb_search_grouptype(struct pdb_search *search,
1904 const DOM_SID *sid, enum lsa_SidType type)
1906 struct group_search *state;
1908 state = TALLOC_P(search->mem_ctx, struct group_search);
1909 if (state == NULL) {
1910 DEBUG(0, ("talloc failed\n"));
1914 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1916 DEBUG(0, ("Could not enum groups\n"));
1920 state->current_group = 0;
1921 search->private_data = state;
1922 search->next_entry = next_entry_groups;
1923 search->search_end = search_end_groups;
1927 static bool pdb_default_search_groups(struct pdb_methods *methods,
1928 struct pdb_search *search)
1930 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1933 static bool pdb_default_search_aliases(struct pdb_methods *methods,
1934 struct pdb_search *search,
1938 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1941 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1944 if (idx < search->num_entries)
1945 return &search->cache[idx];
1947 if (search->search_ended)
1950 while (idx >= search->num_entries) {
1951 struct samr_displayentry entry;
1953 if (!search->next_entry(search, &entry)) {
1954 search->search_end(search);
1955 search->search_ended = True;
1959 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1960 entry, &search->cache, &search->num_entries,
1961 &search->cache_size);
1964 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1967 struct pdb_search *pdb_search_users(uint32 acct_flags)
1969 struct pdb_methods *pdb = pdb_get_methods();
1970 struct pdb_search *result;
1972 result = pdb_search_init(PDB_USER_SEARCH);
1973 if (result == NULL) {
1977 if (!pdb->search_users(pdb, result, acct_flags)) {
1978 talloc_destroy(result->mem_ctx);
1984 struct pdb_search *pdb_search_groups(void)
1986 struct pdb_methods *pdb = pdb_get_methods();
1987 struct pdb_search *result;
1989 result = pdb_search_init(PDB_GROUP_SEARCH);
1990 if (result == NULL) {
1994 if (!pdb->search_groups(pdb, result)) {
1995 talloc_destroy(result->mem_ctx);
2001 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
2003 struct pdb_methods *pdb = pdb_get_methods();
2004 struct pdb_search *result;
2006 if (pdb == NULL) return NULL;
2008 result = pdb_search_init(PDB_ALIAS_SEARCH);
2009 if (result == NULL) return NULL;
2011 if (!pdb->search_aliases(pdb, result, sid)) {
2012 talloc_destroy(result->mem_ctx);
2018 uint32 pdb_search_entries(struct pdb_search *search,
2019 uint32 start_idx, uint32 max_entries,
2020 struct samr_displayentry **result)
2022 struct samr_displayentry *end_entry;
2023 uint32 end_idx = start_idx+max_entries-1;
2025 /* The first entry needs to be searched after the last. Otherwise the
2026 * first entry might have moved due to a realloc during the search for
2027 * the last entry. */
2029 end_entry = pdb_search_getentry(search, end_idx);
2030 *result = pdb_search_getentry(search, start_idx);
2032 if (end_entry != NULL)
2035 if (start_idx >= search->num_entries)
2038 return search->num_entries - start_idx;
2041 void pdb_search_destroy(struct pdb_search *search)
2046 if (!search->search_ended)
2047 search->search_end(search);
2049 talloc_destroy(search->mem_ctx);
2052 /*******************************************************************
2054 *******************************************************************/
2056 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, DOM_SID *sid,
2057 time_t *pass_last_set_time)
2059 struct pdb_methods *pdb = pdb_get_methods();
2060 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2061 pass_last_set_time);
2064 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2067 struct pdb_methods *pdb = pdb_get_methods();
2068 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2071 bool pdb_del_trusteddom_pw(const char *domain)
2073 struct pdb_methods *pdb = pdb_get_methods();
2074 return pdb->del_trusteddom_pw(pdb, domain);
2077 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32 *num_domains,
2078 struct trustdom_info ***domains)
2080 struct pdb_methods *pdb = pdb_get_methods();
2081 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2084 /*******************************************************************
2085 the defaults for trustdom methods:
2086 these simply call the original passdb/secrets.c actions,
2087 to be replaced by pdb_ldap.
2088 *******************************************************************/
2090 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2094 time_t *pass_last_set_time)
2096 return secrets_fetch_trusted_domain_password(domain, pwd,
2097 sid, pass_last_set_time);
2101 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2106 return secrets_store_trusted_domain_password(domain, pwd, sid);
2109 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2112 return trusted_domain_password_delete(domain);
2115 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2116 TALLOC_CTX *mem_ctx,
2117 uint32 *num_domains,
2118 struct trustdom_info ***domains)
2120 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2123 /*******************************************************************
2124 Create a pdb_methods structure and initialize it with the default
2125 operations. In this way a passdb module can simply implement
2126 the functionality it cares about. However, normally this is done
2127 in groups of related functions.
2128 *******************************************************************/
2130 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2132 /* allocate memory for the structure as its own talloc CTX */
2134 if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
2135 return NT_STATUS_NO_MEMORY;
2138 (*methods)->setsampwent = pdb_default_setsampwent;
2139 (*methods)->endsampwent = pdb_default_endsampwent;
2140 (*methods)->getsampwent = pdb_default_getsampwent;
2141 (*methods)->getsampwnam = pdb_default_getsampwnam;
2142 (*methods)->getsampwsid = pdb_default_getsampwsid;
2143 (*methods)->create_user = pdb_default_create_user;
2144 (*methods)->delete_user = pdb_default_delete_user;
2145 (*methods)->add_sam_account = pdb_default_add_sam_account;
2146 (*methods)->update_sam_account = pdb_default_update_sam_account;
2147 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2148 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2149 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2151 (*methods)->getgrsid = pdb_default_getgrsid;
2152 (*methods)->getgrgid = pdb_default_getgrgid;
2153 (*methods)->getgrnam = pdb_default_getgrnam;
2154 (*methods)->create_dom_group = pdb_default_create_dom_group;
2155 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2156 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2157 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2158 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2159 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2160 (*methods)->enum_group_members = pdb_default_enum_group_members;
2161 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2162 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2163 (*methods)->add_groupmem = pdb_default_add_groupmem;
2164 (*methods)->del_groupmem = pdb_default_del_groupmem;
2165 (*methods)->create_alias = pdb_default_create_alias;
2166 (*methods)->delete_alias = pdb_default_delete_alias;
2167 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2168 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2169 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2170 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2171 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2172 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2173 (*methods)->lookup_rids = pdb_default_lookup_rids;
2174 (*methods)->get_account_policy = pdb_default_get_account_policy;
2175 (*methods)->set_account_policy = pdb_default_set_account_policy;
2176 (*methods)->get_seq_num = pdb_default_get_seq_num;
2177 (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2178 (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2179 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2180 (*methods)->sid_to_id = pdb_default_sid_to_id;
2182 (*methods)->search_users = pdb_default_search_users;
2183 (*methods)->search_groups = pdb_default_search_groups;
2184 (*methods)->search_aliases = pdb_default_search_aliases;
2186 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2187 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2188 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2189 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2191 return NT_STATUS_OK;