-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkgtest.sh
More file actions
executable file
·295 lines (262 loc) · 7.92 KB
/
pkgtest.sh
File metadata and controls
executable file
·295 lines (262 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/bash
#
# Inspect the packages and print any problems.
#
# - Check that the root filesystem only contains the expected paths.
# - Check that no statically linked files are present.
# - Check that some directories are not present
# (eg /usr/local, /usr/share/info, ...).
# - Check that all symlinks point to files provided by a package.
# - Check that the permissions are sane.
#
# Author: Alastair Hughes
# Contact: < hobbitalastair at yandex dot com >
set -e
shopt -s nullglob
VERSION="0.1"
USAGE="<config dir> [<pkg path> ...]"
source "$(dirname "$(realpath "$0")")/lib/libmain.sh"
source "$(dirname "$(realpath "$0")")/lib/libpackage.sh"
declare -A symlinks
declare -A files
get_pkgname() {
# Retrieve the package name from the given directory.
# The .PKGINFO file is expected to contain the package name.
local dir="$1"
/usr/bin/grep "${dir}/.PKGINFO" -e 'pkgname' | cut -d'=' -f2 | \
sed 's:[ \t]::g'
}
fail_dir() {
# Record a failure for the given directory.
local dir="$1"
local reason="$2"
message error "$(get_pkgname "${dir}"): ${reason}"
}
fail() {
# Record a general failure.
message error "$1"
}
extract_pkg() {
# Extract the given package.
local dir="$1"
local pkg="$(realpath "$2")"
local pkgname="$(basename "${pkg}")"
message debug "Extracting package '${pkgname}' into '${dir}'"
pushd "${dir}" > /dev/null
bsdtar -xf "${pkg}"
rm -f .MTREE .BUILDINFO
popd > /dev/null
message debug "Package extracted"
}
check_root() {
# Check that the root directory only contains the expected paths.
local dir="$1"
message debug "Checking for invalid files in root..."
local valid valid_filesystem
declare -A valid=(
[boot]=""
[etc]=""
[usr]=""
[var]=""
)
declare -A valid_filesystem=(
[boot]=""
[bin]=""
[dev]=""
[etc]=""
[home]=""
[mnt]=""
[proc]=""
[root]=""
[sys]=""
[tmp]=""
[run]=""
[usr]=""
[var]=""
)
for i in "${dir}"/*; do
root_dir="$(basename "${i}")"
if [ "${valid["${root_dir}"]+set}" != 'set' ]; then
# Only let filesystem provide some dirs.
if [ "${valid_filesystem["${root_dir}"]+set}" == 'set' ] && \
[ "$(get_pkgname "${dir}")" == "filesystem" ]; then
: # Ignore...
else
fail_dir "${dir}" "/${root_dir} exists"
fi
fi
done
}
check_nodirs() {
# Check that some dirs do not exist.
local dir="$1"
message debug "Checking for unwanted dirs..."
for path in usr/share/info usr/local; do
if [ -e "${dir}/${path}" ]; then
fail_dir "${dir}" "Found unwanted path '${path}'"
fi
done
# Fail if a package (except gettext) provides usr/lib/charset.alias.
if [ -e "${dir}/usr/lib/charset.alias" ] && \
[ "$(get_pkgname "${dir}")" != "gettext" ]; then
fail_dir "${dir}" "Found usr/lib/charset.alias!"
fi
}
check_bin() {
# Check that there are no invalid binaries or libraries.
local dir="$1"
local f="$2"
# Ignore files in boot/, as they are special.
if [ "$(printf '%s' "${f}" | cut -d'/' -f1)" == boot ]; then
return
fi
# Ignore *.c32 (syslinux).
if printf '%s' "${f}" | grep -e '\.c32$' > /dev/null; then
return
fi
# Catch statically linked libraries.
if printf '%s' "${f}" | grep -e '\.a$' > /dev/null; then
fail_dir "${dir}" "${f} seems to be a statically linked library!"
fi
local readelf arch
readelf="$(readelf -h -d "${f}" 2> /dev/null)" || true
arch="$(printf '%s' "${readelf}" | sed -n -e '/Machine:/p' | \
sed 's/.*:[ \t]*//' | head -n 1)"
# Check arch.
if [ -z "${arch}" ]; then
arch="${global_arch}"
fi
if [ -z "${global_arch}" ]; then
global_arch="${arch}"
fi
if [ "${global_arch}" != "${arch}" ]; then
fail_dir "${dir}" "${f} has arch '${arch}', not '${global_arch}'!"
fi
# Check that the binary is stripped.
# We ignore object files, as they need the symbols.
local ext="$(printf '%s' "${f}" | rev | cut -d. -f1 | rev)"
if [ "${ext}" != 'o' ] && [ "${ext}" != 'ko' ]; then
if file "${f}" | grep 'not stripped' > /dev/null; then
fail_dir "${dir}" "Found an unstripped file ${f}"
fi
fi
}
check_permissions() {
# Check the permissions for the given file.
local dir="$1"
local f="$2"
perm="$(stat -c "%#a" "${f}")"
if [ -h "${f}" ]; then
if [ "${perm}" -ne 0777 ]; then
fail_dir "${dir}" "Symlink '${f}' has permissions '${perm}'"
fi
elif [ -f "${f}" ]; then
if [ "${perm}" -ne 0644 ] && [ "${perm}" -ne 0755 ] && \
[ "${perm}" -ne 0444 ] && [ "${perm}" -ne 0640 ] && \
[ "${perm}" -ne 0750 ] && [ "${perm}" -ne 0440 ] && \
[ "${perm}" -ne 0555 ] && [ "${perm}" -ne 0600 ] && \
[ "${perm}" -ne 0711 ] && [ "${perm}" -ne 0 ]; then
fail_dir "${dir}" "File '${f}' has permissions '${perm}'"
fi
elif [ -d "${f}" ]; then
if [ "${perm}" -ne 0755 ] && [ "${perm}" -ne 0750 ] && \
[ "${perm}" -ne 0700 ] && [ "${perm}" -ne 0555 ] && \
[ "${perm}" -ne 0711 ]; then
fail_dir "${dir}" "Dir '${f}' has permissions '${perm}'"
fi
fi
}
check_file() {
# Mark the given file.
local dir="$1"
local f="$2"
if target="$(readlink "${f}")"; then
symlinks["${pkgname}: ${f}"]="${target}"
fi
files["${f}"]="${pkgname}"
}
check_pkg() {
# Check the given directory for suitability.
local dir="$1"
local pkg="$2"
message info "Checking package ${pkgname}..."
extract_pkg "${dir}" "${pkg}"
local pkgname="$(get_pkgname "${dir}")" || \
error 1 "Invalid package '${pkg}!'"
check_root "${dir}"
check_nodirs "${dir}"
# Per-file checks.
message debug "Checking files for ${pkgname}..."
pushd "${dir}" > /dev/null
local f
find ./ -print0 | while read -r -d '' f; do
f="$(printf '%s' "${f}" | cut -d/ -f2-)"
if [ -e "${f}" ]; then
check_file "${dir}" "${f}"
check_permissions "${dir}" "${f}"
check_bin "${dir}" "${f}"
fi
done
popd > /dev/null
}
check_symlinks() {
# Check that all symlinks are pointing at a file.
message info "Checking symlinks..."
local sym target
for sym in ${!symlinks[@]}; do
target="${symlinks["${sym}"]}"
if [ -z "${files["${target}"]}" ]; then
fail "Found a broken symlink (${sym})"
fi
done
}
print_targets() {
# Print the given path, or all packages if none are passed.
local configdir="$1"
shift
if [ -n "$@" ]; then
local pkg
for pkg in "$@"; do
printf '%s\n' "${pkg}"
done
else
printallpkgs "${configdir}" $(getpkgdirs cross native)
fi
}
main() {
# Check the packages.
local configdir="$1"
shift
loadrepoconf "${configdir}"
local pkg dir
dir="${config[builddir]}/pkgtest"
trap "rm -rf '${dir}'" EXIT
print_targets "${configdir}" "$@" |
while read pkg; do
local pkgname="$(basename "${pkg}")"
rm -rf "${dir}"; mkdir "${dir}"
check_pkg "${dir}" "${pkg}"
done
check_symlinks
message info "Finished checks!"
}
CONFIGDIR="" # Set the initial config dir.
PKGS="" # Packages to check.
parseargs "$@" # Initial argument parse.
# Manual argument parse.
for arg in "$@"; do
ignore_arg "${arg}" || \
case "${arg}" in
*) if [ -z "${CONFIGDIR}" ]; then
CONFIGDIR="${arg}"
else
if [ -f "${arg}" ]; then
PKGS+=" ${arg}"
else
error 2 "Package '${arg}' is not a file!"
fi
fi;;
esac
done
setup "${CONFIGDIR}"
main "${CONFIGDIR}" "${PKGS}"