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 <security/pam_appl.h>
11 #include <security/pam_modules.h>
17 #include <afs/param.h>
18 #include <sys/param.h>
19 #include <afs/kautils.h>
20 #include "afs_message.h"
26 #define RET(x) { retcode = (x); goto out; }
36 int retcode = PAM_SUCCESS;
37 int errcode = PAM_SUCCESS;
40 int logmask = LOG_UPTO(LOG_INFO);
42 int use_first_pass = 0;
43 int try_first_pass = 0;
47 int set_expires = 0; /* This option is only used in pam_set_cred() */
48 int got_authtok = 0; /* got PAM_AUTHTOK upon entry */
50 char my_password_buf[256];
51 char *user = NULL, *password = NULL;
52 int torch_password = 1;
54 struct pam_conv *pam_convp = NULL;
57 struct passwd unix_pwd, *upwd = NULL;
58 char upwd_buf[2048]; /* size is a guess. */
62 struct sigaction newAction, origAction;
66 openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
68 origmask = setlogmask(logmask);
71 * Parse the user options. Log an error for any unknown options.
73 for (i = 0; i < argc; i++) {
74 if ( strcasecmp(argv[i], "debug" ) == 0) {
75 logmask |= LOG_MASK(LOG_DEBUG);
76 (void) setlogmask(logmask);
77 } else if (strcasecmp(argv[i], "nowarn" ) == 0) {
79 } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
81 } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
83 } else if (strcasecmp(argv[i], "ignore_root" ) == 0) {
85 } else if (strcasecmp(argv[i], "trust_root" ) == 0) {
87 } else if (strcasecmp(argv[i], "catch_su" ) == 0) {
89 } else if (strcasecmp(argv[i], "setenv_password_expires") == 0) {
92 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
96 /* Later we use try_first_pass to see if we can try again. */
97 /* If use_first_pass is true we don't want to ever try again, */
98 /* so turn that flag off right now. */
99 if (use_first_pass) try_first_pass = 0;
101 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass, try_first_pass);
103 /* Try to get the user-interaction info, if available. */
104 errcode = pam_get_item(pamh, PAM_CONV, (void **) &pam_convp);
105 if (errcode != PAM_SUCCESS) {
106 pam_afs_syslog(LOG_WARNING, PAMAFS_NO_USER_INT);
110 /* Who are we trying to authenticate here? */
111 if ((errcode = pam_get_user(pamh, &user, "login: ")) != PAM_SUCCESS) {
112 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
113 RET(PAM_USER_UNKNOWN);
116 if ((!strncmp ("root", user, 4)) && trust_root) {
117 pam_afs_syslog(LOG_INFO, PAMAFS_TRUSTROOT, user);
121 pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
124 * If the user has a "local" (or via nss, possibly nss_dce) pwent,
125 * and its uid==0, and "ignore_root" was given in pam.conf,
128 #if defined(AFS_HPUX_ENV)
129 #if defined(AFS_HPUX110_ENV)
130 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
131 #else /* AFS_HPUX110_ENV */
132 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
133 if ( i == 0 ) /* getpwnam_r success */
135 #endif /* else AFS_HPUX110_ENV */
136 if (ignore_root && i == 0 && upwd->pw_uid == 0) {
137 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
141 #ifdef AFS_LINUX20_ENV
142 upwd = getpwnam(user);
144 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
146 if (upwd != NULL && upwd->pw_uid == 0) {
148 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
150 } else if (trust_root && !catch_su) {
151 pam_afs_syslog(LOG_INFO, PAMAFS_TRUSTROOT, user);
156 errcode = pam_get_item(pamh, PAM_AUTHTOK, (void **) &password);
157 if (errcode != PAM_SUCCESS || password == NULL) {
158 if (use_first_pass) {
159 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
162 password = NULL; /* In case it isn't already NULL */
163 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
164 } else if (password[0] == '\0') {
165 /* Actually we *did* get one but it was empty. */
167 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
168 RET(PAM_NEW_AUTHTOK_REQD);
170 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
174 if (!(use_first_pass || try_first_pass)) {
179 if (password == NULL) {
184 RET(PAM_AUTH_ERR); /* shouldn't happen */
186 try_first_pass = 0; /* we come back if try_first_pass==1 below */
188 if (pam_convp == NULL || pam_convp->conv == NULL) {
189 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
193 errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
194 if (errcode != PAM_SUCCESS || password == NULL) {
195 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
198 if (password[0] == '\0') {
199 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
200 RET(PAM_NEW_AUTHTOK_REQD);
204 * We aren't going to free the password later (we will wipe it,
205 * though), because the storage for it if we get it from other
206 * paths may belong to someone else. Since we do need to free
207 * this storage, copy it to a buffer that won't need to be freed
208 * later, and free this storage now.
210 strncpy(my_password_buf, password, sizeof(my_password_buf));
211 my_password_buf[sizeof(my_password_buf)-1] = '\0';
212 memset(password, 0, strlen(password));
214 password = my_password_buf;
219 /* Prepare for fork(): set SIGCHLD signal handler to default */
220 sigemptyset(&newAction.sa_mask);
221 newAction.sa_handler = SIG_DFL;
222 newAction.sa_flags = 0;
223 code = sigaction(SIGCHLD, &newAction, &origAction);
225 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
229 /* Fork a process and let it verify authentication. So that any
230 * memory/sockets allocated will get cleaned up when the child
231 * exits: defect 11686.
234 if (cpid <= 0) { /* The child process */
235 code = ka_VerifyUserPassword(KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
236 user, /* kerberos name */
237 (char *)0, /* instance */
238 (char *)0, /* realm */
239 password, /* password */
241 &reason /* error string */ );
243 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
248 if (cpid == 0) exit(auth_ok);
251 rcpid = waitpid(cpid, &status, 0);
252 } while ((rcpid == -1) && (errno == EINTR));
254 if ((rcpid == cpid) && WIFEXITED(status)) {
255 auth_ok = WEXITSTATUS(status);
261 /* Restore old signal handler */
262 code = sigaction(SIGCHLD, &origAction, (struct sigaction *)0);
264 pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
267 if (!auth_ok && try_first_pass) {
272 /* We don't care if this fails; all we can do is try. */
273 if (auth_ok && !got_authtok) {
275 pam_set_item(pamh, PAM_AUTHTOK, password);
278 RET(auth_ok ? PAM_SUCCESS : PAM_AUTH_ERR);
283 /* we store the password in the data portion */
284 char* tmp = strdup(password);
285 (void) pam_set_data(pamh, pam_afs_lh, tmp, lc_cleanup);
286 if ( torch_password) memset(password, 0, strlen(password));
288 (void) setlogmask(origmask);
289 #ifndef AFS_SUN56_ENV