Debian Codegraph @

libexec/auto

raw
1 #!/bin/sh
2
3 # Linux Vulnerability Mitigation
4 # Copyright (C) 2026 Daniel Baumann <[email protected]>
5 #
6 # SPDX-License-Identifier: PD
7 #
8 # This program is free software: you have unlimited permission
9 # to copy, distribute and modify it.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 set -e
16
17 PROGRAM="linux-vulnerability-mitigation"
18 COMMAND="$(basename "${0}")"
19
20 SHARE="/usr/share/${PROGRAM}"
21
22 if [ -t 1 ] && [ -t 2 ]
23 then
24 RED="\033[1;33;31m"
25 GREEN="\033[1;33;32m"
26 YELLOW="\033[1;33;33m"
27 NORMAL="\033[0m"
28 fi
29
30 Parameters ()
31 {
32 GETOPT_LONGOPTIONS="install,remove,force,help,"
33 GETOPT_OPTIONS="i,r,f,h,"
34
35 PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name="${COMMAND}" --options ${GETOPT_OPTIONS} --shell sh -- "${@}")"
36
37 # shellcheck disable=SC2181
38 if [ "${?}" != "0" ]
39 then
40 echo "'${COMMAND}': getopt exit" >&2
41 exit 1
42 fi
43
44 eval set -- "${PARAMETERS}"
45
46 while true
47 do
48 case "${1}" in
49 -i|--install)
50 RUNS="${RUNS} install"
51 shift 1
52 ;;
53
54 -r|--remove)
55 RUNS="${RUNS} remove"
56 shift 1
57 ;;
58
59 -f|--force)
60 FORCE="true"
61 shift 1
62 ;;
63
64 -h|--help)
65 Usage
66 ;;
67
68 --)
69 shift 1
70 break
71 ;;
72
73 *)
74 echo "'${COMMAND}': getopt error" >&2
75 exit 1
76 ;;
77 esac
78 done
79 }
80
81 Usage ()
82 {
83 echo "Usage: ${PROGRAM} ${COMMAND} [{-i, --install|-r, --remove}] [-f, --force} " >&2
84 echo
85 echo "See ${PROGRAM}-${COMMAND}(1), ${PROGRAM}(1) and ${PROGRAM}(7) for more information."
86 }
87
88 RUNS=""
89
90 Parameters "${@}"
91
92 if [ -z "${RUNS}" ]
93 then
94 RUNS="remove install"
95 fi
96
97 # Run
98 case "$(id -u)" in
99 0)
100 ;;
101
102 *)
103 echo "${RED}error:${NORMAL} '${COMMAND}' operation requires superuser privilege" >&2
104 exit 1
105 ;;
106 esac
107
108 MITIGATIONS="$(find "${SHARE}/mitigations" -mindepth 1 -printf '%f\n' | sort -V)"
109
110 for RUN in ${RUNS}
111 do
112 case "${RUN}" in
113 install)
114 for MITIGATION in ${MITIGATIONS}
115 do
116 case "${FORCE}" in
117 true)
118 # run mitigation even if run before
119 ;;
120
121 *)
122 set +e
123 "${SHARE}/mitigations/${MITIGATION}" check > /dev/null 2>&1
124 RETURN="${?}"
125 set -e
126
127 case "${RETURN}" in
128 0)
129 echo " ${YELLOW}already installed${NORMAL} $(basename "${MITIGATION}") $(awk -F= '/^DATE=/ { print $2 }' "${SHARE}/mitigations/${MITIGATION}") $(awk -F= '/^NAME=/ { print $2 }' "${SHARE}/mitigations/${MITIGATION}")"
130 continue
131 ;;
132 esac
133 ;;
134 esac
135
136 set +e
137 "${SHARE}/mitigations/${MITIGATION}" status
138 RETURN="${?}"
139 set -e
140
141 case "${RETURN}" in
142 0)
143 # fixed
144 ;;
145
146 *)
147 # mitigated or vulnerable
148 "${SHARE}/mitigations/${MITIGATION}" install
149 echo " ${GREEN}installing ${NORMAL} $(basename "${MITIGATION}") $(awk -F= '/^DATE=/ { print $2 }' "${SHARE}/mitigations/${MITIGATION}") $(awk -F= '/^NAME=/ { print $2 }' "${SHARE}/mitigations/${MITIGATION}")"
150 ;;
151 esac
152 done
153 ;;
154
155 remove)
156 for MITIGATION in ${MITIGATIONS}
157 do
158 case "${FORCE}" in
159 true)
160 # run mitigation even if run before
161 ;;
162
163 *)
164 set +e
165 "${SHARE}/mitigations/${MITIGATION}" check > /dev/null 2>&1
166 RETURN="${?}"
167 set -e
168
169 case "${RETURN}" in
170 0)
171 # removing
172 ;;
173
174 *)
175 echo " ${YELLOW}already removed ${NORMAL} $(basename "${MITIGATION}") $(awk -F= '/^DATE=/ { print $2 }' "${SHARE}/mitigations/${MITIGATION}") $(awk -F= '/^NAME=/ { print $2 }' "${SHARE}/mitigations/${MITIGATION}")"
176 continue
177 ;;
178 esac
179 ;;
180 esac
181
182 set +e
183 "${SHARE}/mitigations/${MITIGATION}" status
184 RETURN="${?}"
185 set -e
186
187 case "${RETURN}" in
188 0)
189 # fixed
190 "${SHARE}/mitigations/${MITIGATION}" remove
191 echo " ${RED}removing ${NORMAL} $(basename "${MITIGATION}") $(awk -F= '/^DATE=/ { print $2 }' "${SHARE}/mitigations/${MITIGATION}") $(awk -F= '/^NAME=/ { print $2 }' "${SHARE}/mitigations/${MITIGATION}")"
192 ;;
193
194 *)
195 # mitigated or vulnerable
196 ;;
197 esac
198 done
199 ;;
200 esac
201 done