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