2 * Copyright (c) 1999-2003 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 */
47 # include "emul/err.h"
48 #endif /* HAVE_ERR_H */
55 #include "sudo_auth.h"
58 static const char rcsid[] = "$Sudo: fwtk.c,v 1.23 2004/02/13 21:36:47 millert Exp $";
62 fwtk_init(pw, promptp, auth)
67 static Cfg *confp; /* Configuration entry struct */
68 char resp[128]; /* Response from the server */
70 if ((confp = cfg_read("sudo")) == (Cfg *)-1) {
71 warnx("cannot read fwtk config");
75 if (auth_open(confp)) {
76 warnx("cannot connect to authentication server");
80 /* Get welcome message from auth server */
81 if (auth_recv(resp, sizeof(resp))) {
82 warnx("lost connection to authentication server");
85 if (strncmp(resp, "Authsrv ready", 13) != 0) {
86 warnx("authentication server error:\n%s", resp);
94 fwtk_verify(pw, prompt, auth)
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 */
105 /* Send username to authentication server. */
106 (void) snprintf(buf, sizeof(buf), "authorize %s 'sudo'", pw->pw_name);
108 if (auth_send(buf) || auth_recv(resp, sizeof(resp))) {
109 warnx("lost connection to authentication server");
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);
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,
126 } else if (strncmp(resp, "display ", 8) == 0) {
127 fprintf(stderr, "%s\n", &resp[8]);
128 strlcpy(buf, "response dummy", sizeof(buf));
134 if (!pass) { /* ^C or error */
136 return(AUTH_FAILURE);
137 } else if (*pass == '\0') /* empty password */
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");
148 if (strncmp(resp, "ok", 2) == 0) {
149 error = AUTH_SUCCESS;
153 /* Main loop prints "Permission Denied" or insult. */
154 if (strcmp(resp, "Permission Denied.") != 0)
156 error = AUTH_FAILURE;
158 zero_bytes(pass, strlen(pass));
159 zero_bytes(buf, strlen(buf));
164 fwtk_cleanup(pw, auth)
170 return(AUTH_SUCCESS);