-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrhcmds.c
1784 lines (1354 loc) · 46.4 KB
/
rhcmds.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
/*
* rawhide - find files using pretty C expressions
* https://raf.org/rawhide
* https://github.com/raforg/rawhide
* https://codeberg.org/raforg/rawhide
*
* Copyright (C) 1990 Ken Stauffer, 2022-2023 raf <raf@raf.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, see <https://www.gnu.org/licenses/>.
*
* 20231013 raf <raf@raf.org>
*/
#define _GNU_SOURCE /* For FNM_EXTMATCH and FNM_CASEFOLD in <fnmatch.h> */
#define _FILE_OFFSET_BITS 64 /* For 64-bit off_t on 32-bit systems (Not AIX) */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fnmatch.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_SYS_SYSMACROS
#include <sys/sysmacros.h>
#endif
#ifdef HAVE_SYS_MKDEV
#include <sys/mkdev.h>
#endif
#ifndef FNM_EXTMATCH
#define FNM_EXTMATCH 0
#endif
#ifdef HAVE_PCRE2
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#ifndef PCRE2_MATCH_INVALID_UTF
#define PCRE2_MATCH_INVALID_UTF 0
#endif
#endif
#ifdef HAVE_ACL
#include <sys/acl.h>
#endif
#if HAVE_LINUX_EA || HAVE_MACOS_EA
#include <sys/xattr.h>
#endif
#ifdef HAVE_FREEBSD_EA
#include <sys/extattr.h>
#endif
#ifdef HAVE_ATTR
#include <e2p/e2p.h>
#include <ext2fs/ext2_fs.h>
#endif
#include "rh.h"
#include "rhdir.h"
#include "rherr.h"
#include "rhstr.h"
/* Operators */
void c_le(llong i) { Stack[SP - 2] = Stack[SP - 2] <= Stack[SP - 1]; SP--; }
void c_lt(llong i) { Stack[SP - 2] = Stack[SP - 2] < Stack[SP - 1]; SP--; }
void c_ge(llong i) { Stack[SP - 2] = Stack[SP - 2] >= Stack[SP - 1]; SP--; }
void c_gt(llong i) { Stack[SP - 2] = Stack[SP - 2] > Stack[SP - 1]; SP--; }
void c_ne(llong i) { Stack[SP - 2] = Stack[SP - 2] != Stack[SP - 1]; SP--; }
void c_eq(llong i) { Stack[SP - 2] = Stack[SP - 2] == Stack[SP - 1]; SP--; }
void c_bitor(llong i) { Stack[SP - 2] = Stack[SP - 2] | Stack[SP - 1]; SP--; }
void c_bitand(llong i) { Stack[SP - 2] = Stack[SP - 2] & Stack[SP - 1]; SP--; }
void c_bitxor(llong i) { Stack[SP - 2] = Stack[SP - 2] ^ Stack[SP - 1]; SP--; }
void c_lshift(llong i) { Stack[SP - 2] = Stack[SP - 2] << Stack[SP - 1]; SP--; }
void c_rshift(llong i) { Stack[SP - 2] = Stack[SP - 2] >> Stack[SP - 1]; SP--; }
void c_plus(llong i) { Stack[SP - 2] = Stack[SP - 2] + Stack[SP - 1]; SP--; }
void c_minus(llong i) { Stack[SP - 2] = Stack[SP - 2] - Stack[SP - 1]; SP--; }
void c_mul(llong i) { Stack[SP - 2] = Stack[SP - 2] * Stack[SP - 1]; SP--; }
void check_zero(void) { if (Stack[SP - 1] == 0) fatal("attempt to divide by zero"); }
void c_mod(llong i) { check_zero(); Stack[SP - 2] = Stack[SP - 2] % Stack[SP - 1]; SP--; }
void c_div(llong i) { check_zero(); Stack[SP - 2] = Stack[SP - 2] / Stack[SP - 1]; SP--; }
void c_not(llong i) { Stack[SP - 1] = !Stack[SP - 1]; }
void c_bitnot(llong i) { Stack[SP - 1] = ~Stack[SP - 1]; }
void c_uniminus(llong i) { Stack[SP - 1] = -Stack[SP - 1]; }
void c_qm(llong i) { PC = (Stack[SP - 1]) ? PC : i; SP--; }
void c_colon(llong i) { PC = i; }
/* Functions */
void c_func(llong i)
{
Stack[SP++] = PC;
Stack[SP++] = FP;
PC = i;
FP = SP - (Program[PC].value + 2);
}
void c_return(llong i)
{
PC = Stack[SP - 3];
FP = Stack[SP - 2];
Stack[SP - (3 + i)] = Stack[SP - 1];
SP -= 2 + i;
}
void c_param(llong i) { Stack[SP++] = Stack[FP + i]; }
/* Count directory contents (excluding . and ..) */
static llong get_dirsize(struct stat *statbuf, const char *name)
{
int dir_fd;
DIR *dir;
struct dirent *entry;
if ((dir_fd = openat(attr.parent_fd, name, 0)) == -1)
return (llong)statbuf->st_size;
if (!(dir = fdopendir(dir_fd)))
{
close(dir_fd);
return (llong)statbuf->st_size;
}
statbuf->st_size = 0;
while ((entry = readdir(dir)))
{
if (entry->d_name[0] == '.' && (!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2])))
continue;
++statbuf->st_size;
}
closedir(dir);
return (llong)statbuf->st_size;
}
static llong dirsize(void)
{
char *name;
if (attr.dirsize_done)
return (llong)attr.statbuf->st_size;
attr.dirsize_done = 1;
name = (attr.parent_fd == AT_FDCWD) ? attr.fpath : attr.basename;
return get_dirsize(attr.statbuf, name);
}
static llong rdirsize(int i)
{
if (RefFile[i].dirsize_done)
return (llong)RefFile[i].statbuf->st_size;
RefFile[i].dirsize_done = 1;
return get_dirsize(RefFile[i].statbuf, Strbuf + RefFile[i].fpathi);
}
static llong tdirsize(void)
{
if (attr.linkdirsize_done)
return (llong)attr.linkstatbuf->st_size;
attr.linkdirsize_done = 1;
return get_dirsize(attr.linkstatbuf, attr.ftarget);
}
void set_dirsize(void)
{
if (isdir(attr.statbuf))
dirsize();
}
/* Built-ins */
void c_number(llong i) { Stack[SP++] = i; }
void c_dev(llong i) { Stack[SP++] = attr.statbuf->st_dev; }
void c_major(llong i) { Stack[SP++] = major(attr.statbuf->st_dev); }
void c_minor(llong i) { Stack[SP++] = minor(attr.statbuf->st_dev); }
void c_ino(llong i) { Stack[SP++] = attr.statbuf->st_ino; }
void c_mode(llong i) { Stack[SP++] = attr.statbuf->st_mode; }
void c_nlink(llong i) { Stack[SP++] = attr.statbuf->st_nlink; }
void c_uid(llong i) { Stack[SP++] = attr.statbuf->st_uid; }
void c_gid(llong i) { Stack[SP++] = attr.statbuf->st_gid; }
void c_rdev(llong i) { Stack[SP++] = attr.statbuf->st_rdev; }
void c_rmajor(llong i) { Stack[SP++] = major(attr.statbuf->st_rdev); }
void c_rminor(llong i) { Stack[SP++] = minor(attr.statbuf->st_rdev); }
void c_size(llong i) { Stack[SP++] = isdir(attr.statbuf) ? dirsize() : attr.statbuf->st_size; }
void c_blksize(llong i) { Stack[SP++] = attr.statbuf->st_blksize; }
void c_blocks(llong i) { Stack[SP++] = attr.statbuf->st_blocks; }
void c_atime(llong i) { Stack[SP++] = attr.statbuf->st_atime; }
void c_mtime(llong i) { Stack[SP++] = attr.statbuf->st_mtime; }
void c_ctime(llong i) { Stack[SP++] = attr.statbuf->st_ctime; }
void c_depth(llong i) { Stack[SP++] = attr.depth; }
void c_prune(llong i) { Stack[SP++] = 1; attr.prune = attr.pruned = 1; }
void c_trim(llong i) { Stack[SP++] = 1; attr.prune = 1; }
void c_exit(llong i) { Stack[SP++] = 1; attr.exit = 1; }
unsigned long get_attr(void)
{
if (!attr.attr_done)
{
attr.attr_done = 1;
#if HAVE_ATTR
fgetflags(attr.fpath, &attr.attr);
#elif HAVE_FLAGS
attr.attr = (unsigned long)attr.statbuf->st_flags;
#endif
}
return attr.attr;
}
unsigned long get_proj(void)
{
#ifdef HAVE_ATTR
if (!attr.proj_done)
{
attr.proj_done = 1;
fgetproject(attr.fpath, &attr.proj);
}
#endif
return attr.proj;
}
unsigned long get_gen(void)
{
#ifdef HAVE_ATTR
if (!attr.gen_done)
{
attr.gen_done = 1;
fgetversion(attr.fpath, &attr.gen);
}
#endif
return attr.gen;
}
#if HAVE_ATTR || HAVE_FLAGS
void c_attr(llong i) { Stack[SP++] = get_attr(); }
#endif
#if HAVE_ATTR
void c_proj(llong i) { Stack[SP++] = get_proj(); }
void c_gen(llong i) { Stack[SP++] = get_gen(); }
#endif
void c_strlen(llong i)
{
int len;
char *c;
for (len = 0, c = attr.fpath; *c; c++)
len = (*c == '/') ? 0 : len + 1;
Stack[SP++] = len;
}
void c_nouser(llong i)
{
Stack[SP++] = !getpwuid(attr.statbuf->st_uid);
}
void c_nogroup(llong i)
{
Stack[SP++] = !getgrgid(attr.statbuf->st_gid);
}
void c_readable(llong i)
{
if (attr.parent_fd != AT_FDCWD && attr.parent_fd != -1)
Stack[SP++] = (faccessat(attr.parent_fd, attr.basename, R_OK, 0) == 0);
else
Stack[SP++] = (access(attr.fpath, R_OK) == 0);
}
void c_writable(llong i)
{
if (attr.parent_fd != AT_FDCWD && attr.parent_fd != -1)
Stack[SP++] = (faccessat(attr.parent_fd, attr.basename, W_OK, 0) == 0);
else
Stack[SP++] = (access(attr.fpath, W_OK) == 0);
}
void c_executable(llong i)
{
if (attr.parent_fd != AT_FDCWD && attr.parent_fd != -1)
Stack[SP++] = (faccessat(attr.parent_fd, attr.basename, X_OK, 0) == 0);
else
Stack[SP++] = (access(attr.fpath, X_OK) == 0);
}
static char *c_basename(void)
{
char *tail;
tail = strrchr(attr.fpath, '/');
tail = (tail) ? tail + 1 : attr.fpath;
return tail;
}
/* Read the current candidate symlink target path */
char *read_symlink(void)
{
ssize_t nbytes = 0;
if (!attr.ftarget_done)
{
if (!attr.ftarget && !(attr.ftarget = malloc(attr.fpath_size)))
fatalsys("out of memory");
if (attr.test_readlinkat_failure)
errno = EPERM;
if (attr.test_readlinkat_failure || (nbytes = readlinkat(attr.parent_fd, (attr.parent_fd == AT_FDCWD) ? attr.fpath : attr.basename, attr.ftarget, attr.fpath_size)) == -1)
fatalsys("readlinkat %s", ok(attr.fpath));
if (attr.test_readlinkat_too_long_failure || nbytes == attr.fpath_size)
fatal("readlinkat %s: target is too long", ok(attr.fpath));
attr.ftarget[nbytes] = '\0';
attr.ftarget_done = 1;
while (--nbytes > 0 && attr.ftarget[nbytes] == '/')
attr.ftarget[nbytes] = '\0';
}
return attr.ftarget;
}
/*
Glob pattern matching.
The i parameter is an index into Strbuf.
The string contained there is the nul-terminated
string that is the pattern (e.g., "*.bak" without
the double quotes).
*/
void c_glob(llong i) { Stack[SP++] = fnmatch(&Strbuf[i], c_basename(), FNM_PATHNAME | FNM_EXTMATCH) == 0; }
void c_path(llong i) { Stack[SP++] = fnmatch(&Strbuf[i], attr.fpath, FNM_EXTMATCH) == 0; }
void c_link(llong i) { Stack[SP++] = (islink(attr.statbuf)) ? fnmatch(&Strbuf[i], read_symlink(), FNM_EXTMATCH) == 0 : 0; }
#ifdef FNM_CASEFOLD
void c_i(llong i) { Stack[SP++] = fnmatch(&Strbuf[i], c_basename(), FNM_PATHNAME | FNM_EXTMATCH | FNM_CASEFOLD) == 0; }
void c_ipath(llong i) { Stack[SP++] = fnmatch(&Strbuf[i], attr.fpath, FNM_EXTMATCH | FNM_CASEFOLD) == 0; }
void c_ilink(llong i) { Stack[SP++] = (islink(attr.statbuf)) ? fnmatch(&Strbuf[i], read_symlink(), FNM_EXTMATCH | FNM_CASEFOLD) == 0 : 0; }
#endif
#ifdef HAVE_PCRE2
/* Wrapper for pcre2_compile() that handles errors with a fatal message */
static pcre2_code *pcre2_compile_checked(const char *pattern, uint32_t options)
{
pcre2_code *re;
int error_number;
PCRE2_SIZE error_offset;
PCRE2_UCHAR error_buffer[256];
if (!(re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, options, &error_number, &error_offset, NULL)))
{
pcre2_get_error_message(error_number, error_buffer, sizeof error_buffer);
fatal("invalid regex %s at offset %d: %s", ok(pattern), (int)error_offset, ok2((char *)error_buffer));
}
return re;
}
/* Wrapper for pcre2_compile() that caches the last two compiled regexes. That should speed up most searches. */
static pcre2_code *pcre2_compile_cached(const char *regex, uint32_t options)
{
static const char *regex_cache[2];
static uint32_t options_cache[2];
static pcre2_code *code_cache[2];
static int mru;
/* Clear the cache when regex is null */
if (!regex)
{
if (code_cache[mru])
pcre2_code_free(code_cache[mru]);
regex_cache[mru] = NULL;
options_cache[mru] = 0;
code_cache[mru] = NULL;
mru = !mru;
if (code_cache[mru])
pcre2_code_free(code_cache[mru]);
regex_cache[mru] = NULL;
options_cache[mru] = 0;
code_cache[mru] = NULL;
return NULL;
}
/* Return the most recently used, if it matches */
if (regex_cache[mru] && code_cache[mru] && options == options_cache[mru] && !strcmp(regex, regex_cache[mru]))
return code_cache[mru];
/* Return the other, if it matches */
mru = !mru;
if (regex_cache[mru] && code_cache[mru] && options == options_cache[mru] && !strcmp(regex, regex_cache[mru]))
return code_cache[mru];
/* Cache a new regex (freeing an old one if necessary) */
if (code_cache[mru])
pcre2_code_free(code_cache[mru]);
regex_cache[mru] = regex;
options_cache[mru] = options;
code_cache[mru] = pcre2_compile_checked(regex, options);
return code_cache[mru];
}
/* Clear the pcre2 cache when finished */
void pcre2_cache_free(void)
{
pcre2_compile_cached(NULL, 0);
}
/* Perl-compatible regex matching (pcre2) */
static int rematch(const char *pattern, const char *subject, size_t subject_length, uint32_t options)
{
pcre2_code *re;
pcre2_match_data *match_data;
int rc;
if (attr.utf)
options |= PCRE2_UTF | PCRE2_MATCH_INVALID_UTF; /* Assume UTF-8 patterns and subject text */
if (attr.dotall_always)
options |= PCRE2_DOTALL; /* . matches anything including newline (like /s) */
if (attr.multiline_always)
options |= PCRE2_MULTILINE; /* ^ matches after every newline, $ matches before every newline (like /m) */
options |= PCRE2_DOLLAR_ENDONLY; /* $ matches only at the end of the subject */
options |= PCRE2_EXTENDED; /* Ignore whitespace and # comments (except in character classes) (like /x) */
options |= PCRE2_EXTENDED_MORE; /* Ignore space and tab inside character classes as well (like /xx) */
options |= PCRE2_NO_AUTO_CAPTURE; /* Prevent automatic numbered capturing parentheses (like /n) */
re = pcre2_compile_cached(pattern, options);
if (!(match_data = pcre2_match_data_create_from_pattern(re, NULL)))
fatalsys("out of memory");
if (subject_length == -1)
subject_length = strlen(subject);
rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)subject_length, 0, 0, match_data, NULL);
pcre2_match_data_free(match_data);
return rc >= 0;
}
void c_re(llong i) { Stack[SP++] = rematch(&Strbuf[i], c_basename(), -1, PCRE2_DOTALL); }
void c_repath(llong i) { Stack[SP++] = rematch(&Strbuf[i], attr.fpath, -1, PCRE2_DOTALL); }
void c_relink(llong i) { Stack[SP++] = (islink(attr.statbuf)) ? rematch(&Strbuf[i], read_symlink(), -1, PCRE2_DOTALL) : 0; }
void c_rei(llong i) { Stack[SP++] = rematch(&Strbuf[i], c_basename(), -1, PCRE2_DOTALL | PCRE2_CASELESS); }
void c_reipath(llong i) { Stack[SP++] = rematch(&Strbuf[i], attr.fpath, -1, PCRE2_DOTALL | PCRE2_CASELESS); }
void c_reilink(llong i) { Stack[SP++] = (islink(attr.statbuf)) ? rematch(&Strbuf[i], read_symlink(), -1, PCRE2_DOTALL | PCRE2_CASELESS) : 0; }
#endif /* HAVE_PCRE2 */
/* Load the file's content into attr.body (deallocated at exit) */
char *get_body(void)
{
if (!isreg(attr.statbuf))
return NULL;
if (!attr.body_done)
{
attr.body_length = 0;
int fd;
if ((fd = open(attr.fpath, O_RDONLY)) == -1)
{
errorsys("%s", ok(attr.fpath));
attr.exit_status = EXIT_FAILURE;
}
else
{
/* Increase the buffer size when necessary (include space for a nul byte) */
if (attr.body_size < attr.statbuf->st_size + 1)
{
if (!(attr.body = realloc(attr.body, attr.statbuf->st_size + 1)))
fatalsys("out of memory");
attr.body_size = attr.statbuf->st_size + 1;
}
/* Read the file */
ssize_t bytes = 0;
#define READ_MAX 0x7ffff000 /* Limit on Linux, also needed on macOS */
while (attr.body_length < attr.statbuf->st_size && (bytes = read(fd, attr.body + attr.body_length, (attr.statbuf->st_size - attr.body_length > READ_MAX) ? READ_MAX : (attr.statbuf->st_size - attr.body_length))) > 0)
attr.body_length += bytes;
if (bytes == -1)
{
errorsys("read %s", ok(attr.fpath));
attr.exit_status = EXIT_FAILURE;
}
close(fd);
/* Nul-terminate the buffer for fnmatch() */
attr.body[attr.body_length] = '\0';
}
attr.body_done = 1;
}
return attr.body;
}
static void body_glob(llong i, int options)
{
Stack[SP++] = get_body() ? fnmatch(&Strbuf[i], get_body(), FNM_EXTMATCH | options) == 0 : 0;
}
void c_body(llong i) { body_glob(i, 0); }
#ifdef FNM_CASEFOLD
void c_ibody(llong i) { body_glob(i, FNM_CASEFOLD); }
#endif
#ifdef HAVE_PCRE2
static void body_re(llong i, int options)
{
Stack[SP++] = get_body() ? rematch(&Strbuf[i], get_body(), attr.body_length, PCRE2_MULTILINE | options) : 0;
}
void c_rebody(llong i) { body_re(i, 0); }
void c_reibody(llong i) { body_re(i, PCRE2_CASELESS); }
#endif
/* Load the file type description into attr.what (libmagic-managed data - deallocated at exit) */
const char *get_what(void)
{
if (!attr.what_done)
{
#ifdef HAVE_MAGIC
if (following_symlinks())
{
if (!attr.what_follow_cookie)
if ((attr.what_follow_cookie = magic_open(MAGIC_RAW | MAGIC_COMPRESS | MAGIC_SYMLINK)))
(void)magic_load(attr.what_follow_cookie, NULL);
if (attr.what_follow_cookie)
attr.what = magic_file(attr.what_follow_cookie, attr.fpath);
}
else
{
if (!attr.what_cookie)
if ((attr.what_cookie = magic_open(MAGIC_RAW | MAGIC_COMPRESS)))
(void)magic_load(attr.what_cookie, NULL);
if (attr.what_cookie)
attr.what = magic_file(attr.what_cookie, attr.fpath);
}
#endif
attr.what_done = 1;
}
return attr.what;
}
#ifdef HAVE_MAGIC
static void what_glob(llong i, int options)
{
Stack[SP++] = get_what() ? fnmatch(&Strbuf[i], get_what(), FNM_EXTMATCH | options) == 0 : 0;
}
void c_what(llong i) { what_glob(i, 0); }
#ifdef FNM_CASEFOLD
void c_iwhat(llong i) { what_glob(i, FNM_CASEFOLD); }
#endif
#ifdef HAVE_PCRE2
static void what_re(llong i, int options)
{
Stack[SP++] = get_what() ? rematch(&Strbuf[i], get_what(), -1, PCRE2_MULTILINE | options) : 0;
}
void c_rewhat(llong i) { what_re(i, 0); }
void c_reiwhat(llong i) { what_re(i, PCRE2_CASELESS); }
#endif
#endif
/* Load the MIME type into attr.what (libmagic-managed data - deallocated at exit) */
const char *get_mime(void)
{
if (!attr.mime_done)
{
#ifdef HAVE_MAGIC
if (following_symlinks())
{
if (!attr.mime_follow_cookie)
if ((attr.mime_follow_cookie = magic_open(MAGIC_MIME | MAGIC_SYMLINK)))
(void)magic_load(attr.mime_follow_cookie, NULL);
if (attr.mime_follow_cookie)
attr.mime = magic_file(attr.mime_follow_cookie, attr.fpath);
}
else
{
if (!attr.mime_cookie)
if ((attr.mime_cookie = magic_open(MAGIC_MIME)))
(void)magic_load(attr.mime_cookie, NULL);
if (attr.mime_cookie)
attr.mime = magic_file(attr.mime_cookie, attr.fpath);
}
#endif
attr.mime_done = 1;
}
return attr.mime;
}
#ifdef HAVE_MAGIC
static void mime_glob(llong i, int options)
{
Stack[SP++] = get_mime() ? fnmatch(&Strbuf[i], get_mime(), FNM_EXTMATCH | options) == 0 : 0;
}
void c_mime(llong i) { mime_glob(i, 0); }
#ifdef FNM_CASEFOLD
void c_imime(llong i) { mime_glob(i, FNM_CASEFOLD); }
#endif
#ifdef HAVE_PCRE2
static void mime_re(llong i, int options)
{
Stack[SP++] = get_mime() ? rematch(&Strbuf[i], get_mime(), -1, PCRE2_DOTALL | options) : 0;
}
void c_remime(llong i) { mime_re(i, 0); }
void c_reimime(llong i) { mime_re(i, PCRE2_CASELESS); }
#endif
#endif
/* Load ACLs into attr.facl (and attr.facl_verbose on FreeBSD/Solaris) which must be deallocated */
#define failure(args) if (want) { errorsys args; attr.exit_status = EXIT_FAILURE; }
char *get_acl(int want)
{
if (!attr.facl_done)
{
/* The two main choices for ACL APIs are "POSIX" and SOLARIS */
#ifdef HAVE_POSIX_ACL
/* macOS has the "POSIX" ACL API, but has its own type: ACL_TYPE_EXTENDED */
#ifdef HAVE_MACOS_ACL
int acl_flags = ACL_TYPE_EXTENDED;
#else
int acl_flags = ACL_TYPE_ACCESS;
#endif
/* Get the "POSIX" or macOS (but not NFSv4) ACL */
acl_t acl = acl_get_file(attr.fpath, acl_flags);
/* FreeBSD fails above with EINVAL when an NFSv4 ACL is present, so try that */
#ifdef HAVE_FREEBSD_ACL
if (!acl && errno == EINVAL)
acl = acl_get_file(attr.fpath, ACL_TYPE_NFS4);
#endif
#ifdef HAVE_MACOS_ACL
if (!acl && errno && errno != ENOENT) /* macOS does this when there are no ACLs */
failure(("acl_get_file %s", ok(attr.fpath)))
#else
if (!acl && errno && errno != EOPNOTSUPP && errno != ENOTSUP) /* FreeBSD does EOPNOTSUPP for char devices */
failure(("acl_get_file %s", ok(attr.fpath)))
#endif
/* Convert the ACL to text */
if (acl)
{
if (!(attr.facl = acl_to_text(acl, NULL)) && errno)
failure(("acl_to_text %s", ok(attr.fpath)))
/* On FreeBSD, NFSv4 ACLs have compact and verbose formats (like Solaris) */
#ifdef HAVE_FREEBSD_ACL
if (!(attr.facl_verbose = acl_to_text_np(acl, NULL, ACL_TEXT_VERBOSE)) && errno)
failure(("acl_to_text_np %s", ok(attr.fpath)))
#endif
acl_free(acl);
}
#endif /* HAVE_POSIX_ACL */
#ifdef HAVE_SOLARIS_ACL
acl_t *acl = NULL;
int acl_flags = (attr.facl_solaris_no_trivial ? ACL_NO_TRIVIAL : 0);
if (acl_get(attr.fpath, acl_flags, &acl) != -1 && acl)
{
if (!(attr.facl = acl_totext(acl, ACL_COMPACT_FMT | ACL_SID_FMT)) && errno)
failure(("acl_totext %s", ok(attr.fpath)))
if (!(attr.facl_verbose = acl_totext(acl, ACL_SID_FMT)) && errno)
failure(("acl_totext %s", ok(attr.fpath)))
acl_free(acl);
}
else if (errno)
failure(("acl_get %s", ok(attr.fpath)))
#endif /* HAVE_SOLARIS_ACL */
attr.facl_done = 1;
}
return attr.facl;
}
#ifdef HAVE_ACL
static void acl_glob(llong i, int options)
{
Stack[SP++] = get_acl(1)
? fnmatch(&Strbuf[i], get_acl(1), FNM_EXTMATCH | options) == 0 ||
(attr.facl_verbose && fnmatch(&Strbuf[i], attr.facl_verbose, FNM_EXTMATCH | options) == 0)
: 0;
}
void c_acl(llong i) { acl_glob(i, 0); }
#ifdef FNM_CASEFOLD
void c_iacl(llong i) { acl_glob(i, FNM_CASEFOLD); }
#endif
#ifdef HAVE_PCRE2
static void acl_re(llong i, int options)
{
Stack[SP++] = get_acl(1)
? rematch(&Strbuf[i], get_acl(1), -1, PCRE2_MULTILINE | options) ||
(attr.facl_verbose && rematch(&Strbuf[i], attr.facl_verbose, -1, PCRE2_MULTILINE | options))
: 0;
}
void c_reacl(llong i) { acl_re(i, 0); }
void c_reiacl(llong i) { acl_re(i, PCRE2_CASELESS); }
#endif
#endif
/* Load EAs into attr.fea which must be deallocated eventually */
char *get_ea(int want)
{
if (!attr.fea_done)
{
#if HAVE_LINUX_EA || HAVE_MACOS_EA
ssize_t buflen, vallen, rc;
char *buf, *name, *val;
size_t namelen;
llong fea_size;
int i, pos = 0;
attr.fea_done = 1;
/* Allocate a long-lived buffer (freed at the end of rawhide_search()) */
fea_size = (attr.fea_size) ? attr.fea_size : 4 * 1024;
if (!attr.fea && !(attr.fea = malloc(fea_size)))
{
errorsys("out of memory");
attr.exit_status = EXIT_FAILURE;
return NULL;
}
/* Handle the extended attributes changing while we're looking */
for (i = 0; i < 5; ++i)
{
/* Get the size of the list of names */
#if HAVE_LINUX_EA
if ((buflen = ((following_symlinks()) ? listxattr : llistxattr)(attr.fpath, NULL, 0)) == -1)
#elif HAVE_MACOS_EA
if ((buflen = listxattr(attr.fpath, NULL, 0, (following_symlinks()) ? 0 : XATTR_NOFOLLOW)) == -1)
#endif
{
if (errno != EPERM && errno != ENOTSUP && errno != EACCES) /* macOS + chardev = EPERM, Cygwin + chardev = ENOTSUP, Cygwin + symlink = EACCES */
failure(("listxattr %s", ok(attr.fpath)))
return NULL;
}
/* If there are none, stop */
if (buflen == 0)
return NULL;
/* Allocate space for the list of names */
if (!(buf = malloc(buflen)))
{
errorsys("out of memory");
attr.exit_status = EXIT_FAILURE;
return NULL;
}
/* Get the list of names, retry if the size got bigger */
#if HAVE_LINUX_EA
if ((rc = ((following_symlinks()) ? listxattr : llistxattr)(attr.fpath, buf, (size_t)buflen)) == -1 && errno == ERANGE)
#elif HAVE_MACOS_EA
if ((rc = listxattr(attr.fpath, buf, (size_t)buflen, (following_symlinks()) ? 0 : XATTR_NOFOLLOW)) == -1 && errno == ERANGE)
#endif
{
free(buf);
buf = NULL;
continue;
}
/* Handle any other error */
if (rc == -1)
{
failure(("listxattr %s", ok(attr.fpath)))
free(buf);
return NULL;
}
/* Success */
break;
}
/* Give up after five attempts */
if (i == 5)
{
failure(("listxattr %s: size keeps changing", ok(attr.fpath)))
if (buf)
free(buf);
return NULL;
}
/* For each name, get its value (if any) */
for (name = buf; buflen > 0; namelen = strlen(name) + 1, buflen -= namelen, name += namelen)
{
/* Maybe the name list size got smaller above and we couldn't detect it. */
/* This might or might not handle that case. */
if (!*name)
break;
/* Add the name */
pos += cescape(attr.fea + pos, fea_size - pos, name, -1, CESCAPE_HEX);
/* Don't consider Linux ACL or selinux EAs to be "real" EAs (for -l) */
if (strcmp(name, "system.posix_acl_access") && strcmp(name, "system.posix_acl_default") && strcmp(name, "security.selinux"))
attr.fea_real = 1;
/* Handle the value changing while we're looking */
for (i = 0; i < 5; ++i)
{
/* Get the size of the value */
#if HAVE_LINUX_EA
if ((vallen = ((following_symlinks()) ? getxattr : lgetxattr)(attr.fpath, name, NULL, 0)) == -1)
#elif HAVE_MACOS_EA
if ((vallen = getxattr(attr.fpath, name, NULL, 0, 0, (following_symlinks()) ? 0 : XATTR_NOFOLLOW)) == -1)
#endif
{
failure(("getxattr %s name %s", ok(attr.fpath), ok2(name)))
free(buf);
return NULL;
}
/* If there is none, move on to the next name */
if (vallen == 0)
break;
/* Allocate space for the value */
if (!(val = malloc(vallen + 1)))
{
errorsys("out of memory");
attr.exit_status = EXIT_FAILURE;
free(buf);
return NULL;
}
/* Get the value, retry if the size got bigger */
#if HAVE_LINUX_EA
if ((rc = ((following_symlinks()) ? getxattr : lgetxattr)(attr.fpath, name, val, (size_t)vallen)) == -1 && errno == ERANGE)
#elif HAVE_MACOS_EA
if ((rc = getxattr(attr.fpath, name, val, (size_t)vallen, 0, (following_symlinks()) ? 0 : XATTR_NOFOLLOW)) == -1 && errno == ERANGE)
#endif
{
free(val);
val = NULL;
continue;
}
/* Handle any other error */