aboutsummaryrefslogtreecommitdiffstats
path: root/fsverity.c
blob: db8966e166e74c95bdb987f212d3af56fa935596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// SPDX-License-Identifier: GPL-2.0+
/*
 * fs-verity userspace tool
 *
 * Copyright (C) 2018 Google, Inc.
 *
 * Written by Eric Biggers, 2018.
 */

#include <stdlib.h>
#include <string.h>

#include "commands.h"
#include "hash_algs.h"

static const struct fsverity_command {
	const char *name;
	int (*func)(const struct fsverity_command *cmd, int argc, char *argv[]);
	const char *short_desc;
	const char *usage_str;
} fsverity_commands[] = {
	{
		.name = "enable",
		.func = fsverity_cmd_enable,
		.short_desc =
"Enable fs-verity on a file with verity metadata",
		.usage_str =
"    fsverity enable FILE\n"
	}, {
		.name = "measure",
		.func = fsverity_cmd_measure,
		.short_desc =
"Display the measurement of the given fs-verity file(s)",
		.usage_str =
"    fsverity measure FILE...\n"
	}, {
		.name = "setup",
		.func = fsverity_cmd_setup,
		.short_desc = "Create the verity metadata for a file",
		.usage_str =
"    fsverity setup INFILE [OUTFILE]\n"
"                   [--hash=HASH_ALG] [--salt=SALT] [--signing-key=KEYFILE]\n"
"                   [--signing-cert=CERTFILE] [--signature=SIGFILE]\n"
"                   [--patch=OFFSET,PATCHFILE] [--elide=OFFSET,LENGTH]\n"
	}
};

static void usage_all(FILE *fp)
{
	int i;

	fputs("Usage:\n", fp);
	for (i = 0; i < ARRAY_SIZE(fsverity_commands); i++)
		fprintf(fp, "  %s:\n%s\n", fsverity_commands[i].short_desc,
			fsverity_commands[i].usage_str);
	fputs(
"  Standard options:\n"
"    fsverity --help\n"
"    fsverity --version\n"
"\n"
"Available hash algorithms: ", fp);
	show_all_hash_algs(fp);
	fputs("\nSee `man fsverity` for more details.\n", fp);
}

static void usage_cmd(const struct fsverity_command *cmd, FILE *fp)
{
	fprintf(fp, "Usage:\n%s", cmd->usage_str);
}

void usage(const struct fsverity_command *cmd, FILE *fp)
{
	if (cmd)
		usage_cmd(cmd, fp);
	else
		usage_all(fp);
}

#define PACKAGE_VERSION    "v0.0-alpha"
#define PACKAGE_BUGREPORT  "linux-fscrypt@vger.kernel.org"

static void show_version(void)
{
	static const char * const str =
"fsverity " PACKAGE_VERSION "\n"
"Copyright (C) 2018 Google, Inc.\n"
"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n"
"Report bugs to " PACKAGE_BUGREPORT ".\n";
	fputs(str, stdout);
}

static void handle_common_options(int argc, char *argv[],
				  const struct fsverity_command *cmd)
{
	int i;

	for (i = 1; i < argc; i++) {
		const char *arg = argv[i];

		if (*arg++ != '-')
			continue;
		if (*arg++ != '-')
			continue;
		if (!strcmp(arg, "help")) {
			usage(cmd, stdout);
			exit(0);
		} else if (!strcmp(arg, "version")) {
			show_version();
			exit(0);
		} else if (!*arg) /* reached "--", no more options */
			return;
	}
}

static const struct fsverity_command *find_command(const char *name)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(fsverity_commands); i++)
		if (!strcmp(name, fsverity_commands[i].name))
			return &fsverity_commands[i];
	return NULL;
}

int main(int argc, char *argv[])
{
	const struct fsverity_command *cmd;

	if (argc < 2) {
		error_msg("no command specified");
		usage_all(stderr);
		return 2;
	}

	cmd = find_command(argv[1]);

	handle_common_options(argc, argv, cmd);

	if (!cmd) {
		error_msg("unrecognized command: '%s'", argv[1]);
		usage_all(stderr);
		return 2;
	}
	return cmd->func(cmd, argc - 1, argv + 1);
}