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 <afsconfig.h>
11 #include <afs/param.h>
21 #include <sys/param.h>
23 #include <afs/kautils.h>
27 #include <security/pam_appl.h>
28 #include <security/pam_modules.h>
30 #include "afs_message.h"
34 #define RET(x) { retcode = (x); goto out; }
37 pam_sm_authenticate(pam_handle_t * pamh, int flags, int argc,
40 int retcode = PAM_SUCCESS;
41 int errcode = PAM_SUCCESS;
44 int logmask = LOG_UPTO(LOG_INFO);
46 int use_first_pass = 0;
47 int try_first_pass = 0;
49 uid_t ignore_uid_id = 0;
50 char my_password_buf[256];
51 char *cell_ptr = NULL;
53 * these options are added to handle stupid apps, which won't call
56 int refresh_token = 0;
62 int set_expires = 0; /* This option is only used in pam_set_cred() */
63 int got_authtok = 0; /* got PAM_AUTHTOK upon entry */
64 char *user = NULL, *password = NULL;
65 long password_expires = -1;
66 int torch_password = 1;
68 struct pam_conv *pam_convp = NULL;
70 struct passwd unix_pwd, *upwd = NULL;
71 char upwd_buf[2048]; /* size is a guess. */
75 struct sigaction newAction, origAction;
79 openlog(pam_afs_ident, LOG_CONS | LOG_PID, LOG_AUTH);
81 origmask = setlogmask(logmask);
84 * Parse the user options. Log an error for any unknown options.
86 for (i = 0; i < argc; i++) {
87 if (strcasecmp(argv[i], "debug") == 0) {
88 logmask |= LOG_MASK(LOG_DEBUG);
89 (void)setlogmask(logmask);
90 } else if (strcasecmp(argv[i], "nowarn") == 0) {
92 } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
94 } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
96 } else if (strcasecmp(argv[i], "ignore_root") == 0) {
99 } else if (strcasecmp(argv[i], "ignore_uid") == 0) {
102 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID,
103 "ignore_uid missing argument");
107 ignore_uid_id = (uid_t) strtol(argv[i], (char **)NULL, 10);
108 if ((ignore_uid_id < 0) || (ignore_uid_id > IGNORE_MAX)) {
110 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID, argv[i]);
113 } else if (strcasecmp(argv[i], "cell") == 0) {
116 pam_afs_syslog(LOG_ERR, PAMAFS_OTHERCELL,
117 "cell missing argument");
120 pam_afs_syslog(LOG_INFO, PAMAFS_OTHERCELL, cell_ptr);
122 } else if (strcasecmp(argv[i], "refresh_token") == 0) {
124 } else if (strcasecmp(argv[i], "set_token") == 0) {
126 } else if (strcasecmp(argv[i], "dont_fork") == 0) {
130 pam_afs_syslog(LOG_ERR, PAMAFS_CONFLICTOPT, "dont_fork");
131 } else if (strcasecmp(argv[i], "use_klog") == 0) {
135 pam_afs_syslog(LOG_ERR, PAMAFS_CONFLICTOPT, "use_klog");
136 } else if (strcasecmp(argv[i], "setenv_password_expires") == 0) {
139 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
143 /* Later we use try_first_pass to see if we can try again. */
144 /* If use_first_pass is true we don't want to ever try again, */
145 /* so turn that flag off right now. */
149 if (logmask && LOG_MASK(LOG_DEBUG))
150 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass,
151 try_first_pass, ignore_uid, ignore_uid_id,
152 refresh_token, set_token, dont_fork, use_klog);
154 /* Try to get the user-interaction info, if available. */
155 errcode = pam_get_item(pamh, PAM_CONV, (const void **)&pam_convp);
156 if (errcode != PAM_SUCCESS) {
157 pam_afs_syslog(LOG_WARNING, PAMAFS_NO_USER_INT);
161 /* Who are we trying to authenticate here? */
163 pam_get_user(pamh, (const char **)&user,
164 "login: ")) != PAM_SUCCESS) {
165 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
166 RET(PAM_USER_UNKNOWN);
169 if (logmask && LOG_MASK(LOG_DEBUG))
170 pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
173 * If the user has a "local" (or via nss, possibly nss_dce) pwent,
174 * and its uid==0, and "ignore_root" was given in pam.conf,
177 /* enhanced: use "ignore_uid <number>" to specify the largest uid
178 * which should be ignored by this module
180 #if defined(AFS_HPUX_ENV)
181 #if defined(AFS_HPUX110_ENV)
182 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
183 #else /* AFS_HPUX110_ENV */
184 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
185 if (i == 0) /* getpwnam_r success */
187 #endif /* else AFS_HPUX110_ENV */
188 if (ignore_uid && i == 0 && upwd->pw_uid <= ignore_uid_id) {
189 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
193 #if defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_NBSD_ENV)
194 upwd = getpwnam(user);
196 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
198 if (ignore_uid && upwd != NULL && upwd->pw_uid <= ignore_uid_id) {
199 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
203 errcode = pam_get_item(pamh, PAM_AUTHTOK, (const void **)&password);
204 if (errcode != PAM_SUCCESS || password == NULL) {
205 if (use_first_pass) {
206 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
209 password = NULL; /* In case it isn't already NULL */
210 if (logmask && LOG_MASK(LOG_DEBUG))
211 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
212 } else if (password[0] == '\0') {
213 /* Actually we *did* get one but it was empty. */
215 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
216 RET(PAM_NEW_AUTHTOK_REQD);
218 if (logmask && LOG_MASK(LOG_DEBUG))
219 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
223 if (!(use_first_pass || try_first_pass)) {
228 if (password == NULL) {
233 RET(PAM_AUTH_ERR); /* shouldn't happen */
235 try_first_pass = 0; /* we come back if try_first_pass==1 below */
237 if (pam_convp == NULL || pam_convp->conv == NULL) {
238 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
242 errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
243 if (errcode != PAM_SUCCESS || password == NULL) {
244 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
247 if (password[0] == '\0') {
248 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
249 RET(PAM_NEW_AUTHTOK_REQD);
253 * We aren't going to free the password later (we will wipe it,
254 * though), because the storage for it if we get it from other
255 * paths may belong to someone else. Since we do need to free
256 * this storage, copy it to a buffer that won't need to be freed
257 * later, and free this storage now.
260 strncpy(my_password_buf, password, sizeof(my_password_buf));
261 my_password_buf[sizeof(my_password_buf) - 1] = '\0';
262 memset(password, 0, strlen(password));
264 password = my_password_buf;
268 /* Be sure to allocate a PAG here if we should set a token,
269 * All of the remaining stuff to authenticate the user and to
270 * get a token is done in a child process - if not suppressed by the config,
272 * But dont get a PAG if the refresh_token option was set
273 * We have to do this in such a way because some
274 * apps (such as screensavers) wont call setcred but authenticate :-(
276 if (!refresh_token) {
278 #ifdef AFS_KERBEROS_ENV
281 if (logmask && LOG_MASK(LOG_DEBUG))
282 syslog(LOG_DEBUG, "New PAG created in pam_authenticate()");
286 /* Prepare for fork(): set SIGCHLD signal handler to default */
287 sigemptyset(&newAction.sa_mask);
288 newAction.sa_handler = SIG_DFL;
289 newAction.sa_flags = 0;
290 code = sigaction(SIGCHLD, &newAction, &origAction);
292 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
296 /* Fork a process and let it verify authentication. So that any
297 * memory/sockets allocated will get cleaned up when the child
298 * exits: defect 11686.
300 if (use_klog) { /* used by kdm 2.x */
301 if (refresh_token || set_token) {
302 i = do_klog(user, password, NULL, cell_ptr);
304 i = do_klog(user, password, "00:00:01", cell_ptr);
305 ktc_ForgetAllTokens();
307 if (logmask && LOG_MASK(LOG_DEBUG))
308 syslog(LOG_DEBUG, "do_klog returned %d", i);
311 if (logmask && LOG_MASK(LOG_DEBUG))
312 syslog(LOG_DEBUG, "forking ...");
314 if (cpid <= 0) { /* The child process */
315 if (logmask && LOG_MASK(LOG_DEBUG))
316 syslog(LOG_DEBUG, "in child");
317 if (refresh_token || set_token)
318 code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, user, /* kerberos name */
320 cell_ptr, /* realm */
321 password, /* password */
322 0, /* default lifetime */
323 &password_expires, 0, /* spare 2 */
325 /* error string */ );
327 code = ka_VerifyUserPassword(KA_USERAUTH_VERSION, user, /* kerberos name */
329 cell_ptr, /* realm */
330 password, /* password */
332 &reason /* error string */ );
334 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user,
340 if (logmask && LOG_MASK(LOG_DEBUG))
341 syslog(LOG_DEBUG, "child: auth_ok=%d", auth_ok);
346 if (logmask && LOG_MASK(LOG_DEBUG))
347 syslog(LOG_DEBUG, "in parent, waiting ...");
348 rcpid = waitpid(cpid, &status, 0);
349 } while ((rcpid == -1) && (errno == EINTR));
351 if ((rcpid == cpid) && WIFEXITED(status)) {
352 auth_ok = WEXITSTATUS(status);
356 if (logmask && LOG_MASK(LOG_DEBUG))
357 syslog(LOG_DEBUG, "parent: auth_ok=%d", auth_ok);
360 /* Restore old signal handler */
361 code = sigaction(SIGCHLD, &origAction, NULL);
363 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
365 } else { /* dont_fork, used by httpd */
366 if (logmask && LOG_MASK(LOG_DEBUG))
367 syslog(LOG_DEBUG, "dont_fork");
368 if (refresh_token || set_token)
369 code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, user, /* kerberos name */
371 cell_ptr, /* realm */
372 password, /* password */
373 0, /* default lifetime */
374 &password_expires, 0, /* spare 2 */
375 &reason /* error string */ );
377 code = ka_VerifyUserPassword(KA_USERAUTH_VERSION, user, /* kerberos name */
379 cell_ptr, /* realm */
380 password, /* password */
382 &reason /* error string */ );
383 if (logmask && LOG_MASK(LOG_DEBUG))
384 syslog(LOG_DEBUG, "dont_fork, code = %d", code);
386 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
391 if (logmask && LOG_MASK(LOG_DEBUG))
392 syslog(LOG_DEBUG, "dont_fork: auth_ok=%d", auth_ok);
395 if (!auth_ok && try_first_pass) {
400 /* We don't care if this fails; all we can do is try. */
401 /* It is not reasonable to store the password only if it was correct
402 * because it could satisfy another module that is called in the chain
407 (void)pam_set_item(pamh, PAM_AUTHTOK, password);
410 if (logmask && LOG_MASK(LOG_DEBUG))
411 syslog(LOG_DEBUG, "leaving auth: auth_ok=%d", auth_ok);
413 RET(PAM_USER_UNKNOWN);
414 RET(auth_ok ? PAM_SUCCESS : PAM_AUTH_ERR);
418 /* we store the password in the data portion */
419 char *tmp = strdup(password);
420 (void)pam_set_data(pamh, pam_afs_lh, tmp, lc_cleanup);
422 memset(password, 0, strlen(password));
424 (void)setlogmask(origmask);
425 #ifndef AFS_SUN56_ENV