2 * Copyright (c) 1999-2005, 2007-2008 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 */
50 #ifdef HAVE_PAM_PAM_APPL_H
51 # include <pam/pam_appl.h>
53 # include <security/pam_appl.h>
58 # if defined(__LINUX_PAM__)
59 # define PAM_TEXT_DOMAIN "Linux-PAM"
60 # elif defined(__sun__)
61 # define PAM_TEXT_DOMAIN "SUNW_OST_SYSOSPAM"
66 #include "sudo_auth.h"
68 /* Only OpenPAM and Linux PAM use const qualifiers. */
69 #if defined(_OPENPAM) || defined(__LIBPAM_VERSION) || defined(__LINUX_PAM__)
70 # define PAM_CONST const
76 __unused static const char rcsid[] = "$Sudo: pam.c,v 1.66 2008/12/09 23:48:19 millert Exp $";
79 static int sudo_conv __P((int, PAM_CONST struct pam_message **,
80 struct pam_response **, void *));
81 static char *def_prompt;
84 #ifndef PAM_DATA_SILENT
85 #define PAM_DATA_SILENT 0
88 static pam_handle_t *pamh; /* global due to pam_prep_user() */
91 pam_init(pw, promptp, auth)
96 static struct pam_conv pam_conv;
97 static int pam_status;
99 /* Initial PAM setup */
101 auth->data = (void *) &pam_status;
102 pam_conv.conv = sudo_conv;
103 pam_status = pam_start("sudo", pw->pw_name, &pam_conv, &pamh);
104 if (pam_status != PAM_SUCCESS) {
105 log_error(USE_ERRNO|NO_EXIT|NO_MAIL, "unable to initialize PAM");
109 * Some versions of pam_lastlog have a bug that
110 * will cause a crash if PAM_TTY is not set so if
111 * there is no tty, set PAM_TTY to the empty string.
113 if (user_ttypath == NULL)
114 (void) pam_set_item(pamh, PAM_TTY, "");
116 (void) pam_set_item(pamh, PAM_TTY, user_ttypath);
118 return(AUTH_SUCCESS);
122 pam_verify(pw, prompt, auth)
128 int *pam_status = (int *) auth->data;
130 def_prompt = prompt; /* for sudo_conv */
132 /* PAM_SILENT prevents the authentication service from generating output. */
133 *pam_status = pam_authenticate(pamh, PAM_SILENT);
134 switch (*pam_status) {
136 *pam_status = pam_acct_mgmt(pamh, PAM_SILENT);
137 switch (*pam_status) {
139 return(AUTH_SUCCESS);
141 log_error(NO_EXIT|NO_MAIL, "pam_acct_mgmt: %d",
143 return(AUTH_FAILURE);
144 case PAM_NEW_AUTHTOK_REQD:
145 log_error(NO_EXIT|NO_MAIL, "%s, %s",
146 "Account or password is expired",
147 "reset your password and try again");
148 *pam_status = pam_chauthtok(pamh,
149 PAM_CHANGE_EXPIRED_AUTHTOK);
150 if (*pam_status == PAM_SUCCESS)
151 return(AUTH_SUCCESS);
152 if ((s = pam_strerror(pamh, *pam_status)))
153 log_error(NO_EXIT|NO_MAIL, "pam_chauthtok: %s", s);
154 return(AUTH_FAILURE);
155 case PAM_AUTHTOK_EXPIRED:
156 log_error(NO_EXIT|NO_MAIL,
157 "Password expired, contact your system administrator");
159 case PAM_ACCT_EXPIRED:
160 log_error(NO_EXIT|NO_MAIL, "%s %s",
161 "Account expired or PAM config lacks an \"account\"",
162 "section for sudo, contact your system administrator");
168 /* error or ^C from tgetpass() */
172 case PAM_PERM_DENIED:
173 return(AUTH_FAILURE);
175 if ((s = pam_strerror(pamh, *pam_status)))
176 log_error(NO_EXIT|NO_MAIL, "pam_authenticate: %s", s);
182 pam_cleanup(pw, auth)
186 int *pam_status = (int *) auth->data;
188 /* If successful, we can't close the session until pam_prep_user() */
189 if (auth->status == AUTH_SUCCESS)
190 return(AUTH_SUCCESS);
192 *pam_status = pam_end(pamh, *pam_status | PAM_DATA_SILENT);
193 return(*pam_status == PAM_SUCCESS ? AUTH_SUCCESS : AUTH_FAILURE);
203 pam_init(pw, NULL, NULL);
206 * Set PAM_USER to the user we are changing *to* and
207 * set PAM_RUSER to the user we are coming *from*.
208 * We set PAM_RHOST to avoid a bug in Solaris 7 and below.
210 (void) pam_set_item(pamh, PAM_USER, pw->pw_name);
211 (void) pam_set_item(pamh, PAM_RUSER, user_name);
212 (void) pam_set_item(pamh, PAM_RHOST, user_host);
215 * Set credentials (may include resource limits, device ownership, etc).
216 * We don't check the return value here because in Linux-PAM 0.75
217 * it returns the last saved return code, not the return code
218 * for the setcred module. Because we haven't called pam_authenticate(),
219 * this is not set and so pam_setcred() returns PAM_PERM_DENIED.
220 * We can't call pam_acct_mgmt() with Linux-PAM for a similar reason.
222 (void) pam_setcred(pamh, PAM_ESTABLISH_CRED);
224 #ifndef NO_PAM_SESSION
226 * To fully utilize PAM sessions we would need to keep a
227 * sudo process around until the command exits. However, we
228 * can at least cause pam_limits to be run by opening and then
229 * immediately closing the session.
231 if ((eval = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
232 (void) pam_end(pamh, eval | PAM_DATA_SILENT);
233 return(AUTH_FAILURE);
235 (void) pam_close_session(pamh, 0);
238 if (pam_end(pamh, PAM_SUCCESS | PAM_DATA_SILENT) == PAM_SUCCESS)
239 return(AUTH_SUCCESS);
241 return(AUTH_FAILURE);
245 * ``Conversation function'' for PAM.
246 * XXX - does not handle PAM_BINARY_PROMPT
249 sudo_conv(num_msg, msg, response, appdata_ptr)
251 PAM_CONST struct pam_message **msg;
252 struct pam_response **response;
255 struct pam_response *pr;
256 PAM_CONST struct pam_message *pm;
259 int n, flags, std_prompt;
261 if ((*response = malloc(num_msg * sizeof(struct pam_response))) == NULL)
262 return(PAM_SYSTEM_ERR);
263 zero_bytes(*response, num_msg * sizeof(struct pam_response));
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:
273 /* Is the sudo prompt standard? (If so, we'l just use PAM's) */
274 std_prompt = strncmp(def_prompt, "Password:", 9) == 0 &&
275 (def_prompt[9] == '\0' ||
276 (def_prompt[9] == ' ' && def_prompt[10] == '\0'));
278 /* Only override PAM prompt if it matches /^Password: ?/ */
279 #if defined(PAM_TEXT_DOMAIN) && defined(HAVE_DGETTEXT)
280 if (!def_passprompt_override && (std_prompt ||
281 (strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password: ")) &&
282 strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password:")))))
285 if (!def_passprompt_override && (std_prompt ||
286 strncmp(pm->msg, "Password:", 9) || (pm->msg[9] != '\0'
287 && (pm->msg[9] != ' ' || pm->msg[10] != '\0'))))
290 /* Read the password unless interrupted. */
291 pass = tgetpass(prompt, def_passwd_timeout * 60, flags);
293 /* We got ^C instead of a password; abort quickly. */
296 #if defined(__darwin__) || defined(__APPLE__)
302 pr->resp = estrdup(pass);
303 zero_bytes(pass, strlen(pass));
307 (void) puts(pm->msg);
311 (void) fputs(pm->msg, stderr);
312 (void) fputc('\n', stderr);
323 /* Zero and free allocated memory and return an error. */
324 for (pr = *response, n = num_msg; n--; pr++) {
325 if (pr->resp != NULL) {
326 zero_bytes(pr->resp, strlen(pr->resp));
331 zero_bytes(*response, num_msg * sizeof(struct pam_response));
334 return(gotintr ? PAM_AUTH_ERR : PAM_CONV_ERR);