-
Notifications
You must be signed in to change notification settings - Fork 2
/
csn-tool.c
207 lines (166 loc) · 4.31 KB
/
csn-tool.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
#include <fcntl.h>
#include <limits.h>
#include "encrypt.h"
#include "chunk.h"
#include "utils.h"
static int chunk_id_from_path(uint8_t id[CHUNK_ID_LEN], const char *path)
{
const char *basename = path;
char *p = strrchr(basename, '/');
if (p)
basename = p+1;
size_t name_len = strlen(basename);
if (name_len != 70 && name_len != 74) {
u_log(ERR, "chunk basename '%s' has wrong length, expected 70 or 74, got %zu", basename, name_len);
return -1;
}
if (!streq(&basename[name_len-6], ".cacnk") && !streq(&basename[name_len-10], ".cacnk.enc")) {
u_log(ERR, "chunk basename '%s' does not end with '.cacnk' or '.cacnk.enc'", basename);
return -1;
}
char id_buf[65];
memcpy(id_buf, basename, 64);
id_buf[64] = 0;
return parse_hex(id, id_buf);
}
static int encrypt_one(struct encrypt_ctx *ec, const char *input_path, const char *output_path)
{
int ret = -1;
uint8_t chunk_id[CHUNK_ID_LEN];
if (chunk_id_from_path(chunk_id, input_path) < 0) {
u_log(ERR, "failed to derive chunk id from path");
return -1;
}
char path_buf[PATH_MAX];
if (!output_path){
size_t len = strlen(input_path);
if (len >= 6 && streq(&input_path[len-6], ".cacnk")) {
if (len > PATH_MAX - 5) {
u_log(ERR, "input path too long");
return -1;
}
strcpy(path_buf, input_path);
strcpy(&path_buf[len], ".enc");
output_path = path_buf;
} else if (len >= 10 && streq(&input_path[len-10], ".cacnk.enc")) {
if (len > PATH_MAX - 1) {
u_log(ERR, "input path too long");
return -1;
}
strcpy(path_buf, input_path);
path_buf[len-4] = 0;
output_path = path_buf;
} else {
u_log(ERR, "unsupported input path format");
return -1;
}
}
if (access(output_path, F_OK) == 0)
return 0;
// this uses the first 24 bytes of chunk_id
if (encrypt_restart(ec, chunk_id) < 0) {
u_log(ERR, "encrypt_restart failed");
return -1;
}
off_t chunk_size;
uint8_t *buf = slurp_file(input_path, false, &chunk_size);
if (!buf) {
u_log(ERR, "failed to load source chunk");
return -1;
}
if (encrypt_do(ec, buf, buf, chunk_size)) {
u_log(ERR, "encrypting chunk failed");
goto out_buf;
}
int fd;
if (streq(output_path, "-")) {
fd = STDOUT_FILENO;
} else {
fd = open(output_path, O_WRONLY | O_TRUNC | O_CREAT, 0444);
if (fd < 0) {
u_log_errno("failed to create output file");
goto out_buf;
}
}
if (writeall(fd, buf, chunk_size) < 0) {
u_log(ERR, "failed to write to output file");
goto out_fd;
}
ret = 0;
out_fd:
if (fd != STDOUT_FILENO)
close(fd);
out_buf:
free(buf);
return ret;
}
static int do_crypt(int argc, char **argv)
{
int ret = EXIT_FAILURE;
if (argc < 2 || argc > 4 ||
streq(argv[1], "-h") || streq(argv[1], "--help")) {
fprintf(stderr, "usage: crypt keyspec [/path/to/input] [/path/to/output]\n");
return EXIT_FAILURE;
}
uint8_t key[32];
if (encrypt_parse_keyspec(key, argv[1]) < 0) {
u_log(ERR, "failed to parse keyspec");
return EXIT_FAILURE;
}
struct encrypt_ctx ec;
if (encrypt_init(&ec, key) < 0) {
u_log(ERR, "encrypt_init failed");
return EXIT_FAILURE;
}
if (argc == 2) {
char line[PATH_MAX];
while ((fgets(line, sizeof(line), stdin))) {
chomp(line);
if (encrypt_one(&ec, line, NULL) < 0)
goto out;
}
ret = EXIT_SUCCESS;
} else {
char *input_path = argv[2];
char *output_path = NULL;
if (argc > 3)
output_path = argv[3];
if (encrypt_one(&ec, input_path, output_path) == 0)
ret = EXIT_SUCCESS;
}
out:
encrypt_close(&ec);
return ret;
}
static struct {
const char *name, *desc;
int (*fn)(int argc, char **argv);
} cmds[] = {
{"crypt", "encrypt/decrypt a single chunk", do_crypt},
{0}
};
__attribute__((noreturn)) static void usage(const char *prog, bool error)
{
FILE *f = error ? stderr : stdout;
fprintf(f, "usage: %s command\n", prog);
fprintf(f, "available commands:\n");
for (size_t i = 0; cmds[i].name; i++)
fprintf(f, " - %s: %s\n", cmds[i].name, cmds[i].desc);
exit(error ? EXIT_FAILURE : EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
u_log_init();
if (argc < 2)
usage(argv[0], true);
if (argc >= 2 && (streq(argv[1], "-h") || streq(argv[1], "--help")))
usage(argv[0], false);
for (size_t i = 0; cmds[i].name; i++)
if (streq(cmds[i].name, argv[1]))
return cmds[i].fn(argc - 1, argv + 1);
usage(argv[0], false);
}