]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
b984f12e | 2 | * Copyright (c) 2001 Markus Friedl. All rights reserved. |
2d86a6cc | 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. | |
2d86a6cc | 12 | * |
b984f12e | 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. | |
5260325f | 23 | */ |
8efc0c15 | 24 | |
25 | #include "includes.h" | |
a75f5360 | 26 | RCSID("$OpenBSD: readpass.c,v 1.34 2006/02/08 12:15:27 stevesk Exp $"); |
27 | ||
28 | #ifdef HAVE_PATHS_H | |
29 | # include <paths.h> | |
30 | #endif | |
1843a425 | 31 | |
8efc0c15 | 32 | #include "xmalloc.h" |
58ae9cb8 | 33 | #include "misc.h" |
561e5254 | 34 | #include "pathnames.h" |
35 | #include "log.h" | |
561e5254 | 36 | #include "ssh.h" |
37 | ||
396c147e | 38 | static char * |
742ee8f2 | 39 | ssh_askpass(char *askpass, const char *msg) |
561e5254 | 40 | { |
41 | pid_t pid; | |
42 | size_t len; | |
56b551e2 | 43 | char *pass; |
f9654cd7 | 44 | int p[2], status, ret; |
561e5254 | 45 | char buf[1024]; |
46 | ||
47 | if (fflush(stdout) != 0) | |
48 | error("ssh_askpass: fflush: %s", strerror(errno)); | |
49 | if (askpass == NULL) | |
50 | fatal("internal error: askpass undefined"); | |
333b5ba7 | 51 | if (pipe(p) < 0) { |
52 | error("ssh_askpass: pipe: %s", strerror(errno)); | |
c4087616 | 53 | return NULL; |
333b5ba7 | 54 | } |
55 | if ((pid = fork()) < 0) { | |
56 | error("ssh_askpass: fork: %s", strerror(errno)); | |
c4087616 | 57 | return NULL; |
333b5ba7 | 58 | } |
561e5254 | 59 | if (pid == 0) { |
60 | seteuid(getuid()); | |
61 | setuid(getuid()); | |
62 | close(p[0]); | |
63 | if (dup2(p[1], STDOUT_FILENO) < 0) | |
64 | fatal("ssh_askpass: dup2: %s", strerror(errno)); | |
65 | execlp(askpass, askpass, msg, (char *) 0); | |
66 | fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno)); | |
67 | } | |
68 | close(p[1]); | |
f9654cd7 | 69 | |
70 | len = ret = 0; | |
71 | do { | |
72 | ret = read(p[0], buf + len, sizeof(buf) - 1 - len); | |
73 | if (ret == -1 && errno == EINTR) | |
74 | continue; | |
75 | if (ret <= 0) | |
76 | break; | |
77 | len += ret; | |
78 | } while (sizeof(buf) - 1 - len > 0); | |
79 | buf[len] = '\0'; | |
80 | ||
561e5254 | 81 | close(p[0]); |
82 | while (waitpid(pid, &status, 0) < 0) | |
83 | if (errno != EINTR) | |
84 | break; | |
f9654cd7 | 85 | |
c4087616 | 86 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
87 | memset(buf, 0, sizeof(buf)); | |
88 | return NULL; | |
89 | } | |
90 | ||
56b551e2 | 91 | buf[strcspn(buf, "\r\n")] = '\0'; |
561e5254 | 92 | pass = xstrdup(buf); |
93 | memset(buf, 0, sizeof(buf)); | |
94 | return pass; | |
95 | } | |
96 | ||
aa3378df | 97 | /* |
1843a425 | 98 | * Reads a passphrase from /dev/tty with echo turned off/on. Returns the |
99 | * passphrase (allocated with xmalloc). Exits if EOF is encountered. If | |
100 | * RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no | |
101 | * tty is available | |
94ec8c6b | 102 | */ |
5260325f | 103 | char * |
1843a425 | 104 | read_passphrase(const char *prompt, int flags) |
8efc0c15 | 105 | { |
1843a425 | 106 | char *askpass = NULL, *ret, buf[1024]; |
107 | int rppflags, use_askpass = 0, ttyfd; | |
561e5254 | 108 | |
1843a425 | 109 | rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF; |
0d34d6ce | 110 | if (flags & RP_USE_ASKPASS) |
111 | use_askpass = 1; | |
112 | else if (flags & RP_ALLOW_STDIN) { | |
c6eb32a1 | 113 | if (!isatty(STDIN_FILENO)) { |
d49d70a8 | 114 | debug("read_passphrase: stdin is not a tty"); |
561e5254 | 115 | use_askpass = 1; |
c6eb32a1 | 116 | } |
561e5254 | 117 | } else { |
1843a425 | 118 | rppflags |= RPP_REQUIRE_TTY; |
a62ebe1f | 119 | ttyfd = open(_PATH_TTY, O_RDWR); |
561e5254 | 120 | if (ttyfd >= 0) |
121 | close(ttyfd); | |
d49d70a8 | 122 | else { |
123 | debug("read_passphrase: can't open %s: %s", _PATH_TTY, | |
124 | strerror(errno)); | |
561e5254 | 125 | use_askpass = 1; |
d49d70a8 | 126 | } |
561e5254 | 127 | } |
128 | ||
0d34d6ce | 129 | if ((flags & RP_USE_ASKPASS) && getenv("DISPLAY") == NULL) |
130 | return (flags & RP_ALLOW_EOF) ? NULL : xstrdup(""); | |
131 | ||
561e5254 | 132 | if (use_askpass && getenv("DISPLAY")) { |
133 | if (getenv(SSH_ASKPASS_ENV)) | |
134 | askpass = getenv(SSH_ASKPASS_ENV); | |
135 | else | |
136 | askpass = _PATH_SSH_ASKPASS_DEFAULT; | |
c4087616 | 137 | if ((ret = ssh_askpass(askpass, prompt)) == NULL) |
138 | if (!(flags & RP_ALLOW_EOF)) | |
139 | return xstrdup(""); | |
140 | return ret; | |
561e5254 | 141 | } |
142 | ||
67ec167d | 143 | if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) { |
144 | if (flags & RP_ALLOW_EOF) | |
145 | return NULL; | |
613021ac | 146 | return xstrdup(""); |
67ec167d | 147 | } |
1843a425 | 148 | |
149 | ret = xstrdup(buf); | |
150 | memset(buf, 'x', sizeof buf); | |
151 | return ret; | |
8efc0c15 | 152 | } |
7a9c7a0b | 153 | |
154 | int | |
155 | ask_permission(const char *fmt, ...) | |
156 | { | |
157 | va_list args; | |
158 | char *p, prompt[1024]; | |
159 | int allowed = 0; | |
160 | ||
161 | va_start(args, fmt); | |
162 | vsnprintf(prompt, sizeof(prompt), fmt, args); | |
163 | va_end(args); | |
164 | ||
165 | p = read_passphrase(prompt, RP_USE_ASKPASS|RP_ALLOW_EOF); | |
166 | if (p != NULL) { | |
167 | /* | |
168 | * Accept empty responses and responses consisting | |
169 | * of the word "yes" as affirmative. | |
170 | */ | |
171 | if (*p == '\0' || *p == '\n' || | |
172 | strcasecmp(p, "yes") == 0) | |
173 | allowed = 1; | |
174 | xfree(p); | |
175 | } | |
176 | ||
177 | return (allowed); | |
178 | } |