-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmisccmds.c
342 lines (299 loc) · 6.52 KB
/
misccmds.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include "x25519.h"
static void genkey_usage(char *progname)
{
printf("usage: %s genkey <file>\n", progname);
}
int genkey_main(int argc, char **argv, char *progname)
{
int c;
void (*usage)(char *) = genkey_usage;
while ((c=getopt(argc, argv, "")) >= 0) switch (c) {
case '?':
usage(progname);
return 1;
}
if (argc-optind != 1) {
usage(progname);
return 1;
}
const char *name = argv[optind];
unsigned char key[32];
if (getentropy(key, sizeof key)) {
perror("getentropy");
return 1;
}
int fd = open(name, O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600);
if (fd < 0) {
fprintf(stderr, "error creating file: %s", name);
perror(0);
return 1;
}
FILE *f = fdopen(fd, "wb");
if (!f) {
close(fd);
perror("fdopen");
return 1;
}
if (fwrite(key, 1, sizeof key, f) != sizeof key || fflush(f)) {
perror("error writing key file");
return 1;
}
fclose(f);
return 0;
}
static void pubkey_usage(char *progname)
{
printf("usage: %s pubkey <secret_key_file> > <public_key_file>\n", progname);
}
int pubkey_main(int argc, char **argv, char *progname)
{
int c;
void (*usage)(char *) = pubkey_usage;
while ((c=getopt(argc, argv, "")) >= 0) switch (c) {
case '?':
usage(progname);
return 1;
}
if (argc-optind != 1) {
usage(progname);
return 1;
}
const char *name = argv[optind];
unsigned char key[32];
int fd = open(name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
fprintf(stderr, "error opening file: %s", name);
perror(0);
return 1;
}
FILE *f = fdopen(fd, "rb");
if (!f) {
close(fd);
perror("fdopen");
return 1;
}
if (fread(key, 1, sizeof key, f) != sizeof key || getc(f)!=EOF) {
fprintf(stderr, "invalid key file %s: musl be %zu bytes", name, sizeof key);
return 1;
}
fclose(f);
x25519_scalarmult(key, key, (unsigned char[32]){9});
if (fwrite(key, 1, sizeof key, stdout) != sizeof key || fflush(stdout)) {
perror("error writing output");
return 1;
}
return 0;
}
static void init_usage(char *progname)
{
printf("usage: %s init [-b <blocksize>] [-l <label>] <pubkeyfile> <rootdir>\n", progname);
}
int init_main(int argc, char **argv, char *progname)
{
int c, fd;
FILE *f;
unsigned char key[32];
void (*usage)(char *) = init_usage;
size_t bsize = 256<<10;
const char *label = "mybackup";
while ((c=getopt(argc, argv, "b:l:")) >= 0) switch (c) {
case 'b':
bsize = strtoull(optarg, 0, 0);
break;
case 'l':
label = optarg;
break;
case '?':
usage(progname);
return 1;
}
if (argc-optind != 2) {
usage(progname);
return 1;
}
if (bsize < 4000) {
fprintf(stderr, "block size %zu too small\n", bsize);
return 1;
} else if (bsize > 64<<20) {
fprintf(stderr, "block size %zu too large\n", bsize);
return 1;
}
const char *keyfile = argv[optind];
const char *rootdir = argv[optind+1];
fd = open(keyfile, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
fprintf(stderr, "cannot open key file %s: ", keyfile);
perror(0);
return 1;
}
f = fdopen(fd, "rb");
if (!f) {
close(fd);
perror("fdopen");
return 1;
}
if (fread(key, 1, sizeof key, f) != sizeof key || getc(f) != EOF) {
fprintf(stderr, "invalid key file %s: musl be %zu bytes", keyfile, sizeof key);
return 1;
}
fclose(f);
DIR *d = opendir(".");
if (!d) {
perror("opendir");
return 1;
}
for (;;) {
errno = 0;
struct dirent *de = readdir(d);
if (!de) {
if (!errno) break;
perror("error reading working directory");
return 1;
}
if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
fprintf(stderr, "working directory is not empty\n");
return 1;
}
}
closedir(d);
if (symlink(rootdir, "root")) {
perror("cannot make root symlink");
return 1;
}
if (mkdir("devices", 0700)) {
perror("cannot make devices directory");
return 1;
}
fd = open("pubkey", O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600);
if (fd < 0) {
perror("error creating key file");
return 1;
}
f = fdopen(fd, "wb");
if (!f) {
close(fd);
perror("fdopen");
return 1;
}
if (fwrite(key, 1, sizeof key, f) != sizeof(key) || fflush(f)) {
fclose(f);
perror("error writing key file");
return 1;
}
fclose(f);
fd = open("config", O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600);
if (fd < 0) {
perror("error creating config file");
return 1;
}
f = fdopen(fd, "wb");
if (!f) {
close(fd);
perror("fdopen");
return 1;
}
if (fprintf(f, "blocksize %zu\nlabel %s\n", bsize, label) < 0 || fflush(f)) {
perror("writing config file");
return 1;
}
fclose(f);
fd = open("store_cmd", O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0700);
if (fd < 0) {
perror("error creating store_cmd file");
return 1;
}
f = fdopen(fd, "wb");
if (!f) {
close(fd);
perror("fdopen");
return 1;
}
if (fprintf(f, "#!/bin/sh\necho 'please edit store_cmd script'\nexit 1\n") < 0 || fflush(f)) {
perror("writing store_cmd file");
return 1;
}
fclose(f);
return 0;
}
static void commit_usage(char *progname)
{
printf("usage: %s commit <indexdir>\n", progname);
}
int commit_main(int argc, char **argv, char *progname)
{
int c;
void (*usage)(char *) = commit_usage;
while ((c=getopt(argc, argv, "")) >= 0) switch (c) {
case '?':
usage(progname);
return 1;
}
if (argc-optind != 1) {
usage(progname);
return 1;
}
const char *indexdir = argv[optind];
int d = open(indexdir, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
if (d < 0) {
fprintf(stderr, "cannot open index directory %s: ", indexdir);
perror(0);
return 1;
}
if (renameat(d, "index.pending", d, "index")) {
close(d);
if (errno == ENOENT) {
printf("no uncommitted backup index\n");
return 0;
}
perror("rename");
return 1;
}
close(d);
printf("successfully committed\n");
return 0;
}
static void abort_usage(char *progname)
{
printf("usage: %s abort <indexdir>\n", progname);
}
int abort_main(int argc, char **argv, char *progname)
{
int c;
void (*usage)(char *) = abort_usage;
while ((c=getopt(argc, argv, "")) >= 0) switch (c) {
case '?':
usage(progname);
return 1;
}
if (argc-optind != 1) {
usage(progname);
return 1;
}
const char *indexdir = argv[optind];
int d = open(indexdir, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
if (d < 0) {
fprintf(stderr, "cannot open index directory %s: ", indexdir);
perror(0);
return 1;
}
if (unlinkat(d, "index.pending", 0)) {
close(d);
if (errno == ENOENT) {
printf("no uncommitted backup index\n");
return 0;
}
perror("unlink");
return 1;
}
close(d);
printf("uncommitted backup aborted\n");
return 0;
}