]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/wincons.c
Make get_user_sid() return the cached copy if one already exists.
[PuTTY.git] / windows / wincons.c
1 /*
2  * wincons.c - various interactive-prompt routines shared between
3  * the Windows console PuTTY tools
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9
10 #include "putty.h"
11 #include "storage.h"
12 #include "ssh.h"
13
14 int console_batch_mode = FALSE;
15
16 static void *console_logctx = NULL;
17
18 /*
19  * Clean up and exit.
20  */
21 void cleanup_exit(int code)
22 {
23     /*
24      * Clean up.
25      */
26     sk_cleanup();
27
28     random_save_seed();
29 #ifdef MSCRYPTOAPI
30     crypto_wrapup();
31 #endif
32
33     exit(code);
34 }
35
36 void set_busy_status(void *frontend, int status)
37 {
38 }
39
40 void notify_remote_exit(void *frontend)
41 {
42 }
43
44 void timer_change_notify(unsigned long next)
45 {
46 }
47
48 int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
49                         char *keystr, char *fingerprint,
50                         void (*callback)(void *ctx, int result), void *ctx)
51 {
52     int ret;
53     HANDLE hin;
54     DWORD savemode, i;
55
56     static const char absentmsg_batch[] =
57         "The server's host key is not cached in the registry. You\n"
58         "have no guarantee that the server is the computer you\n"
59         "think it is.\n"
60         "The server's %s key fingerprint is:\n"
61         "%s\n"
62         "Connection abandoned.\n";
63     static const char absentmsg[] =
64         "The server's host key is not cached in the registry. You\n"
65         "have no guarantee that the server is the computer you\n"
66         "think it is.\n"
67         "The server's %s key fingerprint is:\n"
68         "%s\n"
69         "If you trust this host, enter \"y\" to add the key to\n"
70         "PuTTY's cache and carry on connecting.\n"
71         "If you want to carry on connecting just once, without\n"
72         "adding the key to the cache, enter \"n\".\n"
73         "If you do not trust this host, press Return to abandon the\n"
74         "connection.\n"
75         "Store key in cache? (y/n) ";
76
77     static const char wrongmsg_batch[] =
78         "WARNING - POTENTIAL SECURITY BREACH!\n"
79         "The server's host key does not match the one PuTTY has\n"
80         "cached in the registry. This means that either the\n"
81         "server administrator has changed the host key, or you\n"
82         "have actually connected to another computer pretending\n"
83         "to be the server.\n"
84         "The new %s key fingerprint is:\n"
85         "%s\n"
86         "Connection abandoned.\n";
87     static const char wrongmsg[] =
88         "WARNING - POTENTIAL SECURITY BREACH!\n"
89         "The server's host key does not match the one PuTTY has\n"
90         "cached in the registry. This means that either the\n"
91         "server administrator has changed the host key, or you\n"
92         "have actually connected to another computer pretending\n"
93         "to be the server.\n"
94         "The new %s key fingerprint is:\n"
95         "%s\n"
96         "If you were expecting this change and trust the new key,\n"
97         "enter \"y\" to update PuTTY's cache and continue connecting.\n"
98         "If you want to carry on connecting but without updating\n"
99         "the cache, enter \"n\".\n"
100         "If you want to abandon the connection completely, press\n"
101         "Return to cancel. Pressing Return is the ONLY guaranteed\n"
102         "safe choice.\n"
103         "Update cached key? (y/n, Return cancels connection) ";
104
105     static const char abandoned[] = "Connection abandoned.\n";
106
107     char line[32];
108
109     /*
110      * Verify the key against the registry.
111      */
112     ret = verify_host_key(host, port, keytype, keystr);
113
114     if (ret == 0)                      /* success - key matched OK */
115         return 1;
116
117     if (ret == 2) {                    /* key was different */
118         if (console_batch_mode) {
119             fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
120             return 0;
121         }
122         fprintf(stderr, wrongmsg, keytype, fingerprint);
123         fflush(stderr);
124     }
125     if (ret == 1) {                    /* key was absent */
126         if (console_batch_mode) {
127             fprintf(stderr, absentmsg_batch, keytype, fingerprint);
128             return 0;
129         }
130         fprintf(stderr, absentmsg, keytype, fingerprint);
131         fflush(stderr);
132     }
133
134     hin = GetStdHandle(STD_INPUT_HANDLE);
135     GetConsoleMode(hin, &savemode);
136     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
137                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
138     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
139     SetConsoleMode(hin, savemode);
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);
144         return 1;
145     } else {
146         fprintf(stderr, abandoned);
147         return 0;
148     }
149 }
150
151 void update_specials_menu(void *frontend)
152 {
153 }
154
155 /*
156  * Ask whether the selected algorithm is acceptable (since it was
157  * below the configured 'warn' threshold).
158  */
159 int askalg(void *frontend, const char *algtype, const char *algname,
160            void (*callback)(void *ctx, int result), void *ctx)
161 {
162     HANDLE hin;
163     DWORD savemode, i;
164
165     static const char msg[] =
166         "The first %s supported by the server is\n"
167         "%s, which is below the configured warning threshold.\n"
168         "Continue with connection? (y/n) ";
169     static const char msg_batch[] =
170         "The first %s supported by the server is\n"
171         "%s, which is below the configured warning threshold.\n"
172         "Connection abandoned.\n";
173     static const char abandoned[] = "Connection abandoned.\n";
174
175     char line[32];
176
177     if (console_batch_mode) {
178         fprintf(stderr, msg_batch, algtype, algname);
179         return 0;
180     }
181
182     fprintf(stderr, msg, algtype, algname);
183     fflush(stderr);
184
185     hin = GetStdHandle(STD_INPUT_HANDLE);
186     GetConsoleMode(hin, &savemode);
187     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
188                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
189     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
190     SetConsoleMode(hin, savemode);
191
192     if (line[0] == 'y' || line[0] == 'Y') {
193         return 1;
194     } else {
195         fprintf(stderr, abandoned);
196         return 0;
197     }
198 }
199
200 /*
201  * Ask whether to wipe a session log file before writing to it.
202  * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
203  */
204 int askappend(void *frontend, Filename *filename,
205               void (*callback)(void *ctx, int result), void *ctx)
206 {
207     HANDLE hin;
208     DWORD savemode, i;
209
210     static const char msgtemplate[] =
211         "The session log file \"%.*s\" already exists.\n"
212         "You can overwrite it with a new session log,\n"
213         "append your session log to the end of it,\n"
214         "or disable session logging for this session.\n"
215         "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
216         "or just press Return to disable logging.\n"
217         "Wipe the log file? (y/n, Return cancels logging) ";
218
219     static const char msgtemplate_batch[] =
220         "The session log file \"%.*s\" already exists.\n"
221         "Logging will not be enabled.\n";
222
223     char line[32];
224
225     if (console_batch_mode) {
226         fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename->path);
227         fflush(stderr);
228         return 0;
229     }
230     fprintf(stderr, msgtemplate, FILENAME_MAX, filename->path);
231     fflush(stderr);
232
233     hin = GetStdHandle(STD_INPUT_HANDLE);
234     GetConsoleMode(hin, &savemode);
235     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
236                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
237     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
238     SetConsoleMode(hin, savemode);
239
240     if (line[0] == 'y' || line[0] == 'Y')
241         return 2;
242     else if (line[0] == 'n' || line[0] == 'N')
243         return 1;
244     else
245         return 0;
246 }
247
248 /*
249  * Warn about the obsolescent key file format.
250  * 
251  * Uniquely among these functions, this one does _not_ expect a
252  * frontend handle. This means that if PuTTY is ported to a
253  * platform which requires frontend handles, this function will be
254  * an anomaly. Fortunately, the problem it addresses will not have
255  * been present on that platform, so it can plausibly be
256  * implemented as an empty function.
257  */
258 void old_keyfile_warning(void)
259 {
260     static const char message[] =
261         "You are loading an SSH-2 private key which has an\n"
262         "old version of the file format. This means your key\n"
263         "file is not fully tamperproof. Future versions of\n"
264         "PuTTY may stop supporting this private key format,\n"
265         "so we recommend you convert your key to the new\n"
266         "format.\n"
267         "\n"
268         "Once the key is loaded into PuTTYgen, you can perform\n"
269         "this conversion simply by saving it again.\n";
270
271     fputs(message, stderr);
272 }
273
274 /*
275  * Display the fingerprints of the PGP Master Keys to the user.
276  */
277 void pgp_fingerprints(void)
278 {
279     fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
280           "be used to establish a trust path from this executable to another\n"
281           "one. See the manual for more information.\n"
282           "(Note: these fingerprints have nothing to do with SSH!)\n"
283           "\n"
284           "PuTTY Master Key as of 2015 (RSA, 4096-bit):\n"
285           "  " PGP_MASTER_KEY_FP "\n\n"
286           "Original PuTTY Master Key (RSA, 1024-bit):\n"
287           "  " PGP_RSA_MASTER_KEY_FP "\n"
288           "Original PuTTY Master Key (DSA, 1024-bit):\n"
289           "  " PGP_DSA_MASTER_KEY_FP "\n", stdout);
290 }
291
292 void console_provide_logctx(void *logctx)
293 {
294     console_logctx = logctx;
295 }
296
297 void logevent(void *frontend, const char *string)
298 {
299     log_eventlog(console_logctx, string);
300 }
301
302 static void console_data_untrusted(HANDLE hout, const char *data, int len)
303 {
304     DWORD dummy;
305     /* FIXME: control-character filtering */
306     WriteFile(hout, data, len, &dummy, NULL);
307 }
308
309 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
310 {
311     HANDLE hin, hout;
312     size_t curr_prompt;
313
314     /*
315      * Zero all the results, in case we abort half-way through.
316      */
317     {
318         int i;
319         for (i = 0; i < (int)p->n_prompts; i++)
320             prompt_set_result(p->prompts[i], "");
321     }
322
323     /*
324      * The prompts_t might contain a message to be displayed but no
325      * actual prompt. More usually, though, it will contain
326      * questions that the user needs to answer, in which case we
327      * need to ensure that we're able to get the answers.
328      */
329     if (p->n_prompts) {
330         if (console_batch_mode)
331             return 0;
332         hin = GetStdHandle(STD_INPUT_HANDLE);
333         if (hin == INVALID_HANDLE_VALUE) {
334             fprintf(stderr, "Cannot get standard input handle\n");
335             cleanup_exit(1);
336         }
337     }
338
339     /*
340      * And if we have anything to print, we need standard output.
341      */
342     if ((p->name_reqd && p->name) || p->instruction || p->n_prompts) {
343         hout = GetStdHandle(STD_OUTPUT_HANDLE);
344         if (hout == INVALID_HANDLE_VALUE) {
345             fprintf(stderr, "Cannot get standard output handle\n");
346             cleanup_exit(1);
347         }
348     }
349
350     /*
351      * Preamble.
352      */
353     /* We only print the `name' caption if we have to... */
354     if (p->name_reqd && p->name) {
355         size_t l = strlen(p->name);
356         console_data_untrusted(hout, p->name, l);
357         if (p->name[l-1] != '\n')
358             console_data_untrusted(hout, "\n", 1);
359     }
360     /* ...but we always print any `instruction'. */
361     if (p->instruction) {
362         size_t l = strlen(p->instruction);
363         console_data_untrusted(hout, p->instruction, l);
364         if (p->instruction[l-1] != '\n')
365             console_data_untrusted(hout, "\n", 1);
366     }
367
368     for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {
369
370         DWORD savemode, newmode;
371         int len;
372         prompt_t *pr = p->prompts[curr_prompt];
373
374         GetConsoleMode(hin, &savemode);
375         newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
376         if (!pr->echo)
377             newmode &= ~ENABLE_ECHO_INPUT;
378         else
379             newmode |= ENABLE_ECHO_INPUT;
380         SetConsoleMode(hin, newmode);
381
382         console_data_untrusted(hout, pr->prompt, strlen(pr->prompt));
383
384         len = 0;
385         while (1) {
386             DWORD ret = 0;
387             BOOL r;
388
389             prompt_ensure_result_size(pr, len * 5 / 4 + 512);
390
391             r = ReadFile(hin, pr->result + len, pr->resultsize - len - 1,
392                          &ret, NULL);
393
394             if (!r || ret == 0) {
395                 len = -1;
396                 break;
397             }
398             len += ret;
399             if (pr->result[len - 1] == '\n') {
400                 len--;
401                 if (pr->result[len - 1] == '\r')
402                     len--;
403                 break;
404             }
405         }
406
407         SetConsoleMode(hin, savemode);
408
409         if (!pr->echo) {
410             DWORD dummy;
411             WriteFile(hout, "\r\n", 2, &dummy, NULL);
412         }
413
414         if (len < 0) {
415             return 0;                  /* failure due to read error */
416         }
417
418         pr->result[len] = '\0';
419     }
420
421     return 1; /* success */
422 }
423
424 void frontend_keypress(void *handle)
425 {
426     /*
427      * This is nothing but a stub, in console code.
428      */
429     return;
430 }