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 slprintf(msg, sizeof(msg)-1, "pdb_get_methods_reload: failed to get pdb methods for backend %s\n",
187 lp_passdb_backend() );
193 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
195 slprintf(msg, sizeof(msg)-1, "pdb_get_methods_reload: failed to get pdb methods for backend %s\n",
196 lp_passdb_backend() );
204 static struct pdb_methods *pdb_get_methods(void)
206 return pdb_get_methods_reload(False);
209 /******************************************************************
210 Backward compatibility functions for the original passdb interface
211 *******************************************************************/
213 bool pdb_setsampwent(bool update, uint16 acb_mask)
215 struct pdb_methods *pdb = pdb_get_methods();
216 return NT_STATUS_IS_OK(pdb->setsampwent(pdb, update, acb_mask));
219 void pdb_endsampwent(void)
221 struct pdb_methods *pdb = pdb_get_methods();
222 pdb->endsampwent(pdb);
225 bool pdb_getsampwent(struct samu *user)
227 struct pdb_methods *pdb = pdb_get_methods();
229 if ( !NT_STATUS_IS_OK(pdb->getsampwent(pdb, user) ) ) {
236 bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
238 struct pdb_methods *pdb = pdb_get_methods();
240 if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
245 TALLOC_FREE(csamuser);
248 csamuser = samu_new( NULL );
253 if (!pdb_copy_sam_account(csamuser, sam_acct)) {
254 TALLOC_FREE(csamuser);
261 /**********************************************************************
262 **********************************************************************/
264 bool guest_user_info( struct samu *user )
268 const char *guestname = lp_guestaccount();
270 if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
271 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
276 result = samu_set_unix(user, pwd );
280 return NT_STATUS_IS_OK( result );
283 /**********************************************************************
284 **********************************************************************/
286 bool pdb_getsampwsid(struct samu *sam_acct, const DOM_SID *sid)
288 struct pdb_methods *pdb = pdb_get_methods();
291 /* hard code the Guest RID of 501 */
293 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
296 if ( rid == DOMAIN_USER_RID_GUEST ) {
297 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
298 return guest_user_info( sam_acct );
301 /* check the cache first */
303 if ( csamuser && sid_equal(sid, pdb_get_user_sid(csamuser) ) )
304 return pdb_copy_sam_account(sam_acct, csamuser);
306 return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
309 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
310 TALLOC_CTX *tmp_ctx, const char *name,
311 uint32 acb_info, uint32 *rid)
313 struct samu *sam_pass;
317 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
318 return NT_STATUS_NO_MEMORY;
321 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
326 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
327 pstrcpy(add_script, lp_adduser_script());
329 pstrcpy(add_script, lp_addmachine_script());
332 if (add_script[0] == '\0') {
333 DEBUG(3, ("Could not find user %s and no add script "
335 return NT_STATUS_NO_SUCH_USER;
338 /* lowercase the username before creating the Unix account for
339 compatibility with previous Samba releases */
340 fstrcpy( name2, name );
342 all_string_sub(add_script, "%u", name2, sizeof(add_script));
343 add_ret = smbrun(add_script,NULL);
344 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
345 add_script, add_ret));
347 smb_nscd_flush_user_cache();
352 pwd = Get_Pwnam_alloc(tmp_ctx, name);
355 /* we have a valid SID coming out of this call */
357 status = samu_alloc_rid_unix( sam_pass, pwd );
361 if (!NT_STATUS_IS_OK(status)) {
362 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
366 if (!sid_peek_check_rid(get_global_sam_sid(),
367 pdb_get_user_sid(sam_pass), rid)) {
368 DEBUG(0, ("Could not get RID of fresh user\n"));
369 return NT_STATUS_INTERNAL_ERROR;
372 /* Use the username case specified in the original request */
374 pdb_set_username( sam_pass, name, PDB_SET );
376 /* Disable the account on creation, it does not have a reasonable password yet. */
378 acb_info |= ACB_DISABLED;
380 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
382 status = pdb_add_sam_account(sam_pass);
384 TALLOC_FREE(sam_pass);
389 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
392 struct pdb_methods *pdb = pdb_get_methods();
393 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
396 /****************************************************************************
397 Delete a UNIX user on demand.
398 ****************************************************************************/
400 static int smb_delete_user(const char *unix_user)
407 if ( strequal( unix_user, "root" ) ) {
408 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
412 pstrcpy(del_script, lp_deluser_script());
415 all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
416 ret = smbrun(del_script,NULL);
419 smb_nscd_flush_user_cache();
421 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
426 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
428 struct samu *sam_acct)
433 status = pdb_delete_sam_account(sam_acct);
434 if (!NT_STATUS_IS_OK(status)) {
439 * Now delete the unix side ....
440 * note: we don't check if the delete really happened as the script is
441 * not necessary present and maybe the sysadmin doesn't want to delete
445 /* always lower case the username before handing it off to
448 fstrcpy( username, pdb_get_username(sam_acct) );
449 strlower_m( username );
451 smb_delete_user( username );
456 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
458 struct pdb_methods *pdb = pdb_get_methods();
461 /* sanity check to make sure we don't delete root */
463 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
464 return NT_STATUS_NO_SUCH_USER;
468 return NT_STATUS_ACCESS_DENIED;
471 return pdb->delete_user(pdb, mem_ctx, sam_acct);
474 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
476 struct pdb_methods *pdb = pdb_get_methods();
477 return pdb->add_sam_account(pdb, sam_acct);
480 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
482 struct pdb_methods *pdb = pdb_get_methods();
484 if (csamuser != NULL) {
485 TALLOC_FREE(csamuser);
489 return pdb->update_sam_account(pdb, sam_acct);
492 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
494 struct pdb_methods *pdb = pdb_get_methods();
496 if (csamuser != NULL) {
497 TALLOC_FREE(csamuser);
501 return pdb->delete_sam_account(pdb, sam_acct);
504 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
506 struct pdb_methods *pdb = pdb_get_methods();
510 if (csamuser != NULL) {
511 TALLOC_FREE(csamuser);
515 /* sanity check to make sure we don't rename root */
517 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
518 return NT_STATUS_NO_SUCH_USER;
522 return NT_STATUS_ACCESS_DENIED;
525 status = pdb->rename_sam_account(pdb, oldname, newname);
527 /* always flush the cache here just to be safe */
533 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
535 struct pdb_methods *pdb = pdb_get_methods();
536 return pdb->update_login_attempts(pdb, sam_acct, success);
539 bool pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
541 struct pdb_methods *pdb = pdb_get_methods();
542 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
545 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
547 struct pdb_methods *pdb = pdb_get_methods();
548 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
551 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
553 struct pdb_methods *pdb = pdb_get_methods();
554 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
557 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
565 grp = getgrnam(name);
570 if (smb_create_group(name, &gid) != 0) {
571 return NT_STATUS_ACCESS_DENIED;
578 return NT_STATUS_ACCESS_DENIED;
581 if (pdb_rid_algorithm()) {
582 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
584 if (!pdb_new_rid(rid)) {
585 return NT_STATUS_ACCESS_DENIED;
589 sid_compose(&group_sid, get_global_sam_sid(), *rid);
591 return add_initial_entry(grp->gr_gid, sid_string_static(&group_sid),
592 SID_NAME_DOM_GRP, name, NULL);
595 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
598 struct pdb_methods *pdb = pdb_get_methods();
599 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
602 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
610 const char *grp_name;
612 sid_compose(&group_sid, get_global_sam_sid(), rid);
614 if (!get_domain_group_from_sid(group_sid, &map)) {
615 DEBUG(10, ("Could not find group for rid %d\n", rid));
616 return NT_STATUS_NO_SUCH_GROUP;
619 /* We need the group name for the smb_delete_group later on */
621 if (map.gid == (gid_t)-1) {
622 return NT_STATUS_NO_SUCH_GROUP;
625 grp = getgrgid(map.gid);
627 return NT_STATUS_NO_SUCH_GROUP;
630 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
632 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
633 if (grp_name == NULL) {
634 return NT_STATUS_NO_MEMORY;
637 status = pdb_delete_group_mapping_entry(group_sid);
639 if (!NT_STATUS_IS_OK(status)) {
643 /* Don't check the result of smb_delete_group */
645 smb_delete_group(grp_name);
650 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
652 struct pdb_methods *pdb = pdb_get_methods();
653 return pdb->delete_dom_group(pdb, mem_ctx, rid);
656 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
658 struct pdb_methods *pdb = pdb_get_methods();
659 return pdb->add_group_mapping_entry(pdb, map);
662 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
664 struct pdb_methods *pdb = pdb_get_methods();
665 return pdb->update_group_mapping_entry(pdb, map);
668 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
670 struct pdb_methods *pdb = pdb_get_methods();
671 return pdb->delete_group_mapping_entry(pdb, sid);
674 bool pdb_enum_group_mapping(const DOM_SID *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
675 size_t *p_num_entries, bool unix_only)
677 struct pdb_methods *pdb = pdb_get_methods();
678 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
679 pp_rmap, p_num_entries, unix_only));
682 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
684 uint32 **pp_member_rids,
685 size_t *p_num_members)
687 struct pdb_methods *pdb = pdb_get_methods();
690 result = pdb->enum_group_members(pdb, mem_ctx,
691 sid, pp_member_rids, p_num_members);
693 /* special check for rid 513 */
695 if ( !NT_STATUS_IS_OK( result ) ) {
698 sid_peek_rid( sid, &rid );
700 if ( rid == DOMAIN_GROUP_RID_USERS ) {
702 *pp_member_rids = NULL;
711 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
712 DOM_SID **pp_sids, gid_t **pp_gids,
713 size_t *p_num_groups)
715 struct pdb_methods *pdb = pdb_get_methods();
716 return pdb->enum_group_memberships(
718 pp_sids, pp_gids, p_num_groups);
721 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
723 struct samu *sampass)
728 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
729 (grp = getgrgid(gid)) == NULL) {
730 return NT_STATUS_INVALID_PRIMARY_GROUP;
733 if (smb_set_primary_group(grp->gr_name,
734 pdb_get_username(sampass)) != 0) {
735 return NT_STATUS_ACCESS_DENIED;
741 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
743 struct pdb_methods *pdb = pdb_get_methods();
744 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
748 * Helper function to see whether a user is in a group. We can't use
749 * user_in_group_sid here because this creates dependencies only smbd can
753 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
754 const DOM_SID *group_sid)
758 size_t i, num_groups;
760 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
766 for (i=0; i<num_groups; i++) {
767 if (sid_equal(group_sid, &sids[i])) {
774 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
779 DOM_SID group_sid, member_sid;
780 struct samu *account = NULL;
784 const char *group_name;
787 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
788 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
790 if (!get_domain_group_from_sid(group_sid, &map) ||
791 (map.gid == (gid_t)-1) ||
792 ((grp = getgrgid(map.gid)) == NULL)) {
793 return NT_STATUS_NO_SUCH_GROUP;
796 group_name = talloc_strdup(mem_ctx, grp->gr_name);
797 if (group_name == NULL) {
798 return NT_STATUS_NO_MEMORY;
801 if ( !(account = samu_new( NULL )) ) {
802 return NT_STATUS_NO_MEMORY;
805 if (!pdb_getsampwsid(account, &member_sid) ||
806 !sid_to_uid(&member_sid, &uid) ||
807 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
808 return NT_STATUS_NO_SUCH_USER;
811 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
812 return NT_STATUS_MEMBER_IN_GROUP;
816 * ok, the group exist, the user exist, the user is not in the group,
817 * we can (finally) add it to the group !
820 smb_add_user_group(group_name, pwd->pw_name);
822 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
823 return NT_STATUS_ACCESS_DENIED;
829 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
832 struct pdb_methods *pdb = pdb_get_methods();
833 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
836 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
841 DOM_SID group_sid, member_sid;
842 struct samu *account = NULL;
846 const char *group_name;
849 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
850 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
852 if (!get_domain_group_from_sid(group_sid, &map) ||
853 (map.gid == (gid_t)-1) ||
854 ((grp = getgrgid(map.gid)) == NULL)) {
855 return NT_STATUS_NO_SUCH_GROUP;
858 group_name = talloc_strdup(mem_ctx, grp->gr_name);
859 if (group_name == NULL) {
860 return NT_STATUS_NO_MEMORY;
863 if ( !(account = samu_new( NULL )) ) {
864 return NT_STATUS_NO_MEMORY;
867 if (!pdb_getsampwsid(account, &member_sid) ||
868 !sid_to_uid(&member_sid, &uid) ||
869 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
870 return NT_STATUS_NO_SUCH_USER;
873 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
874 return NT_STATUS_MEMBER_NOT_IN_GROUP;
878 * ok, the group exist, the user exist, the user is in the group,
879 * we can (finally) delete it from the group!
882 smb_delete_user_group(group_name, pwd->pw_name);
884 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
885 return NT_STATUS_ACCESS_DENIED;
891 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
894 struct pdb_methods *pdb = pdb_get_methods();
895 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
898 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
900 struct pdb_methods *pdb = pdb_get_methods();
901 return pdb->create_alias(pdb, name, rid);
904 NTSTATUS pdb_delete_alias(const DOM_SID *sid)
906 struct pdb_methods *pdb = pdb_get_methods();
907 return pdb->delete_alias(pdb, sid);
910 NTSTATUS pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
912 struct pdb_methods *pdb = pdb_get_methods();
913 return pdb->get_aliasinfo(pdb, sid, info);
916 NTSTATUS pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
918 struct pdb_methods *pdb = pdb_get_methods();
919 return pdb->set_aliasinfo(pdb, sid, info);
922 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
924 struct pdb_methods *pdb = pdb_get_methods();
925 return pdb->add_aliasmem(pdb, alias, member);
928 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
930 struct pdb_methods *pdb = pdb_get_methods();
931 return pdb->del_aliasmem(pdb, alias, member);
934 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
935 DOM_SID **pp_members, size_t *p_num_members)
937 struct pdb_methods *pdb = pdb_get_methods();
938 return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
941 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
942 const DOM_SID *domain_sid,
943 const DOM_SID *members, size_t num_members,
944 uint32 **pp_alias_rids,
945 size_t *p_num_alias_rids)
947 struct pdb_methods *pdb = pdb_get_methods();
948 return pdb->enum_alias_memberships(pdb, mem_ctx,
950 members, num_members,
955 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
959 enum lsa_SidType *attrs)
961 struct pdb_methods *pdb = pdb_get_methods();
962 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
966 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
968 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
969 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
970 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
971 * down to are pdb_getsampwnam and pdb_getgrnam instead of
973 * But in principle, it the call belongs to the API and might get
974 * used in this context some day.
977 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
981 enum lsa_SidType *attrs)
983 struct pdb_methods *pdb = pdb_get_methods();
984 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
988 bool pdb_get_account_policy(int policy_index, uint32 *value)
990 struct pdb_methods *pdb = pdb_get_methods();
994 status = pdb->get_account_policy(pdb, policy_index, value);
997 return NT_STATUS_IS_OK(status);
1000 bool pdb_set_account_policy(int policy_index, uint32 value)
1002 struct pdb_methods *pdb = pdb_get_methods();
1006 status = pdb->set_account_policy(pdb, policy_index, value);
1009 return NT_STATUS_IS_OK(status);
1012 bool pdb_get_seq_num(time_t *seq_num)
1014 struct pdb_methods *pdb = pdb_get_methods();
1015 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1018 bool pdb_uid_to_rid(uid_t uid, uint32 *rid)
1020 struct pdb_methods *pdb = pdb_get_methods();
1021 return pdb->uid_to_rid(pdb, uid, rid);
1024 bool pdb_uid_to_sid(uid_t uid, DOM_SID *sid)
1026 struct pdb_methods *pdb = pdb_get_methods();
1027 return pdb->uid_to_sid(pdb, uid, sid);
1030 bool pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1032 struct pdb_methods *pdb = pdb_get_methods();
1033 return pdb->gid_to_sid(pdb, gid, sid);
1036 bool pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1037 enum lsa_SidType *type)
1039 struct pdb_methods *pdb = pdb_get_methods();
1040 return pdb->sid_to_id(pdb, sid, id, type);
1043 bool pdb_rid_algorithm(void)
1045 struct pdb_methods *pdb = pdb_get_methods();
1046 return pdb->rid_algorithm(pdb);
1049 /********************************************************************
1050 Allocate a new RID from the passdb backend. Verify that it is free
1051 by calling lookup_global_sam_rid() to verify that the RID is not
1052 in use. This handles servers that have existing users or groups
1053 with add RIDs (assigned from previous algorithmic mappings)
1054 ********************************************************************/
1056 bool pdb_new_rid(uint32 *rid)
1058 struct pdb_methods *pdb = pdb_get_methods();
1059 const char *name = NULL;
1060 enum lsa_SidType type;
1061 uint32 allocated_rid = 0;
1065 if (pdb_rid_algorithm()) {
1066 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1071 if (algorithmic_rid_base() != BASE_RID) {
1072 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1073 "without algorithmic RIDs is chosen.\n"));
1074 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1075 "add', set the maximum used RID using\n"));
1076 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1080 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1081 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1085 /* Attempt to get an unused RID (max tires is 250...yes that it is
1086 and arbitrary number I pulkled out of my head). -- jerry */
1088 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1091 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1095 /* validate that the RID is not in use */
1097 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1104 if ( allocated_rid == 0 ) {
1105 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1109 *rid = allocated_rid;
1114 /***************************************************************
1115 Initialize the static context (at smbd startup etc).
1117 If uninitialised, context will auto-init on first use.
1118 ***************************************************************/
1120 bool initialize_password_db(bool reload, struct event_context *event_ctx)
1122 pdb_event_ctx = event_ctx;
1123 return (pdb_get_methods_reload(reload) != NULL);
1127 /***************************************************************************
1128 Default implementations of some functions.
1129 ****************************************************************************/
1131 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1133 return NT_STATUS_NO_SUCH_USER;
1136 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1138 return NT_STATUS_NO_SUCH_USER;
1141 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1143 return NT_STATUS_NOT_IMPLEMENTED;
1146 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1148 return NT_STATUS_NOT_IMPLEMENTED;
1151 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1153 return NT_STATUS_NOT_IMPLEMENTED;
1156 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1158 return NT_STATUS_NOT_IMPLEMENTED;
1161 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1163 return NT_STATUS_NOT_IMPLEMENTED;
1166 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, bool update, uint32 acb_mask)
1168 return NT_STATUS_NOT_IMPLEMENTED;
1171 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, struct samu *user)
1173 return NT_STATUS_NOT_IMPLEMENTED;
1176 static void pdb_default_endsampwent(struct pdb_methods *methods)
1178 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1181 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1183 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1186 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1188 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1191 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1193 *seq_num = time(NULL);
1194 return NT_STATUS_OK;
1197 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1200 struct samu *sampw = NULL;
1201 struct passwd *unix_pw;
1204 unix_pw = sys_getpwuid( uid );
1207 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1208 "%lu\n", (unsigned long)uid));
1212 if ( !(sampw = samu_new( NULL )) ) {
1213 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1218 ret = NT_STATUS_IS_OK(
1219 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1223 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1224 "%s (%d)\n", unix_pw->pw_name, uid));
1229 sid_copy(sid, pdb_get_user_sid(sampw));
1236 static bool pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1242 ret = pdb_default_uid_to_sid(methods, uid, &sid);
1247 ret = sid_peek_check_rid(get_global_sam_sid(), &sid, rid);
1250 DEBUG(1, ("Could not peek rid out of sid %s\n",
1251 sid_string_static(&sid)));
1257 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1262 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1266 sid_copy(sid, &map.sid);
1270 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1272 union unid_t *id, enum lsa_SidType *type)
1274 TALLOC_CTX *mem_ctx;
1279 mem_ctx = talloc_new(NULL);
1281 if (mem_ctx == NULL) {
1282 DEBUG(0, ("talloc_new failed\n"));
1286 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1287 /* Here we might have users as well as groups and aliases */
1288 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1292 /* check for "Unix User" */
1294 if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1296 *type = SID_NAME_USER;
1301 /* check for "Unix Group" */
1303 if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1305 *type = SID_NAME_ALIAS;
1312 if (sid_check_is_in_builtin(sid) ||
1313 sid_check_is_in_wellknown_domain(sid)) {
1314 /* Here we only have aliases */
1316 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1317 DEBUG(10, ("Could not find map for sid %s\n",
1318 sid_string_static(sid)));
1321 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1322 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1323 DEBUG(10, ("Map for sid %s is a %s, expected an "
1324 "alias\n", sid_string_static(sid),
1325 sid_type_lookup(map.sid_name_use)));
1330 *type = SID_NAME_ALIAS;
1335 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1336 sid_string_static(sid)));
1340 TALLOC_FREE(mem_ctx);
1344 static bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1345 uid_t uid, uid_t **pp_uids, size_t *p_num)
1349 for (i=0; i<*p_num; i++) {
1350 if ((*pp_uids)[i] == uid)
1354 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1356 if (*pp_uids == NULL)
1359 (*pp_uids)[*p_num] = uid;
1364 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1375 /* We only look at our own sam, so don't care about imported stuff */
1376 winbind_env = winbind_env_set();
1379 if ((grp = getgrgid(gid)) == NULL) {
1380 /* allow winbindd lookups, but only if they weren't already disabled */
1384 /* Primary group members */
1386 while ((pwd = getpwent()) != NULL) {
1387 if (pwd->pw_gid == gid) {
1388 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1396 /* Secondary group members */
1397 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1398 struct passwd *pw = getpwnam(*gr);
1402 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1411 /* allow winbindd lookups, but only if they weren't already disabled */
1419 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1420 TALLOC_CTX *mem_ctx,
1421 const DOM_SID *group,
1422 uint32 **pp_member_rids,
1423 size_t *p_num_members)
1429 *pp_member_rids = NULL;
1432 if (!sid_to_gid(group, &gid))
1433 return NT_STATUS_NO_SUCH_GROUP;
1435 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1436 return NT_STATUS_NO_SUCH_GROUP;
1439 return NT_STATUS_OK;
1441 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1443 for (i=0; i<num_uids; i++) {
1446 uid_to_sid(&sid, uids[i]);
1448 if (!sid_check_is_in_our_domain(&sid)) {
1449 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1450 "in our domain\n"));
1454 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1455 *p_num_members += 1;
1458 return NT_STATUS_OK;
1461 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1462 TALLOC_CTX *mem_ctx,
1466 size_t *p_num_groups)
1471 const char *username = pdb_get_username(user);
1474 /* Ignore the primary group SID. Honor the real Unix primary group.
1475 The primary group SID is only of real use to Windows clients */
1477 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1478 return NT_STATUS_NO_SUCH_USER;
1485 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1486 return NT_STATUS_NO_SUCH_USER;
1489 if (*p_num_groups == 0) {
1490 smb_panic("primary group missing");
1493 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1495 if (*pp_sids == NULL) {
1496 TALLOC_FREE(*pp_gids);
1497 return NT_STATUS_NO_MEMORY;
1500 for (i=0; i<*p_num_groups; i++) {
1501 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1504 return NT_STATUS_OK;
1507 /*******************************************************************
1508 Look up a rid in the SAM we're responsible for (i.e. passdb)
1509 ********************************************************************/
1511 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1513 enum lsa_SidType *psid_name_use,
1514 union unid_t *unix_id)
1516 struct samu *sam_account = NULL;
1521 *psid_name_use = SID_NAME_UNKNOWN;
1523 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1524 (unsigned int)rid));
1526 sid_copy(&sid, get_global_sam_sid());
1527 sid_append_rid(&sid, rid);
1529 /* see if the passdb can help us with the name of the user */
1531 if ( !(sam_account = samu_new( NULL )) ) {
1535 /* BEING ROOT BLLOCK */
1537 if (pdb_getsampwsid(sam_account, &sid)) {
1540 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1541 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1543 TALLOC_FREE(sam_account);
1547 *psid_name_use = SID_NAME_USER;
1549 TALLOC_FREE(sam_account);
1551 if (unix_id == NULL) {
1555 pw = Get_Pwnam(*name);
1559 unix_id->uid = pw->pw_uid;
1562 TALLOC_FREE(sam_account);
1564 ret = pdb_getgrsid(&map, sid);
1566 /* END BECOME_ROOT BLOCK */
1568 /* do not resolve SIDs to a name unless there is a valid
1569 gid associated with it */
1571 if ( ret && (map.gid != (gid_t)-1) ) {
1572 *name = talloc_strdup(mem_ctx, map.nt_name);
1573 *psid_name_use = map.sid_name_use;
1576 unix_id->gid = map.gid;
1582 /* Windows will always map RID 513 to something. On a non-domain
1583 controller, this gets mapped to SERVER\None. */
1586 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1590 if ( rid == DOMAIN_GROUP_RID_USERS ) {
1591 *name = talloc_strdup(mem_ctx, "None" );
1592 *psid_name_use = SID_NAME_DOM_GRP;
1600 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1601 const DOM_SID *domain_sid,
1605 enum lsa_SidType *attrs)
1609 bool have_mapped = False;
1610 bool have_unmapped = False;
1612 if (sid_check_is_builtin(domain_sid)) {
1614 for (i=0; i<num_rids; i++) {
1617 if (lookup_builtin_rid(names, rids[i], &name)) {
1618 attrs[i] = SID_NAME_ALIAS;
1620 DEBUG(5,("lookup_rids: %s:%d\n",
1621 names[i], attrs[i]));
1624 have_unmapped = True;
1625 attrs[i] = SID_NAME_UNKNOWN;
1631 /* Should not happen, but better check once too many */
1632 if (!sid_check_is_domain(domain_sid)) {
1633 return NT_STATUS_INVALID_HANDLE;
1636 for (i = 0; i < num_rids; i++) {
1639 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1642 return NT_STATUS_NO_MEMORY;
1645 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1648 have_unmapped = True;
1649 attrs[i] = SID_NAME_UNKNOWN;
1655 result = NT_STATUS_NONE_MAPPED;
1658 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1664 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1665 const DOM_SID *domain_sid,
1669 enum lsa_SidType *attrs)
1673 bool have_mapped = False;
1674 bool have_unmapped = False;
1676 if (sid_check_is_builtin(domain_sid)) {
1678 for (i=0; i<num_names; i++) {
1681 if (lookup_builtin_name(names[i], &rid)) {
1682 attrs[i] = SID_NAME_ALIAS;
1684 DEBUG(5,("lookup_rids: %s:%d\n",
1685 names[i], attrs[i]));
1688 have_unmapped = True;
1689 attrs[i] = SID_NAME_UNKNOWN;
1695 /* Should not happen, but better check once too many */
1696 if (!sid_check_is_domain(domain_sid)) {
1697 return NT_STATUS_INVALID_HANDLE;
1700 for (i = 0; i < num_names; i++) {
1701 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1702 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1703 rids[i], attrs[i]));
1706 have_unmapped = True;
1707 attrs[i] = SID_NAME_UNKNOWN;
1713 result = NT_STATUS_NONE_MAPPED;
1716 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1722 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1724 TALLOC_CTX *mem_ctx;
1725 struct pdb_search *result;
1727 mem_ctx = talloc_init("pdb_search");
1728 if (mem_ctx == NULL) {
1729 DEBUG(0, ("talloc_init failed\n"));
1733 result = TALLOC_P(mem_ctx, struct pdb_search);
1734 if (result == NULL) {
1735 DEBUG(0, ("talloc failed\n"));
1739 result->mem_ctx = mem_ctx;
1740 result->type = type;
1741 result->cache = NULL;
1742 result->num_entries = 0;
1743 result->cache_size = 0;
1744 result->search_ended = False;
1746 /* Segfault appropriately if not initialized */
1747 result->next_entry = NULL;
1748 result->search_end = NULL;
1753 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1755 const char *account_name,
1756 const char *fullname,
1757 const char *description,
1758 struct samr_displayentry *entry)
1761 entry->acct_flags = acct_flags;
1763 if (account_name != NULL)
1764 entry->account_name = talloc_strdup(mem_ctx, account_name);
1766 entry->account_name = "";
1768 if (fullname != NULL)
1769 entry->fullname = talloc_strdup(mem_ctx, fullname);
1771 entry->fullname = "";
1773 if (description != NULL)
1774 entry->description = talloc_strdup(mem_ctx, description);
1776 entry->description = "";
1779 static bool user_search_in_progress = False;
1780 struct user_search {
1784 static bool next_entry_users(struct pdb_search *s,
1785 struct samr_displayentry *entry)
1787 struct user_search *state = (struct user_search *)s->private_data;
1788 struct samu *user = NULL;
1791 if ( !(user = samu_new( NULL )) ) {
1792 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1796 if (!pdb_getsampwent(user)) {
1801 if ((state->acct_flags != 0) &&
1802 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1807 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1808 pdb_get_acct_ctrl(user), pdb_get_username(user),
1809 pdb_get_fullname(user), pdb_get_acct_desc(user),
1816 static void search_end_users(struct pdb_search *search)
1819 user_search_in_progress = False;
1822 static bool pdb_default_search_users(struct pdb_methods *methods,
1823 struct pdb_search *search,
1826 struct user_search *state;
1828 if (user_search_in_progress) {
1829 DEBUG(1, ("user search in progress\n"));
1833 if (!pdb_setsampwent(False, acct_flags)) {
1834 DEBUG(5, ("Could not start search\n"));
1838 user_search_in_progress = True;
1840 state = TALLOC_P(search->mem_ctx, struct user_search);
1841 if (state == NULL) {
1842 DEBUG(0, ("talloc failed\n"));
1846 state->acct_flags = acct_flags;
1848 search->private_data = state;
1849 search->next_entry = next_entry_users;
1850 search->search_end = search_end_users;
1854 struct group_search {
1856 size_t num_groups, current_group;
1859 static bool next_entry_groups(struct pdb_search *s,
1860 struct samr_displayentry *entry)
1862 struct group_search *state = (struct group_search *)s->private_data;
1864 GROUP_MAP *map = &state->groups[state->current_group];
1866 if (state->current_group == state->num_groups)
1869 sid_peek_rid(&map->sid, &rid);
1871 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1874 state->current_group += 1;
1878 static void search_end_groups(struct pdb_search *search)
1880 struct group_search *state =
1881 (struct group_search *)search->private_data;
1882 SAFE_FREE(state->groups);
1885 static bool pdb_search_grouptype(struct pdb_search *search,
1886 const DOM_SID *sid, enum lsa_SidType type)
1888 struct group_search *state;
1890 state = TALLOC_P(search->mem_ctx, struct group_search);
1891 if (state == NULL) {
1892 DEBUG(0, ("talloc failed\n"));
1896 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1898 DEBUG(0, ("Could not enum groups\n"));
1902 state->current_group = 0;
1903 search->private_data = state;
1904 search->next_entry = next_entry_groups;
1905 search->search_end = search_end_groups;
1909 static bool pdb_default_search_groups(struct pdb_methods *methods,
1910 struct pdb_search *search)
1912 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1915 static bool pdb_default_search_aliases(struct pdb_methods *methods,
1916 struct pdb_search *search,
1920 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1923 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1926 if (idx < search->num_entries)
1927 return &search->cache[idx];
1929 if (search->search_ended)
1932 while (idx >= search->num_entries) {
1933 struct samr_displayentry entry;
1935 if (!search->next_entry(search, &entry)) {
1936 search->search_end(search);
1937 search->search_ended = True;
1941 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1942 entry, &search->cache, &search->num_entries,
1943 &search->cache_size);
1946 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1949 struct pdb_search *pdb_search_users(uint32 acct_flags)
1951 struct pdb_methods *pdb = pdb_get_methods();
1952 struct pdb_search *result;
1954 result = pdb_search_init(PDB_USER_SEARCH);
1955 if (result == NULL) {
1959 if (!pdb->search_users(pdb, result, acct_flags)) {
1960 talloc_destroy(result->mem_ctx);
1966 struct pdb_search *pdb_search_groups(void)
1968 struct pdb_methods *pdb = pdb_get_methods();
1969 struct pdb_search *result;
1971 result = pdb_search_init(PDB_GROUP_SEARCH);
1972 if (result == NULL) {
1976 if (!pdb->search_groups(pdb, result)) {
1977 talloc_destroy(result->mem_ctx);
1983 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1985 struct pdb_methods *pdb = pdb_get_methods();
1986 struct pdb_search *result;
1988 if (pdb == NULL) return NULL;
1990 result = pdb_search_init(PDB_ALIAS_SEARCH);
1991 if (result == NULL) return NULL;
1993 if (!pdb->search_aliases(pdb, result, sid)) {
1994 talloc_destroy(result->mem_ctx);
2000 uint32 pdb_search_entries(struct pdb_search *search,
2001 uint32 start_idx, uint32 max_entries,
2002 struct samr_displayentry **result)
2004 struct samr_displayentry *end_entry;
2005 uint32 end_idx = start_idx+max_entries-1;
2007 /* The first entry needs to be searched after the last. Otherwise the
2008 * first entry might have moved due to a realloc during the search for
2009 * the last entry. */
2011 end_entry = pdb_search_getentry(search, end_idx);
2012 *result = pdb_search_getentry(search, start_idx);
2014 if (end_entry != NULL)
2017 if (start_idx >= search->num_entries)
2020 return search->num_entries - start_idx;
2023 void pdb_search_destroy(struct pdb_search *search)
2028 if (!search->search_ended)
2029 search->search_end(search);
2031 talloc_destroy(search->mem_ctx);
2034 /*******************************************************************
2036 *******************************************************************/
2038 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, DOM_SID *sid,
2039 time_t *pass_last_set_time)
2041 struct pdb_methods *pdb = pdb_get_methods();
2042 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2043 pass_last_set_time);
2046 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2049 struct pdb_methods *pdb = pdb_get_methods();
2050 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2053 bool pdb_del_trusteddom_pw(const char *domain)
2055 struct pdb_methods *pdb = pdb_get_methods();
2056 return pdb->del_trusteddom_pw(pdb, domain);
2059 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32 *num_domains,
2060 struct trustdom_info ***domains)
2062 struct pdb_methods *pdb = pdb_get_methods();
2063 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2066 /*******************************************************************
2067 the defaults for trustdom methods:
2068 these simply call the original passdb/secrets.c actions,
2069 to be replaced by pdb_ldap.
2070 *******************************************************************/
2072 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2076 time_t *pass_last_set_time)
2078 return secrets_fetch_trusted_domain_password(domain, pwd,
2079 sid, pass_last_set_time);
2083 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2088 return secrets_store_trusted_domain_password(domain, pwd, sid);
2091 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2094 return trusted_domain_password_delete(domain);
2097 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2098 TALLOC_CTX *mem_ctx,
2099 uint32 *num_domains,
2100 struct trustdom_info ***domains)
2102 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2105 /*******************************************************************
2106 Create a pdb_methods structure and initialize it with the default
2107 operations. In this way a passdb module can simply implement
2108 the functionality it cares about. However, normally this is done
2109 in groups of related functions.
2110 *******************************************************************/
2112 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2114 /* allocate memory for the structure as its own talloc CTX */
2116 if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
2117 return NT_STATUS_NO_MEMORY;
2120 (*methods)->setsampwent = pdb_default_setsampwent;
2121 (*methods)->endsampwent = pdb_default_endsampwent;
2122 (*methods)->getsampwent = pdb_default_getsampwent;
2123 (*methods)->getsampwnam = pdb_default_getsampwnam;
2124 (*methods)->getsampwsid = pdb_default_getsampwsid;
2125 (*methods)->create_user = pdb_default_create_user;
2126 (*methods)->delete_user = pdb_default_delete_user;
2127 (*methods)->add_sam_account = pdb_default_add_sam_account;
2128 (*methods)->update_sam_account = pdb_default_update_sam_account;
2129 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2130 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2131 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2133 (*methods)->getgrsid = pdb_default_getgrsid;
2134 (*methods)->getgrgid = pdb_default_getgrgid;
2135 (*methods)->getgrnam = pdb_default_getgrnam;
2136 (*methods)->create_dom_group = pdb_default_create_dom_group;
2137 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2138 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2139 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2140 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2141 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2142 (*methods)->enum_group_members = pdb_default_enum_group_members;
2143 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2144 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2145 (*methods)->add_groupmem = pdb_default_add_groupmem;
2146 (*methods)->del_groupmem = pdb_default_del_groupmem;
2147 (*methods)->create_alias = pdb_default_create_alias;
2148 (*methods)->delete_alias = pdb_default_delete_alias;
2149 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2150 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2151 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2152 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2153 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2154 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2155 (*methods)->lookup_rids = pdb_default_lookup_rids;
2156 (*methods)->get_account_policy = pdb_default_get_account_policy;
2157 (*methods)->set_account_policy = pdb_default_set_account_policy;
2158 (*methods)->get_seq_num = pdb_default_get_seq_num;
2159 (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2160 (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2161 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2162 (*methods)->sid_to_id = pdb_default_sid_to_id;
2164 (*methods)->search_users = pdb_default_search_users;
2165 (*methods)->search_groups = pdb_default_search_groups;
2166 (*methods)->search_aliases = pdb_default_search_aliases;
2168 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2169 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2170 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2171 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2173 return NT_STATUS_OK;