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
16 #include <afsconfig.h>
17 #include <afs/param.h>
19 #include <security/pam_appl.h>
20 #include <security/pam_modules.h>
23 #include <sys/param.h>
24 #include <afs/kautils.h>
25 #include "afs_message.h"
30 #define RET(x) { retcode = (x); goto out; }
32 #if defined(AFS_KERBEROS_ENV)
33 extern char *ktc_tkt_string();
37 pam_sm_setcred(pam_handle_t * pamh, int flags, int argc, const char **argv)
39 int retcode = PAM_SUCCESS;
40 int errcode = PAM_SUCCESS;
42 int logmask = LOG_UPTO(LOG_INFO);
44 int use_first_pass = 1; /* use the password passed in by auth */
45 int try_first_pass = 0;
49 uid_t ignore_uid_id = 0;
50 int refresh_token = 0;
51 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];
56 char *cell_ptr = NULL;
58 char *password = NULL;
59 int torch_password = 1;
63 int password_expires = -1;
65 struct passwd unix_pwd, *upwd = NULL;
66 char upwd_buf[2048]; /* size is a guess. */
69 openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
71 origmask = setlogmask(logmask);
74 * Parse the user options. Log an error for any unknown options.
76 for (i = 0; i < argc; i++) {
77 if (strcasecmp(argv[i], "debug") == 0) {
78 logmask |= LOG_MASK(LOG_DEBUG);
79 (void)setlogmask(logmask);
80 } else if (strcasecmp(argv[i], "nowarn") == 0) {
82 } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
83 use_first_pass = 1; /* practically redundant */
84 } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
86 } else if (strcasecmp(argv[i], "ignore_root") == 0) {
89 } else if (strcasecmp(argv[i], "ignore_uid") == 0) {
92 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID,
93 "ignore_uid missing argument");
97 ignore_uid_id = (uid_t) strtol(argv[i], (char **)NULL, 10);
98 if ((0 > ignore_uid_id) || (ignore_uid_id > IGNORE_MAX)) {
100 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID, argv[i]);
103 } else if (strcasecmp(argv[i], "cell") == 0) {
106 pam_afs_syslog(LOG_ERR, PAMAFS_OTHERCELL,
107 "cell missing argument");
109 cell_ptr = (char *)argv[i];
110 pam_afs_syslog(LOG_INFO, PAMAFS_OTHERCELL, cell_ptr);
112 } else if (strcasecmp(argv[i], "no_unlog") == 0) {
114 } else if (strcasecmp(argv[i], "refresh_token") == 0) {
116 } else if (strcasecmp(argv[i], "set_token") == 0) {
118 } else if (strcasecmp(argv[i], "dont_fork") == 0) {
120 } else if (strcasecmp(argv[i], "use_klog") == 0) {
122 } else if (strcasecmp(argv[i], "setenv_password_expires") == 0) {
125 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
132 if (logmask && LOG_MASK(LOG_DEBUG))
133 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass,
134 try_first_pass, ignore_uid, ignore_uid_id, 8, 8, 8, 8);
135 /* Try to get the user-interaction info, if available. */
136 errcode = pam_get_item(pamh, PAM_CONV, (const void **)&pam_convp);
137 if (errcode != PAM_SUCCESS) {
138 if (logmask && LOG_MASK(LOG_DEBUG))
139 pam_afs_syslog(LOG_DEBUG, PAMAFS_NO_USER_INT);
143 /* Who are we trying to authenticate here? */
145 pam_get_user(pamh, (const char **)&user,
146 "AFS username:")) != PAM_SUCCESS) {
147 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
148 RET(PAM_USER_UNKNOWN);
151 * If the user has a "local" (or via nss, possibly nss_dce) pwent,
152 * and its uid==0, and "ignore_root" was given in pam.conf,
155 /* enhanced: use "ignore_uid <number>" to specify the largest uid
156 * which should be ignored by this module
158 #if defined(AFS_HPUX_ENV) || defined(AFS_DARWIN100_ENV)
159 #if defined(AFS_HPUX110_ENV) || defined(AFS_DARWIN100_ENV)
160 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
161 #else /* AFS_HPUX110_ENV */
162 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
163 if (i == 0) /* getpwnam_r success */
165 #endif /* AFS_HPUX110_ENV */
166 if (ignore_uid && i == 0 && upwd->pw_uid <= ignore_uid_id) {
167 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
171 #if defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV)
172 upwd = getpwnam(user);
174 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
176 if (ignore_uid && upwd != NULL && upwd->pw_uid <= ignore_uid_id) {
177 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
182 if (flags & PAM_DELETE_CRED) {
183 if (logmask && LOG_MASK(LOG_DEBUG))
184 pam_afs_syslog(LOG_DEBUG, PAMAFS_DELCRED, user);
187 } else if (flags & PAM_REINITIALIZE_CRED) {
189 if (logmask && LOG_MASK(LOG_DEBUG))
190 pam_afs_syslog(LOG_DEBUG, PAMAFS_REINITCRED, user);
193 } else { /* flags are PAM_REFRESH_CRED, PAM_ESTABLISH_CRED, unknown */
195 if (logmask && LOG_MASK(LOG_DEBUG))
196 pam_afs_syslog(LOG_DEBUG, PAMAFS_ESTABCRED, user);
198 errcode = pam_get_data(pamh, pam_afs_lh, (const void **)&password);
199 if (errcode != PAM_SUCCESS || password == NULL) {
200 if (use_first_pass) {
201 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
204 password = NULL; /* In case it isn't already NULL */
205 if (logmask && LOG_MASK(LOG_DEBUG))
206 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
207 } else if (password[0] == '\0') {
208 /* Actually we *did* get one but it was empty. */
211 /* So don't use it. */
213 if (use_first_pass) {
214 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
215 RET(PAM_NEW_AUTHTOK_REQD);
217 if (logmask && LOG_MASK(LOG_DEBUG))
218 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD, user);
220 if (logmask && LOG_MASK(LOG_DEBUG))
221 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
225 if (!(use_first_pass || try_first_pass)) {
230 if (password == NULL) {
235 RET(PAM_AUTH_ERR); /* shouldn't happen */
237 try_first_pass = 0; /* we come back if try_first_pass==1 below */
239 if (pam_convp == NULL || pam_convp->conv == NULL) {
240 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
245 pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
246 if (errcode != PAM_SUCCESS || password == NULL) {
247 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
250 if (password[0] == '\0') {
251 if (logmask && LOG_MASK(LOG_DEBUG))
252 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD);
253 RET(PAM_NEW_AUTHTOK_REQD);
256 * We aren't going to free the password later (we will wipe it,
257 * though), because the storage for it if we get it from other
258 * paths may belong to someone else. Since we do need to free
259 * this storage, copy it to a buffer that won't need to be freed
260 * later, and free this storage now.
263 strncpy(my_password_buf, password, sizeof(my_password_buf));
264 my_password_buf[sizeof(my_password_buf) - 1] = '\0';
265 memset(password, 0, strlen(password));
267 password = my_password_buf;
270 * We only set a PAG here, if we haven't got one before in
271 * pam_sm_authenticate() or if it was destroyed by the application
273 if ((!refresh_token) && (getPAG() == -1)) {
274 if (logmask && LOG_MASK(LOG_DEBUG))
275 syslog(LOG_DEBUG, "New PAG created in pam_setcred()");
277 #ifdef AFS_KERBEROS_ENV
282 if (flags & PAM_REFRESH_CRED) {
284 auth_ok = !do_klog(user, password, "00:00:01", cell_ptr);
285 ktc_ForgetAllTokens();
287 if (ka_VerifyUserPassword(KA_USERAUTH_VERSION, user, /* kerberos name */
289 cell_ptr, /* realm */
290 password, /* password */
292 &reason /* error string */
294 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user,
302 if (flags & PAM_ESTABLISH_CRED) {
304 auth_ok = !do_klog(user, password, NULL, cell_ptr);
306 if (ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, user, /* kerberos name */
308 cell_ptr, /* realm */
309 password, /* password */
310 0, /* default lifetime */
311 &password_expires, 0, /* spare 2 */
312 &reason /* error string */
314 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user,
322 if (!auth_ok && try_first_pass) {
327 /* pam_sm_authenticate should have set this
328 * if (auth_ok && !got_authtok) {
329 * torch_password = 0;
330 * (void) pam_set_item(pamh, PAM_AUTHTOK, password);
335 if (set_expires && !use_klog && (password_expires >= 0)) {
336 strcpy(sbuffer, "PASSWORD_EXPIRES=");
337 strcat(sbuffer, cv2string(&sbuffer[100], password_expires));
338 errcode = pam_putenv(pamh, sbuffer);
339 if (errcode != PAM_SUCCESS)
340 pam_afs_syslog(LOG_ERR, PAMAFS_PASSEXPFAIL, user);
342 #if defined(AFS_KERBEROS_ENV)
344 if (chown(ktc_tkt_string(), upwd->pw_uid, upwd->pw_gid) < 0)
345 pam_afs_syslog(LOG_ERR, PAMAFS_CHOWNKRB, user);
346 sprintf(sbuffer, "KRBTKFILE=%s", ktc_tkt_string());
347 errcode = pam_putenv(pamh, sbuffer);
348 if (errcode != PAM_SUCCESS)
349 pam_afs_syslog(LOG_ERR, PAMAFS_KRBFAIL, user);
360 if (password && torch_password)
361 memset(password, 0, strlen(password));
362 (void)setlogmask(origmask);
363 #ifndef AFS_SUN56_ENV