free(req);
}
-int vici_dump(vici_res_t *res, char *label, FILE *out)
+int vici_dump(vici_res_t *res, char *label, bool pretty, FILE *out)
{
- if (res->message->dump(res->message, label, out))
+ if (res->message->dump(res->message, label, pretty, out))
{
return 0;
}
#include <stdio.h>
+#include <utils/utils.h>
+
/**
* Opaque vici connection contex.
*/
*
* @param res response message to dump
* @param label a label to print for this message
+ * @param pretty use pretty print with indentation
* @param out FILE to dump to
* @return 0 if dumped complete message, 1 on error
*/
-int vici_dump(vici_res_t *res, char *label, FILE *out);
+int vici_dump(vici_res_t *res, char *label, bool pretty, FILE *out);
/**
* Parse next element from a vici response message.
linked_list_t *strings;
};
-ENUM(vici_type_names, VICI_SECTION_START, VICI_END,
+ENUM(vici_type_names, VICI_START, VICI_END,
+ "start",
"section-start",
"section-end",
"key-value",
{
switch (type)
{
+ case VICI_START:
+ /* should never occur */
+ continue;
case VICI_KEY_VALUE:
if (ctx->level == base && kv)
{
}
METHOD(vici_message_t, dump, bool,
- private_vici_message_t *this, char *label, FILE *out)
+ private_vici_message_t *this, char *label, bool pretty, FILE *out)
{
enumerator_t *enumerator;
- int ident = 0, delta = 2;
- vici_type_t type;
- char *name;
+ int ident = 0, delta;
+ vici_type_t type, last_type = VICI_START;
+ char *name, *term, *sep, *separ, *assign;
chunk_t value;
- fprintf(out, "%s {\n", label);
+ /* pretty print uses indentation on multiple lines */
+ if (pretty)
+ {
+ delta = 2;
+ term = "\n";
+ separ = "";
+ assign = " = ";
+ }
+ else
+ {
+ delta = 0;
+ term = "";
+ separ = " ";
+ assign = "=";
+ }
+
+ fprintf(out, "%s {%s", label, term);
ident += delta;
enumerator = create_enumerator(this);
{
switch (type)
{
+ case VICI_START:
+ /* should never occur */
+ break;
case VICI_SECTION_START:
- fprintf(out, "%*s%s {\n", ident, "", name);
+ sep = (last_type != VICI_SECTION_START &&
+ last_type != VICI_START) ? separ : "";
+ fprintf(out, "%*s%s%s {%s", ident, "", sep, name, term);
ident += delta;
break;
case VICI_SECTION_END:
ident -= delta;
- fprintf(out, "%*s}\n", ident, "");
+ fprintf(out, "%*s}%s", ident, "", term);
break;
case VICI_KEY_VALUE:
+ sep = (last_type != VICI_SECTION_START &&
+ last_type != VICI_START) ? separ : "";
if (chunk_printable(value, NULL, ' '))
{
- fprintf(out, "%*s%s = %.*s\n",
- ident, "", name, (int)value.len, value.ptr);
+ fprintf(out, "%*s%s%s%s%.*s%s", ident, "", sep, name,
+ assign, (int)value.len, value.ptr, term);
}
else
{
- fprintf(out, "%*s%s = 0x%+#B\n",
- ident, "", name, &value);
+ fprintf(out, "%*s%s%s%s0x%+#B%s", ident, "", sep, name,
+ assign, &value, term);
}
break;
case VICI_LIST_START:
- fprintf(out, "%*s%s = [\n", ident, "", name);
+ sep = (last_type != VICI_SECTION_START &&
+ last_type != VICI_START) ? separ : "";
+ fprintf(out, "%*s%s%s%s[%s", ident, "", sep, name, assign, term);
ident += delta;
break;
case VICI_LIST_END:
ident -= delta;
- fprintf(out, "%*s]\n", ident, "");
+ fprintf(out, "%*s]%s", ident, "", term);
break;
case VICI_LIST_ITEM:
+ sep = (last_type != VICI_LIST_START) ? separ : "";
if (chunk_printable(value, NULL, ' '))
{
- fprintf(out, "%*s%.*s\n",
- ident, "", (int)value.len, value.ptr);
+ fprintf(out, "%*s%s%.*s%s", ident, "", sep,
+ (int)value.len, value.ptr, term);
}
else
{
- fprintf(out, "%*s 0x%+#B\n", ident, "", &value);
+ fprintf(out, "%*s%s0x%+#B%s", ident, "", sep,
+ &value, term);
}
break;
case VICI_END:
enumerator->destroy(enumerator);
return TRUE;
}
+ last_type = type;
}
enumerator->destroy(enumerator);
return FALSE;
* Vici message encoding types
*/
enum vici_type_t {
+ /** never used in an argument list, needed by dump as initial value */
+ VICI_START = 0,
+
/** begin of new section, argument is section name as char* */
- VICI_SECTION_START = 0,
+ VICI_SECTION_START = 1,
/** end of current section, no arguments */
- VICI_SECTION_END,
+ VICI_SECTION_END = 2,
/** key/value, arguments are key as char*, value as chunk_t */
- VICI_KEY_VALUE,
+ VICI_KEY_VALUE = 3,
/** list start, argument is list name as char* */
- VICI_LIST_START,
+ VICI_LIST_START = 4,
/** list item, argument is item value as chunk_t */
- VICI_LIST_ITEM,
+ VICI_LIST_ITEM = 5,
/** end of list, no arguments */
- VICI_LIST_END,
+ VICI_LIST_END = 6,
/** end of argument list, no arguments (never encoded) */
- VICI_END
+ VICI_END = 7
};
/**
/**
* Dump a message text representation to a FILE stream.
*
- * @param label label to print for message
- * @param out FILE stream to dump to
- * @return TRUE if message valid
+ * @param label label to print for message
+ * @param pretty use pretty print with indentation
+ * @param out FILE stream to dump to
+ * @return TRUE if message valid
*/
- bool (*dump)(vici_message_t *this, char *label, FILE *out);
+ bool (*dump)(vici_message_t *this, char *label, bool pretty, FILE *out);
/**
* Destroy a vici_message_t.
typedef struct command_t command_t;
typedef struct command_option_t command_option_t;
-typedef enum command_type_t command_type_t;
+typedef enum command_format_options_t command_format_options_t;
/**
* Option specification
command_option_t options[MAX_OPTIONS];
};
+/**
+ * Command format options
+*/
+enum command_format_options_t {
+ COMMAND_FORMAT_NONE = 0,
+ COMMAND_FORMAT_RAW = (1<<0),
+ COMMAND_FORMAT_PRETTY = (1<<1),
+ COMMAND_FORMAT_PEM = (1<<2),
+};
+
/**
* Get the next option, as with getopt.
*/
#include <errno.h>
-
CALLBACK(log_cb, void,
- bool *raw, char *name, vici_res_t *msg)
+ command_format_options_t *format, char *name, vici_res_t *msg)
{
- if (*raw)
+ if (*format & COMMAND_FORMAT_RAW)
{
- vici_dump(msg, "log", stdout);
+ vici_dump(msg, "log", *format & COMMAND_FORMAT_PRETTY, stdout);
}
else
{
{
vici_req_t *req;
vici_res_t *res;
- bool raw = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg, *child = NULL;
int ret = 0, timeout = 0, level = 1;
{
case 'h':
return command_usage(NULL);
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case 'c':
child = arg;
break;
}
- if (vici_register(conn, "control-log", log_cb, &raw) != 0)
+ if (vici_register(conn, "control-log", log_cb, &format) != 0)
{
fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
return errno;
fprintf(stderr, "initiate request failed: %s\n", strerror(errno));
return errno;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "initiate reply", stdout);
+ vici_dump(res, "initiate reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else
{
{
command_register((command_t) {
initiate, 'i', "initiate", "initiate a connection",
- {"--child <name> [--timeout <s>] [--raw]"},
+ {"--child <name> [--timeout <s>] [--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"child", 'c', 1, "initate a CHILD_SA configuration"},
{"timeout", 't', 1, "timeout in seconds before detaching"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
{"loglevel", 'l', 1, "verbosity of redirected log"},
}
});
{
vici_req_t *req;
vici_res_t *res;
- bool raw = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg, *child = NULL;
int ret = 0;
{
case 'h':
return command_usage(NULL);
+ case 'P':
+ format |= COMMAND_FORMAT_RAW;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_PRETTY;
continue;
case 'c':
child = arg;
fprintf(stderr, "%s request failed: %s\n", label, strerror(errno));
return errno;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
puts(label);
- vici_dump(res, " reply", stdout);
+ vici_dump(res, " reply", format & COMMAND_FORMAT_PRETTY, stdout);
}
else
{
{
command_register((command_t) {
uninstall, 'u', "uninstall", "uninstall a trap or shunt policy",
- {"--child <name> [--raw]"},
+ {"--child <name> [--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"child", 'c', 1, "CHILD_SA configuration to uninstall"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
{
command_register((command_t) {
install, 'p', "install", "install a trap or shunt policy",
- {"--child <name> [--raw]"},
+ {"--child <name> [--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"child", 'c', 1, "CHILD_SA configuration to install"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
#include "command.h"
-typedef enum {
- FORMAT_RAW = (1<<0),
- FORMAT_PEM = (1<<1),
-} format_options_t;
-
/**
* Print PEM encoding of a certificate
*/
}
CALLBACK(list_cb, void,
- format_options_t *format, char *name, vici_res_t *res)
+ command_format_options_t *format, char *name, vici_res_t *res)
{
- if (*format & FORMAT_RAW)
+ if (*format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "list-cert event", stdout);
+ vici_dump(res, "list-cert event", *format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else
{
BUILD_END);
if (cert)
{
- if (*format & FORMAT_PEM)
+ if (*format & COMMAND_FORMAT_PEM)
{
print_pem(cert);
}
{
vici_req_t *req;
vici_res_t *res;
- format_options_t format = 0;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg, *subject = NULL, *type = NULL;
while (TRUE)
type = arg;
continue;
case 'p':
- format |= FORMAT_PEM;
+ format |= COMMAND_FORMAT_PEM;
continue;
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- format |= FORMAT_RAW;
+ format |= COMMAND_FORMAT_RAW;
continue;
case EOF:
break;
fprintf(stderr, "list-certs request failed: %s\n", strerror(errno));
return errno;
}
- if (format & FORMAT_RAW)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "list-certs reply", stdout);
+ vici_dump(res, "list-certs reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
vici_free_res(res);
return 0;
{
command_register((command_t) {
list_certs, 'x', "list-certs", "list stored certificates",
- {"[--subject <dn/san>] [--type X509|X509_AC|X509_CRL] [--pem] [--raw]"},
+ {"[--subject <dn/san>] [--type X509|X509_AC|X509_CRL] [--pem] "
+ "[--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"subject", 's', 1, "filter by certificate subject"},
{"type", 't', 1, "filter by certificate type"},
{"pem", 'p', 0, "print PEM encoding of certificate"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
}
CALLBACK(list_cb, void,
- bool *raw, char *name, vici_res_t *res)
+ command_format_options_t *format, char *name, vici_res_t *res)
{
- if (*raw)
+ if (*format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "list-conn event", stdout);
+ vici_dump(res, "list-conn event", *format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else
{
{
vici_req_t *req;
vici_res_t *res;
- bool raw = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg;
while (TRUE)
{
case 'h':
return command_usage(NULL);
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case EOF:
break;
}
break;
}
- if (vici_register(conn, "list-conn", list_cb, &raw) != 0)
+ if (vici_register(conn, "list-conn", list_cb, &format) != 0)
{
fprintf(stderr, "registering for connections failed: %s\n",
strerror(errno));
fprintf(stderr, "list-conns request failed: %s\n", strerror(errno));
return errno;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "list-conns reply", stdout);
+ vici_dump(res, "list-conns reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
vici_free_res(res);
return 0;
{
command_register((command_t) {
list_conns, 'L', "list-conns", "list loaded configurations",
- {"[--raw]"},
+ {"[--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
}
CALLBACK(list_cb, void,
- bool *raw, char *name, vici_res_t *res)
+ command_format_options_t *format, char *name, vici_res_t *res)
{
- if (*raw)
+ if (*format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "list-policy event", stdout);
+ vici_dump(res, "list-policy event", *format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else
{
{
vici_req_t *req;
vici_res_t *res;
- bool raw = FALSE, trap = FALSE, drop = FALSE, pass = FALSE;
+ bool trap = FALSE, drop = FALSE, pass = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg, *child = NULL;
while (TRUE)
case 'p':
pass = TRUE;
continue;
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case EOF:
break;
{
trap = drop = pass = TRUE;
}
- if (vici_register(conn, "list-policy", list_cb, &raw) != 0)
+ if (vici_register(conn, "list-policy", list_cb, &format) != 0)
{
fprintf(stderr, "registering for policies failed: %s\n",
strerror(errno));
fprintf(stderr, "list-policies request failed: %s\n", strerror(errno));
return errno;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "list-policies reply", stdout);
+ vici_dump(res, "list-policies reply", format & COMMAND_FORMAT_PRETTY, stdout);
}
vici_free_res(res);
return 0;
{
command_register((command_t) {
list_pols, 'P', "list-pols", "list currently installed policies",
- {"[--child <name>] [--trap] [--drop] [--pass] [--raw]"},
+ {"[--child <name>] [--trap] [--drop] [--pass] [--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"child", 'c', 1, "filter policies by CHILD_SA config name"},
{"drop", 'd', 0, "list drop policies"},
{"pass", 'p', 0, "list bypass policies"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
{
vici_req_t *req;
vici_res_t *res;
- bool raw = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg;
int ret = 0;
{
case 'h':
return command_usage(NULL);
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case EOF:
break;
fprintf(stderr, "get-pools request failed: %s\n", strerror(errno));
return errno;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "get-pools reply", stdout);
+ vici_dump(res, "get-pools reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else
{
{
command_register((command_t) {
list_pools, 'A', "list-pools", "list loaded pool configurations",
- {"[--raw]"},
+ {"[--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
}
CALLBACK(list_cb, void,
- bool *raw, char *name, vici_res_t *res)
+ command_format_options_t *format, char *name, vici_res_t *res)
{
- if (*raw)
+ if (*format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "list-sa event", stdout);
+ vici_dump(res, "list-sa event", *format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else
{
{
vici_req_t *req;
vici_res_t *res;
- bool raw = FALSE, noblock = FALSE;
+ bool noblock = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg, *ike = NULL;
int ike_id = 0;
case 'n':
noblock = TRUE;
continue;
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case EOF:
break;
}
break;
}
- if (vici_register(conn, "list-sa", list_cb, &raw) != 0)
+ if (vici_register(conn, "list-sa", list_cb, &format) != 0)
{
fprintf(stderr, "registering for SAs failed: %s\n", strerror(errno));
return errno;
fprintf(stderr, "list-sas request failed: %s\n", strerror(errno));
return errno;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "list-sas reply", stdout);
+ vici_dump(res, "list-sas reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
vici_free_res(res);
return 0;
{
command_register((command_t) {
list_sas, 'l', "list-sas", "list currently active IKE_SAs",
- {"[--raw]"},
+ {"[--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"ike", 'i', 1, "filter IKE_SAs by name"},
{"ike-id", 'I', 1, "filter IKE_SAs by unique identifier"},
{"noblock", 'n', 0, "don't wait for IKE_SAs in use"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
* Load an IKE_SA config with CHILD_SA configs from a section
*/
static bool load_conn(vici_conn_t *conn, settings_t *cfg,
- char *section, bool raw)
+ char *section, command_format_options_t format)
{
vici_req_t *req;
vici_res_t *res;
fprintf(stderr, "load-conn request failed: %s\n", strerror(errno));
return FALSE;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "load-conn reply", stdout);
+ vici_dump(res, "load-conn reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else if (!streq(vici_find_str(res, "no", "success"), "yes"))
{
/**
* Create a list of currently loaded connections
*/
-static linked_list_t* list_conns(vici_conn_t *conn, bool raw)
+static linked_list_t* list_conns(vici_conn_t *conn,
+ command_format_options_t format)
{
linked_list_t *list;
vici_res_t *res;
res = vici_submit(vici_begin("get-conns"), conn);
if (res)
{
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "get-conns reply", stdout);
+ vici_dump(res, "get-conns reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
vici_parse_cb(res, NULL, NULL, list_conn, list);
vici_free_res(res);
/**
* Unload a connection by name
*/
-static bool unload_conn(vici_conn_t *conn, char *name, bool raw)
+static bool unload_conn(vici_conn_t *conn, char *name,
+ command_format_options_t format)
{
vici_req_t *req;
vici_res_t *res;
fprintf(stderr, "unload-conn request failed: %s\n", strerror(errno));
return FALSE;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "unload-conn reply", stdout);
+ vici_dump(res, "unload-conn reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else if (!streq(vici_find_str(res, "no", "success"), "yes"))
{
static int load_conns(vici_conn_t *conn)
{
- bool raw = FALSE;
u_int found = 0, loaded = 0, unloaded = 0;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg, *section;
enumerator_t *enumerator;
linked_list_t *conns;
{
case 'h':
return command_usage(NULL);
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case EOF:
break;
return EINVAL;
}
- conns = list_conns(conn, raw);
+ conns = list_conns(conn, format);
enumerator = cfg->create_section_enumerator(cfg, "connections");
while (enumerator->enumerate(enumerator, §ion))
{
remove_from_list(conns, section);
found++;
- if (load_conn(conn, cfg, section, raw))
+ if (load_conn(conn, cfg, section, format))
{
loaded++;
}
/* unload all connection in daemon, but not in file */
while (conns->remove_first(conns, (void**)§ion) == SUCCESS)
{
- if (unload_conn(conn, section, raw))
+ if (unload_conn(conn, section, format))
{
unloaded++;
}
}
conns->destroy(conns);
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
return 0;
}
{
command_register((command_t) {
load_conns, 'c', "load-conns", "(re-)load connection configuration",
- {"[--raw]"},
+ {"[--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
/**
* Load a single certificate over vici
*/
-static bool load_cert(vici_conn_t *conn, bool raw, char *dir,
- char *type, chunk_t data)
+static bool load_cert(vici_conn_t *conn, command_format_options_t format,
+ char *dir, char *type, chunk_t data)
{
vici_req_t *req;
vici_res_t *res;
fprintf(stderr, "load-cert request failed: %s\n", strerror(errno));
return FALSE;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "load-cert reply", stdout);
+ vici_dump(res, "load-cert reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else if (!streq(vici_find_str(res, "no", "success"), "yes"))
{
/**
* Load certficiates from a directory
*/
-static void load_certs(vici_conn_t *conn, bool raw, char *type, char *dir)
+static void load_certs(vici_conn_t *conn, command_format_options_t format,
+ char *type, char *dir)
{
enumerator_t *enumerator;
struct stat st;
map = chunk_map(path, FALSE);
if (map)
{
- load_cert(conn, raw, path, type, *map);
+ load_cert(conn, format, path, type, *map);
chunk_unmap(map);
}
else
/**
* Load a single private key over vici
*/
-static bool load_key(vici_conn_t *conn, bool raw, char *dir,
- char *type, chunk_t data)
+static bool load_key(vici_conn_t *conn, command_format_options_t format,
+ char *dir, char *type, chunk_t data)
{
vici_req_t *req;
vici_res_t *res;
fprintf(stderr, "load-key request failed: %s\n", strerror(errno));
return FALSE;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "load-key reply", stdout);
+ vici_dump(res, "load-key reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else if (!streq(vici_find_str(res, "no", "success"), "yes"))
{
/**
* Try to decrypt and load a private key
*/
-static bool load_encrypted_key(vici_conn_t *conn, bool raw,
+static bool load_encrypted_key(vici_conn_t *conn, command_format_options_t format,
char *rel, char *path, char *type, chunk_t data)
{
private_key_t *private;
switch (private->get_type(private))
{
case KEY_RSA:
- loaded = load_key(conn, raw, path, "rsa", encoding);
+ loaded = load_key(conn, format, path, "rsa", encoding);
break;
case KEY_ECDSA:
- loaded = load_key(conn, raw, path, "ecdsa", encoding);
+ loaded = load_key(conn, format, path, "ecdsa", encoding);
break;
default:
break;
/**
* Load private keys from a directory
*/
-static void load_keys(vici_conn_t *conn, bool raw, bool noprompt,
- char *type, char *dir)
+static void load_keys(vici_conn_t *conn, command_format_options_t format,
+ bool noprompt, char *type, char *dir)
{
enumerator_t *enumerator;
struct stat st;
if (map)
{
if (noprompt ||
- !load_encrypted_key(conn, raw, rel, path, type, *map))
+ !load_encrypted_key(conn, format, rel, path, type, *map))
{
- load_key(conn, raw, path, type, *map);
+ load_key(conn, format, path, type, *map);
}
chunk_unmap(map);
}
* Load a single secret over VICI
*/
static bool load_secret(vici_conn_t *conn, settings_t *cfg,
- char *section, bool raw)
+ char *section, command_format_options_t format)
{
enumerator_t *enumerator;
vici_req_t *req;
fprintf(stderr, "load-shared request failed: %s\n", strerror(errno));
return FALSE;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "load-shared reply", stdout);
+ vici_dump(res, "load-shared reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else if (!streq(vici_find_str(res, "no", "success"), "yes"))
{
/**
* Clear all currently loaded credentials
*/
-static bool clear_creds(vici_conn_t *conn, bool raw)
+static bool clear_creds(vici_conn_t *conn, command_format_options_t format)
{
vici_res_t *res;
fprintf(stderr, "clear-creds request failed: %s\n", strerror(errno));
return FALSE;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "clear-creds reply", stdout);
+ vici_dump(res, "clear-creds reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
vici_free_res(res);
return TRUE;
static int load_creds(vici_conn_t *conn)
{
- bool raw = FALSE, clear = FALSE, noprompt = FALSE;
+ bool clear = FALSE, noprompt = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
enumerator_t *enumerator;
settings_t *cfg;
char *arg, *section;
case 'n':
noprompt = TRUE;
continue;
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case EOF:
break;
if (clear)
{
- if (!clear_creds(conn, raw))
+ if (!clear_creds(conn, format))
{
return ECONNREFUSED;
}
}
- load_certs(conn, raw, "x509", SWANCTL_X509DIR);
- load_certs(conn, raw, "x509ca", SWANCTL_X509CADIR);
- load_certs(conn, raw, "x509aa", SWANCTL_X509AADIR);
- load_certs(conn, raw, "x509crl", SWANCTL_X509CRLDIR);
- load_certs(conn, raw, "x509ac", SWANCTL_X509ACDIR);
+ load_certs(conn, format, "x509", SWANCTL_X509DIR);
+ load_certs(conn, format, "x509ca", SWANCTL_X509CADIR);
+ load_certs(conn, format, "x509aa", SWANCTL_X509AADIR);
+ load_certs(conn, format, "x509crl", SWANCTL_X509CRLDIR);
+ load_certs(conn, format, "x509ac", SWANCTL_X509ACDIR);
- load_keys(conn, raw, noprompt, "rsa", SWANCTL_RSADIR);
- load_keys(conn, raw, noprompt, "ecdsa", SWANCTL_ECDSADIR);
- load_keys(conn, raw, noprompt, "any", SWANCTL_PKCS8DIR);
+ load_keys(conn, format, noprompt, "rsa", SWANCTL_RSADIR);
+ load_keys(conn, format, noprompt, "ecdsa", SWANCTL_ECDSADIR);
+ load_keys(conn, format, noprompt, "any", SWANCTL_PKCS8DIR);
cfg = settings_create(SWANCTL_CONF);
if (!cfg)
enumerator = cfg->create_section_enumerator(cfg, "secrets");
while (enumerator->enumerate(enumerator, §ion))
{
- load_secret(conn, cfg, section, raw);
+ load_secret(conn, cfg, section, format);
}
enumerator->destroy(enumerator);
{
command_register((command_t) {
load_creds, 's', "load-creds", "(re-)load credentials",
- {"[--raw]"},
+ {"[--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"clear", 'c', 0, "clear previously loaded credentials"},
{"noprompt", 'n', 0, "do not prompt for passwords"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
* Load a pool configuration
*/
static bool load_pool(vici_conn_t *conn, settings_t *cfg,
- char *section, bool raw)
+ char *section, command_format_options_t format)
{
vici_req_t *req;
vici_res_t *res;
fprintf(stderr, "load-pool request failed: %s\n", strerror(errno));
return FALSE;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "load-pool reply", stdout);
+ vici_dump(res, "load-pool reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else if (!streq(vici_find_str(res, "no", "success"), "yes"))
{
/**
* Create a list of currently loaded pools
*/
-static linked_list_t* list_pools(vici_conn_t *conn, bool raw)
+static linked_list_t* list_pools(vici_conn_t *conn,
+ command_format_options_t format)
{
linked_list_t *list;
vici_res_t *res;
res = vici_submit(vici_begin("get-pools"), conn);
if (res)
{
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "get-pools reply", stdout);
+ vici_dump(res, "get-pools reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
vici_parse_cb(res, list_pool, NULL, NULL, list);
vici_free_res(res);
/**
* Unload a pool by name
*/
-static bool unload_pool(vici_conn_t *conn, char *name, bool raw)
+static bool unload_pool(vici_conn_t *conn, char *name,
+ command_format_options_t format)
{
vici_req_t *req;
vici_res_t *res;
fprintf(stderr, "unload-pool request failed: %s\n", strerror(errno));
return FALSE;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "unload-pool reply", stdout);
+ vici_dump(res, "unload-pool reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else if (!streq(vici_find_str(res, "no", "success"), "yes"))
{
static int load_pools(vici_conn_t *conn)
{
- bool raw = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
u_int found = 0, loaded = 0, unloaded = 0;
char *arg, *section;
enumerator_t *enumerator;
{
case 'h':
return command_usage(NULL);
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case EOF:
break;
return EINVAL;
}
- pools = list_pools(conn, raw);
+ pools = list_pools(conn, format);
enumerator = cfg->create_section_enumerator(cfg, "pools");
while (enumerator->enumerate(enumerator, §ion))
{
remove_from_list(pools, section);
found++;
- if (load_pool(conn, cfg, section, raw))
+ if (load_pool(conn, cfg, section, format))
{
loaded++;
}
/* unload all pools in daemon, but not in file */
while (pools->remove_first(pools, (void**)§ion) == SUCCESS)
{
- if (unload_pool(conn, section, raw))
+ if (unload_pool(conn, section, format))
{
unloaded++;
}
}
pools->destroy(pools);
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
return 0;
}
{
command_register((command_t) {
load_pools, 'a', "load-pools", "(re-)load pool configuration",
- {"[--raw]"},
+ {"[--raw|--pretty"},
{
{"help", 'h', 0, "show usage information"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
#include <unistd.h>
CALLBACK(log_cb, void,
- bool *raw, char *name, vici_res_t *msg)
+ command_format_options_t *format, char *name, vici_res_t *msg)
{
- if (*raw)
+ if (*format & COMMAND_FORMAT_RAW)
{
- vici_dump(msg, "log", stdout);
+ vici_dump(msg, "log", *format & COMMAND_FORMAT_PRETTY, stdout);
}
else
{
static int logcmd(vici_conn_t *conn)
{
- bool raw = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg;
while (TRUE)
{
case 'h':
return command_usage(NULL);
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case EOF:
break;
break;
}
- if (vici_register(conn, "log", log_cb, &raw) != 0)
+ if (vici_register(conn, "log", log_cb, &format) != 0)
{
fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
return errno;
{
command_register((command_t) {
logcmd, 'T', "log", "trace logging output",
- {"[--raw]"},
+ {"[--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}
#include <errno.h>
-
CALLBACK(log_cb, void,
- bool *raw, char *name, vici_res_t *msg)
+ command_format_options_t *format, char *name, vici_res_t *msg)
{
- if (*raw)
+ if (*format & COMMAND_FORMAT_RAW)
{
- vici_dump(msg, "log", stdout);
+ vici_dump(msg, "log", *format & COMMAND_FORMAT_PRETTY, stdout);
}
else
{
{
vici_req_t *req;
vici_res_t *res;
- bool raw = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
char *arg, *child = NULL, *ike = NULL;
int ret = 0, timeout = 0, level = 1, child_id = 0, ike_id = 0;
{
case 'h':
return command_usage(NULL);
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case 'c':
child = arg;
break;
}
- if (vici_register(conn, "control-log", log_cb, &raw) != 0)
+ if (vici_register(conn, "control-log", log_cb, &format) != 0)
{
fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
return errno;
fprintf(stderr, "terminate request failed: %s\n", strerror(errno));
return errno;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "terminate reply", stdout);
+ vici_dump(res, "terminate reply", format & COMMAND_FORMAT_PRETTY,
+ stdout);
}
else
{
command_register((command_t) {
terminate, 't', "terminate", "terminate a connection",
{"--child <name> | --ike <name | --child-id <id> | --ike-id <id>",
- "[--timeout <s>] [--raw]"},
+ "[--timeout <s>] [--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"child", 'c', 1, "terminate by CHILD_SA name"},
{"ike-id", 'I', 1, "terminate by IKE_SA unique identifier"},
{"timeout", 't', 1, "timeout in seconds before detaching"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
{"loglevel", 'l', 1, "verbosity of redirected log"},
}
});
vici_req_t *req;
vici_res_t *res;
char *arg;
- bool raw = FALSE, daemon = FALSE;;
+ bool daemon = FALSE;
+ command_format_options_t format = COMMAND_FORMAT_NONE;
while (TRUE)
{
{
case 'h':
return command_usage(NULL);
+ case 'P':
+ format |= COMMAND_FORMAT_PRETTY;
+ /* fall through to raw */
case 'r':
- raw = TRUE;
+ format |= COMMAND_FORMAT_RAW;
continue;
case 'd':
daemon = TRUE;
fprintf(stderr, "version request failed: %s\n", strerror(errno));
return errno;
}
- if (raw)
+ if (format & COMMAND_FORMAT_RAW)
{
- vici_dump(res, "version reply", stdout);
+ vici_dump(res, "version reply", format & COMMAND_FORMAT_PRETTY, stdout);
}
else
{
{
command_register((command_t) {
version, 'v', "version", "show version information",
- {"[--raw]"},
+ {"[--raw|--pretty]"},
{
{"help", 'h', 0, "show usage information"},
{"daemon", 'd', 0, "query daemon version"},
{"raw", 'r', 0, "dump raw response message"},
+ {"pretty", 'P', 0, "dump raw response message in pretty print"},
}
});
}