Skip to content

Commit

Permalink
hsmd: drop newdir logic.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed May 5, 2018
1 parent 666e1b3 commit 2ecfbf4
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 37 deletions.
19 changes: 9 additions & 10 deletions hsmd/hsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion hsmd/hsm_client_wire_csv
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ hsmstatus_client_bad_request,,msg,len*u8

# Start the HSM.
hsm_init,11
hsm_init,,new,bool

#include <common/bip32.h>
hsm_init_reply,111
Expand Down
10 changes: 2 additions & 8 deletions lightningd/hsm_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lightningd/hsm_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
8 changes: 3 additions & 5 deletions lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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 */
Expand Down Expand Up @@ -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.*/
Expand Down
6 changes: 2 additions & 4 deletions lightningd/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lightningd/test/run-find_my_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2ecfbf4

Please sign in to comment.