2 * Copyright (c) 2000-2005, 2007-2008, 2010
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(pw, promptp, auth)
62 static auth_session_t *as;
63 extern login_cap_t *lc; /* from sudo.c */
65 if ((as = auth_open()) == NULL) {
66 log_error(USE_ERRNO|NO_EXIT|NO_MAIL,
67 "unable to begin bsd authentication");
71 /* XXX - maybe sanity check the auth style earlier? */
72 login_style = login_getstyle(lc, login_style, "auth-sudo");
73 if (login_style == NULL) {
74 log_error(NO_EXIT|NO_MAIL, "invalid authentication type");
79 if (auth_setitem(as, AUTHV_STYLE, login_style) < 0 ||
80 auth_setitem(as, AUTHV_NAME, pw->pw_name) < 0 ||
81 auth_setitem(as, AUTHV_CLASS, login_class) < 0) {
82 log_error(NO_EXIT|NO_MAIL, "unable to setup authentication");
87 auth->data = (void *) as;
92 bsdauth_verify(pw, prompt, auth)
102 auth_session_t *as = (auth_session_t *) auth->data;
104 /* save old signal handler */
105 sigemptyset(&sa.sa_mask);
106 sa.sa_flags = SA_RESTART;
107 sa.sa_handler = SIG_DFL;
108 (void) sigaction(SIGCHLD, &sa, &osa);
111 * If there is a challenge then print that instead of the normal
112 * prompt. If the user just hits return we prompt again with echo
113 * turned on, which is useful for challenge/response things like
116 if ((s = auth_challenge(as)) == NULL) {
117 pass = tgetpass(prompt, def_passwd_timeout * 60, tgetpass_flags);
119 pass = tgetpass(s, def_passwd_timeout * 60, tgetpass_flags);
120 if (pass && *pass == '\0') {
121 if ((prompt = strrchr(s, '\n')))
127 * Append '[echo on]' to the last line of the challenge and
128 * reprompt with echo turned on.
130 len = strlen(prompt) - 1;
131 while (isspace(prompt[len]) || prompt[len] == ':')
132 prompt[len--] = '\0';
133 easprintf(&s, "%s [echo on]: ", prompt);
134 pass = tgetpass(s, def_passwd_timeout * 60,
135 tgetpass_flags | TGP_ECHO);
141 authok = auth_userresponse(as, pass, 1);
142 zero_bytes(pass, strlen(pass));
145 /* restore old signal handler */
146 (void) sigaction(SIGCHLD, &osa, NULL);
149 return(AUTH_SUCCESS);
154 if ((s = auth_getvalue(as, "errormsg")) != NULL)
155 log_error(NO_EXIT|NO_MAIL, "%s", s);
156 return(AUTH_FAILURE);
160 bsdauth_cleanup(pw, auth)
164 auth_session_t *as = (auth_session_t *) auth->data;
168 return(AUTH_SUCCESS);