#!/bin/bash

#
# Script to synch AD groups with OD groups
#
# It does a decent job but the OD Master must
# be entered into this machine's LDAPv3 plugin
# in Directory Access. Although it does not need
# to be in the authentication path.
#

#
# Rev. 1 8/8/04 - Joel Rennich - Flight to Houston then Honolulu
# Rev. 2 8/18-8/17/04 - Joel Rennich - Crossing the International Date Line and going back in time a day on my way back to Honolulu from Guam
# Rev. 3 8/24/04 - Joel Rennich - Flight to Atlanta
# Rev. 4 9/1/04 - Joel Rennich - New York dodging protestors at the Republican Convention
#

#
# Some definitions
#

AD_GROUP="23498798"                     # This is the group ID of the AD group. NOT the name. Just the GID.
OD_GROUP="othergroup"                   # This is the shortname of the OD Group.
OD_PATH=""             # You'll need to set this
OD_ADMIN=adadmin                        # either supply this here or when you call the script
OD_ADMIN_PASS=test                      # either supply this here or when you call the script
DSCL_IMPORT_FILE=/tmp/dsclimport.tmp    # this should be fine, but change if you need

#
# Version Info
#

VERSION_NUM="1.0"
VERSION=NO

# we need to declare the array we are going to use

declare -a GROUP_MEMBERS

#
# Now for the proceedures
#

# Validate the variables

check_input() {

# This checks to make sure both groups are entered
if [ -z $AD_GROUP ] || [ -z $OD_GROUP ]
then
    echo "You need to enter both group names!"
    exit 0;
fi

# This checks to make sure an OD path is specified
if [ -z $OD_PATH ]
then
    echo "You need to set the Directory Service path to your OD domain"
    exit 0;
fi

}

# This checks the admin pass, and asks for it if we don't have it.

check_admin(){
    if [ ! -z $OD_ADMIN_PASS ]
    then
        echo "How very smart of you to not put your password in an option"
        echo "Please type it now: "
        stty_orig=`stty -g` 
        stty -echo 
        read OD_ADMIN_PASS 
        stty $stty_orig
    fi
}

# This pulls the membership of the AD group
# and formats in a way that dscl will import
# we use lookupd here because dscl barfs on
# the whack in the AD group name

get_group() {

GROUP_MEMBERS=( `lookupd -q group -a gid $AD_GROUP | grep users | sed -e 's/^users:/GroupMembership/' | head -n 1` )

# check to make sure we got something, otherwise bail
# This will cause the script to fail if you have no
# members in your AD group.
# This may not be the behavior that you would like.
# Remove this if necessary.

if [ -z ${GROUP_MEMBERS[1]} ]
then
    echo "AD Group is invalid - no members found"
    exit 0;
fi
}

# This deletes the current group membership
# and then replaces it with the AD group membership

set_group() {

# Grrr, this isn't as elegant as I had hoped but it does work.
# Beauty is in the eye of the coder, after all.

# first create the import script file
echo "create Groups/${OD_GROUP} ${GROUP_MEMBERS[@]}" > $DSCL_IMPORT_FILE
echo "quit" >> $DSCL_IMPORT_FILE

# now load that file authing as an admin
dscl -u $OD_ADMIN -P $OD_ADMIN_PASS $OD_PATH < $DSCL_IMPORT_FILE

# log that we are working
logger "ADODGroupSynch - Adding new members to OD group $OD_GROUP"

# Now to keep things tidy
rm $DSCL_IMPORT_FILE

}

#
# Now the actual script
#

#
# This checks the options the script was called with
#

while getopts u:p:a:o:d: SWITCH
do
    case $SWITCH in
        u) OD_ADMIN=$OPTARG;;
        p) OD_ADMIN_PASS=$OPTARG;;
        a) AD_GROUP=$OPTARG;;
        o) OD_GROUP=$OPTARG;;
        v) VERSION=YES;;
        d) OD_PATH=$OPTARG;;
        *) echo "ADODgroupsynch to synchronize the membership of an AD group with the membership of an OD group"
           echo usage: $PROGRAM [ -u OD Admin ] [ -p OD Admin pass ][ -a AD Group ID ] [ -o OD Group ] [ -d DS path to OD server ]
           echo ""
           echo "You will need to add the OD Master to your LDAPv3 settings"
           echo "of whatever server you are running this on that is connected"
           echo "to AD using the AD plugin"
           echo ""
           echo "examples:"
           echo "ADODgroupsynch -a 9087898734 -o othertest"
           echo "or with the OD admin user/pass included on the CLI"
           echo "ADODgroupsynch -u admin -p adminpass -a 98493842 -o adusers -d /LDAPv3/10.0.1.10"
           exit 1;;
    esac
done

#
# This prints the version and then bails
#

if [ $VERSION = "YES" ]
then
    echo "mailback version $VERSION_NUM"
    exit 1
fi


check_input
check_admin
get_group
set_group

exit 1;
