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 set_busy_status(void *frontend, int status)
38 void update_specials_menu(void *frontend)
42 void notify_remote_exit(void *frontend)
46 void timer_change_notify(long next)
50 int 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)
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"
59 "The server's %s key fingerprint is:\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"
65 "The server's %s key fingerprint is:\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"
73 "Store key in cache? (y/n) ";
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"
81 "The new %s key fingerprint is:\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"
90 "The new %s key fingerprint is:\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"
99 "Update cached key? (y/n, Return cancels connection) ";
101 static const char abandoned[] = "Connection abandoned.\n";
108 ret = verify_host_key(host, port, keytype, keystr);
110 if (ret == 0) /* success - key matched OK */
113 if (ret == 2) { /* key was different */
114 if (console_batch_mode) {
115 fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
118 fprintf(stderr, wrongmsg, keytype, fingerprint);
121 if (ret == 1) { /* key was absent */
122 if (console_batch_mode) {
123 fprintf(stderr, absentmsg_batch, keytype, fingerprint);
126 fprintf(stderr, absentmsg, keytype, fingerprint);
131 struct termios oldmode, newmode;
132 tcgetattr(0, &oldmode);
134 newmode.c_lflag |= ECHO | ISIG | ICANON;
135 tcsetattr(0, TCSANOW, &newmode);
137 read(0, line, sizeof(line) - 1);
138 tcsetattr(0, TCSANOW, &oldmode);
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);
146 fprintf(stderr, abandoned);
152 * Ask whether the selected algorithm is acceptable (since it was
153 * below the configured 'warn' threshold).
155 int askalg(void *frontend, const char *algtype, const char *algname,
156 void (*callback)(void *ctx, int result), void *ctx)
158 static const char msg[] =
159 "The first %s supported by the server is\n"
160 "%s, which is below the configured warning threshold.\n"
161 "Continue with connection? (y/n) ";
162 static const char msg_batch[] =
163 "The first %s supported by the server is\n"
164 "%s, which is below the configured warning threshold.\n"
165 "Connection abandoned.\n";
166 static const char abandoned[] = "Connection abandoned.\n";
170 if (console_batch_mode) {
171 fprintf(stderr, msg_batch, algtype, algname);
175 fprintf(stderr, msg, algtype, algname);
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,
202 void (*callback)(void *ctx, int result), void *ctx)
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) ";
213 static const char msgtemplate_batch[] =
214 "The session log file \"%.*s\" already exists.\n"
215 "Logging will not be enabled.\n";
219 if (console_batch_mode) {
220 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path);
224 fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path);
228 struct termios oldmode, newmode;
229 tcgetattr(0, &oldmode);
231 newmode.c_lflag |= ECHO | ISIG | ICANON;
232 tcsetattr(0, TCSANOW, &newmode);
234 read(0, line, sizeof(line) - 1);
235 tcsetattr(0, TCSANOW, &oldmode);
238 if (line[0] == 'y' || line[0] == 'Y')
240 else if (line[0] == 'n' || line[0] == 'N')
247 * Warn about the obsolescent key file format.
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.
256 void old_keyfile_warning(void)
258 static const char message[] =
259 "You are loading an SSH-2 private key which has an\n"
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"
266 "Once the key is loaded into PuTTYgen, you can perform\n"
267 "this conversion simply by saving it again.\n";
269 fputs(message, stderr);
272 void console_provide_logctx(void *logctx)
274 console_logctx = logctx;
277 void logevent(void *frontend, const char *string)
280 log_eventlog(console_logctx, string);
283 int console_get_line(const char *prompt, char *str,
284 int maxlen, int is_pw)
286 struct termios oldmode, newmode;
289 if (console_batch_mode) {
294 tcgetattr(0, &oldmode);
296 newmode.c_lflag |= ISIG | ICANON;
298 newmode.c_lflag &= ~ECHO;
300 newmode.c_lflag |= ECHO;
301 tcsetattr(0, TCSANOW, &newmode);
303 fputs(prompt, stdout);
305 i = read(0, str, maxlen - 1);
307 tcsetattr(0, TCSANOW, &oldmode);
309 if (i > 0 && str[i-1] == '\n')
320 void frontend_keypress(void *handle)
323 * This is nothing but a stub, in console code.
328 int is_interactive(void)
334 * X11-forwarding-related things suitable for console.
337 const char platform_x11_best_transport[] = "unix";
339 char *platform_get_x_display(void) {
340 return dupstr(getenv("DISPLAY"));