mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-24 01:24:26 +01:00
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>
This commit is contained in:
committed by
Christian Decker
parent
666e1b320f
commit
2ecfbf46e3
19
hsmd/hsm.c
19
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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.*/
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user