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 <afsconfig.h>
22 #include <sys/param.h>
23 #include <afs/kautils.h>
24 #include "afs_message.h"
29 #define RET(x) { retcode = (x); goto out; }
31 #if defined(AFS_KERBEROS_ENV)
32 extern char *ktc_tkt_string();
42 int retcode = PAM_SUCCESS;
43 int errcode = PAM_SUCCESS;
45 int logmask = LOG_UPTO(LOG_INFO);
47 int use_first_pass = 1; /* use the password passed in by auth */
48 int try_first_pass = 0;
52 int set_expires = 0; /* the default is to not to set the env variable */
54 struct pam_conv *pam_convp = NULL;
55 char my_password_buf[256];
57 char *password = NULL;
58 int torch_password = 1;
62 long password_expires= -1;
64 struct passwd unix_pwd, *upwd = NULL;
65 char upwd_buf[2048]; /* size is a guess. */
68 openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
70 origmask = setlogmask(logmask);
73 * Parse the user options. Log an error for any unknown options.
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) {
81 } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
82 use_first_pass = 1; /* practically redundant */
83 } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
85 } else if (strcasecmp(argv[i], "ignore_root" ) == 0) {
87 } else if (strcasecmp(argv[i], "trust_root" ) == 0) {
89 } else if (strcasecmp(argv[i], "catch_su" ) == 0) {
91 } else if (strcasecmp(argv[i], "setenv_password_expires")==0) {
94 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
98 if (use_first_pass) try_first_pass = 0;
100 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass, try_first_pass);
101 /* Try to get the user-interaction info, if available. */
102 errcode = pam_get_item(pamh, PAM_CONV, (void **) &pam_convp);
103 if (errcode != PAM_SUCCESS) {
104 pam_afs_syslog(LOG_DEBUG, PAMAFS_NO_USER_INT);
108 /* Who are we trying to authenticate here? */
109 if ((errcode = pam_get_user(pamh, &user, "AFS username:")) != PAM_SUCCESS) {
110 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
111 RET(PAM_USER_UNKNOWN);
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,
118 #if defined(AFS_HPUX_ENV)
119 #if defined(AFS_HPUX110_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 */
125 #endif /* AFS_HPUX110_ENV */
126 if (ignore_root && i == 0 && upwd->pw_uid == 0) {
127 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
131 #ifdef AFS_LINUX20_ENV
132 upwd = getpwnam(user);
134 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
136 if (upwd != NULL && upwd->pw_uid == 0) {
138 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
140 } else if (trust_root) {
141 pam_afs_syslog(LOG_INFO, PAMAFS_TRUSTROOT, user);
147 if (flags & PAM_DELETE_CRED) {
148 pam_afs_syslog(LOG_DEBUG, PAMAFS_DELCRED, user);
151 } else if (flags & PAM_REINITIALIZE_CRED) {
153 pam_afs_syslog(LOG_DEBUG, PAMAFS_REINITCRED, user);
156 } else { /* flags are PAM_REFRESH_CRED, PAM_ESTABLISH_CRED, unknown */
158 pam_afs_syslog(LOG_DEBUG, PAMAFS_ESTABCRED, user);
160 errcode = pam_get_data(pamh, pam_afs_lh, (const void **) &password);
161 if (errcode != PAM_SUCCESS || password == NULL) {
162 if (use_first_pass) {
163 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
166 password = NULL; /* In case it isn't already NULL */
167 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
168 } else if (password[0] == '\0') {
169 /* Actually we *did* get one but it was empty. */
172 /* So don't use it. */
174 if (use_first_pass) {
175 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
176 RET(PAM_NEW_AUTHTOK_REQD);
178 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD, user);
180 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
184 if (!(use_first_pass || try_first_pass)) {
189 if (password == NULL) {
194 RET(PAM_AUTH_ERR); /* shouldn't happen */
196 try_first_pass = 0; /* we come back if try_first_pass==1 below */
198 if (pam_convp == NULL || pam_convp->conv == NULL) {
199 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
203 errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
204 if (errcode != PAM_SUCCESS || password == NULL) {
205 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
208 if (password[0] == '\0') {
209 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD);
210 RET(PAM_NEW_AUTHTOK_REQD);
213 * We aren't going to free the password later (we will wipe it,
214 * though), because the storage for it if we get it from other
215 * paths may belong to someone else. Since we do need to free
216 * this storage, copy it to a buffer that won't need to be freed
217 * later, and free this storage now.
219 strncpy(my_password_buf, password, sizeof(my_password_buf));
220 my_password_buf[sizeof(my_password_buf)-1] = '\0';
221 memset(password, 0, strlen(password));
223 password = my_password_buf;
226 if ( flags & PAM_REFRESH_CRED ) {
227 if ( ka_VerifyUserPassword(
228 KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
229 user, /* kerberos name */
230 (char *)0, /* instance */
231 (char *)0, /* realm */
232 password, /* password */
234 &reason /* error string */
236 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
242 if ( flags & PAM_ESTABLISH_CRED ) {
243 if ( ka_UserAuthenticateGeneral(
244 KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
245 user, /* kerberos name */
246 (char *)0, /* instance */
247 (char *)0, /* realm */
248 password, /* password */
249 0, /* default lifetime */
252 &reason /* error string */
254 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
260 if (!auth_ok && try_first_pass) {
265 if (auth_ok && !got_authtok) {
267 (void) pam_set_item(pamh, PAM_AUTHTOK, password);
271 if (set_expires && (password_expires >= 0) ) {
272 strcpy(sbuffer, "PASSWORD_EXPIRES=");
273 strcat(sbuffer, cv2string(&sbuffer[100], password_expires));
274 errcode = pam_putenv( pamh, sbuffer);
275 if ( errcode != PAM_SUCCESS )
276 pam_afs_syslog(LOG_ERR, PAMAFS_PASSEXPFAIL, user);
278 #if defined(AFS_KERBEROS_ENV)
281 if ( chown(ktc_tkt_string(), upwd->pw_uid, upwd->pw_gid) < 0 )
282 pam_afs_syslog(LOG_ERR, PAMAFS_CHOWNKRB, user);
283 sprintf(sbuffer, "KRBTKFILE=%s", ktc_tkt_string());
284 errcode = pam_putenv( pamh, sbuffer);
285 if ( errcode != PAM_SUCCESS )
286 pam_afs_syslog(LOG_ERR, PAMAFS_KRBFAIL, user);
297 if (password && torch_password) memset(password, 0, strlen(password));
298 (void) setlogmask(origmask);
299 #ifndef AFS_SUN56_ENV