2018-11-20 02:53:43 +01:00
|
|
|
/* Code for JSON_RPC API.
|
|
|
|
*
|
|
|
|
* Each socket connection is represented by a `struct json_connection`.
|
|
|
|
*
|
|
|
|
* This can have zero, one or more `struct command` in progress at a time:
|
|
|
|
* because the json_connection can be closed at any point, these `struct command`
|
|
|
|
* have a independent lifetimes.
|
|
|
|
*
|
|
|
|
* Each `struct command` writes into a `struct json_stream`, which is created
|
|
|
|
* the moment they start writing output (see attach_json_stream). Initially
|
|
|
|
* the struct command owns it since they're writing into it. When they're
|
|
|
|
* done, the `json_connection` needs to drain it (if it's still around). At
|
|
|
|
* that point, the `json_connection` becomes the owner (or it's simply freed).
|
|
|
|
*/
|
2018-12-21 07:25:31 +01:00
|
|
|
/* eg: { "jsonrpc":"2.0", "method" : "dev-echo", "params" : [ "hello", "Arabella!" ], "id" : "1" } */
|
2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-05-20 12:14:00 +02:00
|
|
|
#include <ccan/asort/asort.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <ccan/io/io.h>
|
2022-01-03 19:45:35 +01:00
|
|
|
#include <ccan/json_escape/json_escape.h>
|
2019-06-12 02:38:55 +02:00
|
|
|
#include <ccan/json_out/json_out.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2019-07-25 08:39:40 +02:00
|
|
|
#include <common/configdir.h>
|
2018-12-08 01:39:25 +01:00
|
|
|
#include <common/json_command.h>
|
2020-05-15 12:30:25 +02:00
|
|
|
#include <common/json_helpers.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <common/json_tok.h>
|
2017-12-15 11:22:57 +01:00
|
|
|
#include <common/memleak.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/param.h>
|
2018-11-20 02:46:32 +01:00
|
|
|
#include <common/timeout.h>
|
2022-01-03 19:45:35 +01:00
|
|
|
#include <db/exec.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <fcntl.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2019-09-27 09:50:50 +02:00
|
|
|
#include <lightningd/plugin_hook.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
2019-09-27 09:50:50 +02:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-12-16 05:49:06 +01:00
|
|
|
/* Dummy structure. */
|
|
|
|
struct command_result {
|
|
|
|
char c;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct command_result param_failed, complete, pending, unknown;
|
|
|
|
|
|
|
|
struct command_result *command_param_failed(void)
|
|
|
|
{
|
|
|
|
return ¶m_failed;
|
|
|
|
}
|
|
|
|
|
2018-12-17 04:52:08 +01:00
|
|
|
struct command_result *command_its_complicated(const char *relationship_details
|
|
|
|
UNNEEDED)
|
2018-12-16 05:49:06 +01:00
|
|
|
{
|
|
|
|
return &unknown;
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* This represents a JSON RPC connection. It can invoke multiple commands, but
|
|
|
|
* a command can outlive the connection, which could close any time. */
|
2018-11-20 02:46:32 +01:00
|
|
|
struct json_connection {
|
|
|
|
/* The global state */
|
|
|
|
struct lightningd *ld;
|
|
|
|
|
|
|
|
/* This io_conn (and our owner!) */
|
|
|
|
struct io_conn *conn;
|
|
|
|
|
|
|
|
/* Logging for this json connection. */
|
|
|
|
struct log *log;
|
|
|
|
|
|
|
|
/* The buffer (required to interpret tokens). */
|
|
|
|
char *buffer;
|
|
|
|
|
|
|
|
/* Internal state: */
|
|
|
|
/* How much is already filled. */
|
|
|
|
size_t used;
|
|
|
|
/* How much has just been filled. */
|
|
|
|
size_t len_read;
|
|
|
|
|
2020-08-20 15:40:34 +02:00
|
|
|
/* JSON parsing state. */
|
|
|
|
jsmn_parser input_parser;
|
|
|
|
jsmntok_t *input_toks;
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* Our commands */
|
|
|
|
struct list_head commands;
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2020-10-12 07:33:50 +02:00
|
|
|
/* Are notifications enabled? */
|
|
|
|
bool notifications_enabled;
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* Our json_streams (owned by the commands themselves while running).
|
|
|
|
* Since multiple streams could start returning data at once, we
|
|
|
|
* always service these in order, freeing once empty. */
|
|
|
|
struct json_stream **js_arr;
|
2018-11-20 02:46:32 +01:00
|
|
|
};
|
|
|
|
|
2018-11-14 08:12:03 +01:00
|
|
|
/**
|
|
|
|
* `jsonrpc` encapsulates the entire state of the JSON-RPC interface,
|
|
|
|
* including a list of methods that the interface supports (can be
|
|
|
|
* appended dynamically, e.g., for plugins, and logs. It also serves
|
|
|
|
* as a convenient `tal`-parent for all JSON-RPC related allocations.
|
|
|
|
*/
|
|
|
|
struct jsonrpc {
|
|
|
|
struct io_listener *rpc_listener;
|
|
|
|
struct json_command **commands;
|
2019-02-04 11:55:42 +01:00
|
|
|
|
|
|
|
/* Map from json command names to usage strings: we don't put this inside
|
|
|
|
* struct json_command as it's good practice to have those const. */
|
|
|
|
STRMAP(const char *) usagemap;
|
2018-11-14 08:12:03 +01:00
|
|
|
};
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* The command itself usually owns the stream, because jcon may get closed.
|
|
|
|
* The command transfers ownership once it's done though. */
|
|
|
|
static struct json_stream *jcon_new_json_stream(const tal_t *ctx,
|
|
|
|
struct json_connection *jcon,
|
|
|
|
struct command *writer)
|
|
|
|
{
|
2019-02-18 03:44:29 +01:00
|
|
|
struct json_stream *js = new_json_stream(ctx, writer, jcon->log);
|
2019-01-15 04:51:27 +01:00
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* Wake writer to start streaming, in case it's not already. */
|
|
|
|
io_wake(jcon);
|
|
|
|
|
|
|
|
/* FIXME: Keep streams around for recycling. */
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&jcon->js_arr, js);
|
|
|
|
return js;
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void jcon_remove_json_stream(struct json_connection *jcon,
|
|
|
|
struct json_stream *js)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(jcon->js_arr); i++) {
|
|
|
|
if (js != jcon->js_arr[i])
|
|
|
|
continue;
|
|
|
|
|
2018-12-16 05:42:06 +01:00
|
|
|
tal_arr_remove(&jcon->js_arr, i);
|
2018-11-20 02:46:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2017-09-28 05:31:47 +02:00
|
|
|
/* jcon and cmd have separate lifetimes: we detach them on either destruction */
|
2017-09-28 05:31:36 +02:00
|
|
|
static void destroy_jcon(struct json_connection *jcon)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
struct command *c;
|
|
|
|
|
2020-02-14 01:35:19 +01:00
|
|
|
list_for_each(&jcon->commands, c, list)
|
2018-11-20 02:46:32 +01:00
|
|
|
c->jcon = NULL;
|
2018-02-16 03:53:00 +01:00
|
|
|
|
2017-09-28 05:31:36 +02:00
|
|
|
/* Make sure this happens last! */
|
|
|
|
tal_free(jcon->log);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_help(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
static const struct json_command help_command = {
|
|
|
|
"help",
|
2019-05-22 16:08:16 +02:00
|
|
|
"utility",
|
2016-01-21 21:11:48 +01:00
|
|
|
json_help,
|
2018-09-13 17:57:24 +02:00
|
|
|
"List available commands, or give verbose help on one {command}.",
|
2018-01-29 01:30:15 +01:00
|
|
|
.verbose = "help [command]\n"
|
|
|
|
"Without [command]:\n"
|
|
|
|
" Outputs an array of objects with 'command' and 'description'\n"
|
|
|
|
"With [command]:\n"
|
|
|
|
" Give a single object containing 'verbose', which completely describes\n"
|
|
|
|
" the command inputs and outputs."
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
2017-01-04 04:38:15 +01:00
|
|
|
AUTODATA(json_command, &help_command);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_stop(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
struct json_out *jout;
|
|
|
|
const char *p;
|
|
|
|
size_t len;
|
|
|
|
|
2018-09-14 16:51:04 +02:00
|
|
|
if (!param(cmd, buffer, params, NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2018-09-14 16:51:04 +02:00
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
log_unusual(cmd->ld->log, "JSON-RPC shutdown");
|
|
|
|
|
2020-11-06 03:44:14 +01:00
|
|
|
/* With rpc_command_hook, jcon might have closed in the meantime! */
|
|
|
|
if (!cmd->jcon) {
|
|
|
|
/* Return us to toplevel lightningd.c */
|
|
|
|
io_break(cmd->ld);
|
|
|
|
return command_still_pending(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd->ld->stop_conn = cmd->jcon->conn;
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
/* This is the one place where result is a literal string. */
|
|
|
|
jout = json_out_new(tmpctx);
|
|
|
|
json_out_start(jout, NULL, '{');
|
|
|
|
json_out_addstr(jout, "jsonrpc", "2.0");
|
|
|
|
/* id may be a string or number, so copy direct. */
|
|
|
|
memcpy(json_out_member_direct(jout, "id", strlen(cmd->id)),
|
|
|
|
cmd->id, strlen(cmd->id));
|
|
|
|
json_out_addstr(jout, "result", "Shutdown complete");
|
|
|
|
json_out_end(jout, '}');
|
|
|
|
json_out_finished(jout);
|
|
|
|
|
|
|
|
/* Add two \n */
|
|
|
|
memcpy(json_out_direct(jout, 2), "\n\n", strlen("\n\n"));
|
|
|
|
p = json_out_contents(jout, &len);
|
|
|
|
cmd->ld->stop_response = tal_strndup(cmd->ld, p, len);
|
|
|
|
|
|
|
|
/* Wake write loop in case it's not already. */
|
|
|
|
io_wake(cmd->jcon);
|
|
|
|
|
|
|
|
return command_still_pending(cmd);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command stop_command = {
|
|
|
|
"stop",
|
2019-05-22 16:08:16 +02:00
|
|
|
"utility",
|
2016-01-21 21:11:48 +01:00
|
|
|
json_stop,
|
2018-01-22 09:55:07 +01:00
|
|
|
"Shut down the lightningd process"
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
2017-01-04 04:38:15 +01:00
|
|
|
AUTODATA(json_command, &stop_command);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2018-11-20 02:46:32 +01:00
|
|
|
struct slowcmd {
|
|
|
|
struct command *cmd;
|
|
|
|
unsigned *msec;
|
|
|
|
struct json_stream *js;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void slowcmd_finish(struct slowcmd *sc)
|
|
|
|
{
|
|
|
|
json_add_num(sc->js, "msec", *sc->msec);
|
2018-12-16 05:52:06 +01:00
|
|
|
was_pending(command_success(sc->cmd, sc->js));
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void slowcmd_start(struct slowcmd *sc)
|
|
|
|
{
|
|
|
|
sc->js = json_stream_success(sc->cmd);
|
2019-06-14 05:49:23 +02:00
|
|
|
new_reltimer(sc->cmd->ld->timers, sc, time_from_msec(*sc->msec),
|
2018-11-20 02:46:32 +01:00
|
|
|
slowcmd_finish, sc);
|
|
|
|
}
|
|
|
|
|
2019-07-16 03:41:51 +02:00
|
|
|
static struct command_result *json_dev(struct command *cmd UNUSED,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
2019-07-16 03:41:51 +02:00
|
|
|
const char *subcmd;
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2019-07-16 03:41:51 +02:00
|
|
|
subcmd = param_subcommand(cmd, buffer, params,
|
|
|
|
"crash", "rhash", "slowcmd", NULL);
|
|
|
|
if (!subcmd)
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2019-07-16 03:41:51 +02:00
|
|
|
if (streq(subcmd, "crash")) {
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("subcommand", param_ignore, cmd),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
fatal("Crash at user request");
|
|
|
|
} else if (streq(subcmd, "slowcmd")) {
|
|
|
|
struct slowcmd *sc = tal(cmd, struct slowcmd);
|
|
|
|
|
|
|
|
sc->cmd = cmd;
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("subcommand", param_ignore, cmd),
|
|
|
|
p_opt_def("msec", param_number, &sc->msec, 1000),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
new_reltimer(cmd->ld->timers, sc, time_from_msec(0),
|
|
|
|
slowcmd_start, sc);
|
|
|
|
return command_still_pending(cmd);
|
|
|
|
} else {
|
|
|
|
assert(streq(subcmd, "rhash"));
|
|
|
|
struct json_stream *response;
|
|
|
|
struct sha256 *secret;
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("subcommand", param_ignore, cmd),
|
|
|
|
p_req("secret", param_sha256, &secret),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
/* Hash in place. */
|
|
|
|
sha256(secret, secret, sizeof(*secret));
|
|
|
|
response = json_stream_success(cmd);
|
2019-08-10 10:39:04 +02:00
|
|
|
json_add_sha256(response, "rhash", secret);
|
2019-07-16 03:41:51 +02:00
|
|
|
return command_success(cmd, response);
|
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
2019-07-16 03:41:51 +02:00
|
|
|
static const struct json_command dev_command = {
|
|
|
|
"dev",
|
2019-05-22 16:08:16 +02:00
|
|
|
"developer",
|
2019-07-16 03:41:51 +02:00
|
|
|
json_dev,
|
2019-08-05 13:04:13 +02:00
|
|
|
"Developer command test multiplexer",
|
|
|
|
.verbose = "dev rhash {secret}\n"
|
|
|
|
" Show SHA256 of {secret}\n"
|
|
|
|
"dev crash\n"
|
|
|
|
" Crash lightningd by calling fatal()\n"
|
|
|
|
"dev slowcmd {msec}\n"
|
|
|
|
" Torture test for slow commands, optional {msec}\n"
|
2016-01-21 21:15:28 +01:00
|
|
|
};
|
2019-07-16 03:41:51 +02:00
|
|
|
AUTODATA(json_command, &dev_command);
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif /* DEVELOPER */
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2017-01-04 04:38:15 +01:00
|
|
|
static size_t num_cmdlist;
|
|
|
|
|
|
|
|
static struct json_command **get_cmdlist(void)
|
|
|
|
{
|
|
|
|
static struct json_command **cmdlist;
|
|
|
|
if (!cmdlist)
|
|
|
|
cmdlist = autodata_get(json_command, &num_cmdlist);
|
|
|
|
|
|
|
|
return cmdlist;
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-10-09 03:09:54 +02:00
|
|
|
static void json_add_help_command(struct command *cmd,
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *response,
|
2018-10-09 03:09:54 +02:00
|
|
|
struct json_command *json_command)
|
|
|
|
{
|
|
|
|
char *usage;
|
|
|
|
|
2020-08-06 02:27:19 +02:00
|
|
|
/* If they disallow deprecated APIs, don't even list them */
|
|
|
|
if (!deprecated_apis && json_command->deprecated)
|
|
|
|
return;
|
|
|
|
|
|
|
|
usage = tal_fmt(cmd, "%s%s %s",
|
2019-02-04 11:55:42 +01:00
|
|
|
json_command->name,
|
2020-08-06 02:27:19 +02:00
|
|
|
json_command->deprecated ? " (DEPRECATED!)" : "",
|
2019-02-04 11:55:42 +01:00
|
|
|
strmap_get(&cmd->ld->jsonrpc->usagemap,
|
|
|
|
json_command->name));
|
2018-10-09 03:09:54 +02:00
|
|
|
json_object_start(response, NULL);
|
|
|
|
|
|
|
|
json_add_string(response, "command", usage);
|
2019-05-22 16:08:16 +02:00
|
|
|
json_add_string(response, "category", json_command->category);
|
2018-10-09 03:09:54 +02:00
|
|
|
json_add_string(response, "description", json_command->description);
|
|
|
|
|
|
|
|
if (!json_command->verbose) {
|
|
|
|
json_add_string(response, "verbose",
|
|
|
|
"HELP! Please contribute"
|
|
|
|
" a description for this"
|
|
|
|
" json_command!");
|
|
|
|
} else {
|
2019-06-12 02:38:54 +02:00
|
|
|
struct json_escape *esc;
|
2018-10-09 03:09:54 +02:00
|
|
|
|
|
|
|
esc = json_escape(NULL, json_command->verbose);
|
|
|
|
json_add_escaped_string(response, "verbose", take(esc));
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_end(response);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:55:35 +01:00
|
|
|
static const struct json_command *find_command(struct json_command **commands,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *cmdtok)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(commands); i++) {
|
|
|
|
if (json_tok_streq(buffer, cmdtok, commands[i]->name))
|
|
|
|
return commands[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-20 12:14:00 +02:00
|
|
|
static int compare_commands_name(struct json_command *const *a,
|
|
|
|
struct json_command *const *b, void *unused)
|
|
|
|
{
|
|
|
|
return strcmp((*a)->name, (*b)->name);
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_help(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *response;
|
2018-07-20 03:14:02 +02:00
|
|
|
const jsmntok_t *cmdtok;
|
2019-02-04 11:55:42 +01:00
|
|
|
struct json_command **commands;
|
2019-02-04 11:55:35 +01:00
|
|
|
const struct json_command *one_cmd;
|
2018-01-29 01:30:15 +01:00
|
|
|
|
2018-07-20 03:14:02 +02:00
|
|
|
if (!param(cmd, buffer, params,
|
2018-12-16 05:50:06 +01:00
|
|
|
p_opt("command", param_tok, &cmdtok),
|
2018-07-20 03:14:02 +02:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2019-02-04 11:55:42 +01:00
|
|
|
commands = cmd->ld->jsonrpc->commands;
|
2018-01-29 01:30:15 +01:00
|
|
|
if (cmdtok) {
|
2019-02-04 11:55:35 +01:00
|
|
|
one_cmd = find_command(commands, buffer, cmdtok);
|
|
|
|
if (!one_cmd)
|
|
|
|
return command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND,
|
|
|
|
"Unknown command '%.*s'",
|
|
|
|
cmdtok->end - cmdtok->start,
|
|
|
|
buffer + cmdtok->start);
|
2020-08-06 02:27:19 +02:00
|
|
|
if (!deprecated_apis && one_cmd->deprecated)
|
|
|
|
return command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND,
|
|
|
|
"Deprecated command '%.*s'",
|
|
|
|
json_tok_full_len(cmdtok),
|
|
|
|
json_tok_full(buffer, cmdtok));
|
2019-02-04 11:55:35 +01:00
|
|
|
} else
|
|
|
|
one_cmd = NULL;
|
2018-01-29 01:30:15 +01:00
|
|
|
|
2019-05-20 12:14:00 +02:00
|
|
|
asort(commands, tal_count(commands), compare_commands_name, NULL);
|
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
response = json_stream_success(cmd);
|
2018-01-16 21:29:36 +01:00
|
|
|
json_array_start(response, "help");
|
2019-02-04 11:55:35 +01:00
|
|
|
for (size_t i = 0; i < tal_count(commands); i++) {
|
|
|
|
if (!one_cmd || one_cmd == commands[i])
|
|
|
|
json_add_help_command(cmd, response, commands[i]);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
json_array_end(response);
|
2018-01-29 01:30:15 +01:00
|
|
|
|
2019-07-26 04:27:01 +02:00
|
|
|
/* Tell cli this is simple enough to be formatted flat for humans */
|
|
|
|
json_add_string(response, "format-hint", "simple");
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_success(cmd, response);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 08:12:03 +01:00
|
|
|
static const struct json_command *find_cmd(const struct jsonrpc *rpc,
|
|
|
|
const char *buffer,
|
2016-01-21 21:11:48 +01:00
|
|
|
const jsmntok_t *tok)
|
|
|
|
{
|
2018-11-14 08:12:03 +01:00
|
|
|
struct json_command **commands = rpc->commands;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-11-22 23:11:26 +01:00
|
|
|
for (size_t i = 0; i < tal_count(commands); i++)
|
2018-12-03 15:14:43 +01:00
|
|
|
if (json_tok_streq(buffer, tok, commands[i]->name))
|
2018-11-22 23:11:26 +01:00
|
|
|
return commands[i];
|
2016-01-21 21:11:48 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
/* This can be called directly on shutdown, even with unfinished cmd */
|
|
|
|
static void destroy_command(struct command *cmd)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2018-10-19 03:17:48 +02:00
|
|
|
if (!cmd->jcon) {
|
2018-02-27 17:41:29 +01:00
|
|
|
log_debug(cmd->ld->log,
|
2016-01-21 21:11:48 +01:00
|
|
|
"Command returned result after jcon close");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-20 02:46:32 +01:00
|
|
|
list_del_from(&cmd->jcon->commands, &cmd->list);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:49:06 +01:00
|
|
|
struct command_result *command_raw_complete(struct command *cmd,
|
|
|
|
struct json_stream *result)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
json_stream_close(result, cmd);
|
|
|
|
|
|
|
|
/* If we have a jcon, it will free result for us. */
|
|
|
|
if (cmd->jcon)
|
|
|
|
tal_steal(cmd->jcon, result);
|
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
tal_free(cmd);
|
2018-12-16 05:49:06 +01:00
|
|
|
return &complete;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
2018-05-24 23:40:18 +02:00
|
|
|
|
2018-12-16 05:49:06 +01:00
|
|
|
struct command_result *command_success(struct command *cmd,
|
|
|
|
struct json_stream *result)
|
2018-12-08 01:35:56 +01:00
|
|
|
{
|
|
|
|
assert(cmd);
|
2019-06-12 02:38:55 +02:00
|
|
|
assert(cmd->json_stream == result);
|
|
|
|
json_object_end(result);
|
2019-06-17 07:52:26 +02:00
|
|
|
json_object_compat_end(result);
|
2018-12-08 01:35:56 +01:00
|
|
|
|
2018-12-16 05:49:06 +01:00
|
|
|
return command_raw_complete(cmd, result);
|
2018-12-08 01:35:56 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:49:06 +01:00
|
|
|
struct command_result *command_failed(struct command *cmd,
|
|
|
|
struct json_stream *result)
|
2018-01-27 03:05:49 +01:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
assert(cmd->json_stream == result);
|
2018-10-19 03:17:48 +02:00
|
|
|
/* Have to close error */
|
2019-06-12 02:38:55 +02:00
|
|
|
json_object_end(result);
|
2019-06-17 07:52:26 +02:00
|
|
|
json_object_compat_end(result);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2018-12-16 05:49:06 +01:00
|
|
|
return command_raw_complete(cmd, result);
|
2018-01-27 03:05:49 +01:00
|
|
|
}
|
2018-05-24 23:40:18 +02:00
|
|
|
|
2020-01-26 13:52:29 +01:00
|
|
|
struct command_result *command_fail(struct command *cmd, errcode_t code,
|
2018-12-16 05:49:06 +01:00
|
|
|
const char *fmt, ...)
|
2018-01-27 03:05:49 +01:00
|
|
|
{
|
2018-10-19 03:17:48 +02:00
|
|
|
const char *errmsg;
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *r;
|
2018-01-27 03:05:49 +01:00
|
|
|
va_list ap;
|
2018-10-19 03:17:48 +02:00
|
|
|
|
2018-01-27 03:05:49 +01:00
|
|
|
va_start(ap, fmt);
|
2018-10-19 03:17:48 +02:00
|
|
|
errmsg = tal_vfmt(cmd, fmt, ap);
|
2018-01-27 03:05:49 +01:00
|
|
|
va_end(ap);
|
2018-10-19 03:17:48 +02:00
|
|
|
r = json_stream_fail_nodata(cmd, code, errmsg);
|
|
|
|
|
2018-12-16 05:49:06 +01:00
|
|
|
return command_failed(cmd, r);
|
2018-01-27 03:05:49 +01:00
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-12-16 05:49:06 +01:00
|
|
|
struct command_result *command_still_pending(struct command *cmd)
|
2017-12-15 11:15:54 +01:00
|
|
|
{
|
2017-12-15 11:29:21 +01:00
|
|
|
notleak_with_children(cmd);
|
2017-12-15 11:15:54 +01:00
|
|
|
cmd->pending = true;
|
2019-06-12 02:38:55 +02:00
|
|
|
|
|
|
|
/* If we've started writing, wake reader. */
|
|
|
|
if (cmd->json_stream)
|
|
|
|
json_stream_flush(cmd->json_stream);
|
|
|
|
|
2018-12-16 05:49:06 +01:00
|
|
|
return &pending;
|
2017-12-15 11:15:54 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static void json_command_malformed(struct json_connection *jcon,
|
|
|
|
const char *id,
|
|
|
|
const char *error)
|
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
/* NULL writer is OK here, since we close it immediately. */
|
|
|
|
struct json_stream *js = jcon_new_json_stream(jcon, jcon, NULL);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
json_object_start(js, NULL);
|
|
|
|
json_add_string(js, "jsonrpc", "2.0");
|
|
|
|
json_add_literal(js, "id", id, strlen(id));
|
|
|
|
json_object_start(js, "error");
|
2020-01-26 13:52:29 +01:00
|
|
|
json_add_member(js, "code", false, "%" PRIerrcode, JSONRPC2_INVALID_REQUEST);
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_string(js, "message", error);
|
|
|
|
json_object_end(js);
|
2019-06-17 07:52:26 +02:00
|
|
|
json_object_compat_end(js);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
json_stream_close(js, NULL);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2020-10-12 07:33:51 +02:00
|
|
|
void json_notify_fmt(struct command *cmd,
|
|
|
|
enum log_level level,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
struct json_stream *js;
|
|
|
|
|
|
|
|
if (!cmd->send_notifications)
|
|
|
|
return;
|
|
|
|
|
|
|
|
js = json_stream_raw_for_cmd(cmd);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
json_object_start(js, NULL);
|
|
|
|
json_add_string(js, "jsonrpc", "2.0");
|
|
|
|
json_add_string(js, "method", "message");
|
|
|
|
json_object_start(js, "params");
|
|
|
|
json_add_string(js, "id", cmd->id);
|
|
|
|
json_add_string(js, "level", log_level_name(level));
|
|
|
|
json_add_string(js, "message", tal_vfmt(tmpctx, fmt, ap));
|
|
|
|
json_object_end(js);
|
|
|
|
json_object_end(js);
|
|
|
|
|
|
|
|
json_stream_double_cr(js);
|
|
|
|
json_stream_flush(js);
|
|
|
|
}
|
|
|
|
|
2018-12-08 01:35:56 +01:00
|
|
|
struct json_stream *json_stream_raw_for_cmd(struct command *cmd)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
struct json_stream *js;
|
|
|
|
|
2020-10-12 07:33:50 +02:00
|
|
|
/* Might have already opened it for a notification */
|
|
|
|
if (cmd->json_stream)
|
|
|
|
return cmd->json_stream;
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* If they still care about the result, attach it to them. */
|
|
|
|
if (cmd->jcon)
|
|
|
|
js = jcon_new_json_stream(cmd, cmd->jcon, cmd);
|
|
|
|
else
|
2019-02-18 03:44:29 +01:00
|
|
|
js = new_json_stream(cmd, cmd, NULL);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
assert(!cmd->json_stream);
|
|
|
|
cmd->json_stream = js;
|
2018-11-20 02:46:32 +01:00
|
|
|
return js;
|
|
|
|
}
|
|
|
|
|
2019-05-23 12:09:17 +02:00
|
|
|
void json_stream_log_suppress_for_cmd(struct json_stream *js,
|
|
|
|
const struct command *cmd)
|
|
|
|
{
|
|
|
|
const char *nm = cmd->json_cmd->name;
|
|
|
|
const char *s = tal_fmt(tmpctx, "Suppressing logging of %s command", nm);
|
2019-11-17 12:41:33 +01:00
|
|
|
log_io(cmd->jcon->log, LOG_IO_OUT, NULL, s, NULL, 0);
|
2019-05-23 12:09:17 +02:00
|
|
|
json_stream_log_suppress(js, strdup(nm));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
static struct json_stream *json_start(struct command *cmd)
|
|
|
|
{
|
2018-12-08 01:35:56 +01:00
|
|
|
struct json_stream *js = json_stream_raw_for_cmd(cmd);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
json_object_start(js, NULL);
|
|
|
|
json_add_string(js, "jsonrpc", "2.0");
|
|
|
|
json_add_literal(js, "id", cmd->id, strlen(cmd->id));
|
2018-11-20 02:46:32 +01:00
|
|
|
return js;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct json_stream *json_stream_success(struct command *cmd)
|
|
|
|
{
|
|
|
|
struct json_stream *r = json_start(cmd);
|
2019-06-12 02:38:55 +02:00
|
|
|
json_object_start(r, "result");
|
2018-11-20 02:46:32 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct json_stream *json_stream_fail_nodata(struct command *cmd,
|
2020-01-26 13:52:29 +01:00
|
|
|
errcode_t code,
|
2018-11-20 02:46:32 +01:00
|
|
|
const char *errmsg)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
struct json_stream *js = json_start(cmd);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
|
|
|
assert(code);
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
json_object_start(js, "error");
|
2020-01-26 13:52:29 +01:00
|
|
|
json_add_member(js, "code", false, "%" PRIerrcode, code);
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_string(js, "message", errmsg);
|
|
|
|
|
|
|
|
return js;
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct json_stream *json_stream_fail(struct command *cmd,
|
2020-01-26 13:52:29 +01:00
|
|
|
errcode_t code,
|
2018-11-20 02:46:32 +01:00
|
|
|
const char *errmsg)
|
|
|
|
{
|
|
|
|
struct json_stream *r = json_stream_fail_nodata(cmd, code, errmsg);
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
json_object_start(r, "data");
|
2018-11-20 02:46:32 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-09-27 09:50:50 +02:00
|
|
|
static struct command_result *command_exec(struct json_connection *jcon,
|
|
|
|
struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *request,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct command_result *res;
|
|
|
|
|
|
|
|
res = cmd->json_cmd->dispatch(cmd, buffer, request, params);
|
|
|
|
|
|
|
|
assert(res == ¶m_failed
|
|
|
|
|| res == &complete
|
|
|
|
|| res == &pending
|
|
|
|
|| res == &unknown);
|
|
|
|
|
|
|
|
/* If they didn't complete it, they must call command_still_pending.
|
|
|
|
* If they completed it, it's freed already. */
|
|
|
|
if (res == &pending)
|
|
|
|
assert(cmd->pending);
|
|
|
|
|
2020-06-16 12:29:32 +02:00
|
|
|
/* The command might outlive the connection. */
|
|
|
|
if (jcon)
|
|
|
|
list_for_each(&jcon->commands, cmd, list)
|
|
|
|
assert(cmd->pending);
|
2019-09-27 09:50:50 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A plugin hook to take over (fail/alter) RPC commands */
|
|
|
|
struct rpc_command_hook_payload {
|
|
|
|
struct command *cmd;
|
|
|
|
const char *buffer;
|
|
|
|
const jsmntok_t *request;
|
2021-01-30 14:35:52 +01:00
|
|
|
|
|
|
|
/* custom response/replace/error options plugins can have */
|
|
|
|
const char *custom_result;
|
|
|
|
const char *custom_error;
|
|
|
|
const jsmntok_t *custom_replace;
|
|
|
|
const char *custom_buffer;
|
2019-09-27 09:50:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void rpc_command_hook_serialize(struct rpc_command_hook_payload *p,
|
2021-06-02 17:33:23 +02:00
|
|
|
struct json_stream *s,
|
|
|
|
struct plugin *plugin)
|
2019-09-27 09:50:50 +02:00
|
|
|
{
|
2020-03-03 14:02:06 +01:00
|
|
|
const jsmntok_t *tok;
|
|
|
|
size_t i;
|
|
|
|
char *key;
|
2019-09-27 09:50:50 +02:00
|
|
|
json_object_start(s, "rpc_command");
|
2020-03-03 14:02:06 +01:00
|
|
|
|
|
|
|
#ifdef COMPAT_V081
|
|
|
|
if (deprecated_apis)
|
|
|
|
json_add_tok(s, "rpc_command", p->request, p->buffer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
json_for_each_obj(i, tok, p->request) {
|
|
|
|
key = tal_strndup(NULL, p->buffer + tok->start,
|
|
|
|
tok->end - tok->start);
|
|
|
|
json_add_tok(s, key, tok + 1, p->buffer);
|
|
|
|
tal_free(key);
|
|
|
|
}
|
2019-09-27 09:50:50 +02:00
|
|
|
json_object_end(s);
|
|
|
|
}
|
|
|
|
|
2019-12-07 01:09:12 +01:00
|
|
|
static void replace_command(struct rpc_command_hook_payload *p,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *replacetok)
|
|
|
|
{
|
2021-08-28 00:41:50 +02:00
|
|
|
const jsmntok_t *method = NULL, *params = NULL;
|
2019-12-07 01:09:12 +01:00
|
|
|
const char *bad;
|
|
|
|
|
|
|
|
/* Must contain "method", "params" and "id" */
|
|
|
|
if (replacetok->type != JSMN_OBJECT) {
|
|
|
|
bad = "'replace' must be an object";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
method = json_get_member(buffer, replacetok, "method");
|
|
|
|
if (!method) {
|
|
|
|
bad = "missing 'method'";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
params = json_get_member(buffer, replacetok, "params");
|
|
|
|
if (!params) {
|
|
|
|
bad = "missing 'params'";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!json_get_member(buffer, replacetok, "id")) {
|
|
|
|
bad = "missing 'id'";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->cmd->json_cmd = find_cmd(p->cmd->ld->jsonrpc, buffer, method);
|
|
|
|
if (!p->cmd->json_cmd) {
|
|
|
|
bad = tal_fmt(tmpctx, "redirected to unknown method '%.*s'",
|
|
|
|
method->end - method->start,
|
|
|
|
buffer + method->start);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-08-28 00:41:50 +02:00
|
|
|
// deprecated phase to give the possibility to all to migrate and stay safe
|
|
|
|
// from this more restrictive change.
|
|
|
|
if (!deprecated_apis) {
|
|
|
|
const jsmntok_t *jsonrpc = json_get_member(buffer, replacetok, "jsonrpc");
|
|
|
|
if (!jsonrpc || jsonrpc->type != JSMN_STRING || !json_tok_streq(buffer, jsonrpc, "2.0")) {
|
|
|
|
bad = "jsonrpc: \"2.0\" must be specified in the request";
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-08-26 11:11:40 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 01:09:12 +01:00
|
|
|
was_pending(command_exec(p->cmd->jcon, p->cmd, buffer, replacetok,
|
|
|
|
params));
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
was_pending(command_fail(p->cmd, JSONRPC2_INVALID_REQUEST,
|
|
|
|
"Bad response to 'rpc_command' hook: %s", bad));
|
|
|
|
}
|
|
|
|
|
2021-01-30 14:35:52 +01:00
|
|
|
static void rpc_command_hook_final(struct rpc_command_hook_payload *p STEALS)
|
2019-09-27 09:50:50 +02:00
|
|
|
{
|
2021-01-30 14:35:52 +01:00
|
|
|
const jsmntok_t *params;
|
2019-09-27 09:50:50 +02:00
|
|
|
|
2020-04-15 12:20:41 +02:00
|
|
|
/* Free payload with cmd */
|
|
|
|
tal_steal(p->cmd, p);
|
|
|
|
|
2021-01-30 14:35:52 +01:00
|
|
|
if (p->custom_result != NULL) {
|
|
|
|
struct json_stream *s = json_start(p->cmd);
|
|
|
|
json_add_jsonstr(s, "result", p->custom_result);
|
|
|
|
json_object_compat_end(s);
|
|
|
|
return was_pending(command_raw_complete(p->cmd, s));
|
|
|
|
}
|
|
|
|
if (p->custom_error != NULL) {
|
|
|
|
struct json_stream *s = json_start(p->cmd);
|
|
|
|
json_add_jsonstr(s, "error", p->custom_error);
|
|
|
|
json_object_compat_end(s);
|
|
|
|
return was_pending(command_raw_complete(p->cmd, s));
|
|
|
|
}
|
|
|
|
if (p->custom_replace != NULL)
|
|
|
|
return replace_command(p, p->custom_buffer, p->custom_replace);
|
|
|
|
|
|
|
|
/* If no plugin requested a change, just continue command execution. */
|
2019-09-27 09:50:50 +02:00
|
|
|
params = json_get_member(p->buffer, p->request, "params");
|
2021-01-30 14:35:52 +01:00
|
|
|
return was_pending(command_exec(p->cmd->jcon,
|
|
|
|
p->cmd,
|
|
|
|
p->buffer,
|
|
|
|
p->request,
|
|
|
|
params));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
rpc_command_hook_callback(struct rpc_command_hook_payload *p,
|
|
|
|
const char *buffer, const jsmntok_t *resulttok)
|
|
|
|
{
|
|
|
|
const struct lightningd *ld = p->cmd->ld;
|
|
|
|
const jsmntok_t *tok, *custom_return;
|
|
|
|
static char *error = "";
|
|
|
|
char *method;
|
2019-09-27 09:50:50 +02:00
|
|
|
|
2021-01-30 14:35:52 +01:00
|
|
|
if (!resulttok || !buffer)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
tok = json_get_member(buffer, resulttok, "result");
|
|
|
|
if (tok) {
|
|
|
|
if (!json_tok_streq(buffer, tok, "continue")) {
|
|
|
|
error = "'result' should only be 'continue'.";
|
|
|
|
goto log_error_and_skip;
|
2020-01-31 06:40:26 +01:00
|
|
|
}
|
2021-01-30 14:35:52 +01:00
|
|
|
/* plugin tells us to do nothing. just pass. */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* didn't just continue but hook was already modified by prior plugin */
|
|
|
|
if (p->custom_result != NULL ||
|
|
|
|
p->custom_error != NULL ||
|
|
|
|
p->custom_replace != NULL) {
|
|
|
|
/* get method name and log error (only the first time). */
|
|
|
|
tok = json_get_member(p->buffer, p->request, "method");
|
|
|
|
method = tal_strndup(p, p->buffer + tok->start, tok->end - tok->start);
|
|
|
|
log_unusual(ld->log, "rpc_command hook '%s' already modified, ignoring.", method );
|
|
|
|
rpc_command_hook_final(p);
|
|
|
|
return false;
|
2019-09-27 09:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the registered plugin did not respond with continue,
|
|
|
|
* it wants either to replace the request... */
|
|
|
|
tok = json_get_member(buffer, resulttok, "replace");
|
2021-01-30 14:35:52 +01:00
|
|
|
if (tok) {
|
|
|
|
/* We need to make copies here, as buffer and tokens
|
|
|
|
* can be reused. */
|
|
|
|
p->custom_replace = json_tok_copy(p, tok);
|
|
|
|
p->custom_buffer = tal_dup_talarr(p, char, buffer);
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-27 09:50:50 +02:00
|
|
|
|
|
|
|
/* ...or return a custom JSONRPC response. */
|
|
|
|
tok = json_get_member(buffer, resulttok, "return");
|
|
|
|
if (tok) {
|
|
|
|
custom_return = json_get_member(buffer, tok, "result");
|
|
|
|
if (custom_return) {
|
2021-01-30 14:35:52 +01:00
|
|
|
p->custom_result = json_strdup(p, buffer, custom_return);
|
|
|
|
return true;
|
2019-09-27 09:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
custom_return = json_get_member(buffer, tok, "error");
|
|
|
|
if (custom_return) {
|
2020-01-26 13:52:29 +01:00
|
|
|
errcode_t code;
|
2019-09-27 09:50:50 +02:00
|
|
|
const char *errmsg;
|
2020-01-26 13:52:29 +01:00
|
|
|
if (!json_to_errcode(buffer,
|
|
|
|
json_get_member(buffer, custom_return, "code"),
|
2021-01-30 14:35:52 +01:00
|
|
|
&code)) {
|
|
|
|
error = "'error' object does not contain a code.";
|
|
|
|
goto log_error_and_skip;
|
|
|
|
}
|
2019-09-27 09:50:50 +02:00
|
|
|
errmsg = json_strdup(tmpctx, buffer,
|
|
|
|
json_get_member(buffer, custom_return, "message"));
|
2021-01-30 14:35:52 +01:00
|
|
|
if (!errmsg) {
|
|
|
|
error = "'error' object does not contain a message.";
|
|
|
|
goto log_error_and_skip;
|
|
|
|
}
|
|
|
|
p->custom_error = json_strdup(p, buffer, custom_return);
|
|
|
|
return true;
|
2019-09-27 09:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 14:35:52 +01:00
|
|
|
log_error_and_skip:
|
|
|
|
/* Just log BROKEN errors. Give other plugins a chance. */
|
|
|
|
log_broken(ld->log, "Bad response to 'rpc_command' hook. %s", error);
|
|
|
|
return true;
|
2019-09-27 09:50:50 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 14:35:52 +01:00
|
|
|
REGISTER_PLUGIN_HOOK(rpc_command,
|
|
|
|
rpc_command_hook_callback,
|
|
|
|
rpc_command_hook_final,
|
|
|
|
rpc_command_hook_serialize,
|
|
|
|
struct rpc_command_hook_payload *);
|
2019-09-27 09:50:50 +02:00
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
/* We return struct command_result so command_fail return value has a natural
|
|
|
|
* sink; we don't actually use the result. */
|
|
|
|
static struct command_result *
|
|
|
|
parse_request(struct json_connection *jcon, const jsmntok_t tok[])
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2021-08-28 00:41:50 +02:00
|
|
|
const jsmntok_t *method, *id, *params;
|
2018-02-16 03:53:00 +01:00
|
|
|
struct command *c;
|
2019-09-27 09:50:50 +02:00
|
|
|
struct rpc_command_hook_payload *rpc_hook;
|
2020-05-05 03:12:19 +02:00
|
|
|
bool completed;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
if (tok[0].type != JSMN_OBJECT) {
|
2016-01-21 21:11:48 +01:00
|
|
|
json_command_malformed(jcon, "null",
|
|
|
|
"Expected {} for json command");
|
2018-12-16 05:52:06 +01:00
|
|
|
return NULL;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
method = json_get_member(jcon->buffer, tok, "method");
|
|
|
|
params = json_get_member(jcon->buffer, tok, "params");
|
|
|
|
id = json_get_member(jcon->buffer, tok, "id");
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
if (!id) {
|
|
|
|
json_command_malformed(jcon, "null", "No id");
|
2018-12-16 05:52:06 +01:00
|
|
|
return NULL;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
2021-08-26 11:11:40 +02:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
if (id->type != JSMN_STRING && id->type != JSMN_PRIMITIVE) {
|
2016-01-21 21:11:48 +01:00
|
|
|
json_command_malformed(jcon, "null",
|
|
|
|
"Expected string/primitive for id");
|
2018-12-16 05:52:06 +01:00
|
|
|
return NULL;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 07:09:48 +02:00
|
|
|
// Adding a deprecated phase to make sure that all the Core Lightning wrapper
|
2021-08-28 00:41:50 +02:00
|
|
|
// can migrate all the frameworks
|
|
|
|
if (!deprecated_apis) {
|
|
|
|
const jsmntok_t *jsonrpc = json_get_member(jcon->buffer, tok, "jsonrpc");
|
|
|
|
|
|
|
|
if (!jsonrpc || jsonrpc->type != JSMN_STRING || !json_tok_streq(jcon->buffer, jsonrpc, "2.0")) {
|
|
|
|
json_command_malformed(jcon, "null", "jsonrpc: \"2.0\" must be specified in the request");
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-08-26 11:11:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 08:12:03 +01:00
|
|
|
/* Allocate the command off of the `jsonrpc` object and not
|
|
|
|
* the connection since the command may outlive `conn`. */
|
|
|
|
c = tal(jcon->ld->jsonrpc, struct command);
|
2018-02-16 03:53:00 +01:00
|
|
|
c->jcon = jcon;
|
2020-10-12 07:33:50 +02:00
|
|
|
c->send_notifications = jcon->notifications_enabled;
|
2018-02-16 03:53:00 +01:00
|
|
|
c->ld = jcon->ld;
|
|
|
|
c->pending = false;
|
2019-06-12 02:38:55 +02:00
|
|
|
c->json_stream = NULL;
|
2018-02-16 03:53:00 +01:00
|
|
|
c->id = tal_strndup(c,
|
2018-12-16 05:48:06 +01:00
|
|
|
json_tok_full(jcon->buffer, id),
|
|
|
|
json_tok_full_len(id));
|
2018-09-12 17:56:39 +02:00
|
|
|
c->mode = CMD_NORMAL;
|
2018-11-20 02:46:32 +01:00
|
|
|
list_add_tail(&jcon->commands, &c->list);
|
2018-10-19 03:17:48 +02:00
|
|
|
tal_add_destructor(c, destroy_command);
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
if (!method || !params) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(c, JSONRPC2_INVALID_REQUEST,
|
|
|
|
method ? "No params" : "No method");
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (method->type != JSMN_STRING) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(c, JSONRPC2_INVALID_REQUEST,
|
|
|
|
"Expected string for method");
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2019-01-20 15:58:51 +01:00
|
|
|
c->json_cmd = find_cmd(jcon->ld->jsonrpc, jcon->buffer, method);
|
|
|
|
if (!c->json_cmd) {
|
|
|
|
return command_fail(
|
|
|
|
c, JSONRPC2_METHOD_NOT_FOUND, "Unknown command '%.*s'",
|
|
|
|
method->end - method->start, jcon->buffer + method->start);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
2018-07-26 23:19:37 +02:00
|
|
|
if (c->json_cmd->deprecated && !deprecated_apis) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(c, JSONRPC2_METHOD_NOT_FOUND,
|
2020-08-06 02:27:19 +02:00
|
|
|
"Command %.*s is deprecated",
|
|
|
|
json_tok_full_len(method),
|
|
|
|
json_tok_full(jcon->buffer, method));
|
2018-01-16 20:43:14 +01:00
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2021-11-10 10:08:49 +01:00
|
|
|
if (jcon->ld->state == LD_STATE_SHUTDOWN) {
|
|
|
|
return command_fail(c, LIGHTNINGD_SHUTDOWN,
|
|
|
|
"lightningd is shutting down");
|
|
|
|
}
|
|
|
|
|
2020-04-21 03:09:03 +02:00
|
|
|
rpc_hook = tal(c, struct rpc_command_hook_payload);
|
2019-09-27 09:50:50 +02:00
|
|
|
rpc_hook->cmd = c;
|
|
|
|
/* Duplicate since we might outlive the connection */
|
2020-02-27 03:17:01 +01:00
|
|
|
rpc_hook->buffer = tal_dup_talarr(rpc_hook, char, jcon->buffer);
|
|
|
|
rpc_hook->request = tal_dup_talarr(rpc_hook, jsmntok_t, tok);
|
2019-09-27 09:50:50 +02:00
|
|
|
|
2021-01-30 14:35:52 +01:00
|
|
|
/* NULL the custom_ values for the hooks */
|
|
|
|
rpc_hook->custom_result = NULL;
|
|
|
|
rpc_hook->custom_error = NULL;
|
|
|
|
rpc_hook->custom_replace = NULL;
|
|
|
|
rpc_hook->custom_buffer = NULL;
|
|
|
|
|
2020-05-05 03:12:19 +02:00
|
|
|
db_begin_transaction(jcon->ld->wallet->db);
|
|
|
|
completed = plugin_hook_call_rpc_command(jcon->ld, rpc_hook);
|
|
|
|
db_commit_transaction(jcon->ld->wallet->db);
|
|
|
|
|
|
|
|
/* If it's deferred, mark it (otherwise, it's completed) */
|
|
|
|
if (!completed)
|
|
|
|
return command_still_pending(c);
|
|
|
|
return NULL;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
/* Mutual recursion */
|
2018-11-20 02:46:32 +01:00
|
|
|
static struct io_plan *stream_out_complete(struct io_conn *conn,
|
2018-11-20 02:46:32 +01:00
|
|
|
struct json_stream *js,
|
2018-11-20 02:46:32 +01:00
|
|
|
struct json_connection *jcon);
|
2018-09-26 20:57:43 +02:00
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
static struct io_plan *start_json_stream(struct io_conn *conn,
|
|
|
|
struct json_connection *jcon)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
/* If something has created an output buffer, start streaming. */
|
2018-11-20 02:46:32 +01:00
|
|
|
if (tal_count(jcon->js_arr))
|
|
|
|
return json_stream_output(jcon->js_arr[0], conn,
|
2018-11-20 02:46:32 +01:00
|
|
|
stream_out_complete, jcon);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* Tell reader it can run next command. */
|
|
|
|
io_wake(conn);
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
/* Once the stop_conn conn is drained, we can shut down. */
|
lightningd: shutdown plugins after subdaemons and assert no write access to db
because:
- shutdown_subdaemons can trigger db write, comments in that function say so at least
- resurrecting the main event loop with subdaemons still running is counter productive
in shutting down activity (such as htlc's, hook_calls etc.)
- custom behavior injected by plugins via hooks should be consistent, see test
in previous commmit
IDEA:
in shutdown_plugins, when starting new io_loop:
- A plugin that is still running can return a jsonrpc_request response, this triggers
response_cb, which cannot be handled because subdaemons are gone -> so any response_cb should be blocked/aborted
- jsonrpc is still there, so users (such as plugins) can make new jsonrpc_request's which
cannot be handled because subdaemons are gone -> so new rpc_request should also be blocked
- But we do want to send/receive notifications and log messages (handled in jsonrpc as jsonrpc_notification)
as these do not trigger subdaemon calls or db_write's
Log messages and notifications do not have "id" field, where jsonrpc_request *do* have an "id" field
PLAN (hypothesis):
- hack into plugin_read_json_one OR plugin_response_handle to filter-out json with
an "id" field, this should
block/abandon any jsonrpc_request responses (and new jsonrpc_requests for plugins?)
Q. Can internal (so not via plugin) jsonrpc_requests called in the main io_loop return/revive in
the shutdown io_loop?
A. No. All code under lightningd/ returning command_still_pending depends on either a subdaemon, timer or
plugin. In shutdown loop the subdaemons are dead, timer struct cleared and plugins will be taken
care of (in next commits).
fixup: we can only io_break the main io_loop once
2021-11-10 10:39:02 +01:00
|
|
|
if (jcon->ld->stop_conn == conn && jcon->ld->state == LD_STATE_RUNNING) {
|
2019-06-12 02:38:55 +02:00
|
|
|
/* Return us to toplevel lightningd.c */
|
|
|
|
io_break(jcon->ld);
|
|
|
|
/* We never come back. */
|
|
|
|
return io_out_wait(conn, conn, io_never, conn);
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
return io_out_wait(conn, jcon, start_json_stream, jcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Command has completed writing, and we've written it all out to conn. */
|
|
|
|
static struct io_plan *stream_out_complete(struct io_conn *conn,
|
2018-11-20 02:46:32 +01:00
|
|
|
struct json_stream *js,
|
2018-11-20 02:46:32 +01:00
|
|
|
struct json_connection *jcon)
|
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
jcon_remove_json_stream(jcon, js);
|
2018-12-16 05:41:06 +01:00
|
|
|
tal_free(js);
|
2018-10-19 03:17:48 +02:00
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
/* Wait for more output. */
|
2018-11-20 02:46:32 +01:00
|
|
|
return start_json_stream(conn, jcon);
|
2018-09-26 20:57:43 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static struct io_plan *read_json(struct io_conn *conn,
|
|
|
|
struct json_connection *jcon)
|
|
|
|
{
|
2020-08-20 15:40:32 +02:00
|
|
|
bool complete;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
if (jcon->len_read)
|
2019-11-17 12:41:33 +01:00
|
|
|
log_io(jcon->log, LOG_IO_IN, NULL, "",
|
2018-10-19 03:17:48 +02:00
|
|
|
jcon->buffer + jcon->used, jcon->len_read);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
/* Resize larger if we're full. */
|
|
|
|
jcon->used += jcon->len_read;
|
|
|
|
if (jcon->used == tal_count(jcon->buffer))
|
|
|
|
tal_resize(&jcon->buffer, jcon->used * 2);
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* We wait for pending output to be consumed, to avoid DoS */
|
2018-11-20 02:46:32 +01:00
|
|
|
if (tal_count(jcon->js_arr) != 0) {
|
2018-11-20 02:46:32 +01:00
|
|
|
jcon->len_read = 0;
|
|
|
|
return io_wait(conn, conn, read_json, jcon);
|
|
|
|
}
|
|
|
|
|
2020-08-20 15:40:34 +02:00
|
|
|
if (!json_parse_input(&jcon->input_parser, &jcon->input_toks,
|
|
|
|
jcon->buffer, jcon->used,
|
2020-08-20 15:40:32 +02:00
|
|
|
&complete)) {
|
2020-12-04 13:30:10 +01:00
|
|
|
json_command_malformed(
|
|
|
|
jcon, "null",
|
|
|
|
tal_fmt(tmpctx, "Invalid token in json input: '%s'",
|
|
|
|
tal_strndup(tmpctx, jcon->buffer, jcon->used)));
|
2020-08-20 15:40:32 +02:00
|
|
|
return io_halfclose(conn);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2020-08-20 15:40:32 +02:00
|
|
|
if (!complete)
|
|
|
|
goto read_more;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Empty buffer? (eg. just whitespace). */
|
2020-08-20 15:40:34 +02:00
|
|
|
if (tal_count(jcon->input_toks) == 1) {
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->used = 0;
|
2020-08-20 15:40:34 +02:00
|
|
|
|
|
|
|
/* Reset parser. */
|
|
|
|
jsmn_init(&jcon->input_parser);
|
|
|
|
toks_reset(jcon->input_toks);
|
2016-01-21 21:11:48 +01:00
|
|
|
goto read_more;
|
|
|
|
}
|
|
|
|
|
2020-08-20 15:40:34 +02:00
|
|
|
parse_request(jcon, jcon->input_toks);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
/* Remove first {}. */
|
2020-08-20 15:40:34 +02:00
|
|
|
memmove(jcon->buffer, jcon->buffer + jcon->input_toks[0].end,
|
|
|
|
tal_count(jcon->buffer) - jcon->input_toks[0].end);
|
|
|
|
jcon->used -= jcon->input_toks[0].end;
|
|
|
|
|
|
|
|
/* Reset parser. */
|
|
|
|
jsmn_init(&jcon->input_parser);
|
|
|
|
toks_reset(jcon->input_toks);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
/* If we have more to process, try again. FIXME: this still gets
|
|
|
|
* first priority in io_loop, so can starve others. Hack would be
|
|
|
|
* a (non-zero) timer, but better would be to have io_loop avoid
|
|
|
|
* such livelock */
|
|
|
|
if (jcon->used) {
|
2018-11-20 02:46:32 +01:00
|
|
|
jcon->len_read = 0;
|
2018-10-19 03:17:48 +02:00
|
|
|
return io_always(conn, read_json, jcon);
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
read_more:
|
|
|
|
return io_read_partial(conn, jcon->buffer + jcon->used,
|
|
|
|
tal_count(jcon->buffer) - jcon->used,
|
|
|
|
&jcon->len_read, read_json, jcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *jcon_connected(struct io_conn *conn,
|
2017-08-28 18:09:01 +02:00
|
|
|
struct lightningd *ld)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct json_connection *jcon;
|
|
|
|
|
2018-12-16 05:43:06 +01:00
|
|
|
/* We live as long as the connection, so we're not a leak. */
|
|
|
|
jcon = notleak(tal(conn, struct json_connection));
|
2018-10-19 03:17:48 +02:00
|
|
|
jcon->conn = conn;
|
2017-08-28 18:09:01 +02:00
|
|
|
jcon->ld = ld;
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->used = 0;
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->buffer = tal_arr(jcon, char, 64);
|
2018-11-20 02:46:32 +01:00
|
|
|
jcon->js_arr = tal_arr(jcon, struct json_stream *, 0);
|
2018-10-19 03:17:48 +02:00
|
|
|
jcon->len_read = 0;
|
2020-08-20 15:40:34 +02:00
|
|
|
jsmn_init(&jcon->input_parser);
|
|
|
|
jcon->input_toks = toks_alloc(jcon);
|
2020-10-12 07:33:50 +02:00
|
|
|
jcon->notifications_enabled = false;
|
2018-11-20 02:46:32 +01:00
|
|
|
list_head_init(&jcon->commands);
|
2018-02-16 03:53:00 +01:00
|
|
|
|
2017-09-28 05:31:36 +02:00
|
|
|
/* We want to log on destruction, so we free this in destructor. */
|
2019-11-18 01:27:18 +01:00
|
|
|
jcon->log = new_log(ld->log_book, ld->log_book, NULL, "jsonrpc#%i",
|
2019-11-18 01:27:15 +01:00
|
|
|
io_conn_fd(conn));
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2017-09-28 05:31:36 +02:00
|
|
|
tal_add_destructor(jcon, destroy_jcon);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
/* Note that write_json and read_json alternate manually, by waking
|
|
|
|
* each other. It would be simpler to not use a duplex io, and have
|
|
|
|
* read_json parse one command, then io_wait() for command completion
|
|
|
|
* and go to write_json.
|
|
|
|
*
|
|
|
|
* However, if we ever have notifications, this neat cmd-response
|
|
|
|
* pattern would break down, so we use this trick. */
|
2016-01-21 21:11:48 +01:00
|
|
|
return io_duplex(conn,
|
2018-10-19 03:17:48 +02:00
|
|
|
read_json(conn, jcon),
|
2018-11-20 02:46:32 +01:00
|
|
|
start_json_stream(conn, jcon));
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *incoming_jcon_connected(struct io_conn *conn,
|
2017-08-28 18:09:01 +02:00
|
|
|
struct lightningd *ld)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2017-12-15 11:22:57 +01:00
|
|
|
/* Lifetime of JSON conn is limited to fd connect time. */
|
|
|
|
return jcon_connected(notleak(conn), ld);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-04 11:55:42 +01:00
|
|
|
static void destroy_json_command(struct json_command *command, struct jsonrpc *rpc)
|
|
|
|
{
|
2019-02-04 11:55:42 +01:00
|
|
|
strmap_del(&rpc->usagemap, command->name, NULL);
|
2019-02-04 11:55:42 +01:00
|
|
|
for (size_t i = 0; i < tal_count(rpc->commands); i++) {
|
|
|
|
if (rpc->commands[i] == command) {
|
|
|
|
tal_arr_remove(&rpc->commands, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:55:42 +01:00
|
|
|
static bool command_add(struct jsonrpc *rpc, struct json_command *command)
|
2018-11-14 08:12:03 +01:00
|
|
|
{
|
|
|
|
size_t count = tal_count(rpc->commands);
|
|
|
|
|
|
|
|
/* Check that we don't clobber a method */
|
|
|
|
for (size_t i = 0; i < count; i++)
|
2018-12-03 15:14:43 +01:00
|
|
|
if (streq(rpc->commands[i]->name, command->name))
|
2018-11-14 08:12:03 +01:00
|
|
|
return false;
|
|
|
|
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&rpc->commands, command);
|
2018-11-14 08:12:03 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:55:42 +01:00
|
|
|
/* Built-in commands get called to construct usage string via param() */
|
|
|
|
static void setup_command_usage(struct lightningd *ld,
|
|
|
|
struct json_command *command)
|
2018-11-22 23:11:26 +01:00
|
|
|
{
|
2019-02-04 11:55:42 +01:00
|
|
|
const struct command_result *res;
|
|
|
|
struct command *dummy;
|
|
|
|
|
|
|
|
/* Call it with minimal cmd, to fill out usagemap */
|
|
|
|
dummy = tal(tmpctx, struct command);
|
|
|
|
dummy->mode = CMD_USAGE;
|
|
|
|
dummy->ld = ld;
|
|
|
|
dummy->json_cmd = command;
|
|
|
|
res = command->dispatch(dummy, NULL, NULL, NULL);
|
|
|
|
assert(res == ¶m_failed);
|
|
|
|
assert(strmap_get(&ld->jsonrpc->usagemap, command->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command,
|
|
|
|
const char *usage TAKES)
|
|
|
|
{
|
|
|
|
if (!command_add(rpc, command))
|
2019-02-04 11:55:42 +01:00
|
|
|
return false;
|
2019-02-04 11:55:42 +01:00
|
|
|
usage = tal_strdup(command, usage);
|
|
|
|
strmap_add(&rpc->usagemap, command->name, usage);
|
2019-02-04 11:55:42 +01:00
|
|
|
tal_add_destructor2(command, destroy_json_command, rpc);
|
|
|
|
return true;
|
2018-11-22 23:11:26 +01:00
|
|
|
}
|
|
|
|
|
2019-02-04 11:55:42 +01:00
|
|
|
static bool jsonrpc_command_add_perm(struct lightningd *ld,
|
|
|
|
struct jsonrpc *rpc,
|
|
|
|
struct json_command *command)
|
|
|
|
{
|
|
|
|
if (!command_add(rpc, command))
|
|
|
|
return false;
|
|
|
|
setup_command_usage(ld, command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-14 05:50:07 +02:00
|
|
|
static void destroy_jsonrpc(struct jsonrpc *jsonrpc)
|
|
|
|
{
|
|
|
|
strmap_clear(&jsonrpc->usagemap);
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:42:05 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
static void memleak_help_jsonrpc(struct htable *memtable,
|
|
|
|
struct jsonrpc *jsonrpc)
|
|
|
|
{
|
|
|
|
memleak_remove_strmap(memtable, &jsonrpc->usagemap);
|
|
|
|
}
|
|
|
|
#endif /* DEVELOPER */
|
|
|
|
|
2019-02-04 11:55:42 +01:00
|
|
|
void jsonrpc_setup(struct lightningd *ld)
|
2018-11-14 08:12:03 +01:00
|
|
|
{
|
|
|
|
struct json_command **commands = get_cmdlist();
|
|
|
|
|
2019-02-04 11:55:42 +01:00
|
|
|
ld->jsonrpc = tal(ld, struct jsonrpc);
|
2019-02-04 11:55:42 +01:00
|
|
|
strmap_init(&ld->jsonrpc->usagemap);
|
2019-02-04 11:55:42 +01:00
|
|
|
ld->jsonrpc->commands = tal_arr(ld->jsonrpc, struct json_command *, 0);
|
2018-11-14 08:12:03 +01:00
|
|
|
for (size_t i=0; i<num_cmdlist; i++) {
|
2019-02-04 11:55:42 +01:00
|
|
|
if (!jsonrpc_command_add_perm(ld, ld->jsonrpc, commands[i]))
|
2019-02-04 11:55:42 +01:00
|
|
|
fatal("Cannot add duplicate command %s",
|
|
|
|
commands[i]->name);
|
2018-11-14 08:12:03 +01:00
|
|
|
}
|
2019-02-04 11:55:42 +01:00
|
|
|
ld->jsonrpc->rpc_listener = NULL;
|
2019-06-14 05:50:07 +02:00
|
|
|
tal_add_destructor(ld->jsonrpc, destroy_jsonrpc);
|
2019-09-06 06:42:05 +02:00
|
|
|
memleak_add_helper(ld->jsonrpc, memleak_help_jsonrpc);
|
2018-11-14 08:12:03 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 01:39:25 +01:00
|
|
|
bool command_usage_only(const struct command *cmd)
|
|
|
|
{
|
|
|
|
return cmd->mode == CMD_USAGE;
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:55:42 +01:00
|
|
|
void command_set_usage(struct command *cmd, const char *usage TAKES)
|
2018-12-08 01:39:25 +01:00
|
|
|
{
|
2019-02-04 11:55:42 +01:00
|
|
|
usage = tal_strdup(cmd->ld, usage);
|
|
|
|
if (!strmap_add(&cmd->ld->jsonrpc->usagemap, cmd->json_cmd->name, usage))
|
|
|
|
fatal("Two usages for command %s?", cmd->json_cmd->name);
|
2018-12-08 01:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool command_check_only(const struct command *cmd)
|
|
|
|
{
|
|
|
|
return cmd->mode == CMD_CHECK;
|
|
|
|
}
|
|
|
|
|
2018-11-22 11:37:08 +01:00
|
|
|
void jsonrpc_listen(struct jsonrpc *jsonrpc, struct lightningd *ld)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
2020-01-24 03:20:45 +01:00
|
|
|
int fd, old_umask, new_umask;
|
2018-11-14 08:12:03 +01:00
|
|
|
const char *rpc_filename = ld->rpc_filename;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-11-22 11:37:08 +01:00
|
|
|
/* Should not initialize it twice. */
|
|
|
|
assert(!jsonrpc->rpc_listener);
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
if (streq(rpc_filename, "/dev/tty")) {
|
|
|
|
fd = open(rpc_filename, O_RDWR);
|
|
|
|
if (fd == -1)
|
|
|
|
err(1, "Opening %s", rpc_filename);
|
2017-12-15 11:22:57 +01:00
|
|
|
/* Technically this is a leak, but there's only one */
|
|
|
|
notleak(io_new_conn(ld, fd, jcon_connected, ld));
|
2018-11-22 11:37:08 +01:00
|
|
|
return;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
2018-01-08 21:09:01 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
errx(1, "domain socket creation failed");
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
if (strlen(rpc_filename) + 1 > sizeof(addr.sun_path))
|
|
|
|
errx(1, "rpc filename '%s' too long", rpc_filename);
|
|
|
|
strcpy(addr.sun_path, rpc_filename);
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
/* Of course, this is racy! */
|
|
|
|
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0)
|
|
|
|
errx(1, "rpc filename '%s' in use", rpc_filename);
|
|
|
|
unlink(rpc_filename);
|
|
|
|
|
2020-01-24 03:20:45 +01:00
|
|
|
/* Set the umask according to the desired file mode. */
|
|
|
|
new_umask = ld->rpc_filemode ^ 0777;
|
|
|
|
old_umask = umask(new_umask);
|
2016-01-21 21:11:48 +01:00
|
|
|
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
|
|
|
|
err(1, "Binding rpc socket to '%s'", rpc_filename);
|
|
|
|
umask(old_umask);
|
|
|
|
|
2019-08-10 05:53:10 +02:00
|
|
|
if (listen(fd, 128) != 0)
|
2016-01-21 21:11:48 +01:00
|
|
|
err(1, "Listening on '%s'", rpc_filename);
|
2018-11-22 11:37:08 +01:00
|
|
|
jsonrpc->rpc_listener = io_new_listener(
|
|
|
|
ld->rpc_filename, fd, incoming_jcon_connected, ld);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
2018-02-15 00:45:04 +01:00
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
static struct command_result *param_command(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
const jsmntok_t **out)
|
2018-11-27 00:48:18 +01:00
|
|
|
{
|
|
|
|
cmd->json_cmd = find_cmd(cmd->jcon->ld->jsonrpc, buffer, tok);
|
2018-12-16 05:50:06 +01:00
|
|
|
if (cmd->json_cmd) {
|
|
|
|
*out = tok;
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-11-27 00:48:18 +01:00
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND,
|
|
|
|
"Unknown command '%.*s'",
|
|
|
|
tok->end - tok->start, buffer + tok->start);
|
2018-11-27 00:48:18 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 18:14:15 +01:00
|
|
|
struct jsonrpc_notification *jsonrpc_notification_start(const tal_t *ctx, const char *method)
|
|
|
|
{
|
|
|
|
struct jsonrpc_notification *n = tal(ctx, struct jsonrpc_notification);
|
|
|
|
n->method = tal_strdup(n, method);
|
2019-02-18 03:44:29 +01:00
|
|
|
n->stream = new_json_stream(n, NULL, NULL);
|
2018-12-11 18:14:15 +01:00
|
|
|
json_object_start(n->stream, NULL);
|
|
|
|
json_add_string(n->stream, "jsonrpc", "2.0");
|
|
|
|
json_add_string(n->stream, "method", method);
|
|
|
|
json_object_start(n->stream, "params");
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void jsonrpc_notification_end(struct jsonrpc_notification *n)
|
|
|
|
{
|
|
|
|
json_object_end(n->stream); /* closes '.params' */
|
|
|
|
json_object_end(n->stream); /* closes '.' */
|
2019-06-12 02:38:55 +02:00
|
|
|
|
|
|
|
/* We guarantee to have \n\n at end of each response. */
|
|
|
|
json_stream_append(n->stream, "\n\n", strlen("\n\n"));
|
2018-12-11 18:14:15 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 14:02:34 +01:00
|
|
|
struct jsonrpc_request *jsonrpc_request_start_(
|
2019-02-18 03:44:29 +01:00
|
|
|
const tal_t *ctx, const char *method, struct log *log,
|
2020-10-12 07:33:50 +02:00
|
|
|
void (*notify_cb)(const char *buffer,
|
|
|
|
const jsmntok_t *methodtok,
|
|
|
|
const jsmntok_t *paramtoks,
|
|
|
|
const jsmntok_t *idtok,
|
|
|
|
void *),
|
2018-12-21 14:02:34 +01:00
|
|
|
void (*response_cb)(const char *buffer, const jsmntok_t *toks,
|
|
|
|
const jsmntok_t *idtok, void *),
|
|
|
|
void *response_cb_arg)
|
|
|
|
{
|
|
|
|
struct jsonrpc_request *r = tal(ctx, struct jsonrpc_request);
|
|
|
|
static u64 next_request_id = 0;
|
|
|
|
r->id = next_request_id++;
|
2020-10-12 07:33:50 +02:00
|
|
|
r->notify_cb = notify_cb;
|
2018-12-21 14:02:34 +01:00
|
|
|
r->response_cb = response_cb;
|
|
|
|
r->response_cb_arg = response_cb_arg;
|
|
|
|
r->method = NULL;
|
2019-02-18 03:44:29 +01:00
|
|
|
r->stream = new_json_stream(r, NULL, log);
|
2018-12-21 14:02:34 +01:00
|
|
|
|
|
|
|
/* If no method is specified we don't prefill the JSON-RPC
|
|
|
|
* request with the header. This serves as an escape hatch to
|
|
|
|
* get a raw request, but get a valid request-id assigned. */
|
|
|
|
if (method != NULL) {
|
|
|
|
r->method = tal_strdup(r, method);
|
|
|
|
json_object_start(r->stream, NULL);
|
|
|
|
json_add_string(r->stream, "jsonrpc", "2.0");
|
|
|
|
json_add_u64(r->stream, "id", r->id);
|
|
|
|
json_add_string(r->stream, "method", method);
|
|
|
|
json_object_start(r->stream, "params");
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void jsonrpc_request_end(struct jsonrpc_request *r)
|
|
|
|
{
|
|
|
|
json_object_end(r->stream); /* closes '.params' */
|
|
|
|
json_object_end(r->stream); /* closes '.' */
|
2019-06-12 02:38:55 +02:00
|
|
|
|
|
|
|
/* We guarantee to have \n\n at end of each response. */
|
|
|
|
json_stream_append(r->stream, "\n\n", strlen("\n\n"));
|
2018-12-21 14:02:34 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 01:39:27 +01:00
|
|
|
/* We add this destructor as a canary to detect cmd failing. */
|
|
|
|
static void destroy_command_canary(struct command *cmd, bool *failed)
|
|
|
|
{
|
|
|
|
*failed = true;
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_check(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2018-11-27 00:48:18 +01:00
|
|
|
{
|
|
|
|
jsmntok_t *mod_params;
|
|
|
|
const jsmntok_t *name_tok;
|
2018-12-08 01:39:27 +01:00
|
|
|
bool failed;
|
2018-11-27 00:48:18 +01:00
|
|
|
struct json_stream *response;
|
2018-12-17 04:51:25 +01:00
|
|
|
struct command_result *res;
|
2018-11-27 00:48:18 +01:00
|
|
|
|
|
|
|
if (cmd->mode == CMD_USAGE) {
|
|
|
|
mod_params = NULL;
|
|
|
|
} else {
|
|
|
|
mod_params = json_tok_copy(cmd, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, mod_params,
|
2018-12-16 05:50:06 +01:00
|
|
|
p_req("command_to_check", param_command, &name_tok),
|
2018-12-08 01:39:25 +01:00
|
|
|
p_opt_any(),
|
2018-11-27 00:48:18 +01:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2018-11-27 00:48:18 +01:00
|
|
|
|
|
|
|
/* Point name_tok to the name, not the value */
|
|
|
|
if (params->type == JSMN_OBJECT)
|
|
|
|
name_tok--;
|
|
|
|
|
2019-07-26 04:23:47 +02:00
|
|
|
json_tok_remove(&mod_params, mod_params, name_tok, 1);
|
2018-11-27 00:48:18 +01:00
|
|
|
|
|
|
|
cmd->mode = CMD_CHECK;
|
2018-12-08 01:39:27 +01:00
|
|
|
failed = false;
|
|
|
|
tal_add_destructor2(cmd, destroy_command_canary, &failed);
|
2018-12-17 04:51:25 +01:00
|
|
|
res = cmd->json_cmd->dispatch(cmd, buffer, mod_params, mod_params);
|
|
|
|
|
|
|
|
/* CMD_CHECK always makes it "fail" parameter parsing. */
|
|
|
|
assert(res == ¶m_failed);
|
2018-11-27 00:48:18 +01:00
|
|
|
|
2018-12-08 01:39:27 +01:00
|
|
|
if (failed)
|
2018-12-17 04:51:25 +01:00
|
|
|
return res;
|
2018-11-27 00:48:18 +01:00
|
|
|
|
|
|
|
response = json_stream_success(cmd);
|
2018-12-05 03:03:46 +01:00
|
|
|
json_add_string(response, "command_to_check", cmd->json_cmd->name);
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_success(cmd, response);
|
2018-11-27 00:48:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command check_command = {
|
|
|
|
"check",
|
2019-05-22 16:08:16 +02:00
|
|
|
"utility",
|
2018-11-27 00:48:18 +01:00
|
|
|
json_check,
|
|
|
|
"Don't run {command_to_check}, just verify parameters.",
|
|
|
|
.verbose = "check command_to_check [parameters...]\n"
|
|
|
|
};
|
|
|
|
|
|
|
|
AUTODATA(json_command, &check_command);
|
2020-10-12 07:33:50 +02:00
|
|
|
|
|
|
|
static struct command_result *json_notifications(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
bool *enable;
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("enable", param_bool, &enable),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
2020-11-06 01:30:14 +01:00
|
|
|
/* Catch the case where they sent this command then hung up. */
|
|
|
|
if (cmd->jcon)
|
|
|
|
cmd->jcon->notifications_enabled = *enable;
|
2020-10-12 07:33:50 +02:00
|
|
|
return command_success(cmd, json_stream_success(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command notifications_command = {
|
|
|
|
"notifications",
|
|
|
|
"utility",
|
|
|
|
json_notifications,
|
|
|
|
"Enable notifications for {level} (or 'false' to disable)",
|
|
|
|
};
|
|
|
|
|
|
|
|
AUTODATA(json_command, ¬ifications_command);
|