forked from rpm-software-management/rpmlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesCheck.py
1390 lines (1179 loc) · 55.9 KB
/
FilesCheck.py
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
# -*- coding: utf-8 -*-
#############################################################################
# File : FilesCheck.py
# Package : rpmlint
# Author : Frederic Lepied
# Created on : Mon Oct 4 19:32:49 1999
# Purpose : test various aspects on files: locations, owner, groups,
# permission, setuid, setgid...
#############################################################################
from datetime import datetime
import os
import re
import stat
import rpm
import AbstractCheck
import Config
from Filter import addDetails, printError, printWarning
from Pkg import b2s, catcmd, getstatusoutput, is_utf8, is_utf8_bytestr, shquote
# must be kept in sync with the filesystem package
STANDARD_DIRS = (
'/',
'/bin',
'/boot',
'/etc',
'/etc/X11',
'/etc/opt',
'/etc/profile.d',
'/etc/skel',
'/etc/xinetd.d',
'/home',
'/lib',
'/lib/modules',
'/lib64',
'/media',
'/mnt',
'/mnt/cdrom',
'/mnt/disk',
'/mnt/floppy',
'/opt',
'/proc',
'/root',
'/run',
'/sbin',
'/selinux',
'/srv',
'/sys',
'/tmp',
'/usr',
'/usr/X11R6',
'/usr/X11R6/bin',
'/usr/X11R6/doc',
'/usr/X11R6/include',
'/usr/X11R6/lib',
'/usr/X11R6/lib64',
'/usr/X11R6/man',
'/usr/X11R6/man/man1',
'/usr/X11R6/man/man2',
'/usr/X11R6/man/man3',
'/usr/X11R6/man/man4',
'/usr/X11R6/man/man5',
'/usr/X11R6/man/man6',
'/usr/X11R6/man/man7',
'/usr/X11R6/man/man8',
'/usr/X11R6/man/man9',
'/usr/X11R6/man/mann',
'/usr/bin',
'/usr/bin/X11',
'/usr/etc',
'/usr/games',
'/usr/include',
'/usr/lib',
'/usr/lib/X11',
'/usr/lib/games',
'/usr/lib/gcc-lib',
'/usr/lib/menu',
'/usr/lib64',
'/usr/lib64/gcc-lib',
'/usr/local',
'/usr/local/bin',
'/usr/local/doc',
'/usr/local/etc',
'/usr/local/games',
'/usr/local/info',
'/usr/local/lib',
'/usr/local/lib64',
'/usr/local/man',
'/usr/local/man/man1',
'/usr/local/man/man2',
'/usr/local/man/man3',
'/usr/local/man/man4',
'/usr/local/man/man5',
'/usr/local/man/man6',
'/usr/local/man/man7',
'/usr/local/man/man8',
'/usr/local/man/man9',
'/usr/local/man/mann',
'/usr/local/sbin',
'/usr/local/share',
'/usr/local/share/man',
'/usr/local/share/man/man1',
'/usr/local/share/man/man2',
'/usr/local/share/man/man3',
'/usr/local/share/man/man4',
'/usr/local/share/man/man5',
'/usr/local/share/man/man6',
'/usr/local/share/man/man7',
'/usr/local/share/man/man8',
'/usr/local/share/man/man9',
'/usr/local/share/man/mann',
'/usr/local/src',
'/usr/sbin',
'/usr/share',
'/usr/share/dict',
'/usr/share/doc',
'/usr/share/icons',
'/usr/share/info',
'/usr/share/man',
'/usr/share/man/man1',
'/usr/share/man/man2',
'/usr/share/man/man3',
'/usr/share/man/man4',
'/usr/share/man/man5',
'/usr/share/man/man6',
'/usr/share/man/man7',
'/usr/share/man/man8',
'/usr/share/man/man9',
'/usr/share/man/mann',
'/usr/share/misc',
'/usr/src',
'/usr/tmp',
'/var',
'/var/cache',
'/var/db',
'/var/lib',
'/var/lib/games',
'/var/lib/misc',
'/var/lib/rpm',
'/var/local',
'/var/log',
'/var/mail',
'/var/nis',
'/var/opt',
'/var/preserve',
'/var/spool',
'/var/spool/mail',
'/var/tmp',
)
DEFAULT_GAMES_GROUPS = 'Games'
DEFAULT_DANGLING_EXCEPTIONS = (
['consolehelper$', 'usermode-consoleonly'],
)
# Standard users and groups from LSB Core 4.0.0: 21.2 User & Group Names
DEFAULT_STANDARD_USERS = ('root', 'bin', 'daemon', 'adm', 'lp', 'sync',
'shutdown', 'halt', 'mail', 'news', 'uucp',
'operator', 'man', 'nobody',)
DEFAULT_STANDARD_GROUPS = ('root', 'bin', 'daemon', 'adm', 'lp', 'sync',
'shutdown', 'halt', 'mail', 'news', 'uucp',
'man', 'nobody',)
DEFAULT_DISALLOWED_DIRS = (
'/home',
'/mnt',
'/opt',
'/tmp',
'/usr/local',
'/usr/tmp',
'/var/local',
'/var/lock',
'/var/run',
'/var/tmp',
)
sub_bin_regex = re.compile(r'^(/usr)?/s?bin/\S+/')
backup_regex = re.compile(r'(~|\#[^/]+\#|\.orig|\.rej)$')
compr_regex = re.compile(r'\.(gz|z|Z|zip|bz2|lzma|xz)$')
absolute_regex = re.compile(r'^/([^/]+)')
absolute2_regex = re.compile(r'^/?([^/]+)')
points_regex = re.compile(r'^\.\./(.*)')
doc_regex = re.compile(r'^/usr(/share|/X11R6)?/(doc|man|info)/')
bin_regex = re.compile(r'^/(?:usr/(?:s?bin|games)|s?bin)/(.*)')
includefile_regex = re.compile(r'\.(c|h)(pp|xx)?$', re.IGNORECASE)
develfile_regex = re.compile(r'\.(a|cmxa?|mli?|gir)$')
buildconfigfile_regex = re.compile(r'(\.pc|/bin/.+-config)$')
# room for improvement with catching more -R, but also for false positives...
buildconfig_rpath_regex = re.compile(r'(?:-rpath|Wl,-R)\b')
sofile_regex = re.compile(r'/lib(64)?/(.+/)?lib[^/]+\.so$')
devel_regex = re.compile(r'(.*)-(debug(info)?|devel|headers|source|static)$')
debuginfo_package_regex = re.compile(r'-debug(info)?$')
lib_regex = re.compile(r'lib(64)?/lib[^/]*(\.so\..*|-[0-9.]+\.so)')
ldconfig_regex = re.compile(r'^[^#]*ldconfig', re.MULTILINE)
depmod_regex = re.compile(r'^[^#]*depmod', re.MULTILINE)
install_info_regex = re.compile(r'^[^#]*install-info', re.MULTILINE)
perl_temp_file_regex = re.compile(r'.*perl.*/(\.packlist|perllocal\.pod)$')
scm_regex = re.compile(r'/CVS/[^/]+$|/\.(bzr|cvs|git|hg)ignore$|/\.hgtags$|/\.(bzr|git|hg|svn)/|/(\.arch-ids|{arch})/')
games_path_regex = re.compile(r'^/usr(/lib(64)?)?/games/')
games_group_regex = re.compile(Config.getOption('RpmGamesGroups', DEFAULT_GAMES_GROUPS))
dangling_exceptions = Config.getOption('DanglingSymlinkExceptions', DEFAULT_DANGLING_EXCEPTIONS)
logrotate_regex = re.compile(r'^/etc/logrotate\.d/(.*)')
module_rpms_ok = Config.getOption('KernelModuleRPMsOK', True)
kernel_modules_regex = re.compile(r'^(?:/usr)/lib/modules/([0-9]+\.[0-9]+\.[0-9]+[^/]*?)/')
kernel_package_regex = re.compile(r'^kernel(22)?(-)?(smp|enterprise|bigmem|secure|BOOT|i686-up-4GB|p3-smp-64GB)?')
normal_zero_length_regex = re.compile(r'^/etc/security/console\.apps/|/\.nosearch$|/__init__\.py$')
perl_regex = re.compile(r'^/usr/lib/perl5/(?:vendor_perl/)?([0-9]+\.[0-9]+)\.([0-9]+)/')
python_regex = re.compile(r'^/usr/lib(?:64)?/python([.0-9]+)/')
python_bytecode_regex_pep3147 = re.compile(r'^(.*)/__pycache__/(.*?)\.([^.]+)(\.opt-[12])?\.py[oc]$')
python_bytecode_regex = re.compile(r'^(.*)(\.py[oc])$')
python_default_version = Config.getOption('PythonDefaultVersion', None)
perl_version_trick = Config.getOption('PerlVersionTrick', True)
log_regex = re.compile(r'^/var/log/[^/]+$')
lib_path_regex = re.compile(r'^(/usr(/X11R6)?)?/lib(64)?')
lib_package_regex = re.compile(r'^(lib|.+-libs)')
hidden_file_regex = re.compile(r'/\.[^/]*$')
manifest_perl_regex = re.compile(r'^/usr/share/doc/perl-.*/MANIFEST(\.SKIP)?$')
shebang_regex = re.compile(br'^#!\s*(\S+)(.*?)$', re.M)
interpreter_regex = re.compile(r'^/(?:usr/)?(?:s?bin|games|libexec(?:/.+)?|(?:lib(?:64)?|share)/.+)/([^/]+)$')
script_regex = re.compile(r'^/((usr/)?s?bin|etc/(rc\.d/init\.d|X11/xinit\.d|cron\.(hourly|daily|monthly|weekly)))/')
sourced_script_regex = re.compile(r'^/etc/(bash_completion\.d|profile\.d)/')
use_utf8 = Config.getOption('UseUTF8', Config.USEUTF8_DEFAULT)
skipdocs_regex = re.compile(Config.getOption('SkipDocsRegexp', r'\.(?:rtf|x?html?|svg|ml[ily]?)$'), re.IGNORECASE)
meta_package_regex = re.compile(Config.getOption('MetaPackageRegexp', r'^(bundle|task)-'))
filesys_packages = ['filesystem'] # TODO: make configurable?
quotes_regex = re.compile(r'[\'"]+')
start_certificate_regex = re.compile(r'^-----BEGIN CERTIFICATE-----$')
start_private_key_regex = re.compile(r'^----BEGIN PRIVATE KEY-----$')
for idx in range(0, len(dangling_exceptions)):
dangling_exceptions[idx][0] = re.compile(dangling_exceptions[idx][0])
del idx
use_relative_symlinks = Config.getOption("UseRelativeSymlinks", True)
standard_groups = Config.getOption('StandardGroups', DEFAULT_STANDARD_GROUPS)
standard_users = Config.getOption('StandardUsers', DEFAULT_STANDARD_USERS)
disallowed_dirs = Config.getOption('DisallowedDirs', DEFAULT_DISALLOWED_DIRS)
non_readable_regexs = (re.compile(r'^/var/log/'),
re.compile(r'^/etc/(g?shadow-?|securetty)$'))
man_base_regex = re.compile(r'^/usr(?:/share)?/man(?:/overrides)?/man[^/]+/(.+)\.[1-9n]')
man_warn_regex = re.compile(r'^([^:]+:)\d+:\s*')
man_nowarn_regex = re.compile(
# From Lintian: ignore common undefined macros from pod2man << Perl 5.10
r'\`(Tr|IX)\' not defined|'
# .so entries won't resolve as we're dealing with stdin
r'No such file or directory|'
# TODO, better handling for these (see e.g. Lintian)
r'(can\'t break|cannot adjust) line')
man_warn_category = Config.getOption('ManWarningCategory', 'mac')
fsf_license_regex = re.compile(br'(GNU((\s+(Library|Lesser|Affero))?(\s+General)?\s+Public|\s+Free\s+Documentation)\s+Licen[cs]e|(GP|FD)L)', re.IGNORECASE)
fsf_wrong_address_regex = re.compile(br'(675\s+Mass\s+Ave|59\s+Temple\s+Place|Franklin\s+Steet|02139|02111-1307)', re.IGNORECASE)
scalable_icon_regex = re.compile(r'^/usr(?:/local)?/share/icons/.*/scalable/')
# "is binary" stuff borrowed from https://pypi.python.org/pypi/binaryornot
# TODO: switch to it sometime later instead of embedding our own copy
printable_extended_ascii = b'\n\r\t\f\b'
if bytes is str:
# Python 2 means we need to invoke chr() explicitly
printable_extended_ascii += b''.join(map(chr, range(32, 256)))
else:
# Python 3 means bytes accepts integer input directly
printable_extended_ascii += bytes(range(32, 256))
def peek(filename, pkg, length=1024):
"""
Peek into a file, return a chunk from its beginning and a flag if it
seems to be a text file.
"""
chunk = None
try:
with open(filename, 'rb') as fobj:
chunk = fobj.read(length)
except IOError as e: # eg. https://bugzilla.redhat.com/209876
printWarning(pkg, 'read-error', e)
return (chunk, False)
if b'\0' in chunk:
return (chunk, False)
if not chunk: # Empty files are considered text
return (chunk, True)
fl = filename.lower()
# PDF's are binary but often detected as text by the algorithm below
if fl.endswith('.pdf') and chunk.startswith(b'%PDF-'):
return (chunk, False)
# Ditto RDoc RI files
if fl.endswith('.ri') and '/ri/' in fl:
return (chunk, False)
# Binary if control chars are > 30% of the string
control_chars = chunk.translate(None, printable_extended_ascii)
nontext_ratio = float(len(control_chars)) / float(len(chunk))
istext = nontext_ratio <= 0.30
return (chunk, istext)
# See Python sources for a full list of the values here.
# https://github.com/python/cpython/blob/master/Lib/importlib/_bootstrap_external.py
# https://github.com/python/cpython/blob/2.7/Python/import.c
# https://github.com/python/cpython/commit/93602e3af70d3b9f98ae2da654b16b3382b68d50
_python_magic_values = {
'2.2': [60717],
'2.3': [62011],
'2.4': [62061],
'2.5': [62131],
'2.6': [62161],
'2.7': [62211],
'3.0': [3130],
'3.1': [3150],
'3.2': [3180],
'3.3': [3230],
'3.4': [3310],
'3.5': [3350, 3351], # 3350 for < 3.5.2
'3.6': [3379],
'3.7': [3390],
}
def get_expected_pyc_magic(path):
"""
.pyc/.pyo files embed a 4-byte magic value identifying which version of
the python bytecode ABI they are for. Given a path to a .pyc/.pyo file,
return a (magic ABI values, python version) tuple. For example,
'/usr/lib/python3.1/foo.pyc' should return (3151, '3.1').
The first value will be None if the python version was not resolved
from the given pathname and the PythonDefaultVersion configuration
variable is not set, or if we don't know the magic ABI values for the
python version (no matter from which source the version came from).
The second value will be None if a python version could not be resolved
from the given pathname.
"""
ver_from_path = None
m = python_regex.search(path)
if m:
ver_from_path = m.group(1)
expected_version = ver_from_path or python_default_version
expected_magic_values = _python_magic_values.get(expected_version)
if not expected_magic_values:
return (None, ver_from_path)
# In Python 2, if Py_UnicodeFlag is set, Python's import code uses a value
# one higher, but this is off by default. In Python 3.0 and 3.1 (but no
# longer in 3.2), it always uses the value one higher:
if expected_version[:3] in ('3.0', '3.1'):
expected_magic_values = [x + 1 for x in expected_magic_values]
return (expected_magic_values, ver_from_path)
def py_demarshal_long(b):
"""
Counterpart to Python's PyMarshal_ReadLongFromFile, operating on the
bytes in a string.
"""
if isinstance(b, str):
b = map(ord, b)
return (b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24))
def python_bytecode_to_script(path):
"""
Given a python bytecode path, give the path of the .py file
(or None if not python bytecode).
"""
res = python_bytecode_regex_pep3147.search(path)
if res:
return res.group(1) + '/' + res.group(2) + '.py'
res = python_bytecode_regex.search(path)
if res:
return res.group(1) + '.py'
return None
def script_interpreter(chunk):
res = shebang_regex.search(chunk) if chunk else None
return (b2s(res.group(1)), b2s(res.group(2)).strip()) \
if res and res.start() == 0 else (None, "")
class FilesCheck(AbstractCheck.AbstractCheck):
def __init__(self):
AbstractCheck.AbstractCheck.__init__(self, 'FilesCheck')
def check(self, pkg):
if use_utf8:
for filename in pkg.header[rpm.RPMTAG_FILENAMES] or ():
if not is_utf8_bytestr(filename):
printError(pkg, 'filename-not-utf8', b2s(filename))
# Rest of the checks are for binary packages only
if pkg.isSource():
return
files = pkg.files()
# Check if the package is a development package
devel_pkg = devel_regex.search(pkg.name)
config_files = pkg.configFiles()
ghost_files = pkg.ghostFiles()
doc_files = pkg.docFiles()
req_names = pkg.req_names()
lib_package = lib_package_regex.search(pkg.name)
is_kernel_package = kernel_package_regex.search(pkg.name)
debuginfo_package = debuginfo_package_regex.search(pkg.name)
# report these errors only once
perl_dep_error = False
python_dep_error = False
lib_file = False
non_lib_file = None
log_files = []
logrotate_file = False
debuginfo_srcs = False
debuginfo_debugs = False
if not doc_files:
printWarning(pkg, 'no-documentation')
if files:
if meta_package_regex.search(pkg.name):
printWarning(pkg, 'file-in-meta-package')
elif debuginfo_package:
printError(pkg, 'empty-debuginfo-package')
# Prefetch scriptlets, strip quotes from them (#169)
postin = pkg[rpm.RPMTAG_POSTIN] or \
pkg.scriptprog(rpm.RPMTAG_POSTINPROG)
if postin:
postin = quotes_regex.sub('', postin)
postun = pkg[rpm.RPMTAG_POSTUN] or \
pkg.scriptprog(rpm.RPMTAG_POSTUNPROG)
if postun:
postun = quotes_regex.sub('', postun)
# Unique (rdev, inode) combinations
hardlinks = {}
# All executable files from standard bin dirs (basename => [paths])
# Hack: basenames with empty paths links are symlinks (not subject
# to duplicate binary check, but yes for man page existence check)
bindir_exes = {}
# All man page "base" names (without section etc extensions)
man_basenames = set()
for f, pkgfile in files.items():
mode = pkgfile.mode
user = pkgfile.user
group = pkgfile.group
link = pkgfile.linkto
size = pkgfile.size
rdev = pkgfile.rdev
inode = pkgfile.inode
is_doc = f in doc_files
nonexec_file = False
for match in AbstractCheck.macro_regex.findall(f):
printWarning(pkg, 'unexpanded-macro', f, match)
if standard_users and user not in standard_users:
printWarning(pkg, 'non-standard-uid', f, user)
if standard_groups and group not in standard_groups:
printWarning(pkg, 'non-standard-gid', f, group)
if not module_rpms_ok and kernel_modules_regex.search(f) and not \
is_kernel_package:
printError(pkg, "kernel-modules-not-in-kernel-packages", f)
for i in disallowed_dirs:
if f.startswith(i):
printError(pkg, 'dir-or-file-in-%s' %
'-'.join(i.split('/')[1:]), f)
if f.startswith('/run/'):
if f not in ghost_files:
printWarning(pkg, 'non-ghost-in-run', f)
elif sub_bin_regex.search(f):
printError(pkg, 'subdir-in-bin', f)
elif '/site_perl/' in f:
printWarning(pkg, 'siteperl-in-perl-module', f)
if backup_regex.search(f):
printError(pkg, 'backup-file-in-package', f)
elif scm_regex.search(f):
printError(pkg, 'version-control-internal-file', f)
elif f.endswith('/.htaccess'):
printError(pkg, 'htaccess-file', f)
elif hidden_file_regex.search(f) and not f.startswith("/etc/skel/"):
printWarning(pkg, 'hidden-file-or-dir', f)
elif manifest_perl_regex.search(f):
printWarning(pkg, 'manifest-in-perl-module', f)
elif f == '/usr/info/dir' or f == '/usr/share/info/dir':
printError(pkg, 'info-dir-file', f)
res = logrotate_regex.search(f)
if res:
logrotate_file = True
if res.group(1) != pkg.name:
printError(pkg, 'incoherent-logrotate-file', f)
if link != '':
ext = compr_regex.search(link)
if ext:
if not re.compile(r'\.%s$' % ext.group(1)).search(f):
printError(pkg, 'compressed-symlink-with-wrong-ext',
f, link)
perm = mode & 0o7777
mode_is_exec = mode & 0o111
if log_regex.search(f):
log_files.append(f)
# Hardlink check
for hardlink in hardlinks.get((rdev, inode), ()):
if os.path.dirname(hardlink) != os.path.dirname(f):
printWarning(pkg, 'cross-directory-hard-link', f, hardlink)
hardlinks.setdefault((rdev, inode), []).append(f)
# normal file check
if stat.S_ISREG(mode):
# set[ug]id bit check
if stat.S_ISGID & mode or stat.S_ISUID & mode:
if stat.S_ISUID & mode:
printError(pkg, 'setuid-binary', f, user, "%o" % perm)
if stat.S_ISGID & mode:
if not (group == 'games' and
(games_path_regex.search(f) or
games_group_regex.search(
pkg[rpm.RPMTAG_GROUP]))):
printError(pkg, 'setgid-binary', f, group,
"%o" % perm)
if mode & 0o777 != 0o755:
printError(pkg, 'non-standard-executable-perm', f,
"%o" % perm)
if not devel_pkg:
if lib_path_regex.search(f):
lib_file = True
elif not is_doc:
non_lib_file = f
if log_regex.search(f):
nonexec_file = True
if user != 'root':
printError(pkg, 'non-root-user-log-file', f, user)
if group != 'root':
printError(pkg, 'non-root-group-log-file', f, group)
if f not in ghost_files:
printError(pkg, 'non-ghost-file', f)
chunk = None
istext = False
res = None
try:
res = os.access(pkgfile.path, os.R_OK)
except UnicodeError as e: # e.g. non-ASCII, C locale, python 3
printWarning(pkg, 'inaccessible-filename', f, e)
else:
if res:
(chunk, istext) = peek(pkgfile.path, pkg)
(interpreter, interpreter_args) = script_interpreter(chunk)
if doc_regex.search(f):
if not interpreter:
nonexec_file = True
if not is_doc:
printError(pkg, 'not-listed-as-documentation', f)
if devel_pkg and f.endswith('.typelib'):
printError(pkg, 'non-devel-file-in-devel-package', f)
# check ldconfig call in %post and %postun
if lib_regex.search(f):
if devel_pkg:
printError(pkg, 'non-devel-file-in-devel-package', f)
if not postin:
printError(pkg, 'library-without-ldconfig-postin', f)
else:
if not ldconfig_regex.search(postin):
printError(pkg, 'postin-without-ldconfig', f)
if not postun:
printError(pkg, 'library-without-ldconfig-postun', f)
else:
if not ldconfig_regex.search(postun):
printError(pkg, 'postun-without-ldconfig', f)
# check depmod call in %post and %postun
res = not is_kernel_package and kernel_modules_regex.search(f)
if res:
kernel_version = res.group(1)
kernel_version_regex = re.compile(
r'\bdepmod\s+-a.*F\s+/boot/System\.map-' +
re.escape(kernel_version) + r'\b.*\b' +
re.escape(kernel_version) + r'\b',
re.MULTILINE | re.DOTALL)
if not postin or not depmod_regex.search(postin):
printError(pkg, 'module-without-depmod-postin', f)
# check that we run depmod on the right kernel
elif not kernel_version_regex.search(postin):
printError(pkg, 'postin-with-wrong-depmod', f)
if not postun or not depmod_regex.search(postun):
printError(pkg, 'module-without-depmod-postun', f)
# check that we run depmod on the right kernel
elif not kernel_version_regex.search(postun):
printError(pkg, 'postun-with-wrong-depmod', f)
# check install-info call in %post and %postun
if f.startswith('/usr/share/info/'):
if not postin:
printError(pkg,
'info-files-without-install-info-postin', f)
elif not install_info_regex.search(postin):
printError(pkg, 'postin-without-install-info', f)
preun = pkg[rpm.RPMTAG_PREUN] or \
pkg.scriptprog(rpm.RPMTAG_PREUNPROG)
if not postun and not preun:
printError(pkg,
'info-files-without-install-info-postun', f)
elif not ((postun and install_info_regex.search(postun)) or
(preun and install_info_regex.search(preun))):
printError(pkg, 'postin-without-install-info', f)
# check perl temp file
if perl_temp_file_regex.search(f):
printWarning(pkg, 'perl-temp-file', f)
is_buildconfig = istext and buildconfigfile_regex.search(f)
# check rpaths in buildconfig files
if is_buildconfig:
ln = pkg.grep(buildconfig_rpath_regex, f)
if ln:
printError(pkg, 'rpath-in-buildconfig', f, 'lines', ln)
res = bin_regex.search(f)
if res:
if not mode_is_exec:
printWarning(pkg, 'non-executable-in-bin', f,
"%o" % perm)
else:
exe = res.group(1)
if "/" not in exe:
bindir_exes.setdefault(exe, []).append(f)
if (not devel_pkg and not is_doc and
(is_buildconfig or includefile_regex.search(f) or
develfile_regex.search(f))):
printWarning(pkg, 'devel-file-in-non-devel-package', f)
if mode & 0o444 != 0o444 and perm & 0o7000 == 0:
ok_nonreadable = False
for regex in non_readable_regexs:
if regex.search(f):
ok_nonreadable = True
break
if not ok_nonreadable:
printError(pkg, 'non-readable', f, "%o" % perm)
if size == 0 and not normal_zero_length_regex.search(f) and \
f not in ghost_files:
printError(pkg, 'zero-length', f)
if mode & stat.S_IWOTH:
printError(pkg, 'world-writable', f, "%o" % perm)
if not perl_dep_error:
res = perl_regex.search(f)
if res:
if perl_version_trick:
vers = res.group(1) + '.' + res.group(2)
else:
vers = res.group(1) + res.group(2)
if not (pkg.check_versioned_dep('perl-base', vers) or
pkg.check_versioned_dep('perl', vers)):
printError(pkg, 'no-dependency-on',
'perl-base', vers)
perl_dep_error = True
if not python_dep_error:
res = python_regex.search(f)
if res and not (pkg.check_versioned_dep('python-base',
res.group(1)) or
pkg.check_versioned_dep('python',
res.group(1))):
printError(pkg, 'no-dependency-on', 'python-base',
res.group(1))
python_dep_error = True
source_file = python_bytecode_to_script(f)
if source_file:
if source_file in files:
if chunk:
# Verify that the magic ABI value embedded in the
# .pyc header is correct
found_magic = py_demarshal_long(chunk[:4]) & 0xffff
exp_magic, exp_version = get_expected_pyc_magic(f)
if exp_magic and found_magic not in exp_magic:
found_version = 'unknown'
for (pv, pm) in _python_magic_values.items():
if found_magic in pm:
found_version = pv
break
# If expected version was from the file path,
# issue # an error, otherwise a warning.
msg = (pkg,
'python-bytecode-wrong-magic-value',
f, "expected %s (%s), found %d (%s)" %
(" or ".join(map(str, exp_magic)),
exp_version or python_default_version,
found_magic, found_version))
if exp_version is not None:
printError(*msg)
else:
printWarning(*msg)
# Verify that the timestamp embedded in the .pyc
# header matches the mtime of the .py file:
pyc_timestamp = py_demarshal_long(chunk[4:8])
# If it's a symlink, check target file mtime.
srcfile = pkg.readlink(files[source_file])
if not srcfile:
printWarning(
pkg, 'python-bytecode-without-source', f)
elif pyc_timestamp != srcfile.mtime:
cts = datetime.fromtimestamp(
pyc_timestamp).isoformat()
sts = datetime.fromtimestamp(
srcfile.mtime).isoformat()
printError(
pkg, 'python-bytecode-inconsistent-mtime',
f, cts, srcfile.name, sts)
else:
printWarning(pkg, 'python-bytecode-without-source', f)
# normal executable check
if mode & stat.S_IXUSR and perm != 0o755:
printError(pkg, 'non-standard-executable-perm',
f, "%o" % perm)
if mode_is_exec:
if f in config_files:
printError(pkg, 'executable-marked-as-config-file', f)
if not nonexec_file:
# doc_regex and log_regex checked earlier, no match,
# check rest of usual cases here. Sourced scripts have
# their own check, so disregard them here.
nonexec_file = f.endswith('.pc') or \
compr_regex.search(f) or \
includefile_regex.search(f) or \
develfile_regex.search(f) or \
logrotate_regex.search(f)
if nonexec_file:
printWarning(pkg, 'spurious-executable-perm', f)
elif f.startswith('/etc/') and f not in config_files and \
f not in ghost_files:
printWarning(pkg, 'non-conffile-in-etc', f)
if pkg.arch == 'noarch' and f.startswith('/usr/lib64/python'):
printError(pkg, 'noarch-python-in-64bit-path', f)
if debuginfo_package:
if f.endswith('.debug'):
debuginfo_debugs = True
else:
debuginfo_srcs = True
res = man_base_regex.search(f)
if res:
man_basenames.add(res.group(1))
if use_utf8 and chunk:
# TODO: sequence based invocation
cmd = getstatusoutput(
'%s %s | gtbl | groff -mtty-char -Tutf8 '
'-P-c -mandoc -w%s >%s' %
(catcmd(f), shquote(pkgfile.path),
shquote(man_warn_category), os.devnull),
shell=True, lc_all="en_US.UTF-8")
for line in cmd[1].split("\n"):
res = man_warn_regex.search(line)
if not res or man_nowarn_regex.search(line):
continue
printWarning(pkg, "manual-page-warning", f,
line[res.end(1):])
if f.endswith(".svgz") and f[0:-1] not in files \
and scalable_icon_regex.search(f):
printWarning(pkg, "gzipped-svg-icon", f)
if f.endswith('.pem') and f not in ghost_files:
if pkg.grep(start_certificate_regex, f):
printWarning(pkg, 'pem-certificate', f)
if pkg.grep(start_private_key_regex, f):
printError(pkg, 'pem-private-key', f)
# text file checks
if istext:
# ignore perl module shebang -- TODO: disputed...
if f.endswith('.pm'):
interpreter = None
# sourced scripts should not be executable
if sourced_script_regex.search(f):
if interpreter:
printError(pkg,
'sourced-script-with-shebang', f,
interpreter, interpreter_args)
if mode_is_exec:
printError(pkg, 'executable-sourced-script',
f, "%o" % perm)
# ...but executed ones should
elif interpreter or mode_is_exec or script_regex.search(f):
if interpreter:
res = interpreter_regex.search(interpreter)
if (res and res.group(1) == 'env') or not res:
printError(pkg, 'wrong-script-interpreter',
f, interpreter, interpreter_args)
elif not nonexec_file and not \
(lib_path_regex.search(f) and
f.endswith('.la')):
printError(pkg, 'script-without-shebang', f)
if not mode_is_exec and not is_doc:
printError(pkg, 'non-executable-script', f,
"%o" % perm, interpreter,
interpreter_args)
if b'\r' in chunk:
printError(
pkg, 'wrong-script-end-of-line-encoding', f)
elif is_doc and not skipdocs_regex.search(f):
if b'\r' in chunk:
printWarning(
pkg, 'wrong-file-end-of-line-encoding', f)
# We check only doc text files for UTF-8-ness;
# checking everything may be slow and can generate
# lots of unwanted noise.
if use_utf8 and not is_utf8(pkgfile.path):
printWarning(pkg, 'file-not-utf8', f)
if fsf_license_regex.search(chunk) and \
fsf_wrong_address_regex.search(chunk):
printError(pkg, 'incorrect-fsf-address', f)
elif is_doc and chunk and compr_regex.search(f):
ff = compr_regex.sub('', f)
if not skipdocs_regex.search(ff):
# compressed docs, eg. info and man files etc
if use_utf8 and not is_utf8(pkgfile.path):
printWarning(pkg, 'file-not-utf8', f)
# normal dir check
elif stat.S_ISDIR(mode):
if mode & 0o1002 == 2: # world writable w/o sticky bit
printError(pkg, 'world-writable', f, "%o" % perm)
if perm != 0o755:
printError(pkg, 'non-standard-dir-perm', f, "%o" % perm)
if pkg.name not in filesys_packages and f in STANDARD_DIRS:
printError(pkg, 'standard-dir-owned-by-package', f)
if hidden_file_regex.search(f):
printWarning(pkg, 'hidden-file-or-dir', f)
# symbolic link check
elif stat.S_ISLNK(mode):
is_so = sofile_regex.search(f)
if not devel_pkg and is_so and not link.endswith('.so'):
printWarning(pkg, 'devel-file-in-non-devel-package', f)
res = man_base_regex.search(f)
if res:
man_basenames.add(res.group(1))
else:
res = bin_regex.search(f)
if res:
exe = res.group(1)
if "/" not in exe:
bindir_exes.setdefault(exe, [])
# absolute link
r = absolute_regex.search(link)
if r:
if not is_so and link not in files and \
link not in req_names:
is_exception = False
for e in dangling_exceptions:
if e[0].search(link):
is_exception = e[1]
break
if is_exception:
if is_exception not in req_names:
printWarning(pkg, 'no-dependency-on',
is_exception)
else:
printWarning(pkg, 'dangling-symlink', f, link)
linktop = r.group(1)
r = absolute_regex.search(f)
if r:
filetop = r.group(1)
if filetop == linktop or use_relative_symlinks:
printWarning(pkg, 'symlink-should-be-relative',
f, link)
# relative link
else:
if not is_so:
abslink = '%s/%s' % (os.path.dirname(f), link)
abslink = os.path.normpath(abslink)
if abslink not in files and abslink not in req_names:
is_exception = False
for e in dangling_exceptions:
if e[0].search(link):
is_exception = e[1]
break
if is_exception:
if is_exception not in req_names:
printWarning(pkg, 'no-dependency-on',
is_exception)
else:
printWarning(pkg, 'dangling-relative-symlink',
f, link)
pathcomponents = f.split('/')[1:]
r = points_regex.search(link)
lastpop = None
mylink = None
while r:
mylink = r.group(1)
if len(pathcomponents) == 0:
printError(pkg, 'symlink-has-too-many-up-segments',
f, link)
break
else:
lastpop = pathcomponents[0]
pathcomponents = pathcomponents[1:]
r = points_regex.search(mylink)
if mylink and lastpop:
r = absolute2_regex.search(mylink)
linktop = r.group(1)
# does the link go up and then down into the same
# directory?
# if linktop == lastpop:
# printWarning(pkg, 'lengthy-symlink', f, link)
# have we reached the root directory?
if len(pathcomponents) == 0 and linktop != lastpop \
and not use_relative_symlinks:
# relative link into other toplevel directory
printWarning(pkg, 'symlink-should-be-absolute', f,
link)
# check additional segments for mistakes like
# `foo/../bar/'
for linksegment in mylink.split('/'):
if linksegment == '..':
printError(
pkg,
'symlink-contains-up-and-down-segments',
f, link)
if f.startswith('/etc/cron.d/'):
if stat.S_ISLNK(mode):
printError(pkg, 'symlink-crontab-file', f)
if mode_is_exec:
printError(pkg, 'executable-crontab-file', f)
if stat.S_IWGRP & mode or stat.S_IWOTH & mode:
printError(pkg, 'non-owner-writeable-only-crontab-file', f)
if len(log_files) and not logrotate_file:
printWarning(pkg, 'log-files-without-logrotate', sorted(log_files))
if lib_package and lib_file and non_lib_file:
printError(pkg, 'outside-libdir-files', non_lib_file)
if debuginfo_package and debuginfo_debugs and not debuginfo_srcs: