Imported Upstream version 1.6.9p6
[debian/sudo] / auth / fwtk.c
1 /*
2  * Copyright (c) 1999-2005 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 #ifdef HAVE_ERR_H
45 # include <err.h>
46 #else
47 # include "emul/err.h"
48 #endif /* HAVE_ERR_H */
49 #include <pwd.h>
50
51 #include <auth.h>
52 #include <firewall.h>
53
54 #include "sudo.h"
55 #include "sudo_auth.h"
56
57 #ifndef lint
58 __unused static const char rcsid[] = "$Sudo: fwtk.c,v 1.23.2.2 2007/06/12 01:28:42 millert Exp $";
59 #endif /* lint */
60
61 int
62 fwtk_init(pw, promptp, auth)
63     struct passwd *pw;
64     char **promptp;
65     sudo_auth *auth;
66 {
67     static Cfg *confp;                  /* Configuration entry struct */
68     char resp[128];                     /* Response from the server */
69
70     if ((confp = cfg_read("sudo")) == (Cfg *)-1) {
71         warnx("cannot read fwtk config");
72         return(AUTH_FATAL);
73     }
74
75     if (auth_open(confp)) {
76         warnx("cannot connect to authentication server");
77         return(AUTH_FATAL);
78     }
79
80     /* Get welcome message from auth server */
81     if (auth_recv(resp, sizeof(resp))) {
82         warnx("lost connection to authentication server");
83         return(AUTH_FATAL);
84     }
85     if (strncmp(resp, "Authsrv ready", 13) != 0) {
86         warnx("authentication server error:\n%s", resp);
87         return(AUTH_FATAL);
88     }
89
90     return(AUTH_SUCCESS);
91 }
92
93 int
94 fwtk_verify(pw, prompt, auth)
95     struct passwd *pw;
96     char *prompt;
97     sudo_auth *auth;
98 {
99     char *pass;                         /* Password from the user */
100     char buf[SUDO_PASS_MAX + 12];       /* General prupose buffer */
101     char resp[128];                     /* Response from the server */
102     int error;
103     extern int nil_pw;
104
105     /* Send username to authentication server. */
106     (void) snprintf(buf, sizeof(buf), "authorize %s 'sudo'", pw->pw_name);
107 restart:
108     if (auth_send(buf) || auth_recv(resp, sizeof(resp))) {
109         warnx("lost connection to authentication server");
110         return(AUTH_FATAL);
111     }
112
113     /* Get the password/response from the user. */
114     if (strncmp(resp, "challenge ", 10) == 0) {
115         (void) snprintf(buf, sizeof(buf), "%s\nResponse: ", &resp[10]);
116         pass = tgetpass(buf, def_passwd_timeout * 60, tgetpass_flags);
117         if (pass && *pass == '\0') {
118             pass = tgetpass("Response [echo on]: ",
119                 def_passwd_timeout * 60, tgetpass_flags | TGP_ECHO);
120         }
121     } else if (strncmp(resp, "chalnecho ", 10) == 0) {
122         pass = tgetpass(&resp[10], def_passwd_timeout * 60, tgetpass_flags);
123     } else if (strncmp(resp, "password", 8) == 0) {
124         pass = tgetpass(prompt, def_passwd_timeout * 60,
125             tgetpass_flags);
126     } else if (strncmp(resp, "display ", 8) == 0) {
127         fprintf(stderr, "%s\n", &resp[8]);
128         strlcpy(buf, "response dummy", sizeof(buf));
129         goto restart;
130     } else {
131         warnx("%s", resp);
132         return(AUTH_FATAL);
133     }
134     if (!pass) {                        /* ^C or error */
135         nil_pw = 1;
136         return(AUTH_FAILURE);
137     } else if (*pass == '\0')           /* empty password */
138         nil_pw = 1;
139
140     /* Send the user's response to the server */
141     (void) snprintf(buf, sizeof(buf), "response '%s'", pass);
142     if (auth_send(buf) || auth_recv(resp, sizeof(resp))) {
143         warnx("lost connection to authentication server");
144         error = AUTH_FATAL;
145         goto done;
146     }
147
148     if (strncmp(resp, "ok", 2) == 0) {
149         error = AUTH_SUCCESS;
150         goto done;
151     }
152
153     /* Main loop prints "Permission Denied" or insult. */
154     if (strcmp(resp, "Permission Denied.") != 0)
155         warnx("%s", resp);
156     error = AUTH_FAILURE;
157 done:
158     zero_bytes(pass, strlen(pass));
159     zero_bytes(buf, strlen(buf));
160     return(error);
161 }
162
163 int
164 fwtk_cleanup(pw, auth)
165     struct passwd *pw;
166     sudo_auth *auth;
167 {
168
169     auth_close();
170     return(AUTH_SUCCESS);
171 }