summaryrefslogtreecommitdiffstats
path: root/lfldap-update.sh
diff options
context:
space:
mode:
Diffstat (limited to 'lfldap-update.sh')
-rw-r--r--lfldap-update.sh57
1 files changed, 57 insertions, 0 deletions
diff --git a/lfldap-update.sh b/lfldap-update.sh
new file mode 100644
index 0000000..50c067f
--- /dev/null
+++ b/lfldap-update.sh
@@ -0,0 +1,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