pam-afs-password-changing-support-20010305
[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 <security/pam_appl.h>
11 #include <security/pam_modules.h>
12 #include <syslog.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <pwd.h>
16 #include <unistd.h>
17 #include <afs/param.h>
18 #include <sys/param.h>
19 #include <afs/kautils.h>
20 #include "afs_message.h"
21 #include "afs_util.h"
22 #include "afs_pam_msg.h"
23 #include <signal.h>
24 #include <sys/wait.h>
25 #include <errno.h>
26
27 #define RET(x) { retcode = (x); goto out; }
28
29 extern int
30 pam_sm_chauthtok(
31         pam_handle_t    *pamh,
32         int             flags,
33         int             argc,
34         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) try_first_pass = 0;
91
92     pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass, try_first_pass);
93     pam_afs_syslog(LOG_DEBUG, PAMAFS_PAMERROR, flags);
94
95     /* Try to get the user-interaction info, if available. */
96     errcode = pam_get_item(pamh, PAM_CONV, (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 = pam_get_user(pamh, (const char **)&user, "AFS username: ")) != PAM_SUCCESS) {
104         pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
105         RET(PAM_USER_UNKNOWN);
106     }
107
108     pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
109
110     /*
111      * If the user has a "local" (or via nss, possibly nss_dce) pwent,
112      * and its uid==0, and "ignore_root" was given in pam.conf,
113      * ignore the user.
114      */
115 #if     defined(AFS_HPUX_ENV)
116 #if     defined(AFS_HPUX110_ENV)
117     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
118 #else   /* AFS_HPUX110_ENV */
119     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
120     if ( i == 0 )                       /* getpwnam_r success */
121         upwd = &unix_pwd; 
122 #endif  /* else AFS_HPUX110_ENV */
123     if (ignore_root && i == 0  && upwd->pw_uid == 0) {
124         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
125         RET(PAM_AUTH_ERR);
126     }
127 #else
128 #ifdef AFS_LINUX20_ENV
129     upwd = getpwnam(user);
130 #else
131     upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
132 #endif
133     if (ignore_root && upwd != NULL && upwd->pw_uid == 0) {
134         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
135         RET(PAM_AUTH_ERR);
136     }
137 #endif
138
139     errcode = pam_get_item(pamh, PAM_AUTHTOK, (const void **) &password);
140     if (errcode != PAM_SUCCESS || password == NULL) {
141         if (use_first_pass) {
142             pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
143             RET(PAM_AUTH_ERR);
144         }
145         password = NULL;        /* In case it isn't already NULL */
146         pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
147     } else if (password[0] == '\0') {
148         /* Actually we *did* get one but it was empty. */
149         torch_password = 0;
150         pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
151         RET(PAM_NEW_AUTHTOK_REQD);
152     } else {
153         pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
154         torch_password = 0;
155         got_authtok = 1;
156     }
157     if (!(use_first_pass || try_first_pass)) {
158         password = NULL;
159     }
160
161     if (password == NULL) {
162         torch_password = 1;
163         if (use_first_pass)
164             RET(PAM_AUTH_ERR);  /* shouldn't happen */
165         if (try_first_pass)
166             try_first_pass = 0; 
167         if (pam_convp == NULL || pam_convp->conv == NULL) {
168             pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
169             RET(PAM_AUTH_ERR);
170         }
171
172         errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
173         if (errcode != PAM_SUCCESS || password == NULL) {
174             pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
175             RET(PAM_AUTH_ERR);
176         }
177         if (password[0] == '\0') {
178             pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
179             RET(PAM_NEW_AUTHTOK_REQD);
180         }
181
182         /*
183          * We aren't going to free the password later (we will wipe it,
184          * though), because the storage for it if we get it from other
185          * paths may belong to someone else.  Since we do need to free
186          * this storage, copy it to a buffer that won't need to be freed
187          * later, and free this storage now.
188          */
189         strncpy(my_password_buf, password, sizeof(my_password_buf));
190         my_password_buf[sizeof(my_password_buf)-1] = '\0';
191         memset(password, 0, strlen(password));
192         free(password);
193         password = my_password_buf;
194     }
195
196     if ( (code = ka_VerifyUserPassword(KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
197                                     user, /* kerberos name */
198                                     (char *)0, /* instance */
199                                     (char *)0, /* realm */
200                                     password, /* password */
201                                     0, /* spare 2 */
202                                     &reason /* error string */ )) !=0 ) {
203         pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
204         RET(PAM_AUTH_ERR);
205     }
206     torch_password = 0;
207     pam_set_item(pamh, PAM_AUTHTOK, password);
208     pam_set_item(pamh, PAM_OLDAUTHTOK, password);
209     if (flags & PAM_PRELIM_CHECK) {
210        /* only auth check was requested, so return success here */
211        return(PAM_SUCCESS);
212     }
213     if (!(flags & PAM_UPDATE_AUTHTOK)) {
214        /* these lines are never executed ... */
215        /* UPDATE_AUTHTOK flag is required, return with error */
216        pam_afs_syslog(LOG_ERR, PAMAFS_FLAGS, "PAM_UPDATE_AUTHTOK");
217        RET(PAM_AUTH_ERR);
218     }
219
220     /* get the new passwd and verify it */
221     errcode = pam_afs_prompt(pam_convp, &new_password, 0, PAMAFS_NEW_PWD_PROMPT);
222     if (errcode != PAM_SUCCESS || new_password == NULL) {
223         pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
224         RET(PAM_AUTH_ERR);
225     }
226     if (new_password[0] == '\0') {
227         pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
228         RET(PAM_AUTH_ERR);
229     }
230     errcode = pam_afs_prompt(pam_convp, &verify_password, 0, PAMAFS_VERIFY_PWD_PROMPT);
231     if (errcode != PAM_SUCCESS || verify_password == NULL) {
232         pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
233         memset(new_password, 0, strlen(new_password));
234         RET(PAM_AUTH_ERR);
235     }
236     if (verify_password[0] == '\0') {
237         pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
238         memset(new_password, 0, strlen(new_password));
239         RET(PAM_AUTH_ERR);
240     }
241     if (strcmp(new_password, verify_password) != 0) {
242         pam_afs_syslog(LOG_INFO, PAMAFS_NE_PASSWORD);
243         memset(new_password, 0, strlen(new_password));
244         memset(verify_password, 0, strlen(verify_password));
245         RET(PAM_AUTH_ERR);
246     }
247     memset(verify_password, 0, strlen(verify_password));
248     /* checking password length and quality is up to other PAM modules */ 
249
250     /* set the new password */
251     if ((code = ka_Init(0)) != 0) {
252         pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
253         RET(PAM_AUTH_ERR);
254     }
255     if ((code = rx_Init(0)) != 0) {
256         pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
257         RET(PAM_AUTH_ERR);
258     }
259     strcpy(instance,"");
260     if ((localcell = ka_LocalCell()) == NULL) {
261         pam_afs_syslog(LOG_ERR, PAMAFS_NOCELLNAME);
262         RET(PAM_AUTH_ERR);
263     }
264     strcpy(realm,localcell);
265     strcpy(cell,realm);
266     /* oldkey is not used in ka_ChangePassword (only for ka_auth) */
267     ka_StringToKey(password, realm, &oldkey);
268     ka_StringToKey(new_password, realm, &newkey);
269     if ((code = ka_GetAdminToken(user, instance, realm, &oldkey, 20, &token, 0)) != 0) {
270         pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
271         RET(PAM_AUTH_ERR);
272     }
273     if ((code = ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token, &conn)) != 0) { 
274         pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
275         RET(PAM_AUTH_ERR);
276     }
277     if ((code = ka_ChangePassword(user,     /* kerberos name */
278                                   instance, /* instance */
279                                   conn,     /* conn */
280                                   0,        /* old password unused */
281                                   &newkey   /* new password */ )) != 0) {
282         pam_afs_syslog(LOG_ERR, PAMAFS_KAPASS_FAIL);
283         memset(new_password, 0, strlen(new_password));
284         RET(PAM_AUTH_ERR);
285     } else {
286         pam_set_item(pamh, PAM_AUTHTOK, new_password);
287         RET(PAM_SUCCESS);
288     }
289         
290  out:
291     if (password && torch_password) {
292         memset(password, 0, strlen(password));
293     }
294     (void) setlogmask(origmask);
295 #ifndef AFS_SUN56_ENV
296     closelog();
297 #endif
298     return retcode;
299 }