#!/bin/sh

# This script imports user accounts from 1C:ChronoGraph XML file
# (http://www.school.chronobus.ru/about/about.htm)
# into LDAP database

. shell-error

SLAPD_HDB_PREFIX="/etc/openldap/slapd-hdb"

# Prints short usage information.
usage()
{

  echo "Usage: $0 [options] [1C:ChronoGraph XML file name]" 1>&2
  echo "Options:"
  echo '    -b DN | --base-dn=DN         LDAP database base DN;'
  echo '    -D DN | --bind-dn=DN         LDAP admin bind DN;'
  echo '    -w PW | --bind-pw=PW         LDAP admin password (PW);'
  echo '    -W | --read-pw               read LDAP admin password from stdin;'
  echo '    -s SID | --sid=SID           generate accounts with additional'
  echo '                                 Samba paramaters using the SID value;'
  echo '    -u UID | --min-uid=UID       minimal UID number;'
  echo '    -g GID | --min-gid=GID       minimal GID number;'
  echo '    -r | --reset-password        set user name as UNIX password;'
  echo '    -R | --reset-smb-password    set user name as Samba password;'
  echo '    -h | --help | --usage        show this message.'
}

# Parse and format the arguments with the help of `getopt'.
OPTS=`getopt -n $0 \
    -l base-dn:,bind-dn:,bind-pw:,read-pw,sid:,min-uid:,min-gid:,reset-password,reset-smb-password,help \
    -o b:D:w:Ws:u:g:rRh -- $@`
RES=$?
# Exit on error.
if [ "$RES" -ne 0 ]; then
    usage
    exit $RES
fi

BASEDN=
BINDDN=
BINDPW=
READPW=
SID=
RESET_PASSWD=
RESET_SMB_PASSWD=
# Parses the formatted arguments to the variables.
parse_opts()
{
    SUFFIX=
    local x=0
    while [ "$x" -eq 0 ] || shift; do
	case "$1" in
	    -b|--base-dn)
		BASEDN=$2
		shift
		;;
	    -D|--bind-dn)
		BINDDN=$2
		shift
		;;
	    -w|--bind-pw)
		BINDPW=$2
		shift
		;;
	    -W|--read-pw)
		READPW=1
		;;
	    -s|--sid)
		SID=$2
		SUFFIX='-smb'
		shift
		;;
	    -u|--min-uid)
		MINUID=$2
		shift
		;;
	    -g|--min-gid)
		MINGID=$2
		shift
		;;
	    -r|--reset-password)
		RESET_PASSWD='-r'
		;;
	    -R|--reset-smb-password)
		RESET_SMB_PASSWD='-R'
		;;
	    -h|--help|--usage)
		usage
		exit 0
		;;
	    --)
		XML=$2
		break
		;;
	esac
	x=1
    done
}

# Parse the options using the helper func.
eval parse_opts `eval echo $OPTS`

XSLFILE="/usr/share/1c-tools/chronograph2ldif${SUFFIX}.xsl"
[ -n "$XML" ] || XML="-"

# use default base that match domain name
[ -n "$BASEDN" ] || BASEDN="$(hostname -d | sed -e 's/\([^\.]\+\)\./dc=\1,/g' -e 's/,\([^,]\+\)$/,dc=\1/')"

[ -n "$READPW" ] && read BINDPW

[ -n "$BINDDN" ] || fatal "No administrator DN specified. Use -D or --bind-dn arguments."
[ -n "$BINDPW" ] || fatal "No administrator password specified. Use -w, -W or --bind-pw,--read-pw arguments."

# save LDIF to a tempfile for later processing
TMPFILE="$(mktemp -t "${0##*/}.XXXXXXXXXX")" || fatal "can't create tempfile"
PARAMS="--param dn-base \"$BASEDN\""
[ -n "$SID" ] && PARAMS="$PARAMS --param sid \"$SID\""
[ -n "$MINUID" ] && PARAMS="$PARAMS --param minuid \"$MINUID\""
[ -n "$MINGID" ] && PARAMS="$PARAMS --param mingid \"$MINGID\""
xsltproc $PARAMS "$XSLFILE" "$XML" |
    perl "/usr/share/1c-tools/reset-password.pl" "$RESET_PASSWD" "$RESET_SMB_PASSWD" > "$TMPFILE"

# add users/groups to the base
pwfile="$(mktemp -t pwfile.XXXXXX)"
chmod 0600 "$pwfile"
printf '%s' "$BINDPW" > "$pwfile"
ldapadd -a -D "$BINDDN" -y "$pwfile" -x -f "$TMPFILE"
if [ $? -ne 0 ]; then
    rm -f "$TMPFILE"
    rm -f "$pwfile"
    fatal "Error registering entries"
fi
rm -f "$pwfile"

# get Postfix spool directory
spool="$(/usr/sbin/postconf -h mail_spool_directory)"

if [ ! -n "$spool" ]; then
	message "unknown spool directory, using default"
	spool="/var/mail"
fi

# read commented user/uid/gid from LDIF file
cat "$TMPFILE" | grep "^###" | while read comment user uid gid; do

	# create a home directory
	if [ -d "/home/$user" ]; then
	    message "home directory '$user' already exists"
	else
	    cp -r /etc/skel "/home/$user"
	    chown -R "$uid:$gid" "/home/$user"
	fi

	# create a mail spool file
	if [ -r "$spool/$user" ];then
		message "mail spool '$user' already exists"
	else
		install -m 0660 -o "$uid" -g mail /dev/null "$spool/$user"
	fi
done

# remove tempfile
rm -f "$TMPFILE"
