| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Copyright (C) 2009-2014 Timo Weingärtner <[email protected]> |
| 4 |
# |
| 5 |
# This file is part of openssh-known-hosts. |
| 6 |
# |
| 7 |
# openssh-known-hosts is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# openssh-known-hosts is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with openssh-known-hosts. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
set -euC |
| 21 |
|
| 22 |
CONFDIR=${CONFDIR:-/etc/openssh-known-hosts} |
| 23 |
PLUGIN_PATH=${PLUGIN_PATH:-/usr/local/share/openssh-known-hosts/plugins:/usr/share/openssh-known-hosts/plugins} |
| 24 |
CACHEDIR=${CACHEDIR:-/var/cache/openssh-known-hosts} |
| 25 |
LOCK=${LOCK:-/var/lock/openssh-known-hosts} |
| 26 |
OUTFILE=${OUTFILE:-/var/lib/openssh-known-hosts/ssh_known_hosts} |
| 27 |
|
| 28 |
readonly CONFDIR PLUGIN_PATH CACHEDIR LOCK OUTFILE |
| 29 |
|
| 30 |
path_search () { |
| 31 |
local search=$1 |
| 32 |
local -a pathlist |
| 33 |
IFS=: read -ra pathlist <<< "$2" |
| 34 |
|
| 35 |
if [[ ${search} =~ / ]]; then |
| 36 |
echo "${search}" |
| 37 |
return 0 |
| 38 |
fi |
| 39 |
for path in "${pathlist[@]}"; do |
| 40 |
if [ -f "${path}/${search}" ]; then |
| 41 |
echo "${path}/${search}" |
| 42 |
return 0 |
| 43 |
fi |
| 44 |
done |
| 45 |
echo "'${search}' not found in '$2'!" >&2 |
| 46 |
exit 127 |
| 47 |
} |
| 48 |
|
| 49 |
cleanup () { |
| 50 |
rm -f "${OUTFILE}.new" |
| 51 |
kill "${LOCKPID}" |
| 52 |
lockfile-remove "${LOCK}" |
| 53 |
} |
| 54 |
|
| 55 |
download_source () ( |
| 56 |
local sourcename=$1 |
| 57 |
local sourcefile=$2 |
| 58 |
|
| 59 |
cd "${CACHEDIR}/${sourcename}" |
| 60 |
set -a |
| 61 |
. "${sourcefile}" |
| 62 |
set +a |
| 63 |
# shellcheck disable=SC2091 |
| 64 |
$(path_search "$PLUGIN" "$PLUGIN_PATH") >| log 2>&1 || { |
| 65 |
exitcode=$? |
| 66 |
rm -f new |
| 67 |
ignore='' |
| 68 |
for e in ${EXIT_IGNORE:-0}; do |
| 69 |
if [[ $e = "$exitcode" ]]; then |
| 70 |
ignore=1 |
| 71 |
break |
| 72 |
fi |
| 73 |
done |
| 74 |
if [ -z "$ignore" ] || [ "$fail" ]; then |
| 75 |
echo "${source} exited with code ${exitcode}, log follows:" |
| 76 |
cat log |
| 77 |
echo |
| 78 |
fi |
| 79 |
if [ "$fail" ]; then |
| 80 |
exit 1 |
| 81 |
fi |
| 82 |
} >&2 |
| 83 |
) |
| 84 |
|
| 85 |
if [ $# -eq 1 ] && [ "$1" = "-f" ]; then |
| 86 |
fail=1 |
| 87 |
else |
| 88 |
fail='' |
| 89 |
fi |
| 90 |
|
| 91 |
trap cleanup EXIT |
| 92 |
|
| 93 |
lockfile-create "${LOCK}" |
| 94 |
lockfile-touch "${LOCK}" & |
| 95 |
LOCKPID="$!" |
| 96 |
|
| 97 |
mkdir -p "${CACHEDIR}" |
| 98 |
cd "${CACHEDIR}" |
| 99 |
|
| 100 |
find -mindepth 2 -maxdepth 2 -type f -name new -delete |
| 101 |
|
| 102 |
run-parts --list "${CONFDIR}/sources/" | while read -r sourcefile; do |
| 103 |
source=${sourcefile##*/} |
| 104 |
mkdir -p "${source}" |
| 105 |
download_source "${source}" "${sourcefile}" |
| 106 |
if [ -e "${source}/new" ]; then |
| 107 |
mv "${source}/new" "${source}/current" |
| 108 |
fi |
| 109 |
if [ -e "${source}/current" ]; then |
| 110 |
if [ -e "${sourcefile}.filter" ]; then |
| 111 |
if [[ ${source}/filtered -ot ${source}/current ]] || [[ ${source}/filtered -ot ${sourcefile}.filter ]]; then |
| 112 |
mapfile -t filter < "${sourcefile}.filter" |
| 113 |
for i in "${!filter[@]}"; do |
| 114 |
if [[ ${filter[$i]} =~ ^($|#) ]]; then |
| 115 |
unset filter[$i] |
| 116 |
fi |
| 117 |
done |
| 118 |
while read -r hostlist rest; do |
| 119 |
IFS=, read -ra hostarray <<<"$hostlist" |
| 120 |
new_hostlist='' |
| 121 |
for host in "${hostarray[@]}"; do |
| 122 |
for rule in "${filter[@]}"; do |
| 123 |
if [[ ${host} =~ ${rule#* } ]]; then |
| 124 |
if [[ ${rule%% *} =~ ^[aopy] ]]; then |
| 125 |
new_hostlist="${new_hostlist}${host}," |
| 126 |
fi |
| 127 |
break |
| 128 |
fi |
| 129 |
done |
| 130 |
done |
| 131 |
[ "$new_hostlist" ] || continue |
| 132 |
echo "${new_hostlist%,} ${rest}" |
| 133 |
done < "${source}/current" | sort -u >| "${source}/filtered.new" |
| 134 |
mv "${source}/filtered.new" "${source}/filtered" |
| 135 |
fi |
| 136 |
cat "${source}/filtered" >&3 |
| 137 |
else |
| 138 |
sort -u "${source}/current" >&3 |
| 139 |
fi |
| 140 |
fi |
| 141 |
done 3>| "${OUTFILE}.new" |
| 142 |
|
| 143 |
if cmp -s "${OUTFILE}" "${OUTFILE}.new"; then |
| 144 |
rm "${OUTFILE}.new" |
| 145 |
else |
| 146 |
mv "${OUTFILE}.new" "${OUTFILE}" |
| 147 |
fi |
| 148 |
|
| 149 |
# clean up cache dirs of vanished sources |
| 150 |
for d in *; do |
| 151 |
[ -d "$d" ] || continue |
| 152 |
[ -e "${CONFDIR}/sources/$d" ] || rm -fr "$d" |
| 153 |
done |
| 154 |
|
| 155 |
# vim:set ft=sh: |