Commit f471a2009f for strongswan.org
commit f471a2009f3c79edb79c3e93b58a40d0fd6a20d5
Author: Tobias Brunner <tobias@strongswan.org>
Date: Mon Nov 30 12:11:59 2026 +0100
swanctl: Dynamically connect to VICI socket from commands
This avoids that e.g. `--version` without `--daemon/-d` requires a
connection to the daemon (otherwise, it printed an error if the daemon
was not running). Similarly, it allows us to first parse command line
args and load configs and only then connect to the daemon.
Also aligned the swanctl version output to the usage output, which was
not ideal when trying to process the command's output.
diff --git a/src/swanctl/command.c b/src/swanctl/command.c
index 368a7e389c..4ff0781b11 100644
--- a/src/swanctl/command.c
+++ b/src/swanctl/command.c
@@ -53,6 +53,11 @@ static int help_idx;
*/
static char *uri = NULL;
+/**
+ * VICI connection if command opened one
+ */
+static vici_conn_t *conn = NULL;
+
static int argc;
static char **argv;
@@ -372,27 +377,6 @@ static void cleanup()
options->destroy(options);
}
-/**
- * Open vici connection, call a command
- */
-static int call_command(command_t *cmd)
-{
- vici_conn_t *conn;
- int ret;
-
- conn = vici_connect(uri);
- if (!conn)
- {
- ret = errno;
- command_usage("connecting to '%s' URI failed: %s",
- uri ?: "default", strerror(errno));
- return ret;
- }
- ret = cmd->call(conn);
- vici_disconnect(conn);
- return ret;
-}
-
/*
* Described in header
*/
@@ -434,18 +418,32 @@ int command_init(int c, char *v[])
return command_usage(op != EOF ? "invalid command" : NULL);
}
-
/*
* Described in header
*/
int command_dispatch()
{
- /* no callback registered for --help and we don't want to connect to the
- * socket anyway */
+ int ret;
+
+ /* no callback registered for --help */
if (active == help_idx)
{
return command_usage(NULL);
}
+ ret = cmds[active].call();
+ if (conn)
+ {
+ vici_disconnect(conn);
+ }
+ return ret;
+}
+
+/*
+ * Described in header
+ */
+bool command_connect_vici(vici_conn_t **out)
+{
+ int err;
if (!uri)
{
@@ -453,5 +451,15 @@ int command_dispatch()
lib->settings->get_str(lib->settings, "%s.plugins.vici.socket",
NULL, lib->ns), lib->ns);
}
- return call_command(&cmds[active]);
+ conn = vici_connect(uri);
+ if (!conn)
+ {
+ err = errno;
+ command_usage("connecting to '%s' URI failed: %s",
+ uri ?: "default", strerror(errno));
+ errno = err;
+ return FALSE;
+ }
+ *out = conn;
+ return TRUE;
}
diff --git a/src/swanctl/command.h b/src/swanctl/command.h
index f6bdfe53be..299d9a0e83 100644
--- a/src/swanctl/command.h
+++ b/src/swanctl/command.h
@@ -64,7 +64,7 @@ struct command_option_t {
*/
struct command_t {
/** Function implementing the command */
- int (*call)(vici_conn_t *conn);
+ int (*call)(void);
/** short option character */
char op;
/** long option string */
@@ -112,4 +112,15 @@ int command_dispatch();
*/
int command_usage(char *error, ...);
+/**
+ * Connect to the configured vici socket. If successfully connected, the
+ * connection is owned by the command dispatcher, so no need to disconnect
+ * manually. If connecting fails, an error and usage output is printed
+ * automatically and errno is set appropriately.
+ *
+ * @param[out] conn connection if successful (owned by command dispatcher)
+ * @return TRUE if connected successfully, FALSE otherwise
+ */
+bool command_connect_vici(vici_conn_t **conn);
+
#endif /** COMMAND_H_ @}*/
diff --git a/src/swanctl/commands/counters.c b/src/swanctl/commands/counters.c
index 48516d8197..0e435563b2 100644
--- a/src/swanctl/commands/counters.c
+++ b/src/swanctl/commands/counters.c
@@ -43,8 +43,9 @@ CALLBACK(counters_sn, int,
return vici_parse_cb(res, conns_sn, NULL, NULL, NULL);
}
-static int counters(vici_conn_t *conn)
+static int counters()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -80,6 +81,11 @@ static int counters(vici_conn_t *conn)
}
break;
}
+
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
if (reset)
{
req = vici_begin("reset-counters");
diff --git a/src/swanctl/commands/flush_certs.c b/src/swanctl/commands/flush_certs.c
index e56d894164..d825a1d685 100644
--- a/src/swanctl/commands/flush_certs.c
+++ b/src/swanctl/commands/flush_certs.c
@@ -18,8 +18,9 @@
#include "command.h"
-static int flush_certs(vici_conn_t *conn)
+static int flush_certs()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -48,6 +49,11 @@ static int flush_certs(vici_conn_t *conn)
}
break;
}
+
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
req = vici_begin("flush-certs");
if (type)
diff --git a/src/swanctl/commands/initiate.c b/src/swanctl/commands/initiate.c
index 7faf3013a2..9a0060ac79 100644
--- a/src/swanctl/commands/initiate.c
+++ b/src/swanctl/commands/initiate.c
@@ -36,8 +36,9 @@ CALLBACK(log_cb, void,
}
}
-static int initiate(vici_conn_t *conn)
+static int initiate()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -76,6 +77,10 @@ static int initiate(vici_conn_t *conn)
break;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
if (vici_register(conn, "control-log", log_cb, &format) != 0)
{
ret = errno;
diff --git a/src/swanctl/commands/install.c b/src/swanctl/commands/install.c
index 0423a355a3..3e777f2557 100644
--- a/src/swanctl/commands/install.c
+++ b/src/swanctl/commands/install.c
@@ -18,8 +18,9 @@
#include <errno.h>
-static int manage_policy(vici_conn_t *conn, char *label)
+static int manage_policy(char *label)
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -51,6 +52,11 @@ static int manage_policy(vici_conn_t *conn, char *label)
}
break;
}
+
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
req = vici_begin(label);
if (child)
{
@@ -89,14 +95,14 @@ static int manage_policy(vici_conn_t *conn, char *label)
return ret;
}
-static int uninstall(vici_conn_t *conn)
+static int uninstall()
{
- return manage_policy(conn, "uninstall");
+ return manage_policy("uninstall");
}
-static int install(vici_conn_t *conn)
+static int install()
{
- return manage_policy(conn, "install");
+ return manage_policy("install");
}
/**
diff --git a/src/swanctl/commands/list_algs.c b/src/swanctl/commands/list_algs.c
index 4f473e1387..8841277680 100644
--- a/src/swanctl/commands/list_algs.c
+++ b/src/swanctl/commands/list_algs.c
@@ -35,8 +35,9 @@ CALLBACK(types, int,
return vici_parse_cb(res, NULL, algs, NULL, NULL);
}
-static int algorithms(vici_conn_t *conn)
+static int algorithms()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
char *arg;
@@ -63,6 +64,10 @@ static int algorithms(vici_conn_t *conn)
break;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
req = vici_begin("get-algorithms");
res = vici_submit(req, conn);
if (!res)
diff --git a/src/swanctl/commands/list_authorities.c b/src/swanctl/commands/list_authorities.c
index a78b5a2a01..f4fe11ba1a 100644
--- a/src/swanctl/commands/list_authorities.c
+++ b/src/swanctl/commands/list_authorities.c
@@ -92,8 +92,9 @@ CALLBACK(list_cb, void,
}
}
-static int list_authorities(vici_conn_t *conn)
+static int list_authorities()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -122,6 +123,11 @@ static int list_authorities(vici_conn_t *conn)
}
break;
}
+
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
if (vici_register(conn, "list-authority", list_cb, &format) != 0)
{
ret = errno;
diff --git a/src/swanctl/commands/list_certs.c b/src/swanctl/commands/list_certs.c
index 32fd8217a3..07903ebb27 100644
--- a/src/swanctl/commands/list_certs.c
+++ b/src/swanctl/commands/list_certs.c
@@ -147,8 +147,9 @@ CALLBACK(list_cb, void,
}
}
-static int list_certs(vici_conn_t *conn)
+static int list_certs()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -193,6 +194,11 @@ static int list_certs(vici_conn_t *conn)
}
break;
}
+
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
if (vici_register(conn, "list-cert", list_cb, &format) != 0)
{
ret = errno;
diff --git a/src/swanctl/commands/list_conns.c b/src/swanctl/commands/list_conns.c
index b6608b9d12..8d39735f9f 100644
--- a/src/swanctl/commands/list_conns.c
+++ b/src/swanctl/commands/list_conns.c
@@ -324,8 +324,9 @@ CALLBACK(list_cb, void,
}
}
-static int list_conns(vici_conn_t *conn)
+static int list_conns()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -354,6 +355,11 @@ static int list_conns(vici_conn_t *conn)
}
break;
}
+
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
if (vici_register(conn, "list-conn", list_cb, &format) != 0)
{
ret = errno;
diff --git a/src/swanctl/commands/list_pols.c b/src/swanctl/commands/list_pols.c
index 3db435bffd..02bb797bfd 100644
--- a/src/swanctl/commands/list_pols.c
+++ b/src/swanctl/commands/list_pols.c
@@ -113,8 +113,9 @@ CALLBACK(list_cb, void,
}
}
-static int list_pols(vici_conn_t *conn)
+static int list_pols()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
bool trap = FALSE, drop = FALSE, pass = FALSE;
@@ -153,6 +154,11 @@ static int list_pols(vici_conn_t *conn)
}
break;
}
+
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
if (!trap && !drop && !pass)
{
trap = drop = pass = TRUE;
diff --git a/src/swanctl/commands/list_pools.c b/src/swanctl/commands/list_pools.c
index 48e46d05f0..7d5317ebc2 100644
--- a/src/swanctl/commands/list_pools.c
+++ b/src/swanctl/commands/list_pools.c
@@ -52,8 +52,9 @@ CALLBACK(list_pool, int,
return vici_parse_cb(res, list_leases, NULL, NULL, name);
}
-static int list_pools(vici_conn_t *conn)
+static int list_pools()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -87,6 +88,10 @@ static int list_pools(vici_conn_t *conn)
break;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
req = vici_begin("get-pools");
if (leases)
{
diff --git a/src/swanctl/commands/list_sas.c b/src/swanctl/commands/list_sas.c
index a598f9598e..8b9c75ec1e 100755
--- a/src/swanctl/commands/list_sas.c
+++ b/src/swanctl/commands/list_sas.c
@@ -396,8 +396,9 @@ CALLBACK(list_cb, void,
}
}
-static int list_sas(vici_conn_t *conn)
+static int list_sas()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
bool noblock = FALSE;
@@ -439,6 +440,11 @@ static int list_sas(vici_conn_t *conn)
}
break;
}
+
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
if (vici_register(conn, "list-sa", list_cb, &format) != 0)
{
ret = errno;
@@ -490,8 +496,9 @@ CALLBACK(close_cb, void,
send_sigint();
}
-static int monitor_sas(vici_conn_t *conn)
+static int monitor_sas()
{
+ vici_conn_t *conn;
command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg;
int ret = 0;
@@ -516,6 +523,10 @@ static int monitor_sas(vici_conn_t *conn)
break;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
vici_on_close(conn, close_cb, &ret);
if (vici_register(conn, "ike-updown", list_cb, &format) != 0)
diff --git a/src/swanctl/commands/load_all.c b/src/swanctl/commands/load_all.c
index 19d4cb64a6..bbd1f3e7aa 100644
--- a/src/swanctl/commands/load_all.c
+++ b/src/swanctl/commands/load_all.c
@@ -27,8 +27,9 @@
#include "load_pools.h"
#include "load_conns.h"
-static int load_all(vici_conn_t *conn)
+static int load_all()
{
+ vici_conn_t *conn;
bool clear = FALSE, noprompt = FALSE;
command_format_options_t format = COMMAND_FORMAT_NONE;
settings_t *cfg;
@@ -70,6 +71,10 @@ static int load_all(vici_conn_t *conn)
return EINVAL;
}
+ if (!command_connect_vici(&conn))
+ {
+ ret = errno;
+ }
if (ret == 0)
{
ret = load_creds_cfg(conn, format, cfg, clear, noprompt);
diff --git a/src/swanctl/commands/load_authorities.c b/src/swanctl/commands/load_authorities.c
index a803cd7046..e8d1297b7c 100644
--- a/src/swanctl/commands/load_authorities.c
+++ b/src/swanctl/commands/load_authorities.c
@@ -308,8 +308,9 @@ int load_authorities_cfg(vici_conn_t *conn, command_format_options_t format,
return EINVAL;
}
-static int load_authorities(vici_conn_t *conn)
+static int load_authorities()
{
+ vici_conn_t *conn;
command_format_options_t format = COMMAND_FORMAT_NONE;
settings_t *cfg;
char *arg, *file = NULL;
@@ -344,7 +345,14 @@ static int load_authorities(vici_conn_t *conn)
return EINVAL;
}
- ret = load_authorities_cfg(conn, format, cfg);
+ if (!command_connect_vici(&conn))
+ {
+ ret = errno;
+ }
+ else
+ {
+ ret = load_authorities_cfg(conn, format, cfg);
+ }
cfg->destroy_clear(cfg);
diff --git a/src/swanctl/commands/load_conns.c b/src/swanctl/commands/load_conns.c
index 11005535ca..d59b2e2215 100644
--- a/src/swanctl/commands/load_conns.c
+++ b/src/swanctl/commands/load_conns.c
@@ -425,8 +425,9 @@ int load_conns_cfg(vici_conn_t *conn, command_format_options_t format,
return EINVAL;
}
-static int load_conns(vici_conn_t *conn)
+static int load_conns()
{
+ vici_conn_t *conn;
command_format_options_t format = COMMAND_FORMAT_NONE;
settings_t *cfg;
char *arg, *file = NULL;
@@ -461,7 +462,14 @@ static int load_conns(vici_conn_t *conn)
return EINVAL;
}
- ret = load_conns_cfg(conn, format, cfg);
+ if (!command_connect_vici(&conn))
+ {
+ ret = errno;
+ }
+ else
+ {
+ ret = load_conns_cfg(conn, format, cfg);
+ }
cfg->destroy_clear(cfg);
diff --git a/src/swanctl/commands/load_creds.c b/src/swanctl/commands/load_creds.c
index 9405972e6d..9cd7820277 100644
--- a/src/swanctl/commands/load_creds.c
+++ b/src/swanctl/commands/load_creds.c
@@ -1004,8 +1004,9 @@ int load_creds_cfg(vici_conn_t *conn, command_format_options_t format,
return 0;
}
-static int load_creds(vici_conn_t *conn)
+static int load_creds()
{
+ vici_conn_t *conn;
bool clear = FALSE, noprompt = FALSE;
command_format_options_t format = COMMAND_FORMAT_NONE;
settings_t *cfg;
@@ -1047,7 +1048,14 @@ static int load_creds(vici_conn_t *conn)
return EINVAL;
}
- ret = load_creds_cfg(conn, format, cfg, clear, noprompt);
+ if (!command_connect_vici(&conn))
+ {
+ ret = errno;
+ }
+ else
+ {
+ ret = load_creds_cfg(conn, format, cfg, clear, noprompt);
+ }
cfg->destroy_clear(cfg);
diff --git a/src/swanctl/commands/load_pools.c b/src/swanctl/commands/load_pools.c
index 1fffc975a0..06598d3320 100644
--- a/src/swanctl/commands/load_pools.c
+++ b/src/swanctl/commands/load_pools.c
@@ -248,8 +248,9 @@ int load_pools_cfg(vici_conn_t *conn, command_format_options_t format,
return EINVAL;
}
-static int load_pools(vici_conn_t *conn)
+static int load_pools()
{
+ vici_conn_t *conn;
command_format_options_t format = COMMAND_FORMAT_NONE;
settings_t *cfg;
char *arg, *file = NULL;
@@ -284,7 +285,14 @@ static int load_pools(vici_conn_t *conn)
return EINVAL;
}
- ret = load_pools_cfg(conn, format, cfg);
+ if (!command_connect_vici(&conn))
+ {
+ ret = errno;
+ }
+ else
+ {
+ ret = load_pools_cfg(conn, format, cfg);
+ }
cfg->destroy_clear(cfg);
diff --git a/src/swanctl/commands/log.c b/src/swanctl/commands/log.c
index 9318a81771..722dc8d19f 100644
--- a/src/swanctl/commands/log.c
+++ b/src/swanctl/commands/log.c
@@ -38,8 +38,9 @@ CALLBACK(log_cb, void,
}
}
-static int logcmd(vici_conn_t *conn)
+static int logcmd()
{
+ vici_conn_t *conn;
command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg;
int ret;
@@ -64,6 +65,10 @@ static int logcmd(vici_conn_t *conn)
break;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
if (vici_register(conn, "log", log_cb, &format) != 0)
{
ret = errno;
diff --git a/src/swanctl/commands/redirect.c b/src/swanctl/commands/redirect.c
index 64b6500d1b..ea769a3da4 100644
--- a/src/swanctl/commands/redirect.c
+++ b/src/swanctl/commands/redirect.c
@@ -18,8 +18,9 @@
#include <errno.h>
-static int redirect(vici_conn_t *conn)
+static int redirect()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -60,6 +61,11 @@ static int redirect(vici_conn_t *conn)
}
break;
}
+
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
req = vici_begin("redirect");
if (ike)
{
diff --git a/src/swanctl/commands/rekey.c b/src/swanctl/commands/rekey.c
index dc83d5ba6a..ffc4766cda 100644
--- a/src/swanctl/commands/rekey.c
+++ b/src/swanctl/commands/rekey.c
@@ -18,8 +18,9 @@
#include <errno.h>
-static int rekey(vici_conn_t *conn)
+static int rekey()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -62,6 +63,10 @@ static int rekey(vici_conn_t *conn)
break;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
req = vici_begin("rekey");
if (child)
{
diff --git a/src/swanctl/commands/reload_settings.c b/src/swanctl/commands/reload_settings.c
index b55786ef4a..10cbb263e2 100644
--- a/src/swanctl/commands/reload_settings.c
+++ b/src/swanctl/commands/reload_settings.c
@@ -18,8 +18,9 @@
#include <errno.h>
-static int reload_settings(vici_conn_t *conn)
+static int reload_settings()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
char *arg;
@@ -46,6 +47,10 @@ static int reload_settings(vici_conn_t *conn)
break;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
req = vici_begin("reload-settings");
res = vici_submit(req, conn);
if (!res)
diff --git a/src/swanctl/commands/stats.c b/src/swanctl/commands/stats.c
index 923df2a332..a1c90e3772 100644
--- a/src/swanctl/commands/stats.c
+++ b/src/swanctl/commands/stats.c
@@ -27,8 +27,9 @@ CALLBACK(list, int,
return 0;
}
-static int stats(vici_conn_t *conn)
+static int stats()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
char *arg;
@@ -55,6 +56,10 @@ static int stats(vici_conn_t *conn)
break;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
req = vici_begin("stats");
res = vici_submit(req, conn);
if (!res)
diff --git a/src/swanctl/commands/terminate.c b/src/swanctl/commands/terminate.c
index bb4a5ec620..1b4f9db6fb 100644
--- a/src/swanctl/commands/terminate.c
+++ b/src/swanctl/commands/terminate.c
@@ -36,8 +36,9 @@ CALLBACK(log_cb, void,
}
}
-static int terminate(vici_conn_t *conn)
+static int terminate()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
command_format_options_t format = COMMAND_FORMAT_NONE;
@@ -86,6 +87,10 @@ static int terminate(vici_conn_t *conn)
break;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
if (vici_register(conn, "control-log", log_cb, &format) != 0)
{
ret = errno;
diff --git a/src/swanctl/commands/version.c b/src/swanctl/commands/version.c
index 97ed5fa604..12be1ad71e 100644
--- a/src/swanctl/commands/version.c
+++ b/src/swanctl/commands/version.c
@@ -18,8 +18,9 @@
#include <errno.h>
-static int version(vici_conn_t *conn)
+static int version()
{
+ vici_conn_t *conn;
vici_req_t *req;
vici_res_t *res;
char *arg;
@@ -52,10 +53,14 @@ static int version(vici_conn_t *conn)
if (!daemon)
{
- printf("strongSwan swanctl %s\n", VERSION);
+ printf("strongSwan %s swanctl\n", VERSION);
return 0;
}
+ if (!command_connect_vici(&conn))
+ {
+ return errno;
+ }
req = vici_begin("version");
res = vici_submit(req, conn);
if (!res)