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 #include <security/pam_appl.h>
16 #include <security/pam_modules.h>
22 #include <sys/param.h>
23 #include <afs/kautils.h>
24 #include "afs_message.h"
30 #define RET(x) { retcode = (x); goto out; }
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;
51 int set_expires = 0; /* This option is only used in pam_set_cred() */
52 int got_authtok = 0; /* got PAM_AUTHTOK upon entry */
54 char my_password_buf[256];
55 char *user = NULL, *password = NULL;
56 int torch_password = 1;
58 struct pam_conv *pam_convp = NULL;
61 struct passwd unix_pwd, *upwd = NULL;
62 char upwd_buf[2048]; /* size is a guess. */
66 struct sigaction newAction, origAction;
70 openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
72 origmask = setlogmask(logmask);
75 * Parse the user options. Log an error for any unknown options.
77 for (i = 0; i < argc; i++) {
78 if ( strcasecmp(argv[i], "debug" ) == 0) {
79 logmask |= LOG_MASK(LOG_DEBUG);
80 (void) setlogmask(logmask);
81 } else if (strcasecmp(argv[i], "nowarn" ) == 0) {
83 } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
85 } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
87 } else if (strcasecmp(argv[i], "ignore_root" ) == 0) {
89 } else if (strcasecmp(argv[i], "trust_root" ) == 0) {
91 } else if (strcasecmp(argv[i], "catch_su" ) == 0) {
93 } else if (strcasecmp(argv[i], "setenv_password_expires") == 0) {
96 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
100 /* Later we use try_first_pass to see if we can try again. */
101 /* If use_first_pass is true we don't want to ever try again, */
102 /* so turn that flag off right now. */
103 if (use_first_pass) try_first_pass = 0;
105 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass, try_first_pass);
107 /* Try to get the user-interaction info, if available. */
108 errcode = pam_get_item(pamh, PAM_CONV, (void **) &pam_convp);
109 if (errcode != PAM_SUCCESS) {
110 pam_afs_syslog(LOG_WARNING, PAMAFS_NO_USER_INT);
114 /* Who are we trying to authenticate here? */
115 if ((errcode = pam_get_user(pamh, &user, "login: ")) != PAM_SUCCESS) {
116 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
117 RET(PAM_USER_UNKNOWN);
120 if ((!strncmp ("root", user, 4)) && trust_root) {
121 pam_afs_syslog(LOG_INFO, PAMAFS_TRUSTROOT, user);
125 pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
128 * If the user has a "local" (or via nss, possibly nss_dce) pwent,
129 * and its uid==0, and "ignore_root" was given in pam.conf,
132 #if defined(AFS_HPUX_ENV)
133 #if defined(AFS_HPUX110_ENV)
134 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
135 #else /* AFS_HPUX110_ENV */
136 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
137 if ( i == 0 ) /* getpwnam_r success */
139 #endif /* else AFS_HPUX110_ENV */
140 if (ignore_root && i == 0 && upwd->pw_uid == 0) {
141 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
145 #ifdef AFS_LINUX20_ENV
146 upwd = getpwnam(user);
148 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
150 if (upwd != NULL && upwd->pw_uid == 0) {
152 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
154 } else if (trust_root && !catch_su) {
155 pam_afs_syslog(LOG_INFO, PAMAFS_TRUSTROOT, user);
160 errcode = pam_get_item(pamh, PAM_AUTHTOK, (void **) &password);
161 if (errcode != PAM_SUCCESS || password == NULL) {
162 if (use_first_pass) {
163 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
166 password = NULL; /* In case it isn't already NULL */
167 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
168 } else if (password[0] == '\0') {
169 /* Actually we *did* get one but it was empty. */
171 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
172 RET(PAM_NEW_AUTHTOK_REQD);
174 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
178 if (!(use_first_pass || try_first_pass)) {
183 if (password == NULL) {
188 RET(PAM_AUTH_ERR); /* shouldn't happen */
190 try_first_pass = 0; /* we come back if try_first_pass==1 below */
192 if (pam_convp == NULL || pam_convp->conv == NULL) {
193 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
197 errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
198 if (errcode != PAM_SUCCESS || password == NULL) {
199 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
202 if (password[0] == '\0') {
203 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
204 RET(PAM_NEW_AUTHTOK_REQD);
208 * We aren't going to free the password later (we will wipe it,
209 * though), because the storage for it if we get it from other
210 * paths may belong to someone else. Since we do need to free
211 * this storage, copy it to a buffer that won't need to be freed
212 * later, and free this storage now.
214 strncpy(my_password_buf, password, sizeof(my_password_buf));
215 my_password_buf[sizeof(my_password_buf)-1] = '\0';
216 memset(password, 0, strlen(password));
218 password = my_password_buf;
223 /* Prepare for fork(): set SIGCHLD signal handler to default */
224 sigemptyset(&newAction.sa_mask);
225 newAction.sa_handler = SIG_DFL;
226 newAction.sa_flags = 0;
227 code = sigaction(SIGCHLD, &newAction, &origAction);
229 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
233 /* Fork a process and let it verify authentication. So that any
234 * memory/sockets allocated will get cleaned up when the child
235 * exits: defect 11686.
238 if (cpid <= 0) { /* The child process */
239 code = ka_VerifyUserPassword(KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
240 user, /* kerberos name */
241 (char *)0, /* instance */
242 (char *)0, /* realm */
243 password, /* password */
245 &reason /* error string */ );
247 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
252 if (cpid == 0) exit(auth_ok);
255 rcpid = waitpid(cpid, &status, 0);
256 } while ((rcpid == -1) && (errno == EINTR));
258 if ((rcpid == cpid) && WIFEXITED(status)) {
259 auth_ok = WEXITSTATUS(status);
265 /* Restore old signal handler */
266 code = sigaction(SIGCHLD, &origAction, (struct sigaction *)0);
268 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
271 if (!auth_ok && try_first_pass) {
276 /* We don't care if this fails; all we can do is try. */
277 if (auth_ok && !got_authtok) {
279 pam_set_item(pamh, PAM_AUTHTOK, password);
282 RET(auth_ok ? PAM_SUCCESS : PAM_AUTH_ERR);
287 /* we store the password in the data portion */
288 char* tmp = strdup(password);
289 (void) pam_set_data(pamh, pam_afs_lh, tmp, lc_cleanup);
290 if ( torch_password) memset(password, 0, strlen(password));
292 (void) setlogmask(origmask);
293 #ifndef AFS_SUN56_ENV