s4-dsdb: Initial implementation for Tombstone reanimation module
[Samba.git] / source3 / passdb / pdb_interface.c
blobb8247f25952035e2b090ce5e37c628116a475bed
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 "system/passwd.h"
25 #include "passdb.h"
26 #include "secrets.h"
27 #include "messages.h"
28 #include "../librpc/gen_ndr/samr.h"
29 #include "../librpc/gen_ndr/drsblobs.h"
30 #include "../librpc/gen_ndr/ndr_drsblobs.h"
31 #include "../librpc/gen_ndr/idmap.h"
32 #include "../lib/util/memcache.h"
33 #include "nsswitch/winbind_client.h"
34 #include "../libcli/security/security.h"
35 #include "../lib/util/util_pw.h"
36 #include "passdb/pdb_secrets.h"
37 #include "lib/util_sid_passdb.h"
38 #include "idmap_cache.h"
40 #undef DBGC_CLASS
41 #define DBGC_CLASS DBGC_PASSDB
43 static_decl_pdb;
45 static struct pdb_init_function_entry *backends = NULL;
47 static void lazy_initialize_passdb(void)
49 static bool initialized = False;
50 if(initialized) {
51 return;
53 static_init_pdb;
54 initialized = True;
57 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
58 const char **name,
59 enum lsa_SidType *psid_name_use,
60 uid_t *uid, gid_t *gid);
62 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
64 struct pdb_init_function_entry *entry = backends;
66 if(version != PASSDB_INTERFACE_VERSION) {
67 DEBUG(0,("Can't register passdb backend!\n"
68 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
69 "while this version of samba uses version %d\n",
70 version,PASSDB_INTERFACE_VERSION));
71 return NT_STATUS_OBJECT_TYPE_MISMATCH;
74 if (!name || !init) {
75 return NT_STATUS_INVALID_PARAMETER;
78 DEBUG(5,("Attempting to register passdb backend %s\n", name));
80 /* Check for duplicates */
81 if (pdb_find_backend_entry(name)) {
82 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
83 return NT_STATUS_OBJECT_NAME_COLLISION;
86 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
87 entry->name = smb_xstrdup(name);
88 entry->init = init;
90 DLIST_ADD(backends, entry);
91 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
92 return NT_STATUS_OK;
95 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
97 struct pdb_init_function_entry *entry = backends;
99 while(entry) {
100 if (strcmp(entry->name, name)==0) return entry;
101 entry = entry->next;
104 return NULL;
107 const struct pdb_init_function_entry *pdb_get_backends(void)
109 return backends;
114 * The event context for the passdb backend. I know this is a bad hack and yet
115 * another static variable, but our pdb API is a global thing per
116 * definition. The first use for this is the LDAP idle function, more might be
117 * added later.
119 * I don't feel too bad about this static variable, it replaces the
120 * smb_idle_event_list that used to exist in lib/module.c. -- VL
123 static struct tevent_context *pdb_tevent_ctx;
125 struct tevent_context *pdb_get_tevent_context(void)
127 return pdb_tevent_ctx;
130 /******************************************************************
131 Make a pdb_methods from scratch
132 *******************************************************************/
134 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
136 char *module_name = smb_xstrdup(selected);
137 char *module_location = NULL, *p;
138 struct pdb_init_function_entry *entry;
139 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
141 lazy_initialize_passdb();
143 p = strchr(module_name, ':');
145 if (p) {
146 *p = 0;
147 module_location = p+1;
148 trim_char(module_location, ' ', ' ');
151 trim_char(module_name, ' ', ' ');
154 DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected, module_name));
156 entry = pdb_find_backend_entry(module_name);
158 /* Try to find a module that contains this module */
159 if (!entry) {
160 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
161 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
162 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
163 SAFE_FREE(module_name);
164 return NT_STATUS_UNSUCCESSFUL;
168 /* No such backend found */
169 if(!entry) {
170 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
171 SAFE_FREE(module_name);
172 return NT_STATUS_INVALID_PARAMETER;
175 DEBUG(5,("Found pdb backend %s\n", module_name));
177 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
178 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
179 selected, nt_errstr(nt_status)));
180 SAFE_FREE(module_name);
181 return nt_status;
184 SAFE_FREE(module_name);
186 DEBUG(5,("pdb backend %s has a valid init\n", selected));
188 return nt_status;
191 /******************************************************************
192 Return an already initialized pdb_methods structure
193 *******************************************************************/
195 static struct pdb_methods *pdb_get_methods_reload( bool reload )
197 static struct pdb_methods *pdb = NULL;
199 if ( pdb && reload ) {
200 if (pdb->free_private_data != NULL) {
201 pdb->free_private_data( &(pdb->private_data) );
203 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
204 return NULL;
208 if ( !pdb ) {
209 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
210 return NULL;
214 return pdb;
217 static struct pdb_methods *pdb_get_methods(void)
219 struct pdb_methods *pdb;
221 pdb = pdb_get_methods_reload(false);
222 if (!pdb) {
223 char *msg = NULL;
224 if (asprintf(&msg, "pdb_get_methods: "
225 "failed to get pdb methods for backend %s\n",
226 lp_passdb_backend()) > 0) {
227 smb_panic(msg);
228 } else {
229 smb_panic("pdb_get_methods");
233 return pdb;
236 struct pdb_domain_info *pdb_get_domain_info(TALLOC_CTX *mem_ctx)
238 struct pdb_methods *pdb = pdb_get_methods();
239 return pdb->get_domain_info(pdb, mem_ctx);
243 * @brief Check if the user account has been locked out and try to unlock it.
245 * If the user has been automatically locked out and a lockout duration is set,
246 * then check if we can unlock the account and reset the bad password values.
248 * @param[in] sampass The sam user to check.
250 * @return True if the function was successfull, false on an error.
252 static bool pdb_try_account_unlock(struct samu *sampass)
254 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
256 if ((acb_info & ACB_NORMAL) && (acb_info & ACB_AUTOLOCK)) {
257 uint32_t lockout_duration;
258 time_t bad_password_time;
259 time_t now = time(NULL);
260 bool ok;
262 ok = pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
263 &lockout_duration);
264 if (!ok) {
265 DEBUG(0, ("pdb_try_account_unlock: "
266 "pdb_get_account_policy failed.\n"));
267 return false;
270 if (lockout_duration == (uint32_t) -1 ||
271 lockout_duration == 0) {
272 DEBUG(9, ("pdb_try_account_unlock: No reset duration, "
273 "can't reset autolock\n"));
274 return false;
276 lockout_duration *= 60;
278 bad_password_time = pdb_get_bad_password_time(sampass);
279 if (bad_password_time == (time_t) 0) {
280 DEBUG(2, ("pdb_try_account_unlock: Account %s "
281 "administratively locked out "
282 "with no bad password "
283 "time. Leaving locked out.\n",
284 pdb_get_username(sampass)));
285 return true;
288 if ((bad_password_time +
289 convert_uint32_t_to_time_t(lockout_duration)) < now) {
290 NTSTATUS status;
292 pdb_set_acct_ctrl(sampass, acb_info & ~ACB_AUTOLOCK,
293 PDB_CHANGED);
294 pdb_set_bad_password_count(sampass, 0, PDB_CHANGED);
295 pdb_set_bad_password_time(sampass, 0, PDB_CHANGED);
297 become_root();
298 status = pdb_update_sam_account(sampass);
299 unbecome_root();
300 if (!NT_STATUS_IS_OK(status)) {
301 DEBUG(0, ("_samr_OpenUser: Couldn't "
302 "update account %s - %s\n",
303 pdb_get_username(sampass),
304 nt_errstr(status)));
305 return false;
310 return true;
314 * @brief Get a sam user structure by the given username.
316 * This functions also checks if the account has been automatically locked out
317 * and unlocks it if a lockout duration time has been defined and the time has
318 * elapsed.
320 * @param[in] sam_acct The sam user structure to fill.
322 * @param[in] username The username to look for.
324 * @return True on success, false on error.
326 bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
328 struct pdb_methods *pdb = pdb_get_methods();
329 struct samu *for_cache;
330 const struct dom_sid *user_sid;
331 NTSTATUS status;
332 bool ok;
334 status = pdb->getsampwnam(pdb, sam_acct, username);
335 if (!NT_STATUS_IS_OK(status)) {
336 return false;
339 ok = pdb_try_account_unlock(sam_acct);
340 if (!ok) {
341 DEBUG(1, ("pdb_getsampwnam: Failed to unlock account %s\n",
342 username));
345 for_cache = samu_new(NULL);
346 if (for_cache == NULL) {
347 return False;
350 if (!pdb_copy_sam_account(for_cache, sam_acct)) {
351 TALLOC_FREE(for_cache);
352 return False;
355 user_sid = pdb_get_user_sid(for_cache);
357 memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
358 data_blob_const(user_sid, sizeof(*user_sid)),
359 &for_cache);
361 return True;
364 /**********************************************************************
365 **********************************************************************/
367 static bool guest_user_info( struct samu *user )
369 struct passwd *pwd;
370 NTSTATUS result;
371 const char *guestname = lp_guest_account();
373 pwd = Get_Pwnam_alloc(talloc_tos(), guestname);
374 if (pwd == NULL) {
375 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
376 guestname));
377 return False;
380 result = samu_set_unix(user, pwd );
382 TALLOC_FREE( pwd );
384 return NT_STATUS_IS_OK( result );
388 * @brief Get a sam user structure by the given username.
390 * This functions also checks if the account has been automatically locked out
391 * and unlocks it if a lockout duration time has been defined and the time has
392 * elapsed.
395 * @param[in] sam_acct The sam user structure to fill.
397 * @param[in] sid The user SDI to look up.
399 * @return True on success, false on error.
401 bool pdb_getsampwsid(struct samu *sam_acct, const struct dom_sid *sid)
403 struct pdb_methods *pdb = pdb_get_methods();
404 uint32_t rid;
405 void *cache_data;
406 bool ok = false;
408 /* hard code the Guest RID of 501 */
410 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
411 return False;
413 if ( rid == DOMAIN_RID_GUEST ) {
414 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
415 return guest_user_info( sam_acct );
418 /* check the cache first */
420 cache_data = memcache_lookup_talloc(
421 NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
423 if (cache_data != NULL) {
424 struct samu *cache_copy = talloc_get_type_abort(
425 cache_data, struct samu);
427 ok = pdb_copy_sam_account(sam_acct, cache_copy);
428 } else {
429 ok = NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
432 if (!ok) {
433 return false;
436 ok = pdb_try_account_unlock(sam_acct);
437 if (!ok) {
438 DEBUG(1, ("pdb_getsampwsid: Failed to unlock account %s\n",
439 sam_acct->username));
442 return true;
445 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
446 TALLOC_CTX *tmp_ctx, const char *name,
447 uint32_t acb_info, uint32_t *rid)
449 struct samu *sam_pass;
450 NTSTATUS status;
451 struct passwd *pwd;
453 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
454 return NT_STATUS_NO_MEMORY;
457 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
458 char *add_script = NULL;
459 int add_ret;
460 fstring name2;
462 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
463 add_script = lp_add_user_script(tmp_ctx);
464 } else {
465 add_script = lp_add_machine_script(tmp_ctx);
468 if (!add_script || add_script[0] == '\0') {
469 DEBUG(3, ("Could not find user %s and no add script "
470 "defined\n", name));
471 return NT_STATUS_NO_SUCH_USER;
474 /* lowercase the username before creating the Unix account for
475 compatibility with previous Samba releases */
476 fstrcpy( name2, name );
477 if (!strlower_m( name2 )) {
478 return NT_STATUS_INVALID_PARAMETER;
480 add_script = talloc_all_string_sub(tmp_ctx,
481 add_script,
482 "%u",
483 name2);
484 if (!add_script) {
485 return NT_STATUS_NO_MEMORY;
487 add_ret = smbrun(add_script,NULL);
488 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
489 add_script, add_ret));
490 if (add_ret == 0) {
491 smb_nscd_flush_user_cache();
494 flush_pwnam_cache();
496 pwd = Get_Pwnam_alloc(tmp_ctx, name);
498 if(pwd == NULL) {
499 DEBUG(3, ("Could not find user %s, add script did not work\n", name));
500 return NT_STATUS_NO_SUCH_USER;
504 /* we have a valid SID coming out of this call */
506 status = samu_alloc_rid_unix(methods, sam_pass, pwd);
508 TALLOC_FREE( pwd );
510 if (!NT_STATUS_IS_OK(status)) {
511 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
512 return status;
515 if (!sid_peek_check_rid(get_global_sam_sid(),
516 pdb_get_user_sid(sam_pass), rid)) {
517 DEBUG(0, ("Could not get RID of fresh user\n"));
518 return NT_STATUS_INTERNAL_ERROR;
521 /* Use the username case specified in the original request */
523 pdb_set_username( sam_pass, name, PDB_SET );
525 /* Disable the account on creation, it does not have a reasonable password yet. */
527 acb_info |= ACB_DISABLED;
529 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
531 status = methods->add_sam_account(methods, sam_pass);
533 TALLOC_FREE(sam_pass);
535 return status;
538 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32_t flags,
539 uint32_t *rid)
541 struct pdb_methods *pdb = pdb_get_methods();
542 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
545 /****************************************************************************
546 Delete a UNIX user on demand.
547 ****************************************************************************/
549 static int smb_delete_user(const char *unix_user)
551 char *del_script = NULL;
552 int ret;
554 /* safety check */
556 if ( strequal( unix_user, "root" ) ) {
557 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
558 return -1;
561 del_script = lp_delete_user_script(talloc_tos());
562 if (!del_script || !*del_script) {
563 return -1;
565 del_script = talloc_all_string_sub(talloc_tos(),
566 del_script,
567 "%u",
568 unix_user);
569 if (!del_script) {
570 return -1;
572 ret = smbrun(del_script,NULL);
573 flush_pwnam_cache();
574 if (ret == 0) {
575 smb_nscd_flush_user_cache();
577 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
579 return ret;
582 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
583 TALLOC_CTX *mem_ctx,
584 struct samu *sam_acct)
586 NTSTATUS status;
587 fstring username;
589 status = methods->delete_sam_account(methods, sam_acct);
590 if (!NT_STATUS_IS_OK(status)) {
591 return status;
595 * Now delete the unix side ....
596 * note: we don't check if the delete really happened as the script is
597 * not necessary present and maybe the sysadmin doesn't want to delete
598 * the unix side
601 /* always lower case the username before handing it off to
602 external scripts */
604 fstrcpy( username, pdb_get_username(sam_acct) );
605 if (!strlower_m( username )) {
606 return status;
609 smb_delete_user( username );
611 return status;
614 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
616 struct pdb_methods *pdb = pdb_get_methods();
617 uid_t uid = -1;
618 NTSTATUS status;
619 const struct dom_sid *user_sid;
620 char *msg_data;
622 user_sid = pdb_get_user_sid(sam_acct);
624 /* sanity check to make sure we don't delete root */
626 if ( !sid_to_uid(user_sid, &uid ) ) {
627 return NT_STATUS_NO_SUCH_USER;
630 if ( uid == 0 ) {
631 return NT_STATUS_ACCESS_DENIED;
634 memcache_delete(NULL,
635 PDB_GETPWSID_CACHE,
636 data_blob_const(user_sid, sizeof(*user_sid)));
638 status = pdb->delete_user(pdb, mem_ctx, sam_acct);
639 if (!NT_STATUS_IS_OK(status)) {
640 return status;
643 msg_data = talloc_asprintf(mem_ctx, "USER %s",
644 pdb_get_username(sam_acct));
645 if (!msg_data) {
646 /* not fatal, and too late to rollback,
647 * just return */
648 return status;
650 message_send_all(server_messaging_context(),
651 ID_CACHE_DELETE,
652 msg_data,
653 strlen(msg_data) + 1,
654 NULL);
656 TALLOC_FREE(msg_data);
657 return status;
660 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
662 struct pdb_methods *pdb = pdb_get_methods();
663 return pdb->add_sam_account(pdb, sam_acct);
666 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
668 struct pdb_methods *pdb = pdb_get_methods();
670 memcache_flush(NULL, PDB_GETPWSID_CACHE);
672 return pdb->update_sam_account(pdb, sam_acct);
675 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
677 struct pdb_methods *pdb = pdb_get_methods();
678 const struct dom_sid *user_sid = pdb_get_user_sid(sam_acct);
680 memcache_delete(NULL,
681 PDB_GETPWSID_CACHE,
682 data_blob_const(user_sid, sizeof(*user_sid)));
684 return pdb->delete_sam_account(pdb, sam_acct);
687 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
689 struct pdb_methods *pdb = pdb_get_methods();
690 uid_t uid;
691 NTSTATUS status;
693 memcache_flush(NULL, PDB_GETPWSID_CACHE);
695 /* sanity check to make sure we don't rename root */
697 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
698 return NT_STATUS_NO_SUCH_USER;
701 if ( uid == 0 ) {
702 return NT_STATUS_ACCESS_DENIED;
705 status = pdb->rename_sam_account(pdb, oldname, newname);
707 /* always flush the cache here just to be safe */
708 flush_pwnam_cache();
710 return status;
713 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
715 struct pdb_methods *pdb = pdb_get_methods();
716 return pdb->update_login_attempts(pdb, sam_acct, success);
719 bool pdb_getgrsid(GROUP_MAP *map, struct dom_sid sid)
721 struct pdb_methods *pdb = pdb_get_methods();
722 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
725 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
727 struct pdb_methods *pdb = pdb_get_methods();
728 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
731 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
733 struct pdb_methods *pdb = pdb_get_methods();
734 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
737 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
738 TALLOC_CTX *mem_ctx,
739 const char *name,
740 uint32_t *rid)
742 struct dom_sid group_sid;
743 struct group *grp;
744 fstring tmp;
746 grp = getgrnam(name);
748 if (grp == NULL) {
749 gid_t gid;
751 if (smb_create_group(name, &gid) != 0) {
752 return NT_STATUS_ACCESS_DENIED;
755 grp = getgrgid(gid);
758 if (grp == NULL) {
759 return NT_STATUS_ACCESS_DENIED;
762 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
763 if (!pdb_new_rid(rid)) {
764 return NT_STATUS_ACCESS_DENIED;
766 } else {
767 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
770 sid_compose(&group_sid, get_global_sam_sid(), *rid);
772 return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
773 SID_NAME_DOM_GRP, name, NULL);
776 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
777 uint32_t *rid)
779 struct pdb_methods *pdb = pdb_get_methods();
780 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
783 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
784 TALLOC_CTX *mem_ctx,
785 uint32_t rid)
787 struct dom_sid group_sid;
788 GROUP_MAP *map;
789 NTSTATUS status;
790 struct group *grp;
791 const char *grp_name;
793 map = talloc_zero(mem_ctx, GROUP_MAP);
794 if (!map) {
795 return NT_STATUS_NO_MEMORY;
798 /* coverity */
799 map->gid = (gid_t) -1;
801 sid_compose(&group_sid, get_global_sam_sid(), rid);
803 if (!get_domain_group_from_sid(group_sid, map)) {
804 DEBUG(10, ("Could not find group for rid %d\n", rid));
805 return NT_STATUS_NO_SUCH_GROUP;
808 /* We need the group name for the smb_delete_group later on */
810 if (map->gid == (gid_t)-1) {
811 return NT_STATUS_NO_SUCH_GROUP;
814 grp = getgrgid(map->gid);
815 if (grp == NULL) {
816 return NT_STATUS_NO_SUCH_GROUP;
819 TALLOC_FREE(map);
821 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
823 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
824 if (grp_name == NULL) {
825 return NT_STATUS_NO_MEMORY;
828 status = pdb_delete_group_mapping_entry(group_sid);
830 if (!NT_STATUS_IS_OK(status)) {
831 return status;
834 /* Don't check the result of smb_delete_group */
836 smb_delete_group(grp_name);
838 return NT_STATUS_OK;
841 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32_t rid)
843 struct pdb_methods *pdb = pdb_get_methods();
844 return pdb->delete_dom_group(pdb, mem_ctx, rid);
847 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
849 struct pdb_methods *pdb = pdb_get_methods();
850 return pdb->add_group_mapping_entry(pdb, map);
853 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
855 struct pdb_methods *pdb = pdb_get_methods();
856 return pdb->update_group_mapping_entry(pdb, map);
859 NTSTATUS pdb_delete_group_mapping_entry(struct dom_sid sid)
861 struct pdb_methods *pdb = pdb_get_methods();
862 return pdb->delete_group_mapping_entry(pdb, sid);
865 bool pdb_enum_group_mapping(const struct dom_sid *sid,
866 enum lsa_SidType sid_name_use,
867 GROUP_MAP ***pp_rmap,
868 size_t *p_num_entries,
869 bool unix_only)
871 struct pdb_methods *pdb = pdb_get_methods();
872 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
873 pp_rmap, p_num_entries, unix_only));
876 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
877 const struct dom_sid *sid,
878 uint32_t **pp_member_rids,
879 size_t *p_num_members)
881 struct pdb_methods *pdb = pdb_get_methods();
882 NTSTATUS result;
884 result = pdb->enum_group_members(pdb, mem_ctx,
885 sid, pp_member_rids, p_num_members);
887 /* special check for rid 513 */
889 if ( !NT_STATUS_IS_OK( result ) ) {
890 uint32_t rid;
892 sid_peek_rid( sid, &rid );
894 if ( rid == DOMAIN_RID_USERS ) {
895 *p_num_members = 0;
896 *pp_member_rids = NULL;
898 return NT_STATUS_OK;
902 return result;
905 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
906 struct dom_sid **pp_sids, gid_t **pp_gids,
907 uint32_t *p_num_groups)
909 struct pdb_methods *pdb = pdb_get_methods();
910 return pdb->enum_group_memberships(
911 pdb, mem_ctx, user,
912 pp_sids, pp_gids, p_num_groups);
915 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
916 TALLOC_CTX *mem_ctx,
917 struct samu *sampass)
919 struct group *grp;
920 gid_t gid;
922 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
923 (grp = getgrgid(gid)) == NULL) {
924 return NT_STATUS_INVALID_PRIMARY_GROUP;
927 if (smb_set_primary_group(grp->gr_name,
928 pdb_get_username(sampass)) != 0) {
929 return NT_STATUS_ACCESS_DENIED;
932 return NT_STATUS_OK;
935 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
937 struct pdb_methods *pdb = pdb_get_methods();
938 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
942 * Helper function to see whether a user is in a group. We can't use
943 * user_in_group_sid here because this creates dependencies only smbd can
944 * fulfil.
947 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
948 const struct dom_sid *group_sid)
950 struct dom_sid *sids;
951 gid_t *gids;
952 uint32_t i, num_groups;
954 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
955 &sids, &gids,
956 &num_groups))) {
957 return False;
960 for (i=0; i<num_groups; i++) {
961 if (dom_sid_equal(group_sid, &sids[i])) {
962 return True;
965 return False;
968 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
969 TALLOC_CTX *mem_ctx,
970 uint32_t group_rid,
971 uint32_t member_rid)
973 struct dom_sid group_sid, member_sid;
974 struct samu *account = NULL;
975 GROUP_MAP *map;
976 struct group *grp;
977 struct passwd *pwd;
978 const char *group_name;
979 uid_t uid;
981 map = talloc_zero(mem_ctx, GROUP_MAP);
982 if (!map) {
983 return NT_STATUS_NO_MEMORY;
986 /* coverity */
987 map->gid = (gid_t) -1;
989 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
990 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
992 if (!get_domain_group_from_sid(group_sid, map) ||
993 (map->gid == (gid_t)-1) ||
994 ((grp = getgrgid(map->gid)) == NULL)) {
995 return NT_STATUS_NO_SUCH_GROUP;
998 TALLOC_FREE(map);
1000 group_name = talloc_strdup(mem_ctx, grp->gr_name);
1001 if (group_name == NULL) {
1002 return NT_STATUS_NO_MEMORY;
1005 if ( !(account = samu_new( NULL )) ) {
1006 return NT_STATUS_NO_MEMORY;
1009 if (!pdb_getsampwsid(account, &member_sid) ||
1010 !sid_to_uid(&member_sid, &uid) ||
1011 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
1012 return NT_STATUS_NO_SUCH_USER;
1015 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1016 return NT_STATUS_MEMBER_IN_GROUP;
1020 * ok, the group exist, the user exist, the user is not in the group,
1021 * we can (finally) add it to the group !
1024 smb_add_user_group(group_name, pwd->pw_name);
1026 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1027 return NT_STATUS_ACCESS_DENIED;
1030 return NT_STATUS_OK;
1033 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1034 uint32_t member_rid)
1036 struct pdb_methods *pdb = pdb_get_methods();
1037 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
1040 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
1041 TALLOC_CTX *mem_ctx,
1042 uint32_t group_rid,
1043 uint32_t member_rid)
1045 struct dom_sid group_sid, member_sid;
1046 struct samu *account = NULL;
1047 GROUP_MAP *map;
1048 struct group *grp;
1049 struct passwd *pwd;
1050 const char *group_name;
1051 uid_t uid;
1053 map = talloc_zero(mem_ctx, GROUP_MAP);
1054 if (!map) {
1055 return NT_STATUS_NO_MEMORY;
1058 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
1059 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
1061 if (!get_domain_group_from_sid(group_sid, map) ||
1062 (map->gid == (gid_t)-1) ||
1063 ((grp = getgrgid(map->gid)) == NULL)) {
1064 return NT_STATUS_NO_SUCH_GROUP;
1067 TALLOC_FREE(map);
1069 group_name = talloc_strdup(mem_ctx, grp->gr_name);
1070 if (group_name == NULL) {
1071 return NT_STATUS_NO_MEMORY;
1074 if ( !(account = samu_new( NULL )) ) {
1075 return NT_STATUS_NO_MEMORY;
1078 if (!pdb_getsampwsid(account, &member_sid) ||
1079 !sid_to_uid(&member_sid, &uid) ||
1080 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
1081 return NT_STATUS_NO_SUCH_USER;
1084 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1085 return NT_STATUS_MEMBER_NOT_IN_GROUP;
1089 * ok, the group exist, the user exist, the user is in the group,
1090 * we can (finally) delete it from the group!
1093 smb_delete_user_group(group_name, pwd->pw_name);
1095 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1096 return NT_STATUS_ACCESS_DENIED;
1099 return NT_STATUS_OK;
1102 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1103 uint32_t member_rid)
1105 struct pdb_methods *pdb = pdb_get_methods();
1106 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
1109 NTSTATUS pdb_create_alias(const char *name, uint32_t *rid)
1111 struct pdb_methods *pdb = pdb_get_methods();
1112 return pdb->create_alias(pdb, name, rid);
1115 NTSTATUS pdb_delete_alias(const struct dom_sid *sid)
1117 struct pdb_methods *pdb = pdb_get_methods();
1118 return pdb->delete_alias(pdb, sid);
1121 NTSTATUS pdb_get_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1123 struct pdb_methods *pdb = pdb_get_methods();
1124 return pdb->get_aliasinfo(pdb, sid, info);
1127 NTSTATUS pdb_set_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1129 struct pdb_methods *pdb = pdb_get_methods();
1130 return pdb->set_aliasinfo(pdb, sid, info);
1133 NTSTATUS pdb_add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1135 struct pdb_methods *pdb = pdb_get_methods();
1136 return pdb->add_aliasmem(pdb, alias, member);
1139 NTSTATUS pdb_del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1141 struct pdb_methods *pdb = pdb_get_methods();
1142 return pdb->del_aliasmem(pdb, alias, member);
1145 NTSTATUS pdb_enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
1146 struct dom_sid **pp_members, size_t *p_num_members)
1148 struct pdb_methods *pdb = pdb_get_methods();
1149 return pdb->enum_aliasmem(pdb, alias, mem_ctx, pp_members,
1150 p_num_members);
1153 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1154 const struct dom_sid *domain_sid,
1155 const struct dom_sid *members, size_t num_members,
1156 uint32_t **pp_alias_rids,
1157 size_t *p_num_alias_rids)
1159 struct pdb_methods *pdb = pdb_get_methods();
1160 return pdb->enum_alias_memberships(pdb, mem_ctx,
1161 domain_sid,
1162 members, num_members,
1163 pp_alias_rids,
1164 p_num_alias_rids);
1167 NTSTATUS pdb_lookup_rids(const struct dom_sid *domain_sid,
1168 int num_rids,
1169 uint32_t *rids,
1170 const char **names,
1171 enum lsa_SidType *attrs)
1173 struct pdb_methods *pdb = pdb_get_methods();
1174 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
1177 bool pdb_get_account_policy(enum pdb_policy_type type, uint32_t *value)
1179 struct pdb_methods *pdb = pdb_get_methods();
1180 NTSTATUS status;
1182 become_root();
1183 status = pdb->get_account_policy(pdb, type, value);
1184 unbecome_root();
1186 return NT_STATUS_IS_OK(status);
1189 bool pdb_set_account_policy(enum pdb_policy_type type, uint32_t value)
1191 struct pdb_methods *pdb = pdb_get_methods();
1192 NTSTATUS status;
1194 become_root();
1195 status = pdb->set_account_policy(pdb, type, value);
1196 unbecome_root();
1198 return NT_STATUS_IS_OK(status);
1201 bool pdb_get_seq_num(time_t *seq_num)
1203 struct pdb_methods *pdb = pdb_get_methods();
1204 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1208 * Instead of passing down a gid or uid, this function sends down a pointer
1209 * to a unixid.
1211 * This acts as an in-out variable so that the idmap functions can correctly
1212 * receive ID_TYPE_BOTH, filling in cache details correctly rather than forcing
1213 * the cache to store ID_TYPE_UID or ID_TYPE_GID.
1215 bool pdb_id_to_sid(struct unixid *id, struct dom_sid *sid)
1217 struct pdb_methods *pdb = pdb_get_methods();
1218 bool ret;
1220 ret = pdb->id_to_sid(pdb, id, sid);
1222 if (ret == true) {
1223 idmap_cache_set_sid2unixid(sid, id);
1226 return ret;
1229 bool pdb_sid_to_id(const struct dom_sid *sid, struct unixid *id)
1231 struct pdb_methods *pdb = pdb_get_methods();
1232 bool ret;
1234 /* only ask the backend if it is responsible */
1235 if (!sid_check_object_is_for_passdb(sid)) {
1236 return false;
1239 ret = pdb->sid_to_id(pdb, sid, id);
1241 if (ret == true) {
1242 idmap_cache_set_sid2unixid(sid, id);
1245 return ret;
1248 uint32_t pdb_capabilities(void)
1250 struct pdb_methods *pdb = pdb_get_methods();
1251 return pdb->capabilities(pdb);
1254 /********************************************************************
1255 Allocate a new RID from the passdb backend. Verify that it is free
1256 by calling lookup_global_sam_rid() to verify that the RID is not
1257 in use. This handles servers that have existing users or groups
1258 with add RIDs (assigned from previous algorithmic mappings)
1259 ********************************************************************/
1261 bool pdb_new_rid(uint32_t *rid)
1263 struct pdb_methods *pdb = pdb_get_methods();
1264 const char *name = NULL;
1265 enum lsa_SidType type;
1266 uint32_t allocated_rid = 0;
1267 int i;
1268 TALLOC_CTX *ctx;
1270 if ((pdb_capabilities() & PDB_CAP_STORE_RIDS) == 0) {
1271 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1272 "are active\n"));
1273 return False;
1276 if (algorithmic_rid_base() != BASE_RID) {
1277 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1278 "without algorithmic RIDs is chosen.\n"));
1279 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1280 "add', set the maximum used RID\n"));
1281 DEBUGADD(0, ("and remove the parameter\n"));
1282 return False;
1285 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1286 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1287 return False;
1290 /* Attempt to get an unused RID (max tires is 250...yes that it is
1291 and arbitrary number I pulkled out of my head). -- jerry */
1293 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1294 /* get a new RID */
1296 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1297 return False;
1300 /* validate that the RID is not in use */
1302 if (lookup_global_sam_rid(ctx, allocated_rid, &name, &type, NULL, NULL)) {
1303 allocated_rid = 0;
1307 TALLOC_FREE( ctx );
1309 if ( allocated_rid == 0 ) {
1310 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1311 return False;
1314 *rid = allocated_rid;
1316 return True;
1319 /***************************************************************
1320 Initialize the static context (at smbd startup etc).
1322 If uninitialised, context will auto-init on first use.
1323 ***************************************************************/
1325 bool initialize_password_db(bool reload, struct tevent_context *tevent_ctx)
1327 if (tevent_ctx) {
1328 pdb_tevent_ctx = tevent_ctx;
1330 return (pdb_get_methods_reload(reload) != NULL);
1333 /***************************************************************************
1334 Default implementations of some functions.
1335 ****************************************************************************/
1337 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1339 return NT_STATUS_NO_SUCH_USER;
1342 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1344 return NT_STATUS_NO_SUCH_USER;
1347 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1349 return NT_STATUS_NOT_IMPLEMENTED;
1352 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1354 return NT_STATUS_NOT_IMPLEMENTED;
1357 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1359 return NT_STATUS_NOT_IMPLEMENTED;
1362 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1364 return NT_STATUS_NOT_IMPLEMENTED;
1367 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1369 /* Only the pdb_nds backend implements this, by
1370 * default just return ok. */
1371 return NT_STATUS_OK;
1374 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t *value)
1376 return account_policy_get(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1379 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t value)
1381 return account_policy_set(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1384 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1386 *seq_num = time(NULL);
1387 return NT_STATUS_OK;
1390 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1391 struct dom_sid *sid)
1393 struct samu *sampw = NULL;
1394 struct passwd *unix_pw;
1395 bool ret;
1397 unix_pw = getpwuid( uid );
1399 if ( !unix_pw ) {
1400 DEBUG(4,("pdb_default_uid_to_sid: host has no idea of uid "
1401 "%lu\n", (unsigned long)uid));
1402 return False;
1405 if ( !(sampw = samu_new( NULL )) ) {
1406 DEBUG(0,("pdb_default_uid_to_sid: samu_new() failed!\n"));
1407 return False;
1410 become_root();
1411 ret = NT_STATUS_IS_OK(
1412 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1413 unbecome_root();
1415 if (!ret) {
1416 DEBUG(5, ("pdb_default_uid_to_sid: Did not find user "
1417 "%s (%u)\n", unix_pw->pw_name, (unsigned int)uid));
1418 TALLOC_FREE(sampw);
1419 return False;
1422 sid_copy(sid, pdb_get_user_sid(sampw));
1424 TALLOC_FREE(sampw);
1426 return True;
1429 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1430 struct dom_sid *sid)
1432 GROUP_MAP *map;
1434 map = talloc_zero(NULL, GROUP_MAP);
1435 if (!map) {
1436 return false;
1439 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, map, gid))) {
1440 TALLOC_FREE(map);
1441 return false;
1444 sid_copy(sid, &map->sid);
1445 TALLOC_FREE(map);
1446 return true;
1449 static bool pdb_default_id_to_sid(struct pdb_methods *methods, struct unixid *id,
1450 struct dom_sid *sid)
1452 switch (id->type) {
1453 case ID_TYPE_UID:
1454 return pdb_default_uid_to_sid(methods, id->id, sid);
1456 case ID_TYPE_GID:
1457 return pdb_default_gid_to_sid(methods, id->id, sid);
1459 default:
1460 return false;
1464 * The "Unix User" and "Unix Group" domains have a special
1465 * id mapping that is a rid-algorithm with range starting at 0.
1467 bool pdb_sid_to_id_unix_users_and_groups(const struct dom_sid *sid,
1468 struct unixid *id)
1470 uint32_t rid;
1472 id->id = -1;
1474 if (sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid)) {
1475 id->id = rid;
1476 id->type = ID_TYPE_UID;
1477 return true;
1480 if (sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid)) {
1481 id->id = rid;
1482 id->type = ID_TYPE_GID;
1483 return true;
1486 return false;
1489 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1490 const struct dom_sid *sid,
1491 struct unixid *id)
1493 TALLOC_CTX *mem_ctx;
1494 bool ret = False;
1495 uint32_t rid;
1496 id->id = -1;
1498 mem_ctx = talloc_new(NULL);
1500 if (mem_ctx == NULL) {
1501 DEBUG(0, ("talloc_new failed\n"));
1502 return False;
1505 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1506 const char *name;
1507 enum lsa_SidType type;
1508 uid_t uid;
1509 gid_t gid;
1510 /* Here we might have users as well as groups and aliases */
1511 ret = lookup_global_sam_rid(mem_ctx, rid, &name, &type, &uid, &gid);
1512 if (ret) {
1513 switch (type) {
1514 case SID_NAME_DOM_GRP:
1515 case SID_NAME_ALIAS:
1516 id->type = ID_TYPE_GID;
1517 id->id = gid;
1518 break;
1519 case SID_NAME_USER:
1520 id->type = ID_TYPE_UID;
1521 id->id = uid;
1522 break;
1523 default:
1524 DEBUG(5, ("SID %s belongs to our domain, and "
1525 "an object exists in the database, "
1526 "but it is neither a user nor a "
1527 "group (got type %d).\n",
1528 sid_string_dbg(sid), type));
1529 ret = false;
1531 } else {
1532 DEBUG(5, ("SID %s belongs to our domain, but there is "
1533 "no corresponding object in the database.\n",
1534 sid_string_dbg(sid)));
1536 goto done;
1540 * "Unix User" and "Unix Group"
1542 ret = pdb_sid_to_id_unix_users_and_groups(sid, id);
1543 if (ret == true) {
1544 goto done;
1547 /* BUILTIN */
1549 if (sid_check_is_in_builtin(sid) ||
1550 sid_check_is_in_wellknown_domain(sid)) {
1551 /* Here we only have aliases */
1552 GROUP_MAP *map;
1554 map = talloc_zero(mem_ctx, GROUP_MAP);
1555 if (!map) {
1556 ret = false;
1557 goto done;
1560 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, map, *sid))) {
1561 DEBUG(10, ("Could not find map for sid %s\n",
1562 sid_string_dbg(sid)));
1563 goto done;
1565 if ((map->sid_name_use != SID_NAME_ALIAS) &&
1566 (map->sid_name_use != SID_NAME_WKN_GRP)) {
1567 DEBUG(10, ("Map for sid %s is a %s, expected an "
1568 "alias\n", sid_string_dbg(sid),
1569 sid_type_lookup(map->sid_name_use)));
1570 goto done;
1573 id->id = map->gid;
1574 id->type = ID_TYPE_GID;
1575 ret = True;
1576 goto done;
1579 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1580 sid_string_dbg(sid)));
1582 done:
1584 TALLOC_FREE(mem_ctx);
1585 return ret;
1588 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, uint32_t *p_num)
1590 struct group *grp;
1591 char **gr;
1592 struct passwd *pwd;
1593 bool winbind_env;
1594 bool ret = False;
1596 *pp_uids = NULL;
1597 *p_num = 0;
1599 /* We only look at our own sam, so don't care about imported stuff */
1600 winbind_env = winbind_env_set();
1601 (void)winbind_off();
1603 if ((grp = getgrgid(gid)) == NULL) {
1604 /* allow winbindd lookups, but only if they weren't already disabled */
1605 goto done;
1608 /* Primary group members */
1609 setpwent();
1610 while ((pwd = getpwent()) != NULL) {
1611 if (pwd->pw_gid == gid) {
1612 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1613 pp_uids, p_num)) {
1614 goto done;
1618 endpwent();
1620 /* Secondary group members */
1621 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1622 struct passwd *pw = getpwnam(*gr);
1624 if (pw == NULL)
1625 continue;
1626 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1627 goto done;
1631 ret = True;
1633 done:
1635 /* allow winbindd lookups, but only if they weren't already disabled */
1636 if (!winbind_env) {
1637 (void)winbind_on();
1640 return ret;
1643 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1644 TALLOC_CTX *mem_ctx,
1645 const struct dom_sid *group,
1646 uint32_t **pp_member_rids,
1647 size_t *p_num_members)
1649 gid_t gid;
1650 uid_t *uids;
1651 uint32_t i, num_uids;
1653 *pp_member_rids = NULL;
1654 *p_num_members = 0;
1656 if (!sid_to_gid(group, &gid))
1657 return NT_STATUS_NO_SUCH_GROUP;
1659 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1660 return NT_STATUS_NO_SUCH_GROUP;
1662 if (num_uids == 0)
1663 return NT_STATUS_OK;
1665 *pp_member_rids = talloc_zero_array(mem_ctx, uint32_t, num_uids);
1667 for (i=0; i<num_uids; i++) {
1668 struct dom_sid sid;
1670 uid_to_sid(&sid, uids[i]);
1672 if (!sid_check_is_in_our_sam(&sid)) {
1673 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1674 "in our domain\n"));
1675 continue;
1678 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1679 *p_num_members += 1;
1682 return NT_STATUS_OK;
1685 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1686 TALLOC_CTX *mem_ctx,
1687 struct samu *user,
1688 struct dom_sid **pp_sids,
1689 gid_t **pp_gids,
1690 uint32_t *p_num_groups)
1692 size_t i;
1693 gid_t gid;
1694 struct passwd *pw;
1695 const char *username = pdb_get_username(user);
1698 /* Ignore the primary group SID. Honor the real Unix primary group.
1699 The primary group SID is only of real use to Windows clients */
1701 if ( !(pw = Get_Pwnam_alloc(mem_ctx, username)) ) {
1702 return NT_STATUS_NO_SUCH_USER;
1705 gid = pw->pw_gid;
1707 TALLOC_FREE( pw );
1709 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1710 return NT_STATUS_NO_SUCH_USER;
1713 if (*p_num_groups == 0) {
1714 smb_panic("primary group missing");
1717 *pp_sids = talloc_array(mem_ctx, struct dom_sid, *p_num_groups);
1719 if (*pp_sids == NULL) {
1720 TALLOC_FREE(*pp_gids);
1721 return NT_STATUS_NO_MEMORY;
1724 for (i=0; i<*p_num_groups; i++) {
1725 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1728 return NT_STATUS_OK;
1731 /*******************************************************************
1732 Look up a rid in the SAM we're responsible for (i.e. passdb)
1733 ********************************************************************/
1735 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32_t rid,
1736 const char **name,
1737 enum lsa_SidType *psid_name_use,
1738 uid_t *uid, gid_t *gid)
1740 struct samu *sam_account = NULL;
1741 GROUP_MAP *map = NULL;
1742 bool ret;
1743 struct dom_sid sid;
1745 *psid_name_use = SID_NAME_UNKNOWN;
1747 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1748 (unsigned int)rid));
1750 sid_compose(&sid, get_global_sam_sid(), rid);
1752 /* see if the passdb can help us with the name of the user */
1754 if ( !(sam_account = samu_new( NULL )) ) {
1755 return False;
1758 map = talloc_zero(mem_ctx, GROUP_MAP);
1759 if (!map) {
1760 return false;
1763 /* BEING ROOT BLOCK */
1764 become_root();
1765 ret = pdb_getsampwsid(sam_account, &sid);
1766 if (!ret) {
1767 TALLOC_FREE(sam_account);
1768 ret = pdb_getgrsid(map, sid);
1770 unbecome_root();
1771 /* END BECOME_ROOT BLOCK */
1773 if (sam_account || !ret) {
1774 TALLOC_FREE(map);
1777 if (sam_account) {
1778 struct passwd *pw;
1780 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1781 if (!*name) {
1782 TALLOC_FREE(sam_account);
1783 return False;
1786 *psid_name_use = SID_NAME_USER;
1788 TALLOC_FREE(sam_account);
1790 if (uid == NULL) {
1791 return True;
1794 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1795 if (pw == NULL) {
1796 return False;
1798 *uid = pw->pw_uid;
1799 TALLOC_FREE(pw);
1800 return True;
1802 } else if (map && (map->gid != (gid_t)-1)) {
1804 /* do not resolve SIDs to a name unless there is a valid
1805 gid associated with it */
1807 *name = talloc_steal(mem_ctx, map->nt_name);
1808 *psid_name_use = map->sid_name_use;
1810 if (gid) {
1811 *gid = map->gid;
1814 TALLOC_FREE(map);
1815 return True;
1818 TALLOC_FREE(map);
1820 /* Windows will always map RID 513 to something. On a non-domain
1821 controller, this gets mapped to SERVER\None. */
1823 if (uid || gid) {
1824 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1825 return False;
1828 if ( rid == DOMAIN_RID_USERS ) {
1829 *name = talloc_strdup(mem_ctx, "None" );
1830 *psid_name_use = SID_NAME_DOM_GRP;
1832 return True;
1835 return False;
1838 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1839 const struct dom_sid *domain_sid,
1840 int num_rids,
1841 uint32_t *rids,
1842 const char **names,
1843 enum lsa_SidType *attrs)
1845 int i;
1846 NTSTATUS result;
1847 bool have_mapped = False;
1848 bool have_unmapped = False;
1850 if (sid_check_is_builtin(domain_sid)) {
1852 for (i=0; i<num_rids; i++) {
1853 const char *name;
1855 if (lookup_builtin_rid(names, rids[i], &name)) {
1856 attrs[i] = SID_NAME_ALIAS;
1857 names[i] = name;
1858 DEBUG(5,("lookup_rids: %s:%d\n",
1859 names[i], attrs[i]));
1860 have_mapped = True;
1861 } else {
1862 have_unmapped = True;
1863 attrs[i] = SID_NAME_UNKNOWN;
1866 goto done;
1869 /* Should not happen, but better check once too many */
1870 if (!sid_check_is_our_sam(domain_sid)) {
1871 return NT_STATUS_INVALID_HANDLE;
1874 for (i = 0; i < num_rids; i++) {
1875 const char *name;
1877 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1878 NULL, NULL)) {
1879 if (name == NULL) {
1880 return NT_STATUS_NO_MEMORY;
1882 names[i] = name;
1883 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1884 have_mapped = True;
1885 } else {
1886 have_unmapped = True;
1887 attrs[i] = SID_NAME_UNKNOWN;
1891 done:
1893 result = NT_STATUS_NONE_MAPPED;
1895 if (have_mapped)
1896 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1898 return result;
1901 static int pdb_search_destructor(struct pdb_search *search)
1903 if ((!search->search_ended) && (search->search_end != NULL)) {
1904 search->search_end(search);
1906 return 0;
1909 struct pdb_search *pdb_search_init(TALLOC_CTX *mem_ctx,
1910 enum pdb_search_type type)
1912 struct pdb_search *result;
1914 result = talloc(mem_ctx, struct pdb_search);
1915 if (result == NULL) {
1916 DEBUG(0, ("talloc failed\n"));
1917 return NULL;
1920 result->type = type;
1921 result->cache = NULL;
1922 result->num_entries = 0;
1923 result->cache_size = 0;
1924 result->search_ended = False;
1925 result->search_end = NULL;
1927 /* Segfault appropriately if not initialized */
1928 result->next_entry = NULL;
1929 result->search_end = NULL;
1931 talloc_set_destructor(result, pdb_search_destructor);
1933 return result;
1936 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32_t rid,
1937 uint16_t acct_flags,
1938 const char *account_name,
1939 const char *fullname,
1940 const char *description,
1941 struct samr_displayentry *entry)
1943 entry->rid = rid;
1944 entry->acct_flags = acct_flags;
1946 if (account_name != NULL)
1947 entry->account_name = talloc_strdup(mem_ctx, account_name);
1948 else
1949 entry->account_name = "";
1951 if (fullname != NULL)
1952 entry->fullname = talloc_strdup(mem_ctx, fullname);
1953 else
1954 entry->fullname = "";
1956 if (description != NULL)
1957 entry->description = talloc_strdup(mem_ctx, description);
1958 else
1959 entry->description = "";
1962 struct group_search {
1963 GROUP_MAP **groups;
1964 size_t num_groups, current_group;
1967 static bool next_entry_groups(struct pdb_search *s,
1968 struct samr_displayentry *entry)
1970 struct group_search *state = (struct group_search *)s->private_data;
1971 uint32_t rid;
1972 GROUP_MAP *map;
1974 if (state->current_group == state->num_groups)
1975 return False;
1977 map = state->groups[state->current_group];
1979 sid_peek_rid(&map->sid, &rid);
1981 fill_displayentry(s, rid, 0, map->nt_name, NULL, map->comment, entry);
1983 state->current_group += 1;
1984 return True;
1987 static void search_end_groups(struct pdb_search *search)
1989 struct group_search *state =
1990 (struct group_search *)search->private_data;
1991 TALLOC_FREE(state->groups);
1994 static bool pdb_search_grouptype(struct pdb_methods *methods,
1995 struct pdb_search *search,
1996 const struct dom_sid *sid, enum lsa_SidType type)
1998 struct group_search *state;
2000 state = talloc_zero(search, struct group_search);
2001 if (state == NULL) {
2002 DEBUG(0, ("talloc failed\n"));
2003 return False;
2006 if (!NT_STATUS_IS_OK(methods->enum_group_mapping(methods, sid, type,
2007 &state->groups, &state->num_groups,
2008 True))) {
2009 DEBUG(0, ("Could not enum groups\n"));
2010 return False;
2013 state->current_group = 0;
2014 search->private_data = state;
2015 search->next_entry = next_entry_groups;
2016 search->search_end = search_end_groups;
2017 return True;
2020 static bool pdb_default_search_groups(struct pdb_methods *methods,
2021 struct pdb_search *search)
2023 return pdb_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
2026 static bool pdb_default_search_aliases(struct pdb_methods *methods,
2027 struct pdb_search *search,
2028 const struct dom_sid *sid)
2031 return pdb_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
2034 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
2035 uint32_t idx)
2037 if (idx < search->num_entries)
2038 return &search->cache[idx];
2040 if (search->search_ended)
2041 return NULL;
2043 while (idx >= search->num_entries) {
2044 struct samr_displayentry entry;
2046 if (!search->next_entry(search, &entry)) {
2047 search->search_end(search);
2048 search->search_ended = True;
2049 break;
2052 ADD_TO_LARGE_ARRAY(search, struct samr_displayentry,
2053 entry, &search->cache, &search->num_entries,
2054 &search->cache_size);
2057 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
2060 struct pdb_search *pdb_search_users(TALLOC_CTX *mem_ctx, uint32_t acct_flags)
2062 struct pdb_methods *pdb = pdb_get_methods();
2063 struct pdb_search *result;
2065 result = pdb_search_init(mem_ctx, PDB_USER_SEARCH);
2066 if (result == NULL) {
2067 return NULL;
2070 if (!pdb->search_users(pdb, result, acct_flags)) {
2071 TALLOC_FREE(result);
2072 return NULL;
2074 return result;
2077 struct pdb_search *pdb_search_groups(TALLOC_CTX *mem_ctx)
2079 struct pdb_methods *pdb = pdb_get_methods();
2080 struct pdb_search *result;
2082 result = pdb_search_init(mem_ctx, PDB_GROUP_SEARCH);
2083 if (result == NULL) {
2084 return NULL;
2087 if (!pdb->search_groups(pdb, result)) {
2088 TALLOC_FREE(result);
2089 return NULL;
2091 return result;
2094 struct pdb_search *pdb_search_aliases(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
2096 struct pdb_methods *pdb = pdb_get_methods();
2097 struct pdb_search *result;
2099 if (pdb == NULL) return NULL;
2101 result = pdb_search_init(mem_ctx, PDB_ALIAS_SEARCH);
2102 if (result == NULL) {
2103 return NULL;
2106 if (!pdb->search_aliases(pdb, result, sid)) {
2107 TALLOC_FREE(result);
2108 return NULL;
2110 return result;
2113 uint32_t pdb_search_entries(struct pdb_search *search,
2114 uint32_t start_idx, uint32_t max_entries,
2115 struct samr_displayentry **result)
2117 struct samr_displayentry *end_entry;
2118 uint32_t end_idx = start_idx+max_entries-1;
2120 /* The first entry needs to be searched after the last. Otherwise the
2121 * first entry might have moved due to a realloc during the search for
2122 * the last entry. */
2124 end_entry = pdb_search_getentry(search, end_idx);
2125 *result = pdb_search_getentry(search, start_idx);
2127 if (end_entry != NULL)
2128 return max_entries;
2130 if (start_idx >= search->num_entries)
2131 return 0;
2133 return search->num_entries - start_idx;
2136 /*******************************************************************
2137 trustdom methods
2138 *******************************************************************/
2140 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, struct dom_sid *sid,
2141 time_t *pass_last_set_time)
2143 struct pdb_methods *pdb = pdb_get_methods();
2144 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2145 pass_last_set_time);
2148 NTSTATUS pdb_get_trusteddom_creds(const char *domain, TALLOC_CTX *mem_ctx,
2149 struct cli_credentials **creds)
2151 struct pdb_methods *pdb = pdb_get_methods();
2152 return pdb->get_trusteddom_creds(pdb, domain, mem_ctx, creds);
2155 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2156 const struct dom_sid *sid)
2158 struct pdb_methods *pdb = pdb_get_methods();
2159 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2162 bool pdb_del_trusteddom_pw(const char *domain)
2164 struct pdb_methods *pdb = pdb_get_methods();
2165 return pdb->del_trusteddom_pw(pdb, domain);
2168 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2169 struct trustdom_info ***domains)
2171 struct pdb_methods *pdb = pdb_get_methods();
2172 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2175 /*******************************************************************
2176 the defaults for trustdom methods:
2177 these simply call the original passdb/secrets.c actions,
2178 to be replaced by pdb_ldap.
2179 *******************************************************************/
2181 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2182 const char *domain,
2183 char** pwd,
2184 struct dom_sid *sid,
2185 time_t *pass_last_set_time)
2187 return secrets_fetch_trusted_domain_password(domain, pwd,
2188 sid, pass_last_set_time);
2192 static NTSTATUS pdb_default_get_trusteddom_creds(struct pdb_methods *methods,
2193 const char *domain,
2194 TALLOC_CTX *mem_ctx,
2195 struct cli_credentials **creds)
2197 *creds = NULL;
2198 return NT_STATUS_NOT_IMPLEMENTED;
2201 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2202 const char* domain,
2203 const char* pwd,
2204 const struct dom_sid *sid)
2206 return secrets_store_trusted_domain_password(domain, pwd, sid);
2209 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2210 const char *domain)
2212 return trusted_domain_password_delete(domain);
2215 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2216 TALLOC_CTX *mem_ctx,
2217 uint32_t *num_domains,
2218 struct trustdom_info ***domains)
2220 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2223 /*******************************************************************
2224 trusted_domain methods
2225 *******************************************************************/
2227 NTSTATUS pdb_get_trusted_domain(TALLOC_CTX *mem_ctx, const char *domain,
2228 struct pdb_trusted_domain **td)
2230 struct pdb_methods *pdb = pdb_get_methods();
2231 return pdb->get_trusted_domain(pdb, mem_ctx, domain, td);
2234 NTSTATUS pdb_get_trusted_domain_by_sid(TALLOC_CTX *mem_ctx, struct dom_sid *sid,
2235 struct pdb_trusted_domain **td)
2237 struct pdb_methods *pdb = pdb_get_methods();
2238 return pdb->get_trusted_domain_by_sid(pdb, mem_ctx, sid, td);
2241 NTSTATUS pdb_set_trusted_domain(const char* domain,
2242 const struct pdb_trusted_domain *td)
2244 struct pdb_methods *pdb = pdb_get_methods();
2245 return pdb->set_trusted_domain(pdb, domain, td);
2248 NTSTATUS pdb_del_trusted_domain(const char *domain)
2250 struct pdb_methods *pdb = pdb_get_methods();
2251 return pdb->del_trusted_domain(pdb, domain);
2254 NTSTATUS pdb_enum_trusted_domains(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2255 struct pdb_trusted_domain ***domains)
2257 struct pdb_methods *pdb = pdb_get_methods();
2258 return pdb->enum_trusted_domains(pdb, mem_ctx, num_domains, domains);
2261 static NTSTATUS pdb_default_get_trusted_domain(struct pdb_methods *methods,
2262 TALLOC_CTX *mem_ctx,
2263 const char *domain,
2264 struct pdb_trusted_domain **td)
2266 struct trustAuthInOutBlob taiob;
2267 struct AuthenticationInformation aia;
2268 struct pdb_trusted_domain *tdom;
2269 enum ndr_err_code ndr_err;
2270 time_t last_set_time;
2271 char *pwd;
2272 bool ok;
2274 tdom = talloc(mem_ctx, struct pdb_trusted_domain);
2275 if (!tdom) {
2276 return NT_STATUS_NO_MEMORY;
2279 tdom->domain_name = talloc_strdup(tdom, domain);
2280 tdom->netbios_name = talloc_strdup(tdom, domain);
2281 if (!tdom->domain_name || !tdom->netbios_name) {
2282 talloc_free(tdom);
2283 return NT_STATUS_NO_MEMORY;
2286 tdom->trust_auth_incoming = data_blob_null;
2288 ok = pdb_get_trusteddom_pw(domain, &pwd, &tdom->security_identifier,
2289 &last_set_time);
2290 if (!ok) {
2291 talloc_free(tdom);
2292 return NT_STATUS_UNSUCCESSFUL;
2295 ZERO_STRUCT(taiob);
2296 ZERO_STRUCT(aia);
2297 taiob.count = 1;
2298 taiob.current.count = 1;
2299 taiob.current.array = &aia;
2300 unix_to_nt_time(&aia.LastUpdateTime, last_set_time);
2301 aia.AuthType = TRUST_AUTH_TYPE_CLEAR;
2302 aia.AuthInfo.clear.password = (uint8_t *) pwd;
2303 aia.AuthInfo.clear.size = strlen(pwd);
2304 taiob.previous.count = 0;
2305 taiob.previous.array = NULL;
2307 ndr_err = ndr_push_struct_blob(&tdom->trust_auth_outgoing,
2308 tdom, &taiob,
2309 (ndr_push_flags_fn_t)ndr_push_trustAuthInOutBlob);
2310 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2311 talloc_free(tdom);
2312 return NT_STATUS_UNSUCCESSFUL;
2315 tdom->trust_direction = LSA_TRUST_DIRECTION_OUTBOUND;
2316 tdom->trust_type = LSA_TRUST_TYPE_DOWNLEVEL;
2317 tdom->trust_attributes = 0;
2318 tdom->trust_forest_trust_info = data_blob_null;
2320 *td = tdom;
2321 return NT_STATUS_OK;
2324 static NTSTATUS pdb_default_get_trusted_domain_by_sid(struct pdb_methods *methods,
2325 TALLOC_CTX *mem_ctx,
2326 struct dom_sid *sid,
2327 struct pdb_trusted_domain **td)
2329 return NT_STATUS_NOT_IMPLEMENTED;
2332 #define IS_NULL_DATA_BLOB(d) ((d).data == NULL && (d).length == 0)
2334 static NTSTATUS pdb_default_set_trusted_domain(struct pdb_methods *methods,
2335 const char* domain,
2336 const struct pdb_trusted_domain *td)
2338 struct trustAuthInOutBlob taiob;
2339 struct AuthenticationInformation *aia;
2340 enum ndr_err_code ndr_err;
2341 char *pwd;
2342 bool ok;
2344 if (td->trust_attributes != 0 ||
2345 td->trust_type != LSA_TRUST_TYPE_DOWNLEVEL ||
2346 td->trust_direction != LSA_TRUST_DIRECTION_OUTBOUND ||
2347 !IS_NULL_DATA_BLOB(td->trust_auth_incoming) ||
2348 !IS_NULL_DATA_BLOB(td->trust_forest_trust_info)) {
2349 return NT_STATUS_NOT_IMPLEMENTED;
2352 ZERO_STRUCT(taiob);
2353 ndr_err = ndr_pull_struct_blob(&td->trust_auth_outgoing, talloc_tos(),
2354 &taiob,
2355 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2356 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2357 return NT_STATUS_UNSUCCESSFUL;
2360 aia = (struct AuthenticationInformation *) taiob.current.array;
2362 if (taiob.count != 1 || taiob.current.count != 1 ||
2363 taiob.previous.count != 0 ||
2364 aia->AuthType != TRUST_AUTH_TYPE_CLEAR) {
2365 return NT_STATUS_NOT_IMPLEMENTED;
2368 pwd = talloc_strndup(talloc_tos(), (char *) aia->AuthInfo.clear.password,
2369 aia->AuthInfo.clear.size);
2370 if (!pwd) {
2371 return NT_STATUS_NO_MEMORY;
2374 ok = pdb_set_trusteddom_pw(domain, pwd, &td->security_identifier);
2375 if (!ok) {
2376 return NT_STATUS_UNSUCCESSFUL;
2379 return NT_STATUS_OK;
2382 static NTSTATUS pdb_default_del_trusted_domain(struct pdb_methods *methods,
2383 const char *domain)
2385 return NT_STATUS_NOT_IMPLEMENTED;
2388 static NTSTATUS pdb_default_enum_trusted_domains(struct pdb_methods *methods,
2389 TALLOC_CTX *mem_ctx,
2390 uint32_t *num_domains,
2391 struct pdb_trusted_domain ***domains)
2393 return NT_STATUS_NOT_IMPLEMENTED;
2396 static struct pdb_domain_info *pdb_default_get_domain_info(
2397 struct pdb_methods *m, TALLOC_CTX *mem_ctx)
2399 return NULL;
2402 /*****************************************************************
2403 UPN suffixes
2404 *****************************************************************/
2405 static NTSTATUS pdb_default_enum_upn_suffixes(struct pdb_methods *pdb,
2406 TALLOC_CTX *mem_ctx,
2407 uint32_t *num_suffixes,
2408 char ***suffixes)
2410 return NT_STATUS_NOT_IMPLEMENTED;
2413 static NTSTATUS pdb_default_set_upn_suffixes(struct pdb_methods *pdb,
2414 uint32_t num_suffixes,
2415 const char **suffixes)
2417 return NT_STATUS_NOT_IMPLEMENTED;
2420 NTSTATUS pdb_enum_upn_suffixes(TALLOC_CTX *mem_ctx,
2421 uint32_t *num_suffixes,
2422 char ***suffixes)
2424 struct pdb_methods *pdb = pdb_get_methods();
2425 return pdb->enum_upn_suffixes(pdb, mem_ctx, num_suffixes, suffixes);
2428 NTSTATUS pdb_set_upn_suffixes(uint32_t num_suffixes,
2429 const char **suffixes)
2431 struct pdb_methods *pdb = pdb_get_methods();
2432 return pdb->set_upn_suffixes(pdb, num_suffixes, suffixes);
2435 /*******************************************************************
2436 idmap control methods
2437 *******************************************************************/
2438 static bool pdb_default_is_responsible_for_our_sam(
2439 struct pdb_methods *methods)
2441 return true;
2444 static bool pdb_default_is_responsible_for_builtin(
2445 struct pdb_methods *methods)
2447 return true;
2450 static bool pdb_default_is_responsible_for_wellknown(
2451 struct pdb_methods *methods)
2453 return false;
2456 static bool pdb_default_is_responsible_for_unix_users(
2457 struct pdb_methods *methods)
2459 return true;
2462 static bool pdb_default_is_responsible_for_unix_groups(
2463 struct pdb_methods *methods)
2465 return true;
2468 static bool pdb_default_is_responsible_for_everything_else(
2469 struct pdb_methods *methods)
2471 return false;
2474 bool pdb_is_responsible_for_our_sam(void)
2476 struct pdb_methods *pdb = pdb_get_methods();
2477 return pdb->is_responsible_for_our_sam(pdb);
2480 bool pdb_is_responsible_for_builtin(void)
2482 struct pdb_methods *pdb = pdb_get_methods();
2483 return pdb->is_responsible_for_builtin(pdb);
2486 bool pdb_is_responsible_for_wellknown(void)
2488 struct pdb_methods *pdb = pdb_get_methods();
2489 return pdb->is_responsible_for_wellknown(pdb);
2492 bool pdb_is_responsible_for_unix_users(void)
2494 struct pdb_methods *pdb = pdb_get_methods();
2495 return pdb->is_responsible_for_unix_users(pdb);
2498 bool pdb_is_responsible_for_unix_groups(void)
2500 struct pdb_methods *pdb = pdb_get_methods();
2501 return pdb->is_responsible_for_unix_groups(pdb);
2504 bool pdb_is_responsible_for_everything_else(void)
2506 struct pdb_methods *pdb = pdb_get_methods();
2507 return pdb->is_responsible_for_everything_else(pdb);
2510 /*******************************************************************
2511 secret methods
2512 *******************************************************************/
2514 NTSTATUS pdb_get_secret(TALLOC_CTX *mem_ctx,
2515 const char *secret_name,
2516 DATA_BLOB *secret_current,
2517 NTTIME *secret_current_lastchange,
2518 DATA_BLOB *secret_old,
2519 NTTIME *secret_old_lastchange,
2520 struct security_descriptor **sd)
2522 struct pdb_methods *pdb = pdb_get_methods();
2523 return pdb->get_secret(pdb, mem_ctx, secret_name,
2524 secret_current, secret_current_lastchange,
2525 secret_old, secret_old_lastchange,
2526 sd);
2529 NTSTATUS pdb_set_secret(const char *secret_name,
2530 DATA_BLOB *secret_current,
2531 DATA_BLOB *secret_old,
2532 struct security_descriptor *sd)
2534 struct pdb_methods *pdb = pdb_get_methods();
2535 return pdb->set_secret(pdb, secret_name,
2536 secret_current,
2537 secret_old,
2538 sd);
2541 NTSTATUS pdb_delete_secret(const char *secret_name)
2543 struct pdb_methods *pdb = pdb_get_methods();
2544 return pdb->delete_secret(pdb, secret_name);
2547 static NTSTATUS pdb_default_get_secret(struct pdb_methods *methods,
2548 TALLOC_CTX *mem_ctx,
2549 const char *secret_name,
2550 DATA_BLOB *secret_current,
2551 NTTIME *secret_current_lastchange,
2552 DATA_BLOB *secret_old,
2553 NTTIME *secret_old_lastchange,
2554 struct security_descriptor **sd)
2556 return lsa_secret_get(mem_ctx, secret_name,
2557 secret_current,
2558 secret_current_lastchange,
2559 secret_old,
2560 secret_old_lastchange,
2561 sd);
2564 static NTSTATUS pdb_default_set_secret(struct pdb_methods *methods,
2565 const char *secret_name,
2566 DATA_BLOB *secret_current,
2567 DATA_BLOB *secret_old,
2568 struct security_descriptor *sd)
2570 return lsa_secret_set(secret_name,
2571 secret_current,
2572 secret_old,
2573 sd);
2576 static NTSTATUS pdb_default_delete_secret(struct pdb_methods *methods,
2577 const char *secret_name)
2579 return lsa_secret_delete(secret_name);
2582 /*******************************************************************
2583 Create a pdb_methods structure and initialize it with the default
2584 operations. In this way a passdb module can simply implement
2585 the functionality it cares about. However, normally this is done
2586 in groups of related functions.
2587 *******************************************************************/
2589 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2591 /* allocate memory for the structure as its own talloc CTX */
2593 *methods = talloc_zero(NULL, struct pdb_methods);
2594 if (*methods == NULL) {
2595 return NT_STATUS_NO_MEMORY;
2598 (*methods)->get_domain_info = pdb_default_get_domain_info;
2599 (*methods)->getsampwnam = pdb_default_getsampwnam;
2600 (*methods)->getsampwsid = pdb_default_getsampwsid;
2601 (*methods)->create_user = pdb_default_create_user;
2602 (*methods)->delete_user = pdb_default_delete_user;
2603 (*methods)->add_sam_account = pdb_default_add_sam_account;
2604 (*methods)->update_sam_account = pdb_default_update_sam_account;
2605 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2606 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2607 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2609 (*methods)->getgrsid = pdb_default_getgrsid;
2610 (*methods)->getgrgid = pdb_default_getgrgid;
2611 (*methods)->getgrnam = pdb_default_getgrnam;
2612 (*methods)->create_dom_group = pdb_default_create_dom_group;
2613 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2614 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2615 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2616 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2617 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2618 (*methods)->enum_group_members = pdb_default_enum_group_members;
2619 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2620 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2621 (*methods)->add_groupmem = pdb_default_add_groupmem;
2622 (*methods)->del_groupmem = pdb_default_del_groupmem;
2623 (*methods)->create_alias = pdb_default_create_alias;
2624 (*methods)->delete_alias = pdb_default_delete_alias;
2625 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2626 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2627 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2628 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2629 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2630 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2631 (*methods)->lookup_rids = pdb_default_lookup_rids;
2632 (*methods)->get_account_policy = pdb_default_get_account_policy;
2633 (*methods)->set_account_policy = pdb_default_set_account_policy;
2634 (*methods)->get_seq_num = pdb_default_get_seq_num;
2635 (*methods)->id_to_sid = pdb_default_id_to_sid;
2636 (*methods)->sid_to_id = pdb_default_sid_to_id;
2638 (*methods)->search_groups = pdb_default_search_groups;
2639 (*methods)->search_aliases = pdb_default_search_aliases;
2641 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2642 (*methods)->get_trusteddom_creds = pdb_default_get_trusteddom_creds;
2643 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2644 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2645 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2647 (*methods)->get_trusted_domain = pdb_default_get_trusted_domain;
2648 (*methods)->get_trusted_domain_by_sid = pdb_default_get_trusted_domain_by_sid;
2649 (*methods)->set_trusted_domain = pdb_default_set_trusted_domain;
2650 (*methods)->del_trusted_domain = pdb_default_del_trusted_domain;
2651 (*methods)->enum_trusted_domains = pdb_default_enum_trusted_domains;
2653 (*methods)->get_secret = pdb_default_get_secret;
2654 (*methods)->set_secret = pdb_default_set_secret;
2655 (*methods)->delete_secret = pdb_default_delete_secret;
2657 (*methods)->enum_upn_suffixes = pdb_default_enum_upn_suffixes;
2658 (*methods)->set_upn_suffixes = pdb_default_set_upn_suffixes;
2660 (*methods)->is_responsible_for_our_sam =
2661 pdb_default_is_responsible_for_our_sam;
2662 (*methods)->is_responsible_for_builtin =
2663 pdb_default_is_responsible_for_builtin;
2664 (*methods)->is_responsible_for_wellknown =
2665 pdb_default_is_responsible_for_wellknown;
2666 (*methods)->is_responsible_for_unix_users =
2667 pdb_default_is_responsible_for_unix_users;
2668 (*methods)->is_responsible_for_unix_groups =
2669 pdb_default_is_responsible_for_unix_groups;
2670 (*methods)->is_responsible_for_everything_else =
2671 pdb_default_is_responsible_for_everything_else;
2673 return NT_STATUS_OK;