-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkglint
executable file
·306 lines (274 loc) · 7.79 KB
/
pkglint
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
#!/bin/sh
# pkglint - verifier for pkgsrc packages
# See COPYING and COPYRIGHT files for corresponding information.
# Intentional: we're using indirect call for functions.
# https://www.shellcheck.net/wiki/SC2317
# shellcheck disable=SC2317
######################################################################
# Notification helpers. #
######################################################################
# Print information message to stdout.
# arg $1: the message
info() {
echo "=======> $1"
}
# Print warning message to stderr.
# arg $1: the message
warning() {
info "WARNING: $1" 1>&2
}
# Print error message to stderr.
# arg $1: the message
error() {
info "ERROR: $1" 1>&2
}
######################################################################
# Check .footprint files for typical errors. #
######################################################################
# Check for SUID/SGID files and directories.
# arg $1: the package source directory
footprint_sugid() {
awk '
BEGIN {
rc=0
}
{
if ($1 ~ /^[^d]..s....../) {
print "suid file found: "FILENAME": "$3;
rc=1;
} else if ($1 ~ /^[^d].....s.../) {
print "sgid file found: "FILENAME": "$3;
rc=1;
} else if ($1 ~ /^d..s....../) {
print "suid directory found: "FILENAME": "$3;
rc=1;
} else if ($1 ~ /^d.....s.../) {
print "sgid directory found: "FILENAME": "$3;
rc=1;
}
}
END {
exit(rc)
}' "$1/.footprint" 1>&2
}
# Check for world-writeable files and directories.
# arg $1: the package source directory
footprint_wowrite() {
awk '
BEGIN {
rc=0
}
{
if ($1 ~ /^d.......w[^t]/) {
print "world writeable directory found: "FILENAME": "$0;
rc=1;
} else if ($1 ~ /^-.......w./) {
print "world writeable file found: "FILENAME": "$0;
rc=1;
}
}
END {
exit(rc)
}' "$1/.footprint" 1>&2
}
# Check for invalid directories.
# arg $1: the package source directory
footprint_invalid() {
awk '
BEGIN {
rc=0
}
{
if ($3 ~ /^usr\/share\/man\/whatis$/) {
# do nothing, skip manpages database
1;
} else if ($3 ~ /^usr\/man\// \
|| $3 ~ /^usr\/local\// \
|| $3 ~ /^usr\/share\/locale\// \
|| $3 ~ /^usr\/locale\// \
|| $3 ~ /^usr\/share\/doc\// \
|| $3 ~ /^usr\/share\/gtk-doc\// \
|| $3 ~ /^usr\/doc\// \
|| $3 ~ /^usr\/info\// \
|| $3 ~ /^usr\/share\/info\// \
|| $3 ~ /^usr\/libexec\// \
|| $3 ~ /^usr\/man\/\.\.\// \
|| $3 ~ /^usr\/share\/man\/\.\.\// \
|| $3 ~ /^usr\/share\/licenses\// \
) {
print "invalid directory found: "FILENAME": "$3;
rc=1;
} else if ($3 ~ /^usr\/share\/man\/[^\/]+$/) {
print "invalid man location found: "FILENAME": "$3;
rc=1;
}
}
END {
exit(rc)
}' "$1/.footprint" 1>&2
}
# Check for junk files.
# arg $1: the package source directory
footprint_junk() {
awk '
BEGIN {
rc=0
}
{
if ($3 ~ /\/.*\/perllocal\.pod$/ \
|| $3 ~ /\/perl5\/.*\/\.packlist$/ \
|| $3 ~ /\/perl5\/.*\/[^\/]+\.bs$/ \
) {
print "junk file found: "FILENAME": "$3;
rc=1;
}
# ignore case
IGNORECASE = 1;
if ($3 ~ /^usr\/share\/terminfo\/n\/news$$/) {
# nothing to do, skip ncurses terminfo file
1;
} else if ( \
$3 ~ /\/AUTHORS$/ \
|| $3 ~ /\/BUGS$/ \
|| $3 ~ /\/COPYING$/ \
|| $3 ~ /\/CHANGELOG$/ \
||($3 ~ /\/INSTALL$$/ && $3 !~ /^usr\/bin\/install$/) \
|| $3 ~ /\/NEWS$/ \
|| $3 ~ /\/README$/ \
|| $3 ~ /\/README.TXT$/ \
|| $3 ~ /\/README.md$/ \
|| $3 ~ /\/THANKS$/ \
|| $3 ~ /\/TODO$/ \
) {
print "junk file found: "FILENAME": "$3;
rc=1;
}
}
END {
exit(rc)
}' "$1/.footprint" 1>&2
}
######################################################################
# Other checks. #
######################################################################
# Grep all URLs from Pkgfile and README files.
# arg $1: the package source directory
grep_urls() {
if [ -r "$1/README" ]; then
grep -PHio "https?://[^\"\\'> ]+" "$1/README"
fi
if [ -r "$1/Pkgfile" ]; then
grep -PHio "URL:\s*\K(https?://[^\"\\'> ]+)" "$1/Pkgfile"
(
# Intentional.
# https://www.shellcheck.net/wiki/SC1091
# shellcheck disable=SC1091
. "$1/Pkgfile"
# Intentional: "var is referenced but not assigned".
# https://www.shellcheck.net/wiki/SC2154
# shellcheck disable=SC2154
for __URL in ${source}; do
case $__URL in
http://* | https://* ) echo "$1/Pkgfile:$__URL" ;;
esac
done
)
fi
}
# Check for dead links.
# arg $1: the package source directory
dead_links() {
__RC1=0
for __LINE in $(grep_urls "$1"); do
unset -v __URL __HTTPCODE
__URL=${__LINE##"$1/Pkgfile:"}
__URL=${__URL##"$1/README:"}
# Intentional word splitting.
# shellcheck disable=SC2086
__HTTPCODE=$(curl $CURL_CMD $CURL_OPTS "$__URL")
if [ "$__HTTPCODE" -ne 200 ]; then
echo "dead link found: [$__HTTPCODE] $__LINE" 1>&2
__RC1=1
fi
done
return "$__RC1"
}
######################################################################
# Command-line helpers. #
######################################################################
print_help() {
cat <<EOF
Usage: $ARGV0 [-dhijsvw] [pkgsrcdir ...]
Detect common mistakes and stylistic issues in pkgsrc package definitions.
-d, --dead-links check for dead links
-i, --invalid check for invalid directories
-j, --junk check for junk files
-s, --suid-sgid check for SUID/SGID files and directories
-w, --world-writeable check for world-writeable files and directories
-v, --version print version and exit
-h, --help print help and exit
EOF
}
print_version() {
echo "$ARGV0 (pkgmaint) @VERSION@"
}
parse_options() {
if ! _opts=$(getopt -a -n "$ARGV0" -l "$LOPTS" -o "$SOPTS" -- "$@"); then
echo "Try '$ARGV0 --help' for more information." 1>&2
exit 1
fi
eval set -- "$_opts"; unset _opts
while true; do
case $1 in
-d| --dead-links) CHECK="$CHECK dead_links" ;;
-i| --invalid) CHECK="$CHECK footprint_invalid" ;;
-j| --junk) CHECK="$CHECK footprint_junk" ;;
-s| --suid-sgid) CHECK="$CHECK footprint_sugid" ;;
-w|--world-writeable) CHECK="$CHECK footprint_wowrite" ;;
-v| --version) print_version ; exit 0 ;;
-h| --help) print_help ; exit 0 ;;
--) shift ; break ;;
esac
shift
done
PKGSRCDIRS=${*:-$PWD}
}
main() {
parse_options "$@"
__RC0=0
for __PKGSRCDIR in $PKGSRCDIRS; do
if [ ! -d "$__PKGSRCDIR" ]; then
warning "missing '$__PKGSRCDIR': skip directory"
__RC0=1
continue
fi
for __FILE in Pkgfile .footprint .md5sum; do
if [ ! -f "$__PKGSRCDIR/$__FILE" ]; then
error "missing '$__PKGSRCDIR/$__FILE': skip check"
__RC0=1
continue 2
fi
done
for __CHK in $CHECK; do
$__CHK "$__PKGSRCDIR" || __RC0=1
done
done
exit "$__RC0"
}
######################################################################
# -e: Exit if command return status greater than 0.
# -f: Disable globbing *?[].
set -ef
# Globals.
export LC_ALL=POSIX
readonly ARGV0="${0##*/}"
LOPTS="dead-links,invalid,junk,suid-sgid,world-writeable,version,help"
SOPTS="dfijswvh"
CURL_CMD="-L -I -f --retry 3 --retry-delay 3 -o /dev/null -sw %{http_code}"
CURL_OPTS=
CHECK=
PKGSRCDIRS=
######################################################################
main "$@"
# vim: cc=72 tw=70
# End of file.