-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtxzchk.c
More file actions
2258 lines (2062 loc) · 69.2 KB
/
txzchk.c
File metadata and controls
2258 lines (2062 loc) · 69.2 KB
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
/*
* txzchk - IOCCC submission tarball validation tool
*
* txzchk verifies that IOCCC submission tarballs conform to the IOCCC rules (no
* feathers stuck in the tarballs :-) ).
*
* txzchk is invoked by mkiocccentry; txzchk in turn uses fnamchk to make
* sure that the tarball was correctly named and formed. In other words txzchk
* makes sure that the mkiocccentry tool was used and there was no screwing
* around with the resultant tarball.
*
* Copyright (c) 2022-2025 by Cody Boone Ferguson. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright, this permission notice and text
* this comment, and the disclaimer below appear in all of the following:
*
* supporting documentation
* source copies
* source works derived from this source
* binaries derived from this source or from derived source
*
* CODY BOONE FERGUSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
* SHALL CODY BOONE FERGUSON BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*
* This tool was written in 2022-2025 by Cody Boone Ferguson:
*
* @xexyl
* https://xexyl.net Cody Boone Ferguson
* https://ioccc.xexyl.net
*
* "Because sometimes even the IOCCC Judges need some help." :-)
*
* Dedicated to:
*
* The many poor souls who have been tarred and feathered:
*
* "Because sometimes people throw feathers on tar." :-(
*
* And to my wonderful Mum and my dear cousin and friend Dani:
*
* "Because even feathery balls of tar need some love." :-)
*
* Disclaimer:
*
* No pitman or coal mines were harmed in the making of this tool and
* neither were any pine trees or birch trees. Whether the fact no coal
* mines were harmed is a good or bad thing might be debatable but
* nevertheless none were harmed. :-) More importantly, no tar pits -
* including the La Brea Tar Pits - were disturbed in the making of this
* tool. :-)
*
* Share and enjoy! :-)
* -- Sirius Cybernetics Corporation Complaints Division, JSON spec department. :-)
*/
/* special comments for the seqcexit tool */
/* exit code out of numerical order - ignore in sequencing - ooo */
/* exit code change of order - use new value in sequencing - coo */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h> /* strcasecmp() */
#include <ctype.h>
#include <stdint.h>
#include <sys/wait.h> /* for WEXITSTATUS() */
#include <locale.h>
/*
* txzchk - IOCCC tarball validation check tool
*/
#include "txzchk.h"
/*
* definitions
*/
#define REQUIRED_ARGS (1) /* number of required arguments on the command line */
/*
* static globals
*/
static intmax_t sum_check; /* negative of previous sum */
static intmax_t count_check; /* negative of previous count */
static char const *tarball_path = NULL; /* the tarball (by path) being checked */
static char const *program = NULL; /* our name */
static bool read_from_text_file = false; /* true ==> assume tarball_path refers to a text file */
static char const *ext = "txz"; /* force extension in fnamchk to be this value */
static char const *tok_sep = " \t"; /* token separators for strtok_r */
static bool show_warnings = false; /* true ==> show warnings even if -q */
static bool entertain = false; /* true ==> show entertaining messages */
static uintmax_t feathery = 3; /* for entertain option */
static bool test_mode = false; /* true ==> use -t in fnamchk */
/*
* txzchk specific structs
*/
static struct txz_line *txz_lines; /* all of the read lines */
static struct tarball tarball; /* all the information collected from tarball */
static struct txz_file *txz_files; /* linked list of the files in the tarball */
/*
* usage message
*
* Use the usage() function to print the usage_msg([0-9]?)+ strings.
*/
static const char * const usage_msg =
"usage: %s [-h] [-v level] [-q] [-e] [-f feathers] [-w] [-V] [-t tar] [-F fnamchk] [-T] [-E ext] [-x] tarball_path\n"
"\n"
"\t-h\t\tprint help message and exit\n"
"\t-v level\tset verbosity level: (def level: %d)\n"
"\t-q\t\tquiet mode: silence msg(), warn(), warnp() if -v 0 (def: loud :-) )\n"
"\t-e\t\tentertainment mode (def: be boring :-) )\n"
"\t-f feathers\tdefine how many feathers is feathery (for -e)\n"
"\t-w\t\talways show warning messages\n"
"\t-V\t\tprint version string and exit\n"
"\t-t tar\t\tpath to tar executable that supports the -J (xz) option (def: %s)\n"
"\t-F fnamchk\tpath to tool that checks if tarball_path is a valid compressed tarball\n"
"\t\t\t filename (def: %s)\n"
"\t-T\t\tassume tarball_path is a text file with tar listing (for testing\n"
"\t\t\t different formats)\n"
"\t-E ext\t\tchange extension to test (def: txz)\n"
"\t-x\t\tforce fnamchk -t even if -T is not used\n"
"\n"
"\ttarball_path\tpath to an IOCCC compressed tarball\n"
"\n"
"Exit codes:\n"
" 0 no feathers stuck in tarball :-)\n"
" 1 tarball was successfully parsed :-) but there's at least one feather stuck in it :-(\n"
" 2 -h and help string printed or -V and version string printed\n"
" 3 invalid command line, invalid option or option missing an argument\n"
" >= 10 internal error has occurred or unknown tar listing format has been encountered\n"
"\n"
"%s version: %s\n"
"jparse utils version: %s\n"
"jparse UTF-8 version: %s\n"
"jparse library version: %s";
/*
* forward declarations
*/
static void usage(int exitcode, char const *prog, char const *str) __attribute__((noreturn));
int
main(int argc, char **argv)
{
extern char *optarg; /* option argument */
extern int optind; /* argv index of the next arg */
char *tar = TAR_PATH_0; /* path to tar executable that supports the -J (xz) option */
char *fnamchk = FNAMCHK_PATH_0; /* path to fnamchk tool */
int i;
bool found_tar = false; /* for find_utils */
bool found_fnamchk = false; /* for find_utils */
bool opt_error = false; /* fchk_inval_opt() return */
enum path_sanity sanity = PATH_ERR_UNSET; /* canon_path error or PATH_OK */
/* IOCCC requires use of C locale */
set_ioccc_locale();
/*
* parse args
*/
program = argv[0];
while ((i = getopt(argc, argv, ":hv:qVF:t:TE:wef:x")) != -1) {
switch (i) {
case 'h': /* -h - print help to stderr and exit 2 */
usage(2, program, ""); /*ooo*/
not_reached();
break;
case 'v': /* -v verbosity */
/*
* parse verbosity
*/
verbosity_level = parse_verbosity(optarg);
if (verbosity_level < 0) {
usage(3, program, "invalid -v verbosity"); /*ooo*/
not_reached();
}
break;
case 'q':
msg_warn_silent = true;
break;
case 'V': /* -V - print version and exit 2 */
print("%s version: %s\n", TXZCHK_BASENAME, TXZCHK_VERSION);
print("jparse utils version: %s\n", JPARSE_UTILS_VERSION);
print("jparse UTF-8 version: %s\n", JPARSE_UTF8_VERSION);
print("jparse library version: %s\n", JPARSE_LIBRARY_VERSION);
exit(2); /*ooo*/
not_reached();
break;
case 'F': /* -F fnamchk - specify path to fnamchk tool */
fnamchk = optarg;
break;
case 't': /* -t tar - specify path to tar (perhaps to tar and feather :-) ) */
tar = optarg;
break;
case 'T': /* -T - text (test) file mode - don't rely on tar: just read file as if it was a text file */
read_from_text_file = true;
break;
case 'E': /* -E ext - specify extension (used with -T for test suite) */
ext = optarg;
break;
case 'w': /* -w - always show warnings */
show_warnings = true;
break;
case 'e': /* whee! */
entertain = true;
break;
case 'f': /* how many feathers is feathery? */
if (!string_to_uintmax(optarg, &feathery)) {
usage(3, program, "invalid -f feathers"); /*ooo*/
not_reached();
}
break;
case 'x': /* when mkiocccentry UUID is "test" we need this */
test_mode = true;
break;
case ':': /* option requires an argument */
case '?': /* illegal option */
default: /* anything else but should not actually happen */
opt_error = fchk_inval_opt(stderr, program, i, optopt);
if (opt_error) {
usage(3, program, ""); /*ooo*/
not_reached();
} else {
fwarn(stderr, TXZCHK_BASENAME, "is %s: getopt() return: %c optopt: %c", __func__, (char)i, (char)optopt);
}
break;
}
}
/* must have the exact required number of args */
if (argc - optind != REQUIRED_ARGS) {
usage(3, program, "wrong number of arguments"); /*ooo*/
not_reached();
}
/* IMPORTANT: canon_path() MUST use a false "lower_case" arg! See the path_in_item_array() function. */
tarball_path = canon_path(argv[optind], 0, 0, 0, &sanity, NULL, NULL, false, false, true, true, NULL);
if (tarball_path == NULL) {
err(3, program, "bogus tarball path: %s error: %s", argv[optind], path_sanity_error(sanity)); /*ooo*/
not_reached();
}
dbg(DBG_MED, "tarball path: %s", tarball_path);
dbg(DBG_MED, "fnamchk test mode: %s", booltostr(test_mode));
dbg(DBG_MED, "entertainment mode: %s", booltostr(entertain));
/* if -w used then we always show warnings from warn() */
if (show_warnings) {
warn_output_allowed = true;
msg_warn_silent = false;
}
if (entertain) {
/*
* Welcome
*/
print("Welcome to txzchk version: %s\n", TXZCHK_VERSION);
}
/*
* environment sanity checks
*/
if (entertain) {
para("", "Checking if your environment is full of tar ...", NULL);
}
/*
* guess where tar is
*
* If the user did not give a -t /path/to/tar then look at the historic
* location for the utility. If the historic location of the utility isn't
* executable, look for an executable in the alternate location.
*
* On some systems where /usr/bin != /bin, the distribution made the mistake of
* moving historic critical applications, look to see if the alternate path works instead.
*/
if (!read_from_text_file) {
/*
* we need tar
*/
find_utils(&found_tar, &tar, NULL, NULL, NULL, NULL, &found_fnamchk, &fnamchk,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
} else {
/*
* we do not need tar
*/
find_utils(NULL, NULL, NULL, NULL, NULL, NULL, &found_fnamchk, &fnamchk,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}
/* additional sanity checks */
txzchk_sanity_chks(tar, fnamchk);
if (entertain) {
para("... environment looks tarry.", NULL);
}
/*
* check the tarball
*/
if (entertain) {
para("", "Looking for feathers in tarball ...", NULL);
}
tarball.total_feathers = check_tarball(tar, fnamchk);
if (entertain) {
if (!tarball.total_feathers) {
para("No feathers stuck in tarball.", NULL);
} else {
if (tarball.total_feathers >= feathery) {
para("\n... looks like someone has been throwing feathers",
"about, because that is quite a feathery ball of tar!", NULL);
}
}
}
show_tarball_info(tarball_path);
/*
* we need to free the paths to the tools
*/
if (tar != NULL && !read_from_text_file && found_tar) {
free(tar);
tar = NULL;
}
if (fnamchk != NULL && found_fnamchk) {
free(fnamchk);
fnamchk = NULL;
}
/*
* All Done!!! All Done!!! -- Jessica Noll, Age 2
*/
if (tarball.total_feathers > 0) {
exit(1); /*ooo*/
}
exit(0); /*ooo*/
}
/*
* show_tarball_info - show information about tarball (if verbosity is >= DBG_MED)
*
* given:
*
* tarball_path - path to tarball we checked
*
* Returns void. Does not return on error.
*/
static void
show_tarball_info(char const *tarball_path)
{
/*
* firewall
*/
if (tarball_path == NULL) {
err(10, __func__, "passed NULL tarball path");
not_reached();
} else if (*tarball_path == '\0') {
err(11, __func__, "passed empty tarball path string");
not_reached();
}
if (verbosity_level >= DBG_MED) {
/* show information about tarball */
para("", "The following information about the tarball was collected:", NULL);
dbg(DBG_MED, "tarball size: %lld", (long long)tarball.size);
dbg(DBG_MED, "total file size: %lld", (long long)tarball.total_size);
dbg(DBG_MED, "total file size shrunk %lld time%s", (long long)tarball.files_size_shrunk,
SINGULAR_OR_PLURAL(tarball.files_size_shrunk));
dbg(DBG_MED, "total number of files with invalid permissions: %lld", (long long)tarball.invalid_perms);
dbg(DBG_MED, "total number of executable files: %lld\n", (long long)tarball.total_exec_files);
if (tarball.total_feathers > 0) {
dbg(DBG_VHIGH, "%s has %ju feather%s stuck in tarball :-(", tarball_path, tarball.total_feathers,
SINGULAR_OR_PLURAL(tarball.total_feathers));
} else {
dbg(DBG_VHIGH, "%s has 0 feathers stuck in tarball :-)", tarball_path);
}
}
}
/*
* usage - print usage to stderr
*
* Example:
* usage(2, program, "wrong number of arguments");
*
* given:
* exitcode value to exit with
* prog our program name
* str top level usage message
*
* NOTE: we warn with extra newlines to help internal fault messages stand out.
* Normally one should NOT include newlines in warn messages.
*
* This function does not return.
*/
static void
usage(int exitcode, char const *prog, char const *str)
{
/*
* firewall
*/
if (str == NULL) {
str = "((NULL str))";
warn(TXZCHK_BASENAME, "\nin usage(): str was NULL, forcing it to be: %s\n", str);
}
if (prog == NULL) {
prog = TXZCHK_BASENAME;
warn(TXZCHK_BASENAME, "\nin usage(): prog was NULL, forcing it to be: %s\n", prog);
}
/*
* print the formatted usage stream
*/
if (*str != '\0') {
fprintf_usage(DO_NOT_EXIT, stderr, "%s\n", str);
}
fprintf_usage(exitcode, stderr, usage_msg, prog, DBG_DEFAULT, TAR_PATH_0, FNAMCHK_PATH_0,
TXZCHK_BASENAME, TXZCHK_VERSION, JPARSE_UTILS_VERSION, JPARSE_UTF8_VERSION, JPARSE_LIBRARY_VERSION);
exit(exitcode); /*ooo*/
not_reached();
}
/*
* txzchk_sanity_chks - perform basic sanity checks
*
* We perform basic sanity checks on paths as well as some of the IOCCC tables.
*
* given:
*
* tar - path to tar that supports the -J (xz) option
* fnamchk - path to the fnamchk utility
*
* NOTE: this function does not return on error or if things are not sane.
*/
static void
txzchk_sanity_chks(char const *tar, char const *fnamchk)
{
/*
* firewall
*/
if ((tar == NULL && !read_from_text_file) || fnamchk == NULL || tarball_path == NULL) {
err(12, __func__, "called with NULL arg(s)");
not_reached();
}
/*
* if text file flag not used tar must be executable
*/
if (!read_from_text_file)
{
if (!exists(tar)) {
fpara(stderr,
"",
"We cannot find tar.",
"",
"A tar program that supports the -J (xz) option is required to test the compressed tarball.",
"Perhaps you need to use:",
"",
" txzchk -t /path/to/tar [...]",
"",
"and/or install a tar program? You can find the source for tar:",
"",
" https://www.gnu.org/software/tar/",
"",
NULL);
err(13, __func__, "tar does not exist: %s", tar);
not_reached();
}
if (!is_file(tar)) {
fpara(stderr,
"",
"The tar path, while it exists, is not a regular file.",
"",
"Perhaps you need to use another path:",
"",
" txzchk -t /path/to/tar [...]",
"",
"and/or install a tar program? You can find the source for tar:",
"",
" https://www.gnu.org/software/tar/",
"",
NULL);
err(14, __func__, "tar is not a regular file: %s", tar);
not_reached();
}
if (!is_exec(tar)) {
fpara(stderr,
"",
"The tar path, while it is a file, is not executable.",
"",
"We suggest you check the permissions on the tar program, or use another path:",
"",
" txzchk -t /path/to/tar [...]",
"",
"and/or install a tar program? You can find the source for tar:",
"",
" https://www.gnu.org/software/tar/",
"",
NULL);
err(15, __func__, "tar is not an executable program: %s", tar);
not_reached();
}
}
/*
* fnamchk must be executable
*/
if (!exists(fnamchk)) {
fpara(stderr,
"",
"We cannot find fnamchk.",
"",
"This tool is required to test the tarball.",
"Perhaps you need to use another path:",
"",
" txzchk -F /path/to/fnamchk [...]",
"",
"or install the mkiocccentry toolkit? You can find the mkiocccentry toolkit at:",
"",
" https://github.com/ioccc-src/mkiocccentry",
"",
NULL);
err(16, __func__, "fnamchk does not exist: %s", fnamchk);
not_reached();
}
if (!is_file(fnamchk)) {
fpara(stderr,
"",
"The fnamchk path, while it exists, is not a regular file.",
"",
"Perhaps you need to use another path:",
"",
" txzchk -F /path/to/fnamchk [...]",
"",
"or install the mkiocccentry toolkit? You can find the mkiocccentry toolkit at:",
"",
" https://github.com/ioccc-src/mkiocccentry",
"",
NULL);
err(17, __func__, "fnamchk is not a regular file: %s", fnamchk);
not_reached();
}
if (!is_exec(fnamchk)) {
fpara(stderr,
"",
"The fnamchk path, while it is a file, is not executable.",
"",
"We suggest you check the permissions on the fnamchk program, or use another path:",
"",
" txzchk -F /path/to/fnamchk [...]",
"",
"or install the mkiocccentry toolkit? You can find the mkiocccentry toolkit at:",
"",
" https://github.com/ioccc-src/mkiocccentry",
"",
NULL);
err(18, __func__, "fnamchk is not an executable program: %s", fnamchk);
not_reached();
}
/*
* tarball_path must be readable
*/
if (!exists(tarball_path)) {
fpara(stderr,
"",
"The tarball path specified does not exist. Perhaps you made a typo?",
"Please check the path and try again."
"",
" txzchk [options] <tarball_path>"
"",
NULL);
err(19, __func__, "tarball_path does not exist: %s", tarball_path);
not_reached();
}
if (!is_file(tarball_path)) {
fpara(stderr,
"",
"The file specified, while it exists, is not a regular file.",
"",
"Perhaps you need to use another path:",
"",
" txzchk [...] <tarball_path>",
"",
NULL);
err(20, __func__, "tarball_path is not a regular file: %s", tarball_path);
not_reached();
}
if (!is_read(tarball_path)) {
fpara(stderr,
"",
"The tarball path, while it is a file, is not readable.",
"",
"We suggest you check the permissions on the path or use another path:",
"",
" txzchk [...] <tarball_path>"
"",
NULL);
err(21, __func__, "tarball_path is not readable: %s", tarball_path);
not_reached();
}
return;
}
/*
* check_all_txz_files - check txz_files list after parsing tarball (or text file)
*
* This function does a few sanity checks but primarily it uses the walk code,
* recording the step of each file and verifying everything is okay.
*
* Returns void. Ignores empty files (though these should not be in the list at
* all).
*
* This function does not return on NULL filenames or basenames (neither of
* which should ever happen).
*/
static void
check_all_txz_files(void)
{
struct txz_file *file; /* to iterate through files list */
struct walk_stat wstat; /* record information and stats about a complete walk */
bool process = false; /* true ==> process item, false ==> ignore item */
bool dup = false; /* true ==> attempt to record a duplicate canonical path */
bool walk_ok = false; /* true ==> no walk errors found, false ==> some walk errors found */
char const *path = NULL; /* a path from a tarball line listing */
char const *cpath = NULL; /* canonicalised path arg as a string */
enum path_sanity sanity = PATH_ERR_UNSET; /* canonicalize_path() error code, or PATH_OK */
size_t path_len = 0; /* canonicalised path length */
int_least32_t deep = -1; /* canonicalised stack depth */
/*
* init walk code
*/
memset(&wstat, 0, sizeof(wstat));
init_walk_stat(&wstat, ".", &walk_txzchk, TXZCHK_BASENAME, MAX_PATH_LEN, MAX_FILENAME_LEN, MAX_PATH_DEPTH, true);
/*
* Now go through the files list to verify the required files are there and
* also to detect any additional feathers stuck in the tarball (or issues in
* the text file).
*/
for (file = txz_files; file != NULL; file = file->next) {
if (file->basename == NULL) {
err(22, __func__, "found NULL file->basename in txz_files list");
not_reached();
} else if (file->filename == NULL) {
err(23, __func__, "found NULL file->filename in txz_files list");
not_reached();
}
/*
* now check empty basenames and filenames, in case any slipped through.
*/
if (*(file->basename) == '\0') {
warn(TXZCHK_BASENAME, "found empty basename in txz_files list");
++tarball.total_feathers;
continue;
}
if (*(file->filename) == '\0') {
warn(TXZCHK_BASENAME, "found empty filename in txz_files list");
++tarball.total_feathers;
continue;
}
check_directory(file, file->dirname, tarball_path);
/*
* first find the first '/'. If NULL warn about it and skip file
*/
path = strchr(file->filename, '/');
if (path == NULL) {
warn(TXZCHK_BASENAME, "no directory found in filename: %s", file->filename);
++tarball.total_feathers;
continue;
} else {
/*
* first canonicalise the path
*/
cpath = canonicalize_path(&wstat, path + 1, &sanity, &path_len, &deep);
if (cpath == NULL) {
warn(TXZCHK_BASENAME, "canonicalize_path had an internal failure and returned NULL");
++tarball.total_feathers;
continue;
}
}
/* process the path, size, and st_mode from the tarball listing line */
process = record_step(&wstat, cpath, file->length, file->mode, &dup, NULL);
if (dup) {
warn(TXZCHK_BASENAME, "file %s is a duplicate file", cpath != NULL?cpath:file->filename);
++tarball.total_feathers;
} else if (process) {
dbg(DBG_MED, "txzchk: file %s has been successfully processed", cpath!=NULL?cpath:file->filename);
}
if (cpath != NULL) {
free((void *)cpath);
cpath = NULL;
}
}
walk_ok = chk_walk(&wstat, stderr, MAX_EXTRA_FILE_COUNT, MAX_EXTRA_DIR_COUNT,
NO_COUNT, NO_COUNT, true);
if (walk_ok) {
dbg(DBG_MED, "all okay walking tarball: %s", tarball_path);
} else {
if (entertain) {
warn(TXZCHK_BASENAME, "encountered one or more feathers in tar pit: %s", tarball_path);
} else {
warn(TXZCHK_BASENAME, "encountered an error walking directory in tarball: %s", tarball_path);
}
++tarball.total_feathers;
}
/*
* free the walk_stat
*/
free_walk_stat(&wstat);
if (tarball.total_size > MAX_SUM_FILELEN) {
warn(TXZCHK_BASENAME, "total length of tarball contents is too long: %lld > limit: %lld", (long long)tarball.total_size,
(long long) MAX_SUM_FILELEN);
++tarball.total_feathers;
} else {
dbg(DBG_LOW, "total length of tarball size: %lld <= limit: %lld", (long long)tarball.total_size,
(long long) MAX_SUM_FILELEN);
}
/*
* report total feathers found
*/
if (tarball.total_feathers > 0) {
warn(TXZCHK_BASENAME, "%s: found %ju feather%s stuck in the tarball",
tarball_path, tarball.total_feathers, tarball.total_feathers==1?"":"s");
}
return;
}
/*
* check_directory - directory specific checks on this _file_
*
* given:
*
* file - file structure
* dirname - the directory expected (or NULL if fnamchk fails)
* tarball_path - the tarball path
*
* Issues a warning if the expected (if fnamchk did not fail i.e. dirname !=
* NULL) directory name in the file is not correct (i.e. the top level directory
* != dirname). If fnamchk did fail it is also warned against.
*
* Does not return on error.
*/
static void
check_directory(struct txz_file *file, char const *dirname, char const *tarball_path)
{
/*
* firewall
*/
if (tarball_path == NULL || file == NULL || file->filename == NULL) {
err(24, __func__, "passed NULL arg(s)");
not_reached();
}
if (dirname != NULL && *dirname != '\0')
{
if (strncmp(file->filename, dirname, strlen(dirname))) {
warn(TXZCHK_BASENAME, "%s: found incorrect top level directory in filename %s", tarball_path, file->filename);
++tarball.total_feathers;
} else {
/* This file has the right top level directory */
dbg(DBG_HIGH, "%s: correct directory %s for file %s", tarball_path, dirname, file->filename);
}
} else if (!test_mode) {
warn(TXZCHK_BASENAME, "%s: found incorrect top level directory in filename %s", tarball_path, file->filename);
++tarball.total_feathers;
}
return;
}
/*
* count_and_sum - wrapper to sum_and_count (util.c) related checks
*
* given:
*
* tarball_path - path to the tarball being checked for feathers
* sum - corresponds to the sum pointer in sum_and_count()
* count - corresponds to the count pointer in sum_and_count()
* length - corresponds to the length in sum_and_count()
*/
static void
count_and_sum(char const *tarball_path, intmax_t *sum, intmax_t *count, intmax_t length)
{
bool test = false; /* status of various tests */
/*
* firewall
*/
if (tarball_path == NULL) {
err(25, __func__, "tarball_path is NULL");
not_reached();
} else if (sum == NULL) {
err(26, __func__, "sum is NULL");
not_reached();
} else if (count == NULL) {
err(27, __func__, "count is NULL");
not_reached();
}
test = sum_and_count(length, sum, count, &sum_check, &count_check);
if (!test) {
/*
* sum_and_count() will have reported the issue so we don't report anything
* specially. We do however increase the number of feathers.
*/
++tarball.total_feathers;
}
/* update the tarball files size total */
tarball.total_size = *sum;
/* check for negative total file length */
if (*sum < 0) {
++tarball.total_feathers;
++tarball.negative_files_size;
warn(TXZCHK_BASENAME, "%s: total file size went below 0: %jd", tarball_path, *sum);
if (*sum < tarball.previous_files_size) {
++tarball.files_size_shrunk;
warn(TXZCHK_BASENAME, "%s: total files size %jd < previous file size %lld", tarball_path, *sum,
(long long)tarball.previous_files_size);
}
}
/* check for sum of total file lengths being too big */
if (*sum > MAX_SUM_FILELEN) {
if (tarball.previous_files_size <= MAX_SUM_FILELEN) {
++tarball.total_feathers;
++tarball.files_size_too_big;
}
warn(TXZCHK_BASENAME, "%s: total file size too big: %jd > %jd", tarball_path,
*sum, (intmax_t)MAX_SUM_FILELEN);
}
/* update the previous files size */
tarball.previous_files_size = *sum;
/* check for no or negative file count */
if (*count <= 0) {
++tarball.total_feathers;
++tarball.invalid_files_count;
warn(TXZCHK_BASENAME, "%s: files count <= 0: %jd", tarball_path, *count);
}
return;
}
/*
* parse_linux_txz_line - parse linux tar output
*
* given:
*
* p - pointer to current field in line
* linep - the line we're parsing
* line_dup - duplicated line
* dirname - directory name retrieved from fnamchk or NULL if it failed
* tarball_path - the tarball path
* saveptr - pointer to char * to save context between each strtok_r() call
* isfile - true ==> normal file, check size and number of files
* sum - pointer to sum for sum_and_count() (which we use in count_and_sum())
* count - pointer to count for sum_and_count() (which we use in count_and_sum())
* isdir - true ==> is a directory
* perms - permission line of file
* isexec - if executable bit (+x) found in permissions
*
* If everything goes okay the line will be completely parsed and the calling
* function (parse_txz_line()) will return to its caller (parse_all_lines()) which
* will in turn read the next line.
*
* This function does not return on error although the word error is used
* loosely here.
*/
static void
parse_linux_txz_line(char *p, char *linep, char *line_dup, char const *dirname, char const *tarball_path,
char **saveptr, bool isfile, intmax_t *sum, intmax_t *count, bool isdir, char *perms, bool isexec)
{
intmax_t length = 0; /* file size */
struct txz_file *file = NULL; /* allocated struct of file info */
int i = 0;
bool test = false; /* tests related to size and max number of files */
/*
* firewall
*/
if (p == NULL || linep == NULL || line_dup == NULL || tarball_path == NULL || saveptr == NULL ||
sum == NULL || count == NULL || perms == NULL) {
err(28, __func__, "called with NULL arg(s)");
not_reached();
}
/* verify that this is a UID, not a username */
for ( ; *p && isascii(*p) && isdigit(*p) && *p != '/'; ) {
++p;
}
if (*p != '/') {
warn(TXZCHK_BASENAME, "found non-numerical UID in line %s", line_dup);
++tarball.total_feathers;
p = strchr(p, '/');
}
if (p == NULL) {
err(29, __func__, "txzchk: encountered NULL pointer when parsing line %s", line_dup);
not_reached();
}
++p;
/*
* now do the same for group id
*/
for ( ; *p && isascii(*p) && isdigit(*p); ) {
++p; /* satisfy frivolous warning (give loop a body instead of having the loop do the actual action) */
}
if (*p) {
warn(TXZCHK_BASENAME, "found non-numerical GID in file in line %s", line_dup);
++tarball.total_feathers;
}
p = strtok_r(NULL, tok_sep, saveptr);
if (p == NULL) {
err(30, __func__, "%s: NULL pointer encountered trying to parse line", tarball_path);
not_reached();
}
test = string_to_intmax(p, &length);
if (!test) {
warn(TXZCHK_BASENAME, "%s: trying to parse file size in on line: <%s>: token: <%s>", tarball_path, line_dup, p);
++tarball.total_feathers;
/*
* we still have to add to the total number of files before we return to
* next line but only if it's a normal file
*/
if (isfile) {
count_and_sum(tarball_path, sum, count, length);
}
if (verbosity_level) {
msg("skipping to next line due to inability to parse file size");
}
return;
} else if (length < 0) {
++tarball.total_feathers;
warn(TXZCHK_BASENAME, "in tarball: %s: length %lld < 0", tarball_path, (long long)length);
} else if (isfile) {
/* add to total number of files and total size if it's a normal file */
count_and_sum(tarball_path, sum, count, length);
}
/*
* the next two fields we don't care about but we loop three times to
* get the following field which we _do_ care about.
*/
for (i = 0; i < 3; ++i) {
p = strtok_r(NULL, tok_sep, saveptr);
if (p == NULL) {
err(31, __func__, "%s: NULL pointer trying to parse line", tarball_path);
not_reached();
}
}
/* p should now contain the filename. */
file = alloc_txz_file(p, dirname, perms, isdir, isfile, isexec, length);
if (file == NULL) {
err(32, __func__, "alloc_txz_file() returned NULL");
not_reached();
}
do {
p = strtok_r(NULL, tok_sep, saveptr);
if (p != NULL) {
warn(TXZCHK_BASENAME, "%s: bogus field found after filename: %s", tarball_path, p);
++tarball.total_feathers;
}