727df13b08fab31533f4665ae5fa9037ee27bfab
[debian/sudo] / auth / bsdauth.c
1 /*
2  * Copyright (c) 2000-2005, 2007-2008, 2010
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
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.
8  *
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.
16  *
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.
20  */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <stdio.h>
27 #ifdef STDC_HEADERS
28 # include <stdlib.h>
29 # include <stddef.h>
30 #else
31 # ifdef HAVE_STDLIB_H
32 #  include <stdlib.h>
33 # endif
34 #endif /* STDC_HEADERS */
35 #ifdef HAVE_STRING_H
36 # include <string.h>
37 #endif /* HAVE_STRING_H */
38 #ifdef HAVE_STRINGS_H
39 # include <strings.h>
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 extern char *login_style;               /* from sudo.c */
55
56 int
57 bsdauth_init(pw, promptp, auth)
58     struct passwd *pw;
59     char **promptp;
60     sudo_auth *auth;
61 {
62     static auth_session_t *as;
63     extern login_cap_t *lc;                     /* from sudo.c */
64
65     if ((as = auth_open()) == NULL) {
66         log_error(USE_ERRNO|NO_EXIT|NO_MAIL,
67             "unable to begin bsd authentication");
68         return AUTH_FATAL;
69     }
70
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");
75         auth_close(as);
76         return AUTH_FATAL;
77     }
78
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");
83         auth_close(as);
84         return AUTH_FATAL;
85     }
86
87     auth->data = (void *) as;
88     return AUTH_SUCCESS;
89 }
90
91 int
92 bsdauth_verify(pw, prompt, auth)
93     struct passwd *pw;
94     char *prompt;
95     sudo_auth *auth;
96 {
97     char *pass;
98     char *s;
99     size_t len;
100     int authok = 0;
101     sigaction_t sa, osa;
102     auth_session_t *as = (auth_session_t *) auth->data;
103
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);
109
110     /*
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
114      * S/Key.
115      */
116     if ((s = auth_challenge(as)) == NULL) {
117         pass = tgetpass(prompt, def_passwd_timeout * 60, tgetpass_flags);
118     } else {
119         pass = tgetpass(s, def_passwd_timeout * 60, tgetpass_flags);
120         if (pass && *pass == '\0') {
121             if ((prompt = strrchr(s, '\n')))
122                 prompt++;
123             else
124                 prompt = s;
125
126             /*
127              * Append '[echo on]' to the last line of the challenge and
128              * reprompt with echo turned on.
129              */
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);
136             free(s);
137         }
138     }
139
140     if (pass) {
141         authok = auth_userresponse(as, pass, 1);
142         zero_bytes(pass, strlen(pass));
143     }
144
145     /* restore old signal handler */
146     (void) sigaction(SIGCHLD, &osa, NULL);
147
148     if (authok)
149         return AUTH_SUCCESS;
150
151     if (!pass)
152         return AUTH_INTR;
153
154     if ((s = auth_getvalue(as, "errormsg")) != NULL)
155         log_error(NO_EXIT|NO_MAIL, "%s", s);
156     return AUTH_FAILURE;
157 }
158
159 int
160 bsdauth_cleanup(pw, auth)
161     struct passwd *pw;
162     sudo_auth *auth;
163 {
164     auth_session_t *as = (auth_session_t *) auth->data;
165
166     auth_close(as);
167
168     return AUTH_SUCCESS;
169 }