Workarounds for compiling with -D_FORTIFY_SOURCE=2 (as Ubuntu does), which
[u/mdw/putty] / unix / uxcons.c
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
19 static void *console_logctx = NULL;
20
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
46 /*
47 * Clean up and exit.
48 */
49 void cleanup_exit(int code)
50 {
51 /*
52 * Clean up.
53 */
54 sk_cleanup();
55 random_save_seed();
56 exit(code);
57 }
58
59 void set_busy_status(void *frontend, int status)
60 {
61 }
62
63 void update_specials_menu(void *frontend)
64 {
65 }
66
67 void notify_remote_exit(void *frontend)
68 {
69 }
70
71 void timer_change_notify(long next)
72 {
73 }
74
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)
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"
84 "The server's %s key fingerprint is:\n"
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"
90 "The server's %s key fingerprint is:\n"
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"
106 "The new %s key fingerprint is:\n"
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"
115 "The new %s key fingerprint is:\n"
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];
129 struct termios cf;
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 */
137 return 1;
138
139 premsg(&cf);
140 if (ret == 2) { /* key was different */
141 if (console_batch_mode) {
142 fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
143 return 0;
144 }
145 fprintf(stderr, wrongmsg, keytype, fingerprint);
146 fflush(stderr);
147 }
148 if (ret == 1) { /* key was absent */
149 if (console_batch_mode) {
150 fprintf(stderr, absentmsg_batch, keytype, fingerprint);
151 return 0;
152 }
153 fprintf(stderr, absentmsg, keytype, fingerprint);
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';
164 if (read(0, line, sizeof(line) - 1) <= 0)
165 /* handled below */;
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);
172 postmsg(&cf);
173 return 1;
174 } else {
175 fprintf(stderr, abandoned);
176 postmsg(&cf);
177 return 0;
178 }
179 }
180
181 /*
182 * Ask whether the selected algorithm is acceptable (since it was
183 * below the configured 'warn' threshold).
184 */
185 int askalg(void *frontend, const char *algtype, const char *algname,
186 void (*callback)(void *ctx, int result), void *ctx)
187 {
188 static const char msg[] =
189 "The first %s supported by the server is\n"
190 "%s, which is below the configured warning threshold.\n"
191 "Continue with connection? (y/n) ";
192 static const char msg_batch[] =
193 "The first %s supported by the server is\n"
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];
199 struct termios cf;
200
201 premsg(&cf);
202 if (console_batch_mode) {
203 fprintf(stderr, msg_batch, algtype, algname);
204 return 0;
205 }
206
207 fprintf(stderr, msg, algtype, algname);
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';
217 if (read(0, line, sizeof(line) - 1) <= 0)
218 /* handled below */;
219 tcsetattr(0, TCSANOW, &oldmode);
220 }
221
222 if (line[0] == 'y' || line[0] == 'Y') {
223 postmsg(&cf);
224 return 1;
225 } else {
226 fprintf(stderr, abandoned);
227 postmsg(&cf);
228 return 0;
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 */
236 int askappend(void *frontend, Filename filename,
237 void (*callback)(void *ctx, int result), void *ctx)
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];
253 struct termios cf;
254
255 premsg(&cf);
256 if (console_batch_mode) {
257 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path);
258 fflush(stderr);
259 return 0;
260 }
261 fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path);
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';
271 if (read(0, line, sizeof(line) - 1) <= 0)
272 /* handled below */;
273 tcsetattr(0, TCSANOW, &oldmode);
274 }
275
276 postmsg(&cf);
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[] =
298 "You are loading an SSH-2 private key which has an\n"
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
308 struct termios cf;
309 premsg(&cf);
310 fputs(message, stderr);
311 postmsg(&cf);
312 }
313
314 void console_provide_logctx(void *logctx)
315 {
316 console_logctx = logctx;
317 }
318
319 void logevent(void *frontend, const char *string)
320 {
321 struct termios cf;
322 premsg(&cf);
323 if (console_logctx)
324 log_eventlog(console_logctx, string);
325 postmsg(&cf);
326 }
327
328 static void console_data_untrusted(const char *data, int len)
329 {
330 int i;
331 for (i = 0; i < len; i++)
332 if ((data[i] & 0x60) || (data[i] == '\n'))
333 fputc(data[i], stdout);
334 fflush(stdout);
335 }
336
337 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
338 {
339 size_t curr_prompt;
340
341 /*
342 * Zero all the results, in case we abort half-way through.
343 */
344 {
345 int i;
346 for (i = 0; i < p->n_prompts; i++)
347 memset(p->prompts[i]->result, 0, p->prompts[i]->result_len);
348 }
349
350 if (p->n_prompts && console_batch_mode)
351 return 0;
352
353 /*
354 * Preamble.
355 */
356 /* We only print the `name' caption if we have to... */
357 if (p->name_reqd && p->name) {
358 size_t l = strlen(p->name);
359 console_data_untrusted(p->name, l);
360 if (p->name[l-1] != '\n')
361 console_data_untrusted("\n", 1);
362 }
363 /* ...but we always print any `instruction'. */
364 if (p->instruction) {
365 size_t l = strlen(p->instruction);
366 console_data_untrusted(p->instruction, l);
367 if (p->instruction[l-1] != '\n')
368 console_data_untrusted("\n", 1);
369 }
370
371 for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {
372
373 struct termios oldmode, newmode;
374 int i;
375 prompt_t *pr = p->prompts[curr_prompt];
376
377 tcgetattr(0, &oldmode);
378 newmode = oldmode;
379 newmode.c_lflag |= ISIG | ICANON;
380 if (!pr->echo)
381 newmode.c_lflag &= ~ECHO;
382 else
383 newmode.c_lflag |= ECHO;
384 tcsetattr(0, TCSANOW, &newmode);
385
386 console_data_untrusted(pr->prompt, strlen(pr->prompt));
387
388 i = read(0, pr->result, pr->result_len - 1);
389
390 tcsetattr(0, TCSANOW, &oldmode);
391
392 if (i > 0 && pr->result[i-1] == '\n')
393 i--;
394 pr->result[i] = '\0';
395
396 if (!pr->echo)
397 fputs("\n", stdout);
398
399 }
400
401 return 1; /* success */
402
403 }
404
405 void frontend_keypress(void *handle)
406 {
407 /*
408 * This is nothing but a stub, in console code.
409 */
410 return;
411 }
412
413 int is_interactive(void)
414 {
415 return isatty(0);
416 }
417
418 /*
419 * X11-forwarding-related things suitable for console.
420 */
421
422 char *platform_get_x_display(void) {
423 return dupstr(getenv("DISPLAY"));
424 }