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