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;
47 int set_expires = 0; /* the default is to not to set the env variable */
49 struct pam_conv *pam_convp = NULL;
50 char my_password_buf[256];
52 char *password = NULL;
53 int torch_password = 1;
57 long password_expires= -1;
59 struct passwd unix_pwd, *upwd = NULL;
60 char upwd_buf[2048]; /* size is a guess. */
63 openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
65 origmask = setlogmask(logmask);
68 * Parse the user options. Log an error for any unknown options.
70 for (i = 0; i < argc; i++) {
71 if ( strcasecmp(argv[i], "debug" ) == 0) {
72 logmask |= LOG_MASK(LOG_DEBUG);
73 (void) setlogmask(logmask);
74 } else if (strcasecmp(argv[i], "nowarn" ) == 0) {
76 } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
77 use_first_pass = 1; /* practically redundant */
78 } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
80 } else if (strcasecmp(argv[i], "ignore_root" ) == 0) {
82 } else if (strcasecmp(argv[i], "setenv_password_expires")==0) {
85 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
89 if (use_first_pass) try_first_pass = 0;
91 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass, try_first_pass);
92 /* Try to get the user-interaction info, if available. */
93 errcode = pam_get_item(pamh, PAM_CONV, (void **) &pam_convp);
94 if (errcode != PAM_SUCCESS) {
95 pam_afs_syslog(LOG_DEBUG, PAMAFS_NO_USER_INT);
99 /* Who are we trying to authenticate here? */
100 if ((errcode = pam_get_user(pamh, &user, "AFS username:")) != PAM_SUCCESS) {
101 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
102 RET(PAM_USER_UNKNOWN);
105 * If the user has a "local" (or via nss, possibly nss_dce) pwent,
106 * and its uid==0, and "ignore_root" was given in pam.conf,
109 #if defined(AFS_HPUX_ENV)
110 #if defined(AFS_HPUX110_ENV)
111 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
112 #else /* AFS_HPUX110_ENV */
113 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
114 if ( i == 0 ) /* getpwnam_r success */
116 #endif /* AFS_HPUX110_ENV */
117 if (ignore_root && i == 0 && upwd->pw_uid == 0) {
118 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
122 #ifdef AFS_LINUX20_ENV
123 upwd = getpwnam(user);
125 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
127 if (ignore_root && upwd != NULL && upwd->pw_uid == 0) {
128 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
133 if (flags & PAM_DELETE_CRED) {
134 pam_afs_syslog(LOG_DEBUG, PAMAFS_DELCRED, user);
137 } else if (flags & PAM_REINITIALIZE_CRED) {
139 pam_afs_syslog(LOG_DEBUG, PAMAFS_REINITCRED, user);
142 } else { /* flags are PAM_REFRESH_CRED, PAM_ESTABLISH_CRED, unknown */
144 pam_afs_syslog(LOG_DEBUG, PAMAFS_ESTABCRED, user);
146 errcode = pam_get_data(pamh, pam_afs_lh, (const void **) &password);
147 if (errcode != PAM_SUCCESS || password == NULL) {
148 if (use_first_pass) {
149 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
152 password = NULL; /* In case it isn't already NULL */
153 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
154 } else if (password[0] == '\0') {
155 /* Actually we *did* get one but it was empty. */
158 /* So don't use it. */
160 if (use_first_pass) {
161 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
162 RET(PAM_NEW_AUTHTOK_REQD);
164 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD, user);
166 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
170 if (!(use_first_pass || try_first_pass)) {
175 if (password == NULL) {
180 RET(PAM_AUTH_ERR); /* shouldn't happen */
182 try_first_pass = 0; /* we come back if try_first_pass==1 below */
184 if (pam_convp == NULL || pam_convp->conv == NULL) {
185 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
189 errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
190 if (errcode != PAM_SUCCESS || password == NULL) {
191 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
194 if (password[0] == '\0') {
195 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD);
196 RET(PAM_NEW_AUTHTOK_REQD);
199 * We aren't going to free the password later (we will wipe it,
200 * though), because the storage for it if we get it from other
201 * paths may belong to someone else. Since we do need to free
202 * this storage, copy it to a buffer that won't need to be freed
203 * later, and free this storage now.
205 strncpy(my_password_buf, password, sizeof(my_password_buf));
206 my_password_buf[sizeof(my_password_buf)-1] = '\0';
207 memset(password, 0, strlen(password));
209 password = my_password_buf;
212 if ( flags & PAM_REFRESH_CRED ) {
213 if ( ka_VerifyUserPassword(
214 KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
215 user, /* kerberos name */
216 (char *)0, /* instance */
217 (char *)0, /* realm */
218 password, /* password */
220 &reason /* error string */
222 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
228 if ( flags & PAM_ESTABLISH_CRED ) {
229 if ( ka_UserAuthenticateGeneral(
230 KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
231 user, /* kerberos name */
232 (char *)0, /* instance */
233 (char *)0, /* realm */
234 password, /* password */
235 0, /* default lifetime */
238 &reason /* error string */
240 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
246 if (!auth_ok && try_first_pass) {
251 if (auth_ok && !got_authtok) {
253 (void) pam_set_item(pamh, PAM_AUTHTOK, password);
257 if (set_expires && (password_expires >= 0) ) {
258 strcpy(sbuffer, "PASSWORD_EXPIRES=");
259 strcat(sbuffer, cv2string(&sbuffer[100], password_expires));
260 errcode = pam_putenv( pamh, sbuffer);
261 if ( errcode != PAM_SUCCESS )
262 pam_afs_syslog(LOG_ERR, PAMAFS_PASSEXPFAIL, user);
264 #if defined(AFS_KERBEROS_ENV)
267 if ( chown(ktc_tkt_string(), upwd->pw_uid, upwd->pw_gid) < 0 )
268 pam_afs_syslog(LOG_ERR, PAMAFS_CHOWNKRB, user);
269 sprintf(sbuffer, "KRBTKFILE=%s", ktc_tkt_string());
270 errcode = pam_putenv( pamh, sbuffer);
271 if ( errcode != PAM_SUCCESS )
272 pam_afs_syslog(LOG_ERR, PAMAFS_KRBFAIL, user);
283 if (password && torch_password) memset(password, 0, strlen(password));
284 (void) setlogmask(origmask);
285 #ifndef AFS_SUN56_ENV