Annotation of src/usr.bin/ssh/readpass.c, Revision 1.34
1.1 deraadt 1: /*
1.26 markus 2: * Copyright (c) 2001 Markus Friedl. All rights reserved.
1.8 deraadt 3: *
4: * Redistribution and use in source and binary forms, with or without
5: * modification, are permitted provided that the following conditions
6: * are met:
7: * 1. Redistributions of source code must retain the above copyright
8: * notice, this list of conditions and the following disclaimer.
9: * 2. Redistributions in binary form must reproduce the above copyright
10: * notice, this list of conditions and the following disclaimer in the
11: * documentation and/or other materials provided with the distribution.
12: *
1.26 markus 13: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.6 deraadt 23: */
1.1 deraadt 24:
25: #include "includes.h"
1.34 ! stevesk 26: RCSID("$OpenBSD: readpass.c,v 1.33 2005/05/02 21:13:22 markus Exp $");
1.19 markus 27:
1.34 ! stevesk 28: #include <paths.h>
1.19 markus 29: #include <readpassphrase.h>
1.1 deraadt 30:
31: #include "xmalloc.h"
1.29 djm 32: #include "misc.h"
1.15 markus 33: #include "pathnames.h"
34: #include "log.h"
35: #include "ssh.h"
36:
1.18 itojun 37: static char *
1.16 mouring 38: ssh_askpass(char *askpass, const char *msg)
1.15 markus 39: {
40: pid_t pid;
41: size_t len;
1.23 markus 42: char *pass;
1.24 djm 43: int p[2], status, ret;
1.15 markus 44: char buf[1024];
45:
46: if (fflush(stdout) != 0)
47: error("ssh_askpass: fflush: %s", strerror(errno));
48: if (askpass == NULL)
49: fatal("internal error: askpass undefined");
1.21 markus 50: if (pipe(p) < 0) {
51: error("ssh_askpass: pipe: %s", strerror(errno));
1.28 markus 52: return NULL;
1.21 markus 53: }
54: if ((pid = fork()) < 0) {
55: error("ssh_askpass: fork: %s", strerror(errno));
1.28 markus 56: return NULL;
1.21 markus 57: }
1.15 markus 58: if (pid == 0) {
59: seteuid(getuid());
60: setuid(getuid());
61: close(p[0]);
62: if (dup2(p[1], STDOUT_FILENO) < 0)
63: fatal("ssh_askpass: dup2: %s", strerror(errno));
64: execlp(askpass, askpass, msg, (char *) 0);
65: fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
66: }
67: close(p[1]);
1.24 djm 68:
69: len = ret = 0;
70: do {
71: ret = read(p[0], buf + len, sizeof(buf) - 1 - len);
72: if (ret == -1 && errno == EINTR)
73: continue;
74: if (ret <= 0)
75: break;
76: len += ret;
77: } while (sizeof(buf) - 1 - len > 0);
78: buf[len] = '\0';
79:
1.15 markus 80: close(p[0]);
81: while (waitpid(pid, &status, 0) < 0)
82: if (errno != EINTR)
83: break;
1.24 djm 84:
1.28 markus 85: if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
86: memset(buf, 0, sizeof(buf));
87: return NULL;
88: }
89:
1.23 markus 90: buf[strcspn(buf, "\r\n")] = '\0';
1.15 markus 91: pass = xstrdup(buf);
92: memset(buf, 0, sizeof(buf));
93: return pass;
94: }
95:
1.7 markus 96: /*
1.19 markus 97: * Reads a passphrase from /dev/tty with echo turned off/on. Returns the
98: * passphrase (allocated with xmalloc). Exits if EOF is encountered. If
99: * RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no
100: * tty is available
1.12 markus 101: */
1.5 markus 102: char *
1.19 markus 103: read_passphrase(const char *prompt, int flags)
1.1 deraadt 104: {
1.19 markus 105: char *askpass = NULL, *ret, buf[1024];
106: int rppflags, use_askpass = 0, ttyfd;
1.15 markus 107:
1.19 markus 108: rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF;
1.30 djm 109: if (flags & RP_USE_ASKPASS)
110: use_askpass = 1;
111: else if (flags & RP_ALLOW_STDIN) {
1.33 markus 112: if (!isatty(STDIN_FILENO)) {
1.32 dtucker 113: debug("read_passphrase: stdin is not a tty");
1.15 markus 114: use_askpass = 1;
1.33 markus 115: }
1.15 markus 116: } else {
1.19 markus 117: rppflags |= RPP_REQUIRE_TTY;
1.25 markus 118: ttyfd = open(_PATH_TTY, O_RDWR);
1.15 markus 119: if (ttyfd >= 0)
120: close(ttyfd);
1.32 dtucker 121: else {
122: debug("read_passphrase: can't open %s: %s", _PATH_TTY,
123: strerror(errno));
1.15 markus 124: use_askpass = 1;
1.32 dtucker 125: }
1.15 markus 126: }
1.30 djm 127:
128: if ((flags & RP_USE_ASKPASS) && getenv("DISPLAY") == NULL)
129: return (flags & RP_ALLOW_EOF) ? NULL : xstrdup("");
1.15 markus 130:
131: if (use_askpass && getenv("DISPLAY")) {
132: if (getenv(SSH_ASKPASS_ENV))
133: askpass = getenv(SSH_ASKPASS_ENV);
134: else
135: askpass = _PATH_SSH_ASKPASS_DEFAULT;
1.28 markus 136: if ((ret = ssh_askpass(askpass, prompt)) == NULL)
137: if (!(flags & RP_ALLOW_EOF))
138: return xstrdup("");
139: return ret;
1.15 markus 140: }
141:
1.27 markus 142: if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) {
143: if (flags & RP_ALLOW_EOF)
144: return NULL;
1.20 markus 145: return xstrdup("");
1.27 markus 146: }
1.19 markus 147:
148: ret = xstrdup(buf);
149: memset(buf, 'x', sizeof buf);
150: return ret;
1.31 djm 151: }
152:
153: int
154: ask_permission(const char *fmt, ...)
155: {
156: va_list args;
157: char *p, prompt[1024];
158: int allowed = 0;
159:
160: va_start(args, fmt);
161: vsnprintf(prompt, sizeof(prompt), fmt, args);
162: va_end(args);
163:
164: p = read_passphrase(prompt, RP_USE_ASKPASS|RP_ALLOW_EOF);
165: if (p != NULL) {
166: /*
167: * Accept empty responses and responses consisting
168: * of the word "yes" as affirmative.
169: */
170: if (*p == '\0' || *p == '\n' ||
171: strcasecmp(p, "yes") == 0)
172: allowed = 1;
173: xfree(p);
174: }
175:
176: return (allowed);
1.1 deraadt 177: }