s3-ipasam: add ipasam_get_trusted_domain_by_sid()
[Samba.git] / source3 / passdb / pdb_interface.c
blobc286cc18f745c9729384d528647a99c29433215a
1 /*
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/>.
23 #include "includes.h"
24 #include "secrets.h"
25 #include "../librpc/gen_ndr/samr.h"
26 #include "memcache.h"
27 #include "nsswitch/winbind_client.h"
28 #include "../libcli/security/security.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_PASSDB
33 static_decl_pdb;
35 static struct pdb_init_function_entry *backends = NULL;
37 static void lazy_initialize_passdb(void)
39 static bool initialized = False;
40 if(initialized) {
41 return;
43 static_init_pdb;
44 initialized = True;
47 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
48 const char **name,
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;
64 if (!name || !init) {
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);
78 entry->init = init;
80 DLIST_ADD(backends, entry);
81 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
82 return NT_STATUS_OK;
85 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
87 struct pdb_init_function_entry *entry = backends;
89 while(entry) {
90 if (strcmp(entry->name, name)==0) return entry;
91 entry = entry->next;
94 return NULL;
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
101 * added later.
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, ':');
129 if (p) {
130 *p = 0;
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 */
143 if (!entry) {
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 */
153 if(!entry) {
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);
165 return nt_status;
168 SAFE_FREE(module_name);
170 DEBUG(5,("pdb backend %s has a valid init\n", selected));
172 return nt_status;
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() ) ) ) {
186 char *msg = NULL;
187 if (asprintf(&msg, "pdb_get_methods_reload: "
188 "failed to get pdb methods for backend %s\n",
189 lp_passdb_backend()) > 0) {
190 smb_panic(msg);
191 } else {
192 smb_panic("pdb_get_methods_reload");
197 if ( !pdb ) {
198 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
199 char *msg = NULL;
200 if (asprintf(&msg, "pdb_get_methods_reload: "
201 "failed to get pdb methods for backend %s\n",
202 lp_passdb_backend()) > 0) {
203 smb_panic(msg);
204 } else {
205 smb_panic("pdb_get_methods_reload");
210 return pdb;
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_info & ACB_AUTOLOCK)) {
239 uint32_t lockout_duration;
240 time_t bad_password_time;
241 time_t now = time(NULL);
242 bool ok;
244 ok = pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
245 &lockout_duration);
246 if (!ok) {
247 DEBUG(0, ("pdb_try_account_unlock: "
248 "pdb_get_account_policy failed.\n"));
249 return false;
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"));
256 return false;
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)));
267 return true;
270 if ((bad_password_time +
271 convert_uint32_t_to_time_t(lockout_duration)) < now) {
272 NTSTATUS status;
274 pdb_set_acct_ctrl(sampass, acb_info & ~ACB_AUTOLOCK,
275 PDB_CHANGED);
276 pdb_set_bad_password_count(sampass, 0, PDB_CHANGED);
277 pdb_set_bad_password_time(sampass, 0, PDB_CHANGED);
279 become_root();
280 status = pdb_update_sam_account(sampass);
281 unbecome_root();
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),
286 nt_errstr(status)));
287 return false;
292 return true;
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
300 * elapsed.
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;
313 NTSTATUS status;
314 bool ok;
316 status = pdb->getsampwnam(pdb, sam_acct, username);
317 if (!NT_STATUS_IS_OK(status)) {
318 return false;
321 ok = pdb_try_account_unlock(sam_acct);
322 if (!ok) {
323 DEBUG(1, ("pdb_getsampwnam: Failed to unlock account %s\n",
324 username));
327 for_cache = samu_new(NULL);
328 if (for_cache == NULL) {
329 return False;
332 if (!pdb_copy_sam_account(for_cache, sam_acct)) {
333 TALLOC_FREE(for_cache);
334 return False;
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)),
341 &for_cache);
343 return True;
346 /**********************************************************************
347 **********************************************************************/
349 static bool guest_user_info( struct samu *user )
351 struct passwd *pwd;
352 NTSTATUS result;
353 const char *guestname = lp_guestaccount();
355 pwd = Get_Pwnam_alloc(talloc_tos(), guestname);
356 if (pwd == NULL) {
357 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
358 guestname));
359 return False;
362 result = samu_set_unix(user, pwd );
364 TALLOC_FREE( 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
374 * elapsed.
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();
386 uint32_t rid;
387 void *cache_data;
388 bool ok = false;
390 /* hard code the Guest RID of 501 */
392 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
393 return False;
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);
410 } else {
411 ok = NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
414 if (!ok) {
415 return false;
418 ok = pdb_try_account_unlock(sam_acct);
419 if (!ok) {
420 DEBUG(1, ("pdb_getsampwsid: Failed to unlock account %s\n",
421 sam_acct->username));
424 return true;
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;
432 NTSTATUS status;
433 struct passwd *pwd;
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;
441 int add_ret;
442 fstring name2;
444 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
445 add_script = talloc_strdup(tmp_ctx,
446 lp_adduser_script());
447 } else {
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 "
454 "defined\n", name));
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 );
461 strlower_m( name2 );
462 add_script = talloc_all_string_sub(tmp_ctx,
463 add_script,
464 "%u",
465 name2);
466 if (!add_script) {
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));
472 if (add_ret == 0) {
473 smb_nscd_flush_user_cache();
476 flush_pwnam_cache();
478 pwd = Get_Pwnam_alloc(tmp_ctx, name);
480 if(pwd == NULL) {
481 DEBUG(3, ("Could not find user %s, add script did not work\n", name));
482 return NT_STATUS_NO_SUCH_USER;
486 /* we have a valid SID coming out of this call */
488 status = samu_alloc_rid_unix( sam_pass, pwd );
490 TALLOC_FREE( pwd );
492 if (!NT_STATUS_IS_OK(status)) {
493 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
494 return status;
497 if (!sid_peek_check_rid(get_global_sam_sid(),
498 pdb_get_user_sid(sam_pass), rid)) {
499 DEBUG(0, ("Could not get RID of fresh user\n"));
500 return NT_STATUS_INTERNAL_ERROR;
503 /* Use the username case specified in the original request */
505 pdb_set_username( sam_pass, name, PDB_SET );
507 /* Disable the account on creation, it does not have a reasonable password yet. */
509 acb_info |= ACB_DISABLED;
511 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
513 status = pdb_add_sam_account(sam_pass);
515 TALLOC_FREE(sam_pass);
517 return status;
520 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32_t flags,
521 uint32_t *rid)
523 struct pdb_methods *pdb = pdb_get_methods();
524 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
527 /****************************************************************************
528 Delete a UNIX user on demand.
529 ****************************************************************************/
531 static int smb_delete_user(const char *unix_user)
533 char *del_script = NULL;
534 int ret;
536 /* safety check */
538 if ( strequal( unix_user, "root" ) ) {
539 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
540 return -1;
543 del_script = talloc_strdup(talloc_tos(), lp_deluser_script());
544 if (!del_script || !*del_script) {
545 return -1;
547 del_script = talloc_all_string_sub(talloc_tos(),
548 del_script,
549 "%u",
550 unix_user);
551 if (!del_script) {
552 return -1;
554 ret = smbrun(del_script,NULL);
555 flush_pwnam_cache();
556 if (ret == 0) {
557 smb_nscd_flush_user_cache();
559 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
561 return ret;
564 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
565 TALLOC_CTX *mem_ctx,
566 struct samu *sam_acct)
568 NTSTATUS status;
569 fstring username;
571 status = pdb_delete_sam_account(sam_acct);
572 if (!NT_STATUS_IS_OK(status)) {
573 return status;
577 * Now delete the unix side ....
578 * note: we don't check if the delete really happened as the script is
579 * not necessary present and maybe the sysadmin doesn't want to delete
580 * the unix side
583 /* always lower case the username before handing it off to
584 external scripts */
586 fstrcpy( username, pdb_get_username(sam_acct) );
587 strlower_m( username );
589 smb_delete_user( username );
591 return status;
594 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
596 struct pdb_methods *pdb = pdb_get_methods();
597 uid_t uid = -1;
599 /* sanity check to make sure we don't delete root */
601 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
602 return NT_STATUS_NO_SUCH_USER;
605 if ( uid == 0 ) {
606 return NT_STATUS_ACCESS_DENIED;
609 return pdb->delete_user(pdb, mem_ctx, sam_acct);
612 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
614 struct pdb_methods *pdb = pdb_get_methods();
615 return pdb->add_sam_account(pdb, sam_acct);
618 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
620 struct pdb_methods *pdb = pdb_get_methods();
622 memcache_flush(NULL, PDB_GETPWSID_CACHE);
624 return pdb->update_sam_account(pdb, sam_acct);
627 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
629 struct pdb_methods *pdb = pdb_get_methods();
631 memcache_flush(NULL, PDB_GETPWSID_CACHE);
633 return pdb->delete_sam_account(pdb, sam_acct);
636 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
638 struct pdb_methods *pdb = pdb_get_methods();
639 uid_t uid;
640 NTSTATUS status;
642 memcache_flush(NULL, PDB_GETPWSID_CACHE);
644 /* sanity check to make sure we don't rename root */
646 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
647 return NT_STATUS_NO_SUCH_USER;
650 if ( uid == 0 ) {
651 return NT_STATUS_ACCESS_DENIED;
654 status = pdb->rename_sam_account(pdb, oldname, newname);
656 /* always flush the cache here just to be safe */
657 flush_pwnam_cache();
659 return status;
662 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
664 struct pdb_methods *pdb = pdb_get_methods();
665 return pdb->update_login_attempts(pdb, sam_acct, success);
668 bool pdb_getgrsid(GROUP_MAP *map, struct dom_sid sid)
670 struct pdb_methods *pdb = pdb_get_methods();
671 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
674 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
676 struct pdb_methods *pdb = pdb_get_methods();
677 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
680 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
682 struct pdb_methods *pdb = pdb_get_methods();
683 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
686 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
687 TALLOC_CTX *mem_ctx,
688 const char *name,
689 uint32_t *rid)
691 struct dom_sid group_sid;
692 struct group *grp;
693 fstring tmp;
695 grp = getgrnam(name);
697 if (grp == NULL) {
698 gid_t gid;
700 if (smb_create_group(name, &gid) != 0) {
701 return NT_STATUS_ACCESS_DENIED;
704 grp = getgrgid(gid);
707 if (grp == NULL) {
708 return NT_STATUS_ACCESS_DENIED;
711 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
712 if (!pdb_new_rid(rid)) {
713 return NT_STATUS_ACCESS_DENIED;
715 } else {
716 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
719 sid_compose(&group_sid, get_global_sam_sid(), *rid);
721 return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
722 SID_NAME_DOM_GRP, name, NULL);
725 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
726 uint32_t *rid)
728 struct pdb_methods *pdb = pdb_get_methods();
729 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
732 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
733 TALLOC_CTX *mem_ctx,
734 uint32_t rid)
736 struct dom_sid group_sid;
737 GROUP_MAP map;
738 NTSTATUS status;
739 struct group *grp;
740 const char *grp_name;
742 /* coverity */
743 map.gid = (gid_t) -1;
745 sid_compose(&group_sid, get_global_sam_sid(), rid);
747 if (!get_domain_group_from_sid(group_sid, &map)) {
748 DEBUG(10, ("Could not find group for rid %d\n", rid));
749 return NT_STATUS_NO_SUCH_GROUP;
752 /* We need the group name for the smb_delete_group later on */
754 if (map.gid == (gid_t)-1) {
755 return NT_STATUS_NO_SUCH_GROUP;
758 grp = getgrgid(map.gid);
759 if (grp == NULL) {
760 return NT_STATUS_NO_SUCH_GROUP;
763 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
765 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
766 if (grp_name == NULL) {
767 return NT_STATUS_NO_MEMORY;
770 status = pdb_delete_group_mapping_entry(group_sid);
772 if (!NT_STATUS_IS_OK(status)) {
773 return status;
776 /* Don't check the result of smb_delete_group */
778 smb_delete_group(grp_name);
780 return NT_STATUS_OK;
783 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32_t rid)
785 struct pdb_methods *pdb = pdb_get_methods();
786 return pdb->delete_dom_group(pdb, mem_ctx, rid);
789 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
791 struct pdb_methods *pdb = pdb_get_methods();
792 return pdb->add_group_mapping_entry(pdb, map);
795 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
797 struct pdb_methods *pdb = pdb_get_methods();
798 return pdb->update_group_mapping_entry(pdb, map);
801 NTSTATUS pdb_delete_group_mapping_entry(struct dom_sid sid)
803 struct pdb_methods *pdb = pdb_get_methods();
804 return pdb->delete_group_mapping_entry(pdb, sid);
807 bool pdb_enum_group_mapping(const struct dom_sid *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
808 size_t *p_num_entries, bool unix_only)
810 struct pdb_methods *pdb = pdb_get_methods();
811 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
812 pp_rmap, p_num_entries, unix_only));
815 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
816 const struct dom_sid *sid,
817 uint32_t **pp_member_rids,
818 size_t *p_num_members)
820 struct pdb_methods *pdb = pdb_get_methods();
821 NTSTATUS result;
823 result = pdb->enum_group_members(pdb, mem_ctx,
824 sid, pp_member_rids, p_num_members);
826 /* special check for rid 513 */
828 if ( !NT_STATUS_IS_OK( result ) ) {
829 uint32_t rid;
831 sid_peek_rid( sid, &rid );
833 if ( rid == DOMAIN_RID_USERS ) {
834 *p_num_members = 0;
835 *pp_member_rids = NULL;
837 return NT_STATUS_OK;
841 return result;
844 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
845 struct dom_sid **pp_sids, gid_t **pp_gids,
846 size_t *p_num_groups)
848 struct pdb_methods *pdb = pdb_get_methods();
849 return pdb->enum_group_memberships(
850 pdb, mem_ctx, user,
851 pp_sids, pp_gids, p_num_groups);
854 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
855 TALLOC_CTX *mem_ctx,
856 struct samu *sampass)
858 struct group *grp;
859 gid_t gid;
861 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
862 (grp = getgrgid(gid)) == NULL) {
863 return NT_STATUS_INVALID_PRIMARY_GROUP;
866 if (smb_set_primary_group(grp->gr_name,
867 pdb_get_username(sampass)) != 0) {
868 return NT_STATUS_ACCESS_DENIED;
871 return NT_STATUS_OK;
874 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
876 struct pdb_methods *pdb = pdb_get_methods();
877 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
881 * Helper function to see whether a user is in a group. We can't use
882 * user_in_group_sid here because this creates dependencies only smbd can
883 * fulfil.
886 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
887 const struct dom_sid *group_sid)
889 struct dom_sid *sids;
890 gid_t *gids;
891 size_t i, num_groups;
893 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
894 &sids, &gids,
895 &num_groups))) {
896 return False;
899 for (i=0; i<num_groups; i++) {
900 if (dom_sid_equal(group_sid, &sids[i])) {
901 return True;
904 return False;
907 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
908 TALLOC_CTX *mem_ctx,
909 uint32_t group_rid,
910 uint32_t member_rid)
912 struct dom_sid group_sid, member_sid;
913 struct samu *account = NULL;
914 GROUP_MAP map;
915 struct group *grp;
916 struct passwd *pwd;
917 const char *group_name;
918 uid_t uid;
920 /* coverity */
921 map.gid = (gid_t) -1;
923 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
924 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
926 if (!get_domain_group_from_sid(group_sid, &map) ||
927 (map.gid == (gid_t)-1) ||
928 ((grp = getgrgid(map.gid)) == NULL)) {
929 return NT_STATUS_NO_SUCH_GROUP;
932 group_name = talloc_strdup(mem_ctx, grp->gr_name);
933 if (group_name == NULL) {
934 return NT_STATUS_NO_MEMORY;
937 if ( !(account = samu_new( NULL )) ) {
938 return NT_STATUS_NO_MEMORY;
941 if (!pdb_getsampwsid(account, &member_sid) ||
942 !sid_to_uid(&member_sid, &uid) ||
943 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
944 return NT_STATUS_NO_SUCH_USER;
947 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
948 return NT_STATUS_MEMBER_IN_GROUP;
952 * ok, the group exist, the user exist, the user is not in the group,
953 * we can (finally) add it to the group !
956 smb_add_user_group(group_name, pwd->pw_name);
958 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
959 return NT_STATUS_ACCESS_DENIED;
962 return NT_STATUS_OK;
965 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
966 uint32_t member_rid)
968 struct pdb_methods *pdb = pdb_get_methods();
969 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
972 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
973 TALLOC_CTX *mem_ctx,
974 uint32_t group_rid,
975 uint32_t member_rid)
977 struct dom_sid group_sid, member_sid;
978 struct samu *account = NULL;
979 GROUP_MAP map;
980 struct group *grp;
981 struct passwd *pwd;
982 const char *group_name;
983 uid_t uid;
985 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
986 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
988 if (!get_domain_group_from_sid(group_sid, &map) ||
989 (map.gid == (gid_t)-1) ||
990 ((grp = getgrgid(map.gid)) == NULL)) {
991 return NT_STATUS_NO_SUCH_GROUP;
994 group_name = talloc_strdup(mem_ctx, grp->gr_name);
995 if (group_name == NULL) {
996 return NT_STATUS_NO_MEMORY;
999 if ( !(account = samu_new( NULL )) ) {
1000 return NT_STATUS_NO_MEMORY;
1003 if (!pdb_getsampwsid(account, &member_sid) ||
1004 !sid_to_uid(&member_sid, &uid) ||
1005 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
1006 return NT_STATUS_NO_SUCH_USER;
1009 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1010 return NT_STATUS_MEMBER_NOT_IN_GROUP;
1014 * ok, the group exist, the user exist, the user is in the group,
1015 * we can (finally) delete it from the group!
1018 smb_delete_user_group(group_name, pwd->pw_name);
1020 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1021 return NT_STATUS_ACCESS_DENIED;
1024 return NT_STATUS_OK;
1027 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1028 uint32_t member_rid)
1030 struct pdb_methods *pdb = pdb_get_methods();
1031 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
1034 NTSTATUS pdb_create_alias(const char *name, uint32_t *rid)
1036 struct pdb_methods *pdb = pdb_get_methods();
1037 return pdb->create_alias(pdb, name, rid);
1040 NTSTATUS pdb_delete_alias(const struct dom_sid *sid)
1042 struct pdb_methods *pdb = pdb_get_methods();
1043 return pdb->delete_alias(pdb, sid);
1046 NTSTATUS pdb_get_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1048 struct pdb_methods *pdb = pdb_get_methods();
1049 return pdb->get_aliasinfo(pdb, sid, info);
1052 NTSTATUS pdb_set_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1054 struct pdb_methods *pdb = pdb_get_methods();
1055 return pdb->set_aliasinfo(pdb, sid, info);
1058 NTSTATUS pdb_add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1060 struct pdb_methods *pdb = pdb_get_methods();
1061 return pdb->add_aliasmem(pdb, alias, member);
1064 NTSTATUS pdb_del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1066 struct pdb_methods *pdb = pdb_get_methods();
1067 return pdb->del_aliasmem(pdb, alias, member);
1070 NTSTATUS pdb_enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
1071 struct dom_sid **pp_members, size_t *p_num_members)
1073 struct pdb_methods *pdb = pdb_get_methods();
1074 return pdb->enum_aliasmem(pdb, alias, mem_ctx, pp_members,
1075 p_num_members);
1078 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1079 const struct dom_sid *domain_sid,
1080 const struct dom_sid *members, size_t num_members,
1081 uint32_t **pp_alias_rids,
1082 size_t *p_num_alias_rids)
1084 struct pdb_methods *pdb = pdb_get_methods();
1085 return pdb->enum_alias_memberships(pdb, mem_ctx,
1086 domain_sid,
1087 members, num_members,
1088 pp_alias_rids,
1089 p_num_alias_rids);
1092 NTSTATUS pdb_lookup_rids(const struct dom_sid *domain_sid,
1093 int num_rids,
1094 uint32_t *rids,
1095 const char **names,
1096 enum lsa_SidType *attrs)
1098 struct pdb_methods *pdb = pdb_get_methods();
1099 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
1103 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
1104 * in the samba code.
1105 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
1106 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
1107 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
1108 * down to are pdb_getsampwnam and pdb_getgrnam instead of
1109 * pdb_lookup_names.
1110 * But in principle, it the call belongs to the API and might get
1111 * used in this context some day.
1113 #if 0
1114 NTSTATUS pdb_lookup_names(const struct dom_sid *domain_sid,
1115 int num_names,
1116 const char **names,
1117 uint32_t *rids,
1118 enum lsa_SidType *attrs)
1120 struct pdb_methods *pdb = pdb_get_methods();
1121 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
1123 #endif
1125 bool pdb_get_account_policy(enum pdb_policy_type type, uint32_t *value)
1127 struct pdb_methods *pdb = pdb_get_methods();
1128 NTSTATUS status;
1130 become_root();
1131 status = pdb->get_account_policy(pdb, type, value);
1132 unbecome_root();
1134 return NT_STATUS_IS_OK(status);
1137 bool pdb_set_account_policy(enum pdb_policy_type type, uint32_t value)
1139 struct pdb_methods *pdb = pdb_get_methods();
1140 NTSTATUS status;
1142 become_root();
1143 status = pdb->set_account_policy(pdb, type, value);
1144 unbecome_root();
1146 return NT_STATUS_IS_OK(status);
1149 bool pdb_get_seq_num(time_t *seq_num)
1151 struct pdb_methods *pdb = pdb_get_methods();
1152 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1155 bool pdb_uid_to_sid(uid_t uid, struct dom_sid *sid)
1157 struct pdb_methods *pdb = pdb_get_methods();
1158 return pdb->uid_to_sid(pdb, uid, sid);
1161 bool pdb_gid_to_sid(gid_t gid, struct dom_sid *sid)
1163 struct pdb_methods *pdb = pdb_get_methods();
1164 return pdb->gid_to_sid(pdb, gid, sid);
1167 bool pdb_sid_to_id(const struct dom_sid *sid, union unid_t *id,
1168 enum lsa_SidType *type)
1170 struct pdb_methods *pdb = pdb_get_methods();
1171 return pdb->sid_to_id(pdb, sid, id, type);
1174 uint32_t pdb_capabilities(void)
1176 struct pdb_methods *pdb = pdb_get_methods();
1177 return pdb->capabilities(pdb);
1180 /********************************************************************
1181 Allocate a new RID from the passdb backend. Verify that it is free
1182 by calling lookup_global_sam_rid() to verify that the RID is not
1183 in use. This handles servers that have existing users or groups
1184 with add RIDs (assigned from previous algorithmic mappings)
1185 ********************************************************************/
1187 bool pdb_new_rid(uint32_t *rid)
1189 struct pdb_methods *pdb = pdb_get_methods();
1190 const char *name = NULL;
1191 enum lsa_SidType type;
1192 uint32_t allocated_rid = 0;
1193 int i;
1194 TALLOC_CTX *ctx;
1196 if ((pdb_capabilities() & PDB_CAP_STORE_RIDS) == 0) {
1197 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1198 "are active\n"));
1199 return False;
1202 if (algorithmic_rid_base() != BASE_RID) {
1203 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1204 "without algorithmic RIDs is chosen.\n"));
1205 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1206 "add', set the maximum used RID\n"));
1207 DEBUGADD(0, ("and remove the parameter\n"));
1208 return False;
1211 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1212 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1213 return False;
1216 /* Attempt to get an unused RID (max tires is 250...yes that it is
1217 and arbitrary number I pulkled out of my head). -- jerry */
1219 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1220 /* get a new RID */
1222 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1223 return False;
1226 /* validate that the RID is not in use */
1228 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1229 allocated_rid = 0;
1233 TALLOC_FREE( ctx );
1235 if ( allocated_rid == 0 ) {
1236 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1237 return False;
1240 *rid = allocated_rid;
1242 return True;
1245 /***************************************************************
1246 Initialize the static context (at smbd startup etc).
1248 If uninitialised, context will auto-init on first use.
1249 ***************************************************************/
1251 bool initialize_password_db(bool reload, struct event_context *event_ctx)
1253 pdb_event_ctx = event_ctx;
1254 return (pdb_get_methods_reload(reload) != NULL);
1258 /***************************************************************************
1259 Default implementations of some functions.
1260 ****************************************************************************/
1262 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1264 return NT_STATUS_NO_SUCH_USER;
1267 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1269 return NT_STATUS_NO_SUCH_USER;
1272 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1274 return NT_STATUS_NOT_IMPLEMENTED;
1277 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1279 return NT_STATUS_NOT_IMPLEMENTED;
1282 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1284 return NT_STATUS_NOT_IMPLEMENTED;
1287 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1289 return NT_STATUS_NOT_IMPLEMENTED;
1292 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1294 /* Only the pdb_nds backend implements this, by
1295 * default just return ok. */
1296 return NT_STATUS_OK;
1299 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t *value)
1301 return account_policy_get(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1304 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t value)
1306 return account_policy_set(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1309 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1311 *seq_num = time(NULL);
1312 return NT_STATUS_OK;
1315 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1316 struct dom_sid *sid)
1318 struct samu *sampw = NULL;
1319 struct passwd *unix_pw;
1320 bool ret;
1322 unix_pw = sys_getpwuid( uid );
1324 if ( !unix_pw ) {
1325 DEBUG(4,("pdb_default_uid_to_sid: host has no idea of uid "
1326 "%lu\n", (unsigned long)uid));
1327 return False;
1330 if ( !(sampw = samu_new( NULL )) ) {
1331 DEBUG(0,("pdb_default_uid_to_sid: samu_new() failed!\n"));
1332 return False;
1335 become_root();
1336 ret = NT_STATUS_IS_OK(
1337 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1338 unbecome_root();
1340 if (!ret) {
1341 DEBUG(5, ("pdb_default_uid_to_sid: Did not find user "
1342 "%s (%u)\n", unix_pw->pw_name, (unsigned int)uid));
1343 TALLOC_FREE(sampw);
1344 return False;
1347 sid_copy(sid, pdb_get_user_sid(sampw));
1349 TALLOC_FREE(sampw);
1351 return True;
1354 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1355 struct dom_sid *sid)
1357 GROUP_MAP map;
1359 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1360 return False;
1363 sid_copy(sid, &map.sid);
1364 return True;
1367 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1368 const struct dom_sid *sid,
1369 union unid_t *id, enum lsa_SidType *type)
1371 TALLOC_CTX *mem_ctx;
1372 bool ret = False;
1373 const char *name;
1374 uint32_t rid;
1376 mem_ctx = talloc_new(NULL);
1378 if (mem_ctx == NULL) {
1379 DEBUG(0, ("talloc_new failed\n"));
1380 return False;
1383 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1384 /* Here we might have users as well as groups and aliases */
1385 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1386 goto done;
1389 /* check for "Unix User" */
1391 if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1392 id->uid = rid;
1393 *type = SID_NAME_USER;
1394 ret = True;
1395 goto done;
1398 /* check for "Unix Group" */
1400 if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1401 id->gid = rid;
1402 *type = SID_NAME_ALIAS;
1403 ret = True;
1404 goto done;
1407 /* BUILTIN */
1409 if (sid_check_is_in_builtin(sid) ||
1410 sid_check_is_in_wellknown_domain(sid)) {
1411 /* Here we only have aliases */
1412 GROUP_MAP map;
1413 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1414 DEBUG(10, ("Could not find map for sid %s\n",
1415 sid_string_dbg(sid)));
1416 goto done;
1418 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1419 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1420 DEBUG(10, ("Map for sid %s is a %s, expected an "
1421 "alias\n", sid_string_dbg(sid),
1422 sid_type_lookup(map.sid_name_use)));
1423 goto done;
1426 id->gid = map.gid;
1427 *type = SID_NAME_ALIAS;
1428 ret = True;
1429 goto done;
1432 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1433 sid_string_dbg(sid)));
1435 done:
1437 TALLOC_FREE(mem_ctx);
1438 return ret;
1441 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1443 struct group *grp;
1444 char **gr;
1445 struct passwd *pwd;
1446 bool winbind_env;
1447 bool ret = False;
1449 *pp_uids = NULL;
1450 *p_num = 0;
1452 /* We only look at our own sam, so don't care about imported stuff */
1453 winbind_env = winbind_env_set();
1454 (void)winbind_off();
1456 if ((grp = getgrgid(gid)) == NULL) {
1457 /* allow winbindd lookups, but only if they weren't already disabled */
1458 goto done;
1461 /* Primary group members */
1462 setpwent();
1463 while ((pwd = getpwent()) != NULL) {
1464 if (pwd->pw_gid == gid) {
1465 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1466 pp_uids, p_num)) {
1467 goto done;
1471 endpwent();
1473 /* Secondary group members */
1474 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1475 struct passwd *pw = getpwnam(*gr);
1477 if (pw == NULL)
1478 continue;
1479 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1480 goto done;
1484 ret = True;
1486 done:
1488 /* allow winbindd lookups, but only if they weren't already disabled */
1489 if (!winbind_env) {
1490 (void)winbind_on();
1493 return ret;
1496 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1497 TALLOC_CTX *mem_ctx,
1498 const struct dom_sid *group,
1499 uint32_t **pp_member_rids,
1500 size_t *p_num_members)
1502 gid_t gid;
1503 uid_t *uids;
1504 size_t i, num_uids;
1506 *pp_member_rids = NULL;
1507 *p_num_members = 0;
1509 if (!sid_to_gid(group, &gid))
1510 return NT_STATUS_NO_SUCH_GROUP;
1512 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1513 return NT_STATUS_NO_SUCH_GROUP;
1515 if (num_uids == 0)
1516 return NT_STATUS_OK;
1518 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32_t, num_uids);
1520 for (i=0; i<num_uids; i++) {
1521 struct dom_sid sid;
1523 uid_to_sid(&sid, uids[i]);
1525 if (!sid_check_is_in_our_domain(&sid)) {
1526 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1527 "in our domain\n"));
1528 continue;
1531 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1532 *p_num_members += 1;
1535 return NT_STATUS_OK;
1538 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1539 TALLOC_CTX *mem_ctx,
1540 struct samu *user,
1541 struct dom_sid **pp_sids,
1542 gid_t **pp_gids,
1543 size_t *p_num_groups)
1545 size_t i;
1546 gid_t gid;
1547 struct passwd *pw;
1548 const char *username = pdb_get_username(user);
1551 /* Ignore the primary group SID. Honor the real Unix primary group.
1552 The primary group SID is only of real use to Windows clients */
1554 if ( !(pw = Get_Pwnam_alloc(mem_ctx, username)) ) {
1555 return NT_STATUS_NO_SUCH_USER;
1558 gid = pw->pw_gid;
1560 TALLOC_FREE( pw );
1562 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1563 return NT_STATUS_NO_SUCH_USER;
1566 if (*p_num_groups == 0) {
1567 smb_panic("primary group missing");
1570 *pp_sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, *p_num_groups);
1572 if (*pp_sids == NULL) {
1573 TALLOC_FREE(*pp_gids);
1574 return NT_STATUS_NO_MEMORY;
1577 for (i=0; i<*p_num_groups; i++) {
1578 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1581 return NT_STATUS_OK;
1584 /*******************************************************************
1585 Look up a rid in the SAM we're responsible for (i.e. passdb)
1586 ********************************************************************/
1588 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32_t rid,
1589 const char **name,
1590 enum lsa_SidType *psid_name_use,
1591 union unid_t *unix_id)
1593 struct samu *sam_account = NULL;
1594 GROUP_MAP map;
1595 bool ret;
1596 struct dom_sid sid;
1598 *psid_name_use = SID_NAME_UNKNOWN;
1600 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1601 (unsigned int)rid));
1603 sid_compose(&sid, get_global_sam_sid(), rid);
1605 /* see if the passdb can help us with the name of the user */
1607 if ( !(sam_account = samu_new( NULL )) ) {
1608 return False;
1611 /* BEING ROOT BLOCK */
1612 become_root();
1613 if (pdb_getsampwsid(sam_account, &sid)) {
1614 struct passwd *pw;
1616 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1617 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1618 if (!*name) {
1619 TALLOC_FREE(sam_account);
1620 return False;
1623 *psid_name_use = SID_NAME_USER;
1625 TALLOC_FREE(sam_account);
1627 if (unix_id == NULL) {
1628 return True;
1631 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1632 if (pw == NULL) {
1633 return False;
1635 unix_id->uid = pw->pw_uid;
1636 TALLOC_FREE(pw);
1637 return True;
1639 TALLOC_FREE(sam_account);
1641 ret = pdb_getgrsid(&map, sid);
1642 unbecome_root();
1643 /* END BECOME_ROOT BLOCK */
1645 /* do not resolve SIDs to a name unless there is a valid
1646 gid associated with it */
1648 if ( ret && (map.gid != (gid_t)-1) ) {
1649 *name = talloc_strdup(mem_ctx, map.nt_name);
1650 *psid_name_use = map.sid_name_use;
1652 if ( unix_id ) {
1653 unix_id->gid = map.gid;
1656 return True;
1659 /* Windows will always map RID 513 to something. On a non-domain
1660 controller, this gets mapped to SERVER\None. */
1662 if ( unix_id ) {
1663 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1664 return False;
1667 if ( rid == DOMAIN_RID_USERS ) {
1668 *name = talloc_strdup(mem_ctx, "None" );
1669 *psid_name_use = SID_NAME_DOM_GRP;
1671 return True;
1674 return False;
1677 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1678 const struct dom_sid *domain_sid,
1679 int num_rids,
1680 uint32_t *rids,
1681 const char **names,
1682 enum lsa_SidType *attrs)
1684 int i;
1685 NTSTATUS result;
1686 bool have_mapped = False;
1687 bool have_unmapped = False;
1689 if (sid_check_is_builtin(domain_sid)) {
1691 for (i=0; i<num_rids; i++) {
1692 const char *name;
1694 if (lookup_builtin_rid(names, rids[i], &name)) {
1695 attrs[i] = SID_NAME_ALIAS;
1696 names[i] = name;
1697 DEBUG(5,("lookup_rids: %s:%d\n",
1698 names[i], attrs[i]));
1699 have_mapped = True;
1700 } else {
1701 have_unmapped = True;
1702 attrs[i] = SID_NAME_UNKNOWN;
1705 goto done;
1708 /* Should not happen, but better check once too many */
1709 if (!sid_check_is_domain(domain_sid)) {
1710 return NT_STATUS_INVALID_HANDLE;
1713 for (i = 0; i < num_rids; i++) {
1714 const char *name;
1716 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1717 NULL)) {
1718 if (name == NULL) {
1719 return NT_STATUS_NO_MEMORY;
1721 names[i] = name;
1722 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1723 have_mapped = True;
1724 } else {
1725 have_unmapped = True;
1726 attrs[i] = SID_NAME_UNKNOWN;
1730 done:
1732 result = NT_STATUS_NONE_MAPPED;
1734 if (have_mapped)
1735 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1737 return result;
1740 #if 0
1741 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1742 const struct dom_sid *domain_sid,
1743 int num_names,
1744 const char **names,
1745 uint32_t *rids,
1746 enum lsa_SidType *attrs)
1748 int i;
1749 NTSTATUS result;
1750 bool have_mapped = False;
1751 bool have_unmapped = False;
1753 if (sid_check_is_builtin(domain_sid)) {
1755 for (i=0; i<num_names; i++) {
1756 uint32_t rid;
1758 if (lookup_builtin_name(names[i], &rid)) {
1759 attrs[i] = SID_NAME_ALIAS;
1760 rids[i] = rid;
1761 DEBUG(5,("lookup_rids: %s:%d\n",
1762 names[i], attrs[i]));
1763 have_mapped = True;
1764 } else {
1765 have_unmapped = True;
1766 attrs[i] = SID_NAME_UNKNOWN;
1769 goto done;
1772 /* Should not happen, but better check once too many */
1773 if (!sid_check_is_domain(domain_sid)) {
1774 return NT_STATUS_INVALID_HANDLE;
1777 for (i = 0; i < num_names; i++) {
1778 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1779 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1780 rids[i], attrs[i]));
1781 have_mapped = True;
1782 } else {
1783 have_unmapped = True;
1784 attrs[i] = SID_NAME_UNKNOWN;
1788 done:
1790 result = NT_STATUS_NONE_MAPPED;
1792 if (have_mapped)
1793 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1795 return result;
1797 #endif
1799 static int pdb_search_destructor(struct pdb_search *search)
1801 if ((!search->search_ended) && (search->search_end != NULL)) {
1802 search->search_end(search);
1804 return 0;
1807 struct pdb_search *pdb_search_init(TALLOC_CTX *mem_ctx,
1808 enum pdb_search_type type)
1810 struct pdb_search *result;
1812 result = talloc(mem_ctx, struct pdb_search);
1813 if (result == NULL) {
1814 DEBUG(0, ("talloc failed\n"));
1815 return NULL;
1818 result->type = type;
1819 result->cache = NULL;
1820 result->num_entries = 0;
1821 result->cache_size = 0;
1822 result->search_ended = False;
1823 result->search_end = NULL;
1825 /* Segfault appropriately if not initialized */
1826 result->next_entry = NULL;
1827 result->search_end = NULL;
1829 talloc_set_destructor(result, pdb_search_destructor);
1831 return result;
1834 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32_t rid,
1835 uint16_t acct_flags,
1836 const char *account_name,
1837 const char *fullname,
1838 const char *description,
1839 struct samr_displayentry *entry)
1841 entry->rid = rid;
1842 entry->acct_flags = acct_flags;
1844 if (account_name != NULL)
1845 entry->account_name = talloc_strdup(mem_ctx, account_name);
1846 else
1847 entry->account_name = "";
1849 if (fullname != NULL)
1850 entry->fullname = talloc_strdup(mem_ctx, fullname);
1851 else
1852 entry->fullname = "";
1854 if (description != NULL)
1855 entry->description = talloc_strdup(mem_ctx, description);
1856 else
1857 entry->description = "";
1860 struct group_search {
1861 GROUP_MAP *groups;
1862 size_t num_groups, current_group;
1865 static bool next_entry_groups(struct pdb_search *s,
1866 struct samr_displayentry *entry)
1868 struct group_search *state = (struct group_search *)s->private_data;
1869 uint32_t rid;
1870 GROUP_MAP *map = &state->groups[state->current_group];
1872 if (state->current_group == state->num_groups)
1873 return False;
1875 sid_peek_rid(&map->sid, &rid);
1877 fill_displayentry(s, rid, 0, map->nt_name, NULL, map->comment, entry);
1879 state->current_group += 1;
1880 return True;
1883 static void search_end_groups(struct pdb_search *search)
1885 struct group_search *state =
1886 (struct group_search *)search->private_data;
1887 SAFE_FREE(state->groups);
1890 static bool pdb_search_grouptype(struct pdb_search *search,
1891 const struct dom_sid *sid, enum lsa_SidType type)
1893 struct group_search *state;
1895 state = talloc(search, struct group_search);
1896 if (state == NULL) {
1897 DEBUG(0, ("talloc failed\n"));
1898 return False;
1901 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1902 True)) {
1903 DEBUG(0, ("Could not enum groups\n"));
1904 return False;
1907 state->current_group = 0;
1908 search->private_data = state;
1909 search->next_entry = next_entry_groups;
1910 search->search_end = search_end_groups;
1911 return True;
1914 static bool pdb_default_search_groups(struct pdb_methods *methods,
1915 struct pdb_search *search)
1917 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1920 static bool pdb_default_search_aliases(struct pdb_methods *methods,
1921 struct pdb_search *search,
1922 const struct dom_sid *sid)
1925 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1928 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1929 uint32_t idx)
1931 if (idx < search->num_entries)
1932 return &search->cache[idx];
1934 if (search->search_ended)
1935 return NULL;
1937 while (idx >= search->num_entries) {
1938 struct samr_displayentry entry;
1940 if (!search->next_entry(search, &entry)) {
1941 search->search_end(search);
1942 search->search_ended = True;
1943 break;
1946 ADD_TO_LARGE_ARRAY(search, struct samr_displayentry,
1947 entry, &search->cache, &search->num_entries,
1948 &search->cache_size);
1951 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1954 struct pdb_search *pdb_search_users(TALLOC_CTX *mem_ctx, uint32_t acct_flags)
1956 struct pdb_methods *pdb = pdb_get_methods();
1957 struct pdb_search *result;
1959 result = pdb_search_init(mem_ctx, PDB_USER_SEARCH);
1960 if (result == NULL) {
1961 return NULL;
1964 if (!pdb->search_users(pdb, result, acct_flags)) {
1965 TALLOC_FREE(result);
1966 return NULL;
1968 return result;
1971 struct pdb_search *pdb_search_groups(TALLOC_CTX *mem_ctx)
1973 struct pdb_methods *pdb = pdb_get_methods();
1974 struct pdb_search *result;
1976 result = pdb_search_init(mem_ctx, PDB_GROUP_SEARCH);
1977 if (result == NULL) {
1978 return NULL;
1981 if (!pdb->search_groups(pdb, result)) {
1982 TALLOC_FREE(result);
1983 return NULL;
1985 return result;
1988 struct pdb_search *pdb_search_aliases(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
1990 struct pdb_methods *pdb = pdb_get_methods();
1991 struct pdb_search *result;
1993 if (pdb == NULL) return NULL;
1995 result = pdb_search_init(mem_ctx, PDB_ALIAS_SEARCH);
1996 if (result == NULL) {
1997 return NULL;
2000 if (!pdb->search_aliases(pdb, result, sid)) {
2001 TALLOC_FREE(result);
2002 return NULL;
2004 return result;
2007 uint32_t pdb_search_entries(struct pdb_search *search,
2008 uint32_t start_idx, uint32_t max_entries,
2009 struct samr_displayentry **result)
2011 struct samr_displayentry *end_entry;
2012 uint32_t end_idx = start_idx+max_entries-1;
2014 /* The first entry needs to be searched after the last. Otherwise the
2015 * first entry might have moved due to a realloc during the search for
2016 * the last entry. */
2018 end_entry = pdb_search_getentry(search, end_idx);
2019 *result = pdb_search_getentry(search, start_idx);
2021 if (end_entry != NULL)
2022 return max_entries;
2024 if (start_idx >= search->num_entries)
2025 return 0;
2027 return search->num_entries - start_idx;
2030 /*******************************************************************
2031 trustdom methods
2032 *******************************************************************/
2034 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, struct dom_sid *sid,
2035 time_t *pass_last_set_time)
2037 struct pdb_methods *pdb = pdb_get_methods();
2038 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2039 pass_last_set_time);
2042 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2043 const struct dom_sid *sid)
2045 struct pdb_methods *pdb = pdb_get_methods();
2046 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2049 bool pdb_del_trusteddom_pw(const char *domain)
2051 struct pdb_methods *pdb = pdb_get_methods();
2052 return pdb->del_trusteddom_pw(pdb, domain);
2055 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2056 struct trustdom_info ***domains)
2058 struct pdb_methods *pdb = pdb_get_methods();
2059 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2062 /*******************************************************************
2063 the defaults for trustdom methods:
2064 these simply call the original passdb/secrets.c actions,
2065 to be replaced by pdb_ldap.
2066 *******************************************************************/
2068 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2069 const char *domain,
2070 char** pwd,
2071 struct dom_sid *sid,
2072 time_t *pass_last_set_time)
2074 return secrets_fetch_trusted_domain_password(domain, pwd,
2075 sid, pass_last_set_time);
2079 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2080 const char* domain,
2081 const char* pwd,
2082 const struct dom_sid *sid)
2084 return secrets_store_trusted_domain_password(domain, pwd, sid);
2087 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2088 const char *domain)
2090 return trusted_domain_password_delete(domain);
2093 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2094 TALLOC_CTX *mem_ctx,
2095 uint32_t *num_domains,
2096 struct trustdom_info ***domains)
2098 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2101 /*******************************************************************
2102 trusted_domain methods
2103 *******************************************************************/
2105 NTSTATUS pdb_get_trusted_domain(TALLOC_CTX *mem_ctx, const char *domain,
2106 struct pdb_trusted_domain **td)
2108 struct pdb_methods *pdb = pdb_get_methods();
2109 return pdb->get_trusted_domain(pdb, mem_ctx, domain, td);
2112 NTSTATUS pdb_get_trusted_domain_by_sid(TALLOC_CTX *mem_ctx, struct dom_sid *sid,
2113 struct pdb_trusted_domain **td)
2115 struct pdb_methods *pdb = pdb_get_methods();
2116 return pdb->get_trusted_domain_by_sid(pdb, mem_ctx, sid, td);
2119 NTSTATUS pdb_set_trusted_domain(const char* domain,
2120 const struct pdb_trusted_domain *td)
2122 struct pdb_methods *pdb = pdb_get_methods();
2123 return pdb->set_trusted_domain(pdb, domain, td);
2126 NTSTATUS pdb_del_trusted_domain(const char *domain)
2128 struct pdb_methods *pdb = pdb_get_methods();
2129 return pdb->del_trusted_domain(pdb, domain);
2132 NTSTATUS pdb_enum_trusted_domains(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2133 struct pdb_trusted_domain ***domains)
2135 struct pdb_methods *pdb = pdb_get_methods();
2136 return pdb->enum_trusted_domains(pdb, mem_ctx, num_domains, domains);
2139 static NTSTATUS pdb_default_get_trusted_domain(struct pdb_methods *methods,
2140 TALLOC_CTX *mem_ctx,
2141 const char *domain,
2142 struct pdb_trusted_domain **td)
2144 return NT_STATUS_NOT_IMPLEMENTED;
2147 static NTSTATUS pdb_default_get_trusted_domain_by_sid(struct pdb_methods *methods,
2148 TALLOC_CTX *mem_ctx,
2149 struct dom_sid *sid,
2150 struct pdb_trusted_domain **td)
2152 return NT_STATUS_NOT_IMPLEMENTED;
2155 static NTSTATUS pdb_default_set_trusted_domain(struct pdb_methods *methods,
2156 const char* domain,
2157 const struct pdb_trusted_domain *td)
2159 return NT_STATUS_NOT_IMPLEMENTED;
2162 static NTSTATUS pdb_default_del_trusted_domain(struct pdb_methods *methods,
2163 const char *domain)
2165 return NT_STATUS_NOT_IMPLEMENTED;
2168 static NTSTATUS pdb_default_enum_trusted_domains(struct pdb_methods *methods,
2169 TALLOC_CTX *mem_ctx,
2170 uint32_t *num_domains,
2171 struct pdb_trusted_domain ***domains)
2173 return NT_STATUS_NOT_IMPLEMENTED;
2176 static struct pdb_domain_info *pdb_default_get_domain_info(
2177 struct pdb_methods *m, TALLOC_CTX *mem_ctx)
2179 return NULL;
2182 /*******************************************************************
2183 Create a pdb_methods structure and initialize it with the default
2184 operations. In this way a passdb module can simply implement
2185 the functionality it cares about. However, normally this is done
2186 in groups of related functions.
2187 *******************************************************************/
2189 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2191 /* allocate memory for the structure as its own talloc CTX */
2193 *methods = talloc_zero(NULL, struct pdb_methods);
2194 if (*methods == NULL) {
2195 return NT_STATUS_NO_MEMORY;
2198 (*methods)->get_domain_info = pdb_default_get_domain_info;
2199 (*methods)->getsampwnam = pdb_default_getsampwnam;
2200 (*methods)->getsampwsid = pdb_default_getsampwsid;
2201 (*methods)->create_user = pdb_default_create_user;
2202 (*methods)->delete_user = pdb_default_delete_user;
2203 (*methods)->add_sam_account = pdb_default_add_sam_account;
2204 (*methods)->update_sam_account = pdb_default_update_sam_account;
2205 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2206 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2207 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2209 (*methods)->getgrsid = pdb_default_getgrsid;
2210 (*methods)->getgrgid = pdb_default_getgrgid;
2211 (*methods)->getgrnam = pdb_default_getgrnam;
2212 (*methods)->create_dom_group = pdb_default_create_dom_group;
2213 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2214 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2215 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2216 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2217 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2218 (*methods)->enum_group_members = pdb_default_enum_group_members;
2219 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2220 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2221 (*methods)->add_groupmem = pdb_default_add_groupmem;
2222 (*methods)->del_groupmem = pdb_default_del_groupmem;
2223 (*methods)->create_alias = pdb_default_create_alias;
2224 (*methods)->delete_alias = pdb_default_delete_alias;
2225 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2226 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2227 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2228 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2229 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2230 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2231 (*methods)->lookup_rids = pdb_default_lookup_rids;
2232 (*methods)->get_account_policy = pdb_default_get_account_policy;
2233 (*methods)->set_account_policy = pdb_default_set_account_policy;
2234 (*methods)->get_seq_num = pdb_default_get_seq_num;
2235 (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2236 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2237 (*methods)->sid_to_id = pdb_default_sid_to_id;
2239 (*methods)->search_groups = pdb_default_search_groups;
2240 (*methods)->search_aliases = pdb_default_search_aliases;
2242 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2243 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2244 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2245 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2247 (*methods)->get_trusted_domain = pdb_default_get_trusted_domain;
2248 (*methods)->get_trusted_domain_by_sid = pdb_default_get_trusted_domain_by_sid;
2249 (*methods)->set_trusted_domain = pdb_default_set_trusted_domain;
2250 (*methods)->del_trusted_domain = pdb_default_del_trusted_domain;
2251 (*methods)->enum_trusted_domains = pdb_default_enum_trusted_domains;
2253 return NT_STATUS_OK;