Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # Created by Thilo Enters, Hochschule Karlsruhe - University of Applied Sciences, 2013
- #
- # This script is supposed to support the magic triangle with Mac OS X 10.8.x as it is not
- # that well-supported anymore. It can be used as a login hook or separately to create
- # augmented user records in an Open Directory server.
- #
- #
- # Usage
- # 1) updateAugments.sh <username>
- # The username will be looked up in the Open Directory servers augments.
- # If an augmented record is found for this user, no further changes will be made.
- # If no augmented record is found, the user is looked up in the Active Directory
- # server and an augmented record will be created in the Open Directory.
- # This is the way the script will be called when used as a LoginHook.
- #
- # 2) updateAugments.sh <username> update
- # Same as above, but the augmented record will get updated.
- #
- # 3) updateAugments.sh allAugments
- # This will update all augments that have been created with their newest details
- # from Active Directory. This option is meant to support a change of the home
- # directory server or similar cases.
- # Note: The information written to the log will be less extensive than that when
- # a user logs in.
- #
- #
- # Tested with
- # Client: Mac OS X 10.8.4 on the late 2012 iMac
- # Server: Mac OS X 10.8.4 on the Mac Mini 2012
- # + Server App 2.2.1
- # + Workgroup Manager 10.8
- # + Directory Utilty 4.0
- #
- # Notes
- # The script currently takes two seconds per user.
- #
- # Some symbols for remote keyboard problems: () {} [] ; = " ' / \ # <>
- SCRIPTNAME="Auto Magic Triangle"
- SCRIPTVERSION="3.2"
- LOG="/var/log/LoginHookHska.log"
- # Server details
- ODSERVER="myappleserver.local"
- ODADMIN="diradmin"
- ODADMINPW="secretPassPhrase"
- ADSPATH="/Active Directory/ADS/All Domains"
- FILESERVER="myappleserver.local"
- FILESERVERSHARE="Homes"
- FILESERVERPATHTOSHARE="/"
- # User details
- USERGROUP="20"
- USERSHELL="usr/bin/false"
- # UserShell: /bin/bash
- # input parameters
- USER="$1"
- FORCEUPDATE="$2"
- # Settings for development phase
- DEBUG="0"
- if [[ "$DEBUG" = "1" ]]; then
- clear
- LOG="tmpLog.log"
- rm $LOG
- fi
- # Write version and time
- #say "Login $SCRIPTVERSION"
- echo "----------" >> $LOG
- echo "Running $SCRIPTNAME $SCRIPTVERSION with parameters '$1' '$2'" >> $LOG
- echo " Time: $(date)" >> $LOG
- echo " Host: $(hostname)" >> $LOG
- # Check path to LDAP - important if script is executed
- # on the same server the open directory is running on
- THISHOST=$( hostname )
- if [[ "$ODSERVER" = "$THISHOST" ]]; then
- ODSERVER="127.0.0.1"
- echo "Running on Open Directory Server, using localhost path (${ODSERVER})..." >> $LOG
- fi
- # Parameter check for forcing augment to get updated
- if [[ "${FORCEUPDATE}" == "update" ]]; then
- FORCEUPDATE=1
- else
- FORCEUPDATE=0
- fi
- # -----------------------------
- #
- # FUNCTIONS
- #
- # -----------------------------
- userIsAugmented() {
- # Returns 1 if the first given parameter is an augmented user
- # Returns 2 if the first given parameter is empty
- # Returns 0 if there is no augmented user with the given name
- local pUser="$1"
- local isAugmented
- local retval=0
- # username is empty => abort
- if [[ -z ${pUser} ]]; then
- retval=2
- else
- isAugmented=$( dscl /Search read /Augments/Users:"${pUser}" 2>>$LOG | grep -c RealName )
- # If more than zero lines contain information in the augmented path expression...
- if [[ "$isAugmented" -gt 0 ]]; then
- retval=1
- fi
- fi
- echo "$retval"
- }
- userExistsInActiveDirectory() {
- # Returns 1 if the first given parameter can be found as a username in active directory
- # Returns 0 if there is no augmented user with the given name
- local pUser="$1"
- local userExists
- local retval=0
- # username is empty => abort
- if [[ -z ${pUser} ]]; then
- retval=0
- else
- userExists=$( dscl "${ADSPATH}" -read /Users/"${pUser}" 2>>$LOG | grep -c RealName )
- # If more than zero lines contain information in the Active Directory path...
- if [[ "$userExists" -gt 0 ]]; then
- retval=1
- fi
- fi
- echo "$retval"
- }
- augmentUserFromActiveDirectory() {
- # Arguments: $1 is the username, $2 [optional] may be "silent" or empty
- # Returns 1 if the username in the first argument was augmented/updated in open directory
- # Returns 0 if there is no active directory user with the given name
- # Information about the augments will be written to the log when $2 is not "silent"
- local pUser="$1"
- local silent="$2"
- local userExistsInADS
- local uRealName
- local uUid
- local uGuid
- local retval=0
- # check for silent mode
- if [[ "${silent}" = "silent" ]]; then
- local LOG="/dev/null"
- fi
- # username is empty => abort
- if [[ -z ${pUser} ]]; then
- retval=0
- else
- # Check whether user exists in active directory
- userExistsInADS=$( userExistsInActiveDirectory "$pUser" )
- if [[ "$userExistsInADS" -eq 0 ]]; then
- echo "User $pUser is not in ADS, therefore no augment will be created!" >> $LOG
- retval=0
- else
- echo "Found user $pUser in Active Directory! Gathering user details..." >> $LOG
- uRealName=$( dscl "${ADSPATH}" -read /Users/"${pUser}" RealName )
- uRealName=$( echo "${uRealName}" | tr '\n' ' ' | sed -e "s/^[\ a-zA-Z]*:[\ ]*//g" -e "s/\ *$//g")
- uUid=$( dscl "${ADSPATH}" -read /Users/"${pUser}" UniqueID )
- uUid=$( echo "${uUid}" | tr '\n' ' ' | sed -e "s/^[\ a-zA-Z]*:[\ ]*//g" -e "s/\ *$//g")
- uGuid=$( dscl "${ADSPATH}" -read /Users/"${pUser}" GeneratedUID )
- uGuid=$( echo "${uGuid}" | tr '\n' ' ' | sed -e "s/^[\ a-zA-Z]*:[\ ]*//g" -e "s/\ *$//g")
- echo "User infos for login ${pUser}:" >> $LOG
- echo " Login name: .${pUser}." >> $LOG
- echo " Real name: .${uRealName}." >> $LOG
- echo " Unique ID: .${uUid}." >> $LOG
- echo " User GUID: .${uGuid}." >> $LOG
- # Now create the augment
- dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" >> $LOG ;
- dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" RealName "${uRealName}" >> $LOG ;
- dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" PrimaryGroupID "${USERGROUP}" >> $LOG ;
- dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" GeneratedUID "${uGuid}" >> $LOG ;
- dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" UniqueID "${uUid}" >> $LOG ;
- dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" UserShell "${USERSHELL}" >> $LOG ;
- dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" HomeDirectory "<home_dir><url>afp://"$FILESERVER"/"$FILESERVERSHARE"</url><path>${pUser}</path></home_dir>" >> $LOG ;
- dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" NFSHomeDirectory "/Network/Servers/"$FILESERVER""$FILESERVERPATHTOSHARE""$FILESERVERSHARE"/${pUser}" >> $LOG ;
- echo "Augmented ${uRealName}" >> $LOG
- retval=1
- fi
- fi
- echo "$retval"
- }
- updateAllAugments() {
- # This function will read all augments and update them with the latest data
- # from Active Directory and those that will be set on augmentation. This is
- # useful, if e.g. the server containing the home directories has changed.
- local user
- local userName
- local line
- local augmentDone
- # Get all augments and split by line break
- local dsclout=$( dscl localhost -list /LDAPv3/"$ODSERVER"/Augments )
- local odaugments=(${dsclout//'\n'/ })
- # Go through each line, separate User:<username> to get the username itself
- # and call the function that creates/updates the augment...
- echo "Updating all existing augmented users..." >> $LOG
- for line in "${odaugments[@]}"; do
- user=(${line//:/ })
- userName="${user[1]}"
- echo " Processing $userName..." >> $LOG
- augmentDone=$( augmentUserFromActiveDirectory "$userName" "silent" )
- done
- echo "...update complete." >> $LOG
- }
- # -----------------------------
- #
- # SCRIPT
- #
- # -----------------------------
- # Check if user is augmented already
- if [[ -z ${USER} ]]; then
- echo "No username entered - no changes will be made" >> $LOG
- elif [[ "${USER}" == "allAugments" ]]; then
- echo "Updating all augmented users. This may take a while, so please be patient..."
- updateAllAugments
- else
- augCheck=$( userIsAugmented "$USER" )
- if [[ $augCheck && $FORCEUPDATE -eq 0 ]]; then
- echo "User $USER is already augmented, no change. (FORCEUPDATE=$FORCEUPDATE)" >> $LOG
- else
- # User is not augmented or an update is forced, therefore will be augmented or updated
- echo "User $USER is not augmented or an update is forced (FORCEUPDATE=$FORCEUPDATE)" >> $LOG
- userGotAugmented=$( augmentUserFromActiveDirectory "$USER" )
- fi
- fi
- echo "-- end of script $SCRIPTNAME at $(date)" >> $LOG
- echo "----------" >> $LOG
- # Directly write the log to the terminal for debug purposes
- if [[ "$DEBUG" = "1" ]]; then
- cat $LOG
- fi
Advertisement
Add Comment
Please, Sign In to add comment