2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Jelmer Vernooij 2002
6 Copyright (C) Simo Sorce 2003
7 Copyright (C) Volker Lendecke 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define DBGC_CLASS DBGC_PASSDB
28 /* Cache of latest SAM lookup query */
30 static struct samu
*csamuser
= NULL
;
34 static struct pdb_init_function_entry
*backends
= NULL
;
36 static void lazy_initialize_passdb(void)
38 static bool initialized
= False
;
46 static bool lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32 rid
,
48 enum lsa_SidType
*psid_name_use
,
49 union unid_t
*unix_id
);
51 NTSTATUS
smb_register_passdb(int version
, const char *name
, pdb_init_function init
)
53 struct pdb_init_function_entry
*entry
= backends
;
55 if(version
!= PASSDB_INTERFACE_VERSION
) {
56 DEBUG(0,("Can't register passdb backend!\n"
57 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
58 "while this version of samba uses version %d\n",
59 version
,PASSDB_INTERFACE_VERSION
));
60 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
64 return NT_STATUS_INVALID_PARAMETER
;
67 DEBUG(5,("Attempting to register passdb backend %s\n", name
));
69 /* Check for duplicates */
70 if (pdb_find_backend_entry(name
)) {
71 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name
));
72 return NT_STATUS_OBJECT_NAME_COLLISION
;
75 entry
= SMB_XMALLOC_P(struct pdb_init_function_entry
);
76 entry
->name
= smb_xstrdup(name
);
79 DLIST_ADD(backends
, entry
);
80 DEBUG(5,("Successfully added passdb backend '%s'\n", name
));
84 struct pdb_init_function_entry
*pdb_find_backend_entry(const char *name
)
86 struct pdb_init_function_entry
*entry
= backends
;
89 if (strcmp(entry
->name
, name
)==0) return entry
;
97 * The event context for the passdb backend. I know this is a bad hack and yet
98 * another static variable, but our pdb API is a global thing per
99 * definition. The first use for this is the LDAP idle function, more might be
102 * I don't feel too bad about this static variable, it replaces the
103 * smb_idle_event_list that used to exist in lib/module.c. -- VL
106 static struct event_context
*pdb_event_ctx
;
108 struct event_context
*pdb_get_event_context(void)
110 return pdb_event_ctx
;
113 /******************************************************************
114 Make a pdb_methods from scratch
115 *******************************************************************/
117 NTSTATUS
make_pdb_method_name(struct pdb_methods
**methods
, const char *selected
)
119 char *module_name
= smb_xstrdup(selected
);
120 char *module_location
= NULL
, *p
;
121 struct pdb_init_function_entry
*entry
;
122 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
124 lazy_initialize_passdb();
126 p
= strchr(module_name
, ':');
130 module_location
= p
+1;
131 trim_char(module_location
, ' ', ' ');
134 trim_char(module_name
, ' ', ' ');
137 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected
, module_name
));
139 entry
= pdb_find_backend_entry(module_name
);
141 /* Try to find a module that contains this module */
143 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
144 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name
)) && !(entry
= pdb_find_backend_entry(module_name
))) {
145 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name
));
146 SAFE_FREE(module_name
);
147 return NT_STATUS_UNSUCCESSFUL
;
151 /* No such backend found */
153 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name
));
154 SAFE_FREE(module_name
);
155 return NT_STATUS_INVALID_PARAMETER
;
158 DEBUG(5,("Found pdb backend %s\n", module_name
));
160 if ( !NT_STATUS_IS_OK( nt_status
= entry
->init(methods
, module_location
) ) ) {
161 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
162 selected
, nt_errstr(nt_status
)));
163 SAFE_FREE(module_name
);
167 SAFE_FREE(module_name
);
169 DEBUG(5,("pdb backend %s has a valid init\n", selected
));
174 /******************************************************************
175 Return an already initialised pdn_methods structure
176 *******************************************************************/
178 static struct pdb_methods
*pdb_get_methods_reload( bool reload
)
180 static struct pdb_methods
*pdb
= NULL
;
182 if ( pdb
&& reload
) {
183 pdb
->free_private_data( &(pdb
->private_data
) );
184 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
186 asprintf(&msg
, "pdb_get_methods_reload: "
187 "failed to get pdb methods for backend %s\n",
188 lp_passdb_backend());
194 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb
, lp_passdb_backend() ) ) ) {
196 asprintf(&msg
, "pdb_get_methods_reload: "
197 "failed to get pdb methods for backend %s\n",
198 lp_passdb_backend());
206 static struct pdb_methods
*pdb_get_methods(void)
208 return pdb_get_methods_reload(False
);
211 bool pdb_getsampwnam(struct samu
*sam_acct
, const char *username
)
213 struct pdb_methods
*pdb
= pdb_get_methods();
215 if (!NT_STATUS_IS_OK(pdb
->getsampwnam(pdb
, sam_acct
, username
))) {
220 TALLOC_FREE(csamuser
);
223 csamuser
= samu_new( NULL
);
228 if (!pdb_copy_sam_account(csamuser
, sam_acct
)) {
229 TALLOC_FREE(csamuser
);
236 /**********************************************************************
237 **********************************************************************/
239 bool guest_user_info( struct samu
*user
)
243 const char *guestname
= lp_guestaccount();
245 if ( !(pwd
= getpwnam_alloc( NULL
, guestname
) ) ) {
246 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
251 result
= samu_set_unix(user
, pwd
);
255 return NT_STATUS_IS_OK( result
);
258 /**********************************************************************
259 **********************************************************************/
261 bool pdb_getsampwsid(struct samu
*sam_acct
, const DOM_SID
*sid
)
263 struct pdb_methods
*pdb
= pdb_get_methods();
266 /* hard code the Guest RID of 501 */
268 if ( !sid_peek_check_rid( get_global_sam_sid(), sid
, &rid
) )
271 if ( rid
== DOMAIN_USER_RID_GUEST
) {
272 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
273 return guest_user_info( sam_acct
);
276 /* check the cache first */
278 if ( csamuser
&& sid_equal(sid
, pdb_get_user_sid(csamuser
) ) )
279 return pdb_copy_sam_account(sam_acct
, csamuser
);
281 return NT_STATUS_IS_OK(pdb
->getsampwsid(pdb
, sam_acct
, sid
));
284 static NTSTATUS
pdb_default_create_user(struct pdb_methods
*methods
,
285 TALLOC_CTX
*tmp_ctx
, const char *name
,
286 uint32 acb_info
, uint32
*rid
)
288 struct samu
*sam_pass
;
292 if ((sam_pass
= samu_new(tmp_ctx
)) == NULL
) {
293 return NT_STATUS_NO_MEMORY
;
296 if ( !(pwd
= Get_Pwnam_alloc(tmp_ctx
, name
)) ) {
297 char *add_script
= NULL
;
301 if ((acb_info
& ACB_NORMAL
) && name
[strlen(name
)-1] != '$') {
302 add_script
= talloc_strdup(tmp_ctx
,
303 lp_adduser_script());
305 add_script
= talloc_strdup(tmp_ctx
,
306 lp_addmachine_script());
309 if (!add_script
|| add_script
[0] == '\0') {
310 DEBUG(3, ("Could not find user %s and no add script "
312 return NT_STATUS_NO_SUCH_USER
;
315 /* lowercase the username before creating the Unix account for
316 compatibility with previous Samba releases */
317 fstrcpy( name2
, name
);
319 add_script
= talloc_all_string_sub(tmp_ctx
,
324 return NT_STATUS_NO_MEMORY
;
326 add_ret
= smbrun(add_script
,NULL
);
327 DEBUG(add_ret
? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
328 add_script
, add_ret
));
330 smb_nscd_flush_user_cache();
335 pwd
= Get_Pwnam_alloc(tmp_ctx
, name
);
338 /* we have a valid SID coming out of this call */
340 status
= samu_alloc_rid_unix( sam_pass
, pwd
);
344 if (!NT_STATUS_IS_OK(status
)) {
345 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status
)));
349 if (!sid_peek_check_rid(get_global_sam_sid(),
350 pdb_get_user_sid(sam_pass
), rid
)) {
351 DEBUG(0, ("Could not get RID of fresh user\n"));
352 return NT_STATUS_INTERNAL_ERROR
;
355 /* Use the username case specified in the original request */
357 pdb_set_username( sam_pass
, name
, PDB_SET
);
359 /* Disable the account on creation, it does not have a reasonable password yet. */
361 acb_info
|= ACB_DISABLED
;
363 pdb_set_acct_ctrl(sam_pass
, acb_info
, PDB_CHANGED
);
365 status
= pdb_add_sam_account(sam_pass
);
367 TALLOC_FREE(sam_pass
);
372 NTSTATUS
pdb_create_user(TALLOC_CTX
*mem_ctx
, const char *name
, uint32 flags
,
375 struct pdb_methods
*pdb
= pdb_get_methods();
376 return pdb
->create_user(pdb
, mem_ctx
, name
, flags
, rid
);
379 /****************************************************************************
380 Delete a UNIX user on demand.
381 ****************************************************************************/
383 static int smb_delete_user(const char *unix_user
)
385 char *del_script
= NULL
;
390 if ( strequal( unix_user
, "root" ) ) {
391 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
395 del_script
= talloc_strdup(talloc_tos(), lp_deluser_script());
396 if (!del_script
|| !*del_script
) {
399 del_script
= talloc_all_string_sub(talloc_tos(),
406 ret
= smbrun(del_script
,NULL
);
409 smb_nscd_flush_user_cache();
411 DEBUG(ret
? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script
,ret
));
416 static NTSTATUS
pdb_default_delete_user(struct pdb_methods
*methods
,
418 struct samu
*sam_acct
)
423 status
= pdb_delete_sam_account(sam_acct
);
424 if (!NT_STATUS_IS_OK(status
)) {
429 * Now delete the unix side ....
430 * note: we don't check if the delete really happened as the script is
431 * not necessary present and maybe the sysadmin doesn't want to delete
435 /* always lower case the username before handing it off to
438 fstrcpy( username
, pdb_get_username(sam_acct
) );
439 strlower_m( username
);
441 smb_delete_user( username
);
446 NTSTATUS
pdb_delete_user(TALLOC_CTX
*mem_ctx
, struct samu
*sam_acct
)
448 struct pdb_methods
*pdb
= pdb_get_methods();
451 /* sanity check to make sure we don't delete root */
453 if ( !sid_to_uid( pdb_get_user_sid(sam_acct
), &uid
) ) {
454 return NT_STATUS_NO_SUCH_USER
;
458 return NT_STATUS_ACCESS_DENIED
;
461 return pdb
->delete_user(pdb
, mem_ctx
, sam_acct
);
464 NTSTATUS
pdb_add_sam_account(struct samu
*sam_acct
)
466 struct pdb_methods
*pdb
= pdb_get_methods();
467 return pdb
->add_sam_account(pdb
, sam_acct
);
470 NTSTATUS
pdb_update_sam_account(struct samu
*sam_acct
)
472 struct pdb_methods
*pdb
= pdb_get_methods();
474 if (csamuser
!= NULL
) {
475 TALLOC_FREE(csamuser
);
479 return pdb
->update_sam_account(pdb
, sam_acct
);
482 NTSTATUS
pdb_delete_sam_account(struct samu
*sam_acct
)
484 struct pdb_methods
*pdb
= pdb_get_methods();
486 if (csamuser
!= NULL
) {
487 TALLOC_FREE(csamuser
);
491 return pdb
->delete_sam_account(pdb
, sam_acct
);
494 NTSTATUS
pdb_rename_sam_account(struct samu
*oldname
, const char *newname
)
496 struct pdb_methods
*pdb
= pdb_get_methods();
500 if (csamuser
!= NULL
) {
501 TALLOC_FREE(csamuser
);
505 /* sanity check to make sure we don't rename root */
507 if ( !sid_to_uid( pdb_get_user_sid(oldname
), &uid
) ) {
508 return NT_STATUS_NO_SUCH_USER
;
512 return NT_STATUS_ACCESS_DENIED
;
515 status
= pdb
->rename_sam_account(pdb
, oldname
, newname
);
517 /* always flush the cache here just to be safe */
523 NTSTATUS
pdb_update_login_attempts(struct samu
*sam_acct
, bool success
)
525 struct pdb_methods
*pdb
= pdb_get_methods();
526 return pdb
->update_login_attempts(pdb
, sam_acct
, success
);
529 bool pdb_getgrsid(GROUP_MAP
*map
, DOM_SID sid
)
531 struct pdb_methods
*pdb
= pdb_get_methods();
532 return NT_STATUS_IS_OK(pdb
->getgrsid(pdb
, map
, sid
));
535 bool pdb_getgrgid(GROUP_MAP
*map
, gid_t gid
)
537 struct pdb_methods
*pdb
= pdb_get_methods();
538 return NT_STATUS_IS_OK(pdb
->getgrgid(pdb
, map
, gid
));
541 bool pdb_getgrnam(GROUP_MAP
*map
, const char *name
)
543 struct pdb_methods
*pdb
= pdb_get_methods();
544 return NT_STATUS_IS_OK(pdb
->getgrnam(pdb
, map
, name
));
547 static NTSTATUS
pdb_default_create_dom_group(struct pdb_methods
*methods
,
556 grp
= getgrnam(name
);
561 if (smb_create_group(name
, &gid
) != 0) {
562 return NT_STATUS_ACCESS_DENIED
;
569 return NT_STATUS_ACCESS_DENIED
;
572 if (pdb_rid_algorithm()) {
573 *rid
= algorithmic_pdb_gid_to_group_rid( grp
->gr_gid
);
575 if (!pdb_new_rid(rid
)) {
576 return NT_STATUS_ACCESS_DENIED
;
580 sid_compose(&group_sid
, get_global_sam_sid(), *rid
);
582 return add_initial_entry(grp
->gr_gid
, sid_to_fstring(tmp
, &group_sid
),
583 SID_NAME_DOM_GRP
, name
, NULL
);
586 NTSTATUS
pdb_create_dom_group(TALLOC_CTX
*mem_ctx
, const char *name
,
589 struct pdb_methods
*pdb
= pdb_get_methods();
590 return pdb
->create_dom_group(pdb
, mem_ctx
, name
, rid
);
593 static NTSTATUS
pdb_default_delete_dom_group(struct pdb_methods
*methods
,
601 const char *grp_name
;
603 sid_compose(&group_sid
, get_global_sam_sid(), rid
);
605 if (!get_domain_group_from_sid(group_sid
, &map
)) {
606 DEBUG(10, ("Could not find group for rid %d\n", rid
));
607 return NT_STATUS_NO_SUCH_GROUP
;
610 /* We need the group name for the smb_delete_group later on */
612 if (map
.gid
== (gid_t
)-1) {
613 return NT_STATUS_NO_SUCH_GROUP
;
616 grp
= getgrgid(map
.gid
);
618 return NT_STATUS_NO_SUCH_GROUP
;
621 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
623 grp_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
624 if (grp_name
== NULL
) {
625 return NT_STATUS_NO_MEMORY
;
628 status
= pdb_delete_group_mapping_entry(group_sid
);
630 if (!NT_STATUS_IS_OK(status
)) {
634 /* Don't check the result of smb_delete_group */
636 smb_delete_group(grp_name
);
641 NTSTATUS
pdb_delete_dom_group(TALLOC_CTX
*mem_ctx
, uint32 rid
)
643 struct pdb_methods
*pdb
= pdb_get_methods();
644 return pdb
->delete_dom_group(pdb
, mem_ctx
, rid
);
647 NTSTATUS
pdb_add_group_mapping_entry(GROUP_MAP
*map
)
649 struct pdb_methods
*pdb
= pdb_get_methods();
650 return pdb
->add_group_mapping_entry(pdb
, map
);
653 NTSTATUS
pdb_update_group_mapping_entry(GROUP_MAP
*map
)
655 struct pdb_methods
*pdb
= pdb_get_methods();
656 return pdb
->update_group_mapping_entry(pdb
, map
);
659 NTSTATUS
pdb_delete_group_mapping_entry(DOM_SID sid
)
661 struct pdb_methods
*pdb
= pdb_get_methods();
662 return pdb
->delete_group_mapping_entry(pdb
, sid
);
665 bool pdb_enum_group_mapping(const DOM_SID
*sid
, enum lsa_SidType sid_name_use
, GROUP_MAP
**pp_rmap
,
666 size_t *p_num_entries
, bool unix_only
)
668 struct pdb_methods
*pdb
= pdb_get_methods();
669 return NT_STATUS_IS_OK(pdb
-> enum_group_mapping(pdb
, sid
, sid_name_use
,
670 pp_rmap
, p_num_entries
, unix_only
));
673 NTSTATUS
pdb_enum_group_members(TALLOC_CTX
*mem_ctx
,
675 uint32
**pp_member_rids
,
676 size_t *p_num_members
)
678 struct pdb_methods
*pdb
= pdb_get_methods();
681 result
= pdb
->enum_group_members(pdb
, mem_ctx
,
682 sid
, pp_member_rids
, p_num_members
);
684 /* special check for rid 513 */
686 if ( !NT_STATUS_IS_OK( result
) ) {
689 sid_peek_rid( sid
, &rid
);
691 if ( rid
== DOMAIN_GROUP_RID_USERS
) {
693 *pp_member_rids
= NULL
;
702 NTSTATUS
pdb_enum_group_memberships(TALLOC_CTX
*mem_ctx
, struct samu
*user
,
703 DOM_SID
**pp_sids
, gid_t
**pp_gids
,
704 size_t *p_num_groups
)
706 struct pdb_methods
*pdb
= pdb_get_methods();
707 return pdb
->enum_group_memberships(
709 pp_sids
, pp_gids
, p_num_groups
);
712 static NTSTATUS
pdb_default_set_unix_primary_group(struct pdb_methods
*methods
,
714 struct samu
*sampass
)
719 if (!sid_to_gid(pdb_get_group_sid(sampass
), &gid
) ||
720 (grp
= getgrgid(gid
)) == NULL
) {
721 return NT_STATUS_INVALID_PRIMARY_GROUP
;
724 if (smb_set_primary_group(grp
->gr_name
,
725 pdb_get_username(sampass
)) != 0) {
726 return NT_STATUS_ACCESS_DENIED
;
732 NTSTATUS
pdb_set_unix_primary_group(TALLOC_CTX
*mem_ctx
, struct samu
*user
)
734 struct pdb_methods
*pdb
= pdb_get_methods();
735 return pdb
->set_unix_primary_group(pdb
, mem_ctx
, user
);
739 * Helper function to see whether a user is in a group. We can't use
740 * user_in_group_sid here because this creates dependencies only smbd can
744 static bool pdb_user_in_group(TALLOC_CTX
*mem_ctx
, struct samu
*account
,
745 const DOM_SID
*group_sid
)
749 size_t i
, num_groups
;
751 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx
, account
,
757 for (i
=0; i
<num_groups
; i
++) {
758 if (sid_equal(group_sid
, &sids
[i
])) {
765 static NTSTATUS
pdb_default_add_groupmem(struct pdb_methods
*methods
,
770 DOM_SID group_sid
, member_sid
;
771 struct samu
*account
= NULL
;
775 const char *group_name
;
778 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
779 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
781 if (!get_domain_group_from_sid(group_sid
, &map
) ||
782 (map
.gid
== (gid_t
)-1) ||
783 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
784 return NT_STATUS_NO_SUCH_GROUP
;
787 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
788 if (group_name
== NULL
) {
789 return NT_STATUS_NO_MEMORY
;
792 if ( !(account
= samu_new( NULL
)) ) {
793 return NT_STATUS_NO_MEMORY
;
796 if (!pdb_getsampwsid(account
, &member_sid
) ||
797 !sid_to_uid(&member_sid
, &uid
) ||
798 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
799 return NT_STATUS_NO_SUCH_USER
;
802 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
803 return NT_STATUS_MEMBER_IN_GROUP
;
807 * ok, the group exist, the user exist, the user is not in the group,
808 * we can (finally) add it to the group !
811 smb_add_user_group(group_name
, pwd
->pw_name
);
813 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
814 return NT_STATUS_ACCESS_DENIED
;
820 NTSTATUS
pdb_add_groupmem(TALLOC_CTX
*mem_ctx
, uint32 group_rid
,
823 struct pdb_methods
*pdb
= pdb_get_methods();
824 return pdb
->add_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
827 static NTSTATUS
pdb_default_del_groupmem(struct pdb_methods
*methods
,
832 DOM_SID group_sid
, member_sid
;
833 struct samu
*account
= NULL
;
837 const char *group_name
;
840 sid_compose(&group_sid
, get_global_sam_sid(), group_rid
);
841 sid_compose(&member_sid
, get_global_sam_sid(), member_rid
);
843 if (!get_domain_group_from_sid(group_sid
, &map
) ||
844 (map
.gid
== (gid_t
)-1) ||
845 ((grp
= getgrgid(map
.gid
)) == NULL
)) {
846 return NT_STATUS_NO_SUCH_GROUP
;
849 group_name
= talloc_strdup(mem_ctx
, grp
->gr_name
);
850 if (group_name
== NULL
) {
851 return NT_STATUS_NO_MEMORY
;
854 if ( !(account
= samu_new( NULL
)) ) {
855 return NT_STATUS_NO_MEMORY
;
858 if (!pdb_getsampwsid(account
, &member_sid
) ||
859 !sid_to_uid(&member_sid
, &uid
) ||
860 ((pwd
= getpwuid_alloc(mem_ctx
, uid
)) == NULL
)) {
861 return NT_STATUS_NO_SUCH_USER
;
864 if (!pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
865 return NT_STATUS_MEMBER_NOT_IN_GROUP
;
869 * ok, the group exist, the user exist, the user is in the group,
870 * we can (finally) delete it from the group!
873 smb_delete_user_group(group_name
, pwd
->pw_name
);
875 if (pdb_user_in_group(mem_ctx
, account
, &group_sid
)) {
876 return NT_STATUS_ACCESS_DENIED
;
882 NTSTATUS
pdb_del_groupmem(TALLOC_CTX
*mem_ctx
, uint32 group_rid
,
885 struct pdb_methods
*pdb
= pdb_get_methods();
886 return pdb
->del_groupmem(pdb
, mem_ctx
, group_rid
, member_rid
);
889 NTSTATUS
pdb_create_alias(const char *name
, uint32
*rid
)
891 struct pdb_methods
*pdb
= pdb_get_methods();
892 return pdb
->create_alias(pdb
, name
, rid
);
895 NTSTATUS
pdb_delete_alias(const DOM_SID
*sid
)
897 struct pdb_methods
*pdb
= pdb_get_methods();
898 return pdb
->delete_alias(pdb
, sid
);
901 NTSTATUS
pdb_get_aliasinfo(const DOM_SID
*sid
, struct acct_info
*info
)
903 struct pdb_methods
*pdb
= pdb_get_methods();
904 return pdb
->get_aliasinfo(pdb
, sid
, info
);
907 NTSTATUS
pdb_set_aliasinfo(const DOM_SID
*sid
, struct acct_info
*info
)
909 struct pdb_methods
*pdb
= pdb_get_methods();
910 return pdb
->set_aliasinfo(pdb
, sid
, info
);
913 NTSTATUS
pdb_add_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
915 struct pdb_methods
*pdb
= pdb_get_methods();
916 return pdb
->add_aliasmem(pdb
, alias
, member
);
919 NTSTATUS
pdb_del_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
921 struct pdb_methods
*pdb
= pdb_get_methods();
922 return pdb
->del_aliasmem(pdb
, alias
, member
);
925 NTSTATUS
pdb_enum_aliasmem(const DOM_SID
*alias
,
926 DOM_SID
**pp_members
, size_t *p_num_members
)
928 struct pdb_methods
*pdb
= pdb_get_methods();
929 return pdb
->enum_aliasmem(pdb
, alias
, pp_members
, p_num_members
);
932 NTSTATUS
pdb_enum_alias_memberships(TALLOC_CTX
*mem_ctx
,
933 const DOM_SID
*domain_sid
,
934 const DOM_SID
*members
, size_t num_members
,
935 uint32
**pp_alias_rids
,
936 size_t *p_num_alias_rids
)
938 struct pdb_methods
*pdb
= pdb_get_methods();
939 return pdb
->enum_alias_memberships(pdb
, mem_ctx
,
941 members
, num_members
,
946 NTSTATUS
pdb_lookup_rids(const DOM_SID
*domain_sid
,
950 enum lsa_SidType
*attrs
)
952 struct pdb_methods
*pdb
= pdb_get_methods();
953 return pdb
->lookup_rids(pdb
, domain_sid
, num_rids
, rids
, names
, attrs
);
957 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
959 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
960 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
961 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
962 * down to are pdb_getsampwnam and pdb_getgrnam instead of
964 * But in principle, it the call belongs to the API and might get
965 * used in this context some day.
968 NTSTATUS
pdb_lookup_names(const DOM_SID
*domain_sid
,
972 enum lsa_SidType
*attrs
)
974 struct pdb_methods
*pdb
= pdb_get_methods();
975 return pdb
->lookup_names(pdb
, domain_sid
, num_names
, names
, rids
, attrs
);
979 bool pdb_get_account_policy(int policy_index
, uint32
*value
)
981 struct pdb_methods
*pdb
= pdb_get_methods();
985 status
= pdb
->get_account_policy(pdb
, policy_index
, value
);
988 return NT_STATUS_IS_OK(status
);
991 bool pdb_set_account_policy(int policy_index
, uint32 value
)
993 struct pdb_methods
*pdb
= pdb_get_methods();
997 status
= pdb
->set_account_policy(pdb
, policy_index
, value
);
1000 return NT_STATUS_IS_OK(status
);
1003 bool pdb_get_seq_num(time_t *seq_num
)
1005 struct pdb_methods
*pdb
= pdb_get_methods();
1006 return NT_STATUS_IS_OK(pdb
->get_seq_num(pdb
, seq_num
));
1009 bool pdb_uid_to_rid(uid_t uid
, uint32
*rid
)
1011 struct pdb_methods
*pdb
= pdb_get_methods();
1012 return pdb
->uid_to_rid(pdb
, uid
, rid
);
1015 bool pdb_uid_to_sid(uid_t uid
, DOM_SID
*sid
)
1017 struct pdb_methods
*pdb
= pdb_get_methods();
1018 return pdb
->uid_to_sid(pdb
, uid
, sid
);
1021 bool pdb_gid_to_sid(gid_t gid
, DOM_SID
*sid
)
1023 struct pdb_methods
*pdb
= pdb_get_methods();
1024 return pdb
->gid_to_sid(pdb
, gid
, sid
);
1027 bool pdb_sid_to_id(const DOM_SID
*sid
, union unid_t
*id
,
1028 enum lsa_SidType
*type
)
1030 struct pdb_methods
*pdb
= pdb_get_methods();
1031 return pdb
->sid_to_id(pdb
, sid
, id
, type
);
1034 bool pdb_rid_algorithm(void)
1036 struct pdb_methods
*pdb
= pdb_get_methods();
1037 return pdb
->rid_algorithm(pdb
);
1040 /********************************************************************
1041 Allocate a new RID from the passdb backend. Verify that it is free
1042 by calling lookup_global_sam_rid() to verify that the RID is not
1043 in use. This handles servers that have existing users or groups
1044 with add RIDs (assigned from previous algorithmic mappings)
1045 ********************************************************************/
1047 bool pdb_new_rid(uint32
*rid
)
1049 struct pdb_methods
*pdb
= pdb_get_methods();
1050 const char *name
= NULL
;
1051 enum lsa_SidType type
;
1052 uint32 allocated_rid
= 0;
1056 if (pdb_rid_algorithm()) {
1057 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1062 if (algorithmic_rid_base() != BASE_RID
) {
1063 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1064 "without algorithmic RIDs is chosen.\n"));
1065 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1066 "add', set the maximum used RID using\n"));
1067 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1071 if ( (ctx
= talloc_init("pdb_new_rid")) == NULL
) {
1072 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1076 /* Attempt to get an unused RID (max tires is 250...yes that it is
1077 and arbitrary number I pulkled out of my head). -- jerry */
1079 for ( i
=0; allocated_rid
==0 && i
<250; i
++ ) {
1082 if ( !pdb
->new_rid(pdb
, &allocated_rid
) ) {
1086 /* validate that the RID is not in use */
1088 if ( lookup_global_sam_rid( ctx
, allocated_rid
, &name
, &type
, NULL
) ) {
1095 if ( allocated_rid
== 0 ) {
1096 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1100 *rid
= allocated_rid
;
1105 /***************************************************************
1106 Initialize the static context (at smbd startup etc).
1108 If uninitialised, context will auto-init on first use.
1109 ***************************************************************/
1111 bool initialize_password_db(bool reload
, struct event_context
*event_ctx
)
1113 pdb_event_ctx
= event_ctx
;
1114 return (pdb_get_methods_reload(reload
) != NULL
);
1118 /***************************************************************************
1119 Default implementations of some functions.
1120 ****************************************************************************/
1122 static NTSTATUS
pdb_default_getsampwnam (struct pdb_methods
*methods
, struct samu
*user
, const char *sname
)
1124 return NT_STATUS_NO_SUCH_USER
;
1127 static NTSTATUS
pdb_default_getsampwsid(struct pdb_methods
*my_methods
, struct samu
* user
, const DOM_SID
*sid
)
1129 return NT_STATUS_NO_SUCH_USER
;
1132 static NTSTATUS
pdb_default_add_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1134 return NT_STATUS_NOT_IMPLEMENTED
;
1137 static NTSTATUS
pdb_default_update_sam_account (struct pdb_methods
*methods
, struct samu
*newpwd
)
1139 return NT_STATUS_NOT_IMPLEMENTED
;
1142 static NTSTATUS
pdb_default_delete_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
)
1144 return NT_STATUS_NOT_IMPLEMENTED
;
1147 static NTSTATUS
pdb_default_rename_sam_account (struct pdb_methods
*methods
, struct samu
*pwd
, const char *newname
)
1149 return NT_STATUS_NOT_IMPLEMENTED
;
1152 static NTSTATUS
pdb_default_update_login_attempts (struct pdb_methods
*methods
, struct samu
*newpwd
, bool success
)
1154 return NT_STATUS_NOT_IMPLEMENTED
;
1157 static NTSTATUS
pdb_default_get_account_policy(struct pdb_methods
*methods
, int policy_index
, uint32
*value
)
1159 return account_policy_get(policy_index
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1162 static NTSTATUS
pdb_default_set_account_policy(struct pdb_methods
*methods
, int policy_index
, uint32 value
)
1164 return account_policy_set(policy_index
, value
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1167 static NTSTATUS
pdb_default_get_seq_num(struct pdb_methods
*methods
, time_t *seq_num
)
1169 *seq_num
= time(NULL
);
1170 return NT_STATUS_OK
;
1173 static bool pdb_default_uid_to_sid(struct pdb_methods
*methods
, uid_t uid
,
1176 struct samu
*sampw
= NULL
;
1177 struct passwd
*unix_pw
;
1180 unix_pw
= sys_getpwuid( uid
);
1183 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1184 "%lu\n", (unsigned long)uid
));
1188 if ( !(sampw
= samu_new( NULL
)) ) {
1189 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1194 ret
= NT_STATUS_IS_OK(
1195 methods
->getsampwnam(methods
, sampw
, unix_pw
->pw_name
));
1199 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1200 "%s (%d)\n", unix_pw
->pw_name
, uid
));
1205 sid_copy(sid
, pdb_get_user_sid(sampw
));
1212 static bool pdb_default_uid_to_rid(struct pdb_methods
*methods
, uid_t uid
,
1218 ret
= pdb_default_uid_to_sid(methods
, uid
, &sid
);
1223 ret
= sid_peek_check_rid(get_global_sam_sid(), &sid
, rid
);
1226 DEBUG(1, ("Could not peek rid out of sid %s\n",
1227 sid_string_dbg(&sid
)));
1233 static bool pdb_default_gid_to_sid(struct pdb_methods
*methods
, gid_t gid
,
1238 if (!NT_STATUS_IS_OK(methods
->getgrgid(methods
, &map
, gid
))) {
1242 sid_copy(sid
, &map
.sid
);
1246 static bool pdb_default_sid_to_id(struct pdb_methods
*methods
,
1248 union unid_t
*id
, enum lsa_SidType
*type
)
1250 TALLOC_CTX
*mem_ctx
;
1255 mem_ctx
= talloc_new(NULL
);
1257 if (mem_ctx
== NULL
) {
1258 DEBUG(0, ("talloc_new failed\n"));
1262 if (sid_peek_check_rid(get_global_sam_sid(), sid
, &rid
)) {
1263 /* Here we might have users as well as groups and aliases */
1264 ret
= lookup_global_sam_rid(mem_ctx
, rid
, &name
, type
, id
);
1268 /* check for "Unix User" */
1270 if ( sid_peek_check_rid(&global_sid_Unix_Users
, sid
, &rid
) ) {
1272 *type
= SID_NAME_USER
;
1277 /* check for "Unix Group" */
1279 if ( sid_peek_check_rid(&global_sid_Unix_Groups
, sid
, &rid
) ) {
1281 *type
= SID_NAME_ALIAS
;
1288 if (sid_check_is_in_builtin(sid
) ||
1289 sid_check_is_in_wellknown_domain(sid
)) {
1290 /* Here we only have aliases */
1292 if (!NT_STATUS_IS_OK(methods
->getgrsid(methods
, &map
, *sid
))) {
1293 DEBUG(10, ("Could not find map for sid %s\n",
1294 sid_string_dbg(sid
)));
1297 if ((map
.sid_name_use
!= SID_NAME_ALIAS
) &&
1298 (map
.sid_name_use
!= SID_NAME_WKN_GRP
)) {
1299 DEBUG(10, ("Map for sid %s is a %s, expected an "
1300 "alias\n", sid_string_dbg(sid
),
1301 sid_type_lookup(map
.sid_name_use
)));
1306 *type
= SID_NAME_ALIAS
;
1311 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1312 sid_string_dbg(sid
)));
1316 TALLOC_FREE(mem_ctx
);
1320 static bool add_uid_to_array_unique(TALLOC_CTX
*mem_ctx
,
1321 uid_t uid
, uid_t
**pp_uids
, size_t *p_num
)
1325 for (i
=0; i
<*p_num
; i
++) {
1326 if ((*pp_uids
)[i
] == uid
)
1330 *pp_uids
= TALLOC_REALLOC_ARRAY(mem_ctx
, *pp_uids
, uid_t
, *p_num
+1);
1332 if (*pp_uids
== NULL
)
1335 (*pp_uids
)[*p_num
] = uid
;
1340 static bool get_memberuids(TALLOC_CTX
*mem_ctx
, gid_t gid
, uid_t
**pp_uids
, size_t *p_num
)
1351 /* We only look at our own sam, so don't care about imported stuff */
1352 winbind_env
= winbind_env_set();
1355 if ((grp
= getgrgid(gid
)) == NULL
) {
1356 /* allow winbindd lookups, but only if they weren't already disabled */
1360 /* Primary group members */
1362 while ((pwd
= getpwent()) != NULL
) {
1363 if (pwd
->pw_gid
== gid
) {
1364 if (!add_uid_to_array_unique(mem_ctx
, pwd
->pw_uid
,
1372 /* Secondary group members */
1373 for (gr
= grp
->gr_mem
; (*gr
!= NULL
) && ((*gr
)[0] != '\0'); gr
+= 1) {
1374 struct passwd
*pw
= getpwnam(*gr
);
1378 if (!add_uid_to_array_unique(mem_ctx
, pw
->pw_uid
, pp_uids
, p_num
)) {
1387 /* allow winbindd lookups, but only if they weren't already disabled */
1395 static NTSTATUS
pdb_default_enum_group_members(struct pdb_methods
*methods
,
1396 TALLOC_CTX
*mem_ctx
,
1397 const DOM_SID
*group
,
1398 uint32
**pp_member_rids
,
1399 size_t *p_num_members
)
1405 *pp_member_rids
= NULL
;
1408 if (!sid_to_gid(group
, &gid
))
1409 return NT_STATUS_NO_SUCH_GROUP
;
1411 if(!get_memberuids(mem_ctx
, gid
, &uids
, &num_uids
))
1412 return NT_STATUS_NO_SUCH_GROUP
;
1415 return NT_STATUS_OK
;
1417 *pp_member_rids
= TALLOC_ZERO_ARRAY(mem_ctx
, uint32
, num_uids
);
1419 for (i
=0; i
<num_uids
; i
++) {
1422 uid_to_sid(&sid
, uids
[i
]);
1424 if (!sid_check_is_in_our_domain(&sid
)) {
1425 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1426 "in our domain\n"));
1430 sid_peek_rid(&sid
, &(*pp_member_rids
)[*p_num_members
]);
1431 *p_num_members
+= 1;
1434 return NT_STATUS_OK
;
1437 static NTSTATUS
pdb_default_enum_group_memberships(struct pdb_methods
*methods
,
1438 TALLOC_CTX
*mem_ctx
,
1442 size_t *p_num_groups
)
1447 const char *username
= pdb_get_username(user
);
1450 /* Ignore the primary group SID. Honor the real Unix primary group.
1451 The primary group SID is only of real use to Windows clients */
1453 if ( !(pw
= getpwnam_alloc(mem_ctx
, username
)) ) {
1454 return NT_STATUS_NO_SUCH_USER
;
1461 if (!getgroups_unix_user(mem_ctx
, username
, gid
, pp_gids
, p_num_groups
)) {
1462 return NT_STATUS_NO_SUCH_USER
;
1465 if (*p_num_groups
== 0) {
1466 smb_panic("primary group missing");
1469 *pp_sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, *p_num_groups
);
1471 if (*pp_sids
== NULL
) {
1472 TALLOC_FREE(*pp_gids
);
1473 return NT_STATUS_NO_MEMORY
;
1476 for (i
=0; i
<*p_num_groups
; i
++) {
1477 gid_to_sid(&(*pp_sids
)[i
], (*pp_gids
)[i
]);
1480 return NT_STATUS_OK
;
1483 /*******************************************************************
1484 Look up a rid in the SAM we're responsible for (i.e. passdb)
1485 ********************************************************************/
1487 static bool lookup_global_sam_rid(TALLOC_CTX
*mem_ctx
, uint32 rid
,
1489 enum lsa_SidType
*psid_name_use
,
1490 union unid_t
*unix_id
)
1492 struct samu
*sam_account
= NULL
;
1497 *psid_name_use
= SID_NAME_UNKNOWN
;
1499 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1500 (unsigned int)rid
));
1502 sid_copy(&sid
, get_global_sam_sid());
1503 sid_append_rid(&sid
, rid
);
1505 /* see if the passdb can help us with the name of the user */
1507 if ( !(sam_account
= samu_new( NULL
)) ) {
1511 /* BEING ROOT BLLOCK */
1513 if (pdb_getsampwsid(sam_account
, &sid
)) {
1516 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1517 *name
= talloc_strdup(mem_ctx
, pdb_get_username(sam_account
));
1519 TALLOC_FREE(sam_account
);
1523 *psid_name_use
= SID_NAME_USER
;
1525 TALLOC_FREE(sam_account
);
1527 if (unix_id
== NULL
) {
1531 pw
= Get_Pwnam_alloc(talloc_tos(), *name
);
1535 unix_id
->uid
= pw
->pw_uid
;
1539 TALLOC_FREE(sam_account
);
1541 ret
= pdb_getgrsid(&map
, sid
);
1543 /* END BECOME_ROOT BLOCK */
1545 /* do not resolve SIDs to a name unless there is a valid
1546 gid associated with it */
1548 if ( ret
&& (map
.gid
!= (gid_t
)-1) ) {
1549 *name
= talloc_strdup(mem_ctx
, map
.nt_name
);
1550 *psid_name_use
= map
.sid_name_use
;
1553 unix_id
->gid
= map
.gid
;
1559 /* Windows will always map RID 513 to something. On a non-domain
1560 controller, this gets mapped to SERVER\None. */
1563 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1567 if ( rid
== DOMAIN_GROUP_RID_USERS
) {
1568 *name
= talloc_strdup(mem_ctx
, "None" );
1569 *psid_name_use
= SID_NAME_DOM_GRP
;
1577 static NTSTATUS
pdb_default_lookup_rids(struct pdb_methods
*methods
,
1578 const DOM_SID
*domain_sid
,
1582 enum lsa_SidType
*attrs
)
1586 bool have_mapped
= False
;
1587 bool have_unmapped
= False
;
1589 if (sid_check_is_builtin(domain_sid
)) {
1591 for (i
=0; i
<num_rids
; i
++) {
1594 if (lookup_builtin_rid(names
, rids
[i
], &name
)) {
1595 attrs
[i
] = SID_NAME_ALIAS
;
1597 DEBUG(5,("lookup_rids: %s:%d\n",
1598 names
[i
], attrs
[i
]));
1601 have_unmapped
= True
;
1602 attrs
[i
] = SID_NAME_UNKNOWN
;
1608 /* Should not happen, but better check once too many */
1609 if (!sid_check_is_domain(domain_sid
)) {
1610 return NT_STATUS_INVALID_HANDLE
;
1613 for (i
= 0; i
< num_rids
; i
++) {
1616 if (lookup_global_sam_rid(names
, rids
[i
], &name
, &attrs
[i
],
1619 return NT_STATUS_NO_MEMORY
;
1622 DEBUG(5,("lookup_rids: %s:%d\n", names
[i
], attrs
[i
]));
1625 have_unmapped
= True
;
1626 attrs
[i
] = SID_NAME_UNKNOWN
;
1632 result
= NT_STATUS_NONE_MAPPED
;
1635 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1641 static NTSTATUS
pdb_default_lookup_names(struct pdb_methods
*methods
,
1642 const DOM_SID
*domain_sid
,
1646 enum lsa_SidType
*attrs
)
1650 bool have_mapped
= False
;
1651 bool have_unmapped
= False
;
1653 if (sid_check_is_builtin(domain_sid
)) {
1655 for (i
=0; i
<num_names
; i
++) {
1658 if (lookup_builtin_name(names
[i
], &rid
)) {
1659 attrs
[i
] = SID_NAME_ALIAS
;
1661 DEBUG(5,("lookup_rids: %s:%d\n",
1662 names
[i
], attrs
[i
]));
1665 have_unmapped
= True
;
1666 attrs
[i
] = SID_NAME_UNKNOWN
;
1672 /* Should not happen, but better check once too many */
1673 if (!sid_check_is_domain(domain_sid
)) {
1674 return NT_STATUS_INVALID_HANDLE
;
1677 for (i
= 0; i
< num_names
; i
++) {
1678 if (lookup_global_sam_name(names
[i
], 0, &rids
[i
], &attrs
[i
])) {
1679 DEBUG(5,("lookup_names: %s-> %d:%d\n", names
[i
],
1680 rids
[i
], attrs
[i
]));
1683 have_unmapped
= True
;
1684 attrs
[i
] = SID_NAME_UNKNOWN
;
1690 result
= NT_STATUS_NONE_MAPPED
;
1693 result
= have_unmapped
? STATUS_SOME_UNMAPPED
: NT_STATUS_OK
;
1699 struct pdb_search
*pdb_search_init(enum pdb_search_type type
)
1701 TALLOC_CTX
*mem_ctx
;
1702 struct pdb_search
*result
;
1704 mem_ctx
= talloc_init("pdb_search");
1705 if (mem_ctx
== NULL
) {
1706 DEBUG(0, ("talloc_init failed\n"));
1710 result
= TALLOC_P(mem_ctx
, struct pdb_search
);
1711 if (result
== NULL
) {
1712 DEBUG(0, ("talloc failed\n"));
1716 result
->mem_ctx
= mem_ctx
;
1717 result
->type
= type
;
1718 result
->cache
= NULL
;
1719 result
->num_entries
= 0;
1720 result
->cache_size
= 0;
1721 result
->search_ended
= False
;
1723 /* Segfault appropriately if not initialized */
1724 result
->next_entry
= NULL
;
1725 result
->search_end
= NULL
;
1730 static void fill_displayentry(TALLOC_CTX
*mem_ctx
, uint32 rid
,
1732 const char *account_name
,
1733 const char *fullname
,
1734 const char *description
,
1735 struct samr_displayentry
*entry
)
1738 entry
->acct_flags
= acct_flags
;
1740 if (account_name
!= NULL
)
1741 entry
->account_name
= talloc_strdup(mem_ctx
, account_name
);
1743 entry
->account_name
= "";
1745 if (fullname
!= NULL
)
1746 entry
->fullname
= talloc_strdup(mem_ctx
, fullname
);
1748 entry
->fullname
= "";
1750 if (description
!= NULL
)
1751 entry
->description
= talloc_strdup(mem_ctx
, description
);
1753 entry
->description
= "";
1756 struct group_search
{
1758 size_t num_groups
, current_group
;
1761 static bool next_entry_groups(struct pdb_search
*s
,
1762 struct samr_displayentry
*entry
)
1764 struct group_search
*state
= (struct group_search
*)s
->private_data
;
1766 GROUP_MAP
*map
= &state
->groups
[state
->current_group
];
1768 if (state
->current_group
== state
->num_groups
)
1771 sid_peek_rid(&map
->sid
, &rid
);
1773 fill_displayentry(s
->mem_ctx
, rid
, 0, map
->nt_name
, NULL
, map
->comment
,
1776 state
->current_group
+= 1;
1780 static void search_end_groups(struct pdb_search
*search
)
1782 struct group_search
*state
=
1783 (struct group_search
*)search
->private_data
;
1784 SAFE_FREE(state
->groups
);
1787 static bool pdb_search_grouptype(struct pdb_search
*search
,
1788 const DOM_SID
*sid
, enum lsa_SidType type
)
1790 struct group_search
*state
;
1792 state
= TALLOC_P(search
->mem_ctx
, struct group_search
);
1793 if (state
== NULL
) {
1794 DEBUG(0, ("talloc failed\n"));
1798 if (!pdb_enum_group_mapping(sid
, type
, &state
->groups
, &state
->num_groups
,
1800 DEBUG(0, ("Could not enum groups\n"));
1804 state
->current_group
= 0;
1805 search
->private_data
= state
;
1806 search
->next_entry
= next_entry_groups
;
1807 search
->search_end
= search_end_groups
;
1811 static bool pdb_default_search_groups(struct pdb_methods
*methods
,
1812 struct pdb_search
*search
)
1814 return pdb_search_grouptype(search
, get_global_sam_sid(), SID_NAME_DOM_GRP
);
1817 static bool pdb_default_search_aliases(struct pdb_methods
*methods
,
1818 struct pdb_search
*search
,
1822 return pdb_search_grouptype(search
, sid
, SID_NAME_ALIAS
);
1825 static struct samr_displayentry
*pdb_search_getentry(struct pdb_search
*search
,
1828 if (idx
< search
->num_entries
)
1829 return &search
->cache
[idx
];
1831 if (search
->search_ended
)
1834 while (idx
>= search
->num_entries
) {
1835 struct samr_displayentry entry
;
1837 if (!search
->next_entry(search
, &entry
)) {
1838 search
->search_end(search
);
1839 search
->search_ended
= True
;
1843 ADD_TO_LARGE_ARRAY(search
->mem_ctx
, struct samr_displayentry
,
1844 entry
, &search
->cache
, &search
->num_entries
,
1845 &search
->cache_size
);
1848 return (search
->num_entries
> idx
) ? &search
->cache
[idx
] : NULL
;
1851 struct pdb_search
*pdb_search_users(uint32 acct_flags
)
1853 struct pdb_methods
*pdb
= pdb_get_methods();
1854 struct pdb_search
*result
;
1856 result
= pdb_search_init(PDB_USER_SEARCH
);
1857 if (result
== NULL
) {
1861 if (!pdb
->search_users(pdb
, result
, acct_flags
)) {
1862 talloc_destroy(result
->mem_ctx
);
1868 struct pdb_search
*pdb_search_groups(void)
1870 struct pdb_methods
*pdb
= pdb_get_methods();
1871 struct pdb_search
*result
;
1873 result
= pdb_search_init(PDB_GROUP_SEARCH
);
1874 if (result
== NULL
) {
1878 if (!pdb
->search_groups(pdb
, result
)) {
1879 talloc_destroy(result
->mem_ctx
);
1885 struct pdb_search
*pdb_search_aliases(const DOM_SID
*sid
)
1887 struct pdb_methods
*pdb
= pdb_get_methods();
1888 struct pdb_search
*result
;
1890 if (pdb
== NULL
) return NULL
;
1892 result
= pdb_search_init(PDB_ALIAS_SEARCH
);
1893 if (result
== NULL
) return NULL
;
1895 if (!pdb
->search_aliases(pdb
, result
, sid
)) {
1896 talloc_destroy(result
->mem_ctx
);
1902 uint32
pdb_search_entries(struct pdb_search
*search
,
1903 uint32 start_idx
, uint32 max_entries
,
1904 struct samr_displayentry
**result
)
1906 struct samr_displayentry
*end_entry
;
1907 uint32 end_idx
= start_idx
+max_entries
-1;
1909 /* The first entry needs to be searched after the last. Otherwise the
1910 * first entry might have moved due to a realloc during the search for
1911 * the last entry. */
1913 end_entry
= pdb_search_getentry(search
, end_idx
);
1914 *result
= pdb_search_getentry(search
, start_idx
);
1916 if (end_entry
!= NULL
)
1919 if (start_idx
>= search
->num_entries
)
1922 return search
->num_entries
- start_idx
;
1925 void pdb_search_destroy(struct pdb_search
*search
)
1930 if (!search
->search_ended
)
1931 search
->search_end(search
);
1933 talloc_destroy(search
->mem_ctx
);
1936 /*******************************************************************
1938 *******************************************************************/
1940 bool pdb_get_trusteddom_pw(const char *domain
, char** pwd
, DOM_SID
*sid
,
1941 time_t *pass_last_set_time
)
1943 struct pdb_methods
*pdb
= pdb_get_methods();
1944 return pdb
->get_trusteddom_pw(pdb
, domain
, pwd
, sid
,
1945 pass_last_set_time
);
1948 bool pdb_set_trusteddom_pw(const char* domain
, const char* pwd
,
1951 struct pdb_methods
*pdb
= pdb_get_methods();
1952 return pdb
->set_trusteddom_pw(pdb
, domain
, pwd
, sid
);
1955 bool pdb_del_trusteddom_pw(const char *domain
)
1957 struct pdb_methods
*pdb
= pdb_get_methods();
1958 return pdb
->del_trusteddom_pw(pdb
, domain
);
1961 NTSTATUS
pdb_enum_trusteddoms(TALLOC_CTX
*mem_ctx
, uint32
*num_domains
,
1962 struct trustdom_info
***domains
)
1964 struct pdb_methods
*pdb
= pdb_get_methods();
1965 return pdb
->enum_trusteddoms(pdb
, mem_ctx
, num_domains
, domains
);
1968 /*******************************************************************
1969 the defaults for trustdom methods:
1970 these simply call the original passdb/secrets.c actions,
1971 to be replaced by pdb_ldap.
1972 *******************************************************************/
1974 static bool pdb_default_get_trusteddom_pw(struct pdb_methods
*methods
,
1978 time_t *pass_last_set_time
)
1980 return secrets_fetch_trusted_domain_password(domain
, pwd
,
1981 sid
, pass_last_set_time
);
1985 static bool pdb_default_set_trusteddom_pw(struct pdb_methods
*methods
,
1990 return secrets_store_trusted_domain_password(domain
, pwd
, sid
);
1993 static bool pdb_default_del_trusteddom_pw(struct pdb_methods
*methods
,
1996 return trusted_domain_password_delete(domain
);
1999 static NTSTATUS
pdb_default_enum_trusteddoms(struct pdb_methods
*methods
,
2000 TALLOC_CTX
*mem_ctx
,
2001 uint32
*num_domains
,
2002 struct trustdom_info
***domains
)
2004 return secrets_trusted_domains(mem_ctx
, num_domains
, domains
);
2007 /*******************************************************************
2008 Create a pdb_methods structure and initialize it with the default
2009 operations. In this way a passdb module can simply implement
2010 the functionality it cares about. However, normally this is done
2011 in groups of related functions.
2012 *******************************************************************/
2014 NTSTATUS
make_pdb_method( struct pdb_methods
**methods
)
2016 /* allocate memory for the structure as its own talloc CTX */
2018 if ( !(*methods
= TALLOC_ZERO_P(NULL
, struct pdb_methods
) ) ) {
2019 return NT_STATUS_NO_MEMORY
;
2022 (*methods
)->getsampwnam
= pdb_default_getsampwnam
;
2023 (*methods
)->getsampwsid
= pdb_default_getsampwsid
;
2024 (*methods
)->create_user
= pdb_default_create_user
;
2025 (*methods
)->delete_user
= pdb_default_delete_user
;
2026 (*methods
)->add_sam_account
= pdb_default_add_sam_account
;
2027 (*methods
)->update_sam_account
= pdb_default_update_sam_account
;
2028 (*methods
)->delete_sam_account
= pdb_default_delete_sam_account
;
2029 (*methods
)->rename_sam_account
= pdb_default_rename_sam_account
;
2030 (*methods
)->update_login_attempts
= pdb_default_update_login_attempts
;
2032 (*methods
)->getgrsid
= pdb_default_getgrsid
;
2033 (*methods
)->getgrgid
= pdb_default_getgrgid
;
2034 (*methods
)->getgrnam
= pdb_default_getgrnam
;
2035 (*methods
)->create_dom_group
= pdb_default_create_dom_group
;
2036 (*methods
)->delete_dom_group
= pdb_default_delete_dom_group
;
2037 (*methods
)->add_group_mapping_entry
= pdb_default_add_group_mapping_entry
;
2038 (*methods
)->update_group_mapping_entry
= pdb_default_update_group_mapping_entry
;
2039 (*methods
)->delete_group_mapping_entry
= pdb_default_delete_group_mapping_entry
;
2040 (*methods
)->enum_group_mapping
= pdb_default_enum_group_mapping
;
2041 (*methods
)->enum_group_members
= pdb_default_enum_group_members
;
2042 (*methods
)->enum_group_memberships
= pdb_default_enum_group_memberships
;
2043 (*methods
)->set_unix_primary_group
= pdb_default_set_unix_primary_group
;
2044 (*methods
)->add_groupmem
= pdb_default_add_groupmem
;
2045 (*methods
)->del_groupmem
= pdb_default_del_groupmem
;
2046 (*methods
)->create_alias
= pdb_default_create_alias
;
2047 (*methods
)->delete_alias
= pdb_default_delete_alias
;
2048 (*methods
)->get_aliasinfo
= pdb_default_get_aliasinfo
;
2049 (*methods
)->set_aliasinfo
= pdb_default_set_aliasinfo
;
2050 (*methods
)->add_aliasmem
= pdb_default_add_aliasmem
;
2051 (*methods
)->del_aliasmem
= pdb_default_del_aliasmem
;
2052 (*methods
)->enum_aliasmem
= pdb_default_enum_aliasmem
;
2053 (*methods
)->enum_alias_memberships
= pdb_default_alias_memberships
;
2054 (*methods
)->lookup_rids
= pdb_default_lookup_rids
;
2055 (*methods
)->get_account_policy
= pdb_default_get_account_policy
;
2056 (*methods
)->set_account_policy
= pdb_default_set_account_policy
;
2057 (*methods
)->get_seq_num
= pdb_default_get_seq_num
;
2058 (*methods
)->uid_to_rid
= pdb_default_uid_to_rid
;
2059 (*methods
)->uid_to_sid
= pdb_default_uid_to_sid
;
2060 (*methods
)->gid_to_sid
= pdb_default_gid_to_sid
;
2061 (*methods
)->sid_to_id
= pdb_default_sid_to_id
;
2063 (*methods
)->search_groups
= pdb_default_search_groups
;
2064 (*methods
)->search_aliases
= pdb_default_search_aliases
;
2066 (*methods
)->get_trusteddom_pw
= pdb_default_get_trusteddom_pw
;
2067 (*methods
)->set_trusteddom_pw
= pdb_default_set_trusteddom_pw
;
2068 (*methods
)->del_trusteddom_pw
= pdb_default_del_trusteddom_pw
;
2069 (*methods
)->enum_trusteddoms
= pdb_default_enum_trusteddoms
;
2071 return NT_STATUS_OK
;