-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnaps.c
733 lines (610 loc) · 17.5 KB
/
snaps.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
#include "parseconfig.h"
#include "rotator.h"
#include "syncer.h"
#define VERSION "1.0.0-beta1"
#define CONFIGFILE "/etc/snaps.conf"
#define EMPTYDIR "/var/empty"
int verbose = 0;
int helpopt = 0;
int versopt = 0;
int forceopt = 0; /* Force a backup, no matter how long ago the last was
* taken.
*/
int cfgcheckonly = 0; /* Do a configuration check only. */
/*
* Use the time the program is started to determine the interval with a
* previously created backup.
*/
time_t starttime;
void print_usage(FILE *);
/*
* Communication with the other processes is as follows:
*
* main (trusted):
* if postexec
* fork postexec
* fork rotator
* fork syncer
* wait for rotator ready or done
* if rotator ready
* signal syncer to start
* wait for syncer exit
* if postexec
* signal postexec syncer exit status
* wait for postexec exit
* if postexec was succesful
* signal rotator to rollin
* else
* signal rotator to cleanup
* else if postexec not configured
* if syncer was succesful
* signal rotator to rollin
* else
* signal rotator to cleanup
* else if rotator done
* signal syncer to stop
* if postexec
* signal postexec to stop
* wait for rotator exit
*
* rotator (trusted):
* ensure root
* obtain a lock (might be locked by an older rotator)
* cleanup any left-behind sync dir
* create new sync dir
* signal parent ready
* wait for signal from parent, either rollin or cleanup
* then move or remove the sync dir, depending on signal
* exit
*
* syncer (untrusted, after rsync started):
* wait for start signal from parent
* close communication channel with parent
* fork/exec rsync
*/
int
main(int argc, char *argv[])
{
struct endpoint **epv;
int cmd, c, n, commfd[2], i, **rsyncexit, trusted, exists, updated;
char *cfgfile, *hostid, **filters, **cpp;
mode_t relax, mode;
extern int opterr;
if ((starttime = time(NULL)) == -1)
err(1, "could not determine current time");
cfgfile = NULL;
filters = NULL;
opterr = 0;
while ((c = getopt(argc, argv, "c:fhnqs:vV")) != -1)
switch(c) {
case 'c':
if ((cfgfile = strdup(optarg)) == NULL)
err(1, "strdup");
break;
case 'f':
forceopt = 1;
break;
case 'h':
helpopt = 1;
break;
case 'n':
cfgcheckonly = 1;
break;
case 'q':
verbose--;
break;
case 's':
if (strlen(optarg) == 0)
errx(1, "empty host filter specified");
filters = addstr(filters, optarg);
break;
case 'v':
verbose++;
break;
case 'V':
versopt = 1;
break;
case '?':
case ':':
print_usage(stderr);
exit(1);
}
argc -= optind;
argv += optind;
if (argc > 0) {
print_usage(stderr);
exit(1);
}
if (helpopt)
print_usage(stdout);
if (versopt)
fprintf(stdout, "%s\n", VERSION);
if (helpopt || versopt)
exit(0);
/* Ensure stdout is line buffered, even when run non-interactively. */
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
errx(1, "setvbuf");
if (cfgfile == NULL)
if ((cfgfile = strdup(CONFIGFILE)) == NULL)
err(1, "strdup");
/*
* Check owner and permissions of the config file. It should be owned by
* the superuser and wheel. It's ok if it's readable by wheel.
*/
if (trustedpath(cfgfile, S_IRGRP, 0, &trusted, &exists) == -1)
err(1, "%s: trustedpath", __func__);
if (trusted && !exists) {
errno = ENOENT;
err(1, "%s", cfgfile);
}
if (!trusted) {
errx(1, "%s untrusted.\nAcceptable ownership and permissions: "
"root:wheel rw-r-----.\n"
"Furthermore all path components leading up to the file"
" must be owned by the\nsuperuser and none should be "
"writable by the group or others.", cfgfile);
}
/* Open and parse config file. */
if ((n = open(cfgfile, O_RDONLY | O_CLOEXEC)) == -1)
err(1, "%s", cfgfile);
if ((epv = parseconfig(n)) == NULL)
errx(0, "no hosts to backup");
if (close(n) == -1)
err(1, "%s: close", __func__);
if (cfgcheckonly) {
if (verbose > -1)
fprintf(stdout, "%s OK\n", cfgfile);
free(cfgfile);
cfgfile = NULL;
exit(0);
}
free(cfgfile);
cfgfile = NULL;
if (close(STDIN_FILENO) == -1)
err(1, "%s: close", __func__);
/* expect stdout and stderr only */
if (isopenfd(STDOUT_FILENO) != 1)
errx(1, "expected stdout to be open");
if (isopenfd(STDERR_FILENO) != 1)
errx(1, "expected stderr to be open");
if (getdtablecount() != 2)
errx(1, "fd leak: %d", getdtablecount());
if (chdir("/") == -1)
err(1, "can't change directory to /");
if (geteuid() != 0)
errx(1, "must run as the superuser");
/* Defaut to confidentiality and integrity. */
umask(077);
/*
* If one or more host filters are used, filter out any hosts that don't
* match any filter.
*/
if (filters) {
for (n = 0; epv[n] != NULL; n++) {
hostid = getepid(epv[n]);
if (strlen(hostid) == 0)
errx(1, "could not determine host id");
/* Find first matching filter. */
for (cpp = filters; *cpp; cpp++)
if (strstr(hostid, *cpp))
break;
if (*cpp == NULL) {
/* host does not match any filter */
epv = snaps_rm_endpoint(epv, epv[n]);
n--;
}
}
clrstrv(&filters);
}
/*
* Make sure the root dir and endpoint path ownership and permissions
* are ok and create directories or fix permissions where possible.
* Remove endpoints where anything is unrecoverably wrong.
*
* All existing components must be owned by the superuser and none
* should be writable by the group or by others.
*
* If the endpoint is shared, the mode of the root dir will be set to
* 0750 and the mode of the endpoint path to 0751. If the endpoint is
* not shared the mode will be set to 0700 and 0711, respectively. Use
* "dir dropping" to let unprivileged processes access contents in the
* root dir.
*/
n = 0;
while (epv[n] != NULL) {
/*
* Make sure the following conditions hold for the root dir:
* - it's absolute
* - it's either 700 or 750 if shared
* - if the path does not exist yet, createroot must be set
*/
if (!isabsolutepath(epv[n]->root)) {
warnx("%s: root must be set to an absolute path: "
"\"%s\"", getepid(epv[n]), epv[n]->root);
epv = snaps_rm_endpoint(epv, epv[n]);
continue;
}
relax = 0;
if (epv[n]->shared != -1)
relax = S_IRGRP | S_IXGRP;
if (trustedpath(epv[n]->root, relax, epv[n]->shared, &trusted,
&exists) == -1)
err(1, "%s: trustedpath", __func__);
if (!trusted) {
warnx("%s: %s is untrusted.\nMake sure all components "
"are owned by the superuser and none are "
"writable by the\ngroup or others. The last "
"component must have mode %s.",
getepid(epv[n]),
epv[n]->root,
epv[n]->shared == -1 ? "0700" : "0750");
epv = snaps_rm_endpoint(epv, epv[n]);
continue;
}
if (!exists && !epv[n]->createroot) {
warnx("%s: make sure the root \"%s\" exists or set "
"createroot to \"yes\"",
getepid(epv[n]), epv[n]->root);
epv = snaps_rm_endpoint(epv, epv[n]);
continue;
}
mode = S_IRWXU;
if (epv[n]->shared != -1)
mode |= S_IRGRP | S_IXGRP;
if (secureensuredir(epv[n]->root, mode, epv[n]->shared,
&updated) == -1)
return -1;
if (updated)
warnx("%s: updated ownership and permissions of \"%s\"",
getepid(epv[n]), epv[n]->root);
/*
* Same thing for the endpoint path. Mode should be either 711
* or 751 depending on whether it's shared or not.
*/
relax = S_IXGRP | S_IXOTH;
if (epv[n]->shared != -1)
relax |= S_IRGRP;
if (trustedpath(epv[n]->path, relax, epv[n]->shared, &trusted,
&exists) == -1)
err(1, "%s: trustedpath", __func__);
if (!trusted) {
if (epv[n]->shared == -1) {
warnx("insecure mode: path must be "
"owned by wheel and must not be"
" readable or writable by the "
"group or others: %s",
epv[n]->path);
} else {
warnx("insecure mode: path must be "
"owned by group id %d and must "
"not be writable by the group "
"or readable or writable by "
"others: %s", epv[n]->shared,
epv[n]->path);
}
epv = snaps_rm_endpoint(epv, epv[n]);
continue;
}
mode = S_IRWXU | S_IXGRP | S_IXOTH;
if (epv[n]->shared != -1)
mode |= S_IRGRP;
if (secureensuredir(epv[n]->path, mode, epv[n]->shared,
&updated) == -1)
return -1;
if (updated)
warnx("%s: updated ownership and permissions of \"%s\"",
getepid(epv[n]), epv[n]->path);
n++;
}
/*
* Pre-fork rotators and syncers.
*/
for (n = 0; epv[n] != NULL; n++) {
/*
* Fork and start postexec if configured. Remove other endpoints
* from the new address space.
*/
if (epv[n]->postexec != NULL) {
/* setup a communication channel to postexec */
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
AF_UNSPEC, commfd) == -1)
err(1, "could not setup a communication channel");
if ((epv[n]->poxpid = fork()) == -1)
err(1, "could not fork postexec");
if (epv[n]->poxpid == 0) {
if (verbose > 1)
fprintf(stdout, "postexec[%d]: %s forked\n",
getpid(), getepid(epv[n]));
if (close(commfd[0]) == -1)
err(1, "closing peer side");
epv[n]->poxfd = commfd[1];
/*
* Open a file descriptor to the root dir to
* work with.
*/
snaps_endpoint_openrootfd(epv[n]);
/*
* Remove other endpoints.
*/
epv = snaps_keep_one_endpoint(epv, epv[n]);
setproctitle("postexec %s", getepid(epv[0]));
postexec(epv[0]);
errx(1, "unexpected return of postexec");
} else {
if (close(commfd[1]) == -1)
err(1, "closing peer side");
epv[n]->poxfd = commfd[0];
}
}
/*
* Fork and start a rotator. Remove other endpoints from the new
* address space.
*/
/* setup a communication channel to the rotator */
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, AF_UNSPEC,
commfd) == -1)
err(1, "could not setup a communication channel");
if ((epv[n]->rotpid = fork()) == -1)
err(1, "could not fork rotator");
if (epv[n]->rotpid == 0) {
if (verbose > 1)
fprintf(stdout, "rotator[%d]: %s forked\n",
getpid(), getepid(epv[n]));
/*
* Close the communication channel with postexec if
* there was any.
*/
if (epv[n]->postexec != NULL) {
if (close(epv[n]->poxfd) == -1)
err(1, "close communication channel to "
"postexec in the rotator");
epv[n]->poxfd = -1;
}
if (close(commfd[0]) == -1)
err(1, "closing peer side");
epv[n]->rotfd = commfd[1];
/*
* Open a file descriptor to the root dir to
* work with.
*/
snaps_endpoint_openrootfd(epv[n]);
/*
* Remove other endpoints.
*/
epv = snaps_keep_one_endpoint(epv, epv[n]);
setproctitle("rotator %s", getepid(epv[0]));
rotator(epv[0], starttime, forceopt);
errx(1, "unexpected return of rotator");
} else {
if (close(commfd[1]) == -1)
err(1, "closing peer side");
epv[n]->rotfd = commfd[0];
}
/*
* Fork and start a syncer process. Remove other endpoints from
* the new address space.
*/
/* setup a communication channel to the syncer process */
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, AF_UNSPEC,
commfd) == -1)
err(1, "could not setup a communication channel");
if ((epv[n]->synpid = fork()) == -1)
err(1, "could not fork syncer");
if (epv[n]->synpid == 0) {
if (verbose > 1)
fprintf(stdout, "syncer[%d]: %s forked\n",
getpid(), getepid(epv[n]));
/*
* Close the communication channel with postexec if
* there was any.
*/
if (epv[n]->postexec != NULL) {
if (close(epv[n]->poxfd) == -1)
err(1, "close communication channel to "
"postexec in the syncer");
epv[n]->poxfd = -1;
}
/*
* Open a file descriptor to the root dir to
* work with.
*/
snaps_endpoint_openrootfd(epv[n]);
/*
* Close the communication channel with the rotator.
*/
if (close(epv[n]->rotfd) == -1)
err(1, "close communication channel to the "
"rotator in the syncer");
epv[n]->rotfd = -1;
/*
* Setup communication channel.
*/
if (close(commfd[0]) == -1)
err(1, "closing peer side");
epv[n]->synfd = commfd[1];
/*
* Remove other endpoints.
*/
epv = snaps_keep_one_endpoint(epv, epv[n]);
setproctitle("syncer %s", getepid(epv[0]));
syncer(epv[0]);
errx(1, "unexpected return of syncer");
} else {
if (close(commfd[1]) == -1)
err(1, "closing peer side");
epv[n]->synfd = commfd[0];
}
}
/*
* Chroot, pledge and wait for the first rotator to be ready, signal the
* accompanying syncer to start, repeat for each backup host, one after
* the other.
*/
if (chroot(EMPTYDIR) == -1 || chdir("/") == -1)
err(1, "%s: chroot %s", __func__, EMPTYDIR);
if (pledge("stdio", NULL) == -1)
err(1, "%s: pledge", __func__);
for (n = 0; epv[n] != NULL; n++) {
/*
* Signal the rotator to start and wait for it to signal it's
* ready or done.
*/
if (writecmd(epv[n]->rotfd, CMDSTART) == -1)
err(1, "%s: write rotator start signal",
getepid(epv[n]));
if (readcmd(epv[n]->rotfd, &cmd) == -1)
err(1, "%s: rotator read error", getepid(epv[n]));
if (cmd != CMDCLOSED && cmd != CMDREADY)
err(1, "%s: unexpected signal from rotator %d",
getepid(epv[n]), cmd);
/*
* Signal the syncer to either start or stop, same with postexec
* if it is running.
*/
if (cmd == CMDREADY) {
if (writecmd(epv[n]->synfd, CMDSTART) == -1)
err(1, "%s: write syncer start signal",
getepid(epv[n]));
if (close(epv[n]->synfd) == -1)
err(1, "%s: closing communication channel to syncer",
getepid(epv[n]));
epv[n]->synfd = -1;
if ((i = reapproc(epv[n]->synpid)) == -1)
errx(1, "%s: reap syncer error", getepid(epv[n]));
if (verbose > 1)
fprintf(stdout, "%s: syncer[%d] exit %d\n",
getepid(epv[n]), epv[n]->synpid, i);
/*
* Signal postexec the syncer exit status or -1 if an error
* occurred, close the communication channel and wait until it's
* done.
*/
if (epv[n]->postexec != NULL) {
if (writecmd(epv[n]->poxfd, CMDCUST) == -1)
err(1, "%s: write postexec custom signal",
getepid(epv[n]));
if (writecmd(epv[n]->poxfd, i) == -1)
err(1, "%s: write postexec syncer exit status",
getepid(epv[n]));
if (close(epv[n]->poxfd) == -1)
err(1, "%s: closing communication channel to postexec",
getepid(epv[n]));
if ((i = reapproc(epv[n]->poxpid)) == -1)
errx(1, "%s: reap postexec error", getepid(epv[n]));
if (verbose > 1)
fprintf(stdout, "%s: postexec[%d] exit %d\n",
getepid(epv[n]), epv[n]->poxpid, i);
epv[n]->poxpid = -1;
}
/*
* Depending on the termination status of the syncer or
* postexec if configured, signal the rotator to either
* cleanup, or rollin the new snapshot.
*/
if (epv[n]->postexec == NULL) {
/*
* i contains the rsync exit status, see if it
* indicates success.
*/
if (i != 0 && epv[n]->rsyncexit) {
for (rsyncexit = epv[n]->rsyncexit;
*rsyncexit; rsyncexit++) {
if (i == **rsyncexit) {
i = 0;
break;
}
}
}
if (i != 0)
warnx("%s: syncer[%d] exit %d",
getepid(epv[n]), epv[n]->synpid, i);
}
epv[n]->synpid = -1;
if (i == 0) {
if (writecmd(epv[n]->rotfd, CMDROTINCLUDE) == -1)
err(1, "%s: write rotator signal",
getepid(epv[n]));
} else {
if (writecmd(epv[n]->rotfd, CMDROTCLEANUP) == -1)
err(1, "%s: write rotator signal",
getepid(epv[n]));
}
} else {
/*
* Signal the syncer and optionally postexec to stop.
* Close communication channels after sending the signal
* and reap.
*/
/* First the syncer. */
if (writecmd(epv[n]->synfd, CMDSTOP) == -1)
err(1, "%s: write syncer stop signal",
getepid(epv[n]));
if (close(epv[n]->synfd) == -1)
err(1, "%s: closing communication channel to syncer",
getepid(epv[n]));
epv[n]->synfd = -1;
if ((i = reapproc(epv[n]->synpid)) == -1)
errx(1, "%s: reap syncer error", getepid(epv[n]));
if (verbose > 1)
fprintf(stdout, "%s: syncer[%d] exit %d\n",
getepid(epv[n]), epv[n]->synpid, i);
epv[n]->synpid = -1;
/* Then postexec, if configured. */
if (epv[n]->postexec != NULL) {
if (writecmd(epv[n]->poxfd, CMDSTOP) == -1)
err(1, "%s: write postexec stop signal",
getepid(epv[n]));
if (close(epv[n]->poxfd) == -1)
err(1, "%s: closing communication channel to postexec",
getepid(epv[n]));
if ((i = reapproc(epv[n]->poxpid)) == -1)
errx(1, "%s: reap postexec error", getepid(epv[n]));
if (verbose > 1)
fprintf(stdout, "%s: postexec[%d] exit %d\n",
getepid(epv[n]), epv[n]->poxpid, i);
epv[n]->poxpid = -1;
}
}
/*
* At last the rotator. It signalled us it was done or we
* signalled it to finish up, so no need to send the stop
* signal. It should already have exited. Close the
* communication-channel with the rotator and reap.
*/
if (close(epv[n]->rotfd) == -1)
warn("%s: closing communication channel to rotator",
getepid(epv[n]));
epv[n]->rotfd = -1;
if ((i = reapproc(epv[n]->rotpid)) == -1)
errx(1, "%s: rotator[%d] reap rotator error",
getepid(epv[n]), epv[n]->rotpid);
if (verbose > 1)
fprintf(stdout, "%s: rotator[%d] exit %d\n",
getepid(epv[n]), epv[n]->rotpid, i);
if (i != 0)
warnx("%s: rotator[%d] exit %d",
getepid(epv[n]), epv[n]->rotpid, i);
epv[n]->rotpid = -1;
}
return 0;
}
void
print_usage(FILE *fp)
{
fprintf(fp, "usage: %s [-fhnqvV] [-c configfile] [-s filter]\n", getprogname());
}