2 * uxcons.c: various interactive-prompt routines shared between the
3 * Unix console PuTTY tools
17 int console_batch_mode
= FALSE
;
22 void cleanup_exit(int code
)
32 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
33 char *keystr
, char *fingerprint
)
37 static const char absentmsg_batch
[] =
38 "The server's host key is not cached. You have no guarantee\n"
39 "that the server is the computer you think it is.\n"
40 "The server's key fingerprint is:\n"
42 "Connection abandoned.\n";
43 static const char absentmsg
[] =
44 "The server's host key is not cached. You have no guarantee\n"
45 "that the server is the computer you think it is.\n"
46 "The server's key fingerprint is:\n"
48 "If you trust this host, enter \"y\" to add the key to\n"
49 "PuTTY's cache and carry on connecting.\n"
50 "If you want to carry on connecting just once, without\n"
51 "adding the key to the cache, enter \"n\".\n"
52 "If you do not trust this host, press Return to abandon the\n"
54 "Store key in cache? (y/n) ";
56 static const char wrongmsg_batch
[] =
57 "WARNING - POTENTIAL SECURITY BREACH!\n"
58 "The server's host key does not match the one PuTTY has\n"
59 "cached. This means that either the server administrator\n"
60 "has changed the host key, or you have actually connected\n"
61 "to another computer pretending to be the server.\n"
62 "The new key fingerprint is:\n"
64 "Connection abandoned.\n";
65 static const char wrongmsg
[] =
66 "WARNING - POTENTIAL SECURITY BREACH!\n"
67 "The server's host key does not match the one PuTTY has\n"
68 "cached. This means that either the server administrator\n"
69 "has changed the host key, or you have actually connected\n"
70 "to another computer pretending to be the server.\n"
71 "The new key fingerprint is:\n"
73 "If you were expecting this change and trust the new key,\n"
74 "enter \"y\" to update PuTTY's cache and continue connecting.\n"
75 "If you want to carry on connecting but without updating\n"
76 "the cache, enter \"n\".\n"
77 "If you want to abandon the connection completely, press\n"
78 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
80 "Update cached key? (y/n, Return cancels connection) ";
82 static const char abandoned
[] = "Connection abandoned.\n";
89 ret
= verify_host_key(host
, port
, keytype
, keystr
);
91 if (ret
== 0) /* success - key matched OK */
94 if (ret
== 2) { /* key was different */
95 if (console_batch_mode
) {
96 fprintf(stderr
, wrongmsg_batch
, fingerprint
);
99 fprintf(stderr
, wrongmsg
, fingerprint
);
102 if (ret
== 1) { /* key was absent */
103 if (console_batch_mode
) {
104 fprintf(stderr
, absentmsg_batch
, fingerprint
);
107 fprintf(stderr
, absentmsg
, fingerprint
);
112 struct termios oldmode
, newmode
;
113 tcgetattr(0, &oldmode
);
115 newmode
.c_lflag
|= ECHO
| ISIG
| ICANON
;
116 tcsetattr(0, TCSANOW
, &newmode
);
118 read(0, line
, sizeof(line
) - 1);
119 tcsetattr(0, TCSANOW
, &oldmode
);
122 if (line
[0] != '\0' && line
[0] != '\r' && line
[0] != '\n') {
123 if (line
[0] == 'y' || line
[0] == 'Y')
124 store_host_key(host
, port
, keytype
, keystr
);
126 fprintf(stderr
, abandoned
);
132 * Ask whether the selected cipher is acceptable (since it was
133 * below the configured 'warn' threshold).
134 * cs: 0 = both ways, 1 = client->server, 2 = server->client
136 void askcipher(void *frontend
, char *ciphername
, int cs
)
138 static const char msg
[] =
139 "The first %scipher supported by the server is\n"
140 "%s, which is below the configured warning threshold.\n"
141 "Continue with connection? (y/n) ";
142 static const char msg_batch
[] =
143 "The first %scipher supported by the server is\n"
144 "%s, which is below the configured warning threshold.\n"
145 "Connection abandoned.\n";
146 static const char abandoned
[] = "Connection abandoned.\n";
150 if (console_batch_mode
) {
151 fprintf(stderr
, msg_batch
,
153 (cs
== 1) ?
"client-to-server " : "server-to-client ",
160 (cs
== 1) ?
"client-to-server " : "server-to-client ",
165 struct termios oldmode
, newmode
;
166 tcgetattr(0, &oldmode
);
168 newmode
.c_lflag
|= ECHO
| ISIG
| ICANON
;
169 tcsetattr(0, TCSANOW
, &newmode
);
171 read(0, line
, sizeof(line
) - 1);
172 tcsetattr(0, TCSANOW
, &oldmode
);
175 if (line
[0] == 'y' || line
[0] == 'Y') {
178 fprintf(stderr
, abandoned
);
184 * Ask whether to wipe a session log file before writing to it.
185 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
187 int askappend(void *frontend
, char *filename
)
189 static const char msgtemplate
[] =
190 "The session log file \"%.*s\" already exists.\n"
191 "You can overwrite it with a new session log,\n"
192 "append your session log to the end of it,\n"
193 "or disable session logging for this session.\n"
194 "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
195 "or just press Return to disable logging.\n"
196 "Wipe the log file? (y/n, Return cancels logging) ";
198 static const char msgtemplate_batch
[] =
199 "The session log file \"%.*s\" already exists.\n"
200 "Logging will not be enabled.\n";
204 if (console_batch_mode
) {
205 fprintf(stderr
, msgtemplate_batch
, FILENAME_MAX
, filename
);
209 fprintf(stderr
, msgtemplate
, FILENAME_MAX
, filename
);
213 struct termios oldmode
, newmode
;
214 tcgetattr(0, &oldmode
);
216 newmode
.c_lflag
|= ECHO
| ISIG
| ICANON
;
217 tcsetattr(0, TCSANOW
, &newmode
);
219 read(0, line
, sizeof(line
) - 1);
220 tcsetattr(0, TCSANOW
, &oldmode
);
223 if (line
[0] == 'y' || line
[0] == 'Y')
225 else if (line
[0] == 'n' || line
[0] == 'N')
232 * Warn about the obsolescent key file format.
234 * Uniquely among these functions, this one does _not_ expect a
235 * frontend handle. This means that if PuTTY is ported to a
236 * platform which requires frontend handles, this function will be
237 * an anomaly. Fortunately, the problem it addresses will not have
238 * been present on that platform, so it can plausibly be
239 * implemented as an empty function.
241 void old_keyfile_warning(void)
243 static const char message
[] =
244 "You are loading an SSH 2 private key which has an\n"
245 "old version of the file format. This means your key\n"
246 "file is not fully tamperproof. Future versions of\n"
247 "PuTTY may stop supporting this private key format,\n"
248 "so we recommend you convert your key to the new\n"
251 "Once the key is loaded into PuTTYgen, you can perform\n"
252 "this conversion simply by saving it again.\n";
254 fputs(message
, stderr
);
257 void logevent(void *frontend
, char *string
)
259 log_eventlog(logctx
, string
);
262 int console_get_line(const char *prompt
, char *str
,
263 int maxlen
, int is_pw
)
265 struct termios oldmode
, newmode
;
268 if (console_batch_mode
) {
272 tcgetattr(0, &oldmode
);
274 newmode
.c_lflag
|= ISIG
| ICANON
;
276 newmode
.c_lflag
&= ~ECHO
;
278 newmode
.c_lflag
|= ECHO
;
279 tcsetattr(0, TCSANOW
, &newmode
);
281 fputs(prompt
, stdout
);
283 i
= read(0, str
, maxlen
- 1);
285 tcsetattr(0, TCSANOW
, &oldmode
);
287 if (i
> 0 && str
[i
-1] == '\n')
297 void frontend_keypress(void *handle
)
300 * This is nothing but a stub, in console code.