s3:smb2_server: add SMB3 encryption support
[Samba/gebeck_regimport.git] / source3 / passdb / pdb_interface.c
blobcdbb378e947970e4e9b0b69c24720ef20d0cc6cd
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 "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"
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_PASSDB
41 static_decl_pdb;
43 static struct pdb_init_function_entry *backends = NULL;
45 static void lazy_initialize_passdb(void)
47 static bool initialized = False;
48 if(initialized) {
49 return;
51 static_init_pdb;
52 initialized = True;
55 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
56 const char **name,
57 enum lsa_SidType *psid_name_use,
58 uid_t *uid, gid_t *gid);
60 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
62 struct pdb_init_function_entry *entry = backends;
64 if(version != PASSDB_INTERFACE_VERSION) {
65 DEBUG(0,("Can't register passdb backend!\n"
66 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
67 "while this version of samba uses version %d\n",
68 version,PASSDB_INTERFACE_VERSION));
69 return NT_STATUS_OBJECT_TYPE_MISMATCH;
72 if (!name || !init) {
73 return NT_STATUS_INVALID_PARAMETER;
76 DEBUG(5,("Attempting to register passdb backend %s\n", name));
78 /* Check for duplicates */
79 if (pdb_find_backend_entry(name)) {
80 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
81 return NT_STATUS_OBJECT_NAME_COLLISION;
84 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
85 entry->name = smb_xstrdup(name);
86 entry->init = init;
88 DLIST_ADD(backends, entry);
89 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
90 return NT_STATUS_OK;
93 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
95 struct pdb_init_function_entry *entry = backends;
97 while(entry) {
98 if (strcmp(entry->name, name)==0) return entry;
99 entry = entry->next;
102 return NULL;
105 const struct pdb_init_function_entry *pdb_get_backends(void)
107 return backends;
112 * The event context for the passdb backend. I know this is a bad hack and yet
113 * another static variable, but our pdb API is a global thing per
114 * definition. The first use for this is the LDAP idle function, more might be
115 * added later.
117 * I don't feel too bad about this static variable, it replaces the
118 * smb_idle_event_list that used to exist in lib/module.c. -- VL
121 static struct tevent_context *pdb_tevent_ctx;
123 struct tevent_context *pdb_get_tevent_context(void)
125 return pdb_tevent_ctx;
128 /******************************************************************
129 Make a pdb_methods from scratch
130 *******************************************************************/
132 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
134 char *module_name = smb_xstrdup(selected);
135 char *module_location = NULL, *p;
136 struct pdb_init_function_entry *entry;
137 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
139 lazy_initialize_passdb();
141 p = strchr(module_name, ':');
143 if (p) {
144 *p = 0;
145 module_location = p+1;
146 trim_char(module_location, ' ', ' ');
149 trim_char(module_name, ' ', ' ');
152 DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected, module_name));
154 entry = pdb_find_backend_entry(module_name);
156 /* Try to find a module that contains this module */
157 if (!entry) {
158 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
159 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
160 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
161 SAFE_FREE(module_name);
162 return NT_STATUS_UNSUCCESSFUL;
166 /* No such backend found */
167 if(!entry) {
168 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
169 SAFE_FREE(module_name);
170 return NT_STATUS_INVALID_PARAMETER;
173 DEBUG(5,("Found pdb backend %s\n", module_name));
175 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
176 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
177 selected, nt_errstr(nt_status)));
178 SAFE_FREE(module_name);
179 return nt_status;
182 SAFE_FREE(module_name);
184 DEBUG(5,("pdb backend %s has a valid init\n", selected));
186 return nt_status;
189 /******************************************************************
190 Return an already initialized pdb_methods structure
191 *******************************************************************/
193 static struct pdb_methods *pdb_get_methods_reload( bool reload )
195 static struct pdb_methods *pdb = NULL;
197 if ( pdb && reload ) {
198 if (pdb->free_private_data != NULL) {
199 pdb->free_private_data( &(pdb->private_data) );
201 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
202 return NULL;
206 if ( !pdb ) {
207 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
208 return NULL;
212 return pdb;
215 static struct pdb_methods *pdb_get_methods(void)
217 struct pdb_methods *pdb;
219 pdb = pdb_get_methods_reload(false);
220 if (!pdb) {
221 char *msg = NULL;
222 if (asprintf(&msg, "pdb_get_methods: "
223 "failed to get pdb methods for backend %s\n",
224 lp_passdb_backend()) > 0) {
225 smb_panic(msg);
226 } else {
227 smb_panic("pdb_get_methods");
231 return pdb;
234 struct pdb_domain_info *pdb_get_domain_info(TALLOC_CTX *mem_ctx)
236 struct pdb_methods *pdb = pdb_get_methods();
237 return pdb->get_domain_info(pdb, mem_ctx);
241 * @brief Check if the user account has been locked out and try to unlock it.
243 * If the user has been automatically locked out and a lockout duration is set,
244 * then check if we can unlock the account and reset the bad password values.
246 * @param[in] sampass The sam user to check.
248 * @return True if the function was successfull, false on an error.
250 static bool pdb_try_account_unlock(struct samu *sampass)
252 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
254 if ((acb_info & ACB_NORMAL) && (acb_info & ACB_AUTOLOCK)) {
255 uint32_t lockout_duration;
256 time_t bad_password_time;
257 time_t now = time(NULL);
258 bool ok;
260 ok = pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
261 &lockout_duration);
262 if (!ok) {
263 DEBUG(0, ("pdb_try_account_unlock: "
264 "pdb_get_account_policy failed.\n"));
265 return false;
268 if (lockout_duration == (uint32_t) -1 ||
269 lockout_duration == 0) {
270 DEBUG(9, ("pdb_try_account_unlock: No reset duration, "
271 "can't reset autolock\n"));
272 return false;
274 lockout_duration *= 60;
276 bad_password_time = pdb_get_bad_password_time(sampass);
277 if (bad_password_time == (time_t) 0) {
278 DEBUG(2, ("pdb_try_account_unlock: Account %s "
279 "administratively locked out "
280 "with no bad password "
281 "time. Leaving locked out.\n",
282 pdb_get_username(sampass)));
283 return true;
286 if ((bad_password_time +
287 convert_uint32_t_to_time_t(lockout_duration)) < now) {
288 NTSTATUS status;
290 pdb_set_acct_ctrl(sampass, acb_info & ~ACB_AUTOLOCK,
291 PDB_CHANGED);
292 pdb_set_bad_password_count(sampass, 0, PDB_CHANGED);
293 pdb_set_bad_password_time(sampass, 0, PDB_CHANGED);
295 become_root();
296 status = pdb_update_sam_account(sampass);
297 unbecome_root();
298 if (!NT_STATUS_IS_OK(status)) {
299 DEBUG(0, ("_samr_OpenUser: Couldn't "
300 "update account %s - %s\n",
301 pdb_get_username(sampass),
302 nt_errstr(status)));
303 return false;
308 return true;
312 * @brief Get a sam user structure by the given username.
314 * This functions also checks if the account has been automatically locked out
315 * and unlocks it if a lockout duration time has been defined and the time has
316 * elapsed.
318 * @param[in] sam_acct The sam user structure to fill.
320 * @param[in] username The username to look for.
322 * @return True on success, false on error.
324 bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
326 struct pdb_methods *pdb = pdb_get_methods();
327 struct samu *for_cache;
328 const struct dom_sid *user_sid;
329 NTSTATUS status;
330 bool ok;
332 status = pdb->getsampwnam(pdb, sam_acct, username);
333 if (!NT_STATUS_IS_OK(status)) {
334 return false;
337 ok = pdb_try_account_unlock(sam_acct);
338 if (!ok) {
339 DEBUG(1, ("pdb_getsampwnam: Failed to unlock account %s\n",
340 username));
343 for_cache = samu_new(NULL);
344 if (for_cache == NULL) {
345 return False;
348 if (!pdb_copy_sam_account(for_cache, sam_acct)) {
349 TALLOC_FREE(for_cache);
350 return False;
353 user_sid = pdb_get_user_sid(for_cache);
355 memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
356 data_blob_const(user_sid, sizeof(*user_sid)),
357 &for_cache);
359 return True;
362 /**********************************************************************
363 **********************************************************************/
365 static bool guest_user_info( struct samu *user )
367 struct passwd *pwd;
368 NTSTATUS result;
369 const char *guestname = lp_guestaccount();
371 pwd = Get_Pwnam_alloc(talloc_tos(), guestname);
372 if (pwd == NULL) {
373 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
374 guestname));
375 return False;
378 result = samu_set_unix(user, pwd );
380 TALLOC_FREE( pwd );
382 return NT_STATUS_IS_OK( result );
386 * @brief Get a sam user structure by the given username.
388 * This functions also checks if the account has been automatically locked out
389 * and unlocks it if a lockout duration time has been defined and the time has
390 * elapsed.
393 * @param[in] sam_acct The sam user structure to fill.
395 * @param[in] sid The user SDI to look up.
397 * @return True on success, false on error.
399 bool pdb_getsampwsid(struct samu *sam_acct, const struct dom_sid *sid)
401 struct pdb_methods *pdb = pdb_get_methods();
402 uint32_t rid;
403 void *cache_data;
404 bool ok = false;
406 /* hard code the Guest RID of 501 */
408 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
409 return False;
411 if ( rid == DOMAIN_RID_GUEST ) {
412 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
413 return guest_user_info( sam_acct );
416 /* check the cache first */
418 cache_data = memcache_lookup_talloc(
419 NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
421 if (cache_data != NULL) {
422 struct samu *cache_copy = talloc_get_type_abort(
423 cache_data, struct samu);
425 ok = pdb_copy_sam_account(sam_acct, cache_copy);
426 } else {
427 ok = NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
430 if (!ok) {
431 return false;
434 ok = pdb_try_account_unlock(sam_acct);
435 if (!ok) {
436 DEBUG(1, ("pdb_getsampwsid: Failed to unlock account %s\n",
437 sam_acct->username));
440 return true;
443 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
444 TALLOC_CTX *tmp_ctx, const char *name,
445 uint32_t acb_info, uint32_t *rid)
447 struct samu *sam_pass;
448 NTSTATUS status;
449 struct passwd *pwd;
451 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
452 return NT_STATUS_NO_MEMORY;
455 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
456 char *add_script = NULL;
457 int add_ret;
458 fstring name2;
460 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
461 add_script = lp_adduser_script(tmp_ctx);
462 } else {
463 add_script = lp_addmachine_script(tmp_ctx);
466 if (!add_script || add_script[0] == '\0') {
467 DEBUG(3, ("Could not find user %s and no add script "
468 "defined\n", name));
469 return NT_STATUS_NO_SUCH_USER;
472 /* lowercase the username before creating the Unix account for
473 compatibility with previous Samba releases */
474 fstrcpy( name2, name );
475 if (!strlower_m( name2 )) {
476 return NT_STATUS_INVALID_PARAMETER;
478 add_script = talloc_all_string_sub(tmp_ctx,
479 add_script,
480 "%u",
481 name2);
482 if (!add_script) {
483 return NT_STATUS_NO_MEMORY;
485 add_ret = smbrun(add_script,NULL);
486 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
487 add_script, add_ret));
488 if (add_ret == 0) {
489 smb_nscd_flush_user_cache();
492 flush_pwnam_cache();
494 pwd = Get_Pwnam_alloc(tmp_ctx, name);
496 if(pwd == NULL) {
497 DEBUG(3, ("Could not find user %s, add script did not work\n", name));
498 return NT_STATUS_NO_SUCH_USER;
502 /* we have a valid SID coming out of this call */
504 status = samu_alloc_rid_unix(methods, sam_pass, pwd);
506 TALLOC_FREE( pwd );
508 if (!NT_STATUS_IS_OK(status)) {
509 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
510 return status;
513 if (!sid_peek_check_rid(get_global_sam_sid(),
514 pdb_get_user_sid(sam_pass), rid)) {
515 DEBUG(0, ("Could not get RID of fresh user\n"));
516 return NT_STATUS_INTERNAL_ERROR;
519 /* Use the username case specified in the original request */
521 pdb_set_username( sam_pass, name, PDB_SET );
523 /* Disable the account on creation, it does not have a reasonable password yet. */
525 acb_info |= ACB_DISABLED;
527 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
529 status = methods->add_sam_account(methods, sam_pass);
531 TALLOC_FREE(sam_pass);
533 return status;
536 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32_t flags,
537 uint32_t *rid)
539 struct pdb_methods *pdb = pdb_get_methods();
540 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
543 /****************************************************************************
544 Delete a UNIX user on demand.
545 ****************************************************************************/
547 static int smb_delete_user(const char *unix_user)
549 char *del_script = NULL;
550 int ret;
552 /* safety check */
554 if ( strequal( unix_user, "root" ) ) {
555 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
556 return -1;
559 del_script = lp_deluser_script(talloc_tos());
560 if (!del_script || !*del_script) {
561 return -1;
563 del_script = talloc_all_string_sub(talloc_tos(),
564 del_script,
565 "%u",
566 unix_user);
567 if (!del_script) {
568 return -1;
570 ret = smbrun(del_script,NULL);
571 flush_pwnam_cache();
572 if (ret == 0) {
573 smb_nscd_flush_user_cache();
575 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
577 return ret;
580 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
581 TALLOC_CTX *mem_ctx,
582 struct samu *sam_acct)
584 NTSTATUS status;
585 fstring username;
587 status = methods->delete_sam_account(methods, sam_acct);
588 if (!NT_STATUS_IS_OK(status)) {
589 return status;
593 * Now delete the unix side ....
594 * note: we don't check if the delete really happened as the script is
595 * not necessary present and maybe the sysadmin doesn't want to delete
596 * the unix side
599 /* always lower case the username before handing it off to
600 external scripts */
602 fstrcpy( username, pdb_get_username(sam_acct) );
603 if (!strlower_m( username )) {
604 return status;
607 smb_delete_user( username );
609 return status;
612 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
614 struct pdb_methods *pdb = pdb_get_methods();
615 uid_t uid = -1;
616 NTSTATUS status;
617 const struct dom_sid *user_sid;
618 char *msg_data;
620 user_sid = pdb_get_user_sid(sam_acct);
622 /* sanity check to make sure we don't delete root */
624 if ( !sid_to_uid(user_sid, &uid ) ) {
625 return NT_STATUS_NO_SUCH_USER;
628 if ( uid == 0 ) {
629 return NT_STATUS_ACCESS_DENIED;
632 memcache_delete(NULL,
633 PDB_GETPWSID_CACHE,
634 data_blob_const(user_sid, sizeof(*user_sid)));
636 status = pdb->delete_user(pdb, mem_ctx, sam_acct);
637 if (!NT_STATUS_IS_OK(status)) {
638 return status;
641 msg_data = talloc_asprintf(mem_ctx, "USER %s",
642 pdb_get_username(sam_acct));
643 if (!msg_data) {
644 /* not fatal, and too late to rollback,
645 * just return */
646 return status;
648 message_send_all(server_messaging_context(),
649 ID_CACHE_DELETE,
650 msg_data,
651 strlen(msg_data) + 1,
652 NULL);
654 TALLOC_FREE(msg_data);
655 return status;
658 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
660 struct pdb_methods *pdb = pdb_get_methods();
661 return pdb->add_sam_account(pdb, sam_acct);
664 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
666 struct pdb_methods *pdb = pdb_get_methods();
668 memcache_flush(NULL, PDB_GETPWSID_CACHE);
670 return pdb->update_sam_account(pdb, sam_acct);
673 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
675 struct pdb_methods *pdb = pdb_get_methods();
676 const struct dom_sid *user_sid = pdb_get_user_sid(sam_acct);
678 memcache_delete(NULL,
679 PDB_GETPWSID_CACHE,
680 data_blob_const(user_sid, sizeof(*user_sid)));
682 return pdb->delete_sam_account(pdb, sam_acct);
685 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
687 struct pdb_methods *pdb = pdb_get_methods();
688 uid_t uid;
689 NTSTATUS status;
691 memcache_flush(NULL, PDB_GETPWSID_CACHE);
693 /* sanity check to make sure we don't rename root */
695 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
696 return NT_STATUS_NO_SUCH_USER;
699 if ( uid == 0 ) {
700 return NT_STATUS_ACCESS_DENIED;
703 status = pdb->rename_sam_account(pdb, oldname, newname);
705 /* always flush the cache here just to be safe */
706 flush_pwnam_cache();
708 return status;
711 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
713 struct pdb_methods *pdb = pdb_get_methods();
714 return pdb->update_login_attempts(pdb, sam_acct, success);
717 bool pdb_getgrsid(GROUP_MAP *map, struct dom_sid sid)
719 struct pdb_methods *pdb = pdb_get_methods();
720 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
723 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
725 struct pdb_methods *pdb = pdb_get_methods();
726 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
729 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
731 struct pdb_methods *pdb = pdb_get_methods();
732 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
735 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
736 TALLOC_CTX *mem_ctx,
737 const char *name,
738 uint32_t *rid)
740 struct dom_sid group_sid;
741 struct group *grp;
742 fstring tmp;
744 grp = getgrnam(name);
746 if (grp == NULL) {
747 gid_t gid;
749 if (smb_create_group(name, &gid) != 0) {
750 return NT_STATUS_ACCESS_DENIED;
753 grp = getgrgid(gid);
756 if (grp == NULL) {
757 return NT_STATUS_ACCESS_DENIED;
760 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
761 if (!pdb_new_rid(rid)) {
762 return NT_STATUS_ACCESS_DENIED;
764 } else {
765 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
768 sid_compose(&group_sid, get_global_sam_sid(), *rid);
770 return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
771 SID_NAME_DOM_GRP, name, NULL);
774 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
775 uint32_t *rid)
777 struct pdb_methods *pdb = pdb_get_methods();
778 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
781 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
782 TALLOC_CTX *mem_ctx,
783 uint32_t rid)
785 struct dom_sid group_sid;
786 GROUP_MAP *map;
787 NTSTATUS status;
788 struct group *grp;
789 const char *grp_name;
791 map = talloc_zero(mem_ctx, GROUP_MAP);
792 if (!map) {
793 return NT_STATUS_NO_MEMORY;
796 /* coverity */
797 map->gid = (gid_t) -1;
799 sid_compose(&group_sid, get_global_sam_sid(), rid);
801 if (!get_domain_group_from_sid(group_sid, map)) {
802 DEBUG(10, ("Could not find group for rid %d\n", rid));
803 return NT_STATUS_NO_SUCH_GROUP;
806 /* We need the group name for the smb_delete_group later on */
808 if (map->gid == (gid_t)-1) {
809 return NT_STATUS_NO_SUCH_GROUP;
812 grp = getgrgid(map->gid);
813 if (grp == NULL) {
814 return NT_STATUS_NO_SUCH_GROUP;
817 TALLOC_FREE(map);
819 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
821 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
822 if (grp_name == NULL) {
823 return NT_STATUS_NO_MEMORY;
826 status = pdb_delete_group_mapping_entry(group_sid);
828 if (!NT_STATUS_IS_OK(status)) {
829 return status;
832 /* Don't check the result of smb_delete_group */
834 smb_delete_group(grp_name);
836 return NT_STATUS_OK;
839 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32_t rid)
841 struct pdb_methods *pdb = pdb_get_methods();
842 return pdb->delete_dom_group(pdb, mem_ctx, rid);
845 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
847 struct pdb_methods *pdb = pdb_get_methods();
848 return pdb->add_group_mapping_entry(pdb, map);
851 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
853 struct pdb_methods *pdb = pdb_get_methods();
854 return pdb->update_group_mapping_entry(pdb, map);
857 NTSTATUS pdb_delete_group_mapping_entry(struct dom_sid sid)
859 struct pdb_methods *pdb = pdb_get_methods();
860 return pdb->delete_group_mapping_entry(pdb, sid);
863 bool pdb_enum_group_mapping(const struct dom_sid *sid,
864 enum lsa_SidType sid_name_use,
865 GROUP_MAP ***pp_rmap,
866 size_t *p_num_entries,
867 bool unix_only)
869 struct pdb_methods *pdb = pdb_get_methods();
870 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
871 pp_rmap, p_num_entries, unix_only));
874 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
875 const struct dom_sid *sid,
876 uint32_t **pp_member_rids,
877 size_t *p_num_members)
879 struct pdb_methods *pdb = pdb_get_methods();
880 NTSTATUS result;
882 result = pdb->enum_group_members(pdb, mem_ctx,
883 sid, pp_member_rids, p_num_members);
885 /* special check for rid 513 */
887 if ( !NT_STATUS_IS_OK( result ) ) {
888 uint32_t rid;
890 sid_peek_rid( sid, &rid );
892 if ( rid == DOMAIN_RID_USERS ) {
893 *p_num_members = 0;
894 *pp_member_rids = NULL;
896 return NT_STATUS_OK;
900 return result;
903 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
904 struct dom_sid **pp_sids, gid_t **pp_gids,
905 uint32_t *p_num_groups)
907 struct pdb_methods *pdb = pdb_get_methods();
908 return pdb->enum_group_memberships(
909 pdb, mem_ctx, user,
910 pp_sids, pp_gids, p_num_groups);
913 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
914 TALLOC_CTX *mem_ctx,
915 struct samu *sampass)
917 struct group *grp;
918 gid_t gid;
920 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
921 (grp = getgrgid(gid)) == NULL) {
922 return NT_STATUS_INVALID_PRIMARY_GROUP;
925 if (smb_set_primary_group(grp->gr_name,
926 pdb_get_username(sampass)) != 0) {
927 return NT_STATUS_ACCESS_DENIED;
930 return NT_STATUS_OK;
933 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
935 struct pdb_methods *pdb = pdb_get_methods();
936 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
940 * Helper function to see whether a user is in a group. We can't use
941 * user_in_group_sid here because this creates dependencies only smbd can
942 * fulfil.
945 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
946 const struct dom_sid *group_sid)
948 struct dom_sid *sids;
949 gid_t *gids;
950 uint32_t i, num_groups;
952 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
953 &sids, &gids,
954 &num_groups))) {
955 return False;
958 for (i=0; i<num_groups; i++) {
959 if (dom_sid_equal(group_sid, &sids[i])) {
960 return True;
963 return False;
966 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
967 TALLOC_CTX *mem_ctx,
968 uint32_t group_rid,
969 uint32_t member_rid)
971 struct dom_sid group_sid, member_sid;
972 struct samu *account = NULL;
973 GROUP_MAP *map;
974 struct group *grp;
975 struct passwd *pwd;
976 const char *group_name;
977 uid_t uid;
979 map = talloc_zero(mem_ctx, GROUP_MAP);
980 if (!map) {
981 return NT_STATUS_NO_MEMORY;
984 /* coverity */
985 map->gid = (gid_t) -1;
987 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
988 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
990 if (!get_domain_group_from_sid(group_sid, map) ||
991 (map->gid == (gid_t)-1) ||
992 ((grp = getgrgid(map->gid)) == NULL)) {
993 return NT_STATUS_NO_SUCH_GROUP;
996 TALLOC_FREE(map);
998 group_name = talloc_strdup(mem_ctx, grp->gr_name);
999 if (group_name == NULL) {
1000 return NT_STATUS_NO_MEMORY;
1003 if ( !(account = samu_new( NULL )) ) {
1004 return NT_STATUS_NO_MEMORY;
1007 if (!pdb_getsampwsid(account, &member_sid) ||
1008 !sid_to_uid(&member_sid, &uid) ||
1009 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
1010 return NT_STATUS_NO_SUCH_USER;
1013 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1014 return NT_STATUS_MEMBER_IN_GROUP;
1018 * ok, the group exist, the user exist, the user is not in the group,
1019 * we can (finally) add it to the group !
1022 smb_add_user_group(group_name, pwd->pw_name);
1024 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1025 return NT_STATUS_ACCESS_DENIED;
1028 return NT_STATUS_OK;
1031 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1032 uint32_t member_rid)
1034 struct pdb_methods *pdb = pdb_get_methods();
1035 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
1038 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
1039 TALLOC_CTX *mem_ctx,
1040 uint32_t group_rid,
1041 uint32_t member_rid)
1043 struct dom_sid group_sid, member_sid;
1044 struct samu *account = NULL;
1045 GROUP_MAP *map;
1046 struct group *grp;
1047 struct passwd *pwd;
1048 const char *group_name;
1049 uid_t uid;
1051 map = talloc_zero(mem_ctx, GROUP_MAP);
1052 if (!map) {
1053 return NT_STATUS_NO_MEMORY;
1056 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
1057 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
1059 if (!get_domain_group_from_sid(group_sid, map) ||
1060 (map->gid == (gid_t)-1) ||
1061 ((grp = getgrgid(map->gid)) == NULL)) {
1062 return NT_STATUS_NO_SUCH_GROUP;
1065 TALLOC_FREE(map);
1067 group_name = talloc_strdup(mem_ctx, grp->gr_name);
1068 if (group_name == NULL) {
1069 return NT_STATUS_NO_MEMORY;
1072 if ( !(account = samu_new( NULL )) ) {
1073 return NT_STATUS_NO_MEMORY;
1076 if (!pdb_getsampwsid(account, &member_sid) ||
1077 !sid_to_uid(&member_sid, &uid) ||
1078 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
1079 return NT_STATUS_NO_SUCH_USER;
1082 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1083 return NT_STATUS_MEMBER_NOT_IN_GROUP;
1087 * ok, the group exist, the user exist, the user is in the group,
1088 * we can (finally) delete it from the group!
1091 smb_delete_user_group(group_name, pwd->pw_name);
1093 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1094 return NT_STATUS_ACCESS_DENIED;
1097 return NT_STATUS_OK;
1100 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1101 uint32_t member_rid)
1103 struct pdb_methods *pdb = pdb_get_methods();
1104 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
1107 NTSTATUS pdb_create_alias(const char *name, uint32_t *rid)
1109 struct pdb_methods *pdb = pdb_get_methods();
1110 return pdb->create_alias(pdb, name, rid);
1113 NTSTATUS pdb_delete_alias(const struct dom_sid *sid)
1115 struct pdb_methods *pdb = pdb_get_methods();
1116 return pdb->delete_alias(pdb, sid);
1119 NTSTATUS pdb_get_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1121 struct pdb_methods *pdb = pdb_get_methods();
1122 return pdb->get_aliasinfo(pdb, sid, info);
1125 NTSTATUS pdb_set_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1127 struct pdb_methods *pdb = pdb_get_methods();
1128 return pdb->set_aliasinfo(pdb, sid, info);
1131 NTSTATUS pdb_add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1133 struct pdb_methods *pdb = pdb_get_methods();
1134 return pdb->add_aliasmem(pdb, alias, member);
1137 NTSTATUS pdb_del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1139 struct pdb_methods *pdb = pdb_get_methods();
1140 return pdb->del_aliasmem(pdb, alias, member);
1143 NTSTATUS pdb_enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
1144 struct dom_sid **pp_members, size_t *p_num_members)
1146 struct pdb_methods *pdb = pdb_get_methods();
1147 return pdb->enum_aliasmem(pdb, alias, mem_ctx, pp_members,
1148 p_num_members);
1151 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1152 const struct dom_sid *domain_sid,
1153 const struct dom_sid *members, size_t num_members,
1154 uint32_t **pp_alias_rids,
1155 size_t *p_num_alias_rids)
1157 struct pdb_methods *pdb = pdb_get_methods();
1158 return pdb->enum_alias_memberships(pdb, mem_ctx,
1159 domain_sid,
1160 members, num_members,
1161 pp_alias_rids,
1162 p_num_alias_rids);
1165 NTSTATUS pdb_lookup_rids(const struct dom_sid *domain_sid,
1166 int num_rids,
1167 uint32_t *rids,
1168 const char **names,
1169 enum lsa_SidType *attrs)
1171 struct pdb_methods *pdb = pdb_get_methods();
1172 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
1175 bool pdb_get_account_policy(enum pdb_policy_type type, uint32_t *value)
1177 struct pdb_methods *pdb = pdb_get_methods();
1178 NTSTATUS status;
1180 become_root();
1181 status = pdb->get_account_policy(pdb, type, value);
1182 unbecome_root();
1184 return NT_STATUS_IS_OK(status);
1187 bool pdb_set_account_policy(enum pdb_policy_type type, uint32_t value)
1189 struct pdb_methods *pdb = pdb_get_methods();
1190 NTSTATUS status;
1192 become_root();
1193 status = pdb->set_account_policy(pdb, type, value);
1194 unbecome_root();
1196 return NT_STATUS_IS_OK(status);
1199 bool pdb_get_seq_num(time_t *seq_num)
1201 struct pdb_methods *pdb = pdb_get_methods();
1202 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1205 bool pdb_uid_to_sid(uid_t uid, struct dom_sid *sid)
1207 struct pdb_methods *pdb = pdb_get_methods();
1208 return pdb->uid_to_sid(pdb, uid, sid);
1211 bool pdb_gid_to_sid(gid_t gid, struct dom_sid *sid)
1213 struct pdb_methods *pdb = pdb_get_methods();
1214 return pdb->gid_to_sid(pdb, gid, sid);
1217 bool pdb_sid_to_id(const struct dom_sid *sid, struct unixid *id)
1219 struct pdb_methods *pdb = pdb_get_methods();
1220 return pdb->sid_to_id(pdb, sid, id);
1223 uint32_t pdb_capabilities(void)
1225 struct pdb_methods *pdb = pdb_get_methods();
1226 return pdb->capabilities(pdb);
1229 /********************************************************************
1230 Allocate a new RID from the passdb backend. Verify that it is free
1231 by calling lookup_global_sam_rid() to verify that the RID is not
1232 in use. This handles servers that have existing users or groups
1233 with add RIDs (assigned from previous algorithmic mappings)
1234 ********************************************************************/
1236 bool pdb_new_rid(uint32_t *rid)
1238 struct pdb_methods *pdb = pdb_get_methods();
1239 const char *name = NULL;
1240 enum lsa_SidType type;
1241 uint32_t allocated_rid = 0;
1242 int i;
1243 TALLOC_CTX *ctx;
1245 if ((pdb_capabilities() & PDB_CAP_STORE_RIDS) == 0) {
1246 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1247 "are active\n"));
1248 return False;
1251 if (algorithmic_rid_base() != BASE_RID) {
1252 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1253 "without algorithmic RIDs is chosen.\n"));
1254 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1255 "add', set the maximum used RID\n"));
1256 DEBUGADD(0, ("and remove the parameter\n"));
1257 return False;
1260 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1261 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1262 return False;
1265 /* Attempt to get an unused RID (max tires is 250...yes that it is
1266 and arbitrary number I pulkled out of my head). -- jerry */
1268 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1269 /* get a new RID */
1271 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1272 return False;
1275 /* validate that the RID is not in use */
1277 if (lookup_global_sam_rid(ctx, allocated_rid, &name, &type, NULL, NULL)) {
1278 allocated_rid = 0;
1282 TALLOC_FREE( ctx );
1284 if ( allocated_rid == 0 ) {
1285 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1286 return False;
1289 *rid = allocated_rid;
1291 return True;
1294 /***************************************************************
1295 Initialize the static context (at smbd startup etc).
1297 If uninitialised, context will auto-init on first use.
1298 ***************************************************************/
1300 bool initialize_password_db(bool reload, struct tevent_context *tevent_ctx)
1302 pdb_tevent_ctx = tevent_ctx;
1303 return (pdb_get_methods_reload(reload) != NULL);
1307 /***************************************************************************
1308 Default implementations of some functions.
1309 ****************************************************************************/
1311 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1313 return NT_STATUS_NO_SUCH_USER;
1316 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1318 return NT_STATUS_NO_SUCH_USER;
1321 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1323 return NT_STATUS_NOT_IMPLEMENTED;
1326 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1328 return NT_STATUS_NOT_IMPLEMENTED;
1331 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1333 return NT_STATUS_NOT_IMPLEMENTED;
1336 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1338 return NT_STATUS_NOT_IMPLEMENTED;
1341 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1343 /* Only the pdb_nds backend implements this, by
1344 * default just return ok. */
1345 return NT_STATUS_OK;
1348 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t *value)
1350 return account_policy_get(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1353 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t value)
1355 return account_policy_set(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1358 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1360 *seq_num = time(NULL);
1361 return NT_STATUS_OK;
1364 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1365 struct dom_sid *sid)
1367 struct samu *sampw = NULL;
1368 struct passwd *unix_pw;
1369 bool ret;
1371 unix_pw = getpwuid( uid );
1373 if ( !unix_pw ) {
1374 DEBUG(4,("pdb_default_uid_to_sid: host has no idea of uid "
1375 "%lu\n", (unsigned long)uid));
1376 return False;
1379 if ( !(sampw = samu_new( NULL )) ) {
1380 DEBUG(0,("pdb_default_uid_to_sid: samu_new() failed!\n"));
1381 return False;
1384 become_root();
1385 ret = NT_STATUS_IS_OK(
1386 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1387 unbecome_root();
1389 if (!ret) {
1390 DEBUG(5, ("pdb_default_uid_to_sid: Did not find user "
1391 "%s (%u)\n", unix_pw->pw_name, (unsigned int)uid));
1392 TALLOC_FREE(sampw);
1393 return False;
1396 sid_copy(sid, pdb_get_user_sid(sampw));
1398 TALLOC_FREE(sampw);
1400 return True;
1403 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1404 struct dom_sid *sid)
1406 GROUP_MAP *map;
1408 map = talloc_zero(NULL, GROUP_MAP);
1409 if (!map) {
1410 return false;
1413 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, map, gid))) {
1414 TALLOC_FREE(map);
1415 return false;
1418 sid_copy(sid, &map->sid);
1419 TALLOC_FREE(map);
1420 return true;
1423 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1424 const struct dom_sid *sid,
1425 struct unixid *id)
1427 TALLOC_CTX *mem_ctx;
1428 bool ret = False;
1429 uint32_t rid;
1430 id->id = -1;
1432 mem_ctx = talloc_new(NULL);
1434 if (mem_ctx == NULL) {
1435 DEBUG(0, ("talloc_new failed\n"));
1436 return False;
1439 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1440 const char *name;
1441 enum lsa_SidType type;
1442 uid_t uid;
1443 gid_t gid;
1444 /* Here we might have users as well as groups and aliases */
1445 ret = lookup_global_sam_rid(mem_ctx, rid, &name, &type, &uid, &gid);
1446 if (ret) {
1447 switch (type) {
1448 case SID_NAME_DOM_GRP:
1449 case SID_NAME_ALIAS:
1450 id->type = ID_TYPE_GID;
1451 id->id = gid;
1452 break;
1453 case SID_NAME_USER:
1454 id->type = ID_TYPE_UID;
1455 id->id = uid;
1456 break;
1457 default:
1458 DEBUG(5, ("SID %s is our domain, but is not mapped to a user or group (got %d)\n",
1459 sid_string_dbg(sid), type));
1460 ret = false;
1462 } else {
1463 DEBUG(5, ("SID %s is or domain, but is unmapped\n",
1464 sid_string_dbg(sid)));
1466 goto done;
1469 /* check for "Unix User" */
1471 if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1472 id->id = rid;
1473 id->type = ID_TYPE_UID;
1474 ret = True;
1475 goto done;
1478 /* check for "Unix Group" */
1480 if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1481 id->id = rid;
1482 id->type = ID_TYPE_GID;
1483 ret = True;
1484 goto done;
1487 /* BUILTIN */
1489 if (sid_check_is_in_builtin(sid) ||
1490 sid_check_is_in_wellknown_domain(sid)) {
1491 /* Here we only have aliases */
1492 GROUP_MAP *map;
1494 map = talloc_zero(mem_ctx, GROUP_MAP);
1495 if (!map) {
1496 ret = false;
1497 goto done;
1500 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, map, *sid))) {
1501 DEBUG(10, ("Could not find map for sid %s\n",
1502 sid_string_dbg(sid)));
1503 goto done;
1505 if ((map->sid_name_use != SID_NAME_ALIAS) &&
1506 (map->sid_name_use != SID_NAME_WKN_GRP)) {
1507 DEBUG(10, ("Map for sid %s is a %s, expected an "
1508 "alias\n", sid_string_dbg(sid),
1509 sid_type_lookup(map->sid_name_use)));
1510 goto done;
1513 id->id = map->gid;
1514 id->type = ID_TYPE_GID;
1515 ret = True;
1516 goto done;
1519 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1520 sid_string_dbg(sid)));
1522 done:
1524 TALLOC_FREE(mem_ctx);
1525 return ret;
1528 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, uint32_t *p_num)
1530 struct group *grp;
1531 char **gr;
1532 struct passwd *pwd;
1533 bool winbind_env;
1534 bool ret = False;
1536 *pp_uids = NULL;
1537 *p_num = 0;
1539 /* We only look at our own sam, so don't care about imported stuff */
1540 winbind_env = winbind_env_set();
1541 (void)winbind_off();
1543 if ((grp = getgrgid(gid)) == NULL) {
1544 /* allow winbindd lookups, but only if they weren't already disabled */
1545 goto done;
1548 /* Primary group members */
1549 setpwent();
1550 while ((pwd = getpwent()) != NULL) {
1551 if (pwd->pw_gid == gid) {
1552 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1553 pp_uids, p_num)) {
1554 goto done;
1558 endpwent();
1560 /* Secondary group members */
1561 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1562 struct passwd *pw = getpwnam(*gr);
1564 if (pw == NULL)
1565 continue;
1566 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1567 goto done;
1571 ret = True;
1573 done:
1575 /* allow winbindd lookups, but only if they weren't already disabled */
1576 if (!winbind_env) {
1577 (void)winbind_on();
1580 return ret;
1583 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1584 TALLOC_CTX *mem_ctx,
1585 const struct dom_sid *group,
1586 uint32_t **pp_member_rids,
1587 size_t *p_num_members)
1589 gid_t gid;
1590 uid_t *uids;
1591 uint32_t i, num_uids;
1593 *pp_member_rids = NULL;
1594 *p_num_members = 0;
1596 if (!sid_to_gid(group, &gid))
1597 return NT_STATUS_NO_SUCH_GROUP;
1599 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1600 return NT_STATUS_NO_SUCH_GROUP;
1602 if (num_uids == 0)
1603 return NT_STATUS_OK;
1605 *pp_member_rids = talloc_zero_array(mem_ctx, uint32_t, num_uids);
1607 for (i=0; i<num_uids; i++) {
1608 struct dom_sid sid;
1610 uid_to_sid(&sid, uids[i]);
1612 if (!sid_check_is_in_our_sam(&sid)) {
1613 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1614 "in our domain\n"));
1615 continue;
1618 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1619 *p_num_members += 1;
1622 return NT_STATUS_OK;
1625 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1626 TALLOC_CTX *mem_ctx,
1627 struct samu *user,
1628 struct dom_sid **pp_sids,
1629 gid_t **pp_gids,
1630 uint32_t *p_num_groups)
1632 size_t i;
1633 gid_t gid;
1634 struct passwd *pw;
1635 const char *username = pdb_get_username(user);
1638 /* Ignore the primary group SID. Honor the real Unix primary group.
1639 The primary group SID is only of real use to Windows clients */
1641 if ( !(pw = Get_Pwnam_alloc(mem_ctx, username)) ) {
1642 return NT_STATUS_NO_SUCH_USER;
1645 gid = pw->pw_gid;
1647 TALLOC_FREE( pw );
1649 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1650 return NT_STATUS_NO_SUCH_USER;
1653 if (*p_num_groups == 0) {
1654 smb_panic("primary group missing");
1657 *pp_sids = talloc_array(mem_ctx, struct dom_sid, *p_num_groups);
1659 if (*pp_sids == NULL) {
1660 TALLOC_FREE(*pp_gids);
1661 return NT_STATUS_NO_MEMORY;
1664 for (i=0; i<*p_num_groups; i++) {
1665 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1668 return NT_STATUS_OK;
1671 /*******************************************************************
1672 Look up a rid in the SAM we're responsible for (i.e. passdb)
1673 ********************************************************************/
1675 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32_t rid,
1676 const char **name,
1677 enum lsa_SidType *psid_name_use,
1678 uid_t *uid, gid_t *gid)
1680 struct samu *sam_account = NULL;
1681 GROUP_MAP *map = NULL;
1682 bool ret;
1683 struct dom_sid sid;
1685 *psid_name_use = SID_NAME_UNKNOWN;
1687 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1688 (unsigned int)rid));
1690 sid_compose(&sid, get_global_sam_sid(), rid);
1692 /* see if the passdb can help us with the name of the user */
1694 if ( !(sam_account = samu_new( NULL )) ) {
1695 return False;
1698 map = talloc_zero(mem_ctx, GROUP_MAP);
1699 if (!map) {
1700 return false;
1703 /* BEING ROOT BLOCK */
1704 become_root();
1705 ret = pdb_getsampwsid(sam_account, &sid);
1706 if (!ret) {
1707 TALLOC_FREE(sam_account);
1708 ret = pdb_getgrsid(map, sid);
1710 unbecome_root();
1711 /* END BECOME_ROOT BLOCK */
1713 if (sam_account || !ret) {
1714 TALLOC_FREE(map);
1717 if (sam_account) {
1718 struct passwd *pw;
1720 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1721 if (!*name) {
1722 TALLOC_FREE(sam_account);
1723 return False;
1726 *psid_name_use = SID_NAME_USER;
1728 TALLOC_FREE(sam_account);
1730 if (uid == NULL) {
1731 return True;
1734 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1735 if (pw == NULL) {
1736 return False;
1738 *uid = pw->pw_uid;
1739 TALLOC_FREE(pw);
1740 return True;
1742 } else if (map && (map->gid != (gid_t)-1)) {
1744 /* do not resolve SIDs to a name unless there is a valid
1745 gid associated with it */
1747 *name = talloc_steal(mem_ctx, map->nt_name);
1748 *psid_name_use = map->sid_name_use;
1750 if (gid) {
1751 *gid = map->gid;
1754 TALLOC_FREE(map);
1755 return True;
1758 TALLOC_FREE(map);
1760 /* Windows will always map RID 513 to something. On a non-domain
1761 controller, this gets mapped to SERVER\None. */
1763 if (uid || gid) {
1764 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1765 return False;
1768 if ( rid == DOMAIN_RID_USERS ) {
1769 *name = talloc_strdup(mem_ctx, "None" );
1770 *psid_name_use = SID_NAME_DOM_GRP;
1772 return True;
1775 return False;
1778 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1779 const struct dom_sid *domain_sid,
1780 int num_rids,
1781 uint32_t *rids,
1782 const char **names,
1783 enum lsa_SidType *attrs)
1785 int i;
1786 NTSTATUS result;
1787 bool have_mapped = False;
1788 bool have_unmapped = False;
1790 if (sid_check_is_builtin(domain_sid)) {
1792 for (i=0; i<num_rids; i++) {
1793 const char *name;
1795 if (lookup_builtin_rid(names, rids[i], &name)) {
1796 attrs[i] = SID_NAME_ALIAS;
1797 names[i] = name;
1798 DEBUG(5,("lookup_rids: %s:%d\n",
1799 names[i], attrs[i]));
1800 have_mapped = True;
1801 } else {
1802 have_unmapped = True;
1803 attrs[i] = SID_NAME_UNKNOWN;
1806 goto done;
1809 /* Should not happen, but better check once too many */
1810 if (!sid_check_is_our_sam(domain_sid)) {
1811 return NT_STATUS_INVALID_HANDLE;
1814 for (i = 0; i < num_rids; i++) {
1815 const char *name;
1817 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1818 NULL, NULL)) {
1819 if (name == NULL) {
1820 return NT_STATUS_NO_MEMORY;
1822 names[i] = name;
1823 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1824 have_mapped = True;
1825 } else {
1826 have_unmapped = True;
1827 attrs[i] = SID_NAME_UNKNOWN;
1831 done:
1833 result = NT_STATUS_NONE_MAPPED;
1835 if (have_mapped)
1836 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1838 return result;
1841 static int pdb_search_destructor(struct pdb_search *search)
1843 if ((!search->search_ended) && (search->search_end != NULL)) {
1844 search->search_end(search);
1846 return 0;
1849 struct pdb_search *pdb_search_init(TALLOC_CTX *mem_ctx,
1850 enum pdb_search_type type)
1852 struct pdb_search *result;
1854 result = talloc(mem_ctx, struct pdb_search);
1855 if (result == NULL) {
1856 DEBUG(0, ("talloc failed\n"));
1857 return NULL;
1860 result->type = type;
1861 result->cache = NULL;
1862 result->num_entries = 0;
1863 result->cache_size = 0;
1864 result->search_ended = False;
1865 result->search_end = NULL;
1867 /* Segfault appropriately if not initialized */
1868 result->next_entry = NULL;
1869 result->search_end = NULL;
1871 talloc_set_destructor(result, pdb_search_destructor);
1873 return result;
1876 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32_t rid,
1877 uint16_t acct_flags,
1878 const char *account_name,
1879 const char *fullname,
1880 const char *description,
1881 struct samr_displayentry *entry)
1883 entry->rid = rid;
1884 entry->acct_flags = acct_flags;
1886 if (account_name != NULL)
1887 entry->account_name = talloc_strdup(mem_ctx, account_name);
1888 else
1889 entry->account_name = "";
1891 if (fullname != NULL)
1892 entry->fullname = talloc_strdup(mem_ctx, fullname);
1893 else
1894 entry->fullname = "";
1896 if (description != NULL)
1897 entry->description = talloc_strdup(mem_ctx, description);
1898 else
1899 entry->description = "";
1902 struct group_search {
1903 GROUP_MAP **groups;
1904 size_t num_groups, current_group;
1907 static bool next_entry_groups(struct pdb_search *s,
1908 struct samr_displayentry *entry)
1910 struct group_search *state = (struct group_search *)s->private_data;
1911 uint32_t rid;
1912 GROUP_MAP *map;
1914 if (state->current_group == state->num_groups)
1915 return False;
1917 map = state->groups[state->current_group];
1919 sid_peek_rid(&map->sid, &rid);
1921 fill_displayentry(s, rid, 0, map->nt_name, NULL, map->comment, entry);
1923 state->current_group += 1;
1924 return True;
1927 static void search_end_groups(struct pdb_search *search)
1929 struct group_search *state =
1930 (struct group_search *)search->private_data;
1931 TALLOC_FREE(state->groups);
1934 static bool pdb_search_grouptype(struct pdb_methods *methods,
1935 struct pdb_search *search,
1936 const struct dom_sid *sid, enum lsa_SidType type)
1938 struct group_search *state;
1940 state = talloc_zero(search, struct group_search);
1941 if (state == NULL) {
1942 DEBUG(0, ("talloc failed\n"));
1943 return False;
1946 if (!NT_STATUS_IS_OK(methods->enum_group_mapping(methods, sid, type,
1947 &state->groups, &state->num_groups,
1948 True))) {
1949 DEBUG(0, ("Could not enum groups\n"));
1950 return False;
1953 state->current_group = 0;
1954 search->private_data = state;
1955 search->next_entry = next_entry_groups;
1956 search->search_end = search_end_groups;
1957 return True;
1960 static bool pdb_default_search_groups(struct pdb_methods *methods,
1961 struct pdb_search *search)
1963 return pdb_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1966 static bool pdb_default_search_aliases(struct pdb_methods *methods,
1967 struct pdb_search *search,
1968 const struct dom_sid *sid)
1971 return pdb_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
1974 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1975 uint32_t idx)
1977 if (idx < search->num_entries)
1978 return &search->cache[idx];
1980 if (search->search_ended)
1981 return NULL;
1983 while (idx >= search->num_entries) {
1984 struct samr_displayentry entry;
1986 if (!search->next_entry(search, &entry)) {
1987 search->search_end(search);
1988 search->search_ended = True;
1989 break;
1992 ADD_TO_LARGE_ARRAY(search, struct samr_displayentry,
1993 entry, &search->cache, &search->num_entries,
1994 &search->cache_size);
1997 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
2000 struct pdb_search *pdb_search_users(TALLOC_CTX *mem_ctx, uint32_t acct_flags)
2002 struct pdb_methods *pdb = pdb_get_methods();
2003 struct pdb_search *result;
2005 result = pdb_search_init(mem_ctx, PDB_USER_SEARCH);
2006 if (result == NULL) {
2007 return NULL;
2010 if (!pdb->search_users(pdb, result, acct_flags)) {
2011 TALLOC_FREE(result);
2012 return NULL;
2014 return result;
2017 struct pdb_search *pdb_search_groups(TALLOC_CTX *mem_ctx)
2019 struct pdb_methods *pdb = pdb_get_methods();
2020 struct pdb_search *result;
2022 result = pdb_search_init(mem_ctx, PDB_GROUP_SEARCH);
2023 if (result == NULL) {
2024 return NULL;
2027 if (!pdb->search_groups(pdb, result)) {
2028 TALLOC_FREE(result);
2029 return NULL;
2031 return result;
2034 struct pdb_search *pdb_search_aliases(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
2036 struct pdb_methods *pdb = pdb_get_methods();
2037 struct pdb_search *result;
2039 if (pdb == NULL) return NULL;
2041 result = pdb_search_init(mem_ctx, PDB_ALIAS_SEARCH);
2042 if (result == NULL) {
2043 return NULL;
2046 if (!pdb->search_aliases(pdb, result, sid)) {
2047 TALLOC_FREE(result);
2048 return NULL;
2050 return result;
2053 uint32_t pdb_search_entries(struct pdb_search *search,
2054 uint32_t start_idx, uint32_t max_entries,
2055 struct samr_displayentry **result)
2057 struct samr_displayentry *end_entry;
2058 uint32_t end_idx = start_idx+max_entries-1;
2060 /* The first entry needs to be searched after the last. Otherwise the
2061 * first entry might have moved due to a realloc during the search for
2062 * the last entry. */
2064 end_entry = pdb_search_getentry(search, end_idx);
2065 *result = pdb_search_getentry(search, start_idx);
2067 if (end_entry != NULL)
2068 return max_entries;
2070 if (start_idx >= search->num_entries)
2071 return 0;
2073 return search->num_entries - start_idx;
2076 /*******************************************************************
2077 trustdom methods
2078 *******************************************************************/
2080 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, struct dom_sid *sid,
2081 time_t *pass_last_set_time)
2083 struct pdb_methods *pdb = pdb_get_methods();
2084 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2085 pass_last_set_time);
2088 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2089 const struct dom_sid *sid)
2091 struct pdb_methods *pdb = pdb_get_methods();
2092 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2095 bool pdb_del_trusteddom_pw(const char *domain)
2097 struct pdb_methods *pdb = pdb_get_methods();
2098 return pdb->del_trusteddom_pw(pdb, domain);
2101 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2102 struct trustdom_info ***domains)
2104 struct pdb_methods *pdb = pdb_get_methods();
2105 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2108 /*******************************************************************
2109 the defaults for trustdom methods:
2110 these simply call the original passdb/secrets.c actions,
2111 to be replaced by pdb_ldap.
2112 *******************************************************************/
2114 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2115 const char *domain,
2116 char** pwd,
2117 struct dom_sid *sid,
2118 time_t *pass_last_set_time)
2120 return secrets_fetch_trusted_domain_password(domain, pwd,
2121 sid, pass_last_set_time);
2125 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2126 const char* domain,
2127 const char* pwd,
2128 const struct dom_sid *sid)
2130 return secrets_store_trusted_domain_password(domain, pwd, sid);
2133 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2134 const char *domain)
2136 return trusted_domain_password_delete(domain);
2139 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2140 TALLOC_CTX *mem_ctx,
2141 uint32_t *num_domains,
2142 struct trustdom_info ***domains)
2144 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2147 /*******************************************************************
2148 trusted_domain methods
2149 *******************************************************************/
2151 NTSTATUS pdb_get_trusted_domain(TALLOC_CTX *mem_ctx, const char *domain,
2152 struct pdb_trusted_domain **td)
2154 struct pdb_methods *pdb = pdb_get_methods();
2155 return pdb->get_trusted_domain(pdb, mem_ctx, domain, td);
2158 NTSTATUS pdb_get_trusted_domain_by_sid(TALLOC_CTX *mem_ctx, struct dom_sid *sid,
2159 struct pdb_trusted_domain **td)
2161 struct pdb_methods *pdb = pdb_get_methods();
2162 return pdb->get_trusted_domain_by_sid(pdb, mem_ctx, sid, td);
2165 NTSTATUS pdb_set_trusted_domain(const char* domain,
2166 const struct pdb_trusted_domain *td)
2168 struct pdb_methods *pdb = pdb_get_methods();
2169 return pdb->set_trusted_domain(pdb, domain, td);
2172 NTSTATUS pdb_del_trusted_domain(const char *domain)
2174 struct pdb_methods *pdb = pdb_get_methods();
2175 return pdb->del_trusted_domain(pdb, domain);
2178 NTSTATUS pdb_enum_trusted_domains(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2179 struct pdb_trusted_domain ***domains)
2181 struct pdb_methods *pdb = pdb_get_methods();
2182 return pdb->enum_trusted_domains(pdb, mem_ctx, num_domains, domains);
2185 static NTSTATUS pdb_default_get_trusted_domain(struct pdb_methods *methods,
2186 TALLOC_CTX *mem_ctx,
2187 const char *domain,
2188 struct pdb_trusted_domain **td)
2190 struct trustAuthInOutBlob taiob;
2191 struct AuthenticationInformation aia;
2192 struct pdb_trusted_domain *tdom;
2193 enum ndr_err_code ndr_err;
2194 time_t last_set_time;
2195 char *pwd;
2196 bool ok;
2198 tdom = talloc(mem_ctx, struct pdb_trusted_domain);
2199 if (!tdom) {
2200 return NT_STATUS_NO_MEMORY;
2203 tdom->domain_name = talloc_strdup(tdom, domain);
2204 tdom->netbios_name = talloc_strdup(tdom, domain);
2205 if (!tdom->domain_name || !tdom->netbios_name) {
2206 talloc_free(tdom);
2207 return NT_STATUS_NO_MEMORY;
2210 tdom->trust_auth_incoming = data_blob_null;
2212 ok = pdb_get_trusteddom_pw(domain, &pwd, &tdom->security_identifier,
2213 &last_set_time);
2214 if (!ok) {
2215 talloc_free(tdom);
2216 return NT_STATUS_UNSUCCESSFUL;
2219 ZERO_STRUCT(taiob);
2220 ZERO_STRUCT(aia);
2221 taiob.count = 1;
2222 taiob.current.count = 1;
2223 taiob.current.array = &aia;
2224 unix_to_nt_time(&aia.LastUpdateTime, last_set_time);
2225 aia.AuthType = TRUST_AUTH_TYPE_CLEAR;
2226 aia.AuthInfo.clear.password = (uint8_t *) pwd;
2227 aia.AuthInfo.clear.size = strlen(pwd);
2228 taiob.previous.count = 0;
2229 taiob.previous.array = NULL;
2231 ndr_err = ndr_push_struct_blob(&tdom->trust_auth_outgoing,
2232 tdom, &taiob,
2233 (ndr_push_flags_fn_t)ndr_push_trustAuthInOutBlob);
2234 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2235 talloc_free(tdom);
2236 return NT_STATUS_UNSUCCESSFUL;
2239 tdom->trust_direction = LSA_TRUST_DIRECTION_OUTBOUND;
2240 tdom->trust_type = LSA_TRUST_TYPE_DOWNLEVEL;
2241 tdom->trust_attributes = 0;
2242 tdom->trust_forest_trust_info = data_blob_null;
2244 *td = tdom;
2245 return NT_STATUS_OK;
2248 static NTSTATUS pdb_default_get_trusted_domain_by_sid(struct pdb_methods *methods,
2249 TALLOC_CTX *mem_ctx,
2250 struct dom_sid *sid,
2251 struct pdb_trusted_domain **td)
2253 return NT_STATUS_NOT_IMPLEMENTED;
2256 #define IS_NULL_DATA_BLOB(d) ((d).data == NULL && (d).length == 0)
2258 static NTSTATUS pdb_default_set_trusted_domain(struct pdb_methods *methods,
2259 const char* domain,
2260 const struct pdb_trusted_domain *td)
2262 struct trustAuthInOutBlob taiob;
2263 struct AuthenticationInformation *aia;
2264 enum ndr_err_code ndr_err;
2265 char *pwd;
2266 bool ok;
2268 if (td->trust_attributes != 0 ||
2269 td->trust_type != LSA_TRUST_TYPE_DOWNLEVEL ||
2270 td->trust_direction != LSA_TRUST_DIRECTION_OUTBOUND ||
2271 !IS_NULL_DATA_BLOB(td->trust_auth_incoming) ||
2272 !IS_NULL_DATA_BLOB(td->trust_forest_trust_info)) {
2273 return NT_STATUS_NOT_IMPLEMENTED;
2276 ZERO_STRUCT(taiob);
2277 ndr_err = ndr_pull_struct_blob(&td->trust_auth_outgoing, talloc_tos(),
2278 &taiob,
2279 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2280 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2281 return NT_STATUS_UNSUCCESSFUL;
2284 aia = (struct AuthenticationInformation *) taiob.current.array;
2286 if (taiob.count != 1 || taiob.current.count != 1 ||
2287 taiob.previous.count != 0 ||
2288 aia->AuthType != TRUST_AUTH_TYPE_CLEAR) {
2289 return NT_STATUS_NOT_IMPLEMENTED;
2292 pwd = talloc_strndup(talloc_tos(), (char *) aia->AuthInfo.clear.password,
2293 aia->AuthInfo.clear.size);
2294 if (!pwd) {
2295 return NT_STATUS_NO_MEMORY;
2298 ok = pdb_set_trusteddom_pw(domain, pwd, &td->security_identifier);
2299 if (!ok) {
2300 return NT_STATUS_UNSUCCESSFUL;
2303 return NT_STATUS_OK;
2306 static NTSTATUS pdb_default_del_trusted_domain(struct pdb_methods *methods,
2307 const char *domain)
2309 return NT_STATUS_NOT_IMPLEMENTED;
2312 static NTSTATUS pdb_default_enum_trusted_domains(struct pdb_methods *methods,
2313 TALLOC_CTX *mem_ctx,
2314 uint32_t *num_domains,
2315 struct pdb_trusted_domain ***domains)
2317 return NT_STATUS_NOT_IMPLEMENTED;
2320 static struct pdb_domain_info *pdb_default_get_domain_info(
2321 struct pdb_methods *m, TALLOC_CTX *mem_ctx)
2323 return NULL;
2326 /*******************************************************************
2327 secret methods
2328 *******************************************************************/
2330 NTSTATUS pdb_get_secret(TALLOC_CTX *mem_ctx,
2331 const char *secret_name,
2332 DATA_BLOB *secret_current,
2333 NTTIME *secret_current_lastchange,
2334 DATA_BLOB *secret_old,
2335 NTTIME *secret_old_lastchange,
2336 struct security_descriptor **sd)
2338 struct pdb_methods *pdb = pdb_get_methods();
2339 return pdb->get_secret(pdb, mem_ctx, secret_name,
2340 secret_current, secret_current_lastchange,
2341 secret_old, secret_old_lastchange,
2342 sd);
2345 NTSTATUS pdb_set_secret(const char *secret_name,
2346 DATA_BLOB *secret_current,
2347 DATA_BLOB *secret_old,
2348 struct security_descriptor *sd)
2350 struct pdb_methods *pdb = pdb_get_methods();
2351 return pdb->set_secret(pdb, secret_name,
2352 secret_current,
2353 secret_old,
2354 sd);
2357 NTSTATUS pdb_delete_secret(const char *secret_name)
2359 struct pdb_methods *pdb = pdb_get_methods();
2360 return pdb->delete_secret(pdb, secret_name);
2363 static NTSTATUS pdb_default_get_secret(struct pdb_methods *methods,
2364 TALLOC_CTX *mem_ctx,
2365 const char *secret_name,
2366 DATA_BLOB *secret_current,
2367 NTTIME *secret_current_lastchange,
2368 DATA_BLOB *secret_old,
2369 NTTIME *secret_old_lastchange,
2370 struct security_descriptor **sd)
2372 return lsa_secret_get(mem_ctx, secret_name,
2373 secret_current,
2374 secret_current_lastchange,
2375 secret_old,
2376 secret_old_lastchange,
2377 sd);
2380 static NTSTATUS pdb_default_set_secret(struct pdb_methods *methods,
2381 const char *secret_name,
2382 DATA_BLOB *secret_current,
2383 DATA_BLOB *secret_old,
2384 struct security_descriptor *sd)
2386 return lsa_secret_set(secret_name,
2387 secret_current,
2388 secret_old,
2389 sd);
2392 static NTSTATUS pdb_default_delete_secret(struct pdb_methods *methods,
2393 const char *secret_name)
2395 return lsa_secret_delete(secret_name);
2398 /*******************************************************************
2399 Create a pdb_methods structure and initialize it with the default
2400 operations. In this way a passdb module can simply implement
2401 the functionality it cares about. However, normally this is done
2402 in groups of related functions.
2403 *******************************************************************/
2405 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2407 /* allocate memory for the structure as its own talloc CTX */
2409 *methods = talloc_zero(NULL, struct pdb_methods);
2410 if (*methods == NULL) {
2411 return NT_STATUS_NO_MEMORY;
2414 (*methods)->get_domain_info = pdb_default_get_domain_info;
2415 (*methods)->getsampwnam = pdb_default_getsampwnam;
2416 (*methods)->getsampwsid = pdb_default_getsampwsid;
2417 (*methods)->create_user = pdb_default_create_user;
2418 (*methods)->delete_user = pdb_default_delete_user;
2419 (*methods)->add_sam_account = pdb_default_add_sam_account;
2420 (*methods)->update_sam_account = pdb_default_update_sam_account;
2421 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2422 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2423 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2425 (*methods)->getgrsid = pdb_default_getgrsid;
2426 (*methods)->getgrgid = pdb_default_getgrgid;
2427 (*methods)->getgrnam = pdb_default_getgrnam;
2428 (*methods)->create_dom_group = pdb_default_create_dom_group;
2429 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2430 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2431 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2432 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2433 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2434 (*methods)->enum_group_members = pdb_default_enum_group_members;
2435 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2436 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2437 (*methods)->add_groupmem = pdb_default_add_groupmem;
2438 (*methods)->del_groupmem = pdb_default_del_groupmem;
2439 (*methods)->create_alias = pdb_default_create_alias;
2440 (*methods)->delete_alias = pdb_default_delete_alias;
2441 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2442 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2443 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2444 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2445 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2446 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2447 (*methods)->lookup_rids = pdb_default_lookup_rids;
2448 (*methods)->get_account_policy = pdb_default_get_account_policy;
2449 (*methods)->set_account_policy = pdb_default_set_account_policy;
2450 (*methods)->get_seq_num = pdb_default_get_seq_num;
2451 (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2452 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2453 (*methods)->sid_to_id = pdb_default_sid_to_id;
2455 (*methods)->search_groups = pdb_default_search_groups;
2456 (*methods)->search_aliases = pdb_default_search_aliases;
2458 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2459 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2460 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2461 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2463 (*methods)->get_trusted_domain = pdb_default_get_trusted_domain;
2464 (*methods)->get_trusted_domain_by_sid = pdb_default_get_trusted_domain_by_sid;
2465 (*methods)->set_trusted_domain = pdb_default_set_trusted_domain;
2466 (*methods)->del_trusted_domain = pdb_default_del_trusted_domain;
2467 (*methods)->enum_trusted_domains = pdb_default_enum_trusted_domains;
2469 (*methods)->get_secret = pdb_default_get_secret;
2470 (*methods)->set_secret = pdb_default_set_secret;
2471 (*methods)->delete_secret = pdb_default_delete_secret;
2473 return NT_STATUS_OK;