#!/bin/bash
#
# File: ldap-load-db
# Description: This script loads data into an ldap database.
# Author: Bill MacAllister <bill@ca-zephyr.org>
# Copyright: 2013 Board of Trustees, Leland Stanford Jr. University.
# Copyright: 2023-2026 CZ Software

# -----------------------------------
# Delete files only if they are there

function del {
    for f in $*
    do
      if [ -d $f ]
          then
          echo "skipping directory $f"
      else
          if [ -e $f ]
              then
              echo "deleteing $f"
              rm $f
          fi
      fi
    done
}

##############################################################################
# Main Routine
##############################################################################

# Set the configuration to use for the load
if [ "$1" = "help" ]
then
    echo "Usage: ldap-load-db [help|manual|files|config]"
    exit 1
fi
if [ "$1" = "manual" ]
then
    pod2text $0
    exit 1
fi

# Get the database to reload
source /etc/ldap-backup.conf
if [ "$DBID" = "" ]
then
    echo "ERROR: environment variable DBID missing"
    exit 1
fi
if [ "$DBID" = "" ]
then
    echo "ERROR: environment variable DBDN missing"
    exit 1
fi

dbDir="/var/lib/ldap/$DBID"
if [ -e $dbDir ]
then
    echo "Cleaning out $dbDir"
else
    echo "ERROR: $dbDir not found"
    exit 1
fi

# Decide on a configuration style to use
if [ "$2" = "files" ]
then
    config="-f /etc/ldap/slapd-cn.conf"
fi
if [ "$2" = "config" ]
then
    config="-F /etc/ldap/slapd.d"
fi

if [ "$config" = "" ]
then
    echo "Using default configuration for load"
else
    echo "Using $config for load"
fi

cd /tmp

dbRoot="/tmp/db-${DBID}.ldif"
dbFile="${dbRoot}.gz"
dbTmp="${dbFile}-tmp"

if [ ! -e $dbFile ]
then
   echo "ERROR: input file not found. ($dbFile)"
   exit 1
fi

del $dbRoot

echo "Uncompressing ..."
/bin/gunzip $dbFile
if [ $? -ne 0 ]
then
    echo "ERROR: problem unzipping input file"
    exit 1
fi

systemctl stop slapd

# Clean out the current database
del /var/lib/ldap/$DBID/*
if [ -e /var/lib/ldap/accesslog ]
then
    del /var/lib/ldap/accesslog/*
else
    mkdir /var/lib/ldap/accesslog
fi

# Empty file system cache.  This most useful for mdb loads to free
# memory, and doesn't hurt bdb loads.
sync
set +o noclobber
echo 3 > /proc/sys/vm/drop_caches

/usr/sbin/slapadd -q $config -b $DBDN -l $dbRoot
mv -v $dbTmp $dbFile

echo "Load complete.  Don't forget to start slapd."

exit

DOCS=<<__END_OF_DOCS__

=head1 NAME

ldap-load-db - Load an OpenLDAP directory

=head1 SYNOPSIS

ldap-load-db <dbID>|help|manual [files|config]

=head1 DESCRIPTION

This script uses slapadd to load an OpenLDAP directory.  The database
directory is assumed to be in /var/lib/ldap.  The accesslog directory
will be created if needed.  The input LDIF file is assumed to be
/tmp/db-<dbID>.gz.

The load process is very careful about the input LDIF file.  It is
backed up before being uncompressed and will be restored if the
process is restarted.

The script reads the configuration file /etc/ldap-backup.conf.
This configuration file is a bash fragment.  The following
variables are used by the script:

    DBID                      - required, no default
    DBDN                      - required, the DN of the LDAP database
    OUT_DIR="/afs/.@cell/backup/ldap/${THIS_YEAR}/${THIS_MONTH}"
                              - the output directory
    THIS_DATE=`date +%Y%m%d`  - date stamp for output
    THIS_YEAR=`date +%Y`      - year stamp for output
    THIS_MONTH=`date +%m`     - month stamp for output
    THIS_HOST=`hostname`      - the host stamp for output file

=head1 OPTIONS

=over 4

=item help

A short help message.

=item manual

This documentation.

=item files

Use the /etc/ldap/slapd.conf configuration file.

=item config

Use the cn=config data base stored in the directory.

=back

=head1 AUTHOR

Bill MacAllister <bill@ca-zephyr.org>

=head1 COPYRIGHT

Copyright 2013 Board of Trustees, Leland Stanford Jr. University.
All rights reserved.

Copyright 2023-2026 CZ Software

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut
__END_OF_DOCS__
