Fix a delete on close divergence from windows [1/2]
[Samba.git] / source / passdb / pdb_interface.c
blobb83d6186df6ebb7c99f2ef1d9fd59347713d5949
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"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_PASSDB
28 static_decl_pdb;
30 static struct pdb_init_function_entry *backends = NULL;
32 static void lazy_initialize_passdb(void)
34 static bool initialized = False;
35 if(initialized) {
36 return;
38 static_init_pdb;
39 initialized = True;
42 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
43 const char **name,
44 enum lsa_SidType *psid_name_use,
45 union unid_t *unix_id);
47 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
49 struct pdb_init_function_entry *entry = backends;
51 if(version != PASSDB_INTERFACE_VERSION) {
52 DEBUG(0,("Can't register passdb backend!\n"
53 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
54 "while this version of samba uses version %d\n",
55 version,PASSDB_INTERFACE_VERSION));
56 return NT_STATUS_OBJECT_TYPE_MISMATCH;
59 if (!name || !init) {
60 return NT_STATUS_INVALID_PARAMETER;
63 DEBUG(5,("Attempting to register passdb backend %s\n", name));
65 /* Check for duplicates */
66 if (pdb_find_backend_entry(name)) {
67 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
68 return NT_STATUS_OBJECT_NAME_COLLISION;
71 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
72 entry->name = smb_xstrdup(name);
73 entry->init = init;
75 DLIST_ADD(backends, entry);
76 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
77 return NT_STATUS_OK;
80 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
82 struct pdb_init_function_entry *entry = backends;
84 while(entry) {
85 if (strcmp(entry->name, name)==0) return entry;
86 entry = entry->next;
89 return NULL;
93 * The event context for the passdb backend. I know this is a bad hack and yet
94 * another static variable, but our pdb API is a global thing per
95 * definition. The first use for this is the LDAP idle function, more might be
96 * added later.
98 * I don't feel too bad about this static variable, it replaces the
99 * smb_idle_event_list that used to exist in lib/module.c. -- VL
102 static struct event_context *pdb_event_ctx;
104 struct event_context *pdb_get_event_context(void)
106 return pdb_event_ctx;
109 /******************************************************************
110 Make a pdb_methods from scratch
111 *******************************************************************/
113 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
115 char *module_name = smb_xstrdup(selected);
116 char *module_location = NULL, *p;
117 struct pdb_init_function_entry *entry;
118 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
120 lazy_initialize_passdb();
122 p = strchr(module_name, ':');
124 if (p) {
125 *p = 0;
126 module_location = p+1;
127 trim_char(module_location, ' ', ' ');
130 trim_char(module_name, ' ', ' ');
133 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
135 entry = pdb_find_backend_entry(module_name);
137 /* Try to find a module that contains this module */
138 if (!entry) {
139 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
140 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
141 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
142 SAFE_FREE(module_name);
143 return NT_STATUS_UNSUCCESSFUL;
147 /* No such backend found */
148 if(!entry) {
149 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
150 SAFE_FREE(module_name);
151 return NT_STATUS_INVALID_PARAMETER;
154 DEBUG(5,("Found pdb backend %s\n", module_name));
156 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
157 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
158 selected, nt_errstr(nt_status)));
159 SAFE_FREE(module_name);
160 return nt_status;
163 SAFE_FREE(module_name);
165 DEBUG(5,("pdb backend %s has a valid init\n", selected));
167 return nt_status;
170 /******************************************************************
171 Return an already initialized pdb_methods structure
172 *******************************************************************/
174 static struct pdb_methods *pdb_get_methods_reload( bool reload )
176 static struct pdb_methods *pdb = NULL;
178 if ( pdb && reload ) {
179 pdb->free_private_data( &(pdb->private_data) );
180 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
181 char *msg = NULL;
182 asprintf(&msg, "pdb_get_methods_reload: "
183 "failed to get pdb methods for backend %s\n",
184 lp_passdb_backend());
185 smb_panic(msg);
189 if ( !pdb ) {
190 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
191 char *msg = NULL;
192 asprintf(&msg, "pdb_get_methods_reload: "
193 "failed to get pdb methods for backend %s\n",
194 lp_passdb_backend());
195 smb_panic(msg);
199 return pdb;
202 static struct pdb_methods *pdb_get_methods(void)
204 return pdb_get_methods_reload(False);
207 bool pdb_getsampwnam(struct samu *sam_acct, const char *username)
209 struct pdb_methods *pdb = pdb_get_methods();
210 struct samu *for_cache;
211 const struct dom_sid *user_sid;
213 if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
214 return False;
217 for_cache = samu_new(NULL);
218 if (for_cache == NULL) {
219 return False;
222 if (!pdb_copy_sam_account(for_cache, sam_acct)) {
223 TALLOC_FREE(for_cache);
224 return False;
227 user_sid = pdb_get_user_sid(for_cache);
229 memcache_add_talloc(NULL, PDB_GETPWSID_CACHE,
230 data_blob_const(user_sid, sizeof(*user_sid)),
231 &for_cache);
233 return True;
236 /**********************************************************************
237 **********************************************************************/
239 bool guest_user_info( struct samu *user )
241 struct passwd *pwd;
242 NTSTATUS result;
243 const char *guestname = lp_guestaccount();
245 if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
246 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
247 guestname));
248 return False;
251 result = samu_set_unix(user, pwd );
253 TALLOC_FREE( pwd );
255 return NT_STATUS_IS_OK( result );
258 /**********************************************************************
259 **********************************************************************/
261 bool pdb_getsampwsid(struct samu *sam_acct, const DOM_SID *sid)
263 struct pdb_methods *pdb = pdb_get_methods();
264 uint32 rid;
265 void *cache_data;
267 /* hard code the Guest RID of 501 */
269 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
270 return False;
272 if ( rid == DOMAIN_USER_RID_GUEST ) {
273 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
274 return guest_user_info( sam_acct );
277 /* check the cache first */
279 cache_data = memcache_lookup_talloc(
280 NULL, PDB_GETPWSID_CACHE, data_blob_const(sid, sizeof(*sid)));
282 if (cache_data != NULL) {
283 struct samu *cache_copy = talloc_get_type_abort(
284 cache_data, struct samu);
286 return pdb_copy_sam_account(sam_acct, cache_copy);
289 return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
292 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
293 TALLOC_CTX *tmp_ctx, const char *name,
294 uint32 acb_info, uint32 *rid)
296 struct samu *sam_pass;
297 NTSTATUS status;
298 struct passwd *pwd;
300 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
301 return NT_STATUS_NO_MEMORY;
304 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
305 char *add_script = NULL;
306 int add_ret;
307 fstring name2;
309 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
310 add_script = talloc_strdup(tmp_ctx,
311 lp_adduser_script());
312 } else {
313 add_script = talloc_strdup(tmp_ctx,
314 lp_addmachine_script());
317 if (!add_script || add_script[0] == '\0') {
318 DEBUG(3, ("Could not find user %s and no add script "
319 "defined\n", name));
320 return NT_STATUS_NO_SUCH_USER;
323 /* lowercase the username before creating the Unix account for
324 compatibility with previous Samba releases */
325 fstrcpy( name2, name );
326 strlower_m( name2 );
327 add_script = talloc_all_string_sub(tmp_ctx,
328 add_script,
329 "%u",
330 name2);
331 if (!add_script) {
332 return NT_STATUS_NO_MEMORY;
334 add_ret = smbrun(add_script,NULL);
335 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
336 add_script, add_ret));
337 if (add_ret == 0) {
338 smb_nscd_flush_user_cache();
341 flush_pwnam_cache();
343 pwd = Get_Pwnam_alloc(tmp_ctx, name);
346 /* we have a valid SID coming out of this call */
348 status = samu_alloc_rid_unix( sam_pass, pwd );
350 TALLOC_FREE( pwd );
352 if (!NT_STATUS_IS_OK(status)) {
353 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
354 return status;
357 if (!sid_peek_check_rid(get_global_sam_sid(),
358 pdb_get_user_sid(sam_pass), rid)) {
359 DEBUG(0, ("Could not get RID of fresh user\n"));
360 return NT_STATUS_INTERNAL_ERROR;
363 /* Use the username case specified in the original request */
365 pdb_set_username( sam_pass, name, PDB_SET );
367 /* Disable the account on creation, it does not have a reasonable password yet. */
369 acb_info |= ACB_DISABLED;
371 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
373 status = pdb_add_sam_account(sam_pass);
375 TALLOC_FREE(sam_pass);
377 return status;
380 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
381 uint32 *rid)
383 struct pdb_methods *pdb = pdb_get_methods();
384 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
387 /****************************************************************************
388 Delete a UNIX user on demand.
389 ****************************************************************************/
391 static int smb_delete_user(const char *unix_user)
393 char *del_script = NULL;
394 int ret;
396 /* safety check */
398 if ( strequal( unix_user, "root" ) ) {
399 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
400 return -1;
403 del_script = talloc_strdup(talloc_tos(), lp_deluser_script());
404 if (!del_script || !*del_script) {
405 return -1;
407 del_script = talloc_all_string_sub(talloc_tos(),
408 del_script,
409 "%u",
410 unix_user);
411 if (!del_script) {
412 return -1;
414 ret = smbrun(del_script,NULL);
415 flush_pwnam_cache();
416 if (ret == 0) {
417 smb_nscd_flush_user_cache();
419 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
421 return ret;
424 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
425 TALLOC_CTX *mem_ctx,
426 struct samu *sam_acct)
428 NTSTATUS status;
429 fstring username;
431 status = pdb_delete_sam_account(sam_acct);
432 if (!NT_STATUS_IS_OK(status)) {
433 return status;
437 * Now delete the unix side ....
438 * note: we don't check if the delete really happened as the script is
439 * not necessary present and maybe the sysadmin doesn't want to delete
440 * the unix side
443 /* always lower case the username before handing it off to
444 external scripts */
446 fstrcpy( username, pdb_get_username(sam_acct) );
447 strlower_m( username );
449 smb_delete_user( username );
451 return status;
454 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
456 struct pdb_methods *pdb = pdb_get_methods();
457 uid_t uid = -1;
459 /* sanity check to make sure we don't delete root */
461 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
462 return NT_STATUS_NO_SUCH_USER;
465 if ( uid == 0 ) {
466 return NT_STATUS_ACCESS_DENIED;
469 return pdb->delete_user(pdb, mem_ctx, sam_acct);
472 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
474 struct pdb_methods *pdb = pdb_get_methods();
475 return pdb->add_sam_account(pdb, sam_acct);
478 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
480 struct pdb_methods *pdb = pdb_get_methods();
482 memcache_flush(NULL, PDB_GETPWSID_CACHE);
484 return pdb->update_sam_account(pdb, sam_acct);
487 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
489 struct pdb_methods *pdb = pdb_get_methods();
491 memcache_flush(NULL, PDB_GETPWSID_CACHE);
493 return pdb->delete_sam_account(pdb, sam_acct);
496 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
498 struct pdb_methods *pdb = pdb_get_methods();
499 uid_t uid;
500 NTSTATUS status;
502 memcache_flush(NULL, PDB_GETPWSID_CACHE);
504 /* sanity check to make sure we don't rename root */
506 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
507 return NT_STATUS_NO_SUCH_USER;
510 if ( uid == 0 ) {
511 return NT_STATUS_ACCESS_DENIED;
514 status = pdb->rename_sam_account(pdb, oldname, newname);
516 /* always flush the cache here just to be safe */
517 flush_pwnam_cache();
519 return status;
522 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, bool success)
524 struct pdb_methods *pdb = pdb_get_methods();
525 return pdb->update_login_attempts(pdb, sam_acct, success);
528 bool pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
530 struct pdb_methods *pdb = pdb_get_methods();
531 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
534 bool pdb_getgrgid(GROUP_MAP *map, gid_t gid)
536 struct pdb_methods *pdb = pdb_get_methods();
537 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
540 bool pdb_getgrnam(GROUP_MAP *map, const char *name)
542 struct pdb_methods *pdb = pdb_get_methods();
543 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
546 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
547 TALLOC_CTX *mem_ctx,
548 const char *name,
549 uint32 *rid)
551 DOM_SID group_sid;
552 struct group *grp;
553 fstring tmp;
555 grp = getgrnam(name);
557 if (grp == NULL) {
558 gid_t gid;
560 if (smb_create_group(name, &gid) != 0) {
561 return NT_STATUS_ACCESS_DENIED;
564 grp = getgrgid(gid);
567 if (grp == NULL) {
568 return NT_STATUS_ACCESS_DENIED;
571 if (pdb_rid_algorithm()) {
572 *rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
573 } else {
574 if (!pdb_new_rid(rid)) {
575 return NT_STATUS_ACCESS_DENIED;
579 sid_compose(&group_sid, get_global_sam_sid(), *rid);
581 return add_initial_entry(grp->gr_gid, sid_to_fstring(tmp, &group_sid),
582 SID_NAME_DOM_GRP, name, NULL);
585 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
586 uint32 *rid)
588 struct pdb_methods *pdb = pdb_get_methods();
589 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
592 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
593 TALLOC_CTX *mem_ctx,
594 uint32 rid)
596 DOM_SID group_sid;
597 GROUP_MAP map;
598 NTSTATUS status;
599 struct group *grp;
600 const char *grp_name;
602 sid_compose(&group_sid, get_global_sam_sid(), rid);
604 if (!get_domain_group_from_sid(group_sid, &map)) {
605 DEBUG(10, ("Could not find group for rid %d\n", rid));
606 return NT_STATUS_NO_SUCH_GROUP;
609 /* We need the group name for the smb_delete_group later on */
611 if (map.gid == (gid_t)-1) {
612 return NT_STATUS_NO_SUCH_GROUP;
615 grp = getgrgid(map.gid);
616 if (grp == NULL) {
617 return NT_STATUS_NO_SUCH_GROUP;
620 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
622 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
623 if (grp_name == NULL) {
624 return NT_STATUS_NO_MEMORY;
627 status = pdb_delete_group_mapping_entry(group_sid);
629 if (!NT_STATUS_IS_OK(status)) {
630 return status;
633 /* Don't check the result of smb_delete_group */
635 smb_delete_group(grp_name);
637 return NT_STATUS_OK;
640 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
642 struct pdb_methods *pdb = pdb_get_methods();
643 return pdb->delete_dom_group(pdb, mem_ctx, rid);
646 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
648 struct pdb_methods *pdb = pdb_get_methods();
649 return pdb->add_group_mapping_entry(pdb, map);
652 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
654 struct pdb_methods *pdb = pdb_get_methods();
655 return pdb->update_group_mapping_entry(pdb, map);
658 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
660 struct pdb_methods *pdb = pdb_get_methods();
661 return pdb->delete_group_mapping_entry(pdb, sid);
664 bool pdb_enum_group_mapping(const DOM_SID *sid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
665 size_t *p_num_entries, bool unix_only)
667 struct pdb_methods *pdb = pdb_get_methods();
668 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
669 pp_rmap, p_num_entries, unix_only));
672 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
673 const DOM_SID *sid,
674 uint32 **pp_member_rids,
675 size_t *p_num_members)
677 struct pdb_methods *pdb = pdb_get_methods();
678 NTSTATUS result;
680 result = pdb->enum_group_members(pdb, mem_ctx,
681 sid, pp_member_rids, p_num_members);
683 /* special check for rid 513 */
685 if ( !NT_STATUS_IS_OK( result ) ) {
686 uint32 rid;
688 sid_peek_rid( sid, &rid );
690 if ( rid == DOMAIN_GROUP_RID_USERS ) {
691 *p_num_members = 0;
692 *pp_member_rids = NULL;
694 return NT_STATUS_OK;
698 return result;
701 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
702 DOM_SID **pp_sids, gid_t **pp_gids,
703 size_t *p_num_groups)
705 struct pdb_methods *pdb = pdb_get_methods();
706 return pdb->enum_group_memberships(
707 pdb, mem_ctx, user,
708 pp_sids, pp_gids, p_num_groups);
711 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
712 TALLOC_CTX *mem_ctx,
713 struct samu *sampass)
715 struct group *grp;
716 gid_t gid;
718 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
719 (grp = getgrgid(gid)) == NULL) {
720 return NT_STATUS_INVALID_PRIMARY_GROUP;
723 if (smb_set_primary_group(grp->gr_name,
724 pdb_get_username(sampass)) != 0) {
725 return NT_STATUS_ACCESS_DENIED;
728 return NT_STATUS_OK;
731 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
733 struct pdb_methods *pdb = pdb_get_methods();
734 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
738 * Helper function to see whether a user is in a group. We can't use
739 * user_in_group_sid here because this creates dependencies only smbd can
740 * fulfil.
743 static bool pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
744 const DOM_SID *group_sid)
746 DOM_SID *sids;
747 gid_t *gids;
748 size_t i, num_groups;
750 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
751 &sids, &gids,
752 &num_groups))) {
753 return False;
756 for (i=0; i<num_groups; i++) {
757 if (sid_equal(group_sid, &sids[i])) {
758 return True;
761 return False;
764 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
765 TALLOC_CTX *mem_ctx,
766 uint32 group_rid,
767 uint32 member_rid)
769 DOM_SID group_sid, member_sid;
770 struct samu *account = NULL;
771 GROUP_MAP map;
772 struct group *grp;
773 struct passwd *pwd;
774 const char *group_name;
775 uid_t uid;
777 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
778 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
780 if (!get_domain_group_from_sid(group_sid, &map) ||
781 (map.gid == (gid_t)-1) ||
782 ((grp = getgrgid(map.gid)) == NULL)) {
783 return NT_STATUS_NO_SUCH_GROUP;
786 group_name = talloc_strdup(mem_ctx, grp->gr_name);
787 if (group_name == NULL) {
788 return NT_STATUS_NO_MEMORY;
791 if ( !(account = samu_new( NULL )) ) {
792 return NT_STATUS_NO_MEMORY;
795 if (!pdb_getsampwsid(account, &member_sid) ||
796 !sid_to_uid(&member_sid, &uid) ||
797 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
798 return NT_STATUS_NO_SUCH_USER;
801 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
802 return NT_STATUS_MEMBER_IN_GROUP;
806 * ok, the group exist, the user exist, the user is not in the group,
807 * we can (finally) add it to the group !
810 smb_add_user_group(group_name, pwd->pw_name);
812 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
813 return NT_STATUS_ACCESS_DENIED;
816 return NT_STATUS_OK;
819 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
820 uint32 member_rid)
822 struct pdb_methods *pdb = pdb_get_methods();
823 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
826 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
827 TALLOC_CTX *mem_ctx,
828 uint32 group_rid,
829 uint32 member_rid)
831 DOM_SID group_sid, member_sid;
832 struct samu *account = NULL;
833 GROUP_MAP map;
834 struct group *grp;
835 struct passwd *pwd;
836 const char *group_name;
837 uid_t uid;
839 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
840 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
842 if (!get_domain_group_from_sid(group_sid, &map) ||
843 (map.gid == (gid_t)-1) ||
844 ((grp = getgrgid(map.gid)) == NULL)) {
845 return NT_STATUS_NO_SUCH_GROUP;
848 group_name = talloc_strdup(mem_ctx, grp->gr_name);
849 if (group_name == NULL) {
850 return NT_STATUS_NO_MEMORY;
853 if ( !(account = samu_new( NULL )) ) {
854 return NT_STATUS_NO_MEMORY;
857 if (!pdb_getsampwsid(account, &member_sid) ||
858 !sid_to_uid(&member_sid, &uid) ||
859 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
860 return NT_STATUS_NO_SUCH_USER;
863 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
864 return NT_STATUS_MEMBER_NOT_IN_GROUP;
868 * ok, the group exist, the user exist, the user is in the group,
869 * we can (finally) delete it from the group!
872 smb_delete_user_group(group_name, pwd->pw_name);
874 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
875 return NT_STATUS_ACCESS_DENIED;
878 return NT_STATUS_OK;
881 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
882 uint32 member_rid)
884 struct pdb_methods *pdb = pdb_get_methods();
885 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
888 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
890 struct pdb_methods *pdb = pdb_get_methods();
891 return pdb->create_alias(pdb, name, rid);
894 NTSTATUS pdb_delete_alias(const DOM_SID *sid)
896 struct pdb_methods *pdb = pdb_get_methods();
897 return pdb->delete_alias(pdb, sid);
900 NTSTATUS pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
902 struct pdb_methods *pdb = pdb_get_methods();
903 return pdb->get_aliasinfo(pdb, sid, info);
906 NTSTATUS pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
908 struct pdb_methods *pdb = pdb_get_methods();
909 return pdb->set_aliasinfo(pdb, sid, info);
912 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
914 struct pdb_methods *pdb = pdb_get_methods();
915 return pdb->add_aliasmem(pdb, alias, member);
918 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
920 struct pdb_methods *pdb = pdb_get_methods();
921 return pdb->del_aliasmem(pdb, alias, member);
924 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
925 DOM_SID **pp_members, size_t *p_num_members)
927 struct pdb_methods *pdb = pdb_get_methods();
928 return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
931 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
932 const DOM_SID *domain_sid,
933 const DOM_SID *members, size_t num_members,
934 uint32 **pp_alias_rids,
935 size_t *p_num_alias_rids)
937 struct pdb_methods *pdb = pdb_get_methods();
938 return pdb->enum_alias_memberships(pdb, mem_ctx,
939 domain_sid,
940 members, num_members,
941 pp_alias_rids,
942 p_num_alias_rids);
945 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
946 int num_rids,
947 uint32 *rids,
948 const char **names,
949 enum lsa_SidType *attrs)
951 struct pdb_methods *pdb = pdb_get_methods();
952 return pdb->lookup_rids(pdb, domain_sid, num_rids, rids, names, attrs);
956 * NOTE: pdb_lookup_names is currently (2007-01-12) not used anywhere
957 * in the samba code.
958 * Unlike _lsa_lookup_sids and _samr_lookup_rids, which eventually
959 * also ask pdb_lookup_rids, thus looking up a bunch of rids at a time,
960 * the pdb_ calls _lsa_lookup_names and _samr_lookup_names come
961 * down to are pdb_getsampwnam and pdb_getgrnam instead of
962 * pdb_lookup_names.
963 * But in principle, it the call belongs to the API and might get
964 * used in this context some day.
966 #if 0
967 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
968 int num_names,
969 const char **names,
970 uint32 *rids,
971 enum lsa_SidType *attrs)
973 struct pdb_methods *pdb = pdb_get_methods();
974 return pdb->lookup_names(pdb, domain_sid, num_names, names, rids, attrs);
976 #endif
978 bool pdb_get_account_policy(int policy_index, uint32 *value)
980 struct pdb_methods *pdb = pdb_get_methods();
981 NTSTATUS status;
983 become_root();
984 status = pdb->get_account_policy(pdb, policy_index, value);
985 unbecome_root();
987 return NT_STATUS_IS_OK(status);
990 bool pdb_set_account_policy(int policy_index, uint32 value)
992 struct pdb_methods *pdb = pdb_get_methods();
993 NTSTATUS status;
995 become_root();
996 status = pdb->set_account_policy(pdb, policy_index, value);
997 unbecome_root();
999 return NT_STATUS_IS_OK(status);
1002 bool pdb_get_seq_num(time_t *seq_num)
1004 struct pdb_methods *pdb = pdb_get_methods();
1005 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1008 bool pdb_uid_to_rid(uid_t uid, uint32 *rid)
1010 struct pdb_methods *pdb = pdb_get_methods();
1011 return pdb->uid_to_rid(pdb, uid, rid);
1014 bool pdb_uid_to_sid(uid_t uid, DOM_SID *sid)
1016 struct pdb_methods *pdb = pdb_get_methods();
1017 return pdb->uid_to_sid(pdb, uid, sid);
1020 bool pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1022 struct pdb_methods *pdb = pdb_get_methods();
1023 return pdb->gid_to_sid(pdb, gid, sid);
1026 bool pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1027 enum lsa_SidType *type)
1029 struct pdb_methods *pdb = pdb_get_methods();
1030 return pdb->sid_to_id(pdb, sid, id, type);
1033 bool pdb_rid_algorithm(void)
1035 struct pdb_methods *pdb = pdb_get_methods();
1036 return pdb->rid_algorithm(pdb);
1039 /********************************************************************
1040 Allocate a new RID from the passdb backend. Verify that it is free
1041 by calling lookup_global_sam_rid() to verify that the RID is not
1042 in use. This handles servers that have existing users or groups
1043 with add RIDs (assigned from previous algorithmic mappings)
1044 ********************************************************************/
1046 bool pdb_new_rid(uint32 *rid)
1048 struct pdb_methods *pdb = pdb_get_methods();
1049 const char *name = NULL;
1050 enum lsa_SidType type;
1051 uint32 allocated_rid = 0;
1052 int i;
1053 TALLOC_CTX *ctx;
1055 if (pdb_rid_algorithm()) {
1056 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1057 "are active\n"));
1058 return False;
1061 if (algorithmic_rid_base() != BASE_RID) {
1062 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1063 "without algorithmic RIDs is chosen.\n"));
1064 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1065 "add', set the maximum used RID using\n"));
1066 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1067 return False;
1070 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1071 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1072 return False;
1075 /* Attempt to get an unused RID (max tires is 250...yes that it is
1076 and arbitrary number I pulkled out of my head). -- jerry */
1078 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1079 /* get a new RID */
1081 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1082 return False;
1085 /* validate that the RID is not in use */
1087 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1088 allocated_rid = 0;
1092 TALLOC_FREE( ctx );
1094 if ( allocated_rid == 0 ) {
1095 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1096 return False;
1099 *rid = allocated_rid;
1101 return True;
1104 /***************************************************************
1105 Initialize the static context (at smbd startup etc).
1107 If uninitialised, context will auto-init on first use.
1108 ***************************************************************/
1110 bool initialize_password_db(bool reload, struct event_context *event_ctx)
1112 pdb_event_ctx = event_ctx;
1113 return (pdb_get_methods_reload(reload) != NULL);
1117 /***************************************************************************
1118 Default implementations of some functions.
1119 ****************************************************************************/
1121 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1123 return NT_STATUS_NO_SUCH_USER;
1126 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1128 return NT_STATUS_NO_SUCH_USER;
1131 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1133 return NT_STATUS_NOT_IMPLEMENTED;
1136 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1138 return NT_STATUS_NOT_IMPLEMENTED;
1141 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1143 return NT_STATUS_NOT_IMPLEMENTED;
1146 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1148 return NT_STATUS_NOT_IMPLEMENTED;
1151 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, bool success)
1153 /* Only the pdb_nds backend implements this, by
1154 * default just return ok. */
1155 return NT_STATUS_OK;
1158 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1160 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1163 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1165 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1168 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1170 *seq_num = time(NULL);
1171 return NT_STATUS_OK;
1174 static bool pdb_default_uid_to_sid(struct pdb_methods *methods, uid_t uid,
1175 DOM_SID *sid)
1177 struct samu *sampw = NULL;
1178 struct passwd *unix_pw;
1179 bool ret;
1181 unix_pw = sys_getpwuid( uid );
1183 if ( !unix_pw ) {
1184 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1185 "%lu\n", (unsigned long)uid));
1186 return False;
1189 if ( !(sampw = samu_new( NULL )) ) {
1190 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1191 return False;
1194 become_root();
1195 ret = NT_STATUS_IS_OK(
1196 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1197 unbecome_root();
1199 if (!ret) {
1200 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1201 "%s (%d)\n", unix_pw->pw_name, uid));
1202 TALLOC_FREE(sampw);
1203 return False;
1206 sid_copy(sid, pdb_get_user_sid(sampw));
1208 TALLOC_FREE(sampw);
1210 return True;
1213 static bool pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1214 uint32 *rid)
1216 DOM_SID sid;
1217 bool ret;
1219 ret = pdb_default_uid_to_sid(methods, uid, &sid);
1220 if (!ret) {
1221 return ret;
1224 ret = sid_peek_check_rid(get_global_sam_sid(), &sid, rid);
1226 if (!ret) {
1227 DEBUG(1, ("Could not peek rid out of sid %s\n",
1228 sid_string_dbg(&sid)));
1231 return ret;
1234 static bool pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1235 DOM_SID *sid)
1237 GROUP_MAP map;
1239 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1240 return False;
1243 sid_copy(sid, &map.sid);
1244 return True;
1247 static bool pdb_default_sid_to_id(struct pdb_methods *methods,
1248 const DOM_SID *sid,
1249 union unid_t *id, enum lsa_SidType *type)
1251 TALLOC_CTX *mem_ctx;
1252 bool ret = False;
1253 const char *name;
1254 uint32 rid;
1256 mem_ctx = talloc_new(NULL);
1258 if (mem_ctx == NULL) {
1259 DEBUG(0, ("talloc_new failed\n"));
1260 return False;
1263 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1264 /* Here we might have users as well as groups and aliases */
1265 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1266 goto done;
1269 /* check for "Unix User" */
1271 if ( sid_peek_check_rid(&global_sid_Unix_Users, sid, &rid) ) {
1272 id->uid = rid;
1273 *type = SID_NAME_USER;
1274 ret = True;
1275 goto done;
1278 /* check for "Unix Group" */
1280 if ( sid_peek_check_rid(&global_sid_Unix_Groups, sid, &rid) ) {
1281 id->gid = rid;
1282 *type = SID_NAME_ALIAS;
1283 ret = True;
1284 goto done;
1287 /* BUILTIN */
1289 if (sid_check_is_in_builtin(sid) ||
1290 sid_check_is_in_wellknown_domain(sid)) {
1291 /* Here we only have aliases */
1292 GROUP_MAP map;
1293 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1294 DEBUG(10, ("Could not find map for sid %s\n",
1295 sid_string_dbg(sid)));
1296 goto done;
1298 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1299 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1300 DEBUG(10, ("Map for sid %s is a %s, expected an "
1301 "alias\n", sid_string_dbg(sid),
1302 sid_type_lookup(map.sid_name_use)));
1303 goto done;
1306 id->gid = map.gid;
1307 *type = SID_NAME_ALIAS;
1308 ret = True;
1309 goto done;
1312 DEBUG(5, ("Sid %s is neither ours, a Unix SID, nor builtin\n",
1313 sid_string_dbg(sid)));
1315 done:
1317 TALLOC_FREE(mem_ctx);
1318 return ret;
1321 static bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1322 uid_t uid, uid_t **pp_uids, size_t *p_num)
1324 size_t i;
1326 for (i=0; i<*p_num; i++) {
1327 if ((*pp_uids)[i] == uid)
1328 return True;
1331 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1333 if (*pp_uids == NULL)
1334 return False;
1336 (*pp_uids)[*p_num] = uid;
1337 *p_num += 1;
1338 return True;
1341 static bool get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1343 struct group *grp;
1344 char **gr;
1345 struct passwd *pwd;
1346 bool winbind_env;
1347 bool ret = False;
1349 *pp_uids = NULL;
1350 *p_num = 0;
1352 /* We only look at our own sam, so don't care about imported stuff */
1353 winbind_env = winbind_env_set();
1354 (void)winbind_off();
1356 if ((grp = getgrgid(gid)) == NULL) {
1357 /* allow winbindd lookups, but only if they weren't already disabled */
1358 goto done;
1361 /* Primary group members */
1362 setpwent();
1363 while ((pwd = getpwent()) != NULL) {
1364 if (pwd->pw_gid == gid) {
1365 if (!add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1366 pp_uids, p_num)) {
1367 goto done;
1371 endpwent();
1373 /* Secondary group members */
1374 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1375 struct passwd *pw = getpwnam(*gr);
1377 if (pw == NULL)
1378 continue;
1379 if (!add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num)) {
1380 goto done;
1384 ret = True;
1386 done:
1388 /* allow winbindd lookups, but only if they weren't already disabled */
1389 if (!winbind_env) {
1390 (void)winbind_on();
1393 return ret;
1396 static NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1397 TALLOC_CTX *mem_ctx,
1398 const DOM_SID *group,
1399 uint32 **pp_member_rids,
1400 size_t *p_num_members)
1402 gid_t gid;
1403 uid_t *uids;
1404 size_t i, num_uids;
1406 *pp_member_rids = NULL;
1407 *p_num_members = 0;
1409 if (!sid_to_gid(group, &gid))
1410 return NT_STATUS_NO_SUCH_GROUP;
1412 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1413 return NT_STATUS_NO_SUCH_GROUP;
1415 if (num_uids == 0)
1416 return NT_STATUS_OK;
1418 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1420 for (i=0; i<num_uids; i++) {
1421 DOM_SID sid;
1423 uid_to_sid(&sid, uids[i]);
1425 if (!sid_check_is_in_our_domain(&sid)) {
1426 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1427 "in our domain\n"));
1428 continue;
1431 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1432 *p_num_members += 1;
1435 return NT_STATUS_OK;
1438 static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1439 TALLOC_CTX *mem_ctx,
1440 struct samu *user,
1441 DOM_SID **pp_sids,
1442 gid_t **pp_gids,
1443 size_t *p_num_groups)
1445 size_t i;
1446 gid_t gid;
1447 struct passwd *pw;
1448 const char *username = pdb_get_username(user);
1451 /* Ignore the primary group SID. Honor the real Unix primary group.
1452 The primary group SID is only of real use to Windows clients */
1454 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1455 return NT_STATUS_NO_SUCH_USER;
1458 gid = pw->pw_gid;
1460 TALLOC_FREE( pw );
1462 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1463 return NT_STATUS_NO_SUCH_USER;
1466 if (*p_num_groups == 0) {
1467 smb_panic("primary group missing");
1470 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1472 if (*pp_sids == NULL) {
1473 TALLOC_FREE(*pp_gids);
1474 return NT_STATUS_NO_MEMORY;
1477 for (i=0; i<*p_num_groups; i++) {
1478 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1481 return NT_STATUS_OK;
1484 /*******************************************************************
1485 Look up a rid in the SAM we're responsible for (i.e. passdb)
1486 ********************************************************************/
1488 static bool lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1489 const char **name,
1490 enum lsa_SidType *psid_name_use,
1491 union unid_t *unix_id)
1493 struct samu *sam_account = NULL;
1494 GROUP_MAP map;
1495 bool ret;
1496 DOM_SID sid;
1498 *psid_name_use = SID_NAME_UNKNOWN;
1500 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1501 (unsigned int)rid));
1503 sid_copy(&sid, get_global_sam_sid());
1504 sid_append_rid(&sid, rid);
1506 /* see if the passdb can help us with the name of the user */
1508 if ( !(sam_account = samu_new( NULL )) ) {
1509 return False;
1512 /* BEING ROOT BLLOCK */
1513 become_root();
1514 if (pdb_getsampwsid(sam_account, &sid)) {
1515 struct passwd *pw;
1517 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1518 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1519 if (!*name) {
1520 TALLOC_FREE(sam_account);
1521 return False;
1524 *psid_name_use = SID_NAME_USER;
1526 TALLOC_FREE(sam_account);
1528 if (unix_id == NULL) {
1529 return True;
1532 pw = Get_Pwnam_alloc(talloc_tos(), *name);
1533 if (pw == NULL) {
1534 return False;
1536 unix_id->uid = pw->pw_uid;
1537 TALLOC_FREE(pw);
1538 return True;
1540 TALLOC_FREE(sam_account);
1542 ret = pdb_getgrsid(&map, sid);
1543 unbecome_root();
1544 /* END BECOME_ROOT BLOCK */
1546 /* do not resolve SIDs to a name unless there is a valid
1547 gid associated with it */
1549 if ( ret && (map.gid != (gid_t)-1) ) {
1550 *name = talloc_strdup(mem_ctx, map.nt_name);
1551 *psid_name_use = map.sid_name_use;
1553 if ( unix_id ) {
1554 unix_id->gid = map.gid;
1557 return True;
1560 /* Windows will always map RID 513 to something. On a non-domain
1561 controller, this gets mapped to SERVER\None. */
1563 if ( unix_id ) {
1564 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1565 return False;
1568 if ( rid == DOMAIN_GROUP_RID_USERS ) {
1569 *name = talloc_strdup(mem_ctx, "None" );
1570 *psid_name_use = SID_NAME_DOM_GRP;
1572 return True;
1575 return False;
1578 static NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1579 const DOM_SID *domain_sid,
1580 int num_rids,
1581 uint32 *rids,
1582 const char **names,
1583 enum lsa_SidType *attrs)
1585 int i;
1586 NTSTATUS result;
1587 bool have_mapped = False;
1588 bool have_unmapped = False;
1590 if (sid_check_is_builtin(domain_sid)) {
1592 for (i=0; i<num_rids; i++) {
1593 const char *name;
1595 if (lookup_builtin_rid(names, rids[i], &name)) {
1596 attrs[i] = SID_NAME_ALIAS;
1597 names[i] = name;
1598 DEBUG(5,("lookup_rids: %s:%d\n",
1599 names[i], attrs[i]));
1600 have_mapped = True;
1601 } else {
1602 have_unmapped = True;
1603 attrs[i] = SID_NAME_UNKNOWN;
1606 goto done;
1609 /* Should not happen, but better check once too many */
1610 if (!sid_check_is_domain(domain_sid)) {
1611 return NT_STATUS_INVALID_HANDLE;
1614 for (i = 0; i < num_rids; i++) {
1615 const char *name;
1617 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1618 NULL)) {
1619 if (name == NULL) {
1620 return NT_STATUS_NO_MEMORY;
1622 names[i] = name;
1623 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1624 have_mapped = True;
1625 } else {
1626 have_unmapped = True;
1627 attrs[i] = SID_NAME_UNKNOWN;
1631 done:
1633 result = NT_STATUS_NONE_MAPPED;
1635 if (have_mapped)
1636 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1638 return result;
1641 #if 0
1642 static NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1643 const DOM_SID *domain_sid,
1644 int num_names,
1645 const char **names,
1646 uint32 *rids,
1647 enum lsa_SidType *attrs)
1649 int i;
1650 NTSTATUS result;
1651 bool have_mapped = False;
1652 bool have_unmapped = False;
1654 if (sid_check_is_builtin(domain_sid)) {
1656 for (i=0; i<num_names; i++) {
1657 uint32 rid;
1659 if (lookup_builtin_name(names[i], &rid)) {
1660 attrs[i] = SID_NAME_ALIAS;
1661 rids[i] = rid;
1662 DEBUG(5,("lookup_rids: %s:%d\n",
1663 names[i], attrs[i]));
1664 have_mapped = True;
1665 } else {
1666 have_unmapped = True;
1667 attrs[i] = SID_NAME_UNKNOWN;
1670 goto done;
1673 /* Should not happen, but better check once too many */
1674 if (!sid_check_is_domain(domain_sid)) {
1675 return NT_STATUS_INVALID_HANDLE;
1678 for (i = 0; i < num_names; i++) {
1679 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1680 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1681 rids[i], attrs[i]));
1682 have_mapped = True;
1683 } else {
1684 have_unmapped = True;
1685 attrs[i] = SID_NAME_UNKNOWN;
1689 done:
1691 result = NT_STATUS_NONE_MAPPED;
1693 if (have_mapped)
1694 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1696 return result;
1698 #endif
1700 struct pdb_search *pdb_search_init(enum pdb_search_type type)
1702 TALLOC_CTX *mem_ctx;
1703 struct pdb_search *result;
1705 mem_ctx = talloc_init("pdb_search");
1706 if (mem_ctx == NULL) {
1707 DEBUG(0, ("talloc_init failed\n"));
1708 return NULL;
1711 result = TALLOC_P(mem_ctx, struct pdb_search);
1712 if (result == NULL) {
1713 DEBUG(0, ("talloc failed\n"));
1714 return NULL;
1717 result->mem_ctx = mem_ctx;
1718 result->type = type;
1719 result->cache = NULL;
1720 result->num_entries = 0;
1721 result->cache_size = 0;
1722 result->search_ended = False;
1724 /* Segfault appropriately if not initialized */
1725 result->next_entry = NULL;
1726 result->search_end = NULL;
1728 return result;
1731 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1732 uint16 acct_flags,
1733 const char *account_name,
1734 const char *fullname,
1735 const char *description,
1736 struct samr_displayentry *entry)
1738 entry->rid = rid;
1739 entry->acct_flags = acct_flags;
1741 if (account_name != NULL)
1742 entry->account_name = talloc_strdup(mem_ctx, account_name);
1743 else
1744 entry->account_name = "";
1746 if (fullname != NULL)
1747 entry->fullname = talloc_strdup(mem_ctx, fullname);
1748 else
1749 entry->fullname = "";
1751 if (description != NULL)
1752 entry->description = talloc_strdup(mem_ctx, description);
1753 else
1754 entry->description = "";
1757 struct group_search {
1758 GROUP_MAP *groups;
1759 size_t num_groups, current_group;
1762 static bool next_entry_groups(struct pdb_search *s,
1763 struct samr_displayentry *entry)
1765 struct group_search *state = (struct group_search *)s->private_data;
1766 uint32 rid;
1767 GROUP_MAP *map = &state->groups[state->current_group];
1769 if (state->current_group == state->num_groups)
1770 return False;
1772 sid_peek_rid(&map->sid, &rid);
1774 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1775 entry);
1777 state->current_group += 1;
1778 return True;
1781 static void search_end_groups(struct pdb_search *search)
1783 struct group_search *state =
1784 (struct group_search *)search->private_data;
1785 SAFE_FREE(state->groups);
1788 static bool pdb_search_grouptype(struct pdb_search *search,
1789 const DOM_SID *sid, enum lsa_SidType type)
1791 struct group_search *state;
1793 state = TALLOC_P(search->mem_ctx, struct group_search);
1794 if (state == NULL) {
1795 DEBUG(0, ("talloc failed\n"));
1796 return False;
1799 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1800 True)) {
1801 DEBUG(0, ("Could not enum groups\n"));
1802 return False;
1805 state->current_group = 0;
1806 search->private_data = state;
1807 search->next_entry = next_entry_groups;
1808 search->search_end = search_end_groups;
1809 return True;
1812 static bool pdb_default_search_groups(struct pdb_methods *methods,
1813 struct pdb_search *search)
1815 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1818 static bool pdb_default_search_aliases(struct pdb_methods *methods,
1819 struct pdb_search *search,
1820 const DOM_SID *sid)
1823 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1826 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1827 uint32 idx)
1829 if (idx < search->num_entries)
1830 return &search->cache[idx];
1832 if (search->search_ended)
1833 return NULL;
1835 while (idx >= search->num_entries) {
1836 struct samr_displayentry entry;
1838 if (!search->next_entry(search, &entry)) {
1839 search->search_end(search);
1840 search->search_ended = True;
1841 break;
1844 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1845 entry, &search->cache, &search->num_entries,
1846 &search->cache_size);
1849 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1852 struct pdb_search *pdb_search_users(uint32 acct_flags)
1854 struct pdb_methods *pdb = pdb_get_methods();
1855 struct pdb_search *result;
1857 result = pdb_search_init(PDB_USER_SEARCH);
1858 if (result == NULL) {
1859 return NULL;
1862 if (!pdb->search_users(pdb, result, acct_flags)) {
1863 talloc_destroy(result->mem_ctx);
1864 return NULL;
1866 return result;
1869 struct pdb_search *pdb_search_groups(void)
1871 struct pdb_methods *pdb = pdb_get_methods();
1872 struct pdb_search *result;
1874 result = pdb_search_init(PDB_GROUP_SEARCH);
1875 if (result == NULL) {
1876 return NULL;
1879 if (!pdb->search_groups(pdb, result)) {
1880 talloc_destroy(result->mem_ctx);
1881 return NULL;
1883 return result;
1886 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1888 struct pdb_methods *pdb = pdb_get_methods();
1889 struct pdb_search *result;
1891 if (pdb == NULL) return NULL;
1893 result = pdb_search_init(PDB_ALIAS_SEARCH);
1894 if (result == NULL) return NULL;
1896 if (!pdb->search_aliases(pdb, result, sid)) {
1897 talloc_destroy(result->mem_ctx);
1898 return NULL;
1900 return result;
1903 uint32 pdb_search_entries(struct pdb_search *search,
1904 uint32 start_idx, uint32 max_entries,
1905 struct samr_displayentry **result)
1907 struct samr_displayentry *end_entry;
1908 uint32 end_idx = start_idx+max_entries-1;
1910 /* The first entry needs to be searched after the last. Otherwise the
1911 * first entry might have moved due to a realloc during the search for
1912 * the last entry. */
1914 end_entry = pdb_search_getentry(search, end_idx);
1915 *result = pdb_search_getentry(search, start_idx);
1917 if (end_entry != NULL)
1918 return max_entries;
1920 if (start_idx >= search->num_entries)
1921 return 0;
1923 return search->num_entries - start_idx;
1926 void pdb_search_destroy(struct pdb_search *search)
1928 if (search == NULL)
1929 return;
1931 if (!search->search_ended)
1932 search->search_end(search);
1934 talloc_destroy(search->mem_ctx);
1937 /*******************************************************************
1938 trustodm methods
1939 *******************************************************************/
1941 bool pdb_get_trusteddom_pw(const char *domain, char** pwd, DOM_SID *sid,
1942 time_t *pass_last_set_time)
1944 struct pdb_methods *pdb = pdb_get_methods();
1945 return pdb->get_trusteddom_pw(pdb, domain, pwd, sid,
1946 pass_last_set_time);
1949 bool pdb_set_trusteddom_pw(const char* domain, const char* pwd,
1950 const DOM_SID *sid)
1952 struct pdb_methods *pdb = pdb_get_methods();
1953 return pdb->set_trusteddom_pw(pdb, domain, pwd, sid);
1956 bool pdb_del_trusteddom_pw(const char *domain)
1958 struct pdb_methods *pdb = pdb_get_methods();
1959 return pdb->del_trusteddom_pw(pdb, domain);
1962 NTSTATUS pdb_enum_trusteddoms(TALLOC_CTX *mem_ctx, uint32 *num_domains,
1963 struct trustdom_info ***domains)
1965 struct pdb_methods *pdb = pdb_get_methods();
1966 return pdb->enum_trusteddoms(pdb, mem_ctx, num_domains, domains);
1969 /*******************************************************************
1970 the defaults for trustdom methods:
1971 these simply call the original passdb/secrets.c actions,
1972 to be replaced by pdb_ldap.
1973 *******************************************************************/
1975 static bool pdb_default_get_trusteddom_pw(struct pdb_methods *methods,
1976 const char *domain,
1977 char** pwd,
1978 DOM_SID *sid,
1979 time_t *pass_last_set_time)
1981 return secrets_fetch_trusted_domain_password(domain, pwd,
1982 sid, pass_last_set_time);
1986 static bool pdb_default_set_trusteddom_pw(struct pdb_methods *methods,
1987 const char* domain,
1988 const char* pwd,
1989 const DOM_SID *sid)
1991 return secrets_store_trusted_domain_password(domain, pwd, sid);
1994 static bool pdb_default_del_trusteddom_pw(struct pdb_methods *methods,
1995 const char *domain)
1997 return trusted_domain_password_delete(domain);
2000 static NTSTATUS pdb_default_enum_trusteddoms(struct pdb_methods *methods,
2001 TALLOC_CTX *mem_ctx,
2002 uint32 *num_domains,
2003 struct trustdom_info ***domains)
2005 return secrets_trusted_domains(mem_ctx, num_domains, domains);
2008 /*******************************************************************
2009 Create a pdb_methods structure and initialize it with the default
2010 operations. In this way a passdb module can simply implement
2011 the functionality it cares about. However, normally this is done
2012 in groups of related functions.
2013 *******************************************************************/
2015 NTSTATUS make_pdb_method( struct pdb_methods **methods )
2017 /* allocate memory for the structure as its own talloc CTX */
2019 if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
2020 return NT_STATUS_NO_MEMORY;
2023 (*methods)->getsampwnam = pdb_default_getsampwnam;
2024 (*methods)->getsampwsid = pdb_default_getsampwsid;
2025 (*methods)->create_user = pdb_default_create_user;
2026 (*methods)->delete_user = pdb_default_delete_user;
2027 (*methods)->add_sam_account = pdb_default_add_sam_account;
2028 (*methods)->update_sam_account = pdb_default_update_sam_account;
2029 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2030 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2031 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2033 (*methods)->getgrsid = pdb_default_getgrsid;
2034 (*methods)->getgrgid = pdb_default_getgrgid;
2035 (*methods)->getgrnam = pdb_default_getgrnam;
2036 (*methods)->create_dom_group = pdb_default_create_dom_group;
2037 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2038 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2039 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2040 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2041 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2042 (*methods)->enum_group_members = pdb_default_enum_group_members;
2043 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2044 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2045 (*methods)->add_groupmem = pdb_default_add_groupmem;
2046 (*methods)->del_groupmem = pdb_default_del_groupmem;
2047 (*methods)->create_alias = pdb_default_create_alias;
2048 (*methods)->delete_alias = pdb_default_delete_alias;
2049 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2050 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2051 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2052 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2053 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2054 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2055 (*methods)->lookup_rids = pdb_default_lookup_rids;
2056 (*methods)->get_account_policy = pdb_default_get_account_policy;
2057 (*methods)->set_account_policy = pdb_default_set_account_policy;
2058 (*methods)->get_seq_num = pdb_default_get_seq_num;
2059 (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2060 (*methods)->uid_to_sid = pdb_default_uid_to_sid;
2061 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2062 (*methods)->sid_to_id = pdb_default_sid_to_id;
2064 (*methods)->search_groups = pdb_default_search_groups;
2065 (*methods)->search_aliases = pdb_default_search_aliases;
2067 (*methods)->get_trusteddom_pw = pdb_default_get_trusteddom_pw;
2068 (*methods)->set_trusteddom_pw = pdb_default_set_trusteddom_pw;
2069 (*methods)->del_trusteddom_pw = pdb_default_del_trusteddom_pw;
2070 (*methods)->enum_trusteddoms = pdb_default_enum_trusteddoms;
2072 return NT_STATUS_OK;