2 * Copyright (c) 1999-2005, 2007-2011 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 */
36 #endif /* HAVE_STRING_H */
39 #endif /* HAVE_STRINGS_H */
42 #endif /* HAVE_UNISTD_H */
46 #ifdef HAVE_PAM_PAM_APPL_H
47 # include <pam/pam_appl.h>
49 # include <security/pam_appl.h>
53 # if defined(__LINUX_PAM__)
54 # define PAM_TEXT_DOMAIN "Linux-PAM"
55 # elif defined(__sun__)
56 # define PAM_TEXT_DOMAIN "SUNW_OST_SYSOSPAM"
61 #include "sudo_auth.h"
63 /* Only OpenPAM and Linux PAM use const qualifiers. */
64 #if defined(_OPENPAM) || defined(OPENPAM_VERSION) || \
65 defined(__LIBPAM_VERSION) || defined(__LINUX_PAM__)
66 # define PAM_CONST const
71 static int converse(int, PAM_CONST struct pam_message **,
72 struct pam_response **, void *);
73 static char *def_prompt = "Password:";
76 #ifndef PAM_DATA_SILENT
77 #define PAM_DATA_SILENT 0
80 static pam_handle_t *pamh;
83 sudo_pam_init(struct passwd *pw, sudo_auth *auth)
85 static struct pam_conv pam_conv;
86 static int pam_status;
87 debug_decl(sudo_pam_init, SUDO_DEBUG_AUTH)
89 /* Initial PAM setup */
91 auth->data = (void *) &pam_status;
92 pam_conv.conv = converse;
94 if (ISSET(sudo_mode, MODE_LOGIN_SHELL))
95 pam_status = pam_start("sudo-i", pw->pw_name, &pam_conv, &pamh);
98 pam_status = pam_start("sudo", pw->pw_name, &pam_conv, &pamh);
99 if (pam_status != PAM_SUCCESS) {
100 log_error(USE_ERRNO|NO_EXIT|NO_MAIL, _("unable to initialize PAM"));
101 debug_return_int(AUTH_FATAL);
105 * Set PAM_RUSER to the invoking user (the "from" user).
106 * We set PAM_RHOST to avoid a bug in Solaris 7 and below.
108 (void) pam_set_item(pamh, PAM_RUSER, user_name);
110 (void) pam_set_item(pamh, PAM_RHOST, user_host);
114 * Some versions of pam_lastlog have a bug that
115 * will cause a crash if PAM_TTY is not set so if
116 * there is no tty, set PAM_TTY to the empty string.
118 if (user_ttypath == NULL)
119 (void) pam_set_item(pamh, PAM_TTY, "");
121 (void) pam_set_item(pamh, PAM_TTY, user_ttypath);
123 debug_return_int(AUTH_SUCCESS);
127 sudo_pam_verify(struct passwd *pw, char *prompt, sudo_auth *auth)
130 int *pam_status = (int *) auth->data;
131 debug_decl(sudo_pam_verify, SUDO_DEBUG_AUTH)
133 def_prompt = prompt; /* for converse */
135 /* PAM_SILENT prevents the authentication service from generating output. */
136 *pam_status = pam_authenticate(pamh, PAM_SILENT);
137 switch (*pam_status) {
139 *pam_status = pam_acct_mgmt(pamh, PAM_SILENT);
140 switch (*pam_status) {
142 debug_return_int(AUTH_SUCCESS);
144 log_error(NO_EXIT|NO_MAIL, _("account validation failure, "
145 "is your account locked?"));
146 debug_return_int(AUTH_FATAL);
147 case PAM_NEW_AUTHTOK_REQD:
148 log_error(NO_EXIT|NO_MAIL, _("Account or password is "
149 "expired, reset your password and try again"));
150 *pam_status = pam_chauthtok(pamh,
151 PAM_CHANGE_EXPIRED_AUTHTOK);
152 if (*pam_status == PAM_SUCCESS)
153 debug_return_int(AUTH_SUCCESS);
154 if ((s = pam_strerror(pamh, *pam_status)))
155 log_error(NO_EXIT|NO_MAIL, _("pam_chauthtok: %s"), s);
156 debug_return_int(AUTH_FAILURE);
157 case PAM_AUTHTOK_EXPIRED:
158 log_error(NO_EXIT|NO_MAIL,
159 _("Password expired, contact your system administrator"));
160 debug_return_int(AUTH_FATAL);
161 case PAM_ACCT_EXPIRED:
162 log_error(NO_EXIT|NO_MAIL,
163 _("Account expired or PAM config lacks an \"account\" "
164 "section for sudo, contact your system administrator"));
165 debug_return_int(AUTH_FATAL);
170 /* error or ^C from tgetpass() */
171 debug_return_int(AUTH_INTR);
174 case PAM_PERM_DENIED:
175 debug_return_int(AUTH_FAILURE);
177 if ((s = pam_strerror(pamh, *pam_status)))
178 log_error(NO_EXIT|NO_MAIL, _("pam_authenticate: %s"), s);
179 debug_return_int(AUTH_FATAL);
184 sudo_pam_cleanup(struct passwd *pw, sudo_auth *auth)
186 int *pam_status = (int *) auth->data;
187 debug_decl(sudo_pam_cleanup, SUDO_DEBUG_AUTH)
189 /* If successful, we can't close the session until pam_end_session() */
190 if (*pam_status == AUTH_SUCCESS)
191 debug_return_int(AUTH_SUCCESS);
193 *pam_status = pam_end(pamh, *pam_status | PAM_DATA_SILENT);
195 debug_return_int(*pam_status == PAM_SUCCESS ? AUTH_SUCCESS : AUTH_FAILURE);
199 sudo_pam_begin_session(struct passwd *pw, sudo_auth *auth)
201 int status = PAM_SUCCESS;
202 debug_decl(sudo_pam_begin_session, SUDO_DEBUG_AUTH)
205 * If there is no valid user we cannot open a PAM session.
206 * This is not an error as sudo can run commands with arbitrary
207 * uids, it just means we are done from a session management standpoint.
211 (void) pam_end(pamh, PAM_SUCCESS | PAM_DATA_SILENT);
218 * Update PAM_USER to reference the user we are running the command
219 * as, as opposed to the user we authenticated as.
221 (void) pam_set_item(pamh, PAM_USER, pw->pw_name);
224 * Set credentials (may include resource limits, device ownership, etc).
225 * We don't check the return value here because in Linux-PAM 0.75
226 * it returns the last saved return code, not the return code
227 * for the setcred module. Because we haven't called pam_authenticate(),
228 * this is not set and so pam_setcred() returns PAM_PERM_DENIED.
229 * We can't call pam_acct_mgmt() with Linux-PAM for a similar reason.
231 (void) pam_setcred(pamh, PAM_ESTABLISH_CRED);
233 #ifndef NO_PAM_SESSION
234 status = pam_open_session(pamh, 0);
235 if (status != PAM_SUCCESS) {
236 (void) pam_end(pamh, status | PAM_DATA_SILENT);
242 debug_return_int(status == PAM_SUCCESS ? AUTH_SUCCESS : AUTH_FAILURE);
246 sudo_pam_end_session(struct passwd *pw, sudo_auth *auth)
248 int status = PAM_SUCCESS;
249 debug_decl(sudo_pam_end_session, SUDO_DEBUG_AUTH)
252 #ifndef NO_PAM_SESSION
254 * Update PAM_USER to reference the user we are running the command
255 * as to match the call to pam_open_session().
257 (void) pam_set_item(pamh, PAM_USER, pw->pw_name);
258 (void) pam_close_session(pamh, PAM_SILENT);
260 status = pam_end(pamh, PAM_SUCCESS | PAM_DATA_SILENT);
264 debug_return_int(status == PAM_SUCCESS ? AUTH_SUCCESS : AUTH_FAILURE);
268 * ``Conversation function'' for PAM.
269 * XXX - does not handle PAM_BINARY_PROMPT
272 converse(int num_msg, PAM_CONST struct pam_message **msg,
273 struct pam_response **response, void *appdata_ptr)
275 struct pam_response *pr;
276 PAM_CONST struct pam_message *pm;
279 int n, type, std_prompt;
280 debug_decl(converse, SUDO_DEBUG_AUTH)
282 if ((*response = malloc(num_msg * sizeof(struct pam_response))) == NULL)
283 debug_return_int(PAM_SYSTEM_ERR);
284 zero_bytes(*response, num_msg * sizeof(struct pam_response));
286 for (pr = *response, pm = *msg, n = num_msg; n--; pr++, pm++) {
287 type = SUDO_CONV_PROMPT_ECHO_OFF;
288 switch (pm->msg_style) {
289 case PAM_PROMPT_ECHO_ON:
290 type = SUDO_CONV_PROMPT_ECHO_ON;
291 case PAM_PROMPT_ECHO_OFF:
294 /* Error out if the last password read was interrupted. */
298 /* Is the sudo prompt standard? (If so, we'l just use PAM's) */
299 std_prompt = strncmp(def_prompt, "Password:", 9) == 0 &&
300 (def_prompt[9] == '\0' ||
301 (def_prompt[9] == ' ' && def_prompt[10] == '\0'));
303 /* Only override PAM prompt if it matches /^Password: ?/ */
304 #if defined(PAM_TEXT_DOMAIN) && defined(HAVE_LIBINTL_H)
305 if (!def_passprompt_override && (std_prompt ||
306 (strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password: ")) &&
307 strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password:")))))
310 if (!def_passprompt_override && (std_prompt ||
311 strncmp(pm->msg, "Password:", 9) || (pm->msg[9] != '\0'
312 && (pm->msg[9] != ' ' || pm->msg[10] != '\0'))))
315 /* Read the password unless interrupted. */
316 pass = auth_getpass(prompt, def_passwd_timeout * 60, type);
318 /* We got ^C instead of a password; abort quickly. */
321 #if defined(__darwin__) || defined(__APPLE__)
327 pr->resp = estrdup(pass);
328 zero_bytes(pass, strlen(pass));
332 (void) puts(pm->msg);
336 (void) fputs(pm->msg, stderr);
337 (void) fputc('\n', stderr);
345 debug_return_int(PAM_SUCCESS);
348 /* Zero and free allocated memory and return an error. */
349 for (pr = *response, n = num_msg; n--; pr++) {
350 if (pr->resp != NULL) {
351 zero_bytes(pr->resp, strlen(pr->resp));
356 zero_bytes(*response, num_msg * sizeof(struct pam_response));
359 debug_return_int(gotintr ? PAM_AUTH_ERR : PAM_CONV_ERR);