mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 15:44:21 +01:00
plugins/chanbackup: use grab_file.
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include <ccan/json_out/json_out.h>
|
#include <ccan/json_out/json_out.h>
|
||||||
#include <ccan/noerr/noerr.h>
|
#include <ccan/noerr/noerr.h>
|
||||||
#include <ccan/read_write_all/read_write_all.h>
|
#include <ccan/read_write_all/read_write_all.h>
|
||||||
|
#include <ccan/tal/grab_file/grab_file.h>
|
||||||
#include <ccan/tal/str/str.h>
|
#include <ccan/tal/str/str.h>
|
||||||
#include <ccan/time/time.h>
|
#include <ccan/time/time.h>
|
||||||
#include <common/features.h>
|
#include <common/features.h>
|
||||||
@@ -166,57 +167,51 @@ static void maybe_create_new_scb(struct plugin *p,
|
|||||||
rename("scb.tmp", FILENAME);
|
rename("scb.tmp", FILENAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static u8* get_file_data(struct plugin *p)
|
||||||
|
{
|
||||||
|
u8 *scb = grab_file(tmpctx, "emergency.recover");
|
||||||
|
if (!scb) {
|
||||||
|
plugin_err(p, "Cannot read emergency.recover: %s", strerror(errno));
|
||||||
|
} else {
|
||||||
|
/* grab_file adds nul term */
|
||||||
|
tal_resize(&scb, tal_bytelen(scb) - 1);
|
||||||
|
}
|
||||||
|
return scb;
|
||||||
|
}
|
||||||
|
|
||||||
/* Returns decrypted SCB in form of a u8 array */
|
/* Returns decrypted SCB in form of a u8 array */
|
||||||
static u8 *decrypt_scb(struct plugin *p)
|
static u8 *decrypt_scb(struct plugin *p)
|
||||||
{
|
{
|
||||||
struct stat st;
|
u8 *filedata = get_file_data(p);
|
||||||
int fd = open(FILENAME, O_RDONLY);
|
|
||||||
|
|
||||||
if (stat(FILENAME, &st) != 0)
|
|
||||||
plugin_err(p, "SCB file is corrupted!: %s",
|
|
||||||
strerror(errno));
|
|
||||||
|
|
||||||
u8 final[st.st_size];
|
|
||||||
|
|
||||||
if (!read_all(fd, &final, st.st_size)) {
|
|
||||||
plugin_log(p, LOG_DBG, "SCB file is corrupted!: %s",
|
|
||||||
strerror(errno));
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
crypto_secretstream_xchacha20poly1305_state crypto_state;
|
crypto_secretstream_xchacha20poly1305_state crypto_state;
|
||||||
|
|
||||||
if (st.st_size < ABYTES +
|
if (tal_bytelen(filedata) < ABYTES +
|
||||||
HEADER_LEN)
|
HEADER_LEN)
|
||||||
plugin_err(p, "SCB file is corrupted!");
|
plugin_err(p, "SCB file is corrupted!");
|
||||||
|
|
||||||
u8 *ans = tal_arr(tmpctx, u8, st.st_size -
|
u8 *decrypt_scb = tal_arr(tmpctx, u8, tal_bytelen(filedata) -
|
||||||
ABYTES -
|
ABYTES -
|
||||||
HEADER_LEN);
|
HEADER_LEN);
|
||||||
|
|
||||||
/* The header part */
|
/* The header part */
|
||||||
if (crypto_secretstream_xchacha20poly1305_init_pull(&crypto_state,
|
if (crypto_secretstream_xchacha20poly1305_init_pull(&crypto_state,
|
||||||
final,
|
filedata,
|
||||||
(&secret)->data) != 0)
|
(&secret)->data) != 0)
|
||||||
{
|
{
|
||||||
plugin_err(p, "SCB file is corrupted!");
|
plugin_err(p, "SCB file is corrupted!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crypto_secretstream_xchacha20poly1305_pull(&crypto_state, ans,
|
if (crypto_secretstream_xchacha20poly1305_pull(&crypto_state, decrypt_scb,
|
||||||
NULL, 0,
|
NULL, 0,
|
||||||
final +
|
filedata +
|
||||||
HEADER_LEN,
|
HEADER_LEN,
|
||||||
st.st_size -
|
tal_bytelen(filedata)-
|
||||||
HEADER_LEN,
|
HEADER_LEN,
|
||||||
NULL, 0) != 0) {
|
NULL, 0) != 0) {
|
||||||
plugin_err(p, "SCB file is corrupted!");
|
plugin_err(p, "SCB file is corrupted!");
|
||||||
}
|
}
|
||||||
|
return decrypt_scb;
|
||||||
if (close(fd) != 0)
|
|
||||||
plugin_err(p, "Closing: %s", strerror(errno));
|
|
||||||
|
|
||||||
return ans;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct command_result *after_recover_rpc(struct command *cmd,
|
static struct command_result *after_recover_rpc(struct command *cmd,
|
||||||
|
|||||||
Reference in New Issue
Block a user