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