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 | |
f7397dc6 |
21 | static struct termios orig_termios_stderr; |
22 | static int stderr_is_a_tty; |
23 | |
24 | void stderr_tty_init() |
25 | { |
26 | /* Ensure that if stderr is a tty, we can get it back to a sane state. */ |
27 | if ((flags & FLAG_STDERR_TTY) && isatty(STDERR_FILENO)) { |
28 | stderr_is_a_tty = TRUE; |
29 | tcgetattr(STDERR_FILENO, &orig_termios_stderr); |
30 | } |
31 | } |
32 | |
33 | void premsg(struct termios *cf) |
34 | { |
35 | if (stderr_is_a_tty) { |
36 | tcgetattr(STDERR_FILENO, cf); |
37 | tcsetattr(STDERR_FILENO, TCSADRAIN, &orig_termios_stderr); |
38 | } |
39 | } |
40 | void postmsg(struct termios *cf) |
41 | { |
42 | if (stderr_is_a_tty) |
43 | tcsetattr(STDERR_FILENO, TCSADRAIN, cf); |
44 | } |
45 | |
c5e438ec |
46 | /* |
47 | * Clean up and exit. |
48 | */ |
49 | void cleanup_exit(int code) |
50 | { |
51 | /* |
52 | * Clean up. |
53 | */ |
54 | sk_cleanup(); |
e85da6a5 |
55 | random_save_seed(); |
c5e438ec |
56 | exit(code); |
57 | } |
58 | |
755e0524 |
59 | void set_busy_status(void *frontend, int status) |
60 | { |
61 | } |
62 | |
125105d1 |
63 | void update_specials_menu(void *frontend) |
64 | { |
65 | } |
66 | |
39934deb |
67 | void notify_remote_exit(void *frontend) |
68 | { |
69 | } |
70 | |
71 | void timer_change_notify(long next) |
72 | { |
73 | } |
74 | |
3d9449a1 |
75 | int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype, |
76 | char *keystr, char *fingerprint, |
77 | void (*callback)(void *ctx, int result), void *ctx) |
c5e438ec |
78 | { |
79 | int ret; |
80 | |
81 | static const char absentmsg_batch[] = |
82 | "The server's host key is not cached. You have no guarantee\n" |
83 | "that the server is the computer you think it is.\n" |
65f66c88 |
84 | "The server's %s key fingerprint is:\n" |
c5e438ec |
85 | "%s\n" |
86 | "Connection abandoned.\n"; |
87 | static const char absentmsg[] = |
88 | "The server's host key is not cached. You have no guarantee\n" |
89 | "that the server is the computer you think it is.\n" |
65f66c88 |
90 | "The server's %s key fingerprint is:\n" |
c5e438ec |
91 | "%s\n" |
92 | "If you trust this host, enter \"y\" to add the key to\n" |
93 | "PuTTY's cache and carry on connecting.\n" |
94 | "If you want to carry on connecting just once, without\n" |
95 | "adding the key to the cache, enter \"n\".\n" |
96 | "If you do not trust this host, press Return to abandon the\n" |
97 | "connection.\n" |
98 | "Store key in cache? (y/n) "; |
99 | |
100 | static const char wrongmsg_batch[] = |
101 | "WARNING - POTENTIAL SECURITY BREACH!\n" |
102 | "The server's host key does not match the one PuTTY has\n" |
103 | "cached. This means that either the server administrator\n" |
104 | "has changed the host key, or you have actually connected\n" |
105 | "to another computer pretending to be the server.\n" |
65f66c88 |
106 | "The new %s key fingerprint is:\n" |
c5e438ec |
107 | "%s\n" |
108 | "Connection abandoned.\n"; |
109 | static const char wrongmsg[] = |
110 | "WARNING - POTENTIAL SECURITY BREACH!\n" |
111 | "The server's host key does not match the one PuTTY has\n" |
112 | "cached. This means that either the server administrator\n" |
113 | "has changed the host key, or you have actually connected\n" |
114 | "to another computer pretending to be the server.\n" |
65f66c88 |
115 | "The new %s key fingerprint is:\n" |
c5e438ec |
116 | "%s\n" |
117 | "If you were expecting this change and trust the new key,\n" |
118 | "enter \"y\" to update PuTTY's cache and continue connecting.\n" |
119 | "If you want to carry on connecting but without updating\n" |
120 | "the cache, enter \"n\".\n" |
121 | "If you want to abandon the connection completely, press\n" |
122 | "Return to cancel. Pressing Return is the ONLY guaranteed\n" |
123 | "safe choice.\n" |
124 | "Update cached key? (y/n, Return cancels connection) "; |
125 | |
126 | static const char abandoned[] = "Connection abandoned.\n"; |
127 | |
128 | char line[32]; |
f7397dc6 |
129 | struct termios cf; |
c5e438ec |
130 | |
131 | /* |
132 | * Verify the key. |
133 | */ |
134 | ret = verify_host_key(host, port, keytype, keystr); |
135 | |
136 | if (ret == 0) /* success - key matched OK */ |
3d9449a1 |
137 | return 1; |
c5e438ec |
138 | |
f7397dc6 |
139 | premsg(&cf); |
c5e438ec |
140 | if (ret == 2) { /* key was different */ |
141 | if (console_batch_mode) { |
65f66c88 |
142 | fprintf(stderr, wrongmsg_batch, keytype, fingerprint); |
3d9449a1 |
143 | return 0; |
c5e438ec |
144 | } |
65f66c88 |
145 | fprintf(stderr, wrongmsg, keytype, fingerprint); |
c5e438ec |
146 | fflush(stderr); |
147 | } |
148 | if (ret == 1) { /* key was absent */ |
149 | if (console_batch_mode) { |
65f66c88 |
150 | fprintf(stderr, absentmsg_batch, keytype, fingerprint); |
3d9449a1 |
151 | return 0; |
c5e438ec |
152 | } |
65f66c88 |
153 | fprintf(stderr, absentmsg, keytype, fingerprint); |
c5e438ec |
154 | fflush(stderr); |
155 | } |
156 | |
157 | { |
158 | struct termios oldmode, newmode; |
159 | tcgetattr(0, &oldmode); |
160 | newmode = oldmode; |
161 | newmode.c_lflag |= ECHO | ISIG | ICANON; |
162 | tcsetattr(0, TCSANOW, &newmode); |
163 | line[0] = '\0'; |
ecb25722 |
164 | if (read(0, line, sizeof(line) - 1) <= 0) |
165 | /* handled below */; |
c5e438ec |
166 | tcsetattr(0, TCSANOW, &oldmode); |
167 | } |
168 | |
169 | if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') { |
170 | if (line[0] == 'y' || line[0] == 'Y') |
171 | store_host_key(host, port, keytype, keystr); |
f7397dc6 |
172 | postmsg(&cf); |
3d9449a1 |
173 | return 1; |
c5e438ec |
174 | } else { |
175 | fprintf(stderr, abandoned); |
f7397dc6 |
176 | postmsg(&cf); |
3d9449a1 |
177 | return 0; |
c5e438ec |
178 | } |
179 | } |
180 | |
181 | /* |
83e7d008 |
182 | * Ask whether the selected algorithm is acceptable (since it was |
c5e438ec |
183 | * below the configured 'warn' threshold). |
c5e438ec |
184 | */ |
3d9449a1 |
185 | int askalg(void *frontend, const char *algtype, const char *algname, |
186 | void (*callback)(void *ctx, int result), void *ctx) |
c5e438ec |
187 | { |
188 | static const char msg[] = |
83e7d008 |
189 | "The first %s supported by the server is\n" |
c5e438ec |
190 | "%s, which is below the configured warning threshold.\n" |
191 | "Continue with connection? (y/n) "; |
192 | static const char msg_batch[] = |
83e7d008 |
193 | "The first %s supported by the server is\n" |
c5e438ec |
194 | "%s, which is below the configured warning threshold.\n" |
195 | "Connection abandoned.\n"; |
196 | static const char abandoned[] = "Connection abandoned.\n"; |
197 | |
198 | char line[32]; |
f7397dc6 |
199 | struct termios cf; |
c5e438ec |
200 | |
f7397dc6 |
201 | premsg(&cf); |
c5e438ec |
202 | if (console_batch_mode) { |
83e7d008 |
203 | fprintf(stderr, msg_batch, algtype, algname); |
3d9449a1 |
204 | return 0; |
c5e438ec |
205 | } |
206 | |
83e7d008 |
207 | fprintf(stderr, msg, algtype, algname); |
c5e438ec |
208 | fflush(stderr); |
209 | |
210 | { |
211 | struct termios oldmode, newmode; |
212 | tcgetattr(0, &oldmode); |
213 | newmode = oldmode; |
214 | newmode.c_lflag |= ECHO | ISIG | ICANON; |
215 | tcsetattr(0, TCSANOW, &newmode); |
216 | line[0] = '\0'; |
ecb25722 |
217 | if (read(0, line, sizeof(line) - 1) <= 0) |
218 | /* handled below */; |
c5e438ec |
219 | tcsetattr(0, TCSANOW, &oldmode); |
220 | } |
221 | |
222 | if (line[0] == 'y' || line[0] == 'Y') { |
f7397dc6 |
223 | postmsg(&cf); |
3d9449a1 |
224 | return 1; |
c5e438ec |
225 | } else { |
226 | fprintf(stderr, abandoned); |
f7397dc6 |
227 | postmsg(&cf); |
3d9449a1 |
228 | return 0; |
c5e438ec |
229 | } |
230 | } |
231 | |
232 | /* |
233 | * Ask whether to wipe a session log file before writing to it. |
234 | * Returns 2 for wipe, 1 for append, 0 for cancel (don't log). |
235 | */ |
919baedb |
236 | int askappend(void *frontend, Filename filename, |
237 | void (*callback)(void *ctx, int result), void *ctx) |
c5e438ec |
238 | { |
239 | static const char msgtemplate[] = |
240 | "The session log file \"%.*s\" already exists.\n" |
241 | "You can overwrite it with a new session log,\n" |
242 | "append your session log to the end of it,\n" |
243 | "or disable session logging for this session.\n" |
244 | "Enter \"y\" to wipe the file, \"n\" to append to it,\n" |
245 | "or just press Return to disable logging.\n" |
246 | "Wipe the log file? (y/n, Return cancels logging) "; |
247 | |
248 | static const char msgtemplate_batch[] = |
249 | "The session log file \"%.*s\" already exists.\n" |
250 | "Logging will not be enabled.\n"; |
251 | |
252 | char line[32]; |
f7397dc6 |
253 | struct termios cf; |
c5e438ec |
254 | |
f7397dc6 |
255 | premsg(&cf); |
c5e438ec |
256 | if (console_batch_mode) { |
9a30e26b |
257 | fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path); |
c5e438ec |
258 | fflush(stderr); |
259 | return 0; |
260 | } |
9a30e26b |
261 | fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path); |
c5e438ec |
262 | fflush(stderr); |
263 | |
264 | { |
265 | struct termios oldmode, newmode; |
266 | tcgetattr(0, &oldmode); |
267 | newmode = oldmode; |
268 | newmode.c_lflag |= ECHO | ISIG | ICANON; |
269 | tcsetattr(0, TCSANOW, &newmode); |
270 | line[0] = '\0'; |
ecb25722 |
271 | if (read(0, line, sizeof(line) - 1) <= 0) |
272 | /* handled below */; |
c5e438ec |
273 | tcsetattr(0, TCSANOW, &oldmode); |
274 | } |
275 | |
f7397dc6 |
276 | postmsg(&cf); |
c5e438ec |
277 | if (line[0] == 'y' || line[0] == 'Y') |
278 | return 2; |
279 | else if (line[0] == 'n' || line[0] == 'N') |
280 | return 1; |
281 | else |
282 | return 0; |
283 | } |
284 | |
285 | /* |
286 | * Warn about the obsolescent key file format. |
287 | * |
288 | * Uniquely among these functions, this one does _not_ expect a |
289 | * frontend handle. This means that if PuTTY is ported to a |
290 | * platform which requires frontend handles, this function will be |
291 | * an anomaly. Fortunately, the problem it addresses will not have |
292 | * been present on that platform, so it can plausibly be |
293 | * implemented as an empty function. |
294 | */ |
295 | void old_keyfile_warning(void) |
296 | { |
297 | static const char message[] = |
2e85c969 |
298 | "You are loading an SSH-2 private key which has an\n" |
c5e438ec |
299 | "old version of the file format. This means your key\n" |
300 | "file is not fully tamperproof. Future versions of\n" |
301 | "PuTTY may stop supporting this private key format,\n" |
302 | "so we recommend you convert your key to the new\n" |
303 | "format.\n" |
304 | "\n" |
305 | "Once the key is loaded into PuTTYgen, you can perform\n" |
306 | "this conversion simply by saving it again.\n"; |
307 | |
f7397dc6 |
308 | struct termios cf; |
309 | premsg(&cf); |
c5e438ec |
310 | fputs(message, stderr); |
f7397dc6 |
311 | postmsg(&cf); |
c5e438ec |
312 | } |
313 | |
b51259f6 |
314 | void console_provide_logctx(void *logctx) |
315 | { |
316 | console_logctx = logctx; |
317 | } |
318 | |
cbe2d68f |
319 | void logevent(void *frontend, const char *string) |
c5e438ec |
320 | { |
f7397dc6 |
321 | struct termios cf; |
322 | premsg(&cf); |
b51259f6 |
323 | if (console_logctx) |
324 | log_eventlog(console_logctx, string); |
f7397dc6 |
325 | postmsg(&cf); |
c5e438ec |
326 | } |
327 | |
59b0bfe7 |
328 | /* |
329 | * Special function to print text to the console for password |
330 | * prompts and the like. Uses /dev/tty or stderr, in that order of |
331 | * preference; also sanitises escape sequences out of the text, on |
332 | * the basis that it might have been sent by a hostile SSH server |
333 | * doing malicious keyboard-interactive. |
334 | */ |
335 | static void console_prompt_text(FILE **confp, const char *data, int len) |
c5e438ec |
336 | { |
c5e438ec |
337 | int i; |
59b0bfe7 |
338 | |
339 | if (!*confp) { |
340 | if ((*confp = fopen("/dev/tty", "w")) == NULL) |
341 | *confp = stderr; |
342 | } |
343 | |
edd0cb8a |
344 | for (i = 0; i < len; i++) |
345 | if ((data[i] & 0x60) || (data[i] == '\n')) |
59b0bfe7 |
346 | fputc(data[i], *confp); |
347 | fflush(*confp); |
edd0cb8a |
348 | } |
c5e438ec |
349 | |
edd0cb8a |
350 | int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen) |
351 | { |
352 | size_t curr_prompt; |
59b0bfe7 |
353 | FILE *confp = NULL; |
edd0cb8a |
354 | |
355 | /* |
356 | * Zero all the results, in case we abort half-way through. |
357 | */ |
358 | { |
359 | int i; |
360 | for (i = 0; i < p->n_prompts; i++) |
361 | memset(p->prompts[i]->result, 0, p->prompts[i]->result_len); |
362 | } |
363 | |
cbe80b75 |
364 | if (p->n_prompts && console_batch_mode) |
a21a6a9a |
365 | return 0; |
edd0cb8a |
366 | |
367 | /* |
368 | * Preamble. |
369 | */ |
370 | /* We only print the `name' caption if we have to... */ |
371 | if (p->name_reqd && p->name) { |
372 | size_t l = strlen(p->name); |
59b0bfe7 |
373 | console_prompt_text(&confp, p->name, l); |
edd0cb8a |
374 | if (p->name[l-1] != '\n') |
59b0bfe7 |
375 | console_prompt_text(&confp, "\n", 1); |
edd0cb8a |
376 | } |
377 | /* ...but we always print any `instruction'. */ |
378 | if (p->instruction) { |
379 | size_t l = strlen(p->instruction); |
59b0bfe7 |
380 | console_prompt_text(&confp, p->instruction, l); |
edd0cb8a |
381 | if (p->instruction[l-1] != '\n') |
59b0bfe7 |
382 | console_prompt_text(&confp, "\n", 1); |
edd0cb8a |
383 | } |
384 | |
385 | for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) { |
386 | |
387 | struct termios oldmode, newmode; |
388 | int i; |
389 | prompt_t *pr = p->prompts[curr_prompt]; |
390 | |
c5e438ec |
391 | tcgetattr(0, &oldmode); |
392 | newmode = oldmode; |
393 | newmode.c_lflag |= ISIG | ICANON; |
edd0cb8a |
394 | if (!pr->echo) |
c5e438ec |
395 | newmode.c_lflag &= ~ECHO; |
396 | else |
397 | newmode.c_lflag |= ECHO; |
398 | tcsetattr(0, TCSANOW, &newmode); |
399 | |
59b0bfe7 |
400 | console_prompt_text(&confp, pr->prompt, strlen(pr->prompt)); |
edd0cb8a |
401 | |
402 | i = read(0, pr->result, pr->result_len - 1); |
c5e438ec |
403 | |
404 | tcsetattr(0, TCSANOW, &oldmode); |
405 | |
edd0cb8a |
406 | if (i > 0 && pr->result[i-1] == '\n') |
c5e438ec |
407 | i--; |
edd0cb8a |
408 | pr->result[i] = '\0'; |
c5e438ec |
409 | |
edd0cb8a |
410 | if (!pr->echo) |
59b0bfe7 |
411 | console_prompt_text(&confp, "\n", 1); |
a21a6a9a |
412 | |
c5e438ec |
413 | } |
edd0cb8a |
414 | |
59b0bfe7 |
415 | if (confp && confp != stderr) |
416 | fclose(confp); |
edd0cb8a |
417 | |
59b0bfe7 |
418 | return 1; /* success */ |
c5e438ec |
419 | } |
420 | |
421 | void frontend_keypress(void *handle) |
422 | { |
423 | /* |
424 | * This is nothing but a stub, in console code. |
425 | */ |
426 | return; |
427 | } |
47a6b94c |
428 | |
429 | int is_interactive(void) |
430 | { |
431 | return isatty(0); |
432 | } |
46ed7b64 |
433 | |
434 | /* |
435 | * X11-forwarding-related things suitable for console. |
436 | */ |
437 | |
46ed7b64 |
438 | char *platform_get_x_display(void) { |
439 | return dupstr(getenv("DISPLAY")); |
440 | } |