From 2ecfbf46e3e8301fa6f1a14e8a42659e9de12b6a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 3 May 2018 21:50:28 +0930 Subject: [PATCH] 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 --- hsmd/hsm.c | 19 +++++++++---------- hsmd/hsm_client_wire_csv | 1 - lightningd/hsm_control.c | 10 ++-------- lightningd/hsm_control.h | 2 +- lightningd/lightningd.c | 8 +++----- lightningd/options.c | 7 +------ lightningd/options.h | 6 ++---- lightningd/test/run-find_my_path.c | 4 ++-- 8 files changed, 20 insertions(+), 37 deletions(-) diff --git a/hsmd/hsm.c b/hsmd/hsm.c index 25f80de42..2007e97d4 100644 --- a/hsmd/hsm.c +++ b/hsmd/hsm.c @@ -460,12 +460,15 @@ static void bitcoin_keypair(struct privkey *privkey, "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); - if (fd < 0) + if (fd < 0) { + if (errno == EEXIST) + return; status_failed(STATUS_FAIL_INTERNAL_ERROR, "creating: %s", strerror(errno)); + } randombytes_buf(&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)); } close(fd); - - populate_secretstuff(); + status_unusual("HSM: created new hsm_secret file"); } 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) { - bool new; - if (!fromwire_hsm_init(msg, &new)) + if (!fromwire_hsm_init(msg)) master_badmsg(WIRE_HSM_INIT, msg); - if (new) - create_new_hsm(); - else - load_hsm(); + maybe_create_new_hsm(); + load_hsm(); send_init_response(master); } diff --git a/hsmd/hsm_client_wire_csv b/hsmd/hsm_client_wire_csv index 154db177a..ce47fa51c 100644 --- a/hsmd/hsm_client_wire_csv +++ b/hsmd/hsm_client_wire_csv @@ -6,7 +6,6 @@ hsmstatus_client_bad_request,,msg,len*u8 # Start the HSM. hsm_init,11 -hsm_init,,new,bool #include hsm_init_reply,111 diff --git a/lightningd/hsm_control.c b/lightningd/hsm_control.c index 2276703fb..7ae168620 100644 --- a/lightningd/hsm_control.c +++ b/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; - bool create; ld->hsm_fd = subd_raw(ld, "lightning_hsmd"); if (ld->hsm_fd < 0) err(1, "Could not subd hsm"); ld->hsm_log = new_log(ld, ld->log_book, "hsmd:"); - if (newdir) - create = true; - else - create = (access("hsm_secret", F_OK) != 0); - - if (!wire_sync_write(ld->hsm_fd, towire_hsm_init(tmpctx, create))) + if (!wire_sync_write(ld->hsm_fd, towire_hsm_init(tmpctx))) err(1, "Writing init msg to hsm"); ld->wallet->bip32_base = tal(ld->wallet, struct ext_key); diff --git a/lightningd/hsm_control.h b/lightningd/hsm_control.h index c96eb9d51..dcad2df45 100644 --- a/lightningd/hsm_control.h +++ b/lightningd/hsm_control.h @@ -8,5 +8,5 @@ struct lightningd; 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 */ diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index dc9ce89bd..686f36bbf 100644 --- a/lightningd/lightningd.c +++ b/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[]) { - setup_locale(); - struct lightningd *ld; - bool newdir; u32 blockheight; + setup_locale(); daemon_setup(argv[0], log_backtrace_print, log_backtrace_exit); ld = new_lightningd(NULL); @@ -306,7 +304,7 @@ int main(int argc, char *argv[]) register_opts(ld); /* 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*/ signal(SIGPIPE, SIG_IGN); @@ -323,7 +321,7 @@ int main(int argc, char *argv[]) io_poll_debug = io_poll_override(io_poll_lightningd); /* Set up HSM. */ - hsm_init(ld, newdir); + hsm_init(ld); /* Now we know our ID, we can set our color/alias if not already. */ setup_color_and_alias(ld); diff --git a/lightningd/options.c b/lightningd/options.c index b35cb8ee3..fd2db18e8 100644 --- a/lightningd/options.c +++ b/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 * to display. The actual values loaded here, will be overwritten later * 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) fatal("Could not change directory %s: %s", ld->config_dir, strerror(errno)); - newdir = true; } /* Now look for config file */ @@ -788,8 +785,6 @@ bool handle_opts(struct lightningd *ld, int argc, char *argv[]) close(fd); } #endif - - return newdir; } /* FIXME: This is a hack! Expose somehow in ccan/opt.*/ diff --git a/lightningd/options.h b/lightningd/options.h index 97de34ea4..9208a1f59 100644 --- a/lightningd/options.h +++ b/lightningd/options.h @@ -8,10 +8,8 @@ struct lightningd; /* You can register additional options *after* this if you want. */ void register_opts(struct lightningd *ld); -/* After this, we're in the .lightning dir, config file parsed. - * If we just created the dir, returns true. - */ -bool handle_opts(struct lightningd *ld, int argc, char *argv[]); +/* After this, we're in the .lightning dir, config files parsed. */ +void handle_opts(struct lightningd *ld, int argc, char *argv[]); /* Derive default color and alias from the pubkey. */ void setup_color_and_alias(struct lightningd *ld); diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index e76d276f3..5deec9119 100644 --- a/lightningd/test/run-find_my_path.c +++ b/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) { fprintf(stderr, "gossip_init called!\n"); abort(); } /* 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(); } /* Generated stub for hash_htlc_key */ size_t hash_htlc_key(const struct htlc_key *htlc_key UNNEEDED) { fprintf(stderr, "hash_htlc_key called!\n"); abort(); } /* 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(); } /* Generated stub for json_escape */ struct json_escaped *json_escape(const tal_t *ctx UNNEEDED, const char *str TAKES UNNEEDED)