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/>.
24 #include "system/passwd.h"
26 #include "../librpc/gen_ndr/samr.h"
28 #include "nsswitch/winbind_client.h"
29 #include "../libcli/security/security.h"
30 #include "../lib/util/util_pw.h"
33 #define DBGC_CLASS DBGC_PASSDB
37 static struct pdb_init_function_entry *backends = NULL;
39 static void lazy_initialize_passdb(void)
41 static bool initialized = False;
49 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
51 enum lsa_SidType *psid_name_use,
52 union unid_t *unix_id);
54 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
56 struct pdb_init_function_entry *entry = backends;
58 if(version != PASSDB_INTERFACE_VERSION) {
59 DEBUG(0,("Can't register passdb backend!\n"
60 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
61 "while this version of samba uses version %d\n",
62 version,PASSDB_INTERFACE_VERSION));
63 return NT_STATUS_OBJECT_TYPE_MISMATCH;
67 return NT_STATUS_INVALID_PARAMETER;
70 DEBUG(5,("Attempting to register passdb backend %s\n", name));
72 /* Check for duplicates */
73 if (pdb_find_backend_entry(name)) {
74 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
75 return NT_STATUS_OBJECT_NAME_COLLISION;
78 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
79 entry->name = smb_xstrdup(name);
82 DLIST_ADD(backends, entry);
83 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
87 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
89 struct pdb_init_function_entry *entry = backends;
92 if (strcmp(entry->name, name)==0) return entry;
100 * The event context for the passdb backend. I know this is a bad hack and yet
101 * another static variable, but our pdb API is a global thing per
102 * definition. The first use for this is the LDAP idle function, more might be
105 * I don't feel too bad about this static variable, it replaces the
106 * smb_idle_event_list that used to exist in lib/module.c. -- VL
109 static struct event_context *pdb_event_ctx;
111 struct event_context *pdb_get_event_context(void)
113 return pdb_event_ctx;
116 /******************************************************************
117 Make a pdb_methods from scratch
118 *******************************************************************/
120 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
122 char *module_name = smb_xstrdup(selected);
123 char *module_location = NULL, *p;
124 struct pdb_init_function_entry *entry;
125 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
127 lazy_initialize_passdb();
129 p = strchr(module_name, ':');
133 module_location = p+1;
134 trim_char(module_location, ' ', ' ');
137 trim_char(module_name, ' ', ' ');
140 DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected, module_name));
142 entry = pdb_find_backend_entry(module_name);
144 /* Try to find a module that contains this module */
146 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
147 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
148 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
149 SAFE_FREE(module_name);
150 return NT_STATUS_UNSUCCESSFUL;
154 /* No such backend found */
156 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
157 SAFE_FREE(module_name);
158 return NT_STATUS_INVALID_PARAMETER;
161 DEBUG(5,("Found pdb backend %s\n", module_name));
163 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
164 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
165 selected, nt_errstr(nt_status)));
166 SAFE_FREE(module_name);
170 SAFE_FREE(module_name);
172 DEBUG(5,("pdb backend %s has a valid init\n", selected));
177 /******************************************************************
178 Return an already initialized pdb_methods structure
179 *******************************************************************/
181 static struct pdb_methods *pdb_get_methods_reload( bool reload )
183 static struct pdb_methods *pdb = NULL;
185 if ( pdb && reload ) {
186 pdb->free_private_data( &(pdb->private_data) );
187 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
189 if (asprintf(&msg, "pdb_get_methods_reload: "
190 "failed to get pdb methods for backend %s\n",
191 lp_passdb_backend()) > 0) {
194 smb_panic("pdb_get_methods_reload");
200 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
202 if (asprintf(&msg, "pdb_get_methods_reload: "
203 "failed to get pdb methods for backend %s\n",
204 lp_passdb_backend()) > 0) {
207 smb_panic("pdb_get_methods_reload");
215 static struct pdb_methods *pdb_get_methods(void)
217 return pdb_get_methods_reload(False);
220 struct pdb_domain_info *pdb_get_domain_info(TALLOC_CTX *mem_ctx)
222 struct pdb_methods *pdb = pdb_get_methods();
223 return pdb->get_domain_info(pdb, mem_ctx);
227 * @brief Check if the user account has been locked out and try to unlock it.
229 * If the user has been automatically locked out and a lockout duration is set,
230 * then check if we can unlock the account and reset the bad password values.
232 * @param[in] sampass The sam user to check.
234 * @return True if the function was successfull, false on an error.
236 static bool pdb_try_account_unlock(struct samu *sampass)
238 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
240 if ((acb_info & ACB_NORMAL) && (acb_info & ACB_AUTOLOCK)) {
241 uint32_t lockout_duration;
242 time_t bad_password_time;
243 time_t now = time(NULL);
246 ok = pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
249 DEBUG(0, ("pdb_try_account_unlock: "
250 "pdb_get_account_policy failed.\n"));
254 if (lockout_duration == (uint32_t) -1 ||
255 lockout_duration == 0) {
256 DEBUG(9, ("pdb_try_account_unlock: No reset duration, "
257 "can't reset autolock\n"));
260 lockout_duration *= 60;
262 bad_password_time = pdb_get_bad_password_time(sampass);
263 if (bad_password_time == (time_t) 0) {
264 DEBUG(2, ("pdb_try_account_unlock: Account %s "
265 "administratively locked out "
266 "with no bad password "
267 "time. Leaving locked out.\n",
268 pdb_get_username(sampass)));
272 if ((bad_password_time +
273 convert_uint32_t_to_time_t(lockout_duration)) < now) {
276 pdb_set_acct_ctrl(sampass, acb_info & ~ACB_AUTOLOCK,
278 pdb_set_bad_password_count(sampass, 0, PDB_CHANGED);
279 pdb_set_bad_password_time(sampass, 0, PDB_CHANGED);
282 status = pdb_update_sam_account(sampass);
284 if (!NT_STATUS_IS_OK(status)) {
285 DEBUG(0, ("_samr_OpenUser: Couldn't "
286 "update account %s - %s\n",
287 pdb_get_username(sampass),
298 * @brief Get a sam user structure by the given username.
300 * This functions also checks if the account has been automatically locked out
301 * and unlocks it if a lockout duration time has been defined and the time has
304 * @param[in] sam_acct The sam user structure to fill.
306 * @param[in] username The username to look for.
308 * @return True on success, false on error.
310 bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
312 struct pdb_methods *pdb = pdb_get_methods();
313 struct samu *for_cache;
314 const struct dom_sid *user_sid;
318 status = pdb->getsampwnam(pdb, sam_acct, username);
319 if (!NT_STATUS_IS_OK(status)) {
323 ok = pdb_try_account_unlock(sam_acct);
325 DEBUG(1, ("pdb_getsampwnam: Failed to unlock account %s\n",
329 for_cache = samu_new(NULL);
330 if (for_cache == NULL) {
334 if (!pdb_copy_sam_account(for_cache, sam_acct)) {
335 TALLOC_FREE(for_cache);
339 user_sid = pdb_get_user_sid(for_cache);
341 memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
342 data_blob_const(user_sid, sizeof(*user_sid)),
348 /**********************************************************************
349 **********************************************************************/
351 static bool guest_user_info( struct samu *user )
355 const char *guestname = lp_guestaccount();
357 pwd = Get_Pwnam_alloc(talloc_tos(), guestname);
359 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
364 result = samu_set_unix(user, pwd );
368 return NT_STATUS_IS_OK( result );
372 * @brief Get a sam user structure by the given username.
374 * This functions also checks if the account has been automatically locked out
375 * and unlocks it if a lockout duration time has been defined and the time has
379 * @param[in] sam_acct The sam user structure to fill.
381 * @param[in] sid The user SDI to look up.
383 * @return True on success, false on error.
385 bool pdb_getsampwsid(struct samu *sam_acct, const struct dom_sid *sid)
387 struct pdb_methods *pdb = pdb_get_methods();
392 /* hard code the Guest RID of 501 */
394 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
397 if ( rid == DOMAIN_RID_GUEST ) {
398 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
399 return guest_user_info( sam_acct );
402 /* check the cache first */
404 cache_data = memcache_lookup_talloc(
405 NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
407 if (cache_data != NULL) {
408 struct samu *cache_copy = talloc_get_type_abort(
409 cache_data, struct samu);
411 ok = pdb_copy_sam_account(sam_acct, cache_copy);
413 ok = NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
420 ok = pdb_try_account_unlock(sam_acct);
422 DEBUG(1, ("pdb_getsampwsid: Failed to unlock account %s\n",
423 sam_acct->username));
429 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
430 TALLOC_CTX *tmp_ctx, const char *name,
431 uint32_t acb_info, uint32_t *rid)
433 struct samu *sam_pass;
437 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
438 return NT_STATUS_NO_MEMORY;
441 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
442 char *add_script = NULL;
446 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
447 add_script = talloc_strdup(tmp_ctx,
448 lp_adduser_script());
450 add_script = talloc_strdup(tmp_ctx,
451 lp_addmachine_script());
454 if (!add_script || add_script[0] == '\0') {
455 DEBUG(3, ("Could not find user %s and no add script "
457 return NT_STATUS_NO_SUCH_USER;
460 /* lowercase the username before creating the Unix account for
461 compatibility with previous Samba releases */
462 fstrcpy( name2, name );
464 add_script = talloc_all_string_sub(tmp_ctx,
469 return NT_STATUS_NO_MEMORY;
471 add_ret = smbrun(add_script,NULL);
472 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
473 add_script, add_ret));
475 smb_nscd_flush_user_cache();
480 pwd = Get_Pwnam_alloc(tmp_ctx, name);
483 DEBUG(3, ("Could not find user %s, add script did not work\n", name));
484 return NT_STATUS_NO_SUCH_USER;
488 /* we have a valid SID coming out of this call */
490 status = samu_alloc_rid_unix( sam_pass, pwd );
494 if (!NT_STATUS_IS_OK(status)) {
495 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
499 if (!sid_peek_check_rid(get_global_sam_sid(),
500 pdb_get_user_sid(sam_pass), rid)) {
501 DEBUG(0, ("Could not get RID of fresh user\n"));
502 return NT_STATUS_INTERNAL_ERROR;
505 /* Use the username case specified in the original request */
507 pdb_set_username( sam_pass, name, PDB_SET );
509 /* Disable the account on creation, it does not have a reasonable password yet. */
511 acb_info |= ACB_DISABLED;
513 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
515 status = pdb_add_sam_account(sam_pass);
517 TALLOC_FREE(sam_pass);
522 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32_t flags,
525 struct pdb_methods *pdb = pdb_get_methods();
526 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
529 /****************************************************************************
530 Delete a UNIX user on demand.
531 ****************************************************************************/
533 static int smb_delete_user(const char *unix_user)
535 char *del_script = NULL;
540 if ( strequal( unix_user, "root" ) ) {
541 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
545 del_script = talloc_strdup(talloc_tos(), lp_deluser_script());
546 if (!del_script || !*del_script) {
549 del_script = talloc_all_string_sub(talloc_tos(),
556 ret = smbrun(del_script,NULL);
559 smb_nscd_flush_user_cache();
561 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
566 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
568 struct samu *sam_acct)
573 status = pdb_delete_sam_account(sam_acct);
574 if (!NT_STATUS_IS_OK(status)) {
579 * Now delete the unix side ....
580 * note: we don't check if the delete really happened as the script is
581 * not necessary present and maybe the sysadmin doesn't want to delete
585 /* always lower case the username before handing it off to
588 fstrcpy( username, pdb_get_username(sam_acct) );
589 strlower_m( username );
591 smb_delete_user( username );
596 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
598 struct pdb_methods *pdb = pdb_get_methods();
601 /* sanity check to make sure we don't delete root */
603 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
604 return NT_STATUS_NO_SUCH_USER;
608 return NT_STATUS_ACCESS_DENIED;
611 return pdb->delete_user(pdb, mem_ctx, sam_acct);
614 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
616 struct pdb_methods *pdb = pdb_get_methods();
617 return pdb->add_sam_account(pdb, sam_acct);
620 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
622 struct pdb_methods *pdb = pdb_get_methods();
624 memcache_flush(NULL, PDB_GETPWSID_CACHE);
626 return pdb->update_sam_account(pdb, sam_acct);
629 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
631 struct pdb_methods *pdb = pdb_get_methods();
633 memcache_flush(NULL, PDB_GETPWSID_CACHE);
635 return pdb->delete_sam_account(pdb, sam_acct);
638 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
640 struct pdb_methods *pdb = pdb_get_methods();
644 memcache_flush(NULL, PDB_GETPWSID_CACHE);
646 /* sanity check to make sure we don't rename root */
648 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
649 return NT_STATUS_NO_SUCH_USER;
653 return NT_STATUS_ACCESS_DENIED;
656 status = pdb->rename_sam_account(pdb, oldname, newname);
658 /* always flush the cache here just to be safe */
664 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
666 struct pdb_methods *pdb = pdb_get_methods();
667 return pdb->update_login_attempts(pdb, sam_acct, success);
670 bool pdb_getgrsid(GROUP_MAP *map, struct dom_sid sid)
672 struct pdb_methods *pdb = pdb_get_methods();
673 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
676 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
678 struct pdb_methods *pdb = pdb_get_methods();
679 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
682 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
684 struct pdb_methods *pdb = pdb_get_methods();
685 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
688 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
693 struct dom_sid group_sid;
697 grp = getgrnam(name);
702 if (smb_create_group(name, &gid) != 0) {
703 return NT_STATUS_ACCESS_DENIED;
710 return NT_STATUS_ACCESS_DENIED;
713 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
714 if (!pdb_new_rid(rid)) {
715 return NT_STATUS_ACCESS_DENIED;
718 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
721 sid_compose(&group_sid, get_global_sam_sid(), *rid);
723 return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
724 SID_NAME_DOM_GRP, name, NULL);
727 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
730 struct pdb_methods *pdb = pdb_get_methods();
731 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
734 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
738 struct dom_sid group_sid;
742 const char *grp_name;
745 map.gid = (gid_t) -1;
747 sid_compose(&group_sid, get_global_sam_sid(), rid);
749 if (!get_domain_group_from_sid(group_sid, &map)) {
750 DEBUG(10, ("Could not find group for rid %d\n", rid));
751 return NT_STATUS_NO_SUCH_GROUP;
754 /* We need the group name for the smb_delete_group later on */
756 if (map.gid == (gid_t)-1) {
757 return NT_STATUS_NO_SUCH_GROUP;
760 grp = getgrgid(map.gid);
762 return NT_STATUS_NO_SUCH_GROUP;
765 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
767 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
768 if (grp_name == NULL) {
769 return NT_STATUS_NO_MEMORY;
772 status = pdb_delete_group_mapping_entry(group_sid);
774 if (!NT_STATUS_IS_OK(status)) {
778 /* Don't check the result of smb_delete_group */
780 smb_delete_group(grp_name);
785 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32_t rid)
787 struct pdb_methods *pdb = pdb_get_methods();
788 return pdb->delete_dom_group(pdb, mem_ctx, rid);
791 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
793 struct pdb_methods *pdb = pdb_get_methods();
794 return pdb->add_group_mapping_entry(pdb, map);
797 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
799 struct pdb_methods *pdb = pdb_get_methods();
800 return pdb->update_group_mapping_entry(pdb, map);
803 NTSTATUS pdb_delete_group_mapping_entry(struct dom_sid sid)
805 struct pdb_methods *pdb = pdb_get_methods();
806 return pdb->delete_group_mapping_entry(pdb, sid);
809 bool pdb_enum_group_mapping(const struct dom_sid *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
810 size_t *p_num_entries, bool unix_only)
812 struct pdb_methods *pdb = pdb_get_methods();
813 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
814 pp_rmap, p_num_entries, unix_only));
817 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
818 const struct dom_sid *sid,
819 uint32_t **pp_member_rids,
820 size_t *p_num_members)
822 struct pdb_methods *pdb = pdb_get_methods();
825 result = pdb->enum_group_members(pdb, mem_ctx,
826 sid, pp_member_rids, p_num_members);
828 /* special check for rid 513 */
830 if ( !NT_STATUS_IS_OK( result ) ) {
833 sid_peek_rid( sid, &rid );
835 if ( rid == DOMAIN_RID_USERS ) {
837 *pp_member_rids = NULL;
846 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
847 struct dom_sid **pp_sids, gid_t **pp_gids,
848 uint32_t *p_num_groups)
850 struct pdb_methods *pdb = pdb_get_methods();
851 return pdb->enum_group_memberships(
853 pp_sids, pp_gids, p_num_groups);
856 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
858 struct samu *sampass)
863 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
864 (grp = getgrgid(gid)) == NULL) {
865 return NT_STATUS_INVALID_PRIMARY_GROUP;
868 if (smb_set_primary_group(grp->gr_name,
869 pdb_get_username(sampass)) != 0) {
870 return NT_STATUS_ACCESS_DENIED;
876 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
878 struct pdb_methods *pdb = pdb_get_methods();
879 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
883 * Helper function to see whether a user is in a group. We can't use
884 * user_in_group_sid here because this creates dependencies only smbd can
888 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
889 const struct dom_sid *group_sid)
891 struct dom_sid *sids;
893 uint32_t i, num_groups;
895 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
901 for (i=0; i<num_groups; i++) {
902 if (dom_sid_equal(group_sid, &sids[i])) {
909 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
914 struct dom_sid group_sid, member_sid;
915 struct samu *account = NULL;
919 const char *group_name;
923 map.gid = (gid_t) -1;
925 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
926 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
928 if (!get_domain_group_from_sid(group_sid, &map) ||
929 (map.gid == (gid_t)-1) ||
930 ((grp = getgrgid(map.gid)) == NULL)) {
931 return NT_STATUS_NO_SUCH_GROUP;
934 group_name = talloc_strdup(mem_ctx, grp->gr_name);
935 if (group_name == NULL) {
936 return NT_STATUS_NO_MEMORY;
939 if ( !(account = samu_new( NULL )) ) {
940 return NT_STATUS_NO_MEMORY;
943 if (!pdb_getsampwsid(account, &member_sid) ||
944 !sid_to_uid(&member_sid, &uid) ||
945 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
946 return NT_STATUS_NO_SUCH_USER;
949 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
950 return NT_STATUS_MEMBER_IN_GROUP;
954 * ok, the group exist, the user exist, the user is not in the group,
955 * we can (finally) add it to the group !
958 smb_add_user_group(group_name, pwd->pw_name);
960 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
961 return NT_STATUS_ACCESS_DENIED;
967 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
970 struct pdb_methods *pdb = pdb_get_methods();
971 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
974 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
979 struct dom_sid group_sid, member_sid;
980 struct samu *account = NULL;
984 const char *group_name;
987 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
988 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
990 if (!get_domain_group_from_sid(group_sid, &map) ||
991 (map.gid == (gid_t)-1) ||
992 ((grp = getgrgid(map.gid)) == NULL)) {
993 return NT_STATUS_NO_SUCH_GROUP;
996 group_name = talloc_strdup(mem_ctx, grp->gr_name);
997 if (group_name == NULL) {
998 return NT_STATUS_NO_MEMORY;
1001 if ( !(account = samu_new( NULL )) ) {
1002 return NT_STATUS_NO_MEMORY;
1005 if (!pdb_getsampwsid(account, &member_sid) ||
1006 !sid_to_uid(&member_sid, &uid) ||
1007 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
1008 return NT_STATUS_NO_SUCH_USER;
1011 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1012 return NT_STATUS_MEMBER_NOT_IN_GROUP;
1016 * ok, the group exist, the user exist, the user is in the group,
1017 * we can (finally) delete it from the group!
1020 smb_delete_user_group(group_name, pwd->pw_name);
1022 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1023 return NT_STATUS_ACCESS_DENIED;
1026 return NT_STATUS_OK;
1029 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1030 uint32_t member_rid)
1032 struct pdb_methods *pdb = pdb_get_methods();
1033 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
1036 NTSTATUS pdb_create_alias(const char *name, uint32_t *rid)
1038 struct pdb_methods *pdb = pdb_get_methods();
1039 return pdb->create_alias(pdb, name, rid);
1042 NTSTATUS pdb_delete_alias(const struct dom_sid *sid)
1044 struct pdb_methods *pdb = pdb_get_methods();
1045 return pdb->delete_alias(pdb, sid);
1048 NTSTATUS pdb_get_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1050 struct pdb_methods *pdb = pdb_get_methods();
1051 return pdb->get_aliasinfo(pdb, sid, info);
1054 NTSTATUS pdb_set_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1056 struct pdb_methods *pdb = pdb_get_methods();
1057 return pdb->set_aliasinfo(pdb, sid, info);
1060 NTSTATUS pdb_add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1062 struct pdb_methods *pdb = pdb_get_methods();
1063 return pdb->add_aliasmem(pdb, alias, member);
1066 NTSTATUS pdb_del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1068 struct pdb_methods *pdb = pdb_get_methods();
1069 return pdb->del_aliasmem(pdb, alias, member);
1072 NTSTATUS pdb_enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
1073 struct dom_sid **pp_members, size_t *p_num_members)
1075 struct pdb_methods *pdb = pdb_get_methods();
1076 return pdb->enum_aliasmem(pdb, alias, mem_ctx, pp_members,
1080 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1081 const struct dom_sid *domain_sid,
1082 const struct dom_sid *members, size_t num_members,
1083 uint32_t **pp_alias_rids,
1084 size_t *p_num_alias_rids)
1086 struct pdb_methods *pdb = pdb_get_methods();
1087 return pdb->enum_alias_memberships(pdb, mem_ctx,
1089 members, num_members,
1094 NTSTATUS pdb_lookup_rids(const struct dom_sid *domain_sid,
1098 enum lsa_SidType *attrs)
1100 struct pdb_methods *pdb = pdb_get_methods();
1101 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
1105 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
1106 * in the samba code.
1107 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
1108 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
1109 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
1110 * down to are pdb_getsampwnam and pdb_getgrnam instead of
1112 * But in principle, it the call belongs to the API and might get
1113 * used in this context some day.
1116 NTSTATUS pdb_lookup_names(const struct dom_sid *domain_sid,
1120 enum lsa_SidType *attrs)
1122 struct pdb_methods *pdb = pdb_get_methods();
1123 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
1127 bool pdb_get_account_policy(enum pdb_policy_type type, uint32_t *value)
1129 struct pdb_methods *pdb = pdb_get_methods();
1133 status = pdb->get_account_policy(pdb, type, value);
1136 return NT_STATUS_IS_OK(status);
1139 bool pdb_set_account_policy(enum pdb_policy_type type, uint32_t value)
1141 struct pdb_methods *pdb = pdb_get_methods();
1145 status = pdb->set_account_policy(pdb, type, value);
1148 return NT_STATUS_IS_OK(status);
1151 bool pdb_get_seq_num(time_t *seq_num)
1153 struct pdb_methods *pdb = pdb_get_methods();
1154 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1157 bool pdb_uid_to_sid(uid_t uid, struct dom_sid *sid)
1159 struct pdb_methods *pdb = pdb_get_methods();
1160 return pdb->uid_to_sid(pdb, uid, sid);
1163 bool pdb_gid_to_sid(gid_t gid, struct dom_sid *sid)
1165 struct pdb_methods *pdb = pdb_get_methods();
1166 return pdb->gid_to_sid(pdb, gid, sid);
1169 bool pdb_sid_to_id(const struct dom_sid *sid, union unid_t *id,
1170 enum lsa_SidType *type)
1172 struct pdb_methods *pdb = pdb_get_methods();
1173 return pdb->sid_to_id(pdb, sid, id, type);
1176 uint32_t pdb_capabilities(void)
1178 struct pdb_methods *pdb = pdb_get_methods();
1179 return pdb->capabilities(pdb);
1182 /********************************************************************
1183 Allocate a new RID from the passdb backend. Verify that it is free
1184 by calling lookup_global_sam_rid() to verify that the RID is not
1185 in use. This handles servers that have existing users or groups
1186 with add RIDs (assigned from previous algorithmic mappings)
1187 ********************************************************************/
1189 bool pdb_new_rid(uint32_t *rid)
1191 struct pdb_methods *pdb = pdb_get_methods();
1192 const char *name = NULL;
1193 enum lsa_SidType type;
1194 uint32_t allocated_rid = 0;
1198 if ((pdb_capabilities() & PDB_CAP_STORE_RIDS) == 0) {
1199 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1204 if (algorithmic_rid_base() != BASE_RID) {
1205 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1206 "without algorithmic RIDs is chosen.\n"));
1207 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1208 "add', set the maximum used RID\n"));
1209 DEBUGADD(0, ("and remove the parameter\n"));
1213 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1214 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1218 /* Attempt to get an unused RID (max tires is 250...yes that it is
1219 and arbitrary number I pulkled out of my head). -- jerry */
1221 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1224 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1228 /* validate that the RID is not in use */
1230 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1237 if ( allocated_rid == 0 ) {
1238 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1242 *rid = allocated_rid;
1247 /***************************************************************
1248 Initialize the static context (at smbd startup etc).
1250 If uninitialised, context will auto-init on first use.
1251 ***************************************************************/
1253 bool initialize_password_db(bool reload, struct event_context *event_ctx)
1255 pdb_event_ctx = event_ctx;
1256 return (pdb_get_methods_reload(reload) != NULL);
1260 /***************************************************************************
1261 Default implementations of some functions.
1262 ****************************************************************************/
1264 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1266 return NT_STATUS_NO_SUCH_USER;
1269 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1271 return NT_STATUS_NO_SUCH_USER;
1274 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1276 return NT_STATUS_NOT_IMPLEMENTED;
1279 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1281 return NT_STATUS_NOT_IMPLEMENTED;
1284 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1286 return NT_STATUS_NOT_IMPLEMENTED;
1289 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1291 return NT_STATUS_NOT_IMPLEMENTED;
1294 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1296 /* Only the pdb_nds backend implements this, by
1297 * default just return ok. */
1298 return NT_STATUS_OK;
1301 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t *value)
1303 return account_policy_get(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1306 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t value)
1308 return account_policy_set(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1311 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1313 *seq_num = time(NULL);
1314 return NT_STATUS_OK;
1317 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1318 struct dom_sid *sid)
1320 struct samu *sampw = NULL;
1321 struct passwd *unix_pw;
1324 unix_pw = sys_getpwuid( uid );
1327 DEBUG(4,("pdb_default_uid_to_sid: host has no idea of uid "
1328 "%lu\n", (unsigned long)uid));
1332 if ( !(sampw = samu_new( NULL )) ) {
1333 DEBUG(0,("pdb_default_uid_to_sid: samu_new() failed!\n"));
1338 ret = NT_STATUS_IS_OK(
1339 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1343 DEBUG(5, ("pdb_default_uid_to_sid: Did not find user "
1344 "%s (%u)\n", unix_pw->pw_name, (unsigned int)uid));
1349 sid_copy(sid, pdb_get_user_sid(sampw));
1356 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1357 struct dom_sid *sid)
1361 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1365 sid_copy(sid, &map.sid);
1369 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1370 const struct dom_sid *sid,
1371 union unid_t *id, enum lsa_SidType *type)
1373 TALLOC_CTX *mem_ctx;
1378 mem_ctx = talloc_new(NULL);
1380 if (mem_ctx == NULL) {
1381 DEBUG(0, ("talloc_new failed\n"));
1385 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1386 /* Here we might have users as well as groups and aliases */
1387 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1391 /* check for "Unix User" */
1393 if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1395 *type = SID_NAME_USER;
1400 /* check for "Unix Group" */
1402 if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1404 *type = SID_NAME_ALIAS;
1411 if (sid_check_is_in_builtin(sid) ||
1412 sid_check_is_in_wellknown_domain(sid)) {
1413 /* Here we only have aliases */
1415 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1416 DEBUG(10, ("Could not find map for sid %s\n",
1417 sid_string_dbg(sid)));
1420 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1421 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1422 DEBUG(10, ("Map for sid %s is a %s, expected an "
1423 "alias\n", sid_string_dbg(sid),
1424 sid_type_lookup(map.sid_name_use)));
1429 *type = SID_NAME_ALIAS;
1434 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1435 sid_string_dbg(sid)));
1439 TALLOC_FREE(mem_ctx);
1443 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, uint32_t *p_num)
1454 /* We only look at our own sam, so don't care about imported stuff */
1455 winbind_env = winbind_env_set();
1456 (void)winbind_off();
1458 if ((grp = getgrgid(gid)) == NULL) {
1459 /* allow winbindd lookups, but only if they weren't already disabled */
1463 /* Primary group members */
1465 while ((pwd = getpwent()) != NULL) {
1466 if (pwd->pw_gid == gid) {
1467 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1475 /* Secondary group members */
1476 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1477 struct passwd *pw = getpwnam(*gr);
1481 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1490 /* allow winbindd lookups, but only if they weren't already disabled */
1498 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1499 TALLOC_CTX *mem_ctx,
1500 const struct dom_sid *group,
1501 uint32_t **pp_member_rids,
1502 size_t *p_num_members)
1506 uint32_t i, num_uids;
1508 *pp_member_rids = NULL;
1511 if (!sid_to_gid(group, &gid))
1512 return NT_STATUS_NO_SUCH_GROUP;
1514 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1515 return NT_STATUS_NO_SUCH_GROUP;
1518 return NT_STATUS_OK;
1520 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32_t, num_uids);
1522 for (i=0; i<num_uids; i++) {
1525 uid_to_sid(&sid, uids[i]);
1527 if (!sid_check_is_in_our_domain(&sid)) {
1528 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1529 "in our domain\n"));
1533 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1534 *p_num_members += 1;
1537 return NT_STATUS_OK;
1540 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1541 TALLOC_CTX *mem_ctx,
1543 struct dom_sid **pp_sids,
1545 uint32_t *p_num_groups)
1550 const char *username = pdb_get_username(user);
1553 /* Ignore the primary group SID. Honor the real Unix primary group.
1554 The primary group SID is only of real use to Windows clients */
1556 if ( !(pw = Get_Pwnam_alloc(mem_ctx, username)) ) {
1557 return NT_STATUS_NO_SUCH_USER;
1564 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1565 return NT_STATUS_NO_SUCH_USER;
1568 if (*p_num_groups == 0) {
1569 smb_panic("primary group missing");
1572 *pp_sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, *p_num_groups);
1574 if (*pp_sids == NULL) {
1575 TALLOC_FREE(*pp_gids);
1576 return NT_STATUS_NO_MEMORY;
1579 for (i=0; i<*p_num_groups; i++) {
1580 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1583 return NT_STATUS_OK;
1586 /*******************************************************************
1587 Look up a rid in the SAM we're responsible for (i.e. passdb)
1588 ********************************************************************/
1590 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32_t rid,
1592 enum lsa_SidType *psid_name_use,
1593 union unid_t *unix_id)
1595 struct samu *sam_account = NULL;
1600 *psid_name_use = SID_NAME_UNKNOWN;
1602 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1603 (unsigned int)rid));
1605 sid_compose(&sid, get_global_sam_sid(), rid);
1607 /* see if the passdb can help us with the name of the user */
1609 if ( !(sam_account = samu_new( NULL )) ) {
1613 /* BEING ROOT BLOCK */
1615 if (pdb_getsampwsid(sam_account, &sid)) {
1618 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1619 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1621 TALLOC_FREE(sam_account);
1625 *psid_name_use = SID_NAME_USER;
1627 TALLOC_FREE(sam_account);
1629 if (unix_id == NULL) {
1633 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1637 unix_id->uid = pw->pw_uid;
1641 TALLOC_FREE(sam_account);
1643 ret = pdb_getgrsid(&map, sid);
1645 /* END BECOME_ROOT BLOCK */
1647 /* do not resolve SIDs to a name unless there is a valid
1648 gid associated with it */
1650 if ( ret && (map.gid != (gid_t)-1) ) {
1651 *name = talloc_strdup(mem_ctx, map.nt_name);
1652 *psid_name_use = map.sid_name_use;
1655 unix_id->gid = map.gid;
1661 /* Windows will always map RID 513 to something. On a non-domain
1662 controller, this gets mapped to SERVER\None. */
1665 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1669 if ( rid == DOMAIN_RID_USERS ) {
1670 *name = talloc_strdup(mem_ctx, "None" );
1671 *psid_name_use = SID_NAME_DOM_GRP;
1679 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1680 const struct dom_sid *domain_sid,
1684 enum lsa_SidType *attrs)
1688 bool have_mapped = False;
1689 bool have_unmapped = False;
1691 if (sid_check_is_builtin(domain_sid)) {
1693 for (i=0; i<num_rids; i++) {
1696 if (lookup_builtin_rid(names, rids[i], &name)) {
1697 attrs[i] = SID_NAME_ALIAS;
1699 DEBUG(5,("lookup_rids: %s:%d\n",
1700 names[i], attrs[i]));
1703 have_unmapped = True;
1704 attrs[i] = SID_NAME_UNKNOWN;
1710 /* Should not happen, but better check once too many */
1711 if (!sid_check_is_domain(domain_sid)) {
1712 return NT_STATUS_INVALID_HANDLE;
1715 for (i = 0; i < num_rids; i++) {
1718 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1721 return NT_STATUS_NO_MEMORY;
1724 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1727 have_unmapped = True;
1728 attrs[i] = SID_NAME_UNKNOWN;
1734 result = NT_STATUS_NONE_MAPPED;
1737 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1743 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1744 const struct dom_sid *domain_sid,
1748 enum lsa_SidType *attrs)
1752 bool have_mapped = False;
1753 bool have_unmapped = False;
1755 if (sid_check_is_builtin(domain_sid)) {
1757 for (i=0; i<num_names; i++) {
1760 if (lookup_builtin_name(names[i], &rid)) {
1761 attrs[i] = SID_NAME_ALIAS;
1763 DEBUG(5,("lookup_rids: %s:%d\n",
1764 names[i], attrs[i]));
1767 have_unmapped = True;
1768 attrs[i] = SID_NAME_UNKNOWN;
1774 /* Should not happen, but better check once too many */
1775 if (!sid_check_is_domain(domain_sid)) {
1776 return NT_STATUS_INVALID_HANDLE;
1779 for (i = 0; i < num_names; i++) {
1780 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1781 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1782 rids[i], attrs[i]));
1785 have_unmapped = True;
1786 attrs[i] = SID_NAME_UNKNOWN;
1792 result = NT_STATUS_NONE_MAPPED;
1795 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1801 static int pdb_search_destructor(struct pdb_search *search)
1803 if ((!search->search_ended) && (search->search_end != NULL)) {
1804 search->search_end(search);
1809 struct pdb_search *pdb_search_init(TALLOC_CTX *mem_ctx,
1810 enum pdb_search_type type)
1812 struct pdb_search *result;
1814 result = talloc(mem_ctx, struct pdb_search);
1815 if (result == NULL) {
1816 DEBUG(0, ("talloc failed\n"));
1820 result->type = type;
1821 result->cache = NULL;
1822 result->num_entries = 0;
1823 result->cache_size = 0;
1824 result->search_ended = False;
1825 result->search_end = NULL;
1827 /* Segfault appropriately if not initialized */
1828 result->next_entry = NULL;
1829 result->search_end = NULL;
1831 talloc_set_destructor(result, pdb_search_destructor);
1836 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32_t rid,
1837 uint16_t acct_flags,
1838 const char *account_name,
1839 const char *fullname,
1840 const char *description,
1841 struct samr_displayentry *entry)
1844 entry->acct_flags = acct_flags;
1846 if (account_name != NULL)
1847 entry->account_name = talloc_strdup(mem_ctx, account_name);
1849 entry->account_name = "";
1851 if (fullname != NULL)
1852 entry->fullname = talloc_strdup(mem_ctx, fullname);
1854 entry->fullname = "";
1856 if (description != NULL)
1857 entry->description = talloc_strdup(mem_ctx, description);
1859 entry->description = "";
1862 struct group_search {
1864 size_t num_groups, current_group;
1867 static bool next_entry_groups(struct pdb_search *s,
1868 struct samr_displayentry *entry)
1870 struct group_search *state = (struct group_search *)s->private_data;
1872 GROUP_MAP *map = &state->groups[state->current_group];
1874 if (state->current_group == state->num_groups)
1877 sid_peek_rid(&map->sid, &rid);
1879 fill_displayentry(s, rid, 0, map->nt_name, NULL, map->comment, entry);
1881 state->current_group += 1;
1885 static void search_end_groups(struct pdb_search *search)
1887 struct group_search *state =
1888 (struct group_search *)search->private_data;
1889 SAFE_FREE(state->groups);
1892 static bool pdb_search_grouptype(struct pdb_search *search,
1893 const struct dom_sid *sid, enum lsa_SidType type)
1895 struct group_search *state;
1897 state = talloc(search, struct group_search);
1898 if (state == NULL) {
1899 DEBUG(0, ("talloc failed\n"));
1903 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1905 DEBUG(0, ("Could not enum groups\n"));
1909 state->current_group = 0;
1910 search->private_data = state;
1911 search->next_entry = next_entry_groups;
1912 search->search_end = search_end_groups;
1916 static bool pdb_default_search_groups(struct pdb_methods *methods,
1917 struct pdb_search *search)
1919 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1922 static bool pdb_default_search_aliases(struct pdb_methods *methods,
1923 struct pdb_search *search,
1924 const struct dom_sid *sid)
1927 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1930 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1933 if (idx < search->num_entries)
1934 return &search->cache[idx];
1936 if (search->search_ended)
1939 while (idx >= search->num_entries) {
1940 struct samr_displayentry entry;
1942 if (!search->next_entry(search, &entry)) {
1943 search->search_end(search);
1944 search->search_ended = True;
1948 ADD_TO_LARGE_ARRAY(search, struct samr_displayentry,
1949 entry, &search->cache, &search->num_entries,
1950 &search->cache_size);
1953 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1956 struct pdb_search *pdb_search_users(TALLOC_CTX *mem_ctx, uint32_t acct_flags)
1958 struct pdb_methods *pdb = pdb_get_methods();
1959 struct pdb_search *result;
1961 result = pdb_search_init(mem_ctx, PDB_USER_SEARCH);
1962 if (result == NULL) {
1966 if (!pdb->search_users(pdb, result, acct_flags)) {
1967 TALLOC_FREE(result);
1973 struct pdb_search *pdb_search_groups(TALLOC_CTX *mem_ctx)
1975 struct pdb_methods *pdb = pdb_get_methods();
1976 struct pdb_search *result;
1978 result = pdb_search_init(mem_ctx, PDB_GROUP_SEARCH);
1979 if (result == NULL) {
1983 if (!pdb->search_groups(pdb, result)) {
1984 TALLOC_FREE(result);
1990 struct pdb_search *pdb_search_aliases(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
1992 struct pdb_methods *pdb = pdb_get_methods();
1993 struct pdb_search *result;
1995 if (pdb == NULL) return NULL;
1997 result = pdb_search_init(mem_ctx, PDB_ALIAS_SEARCH);
1998 if (result == NULL) {
2002 if (!pdb->search_aliases(pdb, result, sid)) {
2003 TALLOC_FREE(result);
2009 uint32_t pdb_search_entries(struct pdb_search *search,
2010 uint32_t start_idx, uint32_t max_entries,
2011 struct samr_displayentry **result)
2013 struct samr_displayentry *end_entry;
2014 uint32_t end_idx = start_idx+max_entries-1;
2016 /* The first entry needs to be searched after the last. Otherwise the
2017 * first entry might have moved due to a realloc during the search for
2018 * the last entry. */
2020 end_entry = pdb_search_getentry(search, end_idx);
2021 *result = pdb_search_getentry(search, start_idx);
2023 if (end_entry != NULL)
2026 if (start_idx >= search->num_entries)
2029 return search->num_entries - start_idx;
2032 /*******************************************************************
2034 *******************************************************************/
2036 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, struct dom_sid *sid,
2037 time_t *pass_last_set_time)
2039 struct pdb_methods *pdb = pdb_get_methods();
2040 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2041 pass_last_set_time);
2044 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2045 const struct dom_sid *sid)
2047 struct pdb_methods *pdb = pdb_get_methods();
2048 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2051 bool pdb_del_trusteddom_pw(const char *domain)
2053 struct pdb_methods *pdb = pdb_get_methods();
2054 return pdb->del_trusteddom_pw(pdb, domain);
2057 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2058 struct trustdom_info ***domains)
2060 struct pdb_methods *pdb = pdb_get_methods();
2061 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2064 /*******************************************************************
2065 the defaults for trustdom methods:
2066 these simply call the original passdb/secrets.c actions,
2067 to be replaced by pdb_ldap.
2068 *******************************************************************/
2070 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2073 struct dom_sid *sid,
2074 time_t *pass_last_set_time)
2076 return secrets_fetch_trusted_domain_password(domain, pwd,
2077 sid, pass_last_set_time);
2081 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2084 const struct dom_sid *sid)
2086 return secrets_store_trusted_domain_password(domain, pwd, sid);
2089 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2092 return trusted_domain_password_delete(domain);
2095 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2096 TALLOC_CTX *mem_ctx,
2097 uint32_t *num_domains,
2098 struct trustdom_info ***domains)
2100 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2103 /*******************************************************************
2104 trusted_domain methods
2105 *******************************************************************/
2107 NTSTATUS pdb_get_trusted_domain(TALLOC_CTX *mem_ctx, const char *domain,
2108 struct pdb_trusted_domain **td)
2110 struct pdb_methods *pdb = pdb_get_methods();
2111 return pdb->get_trusted_domain(pdb, mem_ctx, domain, td);
2114 NTSTATUS pdb_get_trusted_domain_by_sid(TALLOC_CTX *mem_ctx, struct dom_sid *sid,
2115 struct pdb_trusted_domain **td)
2117 struct pdb_methods *pdb = pdb_get_methods();
2118 return pdb->get_trusted_domain_by_sid(pdb, mem_ctx, sid, td);
2121 NTSTATUS pdb_set_trusted_domain(const char* domain,
2122 const struct pdb_trusted_domain *td)
2124 struct pdb_methods *pdb = pdb_get_methods();
2125 return pdb->set_trusted_domain(pdb, domain, td);
2128 NTSTATUS pdb_del_trusted_domain(const char *domain)
2130 struct pdb_methods *pdb = pdb_get_methods();
2131 return pdb->del_trusted_domain(pdb, domain);
2134 NTSTATUS pdb_enum_trusted_domains(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2135 struct pdb_trusted_domain ***domains)
2137 struct pdb_methods *pdb = pdb_get_methods();
2138 return pdb->enum_trusted_domains(pdb, mem_ctx, num_domains, domains);
2141 static NTSTATUS pdb_default_get_trusted_domain(struct pdb_methods *methods,
2142 TALLOC_CTX *mem_ctx,
2144 struct pdb_trusted_domain **td)
2146 return NT_STATUS_NOT_IMPLEMENTED;
2149 static NTSTATUS pdb_default_get_trusted_domain_by_sid(struct pdb_methods *methods,
2150 TALLOC_CTX *mem_ctx,
2151 struct dom_sid *sid,
2152 struct pdb_trusted_domain **td)
2154 return NT_STATUS_NOT_IMPLEMENTED;
2157 static NTSTATUS pdb_default_set_trusted_domain(struct pdb_methods *methods,
2159 const struct pdb_trusted_domain *td)
2161 return NT_STATUS_NOT_IMPLEMENTED;
2164 static NTSTATUS pdb_default_del_trusted_domain(struct pdb_methods *methods,
2167 return NT_STATUS_NOT_IMPLEMENTED;
2170 static NTSTATUS pdb_default_enum_trusted_domains(struct pdb_methods *methods,
2171 TALLOC_CTX *mem_ctx,
2172 uint32_t *num_domains,
2173 struct pdb_trusted_domain ***domains)
2175 return NT_STATUS_NOT_IMPLEMENTED;
2178 static struct pdb_domain_info *pdb_default_get_domain_info(
2179 struct pdb_methods *m, TALLOC_CTX *mem_ctx)
2184 /*******************************************************************
2185 Create a pdb_methods structure and initialize it with the default
2186 operations. In this way a passdb module can simply implement
2187 the functionality it cares about. However, normally this is done
2188 in groups of related functions.
2189 *******************************************************************/
2191 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2193 /* allocate memory for the structure as its own talloc CTX */
2195 *methods = talloc_zero(NULL, struct pdb_methods);
2196 if (*methods == NULL) {
2197 return NT_STATUS_NO_MEMORY;
2200 (*methods)->get_domain_info = pdb_default_get_domain_info;
2201 (*methods)->getsampwnam = pdb_default_getsampwnam;
2202 (*methods)->getsampwsid = pdb_default_getsampwsid;
2203 (*methods)->create_user = pdb_default_create_user;
2204 (*methods)->delete_user = pdb_default_delete_user;
2205 (*methods)->add_sam_account = pdb_default_add_sam_account;
2206 (*methods)->update_sam_account = pdb_default_update_sam_account;
2207 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2208 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2209 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2211 (*methods)->getgrsid = pdb_default_getgrsid;
2212 (*methods)->getgrgid = pdb_default_getgrgid;
2213 (*methods)->getgrnam = pdb_default_getgrnam;
2214 (*methods)->create_dom_group = pdb_default_create_dom_group;
2215 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2216 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2217 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2218 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2219 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2220 (*methods)->enum_group_members = pdb_default_enum_group_members;
2221 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2222 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2223 (*methods)->add_groupmem = pdb_default_add_groupmem;
2224 (*methods)->del_groupmem = pdb_default_del_groupmem;
2225 (*methods)->create_alias = pdb_default_create_alias;
2226 (*methods)->delete_alias = pdb_default_delete_alias;
2227 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2228 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2229 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2230 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2231 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2232 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2233 (*methods)->lookup_rids = pdb_default_lookup_rids;
2234 (*methods)->get_account_policy = pdb_default_get_account_policy;
2235 (*methods)->set_account_policy = pdb_default_set_account_policy;
2236 (*methods)->get_seq_num = pdb_default_get_seq_num;
2237 (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2238 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2239 (*methods)->sid_to_id = pdb_default_sid_to_id;
2241 (*methods)->search_groups = pdb_default_search_groups;
2242 (*methods)->search_aliases = pdb_default_search_aliases;
2244 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2245 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2246 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2247 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2249 (*methods)->get_trusted_domain = pdb_default_get_trusted_domain;
2250 (*methods)->get_trusted_domain_by_sid = pdb_default_get_trusted_domain_by_sid;
2251 (*methods)->set_trusted_domain = pdb_default_set_trusted_domain;
2252 (*methods)->del_trusted_domain = pdb_default_del_trusted_domain;
2253 (*methods)->enum_trusted_domains = pdb_default_enum_trusted_domains;
2255 return NT_STATUS_OK;