misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 = 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. p->n_prompts++;
  51. p->prompts = sresize(p->prompts, p->n_prompts, prompt_t *);
  52. p->prompts[p->n_prompts-1] = pr;
  53. }
  54. void prompt_ensure_result_size(prompt_t *pr, int newlen)
  55. {
  56. if ((int)pr->resultsize < newlen) {
  57. char *newbuf;
  58. newlen = newlen * 5 / 4 + 512; /* avoid too many small allocs */
  59. /*
  60. * We don't use sresize / realloc here, because we will be
  61. * storing sensitive stuff like passwords in here, and we want
  62. * to make sure that the data doesn't get copied around in
  63. * memory without the old copy being destroyed.
  64. */
  65. newbuf = snewn(newlen, char);
  66. memcpy(newbuf, pr->result, pr->resultsize);
  67. smemclr(pr->result, pr->resultsize);
  68. sfree(pr->result);
  69. pr->result = newbuf;
  70. pr->resultsize = newlen;
  71. }
  72. }
  73. void prompt_set_result(prompt_t *pr, const char *newstr)
  74. {
  75. prompt_ensure_result_size(pr, strlen(newstr) + 1);
  76. strcpy(pr->result, newstr);
  77. }
  78. void free_prompts(prompts_t *p)
  79. {
  80. size_t i;
  81. for (i=0; i < p->n_prompts; i++) {
  82. prompt_t *pr = p->prompts[i];
  83. smemclr(pr->result, pr->resultsize); /* burn the evidence */
  84. sfree(pr->result);
  85. sfree(pr->prompt);
  86. sfree(pr);
  87. }
  88. sfree(p->prompts);
  89. sfree(p->name);
  90. sfree(p->instruction);
  91. sfree(p);
  92. }
  93. /*
  94. * Determine whether or not a Conf represents a session which can
  95. * sensibly be launched right now.
  96. */
  97. bool conf_launchable(Conf *conf)
  98. {
  99. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  100. return conf_get_str(conf, CONF_serline)[0] != 0;
  101. else
  102. return conf_get_str(conf, CONF_host)[0] != 0;
  103. }
  104. char const *conf_dest(Conf *conf)
  105. {
  106. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  107. return conf_get_str(conf, CONF_serline);
  108. else
  109. return conf_get_str(conf, CONF_host);
  110. }
  111. /*
  112. * Validate a manual host key specification (either entered in the
  113. * GUI, or via -hostkey). If valid, we return true, and update 'key'
  114. * to contain a canonicalised version of the key string in 'key'
  115. * (which is guaranteed to take up at most as much space as the
  116. * original version), suitable for putting into the Conf. If not
  117. * valid, we return false.
  118. */
  119. bool validate_manual_hostkey(char *key)
  120. {
  121. char *p, *q, *r, *s;
  122. /*
  123. * Step through the string word by word, looking for a word that's
  124. * in one of the formats we like.
  125. */
  126. p = key;
  127. while ((p += strspn(p, " \t"))[0]) {
  128. q = p;
  129. p += strcspn(p, " \t");
  130. if (*p) *p++ = '\0';
  131. /*
  132. * Now q is our word.
  133. */
  134. if (strlen(q) == 16*3 - 1 &&
  135. q[strspn(q, "0123456789abcdefABCDEF:")] == 0) {
  136. /*
  137. * Might be a key fingerprint. Check the colons are in the
  138. * right places, and if so, return the same fingerprint
  139. * canonicalised into lowercase.
  140. */
  141. int i;
  142. for (i = 0; i < 16; i++)
  143. if (q[3*i] == ':' || q[3*i+1] == ':')
  144. goto not_fingerprint; /* sorry */
  145. for (i = 0; i < 15; i++)
  146. if (q[3*i+2] != ':')
  147. goto not_fingerprint; /* sorry */
  148. for (i = 0; i < 16*3 - 1; i++)
  149. key[i] = tolower(q[i]);
  150. key[16*3 - 1] = '\0';
  151. return true;
  152. }
  153. not_fingerprint:;
  154. /*
  155. * Before we check for a public-key blob, trim newlines out of
  156. * the middle of the word, in case someone's managed to paste
  157. * in a public-key blob _with_ them.
  158. */
  159. for (r = s = q; *r; r++)
  160. if (*r != '\n' && *r != '\r')
  161. *s++ = *r;
  162. *s = '\0';
  163. if (strlen(q) % 4 == 0 && strlen(q) > 2*4 &&
  164. q[strspn(q, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  165. "abcdefghijklmnopqrstuvwxyz+/=")] == 0) {
  166. /*
  167. * Might be a base64-encoded SSH-2 public key blob. Check
  168. * that it starts with a sensible algorithm string. No
  169. * canonicalisation is necessary for this string type.
  170. *
  171. * The algorithm string must be at most 64 characters long
  172. * (RFC 4251 section 6).
  173. */
  174. unsigned char decoded[6];
  175. unsigned alglen;
  176. int minlen;
  177. int len = 0;
  178. len += base64_decode_atom(q, decoded+len);
  179. if (len < 3)
  180. goto not_ssh2_blob; /* sorry */
  181. len += base64_decode_atom(q+4, decoded+len);
  182. if (len < 4)
  183. goto not_ssh2_blob; /* sorry */
  184. alglen = GET_32BIT_MSB_FIRST(decoded);
  185. if (alglen > 64)
  186. goto not_ssh2_blob; /* sorry */
  187. minlen = ((alglen + 4) + 2) / 3;
  188. if (strlen(q) < minlen)
  189. goto not_ssh2_blob; /* sorry */
  190. strcpy(key, q);
  191. return true;
  192. }
  193. not_ssh2_blob:;
  194. }
  195. return false;
  196. }
  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 && defined MINEFIELD
  252. strbuf_catf(buf, "%sBuild option: MINEFIELD", newline);
  253. #endif
  254. #ifdef NO_SECURITY
  255. strbuf_catf(buf, "%sBuild option: NO_SECURITY", newline);
  256. #endif
  257. #ifdef NO_SECUREZEROMEMORY
  258. strbuf_catf(buf, "%sBuild option: NO_SECUREZEROMEMORY", newline);
  259. #endif
  260. #ifdef NO_IPV6
  261. strbuf_catf(buf, "%sBuild option: NO_IPV6", newline);
  262. #endif
  263. #ifdef NO_GSSAPI
  264. strbuf_catf(buf, "%sBuild option: NO_GSSAPI", newline);
  265. #endif
  266. #ifdef STATIC_GSSAPI
  267. strbuf_catf(buf, "%sBuild option: STATIC_GSSAPI", newline);
  268. #endif
  269. #ifdef UNPROTECT
  270. strbuf_catf(buf, "%sBuild option: UNPROTECT", newline);
  271. #endif
  272. #ifdef FUZZING
  273. strbuf_catf(buf, "%sBuild option: FUZZING", newline);
  274. #endif
  275. #ifdef DEBUG
  276. strbuf_catf(buf, "%sBuild option: DEBUG", newline);
  277. #endif
  278. strbuf_catf(buf, "%sSource commit: %s", newline, commitid);
  279. return strbuf_to_str(buf);
  280. }
  281. #ifdef MPEXT
  282. #include "version.h"
  283. const char * get_putty_version()
  284. {
  285. return TEXTVER;
  286. }
  287. #endif
  288. size_t nullseat_output(
  289. Seat *seat, bool is_stderr, const void *data, size_t len) { return 0; }
  290. bool nullseat_eof(Seat *seat) { return true; }
  291. int nullseat_get_userpass_input(
  292. Seat *seat, prompts_t *p, bufchain *input) { return 0; }
  293. void nullseat_notify_remote_exit(Seat *seat) {}
  294. void nullseat_connection_fatal(Seat *seat, const char *message) {}
  295. void nullseat_update_specials_menu(Seat *seat) {}
  296. char *nullseat_get_ttymode(Seat *seat, const char *mode) { return NULL; }
  297. void nullseat_set_busy_status(Seat *seat, BusyStatus status) {}
  298. int nullseat_verify_ssh_host_key(
  299. Seat *seat, const char *host, int port,
  300. const char *keytype, char *keystr, char *key_fingerprint,
  301. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  302. int nullseat_confirm_weak_crypto_primitive(
  303. Seat *seat, const char *algtype, const char *algname,
  304. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  305. int nullseat_confirm_weak_cached_hostkey(
  306. Seat *seat, const char *algname, const char *betteralgs,
  307. void (*callback)(void *ctx, int result), void *ctx) { return 0; }
  308. bool nullseat_is_never_utf8(Seat *seat) { return false; }
  309. bool nullseat_is_always_utf8(Seat *seat) { return true; }
  310. void nullseat_echoedit_update(Seat *seat, bool echoing, bool editing) {}
  311. const char *nullseat_get_x_display(Seat *seat) { return NULL; }
  312. bool nullseat_get_windowid(Seat *seat, long *id_out) { return false; }
  313. bool nullseat_get_window_pixel_size(
  314. Seat *seat, int *width, int *height) { return false; }
  315. void sk_free_peer_info(SocketPeerInfo *pi)
  316. {
  317. if (pi) {
  318. sfree((char *)pi->addr_text);
  319. sfree((char *)pi->log_text);
  320. sfree(pi);
  321. }
  322. }
  323. void out_of_memory(void)
  324. {
  325. modalfatalbox("Out of memory");
  326. }