]> git.ipfire.org Git - ipfire-2.x.git/blame - src/stripper
suricata: Change midstream policy to "pass-flow"
[ipfire-2.x.git] / src / stripper
CommitLineData
6c4cc7ea 1#!/usr/bin/env bash
bc259fdc 2
19054331
MT
3paths=()
4excludes=()
d79715db
MT
5strip="strip"
6break_on_error="1"
bc259fdc
MT
7
8while [ $# -gt 0 ]; do
9 case "${1}" in
d79715db
MT
10 --strip=*)
11 strip="${1#*=}"
12 ;;
bc259fdc 13 --exclude=*)
1432531a
MT
14 if [ -n "${1#*=}" ]; then
15 excludes+=( "!" "-path" "${1#*=}" "!" "-path" "${1#*=}/*" )
16 fi
bc259fdc 17 ;;
d79715db
MT
18 --ignore-errors)
19 break_on_error="0"
20 ;;
bc259fdc 21 *)
19054331 22 paths+=( "${1}" )
bc259fdc
MT
23 ;;
24 esac
25 shift
26done
fc44fa1f
MT
27
28function _strip() {
19054331
MT
29 local file="${1}"
30 local args=()
fc44fa1f 31
19054331
MT
32 # Fetch the filetype
33 local type="$(readelf -h "${file}" 2>/dev/null)"
bc259fdc 34
19054331
MT
35 case "${type}" in
36 # Libraries and Relocatable binaries
37 *Type:*"DYN (Shared object file)"*)
38 args+=( "--strip-all" )
39 ;;
37ef9fe4 40
8ac8abb2
MT
41 *Type:*"DYN (Position-Independent Executable file)"*)
42 args+=( "--strip-all" )
43 ;;
44
19054331
MT
45 # Binaries
46 *Type:*"EXEC (Executable file)"*)
47 args+=( "--strip-all" )
48 ;;
25d50589 49
19054331
MT
50 # Static libraries
51 *Type:*"REL (Relocatable file)"*)
52 args+=( "--strip-debug" "--remove-section=.comment" "--remove-section=.note" )
fc44fa1f 53 ;;
19054331
MT
54
55 # Skip any unrecognised files
fc44fa1f 56 *)
19054331 57 return 0
fc44fa1f
MT
58 ;;
59 esac
60
19054331
MT
61 # Fetch any capabilities
62 local capabilities="$(getfattr --no-dereference --name="security.capability" \
63 --absolute-names --dump "${file}" 2>/dev/null)"
64
fc44fa1f 65 echo "Stripping ${file}..."
d79715db
MT
66 if ! "${strip}" "${args[@]}" "${file}"; then
67 return ${break_on_error}
19054331
MT
68 fi
69
37ef9fe4
MT
70 # Restore capabilities
71 if [ -n "${capabilities}" ]; then
72 setfattr --no-dereference --restore=<(echo "${capabilities}")
73 fi
fc44fa1f
MT
74}
75
19054331 76for path in ${paths[@]}; do
f84c2cda 77 for file in $(find -H "${path}" -xdev "${excludes[@]}" -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 \) 2>/dev/null); do
19054331
MT
78 _strip "${file}" || exit $?
79 done
fc44fa1f 80done