ldapsaisie/lsexample/restore_lsexample

122 lines
3.7 KiB
Text
Raw Permalink Normal View History

2021-02-24 20:12:03 +01:00
#!/bin/bash
#
# Script to restore LSexample from source
#
# Installation:
# ln -s /path/to/ldapsaisie/src/lsexample/restore_lsexample /usr/local/sbin/
#
2021-07-22 10:58:57 +02:00
# Tools paths
SERVICE=/usr/sbin/service
SLAPADD=/usr/sbin/slapadd
LDAPVI=/usr/bin/ldapvi
2021-02-24 20:12:03 +01:00
# Configuration
SRC_DIR="$( realpath "$( dirname "$( realpath "$0" )" )/../" )"
2021-02-24 20:12:03 +01:00
SLAPD_CONF_DIR=/etc/ldap/slapd.d
SLAPD_DB_DIR=/var/lib/ldap
SLAPD_USER=openldap
SLAPD_GROUP=openldap
SYS_SCHEMA_DIR=/etc/ldap/schema
2021-02-24 20:12:03 +01:00
# Deducted configuration
SLAPD_CONF_LDIF="$SRC_DIR/lsexample/slapd-config.ldif"
LS_SCHEMA_LDIF="$SRC_DIR/lsexample/schema/ls.ldif"
SAMBA_SCHEMA_LDIF="$SRC_DIR/lsexample/schema/samba.ldif"
SLAPD_DB_LDIF="$SRC_DIR/lsexample/db.ldif"
2021-02-24 20:12:03 +01:00
#
# Start restoration
#
2021-07-22 10:58:57 +02:00
# Handle verbose mode
VERBOSE=0
[[ " $@ " =~ " -v " ]] && VERBOSE=1 || exec 1>/dev/null
# Fatal error helper
function fatalerror() {
echo "$@" 1>&2
[ $VERBOSE -eq 0 ] && echo -e "\nPlease run in verbose mode for details." 1>&2
exit 1
}
# Check tools paths
[ ! -x $SERVICE ] && fatalerror "Command service not found ($SERVICE)"
[ ! -x $SLAPADD ] && fatalerror "Command slapadd not found ($SLAPADD). Please check your slapd installation."
[ ! -x $LDAPVI ] && fatalerror "Command ldapvi not found ($LDAPVI). Please install ldapvi debian package."
2021-02-24 20:12:03 +01:00
# Stop slapd
2021-07-22 10:58:57 +02:00
$SERVICE slapd stop
[ $? -ne 0 ] && fatalerror "An error occurred stopping slapd."
2021-02-24 20:12:03 +01:00
2021-07-22 10:58:57 +02:00
# Purge old slapd config & data
[ $VERBOSE -eq 1 ] && echo "Clean slapd configuration and data"
rm -fr $SLAPD_DB_DIR $SLAPD_CONF_DIR
2021-07-22 10:58:57 +02:00
[ $? -ne 0 ] && fatalerror "An error occurred cleaning slapd configuration and data."
mkdir -p $SLAPD_DB_DIR $SLAPD_CONF_DIR
2021-07-22 10:58:57 +02:00
[ $? -ne 0 ] && fatalerror "An error occurred creating slapd configuration and data directories."
2021-02-24 20:12:03 +01:00
2021-07-22 10:58:57 +02:00
# Install slapd configuration
[ $VERBOSE -eq 1 ] && echo "Install initial slapd configuration"
$SLAPADD -n0 -F $SLAPD_CONF_DIR -l $SLAPD_CONF_LDIF 2>&1
[ $? -ne 0 ] && fatalerror "An error occurred loading initial slapd configuration."
2021-02-24 20:12:03 +01:00
# Install schemas
2021-07-22 10:58:57 +02:00
SCHEMA_FILES="
$SYS_SCHEMA_DIR/core.ldif
$SYS_SCHEMA_DIR/cosine.ldif
$SYS_SCHEMA_DIR/nis.ldif
$SYS_SCHEMA_DIR/inetorgperson.ldif
$SYS_SCHEMA_DIR/dyngroup.ldif
$SAMBA_SCHEMA_LDIF
$LS_SCHEMA_LDIF
"
# Extranet ppolicy schema removed in OpenLDAP 2.5, make its installation optional
[ -f "$SYS_SCHEMA_DIR/ppolicy.ldif" ] && SCHEMA_FILES="$SCHEMA_FILES $SYS_SCHEMA_DIR/ppolicy.ldif"
2021-07-22 10:58:57 +02:00
for file in $SCHEMA_FILES
do
[ $VERBOSE -eq 1 ] && echo "Install $( basename $file) schema..."
$SLAPADD -n0 -F/etc/ldap/slapd.d -l$file 2>&1
[ $? -ne 0 ] && fatalerror "An error occurred loading $( basename $file) schema." && exit 1
done
# Fix rights on slapd config & data
[ $VERBOSE -eq 1 ] && echo "Fix slapd configuration and data owner/group"
chown $SLAPD_USER:$SLAPD_GROUP -R $SLAPD_DB_DIR $SLAPD_CONF_DIR
2021-07-22 10:58:57 +02:00
[ $? -ne 0 ] && fatalerror "An error occurred fixing slapd configuration and data owner/group."
2021-02-24 20:12:03 +01:00
# Start slapd
2021-07-22 10:58:57 +02:00
$SERVICE slapd start
[ $? -ne 0 ] && fatalerror "An error occurred starting slapd."
2021-07-22 10:58:57 +02:00
# Install ldapvi config
[ $VERBOSE -eq 1 ] && echo "Install ldapvi configuration"
cat << EOF > /etc/ldapvi.conf
profile default
unpaged-help: yes
ldap-conf: yes
profile config
host: ldapi://
sasl-mech: EXTERNAL
base: cn=config
profile ls
host: ldapi://
sasl-mech: EXTERNAL
base: o=ls
EOF
2021-07-22 10:58:57 +02:00
[ $? -ne 0 ] && fatalerror "An error occurred installing ldapvi configuration."
# Add database
2021-07-22 10:58:57 +02:00
[ $VERBOSE -eq 1 ] && echo "Import lsexample database in slapd" && VERBOSE_ARG="--verbose" || VERBOSE_ARG=""
# Ldapvi need TERM variable is set
TERM=xterm $LDAPVI -p config --noninteractive $VERBOSE_ARG --ldapmodify --ldapvi --add $SLAPD_DB_LDIF
2021-07-22 10:58:57 +02:00
[ $? -ne 0 ] && fatalerror "An error occurred importing lsexample database in slapd."
[ $VERBOSE -eq 1 ] && echo "LSexample successfully restored"
exit 0