2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Jelmer Vernooij 2002
6 Copyright (C) Simo Sorce 2003
7 Copyright (C) Volker Lendecke 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_PASSDB
29 /* Cache of latest SAM lookup query */
31 static struct samu *csamuser = NULL;
35 static struct pdb_init_function_entry *backends = NULL;
37 static void lazy_initialize_passdb(void)
39 static BOOL initialized = False;
47 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
49 enum lsa_SidType *psid_name_use,
50 union unid_t *unix_id);
52 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
54 struct pdb_init_function_entry *entry = backends;
56 if(version != PASSDB_INTERFACE_VERSION) {
57 DEBUG(0,("Can't register passdb backend!\n"
58 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
59 "while this version of samba uses version %d\n",
60 version,PASSDB_INTERFACE_VERSION));
61 return NT_STATUS_OBJECT_TYPE_MISMATCH;
65 return NT_STATUS_INVALID_PARAMETER;
68 DEBUG(5,("Attempting to register passdb backend %s\n", name));
70 /* Check for duplicates */
71 if (pdb_find_backend_entry(name)) {
72 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
73 return NT_STATUS_OBJECT_NAME_COLLISION;
76 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
77 entry->name = smb_xstrdup(name);
80 DLIST_ADD(backends, entry);
81 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
85 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
87 struct pdb_init_function_entry *entry = backends;
90 if (strcmp(entry->name, name)==0) return entry;
98 * The event context for the passdb backend. I know this is a bad hack and yet
99 * another static variable, but our pdb API is a global thing per
100 * definition. The first use for this is the LDAP idle function, more might be
103 * I don't feel too bad about this static variable, it replaces the
104 * smb_idle_event_list that used to exist in lib/module.c. -- VL
107 static struct event_context *pdb_event_ctx;
109 struct event_context *pdb_get_event_context(void)
111 return pdb_event_ctx;
114 /******************************************************************
115 Make a pdb_methods from scratch
116 *******************************************************************/
118 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
120 char *module_name = smb_xstrdup(selected);
121 char *module_location = NULL, *p;
122 struct pdb_init_function_entry *entry;
123 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
125 lazy_initialize_passdb();
127 p = strchr(module_name, ':');
131 module_location = p+1;
132 trim_char(module_location, ' ', ' ');
135 trim_char(module_name, ' ', ' ');
138 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
140 entry = pdb_find_backend_entry(module_name);
142 /* Try to find a module that contains this module */
144 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
145 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
146 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
147 SAFE_FREE(module_name);
148 return NT_STATUS_UNSUCCESSFUL;
152 /* No such backend found */
154 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
155 SAFE_FREE(module_name);
156 return NT_STATUS_INVALID_PARAMETER;
159 DEBUG(5,("Found pdb backend %s\n", module_name));
161 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
162 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
163 selected, nt_errstr(nt_status)));
164 SAFE_FREE(module_name);
168 SAFE_FREE(module_name);
170 DEBUG(5,("pdb backend %s has a valid init\n", selected));
175 /******************************************************************
176 Return an already initialised pdn_methods structure
177 *******************************************************************/
179 static struct pdb_methods *pdb_get_methods_reload( BOOL reload )
181 static struct pdb_methods *pdb = NULL;
183 if ( pdb && reload ) {
184 pdb->free_private_data( &(pdb->private_data) );
185 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
187 slprintf(msg, sizeof(msg)-1, "pdb_get_methods_reload: 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 slprintf(msg, sizeof(msg)-1, "pdb_get_methods_reload: failed to get pdb methods for backend %s\n",
197 lp_passdb_backend() );
205 static struct pdb_methods *pdb_get_methods(void)
207 return pdb_get_methods_reload(False);
210 /******************************************************************
211 Backward compatibility functions for the original passdb interface
212 *******************************************************************/
214 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask)
216 struct pdb_methods *pdb = pdb_get_methods();
217 return NT_STATUS_IS_OK(pdb->setsampwent(pdb, update, acb_mask));
220 void pdb_endsampwent(void)
222 struct pdb_methods *pdb = pdb_get_methods();
223 pdb->endsampwent(pdb);
226 BOOL pdb_getsampwent(struct samu *user)
228 struct pdb_methods *pdb = pdb_get_methods();
230 if ( !NT_STATUS_IS_OK(pdb->getsampwent(pdb, user) ) ) {
237 BOOL pdb_getsampwnam(struct samu *sam_acct, const char *username)
239 struct pdb_methods *pdb = pdb_get_methods();
241 if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
246 TALLOC_FREE(csamuser);
249 csamuser = samu_new( NULL );
254 if (!pdb_copy_sam_account(csamuser, sam_acct)) {
255 TALLOC_FREE(csamuser);
262 /**********************************************************************
263 **********************************************************************/
265 BOOL guest_user_info( struct samu *user )
269 const char *guestname = lp_guestaccount();
271 if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
272 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
277 result = samu_set_unix(user, pwd );
281 return NT_STATUS_IS_OK( result );
284 /**********************************************************************
285 **********************************************************************/
287 BOOL pdb_getsampwsid(struct samu *sam_acct, const DOM_SID *sid)
289 struct pdb_methods *pdb = pdb_get_methods();
292 /* hard code the Guest RID of 501 */
294 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
297 if ( rid == DOMAIN_USER_RID_GUEST ) {
298 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
299 return guest_user_info( sam_acct );
302 /* check the cache first */
304 if ( csamuser && sid_equal(sid, pdb_get_user_sid(csamuser) ) )
305 return pdb_copy_sam_account(sam_acct, csamuser);
307 return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
310 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
311 TALLOC_CTX *tmp_ctx, const char *name,
312 uint32 acb_info, uint32 *rid)
314 struct samu *sam_pass;
318 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
319 return NT_STATUS_NO_MEMORY;
322 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
327 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
328 pstrcpy(add_script, lp_adduser_script());
330 pstrcpy(add_script, lp_addmachine_script());
333 if (add_script[0] == '\0') {
334 DEBUG(3, ("Could not find user %s and no add script "
336 return NT_STATUS_NO_SUCH_USER;
339 /* lowercase the username before creating the Unix account for
340 compatibility with previous Samba releases */
341 fstrcpy( name2, name );
343 all_string_sub(add_script, "%u", name2, sizeof(add_script));
344 add_ret = smbrun(add_script,NULL);
345 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
346 add_script, add_ret));
348 smb_nscd_flush_user_cache();
351 #ifdef ENABLE_BUILD_FARM_HACKS
353 DEBUG(1, ("Creating a faked user %s for build farm "
354 "purposes\n", name));
355 faked_create_user(name);
361 pwd = Get_Pwnam_alloc(tmp_ctx, name);
364 /* we have a valid SID coming out of this call */
366 status = samu_alloc_rid_unix( sam_pass, pwd );
370 if (!NT_STATUS_IS_OK(status)) {
371 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
375 if (!sid_peek_check_rid(get_global_sam_sid(),
376 pdb_get_user_sid(sam_pass), rid)) {
377 DEBUG(0, ("Could not get RID of fresh user\n"));
378 return NT_STATUS_INTERNAL_ERROR;
381 /* Use the username case specified in the original request */
383 pdb_set_username( sam_pass, name, PDB_SET );
385 /* Disable the account on creation, it does not have a reasonable password yet. */
387 acb_info |= ACB_DISABLED;
389 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
391 status = pdb_add_sam_account(sam_pass);
393 TALLOC_FREE(sam_pass);
398 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
401 struct pdb_methods *pdb = pdb_get_methods();
402 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
405 /****************************************************************************
406 Delete a UNIX user on demand.
407 ****************************************************************************/
409 static int smb_delete_user(const char *unix_user)
416 if ( strequal( unix_user, "root" ) ) {
417 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
421 pstrcpy(del_script, lp_deluser_script());
424 all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
425 ret = smbrun(del_script,NULL);
428 smb_nscd_flush_user_cache();
430 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
435 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
437 struct samu *sam_acct)
442 status = pdb_delete_sam_account(sam_acct);
443 if (!NT_STATUS_IS_OK(status)) {
448 * Now delete the unix side ....
449 * note: we don't check if the delete really happened as the script is
450 * not necessary present and maybe the sysadmin doesn't want to delete
454 /* always lower case the username before handing it off to
457 fstrcpy( username, pdb_get_username(sam_acct) );
458 strlower_m( username );
460 smb_delete_user( username );
465 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
467 struct pdb_methods *pdb = pdb_get_methods();
470 /* sanity check to make sure we don't delete root */
472 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
473 return NT_STATUS_NO_SUCH_USER;
477 return NT_STATUS_ACCESS_DENIED;
480 return pdb->delete_user(pdb, mem_ctx, sam_acct);
483 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
485 struct pdb_methods *pdb = pdb_get_methods();
486 return pdb->add_sam_account(pdb, sam_acct);
489 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
491 struct pdb_methods *pdb = pdb_get_methods();
493 if (csamuser != NULL) {
494 TALLOC_FREE(csamuser);
498 return pdb->update_sam_account(pdb, sam_acct);
501 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
503 struct pdb_methods *pdb = pdb_get_methods();
505 if (csamuser != NULL) {
506 TALLOC_FREE(csamuser);
510 return pdb->delete_sam_account(pdb, sam_acct);
513 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
515 struct pdb_methods *pdb = pdb_get_methods();
519 if (csamuser != NULL) {
520 TALLOC_FREE(csamuser);
524 /* sanity check to make sure we don't rename root */
526 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
527 return NT_STATUS_NO_SUCH_USER;
531 return NT_STATUS_ACCESS_DENIED;
534 status = pdb->rename_sam_account(pdb, oldname, newname);
536 /* always flush the cache here just to be safe */
542 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, BOOL success)
544 struct pdb_methods *pdb = pdb_get_methods();
545 return pdb->update_login_attempts(pdb, sam_acct, success);
548 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
550 struct pdb_methods *pdb = pdb_get_methods();
551 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
554 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
556 struct pdb_methods *pdb = pdb_get_methods();
557 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
560 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
562 struct pdb_methods *pdb = pdb_get_methods();
563 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
566 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
574 grp = getgrnam(name);
579 if (smb_create_group(name, &gid) != 0) {
580 return NT_STATUS_ACCESS_DENIED;
587 return NT_STATUS_ACCESS_DENIED;
590 if (pdb_rid_algorithm()) {
591 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
593 if (!pdb_new_rid(rid)) {
594 return NT_STATUS_ACCESS_DENIED;
598 sid_compose(&group_sid, get_global_sam_sid(), *rid);
600 return add_initial_entry(grp->gr_gid, sid_string_static(&group_sid),
601 SID_NAME_DOM_GRP, name, NULL);
604 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
607 struct pdb_methods *pdb = pdb_get_methods();
608 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
611 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
619 const char *grp_name;
621 sid_compose(&group_sid, get_global_sam_sid(), rid);
623 if (!get_domain_group_from_sid(group_sid, &map)) {
624 DEBUG(10, ("Could not find group for rid %d\n", rid));
625 return NT_STATUS_NO_SUCH_GROUP;
628 /* We need the group name for the smb_delete_group later on */
630 if (map.gid == (gid_t)-1) {
631 return NT_STATUS_NO_SUCH_GROUP;
634 grp = getgrgid(map.gid);
636 return NT_STATUS_NO_SUCH_GROUP;
639 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
641 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
642 if (grp_name == NULL) {
643 return NT_STATUS_NO_MEMORY;
646 status = pdb_delete_group_mapping_entry(group_sid);
648 if (!NT_STATUS_IS_OK(status)) {
652 /* Don't check the result of smb_delete_group */
654 smb_delete_group(grp_name);
659 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
661 struct pdb_methods *pdb = pdb_get_methods();
662 return pdb->delete_dom_group(pdb, mem_ctx, rid);
665 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
667 struct pdb_methods *pdb = pdb_get_methods();
668 return pdb->add_group_mapping_entry(pdb, map);
671 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
673 struct pdb_methods *pdb = pdb_get_methods();
674 return pdb->update_group_mapping_entry(pdb, map);
677 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
679 struct pdb_methods *pdb = pdb_get_methods();
680 return pdb->delete_group_mapping_entry(pdb, sid);
683 BOOL pdb_enum_group_mapping(const DOM_SID *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
684 size_t *p_num_entries, BOOL unix_only)
686 struct pdb_methods *pdb = pdb_get_methods();
687 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
688 pp_rmap, p_num_entries, unix_only));
691 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
693 uint32 **pp_member_rids,
694 size_t *p_num_members)
696 struct pdb_methods *pdb = pdb_get_methods();
699 result = pdb->enum_group_members(pdb, mem_ctx,
700 sid, pp_member_rids, p_num_members);
702 /* special check for rid 513 */
704 if ( !NT_STATUS_IS_OK( result ) ) {
707 sid_peek_rid( sid, &rid );
709 if ( rid == DOMAIN_GROUP_RID_USERS ) {
711 *pp_member_rids = NULL;
720 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
721 DOM_SID **pp_sids, gid_t **pp_gids,
722 size_t *p_num_groups)
724 struct pdb_methods *pdb = pdb_get_methods();
725 return pdb->enum_group_memberships(
727 pp_sids, pp_gids, p_num_groups);
730 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
732 struct samu *sampass)
737 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
738 (grp = getgrgid(gid)) == NULL) {
739 return NT_STATUS_INVALID_PRIMARY_GROUP;
742 if (smb_set_primary_group(grp->gr_name,
743 pdb_get_username(sampass)) != 0) {
744 return NT_STATUS_ACCESS_DENIED;
750 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
752 struct pdb_methods *pdb = pdb_get_methods();
753 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
757 * Helper function to see whether a user is in a group. We can't use
758 * user_in_group_sid here because this creates dependencies only smbd can
762 static BOOL pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
763 const DOM_SID *group_sid)
767 size_t i, num_groups;
769 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
775 for (i=0; i<num_groups; i++) {
776 if (sid_equal(group_sid, &sids[i])) {
783 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
788 DOM_SID group_sid, member_sid;
789 struct samu *account = NULL;
793 const char *group_name;
796 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
797 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
799 if (!get_domain_group_from_sid(group_sid, &map) ||
800 (map.gid == (gid_t)-1) ||
801 ((grp = getgrgid(map.gid)) == NULL)) {
802 return NT_STATUS_NO_SUCH_GROUP;
805 group_name = talloc_strdup(mem_ctx, grp->gr_name);
806 if (group_name == NULL) {
807 return NT_STATUS_NO_MEMORY;
810 if ( !(account = samu_new( NULL )) ) {
811 return NT_STATUS_NO_MEMORY;
814 if (!pdb_getsampwsid(account, &member_sid) ||
815 !sid_to_uid(&member_sid, &uid) ||
816 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
817 return NT_STATUS_NO_SUCH_USER;
820 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
821 return NT_STATUS_MEMBER_IN_GROUP;
825 * ok, the group exist, the user exist, the user is not in the group,
826 * we can (finally) add it to the group !
829 smb_add_user_group(group_name, pwd->pw_name);
831 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
832 return NT_STATUS_ACCESS_DENIED;
838 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
841 struct pdb_methods *pdb = pdb_get_methods();
842 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
845 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
850 DOM_SID group_sid, member_sid;
851 struct samu *account = NULL;
855 const char *group_name;
858 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
859 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
861 if (!get_domain_group_from_sid(group_sid, &map) ||
862 (map.gid == (gid_t)-1) ||
863 ((grp = getgrgid(map.gid)) == NULL)) {
864 return NT_STATUS_NO_SUCH_GROUP;
867 group_name = talloc_strdup(mem_ctx, grp->gr_name);
868 if (group_name == NULL) {
869 return NT_STATUS_NO_MEMORY;
872 if ( !(account = samu_new( NULL )) ) {
873 return NT_STATUS_NO_MEMORY;
876 if (!pdb_getsampwsid(account, &member_sid) ||
877 !sid_to_uid(&member_sid, &uid) ||
878 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
879 return NT_STATUS_NO_SUCH_USER;
882 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
883 return NT_STATUS_MEMBER_NOT_IN_GROUP;
887 * ok, the group exist, the user exist, the user is in the group,
888 * we can (finally) delete it from the group!
891 smb_delete_user_group(group_name, pwd->pw_name);
893 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
894 return NT_STATUS_ACCESS_DENIED;
900 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
903 struct pdb_methods *pdb = pdb_get_methods();
904 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
907 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
909 struct pdb_methods *pdb = pdb_get_methods();
910 return NT_STATUS_IS_OK(pdb->find_alias(pdb, name, sid));
913 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
915 struct pdb_methods *pdb = pdb_get_methods();
916 return pdb->create_alias(pdb, name, rid);
919 BOOL pdb_delete_alias(const DOM_SID *sid)
921 struct pdb_methods *pdb = pdb_get_methods();
922 return NT_STATUS_IS_OK(pdb->delete_alias(pdb, sid));
926 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
928 struct pdb_methods *pdb = pdb_get_methods();
929 return NT_STATUS_IS_OK(pdb->get_aliasinfo(pdb, sid, info));
932 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
934 struct pdb_methods *pdb = pdb_get_methods();
935 return NT_STATUS_IS_OK(pdb->set_aliasinfo(pdb, sid, info));
938 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
940 struct pdb_methods *pdb = pdb_get_methods();
941 return pdb->add_aliasmem(pdb, alias, member);
944 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
946 struct pdb_methods *pdb = pdb_get_methods();
947 return pdb->del_aliasmem(pdb, alias, member);
950 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
951 DOM_SID **pp_members, size_t *p_num_members)
953 struct pdb_methods *pdb = pdb_get_methods();
954 return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
957 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
958 const DOM_SID *domain_sid,
959 const DOM_SID *members, size_t num_members,
960 uint32 **pp_alias_rids,
961 size_t *p_num_alias_rids)
963 struct pdb_methods *pdb = pdb_get_methods();
964 return pdb->enum_alias_memberships(pdb, mem_ctx,
966 members, num_members,
971 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
975 enum lsa_SidType *attrs)
977 struct pdb_methods *pdb = pdb_get_methods();
978 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
982 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
984 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
985 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
986 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
987 * down to are pdb_getsampwnam and pdb_getgrnam instead of
989 * But in principle, it the call belongs to the API and might get
990 * used in this context some day.
993 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
997 enum lsa_SidType *attrs)
999 struct pdb_methods *pdb = pdb_get_methods();
1000 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
1004 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
1006 struct pdb_methods *pdb = pdb_get_methods();
1010 status = pdb->get_account_policy(pdb, policy_index, value);
1013 return NT_STATUS_IS_OK(status);
1016 BOOL pdb_set_account_policy(int policy_index, uint32 value)
1018 struct pdb_methods *pdb = pdb_get_methods();
1022 status = pdb->set_account_policy(pdb, policy_index, value);
1025 return NT_STATUS_IS_OK(status);
1028 BOOL pdb_get_seq_num(time_t *seq_num)
1030 struct pdb_methods *pdb = pdb_get_methods();
1031 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1034 BOOL pdb_uid_to_rid(uid_t uid, uint32 *rid)
1036 struct pdb_methods *pdb = pdb_get_methods();
1037 return pdb->uid_to_rid(pdb, uid, rid);
1040 BOOL pdb_uid_to_sid(uid_t uid, DOM_SID *sid)
1042 struct pdb_methods *pdb = pdb_get_methods();
1043 return pdb->uid_to_sid(pdb, uid, sid);
1046 BOOL pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1048 struct pdb_methods *pdb = pdb_get_methods();
1049 return pdb->gid_to_sid(pdb, gid, sid);
1052 BOOL pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1053 enum lsa_SidType *type)
1055 struct pdb_methods *pdb = pdb_get_methods();
1056 return pdb->sid_to_id(pdb, sid, id, type);
1059 BOOL pdb_rid_algorithm(void)
1061 struct pdb_methods *pdb = pdb_get_methods();
1062 return pdb->rid_algorithm(pdb);
1065 /********************************************************************
1066 Allocate a new RID from the passdb backend. Verify that it is free
1067 by calling lookup_global_sam_rid() to verify that the RID is not
1068 in use. This handles servers that have existing users or groups
1069 with add RIDs (assigned from previous algorithmic mappings)
1070 ********************************************************************/
1072 BOOL pdb_new_rid(uint32 *rid)
1074 struct pdb_methods *pdb = pdb_get_methods();
1075 const char *name = NULL;
1076 enum lsa_SidType type;
1077 uint32 allocated_rid = 0;
1081 if (pdb_rid_algorithm()) {
1082 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1087 if (algorithmic_rid_base() != BASE_RID) {
1088 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1089 "without algorithmic RIDs is chosen.\n"));
1090 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1091 "add', set the maximum used RID using\n"));
1092 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1096 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1097 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1101 /* Attempt to get an unused RID (max tires is 250...yes that it is
1102 and arbitrary number I pulkled out of my head). -- jerry */
1104 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1107 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1111 /* validate that the RID is not in use */
1113 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1120 if ( allocated_rid == 0 ) {
1121 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1125 *rid = allocated_rid;
1130 /***************************************************************
1131 Initialize the static context (at smbd startup etc).
1133 If uninitialised, context will auto-init on first use.
1134 ***************************************************************/
1136 BOOL initialize_password_db(BOOL reload, struct event_context *event_ctx)
1138 pdb_event_ctx = event_ctx;
1139 return (pdb_get_methods_reload(reload) != NULL);
1143 /***************************************************************************
1144 Default implementations of some functions.
1145 ****************************************************************************/
1147 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1149 return NT_STATUS_NO_SUCH_USER;
1152 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1154 return NT_STATUS_NO_SUCH_USER;
1157 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1159 return NT_STATUS_NOT_IMPLEMENTED;
1162 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1164 return NT_STATUS_NOT_IMPLEMENTED;
1167 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1169 return NT_STATUS_NOT_IMPLEMENTED;
1172 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1174 return NT_STATUS_NOT_IMPLEMENTED;
1177 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, BOOL success)
1179 return NT_STATUS_OK;
1182 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint32 acb_mask)
1184 return NT_STATUS_NOT_IMPLEMENTED;
1187 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, struct samu *user)
1189 return NT_STATUS_NOT_IMPLEMENTED;
1192 static void pdb_default_endsampwent(struct pdb_methods *methods)
1194 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1197 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1199 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1202 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1204 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1207 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1209 *seq_num = time(NULL);
1210 return NT_STATUS_OK;
1213 static BOOL pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1216 struct samu *sampw = NULL;
1217 struct passwd *unix_pw;
1220 unix_pw = sys_getpwuid( uid );
1223 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1224 "%lu\n", (unsigned long)uid));
1228 if ( !(sampw = samu_new( NULL )) ) {
1229 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1234 ret = NT_STATUS_IS_OK(
1235 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1239 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1240 "%s (%d)\n", unix_pw->pw_name, uid));
1245 sid_copy(sid, pdb_get_user_sid(sampw));
1252 static BOOL pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1258 ret = pdb_default_uid_to_sid(methods, uid, &sid);
1263 ret = sid_peek_check_rid(get_global_sam_sid(), &sid, rid);
1266 DEBUG(1, ("Could not peek rid out of sid %s\n",
1267 sid_string_static(&sid)));
1273 static BOOL pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1278 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1282 sid_copy(sid, &map.sid);
1286 static BOOL pdb_default_sid_to_id(struct pdb_methods *methods,
1288 union unid_t *id, enum lsa_SidType *type)
1290 TALLOC_CTX *mem_ctx;
1295 mem_ctx = talloc_new(NULL);
1297 if (mem_ctx == NULL) {
1298 DEBUG(0, ("talloc_new failed\n"));
1302 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1303 /* Here we might have users as well as groups and aliases */
1304 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1308 /* check for "Unix User" */
1310 if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1312 *type = SID_NAME_USER;
1317 /* check for "Unix User" */
1319 if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1321 *type = SID_NAME_ALIAS;
1328 if (sid_check_is_in_builtin(sid) ||
1329 sid_check_is_in_wellknown_domain(sid)) {
1330 /* Here we only have aliases */
1332 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1333 DEBUG(10, ("Could not find map for sid %s\n",
1334 sid_string_static(sid)));
1337 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1338 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1339 DEBUG(10, ("Map for sid %s is a %s, expected an "
1340 "alias\n", sid_string_static(sid),
1341 sid_type_lookup(map.sid_name_use)));
1346 *type = SID_NAME_ALIAS;
1351 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1352 sid_string_static(sid)));
1356 TALLOC_FREE(mem_ctx);
1360 static BOOL add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1361 uid_t uid, uid_t **pp_uids, size_t *p_num)
1365 for (i=0; i<*p_num; i++) {
1366 if ((*pp_uids)[i] == uid)
1370 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1372 if (*pp_uids == NULL)
1375 (*pp_uids)[*p_num] = uid;
1380 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1391 /* We only look at our own sam, so don't care about imported stuff */
1392 winbind_env = winbind_env_set();
1395 if ((grp = getgrgid(gid)) == NULL) {
1396 /* allow winbindd lookups, but only if they weren't already disabled */
1400 /* Primary group members */
1402 while ((pwd = getpwent()) != NULL) {
1403 if (pwd->pw_gid == gid) {
1404 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1412 /* Secondary group members */
1413 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1414 struct passwd *pw = getpwnam(*gr);
1418 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1427 /* allow winbindd lookups, but only if they weren't already disabled */
1435 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1436 TALLOC_CTX *mem_ctx,
1437 const DOM_SID *group,
1438 uint32 **pp_member_rids,
1439 size_t *p_num_members)
1445 *pp_member_rids = NULL;
1448 if (!sid_to_gid(group, &gid))
1449 return NT_STATUS_NO_SUCH_GROUP;
1451 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1452 return NT_STATUS_NO_SUCH_GROUP;
1455 return NT_STATUS_OK;
1457 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1459 for (i=0; i<num_uids; i++) {
1462 uid_to_sid(&sid, uids[i]);
1464 if (!sid_check_is_in_our_domain(&sid)) {
1465 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1466 "in our domain\n"));
1470 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1471 *p_num_members += 1;
1474 return NT_STATUS_OK;
1477 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1478 TALLOC_CTX *mem_ctx,
1482 size_t *p_num_groups)
1487 const char *username = pdb_get_username(user);
1490 /* Ignore the primary group SID. Honor the real Unix primary group.
1491 The primary group SID is only of real use to Windows clients */
1493 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1494 return NT_STATUS_NO_SUCH_USER;
1501 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1502 return NT_STATUS_NO_SUCH_USER;
1505 if (*p_num_groups == 0) {
1506 smb_panic("primary group missing");
1509 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1511 if (*pp_sids == NULL) {
1512 TALLOC_FREE(*pp_gids);
1513 return NT_STATUS_NO_MEMORY;
1516 for (i=0; i<*p_num_groups; i++) {
1517 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1520 return NT_STATUS_OK;
1523 /*******************************************************************
1524 Look up a rid in the SAM we're responsible for (i.e. passdb)
1525 ********************************************************************/
1527 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1529 enum lsa_SidType *psid_name_use,
1530 union unid_t *unix_id)
1532 struct samu *sam_account = NULL;
1537 *psid_name_use = SID_NAME_UNKNOWN;
1539 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1540 (unsigned int)rid));
1542 sid_copy(&sid, get_global_sam_sid());
1543 sid_append_rid(&sid, rid);
1545 /* see if the passdb can help us with the name of the user */
1547 if ( !(sam_account = samu_new( NULL )) ) {
1551 /* BEING ROOT BLLOCK */
1553 if (pdb_getsampwsid(sam_account, &sid)) {
1556 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1557 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1559 TALLOC_FREE(sam_account);
1563 *psid_name_use = SID_NAME_USER;
1565 TALLOC_FREE(sam_account);
1567 if (unix_id == NULL) {
1571 pw = Get_Pwnam(*name);
1575 unix_id->uid = pw->pw_uid;
1578 TALLOC_FREE(sam_account);
1580 ret = pdb_getgrsid(&map, sid);
1582 /* END BECOME_ROOT BLOCK */
1584 /* do not resolve SIDs to a name unless there is a valid
1585 gid associated with it */
1587 if ( ret && (map.gid != (gid_t)-1) ) {
1588 *name = talloc_strdup(mem_ctx, map.nt_name);
1589 *psid_name_use = map.sid_name_use;
1592 unix_id->gid = map.gid;
1598 /* Windows will always map RID 513 to something. On a non-domain
1599 controller, this gets mapped to SERVER\None. */
1602 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1606 if ( rid == DOMAIN_GROUP_RID_USERS ) {
1607 *name = talloc_strdup(mem_ctx, "None" );
1608 *psid_name_use = SID_NAME_DOM_GRP;
1616 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1617 const DOM_SID *domain_sid,
1621 enum lsa_SidType *attrs)
1625 BOOL have_mapped = False;
1626 BOOL have_unmapped = False;
1628 if (sid_check_is_builtin(domain_sid)) {
1630 for (i=0; i<num_rids; i++) {
1633 if (lookup_builtin_rid(names, rids[i], &name)) {
1634 attrs[i] = SID_NAME_ALIAS;
1636 DEBUG(5,("lookup_rids: %s:%d\n",
1637 names[i], attrs[i]));
1640 have_unmapped = True;
1641 attrs[i] = SID_NAME_UNKNOWN;
1647 /* Should not happen, but better check once too many */
1648 if (!sid_check_is_domain(domain_sid)) {
1649 return NT_STATUS_INVALID_HANDLE;
1652 for (i = 0; i < num_rids; i++) {
1655 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1658 return NT_STATUS_NO_MEMORY;
1661 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1664 have_unmapped = True;
1665 attrs[i] = SID_NAME_UNKNOWN;
1671 result = NT_STATUS_NONE_MAPPED;
1674 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1680 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1681 const DOM_SID *domain_sid,
1685 enum lsa_SidType *attrs)
1689 BOOL have_mapped = False;
1690 BOOL have_unmapped = False;
1692 if (sid_check_is_builtin(domain_sid)) {
1694 for (i=0; i<num_names; i++) {
1697 if (lookup_builtin_name(names[i], &rid)) {
1698 attrs[i] = SID_NAME_ALIAS;
1700 DEBUG(5,("lookup_rids: %s:%d\n",
1701 names[i], attrs[i]));
1704 have_unmapped = True;
1705 attrs[i] = SID_NAME_UNKNOWN;
1711 /* Should not happen, but better check once too many */
1712 if (!sid_check_is_domain(domain_sid)) {
1713 return NT_STATUS_INVALID_HANDLE;
1716 for (i = 0; i < num_names; i++) {
1717 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1718 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1719 rids[i], attrs[i]));
1722 have_unmapped = True;
1723 attrs[i] = SID_NAME_UNKNOWN;
1729 result = NT_STATUS_NONE_MAPPED;
1732 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1738 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1740 TALLOC_CTX *mem_ctx;
1741 struct pdb_search *result;
1743 mem_ctx = talloc_init("pdb_search");
1744 if (mem_ctx == NULL) {
1745 DEBUG(0, ("talloc_init failed\n"));
1749 result = TALLOC_P(mem_ctx, struct pdb_search);
1750 if (result == NULL) {
1751 DEBUG(0, ("talloc failed\n"));
1755 result->mem_ctx = mem_ctx;
1756 result->type = type;
1757 result->cache = NULL;
1758 result->num_entries = 0;
1759 result->cache_size = 0;
1760 result->search_ended = False;
1762 /* Segfault appropriately if not initialized */
1763 result->next_entry = NULL;
1764 result->search_end = NULL;
1769 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1771 const char *account_name,
1772 const char *fullname,
1773 const char *description,
1774 struct samr_displayentry *entry)
1777 entry->acct_flags = acct_flags;
1779 if (account_name != NULL)
1780 entry->account_name = talloc_strdup(mem_ctx, account_name);
1782 entry->account_name = "";
1784 if (fullname != NULL)
1785 entry->fullname = talloc_strdup(mem_ctx, fullname);
1787 entry->fullname = "";
1789 if (description != NULL)
1790 entry->description = talloc_strdup(mem_ctx, description);
1792 entry->description = "";
1795 static BOOL user_search_in_progress = False;
1796 struct user_search {
1800 static BOOL next_entry_users(struct pdb_search *s,
1801 struct samr_displayentry *entry)
1803 struct user_search *state = (struct user_search *)s->private_data;
1804 struct samu *user = NULL;
1807 if ( !(user = samu_new( NULL )) ) {
1808 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1812 if (!pdb_getsampwent(user)) {
1817 if ((state->acct_flags != 0) &&
1818 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1823 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1824 pdb_get_acct_ctrl(user), pdb_get_username(user),
1825 pdb_get_fullname(user), pdb_get_acct_desc(user),
1832 static void search_end_users(struct pdb_search *search)
1835 user_search_in_progress = False;
1838 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1839 struct pdb_search *search,
1842 struct user_search *state;
1844 if (user_search_in_progress) {
1845 DEBUG(1, ("user search in progress\n"));
1849 if (!pdb_setsampwent(False, acct_flags)) {
1850 DEBUG(5, ("Could not start search\n"));
1854 user_search_in_progress = True;
1856 state = TALLOC_P(search->mem_ctx, struct user_search);
1857 if (state == NULL) {
1858 DEBUG(0, ("talloc failed\n"));
1862 state->acct_flags = acct_flags;
1864 search->private_data = state;
1865 search->next_entry = next_entry_users;
1866 search->search_end = search_end_users;
1870 struct group_search {
1872 size_t num_groups, current_group;
1875 static BOOL next_entry_groups(struct pdb_search *s,
1876 struct samr_displayentry *entry)
1878 struct group_search *state = (struct group_search *)s->private_data;
1880 GROUP_MAP *map = &state->groups[state->current_group];
1882 if (state->current_group == state->num_groups)
1885 sid_peek_rid(&map->sid, &rid);
1887 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1890 state->current_group += 1;
1894 static void search_end_groups(struct pdb_search *search)
1896 struct group_search *state =
1897 (struct group_search *)search->private_data;
1898 SAFE_FREE(state->groups);
1901 static BOOL pdb_search_grouptype(struct pdb_search *search,
1902 const DOM_SID *sid, enum lsa_SidType type)
1904 struct group_search *state;
1906 state = TALLOC_P(search->mem_ctx, struct group_search);
1907 if (state == NULL) {
1908 DEBUG(0, ("talloc failed\n"));
1912 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1914 DEBUG(0, ("Could not enum groups\n"));
1918 state->current_group = 0;
1919 search->private_data = state;
1920 search->next_entry = next_entry_groups;
1921 search->search_end = search_end_groups;
1925 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1926 struct pdb_search *search)
1928 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1931 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1932 struct pdb_search *search,
1936 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1939 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1942 if (idx < search->num_entries)
1943 return &search->cache[idx];
1945 if (search->search_ended)
1948 while (idx >= search->num_entries) {
1949 struct samr_displayentry entry;
1951 if (!search->next_entry(search, &entry)) {
1952 search->search_end(search);
1953 search->search_ended = True;
1957 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1958 entry, &search->cache, &search->num_entries,
1959 &search->cache_size);
1962 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1965 struct pdb_search *pdb_search_users(uint32 acct_flags)
1967 struct pdb_methods *pdb = pdb_get_methods();
1968 struct pdb_search *result;
1970 result = pdb_search_init(PDB_USER_SEARCH);
1971 if (result == NULL) {
1975 if (!pdb->search_users(pdb, result, acct_flags)) {
1976 talloc_destroy(result->mem_ctx);
1982 struct pdb_search *pdb_search_groups(void)
1984 struct pdb_methods *pdb = pdb_get_methods();
1985 struct pdb_search *result;
1987 result = pdb_search_init(PDB_GROUP_SEARCH);
1988 if (result == NULL) {
1992 if (!pdb->search_groups(pdb, result)) {
1993 talloc_destroy(result->mem_ctx);
1999 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
2001 struct pdb_methods *pdb = pdb_get_methods();
2002 struct pdb_search *result;
2004 if (pdb == NULL) return NULL;
2006 result = pdb_search_init(PDB_ALIAS_SEARCH);
2007 if (result == NULL) return NULL;
2009 if (!pdb->search_aliases(pdb, result, sid)) {
2010 talloc_destroy(result->mem_ctx);
2016 uint32 pdb_search_entries(struct pdb_search *search,
2017 uint32 start_idx, uint32 max_entries,
2018 struct samr_displayentry **result)
2020 struct samr_displayentry *end_entry;
2021 uint32 end_idx = start_idx+max_entries-1;
2023 /* The first entry needs to be searched after the last. Otherwise the
2024 * first entry might have moved due to a realloc during the search for
2025 * the last entry. */
2027 end_entry = pdb_search_getentry(search, end_idx);
2028 *result = pdb_search_getentry(search, start_idx);
2030 if (end_entry != NULL)
2033 if (start_idx >= search->num_entries)
2036 return search->num_entries - start_idx;
2039 void pdb_search_destroy(struct pdb_search *search)
2044 if (!search->search_ended)
2045 search->search_end(search);
2047 talloc_destroy(search->mem_ctx);
2050 /*******************************************************************
2052 *******************************************************************/
2054 BOOL pdb_get_trusteddom_pw(const char *domain, char** pwd, DOM_SID *sid,
2055 time_t *pass_last_set_time)
2057 struct pdb_methods *pdb = pdb_get_methods();
2058 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2059 pass_last_set_time);
2062 BOOL pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2065 struct pdb_methods *pdb = pdb_get_methods();
2066 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2069 BOOL pdb_del_trusteddom_pw(const char *domain)
2071 struct pdb_methods *pdb = pdb_get_methods();
2072 return pdb->del_trusteddom_pw(pdb, domain);
2075 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32 *num_domains,
2076 struct trustdom_info ***domains)
2078 struct pdb_methods *pdb = pdb_get_methods();
2079 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2082 /*******************************************************************
2083 the defaults for trustdom methods:
2084 these simply call the original passdb/secrets.c actions,
2085 to be replaced by pdb_ldap.
2086 *******************************************************************/
2088 static BOOL pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2092 time_t *pass_last_set_time)
2094 return secrets_fetch_trusted_domain_password(domain, pwd,
2095 sid, pass_last_set_time);
2099 static BOOL pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2104 return secrets_store_trusted_domain_password(domain, pwd, sid);
2107 static BOOL pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2110 return trusted_domain_password_delete(domain);
2113 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2114 TALLOC_CTX *mem_ctx,
2115 uint32 *num_domains,
2116 struct trustdom_info ***domains)
2118 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2121 /*******************************************************************
2122 Create a pdb_methods structure and initialize it with the default
2123 operations. In this way a passdb module can simply implement
2124 the functionality it cares about. However, normally this is done
2125 in groups of related functions.
2126 *******************************************************************/
2128 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2130 /* allocate memory for the structure as its own talloc CTX */
2132 if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
2133 return NT_STATUS_NO_MEMORY;
2136 (*methods)->setsampwent = pdb_default_setsampwent;
2137 (*methods)->endsampwent = pdb_default_endsampwent;
2138 (*methods)->getsampwent = pdb_default_getsampwent;
2139 (*methods)->getsampwnam = pdb_default_getsampwnam;
2140 (*methods)->getsampwsid = pdb_default_getsampwsid;
2141 (*methods)->create_user = pdb_default_create_user;
2142 (*methods)->delete_user = pdb_default_delete_user;
2143 (*methods)->add_sam_account = pdb_default_add_sam_account;
2144 (*methods)->update_sam_account = pdb_default_update_sam_account;
2145 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2146 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2147 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2149 (*methods)->getgrsid = pdb_default_getgrsid;
2150 (*methods)->getgrgid = pdb_default_getgrgid;
2151 (*methods)->getgrnam = pdb_default_getgrnam;
2152 (*methods)->create_dom_group = pdb_default_create_dom_group;
2153 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2154 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2155 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2156 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2157 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2158 (*methods)->enum_group_members = pdb_default_enum_group_members;
2159 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2160 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2161 (*methods)->add_groupmem = pdb_default_add_groupmem;
2162 (*methods)->del_groupmem = pdb_default_del_groupmem;
2163 (*methods)->find_alias = pdb_default_find_alias;
2164 (*methods)->create_alias = pdb_default_create_alias;
2165 (*methods)->delete_alias = pdb_default_delete_alias;
2166 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2167 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2168 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2169 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2170 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2171 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2172 (*methods)->lookup_rids = pdb_default_lookup_rids;
2173 (*methods)->get_account_policy = pdb_default_get_account_policy;
2174 (*methods)->set_account_policy = pdb_default_set_account_policy;
2175 (*methods)->get_seq_num = pdb_default_get_seq_num;
2176 (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2177 (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2178 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2179 (*methods)->sid_to_id = pdb_default_sid_to_id;
2181 (*methods)->search_users = pdb_default_search_users;
2182 (*methods)->search_groups = pdb_default_search_groups;
2183 (*methods)->search_aliases = pdb_default_search_aliases;
2185 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2186 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2187 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2188 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2190 return NT_STATUS_OK;