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>
15 #ifdef HAVE_SYS_WAIT_H
19 #include <afs/kautils.h>
21 #include <security/pam_appl.h>
22 #include <security/pam_modules.h>
24 #include "afs_message.h"
28 #define RET(x) { retcode = (x); goto out; }
31 pam_sm_authenticate(pam_handle_t * pamh, int flags, int argc,
34 int retcode = PAM_SUCCESS;
35 int errcode = PAM_SUCCESS;
38 int logmask = LOG_UPTO(LOG_INFO);
40 int use_first_pass = 0;
41 int try_first_pass = 0;
43 uid_t ignore_uid_id = 0;
44 char my_password_buf[256];
45 char *cell_ptr = NULL;
47 * these options are added to handle stupid apps, which won't call
50 int refresh_token = 0;
56 int set_expires = 0; /* This option is only used in pam_set_cred() */
57 int got_authtok = 0; /* got PAM_AUTHTOK upon entry */
58 PAM_CONST char *user = NULL, *password = NULL;
59 afs_int32 password_expires = -1;
60 char *torch_password = NULL;
62 PAM_CONST struct pam_conv *pam_convp = NULL;
64 struct passwd unix_pwd, *upwd = NULL;
65 char upwd_buf[2048]; /* size is a guess. */
69 struct sigaction newAction, origAction;
73 openlog(pam_afs_ident, LOG_CONS | LOG_PID, LOG_AUTH);
75 origmask = setlogmask(logmask);
78 * Parse the user options. Log an error for any unknown options.
80 for (i = 0; i < argc; i++) {
81 if (strcasecmp(argv[i], "debug") == 0) {
82 logmask |= LOG_MASK(LOG_DEBUG);
83 (void)setlogmask(logmask);
84 } else if (strcasecmp(argv[i], "nowarn") == 0) {
86 } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
88 } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
90 } else if (strcasecmp(argv[i], "ignore_root") == 0) {
93 } else if (strcasecmp(argv[i], "ignore_uid") == 0) {
96 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID,
97 "ignore_uid missing argument");
101 ignore_uid_id = (uid_t) strtol(argv[i], (char **)NULL, 10);
102 if ((ignore_uid_id < 0) || (ignore_uid_id > IGNORE_MAX)) {
104 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID, argv[i]);
107 } else if (strcasecmp(argv[i], "cell") == 0) {
110 pam_afs_syslog(LOG_ERR, PAMAFS_OTHERCELL,
111 "cell missing argument");
113 cell_ptr = (char *)argv[i];
114 pam_afs_syslog(LOG_INFO, PAMAFS_OTHERCELL, cell_ptr);
116 } else if (strcasecmp(argv[i], "refresh_token") == 0) {
118 } else if (strcasecmp(argv[i], "set_token") == 0) {
120 } else if (strcasecmp(argv[i], "dont_fork") == 0) {
124 pam_afs_syslog(LOG_ERR, PAMAFS_CONFLICTOPT, "dont_fork");
125 } else if (strcasecmp(argv[i], "use_klog") == 0) {
129 pam_afs_syslog(LOG_ERR, PAMAFS_CONFLICTOPT, "use_klog");
130 } else if (strcasecmp(argv[i], "setenv_password_expires") == 0) {
133 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
137 /* Later we use try_first_pass to see if we can try again. */
138 /* If use_first_pass is true we don't want to ever try again, */
139 /* so turn that flag off right now. */
143 if (logmask && LOG_MASK(LOG_DEBUG))
144 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass,
145 try_first_pass, ignore_uid, ignore_uid_id,
146 refresh_token, set_token, dont_fork, use_klog);
148 /* Try to get the user-interaction info, if available. */
149 errcode = pam_get_item(pamh, PAM_CONV, (PAM_CONST void **)&pam_convp);
150 if (errcode != PAM_SUCCESS) {
151 pam_afs_syslog(LOG_WARNING, PAMAFS_NO_USER_INT);
155 /* Who are we trying to authenticate here? */
157 pam_get_user(pamh, &user,
158 "login: ")) != PAM_SUCCESS) {
159 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
160 RET(PAM_USER_UNKNOWN);
163 if (logmask && LOG_MASK(LOG_DEBUG))
164 pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
167 * If the user has a "local" (or via nss, possibly nss_dce) pwent,
168 * and its uid==0, and "ignore_root" was given in pam.conf,
171 /* enhanced: use "ignore_uid <number>" to specify the largest uid
172 * which should be ignored by this module
174 #if defined(AFS_HPUX_ENV) || defined(AFS_DARWIN100_ENV) || defined(AFS_SUN5_ENV)
175 #if defined(AFS_HPUX110_ENV) || defined(AFS_DARWIN100_ENV) || defined(AFS_SUN5_ENV)
176 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
177 #else /* AFS_HPUX110_ENV */
178 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
179 if (i == 0) /* getpwnam_r success */
181 #endif /* else AFS_HPUX110_ENV */
182 if (ignore_uid && i == 0 && upwd && upwd->pw_uid <= ignore_uid_id) {
183 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
187 #if defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV)
188 upwd = getpwnam(user);
190 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
192 if (ignore_uid && upwd != NULL && upwd->pw_uid <= ignore_uid_id) {
193 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
197 errcode = pam_get_item(pamh, PAM_AUTHTOK, (PAM_CONST void **)&password);
198 if (errcode != PAM_SUCCESS || password == NULL) {
199 if (use_first_pass) {
200 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
203 password = NULL; /* In case it isn't already NULL */
204 if (logmask && LOG_MASK(LOG_DEBUG))
205 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
206 } else if (password[0] == '\0') {
207 /* Actually we *did* get one but it was empty. */
208 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
209 RET(PAM_NEW_AUTHTOK_REQD);
211 if (logmask && LOG_MASK(LOG_DEBUG))
212 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
215 if (!(use_first_pass || try_first_pass)) {
220 if (password == NULL) {
221 char *prompt_password;
224 RET(PAM_AUTH_ERR); /* shouldn't happen */
226 try_first_pass = 0; /* we come back if try_first_pass==1 below */
228 if (pam_convp == NULL || pam_convp->conv == NULL) {
229 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
233 errcode = pam_afs_prompt(pam_convp, &prompt_password, 0, PAMAFS_PWD_PROMPT);
234 if (errcode != PAM_SUCCESS || prompt_password == NULL) {
235 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
238 if (prompt_password[0] == '\0') {
239 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
240 RET(PAM_NEW_AUTHTOK_REQD);
244 * We aren't going to free the password later (we will wipe it,
245 * though), because the storage for it if we get it from other
246 * paths may belong to someone else. Since we do need to free
247 * this storage, copy it to a buffer that won't need to be freed
248 * later, and free this storage now.
251 strncpy(my_password_buf, prompt_password, sizeof(my_password_buf));
252 my_password_buf[sizeof(my_password_buf) - 1] = '\0';
253 memset(prompt_password, 0, strlen(prompt_password));
254 free(prompt_password);
255 password = torch_password = my_password_buf;
259 /* Be sure to allocate a PAG here if we should set a token,
260 * All of the remaining stuff to authenticate the user and to
261 * get a token is done in a child process - if not suppressed by the config,
263 * But dont get a PAG if the refresh_token option was set
264 * We have to do this in such a way because some
265 * apps (such as screensavers) wont call setcred but authenticate :-(
267 if (!refresh_token) {
269 #ifdef AFS_KERBEROS_ENV
272 if (logmask && LOG_MASK(LOG_DEBUG))
273 syslog(LOG_DEBUG, "New PAG created in pam_authenticate()");
277 /* Prepare for fork(): set SIGCHLD signal handler to default */
278 sigemptyset(&newAction.sa_mask);
279 newAction.sa_handler = SIG_DFL;
280 newAction.sa_flags = 0;
281 code = sigaction(SIGCHLD, &newAction, &origAction);
283 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
287 /* Fork a process and let it verify authentication. So that any
288 * memory/sockets allocated will get cleaned up when the child
289 * exits: defect 11686.
291 if (use_klog) { /* used by kdm 2.x */
292 if (refresh_token || set_token) {
293 i = do_klog(user, password, NULL, cell_ptr);
295 i = do_klog(user, password, "00:00:01", cell_ptr);
296 ktc_ForgetAllTokens();
298 if (logmask && LOG_MASK(LOG_DEBUG))
299 syslog(LOG_DEBUG, "do_klog returned %d", i);
302 if (logmask && LOG_MASK(LOG_DEBUG))
303 syslog(LOG_DEBUG, "forking ...");
305 if (cpid <= 0) { /* The child process */
306 if (logmask && LOG_MASK(LOG_DEBUG))
307 syslog(LOG_DEBUG, "in child");
308 if (refresh_token || set_token)
309 code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, (char *)user, /* kerberos name */
311 cell_ptr, /* realm */
312 (char *)password, /* password */
313 0, /* default lifetime */
314 &password_expires, 0, /* spare 2 */
316 /* error string */ );
318 code = ka_VerifyUserPassword(KA_USERAUTH_VERSION, (char *)user, /* kerberos name */
320 cell_ptr, /* realm */
321 (char *)password, /* password */
323 &reason /* error string */ );
325 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user,
331 if (logmask && LOG_MASK(LOG_DEBUG))
332 syslog(LOG_DEBUG, "child: auth_ok=%d", auth_ok);
337 if (logmask && LOG_MASK(LOG_DEBUG))
338 syslog(LOG_DEBUG, "in parent, waiting ...");
339 rcpid = waitpid(cpid, &status, 0);
340 } while ((rcpid == -1) && (errno == EINTR));
342 if ((rcpid == cpid) && WIFEXITED(status)) {
343 auth_ok = WEXITSTATUS(status);
347 if (logmask && LOG_MASK(LOG_DEBUG))
348 syslog(LOG_DEBUG, "parent: auth_ok=%d", auth_ok);
351 /* Restore old signal handler */
352 code = sigaction(SIGCHLD, &origAction, NULL);
354 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
356 } else { /* dont_fork, used by httpd */
357 if (logmask && LOG_MASK(LOG_DEBUG))
358 syslog(LOG_DEBUG, "dont_fork");
359 if (refresh_token || set_token)
360 code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, (char *)user, /* kerberos name */
362 cell_ptr, /* realm */
363 (char *)password, /* password */
364 0, /* default lifetime */
365 &password_expires, 0, /* spare 2 */
366 &reason /* error string */ );
368 code = ka_VerifyUserPassword(KA_USERAUTH_VERSION, (char *)user, /* kerberos name */
370 cell_ptr, /* realm */
371 (char *)password, /* password */
373 &reason /* error string */ );
374 if (logmask && LOG_MASK(LOG_DEBUG))
375 syslog(LOG_DEBUG, "dont_fork, code = %d", code);
377 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
382 if (logmask && LOG_MASK(LOG_DEBUG))
383 syslog(LOG_DEBUG, "dont_fork: auth_ok=%d", auth_ok);
386 if (!auth_ok && try_first_pass) {
391 /* We don't care if this fails; all we can do is try. */
392 /* It is not reasonable to store the password only if it was correct
393 * because it could satisfy another module that is called in the chain
397 torch_password = NULL;
398 (void)pam_set_item(pamh, PAM_AUTHTOK, password);
401 if (logmask && LOG_MASK(LOG_DEBUG))
402 syslog(LOG_DEBUG, "leaving auth: auth_ok=%d", auth_ok);
404 RET(PAM_USER_UNKNOWN);
405 RET(auth_ok ? PAM_SUCCESS : PAM_AUTH_ERR);
409 /* we store the password in the data portion */
410 char *tmp = strdup(password);
411 (void)pam_set_data(pamh, pam_afs_lh, tmp, lc_cleanup);
413 memset(torch_password, 0, strlen(torch_password));
415 (void)setlogmask(origmask);