2 * Copyright (c) 2000-2005, 2007-2008, 2010-2013
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 #include <sys/types.h>
33 #endif /* STDC_HEADERS */
36 #endif /* HAVE_STRING_H */
39 #endif /* HAVE_STRING_H */
42 #endif /* HAVE_UNISTD_H */
47 #include <login_cap.h>
51 #include "sudo_auth.h"
53 # ifndef LOGIN_DEFROOTCLASS
54 # define LOGIN_DEFROOTCLASS "daemon"
57 extern char *login_style; /* from sudoers.c */
59 struct bsdauth_state {
65 bsdauth_init(struct passwd *pw, sudo_auth *auth)
67 static struct bsdauth_state state;
68 debug_decl(bsdauth_init, SUDO_DEBUG_AUTH)
70 /* Get login class based on auth user, which may not be invoking user. */
71 if (pw->pw_class && *pw->pw_class)
72 state.lc = login_getclass(pw->pw_class);
74 state.lc = login_getclass(pw->pw_uid ? LOGIN_DEFCLASS : LOGIN_DEFROOTCLASS);
75 if (state.lc == NULL) {
76 log_warning(USE_ERRNO|NO_MAIL,
77 N_("unable to get login class for user %s"), pw->pw_name);
78 debug_return_int(AUTH_FATAL);
81 if ((state.as = auth_open()) == NULL) {
82 log_warning(USE_ERRNO|NO_MAIL,
83 N_("unable to begin bsd authentication"));
84 login_close(state.lc);
85 debug_return_int(AUTH_FATAL);
88 /* XXX - maybe sanity check the auth style earlier? */
89 login_style = login_getstyle(state.lc, login_style, "auth-sudo");
90 if (login_style == NULL) {
91 log_warning(NO_MAIL, N_("invalid authentication type"));
93 login_close(state.lc);
94 debug_return_int(AUTH_FATAL);
97 if (auth_setitem(state.as, AUTHV_STYLE, login_style) < 0 ||
98 auth_setitem(state.as, AUTHV_NAME, pw->pw_name) < 0 ||
99 auth_setitem(state.as, AUTHV_CLASS, login_class) < 0) {
100 log_warning(NO_MAIL, N_("unable to setup authentication"));
101 auth_close(state.as);
102 login_close(state.lc);
103 debug_return_int(AUTH_FATAL);
106 auth->data = (void *) &state;
107 debug_return_int(AUTH_SUCCESS);
111 bsdauth_verify(struct passwd *pw, char *prompt, sudo_auth *auth)
118 auth_session_t *as = ((struct bsdauth_state *) auth->data)->as;
119 debug_decl(bsdauth_verify, SUDO_DEBUG_AUTH)
121 /* save old signal handler */
122 sigemptyset(&sa.sa_mask);
123 sa.sa_flags = SA_RESTART;
124 sa.sa_handler = SIG_DFL;
125 (void) sigaction(SIGCHLD, &sa, &osa);
128 * If there is a challenge then print that instead of the normal
129 * prompt. If the user just hits return we prompt again with echo
130 * turned on, which is useful for challenge/response things like
133 if ((s = auth_challenge(as)) == NULL) {
134 pass = auth_getpass(prompt, def_passwd_timeout * 60, SUDO_CONV_PROMPT_ECHO_OFF);
136 pass = auth_getpass(prompt, def_passwd_timeout * 60, SUDO_CONV_PROMPT_ECHO_OFF);
137 if (pass && *pass == '\0') {
138 if ((prompt = strrchr(s, '\n')))
144 * Append '[echo on]' to the last line of the challenge and
145 * reprompt with echo turned on.
147 len = strlen(prompt) - 1;
148 while (isspace(prompt[len]) || prompt[len] == ':')
149 prompt[len--] = '\0';
150 easprintf(&s, "%s [echo on]: ", prompt);
151 pass = auth_getpass(prompt, def_passwd_timeout * 60,
152 SUDO_CONV_PROMPT_ECHO_ON);
158 authok = auth_userresponse(as, pass, 1);
159 zero_bytes(pass, strlen(pass));
162 /* restore old signal handler */
163 (void) sigaction(SIGCHLD, &osa, NULL);
166 debug_return_int(AUTH_SUCCESS);
169 debug_return_int(AUTH_INTR);
171 if ((s = auth_getvalue(as, "errormsg")) != NULL)
172 log_warning(NO_MAIL, "%s", s);
173 debug_return_int(AUTH_FAILURE);
177 bsdauth_cleanup(struct passwd *pw, sudo_auth *auth)
179 struct bsdauth_state *state = auth->data;
180 debug_decl(bsdauth_cleanup, SUDO_DEBUG_AUTH)
183 auth_close(state->as);
184 login_close(state->lc);
187 debug_return_int(AUTH_SUCCESS);