summaryrefslogtreecommitdiffstats
path: root/lfldap-update.sh
blob: 50c067f8c024a217eb4a3388df60f4837e5b5c58 (plain) (blame)
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
#! /bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

FILTER="(&(objectClass=lfNetwork))"
DBDIR="/var/db/lfldap"
PREFIX="/usr/local"
HOOKDIR="${PREFIX}/share/lfldap/hooks"
NETWORKS_FILE="${DBDIR}/networks"
NETWORKS_TEMP="${DBDIR}/networks.tmp"

trap cleanup 0
cleanup()
{
	rm -f "$NETWORKS_TEMP"
}

mkdir -p "$DBDIR"
rm -f "$NETWORKS_TEMP"

if [ ! -f "$NETWORKS_FILE" ]; then
	touch "$NETWORKS_FILE"
fi

ldapsearch			\
	-x -s sub		\
	"$FILTER"		\
	cidrPrefix		\
	| awk '/^cidrPrefix: / { print $2 }' >${NETWORKS_TEMP}

if ! [ -s "$NETWORKS_TEMP" ]; then
	printf >&2 '%s: no networks returned; configuration error?\n' "$0"
	exit 1
fi

if ! cmp -s "$NETWORKS_FILE" "$NETWORKS_TEMP"; then
	printf '%s updated:\n\n' "$NETWORKS_FILE"
	diff "$NETWORKS_FILE" "$NETWORKS_TEMP"
	printf '\n'

	mv "$NETWORKS_TEMP" "$NETWORKS_FILE"
else
	rm -f "$NETWORKS_TEMP"
fi

# Run hooks even if the networks didn't change, since the user might want
# new hooks to run.

exit=0

export NETWORKS_FILE

for hook in ${HOOKDIR}/*.sh; do
	sh $hook || exit=1
done

exit $exit