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_DFBSD_ENV) || defined(AFS_NBSD_ENV)
194 upwd = getpwnam(user);
195 #elif defined(_POSIX_PTHREAD_SEMANTICS) && defined(AFS_SUN5_ENV)
196 getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
198 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
200 if (ignore_uid && upwd != NULL && upwd->pw_uid <= ignore_uid_id) {
201 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
205 errcode = pam_get_item(pamh, PAM_AUTHTOK, (const void **)&password);
206 if (errcode != PAM_SUCCESS || password == NULL) {
207 if (use_first_pass) {
208 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
211 password = NULL; /* In case it isn't already NULL */
212 if (logmask && LOG_MASK(LOG_DEBUG))
213 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
214 } else if (password[0] == '\0') {
215 /* Actually we *did* get one but it was empty. */
217 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
218 RET(PAM_NEW_AUTHTOK_REQD);
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);
244 errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
245 if (errcode != PAM_SUCCESS || password == NULL) {
246 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
249 if (password[0] == '\0') {
250 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
251 RET(PAM_NEW_AUTHTOK_REQD);
255 * We aren't going to free the password later (we will wipe it,
256 * though), because the storage for it if we get it from other
257 * paths may belong to someone else. Since we do need to free
258 * this storage, copy it to a buffer that won't need to be freed
259 * later, and free this storage now.
262 strncpy(my_password_buf, password, sizeof(my_password_buf));
263 my_password_buf[sizeof(my_password_buf) - 1] = '\0';
264 memset(password, 0, strlen(password));
266 password = my_password_buf;
270 /* Be sure to allocate a PAG here if we should set a token,
271 * All of the remaining stuff to authenticate the user and to
272 * get a token is done in a child process - if not suppressed by the config,
274 * But dont get a PAG if the refresh_token option was set
275 * We have to do this in such a way because some
276 * apps (such as screensavers) wont call setcred but authenticate :-(
278 if (!refresh_token) {
280 #ifdef AFS_KERBEROS_ENV
283 if (logmask && LOG_MASK(LOG_DEBUG))
284 syslog(LOG_DEBUG, "New PAG created in pam_authenticate()");
288 /* Prepare for fork(): set SIGCHLD signal handler to default */
289 sigemptyset(&newAction.sa_mask);
290 newAction.sa_handler = SIG_DFL;
291 newAction.sa_flags = 0;
292 code = sigaction(SIGCHLD, &newAction, &origAction);
294 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
298 /* Fork a process and let it verify authentication. So that any
299 * memory/sockets allocated will get cleaned up when the child
300 * exits: defect 11686.
302 if (use_klog) { /* used by kdm 2.x */
303 if (refresh_token || set_token) {
304 i = do_klog(user, password, NULL, cell_ptr);
306 i = do_klog(user, password, "00:00:01", cell_ptr);
307 ktc_ForgetAllTokens();
309 if (logmask && LOG_MASK(LOG_DEBUG))
310 syslog(LOG_DEBUG, "do_klog returned %d", i);
313 if (logmask && LOG_MASK(LOG_DEBUG))
314 syslog(LOG_DEBUG, "forking ...");
316 if (cpid <= 0) { /* The child process */
317 if (logmask && LOG_MASK(LOG_DEBUG))
318 syslog(LOG_DEBUG, "in child");
319 if (refresh_token || set_token)
320 code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, user, /* kerberos name */
322 cell_ptr, /* realm */
323 password, /* password */
324 0, /* default lifetime */
325 &password_expires, 0, /* spare 2 */
327 /* error string */ );
329 code = ka_VerifyUserPassword(KA_USERAUTH_VERSION, user, /* kerberos name */
331 cell_ptr, /* realm */
332 password, /* password */
334 &reason /* error string */ );
336 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user,
342 if (logmask && LOG_MASK(LOG_DEBUG))
343 syslog(LOG_DEBUG, "child: auth_ok=%d", auth_ok);
348 if (logmask && LOG_MASK(LOG_DEBUG))
349 syslog(LOG_DEBUG, "in parent, waiting ...");
350 rcpid = waitpid(cpid, &status, 0);
351 } while ((rcpid == -1) && (errno == EINTR));
353 if ((rcpid == cpid) && WIFEXITED(status)) {
354 auth_ok = WEXITSTATUS(status);
358 if (logmask && LOG_MASK(LOG_DEBUG))
359 syslog(LOG_DEBUG, "parent: auth_ok=%d", auth_ok);
362 /* Restore old signal handler */
363 code = sigaction(SIGCHLD, &origAction, NULL);
365 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
367 } else { /* dont_fork, used by httpd */
368 if (logmask && LOG_MASK(LOG_DEBUG))
369 syslog(LOG_DEBUG, "dont_fork");
370 if (refresh_token || set_token)
371 code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, user, /* kerberos name */
373 cell_ptr, /* realm */
374 password, /* password */
375 0, /* default lifetime */
376 &password_expires, 0, /* spare 2 */
377 &reason /* error string */ );
379 code = ka_VerifyUserPassword(KA_USERAUTH_VERSION, user, /* kerberos name */
381 cell_ptr, /* realm */
382 password, /* password */
384 &reason /* error string */ );
385 if (logmask && LOG_MASK(LOG_DEBUG))
386 syslog(LOG_DEBUG, "dont_fork, code = %d", code);
388 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
393 if (logmask && LOG_MASK(LOG_DEBUG))
394 syslog(LOG_DEBUG, "dont_fork: auth_ok=%d", auth_ok);
397 if (!auth_ok && try_first_pass) {
402 /* We don't care if this fails; all we can do is try. */
403 /* It is not reasonable to store the password only if it was correct
404 * because it could satisfy another module that is called in the chain
409 (void)pam_set_item(pamh, PAM_AUTHTOK, password);
412 if (logmask && LOG_MASK(LOG_DEBUG))
413 syslog(LOG_DEBUG, "leaving auth: auth_ok=%d", auth_ok);
415 RET(PAM_USER_UNKNOWN);
416 RET(auth_ok ? PAM_SUCCESS : PAM_AUTH_ERR);
420 /* we store the password in the data portion */
421 char *tmp = strdup(password);
422 (void)pam_set_data(pamh, pam_afs_lh, tmp, lc_cleanup);
424 memset(password, 0, strlen(password));
426 (void)setlogmask(origmask);
427 #ifndef AFS_SUN56_ENV