summaryrefslogtreecommitdiff
path: root/update-openssh-known-hosts
blob: 00d74a6a954dbcb7d3a0ea6c131a20ca7e65dba7 (plain)
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
#!/bin/bash

set -euC

CONFDIR=/etc/openssh-known-hosts
PLUGIN_PATH=/usr/share/openssh-known-hosts/plugins:/usr/local/share/openssh-known-hosts/plugins
CACHEDIR=/var/cache/openssh-known-hosts
LOCK=/var/lock/openssh-known-hosts
OUTFILE=/var/lib/openssh-known-hosts/ssh_known_hosts

path_search () {
	search="$1"
	shift
	local IFS
	IFS=:
	set -- $@
	if [ "${search}" != "${search#*/}" ]; then
		echo "${search}"
		return 0
	fi
	for path; do
		if [ -f "${path}/${search}" ]; then
			echo "${path}/${search}"
			return 0
		fi
	done
	echo "'${search}' not found in '$@'!" >&2
	exit 127
}

cleanup () {
	rm -f "${OUTFILE}.new"
	kill "${LOCKPID}"
	lockfile-remove "${LOCK}"
}

if [ $# = 1 ] && [ "$1" = "-f" ]; then
	fail=1
else
	fail=''
fi

trap cleanup EXIT

lockfile-create "${LOCK}"
lockfile-touch "${LOCK}" &
LOCKPID="$!"

mkdir -p "${CACHEDIR}"
cd "${CACHEDIR}"

find -mindepth 2 -maxdepth 2 -type f -name new -delete

run-parts --list "${CONFDIR}/sources/" | while read sourcefile; do
	source=`basename ${sourcefile}`
	mkdir -p ${source}
	(
		set -a
		cd ${source}
		. "${sourcefile}"
		`path_search "$PLUGIN" "$PLUGIN_PATH"` >| log 2>&1 || {
			exitcode=$?
			rm -f new
			ignore=''
			for e in ${EXIT_IGNORE:-0}; do
				if [ "$e" = "$exitcode" ]; then
					ignore=1
					break
				fi
			done
			if [ "$ignore" != "1" -o "$fail" = "1" ]; then
				echo "${source} exited with code ${exitcode}, log follows:"
				cat log
				echo
			fi
			if [ "$fail" = "1" ]; then
				exit 1
			fi
		}
	) || exit 1
	if [ -e ${source}/new ]; then
		mv ${source}/new ${source}/current
	fi
	if [ -e ${source}/current ]; then
		cat ${source}/current >&3
	fi
done 3>&1 | sort -u >| "${OUTFILE}.new"

if cmp -s "${OUTFILE}" "${OUTFILE}.new"; then
	rm "${OUTFILE}.new"
else
	mv "${OUTFILE}.new" "${OUTFILE}"
fi

# clean up cache dirs of vanished sources
for d in *; do
	[ -d $d ] || continue
	[ -e "${CONFDIR}/sources/$d" ] || rm -fr $d
done

# vim:set ft=sh: