OSDN Git Service

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