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