1 /*
2 * Copyright (c) 2000-2004 Todd C. Miller <Todd.Miller@courtesan.com>
3 *
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.
7 *
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.
15 *
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.
19 */
20
21 #include "config.h"
22
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <stdio.h>
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 # include <stddef.h>
29 #else
30 # ifdef HAVE_STDLIB_H
31 # include <stdlib.h>
32 # endif
33 #endif /* STDC_HEADERS */
34 #ifdef HAVE_STRING_H
35 # include <string.h>
36 #else
37 # ifdef HAVE_STRINGS_H
38 # include <strings.h>
39 # endif
40 #endif /* HAVE_STRING_H */
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif /* HAVE_UNISTD_H */
44 #include <ctype.h>
45 #include <pwd.h>
46 #include <signal.h>
47
48 #include <login_cap.h>
49 #include <bsd_auth.h>
50
51 #include "sudo.h"
52 #include "sudo_auth.h"
53
54 #ifndef lint
55 static const char rcsid[] = "$Sudo: bsdauth.c,v 1.16 2004/06/07 00:02:56 millert Exp $";
56 #endif /* lint */
57
58 extern char *login_style; /* from sudo.c */
59
60 int
bsdauth_init(pw,promptp,auth)61 bsdauth_init(pw, promptp, auth)
62 struct passwd *pw;
63 char **promptp;
64 sudo_auth *auth;
65 {
66 static auth_session_t *as;
67 extern login_cap_t *lc; /* from sudo.c */
68
69 if ((as = auth_open()) == NULL) {
70 log_error(USE_ERRNO|NO_EXIT|NO_MAIL,
71 "unable to begin bsd authentication");
72 return(AUTH_FATAL);
73 }
74
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");
79 auth_close(as);
80 return(AUTH_FATAL);
81 }
82
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");
87 auth_close(as);
88 return(AUTH_FATAL);
89 }
90
91 auth->data = (VOID *) as;
92 return(AUTH_SUCCESS);
93 }
94
95 int
bsdauth_verify(pw,prompt,auth)96 bsdauth_verify(pw, prompt, auth)
97 struct passwd *pw;
98 char *prompt;
99 sudo_auth *auth;
100 {
101 char *pass;
102 char *s;
103 size_t len;
104 int authok = 0;
105 sigaction_t sa, osa;
106 auth_session_t *as = (auth_session_t *) auth->data;
107 extern int nil_pw;
108
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);
114
115 /*
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
119 * S/Key.
120 */
121 if ((s = auth_challenge(as)) == NULL) {
122 pass = tgetpass(prompt, def_passwd_timeout * 60, tgetpass_flags);
123 } else {
124 pass = tgetpass(s, def_passwd_timeout * 60, tgetpass_flags);
125 if (pass && *pass == '\0') {
126 if ((prompt = strrchr(s, '\n')))
127 prompt++;
128 else
129 prompt = s;
130
131 /*
132 * Append '[echo on]' to the last line of the challenge and
133 * reprompt with echo turned on.
134 */
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);
141 free(s);
142 }
143 }
144
145 if (!pass || *pass == '\0') /* ^C or empty password */
146 nil_pw = 1;
147
148 if (pass) {
149 authok = auth_userresponse(as, (char *)pass, 1);
150 zero_bytes(pass, strlen(pass));
151 }
152
153 /* restore old signal handler */
154 (void) sigaction(SIGCHLD, &osa, NULL);
155
156 if (authok)
157 return(AUTH_SUCCESS);
158
159 if ((s = auth_getvalue(as, "errormsg")) != NULL)
160 log_error(NO_EXIT|NO_MAIL, "%s", s);
161 return(AUTH_FAILURE);
162 }
163
164 int
bsdauth_cleanup(pw,auth)165 bsdauth_cleanup(pw, auth)
166 struct passwd *pw;
167 sudo_auth *auth;
168 {
169 auth_session_t *as = (auth_session_t *) auth->data;
170
171 auth_close(as);
172
173 return(AUTH_SUCCESS);
174 }
175