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
)
29 if (cfg
.protocol
== PROT_SSH
)
35 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
36 char *keystr
, char *fingerprint
)
40 static const char absentmsg_batch
[] =
41 "The server's host key is not cached. You have no guarantee\n"
42 "that the server is the computer you think it is.\n"
43 "The server's key fingerprint is:\n"
45 "Connection abandoned.\n";
46 static const char absentmsg
[] =
47 "The server's host key is not cached. You have no guarantee\n"
48 "that the server is the computer you think it is.\n"
49 "The server's key fingerprint is:\n"
51 "If you trust this host, enter \"y\" to add the key to\n"
52 "PuTTY's cache and carry on connecting.\n"
53 "If you want to carry on connecting just once, without\n"
54 "adding the key to the cache, enter \"n\".\n"
55 "If you do not trust this host, press Return to abandon the\n"
57 "Store key in cache? (y/n) ";
59 static const char wrongmsg_batch
[] =
60 "WARNING - POTENTIAL SECURITY BREACH!\n"
61 "The server's host key does not match the one PuTTY has\n"
62 "cached. This means that either the server administrator\n"
63 "has changed the host key, or you have actually connected\n"
64 "to another computer pretending to be the server.\n"
65 "The new key fingerprint is:\n"
67 "Connection abandoned.\n";
68 static const char wrongmsg
[] =
69 "WARNING - POTENTIAL SECURITY BREACH!\n"
70 "The server's host key does not match the one PuTTY has\n"
71 "cached. This means that either the server administrator\n"
72 "has changed the host key, or you have actually connected\n"
73 "to another computer pretending to be the server.\n"
74 "The new key fingerprint is:\n"
76 "If you were expecting this change and trust the new key,\n"
77 "enter \"y\" to update PuTTY's cache and continue connecting.\n"
78 "If you want to carry on connecting but without updating\n"
79 "the cache, enter \"n\".\n"
80 "If you want to abandon the connection completely, press\n"
81 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
83 "Update cached key? (y/n, Return cancels connection) ";
85 static const char abandoned
[] = "Connection abandoned.\n";
92 ret
= verify_host_key(host
, port
, keytype
, keystr
);
94 if (ret
== 0) /* success - key matched OK */
97 if (ret
== 2) { /* key was different */
98 if (console_batch_mode
) {
99 fprintf(stderr
, wrongmsg_batch
, fingerprint
);
102 fprintf(stderr
, wrongmsg
, fingerprint
);
105 if (ret
== 1) { /* key was absent */
106 if (console_batch_mode
) {
107 fprintf(stderr
, absentmsg_batch
, fingerprint
);
110 fprintf(stderr
, absentmsg
, fingerprint
);
115 struct termios oldmode
, newmode
;
116 tcgetattr(0, &oldmode
);
118 newmode
.c_lflag
|= ECHO
| ISIG
| ICANON
;
119 tcsetattr(0, TCSANOW
, &newmode
);
121 read(0, line
, sizeof(line
) - 1);
122 tcsetattr(0, TCSANOW
, &oldmode
);
125 if (line
[0] != '\0' && line
[0] != '\r' && line
[0] != '\n') {
126 if (line
[0] == 'y' || line
[0] == 'Y')
127 store_host_key(host
, port
, keytype
, keystr
);
129 fprintf(stderr
, abandoned
);
135 * Ask whether the selected cipher is acceptable (since it was
136 * below the configured 'warn' threshold).
137 * cs: 0 = both ways, 1 = client->server, 2 = server->client
139 void askcipher(void *frontend
, char *ciphername
, int cs
)
141 static const char msg
[] =
142 "The first %scipher supported by the server is\n"
143 "%s, which is below the configured warning threshold.\n"
144 "Continue with connection? (y/n) ";
145 static const char msg_batch
[] =
146 "The first %scipher supported by the server is\n"
147 "%s, which is below the configured warning threshold.\n"
148 "Connection abandoned.\n";
149 static const char abandoned
[] = "Connection abandoned.\n";
153 if (console_batch_mode
) {
154 fprintf(stderr
, msg_batch
,
156 (cs
== 1) ?
"client-to-server " : "server-to-client ",
163 (cs
== 1) ?
"client-to-server " : "server-to-client ",
168 struct termios oldmode
, newmode
;
169 tcgetattr(0, &oldmode
);
171 newmode
.c_lflag
|= ECHO
| ISIG
| ICANON
;
172 tcsetattr(0, TCSANOW
, &newmode
);
174 read(0, line
, sizeof(line
) - 1);
175 tcsetattr(0, TCSANOW
, &oldmode
);
178 if (line
[0] == 'y' || line
[0] == 'Y') {
181 fprintf(stderr
, abandoned
);
187 * Ask whether to wipe a session log file before writing to it.
188 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
190 int askappend(void *frontend
, char *filename
)
192 static const char msgtemplate
[] =
193 "The session log file \"%.*s\" already exists.\n"
194 "You can overwrite it with a new session log,\n"
195 "append your session log to the end of it,\n"
196 "or disable session logging for this session.\n"
197 "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
198 "or just press Return to disable logging.\n"
199 "Wipe the log file? (y/n, Return cancels logging) ";
201 static const char msgtemplate_batch
[] =
202 "The session log file \"%.*s\" already exists.\n"
203 "Logging will not be enabled.\n";
207 if (cfg
.logxfovr
!= LGXF_ASK
) {
208 return ((cfg
.logxfovr
== LGXF_OVR
) ?
2 : 1);
210 if (console_batch_mode
) {
211 fprintf(stderr
, msgtemplate_batch
, FILENAME_MAX
, filename
);
215 fprintf(stderr
, msgtemplate
, FILENAME_MAX
, filename
);
219 struct termios oldmode
, newmode
;
220 tcgetattr(0, &oldmode
);
222 newmode
.c_lflag
|= ECHO
| ISIG
| ICANON
;
223 tcsetattr(0, TCSANOW
, &newmode
);
225 read(0, line
, sizeof(line
) - 1);
226 tcsetattr(0, TCSANOW
, &oldmode
);
229 if (line
[0] == 'y' || line
[0] == 'Y')
231 else if (line
[0] == 'n' || line
[0] == 'N')
238 * Warn about the obsolescent key file format.
240 * Uniquely among these functions, this one does _not_ expect a
241 * frontend handle. This means that if PuTTY is ported to a
242 * platform which requires frontend handles, this function will be
243 * an anomaly. Fortunately, the problem it addresses will not have
244 * been present on that platform, so it can plausibly be
245 * implemented as an empty function.
247 void old_keyfile_warning(void)
249 static const char message
[] =
250 "You are loading an SSH 2 private key which has an\n"
251 "old version of the file format. This means your key\n"
252 "file is not fully tamperproof. Future versions of\n"
253 "PuTTY may stop supporting this private key format,\n"
254 "so we recommend you convert your key to the new\n"
257 "Once the key is loaded into PuTTYgen, you can perform\n"
258 "this conversion simply by saving it again.\n";
260 fputs(message
, stderr
);
263 void logevent(void *frontend
, char *string
)
265 log_eventlog(logctx
, string
);
268 int console_get_line(const char *prompt
, char *str
,
269 int maxlen
, int is_pw
)
271 struct termios oldmode
, newmode
;
274 if (console_batch_mode
) {
278 tcgetattr(0, &oldmode
);
280 newmode
.c_lflag
|= ISIG
| ICANON
;
282 newmode
.c_lflag
&= ~ECHO
;
284 newmode
.c_lflag
|= ECHO
;
285 tcsetattr(0, TCSANOW
, &newmode
);
287 fputs(prompt
, stdout
);
289 i
= read(0, str
, maxlen
- 1);
291 tcsetattr(0, TCSANOW
, &oldmode
);
293 if (i
> 0 && str
[i
-1] == '\n')
303 void frontend_keypress(void *handle
)
306 * This is nothing but a stub, in console code.