7ecfa7d4c3ce139783f32780555736b0595b2ef0
[sfrench/samba-autobuild/.git] / source3 / passdb / pdb_guest.c
1 /*
2  * 'Guest' password backend for samba
3  * Copyright (C) Jelmer Vernooij 2002
4  * Copyright (C) Andrew Bartlett 2003
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  * 
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc., 675
18  * Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22
23 /******************************************************************
24   Lookup a name in the SAM database
25  ******************************************************************/
26
27 static NTSTATUS guestsam_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
28 {
29         NTSTATUS nt_status;
30         struct passwd *pass;
31         const char *guest_account = lp_guestaccount();
32         if (!(guest_account && *guest_account)) {
33                 DEBUG(1, ("NULL guest account!?!?\n"));
34                 return NT_STATUS_UNSUCCESSFUL;
35         }
36
37         if (!methods) {
38                 DEBUG(0,("invalid methods\n"));
39                 return NT_STATUS_UNSUCCESSFUL;
40         }
41         if (!sname) {
42                 DEBUG(0,("invalid name specified"));
43                 return NT_STATUS_UNSUCCESSFUL;
44         }
45
46         if (!strequal(guest_account, sname)) {
47                 return NT_STATUS_NO_SUCH_USER;
48         }
49                 
50         pass = getpwnam_alloc(guest_account);
51
52         nt_status = pdb_fill_sam_pw(user, pass);
53
54         passwd_free(&pass);
55         return nt_status;
56 }
57
58
59 /***************************************************************************
60   Search by rid
61  **************************************************************************/
62
63 static NTSTATUS guestsam_getsampwrid (struct pdb_methods *methods, 
64                                  SAM_ACCOUNT *user, uint32 rid)
65 {
66         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
67         struct passwd *pass = NULL;
68         const char *guest_account = lp_guestaccount();
69         if (!(guest_account && *guest_account)) {
70                 DEBUG(1, ("NULL guest account!?!?\n"));
71                 return nt_status;
72         }
73
74         if (!methods) {
75                 DEBUG(0,("invalid methods\n"));
76                 return nt_status;
77         }
78         
79         if (rid == DOMAIN_USER_RID_GUEST) {
80                 pass = getpwnam_alloc(guest_account);
81                 if (!pass) {
82                         DEBUG(1, ("guest account %s does not seem to exist...\n", guest_account));
83                         return NT_STATUS_NO_SUCH_USER;
84                 }
85         } else {
86                 return NT_STATUS_NO_SUCH_USER;
87         }
88
89         nt_status = pdb_fill_sam_pw(user, pass);
90         passwd_free(&pass);
91
92         return nt_status;
93 }
94
95 static NTSTATUS guestsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
96 {
97         uint32 rid;
98         if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid))
99                 return NT_STATUS_NO_SUCH_USER;
100         return guestsam_getsampwrid(my_methods, user, rid);
101 }
102
103
104 NTSTATUS pdb_init_guestsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
105 {
106         NTSTATUS nt_status;
107         
108         if (!pdb_context) {
109                 DEBUG(0, ("invalid pdb_context specified\n"));
110                 return NT_STATUS_UNSUCCESSFUL;
111         }
112
113         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
114                 return nt_status;
115         }
116         
117         (*pdb_method)->name = "guestsam";
118         
119         (*pdb_method)->getsampwnam = guestsam_getsampwnam;
120         (*pdb_method)->getsampwsid = guestsam_getsampwsid;
121         
122         /* we should do no group mapping here */
123         (*pdb_method)->getgrsid = pdb_nop_getgrsid;
124         (*pdb_method)->getgrgid = pdb_nop_getgrgid;
125         (*pdb_method)->getgrnam = pdb_nop_getgrnam;
126         (*pdb_method)->add_group_mapping_entry = pdb_nop_add_group_mapping_entry;
127         (*pdb_method)->update_group_mapping_entry = pdb_nop_update_group_mapping_entry;
128         (*pdb_method)->delete_group_mapping_entry = pdb_nop_delete_group_mapping_entry;
129         (*pdb_method)->enum_group_mapping = pdb_nop_enum_group_mapping;
130         
131         
132         /* There's not very much to initialise here */
133         return NT_STATUS_OK;
134 }
135
136 NTSTATUS pdb_guest_init(void)
137 {
138         return smb_register_passdb(PASSDB_INTERFACE_VERSION, "guest", pdb_init_guestsam);
139 }
140