-
Notifications
You must be signed in to change notification settings - Fork 6
/
txzchk.c
2048 lines (1852 loc) · 62.3 KB
/
txzchk.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
/*
* txzchk - IOCCC tarball validation tool
*
* txzchk verifies that the tarball does not have any feathers stuck in it (i.e.
* the tarball conforms to the IOCCC tarball rules). 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.
*
* Written in 2022 by:
*
* @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. :-)
*/
/* 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() */
/*
* 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 */
/*
* 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] 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"
"\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 name\n"
"\t\t\tfilename (def: %s)\n\n"
"\t-T\t\tassume tarball_path is a text file with tar listing (for testing different formats)\n"
"\t-E ext\t\tchange extension to test (def: txz)\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 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 */
bool fnamchk_flag_used = false; /* if -F option used */
bool tar_flag_used = false; /* true ==> -t /path/to/tar was given */
int i;
/*
* parse args
*/
program = argv[0];
while ((i = getopt(argc, argv, ":hv:qVF:t:TE:wef:")) != -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 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_flag_used = true;
fnamchk = optarg;
break;
case 't': /* -t tar - specify path to tar (perhaps to tar and feather :-) ) */
tar = optarg;
tar_flag_used = true;
break;
case 'T':
read_from_text_file = true; /* -T - text file mode - don't rely on tar: just read file as if it was a text file */
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 ':': /* option requires an argument */
case '?': /* illegal option */
default: /* anything else but should not actually happen */
check_invalid_option(program, i, optopt);
usage(3, program, ""); /*ooo*/
not_reached();
break;
}
}
/* must have the exact required number of args */
if (argc - optind != REQUIRED_ARGS) {
usage(3, program, "wrong number of arguments"); /*ooo*/
not_reached();
}
tarball_path = argv[optind];
dbg(DBG_MED, "tarball path: %s", tarball_path);
/* 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 -T was used we don't actually need tar(1) so we test for that
* specifically.
*/
if (!read_from_text_file) {
find_utils(tar_flag_used, &tar, false, NULL, false, NULL, false, NULL,
fnamchk_flag_used, &fnamchk, false, NULL);
} else {
find_utils(false, NULL, false, NULL, false, NULL, false, NULL,
fnamchk_flag_used, &fnamchk, false, 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);
/*
* 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 arg");
not_reached();
}
if (verbosity_level >= DBG_MED) {
/* show information about tarball */
para("", "The following information about the tarball was collected:", NULL);
dbg(DBG_MED, "%s %s a .info.json", tarball_path, HAS_DOES_NOT_HAVE(tarball.has_info_json));
dbg(DBG_HIGH, "%s %s an empty .info.json", tarball_path, HAS_DOES_NOT_HAVE(tarball.empty_info_json));
dbg(DBG_HIGH, "%s .info.json size is %jd", tarball_path, (intmax_t)tarball.info_json_size);
dbg(DBG_MED, "%s %s a .auth.json", tarball_path, HAS_DOES_NOT_HAVE(tarball.has_auth_json));
dbg(DBG_HIGH, "%s %s an empty .auth.json", tarball_path, HAS_DOES_NOT_HAVE(tarball.empty_auth_json));
dbg(DBG_HIGH, "%s .auth.json size is %jd", tarball_path, (intmax_t)tarball.auth_json_size);
dbg(DBG_MED, "%s %s a prog.c", tarball_path, HAS_DOES_NOT_HAVE(tarball.has_prog_c));
dbg(DBG_HIGH, "%s %s an empty prog.c", tarball_path, HAS_DOES_NOT_HAVE(tarball.empty_prog_c));
dbg(DBG_HIGH, "%s prog.c size is %jd", tarball_path, (intmax_t)tarball.prog_c_size);
dbg(DBG_MED, "%s %s a remarks.md", tarball_path, HAS_DOES_NOT_HAVE(tarball.has_remarks_md));
dbg(DBG_HIGH, "%s %s an empty remarks.md", tarball_path, HAS_DOES_NOT_HAVE(tarball.empty_remarks_md));
dbg(DBG_HIGH, "%s remarks.md size is %jd", tarball_path, (intmax_t)tarball.remarks_md_size);
dbg(DBG_MED, "%s %s a Makefile", tarball_path, HAS_DOES_NOT_HAVE(tarball.has_Makefile));
dbg(DBG_HIGH, "%s %s an empty Makefile", tarball_path, HAS_DOES_NOT_HAVE(tarball.empty_Makefile));
dbg(DBG_HIGH, "%s Makefile size is %jd", tarball_path, (intmax_t)tarball.Makefile_size);
dbg(DBG_MED, "%s tarball size is %jd according to stat(2)", tarball_path, (intmax_t)tarball.size);
dbg(DBG_MED, "%s total file size is %jd", tarball_path, (intmax_t)tarball.files_size);
dbg(DBG_HIGH, "%s shrunk in files size %ju time%s", tarball_path, tarball.files_size_shrunk,
SINGULAR_OR_PLURAL(tarball.files_size_shrunk));
dbg(DBG_HIGH, "%s went below 0 in all files size %ju time%s", tarball_path, tarball.negative_files_size,
SINGULAR_OR_PLURAL(tarball.negative_files_size));
dbg(DBG_HIGH, "%s went above max files size %ju %ju time%s", tarball_path,
(uintmax_t)MAX_SUM_FILELEN, (uintmax_t)tarball.files_size_too_big,
SINGULAR_OR_PLURAL(tarball.files_size_too_big));
dbg(DBG_MED, "%s has %ju file%s", tarball_path, tarball.total_files-tarball.abnormal_files,
tarball.total_files-tarball.abnormal_files == 1?"":"s");
if (tarball.correct_directory < tarball.total_files) {
dbg(DBG_MED, "%s has %ju incorrect director%s", tarball_path, tarball.total_files - tarball.correct_directory,
tarball.total_files - tarball.correct_directory == 1 ? "y":"ies");
} else {
dbg(DBG_MED, "%s has 0 incorrect directories", tarball_path);
}
dbg(DBG_MED, "%s has %ju invalid dot file%s", tarball_path, tarball.invalid_dot_files,
SINGULAR_OR_PLURAL(tarball.invalid_dot_files));
dbg(DBG_MED, "%s has %ju file%s named '.'", tarball_path, tarball.named_dot, SINGULAR_OR_PLURAL(tarball.named_dot));
dbg(DBG_MED, "%s has %ju file%s with at least one unsafe char", tarball_path, tarball.unsafe_chars,
SINGULAR_OR_PLURAL(tarball.unsafe_chars));
if (tarball.invalid_filenames) {
dbg(DBG_MED, "%s has %ju invalidly named file%s", tarball_path, tarball.invalid_filenames,
SINGULAR_OR_PLURAL(tarball.invalid_filenames));
}
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", "\nin usage(): str was NULL, forcing it to be: %s\n", str);
}
if (prog == NULL) {
prog = TXZCHK_BASENAME;
warn("txzchk", "\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_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(11, __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(12, __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(13, __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(14, __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:",
"",
" txzchk -F /path/to/fnamchk [...]",
NULL);
err(15, __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 [...]",
NULL);
err(16, __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 [...]",
NULL);
err(17, __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(18, __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(19, __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(20, __func__, "tarball_path is not readable: %s", tarball_path);
not_reached();
}
return;
}
/*
* check_txz_file - checks on the current file only
*
* given:
*
* tarball_path - the tarball (or text file) we're processing
* dir_name - the directory name (if fnamchk passed - else NULL)
* file - file structure
*
* Report feathers stuck in the current tarball.
*
* Returns void. Does not return on error.
*
*/
static void
check_txz_file(char const *tarball_path, char const *dir_name, struct txz_file *file)
{
bool allowed_dot_file = false; /* true ==> basename is an allowed '.' file */
/*
* firewall
*/
if (tarball_path == NULL || file == NULL || file->basename == NULL || file->filename == NULL) {
err(21, __func__, "passed NULL arg(s)");
not_reached();
}
/*
* identify if file is an allowed '.' file
*
* NOTE: Files that are allowed to begin with '.' must also be lower case.
* In other words if any of the letters in INFO_JSON_FILENAME or
* AUTH_JSON_FILENAME are upper case the file is an invalid dot file.
*
* Yes there's a certain irony that the macro names for these filenames are
* all upper case and so we're checking for lower case by using upper case
* but this _is_ part of the IOCCC so why not have a bit of confusion and
* irony? :-) If this unduly bothers you you can just call the upper case
* lower case or the lower case upper case but not upper case lower case and
* lower case upper case! :-) Alternatively you can just deal with the
* irony.
*/
if (!strcmp(file->basename, INFO_JSON_FILENAME) || !strcmp(file->basename, AUTH_JSON_FILENAME)) {
allowed_dot_file = true;
}
/*
* filename must use only POSIX portable filename and + chars plus /
*/
if (!posix_plus_safe(file->filename, false, true, false)) {
++tarball.total_feathers; /* report it once and consider it only one feather */
++tarball.unsafe_chars;
warn(__func__, "%s: file does not match regexp ^[/0-9a-z][/0-9a-z._+-]*$: %s",
tarball_path, file->filename);
}
/*
* case: basename is NOT allowed to begin with a dot.
*/
if (!allowed_dot_file) {
/*
* Check for dot files but note that a basename of only '.' also counts
* as a filename with just '.': so if the file starts with a '.' and
* it's not AUTH_JSON_FILENAME and not INFO_JSON_FILENAME then it's an
* invalid dot file; if it's ONLY '.' it counts as BOTH an invalid dot
* file AND a file called just '.' (which would likely be a directory
* but is abuse nonetheless).
*/
if (*(file->basename) == '.') {
++tarball.total_feathers;
warn("txzchk", "%s: found non %s and %s dot file %s",
tarball_path, AUTH_JSON_FILENAME, INFO_JSON_FILENAME, file->basename);
tarball.invalid_dot_files++;
/* check for files called '.' without anything after the dot */
if (file->basename[1] == '\0') {
++tarball.total_feathers;
++tarball.named_dot;
warn("txzchk", "%s: found file called '.' in path %s", tarball_path, file->filename);
}
}
/*
* basename must use only POSIX portable filename and + chars
*/
if (!posix_plus_safe(file->basename, false, false, true)) {
++tarball.total_feathers; /* report it once and consider it only one feather */
++tarball.unsafe_chars;
warn(__func__, "%s: file basename does not match regexp ^[0-9A-Za-z][0-9A-Za-z._+-]*$: %s",
tarball_path, file->basename);
} else if (!strcasecmp(file->basename, INDEX_HTML_FILENAME) || !strcasecmp(file->basename, INVENTORY_HTML_FILENAME) ||
!strcasecmp(file->basename, PROG_FILENAME) || !strcasecmp(file->basename, PROG_ALT_FILENAME) ||
!strcasecmp(file->basename, PROG_ORIG_FILENAME) || !strcasecmp(file->basename, PROG_ORIG_C_FILENAME) ||
!strcasecmp(file->basename, README_MD_FILENAME)) {
++tarball.total_feathers;
++tarball.invalid_filenames;
warn(__func__, "%s: filename not allowed: %s", tarball_path, file->basename);
}
}
/* check the dirs in the path */
check_directories(file, dir_name, tarball_path);
}
/*
* check_file_size - if file is required record size and (depending on file) report invalid size
*
* given:
*
* tarball_path - the tarball (or text file) we're checking
* size - size of the file
* file - the struct txz_file we're checking
*
* Returns void.
*
* Does not return on error (NULL pointers passed in).
*/
static void
check_file_size(char const *tarball_path, off_t size, struct txz_file *file)
{
/*
* firewall
*/
if (tarball_path == NULL || file == NULL) {
err(22, __func__, "called with NULL arg(s)");
not_reached();
} else if (file->basename == NULL || file->filename == NULL) {
err(23, __func__, "file->basename == NULL || file->filename == NULL which should never happen");
not_reached();
}
if (size == 0) {
if (!strcmp(file->basename, AUTH_JSON_FILENAME)) {
++tarball.total_feathers;
warn("txzchk", "%s: found empty %s file", tarball_path, AUTH_JSON_FILENAME);
tarball.empty_auth_json = true;
} else if (!strcmp(file->basename, INFO_JSON_FILENAME)) {
++tarball.total_feathers;
tarball.empty_info_json = true;
warn("txzchk", "%s: found empty %s file", tarball_path, INFO_JSON_FILENAME);
} else if (!strcmp(file->basename, "remarks.md")) {
++tarball.total_feathers;
tarball.empty_remarks_md = true;
warn("txzchk", "%s: found empty remarks.md", tarball_path);
} else if (!strcmp(file->basename, "Makefile")) {
++tarball.total_feathers;
tarball.empty_Makefile = true;
warn("txzchk", "%s: found empty Makefile", tarball_path);
} else if (!strcmp(file->basename, "prog.c")) {
/* this is NOT a feather: it's only for informational purposes! */
tarball.empty_prog_c = true;
}
} else {
/* record size of required files for informational purposes */
if (!strcmp(file->basename, AUTH_JSON_FILENAME)) {
tarball.auth_json_size = size;
} else if (!strcmp(file->basename, INFO_JSON_FILENAME)) {
tarball.info_json_size = size;
} else if (!strcmp(file->basename, "remarks.md")) {
tarball.remarks_md_size = size;
} else if (!strcmp(file->basename, "Makefile")) {
tarball.Makefile_size = size;
} else if (!strcmp(file->basename, "prog.c")) {
tarball.prog_c_size = size;
}
}
}
/*
* check_all_txz_files - check txz_files list after parsing tarball (or text file)
*
* given:
*
* dir_name - fnamchk result (if passed - else NULL)
*
* Reports any additional feathers stuck in the tarball (or issues in the text
* file).
*
* Returns void. Ignores empty files (though these should not be in the list at
* all).
*
* Does not return on NULL filenames or basenames (neither of which should ever
* happen).
*/
static void
check_all_txz_files(char const *dir_name)
{
struct txz_file *file; /* to iterate through files list */
/*
* 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(24, __func__, "found NULL file->basename in txz_files list");
not_reached();
} else if (file->filename == NULL) {
err(25, __func__, "found NULL file->filename in txz_files list");
not_reached();
}
if (!strcmp(file->basename, INFO_JSON_FILENAME)) {
tarball.has_info_json = true;
} else if (!strcmp(file->basename, AUTH_JSON_FILENAME)) {
tarball.has_auth_json = true;
} else if (!strcmp(file->basename, MAKEFILE_FILENAME)) {
tarball.has_Makefile = true;
} else if (!strcmp(file->basename, PROG_C_FILENAME)) {
tarball.has_prog_c = true;
} else if (!strcmp(file->basename, REMARKS_FILENAME)) {
tarball.has_remarks_md = true;
}
if (dir_name != NULL && tarball.correct_directory) {
if (strncmp(file->filename, dir_name, strlen(dir_name))) {
warn("txzchk", "%s: found directory change in filename %s", tarball_path, file->filename);
++tarball.total_feathers;
}
}
if (file->count > 1) {
warn("txzchk", "%s: found a total of %ju files with the name %s", tarball_path, file->count, file->basename);
tarball.total_feathers += file->count - 1;
}
}
/* determine if the required files are there */
if (!tarball.has_info_json) {
++tarball.total_feathers;
warn("txzchk", "%s: no .info.json found", tarball_path);
}
if (!tarball.has_auth_json) {
++tarball.total_feathers;
warn("txzchk", "%s: no .auth.json found", tarball_path);
}
if (!tarball.has_prog_c) {
++tarball.total_feathers;
warn("txzchk", "%s: no prog.c found", tarball_path);
}
if (!tarball.has_Makefile) {
++tarball.total_feathers;
warn("txzchk", "%s: no Makefile found", tarball_path);
}
if (!tarball.has_remarks_md) {
++tarball.total_feathers;
warn("txzchk", "%s: no remarks.md found", tarball_path);
}
if (tarball.correct_directory < tarball.total_files) {
++tarball.total_feathers;
warn("txzchk", "%s: not all files in correct directory", tarball_path);
}
/*
* Report total number of non .auth.json and .info.json dot files.
* Don't increment the number of feathers as this was done in
* check_txz_file().
*/
if (tarball.invalid_dot_files > 0) {
warn("txzchk", "%s: found a total of %ju invalidly named dot file%s",
tarball_path, tarball.invalid_dot_files, tarball.invalid_dot_files==1?"":"s");
}
/*
* report total feathers found
*/
if (tarball.total_feathers > 0) {
warn("txzchk", "%s: found %ju feather%s stuck in the tarball",
tarball_path, tarball.total_feathers, tarball.total_feathers==1?"":"s");
}
}
/*
* check_directories - directory specific checks on the file
*
* given:
*
* file - file structure
* dir_name - the directory expected (or NULL if fnamchk fails)
* tarball_path - the tarball path
*
* Issues a warning for every violation specific to directories (and
* subdirectories).
*
* Does not return on error.
*/
static void
check_directories(struct txz_file *file, char const *dir_name, char const *tarball_path)
{
uintmax_t dir_count = 0; /* number of directories in the path */
int prev = '\0';
int first = '\0';
int i;
/*
* firewall
*/
if (tarball_path == NULL || file == NULL || file->filename == NULL) {
err(26, __func__, "passed NULL arg(s)");
not_reached();
}
/* check that there is a directory */
if (strchr(file->filename, '/') == NULL && strcmp(file->filename, ".")) {
warn("txzchk", "%s: no directory found in filename %s", tarball_path, file->filename);
++tarball.total_feathers;
}
if (strstr(file->filename, "..")) /* check for '..' in path */ {
/*
* Note that this check does NOT detect a file in the form of "../.file"
* but since the basename of each file is checked in check_txz_file() this
* is okay.
*/
++tarball.total_feathers;
warn("txzchk", "%s: found file with '..' in the path: %s", tarball_path, file->filename);
}
if (*(file->filename) == '/') {
++tarball.total_feathers;
warn("txzchk", "%s: found absolute path %s", tarball_path, file->filename);
}
/*
* Check the path to see if there are any subdirectories. The way this is
* done is counting the number of '/' but done carefully: for example the
* path test-1//prog.c would not count as two directories but just one.
*
* Another example: ..// would be counted as one directory but it still has
* ../ so that would have been detected above.
*
* It does this by saving the previous character: if it was also a '/' then
* it's not counted as another directory: the first one will be counted
* however since there wasn't one before it.
*
* We don't count the first character of the path because a path like:
* /test-3/ would be counted as two directories but it's actually only one.
* Well it kind of is two but / is special and it would still trigger an
* absolute path warning - at least from a text file (tar strips it off) -
* because it starts with a '/'.
*
* Note that the path /test-3 would trigger a warning that it's not in the
* correct directory because it's an absolute directory. However since tar
* strips the initial '/'s this would probably not get flagged.
*
* Note also that if the tar output does not have a trailing '/' in a
* submission directory itself it would not count as another directory. However
* since we also check for more than one 'd' line in the output it would
* trigger more than one directory in the tarball.
*
* We keep track of two previous characters. The reason is that
* 'test-3/././file' should count as only one directory but previously
* (first version of this) it detected more than one directory because the
* '.' was not considered. Notice that the path 'test-3/.././file' will
* trigger both '../' in the path as well as more than one directory.
*/
first = file->filename[0];
prev = file->filename[1];
for (i = 1; file->filename[i]; ++i) {
if (file->filename[i] == '/' && prev != '/' && first != '.') {
++dir_count;
}
first = prev;
prev = file->filename[i];
}
if (dir_count > 1) {
++tarball.total_feathers;
warn("txzchk", "%s: found more than one directory in path %s", tarball_path, file->filename);
}
/*
* Now we have to run some tests on the directory name which we obtained
* from fnamchk earlier on - but only if fnamchk did not return an
* error! If it did we'll report other feathers/issues but we won't check
* directory names (at least the directory name expected in the
* tarball).
*/
if (dir_name != NULL && *dir_name != '\0')
{
if (strncmp(file->filename, dir_name, strlen(dir_name))) {
warn("txzchk", "%s: found incorrect directory in filename %s", tarball_path, file->filename);
++tarball.total_feathers;
} else {
/* This file is in the right directory */
tarball.correct_directory++;
}
}
}
/*
* 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
* dir_name - 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
* normal_file - 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())
*
* 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