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/>.
25 #include "../librpc/gen_ndr/samr.h"
28 #define DBGC_CLASS DBGC_PASSDB
32 static struct pdb_init_function_entry
*backends
= NULL
;
34 static void lazy_initialize_passdb(void)
36 static bool initialized
= False
;
44 static bool lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32 rid
,
46 enum lsa_SidType
*psid_name_use
,
47 union unid_t
*unix_id
);
49 NTSTATUS
smb_register_passdb(int version
, const char *name
, pdb_init_function init
)
51 struct pdb_init_function_entry
*entry
= backends
;
53 if(version
!= PASSDB_INTERFACE_VERSION
) {
54 DEBUG(0,("Can't register passdb backend!\n"
55 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
56 "while this version of samba uses version %d\n",
57 version
,PASSDB_INTERFACE_VERSION
));
58 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
62 return NT_STATUS_INVALID_PARAMETER
;
65 DEBUG(5,("Attempting to register passdb backend %s\n", name
));
67 /* Check for duplicates */
68 if (pdb_find_backend_entry(name
)) {
69 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name
));
70 return NT_STATUS_OBJECT_NAME_COLLISION
;
73 entry
= SMB_XMALLOC_P(struct pdb_init_function_entry
);
74 entry
->name
= smb_xstrdup(name
);
77 DLIST_ADD(backends
, entry
);
78 DEBUG(5,("Successfully added passdb backend '%s'\n", name
));
82 struct pdb_init_function_entry
*pdb_find_backend_entry(const char *name
)
84 struct pdb_init_function_entry
*entry
= backends
;
87 if (strcmp(entry
->name
, name
)==0) return entry
;
95 * The event context for the passdb backend. I know this is a bad hack and yet
96 * another static variable, but our pdb API is a global thing per
97 * definition. The first use for this is the LDAP idle function, more might be
100 * I don't feel too bad about this static variable, it replaces the
101 * smb_idle_event_list that used to exist in lib/module.c. -- VL
104 static struct event_context
*pdb_event_ctx
;
106 struct event_context
*pdb_get_event_context(void)
108 return pdb_event_ctx
;
111 /******************************************************************
112 Make a pdb_methods from scratch
113 *******************************************************************/
115 NTSTATUS
make_pdb_method_name(struct pdb_methods
**methods
, const char *selected
)
117 char *module_name
= smb_xstrdup(selected
);
118 char *module_location
= NULL
, *p
;
119 struct pdb_init_function_entry
*entry
;
120 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
122 lazy_initialize_passdb();
124 p
= strchr(module_name
, ':');
128 module_location
= p
+1;
129 trim_char(module_location
, ' ', ' ');
132 trim_char(module_name
, ' ', ' ');
135 DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected
, module_name
));
137 entry
= pdb_find_backend_entry(module_name
);
139 /* Try to find a module that contains this module */
141 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
142 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name
)) && !(entry
= pdb_find_backend_entry(module_name
))) {
143 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name
));
144 SAFE_FREE(module_name
);
145 return NT_STATUS_UNSUCCESSFUL
;
149 /* No such backend found */
151 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name
));
152 SAFE_FREE(module_name
);
153 return NT_STATUS_INVALID_PARAMETER
;
156 DEBUG(5,("Found pdb backend %s\n", module_name
));
158 if ( !NT_STATUS_IS_OK( nt_status
= entry
->init(methods
, module_location
) ) ) {
159 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
160 selected
, nt_errstr(nt_status
)));
161 SAFE_FREE(module_name
);
165 SAFE_FREE(module_name
);
167 DEBUG(5,("pdb backend %s has a valid init\n", selected
));
172 /******************************************************************
173 Return an already initialized pdb_methods structure
174 *******************************************************************/
176 static struct pdb_methods
*pdb_get_methods_reload( bool reload
)
178 static struct pdb_methods
*pdb
= NULL
;
180 if ( pdb
&& reload
) {
181 pdb
->free_private_data( &(pdb
->private_data
) );
182 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
184 if (asprintf(&msg
, "pdb_get_methods_reload: "
185 "failed to get pdb methods for backend %s\n",
186 lp_passdb_backend()) > 0) {
189 smb_panic("pdb_get_methods_reload");
195 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
197 if (asprintf(&msg
, "pdb_get_methods_reload: "
198 "failed to get pdb methods for backend %s\n",
199 lp_passdb_backend()) > 0) {
202 smb_panic("pdb_get_methods_reload");
210 static struct pdb_methods
*pdb_get_methods(void)
212 return pdb_get_methods_reload(False
);
215 struct pdb_domain_info
*pdb_get_domain_info(TALLOC_CTX
*mem_ctx
)
217 struct pdb_methods
*pdb
= pdb_get_methods();
218 return pdb
->get_domain_info(pdb
, mem_ctx
);
221 bool pdb_getsampwnam(struct samu
*sam_acct
, const char *username
)
223 struct pdb_methods
*pdb
= pdb_get_methods();
224 struct samu
*for_cache
;
225 const struct dom_sid
*user_sid
;
227 if (!NT_STATUS_IS_OK(pdb
->getsampwnam(pdb
, sam_acct
, username
))) {
231 for_cache
= samu_new(NULL
);
232 if (for_cache
== NULL
) {
236 if (!pdb_copy_sam_account(for_cache
, sam_acct
)) {
237 TALLOC_FREE(for_cache
);
241 user_sid
= pdb_get_user_sid(for_cache
);
243 memcache_add_talloc(NULL
, PDB_GETPWSID_CACHE
,
244 data_blob_const(user_sid
, sizeof(*user_sid
)),
250 /**********************************************************************
251 **********************************************************************/
253 static bool guest_user_info( struct samu
*user
)
257 const char *guestname
= lp_guestaccount();
259 if ( !(pwd
= getpwnam_alloc(talloc_autofree_context(), guestname
) ) ) {
260 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
265 result
= samu_set_unix(user
, pwd
);
269 return NT_STATUS_IS_OK( result
);
272 /**********************************************************************
273 **********************************************************************/
275 bool pdb_getsampwsid(struct samu
*sam_acct
, const struct dom_sid
*sid
)
277 struct pdb_methods
*pdb
= pdb_get_methods();
281 /* hard code the Guest RID of 501 */
283 if ( !sid_peek_check_rid( get_global_sam_sid(), sid
, &rid
) )
286 if ( rid
== DOMAIN_RID_GUEST
) {
287 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
288 return guest_user_info( sam_acct
);
291 /* check the cache first */
293 cache_data
= memcache_lookup_talloc(
294 NULL
, PDB_GETPWSID_CACHE
, data_blob_const(sid
, sizeof(*sid
)));
296 if (cache_data
!= NULL
) {
297 struct samu
*cache_copy
= talloc_get_type_abort(
298 cache_data
, struct samu
);
300 return pdb_copy_sam_account(sam_acct
, cache_copy
);
303 return NT_STATUS_IS_OK(pdb
->getsampwsid(pdb
, sam_acct
, sid
));
306 static NTSTATUS
pdb_default_create_user(struct pdb_methods
*methods
,
307 TALLOC_CTX
*tmp_ctx
, const char *name
,
308 uint32_t acb_info
, uint32_t *rid
)
310 struct samu
*sam_pass
;
314 if ((sam_pass
= samu_new(tmp_ctx
)) == NULL
) {
315 return NT_STATUS_NO_MEMORY
;
318 if ( !(pwd
= Get_Pwnam_alloc(tmp_ctx
, name
)) ) {
319 char *add_script
= NULL
;
323 if ((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] != '$') {
324 add_script
= talloc_strdup(tmp_ctx
,
325 lp_adduser_script());
327 add_script
= talloc_strdup(tmp_ctx
,
328 lp_addmachine_script());
331 if (!add_script
|| add_script
[0] == '\0') {
332 DEBUG(3, ("Could not find user %s and no add script "
334 return NT_STATUS_NO_SUCH_USER
;
337 /* lowercase the username before creating the Unix account for
338 compatibility with previous Samba releases */
339 fstrcpy( name2
, name
);
341 add_script
= talloc_all_string_sub(tmp_ctx
,
346 return NT_STATUS_NO_MEMORY
;
348 add_ret
= smbrun(add_script
,NULL
);
349 DEBUG(add_ret
? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
350 add_script
, add_ret
));
352 smb_nscd_flush_user_cache();
357 pwd
= Get_Pwnam_alloc(tmp_ctx
, name
);
360 /* we have a valid SID coming out of this call */
362 status
= samu_alloc_rid_unix( sam_pass
, pwd
);
366 if (!NT_STATUS_IS_OK(status
)) {
367 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status
)));
371 if (!sid_peek_check_rid(get_global_sam_sid(),
372 pdb_get_user_sid(sam_pass
), rid
)) {
373 DEBUG(0, ("Could not get RID of fresh user\n"));
374 return NT_STATUS_INTERNAL_ERROR
;
377 /* Use the username case specified in the original request */
379 pdb_set_username( sam_pass
, name
, PDB_SET
);
381 /* Disable the account on creation, it does not have a reasonable password yet. */
383 acb_info
|= ACB_DISABLED
;
385 pdb_set_acct_ctrl(sam_pass
, acb_info
, PDB_CHANGED
);
387 status
= pdb_add_sam_account(sam_pass
);
389 TALLOC_FREE(sam_pass
);
394 NTSTATUS
pdb_create_user(TALLOC_CTX
*mem_ctx
, const char *name
, uint32_t flags
,
397 struct pdb_methods
*pdb
= pdb_get_methods();
398 return pdb
->create_user(pdb
, mem_ctx
, name
, flags
, rid
);
401 /****************************************************************************
402 Delete a UNIX user on demand.
403 ****************************************************************************/
405 static int smb_delete_user(const char *unix_user
)
407 char *del_script
= NULL
;
412 if ( strequal( unix_user
, "root" ) ) {
413 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
417 del_script
= talloc_strdup(talloc_tos(), lp_deluser_script());
418 if (!del_script
|| !*del_script
) {
421 del_script
= talloc_all_string_sub(talloc_tos(),
428 ret
= smbrun(del_script
,NULL
);
431 smb_nscd_flush_user_cache();
433 DEBUG(ret
? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script
,ret
));
438 static NTSTATUS
pdb_default_delete_user(struct pdb_methods
*methods
,
440 struct samu
*sam_acct
)
445 status
= pdb_delete_sam_account(sam_acct
);
446 if (!NT_STATUS_IS_OK(status
)) {
451 * Now delete the unix side ....
452 * note: we don't check if the delete really happened as the script is
453 * not necessary present and maybe the sysadmin doesn't want to delete
457 /* always lower case the username before handing it off to
460 fstrcpy( username
, pdb_get_username(sam_acct
) );
461 strlower_m( username
);
463 smb_delete_user( username
);
468 NTSTATUS
pdb_delete_user(TALLOC_CTX
*mem_ctx
, struct samu
*sam_acct
)
470 struct pdb_methods
*pdb
= pdb_get_methods();
473 /* sanity check to make sure we don't delete root */
475 if ( !sid_to_uid( pdb_get_user_sid(sam_acct
), &uid
) ) {
476 return NT_STATUS_NO_SUCH_USER
;
480 return NT_STATUS_ACCESS_DENIED
;
483 return pdb
->delete_user(pdb
, mem_ctx
, sam_acct
);
486 NTSTATUS
pdb_add_sam_account(struct samu
*sam_acct
)
488 struct pdb_methods
*pdb
= pdb_get_methods();
489 return pdb
->add_sam_account(pdb
, sam_acct
);
492 NTSTATUS
pdb_update_sam_account(struct samu
*sam_acct
)
494 struct pdb_methods
*pdb
= pdb_get_methods();
496 memcache_flush(NULL
, PDB_GETPWSID_CACHE
);
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 memcache_flush(NULL
, PDB_GETPWSID_CACHE
);
507 return pdb
->delete_sam_account(pdb
, sam_acct
);
510 NTSTATUS
pdb_rename_sam_account(struct samu
*oldname
, const char *newname
)
512 struct pdb_methods
*pdb
= pdb_get_methods();
516 memcache_flush(NULL
, PDB_GETPWSID_CACHE
);
518 /* sanity check to make sure we don't rename root */
520 if ( !sid_to_uid( pdb_get_user_sid(oldname
), &uid
) ) {
521 return NT_STATUS_NO_SUCH_USER
;
525 return NT_STATUS_ACCESS_DENIED
;
528 status
= pdb
->rename_sam_account(pdb
, oldname
, newname
);
530 /* always flush the cache here just to be safe */
536 NTSTATUS
pdb_update_login_attempts(struct samu
*sam_acct
, bool success
)
538 struct pdb_methods
*pdb
= pdb_get_methods();
539 return pdb
->update_login_attempts(pdb
, sam_acct
, success
);
542 bool pdb_getgrsid(GROUP_MAP
*map
, struct dom_sid sid
)
544 struct pdb_methods
*pdb
= pdb_get_methods();
545 return NT_STATUS_IS_OK(pdb
->getgrsid(pdb
, map
, sid
));
548 bool pdb_getgrgid(GROUP_MAP
*map
, gid_t gid
)
550 struct pdb_methods
*pdb
= pdb_get_methods();
551 return NT_STATUS_IS_OK(pdb
->getgrgid(pdb
, map
, gid
));
554 bool pdb_getgrnam(GROUP_MAP
*map
, const char *name
)
556 struct pdb_methods
*pdb
= pdb_get_methods();
557 return NT_STATUS_IS_OK(pdb
->getgrnam(pdb
, map
, name
));
560 static NTSTATUS
pdb_default_create_dom_group(struct pdb_methods
*methods
,
565 struct dom_sid group_sid
;
569 grp
= getgrnam(name
);
574 if (smb_create_group(name
, &gid
) != 0) {
575 return NT_STATUS_ACCESS_DENIED
;
582 return NT_STATUS_ACCESS_DENIED
;
585 if (pdb_capabilities() & PDB_CAP_STORE_RIDS
) {
586 if (!pdb_new_rid(rid
)) {
587 return NT_STATUS_ACCESS_DENIED
;
590 *rid
= algorithmic_pdb_gid_to_group_rid( grp
->gr_gid
);
593 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
595 return add_initial_entry(grp
->gr_gid
, sid_to_fstring(tmp
, &group_sid
),
596 SID_NAME_DOM_GRP
, name
, NULL
);
599 NTSTATUS
pdb_create_dom_group(TALLOC_CTX
*mem_ctx
, const char *name
,
602 struct pdb_methods
*pdb
= pdb_get_methods();
603 return pdb
->create_dom_group(pdb
, mem_ctx
, name
, rid
);
606 static NTSTATUS
pdb_default_delete_dom_group(struct pdb_methods
*methods
,
610 struct dom_sid group_sid
;
614 const char *grp_name
;
617 map
.gid
= (gid_t
) -1;
619 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
621 if (!get_domain_group_from_sid(group_sid
, &map
)) {
622 DEBUG(10, ("Could not find group for rid %d\n", rid
));
623 return NT_STATUS_NO_SUCH_GROUP
;
626 /* We need the group name for the smb_delete_group later on */
628 if (map
.gid
== (gid_t
)-1) {
629 return NT_STATUS_NO_SUCH_GROUP
;
632 grp
= getgrgid(map
.gid
);
634 return NT_STATUS_NO_SUCH_GROUP
;
637 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
639 grp_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
640 if (grp_name
== NULL
) {
641 return NT_STATUS_NO_MEMORY
;
644 status
= pdb_delete_group_mapping_entry(group_sid
);
646 if (!NT_STATUS_IS_OK(status
)) {
650 /* Don't check the result of smb_delete_group */
652 smb_delete_group(grp_name
);
657 NTSTATUS
pdb_delete_dom_group(TALLOC_CTX
*mem_ctx
, uint32_t rid
)
659 struct pdb_methods
*pdb
= pdb_get_methods();
660 return pdb
->delete_dom_group(pdb
, mem_ctx
, rid
);
663 NTSTATUS
pdb_add_group_mapping_entry(GROUP_MAP
*map
)
665 struct pdb_methods
*pdb
= pdb_get_methods();
666 return pdb
->add_group_mapping_entry(pdb
, map
);
669 NTSTATUS
pdb_update_group_mapping_entry(GROUP_MAP
*map
)
671 struct pdb_methods
*pdb
= pdb_get_methods();
672 return pdb
->update_group_mapping_entry(pdb
, map
);
675 NTSTATUS
pdb_delete_group_mapping_entry(struct dom_sid sid
)
677 struct pdb_methods
*pdb
= pdb_get_methods();
678 return pdb
->delete_group_mapping_entry(pdb
, sid
);
681 bool pdb_enum_group_mapping(const struct dom_sid
*sid
, enum lsa_SidType sid_name_use
, GROUP_MAP
**pp_rmap
,
682 size_t *p_num_entries
, bool unix_only
)
684 struct pdb_methods
*pdb
= pdb_get_methods();
685 return NT_STATUS_IS_OK(pdb
-> enum_group_mapping(pdb
, sid
, sid_name_use
,
686 pp_rmap
, p_num_entries
, unix_only
));
689 NTSTATUS
pdb_enum_group_members(TALLOC_CTX
*mem_ctx
,
690 const struct dom_sid
*sid
,
691 uint32_t **pp_member_rids
,
692 size_t *p_num_members
)
694 struct pdb_methods
*pdb
= pdb_get_methods();
697 result
= pdb
->enum_group_members(pdb
, mem_ctx
,
698 sid
, pp_member_rids
, p_num_members
);
700 /* special check for rid 513 */
702 if ( !NT_STATUS_IS_OK( result
) ) {
705 sid_peek_rid( sid
, &rid
);
707 if ( rid
== DOMAIN_RID_USERS
) {
709 *pp_member_rids
= NULL
;
718 NTSTATUS
pdb_enum_group_memberships(TALLOC_CTX
*mem_ctx
, struct samu
*user
,
719 struct dom_sid
**pp_sids
, gid_t
**pp_gids
,
720 size_t *p_num_groups
)
722 struct pdb_methods
*pdb
= pdb_get_methods();
723 return pdb
->enum_group_memberships(
725 pp_sids
, pp_gids
, p_num_groups
);
728 static NTSTATUS
pdb_default_set_unix_primary_group(struct pdb_methods
*methods
,
730 struct samu
*sampass
)
735 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
) ||
736 (grp
= getgrgid(gid
)) == NULL
) {
737 return NT_STATUS_INVALID_PRIMARY_GROUP
;
740 if (smb_set_primary_group(grp
->gr_name
,
741 pdb_get_username(sampass
)) != 0) {
742 return NT_STATUS_ACCESS_DENIED
;
748 NTSTATUS
pdb_set_unix_primary_group(TALLOC_CTX
*mem_ctx
, struct samu
*user
)
750 struct pdb_methods
*pdb
= pdb_get_methods();
751 return pdb
->set_unix_primary_group(pdb
, mem_ctx
, user
);
755 * Helper function to see whether a user is in a group. We can't use
756 * user_in_group_sid here because this creates dependencies only smbd can
760 static bool pdb_user_in_group(TALLOC_CTX
*mem_ctx
, struct samu
*account
,
761 const struct dom_sid
*group_sid
)
763 struct dom_sid
*sids
;
765 size_t i
, num_groups
;
767 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx
, account
,
773 for (i
=0; i
<num_groups
; i
++) {
774 if (sid_equal(group_sid
, &sids
[i
])) {
781 static NTSTATUS
pdb_default_add_groupmem(struct pdb_methods
*methods
,
786 struct dom_sid group_sid
, member_sid
;
787 struct samu
*account
= NULL
;
791 const char *group_name
;
795 map
.gid
= (gid_t
) -1;
797 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
798 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
800 if (!get_domain_group_from_sid(group_sid
, &map
) ||
801 (map
.gid
== (gid_t
)-1) ||
802 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
803 return NT_STATUS_NO_SUCH_GROUP
;
806 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
807 if (group_name
== NULL
) {
808 return NT_STATUS_NO_MEMORY
;
811 if ( !(account
= samu_new( NULL
)) ) {
812 return NT_STATUS_NO_MEMORY
;
815 if (!pdb_getsampwsid(account
, &member_sid
) ||
816 !sid_to_uid(&member_sid
, &uid
) ||
817 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
818 return NT_STATUS_NO_SUCH_USER
;
821 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
822 return NT_STATUS_MEMBER_IN_GROUP
;
826 * ok, the group exist, the user exist, the user is not in the group,
827 * we can (finally) add it to the group !
830 smb_add_user_group(group_name
, pwd
->pw_name
);
832 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
833 return NT_STATUS_ACCESS_DENIED
;
839 NTSTATUS
pdb_add_groupmem(TALLOC_CTX
*mem_ctx
, uint32_t group_rid
,
842 struct pdb_methods
*pdb
= pdb_get_methods();
843 return pdb
->add_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
846 static NTSTATUS
pdb_default_del_groupmem(struct pdb_methods
*methods
,
851 struct dom_sid group_sid
, member_sid
;
852 struct samu
*account
= NULL
;
856 const char *group_name
;
859 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
860 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
862 if (!get_domain_group_from_sid(group_sid
, &map
) ||
863 (map
.gid
== (gid_t
)-1) ||
864 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
865 return NT_STATUS_NO_SUCH_GROUP
;
868 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
869 if (group_name
== NULL
) {
870 return NT_STATUS_NO_MEMORY
;
873 if ( !(account
= samu_new( NULL
)) ) {
874 return NT_STATUS_NO_MEMORY
;
877 if (!pdb_getsampwsid(account
, &member_sid
) ||
878 !sid_to_uid(&member_sid
, &uid
) ||
879 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
880 return NT_STATUS_NO_SUCH_USER
;
883 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
884 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
888 * ok, the group exist, the user exist, the user is in the group,
889 * we can (finally) delete it from the group!
892 smb_delete_user_group(group_name
, pwd
->pw_name
);
894 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
895 return NT_STATUS_ACCESS_DENIED
;
901 NTSTATUS
pdb_del_groupmem(TALLOC_CTX
*mem_ctx
, uint32_t group_rid
,
904 struct pdb_methods
*pdb
= pdb_get_methods();
905 return pdb
->del_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
908 NTSTATUS
pdb_create_alias(const char *name
, uint32_t *rid
)
910 struct pdb_methods
*pdb
= pdb_get_methods();
911 return pdb
->create_alias(pdb
, name
, rid
);
914 NTSTATUS
pdb_delete_alias(const struct dom_sid
*sid
)
916 struct pdb_methods
*pdb
= pdb_get_methods();
917 return pdb
->delete_alias(pdb
, sid
);
920 NTSTATUS
pdb_get_aliasinfo(const struct dom_sid
*sid
, struct acct_info
*info
)
922 struct pdb_methods
*pdb
= pdb_get_methods();
923 return pdb
->get_aliasinfo(pdb
, sid
, info
);
926 NTSTATUS
pdb_set_aliasinfo(const struct dom_sid
*sid
, struct acct_info
*info
)
928 struct pdb_methods
*pdb
= pdb_get_methods();
929 return pdb
->set_aliasinfo(pdb
, sid
, info
);
932 NTSTATUS
pdb_add_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
934 struct pdb_methods
*pdb
= pdb_get_methods();
935 return pdb
->add_aliasmem(pdb
, alias
, member
);
938 NTSTATUS
pdb_del_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
940 struct pdb_methods
*pdb
= pdb_get_methods();
941 return pdb
->del_aliasmem(pdb
, alias
, member
);
944 NTSTATUS
pdb_enum_aliasmem(const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
945 struct dom_sid
**pp_members
, size_t *p_num_members
)
947 struct pdb_methods
*pdb
= pdb_get_methods();
948 return pdb
->enum_aliasmem(pdb
, alias
, mem_ctx
, pp_members
,
952 NTSTATUS
pdb_enum_alias_memberships(TALLOC_CTX
*mem_ctx
,
953 const struct dom_sid
*domain_sid
,
954 const struct dom_sid
*members
, size_t num_members
,
955 uint32_t **pp_alias_rids
,
956 size_t *p_num_alias_rids
)
958 struct pdb_methods
*pdb
= pdb_get_methods();
959 return pdb
->enum_alias_memberships(pdb
, mem_ctx
,
961 members
, num_members
,
966 NTSTATUS
pdb_lookup_rids(const struct dom_sid
*domain_sid
,
970 enum lsa_SidType
*attrs
)
972 struct pdb_methods
*pdb
= pdb_get_methods();
973 return pdb
->lookup_rids(pdb
, domain_sid
, num_rids
, rids
, names
, attrs
);
977 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
979 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
980 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
981 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
982 * down to are pdb_getsampwnam and pdb_getgrnam instead of
984 * But in principle, it the call belongs to the API and might get
985 * used in this context some day.
988 NTSTATUS
pdb_lookup_names(const struct dom_sid
*domain_sid
,
992 enum lsa_SidType
*attrs
)
994 struct pdb_methods
*pdb
= pdb_get_methods();
995 return pdb
->lookup_names(pdb
, domain_sid
, num_names
, names
, rids
, attrs
);
999 bool pdb_get_account_policy(enum pdb_policy_type type
, uint32_t *value
)
1001 struct pdb_methods
*pdb
= pdb_get_methods();
1005 status
= pdb
->get_account_policy(pdb
, type
, value
);
1008 return NT_STATUS_IS_OK(status
);
1011 bool pdb_set_account_policy(enum pdb_policy_type type
, uint32_t value
)
1013 struct pdb_methods
*pdb
= pdb_get_methods();
1017 status
= pdb
->set_account_policy(pdb
, type
, value
);
1020 return NT_STATUS_IS_OK(status
);
1023 bool pdb_get_seq_num(time_t *seq_num
)
1025 struct pdb_methods
*pdb
= pdb_get_methods();
1026 return NT_STATUS_IS_OK(pdb
->get_seq_num(pdb
, seq_num
));
1029 bool pdb_uid_to_sid(uid_t uid
, struct dom_sid
*sid
)
1031 struct pdb_methods
*pdb
= pdb_get_methods();
1032 return pdb
->uid_to_sid(pdb
, uid
, sid
);
1035 bool pdb_gid_to_sid(gid_t gid
, struct dom_sid
*sid
)
1037 struct pdb_methods
*pdb
= pdb_get_methods();
1038 return pdb
->gid_to_sid(pdb
, gid
, sid
);
1041 bool pdb_sid_to_id(const struct dom_sid
*sid
, union unid_t
*id
,
1042 enum lsa_SidType
*type
)
1044 struct pdb_methods
*pdb
= pdb_get_methods();
1045 return pdb
->sid_to_id(pdb
, sid
, id
, type
);
1048 uint32_t pdb_capabilities(void)
1050 struct pdb_methods
*pdb
= pdb_get_methods();
1051 return pdb
->capabilities(pdb
);
1054 /********************************************************************
1055 Allocate a new RID from the passdb backend. Verify that it is free
1056 by calling lookup_global_sam_rid() to verify that the RID is not
1057 in use. This handles servers that have existing users or groups
1058 with add RIDs (assigned from previous algorithmic mappings)
1059 ********************************************************************/
1061 bool pdb_new_rid(uint32_t *rid
)
1063 struct pdb_methods
*pdb
= pdb_get_methods();
1064 const char *name
= NULL
;
1065 enum lsa_SidType type
;
1066 uint32_t allocated_rid
= 0;
1070 if ((pdb_capabilities() & PDB_CAP_STORE_RIDS
) == 0) {
1071 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1076 if (algorithmic_rid_base() != BASE_RID
) {
1077 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1078 "without algorithmic RIDs is chosen.\n"));
1079 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1080 "add', set the maximum used RID\n"));
1081 DEBUGADD(0, ("and remove the parameter\n"));
1085 if ( (ctx
= talloc_init("pdb_new_rid")) == NULL
) {
1086 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1090 /* Attempt to get an unused RID (max tires is 250...yes that it is
1091 and arbitrary number I pulkled out of my head). -- jerry */
1093 for ( i
=0; allocated_rid
==0 && i
<250; i
++ ) {
1096 if ( !pdb
->new_rid(pdb
, &allocated_rid
) ) {
1100 /* validate that the RID is not in use */
1102 if ( lookup_global_sam_rid( ctx
, allocated_rid
, &name
, &type
, NULL
) ) {
1109 if ( allocated_rid
== 0 ) {
1110 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1114 *rid
= allocated_rid
;
1119 /***************************************************************
1120 Initialize the static context (at smbd startup etc).
1122 If uninitialised, context will auto-init on first use.
1123 ***************************************************************/
1125 bool initialize_password_db(bool reload
, struct event_context
*event_ctx
)
1127 pdb_event_ctx
= event_ctx
;
1128 return (pdb_get_methods_reload(reload
) != NULL
);
1132 /***************************************************************************
1133 Default implementations of some functions.
1134 ****************************************************************************/
1136 static NTSTATUS
pdb_default_getsampwnam (struct pdb_methods
*methods
, struct samu
*user
, const char *sname
)
1138 return NT_STATUS_NO_SUCH_USER
;
1141 static NTSTATUS
pdb_default_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const struct dom_sid
*sid
)
1143 return NT_STATUS_NO_SUCH_USER
;
1146 static NTSTATUS
pdb_default_add_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1148 return NT_STATUS_NOT_IMPLEMENTED
;
1151 static NTSTATUS
pdb_default_update_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1153 return NT_STATUS_NOT_IMPLEMENTED
;
1156 static NTSTATUS
pdb_default_delete_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
)
1158 return NT_STATUS_NOT_IMPLEMENTED
;
1161 static NTSTATUS
pdb_default_rename_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
, const char *newname
)
1163 return NT_STATUS_NOT_IMPLEMENTED
;
1166 static NTSTATUS
pdb_default_update_login_attempts (struct pdb_methods
*methods
, struct samu
*newpwd
, bool success
)
1168 /* Only the pdb_nds backend implements this, by
1169 * default just return ok. */
1170 return NT_STATUS_OK
;
1173 static NTSTATUS
pdb_default_get_account_policy(struct pdb_methods
*methods
, enum pdb_policy_type type
, uint32_t *value
)
1175 return account_policy_get(type
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1178 static NTSTATUS
pdb_default_set_account_policy(struct pdb_methods
*methods
, enum pdb_policy_type type
, uint32_t value
)
1180 return account_policy_set(type
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1183 static NTSTATUS
pdb_default_get_seq_num(struct pdb_methods
*methods
, time_t *seq_num
)
1185 *seq_num
= time(NULL
);
1186 return NT_STATUS_OK
;
1189 static bool pdb_default_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
1190 struct dom_sid
*sid
)
1192 struct samu
*sampw
= NULL
;
1193 struct passwd
*unix_pw
;
1196 unix_pw
= sys_getpwuid( uid
);
1199 DEBUG(4,("pdb_default_uid_to_sid: host has no idea of uid "
1200 "%lu\n", (unsigned long)uid
));
1204 if ( !(sampw
= samu_new( NULL
)) ) {
1205 DEBUG(0,("pdb_default_uid_to_sid: samu_new() failed!\n"));
1210 ret
= NT_STATUS_IS_OK(
1211 methods
->getsampwnam(methods
, sampw
, unix_pw
->pw_name
));
1215 DEBUG(5, ("pdb_default_uid_to_sid: Did not find user "
1216 "%s (%u)\n", unix_pw
->pw_name
, (unsigned int)uid
));
1221 sid_copy(sid
, pdb_get_user_sid(sampw
));
1228 static bool pdb_default_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
1229 struct dom_sid
*sid
)
1233 if (!NT_STATUS_IS_OK(methods
->getgrgid(methods
, &map
, gid
))) {
1237 sid_copy(sid
, &map
.sid
);
1241 static bool pdb_default_sid_to_id(struct pdb_methods
*methods
,
1242 const struct dom_sid
*sid
,
1243 union unid_t
*id
, enum lsa_SidType
*type
)
1245 TALLOC_CTX
*mem_ctx
;
1250 mem_ctx
= talloc_new(NULL
);
1252 if (mem_ctx
== NULL
) {
1253 DEBUG(0, ("talloc_new failed\n"));
1257 if (sid_peek_check_rid(get_global_sam_sid(), sid
, &rid
)) {
1258 /* Here we might have users as well as groups and aliases */
1259 ret
= lookup_global_sam_rid(mem_ctx
, rid
, &name
, type
, id
);
1263 /* check for "Unix User" */
1265 if ( sid_peek_check_rid(&global_sid_Unix_Users
, sid
, &rid
) ) {
1267 *type
= SID_NAME_USER
;
1272 /* check for "Unix Group" */
1274 if ( sid_peek_check_rid(&global_sid_Unix_Groups
, sid
, &rid
) ) {
1276 *type
= SID_NAME_ALIAS
;
1283 if (sid_check_is_in_builtin(sid
) ||
1284 sid_check_is_in_wellknown_domain(sid
)) {
1285 /* Here we only have aliases */
1287 if (!NT_STATUS_IS_OK(methods
->getgrsid(methods
, &map
, *sid
))) {
1288 DEBUG(10, ("Could not find map for sid %s\n",
1289 sid_string_dbg(sid
)));
1292 if ((map
.sid_name_use
!= SID_NAME_ALIAS
) &&
1293 (map
.sid_name_use
!= SID_NAME_WKN_GRP
)) {
1294 DEBUG(10, ("Map for sid %s is a %s, expected an "
1295 "alias\n", sid_string_dbg(sid
),
1296 sid_type_lookup(map
.sid_name_use
)));
1301 *type
= SID_NAME_ALIAS
;
1306 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1307 sid_string_dbg(sid
)));
1311 TALLOC_FREE(mem_ctx
);
1315 static bool get_memberuids(TALLOC_CTX
*mem_ctx
, gid_t gid
, uid_t
**pp_uids
, size_t *p_num
)
1326 /* We only look at our own sam, so don't care about imported stuff */
1327 winbind_env
= winbind_env_set();
1328 (void)winbind_off();
1330 if ((grp
= getgrgid(gid
)) == NULL
) {
1331 /* allow winbindd lookups, but only if they weren't already disabled */
1335 /* Primary group members */
1337 while ((pwd
= getpwent()) != NULL
) {
1338 if (pwd
->pw_gid
== gid
) {
1339 if (!add_uid_to_array_unique(mem_ctx
, pwd
->pw_uid
,
1347 /* Secondary group members */
1348 for (gr
= grp
->gr_mem
; (*gr
!= NULL
) && ((*gr
)[0] != '\0'); gr
+= 1) {
1349 struct passwd
*pw
= getpwnam(*gr
);
1353 if (!add_uid_to_array_unique(mem_ctx
, pw
->pw_uid
, pp_uids
, p_num
)) {
1362 /* allow winbindd lookups, but only if they weren't already disabled */
1370 static NTSTATUS
pdb_default_enum_group_members(struct pdb_methods
*methods
,
1371 TALLOC_CTX
*mem_ctx
,
1372 const struct dom_sid
*group
,
1373 uint32_t **pp_member_rids
,
1374 size_t *p_num_members
)
1380 *pp_member_rids
= NULL
;
1383 if (!sid_to_gid(group
, &gid
))
1384 return NT_STATUS_NO_SUCH_GROUP
;
1386 if(!get_memberuids(mem_ctx
, gid
, &uids
, &num_uids
))
1387 return NT_STATUS_NO_SUCH_GROUP
;
1390 return NT_STATUS_OK
;
1392 *pp_member_rids
= TALLOC_ZERO_ARRAY(mem_ctx
, uint32_t, num_uids
);
1394 for (i
=0; i
<num_uids
; i
++) {
1397 uid_to_sid(&sid
, uids
[i
]);
1399 if (!sid_check_is_in_our_domain(&sid
)) {
1400 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1401 "in our domain\n"));
1405 sid_peek_rid(&sid
, &(*pp_member_rids
)[*p_num_members
]);
1406 *p_num_members
+= 1;
1409 return NT_STATUS_OK
;
1412 static NTSTATUS
pdb_default_enum_group_memberships(struct pdb_methods
*methods
,
1413 TALLOC_CTX
*mem_ctx
,
1415 struct dom_sid
**pp_sids
,
1417 size_t *p_num_groups
)
1422 const char *username
= pdb_get_username(user
);
1425 /* Ignore the primary group SID. Honor the real Unix primary group.
1426 The primary group SID is only of real use to Windows clients */
1428 if ( !(pw
= getpwnam_alloc(mem_ctx
, username
)) ) {
1429 return NT_STATUS_NO_SUCH_USER
;
1436 if (!getgroups_unix_user(mem_ctx
, username
, gid
, pp_gids
, p_num_groups
)) {
1437 return NT_STATUS_NO_SUCH_USER
;
1440 if (*p_num_groups
== 0) {
1441 smb_panic("primary group missing");
1444 *pp_sids
= TALLOC_ARRAY(mem_ctx
, struct dom_sid
, *p_num_groups
);
1446 if (*pp_sids
== NULL
) {
1447 TALLOC_FREE(*pp_gids
);
1448 return NT_STATUS_NO_MEMORY
;
1451 for (i
=0; i
<*p_num_groups
; i
++) {
1452 gid_to_sid(&(*pp_sids
)[i
], (*pp_gids
)[i
]);
1455 return NT_STATUS_OK
;
1458 /*******************************************************************
1459 Look up a rid in the SAM we're responsible for (i.e. passdb)
1460 ********************************************************************/
1462 static bool lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32_t rid
,
1464 enum lsa_SidType
*psid_name_use
,
1465 union unid_t
*unix_id
)
1467 struct samu
*sam_account
= NULL
;
1472 *psid_name_use
= SID_NAME_UNKNOWN
;
1474 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1475 (unsigned int)rid
));
1477 sid_compose(&sid
, get_global_sam_sid(), rid
);
1479 /* see if the passdb can help us with the name of the user */
1481 if ( !(sam_account
= samu_new( NULL
)) ) {
1485 /* BEING ROOT BLOCK */
1487 if (pdb_getsampwsid(sam_account
, &sid
)) {
1490 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1491 *name
= talloc_strdup(mem_ctx
, pdb_get_username(sam_account
));
1493 TALLOC_FREE(sam_account
);
1497 *psid_name_use
= SID_NAME_USER
;
1499 TALLOC_FREE(sam_account
);
1501 if (unix_id
== NULL
) {
1505 pw
= Get_Pwnam_alloc(talloc_tos(), *name
);
1509 unix_id
->uid
= pw
->pw_uid
;
1513 TALLOC_FREE(sam_account
);
1515 ret
= pdb_getgrsid(&map
, sid
);
1517 /* END BECOME_ROOT BLOCK */
1519 /* do not resolve SIDs to a name unless there is a valid
1520 gid associated with it */
1522 if ( ret
&& (map
.gid
!= (gid_t
)-1) ) {
1523 *name
= talloc_strdup(mem_ctx
, map
.nt_name
);
1524 *psid_name_use
= map
.sid_name_use
;
1527 unix_id
->gid
= map
.gid
;
1533 /* Windows will always map RID 513 to something. On a non-domain
1534 controller, this gets mapped to SERVER\None. */
1537 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1541 if ( rid
== DOMAIN_RID_USERS
) {
1542 *name
= talloc_strdup(mem_ctx
, "None" );
1543 *psid_name_use
= SID_NAME_DOM_GRP
;
1551 static NTSTATUS
pdb_default_lookup_rids(struct pdb_methods
*methods
,
1552 const struct dom_sid
*domain_sid
,
1556 enum lsa_SidType
*attrs
)
1560 bool have_mapped
= False
;
1561 bool have_unmapped
= False
;
1563 if (sid_check_is_builtin(domain_sid
)) {
1565 for (i
=0; i
<num_rids
; i
++) {
1568 if (lookup_builtin_rid(names
, rids
[i
], &name
)) {
1569 attrs
[i
] = SID_NAME_ALIAS
;
1571 DEBUG(5,("lookup_rids: %s:%d\n",
1572 names
[i
], attrs
[i
]));
1575 have_unmapped
= True
;
1576 attrs
[i
] = SID_NAME_UNKNOWN
;
1582 /* Should not happen, but better check once too many */
1583 if (!sid_check_is_domain(domain_sid
)) {
1584 return NT_STATUS_INVALID_HANDLE
;
1587 for (i
= 0; i
< num_rids
; i
++) {
1590 if (lookup_global_sam_rid(names
, rids
[i
], &name
, &attrs
[i
],
1593 return NT_STATUS_NO_MEMORY
;
1596 DEBUG(5,("lookup_rids: %s:%d\n", names
[i
], attrs
[i
]));
1599 have_unmapped
= True
;
1600 attrs
[i
] = SID_NAME_UNKNOWN
;
1606 result
= NT_STATUS_NONE_MAPPED
;
1609 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1615 static NTSTATUS
pdb_default_lookup_names(struct pdb_methods
*methods
,
1616 const struct dom_sid
*domain_sid
,
1620 enum lsa_SidType
*attrs
)
1624 bool have_mapped
= False
;
1625 bool have_unmapped
= False
;
1627 if (sid_check_is_builtin(domain_sid
)) {
1629 for (i
=0; i
<num_names
; i
++) {
1632 if (lookup_builtin_name(names
[i
], &rid
)) {
1633 attrs
[i
] = SID_NAME_ALIAS
;
1635 DEBUG(5,("lookup_rids: %s:%d\n",
1636 names
[i
], attrs
[i
]));
1639 have_unmapped
= True
;
1640 attrs
[i
] = SID_NAME_UNKNOWN
;
1646 /* Should not happen, but better check once too many */
1647 if (!sid_check_is_domain(domain_sid
)) {
1648 return NT_STATUS_INVALID_HANDLE
;
1651 for (i
= 0; i
< num_names
; i
++) {
1652 if (lookup_global_sam_name(names
[i
], 0, &rids
[i
], &attrs
[i
])) {
1653 DEBUG(5,("lookup_names: %s-> %d:%d\n", names
[i
],
1654 rids
[i
], attrs
[i
]));
1657 have_unmapped
= True
;
1658 attrs
[i
] = SID_NAME_UNKNOWN
;
1664 result
= NT_STATUS_NONE_MAPPED
;
1667 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1673 static int pdb_search_destructor(struct pdb_search
*search
)
1675 if ((!search
->search_ended
) && (search
->search_end
!= NULL
)) {
1676 search
->search_end(search
);
1681 struct pdb_search
*pdb_search_init(TALLOC_CTX
*mem_ctx
,
1682 enum pdb_search_type type
)
1684 struct pdb_search
*result
;
1686 result
= talloc(mem_ctx
, struct pdb_search
);
1687 if (result
== NULL
) {
1688 DEBUG(0, ("talloc failed\n"));
1692 result
->type
= type
;
1693 result
->cache
= NULL
;
1694 result
->num_entries
= 0;
1695 result
->cache_size
= 0;
1696 result
->search_ended
= False
;
1697 result
->search_end
= NULL
;
1699 /* Segfault appropriately if not initialized */
1700 result
->next_entry
= NULL
;
1701 result
->search_end
= NULL
;
1703 talloc_set_destructor(result
, pdb_search_destructor
);
1708 static void fill_displayentry(TALLOC_CTX
*mem_ctx
, uint32_t rid
,
1709 uint16_t acct_flags
,
1710 const char *account_name
,
1711 const char *fullname
,
1712 const char *description
,
1713 struct samr_displayentry
*entry
)
1716 entry
->acct_flags
= acct_flags
;
1718 if (account_name
!= NULL
)
1719 entry
->account_name
= talloc_strdup(mem_ctx
, account_name
);
1721 entry
->account_name
= "";
1723 if (fullname
!= NULL
)
1724 entry
->fullname
= talloc_strdup(mem_ctx
, fullname
);
1726 entry
->fullname
= "";
1728 if (description
!= NULL
)
1729 entry
->description
= talloc_strdup(mem_ctx
, description
);
1731 entry
->description
= "";
1734 struct group_search
{
1736 size_t num_groups
, current_group
;
1739 static bool next_entry_groups(struct pdb_search
*s
,
1740 struct samr_displayentry
*entry
)
1742 struct group_search
*state
= (struct group_search
*)s
->private_data
;
1744 GROUP_MAP
*map
= &state
->groups
[state
->current_group
];
1746 if (state
->current_group
== state
->num_groups
)
1749 sid_peek_rid(&map
->sid
, &rid
);
1751 fill_displayentry(s
, rid
, 0, map
->nt_name
, NULL
, map
->comment
, entry
);
1753 state
->current_group
+= 1;
1757 static void search_end_groups(struct pdb_search
*search
)
1759 struct group_search
*state
=
1760 (struct group_search
*)search
->private_data
;
1761 SAFE_FREE(state
->groups
);
1764 static bool pdb_search_grouptype(struct pdb_search
*search
,
1765 const struct dom_sid
*sid
, enum lsa_SidType type
)
1767 struct group_search
*state
;
1769 state
= talloc(search
, struct group_search
);
1770 if (state
== NULL
) {
1771 DEBUG(0, ("talloc failed\n"));
1775 if (!pdb_enum_group_mapping(sid
, type
, &state
->groups
, &state
->num_groups
,
1777 DEBUG(0, ("Could not enum groups\n"));
1781 state
->current_group
= 0;
1782 search
->private_data
= state
;
1783 search
->next_entry
= next_entry_groups
;
1784 search
->search_end
= search_end_groups
;
1788 static bool pdb_default_search_groups(struct pdb_methods
*methods
,
1789 struct pdb_search
*search
)
1791 return pdb_search_grouptype(search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
1794 static bool pdb_default_search_aliases(struct pdb_methods
*methods
,
1795 struct pdb_search
*search
,
1796 const struct dom_sid
*sid
)
1799 return pdb_search_grouptype(search
, sid
, SID_NAME_ALIAS
);
1802 static struct samr_displayentry
*pdb_search_getentry(struct pdb_search
*search
,
1805 if (idx
< search
->num_entries
)
1806 return &search
->cache
[idx
];
1808 if (search
->search_ended
)
1811 while (idx
>= search
->num_entries
) {
1812 struct samr_displayentry entry
;
1814 if (!search
->next_entry(search
, &entry
)) {
1815 search
->search_end(search
);
1816 search
->search_ended
= True
;
1820 ADD_TO_LARGE_ARRAY(search
, struct samr_displayentry
,
1821 entry
, &search
->cache
, &search
->num_entries
,
1822 &search
->cache_size
);
1825 return (search
->num_entries
> idx
) ? &search
->cache
[idx
] : NULL
;
1828 struct pdb_search
*pdb_search_users(TALLOC_CTX
*mem_ctx
, uint32_t acct_flags
)
1830 struct pdb_methods
*pdb
= pdb_get_methods();
1831 struct pdb_search
*result
;
1833 result
= pdb_search_init(mem_ctx
, PDB_USER_SEARCH
);
1834 if (result
== NULL
) {
1838 if (!pdb
->search_users(pdb
, result
, acct_flags
)) {
1839 TALLOC_FREE(result
);
1845 struct pdb_search
*pdb_search_groups(TALLOC_CTX
*mem_ctx
)
1847 struct pdb_methods
*pdb
= pdb_get_methods();
1848 struct pdb_search
*result
;
1850 result
= pdb_search_init(mem_ctx
, PDB_GROUP_SEARCH
);
1851 if (result
== NULL
) {
1855 if (!pdb
->search_groups(pdb
, result
)) {
1856 TALLOC_FREE(result
);
1862 struct pdb_search
*pdb_search_aliases(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
)
1864 struct pdb_methods
*pdb
= pdb_get_methods();
1865 struct pdb_search
*result
;
1867 if (pdb
== NULL
) return NULL
;
1869 result
= pdb_search_init(mem_ctx
, PDB_ALIAS_SEARCH
);
1870 if (result
== NULL
) {
1874 if (!pdb
->search_aliases(pdb
, result
, sid
)) {
1875 TALLOC_FREE(result
);
1881 uint32_t pdb_search_entries(struct pdb_search
*search
,
1882 uint32_t start_idx
, uint32_t max_entries
,
1883 struct samr_displayentry
**result
)
1885 struct samr_displayentry
*end_entry
;
1886 uint32_t end_idx
= start_idx
+max_entries
-1;
1888 /* The first entry needs to be searched after the last. Otherwise the
1889 * first entry might have moved due to a realloc during the search for
1890 * the last entry. */
1892 end_entry
= pdb_search_getentry(search
, end_idx
);
1893 *result
= pdb_search_getentry(search
, start_idx
);
1895 if (end_entry
!= NULL
)
1898 if (start_idx
>= search
->num_entries
)
1901 return search
->num_entries
- start_idx
;
1904 /*******************************************************************
1906 *******************************************************************/
1908 bool pdb_get_trusteddom_pw(const char *domain
, char** pwd
, struct dom_sid
*sid
,
1909 time_t *pass_last_set_time
)
1911 struct pdb_methods
*pdb
= pdb_get_methods();
1912 return pdb
->get_trusteddom_pw(pdb
, domain
, pwd
, sid
,
1913 pass_last_set_time
);
1916 bool pdb_set_trusteddom_pw(const char* domain
, const char* pwd
,
1917 const struct dom_sid
*sid
)
1919 struct pdb_methods
*pdb
= pdb_get_methods();
1920 return pdb
->set_trusteddom_pw(pdb
, domain
, pwd
, sid
);
1923 bool pdb_del_trusteddom_pw(const char *domain
)
1925 struct pdb_methods
*pdb
= pdb_get_methods();
1926 return pdb
->del_trusteddom_pw(pdb
, domain
);
1929 NTSTATUS
pdb_enum_trusteddoms(TALLOC_CTX
*mem_ctx
, uint32_t *num_domains
,
1930 struct trustdom_info
***domains
)
1932 struct pdb_methods
*pdb
= pdb_get_methods();
1933 return pdb
->enum_trusteddoms(pdb
, mem_ctx
, num_domains
, domains
);
1936 /*******************************************************************
1937 the defaults for trustdom methods:
1938 these simply call the original passdb/secrets.c actions,
1939 to be replaced by pdb_ldap.
1940 *******************************************************************/
1942 static bool pdb_default_get_trusteddom_pw(struct pdb_methods
*methods
,
1945 struct dom_sid
*sid
,
1946 time_t *pass_last_set_time
)
1948 return secrets_fetch_trusted_domain_password(domain
, pwd
,
1949 sid
, pass_last_set_time
);
1953 static bool pdb_default_set_trusteddom_pw(struct pdb_methods
*methods
,
1956 const struct dom_sid
*sid
)
1958 return secrets_store_trusted_domain_password(domain
, pwd
, sid
);
1961 static bool pdb_default_del_trusteddom_pw(struct pdb_methods
*methods
,
1964 return trusted_domain_password_delete(domain
);
1967 static NTSTATUS
pdb_default_enum_trusteddoms(struct pdb_methods
*methods
,
1968 TALLOC_CTX
*mem_ctx
,
1969 uint32_t *num_domains
,
1970 struct trustdom_info
***domains
)
1972 return secrets_trusted_domains(mem_ctx
, num_domains
, domains
);
1975 static struct pdb_domain_info
*pdb_default_get_domain_info(
1976 struct pdb_methods
*m
, TALLOC_CTX
*mem_ctx
)
1981 /*******************************************************************
1982 Create a pdb_methods structure and initialize it with the default
1983 operations. In this way a passdb module can simply implement
1984 the functionality it cares about. However, normally this is done
1985 in groups of related functions.
1986 *******************************************************************/
1988 NTSTATUS
make_pdb_method( struct pdb_methods
**methods
)
1990 /* allocate memory for the structure as its own talloc CTX */
1992 *methods
= talloc_zero(talloc_autofree_context(), struct pdb_methods
);
1993 if (*methods
== NULL
) {
1994 return NT_STATUS_NO_MEMORY
;
1997 (*methods
)->get_domain_info
= pdb_default_get_domain_info
;
1998 (*methods
)->getsampwnam
= pdb_default_getsampwnam
;
1999 (*methods
)->getsampwsid
= pdb_default_getsampwsid
;
2000 (*methods
)->create_user
= pdb_default_create_user
;
2001 (*methods
)->delete_user
= pdb_default_delete_user
;
2002 (*methods
)->add_sam_account
= pdb_default_add_sam_account
;
2003 (*methods
)->update_sam_account
= pdb_default_update_sam_account
;
2004 (*methods
)->delete_sam_account
= pdb_default_delete_sam_account
;
2005 (*methods
)->rename_sam_account
= pdb_default_rename_sam_account
;
2006 (*methods
)->update_login_attempts
= pdb_default_update_login_attempts
;
2008 (*methods
)->getgrsid
= pdb_default_getgrsid
;
2009 (*methods
)->getgrgid
= pdb_default_getgrgid
;
2010 (*methods
)->getgrnam
= pdb_default_getgrnam
;
2011 (*methods
)->create_dom_group
= pdb_default_create_dom_group
;
2012 (*methods
)->delete_dom_group
= pdb_default_delete_dom_group
;
2013 (*methods
)->add_group_mapping_entry
= pdb_default_add_group_mapping_entry
;
2014 (*methods
)->update_group_mapping_entry
= pdb_default_update_group_mapping_entry
;
2015 (*methods
)->delete_group_mapping_entry
= pdb_default_delete_group_mapping_entry
;
2016 (*methods
)->enum_group_mapping
= pdb_default_enum_group_mapping
;
2017 (*methods
)->enum_group_members
= pdb_default_enum_group_members
;
2018 (*methods
)->enum_group_memberships
= pdb_default_enum_group_memberships
;
2019 (*methods
)->set_unix_primary_group
= pdb_default_set_unix_primary_group
;
2020 (*methods
)->add_groupmem
= pdb_default_add_groupmem
;
2021 (*methods
)->del_groupmem
= pdb_default_del_groupmem
;
2022 (*methods
)->create_alias
= pdb_default_create_alias
;
2023 (*methods
)->delete_alias
= pdb_default_delete_alias
;
2024 (*methods
)->get_aliasinfo
= pdb_default_get_aliasinfo
;
2025 (*methods
)->set_aliasinfo
= pdb_default_set_aliasinfo
;
2026 (*methods
)->add_aliasmem
= pdb_default_add_aliasmem
;
2027 (*methods
)->del_aliasmem
= pdb_default_del_aliasmem
;
2028 (*methods
)->enum_aliasmem
= pdb_default_enum_aliasmem
;
2029 (*methods
)->enum_alias_memberships
= pdb_default_alias_memberships
;
2030 (*methods
)->lookup_rids
= pdb_default_lookup_rids
;
2031 (*methods
)->get_account_policy
= pdb_default_get_account_policy
;
2032 (*methods
)->set_account_policy
= pdb_default_set_account_policy
;
2033 (*methods
)->get_seq_num
= pdb_default_get_seq_num
;
2034 (*methods
)->uid_to_sid
= pdb_default_uid_to_sid
;
2035 (*methods
)->gid_to_sid
= pdb_default_gid_to_sid
;
2036 (*methods
)->sid_to_id
= pdb_default_sid_to_id
;
2038 (*methods
)->search_groups
= pdb_default_search_groups
;
2039 (*methods
)->search_aliases
= pdb_default_search_aliases
;
2041 (*methods
)->get_trusteddom_pw
= pdb_default_get_trusteddom_pw
;
2042 (*methods
)->set_trusteddom_pw
= pdb_default_set_trusteddom_pw
;
2043 (*methods
)->del_trusteddom_pw
= pdb_default_del_trusteddom_pw
;
2044 (*methods
)->enum_trusteddoms
= pdb_default_enum_trusteddoms
;
2046 return NT_STATUS_OK
;