2 * Copyright (c) 2000-2005, 2007-2008, 2010-2011
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>
25 #include <sys/param.h>
34 #endif /* STDC_HEADERS */
37 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_STRING_H */
43 #endif /* HAVE_UNISTD_H */
48 #include <login_cap.h>
52 #include "sudo_auth.h"
54 extern char *login_style; /* from sudo.c */
57 bsdauth_init(struct passwd *pw, sudo_auth *auth)
59 static auth_session_t *as;
60 extern login_cap_t *lc; /* from sudo.c */
62 if ((as = auth_open()) == NULL) {
63 log_error(USE_ERRNO|NO_EXIT|NO_MAIL,
64 _("unable to begin bsd authentication"));
68 /* XXX - maybe sanity check the auth style earlier? */
69 login_style = login_getstyle(lc, login_style, "auth-sudo");
70 if (login_style == NULL) {
71 log_error(NO_EXIT|NO_MAIL, _("invalid authentication type"));
76 if (auth_setitem(as, AUTHV_STYLE, login_style) < 0 ||
77 auth_setitem(as, AUTHV_NAME, pw->pw_name) < 0 ||
78 auth_setitem(as, AUTHV_CLASS, login_class) < 0) {
79 log_error(NO_EXIT|NO_MAIL, _("unable to setup authentication"));
84 auth->data = (void *) as;
89 bsdauth_verify(struct passwd *pw, char *prompt, sudo_auth *auth)
96 auth_session_t *as = (auth_session_t *) auth->data;
98 /* save old signal handler */
99 sigemptyset(&sa.sa_mask);
100 sa.sa_flags = SA_RESTART;
101 sa.sa_handler = SIG_DFL;
102 (void) sigaction(SIGCHLD, &sa, &osa);
105 * If there is a challenge then print that instead of the normal
106 * prompt. If the user just hits return we prompt again with echo
107 * turned on, which is useful for challenge/response things like
110 if ((s = auth_challenge(as)) == NULL) {
111 pass = auth_getpass(prompt, def_passwd_timeout * 60, SUDO_CONV_PROMPT_ECHO_OFF);
113 pass = auth_getpass(prompt, def_passwd_timeout * 60, SUDO_CONV_PROMPT_ECHO_OFF);
114 if (pass && *pass == '\0') {
115 if ((prompt = strrchr(s, '\n')))
121 * Append '[echo on]' to the last line of the challenge and
122 * reprompt with echo turned on.
124 len = strlen(prompt) - 1;
125 while (isspace(prompt[len]) || prompt[len] == ':')
126 prompt[len--] = '\0';
127 easprintf(&s, "%s [echo on]: ", prompt);
128 pass = auth_getpass(prompt, def_passwd_timeout * 60,
129 SUDO_CONV_PROMPT_ECHO_ON);
135 authok = auth_userresponse(as, pass, 1);
136 zero_bytes(pass, strlen(pass));
139 /* restore old signal handler */
140 (void) sigaction(SIGCHLD, &osa, NULL);
148 if ((s = auth_getvalue(as, "errormsg")) != NULL)
149 log_error(NO_EXIT|NO_MAIL, "%s", s);
154 bsdauth_cleanup(struct passwd *pw, sudo_auth *auth)
156 auth_session_t *as = (auth_session_t *) auth->data;