c5e438ec |
1 | /* |
2 | * uxcons.c: various interactive-prompt routines shared between the |
3 | * Unix console PuTTY tools |
4 | */ |
5 | |
6 | #include <stdio.h> |
7 | #include <stdlib.h> |
8 | #include <stdarg.h> |
9 | #include <assert.h> |
10 | #include <termios.h> |
11 | #include <unistd.h> |
12 | |
13 | #include "putty.h" |
14 | #include "storage.h" |
15 | #include "ssh.h" |
16 | |
17 | int console_batch_mode = FALSE; |
18 | |
b51259f6 |
19 | static void *console_logctx = NULL; |
20 | |
c5e438ec |
21 | /* |
22 | * Clean up and exit. |
23 | */ |
24 | void cleanup_exit(int code) |
25 | { |
26 | /* |
27 | * Clean up. |
28 | */ |
29 | sk_cleanup(); |
e85da6a5 |
30 | random_save_seed(); |
c5e438ec |
31 | exit(code); |
32 | } |
33 | |
125105d1 |
34 | void update_specials_menu(void *frontend) |
35 | { |
36 | } |
37 | |
39934deb |
38 | void notify_remote_exit(void *frontend) |
39 | { |
40 | } |
41 | |
42 | void timer_change_notify(long next) |
43 | { |
44 | } |
45 | |
c5e438ec |
46 | void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype, |
47 | char *keystr, char *fingerprint) |
48 | { |
49 | int ret; |
50 | |
51 | static const char absentmsg_batch[] = |
52 | "The server's host key is not cached. You have no guarantee\n" |
53 | "that the server is the computer you think it is.\n" |
65f66c88 |
54 | "The server's %s key fingerprint is:\n" |
c5e438ec |
55 | "%s\n" |
56 | "Connection abandoned.\n"; |
57 | static const char absentmsg[] = |
58 | "The server's host key is not cached. You have no guarantee\n" |
59 | "that the server is the computer you think it is.\n" |
65f66c88 |
60 | "The server's %s key fingerprint is:\n" |
c5e438ec |
61 | "%s\n" |
62 | "If you trust this host, enter \"y\" to add the key to\n" |
63 | "PuTTY's cache and carry on connecting.\n" |
64 | "If you want to carry on connecting just once, without\n" |
65 | "adding the key to the cache, enter \"n\".\n" |
66 | "If you do not trust this host, press Return to abandon the\n" |
67 | "connection.\n" |
68 | "Store key in cache? (y/n) "; |
69 | |
70 | static const char wrongmsg_batch[] = |
71 | "WARNING - POTENTIAL SECURITY BREACH!\n" |
72 | "The server's host key does not match the one PuTTY has\n" |
73 | "cached. This means that either the server administrator\n" |
74 | "has changed the host key, or you have actually connected\n" |
75 | "to another computer pretending to be the server.\n" |
65f66c88 |
76 | "The new %s key fingerprint is:\n" |
c5e438ec |
77 | "%s\n" |
78 | "Connection abandoned.\n"; |
79 | static const char wrongmsg[] = |
80 | "WARNING - POTENTIAL SECURITY BREACH!\n" |
81 | "The server's host key does not match the one PuTTY has\n" |
82 | "cached. This means that either the server administrator\n" |
83 | "has changed the host key, or you have actually connected\n" |
84 | "to another computer pretending to be the server.\n" |
65f66c88 |
85 | "The new %s key fingerprint is:\n" |
c5e438ec |
86 | "%s\n" |
87 | "If you were expecting this change and trust the new key,\n" |
88 | "enter \"y\" to update PuTTY's cache and continue connecting.\n" |
89 | "If you want to carry on connecting but without updating\n" |
90 | "the cache, enter \"n\".\n" |
91 | "If you want to abandon the connection completely, press\n" |
92 | "Return to cancel. Pressing Return is the ONLY guaranteed\n" |
93 | "safe choice.\n" |
94 | "Update cached key? (y/n, Return cancels connection) "; |
95 | |
96 | static const char abandoned[] = "Connection abandoned.\n"; |
97 | |
98 | char line[32]; |
99 | |
100 | /* |
101 | * Verify the key. |
102 | */ |
103 | ret = verify_host_key(host, port, keytype, keystr); |
104 | |
105 | if (ret == 0) /* success - key matched OK */ |
106 | return; |
107 | |
108 | if (ret == 2) { /* key was different */ |
109 | if (console_batch_mode) { |
65f66c88 |
110 | fprintf(stderr, wrongmsg_batch, keytype, fingerprint); |
c5e438ec |
111 | cleanup_exit(1); |
112 | } |
65f66c88 |
113 | fprintf(stderr, wrongmsg, keytype, fingerprint); |
c5e438ec |
114 | fflush(stderr); |
115 | } |
116 | if (ret == 1) { /* key was absent */ |
117 | if (console_batch_mode) { |
65f66c88 |
118 | fprintf(stderr, absentmsg_batch, keytype, fingerprint); |
c5e438ec |
119 | cleanup_exit(1); |
120 | } |
65f66c88 |
121 | fprintf(stderr, absentmsg, keytype, fingerprint); |
c5e438ec |
122 | fflush(stderr); |
123 | } |
124 | |
125 | { |
126 | struct termios oldmode, newmode; |
127 | tcgetattr(0, &oldmode); |
128 | newmode = oldmode; |
129 | newmode.c_lflag |= ECHO | ISIG | ICANON; |
130 | tcsetattr(0, TCSANOW, &newmode); |
131 | line[0] = '\0'; |
132 | read(0, line, sizeof(line) - 1); |
133 | tcsetattr(0, TCSANOW, &oldmode); |
134 | } |
135 | |
136 | if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') { |
137 | if (line[0] == 'y' || line[0] == 'Y') |
138 | store_host_key(host, port, keytype, keystr); |
139 | } else { |
140 | fprintf(stderr, abandoned); |
141 | cleanup_exit(0); |
142 | } |
143 | } |
144 | |
145 | /* |
83e7d008 |
146 | * Ask whether the selected algorithm is acceptable (since it was |
c5e438ec |
147 | * below the configured 'warn' threshold). |
c5e438ec |
148 | */ |
83e7d008 |
149 | void askalg(void *frontend, const char *algtype, const char *algname) |
c5e438ec |
150 | { |
151 | static const char msg[] = |
83e7d008 |
152 | "The first %s supported by the server is\n" |
c5e438ec |
153 | "%s, which is below the configured warning threshold.\n" |
154 | "Continue with connection? (y/n) "; |
155 | static const char msg_batch[] = |
83e7d008 |
156 | "The first %s supported by the server is\n" |
c5e438ec |
157 | "%s, which is below the configured warning threshold.\n" |
158 | "Connection abandoned.\n"; |
159 | static const char abandoned[] = "Connection abandoned.\n"; |
160 | |
161 | char line[32]; |
162 | |
163 | if (console_batch_mode) { |
83e7d008 |
164 | fprintf(stderr, msg_batch, algtype, algname); |
c5e438ec |
165 | cleanup_exit(1); |
166 | } |
167 | |
83e7d008 |
168 | fprintf(stderr, msg, algtype, algname); |
c5e438ec |
169 | fflush(stderr); |
170 | |
171 | { |
172 | struct termios oldmode, newmode; |
173 | tcgetattr(0, &oldmode); |
174 | newmode = oldmode; |
175 | newmode.c_lflag |= ECHO | ISIG | ICANON; |
176 | tcsetattr(0, TCSANOW, &newmode); |
177 | line[0] = '\0'; |
178 | read(0, line, sizeof(line) - 1); |
179 | tcsetattr(0, TCSANOW, &oldmode); |
180 | } |
181 | |
182 | if (line[0] == 'y' || line[0] == 'Y') { |
183 | return; |
184 | } else { |
185 | fprintf(stderr, abandoned); |
186 | cleanup_exit(0); |
187 | } |
188 | } |
189 | |
190 | /* |
191 | * Ask whether to wipe a session log file before writing to it. |
192 | * Returns 2 for wipe, 1 for append, 0 for cancel (don't log). |
193 | */ |
9a30e26b |
194 | int askappend(void *frontend, Filename filename) |
c5e438ec |
195 | { |
196 | static const char msgtemplate[] = |
197 | "The session log file \"%.*s\" already exists.\n" |
198 | "You can overwrite it with a new session log,\n" |
199 | "append your session log to the end of it,\n" |
200 | "or disable session logging for this session.\n" |
201 | "Enter \"y\" to wipe the file, \"n\" to append to it,\n" |
202 | "or just press Return to disable logging.\n" |
203 | "Wipe the log file? (y/n, Return cancels logging) "; |
204 | |
205 | static const char msgtemplate_batch[] = |
206 | "The session log file \"%.*s\" already exists.\n" |
207 | "Logging will not be enabled.\n"; |
208 | |
209 | char line[32]; |
210 | |
c5e438ec |
211 | if (console_batch_mode) { |
9a30e26b |
212 | fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path); |
c5e438ec |
213 | fflush(stderr); |
214 | return 0; |
215 | } |
9a30e26b |
216 | fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path); |
c5e438ec |
217 | fflush(stderr); |
218 | |
219 | { |
220 | struct termios oldmode, newmode; |
221 | tcgetattr(0, &oldmode); |
222 | newmode = oldmode; |
223 | newmode.c_lflag |= ECHO | ISIG | ICANON; |
224 | tcsetattr(0, TCSANOW, &newmode); |
225 | line[0] = '\0'; |
226 | read(0, line, sizeof(line) - 1); |
227 | tcsetattr(0, TCSANOW, &oldmode); |
228 | } |
229 | |
230 | if (line[0] == 'y' || line[0] == 'Y') |
231 | return 2; |
232 | else if (line[0] == 'n' || line[0] == 'N') |
233 | return 1; |
234 | else |
235 | return 0; |
236 | } |
237 | |
238 | /* |
239 | * Warn about the obsolescent key file format. |
240 | * |
241 | * Uniquely among these functions, this one does _not_ expect a |
242 | * frontend handle. This means that if PuTTY is ported to a |
243 | * platform which requires frontend handles, this function will be |
244 | * an anomaly. Fortunately, the problem it addresses will not have |
245 | * been present on that platform, so it can plausibly be |
246 | * implemented as an empty function. |
247 | */ |
248 | void old_keyfile_warning(void) |
249 | { |
250 | static const char message[] = |
251 | "You are loading an SSH 2 private key which has an\n" |
252 | "old version of the file format. This means your key\n" |
253 | "file is not fully tamperproof. Future versions of\n" |
254 | "PuTTY may stop supporting this private key format,\n" |
255 | "so we recommend you convert your key to the new\n" |
256 | "format.\n" |
257 | "\n" |
258 | "Once the key is loaded into PuTTYgen, you can perform\n" |
259 | "this conversion simply by saving it again.\n"; |
260 | |
261 | fputs(message, stderr); |
262 | } |
263 | |
b51259f6 |
264 | void console_provide_logctx(void *logctx) |
265 | { |
266 | console_logctx = logctx; |
267 | } |
268 | |
cbe2d68f |
269 | void logevent(void *frontend, const char *string) |
c5e438ec |
270 | { |
b51259f6 |
271 | if (console_logctx) |
272 | log_eventlog(console_logctx, string); |
c5e438ec |
273 | } |
274 | |
275 | int console_get_line(const char *prompt, char *str, |
276 | int maxlen, int is_pw) |
277 | { |
278 | struct termios oldmode, newmode; |
279 | int i; |
280 | |
281 | if (console_batch_mode) { |
282 | if (maxlen > 0) |
283 | str[0] = '\0'; |
a21a6a9a |
284 | return 0; |
c5e438ec |
285 | } else { |
286 | tcgetattr(0, &oldmode); |
287 | newmode = oldmode; |
288 | newmode.c_lflag |= ISIG | ICANON; |
289 | if (is_pw) |
290 | newmode.c_lflag &= ~ECHO; |
291 | else |
292 | newmode.c_lflag |= ECHO; |
293 | tcsetattr(0, TCSANOW, &newmode); |
294 | |
295 | fputs(prompt, stdout); |
296 | fflush(stdout); |
297 | i = read(0, str, maxlen - 1); |
298 | |
299 | tcsetattr(0, TCSANOW, &oldmode); |
300 | |
301 | if (i > 0 && str[i-1] == '\n') |
302 | i--; |
303 | str[i] = '\0'; |
304 | |
305 | if (is_pw) |
8d69b472 |
306 | fputs("\n", stdout); |
a21a6a9a |
307 | |
308 | return 1; |
c5e438ec |
309 | } |
c5e438ec |
310 | } |
311 | |
312 | void frontend_keypress(void *handle) |
313 | { |
314 | /* |
315 | * This is nothing but a stub, in console code. |
316 | */ |
317 | return; |
318 | } |
47a6b94c |
319 | |
320 | int is_interactive(void) |
321 | { |
322 | return isatty(0); |
323 | } |
46ed7b64 |
324 | |
325 | /* |
326 | * X11-forwarding-related things suitable for console. |
327 | */ |
328 | |
329 | const char platform_x11_best_transport[] = "unix"; |
330 | |
331 | char *platform_get_x_display(void) { |
332 | return dupstr(getenv("DISPLAY")); |
333 | } |