2 * uxcons.c: various interactive-prompt routines shared between the
3 * Unix console PuTTY tools
17 int console_batch_mode = FALSE;
19 static void *console_logctx = NULL;
24 void cleanup_exit(int code)
34 void update_specials_menu(void *frontend)
38 void notify_remote_exit(void *frontend)
42 void timer_change_notify(long next)
46 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
47 char *keystr, char *fingerprint)
51 static const char absentmsg_batch[] =
52 "The server's host key is not cached. You have no guarantee\n"
53 "that the server is the computer you think it is.\n"
54 "The server's %s key fingerprint is:\n"
56 "Connection abandoned.\n";
57 static const char absentmsg[] =
58 "The server's host key is not cached. You have no guarantee\n"
59 "that the server is the computer you think it is.\n"
60 "The server's %s key fingerprint is:\n"
62 "If you trust this host, enter \"y\" to add the key to\n"
63 "PuTTY's cache and carry on connecting.\n"
64 "If you want to carry on connecting just once, without\n"
65 "adding the key to the cache, enter \"n\".\n"
66 "If you do not trust this host, press Return to abandon the\n"
68 "Store key in cache? (y/n) ";
70 static const char wrongmsg_batch[] =
71 "WARNING - POTENTIAL SECURITY BREACH!\n"
72 "The server's host key does not match the one PuTTY has\n"
73 "cached. This means that either the server administrator\n"
74 "has changed the host key, or you have actually connected\n"
75 "to another computer pretending to be the server.\n"
76 "The new %s key fingerprint is:\n"
78 "Connection abandoned.\n";
79 static const char wrongmsg[] =
80 "WARNING - POTENTIAL SECURITY BREACH!\n"
81 "The server's host key does not match the one PuTTY has\n"
82 "cached. This means that either the server administrator\n"
83 "has changed the host key, or you have actually connected\n"
84 "to another computer pretending to be the server.\n"
85 "The new %s key fingerprint is:\n"
87 "If you were expecting this change and trust the new key,\n"
88 "enter \"y\" to update PuTTY's cache and continue connecting.\n"
89 "If you want to carry on connecting but without updating\n"
90 "the cache, enter \"n\".\n"
91 "If you want to abandon the connection completely, press\n"
92 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
94 "Update cached key? (y/n, Return cancels connection) ";
96 static const char abandoned[] = "Connection abandoned.\n";
103 ret = verify_host_key(host, port, keytype, keystr);
105 if (ret == 0) /* success - key matched OK */
108 if (ret == 2) { /* key was different */
109 if (console_batch_mode) {
110 fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
113 fprintf(stderr, wrongmsg, keytype, fingerprint);
116 if (ret == 1) { /* key was absent */
117 if (console_batch_mode) {
118 fprintf(stderr, absentmsg_batch, keytype, fingerprint);
121 fprintf(stderr, absentmsg, keytype, fingerprint);
126 struct termios oldmode, newmode;
127 tcgetattr(0, &oldmode);
129 newmode.c_lflag |= ECHO | ISIG | ICANON;
130 tcsetattr(0, TCSANOW, &newmode);
132 read(0, line, sizeof(line) - 1);
133 tcsetattr(0, TCSANOW, &oldmode);
136 if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
137 if (line[0] == 'y' || line[0] == 'Y')
138 store_host_key(host, port, keytype, keystr);
140 fprintf(stderr, abandoned);
146 * Ask whether the selected cipher is acceptable (since it was
147 * below the configured 'warn' threshold).
148 * cs: 0 = both ways, 1 = client->server, 2 = server->client
150 void askcipher(void *frontend, char *ciphername, int cs)
152 static const char msg[] =
153 "The first %scipher supported by the server is\n"
154 "%s, which is below the configured warning threshold.\n"
155 "Continue with connection? (y/n) ";
156 static const char msg_batch[] =
157 "The first %scipher supported by the server is\n"
158 "%s, which is below the configured warning threshold.\n"
159 "Connection abandoned.\n";
160 static const char abandoned[] = "Connection abandoned.\n";
164 if (console_batch_mode) {
165 fprintf(stderr, msg_batch,
167 (cs == 1) ? "client-to-server " : "server-to-client ",
174 (cs == 1) ? "client-to-server " : "server-to-client ",
179 struct termios oldmode, newmode;
180 tcgetattr(0, &oldmode);
182 newmode.c_lflag |= ECHO | ISIG | ICANON;
183 tcsetattr(0, TCSANOW, &newmode);
185 read(0, line, sizeof(line) - 1);
186 tcsetattr(0, TCSANOW, &oldmode);
189 if (line[0] == 'y' || line[0] == 'Y') {
192 fprintf(stderr, abandoned);
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).
201 int askappend(void *frontend, Filename filename)
203 static const char msgtemplate[] =
204 "The session log file \"%.*s\" already exists.\n"
205 "You can overwrite it with a new session log,\n"
206 "append your session log to the end of it,\n"
207 "or disable session logging for this session.\n"
208 "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
209 "or just press Return to disable logging.\n"
210 "Wipe the log file? (y/n, Return cancels logging) ";
212 static const char msgtemplate_batch[] =
213 "The session log file \"%.*s\" already exists.\n"
214 "Logging will not be enabled.\n";
218 if (console_batch_mode) {
219 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path);
223 fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path);
227 struct termios oldmode, newmode;
228 tcgetattr(0, &oldmode);
230 newmode.c_lflag |= ECHO | ISIG | ICANON;
231 tcsetattr(0, TCSANOW, &newmode);
233 read(0, line, sizeof(line) - 1);
234 tcsetattr(0, TCSANOW, &oldmode);
237 if (line[0] == 'y' || line[0] == 'Y')
239 else if (line[0] == 'n' || line[0] == 'N')
246 * Warn about the obsolescent key file format.
248 * Uniquely among these functions, this one does _not_ expect a
249 * frontend handle. This means that if PuTTY is ported to a
250 * platform which requires frontend handles, this function will be
251 * an anomaly. Fortunately, the problem it addresses will not have
252 * been present on that platform, so it can plausibly be
253 * implemented as an empty function.
255 void old_keyfile_warning(void)
257 static const char message[] =
258 "You are loading an SSH 2 private key which has an\n"
259 "old version of the file format. This means your key\n"
260 "file is not fully tamperproof. Future versions of\n"
261 "PuTTY may stop supporting this private key format,\n"
262 "so we recommend you convert your key to the new\n"
265 "Once the key is loaded into PuTTYgen, you can perform\n"
266 "this conversion simply by saving it again.\n";
268 fputs(message, stderr);
271 void console_provide_logctx(void *logctx)
273 console_logctx = logctx;
276 void logevent(void *frontend, const char *string)
279 log_eventlog(console_logctx, string);
282 int console_get_line(const char *prompt, char *str,
283 int maxlen, int is_pw)
285 struct termios oldmode, newmode;
288 if (console_batch_mode) {
293 tcgetattr(0, &oldmode);
295 newmode.c_lflag |= ISIG | ICANON;
297 newmode.c_lflag &= ~ECHO;
299 newmode.c_lflag |= ECHO;
300 tcsetattr(0, TCSANOW, &newmode);
302 fputs(prompt, stdout);
304 i = read(0, str, maxlen - 1);
306 tcsetattr(0, TCSANOW, &oldmode);
308 if (i > 0 && str[i-1] == '\n')
319 void frontend_keypress(void *handle)
322 * This is nothing but a stub, in console code.
327 int is_interactive(void)
333 * X11-forwarding-related things suitable for console.
336 const char platform_x11_best_transport[] = "unix";
338 char *platform_get_x_display(void) {
339 return dupstr(getenv("DISPLAY"));