librpc: Remove unused RPC_NDR_AUDIOSRV
[Samba.git] / source3 / passdb / pdb_interface.c
blobd42d5e52932050b4e63364f6d26a53b743caa57c
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 "serverid.h"
29 #include "../librpc/gen_ndr/samr.h"
30 #include "../librpc/gen_ndr/drsblobs.h"
31 #include "../librpc/gen_ndr/ndr_drsblobs.h"
32 #include "../librpc/gen_ndr/idmap.h"
33 #include "../lib/util/memcache.h"
34 #include "nsswitch/winbind_client.h"
35 #include "../libcli/security/security.h"
36 #include "../lib/util/util_pw.h"
37 #include "passdb/pdb_secrets.h"
38 #include "lib/util_sid_passdb.h"
39 #include "idmap_cache.h"
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_PASSDB
44 static_decl_pdb;
46 static struct pdb_init_function_entry *backends = NULL;
48 static void lazy_initialize_passdb(void)
50 static bool initialized = False;
51 if(initialized) {
52 return;
54 static_init_pdb(NULL);
55 initialized = True;
58 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32_t rid,
59 const char **name,
60 enum lsa_SidType *psid_name_use,
61 uid_t *uid, gid_t *gid);
63 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
65 struct pdb_init_function_entry *entry = backends;
67 if(version != PASSDB_INTERFACE_VERSION) {
68 DEBUG(0,("Can't register passdb backend!\n"
69 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
70 "while this version of samba uses version %d\n",
71 version,PASSDB_INTERFACE_VERSION));
72 return NT_STATUS_OBJECT_TYPE_MISMATCH;
75 if (!name || !init) {
76 return NT_STATUS_INVALID_PARAMETER;
79 DEBUG(5,("Attempting to register passdb backend %s\n", name));
81 /* Check for duplicates */
82 if (pdb_find_backend_entry(name)) {
83 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
84 return NT_STATUS_OBJECT_NAME_COLLISION;
87 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
88 entry->name = smb_xstrdup(name);
89 entry->init = init;
91 DLIST_ADD(backends, entry);
92 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
93 return NT_STATUS_OK;
96 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
98 struct pdb_init_function_entry *entry = backends;
100 while(entry) {
101 if (strcmp(entry->name, name)==0) return entry;
102 entry = entry->next;
105 return NULL;
108 const struct pdb_init_function_entry *pdb_get_backends(void)
110 return backends;
115 * The event context for the passdb backend. I know this is a bad hack and yet
116 * another static variable, but our pdb API is a global thing per
117 * definition. The first use for this is the LDAP idle function, more might be
118 * added later.
120 * I don't feel too bad about this static variable, it replaces the
121 * smb_idle_event_list that used to exist in lib/module.c. -- VL
124 static struct tevent_context *pdb_tevent_ctx;
126 struct tevent_context *pdb_get_tevent_context(void)
128 return pdb_tevent_ctx;
131 /******************************************************************
132 Make a pdb_methods from scratch
133 *******************************************************************/
135 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
137 char *module_name = smb_xstrdup(selected);
138 char *module_location = NULL, *p;
139 struct pdb_init_function_entry *entry;
140 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
142 lazy_initialize_passdb();
144 p = strchr(module_name, ':');
146 if (p) {
147 *p = 0;
148 module_location = p+1;
149 trim_char(module_location, ' ', ' ');
152 trim_char(module_name, ' ', ' ');
155 DEBUG(5,("Attempting to find a passdb backend to match %s (%s)\n", selected, module_name));
157 entry = pdb_find_backend_entry(module_name);
159 /* Try to find a module that contains this module */
160 if (!entry) {
161 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
162 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
163 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
164 SAFE_FREE(module_name);
165 return NT_STATUS_UNSUCCESSFUL;
169 /* No such backend found */
170 if(!entry) {
171 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
172 SAFE_FREE(module_name);
173 return NT_STATUS_INVALID_PARAMETER;
176 DEBUG(5,("Found pdb backend %s\n", module_name));
178 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
179 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
180 selected, nt_errstr(nt_status)));
181 SAFE_FREE(module_name);
182 return nt_status;
185 SAFE_FREE(module_name);
187 DEBUG(5,("pdb backend %s has a valid init\n", selected));
189 return nt_status;
192 /******************************************************************
193 Return an already initialized pdb_methods structure
194 *******************************************************************/
196 static struct pdb_methods *pdb_get_methods_reload( bool reload )
198 static struct pdb_methods *pdb = NULL;
200 if ( pdb && reload ) {
201 if (pdb->free_private_data != NULL) {
202 pdb->free_private_data( &(pdb->private_data) );
204 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
205 return NULL;
209 if ( !pdb ) {
210 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
211 return NULL;
215 return pdb;
218 static struct pdb_methods *pdb_get_methods(void)
220 struct pdb_methods *pdb;
222 pdb = pdb_get_methods_reload(false);
223 if (!pdb) {
224 char *msg = NULL;
225 if (asprintf(&msg, "pdb_get_methods: "
226 "failed to get pdb methods for backend %s\n",
227 lp_passdb_backend()) > 0) {
228 smb_panic(msg);
229 } else {
230 smb_panic("pdb_get_methods");
234 return pdb;
237 struct pdb_domain_info *pdb_get_domain_info(TALLOC_CTX *mem_ctx)
239 struct pdb_methods *pdb = pdb_get_methods();
240 return pdb->get_domain_info(pdb, mem_ctx);
244 * @brief Check if the user account has been locked out and try to unlock it.
246 * If the user has been automatically locked out and a lockout duration is set,
247 * then check if we can unlock the account and reset the bad password values.
249 * @param[in] sampass The sam user to check.
251 * @return True if the function was successfull, false on an error.
253 static bool pdb_try_account_unlock(struct samu *sampass)
255 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
257 if ((acb_info & ACB_NORMAL) && (acb_info & ACB_AUTOLOCK)) {
258 uint32_t lockout_duration;
259 time_t bad_password_time;
260 time_t now = time(NULL);
261 bool ok;
263 ok = pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION,
264 &lockout_duration);
265 if (!ok) {
266 DEBUG(0, ("pdb_try_account_unlock: "
267 "pdb_get_account_policy failed.\n"));
268 return false;
271 if (lockout_duration == (uint32_t) -1 ||
272 lockout_duration == 0) {
273 DEBUG(9, ("pdb_try_account_unlock: No reset duration, "
274 "can't reset autolock\n"));
275 return false;
277 lockout_duration *= 60;
279 bad_password_time = pdb_get_bad_password_time(sampass);
280 if (bad_password_time == (time_t) 0) {
281 DEBUG(2, ("pdb_try_account_unlock: Account %s "
282 "administratively locked out "
283 "with no bad password "
284 "time. Leaving locked out.\n",
285 pdb_get_username(sampass)));
286 return true;
289 if ((bad_password_time +
290 convert_uint32_t_to_time_t(lockout_duration)) < now) {
291 NTSTATUS status;
293 pdb_set_acct_ctrl(sampass, acb_info & ~ACB_AUTOLOCK,
294 PDB_CHANGED);
295 pdb_set_bad_password_count(sampass, 0, PDB_CHANGED);
296 pdb_set_bad_password_time(sampass, 0, PDB_CHANGED);
298 become_root();
299 status = pdb_update_sam_account(sampass);
300 unbecome_root();
301 if (!NT_STATUS_IS_OK(status)) {
302 DEBUG(0, ("_samr_OpenUser: Couldn't "
303 "update account %s - %s\n",
304 pdb_get_username(sampass),
305 nt_errstr(status)));
306 return false;
311 return true;
315 * @brief Get a sam user structure by the given username.
317 * This functions also checks if the account has been automatically locked out
318 * and unlocks it if a lockout duration time has been defined and the time has
319 * elapsed.
321 * @param[in] sam_acct The sam user structure to fill.
323 * @param[in] username The username to look for.
325 * @return True on success, false on error.
327 bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
329 struct pdb_methods *pdb = pdb_get_methods();
330 struct samu *for_cache;
331 const struct dom_sid *user_sid;
332 NTSTATUS status;
333 bool ok;
335 status = pdb->getsampwnam(pdb, sam_acct, username);
336 if (!NT_STATUS_IS_OK(status)) {
337 return false;
340 ok = pdb_try_account_unlock(sam_acct);
341 if (!ok) {
342 DEBUG(1, ("pdb_getsampwnam: Failed to unlock account %s\n",
343 username));
346 for_cache = samu_new(NULL);
347 if (for_cache == NULL) {
348 return False;
351 if (!pdb_copy_sam_account(for_cache, sam_acct)) {
352 TALLOC_FREE(for_cache);
353 return False;
356 user_sid = pdb_get_user_sid(for_cache);
358 memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
359 data_blob_const(user_sid, sizeof(*user_sid)),
360 &for_cache);
362 return True;
365 /**********************************************************************
366 **********************************************************************/
368 static bool guest_user_info( struct samu *user )
370 struct passwd *pwd;
371 NTSTATUS result;
372 const char *guestname = lp_guest_account();
374 pwd = Get_Pwnam_alloc(talloc_tos(), guestname);
375 if (pwd == NULL) {
376 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
377 guestname));
378 return False;
381 result = samu_set_unix(user, pwd );
383 TALLOC_FREE( pwd );
385 return NT_STATUS_IS_OK( result );
389 * @brief Get a sam user structure by the given username.
391 * This functions also checks if the account has been automatically locked out
392 * and unlocks it if a lockout duration time has been defined and the time has
393 * elapsed.
396 * @param[in] sam_acct The sam user structure to fill.
398 * @param[in] sid The user SDI to look up.
400 * @return True on success, false on error.
402 bool pdb_getsampwsid(struct samu *sam_acct, const struct dom_sid *sid)
404 struct pdb_methods *pdb = pdb_get_methods();
405 uint32_t rid;
406 void *cache_data;
407 bool ok = false;
409 /* hard code the Guest RID of 501 */
411 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
412 return False;
414 if ( rid == DOMAIN_RID_GUEST ) {
415 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
416 return guest_user_info( sam_acct );
419 /* check the cache first */
421 cache_data = memcache_lookup_talloc(
422 NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
424 if (cache_data != NULL) {
425 struct samu *cache_copy = talloc_get_type_abort(
426 cache_data, struct samu);
428 ok = pdb_copy_sam_account(sam_acct, cache_copy);
429 } else {
430 ok = NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
433 if (!ok) {
434 return false;
437 ok = pdb_try_account_unlock(sam_acct);
438 if (!ok) {
439 DEBUG(1, ("pdb_getsampwsid: Failed to unlock account %s\n",
440 sam_acct->username));
443 return true;
446 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
447 TALLOC_CTX *tmp_ctx, const char *name,
448 uint32_t acb_info, uint32_t *rid)
450 struct samu *sam_pass;
451 NTSTATUS status;
452 struct passwd *pwd;
454 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
455 return NT_STATUS_NO_MEMORY;
458 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
459 char *add_script = NULL;
460 int add_ret;
461 fstring name2;
463 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
464 add_script = lp_add_user_script(tmp_ctx);
465 } else {
466 add_script = lp_add_machine_script(tmp_ctx);
469 if (!add_script || add_script[0] == '\0') {
470 DEBUG(3, ("Could not find user %s and no add script "
471 "defined\n", name));
472 return NT_STATUS_NO_SUCH_USER;
475 /* lowercase the username before creating the Unix account for
476 compatibility with previous Samba releases */
477 fstrcpy( name2, name );
478 if (!strlower_m( name2 )) {
479 return NT_STATUS_INVALID_PARAMETER;
481 add_script = talloc_all_string_sub(tmp_ctx,
482 add_script,
483 "%u",
484 name2);
485 if (!add_script) {
486 return NT_STATUS_NO_MEMORY;
488 add_ret = smbrun(add_script, NULL, NULL);
489 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
490 add_script, add_ret));
491 if (add_ret == 0) {
492 smb_nscd_flush_user_cache();
495 flush_pwnam_cache();
497 pwd = Get_Pwnam_alloc(tmp_ctx, name);
499 if(pwd == NULL) {
500 DEBUG(3, ("Could not find user %s, add script did not work\n", name));
501 return NT_STATUS_NO_SUCH_USER;
505 /* we have a valid SID coming out of this call */
507 status = samu_alloc_rid_unix(methods, sam_pass, pwd);
509 TALLOC_FREE( pwd );
511 if (!NT_STATUS_IS_OK(status)) {
512 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
513 return status;
516 if (!sid_peek_check_rid(get_global_sam_sid(),
517 pdb_get_user_sid(sam_pass), rid)) {
518 DEBUG(0, ("Could not get RID of fresh user\n"));
519 return NT_STATUS_INTERNAL_ERROR;
522 /* Use the username case specified in the original request */
524 pdb_set_username( sam_pass, name, PDB_SET );
526 /* Disable the account on creation, it does not have a reasonable password yet. */
528 acb_info |= ACB_DISABLED;
530 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
532 status = methods->add_sam_account(methods, sam_pass);
534 TALLOC_FREE(sam_pass);
536 return status;
539 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32_t flags,
540 uint32_t *rid)
542 struct pdb_methods *pdb = pdb_get_methods();
543 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
546 /****************************************************************************
547 Delete a UNIX user on demand.
548 ****************************************************************************/
550 static int smb_delete_user(const char *unix_user)
552 char *del_script = NULL;
553 int ret;
555 /* safety check */
557 if ( strequal( unix_user, "root" ) ) {
558 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
559 return -1;
562 del_script = lp_delete_user_script(talloc_tos());
563 if (!del_script || !*del_script) {
564 return -1;
566 del_script = talloc_all_string_sub(talloc_tos(),
567 del_script,
568 "%u",
569 unix_user);
570 if (!del_script) {
571 return -1;
573 ret = smbrun(del_script, NULL, NULL);
574 flush_pwnam_cache();
575 if (ret == 0) {
576 smb_nscd_flush_user_cache();
578 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
580 return ret;
583 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
584 TALLOC_CTX *mem_ctx,
585 struct samu *sam_acct)
587 NTSTATUS status;
588 fstring username;
590 status = methods->delete_sam_account(methods, sam_acct);
591 if (!NT_STATUS_IS_OK(status)) {
592 return status;
596 * Now delete the unix side ....
597 * note: we don't check if the delete really happened as the script is
598 * not necessary present and maybe the sysadmin doesn't want to delete
599 * the unix side
602 /* always lower case the username before handing it off to
603 external scripts */
605 fstrcpy( username, pdb_get_username(sam_acct) );
606 if (!strlower_m( username )) {
607 return status;
610 smb_delete_user( username );
612 return status;
615 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
617 struct pdb_methods *pdb = pdb_get_methods();
618 uid_t uid = -1;
619 NTSTATUS status;
620 const struct dom_sid *user_sid;
621 char *msg_data;
623 user_sid = pdb_get_user_sid(sam_acct);
625 /* sanity check to make sure we don't delete root */
627 if ( !sid_to_uid(user_sid, &uid ) ) {
628 return NT_STATUS_NO_SUCH_USER;
631 if ( uid == 0 ) {
632 return NT_STATUS_ACCESS_DENIED;
635 memcache_delete(NULL,
636 PDB_GETPWSID_CACHE,
637 data_blob_const(user_sid, sizeof(*user_sid)));
639 status = pdb->delete_user(pdb, mem_ctx, sam_acct);
640 if (!NT_STATUS_IS_OK(status)) {
641 return status;
644 msg_data = talloc_asprintf(mem_ctx, "USER %s",
645 pdb_get_username(sam_acct));
646 if (!msg_data) {
647 /* not fatal, and too late to rollback,
648 * just return */
649 return status;
651 messaging_send_all(global_messaging_context(),
652 ID_CACHE_DELETE,
653 msg_data,
654 strlen(msg_data) + 1);
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 struct dom_sid_buf 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(
773 grp->gr_gid,
774 dom_sid_str_buf(&group_sid, &tmp),
775 SID_NAME_DOM_GRP,
776 name,
777 NULL);
780 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
781 uint32_t *rid)
783 struct pdb_methods *pdb = pdb_get_methods();
784 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
787 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
788 TALLOC_CTX *mem_ctx,
789 uint32_t rid)
791 struct dom_sid group_sid;
792 GROUP_MAP *map;
793 NTSTATUS status;
794 struct group *grp;
795 const char *grp_name;
797 map = talloc_zero(mem_ctx, GROUP_MAP);
798 if (!map) {
799 return NT_STATUS_NO_MEMORY;
802 /* coverity */
803 map->gid = (gid_t) -1;
805 sid_compose(&group_sid, get_global_sam_sid(), rid);
807 if (!get_domain_group_from_sid(group_sid, map)) {
808 DEBUG(10, ("Could not find group for rid %d\n", rid));
809 return NT_STATUS_NO_SUCH_GROUP;
812 /* We need the group name for the smb_delete_group later on */
814 if (map->gid == (gid_t)-1) {
815 return NT_STATUS_NO_SUCH_GROUP;
818 grp = getgrgid(map->gid);
819 if (grp == NULL) {
820 return NT_STATUS_NO_SUCH_GROUP;
823 TALLOC_FREE(map);
825 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
827 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
828 if (grp_name == NULL) {
829 return NT_STATUS_NO_MEMORY;
832 status = pdb_delete_group_mapping_entry(group_sid);
834 if (!NT_STATUS_IS_OK(status)) {
835 return status;
838 /* Don't check the result of smb_delete_group */
840 smb_delete_group(grp_name);
842 return NT_STATUS_OK;
845 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32_t rid)
847 struct pdb_methods *pdb = pdb_get_methods();
848 return pdb->delete_dom_group(pdb, mem_ctx, rid);
851 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
853 struct pdb_methods *pdb = pdb_get_methods();
854 return pdb->add_group_mapping_entry(pdb, map);
857 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
859 struct pdb_methods *pdb = pdb_get_methods();
860 return pdb->update_group_mapping_entry(pdb, map);
863 NTSTATUS pdb_delete_group_mapping_entry(struct dom_sid sid)
865 struct pdb_methods *pdb = pdb_get_methods();
866 return pdb->delete_group_mapping_entry(pdb, sid);
869 bool pdb_enum_group_mapping(const struct dom_sid *sid,
870 enum lsa_SidType sid_name_use,
871 GROUP_MAP ***pp_rmap,
872 size_t *p_num_entries,
873 bool unix_only)
875 struct pdb_methods *pdb = pdb_get_methods();
876 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
877 pp_rmap, p_num_entries, unix_only));
880 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
881 const struct dom_sid *sid,
882 uint32_t **pp_member_rids,
883 size_t *p_num_members)
885 struct pdb_methods *pdb = pdb_get_methods();
886 NTSTATUS result;
888 result = pdb->enum_group_members(pdb, mem_ctx,
889 sid, pp_member_rids, p_num_members);
891 /* special check for rid 513 */
893 if ( !NT_STATUS_IS_OK( result ) ) {
894 uint32_t rid;
896 sid_peek_rid( sid, &rid );
898 if ( rid == DOMAIN_RID_USERS ) {
899 *p_num_members = 0;
900 *pp_member_rids = NULL;
902 return NT_STATUS_OK;
906 return result;
909 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
910 struct dom_sid **pp_sids, gid_t **pp_gids,
911 uint32_t *p_num_groups)
913 struct pdb_methods *pdb = pdb_get_methods();
914 return pdb->enum_group_memberships(
915 pdb, mem_ctx, user,
916 pp_sids, pp_gids, p_num_groups);
919 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
920 TALLOC_CTX *mem_ctx,
921 struct samu *sampass)
923 struct group *grp;
924 gid_t gid;
926 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
927 (grp = getgrgid(gid)) == NULL) {
928 return NT_STATUS_INVALID_PRIMARY_GROUP;
931 if (smb_set_primary_group(grp->gr_name,
932 pdb_get_username(sampass)) != 0) {
933 return NT_STATUS_ACCESS_DENIED;
936 return NT_STATUS_OK;
939 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
941 struct pdb_methods *pdb = pdb_get_methods();
942 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
946 * Helper function to see whether a user is in a group. We can't use
947 * user_in_group_sid here because this creates dependencies only smbd can
948 * fulfil.
951 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
952 const struct dom_sid *group_sid)
954 struct dom_sid *sids;
955 gid_t *gids;
956 uint32_t i, num_groups;
958 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
959 &sids, &gids,
960 &num_groups))) {
961 return False;
964 for (i=0; i<num_groups; i++) {
965 if (dom_sid_equal(group_sid, &sids[i])) {
966 return True;
969 return False;
972 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
973 TALLOC_CTX *mem_ctx,
974 uint32_t group_rid,
975 uint32_t member_rid)
977 struct dom_sid group_sid, member_sid;
978 struct samu *account = NULL;
979 GROUP_MAP *map;
980 struct group *grp;
981 struct passwd *pwd;
982 const char *group_name;
983 uid_t uid;
985 map = talloc_zero(mem_ctx, GROUP_MAP);
986 if (!map) {
987 return NT_STATUS_NO_MEMORY;
990 /* coverity */
991 map->gid = (gid_t) -1;
993 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
994 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
996 if (!get_domain_group_from_sid(group_sid, map) ||
997 (map->gid == (gid_t)-1) ||
998 ((grp = getgrgid(map->gid)) == NULL)) {
999 return NT_STATUS_NO_SUCH_GROUP;
1002 TALLOC_FREE(map);
1004 group_name = talloc_strdup(mem_ctx, grp->gr_name);
1005 if (group_name == NULL) {
1006 return NT_STATUS_NO_MEMORY;
1009 if ( !(account = samu_new( NULL )) ) {
1010 return NT_STATUS_NO_MEMORY;
1013 if (!pdb_getsampwsid(account, &member_sid) ||
1014 !sid_to_uid(&member_sid, &uid) ||
1015 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
1016 return NT_STATUS_NO_SUCH_USER;
1019 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1020 return NT_STATUS_MEMBER_IN_GROUP;
1024 * ok, the group exist, the user exist, the user is not in the group,
1025 * we can (finally) add it to the group !
1028 smb_add_user_group(group_name, pwd->pw_name);
1030 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1031 return NT_STATUS_ACCESS_DENIED;
1034 return NT_STATUS_OK;
1037 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1038 uint32_t member_rid)
1040 struct pdb_methods *pdb = pdb_get_methods();
1041 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
1044 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
1045 TALLOC_CTX *mem_ctx,
1046 uint32_t group_rid,
1047 uint32_t member_rid)
1049 struct dom_sid group_sid, member_sid;
1050 struct samu *account = NULL;
1051 GROUP_MAP *map;
1052 struct group *grp;
1053 struct passwd *pwd;
1054 const char *group_name;
1055 uid_t uid;
1057 map = talloc_zero(mem_ctx, GROUP_MAP);
1058 if (!map) {
1059 return NT_STATUS_NO_MEMORY;
1062 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
1063 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
1065 if (!get_domain_group_from_sid(group_sid, map) ||
1066 (map->gid == (gid_t)-1) ||
1067 ((grp = getgrgid(map->gid)) == NULL)) {
1068 return NT_STATUS_NO_SUCH_GROUP;
1071 TALLOC_FREE(map);
1073 group_name = talloc_strdup(mem_ctx, grp->gr_name);
1074 if (group_name == NULL) {
1075 return NT_STATUS_NO_MEMORY;
1078 if ( !(account = samu_new( NULL )) ) {
1079 return NT_STATUS_NO_MEMORY;
1082 if (!pdb_getsampwsid(account, &member_sid) ||
1083 !sid_to_uid(&member_sid, &uid) ||
1084 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
1085 return NT_STATUS_NO_SUCH_USER;
1088 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
1089 return NT_STATUS_MEMBER_NOT_IN_GROUP;
1093 * ok, the group exist, the user exist, the user is in the group,
1094 * we can (finally) delete it from the group!
1097 smb_delete_user_group(group_name, pwd->pw_name);
1099 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
1100 return NT_STATUS_ACCESS_DENIED;
1103 return NT_STATUS_OK;
1106 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32_t group_rid,
1107 uint32_t member_rid)
1109 struct pdb_methods *pdb = pdb_get_methods();
1110 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
1113 NTSTATUS pdb_create_alias(const char *name, uint32_t *rid)
1115 struct pdb_methods *pdb = pdb_get_methods();
1116 return pdb->create_alias(pdb, name, rid);
1119 NTSTATUS pdb_delete_alias(const struct dom_sid *sid)
1121 struct pdb_methods *pdb = pdb_get_methods();
1122 return pdb->delete_alias(pdb, sid);
1125 NTSTATUS pdb_get_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1127 struct pdb_methods *pdb = pdb_get_methods();
1128 return pdb->get_aliasinfo(pdb, sid, info);
1131 NTSTATUS pdb_set_aliasinfo(const struct dom_sid *sid, struct acct_info *info)
1133 struct pdb_methods *pdb = pdb_get_methods();
1134 return pdb->set_aliasinfo(pdb, sid, info);
1137 NTSTATUS pdb_add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1139 struct pdb_methods *pdb = pdb_get_methods();
1140 return pdb->add_aliasmem(pdb, alias, member);
1143 NTSTATUS pdb_del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
1145 struct pdb_methods *pdb = pdb_get_methods();
1146 return pdb->del_aliasmem(pdb, alias, member);
1149 NTSTATUS pdb_enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
1150 struct dom_sid **pp_members, size_t *p_num_members)
1152 struct pdb_methods *pdb = pdb_get_methods();
1153 return pdb->enum_aliasmem(pdb, alias, mem_ctx, pp_members,
1154 p_num_members);
1157 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1158 const struct dom_sid *domain_sid,
1159 const struct dom_sid *members, size_t num_members,
1160 uint32_t **pp_alias_rids,
1161 size_t *p_num_alias_rids)
1163 struct pdb_methods *pdb = pdb_get_methods();
1164 return pdb->enum_alias_memberships(pdb, mem_ctx,
1165 domain_sid,
1166 members, num_members,
1167 pp_alias_rids,
1168 p_num_alias_rids);
1171 NTSTATUS pdb_lookup_rids(const struct dom_sid *domain_sid,
1172 int num_rids,
1173 uint32_t *rids,
1174 const char **names,
1175 enum lsa_SidType *attrs)
1177 struct pdb_methods *pdb = pdb_get_methods();
1178 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
1181 bool pdb_get_account_policy(enum pdb_policy_type type, uint32_t *value)
1183 struct pdb_methods *pdb = pdb_get_methods();
1184 NTSTATUS status;
1186 become_root();
1187 status = pdb->get_account_policy(pdb, type, value);
1188 unbecome_root();
1190 return NT_STATUS_IS_OK(status);
1193 bool pdb_set_account_policy(enum pdb_policy_type type, uint32_t value)
1195 struct pdb_methods *pdb = pdb_get_methods();
1196 NTSTATUS status;
1198 become_root();
1199 status = pdb->set_account_policy(pdb, type, value);
1200 unbecome_root();
1202 return NT_STATUS_IS_OK(status);
1205 bool pdb_get_seq_num(time_t *seq_num)
1207 struct pdb_methods *pdb = pdb_get_methods();
1208 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1212 * Instead of passing down a gid or uid, this function sends down a pointer
1213 * to a unixid.
1215 * This acts as an in-out variable so that the idmap functions can correctly
1216 * receive ID_TYPE_BOTH, filling in cache details correctly rather than forcing
1217 * the cache to store ID_TYPE_UID or ID_TYPE_GID.
1219 bool pdb_id_to_sid(struct unixid *id, struct dom_sid *sid)
1221 struct pdb_methods *pdb = pdb_get_methods();
1222 bool ret;
1224 ret = pdb->id_to_sid(pdb, id, sid);
1226 if (ret) {
1227 idmap_cache_set_sid2unixid(sid, id);
1230 return ret;
1233 bool pdb_sid_to_id(const struct dom_sid *sid, struct unixid *id)
1235 struct pdb_methods *pdb = pdb_get_methods();
1236 bool ret;
1238 /* only ask the backend if it is responsible */
1239 if (!sid_check_object_is_for_passdb(sid)) {
1240 return false;
1243 ret = pdb->sid_to_id(pdb, sid, id);
1245 if (ret) {
1246 idmap_cache_set_sid2unixid(sid, id);
1249 return ret;
1252 uint32_t pdb_capabilities(void)
1254 struct pdb_methods *pdb = pdb_get_methods();
1255 return pdb->capabilities(pdb);
1258 /********************************************************************
1259 Allocate a new RID from the passdb backend. Verify that it is free
1260 by calling lookup_global_sam_rid() to verify that the RID is not
1261 in use. This handles servers that have existing users or groups
1262 with add RIDs (assigned from previous algorithmic mappings)
1263 ********************************************************************/
1265 bool pdb_new_rid(uint32_t *rid)
1267 struct pdb_methods *pdb = pdb_get_methods();
1268 const char *name = NULL;
1269 enum lsa_SidType type;
1270 uint32_t allocated_rid = 0;
1271 int i;
1272 TALLOC_CTX *ctx;
1274 if ((pdb_capabilities() & PDB_CAP_STORE_RIDS) == 0) {
1275 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1276 "are active\n"));
1277 return False;
1280 if (algorithmic_rid_base() != BASE_RID) {
1281 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1282 "without algorithmic RIDs is chosen.\n"));
1283 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1284 "add', set the maximum used RID\n"));
1285 DEBUGADD(0, ("and remove the parameter\n"));
1286 return False;
1289 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1290 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1291 return False;
1294 /* Attempt to get an unused RID (max tires is 250...yes that it is
1295 and arbitrary number I pulkled out of my head). -- jerry */
1297 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1298 /* get a new RID */
1300 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1301 return False;
1304 /* validate that the RID is not in use */
1306 if (lookup_global_sam_rid(ctx, allocated_rid, &name, &type, NULL, NULL)) {
1307 allocated_rid = 0;
1311 TALLOC_FREE( ctx );
1313 if ( allocated_rid == 0 ) {
1314 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1315 return False;
1318 *rid = allocated_rid;
1320 return True;
1323 /***************************************************************
1324 Initialize the static context (at smbd startup etc).
1326 If uninitialised, context will auto-init on first use.
1327 ***************************************************************/
1329 bool initialize_password_db(bool reload, struct tevent_context *tevent_ctx)
1331 if (tevent_ctx) {
1332 pdb_tevent_ctx = tevent_ctx;
1334 return (pdb_get_methods_reload(reload) != NULL);
1337 /***************************************************************************
1338 Default implementations of some functions.
1339 ****************************************************************************/
1341 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1343 return NT_STATUS_NO_SUCH_USER;
1346 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const struct dom_sid *sid)
1348 return NT_STATUS_NO_SUCH_USER;
1351 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1353 return NT_STATUS_NOT_IMPLEMENTED;
1356 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1358 return NT_STATUS_NOT_IMPLEMENTED;
1361 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1363 return NT_STATUS_NOT_IMPLEMENTED;
1366 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1368 return NT_STATUS_NOT_IMPLEMENTED;
1371 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1373 /* Only the pdb_nds backend implements this, by
1374 * default just return ok. */
1375 return NT_STATUS_OK;
1378 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t *value)
1380 return account_policy_get(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1383 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, enum pdb_policy_type type, uint32_t value)
1385 return account_policy_set(type, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1388 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1390 *seq_num = time(NULL);
1391 return NT_STATUS_OK;
1394 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1395 struct dom_sid *sid)
1397 struct samu *sampw = NULL;
1398 struct passwd *unix_pw;
1399 bool ret;
1401 unix_pw = getpwuid( uid );
1403 if ( !unix_pw ) {
1404 DEBUG(4,("pdb_default_uid_to_sid: host has no idea of uid "
1405 "%lu\n", (unsigned long)uid));
1406 return False;
1409 if ( !(sampw = samu_new( NULL )) ) {
1410 DEBUG(0,("pdb_default_uid_to_sid: samu_new() failed!\n"));
1411 return False;
1414 become_root();
1415 ret = NT_STATUS_IS_OK(
1416 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1417 unbecome_root();
1419 if (!ret) {
1420 DEBUG(5, ("pdb_default_uid_to_sid: Did not find user "
1421 "%s (%u)\n", unix_pw->pw_name, (unsigned int)uid));
1422 TALLOC_FREE(sampw);
1423 return False;
1426 sid_copy(sid, pdb_get_user_sid(sampw));
1428 TALLOC_FREE(sampw);
1430 return True;
1433 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1434 struct dom_sid *sid)
1436 GROUP_MAP *map;
1438 map = talloc_zero(NULL, GROUP_MAP);
1439 if (!map) {
1440 return false;
1443 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, map, gid))) {
1444 TALLOC_FREE(map);
1445 return false;
1448 sid_copy(sid, &map->sid);
1449 TALLOC_FREE(map);
1450 return true;
1453 static bool pdb_default_id_to_sid(struct pdb_methods *methods, struct unixid *id,
1454 struct dom_sid *sid)
1456 switch (id->type) {
1457 case ID_TYPE_UID:
1458 return pdb_default_uid_to_sid(methods, id->id, sid);
1460 case ID_TYPE_GID:
1461 return pdb_default_gid_to_sid(methods, id->id, sid);
1463 default:
1464 return false;
1468 * The "Unix User" and "Unix Group" domains have a special
1469 * id mapping that is a rid-algorithm with range starting at 0.
1471 bool pdb_sid_to_id_unix_users_and_groups(const struct dom_sid *sid,
1472 struct unixid *id)
1474 uint32_t rid;
1476 id->id = -1;
1478 if (sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid)) {
1479 id->id = rid;
1480 id->type = ID_TYPE_UID;
1481 return true;
1484 if (sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid)) {
1485 id->id = rid;
1486 id->type = ID_TYPE_GID;
1487 return true;
1490 return false;
1493 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1494 const struct dom_sid *sid,
1495 struct unixid *id)
1497 TALLOC_CTX *mem_ctx;
1498 bool ret = False;
1499 uint32_t rid;
1500 struct dom_sid_buf buf;
1502 id->id = -1;
1504 mem_ctx = talloc_new(NULL);
1506 if (mem_ctx == NULL) {
1507 DEBUG(0, ("talloc_new failed\n"));
1508 return False;
1511 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1512 const char *name;
1513 enum lsa_SidType type;
1514 uid_t uid = (uid_t)-1;
1515 gid_t gid = (gid_t)-1;
1516 /* Here we might have users as well as groups and aliases */
1517 ret = lookup_global_sam_rid(mem_ctx, rid, &name, &type, &uid, &gid);
1518 if (ret) {
1519 switch (type) {
1520 case SID_NAME_DOM_GRP:
1521 case SID_NAME_ALIAS:
1522 id->type = ID_TYPE_GID;
1523 id->id = gid;
1524 break;
1525 case SID_NAME_USER:
1526 id->type = ID_TYPE_UID;
1527 id->id = uid;
1528 break;
1529 default:
1530 DEBUG(5, ("SID %s belongs to our domain, and "
1531 "an object exists in the database, "
1532 "but it is neither a user nor a "
1533 "group (got type %d).\n",
1534 dom_sid_str_buf(sid, &buf),
1535 type));
1536 ret = false;
1538 } else {
1539 DEBUG(5, ("SID %s belongs to our domain, but there is "
1540 "no corresponding object in the database.\n",
1541 dom_sid_str_buf(sid, &buf)));
1543 goto done;
1547 * "Unix User" and "Unix Group"
1549 ret = pdb_sid_to_id_unix_users_and_groups(sid, id);
1550 if (ret) {
1551 goto done;
1554 /* BUILTIN */
1556 if (sid_check_is_in_builtin(sid) ||
1557 sid_check_is_in_wellknown_domain(sid)) {
1558 /* Here we only have aliases */
1559 GROUP_MAP *map;
1561 map = talloc_zero(mem_ctx, GROUP_MAP);
1562 if (!map) {
1563 ret = false;
1564 goto done;
1567 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, map, *sid))) {
1568 DEBUG(10, ("Could not find map for sid %s\n",
1569 dom_sid_str_buf(sid, &buf)));
1570 goto done;
1572 if ((map->sid_name_use != SID_NAME_ALIAS) &&
1573 (map->sid_name_use != SID_NAME_WKN_GRP)) {
1574 DEBUG(10, ("Map for sid %s is a %s, expected an "
1575 "alias\n",
1576 dom_sid_str_buf(sid, &buf),
1577 sid_type_lookup(map->sid_name_use)));
1578 goto done;
1581 id->id = map->gid;
1582 id->type = ID_TYPE_GID;
1583 ret = True;
1584 goto done;
1587 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1588 dom_sid_str_buf(sid, &buf)));
1590 done:
1592 TALLOC_FREE(mem_ctx);
1593 return ret;
1596 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, uint32_t *p_num)
1598 struct group *grp;
1599 char **gr;
1600 struct passwd *pwd;
1601 bool winbind_env;
1602 bool ret = False;
1604 *pp_uids = NULL;
1605 *p_num = 0;
1607 /* We only look at our own sam, so don't care about imported stuff */
1608 winbind_env = winbind_env_set();
1609 (void)winbind_off();
1611 if ((grp = getgrgid(gid)) == NULL) {
1612 /* allow winbindd lookups, but only if they weren't already disabled */
1613 goto done;
1616 /* Primary group members */
1617 setpwent();
1618 while ((pwd = getpwent()) != NULL) {
1619 if (pwd->pw_gid == gid) {
1620 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1621 pp_uids, p_num)) {
1622 goto done;
1626 endpwent();
1628 /* Secondary group members */
1629 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1630 struct passwd *pw = getpwnam(*gr);
1632 if (pw == NULL)
1633 continue;
1634 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1635 goto done;
1639 ret = True;
1641 done:
1643 /* allow winbindd lookups, but only if they weren't already disabled */
1644 if (!winbind_env) {
1645 (void)winbind_on();
1648 return ret;
1651 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1652 TALLOC_CTX *mem_ctx,
1653 const struct dom_sid *group,
1654 uint32_t **pp_member_rids,
1655 size_t *p_num_members)
1657 gid_t gid;
1658 uid_t *uids;
1659 uint32_t i, num_uids;
1661 *pp_member_rids = NULL;
1662 *p_num_members = 0;
1664 if (!sid_to_gid(group, &gid))
1665 return NT_STATUS_NO_SUCH_GROUP;
1667 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1668 return NT_STATUS_NO_SUCH_GROUP;
1670 if (num_uids == 0)
1671 return NT_STATUS_OK;
1673 *pp_member_rids = talloc_zero_array(mem_ctx, uint32_t, num_uids);
1675 for (i=0; i<num_uids; i++) {
1676 struct dom_sid sid;
1678 uid_to_sid(&sid, uids[i]);
1680 if (!sid_check_is_in_our_sam(&sid)) {
1681 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1682 "in our domain\n"));
1683 continue;
1686 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1687 *p_num_members += 1;
1690 return NT_STATUS_OK;
1693 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1694 TALLOC_CTX *mem_ctx,
1695 struct samu *user,
1696 struct dom_sid **pp_sids,
1697 gid_t **pp_gids,
1698 uint32_t *p_num_groups)
1700 size_t i;
1701 gid_t gid;
1702 struct passwd *pw;
1703 const char *username = pdb_get_username(user);
1706 /* Ignore the primary group SID. Honor the real Unix primary group.
1707 The primary group SID is only of real use to Windows clients */
1709 if ( !(pw = Get_Pwnam_alloc(mem_ctx, username)) ) {
1710 return NT_STATUS_NO_SUCH_USER;
1713 gid = pw->pw_gid;
1715 TALLOC_FREE( pw );
1717 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1718 return NT_STATUS_NO_SUCH_USER;
1721 if (*p_num_groups == 0) {
1722 smb_panic("primary group missing");
1725 *pp_sids = talloc_array(mem_ctx, struct dom_sid, *p_num_groups);
1727 if (*pp_sids == NULL) {
1728 TALLOC_FREE(*pp_gids);
1729 return NT_STATUS_NO_MEMORY;
1732 for (i=0; i<*p_num_groups; i++) {
1733 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1736 return NT_STATUS_OK;
1739 /*******************************************************************
1740 Look up a rid in the SAM we're responsible for (i.e. passdb)
1741 ********************************************************************/
1743 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32_t rid,
1744 const char **name,
1745 enum lsa_SidType *psid_name_use,
1746 uid_t *uid, gid_t *gid)
1748 struct samu *sam_account = NULL;
1749 GROUP_MAP *map = NULL;
1750 bool ret;
1751 struct dom_sid sid;
1753 *psid_name_use = SID_NAME_UNKNOWN;
1755 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1756 (unsigned int)rid));
1758 sid_compose(&sid, get_global_sam_sid(), rid);
1760 /* see if the passdb can help us with the name of the user */
1762 if ( !(sam_account = samu_new( NULL )) ) {
1763 return False;
1766 map = talloc_zero(mem_ctx, GROUP_MAP);
1767 if (!map) {
1768 return false;
1771 /* BEING ROOT BLOCK */
1772 become_root();
1773 ret = pdb_getsampwsid(sam_account, &sid);
1774 if (!ret) {
1775 TALLOC_FREE(sam_account);
1776 ret = pdb_getgrsid(map, sid);
1778 unbecome_root();
1779 /* END BECOME_ROOT BLOCK */
1781 if (sam_account || !ret) {
1782 TALLOC_FREE(map);
1785 if (sam_account) {
1786 struct passwd *pw;
1788 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1789 if (!*name) {
1790 TALLOC_FREE(sam_account);
1791 return False;
1794 *psid_name_use = SID_NAME_USER;
1796 TALLOC_FREE(sam_account);
1798 if (uid == NULL) {
1799 return True;
1802 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1803 if (pw == NULL) {
1804 return False;
1806 *uid = pw->pw_uid;
1807 TALLOC_FREE(pw);
1808 return True;
1810 } else if (map && (map->gid != (gid_t)-1)) {
1812 /* do not resolve SIDs to a name unless there is a valid
1813 gid associated with it */
1815 *name = talloc_steal(mem_ctx, map->nt_name);
1816 *psid_name_use = map->sid_name_use;
1818 if (gid) {
1819 *gid = map->gid;
1822 TALLOC_FREE(map);
1823 return True;
1826 TALLOC_FREE(map);
1828 /* Windows will always map RID 513 to something. On a non-domain
1829 controller, this gets mapped to SERVER\None. */
1831 if (uid || gid) {
1832 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1833 return False;
1836 if ( rid == DOMAIN_RID_USERS ) {
1837 *name = talloc_strdup(mem_ctx, "None" );
1838 *psid_name_use = SID_NAME_DOM_GRP;
1840 return True;
1843 return False;
1846 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1847 const struct dom_sid *domain_sid,
1848 int num_rids,
1849 uint32_t *rids,
1850 const char **names,
1851 enum lsa_SidType *attrs)
1853 int i;
1854 NTSTATUS result;
1855 bool have_mapped = False;
1856 bool have_unmapped = False;
1858 if (sid_check_is_builtin(domain_sid)) {
1860 for (i=0; i<num_rids; i++) {
1861 const char *name;
1863 if (lookup_builtin_rid(names, rids[i], &name)) {
1864 attrs[i] = SID_NAME_ALIAS;
1865 names[i] = name;
1866 DEBUG(5,("lookup_rids: %s:%d\n",
1867 names[i], attrs[i]));
1868 have_mapped = True;
1869 } else {
1870 have_unmapped = True;
1871 attrs[i] = SID_NAME_UNKNOWN;
1874 goto done;
1877 /* Should not happen, but better check once too many */
1878 if (!sid_check_is_our_sam(domain_sid)) {
1879 return NT_STATUS_INVALID_HANDLE;
1882 for (i = 0; i < num_rids; i++) {
1883 const char *name;
1885 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1886 NULL, NULL)) {
1887 if (name == NULL) {
1888 return NT_STATUS_NO_MEMORY;
1890 names[i] = name;
1891 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1892 have_mapped = True;
1893 } else {
1894 have_unmapped = True;
1895 attrs[i] = SID_NAME_UNKNOWN;
1899 done:
1901 result = NT_STATUS_NONE_MAPPED;
1903 if (have_mapped)
1904 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1906 return result;
1909 static int pdb_search_destructor(struct pdb_search *search)
1911 if ((!search->search_ended) && (search->search_end != NULL)) {
1912 search->search_end(search);
1914 return 0;
1917 struct pdb_search *pdb_search_init(TALLOC_CTX *mem_ctx,
1918 enum pdb_search_type type)
1920 struct pdb_search *result;
1922 result = talloc(mem_ctx, struct pdb_search);
1923 if (result == NULL) {
1924 DEBUG(0, ("talloc failed\n"));
1925 return NULL;
1928 result->type = type;
1929 result->cache = NULL;
1930 result->num_entries = 0;
1931 result->cache_size = 0;
1932 result->search_ended = False;
1933 result->search_end = NULL;
1935 /* Segfault appropriately if not initialized */
1936 result->next_entry = NULL;
1937 result->search_end = NULL;
1939 talloc_set_destructor(result, pdb_search_destructor);
1941 return result;
1944 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32_t rid,
1945 uint16_t acct_flags,
1946 const char *account_name,
1947 const char *fullname,
1948 const char *description,
1949 struct samr_displayentry *entry)
1951 entry->rid = rid;
1952 entry->acct_flags = acct_flags;
1954 if (account_name != NULL)
1955 entry->account_name = talloc_strdup(mem_ctx, account_name);
1956 else
1957 entry->account_name = "";
1959 if (fullname != NULL)
1960 entry->fullname = talloc_strdup(mem_ctx, fullname);
1961 else
1962 entry->fullname = "";
1964 if (description != NULL)
1965 entry->description = talloc_strdup(mem_ctx, description);
1966 else
1967 entry->description = "";
1970 struct group_search {
1971 GROUP_MAP **groups;
1972 size_t num_groups, current_group;
1975 static bool next_entry_groups(struct pdb_search *s,
1976 struct samr_displayentry *entry)
1978 struct group_search *state = (struct group_search *)s->private_data;
1979 uint32_t rid;
1980 GROUP_MAP *map;
1982 if (state->current_group == state->num_groups)
1983 return False;
1985 map = state->groups[state->current_group];
1987 sid_peek_rid(&map->sid, &rid);
1989 fill_displayentry(s, rid, 0, map->nt_name, NULL, map->comment, entry);
1991 state->current_group += 1;
1992 return True;
1995 static void search_end_groups(struct pdb_search *search)
1997 struct group_search *state =
1998 (struct group_search *)search->private_data;
1999 TALLOC_FREE(state->groups);
2002 static bool pdb_search_grouptype(struct pdb_methods *methods,
2003 struct pdb_search *search,
2004 const struct dom_sid *sid, enum lsa_SidType type)
2006 struct group_search *state;
2008 state = talloc_zero(search, struct group_search);
2009 if (state == NULL) {
2010 DEBUG(0, ("talloc failed\n"));
2011 return False;
2014 if (!NT_STATUS_IS_OK(methods->enum_group_mapping(methods, sid, type,
2015 &state->groups, &state->num_groups,
2016 True))) {
2017 DEBUG(0, ("Could not enum groups\n"));
2018 return False;
2021 state->current_group = 0;
2022 search->private_data = state;
2023 search->next_entry = next_entry_groups;
2024 search->search_end = search_end_groups;
2025 return True;
2028 static bool pdb_default_search_groups(struct pdb_methods *methods,
2029 struct pdb_search *search)
2031 return pdb_search_grouptype(methods, search, get_global_sam_sid(), SID_NAME_DOM_GRP);
2034 static bool pdb_default_search_aliases(struct pdb_methods *methods,
2035 struct pdb_search *search,
2036 const struct dom_sid *sid)
2039 return pdb_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
2042 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
2043 uint32_t idx)
2045 if (idx < search->num_entries)
2046 return &search->cache[idx];
2048 if (search->search_ended)
2049 return NULL;
2051 while (idx >= search->num_entries) {
2052 struct samr_displayentry entry;
2054 if (!search->next_entry(search, &entry)) {
2055 search->search_end(search);
2056 search->search_ended = True;
2057 break;
2060 ADD_TO_LARGE_ARRAY(search, struct samr_displayentry,
2061 entry, &search->cache, &search->num_entries,
2062 &search->cache_size);
2065 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
2068 struct pdb_search *pdb_search_users(TALLOC_CTX *mem_ctx, uint32_t acct_flags)
2070 struct pdb_methods *pdb = pdb_get_methods();
2071 struct pdb_search *result;
2073 result = pdb_search_init(mem_ctx, PDB_USER_SEARCH);
2074 if (result == NULL) {
2075 return NULL;
2078 if (!pdb->search_users(pdb, result, acct_flags)) {
2079 TALLOC_FREE(result);
2080 return NULL;
2082 return result;
2085 struct pdb_search *pdb_search_groups(TALLOC_CTX *mem_ctx)
2087 struct pdb_methods *pdb = pdb_get_methods();
2088 struct pdb_search *result;
2090 result = pdb_search_init(mem_ctx, PDB_GROUP_SEARCH);
2091 if (result == NULL) {
2092 return NULL;
2095 if (!pdb->search_groups(pdb, result)) {
2096 TALLOC_FREE(result);
2097 return NULL;
2099 return result;
2102 struct pdb_search *pdb_search_aliases(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
2104 struct pdb_methods *pdb = pdb_get_methods();
2105 struct pdb_search *result;
2107 if (pdb == NULL) return NULL;
2109 result = pdb_search_init(mem_ctx, PDB_ALIAS_SEARCH);
2110 if (result == NULL) {
2111 return NULL;
2114 if (!pdb->search_aliases(pdb, result, sid)) {
2115 TALLOC_FREE(result);
2116 return NULL;
2118 return result;
2121 uint32_t pdb_search_entries(struct pdb_search *search,
2122 uint32_t start_idx, uint32_t max_entries,
2123 struct samr_displayentry **result)
2125 struct samr_displayentry *end_entry;
2126 uint32_t end_idx = start_idx+max_entries-1;
2128 /* The first entry needs to be searched after the last. Otherwise the
2129 * first entry might have moved due to a realloc during the search for
2130 * the last entry. */
2132 end_entry = pdb_search_getentry(search, end_idx);
2133 *result = pdb_search_getentry(search, start_idx);
2135 if (end_entry != NULL)
2136 return max_entries;
2138 if (start_idx >= search->num_entries)
2139 return 0;
2141 return search->num_entries - start_idx;
2144 /*******************************************************************
2145 trustdom methods
2146 *******************************************************************/
2148 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, struct dom_sid *sid,
2149 time_t *pass_last_set_time)
2151 struct pdb_methods *pdb = pdb_get_methods();
2152 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
2153 pass_last_set_time);
2156 NTSTATUS pdb_get_trusteddom_creds(const char *domain, TALLOC_CTX *mem_ctx,
2157 struct cli_credentials **creds)
2159 struct pdb_methods *pdb = pdb_get_methods();
2160 return pdb->get_trusteddom_creds(pdb, domain, mem_ctx, creds);
2163 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
2164 const struct dom_sid *sid)
2166 struct pdb_methods *pdb = pdb_get_methods();
2167 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
2170 bool pdb_del_trusteddom_pw(const char *domain)
2172 struct pdb_methods *pdb = pdb_get_methods();
2173 return pdb->del_trusteddom_pw(pdb, domain);
2176 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2177 struct trustdom_info ***domains)
2179 struct pdb_methods *pdb = pdb_get_methods();
2180 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
2183 /*******************************************************************
2184 the defaults for trustdom methods:
2185 these simply call the original passdb/secrets.c actions,
2186 to be replaced by pdb_ldap.
2187 *******************************************************************/
2189 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
2190 const char *domain,
2191 char** pwd,
2192 struct dom_sid *sid,
2193 time_t *pass_last_set_time)
2195 return secrets_fetch_trusted_domain_password(domain, pwd,
2196 sid, pass_last_set_time);
2200 static NTSTATUS pdb_default_get_trusteddom_creds(struct pdb_methods *methods,
2201 const char *domain,
2202 TALLOC_CTX *mem_ctx,
2203 struct cli_credentials **creds)
2205 *creds = NULL;
2206 return NT_STATUS_NOT_IMPLEMENTED;
2209 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
2210 const char* domain,
2211 const char* pwd,
2212 const struct dom_sid *sid)
2214 return secrets_store_trusted_domain_password(domain, pwd, sid);
2217 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
2218 const char *domain)
2220 return trusted_domain_password_delete(domain);
2223 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2224 TALLOC_CTX *mem_ctx,
2225 uint32_t *num_domains,
2226 struct trustdom_info ***domains)
2228 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2231 /*******************************************************************
2232 trusted_domain methods
2233 *******************************************************************/
2235 NTSTATUS pdb_get_trusted_domain(TALLOC_CTX *mem_ctx, const char *domain,
2236 struct pdb_trusted_domain **td)
2238 struct pdb_methods *pdb = pdb_get_methods();
2239 return pdb->get_trusted_domain(pdb, mem_ctx, domain, td);
2242 NTSTATUS pdb_get_trusted_domain_by_sid(TALLOC_CTX *mem_ctx, struct dom_sid *sid,
2243 struct pdb_trusted_domain **td)
2245 struct pdb_methods *pdb = pdb_get_methods();
2246 return pdb->get_trusted_domain_by_sid(pdb, mem_ctx, sid, td);
2249 NTSTATUS pdb_set_trusted_domain(const char* domain,
2250 const struct pdb_trusted_domain *td)
2252 struct pdb_methods *pdb = pdb_get_methods();
2253 return pdb->set_trusted_domain(pdb, domain, td);
2256 NTSTATUS pdb_del_trusted_domain(const char *domain)
2258 struct pdb_methods *pdb = pdb_get_methods();
2259 return pdb->del_trusted_domain(pdb, domain);
2262 NTSTATUS pdb_enum_trusted_domains(TALLOC_CTX *mem_ctx, uint32_t *num_domains,
2263 struct pdb_trusted_domain ***domains)
2265 struct pdb_methods *pdb = pdb_get_methods();
2266 return pdb->enum_trusted_domains(pdb, mem_ctx, num_domains, domains);
2269 static NTSTATUS pdb_default_get_trusted_domain(struct pdb_methods *methods,
2270 TALLOC_CTX *mem_ctx,
2271 const char *domain,
2272 struct pdb_trusted_domain **td)
2274 struct trustAuthInOutBlob taiob;
2275 struct AuthenticationInformation aia;
2276 struct pdb_trusted_domain *tdom;
2277 enum ndr_err_code ndr_err;
2278 time_t last_set_time;
2279 char *pwd;
2280 bool ok;
2282 tdom = talloc(mem_ctx, struct pdb_trusted_domain);
2283 if (!tdom) {
2284 return NT_STATUS_NO_MEMORY;
2287 tdom->domain_name = talloc_strdup(tdom, domain);
2288 tdom->netbios_name = talloc_strdup(tdom, domain);
2289 if (!tdom->domain_name || !tdom->netbios_name) {
2290 talloc_free(tdom);
2291 return NT_STATUS_NO_MEMORY;
2294 tdom->trust_auth_incoming = data_blob_null;
2296 ok = pdb_get_trusteddom_pw(domain, &pwd, &tdom->security_identifier,
2297 &last_set_time);
2298 if (!ok) {
2299 talloc_free(tdom);
2300 return NT_STATUS_UNSUCCESSFUL;
2303 ZERO_STRUCT(taiob);
2304 ZERO_STRUCT(aia);
2305 taiob.count = 1;
2306 taiob.current.count = 1;
2307 taiob.current.array = &aia;
2308 unix_to_nt_time(&aia.LastUpdateTime, last_set_time);
2310 aia.AuthType = TRUST_AUTH_TYPE_CLEAR;
2311 aia.AuthInfo.clear.size = strlen(pwd);
2312 aia.AuthInfo.clear.password = (uint8_t *)talloc_memdup(tdom, pwd,
2313 aia.AuthInfo.clear.size);
2314 SAFE_FREE(pwd);
2315 if (aia.AuthInfo.clear.password == NULL) {
2316 talloc_free(tdom);
2317 return NT_STATUS_NO_MEMORY;
2320 taiob.previous.count = 0;
2321 taiob.previous.array = NULL;
2323 ndr_err = ndr_push_struct_blob(&tdom->trust_auth_outgoing,
2324 tdom, &taiob,
2325 (ndr_push_flags_fn_t)ndr_push_trustAuthInOutBlob);
2326 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2327 talloc_free(tdom);
2328 return NT_STATUS_UNSUCCESSFUL;
2331 tdom->trust_direction = LSA_TRUST_DIRECTION_OUTBOUND;
2332 tdom->trust_type = LSA_TRUST_TYPE_DOWNLEVEL;
2333 tdom->trust_attributes = 0;
2334 tdom->trust_forest_trust_info = data_blob_null;
2336 *td = tdom;
2337 return NT_STATUS_OK;
2340 static NTSTATUS pdb_default_get_trusted_domain_by_sid(struct pdb_methods *methods,
2341 TALLOC_CTX *mem_ctx,
2342 struct dom_sid *sid,
2343 struct pdb_trusted_domain **td)
2345 return NT_STATUS_NOT_IMPLEMENTED;
2348 #define IS_NULL_DATA_BLOB(d) ((d).data == NULL && (d).length == 0)
2350 static NTSTATUS pdb_default_set_trusted_domain(struct pdb_methods *methods,
2351 const char* domain,
2352 const struct pdb_trusted_domain *td)
2354 struct trustAuthInOutBlob taiob;
2355 struct AuthenticationInformation *aia;
2356 enum ndr_err_code ndr_err;
2357 char *pwd;
2358 bool ok;
2360 if (td->trust_attributes != 0 ||
2361 td->trust_type != LSA_TRUST_TYPE_DOWNLEVEL ||
2362 td->trust_direction != LSA_TRUST_DIRECTION_OUTBOUND ||
2363 !IS_NULL_DATA_BLOB(td->trust_auth_incoming) ||
2364 !IS_NULL_DATA_BLOB(td->trust_forest_trust_info)) {
2365 return NT_STATUS_NOT_IMPLEMENTED;
2368 ZERO_STRUCT(taiob);
2369 ndr_err = ndr_pull_struct_blob(&td->trust_auth_outgoing, talloc_tos(),
2370 &taiob,
2371 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2372 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2373 return NT_STATUS_UNSUCCESSFUL;
2376 aia = (struct AuthenticationInformation *) taiob.current.array;
2378 if (taiob.count != 1 || taiob.current.count != 1 ||
2379 taiob.previous.count != 0 ||
2380 aia->AuthType != TRUST_AUTH_TYPE_CLEAR) {
2381 return NT_STATUS_NOT_IMPLEMENTED;
2384 pwd = talloc_strndup(talloc_tos(), (char *) aia->AuthInfo.clear.password,
2385 aia->AuthInfo.clear.size);
2386 if (!pwd) {
2387 return NT_STATUS_NO_MEMORY;
2390 ok = pdb_set_trusteddom_pw(domain, pwd, &td->security_identifier);
2391 if (!ok) {
2392 return NT_STATUS_UNSUCCESSFUL;
2395 return NT_STATUS_OK;
2398 static NTSTATUS pdb_default_del_trusted_domain(struct pdb_methods *methods,
2399 const char *domain)
2401 return NT_STATUS_NOT_IMPLEMENTED;
2404 static NTSTATUS pdb_default_enum_trusted_domains(struct pdb_methods *methods,
2405 TALLOC_CTX *mem_ctx,
2406 uint32_t *num_domains,
2407 struct pdb_trusted_domain ***domains)
2409 return NT_STATUS_NOT_IMPLEMENTED;
2412 static struct pdb_domain_info *pdb_default_get_domain_info(
2413 struct pdb_methods *m, TALLOC_CTX *mem_ctx)
2415 return NULL;
2418 /*****************************************************************
2419 UPN suffixes
2420 *****************************************************************/
2421 static NTSTATUS pdb_default_enum_upn_suffixes(struct pdb_methods *pdb,
2422 TALLOC_CTX *mem_ctx,
2423 uint32_t *num_suffixes,
2424 char ***suffixes)
2426 return NT_STATUS_NOT_IMPLEMENTED;
2429 static NTSTATUS pdb_default_set_upn_suffixes(struct pdb_methods *pdb,
2430 uint32_t num_suffixes,
2431 const char **suffixes)
2433 return NT_STATUS_NOT_IMPLEMENTED;
2436 NTSTATUS pdb_enum_upn_suffixes(TALLOC_CTX *mem_ctx,
2437 uint32_t *num_suffixes,
2438 char ***suffixes)
2440 struct pdb_methods *pdb = pdb_get_methods();
2441 return pdb->enum_upn_suffixes(pdb, mem_ctx, num_suffixes, suffixes);
2444 NTSTATUS pdb_set_upn_suffixes(uint32_t num_suffixes,
2445 const char **suffixes)
2447 struct pdb_methods *pdb = pdb_get_methods();
2448 return pdb->set_upn_suffixes(pdb, num_suffixes, suffixes);
2451 /*******************************************************************
2452 idmap control methods
2453 *******************************************************************/
2454 static bool pdb_default_is_responsible_for_our_sam(
2455 struct pdb_methods *methods)
2457 return true;
2460 static bool pdb_default_is_responsible_for_builtin(
2461 struct pdb_methods *methods)
2463 return true;
2466 static bool pdb_default_is_responsible_for_wellknown(
2467 struct pdb_methods *methods)
2469 return false;
2472 static bool pdb_default_is_responsible_for_unix_users(
2473 struct pdb_methods *methods)
2475 return true;
2478 static bool pdb_default_is_responsible_for_unix_groups(
2479 struct pdb_methods *methods)
2481 return true;
2484 static bool pdb_default_is_responsible_for_everything_else(
2485 struct pdb_methods *methods)
2487 return false;
2490 bool pdb_is_responsible_for_our_sam(void)
2492 struct pdb_methods *pdb = pdb_get_methods();
2493 return pdb->is_responsible_for_our_sam(pdb);
2496 bool pdb_is_responsible_for_builtin(void)
2498 struct pdb_methods *pdb = pdb_get_methods();
2499 return pdb->is_responsible_for_builtin(pdb);
2502 bool pdb_is_responsible_for_wellknown(void)
2504 struct pdb_methods *pdb = pdb_get_methods();
2505 return pdb->is_responsible_for_wellknown(pdb);
2508 bool pdb_is_responsible_for_unix_users(void)
2510 struct pdb_methods *pdb = pdb_get_methods();
2511 return pdb->is_responsible_for_unix_users(pdb);
2514 bool pdb_is_responsible_for_unix_groups(void)
2516 struct pdb_methods *pdb = pdb_get_methods();
2517 return pdb->is_responsible_for_unix_groups(pdb);
2520 bool pdb_is_responsible_for_everything_else(void)
2522 struct pdb_methods *pdb = pdb_get_methods();
2523 return pdb->is_responsible_for_everything_else(pdb);
2526 /*******************************************************************
2527 secret methods
2528 *******************************************************************/
2530 NTSTATUS pdb_get_secret(TALLOC_CTX *mem_ctx,
2531 const char *secret_name,
2532 DATA_BLOB *secret_current,
2533 NTTIME *secret_current_lastchange,
2534 DATA_BLOB *secret_old,
2535 NTTIME *secret_old_lastchange,
2536 struct security_descriptor **sd)
2538 struct pdb_methods *pdb = pdb_get_methods();
2539 return pdb->get_secret(pdb, mem_ctx, secret_name,
2540 secret_current, secret_current_lastchange,
2541 secret_old, secret_old_lastchange,
2542 sd);
2545 NTSTATUS pdb_set_secret(const char *secret_name,
2546 DATA_BLOB *secret_current,
2547 DATA_BLOB *secret_old,
2548 struct security_descriptor *sd)
2550 struct pdb_methods *pdb = pdb_get_methods();
2551 return pdb->set_secret(pdb, secret_name,
2552 secret_current,
2553 secret_old,
2554 sd);
2557 NTSTATUS pdb_delete_secret(const char *secret_name)
2559 struct pdb_methods *pdb = pdb_get_methods();
2560 return pdb->delete_secret(pdb, secret_name);
2563 static NTSTATUS pdb_default_get_secret(struct pdb_methods *methods,
2564 TALLOC_CTX *mem_ctx,
2565 const char *secret_name,
2566 DATA_BLOB *secret_current,
2567 NTTIME *secret_current_lastchange,
2568 DATA_BLOB *secret_old,
2569 NTTIME *secret_old_lastchange,
2570 struct security_descriptor **sd)
2572 return lsa_secret_get(mem_ctx, secret_name,
2573 secret_current,
2574 secret_current_lastchange,
2575 secret_old,
2576 secret_old_lastchange,
2577 sd);
2580 static NTSTATUS pdb_default_set_secret(struct pdb_methods *methods,
2581 const char *secret_name,
2582 DATA_BLOB *secret_current,
2583 DATA_BLOB *secret_old,
2584 struct security_descriptor *sd)
2586 return lsa_secret_set(secret_name,
2587 secret_current,
2588 secret_old,
2589 sd);
2592 static NTSTATUS pdb_default_delete_secret(struct pdb_methods *methods,
2593 const char *secret_name)
2595 return lsa_secret_delete(secret_name);
2598 /*******************************************************************
2599 Create a pdb_methods structure and initialize it with the default
2600 operations. In this way a passdb module can simply implement
2601 the functionality it cares about. However, normally this is done
2602 in groups of related functions.
2603 *******************************************************************/
2605 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2607 /* allocate memory for the structure as its own talloc CTX */
2609 *methods = talloc_zero(NULL, struct pdb_methods);
2610 if (*methods == NULL) {
2611 return NT_STATUS_NO_MEMORY;
2614 (*methods)->get_domain_info = pdb_default_get_domain_info;
2615 (*methods)->getsampwnam = pdb_default_getsampwnam;
2616 (*methods)->getsampwsid = pdb_default_getsampwsid;
2617 (*methods)->create_user = pdb_default_create_user;
2618 (*methods)->delete_user = pdb_default_delete_user;
2619 (*methods)->add_sam_account = pdb_default_add_sam_account;
2620 (*methods)->update_sam_account = pdb_default_update_sam_account;
2621 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2622 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2623 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2625 (*methods)->getgrsid = pdb_default_getgrsid;
2626 (*methods)->getgrgid = pdb_default_getgrgid;
2627 (*methods)->getgrnam = pdb_default_getgrnam;
2628 (*methods)->create_dom_group = pdb_default_create_dom_group;
2629 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2630 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2631 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2632 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2633 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2634 (*methods)->enum_group_members = pdb_default_enum_group_members;
2635 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2636 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2637 (*methods)->add_groupmem = pdb_default_add_groupmem;
2638 (*methods)->del_groupmem = pdb_default_del_groupmem;
2639 (*methods)->create_alias = pdb_default_create_alias;
2640 (*methods)->delete_alias = pdb_default_delete_alias;
2641 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2642 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2643 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2644 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2645 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2646 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2647 (*methods)->lookup_rids = pdb_default_lookup_rids;
2648 (*methods)->get_account_policy = pdb_default_get_account_policy;
2649 (*methods)->set_account_policy = pdb_default_set_account_policy;
2650 (*methods)->get_seq_num = pdb_default_get_seq_num;
2651 (*methods)->id_to_sid = pdb_default_id_to_sid;
2652 (*methods)->sid_to_id = pdb_default_sid_to_id;
2654 (*methods)->search_groups = pdb_default_search_groups;
2655 (*methods)->search_aliases = pdb_default_search_aliases;
2657 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2658 (*methods)->get_trusteddom_creds = pdb_default_get_trusteddom_creds;
2659 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2660 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2661 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2663 (*methods)->get_trusted_domain = pdb_default_get_trusted_domain;
2664 (*methods)->get_trusted_domain_by_sid = pdb_default_get_trusted_domain_by_sid;
2665 (*methods)->set_trusted_domain = pdb_default_set_trusted_domain;
2666 (*methods)->del_trusted_domain = pdb_default_del_trusted_domain;
2667 (*methods)->enum_trusted_domains = pdb_default_enum_trusted_domains;
2669 (*methods)->get_secret = pdb_default_get_secret;
2670 (*methods)->set_secret = pdb_default_set_secret;
2671 (*methods)->delete_secret = pdb_default_delete_secret;
2673 (*methods)->enum_upn_suffixes = pdb_default_enum_upn_suffixes;
2674 (*methods)->set_upn_suffixes = pdb_default_set_upn_suffixes;
2676 (*methods)->is_responsible_for_our_sam =
2677 pdb_default_is_responsible_for_our_sam;
2678 (*methods)->is_responsible_for_builtin =
2679 pdb_default_is_responsible_for_builtin;
2680 (*methods)->is_responsible_for_wellknown =
2681 pdb_default_is_responsible_for_wellknown;
2682 (*methods)->is_responsible_for_unix_users =
2683 pdb_default_is_responsible_for_unix_users;
2684 (*methods)->is_responsible_for_unix_groups =
2685 pdb_default_is_responsible_for_unix_groups;
2686 (*methods)->is_responsible_for_everything_else =
2687 pdb_default_is_responsible_for_everything_else;
2689 return NT_STATUS_OK;