Remove the RCSID macro
[openafs.git] / src / pam / afs_password.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <syslog.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <pwd.h>
14 #include <unistd.h>
15 #include <afsconfig.h>
16 #include <afs/param.h>
17
18 #include <security/pam_appl.h>
19 #include <security/pam_modules.h>
20
21
22 #include <sys/param.h>
23 #include <afs/kautils.h>
24 #include "afs_message.h"
25 #include "afs_util.h"
26 #include "afs_pam_msg.h"
27 #include <signal.h>
28 #include <sys/wait.h>
29 #include <errno.h>
30
31 #define RET(x) { retcode = (x); goto out; }
32
33 extern int
34 pam_sm_chauthtok(pam_handle_t * pamh, int flags, int argc, const char **argv)
35 {
36     int retcode = PAM_SUCCESS;
37     int errcode = PAM_SUCCESS;
38     int code;
39     int origmask;
40     int logmask = LOG_UPTO(LOG_INFO);
41     int nowarn = 0;
42     int use_first_pass = 0;
43     int try_first_pass = 0;
44     int ignore_root = 0;
45     int got_authtok = 0;        /* got PAM_AUTHTOK upon entry */
46     int torch_password = 1;
47     int i;
48     char my_password_buf[256];
49     char instance[256];
50     char realm[256];
51     char cell[256];
52     char *localcell;
53     char *user = NULL, *password = NULL;
54     char *new_password = NULL, *verify_password = NULL;
55     char upwd_buf[2048];        /* size is a guess. */
56     char *reason = NULL;
57     struct ktc_encryptionKey oldkey, newkey;
58     struct ktc_token token;
59     struct ubik_client *conn = 0;
60     struct pam_conv *pam_convp = NULL;
61     struct passwd unix_pwd, *upwd = NULL;
62
63 #ifndef AFS_SUN56_ENV
64     openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
65 #endif
66     origmask = setlogmask(logmask);
67
68     /*
69      * Parse the user options.  Log an error for any unknown options.
70      *
71      * Todo options: PAM_SILENT
72      */
73     for (i = 0; i < argc; i++) {
74         if (strcasecmp(argv[i], "debug") == 0) {
75             logmask |= LOG_MASK(LOG_DEBUG);
76             (void)setlogmask(logmask);
77         } else if (strcasecmp(argv[i], "nowarn") == 0) {
78             nowarn = 1;
79         } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
80             use_first_pass = 1;
81         } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
82             try_first_pass = 1;
83         } else if (strcasecmp(argv[i], "ignore_root") == 0) {
84             ignore_root = 1;
85         } else {
86             pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
87         }
88     }
89
90     if (use_first_pass)
91         try_first_pass = 0;
92
93     if (logmask && LOG_MASK(LOG_DEBUG)) {
94         pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass,
95                        try_first_pass);
96         pam_afs_syslog(LOG_DEBUG, PAMAFS_PAMERROR, flags);
97     }
98
99     /* Try to get the user-interaction info, if available. */
100     errcode = pam_get_item(pamh, PAM_CONV, (const void **)&pam_convp);
101     if (errcode != PAM_SUCCESS) {
102         pam_afs_syslog(LOG_WARNING, PAMAFS_NO_USER_INT);
103         pam_convp = NULL;
104     }
105
106     /* Who are we trying to authenticate here? */
107     if ((errcode =
108          pam_get_user(pamh, (const char **)&user,
109                       "AFS username: ")) != PAM_SUCCESS) {
110         pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
111         RET(PAM_USER_UNKNOWN);
112     }
113
114     if (logmask && LOG_MASK(LOG_DEBUG))
115         pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
116
117     /*
118      * If the user has a "local" (or via nss, possibly nss_dce) pwent,
119      * and its uid==0, and "ignore_root" was given in pam.conf,
120      * ignore the user.
121      */
122 #if     defined(AFS_HPUX_ENV)
123 #if     defined(AFS_HPUX110_ENV)
124     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
125 #else /* AFS_HPUX110_ENV */
126     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
127     if (i == 0)                 /* getpwnam_r success */
128         upwd = &unix_pwd;
129 #endif /* else AFS_HPUX110_ENV */
130     if (ignore_root && i == 0 && upwd->pw_uid == 0) {
131         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
132         RET(PAM_AUTH_ERR);
133     }
134 #else
135 #if     defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV)
136     upwd = getpwnam(user);
137 #else
138     upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
139 #endif
140     if (ignore_root && upwd != NULL && upwd->pw_uid == 0) {
141         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
142         RET(PAM_AUTH_ERR);
143     }
144 #endif
145
146     errcode = pam_get_item(pamh, PAM_AUTHTOK, (const void **)&password);
147     if (errcode != PAM_SUCCESS || password == NULL) {
148         if (use_first_pass) {
149             pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
150             RET(PAM_AUTH_ERR);
151         }
152         password = NULL;        /* In case it isn't already NULL */
153         if (logmask && LOG_MASK(LOG_DEBUG))
154             pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
155     } else if (password[0] == '\0') {
156         /* Actually we *did* get one but it was empty. */
157         torch_password = 0;
158         pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
159         RET(PAM_NEW_AUTHTOK_REQD);
160     } else {
161         if (logmask && LOG_MASK(LOG_DEBUG))
162             pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
163         torch_password = 0;
164         got_authtok = 1;
165     }
166     if (!(use_first_pass || try_first_pass)) {
167         password = NULL;
168     }
169
170     if (password == NULL) {
171         torch_password = 1;
172         if (use_first_pass)
173             RET(PAM_AUTH_ERR);  /* shouldn't happen */
174         if (try_first_pass)
175             try_first_pass = 0;
176         if (pam_convp == NULL || pam_convp->conv == NULL) {
177             pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
178             RET(PAM_AUTH_ERR);
179         }
180
181         errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
182         if (errcode != PAM_SUCCESS || password == NULL) {
183             pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
184             RET(PAM_AUTH_ERR);
185         }
186         if (password[0] == '\0') {
187             pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
188             RET(PAM_NEW_AUTHTOK_REQD);
189         }
190
191         /*
192          * We aren't going to free the password later (we will wipe it,
193          * though), because the storage for it if we get it from other
194          * paths may belong to someone else.  Since we do need to free
195          * this storage, copy it to a buffer that won't need to be freed
196          * later, and free this storage now.
197          */
198         strncpy(my_password_buf, password, sizeof(my_password_buf));
199         my_password_buf[sizeof(my_password_buf) - 1] = '\0';
200         memset(password, 0, strlen(password));
201         free(password);
202         password = my_password_buf;
203     }
204
205     if ((code = ka_VerifyUserPassword(KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG, user, /* kerberos name */
206                                       NULL,     /* instance */
207                                       NULL,     /* realm */
208                                       password, /* password */
209                                       0,        /* spare 2 */
210                                       &reason /* error string */ )) != 0) {
211         pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
212         RET(PAM_AUTH_ERR);
213     }
214     torch_password = 0;
215     pam_set_item(pamh, PAM_AUTHTOK, password);
216     pam_set_item(pamh, PAM_OLDAUTHTOK, password);
217     if (flags & PAM_PRELIM_CHECK) {
218         /* only auth check was requested, so return success here */
219         return (PAM_SUCCESS);
220     }
221     if (!(flags & PAM_UPDATE_AUTHTOK)) {
222         /* these lines are never executed ... */
223         /* UPDATE_AUTHTOK flag is required, return with error */
224         pam_afs_syslog(LOG_ERR, PAMAFS_FLAGS, "PAM_UPDATE_AUTHTOK");
225         RET(PAM_AUTH_ERR);
226     }
227
228     /* get the new passwd and verify it */
229     errcode =
230         pam_afs_prompt(pam_convp, &new_password, 0, PAMAFS_NEW_PWD_PROMPT);
231     if (errcode != PAM_SUCCESS || new_password == NULL) {
232         pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
233         RET(PAM_AUTH_ERR);
234     }
235     if (new_password[0] == '\0') {
236         pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
237         RET(PAM_AUTH_ERR);
238     }
239     errcode =
240         pam_afs_prompt(pam_convp, &verify_password, 0,
241                        PAMAFS_VERIFY_PWD_PROMPT);
242     if (errcode != PAM_SUCCESS || verify_password == NULL) {
243         pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
244         memset(new_password, 0, strlen(new_password));
245         RET(PAM_AUTH_ERR);
246     }
247     if (verify_password[0] == '\0') {
248         pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
249         memset(new_password, 0, strlen(new_password));
250         RET(PAM_AUTH_ERR);
251     }
252     if (strcmp(new_password, verify_password) != 0) {
253         pam_afs_syslog(LOG_INFO, PAMAFS_NE_PASSWORD);
254         memset(new_password, 0, strlen(new_password));
255         memset(verify_password, 0, strlen(verify_password));
256         RET(PAM_AUTH_ERR);
257     }
258     memset(verify_password, 0, strlen(verify_password));
259     /* checking password length and quality is up to other PAM modules */
260
261     /* set the new password */
262     if ((code = ka_Init(0)) != 0) {
263         pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
264         RET(PAM_AUTH_ERR);
265     }
266     if ((code = rx_Init(0)) != 0) {
267         pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
268         RET(PAM_AUTH_ERR);
269     }
270     strcpy(instance, "");
271     if ((localcell = ka_LocalCell()) == NULL) {
272         pam_afs_syslog(LOG_ERR, PAMAFS_NOCELLNAME);
273         RET(PAM_AUTH_ERR);
274     }
275     strcpy(realm, localcell);
276     strcpy(cell, realm);
277     /* oldkey is not used in ka_ChangePassword (only for ka_auth) */
278     ka_StringToKey(password, realm, &oldkey);
279     ka_StringToKey(new_password, realm, &newkey);
280     if ((code =
281          ka_GetAdminToken(user, instance, realm, &oldkey, 20, &token,
282                           0)) != 0) {
283         pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
284         RET(PAM_AUTH_ERR);
285     }
286     if ((code =
287          ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token,
288                            &conn)) != 0) {
289         pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
290         RET(PAM_AUTH_ERR);
291     }
292     if ((code = ka_ChangePassword(user, /* kerberos name */
293                                   instance,     /* instance */
294                                   conn, /* conn */
295                                   0,    /* old password unused */
296                                   &newkey /* new password */ )) != 0) {
297         pam_afs_syslog(LOG_ERR, PAMAFS_KAPASS_FAIL);
298         memset(new_password, 0, strlen(new_password));
299         RET(PAM_AUTH_ERR);
300     } else {
301         pam_set_item(pamh, PAM_AUTHTOK, new_password);
302         RET(PAM_SUCCESS);
303     }
304
305   out:
306     if (password && torch_password) {
307         memset(password, 0, strlen(password));
308     }
309     (void)setlogmask(origmask);
310 #ifndef AFS_SUN56_ENV
311     closelog();
312 #endif
313     return retcode;
314 }