#!/bin/bash
######################################################################
# perditiondb_ldap_makedb                                     May 2000
# Horms                                             horms@verge.net.au
#
# Sample programme to make an LDAP based popmap
#
# perdition
# Mail retrieval proxy server
# Copyright (C) 1999-2005  Horms
# 
# This program 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 2 of the
# License, or (at your option) any later version.
# 
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307  USA
#
######################################################################


DEFAULT_DC="cn=admin,dc=nodomain"

if [ -z "$(type -path ldapadd)" ]; then
  echo "Could not find ldapadd command. Bailing" >&2
  exit 1
fi

cat << __EOF__

This script expects perdition.schema to be installed and included
in your slapd.conf file. Typically this is done by adding the
following line to /etc/ldap/slapd.conf after any other include
lines and before any database definitions

include         /etc/ldap/schema/perdition.schema

__EOF__

echo -n "Has perdition.schema been added to /etc/ldap/slapd.conf? [y/N]: "
read flim
if [ -z "$flim" -o "$flim" != "y" -a "$flim" != "Y" ]; then
  echo Bailing...
  exit 0
fi


cat << __EOF__

openldap requires a binddn to bind to the LDAP directory
The default is "$DEFAULT_DC". To use this
hit return, otherwise enter a host to connect to, it should be a
string-represented DN as defined in RFC 1779.
__EOF__

echo -n "Enter binddn [$DEFAULT_DC]: " >&2
read dc
if [ -z "$dc" ]; then
  dc="$DEFAULT_DC"
fi


cat << __EOF__

You will need to enter the password for the
binddn: "$dc".
The default may be "secret", you should change
your ldap configuration if this is the case.
__EOF__

echo -n "Enter the password for \"$dc\": " >&2
stty -echo
read rootpw
stty echo
echo

echo "Initialising LDAP popmap..."

ldapadd -x -D "$dc" -w "$rootpw" << __EOF__
dn: ou=mailbox, dc=nodomain
ou: mailbox
objectClass: top
objectClass: organizationalUnit
description: Popmap for perdition

dn: uid=horms, ou=mailbox,  dc=nodomain
objectClass: top
objectClass: uidObject
objectClass: perditionPopmap
uid: horms
username: horms
mailhost: localhost
port: 110
__EOF__

if [ $? != 0 ]; then
  echo "Error initialising LDAP popmap. Bailing" >&2
  exit 1
fi
cat << __EOF__
Done

You may add more entries of the form: 

dn: uid=horms, ou=mailbox, dc=nodomain
objectClass: top
objectClass: uidObject
objectClass: perditionPopmap
uid: horms
username: horms
mailhost: localhost
port: 110

__EOF__


