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"
30 #define DBGC_CLASS DBGC_PASSDB
34 static struct pdb_init_function_entry *backends = NULL;
36 static void lazy_initialize_passdb(void)
38 static bool initialized = False;
46 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
48 enum lsa_SidType *psid_name_use,
49 union unid_t *unix_id);
51 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
53 struct pdb_init_function_entry *entry = backends;
55 if(version != PASSDB_INTERFACE_VERSION) {
56 DEBUG(0,("Can't register passdb backend!\n"
57 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
58 "while this version of samba uses version %d\n",
59 version,PASSDB_INTERFACE_VERSION));
60 return NT_STATUS_OBJECT_TYPE_MISMATCH;
64 return NT_STATUS_INVALID_PARAMETER;
67 DEBUG(5,("Attempting to register passdb backend %s\n", name));
69 /* Check for duplicates */
70 if (pdb_find_backend_entry(name)) {
71 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
72 return NT_STATUS_OBJECT_NAME_COLLISION;
75 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
76 entry->name = smb_xstrdup(name);
79 DLIST_ADD(backends, entry);
80 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
84 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
86 struct pdb_init_function_entry *entry = backends;
89 if (strcmp(entry->name, name)==0) return entry;
97 * The event context for the passdb backend. I know this is a bad hack and yet
98 * another static variable, but our pdb API is a global thing per
99 * definition. The first use for this is the LDAP idle function, more might be
102 * I don't feel too bad about this static variable, it replaces the
103 * smb_idle_event_list that used to exist in lib/module.c. -- VL
106 static struct event_context *pdb_event_ctx;
108 struct event_context *pdb_get_event_context(void)
110 return pdb_event_ctx;
113 /******************************************************************
114 Make a pdb_methods from scratch
115 *******************************************************************/
117 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
119 char *module_name = smb_xstrdup(selected);
120 char *module_location = NULL, *p;
121 struct pdb_init_function_entry *entry;
122 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
124 lazy_initialize_passdb();
126 p = strchr(module_name, ':');
130 module_location = p+1;
131 trim_char(module_location, ' ', ' ');
134 trim_char(module_name, ' ', ' ');
137 DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected, module_name));
139 entry = pdb_find_backend_entry(module_name);
141 /* Try to find a module that contains this module */
143 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
144 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
145 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
146 SAFE_FREE(module_name);
147 return NT_STATUS_UNSUCCESSFUL;
151 /* No such backend found */
153 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
154 SAFE_FREE(module_name);
155 return NT_STATUS_INVALID_PARAMETER;
158 DEBUG(5,("Found pdb backend %s\n", module_name));
160 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
161 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
162 selected, nt_errstr(nt_status)));
163 SAFE_FREE(module_name);
167 SAFE_FREE(module_name);
169 DEBUG(5,("pdb backend %s has a valid init\n", selected));
174 /******************************************************************
175 Return an already initialized pdb_methods structure
176 *******************************************************************/
178 static struct pdb_methods *pdb_get_methods_reload( bool reload )
180 static struct pdb_methods *pdb = NULL;
182 if ( pdb && reload ) {
183 pdb->free_private_data( &(pdb->private_data) );
184 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
186 if (asprintf(&msg, "pdb_get_methods_reload: "
187 "failed to get pdb methods for backend %s\n",
188 lp_passdb_backend()) > 0) {
191 smb_panic("pdb_get_methods_reload");
197 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
199 if (asprintf(&msg, "pdb_get_methods_reload: "
200 "failed to get pdb methods for backend %s\n",
201 lp_passdb_backend()) > 0) {
204 smb_panic("pdb_get_methods_reload");
212 static struct pdb_methods *pdb_get_methods(void)
214 return pdb_get_methods_reload(False);
217 struct pdb_domain_info *pdb_get_domain_info(TALLOC_CTX *mem_ctx)
219 struct pdb_methods *pdb = pdb_get_methods();
220 return pdb->get_domain_info(pdb, mem_ctx);
224 * @brief Check if the user account has been locked out and try to unlock it.
226 * If the user has been automatically locked out and a lockout duration is set,
227 * then check if we can unlock the account and reset the bad password values.
229 * @param[in] sampass The sam user to check.
231 * @return True if the function was successfull, false on an error.
233 static bool pdb_try_account_unlock(struct samu *sampass)
235 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
237 if (acb_info & (ACB_NORMAL|ACB_AUTOLOCK)) {
238 uint32_t lockout_duration;
239 time_t bad_password_time;
240 time_t now = time(NULL);
243 ok = pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
246 DEBUG(0, ("pdb_try_account_unlock: "
247 "pdb_get_account_policy failed.\n"));
251 if (lockout_duration == (uint32_t) -1 ||
252 lockout_duration == 0) {
253 DEBUG(9, ("pdb_try_account_unlock: No reset duration, "
254 "can't reset autolock\n"));
257 lockout_duration *= 60;
259 bad_password_time = pdb_get_bad_password_time(sampass);
260 if (bad_password_time == (time_t) 0) {
261 DEBUG(2, ("pdb_try_account_unlock: Account %s "
262 "administratively locked out "
263 "with no bad password "
264 "time. Leaving locked out.\n",
265 pdb_get_username(sampass)));
269 if ((bad_password_time +
270 convert_uint32_t_to_time_t(lockout_duration)) < now) {
273 pdb_set_acct_ctrl(sampass, acb_info & ~ACB_AUTOLOCK,
275 pdb_set_bad_password_count(sampass, 0, PDB_CHANGED);
276 pdb_set_bad_password_time(sampass, 0, PDB_CHANGED);
279 status = pdb_update_sam_account(sampass);
281 if (!NT_STATUS_IS_OK(status)) {
282 DEBUG(0, ("_samr_OpenUser: Couldn't "
283 "update account %s - %s\n",
284 pdb_get_username(sampass),
295 * @brief Get a sam user structure by the given username.
297 * This functions also checks if the account has been automatically locked out
298 * and unlocks it if a lockout duration time has been defined and the time has
301 * @param[in] sam_acct The sam user structure to fill.
303 * @param[in] username The username to look for.
305 * @return True on success, false on error.
307 bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
309 struct pdb_methods *pdb = pdb_get_methods();
310 struct samu *for_cache;
311 const struct dom_sid *user_sid;
315 status = pdb->getsampwnam(pdb, sam_acct, username);
316 if (!NT_STATUS_IS_OK(status)) {
320 ok = pdb_try_account_unlock(sam_acct);
322 DEBUG(1, ("pdb_getsampwnam: Failed to unlock account %s\n",
326 for_cache = samu_new(NULL);
327 if (for_cache == NULL) {
331 if (!pdb_copy_sam_account(for_cache, sam_acct)) {
332 TALLOC_FREE(for_cache);
336 user_sid = pdb_get_user_sid(for_cache);
338 memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
339 data_blob_const(user_sid, sizeof(*user_sid)),
345 /**********************************************************************
346 **********************************************************************/
348 static bool guest_user_info( struct samu *user )
352 const char *guestname = lp_guestaccount();
354 if ( !(pwd = getpwnam_alloc(talloc_autofree_context(), guestname ) ) ) {
355 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
360 result = samu_set_unix(user, pwd );
364 return NT_STATUS_IS_OK( result );
368 * @brief Get a sam user structure by the given username.
370 * This functions also checks if the account has been automatically locked out
371 * and unlocks it if a lockout duration time has been defined and the time has
375 * @param[in] sam_acct The sam user structure to fill.
377 * @param[in] sid The user SDI to look up.
379 * @return True on success, false on error.
381 bool pdb_getsampwsid(struct samu *sam_acct, const struct dom_sid *sid)
383 struct pdb_methods *pdb = pdb_get_methods();
388 /* hard code the Guest RID of 501 */
390 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
393 if ( rid == DOMAIN_RID_GUEST ) {
394 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
395 return guest_user_info( sam_acct );
398 /* check the cache first */
400 cache_data = memcache_lookup_talloc(
401 NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
403 if (cache_data != NULL) {
404 struct samu *cache_copy = talloc_get_type_abort(
405 cache_data, struct samu);
407 ok = pdb_copy_sam_account(sam_acct, cache_copy);
409 ok = NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
416 ok = pdb_try_account_unlock(sam_acct);
418 DEBUG(1, ("pdb_getsampwsid: Failed to unlock account %s\n",
419 sam_acct->username));
425 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
426 TALLOC_CTX *tmp_ctx, const char *name,
427 uint32_t acb_info, uint32_t *rid)
429 struct samu *sam_pass;
433 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
434 return NT_STATUS_NO_MEMORY;
437 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
438 char *add_script = NULL;
442 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
443 add_script = talloc_strdup(tmp_ctx,
444 lp_adduser_script());
446 add_script = talloc_strdup(tmp_ctx,
447 lp_addmachine_script());
450 if (!add_script || add_script[0] == '\0') {
451 DEBUG(3, ("Could not find user %s and no add script "
453 return NT_STATUS_NO_SUCH_USER;
456 /* lowercase the username before creating the Unix account for
457 compatibility with previous Samba releases */
458 fstrcpy( name2, name );
460 add_script = talloc_all_string_sub(tmp_ctx,
465 return NT_STATUS_NO_MEMORY;
467 add_ret = smbrun(add_script,NULL);
468 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
469 add_script, add_ret));
471 smb_nscd_flush_user_cache();
476 pwd = Get_Pwnam_alloc(tmp_ctx, name);
479 /* we have a valid SID coming out of this call */
481 status = samu_alloc_rid_unix( sam_pass, pwd );
485 if (!NT_STATUS_IS_OK(status)) {
486 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
490 if (!sid_peek_check_rid(get_global_sam_sid(),
491 pdb_get_user_sid(sam_pass), rid)) {
492 DEBUG(0, ("Could not get RID of fresh user\n"));
493 return NT_STATUS_INTERNAL_ERROR;
496 /* Use the username case specified in the original request */
498 pdb_set_username( sam_pass, name, PDB_SET );
500 /* Disable the account on creation, it does not have a reasonable password yet. */
502 acb_info |= ACB_DISABLED;
504 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
506 status = pdb_add_sam_account(sam_pass);
508 TALLOC_FREE(sam_pass);
513 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32_t flags,
516 struct pdb_methods *pdb = pdb_get_methods();
517 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
520 /****************************************************************************
521 Delete a UNIX user on demand.
522 ****************************************************************************/
524 static int smb_delete_user(const char *unix_user)
526 char *del_script = NULL;
531 if ( strequal( unix_user, "root" ) ) {
532 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
536 del_script = talloc_strdup(talloc_tos(), lp_deluser_script());
537 if (!del_script || !*del_script) {
540 del_script = talloc_all_string_sub(talloc_tos(),
547 ret = smbrun(del_script,NULL);
550 smb_nscd_flush_user_cache();
552 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
557 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
559 struct samu *sam_acct)
564 status = pdb_delete_sam_account(sam_acct);
565 if (!NT_STATUS_IS_OK(status)) {
570 * Now delete the unix side ....
571 * note: we don't check if the delete really happened as the script is
572 * not necessary present and maybe the sysadmin doesn't want to delete
576 /* always lower case the username before handing it off to
579 fstrcpy( username, pdb_get_username(sam_acct) );
580 strlower_m( username );
582 smb_delete_user( username );
587 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
589 struct pdb_methods *pdb = pdb_get_methods();
592 /* sanity check to make sure we don't delete root */
594 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
595 return NT_STATUS_NO_SUCH_USER;
599 return NT_STATUS_ACCESS_DENIED;
602 return pdb->delete_user(pdb, mem_ctx, sam_acct);
605 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
607 struct pdb_methods *pdb = pdb_get_methods();
608 return pdb->add_sam_account(pdb, sam_acct);
611 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
613 struct pdb_methods *pdb = pdb_get_methods();
615 memcache_flush(NULL, PDB_GETPWSID_CACHE);
617 return pdb->update_sam_account(pdb, sam_acct);
620 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
622 struct pdb_methods *pdb = pdb_get_methods();
624 memcache_flush(NULL, PDB_GETPWSID_CACHE);
626 return pdb->delete_sam_account(pdb, sam_acct);
629 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
631 struct pdb_methods *pdb = pdb_get_methods();
635 memcache_flush(NULL, PDB_GETPWSID_CACHE);
637 /* sanity check to make sure we don't rename root */
639 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
640 return NT_STATUS_NO_SUCH_USER;
644 return NT_STATUS_ACCESS_DENIED;
647 status = pdb->rename_sam_account(pdb, oldname, newname);
649 /* always flush the cache here just to be safe */
655 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
657 struct pdb_methods *pdb = pdb_get_methods();
658 return pdb->update_login_attempts(pdb, sam_acct, success);
661 bool pdb_getgrsid(GROUP_MAP *map, struct dom_sid sid)
663 struct pdb_methods *pdb = pdb_get_methods();
664 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
667 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
669 struct pdb_methods *pdb = pdb_get_methods();
670 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
673 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
675 struct pdb_methods *pdb = pdb_get_methods();
676 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
679 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
684 struct dom_sid group_sid;
688 grp = getgrnam(name);
693 if (smb_create_group(name, &gid) != 0) {
694 return NT_STATUS_ACCESS_DENIED;
701 return NT_STATUS_ACCESS_DENIED;
704 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
705 if (!pdb_new_rid(rid)) {
706 return NT_STATUS_ACCESS_DENIED;
709 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
712 sid_compose(&group_sid, get_global_sam_sid(), *rid);
714 return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
715 SID_NAME_DOM_GRP, name, NULL);
718 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
721 struct pdb_methods *pdb = pdb_get_methods();
722 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
725 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
729 struct dom_sid group_sid;
733 const char *grp_name;
736 map.gid = (gid_t) -1;
738 sid_compose(&group_sid, get_global_sam_sid(), rid);
740 if (!get_domain_group_from_sid(group_sid, &map)) {
741 DEBUG(10, ("Could not find group for rid %d\n", rid));
742 return NT_STATUS_NO_SUCH_GROUP;
745 /* We need the group name for the smb_delete_group later on */
747 if (map.gid == (gid_t)-1) {
748 return NT_STATUS_NO_SUCH_GROUP;
751 grp = getgrgid(map.gid);
753 return NT_STATUS_NO_SUCH_GROUP;
756 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
758 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
759 if (grp_name == NULL) {
760 return NT_STATUS_NO_MEMORY;
763 status = pdb_delete_group_mapping_entry(group_sid);
765 if (!NT_STATUS_IS_OK(status)) {
769 /* Don't check the result of smb_delete_group */
771 smb_delete_group(grp_name);
776 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32_t rid)
778 struct pdb_methods *pdb = pdb_get_methods();
779 return pdb->delete_dom_group(pdb, mem_ctx, rid);
782 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
784 struct pdb_methods *pdb = pdb_get_methods();
785 return pdb->add_group_mapping_entry(pdb, map);
788 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
790 struct pdb_methods *pdb = pdb_get_methods();
791 return pdb->update_group_mapping_entry(pdb, map);
794 NTSTATUS pdb_delete_group_mapping_entry(struct dom_sid sid)
796 struct pdb_methods *pdb = pdb_get_methods();
797 return pdb->delete_group_mapping_entry(pdb, sid);
800 bool pdb_enum_group_mapping(const struct dom_sid *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
801 size_t *p_num_entries, bool unix_only)
803 struct pdb_methods *pdb = pdb_get_methods();
804 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
805 pp_rmap, p_num_entries, unix_only));
808 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
809 const struct dom_sid *sid,
810 uint32_t **pp_member_rids,
811 size_t *p_num_members)
813 struct pdb_methods *pdb = pdb_get_methods();
816 result = pdb->enum_group_members(pdb, mem_ctx,
817 sid, pp_member_rids, p_num_members);
819 /* special check for rid 513 */
821 if ( !NT_STATUS_IS_OK( result ) ) {
824 sid_peek_rid( sid, &rid );
826 if ( rid == DOMAIN_RID_USERS ) {
828 *pp_member_rids = NULL;
837 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
838 struct dom_sid **pp_sids, gid_t **pp_gids,
839 size_t *p_num_groups)
841 struct pdb_methods *pdb = pdb_get_methods();
842 return pdb->enum_group_memberships(
844 pp_sids, pp_gids, p_num_groups);
847 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
849 struct samu *sampass)
854 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
855 (grp = getgrgid(gid)) == NULL) {
856 return NT_STATUS_INVALID_PRIMARY_GROUP;
859 if (smb_set_primary_group(grp->gr_name,
860 pdb_get_username(sampass)) != 0) {
861 return NT_STATUS_ACCESS_DENIED;
867 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
869 struct pdb_methods *pdb = pdb_get_methods();
870 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
874 * Helper function to see whether a user is in a group. We can't use
875 * user_in_group_sid here because this creates dependencies only smbd can
879 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
880 const struct dom_sid *group_sid)
882 struct dom_sid *sids;
884 size_t i, num_groups;
886 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
892 for (i=0; i<num_groups; i++) {
893 if (sid_equal(group_sid, &sids[i])) {
900 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
905 struct dom_sid group_sid, member_sid;
906 struct samu *account = NULL;
910 const char *group_name;
914 map.gid = (gid_t) -1;
916 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
917 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
919 if (!get_domain_group_from_sid(group_sid, &map) ||
920 (map.gid == (gid_t)-1) ||
921 ((grp = getgrgid(map.gid)) == NULL)) {
922 return NT_STATUS_NO_SUCH_GROUP;
925 group_name = talloc_strdup(mem_ctx, grp->gr_name);
926 if (group_name == NULL) {
927 return NT_STATUS_NO_MEMORY;
930 if ( !(account = samu_new( NULL )) ) {
931 return NT_STATUS_NO_MEMORY;
934 if (!pdb_getsampwsid(account, &member_sid) ||
935 !sid_to_uid(&member_sid, &uid) ||
936 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
937 return NT_STATUS_NO_SUCH_USER;
940 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
941 return NT_STATUS_MEMBER_IN_GROUP;
945 * ok, the group exist, the user exist, the user is not in the group,
946 * we can (finally) add it to the group !
949 smb_add_user_group(group_name, pwd->pw_name);
951 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
952 return NT_STATUS_ACCESS_DENIED;
958 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
961 struct pdb_methods *pdb = pdb_get_methods();
962 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
965 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
970 struct dom_sid group_sid, member_sid;
971 struct samu *account = NULL;
975 const char *group_name;
978 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
979 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
981 if (!get_domain_group_from_sid(group_sid, &map) ||
982 (map.gid == (gid_t)-1) ||
983 ((grp = getgrgid(map.gid)) == NULL)) {
984 return NT_STATUS_NO_SUCH_GROUP;
987 group_name = talloc_strdup(mem_ctx, grp->gr_name);
988 if (group_name == NULL) {
989 return NT_STATUS_NO_MEMORY;
992 if ( !(account = samu_new( NULL )) ) {
993 return NT_STATUS_NO_MEMORY;
996 if (!pdb_getsampwsid(account, &member_sid) ||
997 !sid_to_uid(&member_sid, &uid) ||
998 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
999 return NT_STATUS_NO_SUCH_USER;
1002 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1003 return NT_STATUS_MEMBER_NOT_IN_GROUP;
1007 * ok, the group exist, the user exist, the user is in the group,
1008 * we can (finally) delete it from the group!
1011 smb_delete_user_group(group_name, pwd->pw_name);
1013 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1014 return NT_STATUS_ACCESS_DENIED;
1017 return NT_STATUS_OK;
1020 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1021 uint32_t member_rid)
1023 struct pdb_methods *pdb = pdb_get_methods();
1024 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
1027 NTSTATUS pdb_create_alias(const char *name, uint32_t *rid)
1029 struct pdb_methods *pdb = pdb_get_methods();
1030 return pdb->create_alias(pdb, name, rid);
1033 NTSTATUS pdb_delete_alias(const struct dom_sid *sid)
1035 struct pdb_methods *pdb = pdb_get_methods();
1036 return pdb->delete_alias(pdb, sid);
1039 NTSTATUS pdb_get_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1041 struct pdb_methods *pdb = pdb_get_methods();
1042 return pdb->get_aliasinfo(pdb, sid, info);
1045 NTSTATUS pdb_set_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1047 struct pdb_methods *pdb = pdb_get_methods();
1048 return pdb->set_aliasinfo(pdb, sid, info);
1051 NTSTATUS pdb_add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1053 struct pdb_methods *pdb = pdb_get_methods();
1054 return pdb->add_aliasmem(pdb, alias, member);
1057 NTSTATUS pdb_del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1059 struct pdb_methods *pdb = pdb_get_methods();
1060 return pdb->del_aliasmem(pdb, alias, member);
1063 NTSTATUS pdb_enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
1064 struct dom_sid **pp_members, size_t *p_num_members)
1066 struct pdb_methods *pdb = pdb_get_methods();
1067 return pdb->enum_aliasmem(pdb, alias, mem_ctx, pp_members,
1071 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1072 const struct dom_sid *domain_sid,
1073 const struct dom_sid *members, size_t num_members,
1074 uint32_t **pp_alias_rids,
1075 size_t *p_num_alias_rids)
1077 struct pdb_methods *pdb = pdb_get_methods();
1078 return pdb->enum_alias_memberships(pdb, mem_ctx,
1080 members, num_members,
1085 NTSTATUS pdb_lookup_rids(const struct dom_sid *domain_sid,
1089 enum lsa_SidType *attrs)
1091 struct pdb_methods *pdb = pdb_get_methods();
1092 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
1096 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
1097 * in the samba code.
1098 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
1099 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
1100 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
1101 * down to are pdb_getsampwnam and pdb_getgrnam instead of
1103 * But in principle, it the call belongs to the API and might get
1104 * used in this context some day.
1107 NTSTATUS pdb_lookup_names(const struct dom_sid *domain_sid,
1111 enum lsa_SidType *attrs)
1113 struct pdb_methods *pdb = pdb_get_methods();
1114 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
1118 bool pdb_get_account_policy(enum pdb_policy_type type, uint32_t *value)
1120 struct pdb_methods *pdb = pdb_get_methods();
1124 status = pdb->get_account_policy(pdb, type, value);
1127 return NT_STATUS_IS_OK(status);
1130 bool pdb_set_account_policy(enum pdb_policy_type type, uint32_t value)
1132 struct pdb_methods *pdb = pdb_get_methods();
1136 status = pdb->set_account_policy(pdb, type, value);
1139 return NT_STATUS_IS_OK(status);
1142 bool pdb_get_seq_num(time_t *seq_num)
1144 struct pdb_methods *pdb = pdb_get_methods();
1145 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1148 bool pdb_uid_to_sid(uid_t uid, struct dom_sid *sid)
1150 struct pdb_methods *pdb = pdb_get_methods();
1151 return pdb->uid_to_sid(pdb, uid, sid);
1154 bool pdb_gid_to_sid(gid_t gid, struct dom_sid *sid)
1156 struct pdb_methods *pdb = pdb_get_methods();
1157 return pdb->gid_to_sid(pdb, gid, sid);
1160 bool pdb_sid_to_id(const struct dom_sid *sid, union unid_t *id,
1161 enum lsa_SidType *type)
1163 struct pdb_methods *pdb = pdb_get_methods();
1164 return pdb->sid_to_id(pdb, sid, id, type);
1167 uint32_t pdb_capabilities(void)
1169 struct pdb_methods *pdb = pdb_get_methods();
1170 return pdb->capabilities(pdb);
1173 /********************************************************************
1174 Allocate a new RID from the passdb backend. Verify that it is free
1175 by calling lookup_global_sam_rid() to verify that the RID is not
1176 in use. This handles servers that have existing users or groups
1177 with add RIDs (assigned from previous algorithmic mappings)
1178 ********************************************************************/
1180 bool pdb_new_rid(uint32_t *rid)
1182 struct pdb_methods *pdb = pdb_get_methods();
1183 const char *name = NULL;
1184 enum lsa_SidType type;
1185 uint32_t allocated_rid = 0;
1189 if ((pdb_capabilities() & PDB_CAP_STORE_RIDS) == 0) {
1190 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1195 if (algorithmic_rid_base() != BASE_RID) {
1196 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1197 "without algorithmic RIDs is chosen.\n"));
1198 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1199 "add', set the maximum used RID\n"));
1200 DEBUGADD(0, ("and remove the parameter\n"));
1204 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1205 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1209 /* Attempt to get an unused RID (max tires is 250...yes that it is
1210 and arbitrary number I pulkled out of my head). -- jerry */
1212 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1215 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1219 /* validate that the RID is not in use */
1221 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1228 if ( allocated_rid == 0 ) {
1229 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1233 *rid = allocated_rid;
1238 /***************************************************************
1239 Initialize the static context (at smbd startup etc).
1241 If uninitialised, context will auto-init on first use.
1242 ***************************************************************/
1244 bool initialize_password_db(bool reload, struct event_context *event_ctx)
1246 pdb_event_ctx = event_ctx;
1247 return (pdb_get_methods_reload(reload) != NULL);
1251 /***************************************************************************
1252 Default implementations of some functions.
1253 ****************************************************************************/
1255 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1257 return NT_STATUS_NO_SUCH_USER;
1260 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1262 return NT_STATUS_NO_SUCH_USER;
1265 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1267 return NT_STATUS_NOT_IMPLEMENTED;
1270 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1272 return NT_STATUS_NOT_IMPLEMENTED;
1275 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1277 return NT_STATUS_NOT_IMPLEMENTED;
1280 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1282 return NT_STATUS_NOT_IMPLEMENTED;
1285 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1287 /* Only the pdb_nds backend implements this, by
1288 * default just return ok. */
1289 return NT_STATUS_OK;
1292 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t *value)
1294 return account_policy_get(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1297 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t value)
1299 return account_policy_set(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1302 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1304 *seq_num = time(NULL);
1305 return NT_STATUS_OK;
1308 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1309 struct dom_sid *sid)
1311 struct samu *sampw = NULL;
1312 struct passwd *unix_pw;
1315 unix_pw = sys_getpwuid( uid );
1318 DEBUG(4,("pdb_default_uid_to_sid: host has no idea of uid "
1319 "%lu\n", (unsigned long)uid));
1323 if ( !(sampw = samu_new( NULL )) ) {
1324 DEBUG(0,("pdb_default_uid_to_sid: samu_new() failed!\n"));
1329 ret = NT_STATUS_IS_OK(
1330 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1334 DEBUG(5, ("pdb_default_uid_to_sid: Did not find user "
1335 "%s (%u)\n", unix_pw->pw_name, (unsigned int)uid));
1340 sid_copy(sid, pdb_get_user_sid(sampw));
1347 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1348 struct dom_sid *sid)
1352 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1356 sid_copy(sid, &map.sid);
1360 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1361 const struct dom_sid *sid,
1362 union unid_t *id, enum lsa_SidType *type)
1364 TALLOC_CTX *mem_ctx;
1369 mem_ctx = talloc_new(NULL);
1371 if (mem_ctx == NULL) {
1372 DEBUG(0, ("talloc_new failed\n"));
1376 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1377 /* Here we might have users as well as groups and aliases */
1378 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1382 /* check for "Unix User" */
1384 if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1386 *type = SID_NAME_USER;
1391 /* check for "Unix Group" */
1393 if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1395 *type = SID_NAME_ALIAS;
1402 if (sid_check_is_in_builtin(sid) ||
1403 sid_check_is_in_wellknown_domain(sid)) {
1404 /* Here we only have aliases */
1406 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1407 DEBUG(10, ("Could not find map for sid %s\n",
1408 sid_string_dbg(sid)));
1411 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1412 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1413 DEBUG(10, ("Map for sid %s is a %s, expected an "
1414 "alias\n", sid_string_dbg(sid),
1415 sid_type_lookup(map.sid_name_use)));
1420 *type = SID_NAME_ALIAS;
1425 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1426 sid_string_dbg(sid)));
1430 TALLOC_FREE(mem_ctx);
1434 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1445 /* We only look at our own sam, so don't care about imported stuff */
1446 winbind_env = winbind_env_set();
1447 (void)winbind_off();
1449 if ((grp = getgrgid(gid)) == NULL) {
1450 /* allow winbindd lookups, but only if they weren't already disabled */
1454 /* Primary group members */
1456 while ((pwd = getpwent()) != NULL) {
1457 if (pwd->pw_gid == gid) {
1458 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1466 /* Secondary group members */
1467 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1468 struct passwd *pw = getpwnam(*gr);
1472 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1481 /* allow winbindd lookups, but only if they weren't already disabled */
1489 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1490 TALLOC_CTX *mem_ctx,
1491 const struct dom_sid *group,
1492 uint32_t **pp_member_rids,
1493 size_t *p_num_members)
1499 *pp_member_rids = NULL;
1502 if (!sid_to_gid(group, &gid))
1503 return NT_STATUS_NO_SUCH_GROUP;
1505 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1506 return NT_STATUS_NO_SUCH_GROUP;
1509 return NT_STATUS_OK;
1511 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32_t, num_uids);
1513 for (i=0; i<num_uids; i++) {
1516 uid_to_sid(&sid, uids[i]);
1518 if (!sid_check_is_in_our_domain(&sid)) {
1519 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1520 "in our domain\n"));
1524 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1525 *p_num_members += 1;
1528 return NT_STATUS_OK;
1531 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1532 TALLOC_CTX *mem_ctx,
1534 struct dom_sid **pp_sids,
1536 size_t *p_num_groups)
1541 const char *username = pdb_get_username(user);
1544 /* Ignore the primary group SID. Honor the real Unix primary group.
1545 The primary group SID is only of real use to Windows clients */
1547 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1548 return NT_STATUS_NO_SUCH_USER;
1555 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1556 return NT_STATUS_NO_SUCH_USER;
1559 if (*p_num_groups == 0) {
1560 smb_panic("primary group missing");
1563 *pp_sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, *p_num_groups);
1565 if (*pp_sids == NULL) {
1566 TALLOC_FREE(*pp_gids);
1567 return NT_STATUS_NO_MEMORY;
1570 for (i=0; i<*p_num_groups; i++) {
1571 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1574 return NT_STATUS_OK;
1577 /*******************************************************************
1578 Look up a rid in the SAM we're responsible for (i.e. passdb)
1579 ********************************************************************/
1581 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32_t rid,
1583 enum lsa_SidType *psid_name_use,
1584 union unid_t *unix_id)
1586 struct samu *sam_account = NULL;
1591 *psid_name_use = SID_NAME_UNKNOWN;
1593 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1594 (unsigned int)rid));
1596 sid_compose(&sid, get_global_sam_sid(), rid);
1598 /* see if the passdb can help us with the name of the user */
1600 if ( !(sam_account = samu_new( NULL )) ) {
1604 /* BEING ROOT BLOCK */
1606 if (pdb_getsampwsid(sam_account, &sid)) {
1609 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1610 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1612 TALLOC_FREE(sam_account);
1616 *psid_name_use = SID_NAME_USER;
1618 TALLOC_FREE(sam_account);
1620 if (unix_id == NULL) {
1624 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1628 unix_id->uid = pw->pw_uid;
1632 TALLOC_FREE(sam_account);
1634 ret = pdb_getgrsid(&map, sid);
1636 /* END BECOME_ROOT BLOCK */
1638 /* do not resolve SIDs to a name unless there is a valid
1639 gid associated with it */
1641 if ( ret && (map.gid != (gid_t)-1) ) {
1642 *name = talloc_strdup(mem_ctx, map.nt_name);
1643 *psid_name_use = map.sid_name_use;
1646 unix_id->gid = map.gid;
1652 /* Windows will always map RID 513 to something. On a non-domain
1653 controller, this gets mapped to SERVER\None. */
1656 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1660 if ( rid == DOMAIN_RID_USERS ) {
1661 *name = talloc_strdup(mem_ctx, "None" );
1662 *psid_name_use = SID_NAME_DOM_GRP;
1670 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1671 const struct dom_sid *domain_sid,
1675 enum lsa_SidType *attrs)
1679 bool have_mapped = False;
1680 bool have_unmapped = False;
1682 if (sid_check_is_builtin(domain_sid)) {
1684 for (i=0; i<num_rids; i++) {
1687 if (lookup_builtin_rid(names, rids[i], &name)) {
1688 attrs[i] = SID_NAME_ALIAS;
1690 DEBUG(5,("lookup_rids: %s:%d\n",
1691 names[i], attrs[i]));
1694 have_unmapped = True;
1695 attrs[i] = SID_NAME_UNKNOWN;
1701 /* Should not happen, but better check once too many */
1702 if (!sid_check_is_domain(domain_sid)) {
1703 return NT_STATUS_INVALID_HANDLE;
1706 for (i = 0; i < num_rids; i++) {
1709 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1712 return NT_STATUS_NO_MEMORY;
1715 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1718 have_unmapped = True;
1719 attrs[i] = SID_NAME_UNKNOWN;
1725 result = NT_STATUS_NONE_MAPPED;
1728 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1734 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1735 const struct dom_sid *domain_sid,
1739 enum lsa_SidType *attrs)
1743 bool have_mapped = False;
1744 bool have_unmapped = False;
1746 if (sid_check_is_builtin(domain_sid)) {
1748 for (i=0; i<num_names; i++) {
1751 if (lookup_builtin_name(names[i], &rid)) {
1752 attrs[i] = SID_NAME_ALIAS;
1754 DEBUG(5,("lookup_rids: %s:%d\n",
1755 names[i], attrs[i]));
1758 have_unmapped = True;
1759 attrs[i] = SID_NAME_UNKNOWN;
1765 /* Should not happen, but better check once too many */
1766 if (!sid_check_is_domain(domain_sid)) {
1767 return NT_STATUS_INVALID_HANDLE;
1770 for (i = 0; i < num_names; i++) {
1771 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1772 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1773 rids[i], attrs[i]));
1776 have_unmapped = True;
1777 attrs[i] = SID_NAME_UNKNOWN;
1783 result = NT_STATUS_NONE_MAPPED;
1786 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1792 static int pdb_search_destructor(struct pdb_search *search)
1794 if ((!search->search_ended) && (search->search_end != NULL)) {
1795 search->search_end(search);
1800 struct pdb_search *pdb_search_init(TALLOC_CTX *mem_ctx,
1801 enum pdb_search_type type)
1803 struct pdb_search *result;
1805 result = talloc(mem_ctx, struct pdb_search);
1806 if (result == NULL) {
1807 DEBUG(0, ("talloc failed\n"));
1811 result->type = type;
1812 result->cache = NULL;
1813 result->num_entries = 0;
1814 result->cache_size = 0;
1815 result->search_ended = False;
1816 result->search_end = NULL;
1818 /* Segfault appropriately if not initialized */
1819 result->next_entry = NULL;
1820 result->search_end = NULL;
1822 talloc_set_destructor(result, pdb_search_destructor);
1827 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32_t rid,
1828 uint16_t acct_flags,
1829 const char *account_name,
1830 const char *fullname,
1831 const char *description,
1832 struct samr_displayentry *entry)
1835 entry->acct_flags = acct_flags;
1837 if (account_name != NULL)
1838 entry->account_name = talloc_strdup(mem_ctx, account_name);
1840 entry->account_name = "";
1842 if (fullname != NULL)
1843 entry->fullname = talloc_strdup(mem_ctx, fullname);
1845 entry->fullname = "";
1847 if (description != NULL)
1848 entry->description = talloc_strdup(mem_ctx, description);
1850 entry->description = "";
1853 struct group_search {
1855 size_t num_groups, current_group;
1858 static bool next_entry_groups(struct pdb_search *s,
1859 struct samr_displayentry *entry)
1861 struct group_search *state = (struct group_search *)s->private_data;
1863 GROUP_MAP *map = &state->groups[state->current_group];
1865 if (state->current_group == state->num_groups)
1868 sid_peek_rid(&map->sid, &rid);
1870 fill_displayentry(s, rid, 0, map->nt_name, NULL, map->comment, entry);
1872 state->current_group += 1;
1876 static void search_end_groups(struct pdb_search *search)
1878 struct group_search *state =
1879 (struct group_search *)search->private_data;
1880 SAFE_FREE(state->groups);
1883 static bool pdb_search_grouptype(struct pdb_search *search,
1884 const struct dom_sid *sid, enum lsa_SidType type)
1886 struct group_search *state;
1888 state = talloc(search, struct group_search);
1889 if (state == NULL) {
1890 DEBUG(0, ("talloc failed\n"));
1894 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1896 DEBUG(0, ("Could not enum groups\n"));
1900 state->current_group = 0;
1901 search->private_data = state;
1902 search->next_entry = next_entry_groups;
1903 search->search_end = search_end_groups;
1907 static bool pdb_default_search_groups(struct pdb_methods *methods,
1908 struct pdb_search *search)
1910 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1913 static bool pdb_default_search_aliases(struct pdb_methods *methods,
1914 struct pdb_search *search,
1915 const struct dom_sid *sid)
1918 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1921 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1924 if (idx < search->num_entries)
1925 return &search->cache[idx];
1927 if (search->search_ended)
1930 while (idx >= search->num_entries) {
1931 struct samr_displayentry entry;
1933 if (!search->next_entry(search, &entry)) {
1934 search->search_end(search);
1935 search->search_ended = True;
1939 ADD_TO_LARGE_ARRAY(search, struct samr_displayentry,
1940 entry, &search->cache, &search->num_entries,
1941 &search->cache_size);
1944 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1947 struct pdb_search *pdb_search_users(TALLOC_CTX *mem_ctx, uint32_t acct_flags)
1949 struct pdb_methods *pdb = pdb_get_methods();
1950 struct pdb_search *result;
1952 result = pdb_search_init(mem_ctx, PDB_USER_SEARCH);
1953 if (result == NULL) {
1957 if (!pdb->search_users(pdb, result, acct_flags)) {
1958 TALLOC_FREE(result);
1964 struct pdb_search *pdb_search_groups(TALLOC_CTX *mem_ctx)
1966 struct pdb_methods *pdb = pdb_get_methods();
1967 struct pdb_search *result;
1969 result = pdb_search_init(mem_ctx, PDB_GROUP_SEARCH);
1970 if (result == NULL) {
1974 if (!pdb->search_groups(pdb, result)) {
1975 TALLOC_FREE(result);
1981 struct pdb_search *pdb_search_aliases(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
1983 struct pdb_methods *pdb = pdb_get_methods();
1984 struct pdb_search *result;
1986 if (pdb == NULL) return NULL;
1988 result = pdb_search_init(mem_ctx, PDB_ALIAS_SEARCH);
1989 if (result == NULL) {
1993 if (!pdb->search_aliases(pdb, result, sid)) {
1994 TALLOC_FREE(result);
2000 uint32_t pdb_search_entries(struct pdb_search *search,
2001 uint32_t start_idx, uint32_t max_entries,
2002 struct samr_displayentry **result)
2004 struct samr_displayentry *end_entry;
2005 uint32_t end_idx = start_idx+max_entries-1;
2007 /* The first entry needs to be searched after the last. Otherwise the
2008 * first entry might have moved due to a realloc during the search for
2009 * the last entry. */
2011 end_entry = pdb_search_getentry(search, end_idx);
2012 *result = pdb_search_getentry(search, start_idx);
2014 if (end_entry != NULL)
2017 if (start_idx >= search->num_entries)
2020 return search->num_entries - start_idx;
2023 /*******************************************************************
2025 *******************************************************************/
2027 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, struct dom_sid *sid,
2028 time_t *pass_last_set_time)
2030 struct pdb_methods *pdb = pdb_get_methods();
2031 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2032 pass_last_set_time);
2035 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2036 const struct dom_sid *sid)
2038 struct pdb_methods *pdb = pdb_get_methods();
2039 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2042 bool pdb_del_trusteddom_pw(const char *domain)
2044 struct pdb_methods *pdb = pdb_get_methods();
2045 return pdb->del_trusteddom_pw(pdb, domain);
2048 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2049 struct trustdom_info ***domains)
2051 struct pdb_methods *pdb = pdb_get_methods();
2052 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2055 /*******************************************************************
2056 the defaults for trustdom methods:
2057 these simply call the original passdb/secrets.c actions,
2058 to be replaced by pdb_ldap.
2059 *******************************************************************/
2061 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2064 struct dom_sid *sid,
2065 time_t *pass_last_set_time)
2067 return secrets_fetch_trusted_domain_password(domain, pwd,
2068 sid, pass_last_set_time);
2072 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2075 const struct dom_sid *sid)
2077 return secrets_store_trusted_domain_password(domain, pwd, sid);
2080 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2083 return trusted_domain_password_delete(domain);
2086 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2087 TALLOC_CTX *mem_ctx,
2088 uint32_t *num_domains,
2089 struct trustdom_info ***domains)
2091 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2094 static struct pdb_domain_info *pdb_default_get_domain_info(
2095 struct pdb_methods *m, TALLOC_CTX *mem_ctx)
2100 /*******************************************************************
2101 Create a pdb_methods structure and initialize it with the default
2102 operations. In this way a passdb module can simply implement
2103 the functionality it cares about. However, normally this is done
2104 in groups of related functions.
2105 *******************************************************************/
2107 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2109 /* allocate memory for the structure as its own talloc CTX */
2111 *methods = talloc_zero(talloc_autofree_context(), struct pdb_methods);
2112 if (*methods == NULL) {
2113 return NT_STATUS_NO_MEMORY;
2116 (*methods)->get_domain_info = pdb_default_get_domain_info;
2117 (*methods)->getsampwnam = pdb_default_getsampwnam;
2118 (*methods)->getsampwsid = pdb_default_getsampwsid;
2119 (*methods)->create_user = pdb_default_create_user;
2120 (*methods)->delete_user = pdb_default_delete_user;
2121 (*methods)->add_sam_account = pdb_default_add_sam_account;
2122 (*methods)->update_sam_account = pdb_default_update_sam_account;
2123 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2124 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2125 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2127 (*methods)->getgrsid = pdb_default_getgrsid;
2128 (*methods)->getgrgid = pdb_default_getgrgid;
2129 (*methods)->getgrnam = pdb_default_getgrnam;
2130 (*methods)->create_dom_group = pdb_default_create_dom_group;
2131 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2132 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2133 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2134 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2135 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2136 (*methods)->enum_group_members = pdb_default_enum_group_members;
2137 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2138 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2139 (*methods)->add_groupmem = pdb_default_add_groupmem;
2140 (*methods)->del_groupmem = pdb_default_del_groupmem;
2141 (*methods)->create_alias = pdb_default_create_alias;
2142 (*methods)->delete_alias = pdb_default_delete_alias;
2143 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2144 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2145 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2146 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2147 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2148 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2149 (*methods)->lookup_rids = pdb_default_lookup_rids;
2150 (*methods)->get_account_policy = pdb_default_get_account_policy;
2151 (*methods)->set_account_policy = pdb_default_set_account_policy;
2152 (*methods)->get_seq_num = pdb_default_get_seq_num;
2153 (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2154 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2155 (*methods)->sid_to_id = pdb_default_sid_to_id;
2157 (*methods)->search_groups = pdb_default_search_groups;
2158 (*methods)->search_aliases = pdb_default_search_aliases;
2160 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2161 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2162 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2163 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2165 return NT_STATUS_OK;