summaryrefslogtreecommitdiff
path: root/update-known-hosts
blob: 14f873f2c42217bacca166b4b0a5c467af586b0a (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
#!/bin/dash

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 () {
	if [ "${1}" != "${1#*/}" ]; then
		echo $1
	else
		echo "$2" | tr ':' '\n' | while read -r path; do
			if [ -f "${path}/${1}" ]; then
				echo "${path}/${1}"
				break
			fi
		done
	fi
}

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

cd "${CACHEDIR}"

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

run-parts --list "${CONFDIR}/sources/" | while read source; do
	source=`basename ${source}`
	mkdir -p ${source}
	(
		set -a
		cd ${source}
		. "${CONFDIR}/sources/${source}"
		`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" ]; then
				echo "${source} exited with code ${exitcode}, log follows:"
				cat log
				echo
			fi
		}
	)
	if [ -e ${source}/new ]; then
		mv ${source}/new ${source}/current
	fi
	if [ -e ${source}/current ]; then
		cat ${source}/current >&3
	fi
done 3>| "${OUTFILE}.new"

mv "${OUTFILE}.new" "${OUTFILE}"

for d in *; do
	[ -d $d ] || continue
	[ -e "${CONFDIR}/sources/$d" ] || rm -fr $d
done

kill "${LOCKPID}"
lockfile-remove "${LOCK}"

# vim:set ft=sh: