misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Platform-independent routines shared between all PuTTY programs.
  3. *
  4. * This file contains functions that use the kind of infrastructure
  5. * like conf.c that tends to only live in the main applications, or
  6. * that do things that only something like a main PuTTY application
  7. * would need. So standalone test programs should generally be able to
  8. * avoid linking against it.
  9. *
  10. * More standalone functions that depend on nothing but the C library
  11. * live in utils.c.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <limits.h>
  17. #include <ctype.h>
  18. #include <assert.h>
  19. #include "defs.h"
  20. #include "putty.h"
  21. #include "misc.h"
  22. void seat_connection_fatal(Seat *seat, const char *fmt, ...)
  23. {
  24. va_list ap;
  25. char *msg;
  26. va_start(ap, fmt);
  27. msg = dupvprintf(fmt, ap);
  28. va_end(ap);
  29. seat->vt->connection_fatal(seat, msg);
  30. sfree(msg); /* if we return */
  31. }
  32. prompts_t *new_prompts(void)
  33. {
  34. prompts_t *p = snew(prompts_t);
  35. p->prompts = NULL;
  36. p->n_prompts = p->prompts_size = 0;
  37. p->data = NULL;
  38. p->to_server = true; /* to be on the safe side */
  39. p->name = p->instruction = NULL;
  40. p->name_reqd = p->instr_reqd = false;
  41. return p;
  42. }
  43. void add_prompt(prompts_t *p, char *promptstr, bool echo)
  44. {
  45. prompt_t *pr = snew(prompt_t);
  46. pr->prompt = promptstr;
  47. pr->echo = echo;
  48. pr->result = NULL;
  49. pr->resultsize = 0;
  50. sgrowarray(p->prompts, p->prompts_size, p->n_prompts);
  51. p->prompts[p->n_prompts++] = pr;
  52. }
  53. void prompt_ensure_result_size(prompt_t *pr, int newlen)
  54. {
  55. if ((int)pr->resultsize < newlen) {
  56. char *newbuf;
  57. newlen = newlen * 5 / 4 + 512; /* avoid too many small allocs */
  58. /*
  59. * We don't use sresize / realloc here, because we will be
  60. * storing sensitive stuff like passwords in here, and we want
  61. * to make sure that the data doesn't get copied around in
  62. * memory without the old copy being destroyed.
  63. */
  64. newbuf = snewn(newlen, char);
  65. memcpy(newbuf, pr->result, pr->resultsize);
  66. smemclr(pr->result, pr->resultsize);
  67. sfree(pr->result);
  68. pr->result = newbuf;
  69. pr->resultsize = newlen;
  70. }
  71. }
  72. void prompt_set_result(prompt_t *pr, const char *newstr)
  73. {
  74. prompt_ensure_result_size(pr, strlen(newstr) + 1);
  75. strcpy(pr->result, newstr);
  76. }
  77. void free_prompts(prompts_t *p)
  78. {
  79. size_t i;
  80. for (i=0; i < p->n_prompts; i++) {
  81. prompt_t *pr = p->prompts[i];
  82. smemclr(pr->result, pr->resultsize); /* burn the evidence */
  83. sfree(pr->result);
  84. sfree(pr->prompt);
  85. sfree(pr);
  86. }
  87. sfree(p->prompts);
  88. sfree(p->name);
  89. sfree(p->instruction);
  90. sfree(p);
  91. }
  92. /*
  93. * Determine whether or not a Conf represents a session which can
  94. * sensibly be launched right now.
  95. */
  96. bool conf_launchable(Conf *conf)
  97. {
  98. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  99. return conf_get_str(conf, CONF_serline)[0] != 0;
  100. else
  101. return conf_get_str(conf, CONF_host)[0] != 0;
  102. }
  103. char const *conf_dest(Conf *conf)
  104. {
  105. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  106. return conf_get_str(conf, CONF_serline);
  107. else
  108. return conf_get_str(conf, CONF_host);
  109. }
  110. /*
  111. * Validate a manual host key specification (either entered in the
  112. * GUI, or via -hostkey). If valid, we return true, and update 'key'
  113. * to contain a canonicalised version of the key string in 'key'
  114. * (which is guaranteed to take up at most as much space as the
  115. * original version), suitable for putting into the Conf. If not
  116. * valid, we return false.
  117. */
  118. bool validate_manual_hostkey(char *key)
  119. {
  120. char *p, *q, *r, *s;
  121. /*
  122. * Step through the string word by word, looking for a word that's
  123. * in one of the formats we like.
  124. */
  125. p = key;
  126. while ((p += strspn(p, " \t"))[0]) {
  127. q = p;
  128. p += strcspn(p, " \t");
  129. if (*p) *p++ = '\0';
  130. /*
  131. * Now q is our word.
  132. */
  133. if (strlen(q) == 16*3 - 1 &&
  134. q[strspn(q, "0123456789abcdefABCDEF:")] == 0) {
  135. /*
  136. * Might be a key fingerprint. Check the colons are in the
  137. * right places, and if so, return the same fingerprint
  138. * canonicalised into lowercase.
  139. */
  140. int i;
  141. for (i = 0; i < 16; i++)
  142. if (q[3*i] == ':' || q[3*i+1] == ':')
  143. goto not_fingerprint; /* sorry */
  144. for (i = 0; i < 15; i++)
  145. if (q[3*i+2] != ':')
  146. goto not_fingerprint; /* sorry */
  147. for (i = 0; i < 16*3 - 1; i++)
  148. key[i] = tolower(q[i]);
  149. key[16*3 - 1] = '\0';
  150. return true;
  151. }
  152. not_fingerprint:;
  153. /*
  154. * Before we check for a public-key blob, trim newlines out of
  155. * the middle of the word, in case someone's managed to paste
  156. * in a public-key blob _with_ them.
  157. */
  158. for (r = s = q; *r; r++)
  159. if (*r != '\n' && *r != '\r')
  160. *s++ = *r;
  161. *s = '\0';
  162. if (strlen(q) % 4 == 0 && strlen(q) > 2*4 &&
  163. q[strspn(q, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  164. "abcdefghijklmnopqrstuvwxyz+/=")] == 0) {
  165. /*
  166. * Might be a base64-encoded SSH-2 public key blob. Check
  167. * that it starts with a sensible algorithm string. No
  168. * canonicalisation is necessary for this string type.
  169. *
  170. * The algorithm string must be at most 64 characters long
  171. * (RFC 4251 section 6).
  172. */
  173. unsigned char decoded[6];
  174. unsigned alglen;
  175. int minlen;
  176. int len = 0;
  177. len += base64_decode_atom(q, decoded+len);
  178. if (len < 3)
  179. goto not_ssh2_blob; /* sorry */
  180. len += base64_decode_atom(q+4, decoded+len);
  181. if (len < 4)
  182. goto not_ssh2_blob; /* sorry */
  183. alglen = GET_32BIT_MSB_FIRST(decoded);
  184. if (alglen > 64)
  185. goto not_ssh2_blob; /* sorry */
  186. minlen = ((alglen + 4) + 2) / 3;
  187. if (strlen(q) < minlen)
  188. goto not_ssh2_blob; /* sorry */
  189. strcpy(key, q);
  190. return true;
  191. }
  192. not_ssh2_blob:;
  193. }
  194. return false;
  195. }
  196. char *buildinfo(const char *newline)
  197. {
  198. strbuf *buf = strbuf_new();
  199. strbuf_catf(buf, "Build platform: %d-bit %s",
  200. (int)(CHAR_BIT * sizeof(void *)),
  201. BUILDINFO_PLATFORM);
  202. #ifdef __clang_version__
  203. #define FOUND_COMPILER
  204. strbuf_catf(buf, "%sCompiler: clang %s", newline, __clang_version__);
  205. #elif defined __GNUC__ && defined __VERSION__
  206. #define FOUND_COMPILER
  207. strbuf_catf(buf, "%sCompiler: gcc %s", newline, __VERSION__);
  208. #endif
  209. #if defined _MSC_VER
  210. #ifndef FOUND_COMPILER
  211. #define FOUND_COMPILER
  212. strbuf_catf(buf, "%sCompiler: ", newline);
  213. #else
  214. strbuf_catf(buf, ", emulating ");
  215. #endif
  216. strbuf_catf(buf, "Visual Studio", newline);
  217. #if 0
  218. /*
  219. * List of _MSC_VER values and their translations taken from
  220. * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
  221. * except for 1920, which is not yet listed on that page as of
  222. * 2019-03-22, and was determined experimentally by Sean Kain.
  223. *
  224. * The pointless #if 0 branch containing this comment is there so
  225. * that every real clause can start with #elif and there's no
  226. * anomalous first clause. That way the patch looks nicer when you
  227. * add extra ones.
  228. */
  229. #elif _MSC_VER == 1920
  230. strbuf_catf(buf, " 2019 (16.x)");
  231. #elif _MSC_VER == 1916
  232. strbuf_catf(buf, " 2017 version 15.9");
  233. #elif _MSC_VER == 1915
  234. strbuf_catf(buf, " 2017 version 15.8");
  235. #elif _MSC_VER == 1914
  236. strbuf_catf(buf, " 2017 version 15.7");
  237. #elif _MSC_VER == 1913
  238. strbuf_catf(buf, " 2017 version 15.6");
  239. #elif _MSC_VER == 1912
  240. strbuf_catf(buf, " 2017 version 15.5");
  241. #elif _MSC_VER == 1911
  242. strbuf_catf(buf, " 2017 version 15.3");
  243. #elif _MSC_VER == 1910
  244. strbuf_catf(buf, " 2017 RTW (15.0)");
  245. #elif _MSC_VER == 1900
  246. strbuf_catf(buf, " 2015 (14.0)");
  247. #elif _MSC_VER == 1800
  248. strbuf_catf(buf, " 2013 (12.0)");
  249. #elif _MSC_VER == 1700
  250. strbuf_catf(buf, " 2012 (11.0)");
  251. #elif _MSC_VER == 1600
  252. strbuf_catf(buf, " 2010 (10.0)");
  253. #elif _MSC_VER == 1500
  254. strbuf_catf(buf, " 2008 (9.0)");
  255. #elif _MSC_VER == 1400
  256. strbuf_catf(buf, " 2005 (8.0)");
  257. #elif _MSC_VER == 1310
  258. strbuf_catf(buf, " .NET 2003 (7.1)");
  259. #elif _MSC_VER == 1300
  260. strbuf_catf(buf, " .NET 2002 (7.0)");
  261. #elif _MSC_VER == 1200
  262. strbuf_catf(buf, " 6.0");
  263. #else
  264. strbuf_catf(buf, ", unrecognised version");
  265. #endif
  266. strbuf_catf(buf, ", _MSC_VER=%d", (int)_MSC_VER);
  267. #endif
  268. #ifdef BUILDINFO_GTK
  269. {
  270. char *gtk_buildinfo = buildinfo_gtk_version();
  271. if (gtk_buildinfo) {
  272. strbuf_catf(buf, "%sCompiled against GTK version %s",
  273. newline, gtk_buildinfo);
  274. sfree(gtk_buildinfo);
  275. }
  276. }
  277. #endif
  278. #if defined _WINDOWS
  279. {
  280. int echm = has_embedded_chm();
  281. if (echm >= 0)
  282. strbuf_catf(buf, "%sEmbedded HTML Help file: %s", newline,
  283. echm ? "yes" : "no");
  284. }
  285. #endif
  286. #if defined _WINDOWS && defined MINEFIELD
  287. strbuf_catf(buf, "%sBuild option: MINEFIELD", newline);
  288. #endif
  289. #ifdef NO_SECURITY
  290. strbuf_catf(buf, "%sBuild option: NO_SECURITY", newline);
  291. #endif
  292. #ifdef NO_SECUREZEROMEMORY
  293. strbuf_catf(buf, "%sBuild option: NO_SECUREZEROMEMORY", newline);
  294. #endif
  295. #ifdef NO_IPV6
  296. strbuf_catf(buf, "%sBuild option: NO_IPV6", newline);
  297. #endif
  298. #ifdef NO_GSSAPI
  299. strbuf_catf(buf, "%sBuild option: NO_GSSAPI", newline);
  300. #endif
  301. #ifdef STATIC_GSSAPI
  302. strbuf_catf(buf, "%sBuild option: STATIC_GSSAPI", newline);
  303. #endif
  304. #ifdef UNPROTECT
  305. strbuf_catf(buf, "%sBuild option: UNPROTECT", newline);
  306. #endif
  307. #ifdef FUZZING
  308. strbuf_catf(buf, "%sBuild option: FUZZING", newline);
  309. #endif
  310. #ifdef DEBUG
  311. strbuf_catf(buf, "%sBuild option: DEBUG", newline);
  312. #endif
  313. strbuf_catf(buf, "%sSource commit: %s", newline, commitid);
  314. return strbuf_to_str(buf);
  315. }
  316. size_t nullseat_output(
  317. Seat *seat, bool is_stderr, const void *data, size_t len) { return 0; }
  318. bool nullseat_eof(Seat *seat) { return true; }
  319. int nullseat_get_userpass_input(
  320. Seat *seat, prompts_t *p, bufchain *input) { return 0; }
  321. void nullseat_notify_remote_exit(Seat *seat) {}
  322. void nullseat_connection_fatal(Seat *seat, const char *message) {}
  323. void nullseat_update_specials_menu(Seat *seat) {}
  324. char *nullseat_get_ttymode(Seat *seat, const char *mode) { return NULL; }
  325. void nullseat_set_busy_status(Seat *seat, BusyStatus status) {}
  326. int nullseat_verify_ssh_host_key(
  327. Seat *seat, const char *host, int port,
  328. const char *keytype, char *keystr, char *key_fingerprint,
  329. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  330. int nullseat_confirm_weak_crypto_primitive(
  331. Seat *seat, const char *algtype, const char *algname,
  332. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  333. int nullseat_confirm_weak_cached_hostkey(
  334. Seat *seat, const char *algname, const char *betteralgs,
  335. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  336. bool nullseat_is_never_utf8(Seat *seat) { return false; }
  337. bool nullseat_is_always_utf8(Seat *seat) { return true; }
  338. void nullseat_echoedit_update(Seat *seat, bool echoing, bool editing) {}
  339. const char *nullseat_get_x_display(Seat *seat) { return NULL; }
  340. bool nullseat_get_windowid(Seat *seat, long *id_out) { return false; }
  341. bool nullseat_get_window_pixel_size(
  342. Seat *seat, int *width, int *height) { return false; }
  343. StripCtrlChars *nullseat_stripctrl_new(
  344. Seat *seat, BinarySink *bs_out, SeatInteractionContext sic) {return NULL;}
  345. bool nullseat_set_trust_status(Seat *seat, bool tr) { return false; }
  346. bool nullseat_set_trust_status_vacuously(Seat *seat, bool tr) { return true; }
  347. void sk_free_peer_info(SocketPeerInfo *pi)
  348. {
  349. if (pi) {
  350. sfree((char *)pi->addr_text);
  351. sfree((char *)pi->log_text);
  352. sfree(pi);
  353. }
  354. }
  355. void out_of_memory(void)
  356. {
  357. modalfatalbox("Out of memory");
  358. }