2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <security/pam_appl.h>
11 #include <security/pam_modules.h>
17 #include <afs/param.h>
18 #include <sys/param.h>
19 #include <afs/kautils.h>
20 #include "afs_message.h"
25 #define RET(x) { retcode = (x); goto out; }
27 #if defined(AFS_KERBEROS_ENV)
28 extern char *ktc_tkt_string();
38 int retcode = PAM_SUCCESS;
39 int errcode = PAM_SUCCESS;
41 int logmask = LOG_UPTO(LOG_INFO);
43 int use_first_pass = 1; /* use the password passed in by auth */
44 int try_first_pass = 0;
48 int set_expires = 0; /* the default is to not to set the env variable */
50 struct pam_conv *pam_convp = NULL;
51 char my_password_buf[256];
53 char *password = NULL;
54 int torch_password = 1;
58 long password_expires= -1;
60 struct passwd unix_pwd, *upwd = NULL;
61 char upwd_buf[2048]; /* size is a guess. */
64 openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
66 origmask = setlogmask(logmask);
69 * Parse the user options. Log an error for any unknown options.
71 for (i = 0; i < argc; i++) {
72 if ( strcasecmp(argv[i], "debug" ) == 0) {
73 logmask |= LOG_MASK(LOG_DEBUG);
74 (void) setlogmask(logmask);
75 } else if (strcasecmp(argv[i], "nowarn" ) == 0) {
77 } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
78 use_first_pass = 1; /* practically redundant */
79 } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
81 } else if (strcasecmp(argv[i], "ignore_root" ) == 0) {
83 } else if (strcasecmp(argv[i], "trust_root" ) == 0) {
85 } else if (strcasecmp(argv[i], "catch_su" ) == 0) {
87 } else if (strcasecmp(argv[i], "setenv_password_expires")==0) {
90 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
94 if (use_first_pass) try_first_pass = 0;
96 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass, try_first_pass);
97 /* Try to get the user-interaction info, if available. */
98 errcode = pam_get_item(pamh, PAM_CONV, (void **) &pam_convp);
99 if (errcode != PAM_SUCCESS) {
100 pam_afs_syslog(LOG_DEBUG, PAMAFS_NO_USER_INT);
104 /* Who are we trying to authenticate here? */
105 if ((errcode = pam_get_user(pamh, &user, "AFS username:")) != PAM_SUCCESS) {
106 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
107 RET(PAM_USER_UNKNOWN);
110 * If the user has a "local" (or via nss, possibly nss_dce) pwent,
111 * and its uid==0, and "ignore_root" was given in pam.conf,
114 #if defined(AFS_HPUX_ENV)
115 #if defined(AFS_HPUX110_ENV)
116 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
117 #else /* AFS_HPUX110_ENV */
118 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
119 if ( i == 0 ) /* getpwnam_r success */
121 #endif /* AFS_HPUX110_ENV */
122 if (ignore_root && i == 0 && upwd->pw_uid == 0) {
123 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
127 #ifdef AFS_LINUX20_ENV
128 upwd = getpwnam(user);
130 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
132 if (upwd != NULL && upwd->pw_uid == 0) {
134 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
136 } else if (trust_root) {
137 pam_afs_syslog(LOG_INFO, PAMAFS_TRUSTROOT, user);
143 if (flags & PAM_DELETE_CRED) {
144 pam_afs_syslog(LOG_DEBUG, PAMAFS_DELCRED, user);
147 } else if (flags & PAM_REINITIALIZE_CRED) {
149 pam_afs_syslog(LOG_DEBUG, PAMAFS_REINITCRED, user);
152 } else { /* flags are PAM_REFRESH_CRED, PAM_ESTABLISH_CRED, unknown */
154 pam_afs_syslog(LOG_DEBUG, PAMAFS_ESTABCRED, user);
156 errcode = pam_get_data(pamh, pam_afs_lh, (const void **) &password);
157 if (errcode != PAM_SUCCESS || password == NULL) {
158 if (use_first_pass) {
159 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
162 password = NULL; /* In case it isn't already NULL */
163 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
164 } else if (password[0] == '\0') {
165 /* Actually we *did* get one but it was empty. */
168 /* So don't use it. */
170 if (use_first_pass) {
171 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
172 RET(PAM_NEW_AUTHTOK_REQD);
174 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD, user);
176 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
180 if (!(use_first_pass || try_first_pass)) {
185 if (password == NULL) {
190 RET(PAM_AUTH_ERR); /* shouldn't happen */
192 try_first_pass = 0; /* we come back if try_first_pass==1 below */
194 if (pam_convp == NULL || pam_convp->conv == NULL) {
195 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
199 errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
200 if (errcode != PAM_SUCCESS || password == NULL) {
201 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
204 if (password[0] == '\0') {
205 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD);
206 RET(PAM_NEW_AUTHTOK_REQD);
209 * We aren't going to free the password later (we will wipe it,
210 * though), because the storage for it if we get it from other
211 * paths may belong to someone else. Since we do need to free
212 * this storage, copy it to a buffer that won't need to be freed
213 * later, and free this storage now.
215 strncpy(my_password_buf, password, sizeof(my_password_buf));
216 my_password_buf[sizeof(my_password_buf)-1] = '\0';
217 memset(password, 0, strlen(password));
219 password = my_password_buf;
222 if ( flags & PAM_REFRESH_CRED ) {
223 if ( ka_VerifyUserPassword(
224 KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
225 user, /* kerberos name */
226 (char *)0, /* instance */
227 (char *)0, /* realm */
228 password, /* password */
230 &reason /* error string */
232 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
238 if ( flags & PAM_ESTABLISH_CRED ) {
239 if ( ka_UserAuthenticateGeneral(
240 KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
241 user, /* kerberos name */
242 (char *)0, /* instance */
243 (char *)0, /* realm */
244 password, /* password */
245 0, /* default lifetime */
248 &reason /* error string */
250 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
256 if (!auth_ok && try_first_pass) {
261 if (auth_ok && !got_authtok) {
263 (void) pam_set_item(pamh, PAM_AUTHTOK, password);
267 if (set_expires && (password_expires >= 0) ) {
268 strcpy(sbuffer, "PASSWORD_EXPIRES=");
269 strcat(sbuffer, cv2string(&sbuffer[100], password_expires));
270 errcode = pam_putenv( pamh, sbuffer);
271 if ( errcode != PAM_SUCCESS )
272 pam_afs_syslog(LOG_ERR, PAMAFS_PASSEXPFAIL, user);
274 #if defined(AFS_KERBEROS_ENV)
277 if ( chown(ktc_tkt_string(), upwd->pw_uid, upwd->pw_gid) < 0 )
278 pam_afs_syslog(LOG_ERR, PAMAFS_CHOWNKRB, user);
279 sprintf(sbuffer, "KRBTKFILE=%s", ktc_tkt_string());
280 errcode = pam_putenv( pamh, sbuffer);
281 if ( errcode != PAM_SUCCESS )
282 pam_afs_syslog(LOG_ERR, PAMAFS_KRBFAIL, user);
293 if (password && torch_password) memset(password, 0, strlen(password));
294 (void) setlogmask(origmask);
295 #ifndef AFS_SUN56_ENV