| 1 |
#!/bin/sh |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
# Copyright 2021 Helmut Grohne <[email protected]> |
| 4 |
|
| 5 |
# A "hashset" is a shell variable containing a sequence of elements separated |
| 6 |
# and surrounded by hash (#) characters. None of the elements may contain a |
| 7 |
# hash character. The character is thus chosen, because it initiates a comment |
| 8 |
# in /etc/shells. All variables ending in _SHELLS in this file are hashsets. |
| 9 |
|
| 10 |
set -e |
| 11 |
|
| 12 |
# Check whether hashset $1 contains element $2. |
| 13 |
hashset_contains() { |
| 14 |
case "$1" in |
| 15 |
*"#$2#"*) return 0 ;; |
| 16 |
*) return 1 ;; |
| 17 |
esac |
| 18 |
} |
| 19 |
|
| 20 |
log() { |
| 21 |
if [ "$VERBOSE" = 1 ]; then |
| 22 |
echo "$*" |
| 23 |
fi |
| 24 |
} |
| 25 |
|
| 26 |
ROOT=${DPKG_ROOT:-} |
| 27 |
VERBOSE=0 |
| 28 |
NOACT=0 |
| 29 |
|
| 30 |
while [ $# -gt 0 ]; do |
| 31 |
case "$1" in |
| 32 |
--help) |
| 33 |
cat <<EOF |
| 34 |
usage: $0 [options] |
| 35 |
|
| 36 |
--no-act Do not move the actual update into place |
| 37 |
--verbose Be more verbose |
| 38 |
--root DIR Operate on the given chroot, defaults to / |
| 39 |
EOF |
| 40 |
exit 0 |
| 41 |
;; |
| 42 |
--no-act) |
| 43 |
NOACT=1 |
| 44 |
;; |
| 45 |
--root) |
| 46 |
shift |
| 47 |
if [ "$#" -lt 1 ]; then |
| 48 |
echo "missing argument to --root" 1>&2 |
| 49 |
exit 1 |
| 50 |
fi |
| 51 |
ROOT=$1 |
| 52 |
;; |
| 53 |
--verbose) |
| 54 |
VERBOSE=1 |
| 55 |
;; |
| 56 |
*) |
| 57 |
echo "unrecognized option $1" 1>&2 |
| 58 |
exit 1 |
| 59 |
;; |
| 60 |
esac |
| 61 |
shift |
| 62 |
done |
| 63 |
|
| 64 |
PKG_DIR="$ROOT/usr/share/debianutils/shells.d" |
| 65 |
STATE_FILE="$ROOT/var/lib/shells.state" |
| 66 |
TEMPLATE_ETC_FILE="$ROOT/usr/share/debianutils/shells" |
| 67 |
TARGET_ETC_FILE="$ROOT/etc/shells" |
| 68 |
SOURCE_ETC_FILE="$TARGET_ETC_FILE" |
| 69 |
NEW_ETC_FILE="$TARGET_ETC_FILE.tmp" |
| 70 |
NEW_STATE_FILE="$STATE_FILE.tmp" |
| 71 |
|
| 72 |
if ! test -e "$SOURCE_ETC_FILE"; then |
| 73 |
SOURCE_ETC_FILE="$TEMPLATE_ETC_FILE" |
| 74 |
fi |
| 75 |
|
| 76 |
PKG_SHELLS='#' |
| 77 |
LC_COLLATE=C.UTF-8 # glob in reproducible order |
| 78 |
for f in "$TEMPLATE_ETC_FILE" "$PKG_DIR/"*; do |
| 79 |
test -f "$f" || continue |
| 80 |
while IFS='#' read -r line _; do |
| 81 |
[ -n "$line" ] || continue |
| 82 |
if ! hashset_contains "$PKG_SHELLS" "$line"; then |
| 83 |
PKG_SHELLS="$PKG_SHELLS$line#" |
| 84 |
fi |
| 85 |
realshell=$(dpkg-realpath --root "$ROOT" "$(dirname "$line")")/$(basename "$line") |
| 86 |
if ! hashset_contains "$PKG_SHELLS" "$realshell"; then |
| 87 |
PKG_SHELLS="$PKG_SHELLS$realshell#" |
| 88 |
fi |
| 89 |
done < "$f" |
| 90 |
done |
| 91 |
|
| 92 |
STATE_SHELLS='#' |
| 93 |
if [ -e "$STATE_FILE" ] ; then |
| 94 |
while IFS='#' read -r line _; do |
| 95 |
[ -n "$line" ] && STATE_SHELLS="$STATE_SHELLS$line#" |
| 96 |
done < "$STATE_FILE" |
| 97 |
fi |
| 98 |
|
| 99 |
cleanup() { |
| 100 |
rm -f "$NEW_ETC_FILE" "$NEW_STATE_FILE" |
| 101 |
} |
| 102 |
trap cleanup EXIT |
| 103 |
|
| 104 |
: > "$NEW_ETC_FILE" |
| 105 |
ETC_SHELLS='#' |
| 106 |
while IFS= read -r line; do |
| 107 |
shell=${line%%#*} |
| 108 |
# copy all comment lines, packaged shells and local additions |
| 109 |
if [ -z "$shell" ] || |
| 110 |
hashset_contains "$PKG_SHELLS" "$shell" || |
| 111 |
! hashset_contains "$STATE_SHELLS" "$shell"; then |
| 112 |
if [ -z "$shell" ] || ! hashset_contains "$ETC_SHELLS" "$shell"; then |
| 113 |
echo "$line" >> "$NEW_ETC_FILE" |
| 114 |
ETC_SHELLS="$ETC_SHELLS$shell#" |
| 115 |
fi |
| 116 |
else |
| 117 |
log "removing shell $shell" |
| 118 |
fi |
| 119 |
done < "$SOURCE_ETC_FILE" |
| 120 |
|
| 121 |
: > "$NEW_STATE_FILE" |
| 122 |
saved_IFS=$IFS |
| 123 |
IFS='#' |
| 124 |
set -f |
| 125 |
# shellcheck disable=SC2086 # word splitting intended, globbing disabled |
| 126 |
set -- ${PKG_SHELLS###} |
| 127 |
set +f |
| 128 |
IFS=$saved_IFS |
| 129 |
for shell; do |
| 130 |
echo "$shell" >> "$NEW_STATE_FILE" |
| 131 |
# add shells that are neither already present nor locally removed |
| 132 |
if ! hashset_contains "$ETC_SHELLS" "$shell" && |
| 133 |
! hashset_contains "$STATE_SHELLS" "$shell"; then |
| 134 |
echo "$shell" >> "$NEW_ETC_FILE" |
| 135 |
log "adding shell $shell" |
| 136 |
fi |
| 137 |
done |
| 138 |
|
| 139 |
if [ "$NOACT" = 0 ]; then |
| 140 |
if [ -e "$STATE_FILE" ]; then |
| 141 |
chmod --reference="${STATE_FILE}" "${NEW_STATE_FILE}" || chmod $(stat -c %a "${STATE_FILE}") "${NEW_STATE_FILE}" |
| 142 |
chown --reference="${STATE_FILE}" "${NEW_STATE_FILE}" || chown $(stat -c %U "${STATE_FILE}") "${NEW_STATE_FILE}" |
| 143 |
else |
| 144 |
chmod 0644 "$NEW_STATE_FILE" |
| 145 |
fi |
| 146 |
chmod --reference="${SOURCE_ETC_FILE}" "${NEW_ETC_FILE}" || chmod $(stat -c %a "${SOURCE_ETC_FILE}") "${NEW_ETC_FILE}" |
| 147 |
chown --reference="${SOURCE_ETC_FILE}" "${NEW_ETC_FILE}" || chown $(stat -c %U "${SOURCE_ETC_FILE}") "${NEW_ETC_FILE}" |
| 148 |
sync -d "$NEW_ETC_FILE" "$NEW_STATE_FILE" |
| 149 |
mv -Z "${NEW_ETC_FILE}" "${TARGET_ETC_FILE}" || mv "${NEW_ETC_FILE}" "${TARGET_ETC_FILE}" |
| 150 |
sync "$TARGET_ETC_FILE" |
| 151 |
sync "$(dirname "$TARGET_ETC_FILE")" |
| 152 |
mv "$NEW_STATE_FILE" "$STATE_FILE" |
| 153 |
sync "$STATE_FILE" |
| 154 |
sync "$(dirname "$STATE_FILE")" |
| 155 |
trap "" EXIT |
| 156 |
fi |