2 * Copyright (c) 2000-2005 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 */
37 # ifdef HAVE_STRINGS_H
40 #endif /* HAVE_STRING_H */
43 #endif /* HAVE_UNISTD_H */
48 #include <login_cap.h>
52 #include "sudo_auth.h"
55 __unused static const char rcsid[] = "$Sudo: bsdauth.c,v 1.16.2.2 2007/06/12 01:28:42 millert Exp $";
58 extern char *login_style; /* from sudo.c */
61 bsdauth_init(pw, promptp, auth)
66 static auth_session_t *as;
67 extern login_cap_t *lc; /* from sudo.c */
69 if ((as = auth_open()) == NULL) {
70 log_error(USE_ERRNO|NO_EXIT|NO_MAIL,
71 "unable to begin bsd authentication");
75 /* XXX - maybe sanity check the auth style earlier? */
76 login_style = login_getstyle(lc, login_style, "auth-sudo");
77 if (login_style == NULL) {
78 log_error(NO_EXIT|NO_MAIL, "invalid authentication type");
83 if (auth_setitem(as, AUTHV_STYLE, login_style) < 0 ||
84 auth_setitem(as, AUTHV_NAME, pw->pw_name) < 0 ||
85 auth_setitem(as, AUTHV_CLASS, login_class) < 0) {
86 log_error(NO_EXIT|NO_MAIL, "unable to setup authentication");
91 auth->data = (VOID *) as;
96 bsdauth_verify(pw, prompt, auth)
106 auth_session_t *as = (auth_session_t *) auth->data;
109 /* save old signal handler */
110 sigemptyset(&sa.sa_mask);
111 sa.sa_flags = SA_RESTART;
112 sa.sa_handler = SIG_DFL;
113 (void) sigaction(SIGCHLD, &sa, &osa);
116 * If there is a challenge then print that instead of the normal
117 * prompt. If the user just hits return we prompt again with echo
118 * turned on, which is useful for challenge/response things like
121 if ((s = auth_challenge(as)) == NULL) {
122 pass = tgetpass(prompt, def_passwd_timeout * 60, tgetpass_flags);
124 pass = tgetpass(s, def_passwd_timeout * 60, tgetpass_flags);
125 if (pass && *pass == '\0') {
126 if ((prompt = strrchr(s, '\n')))
132 * Append '[echo on]' to the last line of the challenge and
133 * reprompt with echo turned on.
135 len = strlen(prompt) - 1;
136 while (isspace(prompt[len]) || prompt[len] == ':')
137 prompt[len--] = '\0';
138 easprintf(&s, "%s [echo on]: ", prompt);
139 pass = tgetpass(s, def_passwd_timeout * 60,
140 tgetpass_flags | TGP_ECHO);
145 if (!pass || *pass == '\0') /* ^C or empty password */
149 authok = auth_userresponse(as, (char *)pass, 1);
150 zero_bytes(pass, strlen(pass));
153 /* restore old signal handler */
154 (void) sigaction(SIGCHLD, &osa, NULL);
157 return(AUTH_SUCCESS);
159 if ((s = auth_getvalue(as, "errormsg")) != NULL)
160 log_error(NO_EXIT|NO_MAIL, "%s", s);
161 return(AUTH_FAILURE);
165 bsdauth_cleanup(pw, auth)
169 auth_session_t *as = (auth_session_t *) auth->data;
173 return(AUTH_SUCCESS);