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
|
2021-02-24 20:20:11 +01:00
|
|
|
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
|
2021-07-21 19:42:22 +02:00
|
|
|
SYS_SCHEMA_DIR=/etc/ldap/schema
|
2021-02-24 20:12:03 +01:00
|
|
|
|
|
|
|
# Deducted configuration
|
2021-07-21 19:42:22 +02:00
|
|
|
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"
|
2021-07-21 19:42:22 +02:00
|
|
|
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."
|
2021-07-21 19:42:22 +02:00
|
|
|
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
|
|
|
|
2021-07-21 19:42:22 +02: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
|
|
|
|
"
|
2023-07-19 12:45:24 +02:00
|
|
|
|
|
|
|
# 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"
|
2021-07-21 19:42:22 +02:00
|
|
|
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-21 19:42:22 +02:00
|
|
|
|
2021-07-22 10:58:57 +02:00
|
|
|
# Install ldapvi config
|
|
|
|
[ $VERBOSE -eq 1 ] && echo "Install ldapvi configuration"
|
2021-07-21 19:42:22 +02:00
|
|
|
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."
|
2021-07-21 19:42:22 +02:00
|
|
|
|
|
|
|
# Add database
|
2021-07-22 10:58:57 +02:00
|
|
|
[ $VERBOSE -eq 1 ] && echo "Import lsexample database in slapd" && VERBOSE_ARG="--verbose" || VERBOSE_ARG=""
|
2021-07-23 09:55:05 +02:00
|
|
|
# Ldapvi need TERM variable is set
|
2022-06-08 12:01:27 +02:00
|
|
|
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."
|
2022-06-08 12:01:27 +02:00
|
|
|
|
|
|
|
[ $VERBOSE -eq 1 ] && echo "LSexample successfully restored"
|
|
|
|
exit 0
|