misc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. void logevent_and_free(Frontend *frontend, char *s)
  317. {
  318. logevent(frontend, s);
  319. sfree(s);
  320. }
  321. int toint(unsigned u)
  322. {
  323. /*
  324. * Convert an unsigned to an int, without running into the
  325. * undefined behaviour which happens by the strict C standard if
  326. * the value overflows. You'd hope that sensible compilers would
  327. * do the sensible thing in response to a cast, but actually I
  328. * don't trust modern compilers not to do silly things like
  329. * assuming that _obviously_ you wouldn't have caused an overflow
  330. * and so they can elide an 'if (i < 0)' test immediately after
  331. * the cast.
  332. *
  333. * Sensible compilers ought of course to optimise this entire
  334. * function into 'just return the input value'!
  335. */
  336. if (u <= (unsigned)INT_MAX)
  337. return (int)u;
  338. else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */
  339. return INT_MIN + (int)(u - (unsigned)INT_MIN);
  340. else
  341. return INT_MIN; /* fallback; should never occur on binary machines */
  342. }
  343. int string_length_for_printf(size_t s)
  344. {
  345. /* Truncate absurdly long strings (should one show up) to fit
  346. * within a positive 'int', which is what the "%.*s" format will
  347. * expect. */
  348. if (s > INT_MAX)
  349. return INT_MAX;
  350. return s;
  351. }
  352. /*
  353. * Do an sprintf(), but into a custom-allocated buffer.
  354. *
  355. * Currently I'm doing this via vsnprintf. This has worked so far,
  356. * but it's not good, because vsnprintf is not available on all
  357. * platforms. There's an ifdef to use `_vsnprintf', which seems
  358. * to be the local name for it on Windows. Other platforms may
  359. * lack it completely, in which case it'll be time to rewrite
  360. * this function in a totally different way.
  361. *
  362. * The only `properly' portable solution I can think of is to
  363. * implement my own format string scanner, which figures out an
  364. * upper bound for the length of each formatting directive,
  365. * allocates the buffer as it goes along, and calls sprintf() to
  366. * actually process each directive. If I ever need to actually do
  367. * this, some caveats:
  368. *
  369. * - It's very hard to find a reliable upper bound for
  370. * floating-point values. %f, in particular, when supplied with
  371. * a number near to the upper or lower limit of representable
  372. * numbers, could easily take several hundred characters. It's
  373. * probably feasible to predict this statically using the
  374. * constants in <float.h>, or even to predict it dynamically by
  375. * looking at the exponent of the specific float provided, but
  376. * it won't be fun.
  377. *
  378. * - Don't forget to _check_, after calling sprintf, that it's
  379. * used at most the amount of space we had available.
  380. *
  381. * - Fault any formatting directive we don't fully understand. The
  382. * aim here is to _guarantee_ that we never overflow the buffer,
  383. * because this is a security-critical function. If we see a
  384. * directive we don't know about, we should panic and die rather
  385. * than run any risk.
  386. */
  387. static char *dupvprintf_inner(char *buf, int oldlen, int *oldsize,
  388. const char *fmt, va_list ap)
  389. {
  390. int len, size, newsize;
  391. assert(*oldsize >= oldlen);
  392. size = *oldsize - oldlen;
  393. if (size == 0) {
  394. size = 512;
  395. newsize = oldlen + size;
  396. buf = sresize(buf, newsize, char);
  397. } else {
  398. newsize = *oldsize;
  399. }
  400. while (1) {
  401. #if defined _WINDOWS && !defined __WINE__ && _MSC_VER < 1900 /* 1900 == VS2015 has real snprintf */
  402. #define vsnprintf _vsnprintf
  403. #endif
  404. #ifdef va_copy
  405. /* Use the `va_copy' macro mandated by C99, if present.
  406. * XXX some environments may have this as __va_copy() */
  407. va_list aq;
  408. va_copy(aq, ap);
  409. len = vsnprintf(buf + oldlen, size, fmt, aq);
  410. va_end(aq);
  411. #else
  412. /* Ugh. No va_copy macro, so do something nasty.
  413. * Technically, you can't reuse a va_list like this: it is left
  414. * unspecified whether advancing a va_list pointer modifies its
  415. * value or something it points to, so on some platforms calling
  416. * vsnprintf twice on the same va_list might fail hideously
  417. * (indeed, it has been observed to).
  418. * XXX the autoconf manual suggests that using memcpy() will give
  419. * "maximum portability". */
  420. len = vsnprintf(buf + oldlen, size, fmt, ap);
  421. #endif
  422. if (len >= 0 && len < size) {
  423. /* This is the C99-specified criterion for snprintf to have
  424. * been completely successful. */
  425. *oldsize = newsize;
  426. return buf;
  427. } else if (len > 0) {
  428. /* This is the C99 error condition: the returned length is
  429. * the required buffer size not counting the NUL. */
  430. size = len + 1;
  431. } else {
  432. /* This is the pre-C99 glibc error condition: <0 means the
  433. * buffer wasn't big enough, so we enlarge it a bit and hope. */
  434. size += 512;
  435. }
  436. newsize = oldlen + size;
  437. buf = sresize(buf, newsize, char);
  438. }
  439. }
  440. char *dupvprintf(const char *fmt, va_list ap)
  441. {
  442. int size = 0;
  443. return dupvprintf_inner(NULL, 0, &size, fmt, ap);
  444. }
  445. char *dupprintf(const char *fmt, ...)
  446. {
  447. char *ret;
  448. va_list ap;
  449. va_start(ap, fmt);
  450. ret = dupvprintf(fmt, ap);
  451. va_end(ap);
  452. return ret;
  453. }
  454. struct strbuf_impl {
  455. int size;
  456. struct strbuf visible;
  457. };
  458. #define STRBUF_SET_PTR(buf, ptr) \
  459. ((buf)->visible.s = (ptr), \
  460. (buf)->visible.u = (unsigned char *)(buf)->visible.s)
  461. void *strbuf_append(strbuf *buf_o, size_t len)
  462. {
  463. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  464. char *toret;
  465. if (buf->size < buf->visible.len + len + 1) {
  466. buf->size = (buf->visible.len + len + 1) * 5 / 4 + 512;
  467. STRBUF_SET_PTR(buf, sresize(buf->visible.s, buf->size, char));
  468. }
  469. toret = buf->visible.s + buf->visible.len;
  470. buf->visible.len += len;
  471. buf->visible.s[buf->visible.len] = '\0';
  472. return toret;
  473. }
  474. static void strbuf_BinarySink_write(
  475. BinarySink *bs, const void *data, size_t len)
  476. {
  477. strbuf *buf_o = BinarySink_DOWNCAST(bs, strbuf);
  478. memcpy(strbuf_append(buf_o, len), data, len);
  479. }
  480. strbuf *strbuf_new(void)
  481. {
  482. struct strbuf_impl *buf = snew(struct strbuf_impl);
  483. BinarySink_INIT(&buf->visible, strbuf_BinarySink_write);
  484. buf->visible.len = 0;
  485. buf->size = 512;
  486. STRBUF_SET_PTR(buf, snewn(buf->size, char));
  487. *buf->visible.s = '\0';
  488. return &buf->visible;
  489. }
  490. void strbuf_free(strbuf *buf_o)
  491. {
  492. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  493. if (buf->visible.s) {
  494. smemclr(buf->visible.s, buf->size);
  495. sfree(buf->visible.s);
  496. }
  497. sfree(buf);
  498. }
  499. char *strbuf_to_str(strbuf *buf_o)
  500. {
  501. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  502. char *ret = buf->visible.s;
  503. sfree(buf);
  504. return ret;
  505. }
  506. void strbuf_catfv(strbuf *buf_o, const char *fmt, va_list ap)
  507. {
  508. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  509. STRBUF_SET_PTR(buf, dupvprintf_inner(buf->visible.s, buf->visible.len,
  510. &buf->size, fmt, ap));
  511. buf->visible.len += strlen(buf->visible.s + buf->visible.len);
  512. }
  513. void strbuf_catf(strbuf *buf_o, const char *fmt, ...)
  514. {
  515. va_list ap;
  516. va_start(ap, fmt);
  517. strbuf_catfv(buf_o, fmt, ap);
  518. va_end(ap);
  519. }
  520. strbuf *strbuf_new_for_agent_query(void)
  521. {
  522. strbuf *buf = strbuf_new();
  523. put_uint32(buf, 0); /* reserve space for length field */
  524. return buf;
  525. }
  526. void strbuf_finalise_agent_query(strbuf *buf_o)
  527. {
  528. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  529. assert(buf->visible.len >= 5);
  530. PUT_32BIT_MSB_FIRST(buf->visible.u, buf->visible.len - 4);
  531. }
  532. /*
  533. * Read an entire line of text from a file. Return a buffer
  534. * malloced to be as big as necessary (caller must free).
  535. */
  536. char *fgetline(FILE *fp)
  537. {
  538. char *ret = snewn(512, char);
  539. int size = 512, len = 0;
  540. while (fgets(ret + len, size - len, fp)) {
  541. len += strlen(ret + len);
  542. if (len > 0 && ret[len-1] == '\n')
  543. break; /* got a newline, we're done */
  544. size = len + 512;
  545. ret = sresize(ret, size, char);
  546. }
  547. if (len == 0) { /* first fgets returned NULL */
  548. sfree(ret);
  549. return NULL;
  550. }
  551. ret[len] = '\0';
  552. return ret;
  553. }
  554. /*
  555. * Perl-style 'chomp', for a line we just read with fgetline. Unlike
  556. * Perl chomp, however, we're deliberately forgiving of strange
  557. * line-ending conventions. Also we forgive NULL on input, so you can
  558. * just write 'line = chomp(fgetline(fp));' and not bother checking
  559. * for NULL until afterwards.
  560. */
  561. char *chomp(char *str)
  562. {
  563. if (str) {
  564. int len = strlen(str);
  565. while (len > 0 && (str[len-1] == '\r' || str[len-1] == '\n'))
  566. len--;
  567. str[len] = '\0';
  568. }
  569. return str;
  570. }
  571. /* ----------------------------------------------------------------------
  572. * Core base64 encoding and decoding routines.
  573. */
  574. void base64_encode_atom(const unsigned char *data, int n, char *out)
  575. {
  576. static const char base64_chars[] =
  577. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  578. unsigned word;
  579. word = data[0] << 16;
  580. if (n > 1)
  581. word |= data[1] << 8;
  582. if (n > 2)
  583. word |= data[2];
  584. out[0] = base64_chars[(word >> 18) & 0x3F];
  585. out[1] = base64_chars[(word >> 12) & 0x3F];
  586. if (n > 1)
  587. out[2] = base64_chars[(word >> 6) & 0x3F];
  588. else
  589. out[2] = '=';
  590. if (n > 2)
  591. out[3] = base64_chars[word & 0x3F];
  592. else
  593. out[3] = '=';
  594. }
  595. int base64_decode_atom(const char *atom, unsigned char *out)
  596. {
  597. int vals[4];
  598. int i, v, len;
  599. unsigned word;
  600. char c;
  601. for (i = 0; i < 4; i++) {
  602. c = atom[i];
  603. if (c >= 'A' && c <= 'Z')
  604. v = c - 'A';
  605. else if (c >= 'a' && c <= 'z')
  606. v = c - 'a' + 26;
  607. else if (c >= '0' && c <= '9')
  608. v = c - '0' + 52;
  609. else if (c == '+')
  610. v = 62;
  611. else if (c == '/')
  612. v = 63;
  613. else if (c == '=')
  614. v = -1;
  615. else
  616. return 0; /* invalid atom */
  617. vals[i] = v;
  618. }
  619. if (vals[0] == -1 || vals[1] == -1)
  620. return 0;
  621. if (vals[2] == -1 && vals[3] != -1)
  622. return 0;
  623. if (vals[3] != -1)
  624. len = 3;
  625. else if (vals[2] != -1)
  626. len = 2;
  627. else
  628. len = 1;
  629. word = ((vals[0] << 18) |
  630. (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));
  631. out[0] = (word >> 16) & 0xFF;
  632. if (len > 1)
  633. out[1] = (word >> 8) & 0xFF;
  634. if (len > 2)
  635. out[2] = word & 0xFF;
  636. return len;
  637. }
  638. /* ----------------------------------------------------------------------
  639. * Generic routines to deal with send buffers: a linked list of
  640. * smallish blocks, with the operations
  641. *
  642. * - add an arbitrary amount of data to the end of the list
  643. * - remove the first N bytes from the list
  644. * - return a (pointer,length) pair giving some initial data in
  645. * the list, suitable for passing to a send or write system
  646. * call
  647. * - retrieve a larger amount of initial data from the list
  648. * - return the current size of the buffer chain in bytes
  649. */
  650. #define BUFFER_MIN_GRANULE 512
  651. struct bufchain_granule {
  652. struct bufchain_granule *next;
  653. char *bufpos, *bufend, *bufmax;
  654. };
  655. void bufchain_init(bufchain *ch)
  656. {
  657. ch->head = ch->tail = NULL;
  658. ch->buffersize = 0;
  659. ch->ic = NULL;
  660. }
  661. void bufchain_clear(bufchain *ch)
  662. {
  663. struct bufchain_granule *b;
  664. while (ch->head) {
  665. b = ch->head;
  666. ch->head = ch->head->next;
  667. sfree(b);
  668. }
  669. ch->tail = NULL;
  670. ch->buffersize = 0;
  671. }
  672. int bufchain_size(bufchain *ch)
  673. {
  674. return ch->buffersize;
  675. }
  676. void bufchain_add(bufchain *ch, const void *data, int len)
  677. {
  678. const char *buf = (const char *)data;
  679. if (len == 0) return;
  680. ch->buffersize += len;
  681. while (len > 0) {
  682. if (ch->tail && ch->tail->bufend < ch->tail->bufmax) {
  683. int copylen = min(len, ch->tail->bufmax - ch->tail->bufend);
  684. memcpy(ch->tail->bufend, buf, copylen);
  685. buf += copylen;
  686. len -= copylen;
  687. ch->tail->bufend += copylen;
  688. }
  689. if (len > 0) {
  690. int grainlen =
  691. max(sizeof(struct bufchain_granule) + len, BUFFER_MIN_GRANULE);
  692. struct bufchain_granule *newbuf;
  693. newbuf = smalloc(grainlen);
  694. newbuf->bufpos = newbuf->bufend =
  695. (char *)newbuf + sizeof(struct bufchain_granule);
  696. newbuf->bufmax = (char *)newbuf + grainlen;
  697. newbuf->next = NULL;
  698. if (ch->tail)
  699. ch->tail->next = newbuf;
  700. else
  701. ch->head = newbuf;
  702. ch->tail = newbuf;
  703. }
  704. }
  705. if (ch->ic)
  706. queue_idempotent_callback(ch->ic);
  707. }
  708. void bufchain_consume(bufchain *ch, int len)
  709. {
  710. struct bufchain_granule *tmp;
  711. assert(ch->buffersize >= len);
  712. while (len > 0) {
  713. int remlen = len;
  714. assert(ch->head != NULL);
  715. if (remlen >= ch->head->bufend - ch->head->bufpos) {
  716. remlen = ch->head->bufend - ch->head->bufpos;
  717. tmp = ch->head;
  718. ch->head = tmp->next;
  719. if (!ch->head)
  720. ch->tail = NULL;
  721. sfree(tmp);
  722. } else
  723. ch->head->bufpos += remlen;
  724. ch->buffersize -= remlen;
  725. len -= remlen;
  726. }
  727. }
  728. void bufchain_prefix(bufchain *ch, void **data, int *len)
  729. {
  730. *len = ch->head->bufend - ch->head->bufpos;
  731. *data = ch->head->bufpos;
  732. }
  733. void bufchain_fetch(bufchain *ch, void *data, int len)
  734. {
  735. struct bufchain_granule *tmp;
  736. char *data_c = (char *)data;
  737. tmp = ch->head;
  738. assert(ch->buffersize >= len);
  739. while (len > 0) {
  740. int remlen = len;
  741. assert(tmp != NULL);
  742. if (remlen >= tmp->bufend - tmp->bufpos)
  743. remlen = tmp->bufend - tmp->bufpos;
  744. memcpy(data_c, tmp->bufpos, remlen);
  745. tmp = tmp->next;
  746. len -= remlen;
  747. data_c += remlen;
  748. }
  749. }
  750. void bufchain_fetch_consume(bufchain *ch, void *data, int len)
  751. {
  752. bufchain_fetch(ch, data, len);
  753. bufchain_consume(ch, len);
  754. }
  755. int bufchain_try_fetch_consume(bufchain *ch, void *data, int len)
  756. {
  757. if (ch->buffersize >= len) {
  758. bufchain_fetch_consume(ch, data, len);
  759. return TRUE;
  760. } else {
  761. return FALSE;
  762. }
  763. }
  764. /* ----------------------------------------------------------------------
  765. * Sanitise terminal output that we have reason not to trust, e.g.
  766. * because it appears in the login banner or password prompt from a
  767. * server, which we'd rather not permit to use arbitrary escape
  768. * sequences.
  769. */
  770. void sanitise_term_data(bufchain *out, const void *vdata, int len)
  771. {
  772. const char *data = (const char *)vdata;
  773. int i;
  774. /*
  775. * FIXME: this method of sanitisation is ASCII-centric. It would
  776. * be nice to permit SSH banners and the like to contain printable
  777. * Unicode, but that would need a lot more complicated code here
  778. * (not to mention knowing what character set it should interpret
  779. * the data as).
  780. */
  781. for (i = 0; i < len; i++) {
  782. if (data[i] == '\n')
  783. bufchain_add(out, "\r\n", 2);
  784. else if (data[i] >= ' ' && data[i] < 0x7F)
  785. bufchain_add(out, data + i, 1);
  786. }
  787. }
  788. /* ----------------------------------------------------------------------
  789. * My own versions of malloc, realloc and free. Because I want
  790. * malloc and realloc to bomb out and exit the program if they run
  791. * out of memory, realloc to reliably call malloc if passed a NULL
  792. * pointer, and free to reliably do nothing if passed a NULL
  793. * pointer. We can also put trace printouts in, if we need to; and
  794. * we can also replace the allocator with an ElectricFence-like
  795. * one.
  796. */
  797. #ifdef MINEFIELD
  798. void *minefield_c_malloc(size_t size);
  799. void minefield_c_free(void *p);
  800. void *minefield_c_realloc(void *p, size_t size);
  801. #endif
  802. #ifdef MALLOC_LOG
  803. static FILE *fp = NULL;
  804. static char *mlog_file = NULL;
  805. static int mlog_line = 0;
  806. void mlog(char *file, int line)
  807. {
  808. mlog_file = file;
  809. mlog_line = line;
  810. if (!fp) {
  811. fp = fopen("putty_mem.log", "w");
  812. setvbuf(fp, NULL, _IONBF, BUFSIZ);
  813. }
  814. if (fp)
  815. fprintf(fp, "%s:%d: ", file, line);
  816. }
  817. #endif
  818. void *safemalloc(size_t n, size_t size)
  819. {
  820. void *p;
  821. if (n > INT_MAX / size) {
  822. p = NULL;
  823. } else {
  824. size *= n;
  825. if (size == 0) size = 1;
  826. #ifdef MINEFIELD
  827. p = minefield_c_malloc(size);
  828. #else
  829. p = malloc(size);
  830. #endif
  831. }
  832. if (!p) {
  833. char str[200];
  834. #ifdef MALLOC_LOG
  835. sprintf(str, "Out of memory! (%s:%d, size=%d)",
  836. mlog_file, mlog_line, size);
  837. fprintf(fp, "*** %s\n", str);
  838. fclose(fp);
  839. #else
  840. strcpy(str, "Out of memory!");
  841. #endif
  842. modalfatalbox("%s", str);
  843. }
  844. #ifdef MALLOC_LOG
  845. if (fp)
  846. fprintf(fp, "malloc(%d) returns %p\n", size, p);
  847. #endif
  848. return p;
  849. }
  850. void *saferealloc(void *ptr, size_t n, size_t size)
  851. {
  852. void *p;
  853. if (n > INT_MAX / size) {
  854. p = NULL;
  855. } else {
  856. size *= n;
  857. if (!ptr) {
  858. #ifdef MINEFIELD
  859. p = minefield_c_malloc(size);
  860. #else
  861. p = malloc(size);
  862. #endif
  863. } else {
  864. #ifdef MINEFIELD
  865. p = minefield_c_realloc(ptr, size);
  866. #else
  867. p = realloc(ptr, size);
  868. #endif
  869. }
  870. }
  871. if (!p) {
  872. char str[200];
  873. #ifdef MALLOC_LOG
  874. sprintf(str, "Out of memory! (%s:%d, size=%d)",
  875. mlog_file, mlog_line, size);
  876. fprintf(fp, "*** %s\n", str);
  877. fclose(fp);
  878. #else
  879. strcpy(str, "Out of memory!");
  880. #endif
  881. modalfatalbox("%s", str);
  882. }
  883. #ifdef MALLOC_LOG
  884. if (fp)
  885. fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
  886. #endif
  887. return p;
  888. }
  889. void safefree(void *ptr)
  890. {
  891. if (ptr) {
  892. #ifdef MALLOC_LOG
  893. if (fp)
  894. fprintf(fp, "free(%p)\n", ptr);
  895. #endif
  896. #ifdef MINEFIELD
  897. minefield_c_free(ptr);
  898. #else
  899. free(ptr);
  900. #endif
  901. }
  902. #ifdef MALLOC_LOG
  903. else if (fp)
  904. fprintf(fp, "freeing null pointer - no action taken\n");
  905. #endif
  906. }
  907. /* ----------------------------------------------------------------------
  908. * Debugging routines.
  909. */
  910. #ifdef DEBUG
  911. extern void dputs(const char *); /* defined in per-platform *misc.c */
  912. void debug_printf(const char *fmt, ...)
  913. {
  914. char *buf;
  915. va_list ap;
  916. va_start(ap, fmt);
  917. buf = dupvprintf(fmt, ap);
  918. dputs(buf);
  919. sfree(buf);
  920. va_end(ap);
  921. }
  922. void debug_memdump(const void *buf, int len, int L)
  923. {
  924. int i;
  925. const unsigned char *p = buf;
  926. char foo[17];
  927. if (L) {
  928. int delta;
  929. debug_printf("\t%d (0x%x) bytes:\n", len, len);
  930. delta = 15 & (uintptr_t)p;
  931. p -= delta;
  932. len += delta;
  933. }
  934. for (; 0 < len; p += 16, len -= 16) {
  935. dputs(" ");
  936. if (L)
  937. debug_printf("%p: ", p);
  938. strcpy(foo, "................"); /* sixteen dots */
  939. for (i = 0; i < 16 && i < len; ++i) {
  940. if (&p[i] < (unsigned char *) buf) {
  941. dputs(" "); /* 3 spaces */
  942. foo[i] = ' ';
  943. } else {
  944. debug_printf("%c%02.2x",
  945. &p[i] != (unsigned char *) buf
  946. && i % 4 ? '.' : ' ', p[i]
  947. );
  948. if (p[i] >= ' ' && p[i] <= '~')
  949. foo[i] = (char) p[i];
  950. }
  951. }
  952. foo[i] = '\0';
  953. debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
  954. }
  955. }
  956. #endif /* def DEBUG */
  957. /*
  958. * Determine whether or not a Conf represents a session which can
  959. * sensibly be launched right now.
  960. */
  961. int conf_launchable(Conf *conf)
  962. {
  963. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  964. return conf_get_str(conf, CONF_serline)[0] != 0;
  965. else
  966. return conf_get_str(conf, CONF_host)[0] != 0;
  967. }
  968. char const *conf_dest(Conf *conf)
  969. {
  970. if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
  971. return conf_get_str(conf, CONF_serline);
  972. else
  973. return conf_get_str(conf, CONF_host);
  974. }
  975. #ifndef PLATFORM_HAS_SMEMCLR
  976. /*
  977. * Securely wipe memory.
  978. *
  979. * The actual wiping is no different from what memset would do: the
  980. * point of 'securely' is to try to be sure over-clever compilers
  981. * won't optimise away memsets on variables that are about to be freed
  982. * or go out of scope. See
  983. * https://buildsecurityin.us-cert.gov/bsi-rules/home/g1/771-BSI.html
  984. *
  985. * Some platforms (e.g. Windows) may provide their own version of this
  986. * function.
  987. */
  988. void smemclr(void *b, size_t n) {
  989. volatile char *vp;
  990. if (b && n > 0) {
  991. /*
  992. * Zero out the memory.
  993. */
  994. memset(b, 0, n);
  995. /*
  996. * Perform a volatile access to the object, forcing the
  997. * compiler to admit that the previous memset was important.
  998. *
  999. * This while loop should in practice run for zero iterations
  1000. * (since we know we just zeroed the object out), but in
  1001. * theory (as far as the compiler knows) it might range over
  1002. * the whole object. (If we had just written, say, '*vp =
  1003. * *vp;', a compiler could in principle have 'helpfully'
  1004. * optimised the memset into only zeroing out the first byte.
  1005. * This should be robust.)
  1006. */
  1007. vp = b;
  1008. while (*vp) vp++;
  1009. }
  1010. }
  1011. #endif
  1012. /*
  1013. * Validate a manual host key specification (either entered in the
  1014. * GUI, or via -hostkey). If valid, we return TRUE, and update 'key'
  1015. * to contain a canonicalised version of the key string in 'key'
  1016. * (which is guaranteed to take up at most as much space as the
  1017. * original version), suitable for putting into the Conf. If not
  1018. * valid, we return FALSE.
  1019. */
  1020. int validate_manual_hostkey(char *key)
  1021. {
  1022. char *p, *q, *r, *s;
  1023. /*
  1024. * Step through the string word by word, looking for a word that's
  1025. * in one of the formats we like.
  1026. */
  1027. p = key;
  1028. while ((p += strspn(p, " \t"))[0]) {
  1029. q = p;
  1030. p += strcspn(p, " \t");
  1031. if (*p) *p++ = '\0';
  1032. /*
  1033. * Now q is our word.
  1034. */
  1035. if (strlen(q) == 16*3 - 1 &&
  1036. q[strspn(q, "0123456789abcdefABCDEF:")] == 0) {
  1037. /*
  1038. * Might be a key fingerprint. Check the colons are in the
  1039. * right places, and if so, return the same fingerprint
  1040. * canonicalised into lowercase.
  1041. */
  1042. int i;
  1043. for (i = 0; i < 16; i++)
  1044. if (q[3*i] == ':' || q[3*i+1] == ':')
  1045. goto not_fingerprint; /* sorry */
  1046. for (i = 0; i < 15; i++)
  1047. if (q[3*i+2] != ':')
  1048. goto not_fingerprint; /* sorry */
  1049. for (i = 0; i < 16*3 - 1; i++)
  1050. key[i] = tolower(q[i]);
  1051. key[16*3 - 1] = '\0';
  1052. return TRUE;
  1053. }
  1054. not_fingerprint:;
  1055. /*
  1056. * Before we check for a public-key blob, trim newlines out of
  1057. * the middle of the word, in case someone's managed to paste
  1058. * in a public-key blob _with_ them.
  1059. */
  1060. for (r = s = q; *r; r++)
  1061. if (*r != '\n' && *r != '\r')
  1062. *s++ = *r;
  1063. *s = '\0';
  1064. if (strlen(q) % 4 == 0 && strlen(q) > 2*4 &&
  1065. q[strspn(q, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1066. "abcdefghijklmnopqrstuvwxyz+/=")] == 0) {
  1067. /*
  1068. * Might be a base64-encoded SSH-2 public key blob. Check
  1069. * that it starts with a sensible algorithm string. No
  1070. * canonicalisation is necessary for this string type.
  1071. *
  1072. * The algorithm string must be at most 64 characters long
  1073. * (RFC 4251 section 6).
  1074. */
  1075. unsigned char decoded[6];
  1076. unsigned alglen;
  1077. int minlen;
  1078. int len = 0;
  1079. len += base64_decode_atom(q, decoded+len);
  1080. if (len < 3)
  1081. goto not_ssh2_blob; /* sorry */
  1082. len += base64_decode_atom(q+4, decoded+len);
  1083. if (len < 4)
  1084. goto not_ssh2_blob; /* sorry */
  1085. alglen = GET_32BIT_MSB_FIRST(decoded);
  1086. if (alglen > 64)
  1087. goto not_ssh2_blob; /* sorry */
  1088. minlen = ((alglen + 4) + 2) / 3;
  1089. if (strlen(q) < minlen)
  1090. goto not_ssh2_blob; /* sorry */
  1091. strcpy(key, q);
  1092. return TRUE;
  1093. }
  1094. not_ssh2_blob:;
  1095. }
  1096. return FALSE;
  1097. }
  1098. int smemeq(const void *av, const void *bv, size_t len)
  1099. {
  1100. const unsigned char *a = (const unsigned char *)av;
  1101. const unsigned char *b = (const unsigned char *)bv;
  1102. unsigned val = 0;
  1103. while (len-- > 0) {
  1104. val |= *a++ ^ *b++;
  1105. }
  1106. /* Now val is 0 iff we want to return 1, and in the range
  1107. * 0x01..0xFF iff we want to return 0. So subtracting from 0x100
  1108. * will clear bit 8 iff we want to return 0, and leave it set iff
  1109. * we want to return 1, so then we can just shift down. */
  1110. return (0x100 - val) >> 8;
  1111. }
  1112. int nullstrcmp(const char *a, const char *b)
  1113. {
  1114. if (a == NULL && b == NULL)
  1115. return 0;
  1116. if (a == NULL)
  1117. return -1;
  1118. if (b == NULL)
  1119. return +1;
  1120. return strcmp(a, b);
  1121. }
  1122. ptrlen make_ptrlen(const void *ptr, size_t len)
  1123. {
  1124. ptrlen pl;
  1125. pl.ptr = ptr;
  1126. pl.len = len;
  1127. return pl;
  1128. }
  1129. int ptrlen_eq_string(ptrlen pl, const char *str)
  1130. {
  1131. size_t len = strlen(str);
  1132. return (pl.len == len && !memcmp(pl.ptr, str, len));
  1133. }
  1134. char *mkstr(ptrlen pl)
  1135. {
  1136. char *p = snewn(pl.len + 1, char);
  1137. memcpy(p, pl.ptr, pl.len);
  1138. p[pl.len] = '\0';
  1139. return p;
  1140. }
  1141. int strstartswith(const char *s, const char *t)
  1142. {
  1143. return !memcmp(s, t, strlen(t));
  1144. }
  1145. int strendswith(const char *s, const char *t)
  1146. {
  1147. size_t slen = strlen(s), tlen = strlen(t);
  1148. return slen >= tlen && !strcmp(s + (slen - tlen), t);
  1149. }
  1150. char *buildinfo(const char *newline)
  1151. {
  1152. strbuf *buf = strbuf_new();
  1153. extern const char commitid[]; /* in commitid.c */
  1154. strbuf_catf(buf, "Build platform: %d-bit %s",
  1155. (int)(CHAR_BIT * sizeof(void *)),
  1156. BUILDINFO_PLATFORM);
  1157. #ifdef __clang_version__
  1158. #define FOUND_COMPILER
  1159. strbuf_catf(buf, "%sCompiler: clang %s", newline, __clang_version__);
  1160. #elif defined __GNUC__ && defined __VERSION__
  1161. #define FOUND_COMPILER
  1162. strbuf_catf(buf, "%sCompiler: gcc %s", newline, __VERSION__);
  1163. #endif
  1164. #if defined _MSC_VER
  1165. #ifndef FOUND_COMPILER
  1166. #define FOUND_COMPILER
  1167. strbuf_catf(buf, "%sCompiler: ", newline);
  1168. #else
  1169. strbuf_catf(buf, ", emulating ");
  1170. #endif
  1171. strbuf_catf(buf, "Visual Studio", newline);
  1172. #if _MSC_VER == 1900
  1173. strbuf_catf(buf, " 2015 / MSVC++ 14.0");
  1174. #elif _MSC_VER == 1912
  1175. strbuf_catf(buf, " 2017 / MSVC++ 14.12");
  1176. #elif _MSC_VER == 1800
  1177. strbuf_catf(buf, " 2013 / MSVC++ 12.0");
  1178. #elif _MSC_VER == 1700
  1179. strbuf_catf(buf, " 2012 / MSVC++ 11.0");
  1180. #elif _MSC_VER == 1600
  1181. strbuf_catf(buf, " 2010 / MSVC++ 10.0");
  1182. #elif _MSC_VER == 1500
  1183. strbuf_catf(buf, " 2008 / MSVC++ 9.0");
  1184. #elif _MSC_VER == 1400
  1185. strbuf_catf(buf, " 2005 / MSVC++ 8.0");
  1186. #elif _MSC_VER == 1310
  1187. strbuf_catf(buf, " 2003 / MSVC++ 7.1");
  1188. #elif _MSC_VER == 1300
  1189. strbuf_catf(buf, " 2003 / MSVC++ 7.0");
  1190. #else
  1191. strbuf_catf(buf, ", unrecognised version");
  1192. #endif
  1193. strbuf_catf(buf, " (_MSC_VER=%d)", (int)_MSC_VER);
  1194. #endif
  1195. #ifdef BUILDINFO_GTK
  1196. {
  1197. char *gtk_buildinfo = buildinfo_gtk_version();
  1198. if (gtk_buildinfo) {
  1199. strbuf_catf(buf, "%sCompiled against GTK version %s",
  1200. newline, gtk_buildinfo);
  1201. sfree(gtk_buildinfo);
  1202. }
  1203. }
  1204. #endif
  1205. #if defined _WINDOWS && defined MINEFIELD
  1206. strbuf_catf(buf, "%sBuild option: MINEFIELD", newline);
  1207. #endif
  1208. #ifdef NO_SECURITY
  1209. strbuf_catf(buf, "%sBuild option: NO_SECURITY", newline);
  1210. #endif
  1211. #ifdef NO_SECUREZEROMEMORY
  1212. strbuf_catf(buf, "%sBuild option: NO_SECUREZEROMEMORY", newline);
  1213. #endif
  1214. #ifdef NO_IPV6
  1215. strbuf_catf(buf, "%sBuild option: NO_IPV6", newline);
  1216. #endif
  1217. #ifdef NO_GSSAPI
  1218. strbuf_catf(buf, "%sBuild option: NO_GSSAPI", newline);
  1219. #endif
  1220. #ifdef STATIC_GSSAPI
  1221. strbuf_catf(buf, "%sBuild option: STATIC_GSSAPI", newline);
  1222. #endif
  1223. #ifdef UNPROTECT
  1224. strbuf_catf(buf, "%sBuild option: UNPROTECT", newline);
  1225. #endif
  1226. #ifdef FUZZING
  1227. strbuf_catf(buf, "%sBuild option: FUZZING", newline);
  1228. #endif
  1229. #ifdef DEBUG
  1230. strbuf_catf(buf, "%sBuild option: DEBUG", newline);
  1231. #endif
  1232. strbuf_catf(buf, "%sSource commit: %s", newline, commitid);
  1233. return strbuf_to_str(buf);
  1234. }