uxcons.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * uxcons.c: various interactive-prompt routines shared between the
  3. * Unix console PuTTY tools
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdarg.h>
  8. #include <assert.h>
  9. #include <termios.h>
  10. #include <unistd.h>
  11. #include "putty.h"
  12. #include "storage.h"
  13. #include "ssh.h"
  14. int console_batch_mode = FALSE;
  15. static void *console_logctx = NULL;
  16. /*
  17. * Clean up and exit.
  18. */
  19. void cleanup_exit(int code)
  20. {
  21. /*
  22. * Clean up.
  23. */
  24. sk_cleanup();
  25. random_save_seed();
  26. exit(code);
  27. }
  28. void update_specials_menu(void *frontend)
  29. {
  30. }
  31. void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
  32. char *keystr, char *fingerprint)
  33. {
  34. int ret;
  35. static const char absentmsg_batch[] =
  36. "The server's host key is not cached. You have no guarantee\n"
  37. "that the server is the computer you think it is.\n"
  38. "The server's %s key fingerprint is:\n"
  39. "%s\n"
  40. "Connection abandoned.\n";
  41. static const char absentmsg[] =
  42. "The server's host key is not cached. You have no guarantee\n"
  43. "that the server is the computer you think it is.\n"
  44. "The server's %s key fingerprint is:\n"
  45. "%s\n"
  46. "If you trust this host, enter \"y\" to add the key to\n"
  47. "PuTTY's cache and carry on connecting.\n"
  48. "If you want to carry on connecting just once, without\n"
  49. "adding the key to the cache, enter \"n\".\n"
  50. "If you do not trust this host, press Return to abandon the\n"
  51. "connection.\n"
  52. "Store key in cache? (y/n) ";
  53. static const char wrongmsg_batch[] =
  54. "WARNING - POTENTIAL SECURITY BREACH!\n"
  55. "The server's host key does not match the one PuTTY has\n"
  56. "cached. This means that either the server administrator\n"
  57. "has changed the host key, or you have actually connected\n"
  58. "to another computer pretending to be the server.\n"
  59. "The new %s key fingerprint is:\n"
  60. "%s\n"
  61. "Connection abandoned.\n";
  62. static const char wrongmsg[] =
  63. "WARNING - POTENTIAL SECURITY BREACH!\n"
  64. "The server's host key does not match the one PuTTY has\n"
  65. "cached. This means that either the server administrator\n"
  66. "has changed the host key, or you have actually connected\n"
  67. "to another computer pretending to be the server.\n"
  68. "The new %s key fingerprint is:\n"
  69. "%s\n"
  70. "If you were expecting this change and trust the new key,\n"
  71. "enter \"y\" to update PuTTY's cache and continue connecting.\n"
  72. "If you want to carry on connecting but without updating\n"
  73. "the cache, enter \"n\".\n"
  74. "If you want to abandon the connection completely, press\n"
  75. "Return to cancel. Pressing Return is the ONLY guaranteed\n"
  76. "safe choice.\n"
  77. "Update cached key? (y/n, Return cancels connection) ";
  78. static const char abandoned[] = "Connection abandoned.\n";
  79. char line[32];
  80. /*
  81. * Verify the key.
  82. */
  83. ret = verify_host_key(host, port, keytype, keystr);
  84. if (ret == 0) /* success - key matched OK */
  85. return;
  86. if (ret == 2) { /* key was different */
  87. if (console_batch_mode) {
  88. fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
  89. cleanup_exit(1);
  90. }
  91. fprintf(stderr, wrongmsg, keytype, fingerprint);
  92. fflush(stderr);
  93. }
  94. if (ret == 1) { /* key was absent */
  95. if (console_batch_mode) {
  96. fprintf(stderr, absentmsg_batch, keytype, fingerprint);
  97. cleanup_exit(1);
  98. }
  99. fprintf(stderr, absentmsg, keytype, fingerprint);
  100. fflush(stderr);
  101. }
  102. {
  103. struct termios oldmode, newmode;
  104. tcgetattr(0, &oldmode);
  105. newmode = oldmode;
  106. newmode.c_lflag |= ECHO | ISIG | ICANON;
  107. tcsetattr(0, TCSANOW, &newmode);
  108. line[0] = '\0';
  109. read(0, line, sizeof(line) - 1);
  110. tcsetattr(0, TCSANOW, &oldmode);
  111. }
  112. if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
  113. if (line[0] == 'y' || line[0] == 'Y')
  114. store_host_key(host, port, keytype, keystr);
  115. } else {
  116. fprintf(stderr, abandoned);
  117. cleanup_exit(0);
  118. }
  119. }
  120. /*
  121. * Ask whether the selected cipher is acceptable (since it was
  122. * below the configured 'warn' threshold).
  123. * cs: 0 = both ways, 1 = client->server, 2 = server->client
  124. */
  125. void askcipher(void *frontend, char *ciphername, int cs)
  126. {
  127. static const char msg[] =
  128. "The first %scipher supported by the server is\n"
  129. "%s, which is below the configured warning threshold.\n"
  130. "Continue with connection? (y/n) ";
  131. static const char msg_batch[] =
  132. "The first %scipher supported by the server is\n"
  133. "%s, which is below the configured warning threshold.\n"
  134. "Connection abandoned.\n";
  135. static const char abandoned[] = "Connection abandoned.\n";
  136. char line[32];
  137. if (console_batch_mode) {
  138. fprintf(stderr, msg_batch,
  139. (cs == 0) ? "" :
  140. (cs == 1) ? "client-to-server " : "server-to-client ",
  141. ciphername);
  142. cleanup_exit(1);
  143. }
  144. fprintf(stderr, msg,
  145. (cs == 0) ? "" :
  146. (cs == 1) ? "client-to-server " : "server-to-client ",
  147. ciphername);
  148. fflush(stderr);
  149. {
  150. struct termios oldmode, newmode;
  151. tcgetattr(0, &oldmode);
  152. newmode = oldmode;
  153. newmode.c_lflag |= ECHO | ISIG | ICANON;
  154. tcsetattr(0, TCSANOW, &newmode);
  155. line[0] = '\0';
  156. read(0, line, sizeof(line) - 1);
  157. tcsetattr(0, TCSANOW, &oldmode);
  158. }
  159. if (line[0] == 'y' || line[0] == 'Y') {
  160. return;
  161. } else {
  162. fprintf(stderr, abandoned);
  163. cleanup_exit(0);
  164. }
  165. }
  166. /*
  167. * Ask whether to wipe a session log file before writing to it.
  168. * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
  169. */
  170. int askappend(void *frontend, Filename filename)
  171. {
  172. static const char msgtemplate[] =
  173. "The session log file \"%.*s\" already exists.\n"
  174. "You can overwrite it with a new session log,\n"
  175. "append your session log to the end of it,\n"
  176. "or disable session logging for this session.\n"
  177. "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
  178. "or just press Return to disable logging.\n"
  179. "Wipe the log file? (y/n, Return cancels logging) ";
  180. static const char msgtemplate_batch[] =
  181. "The session log file \"%.*s\" already exists.\n"
  182. "Logging will not be enabled.\n";
  183. char line[32];
  184. if (console_batch_mode) {
  185. fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path);
  186. fflush(stderr);
  187. return 0;
  188. }
  189. fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path);
  190. fflush(stderr);
  191. {
  192. struct termios oldmode, newmode;
  193. tcgetattr(0, &oldmode);
  194. newmode = oldmode;
  195. newmode.c_lflag |= ECHO | ISIG | ICANON;
  196. tcsetattr(0, TCSANOW, &newmode);
  197. line[0] = '\0';
  198. read(0, line, sizeof(line) - 1);
  199. tcsetattr(0, TCSANOW, &oldmode);
  200. }
  201. if (line[0] == 'y' || line[0] == 'Y')
  202. return 2;
  203. else if (line[0] == 'n' || line[0] == 'N')
  204. return 1;
  205. else
  206. return 0;
  207. }
  208. /*
  209. * Warn about the obsolescent key file format.
  210. *
  211. * Uniquely among these functions, this one does _not_ expect a
  212. * frontend handle. This means that if PuTTY is ported to a
  213. * platform which requires frontend handles, this function will be
  214. * an anomaly. Fortunately, the problem it addresses will not have
  215. * been present on that platform, so it can plausibly be
  216. * implemented as an empty function.
  217. */
  218. void old_keyfile_warning(void)
  219. {
  220. static const char message[] =
  221. "You are loading an SSH 2 private key which has an\n"
  222. "old version of the file format. This means your key\n"
  223. "file is not fully tamperproof. Future versions of\n"
  224. "PuTTY may stop supporting this private key format,\n"
  225. "so we recommend you convert your key to the new\n"
  226. "format.\n"
  227. "\n"
  228. "Once the key is loaded into PuTTYgen, you can perform\n"
  229. "this conversion simply by saving it again.\n";
  230. fputs(message, stderr);
  231. }
  232. void console_provide_logctx(void *logctx)
  233. {
  234. console_logctx = logctx;
  235. }
  236. void logevent(void *frontend, const char *string)
  237. {
  238. if (console_logctx)
  239. log_eventlog(console_logctx, string);
  240. }
  241. int console_get_line(const char *prompt, char *str,
  242. int maxlen, int is_pw)
  243. {
  244. struct termios oldmode, newmode;
  245. int i;
  246. if (console_batch_mode) {
  247. if (maxlen > 0)
  248. str[0] = '\0';
  249. } else {
  250. tcgetattr(0, &oldmode);
  251. newmode = oldmode;
  252. newmode.c_lflag |= ISIG | ICANON;
  253. if (is_pw)
  254. newmode.c_lflag &= ~ECHO;
  255. else
  256. newmode.c_lflag |= ECHO;
  257. tcsetattr(0, TCSANOW, &newmode);
  258. fputs(prompt, stdout);
  259. fflush(stdout);
  260. i = read(0, str, maxlen - 1);
  261. tcsetattr(0, TCSANOW, &oldmode);
  262. if (i > 0 && str[i-1] == '\n')
  263. i--;
  264. str[i] = '\0';
  265. if (is_pw)
  266. fputs("\n", stdout);
  267. }
  268. return 1;
  269. }
  270. void frontend_keypress(void *handle)
  271. {
  272. /*
  273. * This is nothing but a stub, in console code.
  274. */
  275. return;
  276. }
  277. int is_interactive(void)
  278. {
  279. return isatty(0);
  280. }
  281. /*
  282. * X11-forwarding-related things suitable for console.
  283. */
  284. const char platform_x11_best_transport[] = "unix";
  285. char *platform_get_x_display(void) {
  286. return dupstr(getenv("DISPLAY"));
  287. }