Browse Source

hsmd: drop newdir logic.

Originally we were supposed to tell the HSM we had just created the directory,
otherwise it wouldn't create a new seed.  But we modified it to check if
there was a seed file anyway: just move that logic into a branch of hsmd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
2ecfbf46e3
  1. 19
      hsmd/hsm.c
  2. 1
      hsmd/hsm_client_wire_csv
  3. 10
      lightningd/hsm_control.c
  4. 2
      lightningd/hsm_control.h
  5. 8
      lightningd/lightningd.c
  6. 7
      lightningd/options.c
  7. 6
      lightningd/options.h
  8. 4
      lightningd/test/run-find_my_path.c

19
hsmd/hsm.c

@ -460,12 +460,15 @@ static void bitcoin_keypair(struct privkey *privkey,
"BIP32 pubkey %u create failed", index); "BIP32 pubkey %u create failed", index);
} }
static void create_new_hsm(void) static void maybe_create_new_hsm(void)
{ {
int fd = open("hsm_secret", O_CREAT|O_EXCL|O_WRONLY, 0400); int fd = open("hsm_secret", O_CREAT|O_EXCL|O_WRONLY, 0400);
if (fd < 0) if (fd < 0) {
if (errno == EEXIST)
return;
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"creating: %s", strerror(errno)); "creating: %s", strerror(errno));
}
randombytes_buf(&secretstuff.hsm_secret, sizeof(secretstuff.hsm_secret)); randombytes_buf(&secretstuff.hsm_secret, sizeof(secretstuff.hsm_secret));
if (!write_all(fd, &secretstuff.hsm_secret, sizeof(secretstuff.hsm_secret))) { if (!write_all(fd, &secretstuff.hsm_secret, sizeof(secretstuff.hsm_secret))) {
@ -494,8 +497,7 @@ static void create_new_hsm(void)
"fsyncdir: %s", strerror(errno)); "fsyncdir: %s", strerror(errno));
} }
close(fd); close(fd);
status_unusual("HSM: created new hsm_secret file");
populate_secretstuff();
} }
static void load_hsm(void) static void load_hsm(void)
@ -514,15 +516,12 @@ static void load_hsm(void)
static void init_hsm(struct daemon_conn *master, const u8 *msg) static void init_hsm(struct daemon_conn *master, const u8 *msg)
{ {
bool new;
if (!fromwire_hsm_init(msg, &new)) if (!fromwire_hsm_init(msg))
master_badmsg(WIRE_HSM_INIT, msg); master_badmsg(WIRE_HSM_INIT, msg);
if (new) maybe_create_new_hsm();
create_new_hsm(); load_hsm();
else
load_hsm();
send_init_response(master); send_init_response(master);
} }

1
hsmd/hsm_client_wire_csv

@ -6,7 +6,6 @@ hsmstatus_client_bad_request,,msg,len*u8
# Start the HSM. # Start the HSM.
hsm_init,11 hsm_init,11
hsm_init,,new,bool
#include <common/bip32.h> #include <common/bip32.h>
hsm_init_reply,111 hsm_init_reply,111

10
lightningd/hsm_control.c

@ -30,22 +30,16 @@ u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld)
} }
} }
void hsm_init(struct lightningd *ld, bool newdir) void hsm_init(struct lightningd *ld)
{ {
u8 *msg; u8 *msg;
bool create;
ld->hsm_fd = subd_raw(ld, "lightning_hsmd"); ld->hsm_fd = subd_raw(ld, "lightning_hsmd");
if (ld->hsm_fd < 0) if (ld->hsm_fd < 0)
err(1, "Could not subd hsm"); err(1, "Could not subd hsm");
ld->hsm_log = new_log(ld, ld->log_book, "hsmd:"); ld->hsm_log = new_log(ld, ld->log_book, "hsmd:");
if (newdir) if (!wire_sync_write(ld->hsm_fd, towire_hsm_init(tmpctx)))
create = true;
else
create = (access("hsm_secret", F_OK) != 0);
if (!wire_sync_write(ld->hsm_fd, towire_hsm_init(tmpctx, create)))
err(1, "Writing init msg to hsm"); err(1, "Writing init msg to hsm");
ld->wallet->bip32_base = tal(ld->wallet, struct ext_key); ld->wallet->bip32_base = tal(ld->wallet, struct ext_key);

2
lightningd/hsm_control.h

@ -8,5 +8,5 @@
struct lightningd; struct lightningd;
u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld); u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld);
void hsm_init(struct lightningd *ld, bool newdir); void hsm_init(struct lightningd *ld);
#endif /* LIGHTNING_LIGHTNINGD_HSM_CONTROL_H */ #endif /* LIGHTNING_LIGHTNINGD_HSM_CONTROL_H */

8
lightningd/lightningd.c

@ -289,12 +289,10 @@ static int io_poll_lightningd(struct pollfd *fds, nfds_t nfds, int timeout)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
setup_locale();
struct lightningd *ld; struct lightningd *ld;
bool newdir;
u32 blockheight; u32 blockheight;
setup_locale();
daemon_setup(argv[0], log_backtrace_print, log_backtrace_exit); daemon_setup(argv[0], log_backtrace_print, log_backtrace_exit);
ld = new_lightningd(NULL); ld = new_lightningd(NULL);
@ -306,7 +304,7 @@ int main(int argc, char *argv[])
register_opts(ld); register_opts(ld);
/* Handle options and config; move to .lightningd */ /* Handle options and config; move to .lightningd */
newdir = handle_opts(ld, argc, argv); handle_opts(ld, argc, argv);
/* Ignore SIGPIPE: we look at our write return values*/ /* Ignore SIGPIPE: we look at our write return values*/
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
@ -323,7 +321,7 @@ int main(int argc, char *argv[])
io_poll_debug = io_poll_override(io_poll_lightningd); io_poll_debug = io_poll_override(io_poll_lightningd);
/* Set up HSM. */ /* Set up HSM. */
hsm_init(ld, newdir); hsm_init(ld);
/* Now we know our ID, we can set our color/alias if not already. */ /* Now we know our ID, we can set our color/alias if not already. */
setup_color_and_alias(ld); setup_color_and_alias(ld);

7
lightningd/options.c

@ -735,10 +735,8 @@ void setup_color_and_alias(struct lightningd *ld)
} }
} }
bool handle_opts(struct lightningd *ld, int argc, char *argv[]) void handle_opts(struct lightningd *ld, int argc, char *argv[])
{ {
bool newdir = false;
/* Load defaults first, so that --help (in early options) has something /* Load defaults first, so that --help (in early options) has something
* to display. The actual values loaded here, will be overwritten later * to display. The actual values loaded here, will be overwritten later
* by opt_parse_from_config. */ * by opt_parse_from_config. */
@ -757,7 +755,6 @@ bool handle_opts(struct lightningd *ld, int argc, char *argv[])
if (chdir(ld->config_dir) != 0) if (chdir(ld->config_dir) != 0)
fatal("Could not change directory %s: %s", fatal("Could not change directory %s: %s",
ld->config_dir, strerror(errno)); ld->config_dir, strerror(errno));
newdir = true;
} }
/* Now look for config file */ /* Now look for config file */
@ -788,8 +785,6 @@ bool handle_opts(struct lightningd *ld, int argc, char *argv[])
close(fd); close(fd);
} }
#endif #endif
return newdir;
} }
/* FIXME: This is a hack! Expose somehow in ccan/opt.*/ /* FIXME: This is a hack! Expose somehow in ccan/opt.*/

6
lightningd/options.h

@ -8,10 +8,8 @@ struct lightningd;
/* You can register additional options *after* this if you want. */ /* You can register additional options *after* this if you want. */
void register_opts(struct lightningd *ld); void register_opts(struct lightningd *ld);
/* After this, we're in the .lightning dir, config file parsed. /* After this, we're in the .lightning dir, config files parsed. */
* If we just created the dir, returns true. void handle_opts(struct lightningd *ld, int argc, char *argv[]);
*/
bool handle_opts(struct lightningd *ld, int argc, char *argv[]);
/* Derive default color and alias from the pubkey. */ /* Derive default color and alias from the pubkey. */
void setup_color_and_alias(struct lightningd *ld); void setup_color_and_alias(struct lightningd *ld);

4
lightningd/test/run-find_my_path.c

@ -49,13 +49,13 @@ void gossip_activate(struct lightningd *ld UNNEEDED)
void gossip_init(struct lightningd *ld UNNEEDED) void gossip_init(struct lightningd *ld UNNEEDED)
{ fprintf(stderr, "gossip_init called!\n"); abort(); } { fprintf(stderr, "gossip_init called!\n"); abort(); }
/* Generated stub for handle_opts */ /* Generated stub for handle_opts */
bool handle_opts(struct lightningd *ld UNNEEDED, int argc UNNEEDED, char *argv[]) void handle_opts(struct lightningd *ld UNNEEDED, int argc UNNEEDED, char *argv[])
{ fprintf(stderr, "handle_opts called!\n"); abort(); } { fprintf(stderr, "handle_opts called!\n"); abort(); }
/* Generated stub for hash_htlc_key */ /* Generated stub for hash_htlc_key */
size_t hash_htlc_key(const struct htlc_key *htlc_key UNNEEDED) size_t hash_htlc_key(const struct htlc_key *htlc_key UNNEEDED)
{ fprintf(stderr, "hash_htlc_key called!\n"); abort(); } { fprintf(stderr, "hash_htlc_key called!\n"); abort(); }
/* Generated stub for hsm_init */ /* Generated stub for hsm_init */
void hsm_init(struct lightningd *ld UNNEEDED, bool newdir UNNEEDED) void hsm_init(struct lightningd *ld UNNEEDED)
{ fprintf(stderr, "hsm_init called!\n"); abort(); } { fprintf(stderr, "hsm_init called!\n"); abort(); }
/* Generated stub for json_escape */ /* Generated stub for json_escape */
struct json_escaped *json_escape(const tal_t *ctx UNNEEDED, const char *str TAKES UNNEEDED) struct json_escaped *json_escape(const tal_t *ctx UNNEEDED, const char *str TAKES UNNEEDED)

Loading…
Cancel
Save