misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #ifndef WINSCP
  197. char *buildinfo(const char *newline)
  198. {
  199. strbuf *buf = strbuf_new();
  200. strbuf_catf(buf, "Build platform: %d-bit %s",
  201. (int)(CHAR_BIT * sizeof(void *)),
  202. BUILDINFO_PLATFORM);
  203. #ifdef __clang_version__
  204. #define FOUND_COMPILER
  205. strbuf_catf(buf, "%sCompiler: clang %s", newline, __clang_version__);
  206. #elif defined __GNUC__ && defined __VERSION__
  207. #define FOUND_COMPILER
  208. strbuf_catf(buf, "%sCompiler: gcc %s", newline, __VERSION__);
  209. #endif
  210. #if defined _MSC_VER
  211. #ifndef FOUND_COMPILER
  212. #define FOUND_COMPILER
  213. strbuf_catf(buf, "%sCompiler: ", newline);
  214. #else
  215. strbuf_catf(buf, ", emulating ");
  216. #endif
  217. strbuf_catf(buf, "Visual Studio", newline);
  218. #if _MSC_VER == 1900
  219. strbuf_catf(buf, " 2015 / MSVC++ 14.0");
  220. #elif _MSC_VER == 1912
  221. strbuf_catf(buf, " 2017 / MSVC++ 14.12");
  222. #elif _MSC_VER == 1800
  223. strbuf_catf(buf, " 2013 / MSVC++ 12.0");
  224. #elif _MSC_VER == 1700
  225. strbuf_catf(buf, " 2012 / MSVC++ 11.0");
  226. #elif _MSC_VER == 1600
  227. strbuf_catf(buf, " 2010 / MSVC++ 10.0");
  228. #elif _MSC_VER == 1500
  229. strbuf_catf(buf, " 2008 / MSVC++ 9.0");
  230. #elif _MSC_VER == 1400
  231. strbuf_catf(buf, " 2005 / MSVC++ 8.0");
  232. #elif _MSC_VER == 1310
  233. strbuf_catf(buf, " 2003 / MSVC++ 7.1");
  234. #elif _MSC_VER == 1300
  235. strbuf_catf(buf, " 2003 / MSVC++ 7.0");
  236. #else
  237. strbuf_catf(buf, ", unrecognised version");
  238. #endif
  239. strbuf_catf(buf, " (_MSC_VER=%d)", (int)_MSC_VER);
  240. #endif
  241. #ifdef BUILDINFO_GTK
  242. {
  243. char *gtk_buildinfo = buildinfo_gtk_version();
  244. if (gtk_buildinfo) {
  245. strbuf_catf(buf, "%sCompiled against GTK version %s",
  246. newline, gtk_buildinfo);
  247. sfree(gtk_buildinfo);
  248. }
  249. }
  250. #endif
  251. #if defined _WINDOWS
  252. {
  253. int echm = has_embedded_chm();
  254. if (echm >= 0)
  255. strbuf_catf(buf, "%sEmbedded HTML Help file: %s", newline,
  256. echm ? "yes" : "no");
  257. }
  258. #endif
  259. #if defined _WINDOWS && defined MINEFIELD
  260. strbuf_catf(buf, "%sBuild option: MINEFIELD", newline);
  261. #endif
  262. #ifdef NO_SECURITY
  263. strbuf_catf(buf, "%sBuild option: NO_SECURITY", newline);
  264. #endif
  265. #ifdef NO_SECUREZEROMEMORY
  266. strbuf_catf(buf, "%sBuild option: NO_SECUREZEROMEMORY", newline);
  267. #endif
  268. #ifdef NO_IPV6
  269. strbuf_catf(buf, "%sBuild option: NO_IPV6", newline);
  270. #endif
  271. #ifdef NO_GSSAPI
  272. strbuf_catf(buf, "%sBuild option: NO_GSSAPI", newline);
  273. #endif
  274. #ifdef STATIC_GSSAPI
  275. strbuf_catf(buf, "%sBuild option: STATIC_GSSAPI", newline);
  276. #endif
  277. #ifdef UNPROTECT
  278. strbuf_catf(buf, "%sBuild option: UNPROTECT", newline);
  279. #endif
  280. #ifdef FUZZING
  281. strbuf_catf(buf, "%sBuild option: FUZZING", newline);
  282. #endif
  283. #ifdef DEBUG
  284. strbuf_catf(buf, "%sBuild option: DEBUG", newline);
  285. #endif
  286. strbuf_catf(buf, "%sSource commit: %s", newline, commitid);
  287. return strbuf_to_str(buf);
  288. }
  289. #endif !WINSCP
  290. #ifdef MPEXT
  291. #include "version.h"
  292. const char * get_putty_version()
  293. {
  294. return TEXTVER;
  295. }
  296. #endif
  297. size_t nullseat_output(
  298. Seat *seat, bool is_stderr, const void *data, size_t len) { return 0; }
  299. bool nullseat_eof(Seat *seat) { return true; }
  300. int nullseat_get_userpass_input(
  301. Seat *seat, prompts_t *p, bufchain *input) { return 0; }
  302. void nullseat_notify_remote_exit(Seat *seat) {}
  303. void nullseat_connection_fatal(Seat *seat, const char *message) {}
  304. void nullseat_update_specials_menu(Seat *seat) {}
  305. char *nullseat_get_ttymode(Seat *seat, const char *mode) { return NULL; }
  306. void nullseat_set_busy_status(Seat *seat, BusyStatus status) {}
  307. int nullseat_verify_ssh_host_key(
  308. Seat *seat, const char *host, int port,
  309. const char *keytype, char *keystr, char *key_fingerprint,
  310. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  311. int nullseat_confirm_weak_crypto_primitive(
  312. Seat *seat, const char *algtype, const char *algname,
  313. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  314. int nullseat_confirm_weak_cached_hostkey(
  315. Seat *seat, const char *algname, const char *betteralgs,
  316. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  317. bool nullseat_is_never_utf8(Seat *seat) { return false; }
  318. bool nullseat_is_always_utf8(Seat *seat) { return true; }
  319. void nullseat_echoedit_update(Seat *seat, bool echoing, bool editing) {}
  320. const char *nullseat_get_x_display(Seat *seat) { return NULL; }
  321. bool nullseat_get_windowid(Seat *seat, long *id_out) { return false; }
  322. bool nullseat_get_window_pixel_size(
  323. Seat *seat, int *width, int *height) { return false; }
  324. StripCtrlChars *nullseat_stripctrl_new(
  325. Seat *seat, BinarySink *bs_out, SeatInteractionContext sic) {return NULL;}
  326. bool nullseat_set_trust_status(Seat *seat, bool tr) { return false; }
  327. bool nullseat_set_trust_status_vacuously(Seat *seat, bool tr) { return true; }
  328. void sk_free_peer_info(SocketPeerInfo *pi)
  329. {
  330. if (pi) {
  331. sfree((char *)pi->addr_text);
  332. sfree((char *)pi->log_text);
  333. sfree(pi);
  334. }
  335. }
  336. void out_of_memory(void)
  337. {
  338. modalfatalbox("Out of memory");
  339. }