#!/usr/bin/bash
# $Header: /code/convert/cvsroot/infrastructure/diradm/src/diradm.in,v 1.27 2007/03/30 09:03:51 robbat2 Exp $
# vim: ts=4 sts=4 noexpandtab sw=4 ft=sh syntax=sh:
################################################################################
#
# diradm: A tool for managing posix-like things in a LDAP directory. It uses ldap[add|modify|delete] from the OpenLDAP project.
#
# Version:	2.9.7.1
#
# Original Copyright (C) 2003  Daniel Himler <dan@hits.at>
# Modifications Copyright (C) 2003,2004 by Robin Johnson <robbat2@gentoo.org> and
# Patrick Lougheed <pat@tfsb.org>.
#
# 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
#
################################################################################
		
# Misc docs used to write this
# host - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/hosts.htm#seg3b0sara
# network - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/networks_NFS.htm#idx232
# protocol - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/protocols.htm#idx546
# service - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/services.htm#idx574
# rpc - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/rpc.htm#idx286
# ether - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/ethers.htm#idx111
# netmask - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/netmasks.htm#idx226
# netgroup - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/netgroup.htm#xqr310mart
# bootparam - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/bootparams.htm#idx32
# automount - ? http://publib.boulder.ibm.com/infocenter/pseries/index.jsp?topic=/com.ibm.aix.doc/aixbman/nisplus/nis_automount.htm
# aliases - http://publib16.boulder.ibm.com/pseries/en_US/files/aixfiles/aliases.htm#idx12

# populated by configure...
prefix="/usr"
exec_prefix="${prefix}"
bindir="${exec_prefix}/bin"
sbindir="${exec_prefix}/sbin"
# the above actually don't get used
# but these ones do
[ -z "${sysconfdir}" ] && sysconfdir="/etc"
[ -z "${libexecdir}" ] && libexecdir="${exec_prefix}/libexec"
VERSION="2.9.7.1"
# This is the path to the script bundled with diradm
# we use it to generate the passwords needed quickly.
MKPASSWD="${libexecdir}/diradm-mkpasswd.pl"

# for recursive calls
#DIRADM="${0}"

# find the config file
# 1. look in SYSCONFDIR
# 2. look in same directory as this script
# 3. look in ~ with prefix '.'
CONFIG_FILENAME="diradm.conf"
CONFIG="${sysconfdir}/${CONFIG_FILENAME}"
#if [ "${1}" == "-c" ]; then
#	CONFIG="${2}"
#	shift 2
#fi
skip=0
for arg in ${*}; do
	if [ $skip -gt 0 ]; then
		skip=$(($skip - 1))
		continue
	fi
	case "${arg}" in
		-c|-C)
			CONFIG="${2}"
			skip=1
			shift 2
			;;
		-m|-M)
			MKPASSWD="${2}"
			skip=1
			shift 2
			;;
		*)
			# continue outside now
			break
			;;
	esac
done
[ ! -f "${CONFIG}" ] && CONFIG="`dirname ${0}`/${CONFIG_FILENAME}"
[ ! -f "${CONFIG}" ] && CONFIG="~/.${CONFIG_FILENAME}"

##################### Don't touch anything below this line #####################

if [ ! -r "${CONFIG}" ]; then
	echo "Unable to open configuration file \"${CONFIG}\"!"
	exit 1
fi

# filled by configure
AWK="/usr/bin/gawk"
BASHSH="/usr/bin/bash"
CHMOD="/usr/bin/chmod"
CHOWN="/usr/bin/chown"
CUT="/usr/bin/cut"
DATE="/usr/bin/date"
EGREP="/usr/bin/egrep"
FALSE="/usr/bin/false"
FGREP="/usr/bin/fgrep"
GETENT="/usr/bin/getent"
GREP="/usr/bin/grep"
HEAD="/usr/bin/head"
LDAPADD="/usr/bin/ldapadd"
LDAPDELETE="/usr/bin/ldapdelete"
LDAPMODIFY="/usr/bin/ldapmodify"
LDAPSEARCH="/usr/bin/ldapsearch"
PERL="/usr/bin/perl"
SAMBA_NET=""
SED="/usr/bin/sed"
SEDEXT="/usr/bin/sed -r"
SORT="/usr/bin/sort"
STAT="/usr/bin/stat"
TAIL="/usr/bin/tail"
UNIQ="/usr/bin/uniq"
WC="/usr/bin/wc"
WHOAMI="/usr/bin/whoami"

# bring in config file
source "${CONFIG}"
# These are the share functions
source "${libexecdir}/diradm.common.sh"
# always bring these in, as they are for help/output stuff.
source "${libexecdir}/diradm.misc.sh"

OPTIONS="${LDAPOPTS} -H ${LDAPURI}"
if [ -n "$DEBUG" ]; then
	OPTIONS="${OPTIONS} -v"
fi
ADMINOPTIONS="${OPTIONS} -D ${BINDDN}"
if [ -n "${BINDPASS}" ]; then
	ADMINOPTIONS="${ADMINOPTIONS} -w ${BINDPASS}"
else
	ADMINOPTIONS="${ADMINOPTIONS} -W"
fi
#echo $ADMINOPTIONS
LDAPSEARCH_CMD="${LDAPSEARCH} ${ADMINOPTIONS} -z max"
LDAPADD_CMD="${LDAPADD} ${ADMINOPTIONS}"
LDAPMODIFY_CMD="${LDAPMODIFY} -c ${ADMINOPTIONS}"
LDAPDELETE_CMD="${LDAPDELETE} ${ADMINOPTIONS}"

commands_user="useradd usermod userdel chsh chfn chage passwd"
commands_group="groupadd groupmod groupdel gpasswd"
commands_samba="smbhostadd smbhostdel"
commands_host="hostadd hostdel hostmod"
commands_misc="help version check _header"
commands_automount="amadd amdel ammapadd ammapdel"
valid_commands="${commands_user} ${commands_group} ${commands_host} ${commands_misc}"
[ "$ENABLE_SAMBA" == "yes" ] && valid_commands="${valid_commands} ${commands_samba}"
[ "$ENABLE_AUTOMOUNT" == "yes" ] && valid_commands="${valid_commands} ${commands_automount}"

# check for the correct command
ACTION=""
for vc in ${valid_commands}; do
	if [ "x${vc}" == "x${1}" ]; then
		ACTION="${vc}"
		break
	fi
done
# now bring in just the set we need
case ${ACTION} in
	useradd|usermod|userdel|chsh|chfn) source ${libexecdir}/diradm.user.sh ;;
	chage|passwd) source ${libexecdir}/diradm.shadow.sh ;;
	groupadd|groupmod|groupdel|gpasswd) source ${libexecdir}/diradm.group.sh ;;
	smbhostadd|smbhostdel) source ${libexecdir}/diradm.samba.sh ;;
	hostadd|hostdel|hostmod) source ${libexecdir}/diradm.host.sh  ;;
	amadd|amdel|ammapadd|ammapdel) source ${libexecdir}/diradm.automount.sh ;;
	check) source ${libexecdir}/diradm.check.sh ;;
esac
# now run it
if [ -n "${ACTION}" ]; then
	shift 1
	${ACTION} "$@"
	exit 0
else
	help
	exit 1
fi

# should not ever reach this point
exit 3
