1 /* $OpenBSD: cli.c,v 1.10 2001/03/01 03:38:33 deraadt Exp $ */
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 RCSID("$OpenBSD: cli.c,v 1.10 2001/03/01 03:38:33 deraadt Exp $");
34 static int cli_input = -1;
35 static int cli_output = -1;
36 static int cli_from_stdin = 0;
49 cli_open(int from_stdin)
51 if (cli_input >= 0 && cli_output >= 0 && cli_from_stdin == from_stdin)
55 if (!cli_from_stdin && cli_input >= 0) {
56 (void)close(cli_input);
58 cli_input = STDIN_FILENO;
59 cli_output = STDERR_FILENO;
61 cli_input = cli_output = open(_PATH_TTY, O_RDWR);
63 fatal("You have no controlling tty. Cannot read passphrase.");
66 cli_from_stdin = from_stdin;
68 return cli_input >= 0 && cli_output >= 0 && cli_from_stdin == from_stdin;
74 if (!cli_from_stdin && cli_input >= 0)
89 cli_echo_disable(void)
92 sigaddset(&nset, SIGTSTP);
93 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
97 memset(&nsa, 0, sizeof(nsa));
98 nsa.sa_handler = intrcatch;
99 (void) sigaction(SIGINT, &nsa, &osa);
102 if (tcgetattr(cli_input, &otio) == 0 && (otio.c_lflag & ECHO)) {
105 ntio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
106 (void) tcsetattr(cli_input, TCSANOW, &ntio);
112 cli_echo_restore(void)
114 if (echo_modified != 0) {
115 tcsetattr(cli_input, TCSANOW, &otio);
119 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
120 (void) sigaction(SIGINT, &osa, NULL);
123 kill(getpid(), SIGINT);
125 /* XXX tty has not neccessarily drained by now? */
133 cli_read(char* buf, int size, int echo)
142 if (read(cli_input, &ch, 1) != 1)
144 if (ch == '\n' || intr != 0)
154 (void) write(cli_output, "\n", 1);
159 cli_write(const char* buf, int size)
161 int i, len, pos, ret = 0;
164 output = xmalloc(4*size);
165 for (p = output, i = 0; i < size; i++) {
166 if (buf[i] == '\n' || buf[i] == '\r')
169 p = vis(p, buf[i], 0, 0);
173 for (pos = 0; pos < len; pos += ret) {
174 ret = write(cli_output, output + pos, len - pos);
185 * Presents a prompt and returns the response allocated with xmalloc().
186 * Uses /dev/tty or stdin/out depending on arg. Optionally disables echo
187 * of response depending on arg. Tries to ensure that no other userland
188 * buffer is storing the response.
191 cli_read_passphrase(const char* prompt, int from_stdin, int echo_enable)
196 if (!cli_open(from_stdin))
197 fatal("Cannot read passphrase.");
201 cli_write(prompt, strlen(prompt));
202 cli_read(buf, sizeof buf, echo_enable);
207 memset(buf, 0, sizeof(buf));
212 cli_prompt(char* prompt, int echo_enable)
214 return cli_read_passphrase(prompt, 0, echo_enable);
221 cli_write(mesg, strlen(mesg));
222 cli_write("\n", strlen("\n"));