theten

Auto Magic Triangle with OS X 10.8.4 Mountain Lion

Sep 11th, 2013
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.16 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Created by Thilo Enters, Hochschule Karlsruhe - University of Applied Sciences, 2013
  4. #
  5. # This script is supposed to support the magic triangle with Mac OS X 10.8.x as it is not
  6. # that well-supported anymore. It can be used as a login hook or separately to create
  7. # augmented user records in an Open Directory server.
  8. #
  9. #
  10. # Usage
  11. #  1) updateAugments.sh <username>
  12. #       The username will be looked up in the Open Directory servers augments.
  13. #       If an augmented record is found for this user, no further changes will be made.
  14. #       If no augmented record is found, the user is looked up in the Active Directory
  15. #       server and an augmented record will be created in the Open Directory.
  16. #       This is the way the script will be called when used as a LoginHook.
  17. #
  18. #  2) updateAugments.sh <username> update
  19. #       Same as above, but the augmented record will get updated.
  20. #
  21. #  3) updateAugments.sh allAugments
  22. #       This will update all augments that have been created with their newest details
  23. #       from Active Directory. This option is meant to support a change of the home
  24. #       directory server or similar cases.
  25. #       Note: The information written to the log will be less extensive than that when
  26. #             a user logs in.
  27. #
  28. #
  29. # Tested with
  30. #   Client: Mac OS X 10.8.4 on the late 2012 iMac
  31. #   Server: Mac OS X 10.8.4 on the Mac Mini 2012
  32. #             + Server App 2.2.1
  33. #             + Workgroup Manager 10.8
  34. #             + Directory Utilty 4.0
  35. #
  36. # Notes
  37. #   The script currently takes two seconds per user.
  38. #
  39. # Some symbols for remote keyboard problems: () {} [] ; = " ' / \ # <>
  40.  
  41. SCRIPTNAME="Auto Magic Triangle"
  42. SCRIPTVERSION="3.2"
  43. LOG="/var/log/LoginHookHska.log"
  44.  
  45. # Server details
  46. ODSERVER="myappleserver.local"
  47. ODADMIN="diradmin"
  48. ODADMINPW="secretPassPhrase"
  49.  
  50. ADSPATH="/Active Directory/ADS/All Domains"
  51.  
  52. FILESERVER="myappleserver.local"
  53. FILESERVERSHARE="Homes"
  54. FILESERVERPATHTOSHARE="/"
  55.  
  56. # User details
  57. USERGROUP="20"
  58. USERSHELL="usr/bin/false"
  59. # UserShell: /bin/bash
  60.  
  61. # input parameters
  62. USER="$1"
  63. FORCEUPDATE="$2"
  64.  
  65. # Settings for development phase
  66. DEBUG="0"
  67. if [[ "$DEBUG" = "1" ]]; then
  68.   clear
  69.   LOG="tmpLog.log"
  70.   rm $LOG
  71. fi
  72.  
  73. # Write version and time
  74. #say "Login $SCRIPTVERSION"
  75. echo "----------" >> $LOG
  76. echo "Running $SCRIPTNAME $SCRIPTVERSION with parameters '$1' '$2'" >> $LOG
  77. echo "  Time: $(date)" >> $LOG
  78. echo "  Host: $(hostname)" >> $LOG
  79.  
  80. # Check path to LDAP - important if script is executed
  81. # on the same server the open directory is running on
  82. THISHOST=$( hostname )
  83.  
  84. if [[ "$ODSERVER" = "$THISHOST" ]]; then
  85.   ODSERVER="127.0.0.1"
  86.   echo "Running on Open Directory Server, using localhost path (${ODSERVER})..." >> $LOG
  87. fi
  88.  
  89. # Parameter check for forcing augment to get updated
  90. if [[ "${FORCEUPDATE}" == "update" ]]; then
  91.   FORCEUPDATE=1
  92. else
  93.   FORCEUPDATE=0
  94. fi
  95.  
  96. # -----------------------------
  97. #
  98. #    FUNCTIONS
  99. #
  100. # -----------------------------
  101.  
  102. userIsAugmented() {
  103.   # Returns 1 if the first given parameter is an augmented user
  104.   # Returns 2 if the first given parameter is empty
  105.   # Returns 0 if there is no augmented user with the given name
  106.  
  107.   local pUser="$1"
  108.   local isAugmented
  109.   local retval=0
  110.  
  111.   # username is empty => abort
  112.   if [[ -z ${pUser} ]]; then
  113.     retval=2
  114.   else
  115.     isAugmented=$( dscl /Search read /Augments/Users:"${pUser}" 2>>$LOG | grep -c RealName )
  116.    
  117.     # If more than zero lines contain information in the augmented path expression...
  118.     if [[ "$isAugmented" -gt 0 ]]; then
  119.       retval=1
  120.     fi
  121.   fi
  122.  
  123.   echo "$retval"
  124. }
  125.  
  126. userExistsInActiveDirectory() {
  127.   # Returns 1 if the first given parameter can be found as a username in active directory
  128.   # Returns 0 if there is no augmented user with the given name
  129.   local pUser="$1"
  130.   local userExists
  131.   local retval=0
  132.  
  133.   # username is empty => abort
  134.   if [[ -z ${pUser} ]]; then
  135.     retval=0
  136.   else
  137.     userExists=$( dscl "${ADSPATH}" -read /Users/"${pUser}" 2>>$LOG | grep -c RealName )
  138.  
  139.     # If more than zero lines contain information in the Active Directory path...
  140.     if [[ "$userExists" -gt 0 ]]; then
  141.       retval=1
  142.     fi
  143.   fi
  144.  
  145.   echo "$retval"
  146. }
  147.  
  148. augmentUserFromActiveDirectory() {
  149.   # Arguments: $1 is the username, $2 [optional] may be "silent" or empty
  150.   # Returns 1 if the username in the first argument was augmented/updated in open directory
  151.   # Returns 0 if there is no active directory user with the given name
  152.   # Information about the augments will be written to the log when $2 is not "silent"
  153.   local pUser="$1"
  154.   local silent="$2"
  155.   local userExistsInADS
  156.   local uRealName
  157.   local uUid
  158.   local uGuid
  159.   local retval=0
  160.  
  161.   # check for silent mode
  162.   if [[ "${silent}" = "silent" ]]; then
  163.     local LOG="/dev/null"
  164.   fi
  165.    
  166.   # username is empty => abort
  167.   if [[ -z ${pUser} ]]; then
  168.     retval=0
  169.   else
  170.     # Check whether user exists in active directory
  171.     userExistsInADS=$( userExistsInActiveDirectory "$pUser" )
  172.  
  173.     if [[ "$userExistsInADS" -eq 0 ]]; then
  174.       echo "User $pUser is not in ADS, therefore no augment will be created!" >> $LOG
  175.       retval=0
  176.     else
  177.       echo "Found user $pUser in Active Directory! Gathering user details..." >> $LOG
  178.       uRealName=$( dscl "${ADSPATH}" -read /Users/"${pUser}" RealName )
  179.       uRealName=$( echo "${uRealName}" | tr '\n' ' ' | sed -e "s/^[\ a-zA-Z]*:[\ ]*//g" -e "s/\ *$//g")
  180.  
  181.       uUid=$( dscl "${ADSPATH}" -read /Users/"${pUser}" UniqueID )
  182.       uUid=$( echo "${uUid}" | tr '\n' ' ' | sed -e "s/^[\ a-zA-Z]*:[\ ]*//g" -e "s/\ *$//g")
  183.  
  184.       uGuid=$( dscl "${ADSPATH}" -read /Users/"${pUser}" GeneratedUID )
  185.       uGuid=$( echo "${uGuid}" | tr '\n' ' ' | sed -e "s/^[\ a-zA-Z]*:[\ ]*//g" -e "s/\ *$//g")
  186.  
  187.       echo "User infos for login ${pUser}:" >> $LOG
  188.       echo " Login name: .${pUser}." >> $LOG
  189.       echo " Real name:  .${uRealName}." >> $LOG
  190.       echo " Unique ID:  .${uUid}." >> $LOG
  191.       echo " User GUID:  .${uGuid}." >> $LOG
  192.  
  193.       # Now create the augment
  194.       dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" >> $LOG ;
  195.       dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" RealName "${uRealName}" >> $LOG ;
  196.       dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" PrimaryGroupID "${USERGROUP}" >> $LOG ;
  197.       dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" GeneratedUID "${uGuid}" >> $LOG ;
  198.       dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" UniqueID "${uUid}" >> $LOG ;
  199.       dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" UserShell "${USERSHELL}" >> $LOG ;
  200.       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 ;
  201.       dscl -u "$ODADMIN" -P "$ODADMINPW" /LDAPv3/"$ODSERVER" create /Augments/Users:"${pUser}" NFSHomeDirectory "/Network/Servers/"$FILESERVER""$FILESERVERPATHTOSHARE""$FILESERVERSHARE"/${pUser}" >> $LOG ;
  202.  
  203.       echo "Augmented ${uRealName}" >> $LOG
  204.       retval=1
  205.     fi
  206.   fi
  207.  
  208.   echo "$retval"
  209. }
  210.  
  211. updateAllAugments() {
  212.   # This function will read all augments and update them with the latest data
  213.   # from Active Directory and those that will be set on augmentation. This is
  214.   # useful, if e.g. the server containing the home directories has changed.
  215.   local user
  216.   local userName
  217.   local line
  218.   local augmentDone
  219.  
  220.   # Get all augments and split by line break
  221.   local dsclout=$( dscl localhost -list /LDAPv3/"$ODSERVER"/Augments )
  222.   local odaugments=(${dsclout//'\n'/ })
  223.  
  224.   # Go through each line, separate User:<username> to get the username itself
  225.   # and call the function that creates/updates the augment...
  226.   echo "Updating all existing augmented users..." >> $LOG
  227.   for line in "${odaugments[@]}"; do
  228.     user=(${line//:/ })
  229.     userName="${user[1]}"
  230.    
  231.     echo "  Processing $userName..." >> $LOG
  232.     augmentDone=$( augmentUserFromActiveDirectory "$userName" "silent" )
  233.   done
  234.   echo "...update complete." >> $LOG
  235. }
  236.  
  237. # -----------------------------
  238. #
  239. #    SCRIPT
  240. #
  241. # -----------------------------
  242.  
  243. # Check if user is augmented already
  244. if [[ -z ${USER} ]]; then
  245.   echo "No username entered - no changes will be made" >> $LOG
  246. elif [[ "${USER}" == "allAugments" ]]; then
  247.   echo "Updating all augmented users. This may take a while, so please be patient..."
  248.   updateAllAugments
  249. else
  250.   augCheck=$( userIsAugmented "$USER" )
  251.   if [[ $augCheck && $FORCEUPDATE -eq 0 ]]; then
  252.     echo "User $USER is already augmented, no change. (FORCEUPDATE=$FORCEUPDATE)" >> $LOG
  253.   else
  254.     # User is not augmented or an update is forced, therefore will be augmented or updated
  255.     echo "User $USER is not augmented or an update is forced (FORCEUPDATE=$FORCEUPDATE)" >> $LOG
  256.     userGotAugmented=$( augmentUserFromActiveDirectory "$USER" )
  257.   fi
  258. fi
  259.  
  260. echo "-- end of script $SCRIPTNAME at $(date)" >> $LOG
  261. echo "----------" >> $LOG
  262.  
  263. # Directly write the log to the terminal for debug purposes
  264. if [[ "$DEBUG" = "1" ]]; then
  265.   cat $LOG
  266. fi
Advertisement
Add Comment
Please, Sign In to add comment