misc.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. /*
  2. * Platform-independent routines shared between all PuTTY programs.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #include <limits.h>
  8. #include <ctype.h>
  9. #include <assert.h>
  10. #include "putty.h"
  11. #include "misc.h"
  12. /*
  13. * Parse a string block size specification. This is approximately a
  14. * subset of the block size specs supported by GNU fileutils:
  15. * "nk" = n kilobytes
  16. * "nM" = n megabytes
  17. * "nG" = n gigabytes
  18. * All numbers are decimal, and suffixes refer to powers of two.
  19. * Case-insensitive.
  20. */
  21. unsigned long parse_blocksize(const char *bs)
  22. {
  23. char *suf;
  24. unsigned long r = strtoul(bs, &suf, 10);
  25. if (*suf != '\0') {
  26. while (*suf && isspace((unsigned char)*suf)) suf++;
  27. switch (*suf) {
  28. case 'k': case 'K':
  29. r *= 1024ul;
  30. break;
  31. case 'm': case 'M':
  32. r *= 1024ul * 1024ul;
  33. break;
  34. case 'g': case 'G':
  35. r *= 1024ul * 1024ul * 1024ul;
  36. break;
  37. case '\0':
  38. default:
  39. break;
  40. }
  41. }
  42. return r;
  43. }
  44. /*
  45. * Parse a ^C style character specification.
  46. * Returns NULL in `next' if we didn't recognise it as a control character,
  47. * in which case `c' should be ignored.
  48. * The precise current parsing is an oddity inherited from the terminal
  49. * answerback-string parsing code. All sequences start with ^; all except
  50. * ^<123> are two characters. The ones that are worth keeping are probably:
  51. * ^? 127
  52. * ^@A-Z[\]^_ 0-31
  53. * a-z 1-26
  54. * <num> specified by number (decimal, 0octal, 0xHEX)
  55. * ~ ^ escape
  56. */
  57. char ctrlparse(char *s, char **next)
  58. {
  59. char c = 0;
  60. if (*s != '^') {
  61. *next = NULL;
  62. } else {
  63. s++;
  64. if (*s == '\0') {
  65. *next = NULL;
  66. } else if (*s == '<') {
  67. s++;
  68. c = (char)strtol(s, next, 0);
  69. if ((*next == s) || (**next != '>')) {
  70. c = 0;
  71. *next = NULL;
  72. } else
  73. (*next)++;
  74. } else if (*s >= 'a' && *s <= 'z') {
  75. c = (*s - ('a' - 1));
  76. *next = s+1;
  77. } else if ((*s >= '@' && *s <= '_') || *s == '?' || (*s & 0x80)) {
  78. c = ('@' ^ *s);
  79. *next = s+1;
  80. } else if (*s == '~') {
  81. c = '^';
  82. *next = s+1;
  83. }
  84. }
  85. return c;
  86. }
  87. /*
  88. * Find a character in a string, unless it's a colon contained within
  89. * square brackets. Used for untangling strings of the form
  90. * 'host:port', where host can be an IPv6 literal.
  91. *
  92. * We provide several variants of this function, with semantics like
  93. * various standard string.h functions.
  94. */
  95. static const char *host_strchr_internal(const char *s, const char *set,
  96. int first)
  97. {
  98. int brackets = 0;
  99. const char *ret = NULL;
  100. while (1) {
  101. if (!*s)
  102. return ret;
  103. if (*s == '[')
  104. brackets++;
  105. else if (*s == ']' && brackets > 0)
  106. brackets--;
  107. else if (brackets && *s == ':')
  108. /* never match */ ;
  109. else if (strchr(set, *s)) {
  110. ret = s;
  111. if (first)
  112. return ret;
  113. }
  114. s++;
  115. }
  116. }
  117. size_t host_strcspn(const char *s, const char *set)
  118. {
  119. const char *answer = host_strchr_internal(s, set, TRUE);
  120. if (answer)
  121. return answer - s;
  122. else
  123. return strlen(s);
  124. }
  125. char *host_strchr(const char *s, int c)
  126. {
  127. char set[2];
  128. set[0] = c;
  129. set[1] = '\0';
  130. return (char *) host_strchr_internal(s, set, TRUE);
  131. }
  132. char *host_strrchr(const char *s, int c)
  133. {
  134. char set[2];
  135. set[0] = c;
  136. set[1] = '\0';
  137. return (char *) host_strchr_internal(s, set, FALSE);
  138. }
  139. #ifdef TEST_HOST_STRFOO
  140. int main(void)
  141. {
  142. int passes = 0, fails = 0;
  143. #define TEST1(func, string, arg2, suffix, result) do \
  144. { \
  145. const char *str = string; \
  146. unsigned ret = func(string, arg2) suffix; \
  147. if (ret == result) { \
  148. passes++; \
  149. } else { \
  150. printf("fail: %s(%s,%s)%s = %u, expected %u\n", \
  151. #func, #string, #arg2, #suffix, ret, \
  152. (unsigned)result); \
  153. fails++; \
  154. } \
  155. } while (0)
  156. TEST1(host_strchr, "[1:2:3]:4:5", ':', -str, 7);
  157. TEST1(host_strrchr, "[1:2:3]:4:5", ':', -str, 9);
  158. TEST1(host_strcspn, "[1:2:3]:4:5", "/:",, 7);
  159. TEST1(host_strchr, "[1:2:3]", ':', == NULL, 1);
  160. TEST1(host_strrchr, "[1:2:3]", ':', == NULL, 1);
  161. TEST1(host_strcspn, "[1:2:3]", "/:",, 7);
  162. TEST1(host_strcspn, "[1:2/3]", "/:",, 4);
  163. TEST1(host_strcspn, "[1:2:3]/", "/:",, 7);
  164. printf("passed %d failed %d total %d\n", passes, fails, passes+fails);
  165. return fails != 0 ? 1 : 0;
  166. }
  167. /* Stubs to stop the rest of this module causing compile failures. */
  168. void modalfatalbox(const char *fmt, ...) {}
  169. int conf_get_int(Conf *conf, int primary) { return 0; }
  170. char *conf_get_str(Conf *conf, int primary) { return NULL; }
  171. #endif /* TEST_HOST_STRFOO */
  172. /*
  173. * Trim square brackets off the outside of an IPv6 address literal.
  174. * Leave all other strings unchanged. Returns a fresh dynamically
  175. * allocated string.
  176. */
  177. char *host_strduptrim(const char *s)
  178. {
  179. if (s[0] == '[') {
  180. const char *p = s+1;
  181. int colons = 0;
  182. while (*p && *p != ']') {
  183. if (isxdigit((unsigned char)*p))
  184. /* OK */;
  185. else if (*p == ':')
  186. colons++;
  187. else
  188. break;
  189. p++;
  190. }
  191. if (*p == ']' && !p[1] && colons > 1) {
  192. /*
  193. * This looks like an IPv6 address literal (hex digits and
  194. * at least two colons, contained in square brackets).
  195. * Trim off the brackets.
  196. */
  197. return dupprintf("%.*s", (int)(p - (s+1)), s+1);
  198. }
  199. }
  200. /*
  201. * Any other shape of string is simply duplicated.
  202. */
  203. return dupstr(s);
  204. }
  205. prompts_t *new_prompts(Frontend *frontend)
  206. {
  207. prompts_t *p = snew(prompts_t);
  208. p->prompts = NULL;
  209. p->n_prompts = 0;
  210. p->frontend = frontend;
  211. p->data = NULL;
  212. p->to_server = TRUE; /* to be on the safe side */
  213. p->name = p->instruction = NULL;
  214. p->name_reqd = p->instr_reqd = FALSE;
  215. return p;
  216. }
  217. void add_prompt(prompts_t *p, char *promptstr, int echo)
  218. {
  219. prompt_t *pr = snew(prompt_t);
  220. pr->prompt = promptstr;
  221. pr->echo = echo;
  222. pr->result = NULL;
  223. pr->resultsize = 0;
  224. p->n_prompts++;
  225. p->prompts = sresize(p->prompts, p->n_prompts, prompt_t *);
  226. p->prompts[p->n_prompts-1] = pr;
  227. }
  228. void prompt_ensure_result_size(prompt_t *pr, int newlen)
  229. {
  230. if ((int)pr->resultsize < newlen) {
  231. char *newbuf;
  232. newlen = newlen * 5 / 4 + 512; /* avoid too many small allocs */
  233. /*
  234. * We don't use sresize / realloc here, because we will be
  235. * storing sensitive stuff like passwords in here, and we want
  236. * to make sure that the data doesn't get copied around in
  237. * memory without the old copy being destroyed.
  238. */
  239. newbuf = snewn(newlen, char);
  240. memcpy(newbuf, pr->result, pr->resultsize);
  241. smemclr(pr->result, pr->resultsize);
  242. sfree(pr->result);
  243. pr->result = newbuf;
  244. pr->resultsize = newlen;
  245. }
  246. }
  247. void prompt_set_result(prompt_t *pr, const char *newstr)
  248. {
  249. prompt_ensure_result_size(pr, strlen(newstr) + 1);
  250. strcpy(pr->result, newstr);
  251. }
  252. void free_prompts(prompts_t *p)
  253. {
  254. size_t i;
  255. for (i=0; i < p->n_prompts; i++) {
  256. prompt_t *pr = p->prompts[i];
  257. smemclr(pr->result, pr->resultsize); /* burn the evidence */
  258. sfree(pr->result);
  259. sfree(pr->prompt);
  260. sfree(pr);
  261. }
  262. sfree(p->prompts);
  263. sfree(p->name);
  264. sfree(p->instruction);
  265. sfree(p);
  266. }
  267. /* ----------------------------------------------------------------------
  268. * String handling routines.
  269. */
  270. char *dupstr(const char *s)
  271. {
  272. char *p = NULL;
  273. if (s) {
  274. int len = strlen(s);
  275. p = snewn(len + 1, char);
  276. strcpy(p, s);
  277. }
  278. return p;
  279. }
  280. /* Allocate the concatenation of N strings. Terminate arg list with NULL. */
  281. char *dupcat(const char *s1, ...)
  282. {
  283. int len;
  284. char *p, *q, *sn;
  285. va_list ap;
  286. len = strlen(s1);
  287. va_start(ap, s1);
  288. while (1) {
  289. sn = va_arg(ap, char *);
  290. if (!sn)
  291. break;
  292. len += strlen(sn);
  293. }
  294. va_end(ap);
  295. p = snewn(len + 1, char);
  296. strcpy(p, s1);
  297. q = p + strlen(p);
  298. va_start(ap, s1);
  299. while (1) {
  300. sn = va_arg(ap, char *);
  301. if (!sn)
  302. break;
  303. strcpy(q, sn);
  304. q += strlen(q);
  305. }
  306. va_end(ap);
  307. return p;
  308. }
  309. void burnstr(char *string) /* sfree(str), only clear it first */
  310. {
  311. if (string) {
  312. smemclr(string, strlen(string));
  313. sfree(string);
  314. }
  315. }
  316. int toint(unsigned u)
  317. {
  318. /*
  319. * Convert an unsigned to an int, without running into the
  320. * undefined behaviour which happens by the strict C standard if
  321. * the value overflows. You'd hope that sensible compilers would
  322. * do the sensible thing in response to a cast, but actually I
  323. * don't trust modern compilers not to do silly things like
  324. * assuming that _obviously_ you wouldn't have caused an overflow
  325. * and so they can elide an 'if (i < 0)' test immediately after
  326. * the cast.
  327. *
  328. * Sensible compilers ought of course to optimise this entire
  329. * function into 'just return the input value'!
  330. */
  331. if (u <= (unsigned)INT_MAX)
  332. return (int)u;
  333. else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */
  334. return INT_MIN + (int)(u - (unsigned)INT_MIN);
  335. else
  336. return INT_MIN; /* fallback; should never occur on binary machines */
  337. }
  338. int string_length_for_printf(size_t s)
  339. {
  340. /* Truncate absurdly long strings (should one show up) to fit
  341. * within a positive 'int', which is what the "%.*s" format will
  342. * expect. */
  343. if (s > INT_MAX)
  344. return INT_MAX;
  345. return s;
  346. }
  347. /*
  348. * Do an sprintf(), but into a custom-allocated buffer.
  349. *
  350. * Currently I'm doing this via vsnprintf. This has worked so far,
  351. * but it's not good, because vsnprintf is not available on all
  352. * platforms. There's an ifdef to use `_vsnprintf', which seems
  353. * to be the local name for it on Windows. Other platforms may
  354. * lack it completely, in which case it'll be time to rewrite
  355. * this function in a totally different way.
  356. *
  357. * The only `properly' portable solution I can think of is to
  358. * implement my own format string scanner, which figures out an
  359. * upper bound for the length of each formatting directive,
  360. * allocates the buffer as it goes along, and calls sprintf() to
  361. * actually process each directive. If I ever need to actually do
  362. * this, some caveats:
  363. *
  364. * - It's very hard to find a reliable upper bound for
  365. * floating-point values. %f, in particular, when supplied with
  366. * a number near to the upper or lower limit of representable
  367. * numbers, could easily take several hundred characters. It's
  368. * probably feasible to predict this statically using the
  369. * constants in <float.h>, or even to predict it dynamically by
  370. * looking at the exponent of the specific float provided, but
  371. * it won't be fun.
  372. *
  373. * - Don't forget to _check_, after calling sprintf, that it's
  374. * used at most the amount of space we had available.
  375. *
  376. * - Fault any formatting directive we don't fully understand. The
  377. * aim here is to _guarantee_ that we never overflow the buffer,
  378. * because this is a security-critical function. If we see a
  379. * directive we don't know about, we should panic and die rather
  380. * than run any risk.
  381. */
  382. static char *dupvprintf_inner(char *buf, int oldlen, int *oldsize,
  383. const char *fmt, va_list ap)
  384. {
  385. int len, size, newsize;
  386. assert(*oldsize >= oldlen);
  387. size = *oldsize - oldlen;
  388. if (size == 0) {
  389. size = 512;
  390. newsize = oldlen + size;
  391. buf = sresize(buf, newsize, char);
  392. } else {
  393. newsize = *oldsize;
  394. }
  395. while (1) {
  396. #if defined _WINDOWS && !defined __WINE__ && _MSC_VER < 1900 /* 1900 == VS2015 has real snprintf */
  397. #define vsnprintf _vsnprintf
  398. #endif
  399. #ifdef va_copy
  400. /* Use the `va_copy' macro mandated by C99, if present.
  401. * XXX some environments may have this as __va_copy() */
  402. va_list aq;
  403. va_copy(aq, ap);
  404. len = vsnprintf(buf + oldlen, size, fmt, aq);
  405. va_end(aq);
  406. #else
  407. /* Ugh. No va_copy macro, so do something nasty.
  408. * Technically, you can't reuse a va_list like this: it is left
  409. * unspecified whether advancing a va_list pointer modifies its
  410. * value or something it points to, so on some platforms calling
  411. * vsnprintf twice on the same va_list might fail hideously
  412. * (indeed, it has been observed to).
  413. * XXX the autoconf manual suggests that using memcpy() will give
  414. * "maximum portability". */
  415. #if defined _DEBUG && defined IDE
  416. // CodeGuard hangs in v*printf functions. But while it's possible to disable CodeGuard in vsprintf, it's not possible for vsnprintf.
  417. // We never want to distribute this version of the code, hence the IDE condition.
  418. // Put this into WinSCP.cgi along with WinSCP.exe
  419. // [vsprintf]
  420. // Disable=yes
  421. len = vsprintf(buf + oldlen, fmt, ap);
  422. #else
  423. len = vsnprintf(buf + oldlen, size, fmt, ap);
  424. #endif
  425. #endif
  426. if (len >= 0 && len < size) {
  427. /* This is the C99-specified criterion for snprintf to have
  428. * been completely successful. */
  429. *oldsize = newsize;
  430. return buf;
  431. } else if (len > 0) {
  432. /* This is the C99 error condition: the returned length is
  433. * the required buffer size not counting the NUL. */
  434. size = len + 1;
  435. } else {
  436. /* This is the pre-C99 glibc error condition: <0 means the
  437. * buffer wasn't big enough, so we enlarge it a bit and hope. */
  438. size += 512;
  439. }
  440. newsize = oldlen + size;
  441. buf = sresize(buf, newsize, char);
  442. }
  443. }
  444. char *dupvprintf(const char *fmt, va_list ap)
  445. {
  446. int size = 0;
  447. return dupvprintf_inner(NULL, 0, &size, fmt, ap);
  448. }
  449. char *dupprintf(const char *fmt, ...)
  450. {
  451. char *ret;
  452. va_list ap;
  453. va_start(ap, fmt);
  454. ret = dupvprintf(fmt, ap);
  455. va_end(ap);
  456. return ret;
  457. }
  458. struct strbuf_impl {
  459. int size;
  460. struct strbuf visible;
  461. };
  462. #define STRBUF_SET_PTR(buf, ptr) \
  463. ((buf)->visible.s = (ptr), \
  464. (buf)->visible.u = (unsigned char *)(buf)->visible.s)
  465. void *strbuf_append(strbuf *buf_o, size_t len)
  466. {
  467. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  468. char *toret;
  469. if (buf->size < buf->visible.len + len + 1) {
  470. buf->size = (buf->visible.len + len + 1) * 5 / 4 + 512;
  471. STRBUF_SET_PTR(buf, sresize(buf->visible.s, buf->size, char));
  472. }
  473. toret = buf->visible.s + buf->visible.len;
  474. buf->visible.len += len;
  475. buf->visible.s[buf->visible.len] = '\0';
  476. return toret;
  477. }
  478. static void strbuf_BinarySink_write(
  479. BinarySink *bs, const void *data, size_t len)
  480. {
  481. strbuf *buf_o = BinarySink_DOWNCAST(bs, strbuf);
  482. memcpy(strbuf_append(buf_o, len), data, len);
  483. }
  484. strbuf *strbuf_new(void)
  485. {
  486. struct strbuf_impl *buf = snew(struct strbuf_impl);
  487. BinarySink_INIT(&buf->visible, strbuf_BinarySink_write);
  488. buf->visible.len = 0;
  489. buf->size = 512;
  490. STRBUF_SET_PTR(buf, snewn(buf->size, char));
  491. *buf->visible.s = '\0';
  492. return &buf->visible;
  493. }
  494. void strbuf_free(strbuf *buf_o)
  495. {
  496. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  497. if (buf->visible.s) {
  498. smemclr(buf->visible.s, buf->size);
  499. sfree(buf->visible.s);
  500. }
  501. sfree(buf);
  502. }
  503. char *strbuf_to_str(strbuf *buf_o)
  504. {
  505. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  506. char *ret = buf->visible.s;
  507. sfree(buf);
  508. return ret;
  509. }
  510. void strbuf_catfv(strbuf *buf_o, const char *fmt, va_list ap)
  511. {
  512. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  513. STRBUF_SET_PTR(buf, dupvprintf_inner(buf->visible.s, buf->visible.len,
  514. &buf->size, fmt, ap));
  515. buf->visible.len += strlen(buf->visible.s + buf->visible.len);
  516. }
  517. void strbuf_catf(strbuf *buf_o, const char *fmt, ...)
  518. {
  519. va_list ap;
  520. va_start(ap, fmt);
  521. strbuf_catfv(buf_o, fmt, ap);
  522. va_end(ap);
  523. }
  524. strbuf *strbuf_new_for_agent_query(void)
  525. {
  526. strbuf *buf = strbuf_new();
  527. put_uint32(buf, 0); /* reserve space for length field */
  528. return buf;
  529. }
  530. void strbuf_finalise_agent_query(strbuf *buf_o)
  531. {
  532. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  533. assert(buf->visible.len >= 5);
  534. PUT_32BIT_MSB_FIRST(buf->visible.u, buf->visible.len - 4);
  535. }
  536. /*
  537. * Read an entire line of text from a file. Return a buffer
  538. * malloced to be as big as necessary (caller must free).
  539. */
  540. char *fgetline(FILE *fp)
  541. {
  542. char *ret = snewn(512, char);
  543. int size = 512, len = 0;
  544. while (fgets(ret + len, size - len, fp)) {
  545. len += strlen(ret + len);
  546. if (len > 0 && ret[len-1] == '\n')
  547. break; /* got a newline, we're done */
  548. size = len + 512;
  549. ret = sresize(ret, size, char);
  550. }
  551. if (len == 0) { /* first fgets returned NULL */
  552. sfree(ret);
  553. return NULL;
  554. }
  555. ret[len] = '\0';
  556. return ret;
  557. }
  558. /*
  559. * Perl-style 'chomp', for a line we just read with fgetline. Unlike
  560. * Perl chomp, however, we're deliberately forgiving of strange
  561. * line-ending conventions. Also we forgive NULL on input, so you can
  562. * just write 'line = chomp(fgetline(fp));' and not bother checking
  563. * for NULL until afterwards.
  564. */
  565. char *chomp(char *str)
  566. {
  567. if (str) {
  568. int len = strlen(str);
  569. while (len > 0 && (str[len-1] == '\r' || str[len-1] == '\n'))
  570. len--;
  571. str[len] = '\0';
  572. }
  573. return str;
  574. }
  575. /* ----------------------------------------------------------------------
  576. * Core base64 encoding and decoding routines.
  577. */
  578. void base64_encode_atom(const unsigned char *data, int n, char *out)
  579. {
  580. static const char base64_chars[] =
  581. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  582. unsigned word;
  583. word = data[0] << 16;
  584. if (n > 1)
  585. word |= data[1] << 8;
  586. if (n > 2)
  587. word |= data[2];
  588. out[0] = base64_chars[(word >> 18) & 0x3F];
  589. out[1] = base64_chars[(word >> 12) & 0x3F];
  590. if (n > 1)
  591. out[2] = base64_chars[(word >> 6) & 0x3F];
  592. else
  593. out[2] = '=';
  594. if (n > 2)
  595. out[3] = base64_chars[word & 0x3F];
  596. else
  597. out[3] = '=';
  598. }
  599. int base64_decode_atom(const char *atom, unsigned char *out)
  600. {
  601. int vals[4];
  602. int i, v, len;
  603. unsigned word;
  604. char c;
  605. for (i = 0; i < 4; i++) {
  606. c = atom[i];
  607. if (c >= 'A' && c <= 'Z')
  608. v = c - 'A';
  609. else if (c >= 'a' && c <= 'z')
  610. v = c - 'a' + 26;
  611. else if (c >= '0' && c <= '9')
  612. v = c - '0' + 52;
  613. else if (c == '+')
  614. v = 62;
  615. else if (c == '/')
  616. v = 63;
  617. else if (c == '=')
  618. v = -1;
  619. else
  620. return 0; /* invalid atom */
  621. vals[i] = v;
  622. }
  623. if (vals[0] == -1 || vals[1] == -1)
  624. return 0;
  625. if (vals[2] == -1 && vals[3] != -1)
  626. return 0;
  627. if (vals[3] != -1)
  628. len = 3;
  629. else if (vals[2] != -1)
  630. len = 2;
  631. else
  632. len = 1;
  633. word = ((vals[0] << 18) |
  634. (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));
  635. out[0] = (word >> 16) & 0xFF;
  636. if (len > 1)
  637. out[1] = (word >> 8) & 0xFF;
  638. if (len > 2)
  639. out[2] = word & 0xFF;
  640. return len;
  641. }
  642. /* ----------------------------------------------------------------------
  643. * Generic routines to deal with send buffers: a linked list of
  644. * smallish blocks, with the operations
  645. *
  646. * - add an arbitrary amount of data to the end of the list
  647. * - remove the first N bytes from the list
  648. * - return a (pointer,length) pair giving some initial data in
  649. * the list, suitable for passing to a send or write system
  650. * call
  651. * - retrieve a larger amount of initial data from the list
  652. * - return the current size of the buffer chain in bytes
  653. */
  654. /* MP:
  655. * Default granule of 512 leads to low performance.
  656. */
  657. #define BUFFER_MIN_GRANULE 512*2*32
  658. struct bufchain_granule {
  659. struct bufchain_granule *next;
  660. char *bufpos, *bufend, *bufmax;
  661. };
  662. void bufchain_init(bufchain *ch)
  663. {
  664. ch->head = ch->tail = NULL;
  665. ch->buffersize = 0;
  666. ch->ic = NULL;
  667. }
  668. void bufchain_clear(bufchain *ch)
  669. {
  670. struct bufchain_granule *b;
  671. while (ch->head) {
  672. b = ch->head;
  673. ch->head = ch->head->next;
  674. sfree(b);
  675. }
  676. ch->tail = NULL;
  677. ch->buffersize = 0;
  678. }
  679. int bufchain_size(bufchain *ch)
  680. {
  681. return ch->buffersize;
  682. }
  683. void bufchain_add(bufchain *ch, const void *data, int len)
  684. {
  685. const char *buf = (const char *)data;
  686. if (len == 0) return;
  687. ch->buffersize += len;
  688. while (len > 0) {
  689. if (ch->tail && ch->tail->bufend < ch->tail->bufmax) {
  690. int copylen = min(len, ch->tail->bufmax - ch->tail->bufend);
  691. memcpy(ch->tail->bufend, buf, copylen);
  692. buf += copylen;
  693. len -= copylen;
  694. ch->tail->bufend += copylen;
  695. }
  696. if (len > 0) {
  697. int grainlen =
  698. max(sizeof(struct bufchain_granule) + len, BUFFER_MIN_GRANULE);
  699. struct bufchain_granule *newbuf;
  700. newbuf = smalloc(grainlen);
  701. newbuf->bufpos = newbuf->bufend =
  702. (char *)newbuf + sizeof(struct bufchain_granule);
  703. newbuf->bufmax = (char *)newbuf + grainlen;
  704. newbuf->next = NULL;
  705. if (ch->tail)
  706. ch->tail->next = newbuf;
  707. else
  708. ch->head = newbuf;
  709. ch->tail = newbuf;
  710. }
  711. }
  712. if (ch->ic)
  713. queue_idempotent_callback(ch->ic);
  714. }
  715. void bufchain_consume(bufchain *ch, int len)
  716. {
  717. struct bufchain_granule *tmp;
  718. assert(ch->buffersize >= len);
  719. while (len > 0) {
  720. int remlen = len;
  721. assert(ch->head != NULL);
  722. if (remlen >= ch->head->bufend - ch->head->bufpos) {
  723. remlen = ch->head->bufend - ch->head->bufpos;
  724. tmp = ch->head;
  725. ch->head = tmp->next;
  726. if (!ch->head)
  727. ch->tail = NULL;
  728. sfree(tmp);
  729. } else
  730. ch->head->bufpos += remlen;
  731. ch->buffersize -= remlen;
  732. len -= remlen;
  733. }
  734. }
  735. void bufchain_prefix(bufchain *ch, void **data, int *len)
  736. {
  737. *len = ch->head->bufend - ch->head->bufpos;
  738. *data = ch->head->bufpos;
  739. }
  740. void bufchain_fetch(bufchain *ch, void *data, int len)
  741. {
  742. struct bufchain_granule *tmp;
  743. char *data_c = (char *)data;
  744. tmp = ch->head;
  745. assert(ch->buffersize >= len);
  746. while (len > 0) {
  747. int remlen = len;
  748. assert(tmp != NULL);
  749. if (remlen >= tmp->bufend - tmp->bufpos)
  750. remlen = tmp->bufend - tmp->bufpos;
  751. memcpy(data_c, tmp->bufpos, remlen);
  752. tmp = tmp->next;
  753. len -= remlen;
  754. data_c += remlen;
  755. }
  756. }
  757. void bufchain_fetch_consume(bufchain *ch, void *data, int len)
  758. {
  759. bufchain_fetch(ch, data, len);
  760. bufchain_consume(ch, len);
  761. }
  762. int bufchain_try_fetch_consume(bufchain *ch, void *data, int len)
  763. {
  764. if (ch->buffersize >= len) {
  765. bufchain_fetch_consume(ch, data, len);
  766. return TRUE;
  767. } else {
  768. return FALSE;
  769. }
  770. }
  771. /* ----------------------------------------------------------------------
  772. * Sanitise terminal output that we have reason not to trust, e.g.
  773. * because it appears in the login banner or password prompt from a
  774. * server, which we'd rather not permit to use arbitrary escape
  775. * sequences.
  776. */
  777. void sanitise_term_data(bufchain *out, const void *vdata, int len)
  778. {
  779. const char *data = (const char *)vdata;
  780. int i;
  781. /*
  782. * FIXME: this method of sanitisation is ASCII-centric. It would
  783. * be nice to permit SSH banners and the like to contain printable
  784. * Unicode, but that would need a lot more complicated code here
  785. * (not to mention knowing what character set it should interpret
  786. * the data as).
  787. */
  788. for (i = 0; i < len; i++) {
  789. if (data[i] == '\n')
  790. bufchain_add(out, "\r\n", 2);
  791. else if (data[i] >= ' ' && data[i] < 0x7F)
  792. bufchain_add(out, data + i, 1);
  793. }
  794. }
  795. /* ----------------------------------------------------------------------
  796. * My own versions of malloc, realloc and free. Because I want
  797. * malloc and realloc to bomb out and exit the program if they run
  798. * out of memory, realloc to reliably call malloc if passed a NULL
  799. * pointer, and free to reliably do nothing if passed a NULL
  800. * pointer. We can also put trace printouts in, if we need to; and
  801. * we can also replace the allocator with an ElectricFence-like
  802. * one.
  803. */
  804. #ifdef MINEFIELD
  805. void *minefield_c_malloc(size_t size);
  806. void minefield_c_free(void *p);
  807. void *minefield_c_realloc(void *p, size_t size);
  808. #endif
  809. #ifdef MALLOC_LOG
  810. static FILE *fp = NULL;
  811. static char *mlog_file = NULL;
  812. static int mlog_line = 0;
  813. void mlog(char *file, int line)
  814. {
  815. mlog_file = file;
  816. mlog_line = line;
  817. if (!fp) {
  818. fp = fopen("putty_mem.log", "w");
  819. setvbuf(fp, NULL, _IONBF, BUFSIZ);
  820. }
  821. if (fp)
  822. fprintf(fp, "%s:%d: ", file, line);
  823. }
  824. #endif
  825. void *safemalloc(size_t n, size_t size)
  826. {
  827. void *p;
  828. if (n > INT_MAX / size) {
  829. p = NULL;
  830. } else {
  831. size *= n;
  832. if (size == 0) size = 1;
  833. #ifdef MINEFIELD
  834. p = minefield_c_malloc(size);
  835. #else
  836. p = malloc(size);
  837. #endif
  838. }
  839. if (!p) {
  840. char str[200];
  841. #ifdef MALLOC_LOG
  842. sprintf(str, "Out of memory! (%s:%d, size=%d)",
  843. mlog_file, mlog_line, size);
  844. fprintf(fp, "*** %s\n", str);
  845. fclose(fp);
  846. #else
  847. strcpy(str, "Out of memory!");
  848. #endif
  849. modalfatalbox("%s", str);
  850. }
  851. #ifdef MALLOC_LOG
  852. if (fp)
  853. fprintf(fp, "malloc(%d) returns %p\n", size, p);
  854. #endif
  855. return p;
  856. }
  857. void *saferealloc(void *ptr, size_t n, size_t size)
  858. {
  859. void *p;
  860. if (n > INT_MAX / size) {
  861. p = NULL;
  862. } else {
  863. size *= n;
  864. if (!ptr) {
  865. #ifdef MINEFIELD
  866. p = minefield_c_malloc(size);
  867. #else
  868. p = malloc(size);
  869. #endif
  870. } else {
  871. #ifdef MINEFIELD
  872. p = minefield_c_realloc(ptr, size);
  873. #else
  874. p = realloc(ptr, size);
  875. #endif
  876. }
  877. }
  878. if (!p) {
  879. char str[200];
  880. #ifdef MALLOC_LOG
  881. sprintf(str, "Out of memory! (%s:%d, size=%d)",
  882. mlog_file, mlog_line, size);
  883. fprintf(fp, "*** %s\n", str);
  884. fclose(fp);
  885. #else
  886. strcpy(str, "Out of memory!");
  887. #endif
  888. modalfatalbox("%s", str);
  889. }
  890. #ifdef MALLOC_LOG
  891. if (fp)
  892. fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
  893. #endif
  894. return p;
  895. }
  896. void safefree(void *ptr)
  897. {
  898. if (ptr) {
  899. #ifdef MALLOC_LOG
  900. if (fp)
  901. fprintf(fp, "free(%p)\n", ptr);
  902. #endif
  903. #ifdef MINEFIELD
  904. minefield_c_free(ptr);
  905. #else
  906. free(ptr);
  907. #endif
  908. }
  909. #ifdef MALLOC_LOG
  910. else if (fp)
  911. fprintf(fp, "freeing null pointer - no action taken\n");
  912. #endif
  913. }
  914. /* ----------------------------------------------------------------------
  915. * Debugging routines.
  916. */
  917. #ifdef DEBUG
  918. extern void dputs(const char *); /* defined in per-platform *misc.c */
  919. void debug_printf(const char *fmt, ...)
  920. {
  921. char *buf;
  922. va_list ap;
  923. va_start(ap, fmt);
  924. buf = dupvprintf(fmt, ap);
  925. dputs(buf);
  926. sfree(buf);
  927. va_end(ap);
  928. }
  929. void debug_memdump(const void *buf, int len, int L)
  930. {
  931. int i;
  932. const unsigned char *p = buf;
  933. char foo[17];
  934. if (L) {
  935. int delta;
  936. debug_printf("\t%d (0x%x) bytes:\n", len, len);
  937. delta = 15 & (uintptr_t)p;
  938. p -= delta;
  939. len += delta;
  940. }
  941. for (; 0 < len; p += 16, len -= 16) {
  942. dputs(" ");
  943. if (L)
  944. debug_printf("%p: ", p);
  945. strcpy(foo, "................"); /* sixteen dots */
  946. for (i = 0; i < 16 && i < len; ++i) {
  947. if (&p[i] < (unsigned char *) buf) {
  948. dputs(" "); /* 3 spaces */
  949. foo[i] = ' ';
  950. } else {
  951. debug_printf("%c%02.2x",
  952. &p[i] != (unsigned char *) buf
  953. && i % 4 ? '.' : ' ', p[i]
  954. );
  955. if (p[i] >= ' ' && p[i] <= '~')
  956. foo[i] = (char) p[i];
  957. }
  958. }
  959. foo[i] = '\0';
  960. debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
  961. }
  962. }
  963. #endif /* def DEBUG */
  964. /*
  965. * Determine whether or not a Conf represents a session which can
  966. * sensibly be launched right now.
  967. */
  968. int conf_launchable(Conf *conf)
  969. {
  970. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  971. return conf_get_str(conf, CONF_serline)[0] != 0;
  972. else
  973. return conf_get_str(conf, CONF_host)[0] != 0;
  974. }
  975. char const *conf_dest(Conf *conf)
  976. {
  977. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  978. return conf_get_str(conf, CONF_serline);
  979. else
  980. return conf_get_str(conf, CONF_host);
  981. }
  982. #ifndef PLATFORM_HAS_SMEMCLR
  983. /*
  984. * Securely wipe memory.
  985. *
  986. * The actual wiping is no different from what memset would do: the
  987. * point of 'securely' is to try to be sure over-clever compilers
  988. * won't optimise away memsets on variables that are about to be freed
  989. * or go out of scope. See
  990. * https://buildsecurityin.us-cert.gov/bsi-rules/home/g1/771-BSI.html
  991. *
  992. * Some platforms (e.g. Windows) may provide their own version of this
  993. * function.
  994. */
  995. void smemclr(void *b, size_t n) {
  996. volatile char *vp;
  997. if (b && n > 0) {
  998. /*
  999. * Zero out the memory.
  1000. */
  1001. memset(b, 0, n);
  1002. /*
  1003. * Perform a volatile access to the object, forcing the
  1004. * compiler to admit that the previous memset was important.
  1005. *
  1006. * This while loop should in practice run for zero iterations
  1007. * (since we know we just zeroed the object out), but in
  1008. * theory (as far as the compiler knows) it might range over
  1009. * the whole object. (If we had just written, say, '*vp =
  1010. * *vp;', a compiler could in principle have 'helpfully'
  1011. * optimised the memset into only zeroing out the first byte.
  1012. * This should be robust.)
  1013. */
  1014. vp = b;
  1015. while (*vp) vp++;
  1016. }
  1017. }
  1018. #endif
  1019. /*
  1020. * Validate a manual host key specification (either entered in the
  1021. * GUI, or via -hostkey). If valid, we return TRUE, and update 'key'
  1022. * to contain a canonicalised version of the key string in 'key'
  1023. * (which is guaranteed to take up at most as much space as the
  1024. * original version), suitable for putting into the Conf. If not
  1025. * valid, we return FALSE.
  1026. */
  1027. int validate_manual_hostkey(char *key)
  1028. {
  1029. char *p, *q, *r, *s;
  1030. /*
  1031. * Step through the string word by word, looking for a word that's
  1032. * in one of the formats we like.
  1033. */
  1034. p = key;
  1035. while ((p += strspn(p, " \t"))[0]) {
  1036. q = p;
  1037. p += strcspn(p, " \t");
  1038. if (*p) *p++ = '\0';
  1039. /*
  1040. * Now q is our word.
  1041. */
  1042. if (strlen(q) == 16*3 - 1 &&
  1043. q[strspn(q, "0123456789abcdefABCDEF:")] == 0) {
  1044. /*
  1045. * Might be a key fingerprint. Check the colons are in the
  1046. * right places, and if so, return the same fingerprint
  1047. * canonicalised into lowercase.
  1048. */
  1049. int i;
  1050. for (i = 0; i < 16; i++)
  1051. if (q[3*i] == ':' || q[3*i+1] == ':')
  1052. goto not_fingerprint; /* sorry */
  1053. for (i = 0; i < 15; i++)
  1054. if (q[3*i+2] != ':')
  1055. goto not_fingerprint; /* sorry */
  1056. for (i = 0; i < 16*3 - 1; i++)
  1057. key[i] = tolower(q[i]);
  1058. key[16*3 - 1] = '\0';
  1059. return TRUE;
  1060. }
  1061. not_fingerprint:;
  1062. /*
  1063. * Before we check for a public-key blob, trim newlines out of
  1064. * the middle of the word, in case someone's managed to paste
  1065. * in a public-key blob _with_ them.
  1066. */
  1067. for (r = s = q; *r; r++)
  1068. if (*r != '\n' && *r != '\r')
  1069. *s++ = *r;
  1070. *s = '\0';
  1071. if (strlen(q) % 4 == 0 && strlen(q) > 2*4 &&
  1072. q[strspn(q, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1073. "abcdefghijklmnopqrstuvwxyz+/=")] == 0) {
  1074. /*
  1075. * Might be a base64-encoded SSH-2 public key blob. Check
  1076. * that it starts with a sensible algorithm string. No
  1077. * canonicalisation is necessary for this string type.
  1078. *
  1079. * The algorithm string must be at most 64 characters long
  1080. * (RFC 4251 section 6).
  1081. */
  1082. unsigned char decoded[6];
  1083. unsigned alglen;
  1084. int minlen;
  1085. int len = 0;
  1086. len += base64_decode_atom(q, decoded+len);
  1087. if (len < 3)
  1088. goto not_ssh2_blob; /* sorry */
  1089. len += base64_decode_atom(q+4, decoded+len);
  1090. if (len < 4)
  1091. goto not_ssh2_blob; /* sorry */
  1092. alglen = GET_32BIT_MSB_FIRST(decoded);
  1093. if (alglen > 64)
  1094. goto not_ssh2_blob; /* sorry */
  1095. minlen = ((alglen + 4) + 2) / 3;
  1096. if (strlen(q) < minlen)
  1097. goto not_ssh2_blob; /* sorry */
  1098. strcpy(key, q);
  1099. return TRUE;
  1100. }
  1101. not_ssh2_blob:;
  1102. }
  1103. return FALSE;
  1104. }
  1105. int smemeq(const void *av, const void *bv, size_t len)
  1106. {
  1107. const unsigned char *a = (const unsigned char *)av;
  1108. const unsigned char *b = (const unsigned char *)bv;
  1109. unsigned val = 0;
  1110. while (len-- > 0) {
  1111. val |= *a++ ^ *b++;
  1112. }
  1113. /* Now val is 0 iff we want to return 1, and in the range
  1114. * 0x01..0xFF iff we want to return 0. So subtracting from 0x100
  1115. * will clear bit 8 iff we want to return 0, and leave it set iff
  1116. * we want to return 1, so then we can just shift down. */
  1117. return (0x100 - val) >> 8;
  1118. }
  1119. int nullstrcmp(const char *a, const char *b)
  1120. {
  1121. if (a == NULL && b == NULL)
  1122. return 0;
  1123. if (a == NULL)
  1124. return -1;
  1125. if (b == NULL)
  1126. return +1;
  1127. return strcmp(a, b);
  1128. }
  1129. ptrlen make_ptrlen(const void *ptr, size_t len)
  1130. {
  1131. ptrlen pl;
  1132. pl.ptr = ptr;
  1133. pl.len = len;
  1134. return pl;
  1135. }
  1136. int ptrlen_eq_string(ptrlen pl, const char *str)
  1137. {
  1138. size_t len = strlen(str);
  1139. return (pl.len == len && !memcmp(pl.ptr, str, len));
  1140. }
  1141. char *mkstr(ptrlen pl)
  1142. {
  1143. char *p = snewn(pl.len + 1, char);
  1144. memcpy(p, pl.ptr, pl.len);
  1145. p[pl.len] = '\0';
  1146. return p;
  1147. }
  1148. int strstartswith(const char *s, const char *t)
  1149. {
  1150. return !memcmp(s, t, strlen(t));
  1151. }
  1152. int strendswith(const char *s, const char *t)
  1153. {
  1154. size_t slen = strlen(s), tlen = strlen(t);
  1155. return slen >= tlen && !strcmp(s + (slen - tlen), t);
  1156. }
  1157. char *buildinfo(const char *newline)
  1158. {
  1159. strbuf *buf = strbuf_new();
  1160. extern const char commitid[]; /* in commitid.c */
  1161. strbuf_catf(buf, "Build platform: %d-bit %s",
  1162. (int)(CHAR_BIT * sizeof(void *)),
  1163. BUILDINFO_PLATFORM);
  1164. #ifdef __clang_version__
  1165. #define FOUND_COMPILER
  1166. strbuf_catf(buf, "%sCompiler: clang %s", newline, __clang_version__);
  1167. #elif defined __GNUC__ && defined __VERSION__
  1168. #define FOUND_COMPILER
  1169. strbuf_catf(buf, "%sCompiler: gcc %s", newline, __VERSION__);
  1170. #endif
  1171. #if defined _MSC_VER
  1172. #ifndef FOUND_COMPILER
  1173. #define FOUND_COMPILER
  1174. strbuf_catf(buf, "%sCompiler: ", newline);
  1175. #else
  1176. strbuf_catf(buf, ", emulating ");
  1177. #endif
  1178. strbuf_catf(buf, "Visual Studio", newline);
  1179. #if _MSC_VER == 1900
  1180. strbuf_catf(buf, " 2015 / MSVC++ 14.0");
  1181. #elif _MSC_VER == 1912
  1182. strbuf_catf(buf, " 2017 / MSVC++ 14.12");
  1183. #elif _MSC_VER == 1800
  1184. strbuf_catf(buf, " 2013 / MSVC++ 12.0");
  1185. #elif _MSC_VER == 1700
  1186. strbuf_catf(buf, " 2012 / MSVC++ 11.0");
  1187. #elif _MSC_VER == 1600
  1188. strbuf_catf(buf, " 2010 / MSVC++ 10.0");
  1189. #elif _MSC_VER == 1500
  1190. strbuf_catf(buf, " 2008 / MSVC++ 9.0");
  1191. #elif _MSC_VER == 1400
  1192. strbuf_catf(buf, " 2005 / MSVC++ 8.0");
  1193. #elif _MSC_VER == 1310
  1194. strbuf_catf(buf, " 2003 / MSVC++ 7.1");
  1195. #elif _MSC_VER == 1300
  1196. strbuf_catf(buf, " 2003 / MSVC++ 7.0");
  1197. #else
  1198. strbuf_catf(buf, ", unrecognised version");
  1199. #endif
  1200. strbuf_catf(buf, " (_MSC_VER=%d)", (int)_MSC_VER);
  1201. #endif
  1202. #ifdef BUILDINFO_GTK
  1203. {
  1204. char *gtk_buildinfo = buildinfo_gtk_version();
  1205. if (gtk_buildinfo) {
  1206. strbuf_catf(buf, "%sCompiled against GTK version %s",
  1207. newline, gtk_buildinfo);
  1208. sfree(gtk_buildinfo);
  1209. }
  1210. }
  1211. #endif
  1212. #if defined _WINDOWS && defined MINEFIELD
  1213. strbuf_catf(buf, "%sBuild option: MINEFIELD", newline);
  1214. #endif
  1215. #ifdef NO_SECURITY
  1216. strbuf_catf(buf, "%sBuild option: NO_SECURITY", newline);
  1217. #endif
  1218. #ifdef NO_SECUREZEROMEMORY
  1219. strbuf_catf(buf, "%sBuild option: NO_SECUREZEROMEMORY", newline);
  1220. #endif
  1221. #ifdef NO_IPV6
  1222. strbuf_catf(buf, "%sBuild option: NO_IPV6", newline);
  1223. #endif
  1224. #ifdef NO_GSSAPI
  1225. strbuf_catf(buf, "%sBuild option: NO_GSSAPI", newline);
  1226. #endif
  1227. #ifdef STATIC_GSSAPI
  1228. strbuf_catf(buf, "%sBuild option: STATIC_GSSAPI", newline);
  1229. #endif
  1230. #ifdef UNPROTECT
  1231. strbuf_catf(buf, "%sBuild option: UNPROTECT", newline);
  1232. #endif
  1233. #ifdef FUZZING
  1234. strbuf_catf(buf, "%sBuild option: FUZZING", newline);
  1235. #endif
  1236. #ifdef DEBUG
  1237. strbuf_catf(buf, "%sBuild option: DEBUG", newline);
  1238. #endif
  1239. strbuf_catf(buf, "%sSource commit: %s", newline, commitid);
  1240. return strbuf_to_str(buf);
  1241. }
  1242. #ifdef MPEXT
  1243. #include "version.h"
  1244. const char * get_putty_version()
  1245. {
  1246. return TEXTVER;
  1247. }
  1248. #endif