Workarounds for compiling with -D_FORTIFY_SOURCE=2 (as Ubuntu does), which
[u/mdw/putty] / unix / uxcons.c
CommitLineData
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
17int console_batch_mode = FALSE;
18
b51259f6 19static void *console_logctx = NULL;
20
f7397dc6 21static struct termios orig_termios_stderr;
22static int stderr_is_a_tty;
23
24void 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
33void 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}
40void 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 */
49void 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 59void set_busy_status(void *frontend, int status)
60{
61}
62
125105d1 63void update_specials_menu(void *frontend)
64{
65}
66
39934deb 67void notify_remote_exit(void *frontend)
68{
69}
70
71void timer_change_notify(long next)
72{
73}
74
3d9449a1 75int 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 185int 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 236int 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 */
295void 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 314void console_provide_logctx(void *logctx)
315{
316 console_logctx = logctx;
317}
318
cbe2d68f 319void 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
edd0cb8a 328static void console_data_untrusted(const char *data, int len)
c5e438ec 329{
c5e438ec 330 int i;
edd0cb8a 331 for (i = 0; i < len; i++)
332 if ((data[i] & 0x60) || (data[i] == '\n'))
333 fputc(data[i], stdout);
334 fflush(stdout);
335}
c5e438ec 336
edd0cb8a 337int 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
cbe80b75 350 if (p->n_prompts && console_batch_mode)
a21a6a9a 351 return 0;
edd0cb8a 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
c5e438ec 377 tcgetattr(0, &oldmode);
378 newmode = oldmode;
379 newmode.c_lflag |= ISIG | ICANON;
edd0cb8a 380 if (!pr->echo)
c5e438ec 381 newmode.c_lflag &= ~ECHO;
382 else
383 newmode.c_lflag |= ECHO;
384 tcsetattr(0, TCSANOW, &newmode);
385
edd0cb8a 386 console_data_untrusted(pr->prompt, strlen(pr->prompt));
387
388 i = read(0, pr->result, pr->result_len - 1);
c5e438ec 389
390 tcsetattr(0, TCSANOW, &oldmode);
391
edd0cb8a 392 if (i > 0 && pr->result[i-1] == '\n')
c5e438ec 393 i--;
edd0cb8a 394 pr->result[i] = '\0';
c5e438ec 395
edd0cb8a 396 if (!pr->echo)
8d69b472 397 fputs("\n", stdout);
a21a6a9a 398
c5e438ec 399 }
edd0cb8a 400
401 return 1; /* success */
402
c5e438ec 403}
404
405void frontend_keypress(void *handle)
406{
407 /*
408 * This is nothing but a stub, in console code.
409 */
410 return;
411}
47a6b94c 412
413int is_interactive(void)
414{
415 return isatty(0);
416}
46ed7b64 417
418/*
419 * X11-forwarding-related things suitable for console.
420 */
421
46ed7b64 422char *platform_get_x_display(void) {
423 return dupstr(getenv("DISPLAY"));
424}