Imported Upstream version 1.7.0
[debian/sudo] / auth / bsdauth.c
1 /*
2  * Copyright (c) 2000-2005, 2007-2008 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 __unused static const char rcsid[] = "$Sudo: bsdauth.c,v 1.23 2008/11/09 14:13:13 millert Exp $";
56 #endif /* lint */
57
58 extern char *login_style;               /* from sudo.c */
59
60 int
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
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
108     /* save old signal handler */
109     sigemptyset(&sa.sa_mask);
110     sa.sa_flags = SA_RESTART;
111     sa.sa_handler = SIG_DFL;
112     (void) sigaction(SIGCHLD, &sa, &osa);
113
114     /*
115      * If there is a challenge then print that instead of the normal
116      * prompt.  If the user just hits return we prompt again with echo
117      * turned on, which is useful for challenge/response things like
118      * S/Key.
119      */
120     if ((s = auth_challenge(as)) == NULL) {
121         pass = tgetpass(prompt, def_passwd_timeout * 60, tgetpass_flags);
122     } else {
123         pass = tgetpass(s, def_passwd_timeout * 60, tgetpass_flags);
124         if (pass && *pass == '\0') {
125             if ((prompt = strrchr(s, '\n')))
126                 prompt++;
127             else
128                 prompt = s;
129
130             /*
131              * Append '[echo on]' to the last line of the challenge and
132              * reprompt with echo turned on.
133              */
134             len = strlen(prompt) - 1;
135             while (isspace(prompt[len]) || prompt[len] == ':')
136                 prompt[len--] = '\0';
137             easprintf(&s, "%s [echo on]: ", prompt);
138             pass = tgetpass(s, def_passwd_timeout * 60,
139                 tgetpass_flags | TGP_ECHO);
140             free(s);
141         }
142     }
143
144     if (pass) {
145         authok = auth_userresponse(as, pass, 1);
146         zero_bytes(pass, strlen(pass));
147     }
148
149     /* restore old signal handler */
150     (void) sigaction(SIGCHLD, &osa, NULL);
151
152     if (authok)
153         return(AUTH_SUCCESS);
154
155     if (!pass)
156         return(AUTH_INTR);
157
158     if ((s = auth_getvalue(as, "errormsg")) != NULL)
159         log_error(NO_EXIT|NO_MAIL, "%s", s);
160     return(AUTH_FAILURE);
161 }
162
163 int
164 bsdauth_cleanup(pw, auth)
165     struct passwd *pw;
166     sudo_auth *auth;
167 {
168     auth_session_t *as = (auth_session_t *) auth->data;
169
170     auth_close(as);
171
172     return(AUTH_SUCCESS);
173 }