#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb _at_ iredmail.org)
#---------------------------------------------------------------------
# This file is part of iRedMail, which is an open source mail server
# solution for Red Hat(R) Enterprise Linux, CentOS, Debian and Ubuntu.
#
# iRedMail is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iRedMail 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iRedMail. If not, see .
#---------------------------------------------------------------------
# -------------------------------------------------------
# ------------------- OpenLDAP --------------------------
# -------------------------------------------------------
openldap_config()
{
ECHO_INFO "Configure LDAP server: OpenLDAP."
ECHO_DEBUG "Stoping OpenLDAP."
service_control stop ${OPENLDAP_RC_SCRIPT_NAME} >> ${INSTALL_LOG} 2>&1
backup_file ${OPENLDAP_SLAPD_CONF} ${OPENLDAP_LDAP_CONF}
if [ X"${DISTRO}" == X'RHEL' ]; then
# Run slapd with `slapd.conf` instead of `slapd.d`.
if [ X"${DISTRO_VERSION}" == X'7' ]; then
perl -pi -e 's/#(SLAPD_OPTIONS=).*/${1}"-f $ENV{OPENLDAP_SLAPD_CONF}"/' ${OPENLDAP_SYSCONFIG_CONF}
elif [ X"${DISTRO_VERSION}" == X'8' ]; then
perl -pi -e 's#PH_SYS_USER_LDAP#$ENV{SYS_USER_LDAP}#g' ${SAMPLE_DIR}/systemd/slapd.service.d/override.conf
perl -pi -e 's#PH_LDAP_SERVER_HOST#$ENV{LDAP_SERVER_HOST}#g' ${SAMPLE_DIR}/systemd/slapd.service.d/override.conf
perl -pi -e 's#PH_LDAP_SERVER_PORT#$ENV{LDAP_SERVER_PORT}#g' ${SAMPLE_DIR}/systemd/slapd.service.d/override.conf
perl -pi -e 's#PH_OPENLDAP_SLAPD_CONF#$ENV{OPENLDAP_SLAPD_CONF}#g' ${SAMPLE_DIR}/systemd/slapd.service.d/override.conf
cp -rf ${SAMPLE_DIR}/systemd/slapd.service.d /etc/systemd/system/
systemctl daemon-reload
fi
elif [ X"${DISTRO}" == X'DEBIAN' -o X"${DISTRO}" == X'UBUNTU' ]; then
# Add openldap daemon user to 'ssl-cert' group, so that slapd can read SSL key.
usermod -G ssl-cert ${SYS_USER_LDAP}
elif [ X"${DISTRO}" == X'FREEBSD' ]; then
# Start service when system start up.
# 'slapd_enable=YES' is required to start service immediately.
service_control enable 'slapd_enable' 'YES'
service_control enable 'slapd_flags' "-h 'ldapi://%2fvar%2frun%2fopenldap%2fldapi/ ldap://0.0.0.0/ ldaps://0.0.0.0/'"
service_control enable 'slapd_sockets' '/var/run/openldap/ldapi'
elif [ X"${DISTRO}" == X'OPENBSD' ]; then
# Enable TLS/SSL support
cat >> ${RC_CONF_LOCAL} <> ${INSTALL_LOG} 2>&1
perl -pi -e 's#PH_OPENLDAP_LOG_FILE#$ENV{OPENLDAP_LOG_FILE}#g' ${SYSLOG_CONF_DIR}/slapd.conf
#
# modular newsyslog (log rotate) config file
#
cp -f ${SAMPLE_DIR}/freebsd/newsyslog.conf.d/slapd ${LOGROTATE_DIR}
perl -pi -e 's#PH_OPENLDAP_LOG_FILE#$ENV{OPENLDAP_LOG_FILE}#g' ${LOGROTATE_DIR}/slapd
perl -pi -e 's#PH_SYS_USER_SYSLOG#$ENV{SYS_USER_SYSLOG}#g' ${LOGROTATE_DIR}/slapd
perl -pi -e 's#PH_SYS_GROUP_SYSLOG#$ENV{SYS_GROUP_SYSLOG}#g' ${LOGROTATE_DIR}/slapd
elif [ X"${DISTRO}" == X'OPENBSD' ]; then
# '!!' means abort further evaluation after first match
echo -e '!!slapd' >> ${SYSLOG_CONF}
echo -e "*.*\t\t\t\t\t\t${OPENLDAP_LOG_FILE}" >> ${SYSLOG_CONF}
if ! grep "${OPENLDAP_LOG_FILE}" /etc/newsyslog.conf &>/dev/null; then
cat >> /etc/newsyslog.conf <> ${INSTALL_LOG} 2>&1
fi
echo 'export status_openldap_config="DONE"' >> ${STATUS_FILE}
}
openldap_data_initialize()
{
ECHO_DEBUG "Create instance directory for openldap tree: ${LDAP_DATA_DIR}."
mkdir -p ${LDAP_DATA_DIR}
# Get DB_CONFIG.example.
if [ X"${OPENLDAP_DEFAULT_DBTYPE}" == X'hdb' ]; then
cp -f ${OPENLDAP_DB_CONFIG_SAMPLE} ${LDAP_DATA_DIR}/DB_CONFIG
fi
chown -R ${SYS_USER_LDAP}:${SYS_GROUP_LDAP} ${OPENLDAP_DATA_DIR}
chmod -R 0700 ${OPENLDAP_DATA_DIR}
ECHO_DEBUG "Starting OpenLDAP."
service_control restart ${OPENLDAP_RC_SCRIPT_NAME} >> ${INSTALL_LOG} 2>&1
ECHO_DEBUG "Sleep 5 seconds for LDAP daemon initialization ..."
sleep 5
ECHO_DEBUG "Populate LDAP tree."
ldapadd -x -D "${LDAP_ROOTDN}" -w "${LDAP_ROOTPW}" -f ${LDAP_INIT_LDIF} >> ${INSTALL_LOG} 2>&1
cat >> ${TIP_FILE} <> ${STATUS_FILE}
}