misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 = strbuf_new_nm();
  49. sgrowarray(p->prompts, p->prompts_size, p->n_prompts);
  50. p->prompts[p->n_prompts++] = pr;
  51. }
  52. void prompt_set_result(prompt_t *pr, const char *newstr)
  53. {
  54. strbuf_clear(pr->result);
  55. put_datapl(pr->result, ptrlen_from_asciz(newstr));
  56. }
  57. const char *prompt_get_result_ref(prompt_t *pr)
  58. {
  59. return pr->result->s;
  60. }
  61. char *prompt_get_result(prompt_t *pr)
  62. {
  63. return dupstr(pr->result->s);
  64. }
  65. void free_prompts(prompts_t *p)
  66. {
  67. size_t i;
  68. for (i=0; i < p->n_prompts; i++) {
  69. prompt_t *pr = p->prompts[i];
  70. strbuf_free(pr->result);
  71. sfree(pr->prompt);
  72. sfree(pr);
  73. }
  74. sfree(p->prompts);
  75. sfree(p->name);
  76. sfree(p->instruction);
  77. sfree(p);
  78. }
  79. /*
  80. * Determine whether or not a Conf represents a session which can
  81. * sensibly be launched right now.
  82. */
  83. bool conf_launchable(Conf *conf)
  84. {
  85. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  86. return conf_get_str(conf, CONF_serline)[0] != 0;
  87. else
  88. return conf_get_str(conf, CONF_host)[0] != 0;
  89. }
  90. char const *conf_dest(Conf *conf)
  91. {
  92. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  93. return conf_get_str(conf, CONF_serline);
  94. else
  95. return conf_get_str(conf, CONF_host);
  96. }
  97. /*
  98. * Validate a manual host key specification (either entered in the
  99. * GUI, or via -hostkey). If valid, we return true, and update 'key'
  100. * to contain a canonicalised version of the key string in 'key'
  101. * (which is guaranteed to take up at most as much space as the
  102. * original version), suitable for putting into the Conf. If not
  103. * valid, we return false.
  104. */
  105. bool validate_manual_hostkey(char *key)
  106. {
  107. char *p, *q, *r, *s;
  108. /*
  109. * Step through the string word by word, looking for a word that's
  110. * in one of the formats we like.
  111. */
  112. p = key;
  113. while ((p += strspn(p, " \t"))[0]) {
  114. q = p;
  115. p += strcspn(p, " \t");
  116. if (*p) *p++ = '\0';
  117. /*
  118. * Now q is our word.
  119. */
  120. if (strlen(q) == 16*3 - 1 &&
  121. q[strspn(q, "0123456789abcdefABCDEF:")] == 0) {
  122. /*
  123. * Might be a key fingerprint. Check the colons are in the
  124. * right places, and if so, return the same fingerprint
  125. * canonicalised into lowercase.
  126. */
  127. int i;
  128. for (i = 0; i < 16; i++)
  129. if (q[3*i] == ':' || q[3*i+1] == ':')
  130. goto not_fingerprint; /* sorry */
  131. for (i = 0; i < 15; i++)
  132. if (q[3*i+2] != ':')
  133. goto not_fingerprint; /* sorry */
  134. for (i = 0; i < 16*3 - 1; i++)
  135. key[i] = tolower(q[i]);
  136. key[16*3 - 1] = '\0';
  137. return true;
  138. }
  139. not_fingerprint:;
  140. /*
  141. * Before we check for a public-key blob, trim newlines out of
  142. * the middle of the word, in case someone's managed to paste
  143. * in a public-key blob _with_ them.
  144. */
  145. for (r = s = q; *r; r++)
  146. if (*r != '\n' && *r != '\r')
  147. *s++ = *r;
  148. *s = '\0';
  149. if (strlen(q) % 4 == 0 && strlen(q) > 2*4 &&
  150. q[strspn(q, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  151. "abcdefghijklmnopqrstuvwxyz+/=")] == 0) {
  152. /*
  153. * Might be a base64-encoded SSH-2 public key blob. Check
  154. * that it starts with a sensible algorithm string. No
  155. * canonicalisation is necessary for this string type.
  156. *
  157. * The algorithm string must be at most 64 characters long
  158. * (RFC 4251 section 6).
  159. */
  160. unsigned char decoded[6];
  161. unsigned alglen;
  162. int minlen;
  163. int len = 0;
  164. len += base64_decode_atom(q, decoded+len);
  165. if (len < 3)
  166. goto not_ssh2_blob; /* sorry */
  167. len += base64_decode_atom(q+4, decoded+len);
  168. if (len < 4)
  169. goto not_ssh2_blob; /* sorry */
  170. alglen = GET_32BIT_MSB_FIRST(decoded);
  171. if (alglen > 64)
  172. goto not_ssh2_blob; /* sorry */
  173. minlen = ((alglen + 4) + 2) / 3;
  174. if (strlen(q) < minlen)
  175. goto not_ssh2_blob; /* sorry */
  176. strcpy(key, q);
  177. return true;
  178. }
  179. not_ssh2_blob:;
  180. }
  181. return false;
  182. }
  183. char *buildinfo(const char *newline)
  184. {
  185. strbuf *buf = strbuf_new();
  186. strbuf_catf(buf, "Build platform: %d-bit %s",
  187. (int)(CHAR_BIT * sizeof(void *)),
  188. BUILDINFO_PLATFORM);
  189. #ifdef __clang_version__
  190. #define FOUND_COMPILER
  191. strbuf_catf(buf, "%sCompiler: clang %s", newline, __clang_version__);
  192. #elif defined __GNUC__ && defined __VERSION__
  193. #define FOUND_COMPILER
  194. strbuf_catf(buf, "%sCompiler: gcc %s", newline, __VERSION__);
  195. #endif
  196. #if defined _MSC_VER
  197. #ifndef FOUND_COMPILER
  198. #define FOUND_COMPILER
  199. strbuf_catf(buf, "%sCompiler: ", newline);
  200. #else
  201. strbuf_catf(buf, ", emulating ");
  202. #endif
  203. strbuf_catf(buf, "Visual Studio");
  204. #if 0
  205. /*
  206. * List of _MSC_VER values and their translations taken from
  207. * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
  208. *
  209. * The pointless #if 0 branch containing this comment is there so
  210. * that every real clause can start with #elif and there's no
  211. * anomalous first clause. That way the patch looks nicer when you
  212. * add extra ones.
  213. */
  214. #elif _MSC_VER == 1923
  215. strbuf_catf(buf, " 2019 (16.3)");
  216. #elif _MSC_VER == 1922
  217. strbuf_catf(buf, " 2019 (16.2)");
  218. #elif _MSC_VER == 1921
  219. strbuf_catf(buf, " 2019 (16.1)");
  220. #elif _MSC_VER == 1920
  221. strbuf_catf(buf, " 2019 (16.0)");
  222. #elif _MSC_VER == 1916
  223. strbuf_catf(buf, " 2017 version 15.9");
  224. #elif _MSC_VER == 1915
  225. strbuf_catf(buf, " 2017 version 15.8");
  226. #elif _MSC_VER == 1914
  227. strbuf_catf(buf, " 2017 version 15.7");
  228. #elif _MSC_VER == 1913
  229. strbuf_catf(buf, " 2017 version 15.6");
  230. #elif _MSC_VER == 1912
  231. strbuf_catf(buf, " 2017 version 15.5");
  232. #elif _MSC_VER == 1911
  233. strbuf_catf(buf, " 2017 version 15.3");
  234. #elif _MSC_VER == 1910
  235. strbuf_catf(buf, " 2017 RTW (15.0)");
  236. #elif _MSC_VER == 1900
  237. strbuf_catf(buf, " 2015 (14.0)");
  238. #elif _MSC_VER == 1800
  239. strbuf_catf(buf, " 2013 (12.0)");
  240. #elif _MSC_VER == 1700
  241. strbuf_catf(buf, " 2012 (11.0)");
  242. #elif _MSC_VER == 1600
  243. strbuf_catf(buf, " 2010 (10.0)");
  244. #elif _MSC_VER == 1500
  245. strbuf_catf(buf, " 2008 (9.0)");
  246. #elif _MSC_VER == 1400
  247. strbuf_catf(buf, " 2005 (8.0)");
  248. #elif _MSC_VER == 1310
  249. strbuf_catf(buf, " .NET 2003 (7.1)");
  250. #elif _MSC_VER == 1300
  251. strbuf_catf(buf, " .NET 2002 (7.0)");
  252. #elif _MSC_VER == 1200
  253. strbuf_catf(buf, " 6.0");
  254. #else
  255. strbuf_catf(buf, ", unrecognised version");
  256. #endif
  257. strbuf_catf(buf, ", _MSC_VER=%d", (int)_MSC_VER);
  258. #endif
  259. #ifdef BUILDINFO_GTK
  260. {
  261. char *gtk_buildinfo = buildinfo_gtk_version();
  262. if (gtk_buildinfo) {
  263. strbuf_catf(buf, "%sCompiled against GTK version %s",
  264. newline, gtk_buildinfo);
  265. sfree(gtk_buildinfo);
  266. }
  267. }
  268. #endif
  269. #if defined _WINDOWS
  270. {
  271. int echm = has_embedded_chm();
  272. if (echm >= 0)
  273. strbuf_catf(buf, "%sEmbedded HTML Help file: %s", newline,
  274. echm ? "yes" : "no");
  275. }
  276. #endif
  277. #if defined _WINDOWS && defined MINEFIELD
  278. strbuf_catf(buf, "%sBuild option: MINEFIELD", newline);
  279. #endif
  280. #ifdef NO_SECURITY
  281. strbuf_catf(buf, "%sBuild option: NO_SECURITY", newline);
  282. #endif
  283. #ifdef NO_SECUREZEROMEMORY
  284. strbuf_catf(buf, "%sBuild option: NO_SECUREZEROMEMORY", newline);
  285. #endif
  286. #ifdef NO_IPV6
  287. strbuf_catf(buf, "%sBuild option: NO_IPV6", newline);
  288. #endif
  289. #ifdef NO_GSSAPI
  290. strbuf_catf(buf, "%sBuild option: NO_GSSAPI", newline);
  291. #endif
  292. #ifdef STATIC_GSSAPI
  293. strbuf_catf(buf, "%sBuild option: STATIC_GSSAPI", newline);
  294. #endif
  295. #ifdef UNPROTECT
  296. strbuf_catf(buf, "%sBuild option: UNPROTECT", newline);
  297. #endif
  298. #ifdef FUZZING
  299. strbuf_catf(buf, "%sBuild option: FUZZING", newline);
  300. #endif
  301. #ifdef DEBUG
  302. strbuf_catf(buf, "%sBuild option: DEBUG", newline);
  303. #endif
  304. strbuf_catf(buf, "%sSource commit: %s", newline, commitid);
  305. return strbuf_to_str(buf);
  306. }
  307. size_t nullseat_output(
  308. Seat *seat, bool is_stderr, const void *data, size_t len) { return 0; }
  309. bool nullseat_eof(Seat *seat) { return true; }
  310. int nullseat_get_userpass_input(
  311. Seat *seat, prompts_t *p, bufchain *input) { return 0; }
  312. void nullseat_notify_remote_exit(Seat *seat) {}
  313. void nullseat_connection_fatal(Seat *seat, const char *message) {}
  314. void nullseat_update_specials_menu(Seat *seat) {}
  315. char *nullseat_get_ttymode(Seat *seat, const char *mode) { return NULL; }
  316. void nullseat_set_busy_status(Seat *seat, BusyStatus status) {}
  317. int nullseat_verify_ssh_host_key(
  318. Seat *seat, const char *host, int port,
  319. const char *keytype, char *keystr, char *key_fingerprint,
  320. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  321. int nullseat_confirm_weak_crypto_primitive(
  322. Seat *seat, const char *algtype, const char *algname,
  323. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  324. int nullseat_confirm_weak_cached_hostkey(
  325. Seat *seat, const char *algname, const char *betteralgs,
  326. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  327. bool nullseat_is_never_utf8(Seat *seat) { return false; }
  328. bool nullseat_is_always_utf8(Seat *seat) { return true; }
  329. void nullseat_echoedit_update(Seat *seat, bool echoing, bool editing) {}
  330. const char *nullseat_get_x_display(Seat *seat) { return NULL; }
  331. bool nullseat_get_windowid(Seat *seat, long *id_out) { return false; }
  332. bool nullseat_get_window_pixel_size(
  333. Seat *seat, int *width, int *height) { return false; }
  334. StripCtrlChars *nullseat_stripctrl_new(
  335. Seat *seat, BinarySink *bs_out, SeatInteractionContext sic) {return NULL;}
  336. bool nullseat_set_trust_status(Seat *seat, bool tr) { return false; }
  337. bool nullseat_set_trust_status_vacuously(Seat *seat, bool tr) { return true; }
  338. void sk_free_peer_info(SocketPeerInfo *pi)
  339. {
  340. if (pi) {
  341. sfree((char *)pi->addr_text);
  342. sfree((char *)pi->log_text);
  343. sfree(pi);
  344. }
  345. }
  346. void out_of_memory(void)
  347. {
  348. modalfatalbox("Out of memory");
  349. }