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>
19 #include <sys/param.h>
21 #include <afs/kautils.h>
25 #include <security/pam_appl.h>
26 #include <security/pam_modules.h>
28 #include "afs_message.h"
32 #define RET(x) { retcode = (x); goto out; }
35 pam_sm_authenticate(pam_handle_t * pamh, int flags, int argc,
38 int retcode = PAM_SUCCESS;
39 int errcode = PAM_SUCCESS;
42 int logmask = LOG_UPTO(LOG_INFO);
44 int use_first_pass = 0;
45 int try_first_pass = 0;
47 uid_t ignore_uid_id = 0;
48 char my_password_buf[256];
49 char *cell_ptr = NULL;
51 * these options are added to handle stupid apps, which won't call
54 int refresh_token = 0;
60 int set_expires = 0; /* This option is only used in pam_set_cred() */
61 int got_authtok = 0; /* got PAM_AUTHTOK upon entry */
62 char *user = NULL, *password = NULL;
63 long password_expires = -1;
64 int torch_password = 1;
66 struct pam_conv *pam_convp = NULL;
68 struct passwd unix_pwd, *upwd = NULL;
69 char upwd_buf[2048]; /* size is a guess. */
73 struct sigaction newAction, origAction;
77 openlog(pam_afs_ident, LOG_CONS | LOG_PID, LOG_AUTH);
79 origmask = setlogmask(logmask);
82 * Parse the user options. Log an error for any unknown options.
84 for (i = 0; i < argc; i++) {
85 if (strcasecmp(argv[i], "debug") == 0) {
86 logmask |= LOG_MASK(LOG_DEBUG);
87 (void)setlogmask(logmask);
88 } else if (strcasecmp(argv[i], "nowarn") == 0) {
90 } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
92 } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
94 } else if (strcasecmp(argv[i], "ignore_root") == 0) {
97 } else if (strcasecmp(argv[i], "ignore_uid") == 0) {
100 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID,
101 "ignore_uid missing argument");
105 ignore_uid_id = (uid_t) strtol(argv[i], (char **)NULL, 10);
106 if ((ignore_uid_id < 0) || (ignore_uid_id > IGNORE_MAX)) {
108 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID, argv[i]);
111 } else if (strcasecmp(argv[i], "cell") == 0) {
114 pam_afs_syslog(LOG_ERR, PAMAFS_OTHERCELL,
115 "cell missing argument");
118 pam_afs_syslog(LOG_INFO, PAMAFS_OTHERCELL, cell_ptr);
120 } else if (strcasecmp(argv[i], "refresh_token") == 0) {
122 } else if (strcasecmp(argv[i], "set_token") == 0) {
124 } else if (strcasecmp(argv[i], "dont_fork") == 0) {
128 pam_afs_syslog(LOG_ERR, PAMAFS_CONFLICTOPT, "dont_fork");
129 } else if (strcasecmp(argv[i], "use_klog") == 0) {
133 pam_afs_syslog(LOG_ERR, PAMAFS_CONFLICTOPT, "use_klog");
134 } else if (strcasecmp(argv[i], "setenv_password_expires") == 0) {
137 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
141 /* Later we use try_first_pass to see if we can try again. */
142 /* If use_first_pass is true we don't want to ever try again, */
143 /* so turn that flag off right now. */
147 if (logmask && LOG_MASK(LOG_DEBUG))
148 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass,
149 try_first_pass, ignore_uid, ignore_uid_id,
150 refresh_token, set_token, dont_fork, use_klog);
152 /* Try to get the user-interaction info, if available. */
153 errcode = pam_get_item(pamh, PAM_CONV, (const void **)&pam_convp);
154 if (errcode != PAM_SUCCESS) {
155 pam_afs_syslog(LOG_WARNING, PAMAFS_NO_USER_INT);
159 /* Who are we trying to authenticate here? */
161 pam_get_user(pamh, (const char **)&user,
162 "login: ")) != PAM_SUCCESS) {
163 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
164 RET(PAM_USER_UNKNOWN);
167 if (logmask && LOG_MASK(LOG_DEBUG))
168 pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
171 * If the user has a "local" (or via nss, possibly nss_dce) pwent,
172 * and its uid==0, and "ignore_root" was given in pam.conf,
175 /* enhanced: use "ignore_uid <number>" to specify the largest uid
176 * which should be ignored by this module
178 #if defined(AFS_HPUX_ENV) || defined(AFS_DARWIN100_ENV)
179 #if defined(AFS_HPUX110_ENV) || defined(AFS_DARWIN100_ENV)
180 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
181 #else /* AFS_HPUX110_ENV */
182 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
183 if (i == 0) /* getpwnam_r success */
185 #endif /* else AFS_HPUX110_ENV */
186 if (ignore_uid && i == 0 && upwd->pw_uid <= ignore_uid_id) {
187 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
191 #if defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV)
192 upwd = getpwnam(user);
193 #elif defined(_POSIX_PTHREAD_SEMANTICS) && defined(AFS_SUN5_ENV)
194 getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
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