2 * Copyright (c) 1999-2005 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 #include <sys/types.h>
24 #include <sys/param.h>
33 #endif /* STDC_HEADERS */
35 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
40 # ifdef HAVE_STRINGS_H
43 #endif /* HAVE_STRING_H */
46 #endif /* HAVE_UNISTD_H */
49 #ifdef HAVE_PAM_PAM_APPL_H
50 # include <pam/pam_appl.h>
52 # include <security/pam_appl.h>
57 # if defined(__LINUX_PAM__)
58 # define PAM_TEXT_DOMAIN "Linux-PAM"
59 # elif defined(__sun__)
60 # define PAM_TEXT_DOMAIN "SUNW_OST_SYSOSPAM"
65 #include "sudo_auth.h"
67 /* Only OpenPAM and Linux PAM use const qualifiers. */
68 #if defined(_OPENPAM) || defined(__LIBPAM_VERSION) || defined(__LINUX_PAM__)
69 # define PAM_CONST const
75 __unused static const char rcsid[] = "$Sudo: pam.c,v 1.43.2.9 2007/12/02 17:13:52 millert Exp $";
78 static int sudo_conv __P((int, PAM_CONST struct pam_message **,
79 struct pam_response **, VOID *));
80 static char *def_prompt;
82 #ifndef PAM_DATA_SILENT
83 #define PAM_DATA_SILENT 0
86 static pam_handle_t *pamh; /* global due to pam_prep_user() */
89 pam_init(pw, promptp, auth)
94 static struct pam_conv pam_conv;
95 static int pam_status;
97 /* Initial PAM setup */
99 auth->data = (VOID *) &pam_status;
100 pam_conv.conv = sudo_conv;
101 pam_status = pam_start("sudo", pw->pw_name, &pam_conv, &pamh);
102 if (pam_status != PAM_SUCCESS) {
103 log_error(USE_ERRNO|NO_EXIT|NO_MAIL, "unable to initialize PAM");
107 * Some versions of pam_lastlog have a bug that
108 * will cause a crash if PAM_TTY is not set so if
109 * there is no tty, set PAM_TTY to the empty string.
111 if (user_ttypath == NULL)
112 (void) pam_set_item(pamh, PAM_TTY, "");
114 (void) pam_set_item(pamh, PAM_TTY, user_ttypath);
116 return(AUTH_SUCCESS);
120 pam_verify(pw, prompt, auth)
126 int *pam_status = (int *) auth->data;
128 def_prompt = prompt; /* for sudo_conv */
130 /* PAM_SILENT prevents the authentication service from generating output. */
131 *pam_status = pam_authenticate(pamh, PAM_SILENT);
132 switch (*pam_status) {
134 *pam_status = pam_acct_mgmt(pamh, PAM_SILENT);
135 switch (*pam_status) {
137 return(AUTH_SUCCESS);
139 log_error(NO_EXIT|NO_MAIL, "pam_acct_mgmt: %d",
141 return(AUTH_FAILURE);
142 case PAM_NEW_AUTHTOK_REQD:
143 log_error(NO_EXIT|NO_MAIL, "%s, %s",
144 "Account or password is expired",
145 "reset your password and try again");
146 *pam_status = pam_chauthtok(pamh,
147 PAM_CHANGE_EXPIRED_AUTHTOK);
148 if (*pam_status == PAM_SUCCESS)
149 return(AUTH_SUCCESS);
150 if ((s = pam_strerror(pamh, *pam_status)))
151 log_error(NO_EXIT|NO_MAIL, "pam_chauthtok: %s", s);
152 return(AUTH_FAILURE);
153 case PAM_AUTHTOK_EXPIRED:
154 log_error(NO_EXIT|NO_MAIL,
155 "Password expired, contact your system administrator");
157 case PAM_ACCT_EXPIRED:
158 log_error(NO_EXIT|NO_MAIL, "%s %s",
159 "Account expired or PAM config lacks an \"account\"",
160 "section for sudo, contact your system administrator");
166 case PAM_PERM_DENIED:
167 return(AUTH_FAILURE);
169 if ((s = pam_strerror(pamh, *pam_status)))
170 log_error(NO_EXIT|NO_MAIL, "pam_authenticate: %s", s);
176 pam_cleanup(pw, auth)
180 int *pam_status = (int *) auth->data;
182 /* If successful, we can't close the session until pam_prep_user() */
183 if (auth->status == AUTH_SUCCESS)
184 return(AUTH_SUCCESS);
186 *pam_status = pam_end(pamh, *pam_status | PAM_DATA_SILENT);
187 return(*pam_status == PAM_SUCCESS ? AUTH_SUCCESS : AUTH_FAILURE);
197 pam_init(pw, NULL, NULL);
200 * Set PAM_USER to the user we are changing *to* and
201 * set PAM_RUSER to the user we are coming *from*.
202 * We set PAM_RHOST to avoid a bug in Solaris 7 and below.
204 (void) pam_set_item(pamh, PAM_USER, pw->pw_name);
205 (void) pam_set_item(pamh, PAM_RUSER, user_name);
206 (void) pam_set_item(pamh, PAM_RHOST, user_host);
209 * Set credentials (may include resource limits, device ownership, etc).
210 * We don't check the return value here because in Linux-PAM 0.75
211 * it returns the last saved return code, not the return code
212 * for the setcred module. Because we haven't called pam_authenticate(),
213 * this is not set and so pam_setcred() returns PAM_PERM_DENIED.
214 * We can't call pam_acct_mgmt() with Linux-PAM for a similar reason.
216 (void) pam_setcred(pamh, PAM_ESTABLISH_CRED);
218 #ifndef NO_PAM_SESSION
220 * To fully utilize PAM sessions we would need to keep a
221 * sudo process around until the command exits. However, we
222 * can at least cause pam_limits to be run by opening and then
223 * immediately closing the session.
225 if ((eval = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
226 (void) pam_end(pamh, eval | PAM_DATA_SILENT);
227 return(AUTH_FAILURE);
229 (void) pam_close_session(pamh, 0);
232 if (pam_end(pamh, PAM_SUCCESS | PAM_DATA_SILENT) == PAM_SUCCESS)
233 return(AUTH_SUCCESS);
235 return(AUTH_FAILURE);
239 * ``Conversation function'' for PAM.
240 * XXX - does not handle PAM_BINARY_PROMPT
243 sudo_conv(num_msg, msg, response, appdata_ptr)
245 PAM_CONST struct pam_message **msg;
246 struct pam_response **response;
249 struct pam_response *pr;
250 PAM_CONST struct pam_message *pm;
253 int n, flags, std_prompt;
256 if ((*response = malloc(num_msg * sizeof(struct pam_response))) == NULL)
257 return(PAM_CONV_ERR);
258 zero_bytes(*response, num_msg * sizeof(struct pam_response));
260 /* Is the sudo prompt standard? (If so, we'l just use PAM's) */
261 std_prompt = strncmp(def_prompt, "Password:", 9) == 0 &&
262 (def_prompt[9] == '\0' ||
263 (def_prompt[9] == ' ' && def_prompt[10] == '\0'));
265 for (pr = *response, pm = *msg, n = num_msg; n--; pr++, pm++) {
266 flags = tgetpass_flags;
267 switch (pm->msg_style) {
268 case PAM_PROMPT_ECHO_ON:
269 SET(flags, TGP_ECHO);
270 case PAM_PROMPT_ECHO_OFF:
272 /* Only override PAM prompt if it matches /^Password: ?/ */
273 #if defined(PAM_TEXT_DOMAIN) && defined(HAVE_DGETTEXT)
274 if (!def_passprompt_override && (std_prompt ||
275 (strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password: ")) &&
276 strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password:")))))
279 if (!def_passprompt_override && (std_prompt ||
280 strncmp(pm->msg, "Password:", 9) || (pm->msg[9] != '\0'
281 && (pm->msg[9] != ' ' || pm->msg[10] != '\0'))))
284 /* Read the password. */
285 pass = tgetpass(prompt, def_passwd_timeout * 60, flags);
287 /* We got ^C instead of a password; abort quickly. */
291 pr->resp = estrdup(pass);
292 if (*pr->resp == '\0')
293 nil_pw = 1; /* empty password */
295 zero_bytes(pass, strlen(pass));
299 (void) puts(pm->msg);
303 (void) fputs(pm->msg, stderr);
304 (void) fputc('\n', stderr);
315 /* Zero and free allocated memory and return an error. */
316 for (pr = *response, n = num_msg; n--; pr++) {
317 if (pr->resp != NULL) {
318 zero_bytes(pr->resp, strlen(pr->resp));
323 zero_bytes(*response, num_msg * sizeof(struct pam_response));
326 return(PAM_CONV_ERR);