Skip to content

eBPF-based security platform

shirudo-build: parse the helper's options with the option table

Daniel Borkmann7961afa02946
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rst
index 8444481d..6fc35d5e 100644
--- a/Documentation/reference/cli/bpm.rst
+++ b/Documentation/reference/cli/bpm.rst
@@ -552,16 +552,14 @@ Exit status
* - 0
- Success, or ``tree sync`` without a configured tree.
* - 1
- - Failure, printed as ``bpm <action>: <error>``. Also the usage
- errors of ``build``, ``config update``, ``tree append`` and
- ``tree show``, which the helper reports, and ``config register
- add`` or ``remove`` and ``key add`` on a target with a config
- image.
+ - Failure, printed as ``bpm <action>: <error>``. Also ``config
+ register add`` or ``remove`` and ``key add`` on a target with a
+ config image.
* - 2
- - A usage error of the other subcommands, printed as ``bpm
- <subcommand>: <what is wrong>``: an unknown subcommand, action or
- option, an option given twice, a value missing or empty, or an
- argument missing or one too many.
+ - A usage error, printed as ``bpm <subcommand>: <what is wrong>``:
+ an unknown subcommand, action or option, an option given twice, a
+ value missing, empty or out of range, or an argument missing or
+ one too many.
* - 127
- A helper subcommand cannot execute ``shirudo-build``.
diff --git a/Documentation/reference/cli/build.rst b/Documentation/reference/cli/build.rst
index 738bbd74..21bb7f6f 100644
--- a/Documentation/reference/cli/build.rst
+++ b/Documentation/reference/cli/build.rst
@@ -200,10 +200,12 @@ Exit status
- The key or certificate is unusable, ``vmlinux.h`` or clang is
missing, compilation failed, or packaging and signing failed.
* - 2
- - An unknown option, a second input, an input that is not
+ - A usage error: an unknown option or one given twice, a value
+ missing or empty, a second input, an input that is not
``.bpf.c``, ``.bpf.o`` or ``.o``, no input, an input together with
``--unload``, or a ``--payload-sha256`` that is not 64 hexadecimal
- characters.
+ characters. Also a usage error of ``image sign-root`` or ``upatch
+ rewrite-rodata``.
* - 127
- ``shirudo`` cannot execute the helper.
diff --git a/Documentation/reference/cli/image.rst b/Documentation/reference/cli/image.rst
index 151ec8c4..64379010 100644
--- a/Documentation/reference/cli/image.rst
+++ b/Documentation/reference/cli/image.rst
@@ -212,7 +212,7 @@ Exit status
- No subcommand, an unknown subcommand, an unknown option or one
given twice, a wrong number of arguments to ``update``, ``stage``
or ``status``, ``stage`` with both a prefix and ``--cancel`` or
- neither, or ``verify`` arguments in another form.
+ neither, or ``verify`` without a prefix or ``--cert``.
* - 127
- ``build``, ``verify``: ``shirudo`` cannot execute the helper.
diff --git a/Documentation/reference/cli/shirudo.rst b/Documentation/reference/cli/shirudo.rst
index ab695344..6bd106a6 100644
--- a/Documentation/reference/cli/shirudo.rst
+++ b/Documentation/reference/cli/shirudo.rst
@@ -99,8 +99,7 @@ Every invocation goes through the same steps before the command runs:
Progress, warning and error messages go to standard error and to syslog
with the identifier ``shirudo`` and the facility ``LOG_AUTHPRIV``, where
journald picks them up. Results go to standard output. Help text goes to
-standard error, except the help of the
-``shirudo-build`` helper and of the generator scripts, which goes to
+standard error, except the help of the generator scripts, which goes to
standard output.
Command names
@@ -128,15 +127,20 @@ command names one per line. Bash completion reads them from there.
Option forms
~~~~~~~~~~~~
-An option that takes a value takes it after an equals sign or as the
-next argument: ``--cert=cert.der`` and ``--cert cert.der`` are the
-same, and an option given last with nothing after it is a usage error.
-Two groups differ. The commands the ``shirudo-build`` helper serves
-(``build``, ``keys vouch``, ``image build``, ``image verify`` and ``bpm
-config update``) take the value as the next argument only, except
-``--keys-dir``, which takes both forms. ``upatch map --json=<path>``
-takes its path after the equals sign only. Each command page states the
-forms it accepts. The same function has the same name on every command:
+Every command parses its options by the same rules. An option that
+takes a value takes it after an equals sign or as the next argument:
+``--cert=cert.der`` and ``--cert cert.der`` are the same. An unknown
+option, an option given twice where it does not repeat, a value missing
+or empty, and an argument the command does not take are usage errors:
+the command prints one line, ``<command>: <what is wrong>``, with the
+closest option names after an unknown one, and exits with 2. ``--``
+ends the options, ``-h`` and ``--help`` print the command's help, and
+``--list-opts`` prints its options one a line, a value's placeholder
+after a tab, which the bash completion reads. ``upatch map
+--json=<path>`` takes its path after the equals sign only, as ``--json``
+alone prints the JSON. ``image build`` hands its arguments to
+``shirudo-gen-verity``, which takes each value as the next argument
+only. The same function has the same name on every command:
a file to write is ``-o <path>`` or ``--output <path>``, a certificate
file is ``--cert``, a signing key ``--key``, the directory that holds
both ``--keys-dir``, a Beefile ``--file``, and ``--json`` prints the
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 0e91b554..ed1e75e6 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -554,7 +554,8 @@ tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o util/config_image.o
util/bpm_oci.o util/curl.o util/bpm_package.o \
util/upatch_rodata.o \
util/bpm_tree.o util/bpm_tree_overlap.o \
- util/chain_history.o \
+ util/chain_history.o util/options.o \
+ util/help.o util/similar.o \
util/kernel_release.o $(VERSION_OBJ) \
$(LIBBPF_A)
@printf ' LD $@\n'
diff --git a/builtin-keys.c b/builtin-keys.c
index f26c784e..0edd8bfa 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -551,27 +551,6 @@ static const struct help keys_add_help = {
.n_sections = ARRAY_SIZE(keys_add_sections),
};
-static const struct help_item keys_vouch_options[] = {
- { "-o <file>", "where to write the vouch (default <cert.der>.vouch)" },
- { "--keys-dir <dir>", "read signing_key.pem and cert.der from <dir>" },
- { "--key <pem>", "the signing key (overrides --keys-dir)" },
- { "--cert <der>", "the signing certificate (overrides --keys-dir)" },
-};
-
-static const struct help_section keys_vouch_sections[] = {
- HELP_ITEMS("options:", keys_vouch_options),
-};
-
-static const struct help keys_vouch_help = {
- .usage = "shirudo keys vouch <cert.der> [-o <file>] [--keys-dir <dir>]",
- .summary = "Sign a vouch for another operator's certificate with this "
- "operator's key,\nfor `shirudo keys add --vouch` on the "
- "targets that enrol this key.\nPrints the vouched "
- "certificate's fingerprint.",
- .sections = keys_vouch_sections,
- .n_sections = ARRAY_SIZE(keys_vouch_sections),
-};
-
/* The public key must be one the kernel verifies loaders with. */
static int check_cert_algorithm(const __u8 *der, size_t der_sz,
const char *what)
@@ -960,10 +939,6 @@ static int keys_revoke(int argc, const char **argv)
static int keys_vouch(int argc, const char **argv)
{
- if (argc >= 2 && is_help(argv[1])) {
- print_help(&keys_vouch_help);
- return 0;
- }
return build_forward("keys vouch",
(const char *const[]){ "keys", "vouch", NULL },
argc - 1, argv + 1);
diff --git a/include/shirudo/misc.h b/include/shirudo/misc.h
index a53a9f78..c86a4a50 100644
--- a/include/shirudo/misc.h
+++ b/include/shirudo/misc.h
@@ -23,6 +23,10 @@
typeof(&(a)[0])))
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + __must_be_array(a))
+/* The text of x once its macros are expanded, as a string literal. */
+#define __stringify_1(x) #x
+#define __stringify(x) __stringify_1(x)
+
/*
* The size of a member, and the offset just past it, without an object;
* the member may be a path such as a.b, as offsetof() takes it.
diff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.sh
index 7a883682..4aa821de 100644
--- a/testing/functional/bpm/t7005-bpm-publish.sh
+++ b/testing/functional/bpm/t7005-bpm-publish.sh
@@ -567,7 +567,7 @@ test_expect_success 'tree append uploads nothing for an entry no target would ta
'
test_expect_success 'the operator commands are reachable directly in shirudo-build' '
- "$B" bpm --help | grep -q "tree append <package-file> <repository>" &&
+ "$B" bpm --help 2>&1 | grep -q "tree append <package-file> <repository>" &&
"$B" bpm tree append --help 2>&1 | grep -q "\-\-op add|delete" &&
"$B" bpm push --help 2>&1 | grep -q "<namespace>/bpm-index:stable" &&
! "$B" bpm not-a-command >/dev/null 2>&1
diff --git a/testing/functional/bpm/t7008-bpm-tree-show.sh b/testing/functional/bpm/t7008-bpm-tree-show.sh
index 148a0c61..d010d2f8 100755
--- a/testing/functional/bpm/t7008-bpm-tree-show.sh
+++ b/testing/functional/bpm/t7008-bpm-tree-show.sh
@@ -260,8 +260,9 @@ test_expect_success 'show refuses a signed document tree sync would refuse' '
test_expect_success 'show without --cert is a usage error' '
restore &&
- { show "$TREE"; test $? -eq 1; } &&
- grep -q "^usage: shirudo bpm tree show" "$WORK/err" &&
+ { show "$TREE"; test $? -eq 2; } &&
+ grep -qx "bpm tree show: takes <repository> and --cert <cert.der>" \
+ "$WORK/err" &&
test ! -s "$WORK/out" &&
show "$TREE" --cert "$CERT"
'
diff --git a/testing/functional/bpm/t7009-bpm-tree-seal.sh b/testing/functional/bpm/t7009-bpm-tree-seal.sh
index bfa5ac41..6f6c2907 100755
--- a/testing/functional/bpm/t7009-bpm-tree-seal.sh
+++ b/testing/functional/bpm/t7009-bpm-tree-seal.sh
@@ -288,11 +288,14 @@ test_expect_success 'a seal an earlier run tagged keeps its tag' '
test_expect_success '--segment-ops outside 1 to 512 is refused' '
cp "$REG/ns/seal/manifests/stable" "$WORK/before.manifest" &&
- for n in 0 513 -1 1x ""; do
+ for n in 0 513 -1 1x; do
! append t7009a seal --segment-ops "$n" &&
- grep -qx "bpm tree append: --segment-ops must be 1 to 512" \
+ grep -qx "bpm tree append: --segment-ops takes a number from 1 to 512" \
"$WORK/err" || exit 1
done &&
+ ! append t7009a seal --segment-ops "" &&
+ grep -qx "bpm tree append: --segment-ops takes a value, not an empty one" \
+ "$WORK/err" &&
cmp -s "$WORK/before.manifest" "$REG/ns/seal/manifests/stable"
'
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 9aea7da6..f47d145c 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -35,7 +35,9 @@
#include "crypto.h"
#include "io.h"
#include "json.h"
+#include "help.h"
#include "log.h"
+#include "options.h"
#include "hex.h"
#include "malloc.h"
#include "timefmt.h"
@@ -1707,19 +1709,88 @@ static const char *build_resolve_keys_dir(const char *arg, char *out, size_t cap
return out;
}
-static void build_usage(void)
-{
- fprintf(stderr,
- "usage: shirudo bpm build [context] --version <version>\n"
- " shirudo bpm build [context] --file <name>.bee --version <version>\n"
- " [--keys-dir <dir>]\n"
- " [--vmlinux <vmlinux.h>]\n"
- " [--rodata-input <name>=<path>]...\n\n"
- "Builds a signed .bee package from a Beefile. The build command\n"
- "is available only in shirudo-operator and requires signing keys.\n");
-}
+enum bpm_build_opt {
+ BPM_BUILD_FILE,
+ BPM_BUILD_VERSION,
+ BPM_BUILD_KEYS_DIR,
+ BPM_BUILD_VMLINUX,
+ BPM_BUILD_RODATA_INPUT,
+};
+
+static const struct opt_spec bpm_build_opts[] = {
+ [BPM_BUILD_FILE] = { "--file", 0, 0, "<name>.bee",
+ "the Beefile to build, in the context directory" },
+ [BPM_BUILD_VERSION] = { "--version", 0, 0, "<version>",
+ "the SemVer 2.0.0 version of the package" },
+ [BPM_BUILD_KEYS_DIR] = { "--keys-dir", 0, 0, "<dir>",
+ "read signing_key.pem and cert.der from <dir>" },
+ [BPM_BUILD_VMLINUX] = { "--vmlinux", 0, 0, "<vmlinux.h>",
+ "compile against it" },
+ [BPM_BUILD_RODATA_INPUT] = { "--rodata-input", 0, OPT_REPEAT,
+ "<name>=<path>", "a file for a .rodata input (repeatable)" },
+};
+
+static const struct help_section bpm_build_sections[] = {
+ HELP_OPTIONS("options:", bpm_build_opts),
+};
+
+static const struct help bpm_build_help = {
+ .usage = "shirudo bpm build [context] --version <version> "
+ "[--file <name>.bee]\n"
+ " [--keys-dir <dir>] "
+ "[--vmlinux <vmlinux.h>]\n"
+ " [--rodata-input <name>=<path>]...",
+ .summary = "Build a signed .bee package from a Beefile. Operator-only; "
+ "needs the\nsigning keys.",
+ .sections = bpm_build_sections,
+ .n_sections = ARRAY_SIZE(bpm_build_sections),
+};
+
+enum config_update_opt {
+ CONFIG_UPDATE_FILE,
+ CONFIG_UPDATE_BASE_PACKAGE,
+ CONFIG_UPDATE_BASE_DIGEST,
+ CONFIG_UPDATE_VERSION,
+ CONFIG_UPDATE_KEYS_DIR,
+ CONFIG_UPDATE_RODATA_INPUT,
+};
-static int __build_bpm_build(int argc, char **argv,
+static const struct opt_spec config_update_opts[] = {
+ [CONFIG_UPDATE_FILE] = { "--file", 0, 0, "<name>.bee",
+ "the Beefile, instead of the base package's" },
+ [CONFIG_UPDATE_BASE_PACKAGE] = { "--base-package", 0, 0,
+ "<package.bee|oci://host/repo:tag>",
+ "the package the update builds on" },
+ [CONFIG_UPDATE_BASE_DIGEST] = { "--base-digest", 0, 0,
+ "sha256:<manifest>",
+ "the manifest digest of an oci:// base package" },
+ [CONFIG_UPDATE_VERSION] = { "--version", 0, 0, "<version>",
+ "the SemVer 2.0.0 version of the update" },
+ [CONFIG_UPDATE_KEYS_DIR] = { "--keys-dir", 0, 0, "<dir>",
+ "read signing_key.pem and cert.der from <dir>" },
+ [CONFIG_UPDATE_RODATA_INPUT] = { "--rodata-input", 0, OPT_REPEAT,
+ "<name>=<path>", "a file for a .rodata input (repeatable)" },
+};
+
+static const struct help_section config_update_sections[] = {
+ HELP_OPTIONS("options:", config_update_opts),
+};
+
+static const struct help config_update_help = {
+ .usage = "shirudo bpm config update --base-package "
+ "<package.bee|oci://host/repo:tag>\n"
+ " --version <version> "
+ "[options]",
+ .summary = "Build a config update package on a base package. Without "
+ "--file, the\nBeefile and the updater inputs come from "
+ "--base-package and are trusted\nas operator-side code. "
+ "An oci:// base package needs --base-digest, the\n"
+ "manifest digest bpm push printed for it.",
+ .sections = config_update_sections,
+ .n_sections = ARRAY_SIZE(config_update_sections),
+};
+
+static int __build_bpm_build(int argc, const char **argv,
struct bpm_build_input_override *overrides)
{
const char *context = ".", *file_arg = NULL, *version = NULL;
@@ -1735,60 +1806,55 @@ static int __build_bpm_build(int argc, char **argv,
__u8 digest[CRYPTO_SHA256_LEN];
struct cert cert = {};
struct bpm_build_spec *spec;
- int positional = 0, ret;
+ int positional = 0, ret, opt;
+ struct opt_parser p;
char out_dir[PATH_MAX];
size_t body_len = 0;
__u8 *body = NULL;
- for (int i = 1; i < argc; i++) {
- if (build_is_help(argv[i])) {
- build_usage();
- return 0;
- } else if (!strcmp(argv[i], "--file")) {
- if (++i >= argc)
- goto usage;
- file_arg = argv[i];
- } else if (!strncmp(argv[i], "--file=", 7)) {
- file_arg = argv[i] + 7;
- } else if (!strcmp(argv[i], "--version")) {
- if (++i >= argc)
- goto usage;
- version = argv[i];
- } else if (!strncmp(argv[i], "--version=", 10)) {
- version = argv[i] + 10;
- } else if (!strcmp(argv[i], "--keys-dir")) {
- if (++i >= argc)
- goto usage;
- keys_arg = argv[i];
- } else if (!strncmp(argv[i], "--keys-dir=", 11)) {
- keys_arg = argv[i] + 11;
- } else if (!strcmp(argv[i], "--vmlinux")) {
- if (++i >= argc)
- goto usage;
- vmlinux_arg = argv[i];
- } else if (!strncmp(argv[i], "--vmlinux=", 10)) {
- vmlinux_arg = argv[i] + 10;
- } else if (!strcmp(argv[i], "--rodata-input")) {
- if (++i >= argc)
- goto usage;
- ret = parse_rodata_input(argv[i], overrides,
+ OPT_INIT(&p, "bpm build", bpm_build_opts, &bpm_build_help, argc, argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case BPM_BUILD_FILE:
+ file_arg = p.value;
+ break;
+ case BPM_BUILD_VERSION:
+ version = p.value;
+ break;
+ case BPM_BUILD_KEYS_DIR:
+ keys_arg = p.value;
+ break;
+ case BPM_BUILD_VMLINUX:
+ vmlinux_arg = p.value;
+ break;
+ case BPM_BUILD_RODATA_INPUT:
+ ret = parse_rodata_input(p.value, overrides,
&n_overrides,
BPM_BUILD_MAX_ENTRIES);
- if (ret > 0)
- goto usage;
+ if (ret > 0) {
+ opt_usage(&p, "--rodata-input takes <name>=<path>");
+ return EXIT_USAGE;
+ }
if (ret)
return ret;
- } else if (argv[i][0] == '-') {
- goto usage;
- } else if (++positional == 1) {
- context = argv[i];
- } else {
- goto usage;
+ break;
+ case OPT_ARG:
+ if (++positional > 1) {
+ opt_usage(&p, "unexpected argument '%s'",
+ p.value);
+ return EXIT_USAGE;
+ }
+ context = p.value;
+ break;
+ case OPT_DONE:
+ return 0;
+ default:
+ return EXIT_USAGE;
}
}
if (!version) {
- pr_err("bpm build: --version <version> is required");
- goto usage;
+ opt_usage(&p, "--version <version> is required");
+ return EXIT_USAGE;
}
if (!build_version_ok(version)) {
pr_err("bpm build: --version %s is not a SemVer 2.0.0 version such as 1.2.0, 1.2.0-rc.1 or 1.2.0+build.5",
@@ -1885,13 +1951,10 @@ out:
xfree(body, body_len + 1);
bpm_build_spec_free(spec);
return ret;
-usage:
- build_usage();
- return -EINVAL;
}
/* The overrides take half a MiB, which a stack frame should not carry. */
-static int build_bpm_build(int argc, char **argv)
+static int build_bpm_build(int argc, const char **argv)
{
struct bpm_build_input_override *overrides;
int ret;
@@ -2050,7 +2113,7 @@ out:
return ret;
}
-static int __build_bpm_config_update(int argc, char **argv,
+static int __build_bpm_config_update(int argc, const char **argv,
struct bpm_build_input_override *overrides)
{
const char *file_arg = NULL, *base_arg = NULL, *base_digest = NULL;
@@ -2067,60 +2130,53 @@ static int __build_bpm_config_update(int argc, char **argv,
struct bpm_package_info base_info = {};
struct bpm_build_spec *spec;
size_t body_len = 0;
- int ret;
+ struct opt_parser p;
+ int ret, opt;
bool has_rodata = false, package_source, stage_open = false;
bool downloaded_package = false;
- for (int i = 1; i < argc; i++) {
- if (build_is_help(argv[i])) {
- fprintf(stdout,
- "usage: shirudo bpm config update "
- "--base-package <package.bee|oci://host/repo:tag> "
- "[--base-digest sha256:<manifest>] "
- "--version <version> "
- "[--file <name>.bee] [--keys-dir <dir>] "
- "[--rodata-input <name>=<path>]...\n"
- "Without --file, the Beefile and updater inputs come from "
- "--base-package and are trusted as operator-side code.\n"
- "An oci:// base package needs --base-digest, the manifest "
- "digest bpm push printed for it.\n");
- return 0;
- } else if (!strcmp(argv[i], "--file")) {
- if (++i >= argc)
- goto usage;
- file_arg = argv[i];
- } else if (!strcmp(argv[i], "--base-package")) {
- if (++i >= argc)
- goto usage;
- base_arg = argv[i];
- } else if (!strcmp(argv[i], "--base-digest")) {
- if (++i >= argc)
- goto usage;
- base_digest = argv[i];
- } else if (!strcmp(argv[i], "--version")) {
- if (++i >= argc)
- goto usage;
- version = argv[i];
- } else if (!strcmp(argv[i], "--keys-dir")) {
- if (++i >= argc)
- goto usage;
- keys_arg = argv[i];
- } else if (!strcmp(argv[i], "--rodata-input")) {
- if (++i >= argc)
- goto usage;
- ret = parse_rodata_input(argv[i], overrides,
+ OPT_INIT(&p, "bpm config update", config_update_opts, &config_update_help, argc, argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case CONFIG_UPDATE_FILE:
+ file_arg = p.value;
+ break;
+ case CONFIG_UPDATE_BASE_PACKAGE:
+ base_arg = p.value;
+ break;
+ case CONFIG_UPDATE_BASE_DIGEST:
+ base_digest = p.value;
+ break;
+ case CONFIG_UPDATE_VERSION:
+ version = p.value;
+ break;
+ case CONFIG_UPDATE_KEYS_DIR:
+ keys_arg = p.value;
+ break;
+ case CONFIG_UPDATE_RODATA_INPUT:
+ ret = parse_rodata_input(p.value, overrides,
&n_overrides,
BPM_BUILD_MAX_ENTRIES);
- if (ret > 0)
- goto usage;
+ if (ret > 0) {
+ opt_usage(&p, "--rodata-input takes <name>=<path>");
+ return EXIT_USAGE;
+ }
if (ret)
return ret;
- } else {
- goto usage;
+ break;
+ case OPT_ARG:
+ opt_usage(&p, "unexpected argument '%s'", p.value);
+ return EXIT_USAGE;
+ case OPT_DONE:
+ return 0;
+ default:
+ return EXIT_USAGE;
}
}
- if (!base_arg || !version)
- goto usage;
+ if (!base_arg || !version) {
+ opt_usage(&p, "takes --base-package and --version");
+ return EXIT_USAGE;
+ }
if (!build_version_ok(version)) {
pr_err("bpm config update: --version %s is not a SemVer 2.0.0 version such as 1.2.0, 1.2.0-rc.1 or 1.2.0+build.5",
version);
@@ -2149,7 +2205,8 @@ static int __build_bpm_config_update(int argc, char **argv,
}
downloaded_package = true;
} else if (!realpath(base_arg, base_package)) {
- goto usage;
+ opt_usage(&p, "--base-package %s: %s", base_arg, strerror(errno));
+ return EXIT_USAGE;
}
ret = bpm_pkg_load_info(base_package, &base_info);
if (ret)
@@ -2378,18 +2435,10 @@ out_base:
if (downloaded_package)
unlink(base_package);
return ret;
-usage:
- fprintf(stderr,
- "usage: shirudo bpm config update --base-package "
- "<package.bee|oci://host/repo:tag> "
- "[--base-digest sha256:<manifest>] --version <version> "
- "[--file <name>.bee] "
- "[--rodata-input <name>=<path>]... [--keys-dir <dir>]\n");
- return -EINVAL;
}
/* The overrides take half a MiB, which a stack frame should not carry. */
-static int build_bpm_config_update(int argc, char **argv)
+static int build_bpm_config_update(int argc, const char **argv)
{
struct bpm_build_input_override *overrides;
int ret;
@@ -2402,40 +2451,32 @@ static int build_bpm_config_update(int argc, char **argv)
return ret;
}
-static void bpm_usage(FILE *fp)
-{
- fprintf(fp,
- "usage: shirudo bpm build [context] --version <version> [--file <name>.bee]\n"
- " [--keys-dir <dir>] [--vmlinux <vmlinux.h>]\n"
- " [--rodata-input <name>=<path>]...\n"
- " shirudo bpm config update --base-package <package.bee|oci://...>\n"
- " [--base-digest sha256:<manifest>]\n"
- " --version <version> [--file <name>.bee]\n"
- " [--rodata-input <name>=<path>]...\n"
- " shirudo bpm push <package-file> <repository> [--keys-dir <dir> |\n"
- " --index-cert <cert.der> --index-key <key.pem>]\n"
- " shirudo bpm tree append <package-file> <repository> [options]\n"
- " shirudo bpm tree show <repository> --cert <cert.der> [--reference <ref>]\n\n"
- "Operator-only BPM commands, implemented in shirudo-build and reached\n"
- "through `shirudo bpm`. Each accepts --help.\n");
-}
-
-bool build_is_help(const char *arg)
-{
- return arg && (!strcmp(arg, "--help") || !strcmp(arg, "-h") ||
- !strcmp(arg, "help"));
-}
+static const struct help bpm_operator_help = {
+ .usage = "shirudo bpm build [context] --version <version> [options]\n"
+ " shirudo bpm config update --base-package "
+ "<package> --version <version> [options]\n"
+ " shirudo bpm push <package-file> <repository> "
+ "[options]\n"
+ " shirudo bpm tree append <package-file> <repository> "
+ "[options]\n"
+ " shirudo bpm tree show <repository> --cert <cert.der> "
+ "[options]",
+ .summary = "Operator-only BPM commands, implemented in shirudo-build "
+ "and reached\nthrough `shirudo bpm`. Each takes --help.",
+};
-int build_bpm(int argc, char **argv)
+int build_bpm(int argc, const char **argv)
{
int ret;
- if (argc < 2 || build_is_help(argv[1])) {
- bpm_usage(argc < 2 ? stderr : stdout);
- return argc < 2 ? 2 : 0;
+ if (argc < 2 || is_help(argv[1])) {
+ print_help(&bpm_operator_help);
+ return argc < 2 ? EXIT_USAGE : 0;
}
if (!strcmp(argv[1], "build")) {
ret = build_bpm_build(argc - 1, argv + 1);
+ if (ret == EXIT_USAGE)
+ return EXIT_USAGE;
if (ret < 0 && ret != -EINVAL)
pr_err("bpm build: %s", strerror(-ret));
return ret ? 1 : 0;
@@ -2443,6 +2484,8 @@ int build_bpm(int argc, char **argv)
if (!strcmp(argv[1], "config") && argc >= 3 &&
!strcmp(argv[2], "update")) {
ret = build_bpm_config_update(argc - 2, argv + 2);
+ if (ret == EXIT_USAGE)
+ return EXIT_USAGE;
if (ret < 0 && ret != -EINVAL)
pr_err("bpm config update: %s", strerror(-ret));
return ret ? 1 : 0;
@@ -2451,7 +2494,6 @@ int build_bpm(int argc, char **argv)
return build_bpm_push(argc - 1, argv + 1);
if (!strcmp(argv[1], "tree"))
return build_bpm_tree(argc - 1, argv + 1);
- pr_err("shirudo-build bpm: '%s' is not an operator bpm command", argv[1]);
- bpm_usage(stderr);
- return 2;
+ pr_err("bpm: '%s' is not an operator bpm command", argv[1]);
+ return EXIT_USAGE;
}
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 5c356ae4..628b2d50 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -30,7 +30,9 @@
#include "hex.h"
#include "io.h"
#include "json.h"
+#include "help.h"
#include "log.h"
+#include "options.h"
#include "malloc.h"
#include "paths.h"
#include "sign.h"
@@ -44,23 +46,58 @@
/* One append never splits across segments, so it fits in one. */
BUILD_BUG_ON(BPM_BUILD_MAX_ENTRIES > TREE_MAX_OPS);
-static void tree_append_usage(void)
-{
- fprintf(stderr,
- "usage: shirudo bpm tree append <package-file> <repository>\n"
- " [--keys-dir <dir>] [--key <signing_key.pem>]\n"
- " [--cert <cert.der>] [--previous-cert <cert.der>]\n"
- " [--tree-id <id>]\n"
- " [--reference <ref>] [--op add|delete]\n"
- " [--segment-ops <n>]\n\n"
- "Publishes the [upatches.tree] entries of a built package as the next\n"
- "operations of the signed tree at <repository>:<ref> (default stable).\n"
- "Extending an existing tree needs --tree-id naming it.\n"
- "A segment that holds --segment-ops operations (default %d, 1 to %d)\n"
- "is sealed and the entries start the next one.\n"
- "The key defaults to --keys-dir, SHIRUDO_KEYS_DIR or %s.\n",
- TREE_SEGMENT_OPS, TREE_MAX_OPS, KEYS_DIR);
-}
+enum tree_append_opt {
+ TREE_APPEND_KEYS_DIR,
+ TREE_APPEND_KEY,
+ TREE_APPEND_CERT,
+ TREE_APPEND_PREVIOUS_CERT,
+ TREE_APPEND_TREE_ID,
+ TREE_APPEND_REFERENCE,
+ TREE_APPEND_OP,
+ TREE_APPEND_SEGMENT_OPS,
+};
+
+static const struct opt_spec tree_append_opts[] = {
+ [TREE_APPEND_KEYS_DIR] = { "--keys-dir", 0, 0, "<dir>",
+ "read signing_key.pem and cert.der from <dir>" },
+ [TREE_APPEND_KEY] = { "--key", 0, 0, "<signing_key.pem>",
+ "the signing key, over --keys-dir" },
+ [TREE_APPEND_CERT] = { "--cert", 0, 0, "<cert.der>",
+ "the certificate, over --keys-dir" },
+ [TREE_APPEND_PREVIOUS_CERT] = { "--previous-cert", 0, 0, "<cert.der>",
+ "the certificate the tree was signed with so far" },
+ [TREE_APPEND_TREE_ID] = { "--tree-id", 0, 0, "<id>",
+ "the tree to extend, which an existing tree needs" },
+ [TREE_APPEND_REFERENCE] = { "--reference", 0, 0, "<ref>",
+ "the tag of the publication (default stable)" },
+ [TREE_APPEND_OP] = { "--op", 0, 0, "add|delete",
+ "add the entries, or delete them (default add)" },
+ [TREE_APPEND_SEGMENT_OPS] = { "--segment-ops", 0, 0, "<n>",
+ "seal a segment holding n operations and start the next, 1 to "
+ __stringify(TREE_MAX_OPS) " (default "
+ __stringify(TREE_SEGMENT_OPS) ")" },
+};
+
+static const struct help_section tree_append_sections[] = {
+ HELP_OPTIONS("options:", tree_append_opts),
+};
+
+static const struct help tree_append_help = {
+ .usage = "shirudo bpm tree append <package-file> <repository> "
+ "[options]",
+ .summary = "Publish the [upatches.tree] entries of a built package "
+ "as the next\noperations of the signed tree at "
+ "<repository>:<ref>. The key defaults to\n--keys-dir, "
+ KEYS_DIR_ENV " or " KEYS_DIR ".",
+ .sections = tree_append_sections,
+ .n_sections = ARRAY_SIZE(tree_append_sections),
+};
+
+/* Indexed by what each stands for, so that a value names itself. */
+static const struct opt_name tree_append_ops[] = {
+ [false] = { "add", false },
+ [true] = { "delete", true },
+};
static int publish_fail(const char *action, int ret)
{
@@ -1166,15 +1203,14 @@ static int tree_new_id(const char *tree_id, const char *package,
return 0;
}
-static int cmd_bpm_tree_append(int argc, char **argv)
+static int cmd_bpm_tree_append(int argc, const char **argv)
{
const char *package = NULL, *raw_repository = NULL, *keys_dir_arg = NULL;
char repository[BPM_MAX_ENDPOINT], repo_ref[BPM_MAX_ENDPOINT];
struct bpm_descriptor config, payload_layer, sig_layer;
const char *key = NULL, *cert = NULL, *tree_id = NULL;
const char *previous_cert = NULL;
- const char *reference = "stable", *operation = "add";
- const char *segment_ops_arg = NULL;
+ const char *reference = "stable";
char key_path[PATH_MAX], cert_path[PATH_MAX];
char parent[BPM_MAX_DIGEST] = {}, digest[72];
struct tree_publication *sealed = NULL;
@@ -1196,77 +1232,68 @@ static int cmd_bpm_tree_append(int argc, char **argv)
struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
char empty_config[] = "{}";
char package_dir[101];
- int appended = 0, ret;
+ int appended = 0, ret, opt, op_delete = false;
+ struct opt_parser p;
bool unload;
-
- for (int i = 2; i < argc; i++) {
- const char *a = argv[i];
-
- if (!strcmp(a, "--keys-dir") && i + 1 < argc) {
- keys_dir_arg = argv[++i];
- } else if (!strncmp(a, "--keys-dir=", 11)) {
- keys_dir_arg = a + 11;
- } else if (!strcmp(a, "--key") && i + 1 < argc) {
- key = argv[++i];
- } else if (!strncmp(a, "--key=", 6)) {
- key = a + 6;
- } else if (!strcmp(a, "--cert") && i + 1 < argc) {
- cert = argv[++i];
- } else if (!strncmp(a, "--cert=", 7)) {
- cert = a + 7;
- } else if (!strcmp(a, "--previous-cert") && i + 1 < argc) {
- previous_cert = argv[++i];
- } else if (!strncmp(a, "--previous-cert=", 16)) {
- previous_cert = a + 16;
- } else if (!strcmp(a, "--tree-id") && i + 1 < argc) {
- tree_id = argv[++i];
- } else if (!strncmp(a, "--tree-id=", 10)) {
- tree_id = a + 10;
- } else if (!strcmp(a, "--reference") && i + 1 < argc) {
- reference = argv[++i];
- } else if (!strncmp(a, "--reference=", 12)) {
- reference = a + 12;
- } else if (!strcmp(a, "--op") && i + 1 < argc) {
- operation = argv[++i];
- } else if (!strncmp(a, "--op=", 5)) {
- operation = a + 5;
- } else if (!strcmp(a, "--segment-ops") && i + 1 < argc) {
- segment_ops_arg = argv[++i];
- } else if (!strncmp(a, "--segment-ops=", 14)) {
- segment_ops_arg = a + 14;
- } else if (a[0] == '-' || (package && raw_repository)) {
- tree_append_usage();
- return -EINVAL;
- } else if (!package) {
- package = a;
- } else {
- raw_repository = a;
+ __u64 n_ops;
+
+ OPT_INIT(&p, "bpm tree append", tree_append_opts, &tree_append_help,
+ argc, argv, 2);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case TREE_APPEND_KEYS_DIR:
+ keys_dir_arg = p.value;
+ break;
+ case TREE_APPEND_KEY:
+ key = p.value;
+ break;
+ case TREE_APPEND_CERT:
+ cert = p.value;
+ break;
+ case TREE_APPEND_PREVIOUS_CERT:
+ previous_cert = p.value;
+ break;
+ case TREE_APPEND_TREE_ID:
+ if (!bpm_package_name_ok(p.value)) {
+ opt_usage(&p, "invalid --tree-id %s", p.value);
+ return EXIT_USAGE;
+ }
+ tree_id = p.value;
+ break;
+ case TREE_APPEND_REFERENCE:
+ reference = p.value;
+ break;
+ case TREE_APPEND_OP:
+ if (OPT_CHOICE(&p, tree_append_ops, &op_delete))
+ return EXIT_USAGE;
+ break;
+ case TREE_APPEND_SEGMENT_OPS:
+ if (opt_u64(&p, 1, TREE_MAX_OPS, &n_ops))
+ return EXIT_USAGE;
+ segment_ops = (int)n_ops;
+ break;
+ case OPT_ARG:
+ if (raw_repository) {
+ opt_usage(&p, "unexpected argument '%s'",
+ p.value);
+ return EXIT_USAGE;
+ }
+ if (package)
+ raw_repository = p.value;
+ else
+ package = p.value;
+ break;
+ case OPT_DONE:
+ return 0;
+ default:
+ return EXIT_USAGE;
}
}
if (!package || !raw_repository) {
- tree_append_usage();
- return -EINVAL;
- }
- if (strcmp(operation, "add") && strcmp(operation, "delete")) {
- pr_err("bpm tree append: --op must be add or delete");
- return -EINVAL;
- }
- if (tree_id && !bpm_package_name_ok(tree_id)) {
- pr_err("bpm tree append: invalid --tree-id %s", tree_id);
- return -EINVAL;
+ opt_usage(&p, "takes <package-file> and <repository>");
+ return EXIT_USAGE;
}
- if (segment_ops_arg) {
- __u64 n;
-
- if (parse_u64(segment_ops_arg, strlen(segment_ops_arg),
- TREE_MAX_OPS, &n) || !n) {
- pr_err("bpm tree append: --segment-ops must be 1 to %d",
- TREE_MAX_OPS);
- return -EINVAL;
- }
- segment_ops = (int)n;
- }
- unload = !strcmp(operation, "delete");
+ unload = op_delete;
if (!key || !cert) {
const char *keys_dir = keys_dir_resolve(keys_dir_arg);
@@ -1432,7 +1459,8 @@ static int cmd_bpm_tree_append(int argc, char **argv)
}
tree_selector_free(&op->selector);
*op = (struct tree_operation){ .seq = seq };
- bpm_copy_string(op->op, sizeof(op->op), operation);
+ bpm_copy_string(op->op, sizeof(op->op),
+ tree_append_ops[unload].name);
bpm_copy_string(op->id, sizeof(op->id), u->tree.id);
if (u->tree.selector) {
ret = tree_selector_copy(&op->selector, u->tree.selector);
@@ -1559,65 +1587,91 @@ out:
return ret;
}
-static void bpm_push_usage(void)
-{
- fprintf(stderr,
- "usage: shirudo bpm push <package-file> <repository>\n"
- " [--keys-dir <dir> | --index-cert <cert.der>\n"
- " --index-key <signing_key.pem>] [--new-index]\n\n"
- "The repository must start with oci:// and must not include a tag.\n"
- "With signing material the registry's signed index at\n"
- "<namespace>/bpm-index:stable gains the package entry, signed with the\n"
- "operator key; without it only the package is published. A namespace\n"
- "without an index is refused unless --new-index starts one, which in\n"
- "turn is refused where an index exists.\n"
- "Examples:\n"
- " shirudo bpm push ./package.bee oci://quay.io/namespace/package\n"
- " shirudo bpm push ./package.bee oci://quay.io/namespace/package \\\n"
- " --keys-dir /var/lib/shirudo/keys\n");
-}
+enum push_opt {
+ PUSH_KEYS_DIR,
+ PUSH_INDEX_CERT,
+ PUSH_INDEX_KEY,
+ PUSH_NEW_INDEX,
+};
-int build_bpm_push(int argc, char **argv)
+static const struct opt_spec push_opts[] = {
+ [PUSH_KEYS_DIR] = { "--keys-dir", 0, 0, "<dir>",
+ "sign the index entry with signing_key.pem and cert.der there" },
+ [PUSH_INDEX_CERT] = { "--index-cert", 0, 0, "<cert.der>",
+ "the index certificate, with --index-key" },
+ [PUSH_INDEX_KEY] = { "--index-key", 0, 0, "<signing_key.pem>",
+ "the index key, with --index-cert" },
+ [PUSH_NEW_INDEX] = { "--new-index", 0, 0, NULL,
+ "start the index of a namespace that has none" },
+};
+
+static const struct help_section push_sections[] = {
+ HELP_OPTIONS("options:", push_opts),
+};
+
+static const struct help push_help = {
+ .usage = "shirudo bpm push <package-file> <repository> [options]",
+ .summary = "Publish a package to an oci:// repository without a tag. "
+ "With signing\nmaterial, the signed index at "
+ "<namespace>/bpm-index:stable gains the\npackage's "
+ "entry; without it, only the package is published. A "
+ "namespace\nwithout an index is refused unless "
+ "--new-index starts one, which is\nrefused where an "
+ "index exists.",
+ .sections = push_sections,
+ .n_sections = ARRAY_SIZE(push_sections),
+ .footer = "Examples:\n"
+ " shirudo bpm push ./package.bee "
+ "oci://quay.io/namespace/package\n"
+ " shirudo bpm push ./package.bee "
+ "oci://quay.io/namespace/package \\\n"
+ " --keys-dir /var/lib/shirudo/keys",
+};
+
+int build_bpm_push(int argc, const char **argv)
{
const char *package = NULL, *repository = NULL, *reference = NULL;
const char *keys_dir_arg = NULL, *index_cert = NULL, *index_key = NULL;
char cert_path[PATH_MAX], key_path[PATH_MAX];
bool new_index = false;
- int ret;
-
- if (argc == 2 && build_is_help(argv[1])) {
- bpm_push_usage();
- return 0;
- }
- for (int i = 1; i < argc; i++) {
- const char *a = argv[i];
-
- if (!strcmp(a, "--keys-dir") && i + 1 < argc) {
- keys_dir_arg = argv[++i];
- } else if (!strncmp(a, "--keys-dir=", 11)) {
- keys_dir_arg = a + 11;
- } else if (!strcmp(a, "--index-cert") && i + 1 < argc) {
- index_cert = argv[++i];
- } else if (!strncmp(a, "--index-cert=", 13)) {
- index_cert = a + 13;
- } else if (!strcmp(a, "--index-key") && i + 1 < argc) {
- index_key = argv[++i];
- } else if (!strncmp(a, "--index-key=", 12)) {
- index_key = a + 12;
- } else if (!strcmp(a, "--new-index")) {
+ struct opt_parser p;
+ int ret, opt;
+
+ OPT_INIT(&p, "bpm push", push_opts, &push_help, argc, argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case PUSH_KEYS_DIR:
+ keys_dir_arg = p.value;
+ break;
+ case PUSH_INDEX_CERT:
+ index_cert = p.value;
+ break;
+ case PUSH_INDEX_KEY:
+ index_key = p.value;
+ break;
+ case PUSH_NEW_INDEX:
new_index = true;
- } else if (a[0] == '-' || (package && repository)) {
- bpm_push_usage();
- return 2;
- } else if (!package) {
- package = a;
- } else {
- repository = a;
+ break;
+ case OPT_ARG:
+ if (repository) {
+ opt_usage(&p, "unexpected argument '%s'",
+ p.value);
+ return EXIT_USAGE;
+ }
+ if (package)
+ repository = p.value;
+ else
+ package = p.value;
+ break;
+ case OPT_DONE:
+ return 0;
+ default:
+ return EXIT_USAGE;
}
}
if (!package || !repository) {
- bpm_push_usage();
- return 2;
+ opt_usage(&p, "takes <package-file> and <repository>");
+ return EXIT_USAGE;
}
if (keys_dir_arg) {
if ((!index_cert &&
@@ -1633,12 +1687,12 @@ int build_bpm_push(int argc, char **argv)
index_key = key_path;
}
if (!index_cert != !index_key) {
- pr_err("bpm push: --index-cert and --index-key must be supplied together");
- return 2;
+ opt_usage(&p, "--index-cert and --index-key must be supplied together");
+ return EXIT_USAGE;
}
if (new_index && !index_key) {
- pr_err("bpm push: --new-index needs --keys-dir, or --index-cert and --index-key");
- return 2;
+ opt_usage(&p, "--new-index needs --keys-dir, or --index-cert and --index-key");
+ return EXIT_USAGE;
}
if (index_key && build_check_keys(index_key, index_cert))
return 1;
@@ -1654,23 +1708,21 @@ int build_bpm_push(int argc, char **argv)
return 0;
}
-int build_bpm_tree(int argc, char **argv)
+int build_bpm_tree(int argc, const char **argv)
{
int ret;
if (argc >= 2 && !strcmp(argv[1], "show"))
return build_bpm_tree_show(argc - 1, argv + 1);
if (argc < 2 || strcmp(argv[1], "append")) {
- tree_append_usage();
- return argc >= 2 && build_is_help(argv[1]) ? 0 : 2;
- }
- if (argc == 3 && build_is_help(argv[2])) {
- tree_append_usage();
- return 0;
+ print_help(&tree_append_help);
+ return argc >= 2 && is_help(argv[1]) ? 0 : EXIT_USAGE;
}
if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK)
return publish_fail("initialize curl", -EIO);
ret = cmd_bpm_tree_append(argc, argv);
curl_global_cleanup();
+ if (ret == EXIT_USAGE)
+ return EXIT_USAGE;
return ret ? publish_fail("tree append", ret) : 0;
}
diff --git a/tools/shirudo-build/bpm-show.c b/tools/shirudo-build/bpm-show.c
index 17875497..0affaa95 100644
--- a/tools/shirudo-build/bpm-show.c
+++ b/tools/shirudo-build/bpm-show.c
@@ -24,21 +24,42 @@
#include "io.h"
#include "json.h"
#include "keys.h"
+#include "help.h"
#include "log.h"
+#include "options.h"
#include "malloc.h"
-static void tree_show_usage(void)
-{
- fprintf(stderr,
- "usage: shirudo bpm tree show <repository> --cert <cert.der>\n"
- " [--reference <ref>] [--walk]\n\n"
- "Fetches the tree publication at <repository>:<ref> (default stable),\n"
- "verifies it against the certificate as tree sync does, and prints\n"
- "{\"manifest_digest\":\"sha256:<hex>\",\"document\":<document>} with the\n"
- "document's bytes verbatim, then a newline. --walk prints every\n"
- "segment of the tree that way, oldest first, back from the one the\n"
- "reference names.\n");
-}
+enum tree_show_opt {
+ TREE_SHOW_CERT,
+ TREE_SHOW_REFERENCE,
+ TREE_SHOW_WALK,
+};
+
+static const struct opt_spec tree_show_opts[] = {
+ [TREE_SHOW_CERT] = { "--cert", 0, 0, "<cert.der>",
+ "the certificate the tree is signed for" },
+ [TREE_SHOW_REFERENCE] = { "--reference", 0, 0, "<ref>",
+ "the tag of the publication (default stable)" },
+ [TREE_SHOW_WALK] = { "--walk", 0, 0, NULL,
+ "print every segment, oldest first, back from the one the "
+ "reference names" },
+};
+
+static const struct help_section tree_show_sections[] = {
+ HELP_OPTIONS("options:", tree_show_opts),
+};
+
+static const struct help tree_show_help = {
+ .usage = "shirudo bpm tree show <repository> --cert <cert.der> "
+ "[--reference <ref>] [--walk]",
+ .summary = "Fetch the tree publication at <repository>:<ref>, verify "
+ "it against the\ncertificate as tree sync does, and print\n"
+ "{\"manifest_digest\":\"sha256:<hex>\",\"document\":"
+ "<document>} with the document's\nbytes verbatim, then a "
+ "newline.",
+ .sections = tree_show_sections,
+ .n_sections = ARRAY_SIZE(tree_show_sections),
+};
int build_tree_fetch_checked(struct bpm_oci_client *c, const char *repository,
const char *reference, const char *cert_path,
@@ -221,7 +242,7 @@ static int tree_show_walk(struct bpm_oci_client *c, const char *repository,
return ret;
}
-static int cmd_bpm_tree_show(int argc, char **argv)
+static int cmd_bpm_tree_show(int argc, const char **argv)
{
struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
const char *raw_repository = NULL, *cert = NULL;
@@ -230,32 +251,40 @@ static int cmd_bpm_tree_show(int argc, char **argv)
struct trusted_set *trusted = NULL;
struct tree_publication pub = {};
struct bpm_buffer document = {};
+ struct opt_parser p;
bool walk = false;
- int ret;
-
- for (int i = 1; i < argc; i++) {
- const char *a = argv[i];
+ int ret, opt;
- if (!strcmp(a, "--cert") && i + 1 < argc) {
- cert = argv[++i];
- } else if (!strncmp(a, "--cert=", 7)) {
- cert = a + 7;
- } else if (!strcmp(a, "--reference") && i + 1 < argc) {
- reference = argv[++i];
- } else if (!strncmp(a, "--reference=", 12)) {
- reference = a + 12;
- } else if (!strcmp(a, "--walk")) {
+ OPT_INIT(&p, "bpm tree show", tree_show_opts, &tree_show_help, argc,
+ argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case TREE_SHOW_CERT:
+ cert = p.value;
+ break;
+ case TREE_SHOW_REFERENCE:
+ reference = p.value;
+ break;
+ case TREE_SHOW_WALK:
walk = true;
- } else if (a[0] != '-' && !raw_repository) {
- raw_repository = a;
- } else {
- tree_show_usage();
- return -EINVAL;
+ break;
+ case OPT_ARG:
+ if (raw_repository) {
+ opt_usage(&p, "unexpected argument '%s'",
+ p.value);
+ return EXIT_USAGE;
+ }
+ raw_repository = p.value;
+ break;
+ case OPT_DONE:
+ return 0;
+ default:
+ return EXIT_USAGE;
}
}
- if (!raw_repository || !cert || !cert[0]) {
- tree_show_usage();
- return -EINVAL;
+ if (!raw_repository || !cert) {
+ opt_usage(&p, "takes <repository> and --cert <cert.der>");
+ return EXIT_USAGE;
}
ret = bpm_oci_parse_tree_repo(raw_repository, &c, repository,
sizeof(repository));
@@ -290,20 +319,18 @@ out:
return ret;
}
-int build_bpm_tree_show(int argc, char **argv)
+int build_bpm_tree_show(int argc, const char **argv)
{
int ret;
- if (argc == 2 && build_is_help(argv[1])) {
- tree_show_usage();
- return 0;
- }
if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
pr_err("bpm tree show: %s", strerror(EIO));
return 1;
}
ret = cmd_bpm_tree_show(argc, argv);
curl_global_cleanup();
+ if (ret == EXIT_USAGE)
+ return EXIT_USAGE;
if (ret < 0)
pr_err("bpm tree show: %s", strerror(-ret));
return ret ? 1 : 0;
diff --git a/tools/shirudo-build/bpm.h b/tools/shirudo-build/bpm.h
index d01148e6..72167f76 100644
--- a/tools/shirudo-build/bpm.h
+++ b/tools/shirudo-build/bpm.h
@@ -111,11 +111,10 @@ void bpm_build_spec_free(struct bpm_build_spec *spec);
void bpm_strings_sort(char strings[][TREE_STRING_MAX], int n);
bool bpm_fix_id_ok(const char *id);
-int build_bpm(int argc, char **argv);
-int build_bpm_push(int argc, char **argv);
-int build_bpm_tree(int argc, char **argv);
-int build_bpm_tree_show(int argc, char **argv);
-bool build_is_help(const char *arg);
+int build_bpm(int argc, const char **argv);
+int build_bpm_push(int argc, const char **argv);
+int build_bpm_tree(int argc, const char **argv);
+int build_bpm_tree_show(int argc, const char **argv);
struct bpm_buffer;
struct bpm_oci_client;
@@ -149,6 +148,6 @@ int build_check_keys(const char *pkey, const char *cert);
/* The file name of source less .bpf.c, .bpf.o or .o. 0 or -E2BIG. */
int build_upatch_name(const char *source, char *out, size_t cap);
-int build_keys_vouch(int argc, char **argv);
+int build_keys_vouch(int argc, const char **argv);
#endif /* SHIRUDO_BUILD_BPM_H */
diff --git a/tools/shirudo-build/image.c b/tools/shirudo-build/image.c
index 4f4040b4..212a4b3b 100644
--- a/tools/shirudo-build/image.c
+++ b/tools/shirudo-build/image.c
@@ -21,26 +21,60 @@
#include "hex.h"
#include "image.h"
#include "io.h"
+#include "help.h"
#include "log.h"
+#include "options.h"
#include "malloc.h"
#include "paths.h"
#include "sign.h"
-static void image_usage(FILE *fp)
-{
- fprintf(fp, "usage: shirudo-build image build <filesystem.img> -o "
- "<prefix>\n"
- " [--kind config|runtime] [--keys-dir <dir>]\n"
- " [--key <private.pem>] [--cert <cert.der>] [--salt "
- "<64-hex>]\n"
- "Writes <prefix>.img/.verity/.roothash/.p7s; existing files "
- "are refused.\n"
- "Input is a 4096-byte-aligned ext4 filesystem image.\n"
- " shirudo-build image verify <prefix> --cert <cert.der>\n"
- "Build and offline verification require veritysetup.\n");
-}
+enum image_verify_opt {
+ IMAGE_VERIFY_CERT,
+};
+
+static const struct opt_spec image_verify_opts[] = {
+ [IMAGE_VERIFY_CERT] = { "--cert", 0, 0, "<cert.der>",
+ "the certificate the image's root hash is signed for" },
+};
+
+enum image_sign_opt {
+ IMAGE_SIGN_OUTPUT,
+ IMAGE_SIGN_KEYS_DIR,
+ IMAGE_SIGN_KEY,
+ IMAGE_SIGN_CERT,
+};
+
+static const struct opt_spec image_sign_opts[] = {
+ [IMAGE_SIGN_OUTPUT] = { "--output", 'o', 0, "<signature>",
+ "where to write the signature" },
+ [IMAGE_SIGN_KEYS_DIR] = { "--keys-dir", 0, 0, "<dir>",
+ "read signing_key.pem and cert.der from <dir>" },
+ [IMAGE_SIGN_KEY] = { "--key", 0, 0, "<private.pem>",
+ "the signing key, over --keys-dir" },
+ [IMAGE_SIGN_CERT] = { "--cert", 0, 0, "<cert.der>",
+ "the certificate, over --keys-dir" },
+};
-static int __build_image(int argc, char **argv)
+static const struct help_section image_sections[] = {
+ HELP_OPTIONS("verify options:", image_verify_opts),
+};
+
+static const struct help image_help = {
+ .usage = "shirudo image build <filesystem.img> -o <prefix> "
+ "[--kind config|runtime]\n"
+ " [--keys-dir <dir>] "
+ "[--key <private.pem>] [--cert <cert.der>]\n"
+ " [--salt <64-hex>]\n"
+ " shirudo image verify <prefix> --cert <cert.der>",
+ .summary = "build writes <prefix>.img, .verity, .roothash and .p7s "
+ "from a 4096-byte\naligned ext4 file system image and "
+ "refuses files that exist. Build and\noffline "
+ "verification require veritysetup.",
+ .sections = image_sections,
+ .n_sections = ARRAY_SIZE(image_sections),
+};
+
+static int __build_image(int argc, const char **argv)
{
char executable[PATH_MAX], script[PATH_MAX], directory[PATH_MAX];
const char **command;
@@ -234,73 +268,116 @@ static int sign_root(const char *input, const char *output, const char *pkey,
return ret;
}
-int build_image(int argc, char **argv)
+static int image_verify_cmd(int argc, const char **argv)
+{
+ const char *prefix = NULL, *cert = NULL;
+ struct opt_parser p;
+ int opt, ret;
+
+ OPT_INIT(&p, "image verify", image_verify_opts, &image_help, argc,
+ argv, 2);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case IMAGE_VERIFY_CERT:
+ cert = p.value;
+ break;
+ case OPT_ARG:
+ if (prefix) {
+ opt_usage(&p, "unexpected argument '%s'",
+ p.value);
+ return EXIT_USAGE;
+ }
+ prefix = p.value;
+ break;
+ case OPT_DONE:
+ return 0;
+ default:
+ return EXIT_USAGE;
+ }
+ }
+ if (!prefix || !cert) {
+ opt_usage(&p, "takes <prefix> and --cert <cert.der>");
+ return EXIT_USAGE;
+ }
+ ret = verify_image(prefix, cert);
+ if (ret)
+ pr_err("image verify: %s", strerror(-ret));
+ return ret ? 1 : 0;
+}
+
+/* What shirudo-gen-verity calls back with for the root hash it made. */
+static int image_sign_root_cmd(int argc, const char **argv)
{
- bool explicit_key = false, explicit_cert = false;
char pkey_path[PATH_MAX], cert_path[PATH_MAX];
+ const char *input = NULL, *signature = NULL;
const char *pkey = SIGNING_KEY_PATH;
- const char *input = NULL, *prefix = NULL;
const char *cert = CERT_PATH;
const char *keys_dir = NULL;
- int i, ret;
-
- if (argc >= 2 && !strcmp(argv[1], "build"))
- return __build_image(argc, argv);
- if (argc == 2 && !strcmp(argv[1], "--help")) {
- image_usage(stdout);
- return 0;
- }
- if (argc == 5 && !strcmp(argv[1], "verify") &&
- !strcmp(argv[3], "--cert")) {
- ret = verify_image(argv[2], argv[4]);
- if (ret)
- pr_err("image verify: %s", strerror(-ret));
- return ret ? 1 : 0;
- }
- if (argc < 2 || strcmp(argv[1], "sign-root")) {
- image_usage(stderr);
- return 2;
- }
- for (i = 2; i < argc; i++) {
- const char *arg = argv[i];
+ struct opt_parser p;
+ int opt, ret;
- if (!strcmp(arg, "--help")) {
- image_usage(stdout);
+ OPT_INIT(&p, "image sign-root", image_sign_opts, NULL, argc, argv, 2);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case IMAGE_SIGN_OUTPUT:
+ signature = p.value;
+ break;
+ case IMAGE_SIGN_KEYS_DIR:
+ keys_dir = p.value;
+ break;
+ case IMAGE_SIGN_KEY:
+ pkey = p.value;
+ break;
+ case IMAGE_SIGN_CERT:
+ cert = p.value;
+ break;
+ case OPT_ARG:
+ if (input) {
+ opt_usage(&p, "unexpected argument '%s'",
+ p.value);
+ return EXIT_USAGE;
+ }
+ input = p.value;
+ break;
+ case OPT_DONE:
return 0;
- }
- if ((!strcmp(arg, "-o") || !strcmp(arg, "--output")) &&
- i + 1 < argc) {
- prefix = argv[++i];
- } else if (!strcmp(arg, "--keys-dir") && i + 1 < argc) {
- keys_dir = argv[++i];
- } else if (!strcmp(arg, "--key") && i + 1 < argc) {
- pkey = argv[++i];
- explicit_key = true;
- } else if (!strcmp(arg, "--cert") && i + 1 < argc) {
- cert = argv[++i];
- explicit_cert = true;
- } else if (arg[0] != '-' && !input) {
- input = arg;
- } else {
- image_usage(stderr);
- return 2;
+ default:
+ return EXIT_USAGE;
}
}
- if (!input || !prefix || !*prefix)
- return 2;
+ if (!input || !signature) {
+ opt_usage(&p, "takes <root-hash-file> and -o <signature>");
+ return EXIT_USAGE;
+ }
keys_dir = keys_dir_resolve(keys_dir);
if (keys_dir) {
ret = key_paths(keys_dir, pkey_path, sizeof(pkey_path),
cert_path, sizeof(cert_path));
if (ret)
- return 2;
- if (!explicit_key)
+ return EXIT_USAGE;
+ if (!opt_given(&p, IMAGE_SIGN_KEY))
pkey = pkey_path;
- if (!explicit_cert)
+ if (!opt_given(&p, IMAGE_SIGN_CERT))
cert = cert_path;
}
- ret = sign_root(input, prefix, pkey, cert);
+ ret = sign_root(input, signature, pkey, cert);
if (ret)
pr_err("image sign-root: %s", strerror(-ret));
return ret ? 1 : 0;
}
+
+int build_image(int argc, const char **argv)
+{
+ if (argc >= 2 && !strcmp(argv[1], "build"))
+ return __build_image(argc, argv);
+ if (argc < 2 || is_help(argv[1])) {
+ print_help(&image_help);
+ return argc < 2 ? EXIT_USAGE : 0;
+ }
+ if (!strcmp(argv[1], "verify"))
+ return image_verify_cmd(argc, argv);
+ if (!strcmp(argv[1], "sign-root"))
+ return image_sign_root_cmd(argc, argv);
+ pr_err("image: '%s' is not a subcommand", argv[1]);
+ return EXIT_USAGE;
+}
diff --git a/tools/shirudo-build/image.h b/tools/shirudo-build/image.h
index dca4f564..d807a3ac 100644
--- a/tools/shirudo-build/image.h
+++ b/tools/shirudo-build/image.h
@@ -4,6 +4,6 @@
#ifndef SHIRUDO_BUILD_IMAGE_H
#define SHIRUDO_BUILD_IMAGE_H
-int build_image(int argc, char **argv);
+int build_image(int argc, const char **argv);
#endif /* SHIRUDO_BUILD_IMAGE_H */
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index 16c46802..c98a89b8 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -32,7 +32,9 @@
#include "elf_target.h"
#include "io.h"
#include "loader.h"
+#include "help.h"
#include "log.h"
+#include "options.h"
#include "malloc.h"
#include "package.h"
#include "paths.h"
@@ -433,7 +435,65 @@ int build_package_verify(const char *input, const char *cert_path)
return ret;
}
-int build_upatch_cli(int argc, char **argv)
+enum rewrite_opt {
+ REWRITE_OUTPUT,
+ REWRITE_DATA_RANGE,
+ REWRITE_KEYS_DIR,
+ REWRITE_KEY,
+ REWRITE_CERT,
+};
+
+static const struct opt_spec rewrite_opts[] = {
+ [REWRITE_OUTPUT] = { "--output", 'o', 0, "<output.upatch>",
+ "where to write the re-signed upatch" },
+ [REWRITE_DATA_RANGE] = { "--data-range", 0, OPT_REPEAT,
+ "<offset:length>",
+ "a range of the loader data that may differ (repeatable)" },
+ [REWRITE_KEYS_DIR] = { "--keys-dir", 0, 0, "<dir>",
+ "read signing_key.pem and cert.der from <dir>" },
+ [REWRITE_KEY] = { "--key", 0, 0, "<signing_key.pem>",
+ "the signing key, over --keys-dir" },
+ [REWRITE_CERT] = { "--cert", 0, 0, "<cert.der>",
+ "the certificate, over --keys-dir" },
+};
+
+static const struct help_section rewrite_sections[] = {
+ HELP_OPTIONS("options:", rewrite_opts),
+};
+
+static const struct help rewrite_help = {
+ .usage = "shirudo-build upatch rewrite-rodata <original.upatch> "
+ "<modified.upatch>\n"
+ " -o <output.upatch> "
+ "--data-range <offset:length>... [options]",
+ .summary = "Re-sign an upatch whose loader data changed inside the "
+ "given ranges.",
+ .sections = rewrite_sections,
+ .n_sections = ARRAY_SIZE(rewrite_sections),
+};
+
+/* An <offset:length> of --data-range, both in 32 bits, length not 0. */
+static int parse_data_range(const char *range, struct data_range *out)
+{
+ size_t off_len = digits(range, strlen(range));
+ const char *length;
+ size_t len_len;
+ __u64 offset, len;
+
+ if (!off_len || range[off_len] != ':')
+ return -EINVAL;
+ length = range + off_len + 1;
+ len_len = digits(length, strlen(length));
+ if (!len_len || length[len_len] ||
+ parse_u64(range, off_len, UINT32_MAX, &offset) ||
+ parse_u64(length, len_len, UINT32_MAX, &len) || !len)
+ return -EINVAL;
+ out->offset = (__u32)offset;
+ out->length = (__u32)len;
+ return 0;
+}
+
+int build_upatch_cli(int argc, const char **argv)
{
const char *keys_dir = NULL, *original = NULL, *modified = NULL;
const char *output = NULL;
@@ -441,78 +501,73 @@ int build_upatch_cli(int argc, char **argv)
const char *cert = CERT_PATH;
struct data_range ranges[16] = {};
char pkey_buf[PATH_MAX], cert_buf[PATH_MAX];
- int pkey_explicit = 0, cert_explicit = 0;
size_t nr_ranges = 0;
- int i;
- int ret;
+ struct opt_parser p;
+ int ret, opt;
if (argc < 2 || strcmp(argv[1], "rewrite-rodata")) {
- fprintf(stderr,
- "usage: shirudo-build upatch rewrite-rodata "
- "<original.upatch> <modified.upatch> -o <output.upatch> "
- "--data-range <offset:length>... [--keys-dir <dir>] "
- "[--key <signing_key.pem>] [--cert <cert.der>]\n");
- return 2;
- }
- for (i = 2; i < argc; i++) {
- if ((!strcmp(argv[i], "-o") || !strcmp(argv[i], "--output")) &&
- i + 1 < argc) {
- output = argv[++i];
- } else if (!strcmp(argv[i], "--data-range") && i + 1 < argc) {
- const char *range = argv[++i];
- size_t off_len = digits(range, strlen(range));
- const char *length;
- size_t len_len;
- __u64 offset, len;
-
- if (nr_ranges == ARRAY_SIZE(ranges) || !off_len ||
- off_len >= strlen(range) ||
- range[off_len] != ':')
- return 2;
- length = range + off_len + 1;
- len_len = digits(length, strlen(length));
- if (!len_len || length[len_len] ||
- parse_u64(range, off_len, UINT32_MAX, &offset) ||
- parse_u64(length, len_len, UINT32_MAX, &len) || !len)
- return 2;
- ranges[nr_ranges].offset = (__u32)offset;
- ranges[nr_ranges].length = (__u32)len;
+ print_help(&rewrite_help);
+ return EXIT_USAGE;
+ }
+ OPT_INIT(&p, "upatch rewrite-rodata", rewrite_opts, &rewrite_help,
+ argc, argv, 2);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case REWRITE_OUTPUT:
+ output = p.value;
+ break;
+ case REWRITE_DATA_RANGE:
+ if (nr_ranges == ARRAY_SIZE(ranges)) {
+ opt_usage(&p, "at most %zu --data-range",
+ ARRAY_SIZE(ranges));
+ return EXIT_USAGE;
+ }
+ if (parse_data_range(p.value, &ranges[nr_ranges])) {
+ opt_usage(&p, "--data-range takes <offset:length>, length not 0");
+ return EXIT_USAGE;
+ }
nr_ranges++;
- } else if (!strcmp(argv[i], "--keys-dir") && i + 1 < argc) {
- keys_dir = argv[++i];
- } else if (!strcmp(argv[i], "--key") && i + 1 < argc) {
- pkey = argv[++i];
- pkey_explicit = 1;
- } else if (!strcmp(argv[i], "--cert") && i + 1 < argc) {
- cert = argv[++i];
- cert_explicit = 1;
- } else if (argv[i][0] == '-') {
- pr_err("upatch rewrite-rodata: unknown option %s", argv[i]);
- return 2;
- } else if (!original) {
- original = argv[i];
- } else if (!modified) {
- modified = argv[i];
- } else {
- pr_err("upatch rewrite-rodata: unexpected argument %s",
- argv[i]);
- return 2;
+ break;
+ case REWRITE_KEYS_DIR:
+ keys_dir = p.value;
+ break;
+ case REWRITE_KEY:
+ pkey = p.value;
+ break;
+ case REWRITE_CERT:
+ cert = p.value;
+ break;
+ case OPT_ARG:
+ if (modified) {
+ opt_usage(&p, "unexpected argument '%s'",
+ p.value);
+ return EXIT_USAGE;
+ }
+ if (original)
+ modified = p.value;
+ else
+ original = p.value;
+ break;
+ case OPT_DONE:
+ return 0;
+ default:
+ return EXIT_USAGE;
}
}
if (!original || !modified || !output || !nr_ranges) {
- pr_err("upatch rewrite-rodata requires two inputs, -o, and data ranges");
- return 2;
+ opt_usage(&p, "takes two inputs, -o and data ranges");
+ return EXIT_USAGE;
}
keys_dir = keys_dir_resolve(keys_dir);
if (keys_dir) {
- if (!pkey_explicit) {
+ if (!opt_given(&p, REWRITE_KEY)) {
ret = format_key_path(pkey_buf, sizeof(pkey_buf), keys_dir,
SIGNING_KEY_FILE);
if (ret)
return 2;
pkey = pkey_buf;
}
- if (!cert_explicit) {
+ if (!opt_given(&p, REWRITE_CERT)) {
ret = format_key_path(cert_buf, sizeof(cert_buf), keys_dir,
CERT_FILE);
if (ret)
diff --git a/tools/shirudo-build/package.h b/tools/shirudo-build/package.h
index ab6b8c53..134bd582 100644
--- a/tools/shirudo-build/package.h
+++ b/tools/shirudo-build/package.h
@@ -29,6 +29,6 @@ int build_package_patch_rodata(const char *input, const char *output,
size_t value_len, const char *pkey_path,
const char *cert_path);
int build_package_verify(const char *input, const char *cert_path);
-int build_upatch_cli(int argc, char **argv);
+int build_upatch_cli(int argc, const char **argv);
#endif /* SHIRUDO_BUILD_PACKAGE_H */
diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.c
index 50d937a4..1d060038 100644
--- a/tools/shirudo-build/shirudo-build.c
+++ b/tools/shirudo-build/shirudo-build.c
@@ -19,7 +19,9 @@
#include "bpm.h"
#include "compile.h"
#include "harden.h"
+#include "help.h"
#include "log.h"
+#include "options.h"
#include "hex.h"
#include "image.h"
#include "malloc.h"
@@ -33,53 +35,66 @@
#define VMLINUX_PATH "/usr/local/share/shirudo/vmlinux.h"
#endif
-static void usage(FILE *fp)
-{
- fprintf(fp,
- "usage:\n"
- " shirudo-build <input> [-o <output.upatch>]\n"
- " [--name <upatch-name>]\n"
- " [--keys-dir <dir>]\n"
- " [--key <signing_key.pem>] [--cert <cert.der>]\n"
- " [--vmlinux <vmlinux.h>] [--extra-cflags '<flags>']\n"
- " [--emit-artifacts]\n"
- "\n"
- " shirudo-build --unload <upatch-name> [-o <output.upatch>]\n"
- " [--payload-sha256 <64-hex>]\n"
- " [--keys-dir <dir>]\n"
- " [--key <signing_key.pem>] [--cert <cert.der>]\n"
- "\n"
- " shirudo-build image build <filesystem.img> -o <prefix> ...\n"
- " shirudo-build bpm <build|push|tree append|tree show> ...\n"
- " shirudo-build keys vouch <cert.der> [-o <file>] ...\n"
- "\n"
- "<input> is .bpf.c (compiled via clang) or .bpf.o.\n"
- "-o defaults to <input> with a trailing .c/.o replaced by\n"
- " .upatch (foo/bar.bpf.c -> foo/bar.bpf.upatch), written\n"
- " next to the input; for --unload it defaults to\n"
- " <name>.unload.upatch in the current directory. Pass -o\n"
- " only to write somewhere else.\n"
- "--keys-dir defaults key/cert to <dir>/signing_key.pem and\n"
- " <dir>/cert.der. If omitted, %s is used when set.\n"
- " Explicit --keys-dir overrides the environment;\n"
- " explicit --key/--cert override both.\n"
- "--key default without --keys-dir/%s: %s\n"
- "--cert default without --keys-dir/%s: %s\n"
- "--vmlinux: explicit; else ./vmlinux.h, ./vendor/linux/vmlinux.h,\n"
- " then %s.\n"
- "--payload-sha256: 64 hex chars; if omitted, read from the local\n"
- " archive /var/lib/shirudo/upatches/<name>.upatch,\n"
- " which must then exist.\n"
- "--emit-artifacts: for a .bpf.c input, also keep the compiled\n"
- " .bpf.o next to the .upatch (instead of a temp\n"
- " file that is removed). Reports every emitted\n"
- " path on stdout. A debugging aid.\n"
- "bpm: the operator-only package commands (`shirudo bpm build`,\n"
- " `push`, `tree append`, `tree show`); see `shirudo-build bpm --help`.\n",
- KEYS_DIR_ENV, KEYS_DIR_ENV,
- SIGNING_KEY_PATH, KEYS_DIR_ENV,
- CERT_PATH, VMLINUX_PATH);
-}
+enum build_opt {
+ BUILD_OUTPUT,
+ BUILD_NAME,
+ BUILD_KEYS_DIR,
+ BUILD_KEY,
+ BUILD_CERT,
+ BUILD_VMLINUX,
+ BUILD_EXTRA_CFLAGS,
+ BUILD_UNLOAD,
+ BUILD_PAYLOAD_SHA256,
+ BUILD_EMIT_ARTIFACTS,
+};
+
+static const struct opt_spec build_opts[] = {
+ [BUILD_OUTPUT] = { "--output", 'o', 0, "<output.upatch>",
+ "where to write the upatch instead of beside the input, or of "
+ "<name>.unload.upatch in the current directory" },
+ [BUILD_NAME] = { "--name", 0, 0, "<upatch-name>",
+ "the upatch's name instead of the one the input gives" },
+ [BUILD_KEYS_DIR] = { "--keys-dir", 0, 0, "<dir>",
+ "read signing_key.pem and cert.der from <dir>, where "
+ KEYS_DIR_ENV " points otherwise" },
+ [BUILD_KEY] = { "--key", 0, 0, "<signing_key.pem>",
+ "the signing key, over --keys-dir (default "
+ SIGNING_KEY_PATH ")" },
+ [BUILD_CERT] = { "--cert", 0, 0, "<cert.der>",
+ "the certificate, over --keys-dir (default " CERT_PATH ")" },
+ [BUILD_VMLINUX] = { "--vmlinux", 0, 0, "<vmlinux.h>",
+ "compile against it instead of ./vmlinux.h, "
+ "./vendor/linux/vmlinux.h or " VMLINUX_PATH },
+ [BUILD_EXTRA_CFLAGS] = { "--extra-cflags", 0, 0, "'<flags>'",
+ "more flags for clang" },
+ [BUILD_UNLOAD] = { "--unload", 0, 0, "<upatch-name>",
+ "sign the UNLOAD directive of the named upatch" },
+ [BUILD_PAYLOAD_SHA256] = { "--payload-sha256", 0, 0, "<64-hex>",
+ "with --unload, the payload digest, which the archive "
+ "/var/lib/shirudo/upatches/<name>.upatch gives otherwise" },
+ [BUILD_EMIT_ARTIFACTS] = { "--emit-artifacts", 0, 0, NULL,
+ "keep the compiled .bpf.o beside the .upatch and print each "
+ "path written" },
+};
+
+static const struct help_section build_sections[] = {
+ HELP_OPTIONS("options:", build_opts),
+};
+
+static const struct help build_help = {
+ .usage = "shirudo build <input.bpf.c|.bpf.o> [-o <output.upatch>] "
+ "[options]\n"
+ " shirudo build --unload <upatch-name> "
+ "[-o <output.upatch>] [options]",
+ .summary = "Compile a .bpf.c, or take a .bpf.o, and sign it into a "
+ ".upatch, or sign\nthe UNLOAD directive of a loaded "
+ "upatch. foo/bar.bpf.c becomes\nfoo/bar.bpf.upatch.",
+ .sections = build_sections,
+ .n_sections = ARRAY_SIZE(build_sections),
+ .footer = "The helper also serves shirudo image build and verify, "
+ "the operator\nbpm commands and keys vouch; each takes "
+ "--help.",
+};
/* foo/bar.bpf.c lands at foo/bar.bpf.upatch beside its input. */
static int derive_output(const char *input, char *buf, size_t buf_sz)
@@ -154,6 +169,8 @@ int build_check_keys(const char *pkey, const char *cert)
int main(int argc, char **argv)
{
+ /* Nothing writes the arguments; the parsers take them as const. */
+ const char **args = (const char **)argv;
const char *pkey = SIGNING_KEY_PATH;
const char *payload_sha256_hex = NULL;
const char *cert = CERT_PATH;
@@ -178,66 +195,81 @@ int main(int argc, char **argv)
int emit_artifacts = 0;
int pkey_explicit = 0;
int cert_explicit = 0;
- int ret;
- int i;
+ struct opt_parser p;
+ int ret, opt;
harden_stdio();
log_init();
if (harden_self())
return 1;
- if (argc >= 2 && strcmp(argv[1], "image") == 0)
- return build_image(argc - 1, argv + 1);
- if (argc >= 2 && strcmp(argv[1], "upatch") == 0)
- return build_upatch_cli(argc - 1, argv + 1);
- if (argc >= 2 && strcmp(argv[1], "bpm") == 0)
- return build_bpm(argc - 1, argv + 1);
- if (argc >= 3 && strcmp(argv[1], "keys") == 0 &&
- strcmp(argv[2], "vouch") == 0)
- return build_keys_vouch(argc - 2, argv + 2);
-
- for (i = 1; i < argc; i++) {
- const char *a = argv[i];
-
- if ((strcmp(a, "-o") == 0 || strcmp(a, "--output") == 0) &&
- i + 1 < argc) {
- output = argv[++i];
- } else if (strcmp(a, "--name") == 0 && i + 1 < argc) {
- name = argv[++i];
- } else if (strcmp(a, "--keys-dir") == 0 && i + 1 < argc) {
- keys_dir_arg = argv[++i];
- } else if (strncmp(a, "--keys-dir=", 11) == 0) {
- keys_dir_arg = a + 11;
- } else if (strcmp(a, "--key") == 0 && i + 1 < argc) {
- pkey = argv[++i];
+ if (argc >= 2 && strcmp(args[1], "image") == 0)
+ return build_image(argc - 1, args + 1);
+ if (argc >= 2 && strcmp(args[1], "upatch") == 0)
+ return build_upatch_cli(argc - 1, args + 1);
+ if (argc >= 2 && strcmp(args[1], "bpm") == 0)
+ return build_bpm(argc - 1, args + 1);
+ if (argc >= 3 && strcmp(args[1], "keys") == 0 &&
+ strcmp(args[2], "vouch") == 0)
+ return build_keys_vouch(argc - 2, args + 2);
+
+ OPT_INIT(&p, "build", build_opts, &build_help, argc, args, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case BUILD_OUTPUT:
+ output = p.value;
+ break;
+ case BUILD_NAME:
+ name = p.value;
+ break;
+ case BUILD_KEYS_DIR:
+ keys_dir_arg = p.value;
+ break;
+ case BUILD_KEY:
+ pkey = p.value;
pkey_explicit = 1;
- } else if (strcmp(a, "--cert") == 0 && i + 1 < argc) {
- cert = argv[++i];
+ break;
+ case BUILD_CERT:
+ cert = p.value;
cert_explicit = 1;
- } else if (strcmp(a, "--vmlinux") == 0 && i + 1 < argc) {
- vmlinux_override = argv[++i];
- } else if (strcmp(a, "--extra-cflags") == 0 && i + 1 < argc) {
- extra_cflags = argv[++i];
- } else if (strcmp(a, "--unload") == 0 && i + 1 < argc) {
- unload_name = argv[++i];
- } else if (strcmp(a, "--payload-sha256") == 0 && i + 1 < argc) {
- payload_sha256_hex = argv[++i];
- } else if (strcmp(a, "--emit-artifacts") == 0) {
+ break;
+ case BUILD_VMLINUX:
+ vmlinux_override = p.value;
+ break;
+ case BUILD_EXTRA_CFLAGS:
+ extra_cflags = p.value;
+ break;
+ case BUILD_UNLOAD:
+ unload_name = p.value;
+ break;
+ case BUILD_PAYLOAD_SHA256:
+ payload_sha256_hex = p.value;
+ break;
+ case BUILD_EMIT_ARTIFACTS:
emit_artifacts = 1;
- } else if (strcmp(a, "-h") == 0 || strcmp(a, "--help") == 0) {
- usage(stdout);
+ break;
+ case OPT_ARG:
+ if (input) {
+ opt_usage(&p, "unexpected argument '%s'",
+ p.value);
+ return EXIT_USAGE;
+ }
+ input = p.value;
+ break;
+ case OPT_DONE:
return 0;
- } else if (a[0] == '-') {
- pr_err("unknown option '%s'", a);
- usage(stderr);
- return 2;
- } else if (!input) {
- input = a;
- } else {
- pr_err("unexpected positional arg '%s'", a);
- return 2;
+ default:
+ return EXIT_USAGE;
}
}
+ if (unload_name && input) {
+ opt_usage(&p, "--unload takes no input");
+ return EXIT_USAGE;
+ }
+ if (!unload_name && !input) {
+ opt_usage(&p, "takes <input.bpf.c|.bpf.o> or --unload <upatch-name>");
+ return EXIT_USAGE;
+ }
keys_dir = keys_dir_resolve(keys_dir_arg);
@@ -269,15 +301,11 @@ int main(int argc, char **argv)
return 1;
if (unload_name) {
- if (input) {
- pr_err("--unload takes no positional input");
- return 2;
- }
if (payload_sha256_hex) {
if (hex_decode(payload_sha256_hex,
payload_sha256, 32)) {
- pr_err("--payload-sha256 must be 64 hex chars");
- return 2;
+ opt_usage(&p, "--payload-sha256 must be 64 hex characters");
+ return EXIT_USAGE;
}
} else {
/*
@@ -329,10 +357,6 @@ int main(int argc, char **argv)
return ret ? 1 : 0;
}
- if (!input) {
- usage(stderr);
- return 2;
- }
/*
* Resolve the output path before compiling: --emit-artifacts
diff --git a/tools/shirudo-build/vouch.c b/tools/shirudo-build/vouch.c
index 2ad50ab4..fd12d408 100644
--- a/tools/shirudo-build/vouch.c
+++ b/tools/shirudo-build/vouch.c
@@ -22,23 +22,48 @@
#include "util/io.h"
#include "util/keys.h"
#include "util/log.h"
+#include "util/help.h"
#include "util/malloc.h"
+#include "util/options.h"
#include "util/paths.h"
#include "util/sign.h"
#define VOUCH_SIG_MAX (64u * 1024u)
-static void usage(FILE *fp)
-{
- fprintf(fp,
- "usage: shirudo-build keys vouch <cert.der> [-o <file>]\n"
- " [--keys-dir <dir>] [--key <signing_key.pem>]\n"
- " [--cert <cert.der>]\n"
- "\n"
- "Sign a vouch for <cert.der> with the operator key, for\n"
- "`shirudo keys add --vouch` on the targets that enrol this\n"
- "key. -o defaults to <cert.der>.vouch.\n");
-}
+enum vouch_opt {
+ VOUCH_OUTPUT,
+ VOUCH_KEYS_DIR,
+ VOUCH_KEY,
+ VOUCH_CERT,
+};
+
+static const struct opt_spec vouch_opts[] = {
+ [VOUCH_OUTPUT] = { "--output", 'o', 0, "<file>",
+ "where to write the vouch (default <cert.der>.vouch)" },
+ [VOUCH_KEYS_DIR] = { "--keys-dir", 0, 0, "<dir>",
+ "read signing_key.pem and cert.der from <dir>" },
+ [VOUCH_KEY] = { "--key", 0, 0, "<signing_key.pem>",
+ "the signing key (overrides --keys-dir)" },
+ [VOUCH_CERT] = { "--cert", 0, 0, "<cert.der>",
+ "the signing certificate (overrides --keys-dir)" },
+};
+
+static const struct help_section vouch_sections[] = {
+ HELP_OPTIONS("options:", vouch_opts),
+};
+
+static const struct help vouch_help = {
+ .usage = "shirudo keys vouch <cert.der> [-o <file>] "
+ "[--keys-dir <dir>]\n"
+ " [--key <signing_key.pem>] "
+ "[--cert <cert.der>]",
+ .summary = "Sign a vouch for another operator's certificate with "
+ "this operator's key,\nfor `shirudo keys add --vouch` on "
+ "the targets that enrol this key.\nPrints the vouched "
+ "certificate's fingerprint.",
+ .sections = vouch_sections,
+ .n_sections = ARRAY_SIZE(vouch_sections),
+};
static int cert_algorithm_ok(const __u8 *der, size_t der_sz,
const char *what)
@@ -77,7 +102,7 @@ static int write_file(const char *path, const void *data, size_t len)
return ret;
}
-int build_keys_vouch(int argc, char **argv)
+int build_keys_vouch(int argc, const char **argv)
{
const char *pkey = SIGNING_KEY_PATH, *cert = CERT_PATH;
const char *keys_dir_arg = NULL, *input = NULL, *output = NULL;
@@ -90,42 +115,44 @@ int build_keys_vouch(int argc, char **argv)
char pkey_path[PATH_MAX];
char cert_path[PATH_MAX];
char out_path[PATH_MAX];
+ struct opt_parser p;
const char *keys_dir;
- int ret = 1;
+ int ret = 1, opt;
- for (int i = 1; i < argc; i++) {
- const char *a = argv[i];
-
- if ((!strcmp(a, "-o") || !strcmp(a, "--output")) &&
- i + 1 < argc) {
- output = argv[++i];
- } else if (!strcmp(a, "--keys-dir") && i + 1 < argc) {
- keys_dir_arg = argv[++i];
- } else if (!strncmp(a, "--keys-dir=", 11)) {
- keys_dir_arg = a + 11;
- } else if (!strcmp(a, "--key") && i + 1 < argc) {
- pkey = argv[++i];
+ OPT_INIT(&p, "keys vouch", vouch_opts, &vouch_help, argc, argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case VOUCH_OUTPUT:
+ output = p.value;
+ break;
+ case VOUCH_KEYS_DIR:
+ keys_dir_arg = p.value;
+ break;
+ case VOUCH_KEY:
+ pkey = p.value;
pkey_explicit = 1;
- } else if (!strcmp(a, "--cert") && i + 1 < argc) {
- cert = argv[++i];
+ break;
+ case VOUCH_CERT:
+ cert = p.value;
cert_explicit = 1;
- } else if (!strcmp(a, "-h") || !strcmp(a, "--help")) {
- usage(stdout);
+ break;
+ case OPT_ARG:
+ if (input) {
+ opt_usage(&p, "unexpected argument '%s'",
+ p.value);
+ return EXIT_USAGE;
+ }
+ input = p.value;
+ break;
+ case OPT_DONE:
return 0;
- } else if (a[0] == '-') {
- pr_err("keys vouch: unknown option '%s'", a);
- usage(stderr);
- return 2;
- } else if (!input) {
- input = a;
- } else {
- pr_err("keys vouch: unexpected argument '%s'", a);
- return 2;
+ default:
+ return EXIT_USAGE;
}
}
if (!input) {
- usage(stderr);
- return 2;
+ opt_usage(&p, "takes <cert.der>");
+ return EXIT_USAGE;
}
keys_dir = keys_dir_resolve(keys_dir_arg);
if (keys_dir) {