-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsnebu-submitfiles.c
1663 lines (1542 loc) · 50.7 KB
/
snebu-submitfiles.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2009 - 2021 Derek Pressnall
*
* This file is part of Snebu, the Simple Network Encrypting Backup Utility
*
* Snebu is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 3
* as published by the Free Software Foundation.
*
* Snebu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Snebu. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <utime.h>
#include <time.h>
#include <getopt.h>
#include <sqlite3.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include "tarlib.h"
int submitfiles2(int out);
char *stresc(char *src, char **target);
char *strescb(char *src, char **target, int len);
char *strunesc(char *src, char **target);
void stresc_free(char **target);
struct ringbuf *rbinit(size_t s);
size_t rbwrite(void *buf, size_t b, size_t c, struct ringbuf *r);
size_t rbread(void *buf, size_t b, size_t c, struct ringbuf *r);
size_t rbrewind(struct ringbuf *r, size_t c);
size_t rbsize(struct ringbuf *r);
size_t rbused(struct ringbuf *r);
size_t rbavail(struct ringbuf *r);
void rbfree(struct ringbuf *r);
int pipebuf(int *in, int *out);
void usage();
int checkperm(sqlite3 *bkcatalog, char *action, char *backupname);
extern sqlite3 *bkcatalog;
extern struct {
char *vault;
char *meta;
int hash;
} config;
extern char *SHN;
char *EncodeBlock2(char *out, char *in, int m, int *n);
char *DecodeBlock2(char *out, char *in, int m, int *n);
double ftime();
int flush_received_files(sqlite3 *bkcatalog, int verbose, int bkid,
unsigned long long est_size, unsigned long long *bytes_read);
int submitfiles_tmptables(sqlite3 *bkcatalog, int bkid);
sqlite3 *opendb();
long int strtoln(char *nptr, char **endptr, int base, int len);
void update_status(unsigned long long total_bytes_received, unsigned long long est_size, char *cur_filename, time_t cur_time, time_t start_time, char indicator);
int logaction(sqlite3 *bkcatalog, int backupset_id, int action, char *message);
struct {
unsigned long long unit;
char *label;
} display_units [] = {
{ 1, "B" },
{ 1000, "KB" },
{ 1000000, "MB" },
{ 1000000000, "GB" },
{ 1000000000000, "TB" },
};
int submitfiles(int argc, char **argv)
{
int in = -1;
int out = -1;
char *inbuf = NULL;
size_t len = 0;
int optc;
char bkname[128];
char datestamp[128];
int foundopts = 0;
int verbose = 0;
FILE *metadata;
char **mdfields = NULL;
char *sqlstmt = NULL;
sqlite3_stmt *sqlres;
char *sqlerr = NULL;
sqlite3_stmt *inbfrec;
long cipherid = 0;
char *xattru = NULL;
int xattrn;
char *paxdata;
int paxdatalen;
int cipher_record;
char *filenameu = NULL;
char *linknameu = NULL;
unsigned long long linkedfiles_bytes = 0;
char *eprvkeyu = NULL;
char *pubkeyu = NULL;
char *commentu = NULL;
struct option longopts[] = {
{ "name", required_argument, NULL, 'n' },
{ "datestamp", required_argument, NULL, 'd' },
{ "verbose", no_argument, NULL, 'v' },
{ NULL, no_argument, NULL, 0 }
};
struct cryptinfo_s {
int keynum;
char *hmac;
};
char *keygroups = NULL;
char **keygroupsp = NULL;
int longoptidx;
int numkeys = 0;
struct cryptinfo_s *cryptinfo = NULL;
int cryptinfo_n = 0;
char xattr_varstring[1024];
int bkid;
unsigned long long est_size = 0;
int est_files = 0;
int tot_files = 0;
while ((optc = getopt_long(argc, argv, "n:d:v", longopts, &longoptidx)) >= 0)
switch (optc) {
case 'n':
strncpy(bkname, optarg, 127);
bkname[127] = 0;
foundopts |= 1;
break;
case 'd':
strncpy(datestamp, optarg, 127);
datestamp[127] = 0;
foundopts |= 2;
break;
case 'v':
verbose += 1;
foundopts |= 4;
break;
default:
usage();
return(1);
}
if ((foundopts & 3) != 3) {
fprintf(stderr, "Didn't find all arguments %d\n", foundopts);
usage();
return(1);
}
/* needed to populate config that slubmitfiles2 needs in separate process */
opendb(bkcatalog);
sqlite3_close(bkcatalog);
pipebuf(&in, &out);
submitfiles2(in);
opendb(bkcatalog);
if (checkperm(bkcatalog, "backup", bkname)) {
sqlite3_close(bkcatalog);
return(1);
}
sqlite3_prepare_v2(bkcatalog,
(sqlstmt = sqlite3_mprintf("select backupset_id from backupsets "
"where name = '%q' and serial = '%q'",
bkname, datestamp)), -1, &sqlres, 0);
if (sqlite3_step(sqlres) == SQLITE_ROW) {
bkid = sqlite3_column_int(sqlres, 0);
}
else {
fprintf(stderr, "bkid not found 1: %s\n", sqlstmt);
return(1);
}
sqlite3_free(sqlstmt);
sqlite3_finalize(sqlres);
sqlite3_prepare_v2(bkcatalog,
(sqlstmt = sqlite3_mprintf("select sum(size) "
"from needed_file_entities where backupset_id = %d",
bkid)), -1, &sqlres, 0);
sqlite3_free(sqlstmt);
if (sqlite3_step(sqlres) == SQLITE_ROW) {
est_size = sqlite3_column_int64(sqlres, 0);
}
else
est_size = 0;
sqlite3_finalize(sqlres);
sqlite3_prepare_v2(bkcatalog,
(sqlstmt = sqlite3_mprintf("select count(*) "
"from needed_file_entities where backupset_id = %d",
bkid)), -1, &sqlres, 0);
sqlite3_free(sqlstmt);
if (sqlite3_step(sqlres) == SQLITE_ROW) {
est_files = sqlite3_column_int64(sqlres, 0);
}
sqlite3_finalize(sqlres);
int b_total_unit = 0;
for (int i = 0; i < sizeof(display_units) / sizeof(*display_units); i++)
if (est_size >= display_units[i].unit)
b_total_unit = i;
submitfiles_tmptables(bkcatalog, bkid);
metadata = fdopen(out, "r");
sqlstmt = sqlite3_mprintf(
"insert or replace into received_file_entities_t "
"(backupset_id, ftype, permission, user_name, user_id, "
"group_name, group_id, size, hash, datestamp, filename, extdata, xheader) "
"values (@bkid, @ftype, @mode, @auid, @nuid, @agid, "
"@ngid, @filesize, @hash, @modtime, @filename, @linktarget, @xheader)");
sqlite3_prepare_v2(bkcatalog, sqlstmt, -1, &inbfrec, 0);
sqlite3_free(sqlstmt);
sqlite3_exec(bkcatalog,
"create temporary table if not exists temp_cipher_detail "
"as select * from cipher_detail where 0", 0, 0, &sqlerr);
if (sqlerr != 0) {
fprintf(stderr, "%s %s\n", sqlerr, sqlstmt);
sqlite3_free(sqlerr);
}
sqlite3_exec(bkcatalog,
"create temporary table if not exists temp_key_map ( "
"keyposition integer, "
"id integer, "
"constraint temp_key_map_c1 unique ( "
"keyposition, id))", 0, 0, &sqlerr);
if (sqlerr != 0) {
fprintf(stderr, "%s\n", sqlerr);
sqlite3_free(sqlerr);
}
int inlen = 0;
unsigned long long total_bytes_received = 0;
double start_time = ftime();
double lastupdate_time = 0;
double lastflush_time = start_time;
double curtime = ftime();
if (verbose >= 1)
fprintf(stderr, "Transfering files\n");
if (verbose >= 1)
fprintf(stderr, "Estimated backup size: %6.2f %s\n\n",
(double) est_size / display_units[b_total_unit].unit,
display_units[b_total_unit].label);
logaction(bkcatalog, bkid, 4, "Begin receiving files");
while ((inlen = getline(&inbuf, &len, metadata)) > 0) {
if (inbuf[inlen - 1] == '\n')
inbuf[inlen - 1] = '\0';
else {
fprintf(stderr, "Metadata recording error\n");
exit(1);
}
parse(inbuf, &mdfields, '\t');
// Record encryption key data from global header
if (strcmp(mdfields[0], "0") == 0) {
if (atoi(mdfields[1]) + 1 > numkeys)
numkeys = atoi(mdfields[1]) + 1;
strunesc(mdfields[3], &eprvkeyu);
strunesc(mdfields[4], &pubkeyu);
strunesc(mdfields[6], &commentu);
sqlite3_exec(bkcatalog, (sqlstmt = sqlite3_mprintf(
"insert or ignore into cipher_master (pkfp, eprivkey, pubkey, hmackeyhash, comment) "
"values ('%q', '%q', '%q', '%q', '%q')", mdfields[2], eprvkeyu, pubkeyu,
mdfields[5], commentu)), 0, 0, &sqlerr);
if (sqlerr != 0) {
fprintf(stderr, "%s %s\n", sqlerr, sqlstmt);
sqlite3_free(sqlerr);
}
sqlite3_free(sqlstmt);
// get the primary key (cipherid) of recorded key data
if (sqlite3_prepare_v2(bkcatalog,
sqlstmt = sqlite3_mprintf("select cipherid from cipher_master "
"where pkfp = '%q' and eprivkey = '%q' and pubkey = '%q' "
"and hmackeyhash = '%q' and comment = '%q'", mdfields[2], eprvkeyu, pubkeyu,
mdfields[5], commentu), -1, &sqlres, 0) == SQLITE_OK) {
sqlite3_free(sqlstmt);
if (sqlite3_step(sqlres) == SQLITE_ROW) {
cipherid = sqlite3_column_int(sqlres, 0);
}
else {
fprintf(stderr, "Cipher key recording failure\n");
exit(1);
}
sqlite3_finalize(sqlres);
}
// temporary map of inbound key number and recorded key
sqlite3_exec(bkcatalog, (sqlstmt = sqlite3_mprintf(
"insert or replace into temp_key_map (keyposition, id)"
"values (%d, %d)", atoi(mdfields[1]), cipherid)), 0, 0, 0);
sqlite3_free(sqlstmt);
if (numkeys > cryptinfo_n) {
if (cryptinfo == NULL)
cryptinfo = malloc(sizeof(struct cryptinfo_s) * numkeys);
else
cryptinfo = realloc(cryptinfo, sizeof(struct cryptinfo_s) * numkeys);
for (int i = cryptinfo_n; i < numkeys; i++) {
cryptinfo[i].hmac = NULL;
}
cryptinfo_n = numkeys;
}
}
else if (strcmp(mdfields[0], "1") == 0) {
if (xattru == NULL)
xattru = dmalloc((int) (strlen(mdfields[13]) * 3 / 4));
else
if (dmalloc_size(xattru) < ((int) (strlen(mdfields[13]) * 3 / 4)))
xattru = drealloc(xattru, (int) (strlen(mdfields[13]) * 3 / 4));
DecodeBlock2(xattru, mdfields[13], strlen(mdfields[13]), &xattrn);
cipher_record = 0;
if (getpaxvar(xattru, xattrn, "TC.cipher", &paxdata, &paxdatalen) == 0) {
cipher_record = 1;
cpypaxvarstr(xattru, xattrn, "TC.keygroup", &keygroups);
parse(keygroups, &keygroupsp, '|');
for (int i = 0; keygroupsp[i] != NULL; i++) {
int n = atoi(keygroupsp[i]);
if (n < numkeys) {
cryptinfo[n].keynum = n;
if (numkeys > 1)
sprintf(xattr_varstring, "TC.hmac.%d", n);
else
sprintf(xattr_varstring, "TC.hmac");
cpypaxvarstr(xattru, xattrn, xattr_varstring, &(cryptinfo[n].hmac));
delpaxvar(&xattru, &xattrn, xattr_varstring);
}
}
}
delpaxvar(&xattru, &xattrn, "atime");
delpaxvar(&xattru, &xattrn, "TC.segmented.header");
sqlite3_bind_int(inbfrec, 1, bkid);
sqlite3_bind_text(inbfrec, 2, mdfields[1], -1, SQLITE_STATIC);
sqlite3_bind_text(inbfrec, 3, mdfields[2], -1, SQLITE_STATIC);
sqlite3_bind_text(inbfrec, 4, mdfields[3], -1, SQLITE_STATIC);
sqlite3_bind_int(inbfrec, 5, atoi(mdfields[4]));
sqlite3_bind_text(inbfrec, 6, mdfields[5], -1, SQLITE_STATIC);
sqlite3_bind_int(inbfrec, 7, atoi(mdfields[6]));
sqlite3_bind_int64(inbfrec, 8, atoll(mdfields[7]));
sqlite3_bind_text(inbfrec, 9, mdfields[8], -1, SQLITE_STATIC);
sqlite3_bind_int(inbfrec, 10, atoi(mdfields[9]));
strunesc(mdfields[10], &filenameu);
sqlite3_bind_text(inbfrec, 11, filenameu, -1, SQLITE_STATIC);
strunesc(mdfields[11], &linknameu);
sqlite3_bind_text(inbfrec, 12, linknameu, -1, SQLITE_STATIC);
sqlite3_bind_blob(inbfrec, 13, xattru, xattrn, SQLITE_STATIC);
if (! sqlite3_step(inbfrec)) {
fprintf(stderr, "Error inserting metadata record into temporary table\n"); ;
exit(1);
}
sqlite3_int64 fileid = sqlite3_last_insert_rowid(bkcatalog);
total_bytes_received += atoll(mdfields[7]);
tot_files++;
sqlite3_exec(bkcatalog, (sqlstmt = sqlite3_mprintf(
"insert or ignore into diskfiles_t (hash) "
"values ('%q')", mdfields[8])), 0, 0, &sqlerr);
if (sqlerr != 0) {
fprintf(stderr, "%s\n", sqlerr);
sqlite3_free(sqlerr);
}
sqlite3_free(sqlstmt);
if (cipher_record == 1) {
for (int i = 0; keygroupsp[i] != NULL; i++) {
int n = atoi(keygroupsp[i]);
sqlite3_exec(bkcatalog, (sqlstmt = sqlite3_mprintf(
"insert or ignore into temp_cipher_detail (file_id, keynum, hmac) "
"values (%d, %d, '%q')", fileid, cryptinfo[n].keynum,
cryptinfo[n].hmac)), 0, 0, &sqlerr);
if (sqlerr != 0) {
fprintf(stderr, "%s %s\n", sqlerr, sqlstmt);
sqlite3_free(sqlerr);
}
sqlite3_free(sqlstmt);
}
}
sqlite3_reset(inbfrec);
// Update status line
if ((curtime = ftime()) > lastupdate_time + 1 || lastupdate_time == 0) {
lastupdate_time = curtime;
if ((curtime = ftime()) > lastflush_time + 5) {
if (verbose >= 1)
update_status(total_bytes_received, est_size, mdfields[10], curtime, start_time, '*');
flush_received_files(bkcatalog, verbose, bkid, est_size, &linkedfiles_bytes);
total_bytes_received += linkedfiles_bytes;
lastflush_time = curtime;
}
if (verbose >= 1)
update_status(total_bytes_received, est_size, mdfields[10], curtime, start_time, ' ');
}
}
else if (strcmp(mdfields[0], "2") == 0) {
if ((curtime = ftime()) > lastupdate_time + 1 || lastupdate_time == 0) {
lastupdate_time = curtime;
if (verbose >= 1)
update_status(total_bytes_received + atoll(mdfields[2]), est_size, mdfields[1], curtime, start_time, '+');
}
}
else if (strcmp(mdfields[0], "2") == 0) {
flush_received_files(bkcatalog, verbose, bkid, est_size, &linkedfiles_bytes);
lastflush_time = curtime;
}
inbuf[0] = '\0';
}
free(inbuf);
dfree(keygroups);
dfree(keygroupsp);
dfree(mdfields);
dfree(eprvkeyu);
dfree(pubkeyu);
dfree(commentu);
for (int i = 0; i < numkeys; i++) {
dfree(cryptinfo[i].hmac);
}
free(cryptinfo);
dfree(xattru);
dfree(filenameu);
dfree(linknameu);
sqlite3_finalize(inbfrec);
if (verbose >= 1)
update_status(total_bytes_received, est_size, "Completed", curtime, start_time, '*');
flush_received_files(bkcatalog, verbose, bkid, est_size, &linkedfiles_bytes);
total_bytes_received += linkedfiles_bytes;
if (verbose >= 1) {
update_status(total_bytes_received, est_size, "Completed", curtime, start_time, ' ');
fprintf(stderr, "\n");
}
if (verbose >= 1)
fprintf(stderr, "%6.2f %s in %d files requested,\n",
(double) est_size / display_units[b_total_unit].unit,
display_units[b_total_unit].label, est_files);
int b_received_unit = 0;
for (int i = 0; i < sizeof(display_units) / sizeof(*display_units); i++)
if (total_bytes_received >= display_units[i].unit)
b_received_unit = i;
if (verbose >= 1)
fprintf(stderr, "%6.2f %s in %d files received.\n",
(double) total_bytes_received / display_units[b_received_unit].unit,
display_units[b_received_unit].label, tot_files);
fclose(metadata);
logaction(bkcatalog, bkid, 7, "End receiving files");
sqlite3_close(bkcatalog);
return(0);
}
int submitfiles2(int out_h)
{
struct filespec fs;
char *tmpfiledir = config.vault;
char *destdir = config.vault;
char destdir2[3];
char tmpfilepath[1024];
char targetdir[1024];
char targetpath[1024];
struct stat tmpfstat;
size_t sizeremaining;
char *paxdata;
int paxdatalen;
int curtmpfile;
FILE *curfile;
struct tarsplit_file *tsf;
size_t c;
size_t bufsize = 256 * 1024;
char databuf[bufsize];
int padding;
char *escfname = NULL;
char *esclname = NULL;
char *escxheader = NULL;
struct lzop_file *lzf;
struct sha_file *s1f;
unsigned char cfsha1[SHA_DIGEST_LENGTH];
unsigned char cfsha1x[SHA_DIGEST_LENGTH * 2 + 1];
unsigned char cfsha2[SHA256_DIGEST_LENGTH];
unsigned char cfsha2x[SHA256_DIGEST_LENGTH * 2 + 1];
unsigned char *cfshax = NULL;
int use_hmac = 0;
unsigned char hmac[EVP_MAX_MD_SIZE * 2 + 1];
int wrote_file = 0;
pid_t child;
int numkeys = 0;
char paxhdr_varstring[256];
if ((child = fork()) == 0) {
FILE *out = fdopen(out_h, "w");
char *sparsetext = NULL;
char *ciphertype = NULL;
fsinit(&fs);
while (tar_get_next_hdr(&fs)) {
use_hmac = 0;
wrote_file = 0;
if (fs.ftype == 'g') {
char *paxdatacpy = NULL;
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.version", &paxdata, &paxdatalen) == 0) {
struct key_st *keys;
char *esceprvkey = NULL;
char *escpubkey = NULL;
char *esccomment= NULL;
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.numkeys", &paxdata, &paxdatalen) == 0) {
strncpya0(&paxdatacpy, paxdata, paxdatalen);
numkeys = atoi(paxdatacpy);
}
else
numkeys = 1;
keys = malloc(sizeof(struct key_st) * numkeys);
for (int i = 0; i < numkeys; i++) {
keys[i].fingerprint = NULL;
keys[i].eprvkey = NULL;
keys[i].pubkey = NULL;
keys[i].hmac_hash_b64 = NULL;
keys[i].comment = NULL;
}
for (int i = 0; i < numkeys; i++) {
if (numkeys > 1) {
sprintf(paxhdr_varstring, "TC.pubkey.fingerprint.%d", i);
cpypaxvarstr(fs.xheader, fs.xheaderlen, paxhdr_varstring, &(keys[i].fingerprint));
sprintf(paxhdr_varstring, "TC.eprivkey.%d", i);
cpypaxvarstr(fs.xheader, fs.xheaderlen, paxhdr_varstring, &(keys[i].eprvkey));
sprintf(paxhdr_varstring, "TC.pubkey.%d", i);
cpypaxvarstr(fs.xheader, fs.xheaderlen, paxhdr_varstring, &(keys[i].pubkey));
sprintf(paxhdr_varstring, "TC.hmackeyhash.%d", i);
cpypaxvarstr(fs.xheader, fs.xheaderlen, paxhdr_varstring, &(keys[i].hmac_hash_b64));
sprintf(paxhdr_varstring, "TC.keyfile.comment.%d", i);
cpypaxvarstr(fs.xheader, fs.xheaderlen, paxhdr_varstring, &(keys[i].comment));
}
else {
cpypaxvarstr(fs.xheader, fs.xheaderlen, "TC.pubkey.fingerprint", &(keys[0].fingerprint));
cpypaxvarstr(fs.xheader, fs.xheaderlen, "TC.eprivkey", &(keys[0].eprvkey));
cpypaxvarstr(fs.xheader, fs.xheaderlen, "TC.pubkey",&(keys[0].pubkey));
cpypaxvarstr(fs.xheader, fs.xheaderlen, "TC.hmackeyhash", &(keys[0].hmac_hash_b64));
cpypaxvarstr(fs.xheader, fs.xheaderlen, "TC.keyfile.comment", &(keys[0].comment));
}
}
fprintf(out, "3\n");
for (int i = 0; i < numkeys; i++) {
fprintf(out, "0\t%d\t%s\t%s\t%s\t%s\t%s\t\n", i,
keys[i].fingerprint, stresc(keys[i].eprvkey, &esceprvkey), stresc(keys[i].pubkey, &escpubkey),
keys[i].hmac_hash_b64, stresc(keys[i].comment, &esccomment));
}
if (numkeys > 0) {
for (int i = 0; i < numkeys; i++) {
dfree(keys[i].comment);
dfree(keys[i].fingerprint);
dfree(keys[i].hmac_hash_b64);
dfree(keys[i].eprvkey);
dfree(keys[i].pubkey);
}
free(keys);
}
dfree(esceprvkey);
dfree(escpubkey);
dfree(esccomment);
}
dfree(paxdatacpy);
}
else if (fs.ftype == '5' && getpaxvar(fs.xheader, fs.xheaderlen, "TC.segmented.header",
&paxdata, &paxdatalen) == 0) {
sprintf(tmpfilepath, "%s/tbXXXXXX", tmpfiledir);
curtmpfile = mkstemp(tmpfilepath);
curfile = fdopen(curtmpfile, "w");
tsf = tarsplit_init_r(fread, stdin, numkeys);
if (ciphertype != NULL)
ciphertype[0] = '\0';
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.compression", &paxdata, &paxdatalen) == 0) {
strncata0(&ciphertype, paxdata, paxdatalen - 1);
}
strncata0(&ciphertype, "|", 1);
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.cipher", &paxdata, &paxdatalen) == 0) {
strncata0(&ciphertype, paxdata, paxdatalen - 1);
}
fprintf(curfile, "%s\n", ciphertype);
double curtime = ftime();
double lastupdate_time = curtime;
size_t tot_size = 0;
while ((c = tarsplit_read(databuf, 1, bufsize, tsf)) > 0) {
fwrite(databuf, 1, c, curfile);
tot_size += c;
if ((curtime = ftime()) > lastupdate_time + 1) {
fprintf(out, "2\t%s\t%lu\n", stresc(fs.filename, &escfname), tot_size);
}
}
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.cipher", &paxdata, &paxdatalen) == 0) {
for (int i = 0; i < numkeys; i++) {
if ((tsf->hmac[i])[0] != '\0') {
strncata0(&ciphertype, (char *) (tsf->hmac[i]), EVP_MAX_MD_SIZE * 2);
use_hmac = 1;
}
}
if (use_hmac == 1) {
unsigned char *tmphash;
strcpy((char *) hmac, (char *) (tmphash = sha256_hex(ciphertype)));
free(tmphash);
}
#if 0
if ((tsf->hmac[0])[0] != '\0') {
strncpy((char *) hmac, (char *) (tsf->hmac[0]), EVP_MAX_MD_SIZE * 2);
hmac[EVP_MAX_MD_SIZE * 2] = '\0';
if (hmac[strlen((char *) hmac) - 1] == '\n')
hmac[strlen((char *) hmac) - 1] = '\0';
use_hmac = 1;
}
#endif
if (numkeys > 1) {
for (int i = 0; i < numkeys; i++) {
sprintf(paxhdr_varstring, "TC.hmac.%d", i);
setpaxvar(&(fs.xheader), &(fs.xheaderlen), paxhdr_varstring, (char *) tsf->hmac[i], strlen((char *) tsf->hmac[i]));
}
}
else {
setpaxvar(&(fs.xheader), &(fs.xheaderlen), "TC.hmac", (char *) tsf->hmac[0], strlen((char *) tsf->hmac[0]));
}
}
if (escxheader == NULL)
escxheader = dmalloc(((int)((fs.xheaderlen + 2) / 3)) * 4 + 1);
else
if (dmalloc_size(escxheader) < ((int)((fs.xheaderlen + 2) / 3)) * 4 + 1)
escxheader = drealloc(escxheader, ((int)((fs.xheaderlen + 2) / 3)) * 4 + 1);
unsigned long long int filesize = fs.filesize;
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.original.size", &paxdata, &paxdatalen) == 0) {
filesize = strtoull(paxdata, 0, 10);
}
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.sparse.original.size", &paxdata, &paxdatalen) == 0) {
filesize = strtoull(paxdata, 0, 10);
}
fprintf(out, "1\t%c\t%4.4o\t%s\t%d\t%s\t%d\t%lld\t%s\t%lu\t%s\t%s\t%d\t%s",
'E', fs.mode, fs.auid, fs.nuid, fs.agid, fs.ngid, filesize,
use_hmac == 0 ? cfsha1x : hmac, fs.modtime, stresc(fs.filename, &escfname),
stresc(fs.linktarget == 0 ? "" : fs.linktarget, &esclname), fs.xheaderlen,
fs.xheaderlen == 0 ? "" : EncodeBlock2(escxheader, fs.xheader, fs.xheaderlen, NULL)
);
tarsplit_finalize_r(tsf);
if (fclose(curfile) == 0)
wrote_file = 1;
else {
fprintf(stderr, "Error writing file, aborting\n");
exit(1);
}
}
else if (fs.filesize > 0 || fs.n_sparsedata > 0) {
int is_ciphered = 0;
size_t (*c_fwrite)();
void *c_handle;
unsigned long long int filesize = fs.filesize;
sprintf(tmpfilepath, "%s/tbXXXXXX", tmpfiledir);
curtmpfile = mkstemp(tmpfilepath);
curfile = fdopen(curtmpfile, "w");
if (ciphertype != NULL)
ciphertype[0] = '\0';
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.compression", &paxdata, &paxdatalen) == 0) {
strncata0(&ciphertype, paxdata, paxdatalen - 1);
}
strncata0(&ciphertype, "|", 1);
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.cipher", &paxdata, &paxdatalen) == 0) {
strncata0(&ciphertype, paxdata, paxdatalen - 1);
is_ciphered = 1;
}
if (is_ciphered == 1)
fprintf(curfile, "%s\n", ciphertype);
sizeremaining = fs.filesize;
padding = 512 - ((fs.filesize - 1) % 512 + 1);
if (is_ciphered == 1) {
is_ciphered = 1;
if (numkeys > 1)
for (int i = 0; i < numkeys; i++) {
sprintf(paxhdr_varstring, "TC.hmac.%d", i);
if (getpaxvar(fs.xheader, fs.xheaderlen, paxhdr_varstring, &paxdata, &paxdatalen) == 0) {
strncata0(&ciphertype, paxdata, paxdatalen - 1);
use_hmac = 1;
}
}
else {
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.hmac", &paxdata, &paxdatalen) == 0) {
strncata0(&ciphertype, paxdata, paxdatalen - 1);
use_hmac = 1;
}
}
if (use_hmac == 1) {
unsigned char *tmphash;
strcpy((char *) hmac, (char *) (tmphash = sha256_hex(ciphertype)));
free(tmphash);
}
}
if (config.hash == 1)
s1f = sha_file_init_w(fwrite, curfile, 1);
else if (config.hash == 2)
s1f = sha_file_init_w(fwrite, curfile, 2);
else {
fprintf(stderr, "Couldn't determine hash type %d\n", config.hash);
exit(1);
}
c_fwrite = sha_file_write;
c_handle = s1f;
if (use_hmac == 0) {
lzf = lzop_init_w(sha_file_write, s1f);
c_fwrite = lzop_write;
c_handle = lzf;
}
if (fs.n_sparsedata > 0) {
int stlen;
if (sparsetext != NULL)
sparsetext[0] = '\0';
stlen = gen_sparse_data_string(&fs, &sparsetext);
c_fwrite(sparsetext, 1, stlen, c_handle);
filesize = fs.sparse_realsize;
}
double curtime = ftime();
double lastupdate_time = curtime;
size_t tot_size = 0;
while (sizeremaining > 0) {
c = fread(databuf, 1, sizeremaining < bufsize ? sizeremaining : bufsize, stdin);
c_fwrite(databuf, 1, c, c_handle);
sizeremaining -= c;
tot_size += c;
if ((curtime = ftime()) > lastupdate_time + 1) {
fprintf(out, "2\t%s\t%lu\n", stresc(fs.filename, &escfname), tot_size);
}
}
if (use_hmac == 0)
lzop_finalize_w(lzf);
if (config.hash == 1) {
sha_finalize_w(s1f, cfsha1);
encode_block_16(cfsha1x, cfsha1, SHA_DIGEST_LENGTH);
cfshax = cfsha1x;
} else if (config.hash == 2) {
sha_finalize_w(s1f, cfsha2);
encode_block_16(cfsha2x, cfsha2, SHA256_DIGEST_LENGTH);
cfshax = cfsha2x;
cfsha2x[40] = '\0';
}
else {
fprintf(stderr, "Error in determining hash type\n");
exit(1);
}
if (fclose(curfile) == 0)
wrote_file = 1;
else {
fprintf(stderr, "Error writing file, aborting\n");
exit(1);
}
while (padding > 0)
padding -= fread(databuf, 1, padding < bufsize ? padding : bufsize, stdin);
if (escxheader == NULL)
escxheader = dmalloc(((int)((fs.xheaderlen + 2) / 3)) * 4 + 1);
else
if (dmalloc_size(escxheader) < ((int)((fs.xheaderlen + 2) / 3)) * 4 + 1)
escxheader = drealloc(escxheader, ((int)((fs.xheaderlen + 2) / 3)) * 4 + 1);
if (is_ciphered == 1) {
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.original.size", &paxdata, &paxdatalen) == 0) {
filesize = strtoull(paxdata, 0, 10);
}
if (getpaxvar(fs.xheader, fs.xheaderlen, "TC.sparse.original.size", &paxdata, &paxdatalen) == 0) {
filesize = strtoull(paxdata, 0, 10);
}
}
fprintf(out, "1\t%c\t%4.4o\t%s\t%d\t%s\t%d\t%lld\t%s\t%lu\t%s\t%s\t%d\t%s",
is_ciphered == 1 ? 'E' : fs.n_sparsedata > 0 ? 'S' : fs.ftype, fs.mode,
fs.auid, fs.nuid, fs.agid, fs.ngid, filesize, use_hmac == 0 ? cfshax : hmac,
fs.modtime, stresc(fs.filename, &escfname),
stresc(fs.linktarget == 0 ? "" : fs.linktarget, &esclname), fs.xheaderlen,
fs.xheaderlen == 0 ? "" : EncodeBlock2(escxheader, fs.xheader, fs.xheaderlen, NULL)
);
wrote_file = 1;
}
else {
if (escxheader == NULL)
escxheader = dmalloc(((int)((fs.xheaderlen + 2) / 3)) * 4 + 1);
else
if (dmalloc_size(escxheader) < ((int)((fs.xheaderlen + 2) / 3)) * 4 + 1)
escxheader = drealloc(escxheader, ((int)((fs.xheaderlen + 2) / 3)) * 4 + 1);
if (fs.ftype == '5' && strlen(fs.filename) > 0 && fs.filename[strlen(fs.filename) - 1] == '/') {
fs.filename[strlen(fs.filename) - 1] = '\0';
}
fprintf(out, "1\t%c\t%4.4o\t%s\t%d\t%s\t%d\t%lld\t%s\t%lu\t%s\t%s\t%d\t%s\n",
fs.ftype, fs.mode, fs.auid, fs.nuid, fs.agid, fs.ngid,
fs.filesize, "0", fs.modtime, stresc(fs.filename, &escfname),
stresc(fs.linktarget == 0 ? "" : fs.linktarget, &esclname), fs.xheaderlen,
fs.xheaderlen == 0 ? "" : EncodeBlock2(escxheader, fs.xheader, fs.xheaderlen, NULL)
);
}
if (wrote_file == 1) {
if (use_hmac == 0) {
strncpy(destdir2, (char *) cfshax, 2);
destdir2[2] = '\0';
snprintf(targetdir, 1024, "%s/%s", destdir, destdir2);
snprintf(targetpath, 1024, "%s/%s/%s.lzo", destdir, destdir2, cfshax + 2);
}
else {
strncpy(destdir2, (char *) hmac, 2);
destdir2[2] = '\0';
snprintf(targetdir, 1024, "%s/%s", destdir, destdir2);
snprintf(targetpath, 1024, "%s/%s/%s.enc", destdir, destdir2, hmac + 2);
}
if (stat(targetdir, &tmpfstat) != 0) {
if (mkdir(targetdir, 0770) != 0) {
if (stat(targetdir, &tmpfstat) != 0) {
fprintf(stderr, "Error creating directory %s\n", targetdir);
exit(1);
}
}
}
if (stat(targetpath, &tmpfstat) != 0 || utime(targetpath, NULL) != 0) {
if (rename(tmpfilepath, targetpath) == 0) {
}
else {
fprintf(stderr, "Error moving file to vault, aborting\n");
exit(1);
}
}
else {
unlink(tmpfilepath);
}
fprintf(out, "\n");
}
fsclear(&fs);
}
fsfree(&fs);
if (escfname != NULL)
stresc_free(&escfname);
if (esclname != NULL)
stresc_free(&esclname);
dfree(escxheader);
fflush(out);
fclose(out);
close(out_h);
free(config.vault);
free(config.meta);
dfree(ciphertype);
dfree(sparsetext);
exit(0);
}
else {
close(out_h);
}
return(0);
}
int pipebuf(int *in, int *out)
{
int pipein[2];
int pipeout[2];
pid_t child;
if (*in < 0)
if (pipe(pipein) < 0) {
fprintf(stderr, "Pipe error\n");
exit(1);
}
if (*out < 0)
if (pipe(pipeout) < 0) {
fprintf(stderr, "Pipe error\n");
exit(1);
}
if ((child = fork()) > 0) {
if (*in < 0) {
close(pipein[0]);
*in = pipein[1];
}
if (*out < 0) {
close(pipeout[1]);
*out = pipeout[0];
}
return(0);
}
else {
struct ringbuf *r;
struct timeval s_tm;
int bufsize = 4096;
char buf[bufsize];
ssize_t n;
ssize_t o;
int ateof = 0;
fclose(stdout);
if (*in < 0)
close(pipein[1]);
else
pipein[0] = *in;
if (*out < 0)
close(pipeout[0]);
else
pipeout[1] = *out;
r = rbinit(1024 * 1024 * 1024);
fd_set s_in;
fd_set s_out;
free(config.vault);
free(config.meta);
// fcntl(pipein[0], F_SETFL, O_NONBLOCK);
// fcntl(pipeout[1], F_SETFL, O_NONBLOCK);
while (1) {
if (rbused(r) == 0 && ateof == 1) {
rbfree(r);
exit(0);
}
// if the buffer is empty, then wait for input and see if output will block
if (rbused(r) == 0 && ateof == 0) {
FD_ZERO(&s_in);
FD_SET(pipein[0], &s_in);
select(1024, &s_in, NULL, NULL, NULL);
s_tm.tv_sec = 0;
s_tm.tv_usec = 0;
FD_ZERO(&s_out);
FD_SET(pipeout[1], &s_out);
select(1024, NULL, &s_out, NULL, &s_tm);
n = read(pipein[0], buf, bufsize);
if (n <= 0) {
ateof = 1;
}
if (FD_ISSET(pipeout[1], &s_out)) {
o = write(pipeout[1], buf, n);
if (o < n) {
fprintf(stderr, "Short write a of %lu, wanted %lu, putting %lu bytes back in buffer\n", o, n, n - o);
rbrewind(r, n - o);
}
}
else {
rbwrite(buf, 1, n, r);
}
}
// if there is room in the buffer, wait for either input or output
else if (rbavail(r) > bufsize && ateof == 0) {
FD_ZERO(&s_in);
FD_SET(pipein[0], &s_in);
FD_ZERO(&s_out);
FD_SET(pipeout[1], &s_out);
select(1024, &s_in, &s_out, NULL, NULL);
// flush ring buffer until output blocks
while (FD_ISSET(pipeout[1], &s_out) && rbused(r) > 0) {
n = rbread(buf, 1, bufsize, r);
if (n <= 0)
fprintf(stderr, "Error 1 -- n is %lu\n", n);
o = write(pipeout[1], buf, n);
// if not all written, put remainder back in ring buffer
if (o < n) {
fprintf(stderr, "Short write b of %lu, wanted %lu, putting %lu bytes back in buffer\n", o, n, n - o);
rbrewind(r, n - o);
}
s_tm.tv_sec = 0;
s_tm.tv_usec = 0;
FD_ZERO(&s_out);
FD_SET(pipeout[1], &s_out);
select(1024, NULL, &s_out, NULL, &s_tm);
}
if (ateof == 0) {
n = read(pipein[0], buf, bufsize);
if (n <= 0) {
ateof = 1;
}
if (rbused(r) == 0) {
o = write(pipeout[1], buf, n);
if (o < n) {
fprintf(stderr, "Short write c of %lu, wanted %lu, putting %lu bytes back in buffer\n", o, n, n - o);
rbrewind(r, n - o);
}
}
else {
rbwrite(buf, 1, n, r);
}
}
}
// otherwise, buffer is full, wait for output
else {
FD_ZERO(&s_out);