r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[samba.git] / source3 / pam_smbpass / pam_smb_auth.c
1 /* Unix NT password database implementation, version 0.7.5.
2  *
3  * This program is free software; you can redistribute it and/or modify it under
4  * the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 3 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, see <http://www.gnu.org/licenses/>.
15 */
16
17 /* indicate the following groups are defined */
18 #define PAM_SM_AUTH
19
20 #include "includes.h"
21 #include "debug.h"
22
23 #ifndef LINUX
24
25 /* This is only used in the Sun implementation. */
26 #if defined(HAVE_SECURITY_PAM_APPL_H)
27 #include <security/pam_appl.h>
28 #elif defined(HAVE_PAM_PAM_APPL_H)
29 #include <pam/pam_appl.h>
30 #endif
31
32 #endif  /* LINUX */
33
34 #if defined(HAVE_SECURITY_PAM_MODULES_H)
35 #include <security/pam_modules.h>
36 #elif defined(HAVE_PAM_PAM_MODULES_H)
37 #include <pam/pam_modules.h>
38 #endif
39
40 #include "general.h"
41
42 #include "support.h"
43
44 #define AUTH_RETURN                                             \
45 do {                                                            \
46         /* Restore application signal handler */                \
47         CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);       \
48         if(ret_data) {                                          \
49                 *ret_data = retval;                             \
50                 pam_set_data( pamh, "smb_setcred_return"        \
51                               , (void *) ret_data, NULL );      \
52         }                                                       \
53         return retval;                                          \
54 } while (0)
55
56 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
57                          const char *name, struct samu *sampass, BOOL exist);
58
59
60 /*
61  * pam_sm_authenticate() authenticates users against the samba password file.
62  *
63  *      First, obtain the password from the user. Then use a
64  *      routine in 'support.c' to authenticate the user.
65  */
66
67 #define _SMB_AUTHTOK  "-SMB-PASS"
68
69 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
70                         int argc, const char **argv)
71 {
72         unsigned int ctrl;
73         int retval, *ret_data = NULL;
74         struct samu *sampass = NULL;
75         extern BOOL in_client;
76         const char *name;
77         void (*oldsig_handler)(int) = NULL;
78         BOOL found;
79
80         /* Points to memory managed by the PAM library. Do not free. */
81         char *p = NULL;
82
83         /* Samba initialization. */
84         load_case_tables();
85         setup_logging("pam_smbpass",False);
86         in_client = True;
87
88         ctrl = set_ctrl(flags, argc, argv);
89
90         /* Get a few bytes so we can pass our return value to
91                 pam_sm_setcred(). */
92         ret_data = SMB_MALLOC_P(int);
93
94         /* we need to do this before we call AUTH_RETURN */
95         /* Getting into places that might use LDAP -- protect the app
96         from a SIGPIPE it's not expecting */
97         oldsig_handler = CatchSignal(SIGPIPE, SIGNAL_CAST SIG_IGN);
98
99         /* get the username */
100         retval = pam_get_user( pamh, &name, "Username: " );
101         if ( retval != PAM_SUCCESS ) {
102                 if (on( SMB_DEBUG, ctrl )) {
103                         _log_err(LOG_DEBUG, "auth: could not identify user");
104                 }
105                 AUTH_RETURN;
106         }
107         if (on( SMB_DEBUG, ctrl )) {
108                 _log_err( LOG_DEBUG, "username [%s] obtained", name );
109         }
110
111         if (!initialize_password_db(True, NULL)) {
112                 _log_err( LOG_ALERT, "Cannot access samba password database" );
113                 retval = PAM_AUTHINFO_UNAVAIL;
114                 AUTH_RETURN;
115         }
116
117         sampass = samu_new( NULL );
118         if (!sampass) {
119                 _log_err( LOG_ALERT, "Cannot talloc a samu struct" );
120                 retval = nt_status_to_pam(NT_STATUS_NO_MEMORY);
121                 AUTH_RETURN;
122         }
123
124         found = pdb_getsampwnam( sampass, name );
125
126         if (on( SMB_MIGRATE, ctrl )) {
127                 retval = _smb_add_user(pamh, ctrl, name, sampass, found);
128                 TALLOC_FREE(sampass);
129                 AUTH_RETURN;
130         }
131
132         if (!found) {
133                 _log_err(LOG_ALERT, "Failed to find entry for user %s.", name);
134                 retval = PAM_USER_UNKNOWN;
135                 TALLOC_FREE(sampass);
136                 sampass = NULL;
137                 AUTH_RETURN;
138         }
139    
140         /* if this user does not have a password... */
141
142         if (_smb_blankpasswd( ctrl, sampass )) {
143                 TALLOC_FREE(sampass);
144                 retval = PAM_SUCCESS;
145                 AUTH_RETURN;
146         }
147
148         /* get this user's authentication token */
149
150         retval = _smb_read_password(pamh, ctrl, NULL, "Password: ", NULL, _SMB_AUTHTOK, &p);
151         if (retval != PAM_SUCCESS ) {
152                 _log_err(LOG_CRIT, "auth: no password provided for [%s]", name);
153                 TALLOC_FREE(sampass);
154                 AUTH_RETURN;
155         }
156
157         /* verify the password of this user */
158
159         retval = _smb_verify_password( pamh, sampass, p, ctrl );
160         TALLOC_FREE(sampass);
161         p = NULL;
162         AUTH_RETURN;
163 }
164
165 /*
166  * This function is for setting samba credentials.  If anyone comes up
167  * with any credentials they think should be set, let me know.
168  */
169
170 int pam_sm_setcred(pam_handle_t *pamh, int flags,
171                    int argc, const char **argv)
172 {
173     int retval, *pretval = NULL;
174
175     retval = PAM_SUCCESS;
176
177     pam_get_data(pamh, "smb_setcred_return", (const void **) &pretval);
178     if(pretval) {
179         retval = *pretval;
180         SAFE_FREE(pretval);
181     }
182     pam_set_data(pamh, "smb_setcred_return", NULL, NULL);
183
184     return retval;
185 }
186
187
188 /* Helper function for adding a user to the db. */
189 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
190                          const char *name, struct samu *sampass, BOOL exist)
191 {
192     pstring err_str;
193     pstring msg_str;
194     const char *pass = NULL;
195     int retval;
196
197     err_str[0] = '\0';
198     msg_str[0] = '\0';
199
200     /* Get the authtok; if we don't have one, silently fail. */
201     retval = pam_get_item( pamh, PAM_AUTHTOK, (const void **) &pass );
202
203     if (retval != PAM_SUCCESS) {
204         _log_err( LOG_ALERT
205                   , "pam_get_item returned error to pam_sm_authenticate" );
206         return PAM_AUTHTOK_RECOVER_ERR;
207     } else if (pass == NULL) {
208         return PAM_AUTHTOK_RECOVER_ERR;
209     }
210
211     /* Add the user to the db if they aren't already there. */
212    if (!exist) {
213         retval = NT_STATUS_IS_OK(local_password_change( name, LOCAL_ADD_USER|LOCAL_SET_PASSWORD,
214                                          pass, err_str,
215                                          sizeof(err_str),
216                                          msg_str, sizeof(msg_str) ));
217         if (!retval && *err_str)
218         {
219             err_str[PSTRING_LEN-1] = '\0';
220             make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
221         }
222         else if (*msg_str)
223         {
224             msg_str[PSTRING_LEN-1] = '\0';
225             make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
226         }
227         pass = NULL;
228
229         return PAM_IGNORE;
230    }
231    else {
232     /* mimick 'update encrypted' as long as the 'no pw req' flag is not set */
233     if ( pdb_get_acct_ctrl(sampass) & ~ACB_PWNOTREQ )
234     {
235         retval = NT_STATUS_IS_OK(local_password_change( name, LOCAL_SET_PASSWORD, pass, err_str, sizeof(err_str),
236                                          msg_str, sizeof(msg_str) ));
237         if (!retval && *err_str)
238         {
239             err_str[PSTRING_LEN-1] = '\0';
240             make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
241         }
242         else if (*msg_str)
243         {
244             msg_str[PSTRING_LEN-1] = '\0';
245             make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
246         }
247     }
248    }
249     
250     pass = NULL;
251
252     return PAM_IGNORE;
253 }
254
255
256 /* static module data */
257 #ifdef PAM_STATIC
258 struct pam_module _pam_smbpass_auth_modstruct = {
259      "pam_smbpass",
260      pam_sm_authenticate,
261      pam_sm_setcred,
262      NULL,
263      NULL,
264      NULL,
265      NULL
266 };
267 #endif