r16418: Pull in more Klocwork fixes (up to r16415)
[Samba/gbeck.git] / source / passdb / pdb_interface.c
blob5d70c2c1227933904da3da4892bb065467bab773
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_PASSDB
29 /* Cache of latest SAM lookup query */
31 static struct samu *csamuser = NULL;
33 static_decl_pdb;
35 static struct pdb_init_function_entry *backends = NULL;
37 static void lazy_initialize_passdb(void)
39 static BOOL initialized = False;
40 if(initialized) {
41 return;
43 static_init_pdb;
44 initialized = True;
47 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
48 const char **name,
49 enum SID_NAME_USE *psid_name_use,
50 union unid_t *unix_id);
51 /*******************************************************************
52 Clean up uninitialised passwords. The only way to tell
53 that these values are not 'real' is that they do not
54 have a valid last set time. Instead, the value is fixed at 0.
55 Therefore we use that as the key for 'is this a valid password'.
56 However, it is perfectly valid to have a 'default' last change
57 time, such LDAP with a missing attribute would produce.
58 ********************************************************************/
60 static void pdb_force_pw_initialization(struct samu *pass)
62 const uint8 *lm_pwd, *nt_pwd;
64 /* only reset a password if the last set time has been
65 explicitly been set to zero. A default last set time
66 is ignored */
68 if ( (pdb_get_init_flags(pass, PDB_PASSLASTSET) != PDB_DEFAULT)
69 && (pdb_get_pass_last_set_time(pass) == 0) )
72 if (pdb_get_init_flags(pass, PDB_LMPASSWD) != PDB_DEFAULT)
74 lm_pwd = pdb_get_lanman_passwd(pass);
75 if (lm_pwd)
76 pdb_set_lanman_passwd(pass, NULL, PDB_CHANGED);
78 if (pdb_get_init_flags(pass, PDB_NTPASSWD) != PDB_DEFAULT)
80 nt_pwd = pdb_get_nt_passwd(pass);
81 if (nt_pwd)
82 pdb_set_nt_passwd(pass, NULL, PDB_CHANGED);
86 return;
89 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
91 struct pdb_init_function_entry *entry = backends;
93 if(version != PASSDB_INTERFACE_VERSION) {
94 DEBUG(0,("Can't register passdb backend!\n"
95 "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
96 "while this version of samba uses version %d\n",
97 version,PASSDB_INTERFACE_VERSION));
98 return NT_STATUS_OBJECT_TYPE_MISMATCH;
101 if (!name || !init) {
102 return NT_STATUS_INVALID_PARAMETER;
105 DEBUG(5,("Attempting to register passdb backend %s\n", name));
107 /* Check for duplicates */
108 if (pdb_find_backend_entry(name)) {
109 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
110 return NT_STATUS_OBJECT_NAME_COLLISION;
113 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
114 entry->name = smb_xstrdup(name);
115 entry->init = init;
117 DLIST_ADD(backends, entry);
118 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
119 return NT_STATUS_OK;
122 struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
124 struct pdb_init_function_entry *entry = backends;
126 while(entry) {
127 if (strcmp(entry->name, name)==0) return entry;
128 entry = entry->next;
131 return NULL;
134 /******************************************************************
135 Make a pdb_methods from scratch
136 *******************************************************************/
138 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
140 char *module_name = smb_xstrdup(selected);
141 char *module_location = NULL, *p;
142 struct pdb_init_function_entry *entry;
143 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
145 lazy_initialize_passdb();
147 p = strchr(module_name, ':');
149 if (p) {
150 *p = 0;
151 module_location = p+1;
152 trim_char(module_location, ' ', ' ');
155 trim_char(module_name, ' ', ' ');
158 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
160 entry = pdb_find_backend_entry(module_name);
162 /* Try to find a module that contains this module */
163 if (!entry) {
164 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
165 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
166 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
167 SAFE_FREE(module_name);
168 return NT_STATUS_UNSUCCESSFUL;
172 /* No such backend found */
173 if(!entry) {
174 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
175 SAFE_FREE(module_name);
176 return NT_STATUS_INVALID_PARAMETER;
179 DEBUG(5,("Found pdb backend %s\n", module_name));
181 if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
182 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n",
183 selected, nt_errstr(nt_status)));
184 SAFE_FREE(module_name);
185 return nt_status;
188 SAFE_FREE(module_name);
190 DEBUG(5,("pdb backend %s has a valid init\n", selected));
192 return nt_status;
195 /******************************************************************
196 Return an already initialised pdn_methods structure
197 *******************************************************************/
199 static struct pdb_methods *pdb_get_methods_reload( BOOL reload )
201 static struct pdb_methods *pdb = NULL;
203 if ( pdb && reload ) {
204 pdb->free_private_data( &(pdb->private_data) );
205 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
206 pstring msg;
207 slprintf(msg, sizeof(msg)-1, "pdb_get_methods_reload: failed to get pdb methods for backend %s\n",
208 lp_passdb_backend() );
209 smb_panic(msg);
213 if ( !pdb ) {
214 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
215 pstring msg;
216 slprintf(msg, sizeof(msg)-1, "pdb_get_methods_reload: failed to get pdb methods for backend %s\n",
217 lp_passdb_backend() );
218 smb_panic(msg);
222 return pdb;
225 static struct pdb_methods *pdb_get_methods(void)
227 return pdb_get_methods_reload(False);
230 /******************************************************************
231 Backward compatibility functions for the original passdb interface
232 *******************************************************************/
234 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask)
236 struct pdb_methods *pdb = pdb_get_methods();
237 return NT_STATUS_IS_OK(pdb->setsampwent(pdb, update, acb_mask));
240 void pdb_endsampwent(void)
242 struct pdb_methods *pdb = pdb_get_methods();
243 pdb->endsampwent(pdb);
246 BOOL pdb_getsampwent(struct samu *user)
248 struct pdb_methods *pdb = pdb_get_methods();
250 if ( !NT_STATUS_IS_OK(pdb->getsampwent(pdb, user) ) ) {
251 return False;
253 pdb_force_pw_initialization( user );
254 return True;
257 BOOL pdb_getsampwnam(struct samu *sam_acct, const char *username)
259 struct pdb_methods *pdb = pdb_get_methods();
261 if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
262 return False;
265 if ( csamuser ) {
266 TALLOC_FREE(csamuser);
269 pdb_force_pw_initialization( sam_acct );
271 csamuser = samu_new( NULL );
272 if (!csamuser) {
273 return False;
276 if (!pdb_copy_sam_account(csamuser, sam_acct)) {
277 TALLOC_FREE(csamuser);
278 return False;
281 return True;
284 /**********************************************************************
285 **********************************************************************/
287 BOOL guest_user_info( struct samu *user )
289 struct passwd *pwd;
290 NTSTATUS result;
291 const char *guestname = lp_guestaccount();
293 if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
294 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n",
295 guestname));
296 return False;
299 result = samu_set_unix(user, pwd );
301 TALLOC_FREE( pwd );
303 return NT_STATUS_IS_OK( result );
306 /**********************************************************************
307 **********************************************************************/
309 BOOL pdb_getsampwsid(struct samu *sam_acct, const DOM_SID *sid)
311 struct pdb_methods *pdb = pdb_get_methods();
312 uint32 rid;
314 /* hard code the Guest RID of 501 */
316 if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
317 return False;
319 if ( rid == DOMAIN_USER_RID_GUEST ) {
320 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
321 return guest_user_info( sam_acct );
324 /* check the cache first */
326 if ( csamuser && sid_equal(sid, pdb_get_user_sid(csamuser) ) )
327 return pdb_copy_sam_account(sam_acct, csamuser);
329 return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
332 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
333 TALLOC_CTX *tmp_ctx, const char *name,
334 uint32 acb_info, uint32 *rid)
336 struct samu *sam_pass;
337 NTSTATUS status;
338 struct passwd *pwd;
340 if ((sam_pass = samu_new(tmp_ctx)) == NULL) {
341 return NT_STATUS_NO_MEMORY;
344 if ( !(pwd = Get_Pwnam_alloc(tmp_ctx, name)) ) {
345 pstring add_script;
346 int add_ret;
348 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
349 pstrcpy(add_script, lp_adduser_script());
350 } else {
351 pstrcpy(add_script, lp_addmachine_script());
354 if (add_script[0] == '\0') {
355 DEBUG(3, ("Could not find user %s and no add script "
356 "defined\n", name));
357 return NT_STATUS_NO_SUCH_USER;
360 all_string_sub(add_script, "%u", name, sizeof(add_script));
361 add_ret = smbrun(add_script,NULL);
362 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n",
363 add_script, add_ret));
364 flush_pwnam_cache();
366 pwd = Get_Pwnam_alloc(tmp_ctx, name);
369 /* we have a valid SID coming out of this call */
371 status = samu_alloc_rid_unix( sam_pass, pwd );
373 TALLOC_FREE( pwd );
375 if (!NT_STATUS_IS_OK(status)) {
376 DEBUG(3, ("pdb_default_create_user: failed to create a new user structure: %s\n", nt_errstr(status)));
377 return status;
380 if (!sid_peek_check_rid(get_global_sam_sid(),
381 pdb_get_user_sid(sam_pass), rid)) {
382 DEBUG(0, ("Could not get RID of fresh user\n"));
383 return NT_STATUS_INTERNAL_ERROR;
386 /* Disable the account on creation, it does not have a reasonable password yet. */
388 acb_info |= ACB_DISABLED;
390 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
392 status = pdb_add_sam_account(sam_pass);
394 TALLOC_FREE(sam_pass);
396 return status;
399 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
400 uint32 *rid)
402 struct pdb_methods *pdb = pdb_get_methods();
403 return pdb->create_user(pdb, mem_ctx, name, flags, rid);
406 /****************************************************************************
407 Delete a UNIX user on demand.
408 ****************************************************************************/
410 static int smb_delete_user(const char *unix_user)
412 pstring del_script;
413 int ret;
415 /* safety check */
417 if ( strequal( unix_user, "root" ) ) {
418 DEBUG(0,("smb_delete_user: Refusing to delete local system root account!\n"));
419 return -1;
422 pstrcpy(del_script, lp_deluser_script());
423 if (! *del_script)
424 return -1;
425 all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
426 ret = smbrun(del_script,NULL);
427 flush_pwnam_cache();
428 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
430 return ret;
433 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
434 TALLOC_CTX *mem_ctx,
435 struct samu *sam_acct)
437 NTSTATUS status;
439 status = pdb_delete_sam_account(sam_acct);
440 if (!NT_STATUS_IS_OK(status)) {
441 return status;
445 * Now delete the unix side ....
446 * note: we don't check if the delete really happened as the script is
447 * not necessary present and maybe the sysadmin doesn't want to delete
448 * the unix side
450 smb_delete_user( pdb_get_username(sam_acct) );
452 return status;
455 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, struct samu *sam_acct)
457 struct pdb_methods *pdb = pdb_get_methods();
458 uid_t uid = -1;
460 /* sanity check to make sure we don't delete root */
462 if ( !sid_to_uid( pdb_get_user_sid(sam_acct), &uid ) ) {
463 return NT_STATUS_NO_SUCH_USER;
466 if ( uid == 0 ) {
467 return NT_STATUS_ACCESS_DENIED;
470 return pdb->delete_user(pdb, mem_ctx, sam_acct);
473 NTSTATUS pdb_add_sam_account(struct samu *sam_acct)
475 struct pdb_methods *pdb = pdb_get_methods();
476 return pdb->add_sam_account(pdb, sam_acct);
479 NTSTATUS pdb_update_sam_account(struct samu *sam_acct)
481 struct pdb_methods *pdb = pdb_get_methods();
483 if (csamuser != NULL) {
484 TALLOC_FREE(csamuser);
485 csamuser = NULL;
488 return pdb->update_sam_account(pdb, sam_acct);
491 NTSTATUS pdb_delete_sam_account(struct samu *sam_acct)
493 struct pdb_methods *pdb = pdb_get_methods();
495 if (csamuser != NULL) {
496 TALLOC_FREE(csamuser);
497 csamuser = NULL;
500 return pdb->delete_sam_account(pdb, sam_acct);
503 NTSTATUS pdb_rename_sam_account(struct samu *oldname, const char *newname)
505 struct pdb_methods *pdb = pdb_get_methods();
506 uid_t uid;
508 if (csamuser != NULL) {
509 TALLOC_FREE(csamuser);
510 csamuser = NULL;
513 /* sanity check to make sure we don't rename root */
515 if ( !sid_to_uid( pdb_get_user_sid(oldname), &uid ) ) {
516 return NT_STATUS_NO_SUCH_USER;
519 if ( uid == 0 ) {
520 return NT_STATUS_ACCESS_DENIED;
523 return pdb->rename_sam_account(pdb, oldname, newname);
526 NTSTATUS pdb_update_login_attempts(struct samu *sam_acct, BOOL success)
528 struct pdb_methods *pdb = pdb_get_methods();
529 return pdb->update_login_attempts(pdb, sam_acct, success);
532 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
534 struct pdb_methods *pdb = pdb_get_methods();
535 return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
538 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
540 struct pdb_methods *pdb = pdb_get_methods();
541 return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
544 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
546 struct pdb_methods *pdb = pdb_get_methods();
547 return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
550 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
551 TALLOC_CTX *mem_ctx,
552 const char *name,
553 uint32 *rid)
555 DOM_SID group_sid;
556 struct group *grp;
558 grp = getgrnam(name);
560 if (grp == NULL) {
561 gid_t gid;
563 if (smb_create_group(name, &gid) != 0) {
564 return NT_STATUS_ACCESS_DENIED;
567 grp = getgrgid(gid);
570 if (grp == NULL) {
571 return NT_STATUS_ACCESS_DENIED;
574 if (pdb_rid_algorithm()) {
575 *rid = pdb_gid_to_group_rid( grp->gr_gid );
576 } else {
577 if (!pdb_new_rid(rid)) {
578 return NT_STATUS_ACCESS_DENIED;
582 sid_compose(&group_sid, get_global_sam_sid(), *rid);
584 return add_initial_entry(grp->gr_gid, sid_string_static(&group_sid),
585 SID_NAME_DOM_GRP, name, NULL);
588 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
589 uint32 *rid)
591 struct pdb_methods *pdb = pdb_get_methods();
592 return pdb->create_dom_group(pdb, mem_ctx, name, rid);
595 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
596 TALLOC_CTX *mem_ctx,
597 uint32 rid)
599 DOM_SID group_sid;
600 GROUP_MAP map;
601 NTSTATUS status;
602 struct group *grp;
603 const char *grp_name;
605 sid_compose(&group_sid, get_global_sam_sid(), rid);
607 if (!get_domain_group_from_sid(group_sid, &map)) {
608 DEBUG(10, ("Could not find group for rid %d\n", rid));
609 return NT_STATUS_NO_SUCH_GROUP;
612 /* We need the group name for the smb_delete_group later on */
614 if (map.gid == (gid_t)-1) {
615 return NT_STATUS_NO_SUCH_GROUP;
618 grp = getgrgid(map.gid);
619 if (grp == NULL) {
620 return NT_STATUS_NO_SUCH_GROUP;
623 /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
625 grp_name = talloc_strdup(mem_ctx, grp->gr_name);
626 if (grp_name == NULL) {
627 return NT_STATUS_NO_MEMORY;
630 status = pdb_delete_group_mapping_entry(group_sid);
632 if (!NT_STATUS_IS_OK(status)) {
633 return status;
636 /* Don't check the result of smb_delete_group */
638 smb_delete_group(grp_name);
640 return NT_STATUS_OK;
643 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
645 struct pdb_methods *pdb = pdb_get_methods();
646 return pdb->delete_dom_group(pdb, mem_ctx, rid);
649 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
651 struct pdb_methods *pdb = pdb_get_methods();
652 return pdb->add_group_mapping_entry(pdb, map);
655 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
657 struct pdb_methods *pdb = pdb_get_methods();
658 return pdb->update_group_mapping_entry(pdb, map);
661 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
663 struct pdb_methods *pdb = pdb_get_methods();
664 return pdb->delete_group_mapping_entry(pdb, sid);
667 BOOL pdb_enum_group_mapping(const DOM_SID *sid, enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
668 size_t *p_num_entries, BOOL unix_only)
670 struct pdb_methods *pdb = pdb_get_methods();
671 return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid, sid_name_use,
672 pp_rmap, p_num_entries, unix_only));
675 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
676 const DOM_SID *sid,
677 uint32 **pp_member_rids,
678 size_t *p_num_members)
680 struct pdb_methods *pdb = pdb_get_methods();
681 NTSTATUS result;
683 result = pdb->enum_group_members(pdb, mem_ctx,
684 sid, pp_member_rids, p_num_members);
686 /* special check for rid 513 */
688 if ( !NT_STATUS_IS_OK( result ) ) {
689 uint32 rid;
691 sid_peek_rid( sid, &rid );
693 if ( rid == DOMAIN_GROUP_RID_USERS ) {
694 *p_num_members = 0;
695 *pp_member_rids = NULL;
697 return NT_STATUS_OK;
701 return result;
704 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, struct samu *user,
705 DOM_SID **pp_sids, gid_t **pp_gids,
706 size_t *p_num_groups)
708 struct pdb_methods *pdb = pdb_get_methods();
709 return pdb->enum_group_memberships(
710 pdb, mem_ctx, user,
711 pp_sids, pp_gids, p_num_groups);
714 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
715 TALLOC_CTX *mem_ctx,
716 struct samu *sampass)
718 struct group *grp;
719 gid_t gid;
721 if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
722 (grp = getgrgid(gid)) == NULL) {
723 return NT_STATUS_INVALID_PRIMARY_GROUP;
726 if (smb_set_primary_group(grp->gr_name,
727 pdb_get_username(sampass)) != 0) {
728 return NT_STATUS_ACCESS_DENIED;
731 return NT_STATUS_OK;
734 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, struct samu *user)
736 struct pdb_methods *pdb = pdb_get_methods();
737 return pdb->set_unix_primary_group(pdb, mem_ctx, user);
741 * Helper function to see whether a user is in a group. We can't use
742 * user_in_group_sid here because this creates dependencies only smbd can
743 * fulfil.
746 static BOOL pdb_user_in_group(TALLOC_CTX *mem_ctx, struct samu *account,
747 const DOM_SID *group_sid)
749 DOM_SID *sids;
750 gid_t *gids;
751 size_t i, num_groups;
753 if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
754 &sids, &gids,
755 &num_groups))) {
756 return False;
759 for (i=0; i<num_groups; i++) {
760 if (sid_equal(group_sid, &sids[i])) {
761 return True;
764 return False;
767 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
768 TALLOC_CTX *mem_ctx,
769 uint32 group_rid,
770 uint32 member_rid)
772 DOM_SID group_sid, member_sid;
773 struct samu *account = NULL;
774 GROUP_MAP map;
775 struct group *grp;
776 struct passwd *pwd;
777 const char *group_name;
778 uid_t uid;
780 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
781 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
783 if (!get_domain_group_from_sid(group_sid, &map) ||
784 (map.gid == (gid_t)-1) ||
785 ((grp = getgrgid(map.gid)) == NULL)) {
786 return NT_STATUS_NO_SUCH_GROUP;
789 group_name = talloc_strdup(mem_ctx, grp->gr_name);
790 if (group_name == NULL) {
791 return NT_STATUS_NO_MEMORY;
794 if ( !(account = samu_new( NULL )) ) {
795 return NT_STATUS_NO_MEMORY;
798 if (!pdb_getsampwsid(account, &member_sid) ||
799 !sid_to_uid(&member_sid, &uid) ||
800 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
801 return NT_STATUS_NO_SUCH_USER;
804 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
805 return NT_STATUS_MEMBER_IN_GROUP;
809 * ok, the group exist, the user exist, the user is not in the group,
810 * we can (finally) add it to the group !
813 smb_add_user_group(group_name, pwd->pw_name);
815 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
816 return NT_STATUS_ACCESS_DENIED;
819 return NT_STATUS_OK;
822 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
823 uint32 member_rid)
825 struct pdb_methods *pdb = pdb_get_methods();
826 return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
829 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
830 TALLOC_CTX *mem_ctx,
831 uint32 group_rid,
832 uint32 member_rid)
834 DOM_SID group_sid, member_sid;
835 struct samu *account = NULL;
836 GROUP_MAP map;
837 struct group *grp;
838 struct passwd *pwd;
839 const char *group_name;
840 uid_t uid;
842 sid_compose(&group_sid, get_global_sam_sid(), group_rid);
843 sid_compose(&member_sid, get_global_sam_sid(), member_rid);
845 if (!get_domain_group_from_sid(group_sid, &map) ||
846 (map.gid == (gid_t)-1) ||
847 ((grp = getgrgid(map.gid)) == NULL)) {
848 return NT_STATUS_NO_SUCH_GROUP;
851 group_name = talloc_strdup(mem_ctx, grp->gr_name);
852 if (group_name == NULL) {
853 return NT_STATUS_NO_MEMORY;
856 if ( !(account = samu_new( NULL )) ) {
857 return NT_STATUS_NO_MEMORY;
860 if (!pdb_getsampwsid(account, &member_sid) ||
861 !sid_to_uid(&member_sid, &uid) ||
862 ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
863 return NT_STATUS_NO_SUCH_USER;
866 if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
867 return NT_STATUS_MEMBER_NOT_IN_GROUP;
871 * ok, the group exist, the user exist, the user is in the group,
872 * we can (finally) delete it from the group!
875 smb_delete_user_group(group_name, pwd->pw_name);
877 if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
878 return NT_STATUS_ACCESS_DENIED;
881 return NT_STATUS_OK;
884 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
885 uint32 member_rid)
887 struct pdb_methods *pdb = pdb_get_methods();
888 return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
891 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
893 struct pdb_methods *pdb = pdb_get_methods();
894 return NT_STATUS_IS_OK(pdb->find_alias(pdb, name, sid));
897 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
899 struct pdb_methods *pdb = pdb_get_methods();
900 return pdb->create_alias(pdb, name, rid);
903 BOOL pdb_delete_alias(const DOM_SID *sid)
905 struct pdb_methods *pdb = pdb_get_methods();
906 return NT_STATUS_IS_OK(pdb->delete_alias(pdb, sid));
910 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
912 struct pdb_methods *pdb = pdb_get_methods();
913 return NT_STATUS_IS_OK(pdb->get_aliasinfo(pdb, sid, info));
916 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
918 struct pdb_methods *pdb = pdb_get_methods();
919 return NT_STATUS_IS_OK(pdb->set_aliasinfo(pdb, sid, info));
922 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
924 struct pdb_methods *pdb = pdb_get_methods();
925 return pdb->add_aliasmem(pdb, alias, member);
928 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
930 struct pdb_methods *pdb = pdb_get_methods();
931 return pdb->del_aliasmem(pdb, alias, member);
934 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
935 DOM_SID **pp_members, size_t *p_num_members)
937 struct pdb_methods *pdb = pdb_get_methods();
938 return pdb->enum_aliasmem(pdb, alias, pp_members, p_num_members);
941 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
942 const DOM_SID *domain_sid,
943 const DOM_SID *members, size_t num_members,
944 uint32 **pp_alias_rids,
945 size_t *p_num_alias_rids)
947 struct pdb_methods *pdb = pdb_get_methods();
948 return pdb->enum_alias_memberships(pdb, mem_ctx,
949 domain_sid,
950 members, num_members,
951 pp_alias_rids,
952 p_num_alias_rids);
955 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
956 int num_rids,
957 uint32 *rids,
958 const char **names,
959 uint32 *attrs)
961 struct pdb_methods *pdb = pdb_get_methods();
962 return pdb->lookup_rids(pdb, domain_sid,
963 num_rids, rids, names, attrs);
966 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
967 int num_names,
968 const char **names,
969 uint32 *rids,
970 uint32 *attrs)
972 struct pdb_methods *pdb = pdb_get_methods();
973 return pdb->lookup_names(pdb, domain_sid,
974 num_names, names, rids, attrs);
977 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
979 struct pdb_methods *pdb = pdb_get_methods();
980 return NT_STATUS_IS_OK(pdb->get_account_policy(pdb, policy_index, value));
983 BOOL pdb_set_account_policy(int policy_index, uint32 value)
985 struct pdb_methods *pdb = pdb_get_methods();
986 return NT_STATUS_IS_OK(pdb->set_account_policy(pdb, policy_index, value));
989 BOOL pdb_get_seq_num(time_t *seq_num)
991 struct pdb_methods *pdb = pdb_get_methods();
992 return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
995 BOOL pdb_uid_to_rid(uid_t uid, uint32 *rid)
997 struct pdb_methods *pdb = pdb_get_methods();
998 return pdb->uid_to_rid(pdb, uid, rid);
1001 BOOL pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1003 struct pdb_methods *pdb = pdb_get_methods();
1004 return pdb->gid_to_sid(pdb, gid, sid);
1007 BOOL pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1008 enum SID_NAME_USE *type)
1010 struct pdb_methods *pdb = pdb_get_methods();
1011 return pdb->sid_to_id(pdb, sid, id, type);
1014 BOOL pdb_rid_algorithm(void)
1016 struct pdb_methods *pdb = pdb_get_methods();
1017 return pdb->rid_algorithm(pdb);
1020 /********************************************************************
1021 Allocate a new RID from the passdb backend. Verify that it is free
1022 by calling lookup_global_sam_rid() to verify that the RID is not
1023 in use. This handles servers that have existing users or groups
1024 with add RIDs (assigned from previous algorithmic mappings)
1025 ********************************************************************/
1027 BOOL pdb_new_rid(uint32 *rid)
1029 struct pdb_methods *pdb = pdb_get_methods();
1030 const char *name = NULL;
1031 enum SID_NAME_USE type;
1032 uint32 allocated_rid = 0;
1033 int i;
1034 TALLOC_CTX *ctx;
1036 if (pdb_rid_algorithm()) {
1037 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1038 "are active\n"));
1039 return False;
1042 if (algorithmic_rid_base() != BASE_RID) {
1043 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1044 "without algorithmic RIDs is chosen.\n"));
1045 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1046 "add', set the maximum used RID using\n"));
1047 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1048 return False;
1051 if ( (ctx = talloc_init("pdb_new_rid")) == NULL ) {
1052 DEBUG(0,("pdb_new_rid: Talloc initialization failure\n"));
1053 return False;
1056 /* Attempt to get an unused RID (max tires is 250...yes that it is
1057 and arbitrary number I pulkled out of my head). -- jerry */
1059 for ( i=0; allocated_rid==0 && i<250; i++ ) {
1060 /* get a new RID */
1062 if ( !pdb->new_rid(pdb, &allocated_rid) ) {
1063 return False;
1066 /* validate that the RID is not in use */
1068 if ( lookup_global_sam_rid( ctx, allocated_rid, &name, &type, NULL ) ) {
1069 allocated_rid = 0;
1073 TALLOC_FREE( ctx );
1075 if ( allocated_rid == 0 ) {
1076 DEBUG(0,("pdb_new_rid: Failed to find unused RID\n"));
1077 return False;
1080 *rid = allocated_rid;
1082 return True;
1085 /***************************************************************
1086 Initialize the static context (at smbd startup etc).
1088 If uninitialised, context will auto-init on first use.
1089 ***************************************************************/
1091 BOOL initialize_password_db(BOOL reload)
1093 return (pdb_get_methods_reload(reload) != NULL);
1097 /***************************************************************************
1098 Default implementations of some functions.
1099 ****************************************************************************/
1101 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, struct samu *user, const char *sname)
1103 return NT_STATUS_NO_SUCH_USER;
1106 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, struct samu * user, const DOM_SID *sid)
1108 return NT_STATUS_NO_SUCH_USER;
1111 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1113 return NT_STATUS_NOT_IMPLEMENTED;
1116 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, struct samu *newpwd)
1118 return NT_STATUS_NOT_IMPLEMENTED;
1121 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, struct samu *pwd)
1123 return NT_STATUS_NOT_IMPLEMENTED;
1126 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, struct samu *pwd, const char *newname)
1128 return NT_STATUS_NOT_IMPLEMENTED;
1131 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, struct samu *newpwd, BOOL success)
1133 return NT_STATUS_OK;
1136 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint32 acb_mask)
1138 return NT_STATUS_NOT_IMPLEMENTED;
1141 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, struct samu *user)
1143 return NT_STATUS_NOT_IMPLEMENTED;
1146 static void pdb_default_endsampwent(struct pdb_methods *methods)
1148 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1151 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1153 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1156 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1158 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1161 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1163 *seq_num = time(NULL);
1164 return NT_STATUS_OK;
1167 static BOOL pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1168 uint32 *rid)
1170 struct samu *sampw = NULL;
1171 struct passwd *unix_pw;
1172 BOOL ret;
1174 unix_pw = sys_getpwuid( uid );
1176 if ( !unix_pw ) {
1177 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1178 "%lu\n", (unsigned long)uid));
1179 return False;
1182 if ( !(sampw = samu_new( NULL )) ) {
1183 DEBUG(0,("pdb_default_uid_to_rid: samu_new() failed!\n"));
1184 return False;
1187 become_root();
1188 ret = NT_STATUS_IS_OK(
1189 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1190 unbecome_root();
1192 if (!ret) {
1193 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1194 "%s (%d)\n", unix_pw->pw_name, uid));
1195 TALLOC_FREE(sampw);
1196 return False;
1199 ret = sid_peek_check_rid(get_global_sam_sid(),
1200 pdb_get_user_sid(sampw), rid);
1202 if (!ret) {
1203 DEBUG(1, ("Could not peek rid out of sid %s\n",
1204 sid_string_static(pdb_get_user_sid(sampw))));
1207 TALLOC_FREE(sampw);
1208 return ret;
1211 static BOOL pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1212 DOM_SID *sid)
1214 GROUP_MAP map;
1216 if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1217 return False;
1220 sid_copy(sid, &map.sid);
1221 return True;
1224 static BOOL pdb_default_sid_to_id(struct pdb_methods *methods,
1225 const DOM_SID *sid,
1226 union unid_t *id, enum SID_NAME_USE *type)
1228 TALLOC_CTX *mem_ctx;
1229 BOOL ret = False;
1230 const char *name;
1231 uint32 rid;
1233 mem_ctx = talloc_new(NULL);
1235 if (mem_ctx == NULL) {
1236 DEBUG(0, ("talloc_new failed\n"));
1237 return False;
1240 if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1241 /* Here we might have users as well as groups and aliases */
1242 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1243 goto done;
1246 if (sid_peek_check_rid(&global_sid_Builtin, sid, &rid)) {
1247 /* Here we only have aliases */
1248 GROUP_MAP map;
1249 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1250 DEBUG(10, ("Could not find map for sid %s\n",
1251 sid_string_static(sid)));
1252 goto done;
1254 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1255 (map.sid_name_use != SID_NAME_WKN_GRP)) {
1256 DEBUG(10, ("Map for sid %s is a %s, expected an "
1257 "alias\n", sid_string_static(sid),
1258 sid_type_lookup(map.sid_name_use)));
1259 goto done;
1262 id->gid = map.gid;
1263 *type = SID_NAME_ALIAS;
1264 ret = True;
1265 goto done;
1268 DEBUG(5, ("Sid %s is neither ours nor builtin, don't know it\n",
1269 sid_string_static(sid)));
1271 done:
1273 TALLOC_FREE(mem_ctx);
1274 return ret;
1277 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1278 uid_t uid, uid_t **pp_uids, size_t *p_num)
1280 size_t i;
1282 for (i=0; i<*p_num; i++) {
1283 if ((*pp_uids)[i] == uid)
1284 return;
1287 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1289 if (*pp_uids == NULL)
1290 return;
1292 (*pp_uids)[*p_num] = uid;
1293 *p_num += 1;
1296 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1298 struct group *grp;
1299 char **gr;
1300 struct passwd *pwd;
1301 char *winbindd_env;
1303 *pp_uids = NULL;
1304 *p_num = 0;
1306 /* We only look at our own sam, so don't care about imported stuff */
1308 winbindd_env = getenv(WINBINDD_DONT_ENV);
1309 winbind_off();
1311 if ((grp = getgrgid(gid)) == NULL) {
1312 /* allow winbindd lookups, but only if they weren't already disabled */
1313 if ( !(winbindd_env && strequal(winbindd_env, "1")) ) {
1314 winbind_on();
1317 return False;
1320 /* Primary group members */
1322 setpwent();
1323 while ((pwd = getpwent()) != NULL) {
1324 if (pwd->pw_gid == gid) {
1325 add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1326 pp_uids, p_num);
1329 endpwent();
1331 /* Secondary group members */
1333 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1334 struct passwd *pw = getpwnam(*gr);
1336 if (pw == NULL)
1337 continue;
1338 add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num);
1341 /* allow winbindd lookups, but only if they weren't already disabled */
1343 if ( !(winbindd_env && strequal(winbindd_env, "1")) ) {
1344 winbind_on();
1347 return True;
1350 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1351 TALLOC_CTX *mem_ctx,
1352 const DOM_SID *group,
1353 uint32 **pp_member_rids,
1354 size_t *p_num_members)
1356 gid_t gid;
1357 uid_t *uids;
1358 size_t i, num_uids;
1360 *pp_member_rids = NULL;
1361 *p_num_members = 0;
1363 if (!sid_to_gid(group, &gid))
1364 return NT_STATUS_NO_SUCH_GROUP;
1366 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1367 return NT_STATUS_NO_SUCH_GROUP;
1369 if (num_uids == 0)
1370 return NT_STATUS_OK;
1372 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1374 for (i=0; i<num_uids; i++) {
1375 DOM_SID sid;
1377 uid_to_sid(&sid, uids[i]);
1379 if (!sid_check_is_in_our_domain(&sid)) {
1380 DEBUG(5, ("Inconsistent SAM -- group member uid not "
1381 "in our domain\n"));
1382 continue;
1385 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1386 *p_num_members += 1;
1389 return NT_STATUS_OK;
1392 NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1393 TALLOC_CTX *mem_ctx,
1394 struct samu *user,
1395 DOM_SID **pp_sids,
1396 gid_t **pp_gids,
1397 size_t *p_num_groups)
1399 size_t i;
1400 gid_t gid;
1401 struct passwd *pw;
1402 const char *username = pdb_get_username(user);
1405 /* Ignore the primary group SID. Honor the real Unix primary group.
1406 The primary group SID is only of real use to Windows clients */
1408 if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
1409 return NT_STATUS_NO_SUCH_USER;
1412 gid = pw->pw_gid;
1414 TALLOC_FREE( pw );
1416 if (!getgroups_unix_user(mem_ctx, username, gid, pp_gids, p_num_groups)) {
1417 return NT_STATUS_NO_SUCH_USER;
1420 if (*p_num_groups == 0) {
1421 smb_panic("primary group missing");
1424 *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1426 if (*pp_sids == NULL) {
1427 TALLOC_FREE(*pp_gids);
1428 return NT_STATUS_NO_MEMORY;
1431 for (i=0; i<*p_num_groups; i++) {
1432 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1435 return NT_STATUS_OK;
1438 /*******************************************************************
1439 Look up a rid in the SAM we're responsible for (i.e. passdb)
1440 ********************************************************************/
1442 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1443 const char **name,
1444 enum SID_NAME_USE *psid_name_use,
1445 union unid_t *unix_id)
1447 struct samu *sam_account = NULL;
1448 GROUP_MAP map;
1449 BOOL ret;
1450 DOM_SID sid;
1452 *psid_name_use = SID_NAME_UNKNOWN;
1454 DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1455 (unsigned int)rid));
1457 sid_copy(&sid, get_global_sam_sid());
1458 sid_append_rid(&sid, rid);
1460 /* see if the passdb can help us with the name of the user */
1462 if ( !(sam_account = samu_new( NULL )) ) {
1463 return False;
1466 /* BEING ROOT BLLOCK */
1467 become_root();
1468 if (pdb_getsampwsid(sam_account, &sid)) {
1469 struct passwd *pw;
1471 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
1472 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1473 if (!*name) {
1474 TALLOC_FREE(sam_account);
1475 return False;
1478 *psid_name_use = SID_NAME_USER;
1480 TALLOC_FREE(sam_account);
1482 if (unix_id == NULL) {
1483 return True;
1486 pw = Get_Pwnam(*name);
1487 if (pw == NULL) {
1488 return False;
1490 unix_id->uid = pw->pw_uid;
1491 return True;
1493 TALLOC_FREE(sam_account);
1495 ret = pdb_getgrsid(&map, sid);
1496 unbecome_root();
1497 /* END BECOME_ROOT BLOCK */
1499 /* do not resolve SIDs to a name unless there is a valid
1500 gid associated with it */
1502 if ( ret && (map.gid != (gid_t)-1) ) {
1503 *name = talloc_strdup(mem_ctx, map.nt_name);
1504 *psid_name_use = map.sid_name_use;
1506 if ( unix_id ) {
1507 unix_id->gid = map.gid;
1510 return True;
1513 /* Windows will always map RID 513 to something. On a non-domain
1514 controller, this gets mapped to SERVER\None. */
1516 if ( unix_id ) {
1517 DEBUG(5, ("Can't find a unix id for an unmapped group\n"));
1518 return False;
1521 if ( rid == DOMAIN_GROUP_RID_USERS ) {
1522 *name = talloc_strdup(mem_ctx, "None" );
1523 *psid_name_use = SID_NAME_DOM_GRP;
1525 return True;
1528 return False;
1531 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1532 const DOM_SID *domain_sid,
1533 int num_rids,
1534 uint32 *rids,
1535 const char **names,
1536 uint32 *attrs)
1538 int i;
1539 NTSTATUS result;
1540 BOOL have_mapped = False;
1541 BOOL have_unmapped = False;
1543 if (sid_check_is_builtin(domain_sid)) {
1545 for (i=0; i<num_rids; i++) {
1546 const char *name;
1548 if (lookup_builtin_rid(names, rids[i], &name)) {
1549 attrs[i] = SID_NAME_ALIAS;
1550 names[i] = name;
1551 DEBUG(5,("lookup_rids: %s:%d\n",
1552 names[i], attrs[i]));
1553 have_mapped = True;
1554 } else {
1555 have_unmapped = True;
1556 attrs[i] = SID_NAME_UNKNOWN;
1559 goto done;
1562 /* Should not happen, but better check once too many */
1563 if (!sid_check_is_domain(domain_sid)) {
1564 return NT_STATUS_INVALID_HANDLE;
1567 for (i = 0; i < num_rids; i++) {
1568 const char *name;
1570 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1571 NULL)) {
1572 if (name == NULL) {
1573 return NT_STATUS_NO_MEMORY;
1575 names[i] = name;
1576 DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1577 have_mapped = True;
1578 } else {
1579 have_unmapped = True;
1580 attrs[i] = SID_NAME_UNKNOWN;
1584 done:
1586 result = NT_STATUS_NONE_MAPPED;
1588 if (have_mapped)
1589 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1591 return result;
1594 NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1595 const DOM_SID *domain_sid,
1596 int num_names,
1597 const char **names,
1598 uint32 *rids,
1599 uint32 *attrs)
1601 int i;
1602 NTSTATUS result;
1603 BOOL have_mapped = False;
1604 BOOL have_unmapped = False;
1606 if (sid_check_is_builtin(domain_sid)) {
1608 for (i=0; i<num_names; i++) {
1609 uint32 rid;
1611 if (lookup_builtin_name(names[i], &rid)) {
1612 attrs[i] = SID_NAME_ALIAS;
1613 rids[i] = rid;
1614 DEBUG(5,("lookup_rids: %s:%d\n",
1615 names[i], attrs[i]));
1616 have_mapped = True;
1617 } else {
1618 have_unmapped = True;
1619 attrs[i] = SID_NAME_UNKNOWN;
1622 goto done;
1625 /* Should not happen, but better check once too many */
1626 if (!sid_check_is_domain(domain_sid)) {
1627 return NT_STATUS_INVALID_HANDLE;
1630 for (i = 0; i < num_names; i++) {
1631 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1632 DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1633 rids[i], attrs[i]));
1634 have_mapped = True;
1635 } else {
1636 have_unmapped = True;
1637 attrs[i] = SID_NAME_UNKNOWN;
1641 done:
1643 result = NT_STATUS_NONE_MAPPED;
1645 if (have_mapped)
1646 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1648 return result;
1651 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1653 TALLOC_CTX *mem_ctx;
1654 struct pdb_search *result;
1656 mem_ctx = talloc_init("pdb_search");
1657 if (mem_ctx == NULL) {
1658 DEBUG(0, ("talloc_init failed\n"));
1659 return NULL;
1662 result = TALLOC_P(mem_ctx, struct pdb_search);
1663 if (result == NULL) {
1664 DEBUG(0, ("talloc failed\n"));
1665 return NULL;
1668 result->mem_ctx = mem_ctx;
1669 result->type = type;
1670 result->cache = NULL;
1671 result->num_entries = 0;
1672 result->cache_size = 0;
1673 result->search_ended = False;
1675 /* Segfault appropriately if not initialized */
1676 result->next_entry = NULL;
1677 result->search_end = NULL;
1679 return result;
1682 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1683 uint16 acct_flags,
1684 const char *account_name,
1685 const char *fullname,
1686 const char *description,
1687 struct samr_displayentry *entry)
1689 entry->rid = rid;
1690 entry->acct_flags = acct_flags;
1692 if (account_name != NULL)
1693 entry->account_name = talloc_strdup(mem_ctx, account_name);
1694 else
1695 entry->account_name = "";
1697 if (fullname != NULL)
1698 entry->fullname = talloc_strdup(mem_ctx, fullname);
1699 else
1700 entry->fullname = "";
1702 if (description != NULL)
1703 entry->description = talloc_strdup(mem_ctx, description);
1704 else
1705 entry->description = "";
1708 static BOOL user_search_in_progress = False;
1709 struct user_search {
1710 uint16 acct_flags;
1713 static BOOL next_entry_users(struct pdb_search *s,
1714 struct samr_displayentry *entry)
1716 struct user_search *state = s->private_data;
1717 struct samu *user = NULL;
1719 next:
1720 if ( !(user = samu_new( NULL )) ) {
1721 DEBUG(0, ("next_entry_users: samu_new() failed!\n"));
1722 return False;
1725 if (!pdb_getsampwent(user)) {
1726 TALLOC_FREE(user);
1727 return False;
1730 if ((state->acct_flags != 0) &&
1731 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1732 TALLOC_FREE(user);
1733 goto next;
1736 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1737 pdb_get_acct_ctrl(user), pdb_get_username(user),
1738 pdb_get_fullname(user), pdb_get_acct_desc(user),
1739 entry);
1741 TALLOC_FREE(user);
1742 return True;
1745 static void search_end_users(struct pdb_search *search)
1747 pdb_endsampwent();
1748 user_search_in_progress = False;
1751 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1752 struct pdb_search *search,
1753 uint32 acct_flags)
1755 struct user_search *state;
1757 if (user_search_in_progress) {
1758 DEBUG(1, ("user search in progress\n"));
1759 return False;
1762 if (!pdb_setsampwent(False, acct_flags)) {
1763 DEBUG(5, ("Could not start search\n"));
1764 return False;
1767 user_search_in_progress = True;
1769 state = TALLOC_P(search->mem_ctx, struct user_search);
1770 if (state == NULL) {
1771 DEBUG(0, ("talloc failed\n"));
1772 return False;
1775 state->acct_flags = acct_flags;
1777 search->private_data = state;
1778 search->next_entry = next_entry_users;
1779 search->search_end = search_end_users;
1780 return True;
1783 struct group_search {
1784 GROUP_MAP *groups;
1785 size_t num_groups, current_group;
1788 static BOOL next_entry_groups(struct pdb_search *s,
1789 struct samr_displayentry *entry)
1791 struct group_search *state = s->private_data;
1792 uint32 rid;
1793 GROUP_MAP *map = &state->groups[state->current_group];
1795 if (state->current_group == state->num_groups)
1796 return False;
1798 sid_peek_rid(&map->sid, &rid);
1800 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1801 entry);
1803 state->current_group += 1;
1804 return True;
1807 static void search_end_groups(struct pdb_search *search)
1809 struct group_search *state = search->private_data;
1810 SAFE_FREE(state->groups);
1813 static BOOL pdb_search_grouptype(struct pdb_search *search,
1814 const DOM_SID *sid, enum SID_NAME_USE type)
1816 struct group_search *state;
1818 state = TALLOC_P(search->mem_ctx, struct group_search);
1819 if (state == NULL) {
1820 DEBUG(0, ("talloc failed\n"));
1821 return False;
1824 if (!pdb_enum_group_mapping(sid, type, &state->groups, &state->num_groups,
1825 True)) {
1826 DEBUG(0, ("Could not enum groups\n"));
1827 return False;
1830 state->current_group = 0;
1831 search->private_data = state;
1832 search->next_entry = next_entry_groups;
1833 search->search_end = search_end_groups;
1834 return True;
1837 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1838 struct pdb_search *search)
1840 return pdb_search_grouptype(search, get_global_sam_sid(), SID_NAME_DOM_GRP);
1843 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1844 struct pdb_search *search,
1845 const DOM_SID *sid)
1848 return pdb_search_grouptype(search, sid, SID_NAME_ALIAS);
1851 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1852 uint32 idx)
1854 if (idx < search->num_entries)
1855 return &search->cache[idx];
1857 if (search->search_ended)
1858 return NULL;
1860 while (idx >= search->num_entries) {
1861 struct samr_displayentry entry;
1863 if (!search->next_entry(search, &entry)) {
1864 search->search_end(search);
1865 search->search_ended = True;
1866 break;
1869 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1870 entry, &search->cache, &search->num_entries,
1871 &search->cache_size);
1874 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1877 struct pdb_search *pdb_search_users(uint32 acct_flags)
1879 struct pdb_methods *pdb = pdb_get_methods();
1880 struct pdb_search *result;
1882 result = pdb_search_init(PDB_USER_SEARCH);
1883 if (result == NULL) {
1884 return NULL;
1887 if (!pdb->search_users(pdb, result, acct_flags)) {
1888 talloc_destroy(result->mem_ctx);
1889 return NULL;
1891 return result;
1894 struct pdb_search *pdb_search_groups(void)
1896 struct pdb_methods *pdb = pdb_get_methods();
1897 struct pdb_search *result;
1899 result = pdb_search_init(PDB_GROUP_SEARCH);
1900 if (result == NULL) {
1901 return NULL;
1904 if (!pdb->search_groups(pdb, result)) {
1905 talloc_destroy(result->mem_ctx);
1906 return NULL;
1908 return result;
1911 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1913 struct pdb_methods *pdb = pdb_get_methods();
1914 struct pdb_search *result;
1916 if (pdb == NULL) return NULL;
1918 result = pdb_search_init(PDB_ALIAS_SEARCH);
1919 if (result == NULL) return NULL;
1921 if (!pdb->search_aliases(pdb, result, sid)) {
1922 talloc_destroy(result->mem_ctx);
1923 return NULL;
1925 return result;
1928 uint32 pdb_search_entries(struct pdb_search *search,
1929 uint32 start_idx, uint32 max_entries,
1930 struct samr_displayentry **result)
1932 struct samr_displayentry *end_entry;
1933 uint32 end_idx = start_idx+max_entries-1;
1935 /* The first entry needs to be searched after the last. Otherwise the
1936 * first entry might have moved due to a realloc during the search for
1937 * the last entry. */
1939 end_entry = pdb_search_getentry(search, end_idx);
1940 *result = pdb_search_getentry(search, start_idx);
1942 if (end_entry != NULL)
1943 return max_entries;
1945 if (start_idx >= search->num_entries)
1946 return 0;
1948 return search->num_entries - start_idx;
1951 void pdb_search_destroy(struct pdb_search *search)
1953 if (search == NULL)
1954 return;
1956 if (!search->search_ended)
1957 search->search_end(search);
1959 talloc_destroy(search->mem_ctx);
1962 /*******************************************************************
1963 Create a pdb_methods structure and initialize it with the default
1964 operations. In this way a passdb module can simply implement
1965 the functionality it cares about. However, normally this is done
1966 in groups of related functions.
1967 *******************************************************************/
1969 NTSTATUS make_pdb_method( struct pdb_methods **methods )
1971 /* allocate memory for the structure as its own talloc CTX */
1973 if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
1974 return NT_STATUS_NO_MEMORY;
1977 (*methods)->setsampwent = pdb_default_setsampwent;
1978 (*methods)->endsampwent = pdb_default_endsampwent;
1979 (*methods)->getsampwent = pdb_default_getsampwent;
1980 (*methods)->getsampwnam = pdb_default_getsampwnam;
1981 (*methods)->getsampwsid = pdb_default_getsampwsid;
1982 (*methods)->create_user = pdb_default_create_user;
1983 (*methods)->delete_user = pdb_default_delete_user;
1984 (*methods)->add_sam_account = pdb_default_add_sam_account;
1985 (*methods)->update_sam_account = pdb_default_update_sam_account;
1986 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
1987 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
1988 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
1990 (*methods)->getgrsid = pdb_default_getgrsid;
1991 (*methods)->getgrgid = pdb_default_getgrgid;
1992 (*methods)->getgrnam = pdb_default_getgrnam;
1993 (*methods)->create_dom_group = pdb_default_create_dom_group;
1994 (*methods)->delete_dom_group = pdb_default_delete_dom_group;
1995 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
1996 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
1997 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
1998 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
1999 (*methods)->enum_group_members = pdb_default_enum_group_members;
2000 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2001 (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2002 (*methods)->add_groupmem = pdb_default_add_groupmem;
2003 (*methods)->del_groupmem = pdb_default_del_groupmem;
2004 (*methods)->find_alias = pdb_default_find_alias;
2005 (*methods)->create_alias = pdb_default_create_alias;
2006 (*methods)->delete_alias = pdb_default_delete_alias;
2007 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2008 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2009 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2010 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2011 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2012 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2013 (*methods)->lookup_rids = pdb_default_lookup_rids;
2014 (*methods)->get_account_policy = pdb_default_get_account_policy;
2015 (*methods)->set_account_policy = pdb_default_set_account_policy;
2016 (*methods)->get_seq_num = pdb_default_get_seq_num;
2017 (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2018 (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2019 (*methods)->sid_to_id = pdb_default_sid_to_id;
2021 (*methods)->search_users = pdb_default_search_users;
2022 (*methods)->search_groups = pdb_default_search_groups;
2023 (*methods)->search_aliases = pdb_default_search_aliases;
2025 return NT_STATUS_OK;