2 * Copyright (c) 1999-2005, 2008, 2010
3 * Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
24 #include <sys/types.h>
25 #include <sys/param.h>
34 #endif /* STDC_HEADERS */
37 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_STRING_H */
43 #endif /* HAVE_UNISTD_H */
50 #include "sudo_auth.h"
53 fwtk_init(pw, promptp, auth)
58 static Cfg *confp; /* Configuration entry struct */
59 char resp[128]; /* Response from the server */
61 if ((confp = cfg_read("sudo")) == (Cfg *)-1) {
62 warningx("cannot read fwtk config");
66 if (auth_open(confp)) {
67 warningx("cannot connect to authentication server");
71 /* Get welcome message from auth server */
72 if (auth_recv(resp, sizeof(resp))) {
73 warningx("lost connection to authentication server");
76 if (strncmp(resp, "Authsrv ready", 13) != 0) {
77 warningx("authentication server error:\n%s", resp);
85 fwtk_verify(pw, prompt, auth)
90 char *pass; /* Password from the user */
91 char buf[SUDO_PASS_MAX + 12]; /* General prupose buffer */
92 char resp[128]; /* Response from the server */
95 /* Send username to authentication server. */
96 (void) snprintf(buf, sizeof(buf), "authorize %s 'sudo'", pw->pw_name);
98 if (auth_send(buf) || auth_recv(resp, sizeof(resp))) {
99 warningx("lost connection to authentication server");
103 /* Get the password/response from the user. */
104 if (strncmp(resp, "challenge ", 10) == 0) {
105 (void) snprintf(buf, sizeof(buf), "%s\nResponse: ", &resp[10]);
106 pass = tgetpass(buf, def_passwd_timeout * 60, tgetpass_flags);
107 if (pass && *pass == '\0') {
108 pass = tgetpass("Response [echo on]: ",
109 def_passwd_timeout * 60, tgetpass_flags | TGP_ECHO);
111 } else if (strncmp(resp, "chalnecho ", 10) == 0) {
112 pass = tgetpass(&resp[10], def_passwd_timeout * 60, tgetpass_flags);
113 } else if (strncmp(resp, "password", 8) == 0) {
114 pass = tgetpass(prompt, def_passwd_timeout * 60,
116 } else if (strncmp(resp, "display ", 8) == 0) {
117 fprintf(stderr, "%s\n", &resp[8]);
118 strlcpy(buf, "response dummy", sizeof(buf));
121 warningx("%s", resp);
124 if (!pass) { /* ^C or error */
128 /* Send the user's response to the server */
129 (void) snprintf(buf, sizeof(buf), "response '%s'", pass);
130 if (auth_send(buf) || auth_recv(resp, sizeof(resp))) {
131 warningx("lost connection to authentication server");
136 if (strncmp(resp, "ok", 2) == 0) {
137 error = AUTH_SUCCESS;
141 /* Main loop prints "Permission Denied" or insult. */
142 if (strcmp(resp, "Permission Denied.") != 0)
143 warningx("%s", resp);
144 error = AUTH_FAILURE;
146 zero_bytes(pass, strlen(pass));
147 zero_bytes(buf, strlen(buf));
152 fwtk_cleanup(pw, auth)
158 return(AUTH_SUCCESS);