#!/bin/sh

# /etc/network/if-up.d/nslcd script for signalling network changes
# This file is also compatible with networkd-dispatcher.
#
# Copyright (C) 2013 Arthur de Jong
# Copyright (C) 2021 Sergio Durigan Junior
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

NSLCD_NAME=nslcd
NSLCD_BIN=/usr/sbin/$NSLCD_NAME
NSLCD_CFG=/etc/nslcd.conf
NSLCD_CFG=/etc/nslcd.conf
NSLCD_STATEDIR=/run/nslcd
NSLCD_PIDFILE=$NSLCD_STATEDIR/nslcd.pid

# ignore lo interface
[ -n "$IFACE" ] && [ "$IFACE" = "lo" ] && exit 0

# only do things when starting the interface
[ -n "$MODE" ] && [ "$MODE" != "start" ] && exit 0

# only care about IPv4 and IPv6 interfaces
[ -n "$ADDRFAM" ] && [ "$ADDRFAM" != "inet" ] && [ "$ADDRFAM" != "inet6" ] && exit 0

# package must be installed
[ -x "$NSLCD_BIN" ] || exit 0
[ -f "$NSLCD_CFG" ] || exit 0

# daemon must be running
[ -f "$NSLCD_PIDFILE" ] || exit 0
"$NSLCD_BIN" --check || exit 0

# check daemon version (older versions terminate on SIGUSR1)
VERSION=$("$NSLCD_BIN" --version | sed -n 's/^nss-pam-ldapd \(0\.9\.[1-9][0-9]*\|0\.8\.1[4-9]\)/\1/p')
[ -n "$VERSION" ] || exit 0

# signal the daemon
. /lib/lsb/init-functions
log_action_begin_msg "Sending network state change signal to $NSLCD_NAME"
kill -s USR1 $(cat "$NSLCD_PIDFILE")
log_action_end_msg $?

exit 0
