misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #ifndef WINSCP
  184. char *buildinfo(const char *newline)
  185. {
  186. strbuf *buf = strbuf_new();
  187. strbuf_catf(buf, "Build platform: %d-bit %s",
  188. (int)(CHAR_BIT * sizeof(void *)),
  189. BUILDINFO_PLATFORM);
  190. #ifdef __clang_version__
  191. #define FOUND_COMPILER
  192. strbuf_catf(buf, "%sCompiler: clang %s", newline, __clang_version__);
  193. #elif defined __GNUC__ && defined __VERSION__
  194. #define FOUND_COMPILER
  195. strbuf_catf(buf, "%sCompiler: gcc %s", newline, __VERSION__);
  196. #endif
  197. #if defined _MSC_VER
  198. #ifndef FOUND_COMPILER
  199. #define FOUND_COMPILER
  200. strbuf_catf(buf, "%sCompiler: ", newline);
  201. #else
  202. strbuf_catf(buf, ", emulating ");
  203. #endif
  204. strbuf_catf(buf, "Visual Studio");
  205. #if 0
  206. /*
  207. * List of _MSC_VER values and their translations taken from
  208. * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
  209. *
  210. * The pointless #if 0 branch containing this comment is there so
  211. * that every real clause can start with #elif and there's no
  212. * anomalous first clause. That way the patch looks nicer when you
  213. * add extra ones.
  214. */
  215. #elif _MSC_VER == 1923
  216. strbuf_catf(buf, " 2019 (16.3)");
  217. #elif _MSC_VER == 1922
  218. strbuf_catf(buf, " 2019 (16.2)");
  219. #elif _MSC_VER == 1921
  220. strbuf_catf(buf, " 2019 (16.1)");
  221. #elif _MSC_VER == 1920
  222. strbuf_catf(buf, " 2019 (16.0)");
  223. #elif _MSC_VER == 1916
  224. strbuf_catf(buf, " 2017 version 15.9");
  225. #elif _MSC_VER == 1915
  226. strbuf_catf(buf, " 2017 version 15.8");
  227. #elif _MSC_VER == 1914
  228. strbuf_catf(buf, " 2017 version 15.7");
  229. #elif _MSC_VER == 1913
  230. strbuf_catf(buf, " 2017 version 15.6");
  231. #elif _MSC_VER == 1912
  232. strbuf_catf(buf, " 2017 version 15.5");
  233. #elif _MSC_VER == 1911
  234. strbuf_catf(buf, " 2017 version 15.3");
  235. #elif _MSC_VER == 1910
  236. strbuf_catf(buf, " 2017 RTW (15.0)");
  237. #elif _MSC_VER == 1900
  238. strbuf_catf(buf, " 2015 (14.0)");
  239. #elif _MSC_VER == 1800
  240. strbuf_catf(buf, " 2013 (12.0)");
  241. #elif _MSC_VER == 1700
  242. strbuf_catf(buf, " 2012 (11.0)");
  243. #elif _MSC_VER == 1600
  244. strbuf_catf(buf, " 2010 (10.0)");
  245. #elif _MSC_VER == 1500
  246. strbuf_catf(buf, " 2008 (9.0)");
  247. #elif _MSC_VER == 1400
  248. strbuf_catf(buf, " 2005 (8.0)");
  249. #elif _MSC_VER == 1310
  250. strbuf_catf(buf, " .NET 2003 (7.1)");
  251. #elif _MSC_VER == 1300
  252. strbuf_catf(buf, " .NET 2002 (7.0)");
  253. #elif _MSC_VER == 1200
  254. strbuf_catf(buf, " 6.0");
  255. #else
  256. strbuf_catf(buf, ", unrecognised version");
  257. #endif
  258. strbuf_catf(buf, ", _MSC_VER=%d", (int)_MSC_VER);
  259. #endif
  260. #ifdef BUILDINFO_GTK
  261. {
  262. char *gtk_buildinfo = buildinfo_gtk_version();
  263. if (gtk_buildinfo) {
  264. strbuf_catf(buf, "%sCompiled against GTK version %s",
  265. newline, gtk_buildinfo);
  266. sfree(gtk_buildinfo);
  267. }
  268. }
  269. #endif
  270. #if defined _WINDOWS
  271. {
  272. int echm = has_embedded_chm();
  273. if (echm >= 0)
  274. strbuf_catf(buf, "%sEmbedded HTML Help file: %s", newline,
  275. echm ? "yes" : "no");
  276. }
  277. #endif
  278. #if defined _WINDOWS && defined MINEFIELD
  279. strbuf_catf(buf, "%sBuild option: MINEFIELD", newline);
  280. #endif
  281. #ifdef NO_SECURITY
  282. strbuf_catf(buf, "%sBuild option: NO_SECURITY", newline);
  283. #endif
  284. #ifdef NO_SECUREZEROMEMORY
  285. strbuf_catf(buf, "%sBuild option: NO_SECUREZEROMEMORY", newline);
  286. #endif
  287. #ifdef NO_IPV6
  288. strbuf_catf(buf, "%sBuild option: NO_IPV6", newline);
  289. #endif
  290. #ifdef NO_GSSAPI
  291. strbuf_catf(buf, "%sBuild option: NO_GSSAPI", newline);
  292. #endif
  293. #ifdef STATIC_GSSAPI
  294. strbuf_catf(buf, "%sBuild option: STATIC_GSSAPI", newline);
  295. #endif
  296. #ifdef UNPROTECT
  297. strbuf_catf(buf, "%sBuild option: UNPROTECT", newline);
  298. #endif
  299. #ifdef FUZZING
  300. strbuf_catf(buf, "%sBuild option: FUZZING", newline);
  301. #endif
  302. #ifdef DEBUG
  303. strbuf_catf(buf, "%sBuild option: DEBUG", newline);
  304. #endif
  305. strbuf_catf(buf, "%sSource commit: %s", newline, commitid);
  306. return strbuf_to_str(buf);
  307. }
  308. #endif !WINSCP
  309. #ifdef MPEXT
  310. #include "version.h"
  311. const char * get_putty_version()
  312. {
  313. return TEXTVER;
  314. }
  315. #endif
  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. }