2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Jelmer Vernooij 2002
6 Copyright (C) Simo Sorce 2003
7 Copyright (C) Volker Lendecke 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "../librpc/gen_ndr/samr.h"
27 #include "nsswitch/winbind_client.h"
28 #include "../libcli/security/security.h"
31 #define DBGC_CLASS DBGC_PASSDB
35 static struct pdb_init_function_entry *backends = NULL;
37 static void lazy_initialize_passdb(void)
39 static bool initialized = False;
47 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
49 enum lsa_SidType *psid_name_use,
50 union unid_t *unix_id);
52 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
54 struct pdb_init_function_entry *entry = backends;
56 if(version != PASSDB_INTERFACE_VERSION) {
57 DEBUG(0,("Can't register passdb backend!\n"
58 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
59 "while this version of samba uses version %d\n",
60 version,PASSDB_INTERFACE_VERSION));
61 return NT_STATUS_OBJECT_TYPE_MISMATCH;
65 return NT_STATUS_INVALID_PARAMETER;
68 DEBUG(5,("Attempting to register passdb backend %s\n", name));
70 /* Check for duplicates */
71 if (pdb_find_backend_entry(name)) {
72 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
73 return NT_STATUS_OBJECT_NAME_COLLISION;
76 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
77 entry->name = smb_xstrdup(name);
80 DLIST_ADD(backends, entry);
81 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
85 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
87 struct pdb_init_function_entry *entry = backends;
90 if (strcmp(entry->name, name)==0) return entry;
98 * The event context for the passdb backend. I know this is a bad hack and yet
99 * another static variable, but our pdb API is a global thing per
100 * definition. The first use for this is the LDAP idle function, more might be
103 * I don't feel too bad about this static variable, it replaces the
104 * smb_idle_event_list that used to exist in lib/module.c. -- VL
107 static struct event_context *pdb_event_ctx;
109 struct event_context *pdb_get_event_context(void)
111 return pdb_event_ctx;
114 /******************************************************************
115 Make a pdb_methods from scratch
116 *******************************************************************/
118 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
120 char *module_name = smb_xstrdup(selected);
121 char *module_location = NULL, *p;
122 struct pdb_init_function_entry *entry;
123 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
125 lazy_initialize_passdb();
127 p = strchr(module_name, ':');
131 module_location = p+1;
132 trim_char(module_location, ' ', ' ');
135 trim_char(module_name, ' ', ' ');
138 DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected, module_name));
140 entry = pdb_find_backend_entry(module_name);
142 /* Try to find a module that contains this module */
144 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
145 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
146 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
147 SAFE_FREE(module_name);
148 return NT_STATUS_UNSUCCESSFUL;
152 /* No such backend found */
154 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
155 SAFE_FREE(module_name);
156 return NT_STATUS_INVALID_PARAMETER;
159 DEBUG(5,("Found pdb backend %s\n", module_name));
161 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
162 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
163 selected, nt_errstr(nt_status)));
164 SAFE_FREE(module_name);
168 SAFE_FREE(module_name);
170 DEBUG(5,("pdb backend %s has a valid init\n", selected));
175 /******************************************************************
176 Return an already initialized pdb_methods structure
177 *******************************************************************/
179 static struct pdb_methods *pdb_get_methods_reload( bool reload )
181 static struct pdb_methods *pdb = NULL;
183 if ( pdb && reload ) {
184 pdb->free_private_data( &(pdb->private_data) );
185 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
187 if (asprintf(&msg, "pdb_get_methods_reload: "
188 "failed to get pdb methods for backend %s\n",
189 lp_passdb_backend()) > 0) {
192 smb_panic("pdb_get_methods_reload");
198 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
200 if (asprintf(&msg, "pdb_get_methods_reload: "
201 "failed to get pdb methods for backend %s\n",
202 lp_passdb_backend()) > 0) {
205 smb_panic("pdb_get_methods_reload");
213 static struct pdb_methods *pdb_get_methods(void)
215 return pdb_get_methods_reload(False);
218 struct pdb_domain_info *pdb_get_domain_info(TALLOC_CTX *mem_ctx)
220 struct pdb_methods *pdb = pdb_get_methods();
221 return pdb->get_domain_info(pdb, mem_ctx);
225 * @brief Check if the user account has been locked out and try to unlock it.
227 * If the user has been automatically locked out and a lockout duration is set,
228 * then check if we can unlock the account and reset the bad password values.
230 * @param[in] sampass The sam user to check.
232 * @return True if the function was successfull, false on an error.
234 static bool pdb_try_account_unlock(struct samu *sampass)
236 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
238 if (acb_info & (ACB_NORMAL|ACB_AUTOLOCK)) {
239 uint32_t lockout_duration;
240 time_t bad_password_time;
241 time_t now = time(NULL);
244 ok = pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
247 DEBUG(0, ("pdb_try_account_unlock: "
248 "pdb_get_account_policy failed.\n"));
252 if (lockout_duration == (uint32_t) -1 ||
253 lockout_duration == 0) {
254 DEBUG(9, ("pdb_try_account_unlock: No reset duration, "
255 "can't reset autolock\n"));
258 lockout_duration *= 60;
260 bad_password_time = pdb_get_bad_password_time(sampass);
261 if (bad_password_time == (time_t) 0) {
262 DEBUG(2, ("pdb_try_account_unlock: Account %s "
263 "administratively locked out "
264 "with no bad password "
265 "time. Leaving locked out.\n",
266 pdb_get_username(sampass)));
270 if ((bad_password_time +
271 convert_uint32_t_to_time_t(lockout_duration)) < now) {
274 pdb_set_acct_ctrl(sampass, acb_info & ~ACB_AUTOLOCK,
276 pdb_set_bad_password_count(sampass, 0, PDB_CHANGED);
277 pdb_set_bad_password_time(sampass, 0, PDB_CHANGED);
280 status = pdb_update_sam_account(sampass);
282 if (!NT_STATUS_IS_OK(status)) {
283 DEBUG(0, ("_samr_OpenUser: Couldn't "
284 "update account %s - %s\n",
285 pdb_get_username(sampass),
296 * @brief Get a sam user structure by the given username.
298 * This functions also checks if the account has been automatically locked out
299 * and unlocks it if a lockout duration time has been defined and the time has
302 * @param[in] sam_acct The sam user structure to fill.
304 * @param[in] username The username to look for.
306 * @return True on success, false on error.
308 bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
310 struct pdb_methods *pdb = pdb_get_methods();
311 struct samu *for_cache;
312 const struct dom_sid *user_sid;
316 status = pdb->getsampwnam(pdb, sam_acct, username);
317 if (!NT_STATUS_IS_OK(status)) {
321 ok = pdb_try_account_unlock(sam_acct);
323 DEBUG(1, ("pdb_getsampwnam: Failed to unlock account %s\n",
327 for_cache = samu_new(NULL);
328 if (for_cache == NULL) {
332 if (!pdb_copy_sam_account(for_cache, sam_acct)) {
333 TALLOC_FREE(for_cache);
337 user_sid = pdb_get_user_sid(for_cache);
339 memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
340 data_blob_const(user_sid, sizeof(*user_sid)),
346 /**********************************************************************
347 **********************************************************************/
349 static bool guest_user_info( struct samu *user )
353 const char *guestname = lp_guestaccount();
355 pwd = Get_Pwnam_alloc(talloc_tos(), guestname);
357 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
362 result = samu_set_unix(user, pwd );
366 return NT_STATUS_IS_OK( result );
370 * @brief Get a sam user structure by the given username.
372 * This functions also checks if the account has been automatically locked out
373 * and unlocks it if a lockout duration time has been defined and the time has
377 * @param[in] sam_acct The sam user structure to fill.
379 * @param[in] sid The user SDI to look up.
381 * @return True on success, false on error.
383 bool pdb_getsampwsid(struct samu *sam_acct, const struct dom_sid *sid)
385 struct pdb_methods *pdb = pdb_get_methods();
390 /* hard code the Guest RID of 501 */
392 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
395 if ( rid == DOMAIN_RID_GUEST ) {
396 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
397 return guest_user_info( sam_acct );
400 /* check the cache first */
402 cache_data = memcache_lookup_talloc(
403 NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
405 if (cache_data != NULL) {
406 struct samu *cache_copy = talloc_get_type_abort(
407 cache_data, struct samu);
409 ok = pdb_copy_sam_account(sam_acct, cache_copy);
411 ok = NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
418 ok = pdb_try_account_unlock(sam_acct);
420 DEBUG(1, ("pdb_getsampwsid: Failed to unlock account %s\n",
421 sam_acct->username));
427 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
428 TALLOC_CTX *tmp_ctx, const char *name,
429 uint32_t acb_info, uint32_t *rid)
431 struct samu *sam_pass;
435 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
436 return NT_STATUS_NO_MEMORY;
439 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
440 char *add_script = NULL;
444 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
445 add_script = talloc_strdup(tmp_ctx,
446 lp_adduser_script());
448 add_script = talloc_strdup(tmp_ctx,
449 lp_addmachine_script());
452 if (!add_script || add_script[0] == '\0') {
453 DEBUG(3, ("Could not find user %s and no add script "
455 return NT_STATUS_NO_SUCH_USER;
458 /* lowercase the username before creating the Unix account for
459 compatibility with previous Samba releases */
460 fstrcpy( name2, name );
462 add_script = talloc_all_string_sub(tmp_ctx,
467 return NT_STATUS_NO_MEMORY;
469 add_ret = smbrun(add_script,NULL);
470 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
471 add_script, add_ret));
473 smb_nscd_flush_user_cache();
478 pwd = Get_Pwnam_alloc(tmp_ctx, name);
481 /* we have a valid SID coming out of this call */
483 status = samu_alloc_rid_unix( sam_pass, pwd );
487 if (!NT_STATUS_IS_OK(status)) {
488 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
492 if (!sid_peek_check_rid(get_global_sam_sid(),
493 pdb_get_user_sid(sam_pass), rid)) {
494 DEBUG(0, ("Could not get RID of fresh user\n"));
495 return NT_STATUS_INTERNAL_ERROR;
498 /* Use the username case specified in the original request */
500 pdb_set_username( sam_pass, name, PDB_SET );
502 /* Disable the account on creation, it does not have a reasonable password yet. */
504 acb_info |= ACB_DISABLED;
506 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
508 status = pdb_add_sam_account(sam_pass);
510 TALLOC_FREE(sam_pass);
515 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32_t flags,
518 struct pdb_methods *pdb = pdb_get_methods();
519 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
522 /****************************************************************************
523 Delete a UNIX user on demand.
524 ****************************************************************************/
526 static int smb_delete_user(const char *unix_user)
528 char *del_script = NULL;
533 if ( strequal( unix_user, "root" ) ) {
534 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
538 del_script = talloc_strdup(talloc_tos(), lp_deluser_script());
539 if (!del_script || !*del_script) {
542 del_script = talloc_all_string_sub(talloc_tos(),
549 ret = smbrun(del_script,NULL);
552 smb_nscd_flush_user_cache();
554 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
559 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
561 struct samu *sam_acct)
566 status = pdb_delete_sam_account(sam_acct);
567 if (!NT_STATUS_IS_OK(status)) {
572 * Now delete the unix side ....
573 * note: we don't check if the delete really happened as the script is
574 * not necessary present and maybe the sysadmin doesn't want to delete
578 /* always lower case the username before handing it off to
581 fstrcpy( username, pdb_get_username(sam_acct) );
582 strlower_m( username );
584 smb_delete_user( username );
589 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
591 struct pdb_methods *pdb = pdb_get_methods();
594 /* sanity check to make sure we don't delete root */
596 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
597 return NT_STATUS_NO_SUCH_USER;
601 return NT_STATUS_ACCESS_DENIED;
604 return pdb->delete_user(pdb, mem_ctx, sam_acct);
607 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
609 struct pdb_methods *pdb = pdb_get_methods();
610 return pdb->add_sam_account(pdb, sam_acct);
613 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
615 struct pdb_methods *pdb = pdb_get_methods();
617 memcache_flush(NULL, PDB_GETPWSID_CACHE);
619 return pdb->update_sam_account(pdb, sam_acct);
622 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
624 struct pdb_methods *pdb = pdb_get_methods();
626 memcache_flush(NULL, PDB_GETPWSID_CACHE);
628 return pdb->delete_sam_account(pdb, sam_acct);
631 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
633 struct pdb_methods *pdb = pdb_get_methods();
637 memcache_flush(NULL, PDB_GETPWSID_CACHE);
639 /* sanity check to make sure we don't rename root */
641 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
642 return NT_STATUS_NO_SUCH_USER;
646 return NT_STATUS_ACCESS_DENIED;
649 status = pdb->rename_sam_account(pdb, oldname, newname);
651 /* always flush the cache here just to be safe */
657 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
659 struct pdb_methods *pdb = pdb_get_methods();
660 return pdb->update_login_attempts(pdb, sam_acct, success);
663 bool pdb_getgrsid(GROUP_MAP *map, struct dom_sid sid)
665 struct pdb_methods *pdb = pdb_get_methods();
666 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
669 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
671 struct pdb_methods *pdb = pdb_get_methods();
672 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
675 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
677 struct pdb_methods *pdb = pdb_get_methods();
678 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
681 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
686 struct dom_sid group_sid;
690 grp = getgrnam(name);
695 if (smb_create_group(name, &gid) != 0) {
696 return NT_STATUS_ACCESS_DENIED;
703 return NT_STATUS_ACCESS_DENIED;
706 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
707 if (!pdb_new_rid(rid)) {
708 return NT_STATUS_ACCESS_DENIED;
711 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
714 sid_compose(&group_sid, get_global_sam_sid(), *rid);
716 return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
717 SID_NAME_DOM_GRP, name, NULL);
720 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
723 struct pdb_methods *pdb = pdb_get_methods();
724 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
727 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
731 struct dom_sid group_sid;
735 const char *grp_name;
738 map.gid = (gid_t) -1;
740 sid_compose(&group_sid, get_global_sam_sid(), rid);
742 if (!get_domain_group_from_sid(group_sid, &map)) {
743 DEBUG(10, ("Could not find group for rid %d\n", rid));
744 return NT_STATUS_NO_SUCH_GROUP;
747 /* We need the group name for the smb_delete_group later on */
749 if (map.gid == (gid_t)-1) {
750 return NT_STATUS_NO_SUCH_GROUP;
753 grp = getgrgid(map.gid);
755 return NT_STATUS_NO_SUCH_GROUP;
758 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
760 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
761 if (grp_name == NULL) {
762 return NT_STATUS_NO_MEMORY;
765 status = pdb_delete_group_mapping_entry(group_sid);
767 if (!NT_STATUS_IS_OK(status)) {
771 /* Don't check the result of smb_delete_group */
773 smb_delete_group(grp_name);
778 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32_t rid)
780 struct pdb_methods *pdb = pdb_get_methods();
781 return pdb->delete_dom_group(pdb, mem_ctx, rid);
784 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
786 struct pdb_methods *pdb = pdb_get_methods();
787 return pdb->add_group_mapping_entry(pdb, map);
790 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
792 struct pdb_methods *pdb = pdb_get_methods();
793 return pdb->update_group_mapping_entry(pdb, map);
796 NTSTATUS pdb_delete_group_mapping_entry(struct dom_sid sid)
798 struct pdb_methods *pdb = pdb_get_methods();
799 return pdb->delete_group_mapping_entry(pdb, sid);
802 bool pdb_enum_group_mapping(const struct dom_sid *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
803 size_t *p_num_entries, bool unix_only)
805 struct pdb_methods *pdb = pdb_get_methods();
806 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
807 pp_rmap, p_num_entries, unix_only));
810 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
811 const struct dom_sid *sid,
812 uint32_t **pp_member_rids,
813 size_t *p_num_members)
815 struct pdb_methods *pdb = pdb_get_methods();
818 result = pdb->enum_group_members(pdb, mem_ctx,
819 sid, pp_member_rids, p_num_members);
821 /* special check for rid 513 */
823 if ( !NT_STATUS_IS_OK( result ) ) {
826 sid_peek_rid( sid, &rid );
828 if ( rid == DOMAIN_RID_USERS ) {
830 *pp_member_rids = NULL;
839 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
840 struct dom_sid **pp_sids, gid_t **pp_gids,
841 size_t *p_num_groups)
843 struct pdb_methods *pdb = pdb_get_methods();
844 return pdb->enum_group_memberships(
846 pp_sids, pp_gids, p_num_groups);
849 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
851 struct samu *sampass)
856 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
857 (grp = getgrgid(gid)) == NULL) {
858 return NT_STATUS_INVALID_PRIMARY_GROUP;
861 if (smb_set_primary_group(grp->gr_name,
862 pdb_get_username(sampass)) != 0) {
863 return NT_STATUS_ACCESS_DENIED;
869 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
871 struct pdb_methods *pdb = pdb_get_methods();
872 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
876 * Helper function to see whether a user is in a group. We can't use
877 * user_in_group_sid here because this creates dependencies only smbd can
881 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
882 const struct dom_sid *group_sid)
884 struct dom_sid *sids;
886 size_t i, num_groups;
888 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
894 for (i=0; i<num_groups; i++) {
895 if (dom_sid_equal(group_sid, &sids[i])) {
902 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
907 struct dom_sid group_sid, member_sid;
908 struct samu *account = NULL;
912 const char *group_name;
916 map.gid = (gid_t) -1;
918 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
919 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
921 if (!get_domain_group_from_sid(group_sid, &map) ||
922 (map.gid == (gid_t)-1) ||
923 ((grp = getgrgid(map.gid)) == NULL)) {
924 return NT_STATUS_NO_SUCH_GROUP;
927 group_name = talloc_strdup(mem_ctx, grp->gr_name);
928 if (group_name == NULL) {
929 return NT_STATUS_NO_MEMORY;
932 if ( !(account = samu_new( NULL )) ) {
933 return NT_STATUS_NO_MEMORY;
936 if (!pdb_getsampwsid(account, &member_sid) ||
937 !sid_to_uid(&member_sid, &uid) ||
938 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
939 return NT_STATUS_NO_SUCH_USER;
942 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
943 return NT_STATUS_MEMBER_IN_GROUP;
947 * ok, the group exist, the user exist, the user is not in the group,
948 * we can (finally) add it to the group !
951 smb_add_user_group(group_name, pwd->pw_name);
953 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
954 return NT_STATUS_ACCESS_DENIED;
960 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
963 struct pdb_methods *pdb = pdb_get_methods();
964 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
967 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
972 struct dom_sid group_sid, member_sid;
973 struct samu *account = NULL;
977 const char *group_name;
980 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
981 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
983 if (!get_domain_group_from_sid(group_sid, &map) ||
984 (map.gid == (gid_t)-1) ||
985 ((grp = getgrgid(map.gid)) == NULL)) {
986 return NT_STATUS_NO_SUCH_GROUP;
989 group_name = talloc_strdup(mem_ctx, grp->gr_name);
990 if (group_name == NULL) {
991 return NT_STATUS_NO_MEMORY;
994 if ( !(account = samu_new( NULL )) ) {
995 return NT_STATUS_NO_MEMORY;
998 if (!pdb_getsampwsid(account, &member_sid) ||
999 !sid_to_uid(&member_sid, &uid) ||
1000 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
1001 return NT_STATUS_NO_SUCH_USER;
1004 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1005 return NT_STATUS_MEMBER_NOT_IN_GROUP;
1009 * ok, the group exist, the user exist, the user is in the group,
1010 * we can (finally) delete it from the group!
1013 smb_delete_user_group(group_name, pwd->pw_name);
1015 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1016 return NT_STATUS_ACCESS_DENIED;
1019 return NT_STATUS_OK;
1022 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1023 uint32_t member_rid)
1025 struct pdb_methods *pdb = pdb_get_methods();
1026 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
1029 NTSTATUS pdb_create_alias(const char *name, uint32_t *rid)
1031 struct pdb_methods *pdb = pdb_get_methods();
1032 return pdb->create_alias(pdb, name, rid);
1035 NTSTATUS pdb_delete_alias(const struct dom_sid *sid)
1037 struct pdb_methods *pdb = pdb_get_methods();
1038 return pdb->delete_alias(pdb, sid);
1041 NTSTATUS pdb_get_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1043 struct pdb_methods *pdb = pdb_get_methods();
1044 return pdb->get_aliasinfo(pdb, sid, info);
1047 NTSTATUS pdb_set_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1049 struct pdb_methods *pdb = pdb_get_methods();
1050 return pdb->set_aliasinfo(pdb, sid, info);
1053 NTSTATUS pdb_add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1055 struct pdb_methods *pdb = pdb_get_methods();
1056 return pdb->add_aliasmem(pdb, alias, member);
1059 NTSTATUS pdb_del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1061 struct pdb_methods *pdb = pdb_get_methods();
1062 return pdb->del_aliasmem(pdb, alias, member);
1065 NTSTATUS pdb_enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
1066 struct dom_sid **pp_members, size_t *p_num_members)
1068 struct pdb_methods *pdb = pdb_get_methods();
1069 return pdb->enum_aliasmem(pdb, alias, mem_ctx, pp_members,
1073 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1074 const struct dom_sid *domain_sid,
1075 const struct dom_sid *members, size_t num_members,
1076 uint32_t **pp_alias_rids,
1077 size_t *p_num_alias_rids)
1079 struct pdb_methods *pdb = pdb_get_methods();
1080 return pdb->enum_alias_memberships(pdb, mem_ctx,
1082 members, num_members,
1087 NTSTATUS pdb_lookup_rids(const struct dom_sid *domain_sid,
1091 enum lsa_SidType *attrs)
1093 struct pdb_methods *pdb = pdb_get_methods();
1094 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
1098 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
1099 * in the samba code.
1100 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
1101 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
1102 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
1103 * down to are pdb_getsampwnam and pdb_getgrnam instead of
1105 * But in principle, it the call belongs to the API and might get
1106 * used in this context some day.
1109 NTSTATUS pdb_lookup_names(const struct dom_sid *domain_sid,
1113 enum lsa_SidType *attrs)
1115 struct pdb_methods *pdb = pdb_get_methods();
1116 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
1120 bool pdb_get_account_policy(enum pdb_policy_type type, uint32_t *value)
1122 struct pdb_methods *pdb = pdb_get_methods();
1126 status = pdb->get_account_policy(pdb, type, value);
1129 return NT_STATUS_IS_OK(status);
1132 bool pdb_set_account_policy(enum pdb_policy_type type, uint32_t value)
1134 struct pdb_methods *pdb = pdb_get_methods();
1138 status = pdb->set_account_policy(pdb, type, value);
1141 return NT_STATUS_IS_OK(status);
1144 bool pdb_get_seq_num(time_t *seq_num)
1146 struct pdb_methods *pdb = pdb_get_methods();
1147 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1150 bool pdb_uid_to_sid(uid_t uid, struct dom_sid *sid)
1152 struct pdb_methods *pdb = pdb_get_methods();
1153 return pdb->uid_to_sid(pdb, uid, sid);
1156 bool pdb_gid_to_sid(gid_t gid, struct dom_sid *sid)
1158 struct pdb_methods *pdb = pdb_get_methods();
1159 return pdb->gid_to_sid(pdb, gid, sid);
1162 bool pdb_sid_to_id(const struct dom_sid *sid, union unid_t *id,
1163 enum lsa_SidType *type)
1165 struct pdb_methods *pdb = pdb_get_methods();
1166 return pdb->sid_to_id(pdb, sid, id, type);
1169 uint32_t pdb_capabilities(void)
1171 struct pdb_methods *pdb = pdb_get_methods();
1172 return pdb->capabilities(pdb);
1175 /********************************************************************
1176 Allocate a new RID from the passdb backend. Verify that it is free
1177 by calling lookup_global_sam_rid() to verify that the RID is not
1178 in use. This handles servers that have existing users or groups
1179 with add RIDs (assigned from previous algorithmic mappings)
1180 ********************************************************************/
1182 bool pdb_new_rid(uint32_t *rid)
1184 struct pdb_methods *pdb = pdb_get_methods();
1185 const char *name = NULL;
1186 enum lsa_SidType type;
1187 uint32_t allocated_rid = 0;
1191 if ((pdb_capabilities() & PDB_CAP_STORE_RIDS) == 0) {
1192 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1197 if (algorithmic_rid_base() != BASE_RID) {
1198 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1199 "without algorithmic RIDs is chosen.\n"));
1200 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1201 "add', set the maximum used RID\n"));
1202 DEBUGADD(0, ("and remove the parameter\n"));
1206 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1207 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1211 /* Attempt to get an unused RID (max tires is 250...yes that it is
1212 and arbitrary number I pulkled out of my head). -- jerry */
1214 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1217 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1221 /* validate that the RID is not in use */
1223 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1230 if ( allocated_rid == 0 ) {
1231 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1235 *rid = allocated_rid;
1240 /***************************************************************
1241 Initialize the static context (at smbd startup etc).
1243 If uninitialised, context will auto-init on first use.
1244 ***************************************************************/
1246 bool initialize_password_db(bool reload, struct event_context *event_ctx)
1248 pdb_event_ctx = event_ctx;
1249 return (pdb_get_methods_reload(reload) != NULL);
1253 /***************************************************************************
1254 Default implementations of some functions.
1255 ****************************************************************************/
1257 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1259 return NT_STATUS_NO_SUCH_USER;
1262 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1264 return NT_STATUS_NO_SUCH_USER;
1267 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1269 return NT_STATUS_NOT_IMPLEMENTED;
1272 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1274 return NT_STATUS_NOT_IMPLEMENTED;
1277 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1279 return NT_STATUS_NOT_IMPLEMENTED;
1282 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1284 return NT_STATUS_NOT_IMPLEMENTED;
1287 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1289 /* Only the pdb_nds backend implements this, by
1290 * default just return ok. */
1291 return NT_STATUS_OK;
1294 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t *value)
1296 return account_policy_get(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1299 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t value)
1301 return account_policy_set(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1304 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1306 *seq_num = time(NULL);
1307 return NT_STATUS_OK;
1310 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1311 struct dom_sid *sid)
1313 struct samu *sampw = NULL;
1314 struct passwd *unix_pw;
1317 unix_pw = sys_getpwuid( uid );
1320 DEBUG(4,("pdb_default_uid_to_sid: host has no idea of uid "
1321 "%lu\n", (unsigned long)uid));
1325 if ( !(sampw = samu_new( NULL )) ) {
1326 DEBUG(0,("pdb_default_uid_to_sid: samu_new() failed!\n"));
1331 ret = NT_STATUS_IS_OK(
1332 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1336 DEBUG(5, ("pdb_default_uid_to_sid: Did not find user "
1337 "%s (%u)\n", unix_pw->pw_name, (unsigned int)uid));
1342 sid_copy(sid, pdb_get_user_sid(sampw));
1349 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1350 struct dom_sid *sid)
1354 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1358 sid_copy(sid, &map.sid);
1362 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1363 const struct dom_sid *sid,
1364 union unid_t *id, enum lsa_SidType *type)
1366 TALLOC_CTX *mem_ctx;
1371 mem_ctx = talloc_new(NULL);
1373 if (mem_ctx == NULL) {
1374 DEBUG(0, ("talloc_new failed\n"));
1378 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1379 /* Here we might have users as well as groups and aliases */
1380 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1384 /* check for "Unix User" */
1386 if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1388 *type = SID_NAME_USER;
1393 /* check for "Unix Group" */
1395 if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1397 *type = SID_NAME_ALIAS;
1404 if (sid_check_is_in_builtin(sid) ||
1405 sid_check_is_in_wellknown_domain(sid)) {
1406 /* Here we only have aliases */
1408 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1409 DEBUG(10, ("Could not find map for sid %s\n",
1410 sid_string_dbg(sid)));
1413 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1414 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1415 DEBUG(10, ("Map for sid %s is a %s, expected an "
1416 "alias\n", sid_string_dbg(sid),
1417 sid_type_lookup(map.sid_name_use)));
1422 *type = SID_NAME_ALIAS;
1427 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1428 sid_string_dbg(sid)));
1432 TALLOC_FREE(mem_ctx);
1436 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1447 /* We only look at our own sam, so don't care about imported stuff */
1448 winbind_env = winbind_env_set();
1449 (void)winbind_off();
1451 if ((grp = getgrgid(gid)) == NULL) {
1452 /* allow winbindd lookups, but only if they weren't already disabled */
1456 /* Primary group members */
1458 while ((pwd = getpwent()) != NULL) {
1459 if (pwd->pw_gid == gid) {
1460 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1468 /* Secondary group members */
1469 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1470 struct passwd *pw = getpwnam(*gr);
1474 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1483 /* allow winbindd lookups, but only if they weren't already disabled */
1491 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1492 TALLOC_CTX *mem_ctx,
1493 const struct dom_sid *group,
1494 uint32_t **pp_member_rids,
1495 size_t *p_num_members)
1501 *pp_member_rids = NULL;
1504 if (!sid_to_gid(group, &gid))
1505 return NT_STATUS_NO_SUCH_GROUP;
1507 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1508 return NT_STATUS_NO_SUCH_GROUP;
1511 return NT_STATUS_OK;
1513 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32_t, num_uids);
1515 for (i=0; i<num_uids; i++) {
1518 uid_to_sid(&sid, uids[i]);
1520 if (!sid_check_is_in_our_domain(&sid)) {
1521 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1522 "in our domain\n"));
1526 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1527 *p_num_members += 1;
1530 return NT_STATUS_OK;
1533 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1534 TALLOC_CTX *mem_ctx,
1536 struct dom_sid **pp_sids,
1538 size_t *p_num_groups)
1543 const char *username = pdb_get_username(user);
1546 /* Ignore the primary group SID. Honor the real Unix primary group.
1547 The primary group SID is only of real use to Windows clients */
1549 if ( !(pw = Get_Pwnam_alloc(mem_ctx, username)) ) {
1550 return NT_STATUS_NO_SUCH_USER;
1557 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1558 return NT_STATUS_NO_SUCH_USER;
1561 if (*p_num_groups == 0) {
1562 smb_panic("primary group missing");
1565 *pp_sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, *p_num_groups);
1567 if (*pp_sids == NULL) {
1568 TALLOC_FREE(*pp_gids);
1569 return NT_STATUS_NO_MEMORY;
1572 for (i=0; i<*p_num_groups; i++) {
1573 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1576 return NT_STATUS_OK;
1579 /*******************************************************************
1580 Look up a rid in the SAM we're responsible for (i.e. passdb)
1581 ********************************************************************/
1583 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32_t rid,
1585 enum lsa_SidType *psid_name_use,
1586 union unid_t *unix_id)
1588 struct samu *sam_account = NULL;
1593 *psid_name_use = SID_NAME_UNKNOWN;
1595 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1596 (unsigned int)rid));
1598 sid_compose(&sid, get_global_sam_sid(), rid);
1600 /* see if the passdb can help us with the name of the user */
1602 if ( !(sam_account = samu_new( NULL )) ) {
1606 /* BEING ROOT BLOCK */
1608 if (pdb_getsampwsid(sam_account, &sid)) {
1611 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1612 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1614 TALLOC_FREE(sam_account);
1618 *psid_name_use = SID_NAME_USER;
1620 TALLOC_FREE(sam_account);
1622 if (unix_id == NULL) {
1626 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1630 unix_id->uid = pw->pw_uid;
1634 TALLOC_FREE(sam_account);
1636 ret = pdb_getgrsid(&map, sid);
1638 /* END BECOME_ROOT BLOCK */
1640 /* do not resolve SIDs to a name unless there is a valid
1641 gid associated with it */
1643 if ( ret && (map.gid != (gid_t)-1) ) {
1644 *name = talloc_strdup(mem_ctx, map.nt_name);
1645 *psid_name_use = map.sid_name_use;
1648 unix_id->gid = map.gid;
1654 /* Windows will always map RID 513 to something. On a non-domain
1655 controller, this gets mapped to SERVER\None. */
1658 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1662 if ( rid == DOMAIN_RID_USERS ) {
1663 *name = talloc_strdup(mem_ctx, "None" );
1664 *psid_name_use = SID_NAME_DOM_GRP;
1672 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1673 const struct dom_sid *domain_sid,
1677 enum lsa_SidType *attrs)
1681 bool have_mapped = False;
1682 bool have_unmapped = False;
1684 if (sid_check_is_builtin(domain_sid)) {
1686 for (i=0; i<num_rids; i++) {
1689 if (lookup_builtin_rid(names, rids[i], &name)) {
1690 attrs[i] = SID_NAME_ALIAS;
1692 DEBUG(5,("lookup_rids: %s:%d\n",
1693 names[i], attrs[i]));
1696 have_unmapped = True;
1697 attrs[i] = SID_NAME_UNKNOWN;
1703 /* Should not happen, but better check once too many */
1704 if (!sid_check_is_domain(domain_sid)) {
1705 return NT_STATUS_INVALID_HANDLE;
1708 for (i = 0; i < num_rids; i++) {
1711 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1714 return NT_STATUS_NO_MEMORY;
1717 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1720 have_unmapped = True;
1721 attrs[i] = SID_NAME_UNKNOWN;
1727 result = NT_STATUS_NONE_MAPPED;
1730 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1736 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1737 const struct dom_sid *domain_sid,
1741 enum lsa_SidType *attrs)
1745 bool have_mapped = False;
1746 bool have_unmapped = False;
1748 if (sid_check_is_builtin(domain_sid)) {
1750 for (i=0; i<num_names; i++) {
1753 if (lookup_builtin_name(names[i], &rid)) {
1754 attrs[i] = SID_NAME_ALIAS;
1756 DEBUG(5,("lookup_rids: %s:%d\n",
1757 names[i], attrs[i]));
1760 have_unmapped = True;
1761 attrs[i] = SID_NAME_UNKNOWN;
1767 /* Should not happen, but better check once too many */
1768 if (!sid_check_is_domain(domain_sid)) {
1769 return NT_STATUS_INVALID_HANDLE;
1772 for (i = 0; i < num_names; i++) {
1773 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1774 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1775 rids[i], attrs[i]));
1778 have_unmapped = True;
1779 attrs[i] = SID_NAME_UNKNOWN;
1785 result = NT_STATUS_NONE_MAPPED;
1788 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1794 static int pdb_search_destructor(struct pdb_search *search)
1796 if ((!search->search_ended) && (search->search_end != NULL)) {
1797 search->search_end(search);
1802 struct pdb_search *pdb_search_init(TALLOC_CTX *mem_ctx,
1803 enum pdb_search_type type)
1805 struct pdb_search *result;
1807 result = talloc(mem_ctx, struct pdb_search);
1808 if (result == NULL) {
1809 DEBUG(0, ("talloc failed\n"));
1813 result->type = type;
1814 result->cache = NULL;
1815 result->num_entries = 0;
1816 result->cache_size = 0;
1817 result->search_ended = False;
1818 result->search_end = NULL;
1820 /* Segfault appropriately if not initialized */
1821 result->next_entry = NULL;
1822 result->search_end = NULL;
1824 talloc_set_destructor(result, pdb_search_destructor);
1829 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32_t rid,
1830 uint16_t acct_flags,
1831 const char *account_name,
1832 const char *fullname,
1833 const char *description,
1834 struct samr_displayentry *entry)
1837 entry->acct_flags = acct_flags;
1839 if (account_name != NULL)
1840 entry->account_name = talloc_strdup(mem_ctx, account_name);
1842 entry->account_name = "";
1844 if (fullname != NULL)
1845 entry->fullname = talloc_strdup(mem_ctx, fullname);
1847 entry->fullname = "";
1849 if (description != NULL)
1850 entry->description = talloc_strdup(mem_ctx, description);
1852 entry->description = "";
1855 struct group_search {
1857 size_t num_groups, current_group;
1860 static bool next_entry_groups(struct pdb_search *s,
1861 struct samr_displayentry *entry)
1863 struct group_search *state = (struct group_search *)s->private_data;
1865 GROUP_MAP *map = &state->groups[state->current_group];
1867 if (state->current_group == state->num_groups)
1870 sid_peek_rid(&map->sid, &rid);
1872 fill_displayentry(s, rid, 0, map->nt_name, NULL, map->comment, entry);
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 struct dom_sid *sid, enum lsa_SidType type)
1888 struct group_search *state;
1890 state = talloc(search, 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,
1917 const struct dom_sid *sid)
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, 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(TALLOC_CTX *mem_ctx, uint32_t acct_flags)
1951 struct pdb_methods *pdb = pdb_get_methods();
1952 struct pdb_search *result;
1954 result = pdb_search_init(mem_ctx, PDB_USER_SEARCH);
1955 if (result == NULL) {
1959 if (!pdb->search_users(pdb, result, acct_flags)) {
1960 TALLOC_FREE(result);
1966 struct pdb_search *pdb_search_groups(TALLOC_CTX *mem_ctx)
1968 struct pdb_methods *pdb = pdb_get_methods();
1969 struct pdb_search *result;
1971 result = pdb_search_init(mem_ctx, PDB_GROUP_SEARCH);
1972 if (result == NULL) {
1976 if (!pdb->search_groups(pdb, result)) {
1977 TALLOC_FREE(result);
1983 struct pdb_search *pdb_search_aliases(TALLOC_CTX *mem_ctx, const struct 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(mem_ctx, PDB_ALIAS_SEARCH);
1991 if (result == NULL) {
1995 if (!pdb->search_aliases(pdb, result, sid)) {
1996 TALLOC_FREE(result);
2002 uint32_t pdb_search_entries(struct pdb_search *search,
2003 uint32_t start_idx, uint32_t max_entries,
2004 struct samr_displayentry **result)
2006 struct samr_displayentry *end_entry;
2007 uint32_t end_idx = start_idx+max_entries-1;
2009 /* The first entry needs to be searched after the last. Otherwise the
2010 * first entry might have moved due to a realloc during the search for
2011 * the last entry. */
2013 end_entry = pdb_search_getentry(search, end_idx);
2014 *result = pdb_search_getentry(search, start_idx);
2016 if (end_entry != NULL)
2019 if (start_idx >= search->num_entries)
2022 return search->num_entries - start_idx;
2025 /*******************************************************************
2027 *******************************************************************/
2029 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, struct dom_sid *sid,
2030 time_t *pass_last_set_time)
2032 struct pdb_methods *pdb = pdb_get_methods();
2033 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2034 pass_last_set_time);
2037 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2038 const struct dom_sid *sid)
2040 struct pdb_methods *pdb = pdb_get_methods();
2041 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2044 bool pdb_del_trusteddom_pw(const char *domain)
2046 struct pdb_methods *pdb = pdb_get_methods();
2047 return pdb->del_trusteddom_pw(pdb, domain);
2050 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2051 struct trustdom_info ***domains)
2053 struct pdb_methods *pdb = pdb_get_methods();
2054 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2057 /*******************************************************************
2058 the defaults for trustdom methods:
2059 these simply call the original passdb/secrets.c actions,
2060 to be replaced by pdb_ldap.
2061 *******************************************************************/
2063 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2066 struct dom_sid *sid,
2067 time_t *pass_last_set_time)
2069 return secrets_fetch_trusted_domain_password(domain, pwd,
2070 sid, pass_last_set_time);
2074 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2077 const struct dom_sid *sid)
2079 return secrets_store_trusted_domain_password(domain, pwd, sid);
2082 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2085 return trusted_domain_password_delete(domain);
2088 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2089 TALLOC_CTX *mem_ctx,
2090 uint32_t *num_domains,
2091 struct trustdom_info ***domains)
2093 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2096 static struct pdb_domain_info *pdb_default_get_domain_info(
2097 struct pdb_methods *m, TALLOC_CTX *mem_ctx)
2102 /*******************************************************************
2103 Create a pdb_methods structure and initialize it with the default
2104 operations. In this way a passdb module can simply implement
2105 the functionality it cares about. However, normally this is done
2106 in groups of related functions.
2107 *******************************************************************/
2109 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2111 /* allocate memory for the structure as its own talloc CTX */
2113 *methods = talloc_zero(NULL, struct pdb_methods);
2114 if (*methods == NULL) {
2115 return NT_STATUS_NO_MEMORY;
2118 (*methods)->get_domain_info = pdb_default_get_domain_info;
2119 (*methods)->getsampwnam = pdb_default_getsampwnam;
2120 (*methods)->getsampwsid = pdb_default_getsampwsid;
2121 (*methods)->create_user = pdb_default_create_user;
2122 (*methods)->delete_user = pdb_default_delete_user;
2123 (*methods)->add_sam_account = pdb_default_add_sam_account;
2124 (*methods)->update_sam_account = pdb_default_update_sam_account;
2125 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2126 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2127 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2129 (*methods)->getgrsid = pdb_default_getgrsid;
2130 (*methods)->getgrgid = pdb_default_getgrgid;
2131 (*methods)->getgrnam = pdb_default_getgrnam;
2132 (*methods)->create_dom_group = pdb_default_create_dom_group;
2133 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2134 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2135 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2136 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2137 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2138 (*methods)->enum_group_members = pdb_default_enum_group_members;
2139 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2140 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2141 (*methods)->add_groupmem = pdb_default_add_groupmem;
2142 (*methods)->del_groupmem = pdb_default_del_groupmem;
2143 (*methods)->create_alias = pdb_default_create_alias;
2144 (*methods)->delete_alias = pdb_default_delete_alias;
2145 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2146 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2147 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2148 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2149 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2150 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2151 (*methods)->lookup_rids = pdb_default_lookup_rids;
2152 (*methods)->get_account_policy = pdb_default_get_account_policy;
2153 (*methods)->set_account_policy = pdb_default_set_account_policy;
2154 (*methods)->get_seq_num = pdb_default_get_seq_num;
2155 (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2156 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2157 (*methods)->sid_to_id = pdb_default_sid_to_id;
2159 (*methods)->search_groups = pdb_default_search_groups;
2160 (*methods)->search_aliases = pdb_default_search_aliases;
2162 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2163 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2164 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2165 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2167 return NT_STATUS_OK;