storage.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*
  2. * storage.c: Unix-specific implementation of the interface defined
  3. * in storage.h.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <ctype.h>
  11. #include <limits.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <dirent.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <pwd.h>
  18. #include "putty.h"
  19. #include "storage.h"
  20. #include "tree234.h"
  21. #ifdef PATH_MAX
  22. #define FNLEN PATH_MAX
  23. #else
  24. #define FNLEN 1024 /* XXX */
  25. #endif
  26. enum {
  27. INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED,
  28. INDEX_SESSIONDIR, INDEX_SESSION, INDEX_HOSTCADIR, INDEX_HOSTCA
  29. };
  30. static const char hex[16] = "0123456789ABCDEF";
  31. static void make_session_filename(const char *in, strbuf *out)
  32. {
  33. if (!in || !*in)
  34. in = "Default Settings";
  35. while (*in) {
  36. /*
  37. * There are remarkably few punctuation characters that
  38. * aren't shell-special in some way or likely to be used as
  39. * separators in some file format or another! Hence we use
  40. * opt-in for safe characters rather than opt-out for
  41. * specific unsafe ones...
  42. */
  43. if (*in!='+' && *in!='-' && *in!='.' && *in!='@' && *in!='_' &&
  44. !(*in >= '0' && *in <= '9') &&
  45. !(*in >= 'A' && *in <= 'Z') &&
  46. !(*in >= 'a' && *in <= 'z')) {
  47. put_byte(out, '%');
  48. put_byte(out, hex[((unsigned char) *in) >> 4]);
  49. put_byte(out, hex[((unsigned char) *in) & 15]);
  50. } else
  51. put_byte(out, *in);
  52. in++;
  53. }
  54. }
  55. static void decode_session_filename(const char *in, strbuf *out)
  56. {
  57. while (*in) {
  58. if (*in == '%' && in[1] && in[2]) {
  59. int i, j;
  60. i = in[1] - '0';
  61. i -= (i > 9 ? 7 : 0);
  62. j = in[2] - '0';
  63. j -= (j > 9 ? 7 : 0);
  64. put_byte(out, (i << 4) + j);
  65. in += 3;
  66. } else {
  67. put_byte(out, *in++);
  68. }
  69. }
  70. }
  71. static char *make_filename(int index, const char *subname)
  72. {
  73. char *env, *tmp, *ret;
  74. /*
  75. * Allow override of the PuTTY configuration location, and of
  76. * specific subparts of it, by means of environment variables.
  77. */
  78. if (index == INDEX_DIR) {
  79. struct passwd *pwd;
  80. char *xdg_dir, *old_dir, *old_dir2, *old_dir3, *home, *pwd_home;
  81. env = getenv("PUTTYDIR");
  82. if (env)
  83. return dupstr(env);
  84. home = getenv("HOME");
  85. pwd = getpwuid(getuid());
  86. if (pwd && pwd->pw_dir) {
  87. pwd_home = pwd->pw_dir;
  88. } else {
  89. pwd_home = NULL;
  90. }
  91. xdg_dir = NULL;
  92. env = getenv("XDG_CONFIG_HOME");
  93. if (env && *env) {
  94. xdg_dir = dupprintf("%s/putty", env);
  95. }
  96. if (!xdg_dir) {
  97. if (home) {
  98. tmp = home;
  99. } else if (pwd_home) {
  100. tmp = pwd_home;
  101. } else {
  102. tmp = "";
  103. }
  104. xdg_dir = dupprintf("%s/.config/putty", tmp);
  105. }
  106. if (xdg_dir && access(xdg_dir, F_OK) == 0) {
  107. return xdg_dir;
  108. }
  109. old_dir = old_dir2 = old_dir3 = NULL;
  110. if (home) {
  111. old_dir = dupprintf("%s/.putty", home);
  112. }
  113. if (pwd_home) {
  114. old_dir2 = dupprintf("%s/.putty", pwd_home);
  115. }
  116. old_dir3 = dupstr("/.putty");
  117. if (old_dir && access(old_dir, F_OK) == 0) {
  118. ret = old_dir;
  119. goto out;
  120. }
  121. if (old_dir2 && access(old_dir2, F_OK) == 0) {
  122. ret = old_dir2;
  123. goto out;
  124. }
  125. if (access(old_dir3, F_OK) == 0) {
  126. ret = old_dir3;
  127. goto out;
  128. }
  129. #ifdef XDG_DEFAULT
  130. if (xdg_dir) {
  131. ret = xdg_dir;
  132. goto out;
  133. }
  134. #endif
  135. ret = old_dir ? old_dir : (old_dir2 ? old_dir2 : old_dir3);
  136. out:
  137. if (ret != old_dir)
  138. sfree(old_dir);
  139. if (ret != old_dir2)
  140. sfree(old_dir2);
  141. if (ret != old_dir3)
  142. sfree(old_dir3);
  143. if (ret != xdg_dir)
  144. sfree(xdg_dir);
  145. return ret;
  146. }
  147. if (index == INDEX_SESSIONDIR) {
  148. env = getenv("PUTTYSESSIONS");
  149. if (env)
  150. return dupstr(env);
  151. tmp = make_filename(INDEX_DIR, NULL);
  152. ret = dupprintf("%s/sessions", tmp);
  153. sfree(tmp);
  154. return ret;
  155. }
  156. if (index == INDEX_SESSION) {
  157. strbuf *sb = strbuf_new();
  158. tmp = make_filename(INDEX_SESSIONDIR, NULL);
  159. put_fmt(sb, "%s/", tmp);
  160. sfree(tmp);
  161. make_session_filename(subname, sb);
  162. return strbuf_to_str(sb);
  163. }
  164. if (index == INDEX_HOSTKEYS) {
  165. env = getenv("PUTTYSSHHOSTKEYS");
  166. if (env)
  167. return dupstr(env);
  168. tmp = make_filename(INDEX_DIR, NULL);
  169. ret = dupprintf("%s/sshhostkeys", tmp);
  170. sfree(tmp);
  171. return ret;
  172. }
  173. if (index == INDEX_HOSTKEYS_TMP) {
  174. tmp = make_filename(INDEX_HOSTKEYS, NULL);
  175. ret = dupprintf("%s.tmp", tmp);
  176. sfree(tmp);
  177. return ret;
  178. }
  179. if (index == INDEX_RANDSEED) {
  180. env = getenv("PUTTYRANDOMSEED");
  181. if (env)
  182. return dupstr(env);
  183. tmp = make_filename(INDEX_DIR, NULL);
  184. ret = dupprintf("%s/randomseed", tmp);
  185. sfree(tmp);
  186. return ret;
  187. }
  188. if (index == INDEX_HOSTCADIR) {
  189. env = getenv("PUTTYSSHHOSTCAS");
  190. if (env)
  191. return dupstr(env);
  192. tmp = make_filename(INDEX_DIR, NULL);
  193. ret = dupprintf("%s/sshhostcas", tmp);
  194. sfree(tmp);
  195. return ret;
  196. }
  197. if (index == INDEX_HOSTCA) {
  198. strbuf *sb = strbuf_new();
  199. tmp = make_filename(INDEX_HOSTCADIR, NULL);
  200. put_fmt(sb, "%s/", tmp);
  201. sfree(tmp);
  202. make_session_filename(subname, sb);
  203. return strbuf_to_str(sb);
  204. }
  205. tmp = make_filename(INDEX_DIR, NULL);
  206. ret = dupprintf("%s/ERROR", tmp);
  207. sfree(tmp);
  208. return ret;
  209. }
  210. struct settings_w {
  211. FILE *fp;
  212. };
  213. settings_w *open_settings_w(const char *sessionname, char **errmsg)
  214. {
  215. char *filename, *err;
  216. FILE *fp;
  217. *errmsg = NULL;
  218. /*
  219. * Start by making sure the .putty directory and its sessions
  220. * subdir actually exist.
  221. */
  222. filename = make_filename(INDEX_DIR, NULL);
  223. if ((err = make_dir_path(filename, 0700)) != NULL) {
  224. *errmsg = dupprintf("Unable to save session: %s", err);
  225. sfree(err);
  226. sfree(filename);
  227. return NULL;
  228. }
  229. sfree(filename);
  230. filename = make_filename(INDEX_SESSIONDIR, NULL);
  231. if ((err = make_dir_path(filename, 0700)) != NULL) {
  232. *errmsg = dupprintf("Unable to save session: %s", err);
  233. sfree(err);
  234. sfree(filename);
  235. return NULL;
  236. }
  237. sfree(filename);
  238. filename = make_filename(INDEX_SESSION, sessionname);
  239. fp = fopen(filename, "w");
  240. if (!fp) {
  241. *errmsg = dupprintf("Unable to save session: open(\"%s\") "
  242. "returned '%s'", filename, strerror(errno));
  243. sfree(filename);
  244. return NULL; /* can't open */
  245. }
  246. sfree(filename);
  247. settings_w *toret = snew(settings_w);
  248. toret->fp = fp;
  249. return toret;
  250. }
  251. void write_setting_s(settings_w *handle, const char *key, const char *value)
  252. {
  253. fprintf(handle->fp, "%s=%s\n", key, value);
  254. }
  255. void write_setting_i(settings_w *handle, const char *key, int value)
  256. {
  257. fprintf(handle->fp, "%s=%d\n", key, value);
  258. }
  259. void close_settings_w(settings_w *handle)
  260. {
  261. fclose(handle->fp);
  262. sfree(handle);
  263. }
  264. /* ----------------------------------------------------------------------
  265. * System for treating X resources as a fallback source of defaults,
  266. * after data read from a saved-session disk file.
  267. *
  268. * The read_setting_* functions will call get_setting(key) as a
  269. * fallback if the setting isn't in the file they loaded. That in turn
  270. * will hand on to x_get_default, which the front end application
  271. * provides, and which actually reads resources from the X server (if
  272. * appropriate). In between, there's a tree234 of X-resource shaped
  273. * settings living locally in this file: the front end can call
  274. * provide_xrm_string() to insert a setting into this tree (typically
  275. * in response to an -xrm command line option or similar), and those
  276. * will override the actual X resources.
  277. */
  278. struct skeyval {
  279. const char *key;
  280. const char *value;
  281. };
  282. static tree234 *xrmtree = NULL;
  283. static int keycmp(void *av, void *bv)
  284. {
  285. struct skeyval *a = (struct skeyval *)av;
  286. struct skeyval *b = (struct skeyval *)bv;
  287. return strcmp(a->key, b->key);
  288. }
  289. void provide_xrm_string(const char *string, const char *progname)
  290. {
  291. const char *p, *q;
  292. struct skeyval *xrms, *ret;
  293. p = q = strchr(string, ':');
  294. if (!q) {
  295. fprintf(stderr, "%s: expected a colon in resource string"
  296. " \"%s\"\n", progname, string);
  297. return;
  298. }
  299. xrms = snew(struct skeyval);
  300. while (p > string && p[-1] != '.' && p[-1] != '*')
  301. p--;
  302. xrms->key = mkstr(make_ptrlen(p, q-p));
  303. q++;
  304. while (*q && isspace((unsigned char)*q))
  305. q++;
  306. xrms->value = dupstr(q);
  307. if (!xrmtree)
  308. xrmtree = newtree234(keycmp);
  309. ret = add234(xrmtree, xrms);
  310. if (ret) {
  311. /* Override an existing string. */
  312. del234(xrmtree, ret);
  313. add234(xrmtree, xrms);
  314. }
  315. }
  316. static const char *get_setting(const char *key)
  317. {
  318. struct skeyval tmp, *ret;
  319. tmp.key = key;
  320. if (xrmtree) {
  321. ret = find234(xrmtree, &tmp, NULL);
  322. if (ret)
  323. return ret->value;
  324. }
  325. return x_get_default(key);
  326. }
  327. /* ----------------------------------------------------------------------
  328. * Main code for reading settings from a disk file, calling the above
  329. * get_setting() as a fallback if necessary.
  330. */
  331. struct settings_r {
  332. tree234 *t;
  333. };
  334. settings_r *open_settings_r(const char *sessionname)
  335. {
  336. char *filename;
  337. FILE *fp;
  338. char *line;
  339. settings_r *toret;
  340. filename = make_filename(INDEX_SESSION, sessionname);
  341. fp = fopen(filename, "r");
  342. sfree(filename);
  343. if (!fp)
  344. return NULL; /* can't open */
  345. toret = snew(settings_r);
  346. toret->t = newtree234(keycmp);
  347. while ( (line = fgetline(fp)) ) {
  348. char *value = strchr(line, '=');
  349. struct skeyval *kv;
  350. if (!value) {
  351. sfree(line);
  352. continue;
  353. }
  354. *value++ = '\0';
  355. value[strcspn(value, "\r\n")] = '\0'; /* trim trailing NL */
  356. kv = snew(struct skeyval);
  357. kv->key = dupstr(line);
  358. kv->value = dupstr(value);
  359. add234(toret->t, kv);
  360. sfree(line);
  361. }
  362. fclose(fp);
  363. return toret;
  364. }
  365. char *read_setting_s(settings_r *handle, const char *key)
  366. {
  367. const char *val;
  368. struct skeyval tmp, *kv;
  369. tmp.key = key;
  370. if (handle != NULL &&
  371. (kv = find234(handle->t, &tmp, NULL)) != NULL) {
  372. val = kv->value;
  373. assert(val != NULL);
  374. } else
  375. val = get_setting(key);
  376. if (!val)
  377. return NULL;
  378. else
  379. return dupstr(val);
  380. }
  381. int read_setting_i(settings_r *handle, const char *key, int defvalue)
  382. {
  383. const char *val;
  384. struct skeyval tmp, *kv;
  385. tmp.key = key;
  386. if (handle != NULL &&
  387. (kv = find234(handle->t, &tmp, NULL)) != NULL) {
  388. val = kv->value;
  389. assert(val != NULL);
  390. } else
  391. val = get_setting(key);
  392. if (!val)
  393. return defvalue;
  394. else
  395. return atoi(val);
  396. }
  397. FontSpec *read_setting_fontspec(settings_r *handle, const char *name)
  398. {
  399. /*
  400. * In GTK1-only PuTTY, we used to store font names simply as a
  401. * valid X font description string (logical or alias), under a
  402. * bare key such as "Font".
  403. *
  404. * In GTK2 PuTTY, we have a prefix system where "client:"
  405. * indicates a Pango font and "server:" an X one; existing
  406. * configuration needs to be reinterpreted as having the
  407. * "server:" prefix, so we change the storage key from the
  408. * provided name string (e.g. "Font") to a suffixed one
  409. * ("FontName").
  410. */
  411. char *suffname = dupcat(name, "Name");
  412. char *tmp;
  413. if ((tmp = read_setting_s(handle, suffname)) != NULL) {
  414. FontSpec *fs = fontspec_new(tmp);
  415. sfree(suffname);
  416. sfree(tmp);
  417. return fs; /* got new-style name */
  418. }
  419. sfree(suffname);
  420. /* Fall back to old-style name. */
  421. tmp = read_setting_s(handle, name);
  422. if (tmp && *tmp) {
  423. char *tmp2 = dupcat("server:", tmp);
  424. FontSpec *fs = fontspec_new(tmp2);
  425. sfree(tmp2);
  426. sfree(tmp);
  427. return fs;
  428. } else {
  429. sfree(tmp);
  430. return NULL;
  431. }
  432. }
  433. Filename *read_setting_filename(settings_r *handle, const char *name)
  434. {
  435. char *tmp = read_setting_s(handle, name);
  436. if (tmp) {
  437. Filename *ret = filename_from_str(tmp);
  438. sfree(tmp);
  439. return ret;
  440. } else
  441. return NULL;
  442. }
  443. void write_setting_fontspec(settings_w *handle, const char *name, FontSpec *fs)
  444. {
  445. /*
  446. * read_setting_fontspec had to handle two cases, but when
  447. * writing our settings back out we simply always generate the
  448. * new-style name.
  449. */
  450. char *suffname = dupcat(name, "Name");
  451. write_setting_s(handle, suffname, fs->name);
  452. sfree(suffname);
  453. }
  454. void write_setting_filename(settings_w *handle,
  455. const char *name, Filename *result)
  456. {
  457. write_setting_s(handle, name, result->path);
  458. }
  459. void close_settings_r(settings_r *handle)
  460. {
  461. struct skeyval *kv;
  462. if (!handle)
  463. return;
  464. while ( (kv = index234(handle->t, 0)) != NULL) {
  465. del234(handle->t, kv);
  466. sfree((char *)kv->key);
  467. sfree((char *)kv->value);
  468. sfree(kv);
  469. }
  470. freetree234(handle->t);
  471. sfree(handle);
  472. }
  473. void del_settings(const char *sessionname)
  474. {
  475. char *filename;
  476. filename = make_filename(INDEX_SESSION, sessionname);
  477. unlink(filename);
  478. sfree(filename);
  479. }
  480. struct settings_e {
  481. DIR *dp;
  482. };
  483. settings_e *enum_settings_start(void)
  484. {
  485. DIR *dp;
  486. char *filename;
  487. filename = make_filename(INDEX_SESSIONDIR, NULL);
  488. dp = opendir(filename);
  489. sfree(filename);
  490. settings_e *toret = snew(settings_e);
  491. toret->dp = dp;
  492. return toret;
  493. }
  494. static bool enum_dir_next(DIR *dp, int index, strbuf *out)
  495. {
  496. struct dirent *de;
  497. struct stat st;
  498. strbuf *fullpath;
  499. if (!dp)
  500. return false;
  501. fullpath = strbuf_new();
  502. char *sessiondir = make_filename(index, NULL);
  503. put_dataz(fullpath, sessiondir);
  504. sfree(sessiondir);
  505. put_byte(fullpath, '/');
  506. size_t baselen = fullpath->len;
  507. while ( (de = readdir(dp)) != NULL ) {
  508. strbuf_shrink_to(fullpath, baselen);
  509. put_dataz(fullpath, de->d_name);
  510. if (stat(fullpath->s, &st) < 0 || !S_ISREG(st.st_mode))
  511. continue; /* try another one */
  512. decode_session_filename(de->d_name, out);
  513. strbuf_free(fullpath);
  514. return true;
  515. }
  516. strbuf_free(fullpath);
  517. return false;
  518. }
  519. bool enum_settings_next(settings_e *handle, strbuf *out)
  520. {
  521. return enum_dir_next(handle->dp, INDEX_SESSIONDIR, out);
  522. }
  523. void enum_settings_finish(settings_e *handle)
  524. {
  525. if (handle->dp)
  526. closedir(handle->dp);
  527. sfree(handle);
  528. }
  529. struct host_ca_enum {
  530. DIR *dp;
  531. };
  532. host_ca_enum *enum_host_ca_start(void)
  533. {
  534. host_ca_enum *handle = snew(host_ca_enum);
  535. char *filename = make_filename(INDEX_HOSTCADIR, NULL);
  536. handle->dp = opendir(filename);
  537. sfree(filename);
  538. return handle;
  539. }
  540. bool enum_host_ca_next(host_ca_enum *handle, strbuf *out)
  541. {
  542. return enum_dir_next(handle->dp, INDEX_HOSTCADIR, out);
  543. }
  544. void enum_host_ca_finish(host_ca_enum *handle)
  545. {
  546. if (handle->dp)
  547. closedir(handle->dp);
  548. sfree(handle);
  549. }
  550. host_ca *host_ca_load(const char *name)
  551. {
  552. char *filename = make_filename(INDEX_HOSTCA, name);
  553. FILE *fp = fopen(filename, "r");
  554. sfree(filename);
  555. if (!fp)
  556. return NULL;
  557. host_ca *hca = host_ca_new();
  558. hca->name = dupstr(name);
  559. char *line;
  560. CertExprBuilder *eb = NULL;
  561. while ( (line = fgetline(fp)) ) {
  562. char *value = strchr(line, '=');
  563. if (!value) {
  564. sfree(line);
  565. continue;
  566. }
  567. *value++ = '\0';
  568. value[strcspn(value, "\r\n")] = '\0'; /* trim trailing NL */
  569. if (!strcmp(line, "PublicKey")) {
  570. hca->ca_public_key = base64_decode_sb(ptrlen_from_asciz(value));
  571. } else if (!strcmp(line, "MatchHosts")) {
  572. if (!eb)
  573. eb = cert_expr_builder_new();
  574. cert_expr_builder_add(eb, value);
  575. } else if (!strcmp(line, "Validity")) {
  576. hca->validity_expression = strbuf_to_str(
  577. percent_decode_sb(ptrlen_from_asciz(value)));
  578. } else if (!strcmp(line, "PermitRSASHA1")) {
  579. hca->opts.permit_rsa_sha1 = atoi(value);
  580. } else if (!strcmp(line, "PermitRSASHA256")) {
  581. hca->opts.permit_rsa_sha256 = atoi(value);
  582. } else if (!strcmp(line, "PermitRSASHA512")) {
  583. hca->opts.permit_rsa_sha512 = atoi(value);
  584. }
  585. sfree(line);
  586. }
  587. fclose(fp);
  588. if (eb) {
  589. if (!hca->validity_expression) {
  590. hca->validity_expression = cert_expr_expression(eb);
  591. }
  592. cert_expr_builder_free(eb);
  593. }
  594. return hca;
  595. }
  596. char *host_ca_save(host_ca *hca)
  597. {
  598. if (!*hca->name)
  599. return dupstr("CA record must have a name");
  600. char *filename = make_filename(INDEX_HOSTCA, hca->name);
  601. FILE *fp = fopen(filename, "w");
  602. if (!fp)
  603. return dupprintf("Unable to open file '%s'", filename);
  604. fprintf(fp, "PublicKey=");
  605. base64_encode_fp(fp, ptrlen_from_strbuf(hca->ca_public_key), 0);
  606. fprintf(fp, "\n");
  607. fprintf(fp, "Validity=");
  608. percent_encode_fp(fp, ptrlen_from_asciz(hca->validity_expression), NULL);
  609. fprintf(fp, "\n");
  610. fprintf(fp, "PermitRSASHA1=%d\n", (int)hca->opts.permit_rsa_sha1);
  611. fprintf(fp, "PermitRSASHA256=%d\n", (int)hca->opts.permit_rsa_sha256);
  612. fprintf(fp, "PermitRSASHA512=%d\n", (int)hca->opts.permit_rsa_sha512);
  613. bool bad = ferror(fp);
  614. if (fclose(fp) < 0)
  615. bad = true;
  616. char *err = NULL;
  617. if (bad)
  618. err = dupprintf("Unable to write file '%s'", filename);
  619. sfree(filename);
  620. return err;
  621. }
  622. char *host_ca_delete(const char *name)
  623. {
  624. if (!*name)
  625. return dupstr("CA record must have a name");
  626. char *filename = make_filename(INDEX_HOSTCA, name);
  627. bool bad = remove(filename) < 0;
  628. char *err = NULL;
  629. if (bad)
  630. err = dupprintf("Unable to delete file '%s'", filename);
  631. sfree(filename);
  632. return err;
  633. }
  634. /*
  635. * Lines in the host keys file are of the form
  636. *
  637. * type@port:hostname keydata
  638. *
  639. * e.g.
  640. *
  641. * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
  642. */
  643. int check_stored_host_key(const char *hostname, int port,
  644. const char *keytype, const char *key)
  645. {
  646. FILE *fp;
  647. char *filename;
  648. char *line;
  649. int ret;
  650. filename = make_filename(INDEX_HOSTKEYS, NULL);
  651. fp = fopen(filename, "r");
  652. sfree(filename);
  653. if (!fp)
  654. return 1; /* key does not exist */
  655. ret = 1;
  656. while ( (line = fgetline(fp)) ) {
  657. int i;
  658. char *p = line;
  659. char porttext[20];
  660. line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */
  661. i = strlen(keytype);
  662. if (strncmp(p, keytype, i))
  663. goto done;
  664. p += i;
  665. if (*p != '@')
  666. goto done;
  667. p++;
  668. sprintf(porttext, "%d", port);
  669. i = strlen(porttext);
  670. if (strncmp(p, porttext, i))
  671. goto done;
  672. p += i;
  673. if (*p != ':')
  674. goto done;
  675. p++;
  676. i = strlen(hostname);
  677. if (strncmp(p, hostname, i))
  678. goto done;
  679. p += i;
  680. if (*p != ' ')
  681. goto done;
  682. p++;
  683. /*
  684. * Found the key. Now just work out whether it's the right
  685. * one or not.
  686. */
  687. if (!strcmp(p, key))
  688. ret = 0; /* key matched OK */
  689. else
  690. ret = 2; /* key mismatch */
  691. done:
  692. sfree(line);
  693. if (ret != 1)
  694. break;
  695. }
  696. fclose(fp);
  697. return ret;
  698. }
  699. bool have_ssh_host_key(const char *hostname, int port,
  700. const char *keytype)
  701. {
  702. /*
  703. * If we have a host key, check_stored_host_key will return 0 or 2.
  704. * If we don't have one, it'll return 1.
  705. */
  706. return check_stored_host_key(hostname, port, keytype, "") != 1;
  707. }
  708. void store_host_key(Seat *seat, const char *hostname, int port,
  709. const char *keytype, const char *key)
  710. {
  711. FILE *rfp, *wfp;
  712. char *newtext, *line;
  713. int headerlen;
  714. char *filename, *tmpfilename;
  715. /*
  716. * Open both the old file and a new file.
  717. */
  718. tmpfilename = make_filename(INDEX_HOSTKEYS_TMP, NULL);
  719. wfp = fopen(tmpfilename, "w");
  720. if (!wfp && errno == ENOENT) {
  721. char *dir, *errmsg;
  722. dir = make_filename(INDEX_DIR, NULL);
  723. if ((errmsg = make_dir_path(dir, 0700)) != NULL) {
  724. seat_nonfatal(seat, "Unable to store host key: %s", errmsg);
  725. sfree(errmsg);
  726. sfree(dir);
  727. sfree(tmpfilename);
  728. return;
  729. }
  730. sfree(dir);
  731. wfp = fopen(tmpfilename, "w");
  732. }
  733. if (!wfp) {
  734. seat_nonfatal(seat, "Unable to store host key: open(\"%s\") "
  735. "returned '%s'", tmpfilename, strerror(errno));
  736. sfree(tmpfilename);
  737. return;
  738. }
  739. filename = make_filename(INDEX_HOSTKEYS, NULL);
  740. rfp = fopen(filename, "r");
  741. newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key);
  742. headerlen = 1 + strcspn(newtext, " "); /* count the space too */
  743. /*
  744. * Copy all lines from the old file to the new one that _don't_
  745. * involve the same host key identifier as the one we're adding.
  746. */
  747. if (rfp) {
  748. while ( (line = fgetline(rfp)) ) {
  749. if (strncmp(line, newtext, headerlen))
  750. fputs(line, wfp);
  751. sfree(line);
  752. }
  753. fclose(rfp);
  754. }
  755. /*
  756. * Now add the new line at the end.
  757. */
  758. fputs(newtext, wfp);
  759. fclose(wfp);
  760. if (rename(tmpfilename, filename) < 0) {
  761. seat_nonfatal(seat, "Unable to store host key: rename(\"%s\",\"%s\")"
  762. " returned '%s'", tmpfilename, filename,
  763. strerror(errno));
  764. }
  765. sfree(tmpfilename);
  766. sfree(filename);
  767. sfree(newtext);
  768. }
  769. void read_random_seed(noise_consumer_t consumer)
  770. {
  771. int fd;
  772. char *fname;
  773. fname = make_filename(INDEX_RANDSEED, NULL);
  774. fd = open(fname, O_RDONLY);
  775. sfree(fname);
  776. if (fd >= 0) {
  777. char buf[512];
  778. int ret;
  779. while ( (ret = read(fd, buf, sizeof(buf))) > 0)
  780. consumer(buf, ret);
  781. close(fd);
  782. }
  783. }
  784. void write_random_seed(void *data, int len)
  785. {
  786. int fd;
  787. char *fname;
  788. fname = make_filename(INDEX_RANDSEED, NULL);
  789. /*
  790. * Don't truncate the random seed file if it already exists; if
  791. * something goes wrong half way through writing it, it would
  792. * be better to leave the old data there than to leave it empty.
  793. */
  794. fd = open(fname, O_CREAT | O_WRONLY, 0600);
  795. if (fd < 0) {
  796. if (errno != ENOENT) {
  797. nonfatal("Unable to write random seed: open(\"%s\") "
  798. "returned '%s'", fname, strerror(errno));
  799. sfree(fname);
  800. return;
  801. }
  802. char *dir, *errmsg;
  803. dir = make_filename(INDEX_DIR, NULL);
  804. if ((errmsg = make_dir_path(dir, 0700)) != NULL) {
  805. nonfatal("Unable to write random seed: %s", errmsg);
  806. sfree(errmsg);
  807. sfree(fname);
  808. sfree(dir);
  809. return;
  810. }
  811. sfree(dir);
  812. fd = open(fname, O_CREAT | O_WRONLY, 0600);
  813. if (fd < 0) {
  814. nonfatal("Unable to write random seed: open(\"%s\") "
  815. "returned '%s'", fname, strerror(errno));
  816. sfree(fname);
  817. return;
  818. }
  819. }
  820. while (len > 0) {
  821. int ret = write(fd, data, len);
  822. if (ret < 0) {
  823. nonfatal("Unable to write random seed: write "
  824. "returned '%s'", strerror(errno));
  825. break;
  826. }
  827. len -= ret;
  828. data = (char *)data + len;
  829. }
  830. close(fd);
  831. sfree(fname);
  832. }
  833. void cleanup_all(void)
  834. {
  835. }