Skip to content

eBPF-based security platform

quote-check: take its arguments through the option parser

Daniel Borkmann6ca458891358
diff --git a/Documentation/reference/cli/quote-check.rst b/Documentation/reference/cli/quote-check.rst
index 54cd6469..6472c927 100644
--- a/Documentation/reference/cli/quote-check.rst
+++ b/Documentation/reference/cli/quote-check.rst
@@ -32,7 +32,7 @@ Usage
shirudo-quote-check
The program takes no option besides ``-h`` and ``--help``, which print
-the usage on standard output. It reads rows from standard input until
+the usage on standard error. It reads rows from standard input until
the input ends and writes one line to standard output for each row, in
the order of the rows. It flushes standard output after every line, so
a caller that keeps the process running gets each answer before it sends
@@ -233,7 +233,8 @@ Exit status
``shirudo-quote-check: <reason>`` on standard error. The row it
stopped at has no line.
* - 2
- - An argument other than ``-h`` or ``--help``.
+ - An argument or an option other than ``-h`` or ``--help``, reported
+ on one line.
Examples
--------
diff --git a/Makefile.shirudo b/Makefile.shirudo
index ed1e75e6..9aea9d0a 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -568,7 +568,8 @@ tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o util/config_image.o
PKG_CONFIG ?= pkg-config
QUOTE_CHECK_UTIL_SRCS := util/tpm_wire.c util/crypto_openssl.c util/hex.c \
util/json.c util/io.c util/seal.c util/log.c \
- util/malloc.c util/string.c
+ util/malloc.c util/string.c util/options.c \
+ util/help.c util/similar.c
# A distribution without a static libcrypto, Fedora for one, gets the
# prefix of a static OpenSSL here. The checker's objects then compile
diff --git a/tools/quote-check/quote-check.c b/tools/quote-check/quote-check.c
index fc949f41..ffbe7d03 100644
--- a/tools/quote-check/quote-check.c
+++ b/tools/quote-check/quote-check.c
@@ -10,26 +10,25 @@
#include <stdio.h>
#include <string.h>
+#include "help.h"
#include "io.h"
#include "malloc.h"
+#include "options.h"
#include "row.h"
-static void usage(FILE *fp)
-{
- fprintf(fp,
- "usage: shirudo-quote-check\n"
- "\n"
- "Reads TabSeparated rows from standard input until it ends:\n"
- " <attest>\\t<sig>\\t<ak>\\t<body_sha256>\n"
- "attest, sig and ak are a keepalive quote's TPMS_ATTEST and\n"
- "TPMT_SIGNATURE and the signing key's TPMT_PUBLIC, in standard\n"
- "base64. body_sha256 is the SHA-256 of the record's body, 64\n"
- "hex digits. Writes one JSON line per row, in order:\n"
- " {\"ok\":1,\"ak_name\":\"<hex>\",\"ak_public\":\"<base64>\",\n"
- " \"reset_count\":<n>,\"clock\":<n>}\n"
- " {\"ok\":0,\"reason\":\"bad_signature\"|\"body_mismatch\"|\n"
- " \"malformed\"}\n");
-}
+static const struct help quote_check_help = {
+ .usage = "shirudo-quote-check",
+ .summary = "Reads TabSeparated rows from standard input until it ends:\n"
+ " <attest>\\t<sig>\\t<ak>\\t<body_sha256>\n"
+ "attest, sig and ak are a keepalive quote's TPMS_ATTEST and\n"
+ "TPMT_SIGNATURE and the signing key's TPMT_PUBLIC, in standard\n"
+ "base64. body_sha256 is the SHA-256 of the record's body, 64\n"
+ "hex digits. Writes one JSON line per row, in order:\n"
+ " {\"ok\":1,\"ak_name\":\"<hex>\",\"ak_public\":\"<base64>\",\n"
+ " \"reset_count\":<n>,\"clock\":<n>}\n"
+ " {\"ok\":0,\"reason\":\"bad_signature\"|\"body_mismatch\"|\n"
+ " \"malformed\"}",
+};
/* got is what read_text_line() returned for line, never 0. */
static int line_check(const char *line, int got, struct quote_result *res)
@@ -67,16 +66,19 @@ int main(int argc, char **argv)
{
struct quote_result res;
char *line = NULL;
+ struct opt_parser p;
size_t cap = 0;
- int got, ret = 0;
+ int got, ret = 0, opt;
- if (argc == 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) {
- usage(stdout);
+ opt_init(&p, "shirudo-quote-check", NULL, 0, &quote_check_help, argc,
+ (const char **)argv, 1);
+ opt = opt_next(&p);
+ if (opt == OPT_DONE)
return 0;
- }
- if (argc != 1) {
- usage(stderr);
- return 2;
+ if (opt != OPT_END) {
+ if (opt == OPT_ARG)
+ opt_usage(&p, "takes no argument, got '%s'", p.value);
+ return EXIT_USAGE;
}
do {