blob: a463c7f8c88b21a839087fee147b39c8ec5504c3 [file] [log] [blame]
Eric Biggers431c67b2018-06-27 15:01:06 -07001// SPDX-License-Identifier: GPL-2.0+
Eric Biggers1e64b3d2018-03-21 17:53:20 -07002/*
3 * fs-verity userspace tool
4 *
Eric Biggers8387ad32018-08-21 12:37:56 -07005 * Copyright (C) 2018 Google LLC
Eric Biggers431c67b2018-06-27 15:01:06 -07006 *
Eric Biggers8387ad32018-08-21 12:37:56 -07007 * Written by Eric Biggers.
Eric Biggers1e64b3d2018-03-21 17:53:20 -07008 */
9
Eric Biggers1e64b3d2018-03-21 17:53:20 -070010#include <stdlib.h>
11#include <string.h>
Eric Biggers1e64b3d2018-03-21 17:53:20 -070012
Eric Biggers431c67b2018-06-27 15:01:06 -070013#include "commands.h"
14#include "hash_algs.h"
Eric Biggers1e64b3d2018-03-21 17:53:20 -070015
Eric Biggers431c67b2018-06-27 15:01:06 -070016static const struct fsverity_command {
Eric Biggers1e64b3d2018-03-21 17:53:20 -070017 const char *name;
Eric Biggers431c67b2018-06-27 15:01:06 -070018 int (*func)(const struct fsverity_command *cmd, int argc, char *argv[]);
19 const char *short_desc;
20 const char *usage_str;
21} fsverity_commands[] = {
22 {
23 .name = "enable",
24 .func = fsverity_cmd_enable,
25 .short_desc =
26"Enable fs-verity on a file with verity metadata",
27 .usage_str =
28" fsverity enable FILE\n"
29 }, {
Eric Biggers25b59452018-07-27 10:47:02 -070030 .name = "measure",
31 .func = fsverity_cmd_measure,
32 .short_desc =
33"Display the measurement of the given fs-verity file(s)",
34 .usage_str =
35" fsverity measure FILE...\n"
36 }, {
Eric Biggers431c67b2018-06-27 15:01:06 -070037 .name = "setup",
38 .func = fsverity_cmd_setup,
39 .short_desc = "Create the verity metadata for a file",
40 .usage_str =
41" fsverity setup INFILE [OUTFILE]\n"
42" [--hash=HASH_ALG] [--salt=SALT] [--signing-key=KEYFILE]\n"
43" [--signing-cert=CERTFILE] [--signature=SIGFILE]\n"
44" [--patch=OFFSET,PATCHFILE] [--elide=OFFSET,LENGTH]\n"
Eric Biggers25b59452018-07-27 10:47:02 -070045 }
Eric Biggers1e64b3d2018-03-21 17:53:20 -070046};
47
Eric Biggers431c67b2018-06-27 15:01:06 -070048static void usage_all(FILE *fp)
Eric Biggers1e64b3d2018-03-21 17:53:20 -070049{
Eric Biggers431c67b2018-06-27 15:01:06 -070050 int i;
Eric Biggers1e64b3d2018-03-21 17:53:20 -070051
Eric Biggers431c67b2018-06-27 15:01:06 -070052 fputs("Usage:\n", fp);
53 for (i = 0; i < ARRAY_SIZE(fsverity_commands); i++)
54 fprintf(fp, " %s:\n%s\n", fsverity_commands[i].short_desc,
55 fsverity_commands[i].usage_str);
56 fputs(
57" Standard options:\n"
58" fsverity --help\n"
59" fsverity --version\n"
60"\n"
61"Available hash algorithms: ", fp);
62 show_all_hash_algs(fp);
63 fputs("\nSee `man fsverity` for more details.\n", fp);
Eric Biggers1e64b3d2018-03-21 17:53:20 -070064}
65
Eric Biggers431c67b2018-06-27 15:01:06 -070066static void usage_cmd(const struct fsverity_command *cmd, FILE *fp)
Eric Biggers1e64b3d2018-03-21 17:53:20 -070067{
Eric Biggers431c67b2018-06-27 15:01:06 -070068 fprintf(fp, "Usage:\n%s", cmd->usage_str);
69}
Eric Biggers1e64b3d2018-03-21 17:53:20 -070070
Eric Biggers431c67b2018-06-27 15:01:06 -070071void usage(const struct fsverity_command *cmd, FILE *fp)
72{
73 if (cmd)
74 usage_cmd(cmd, fp);
75 else
76 usage_all(fp);
77}
78
79#define PACKAGE_VERSION "v0.0-alpha"
80#define PACKAGE_BUGREPORT "linux-fscrypt@vger.kernel.org"
81
82static void show_version(void)
83{
84 static const char * const str =
85"fsverity " PACKAGE_VERSION "\n"
Eric Biggers8387ad32018-08-21 12:37:56 -070086"Copyright (C) 2018 Google LLC\n"
Eric Biggers431c67b2018-06-27 15:01:06 -070087"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.\n"
88"This is free software: you are free to change and redistribute it.\n"
89"There is NO WARRANTY, to the extent permitted by law.\n"
90"\n"
91"Report bugs to " PACKAGE_BUGREPORT ".\n";
92 fputs(str, stdout);
93}
94
95static void handle_common_options(int argc, char *argv[],
96 const struct fsverity_command *cmd)
97{
98 int i;
99
100 for (i = 1; i < argc; i++) {
101 const char *arg = argv[i];
102
103 if (*arg++ != '-')
104 continue;
105 if (*arg++ != '-')
106 continue;
107 if (!strcmp(arg, "help")) {
108 usage(cmd, stdout);
109 exit(0);
110 } else if (!strcmp(arg, "version")) {
111 show_version();
112 exit(0);
113 } else if (!*arg) /* reached "--", no more options */
114 return;
Eric Biggers1e64b3d2018-03-21 17:53:20 -0700115 }
Eric Biggers431c67b2018-06-27 15:01:06 -0700116}
117
118static const struct fsverity_command *find_command(const char *name)
119{
120 int i;
121
122 for (i = 0; i < ARRAY_SIZE(fsverity_commands); i++)
123 if (!strcmp(name, fsverity_commands[i].name))
124 return &fsverity_commands[i];
Eric Biggers1e64b3d2018-03-21 17:53:20 -0700125 return NULL;
126}
127
Eric Biggers431c67b2018-06-27 15:01:06 -0700128int main(int argc, char *argv[])
Eric Biggers1e64b3d2018-03-21 17:53:20 -0700129{
Eric Biggers431c67b2018-06-27 15:01:06 -0700130 const struct fsverity_command *cmd;
Eric Biggers1e64b3d2018-03-21 17:53:20 -0700131
Eric Biggers431c67b2018-06-27 15:01:06 -0700132 if (argc < 2) {
133 error_msg("no command specified");
134 usage_all(stderr);
Eric Biggers1e64b3d2018-03-21 17:53:20 -0700135 return 2;
136 }
137
Eric Biggers431c67b2018-06-27 15:01:06 -0700138 cmd = find_command(argv[1]);
139
140 handle_common_options(argc, argv, cmd);
141
142 if (!cmd) {
143 error_msg("unrecognized command: '%s'", argv[1]);
144 usage_all(stderr);
145 return 2;
Eric Biggers1e64b3d2018-03-21 17:53:20 -0700146 }
Eric Biggers431c67b2018-06-27 15:01:06 -0700147 return cmd->func(cmd, argc - 1, argv + 1);
Eric Biggers1e64b3d2018-03-21 17:53:20 -0700148}