Drive Mount Script not working when run through Casper

aburrow
Contributor

Through looking at other posts I've come up with this script which works fine when run from a terminal.

I replace $3 with $USER when I run from terminal.

#!/bin/sh 
currentuser=$3
smbhome=`dscl '/Active Directory/CSUMAIN/All Domains' -read /Users/$currentuser SMBHome | awk '{print $2}' | sed -e 's/\\\/smb:///g' | sed 's:\:/:g'`
echo $smbhome
if [ $? != "0" ] ; then 
        echo could not get smb home.  Offline? 
        exit 
fi

if [ "$smbhome" == '' ] ; then 
        echo could not get smb home.  Not defined for user $currentuser? 
        exit 
fi 

#Make the users home folder and mount it...
#Get rid of remnants if there was previously an issue...
rmdir /Volumes/$currentuser
mkdir /Volumes/$currentuser
mount_smbfs $smbhome /Volumes/$currentuser

#Place it in the Dock for the user
defaults write com.apple.dock persistent-others -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Volumes/$currentuser</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"'
#restart the Dock to make sure it's there
killall Dock

exit 0

When I set it up to run through a policy at login I receive a "Script result: dsRecTypeStandard:Users" message and then a URL parsing failed error with mount_smbfs.

I've tried using the $USER variable with similar results.

I'm logging in as a network user not a local account.

1 ACCEPTED SOLUTION

russeller
Contributor III

@Andrina
I dig CocoaDialog, I saw it in another one of your posts. I'm re-writing some of my scripts to incorporate it.

I also am using a script to mount the users network home at login, but I used a launch agent loaded in /Library/LaunchAgents so it'll run for every user at login. The nice thing about using a launch agent that calls a script is that you don't have do "su" the mount command. I included a check in the script to exit if the uid is below 1000 (non ad-users). The other nice thing is that it doesn't require the JSS to run the login policy so it doesn't create ten of thousands of policy logs. You can specify a log file in the launch agent plist so you can troubleshoot issues if needed. Here is a sample of my launch agent plist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>org.companyname.mountserverhome</string>
    <key>LimitLoadToSessionType</key>
    <string>Aqua</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Path/to/Script/script_to_mount_server_home.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/Path/to/errorLog/mount_home_err.log</string>
    <key>StandardOutPath</key>
    <string>/Path/to/standardOut/mount_home.log</string>
</dict>
</plist>

Be sure to set the permissions correctly on the plist or it won't launch.

chown root:wheel /Library/LaunchAgents/org.companyname.mountserverhome.plist
chmod 644 /Library/LaunchAgents/org.companyname.mountserverhome.plist

View solution in original post

3 REPLIES 3

Andrina
Contributor

I've got a very similar script for mounting home directories, I think it's the mount_smbfs line you're running into an issue - this needs to be done as the user, not as root.

i.e.:

su $currentuser -c "mkdir /Volumes/$currentuser; mount -t smbfs -o nodev,nosuid $smbhome /Volumes/$currentuser"

My Script makes use of CocoaDialog as I've got it in Self Service with some feedback, but here's what I use:

#!/bin/bash

#Mount a users home directory outside of the AD Plugin UNC Path
SMBPATH=`dscl localhost read /Active Directory/DOMAIN/All Domains/Users/"$3" SMBHome | awk '{print $2}' | sed -e 's/\\\/smb:///g' | sed 's:\:/:g'`
CD="/Local/Path/To/CocoaDialog.app/Contents/MacOS/CocoaDialog"

#Check User is logged in with AD credentials to allow kerberos mount
dscl localhost read /Search/Users/$USER | grep SMBHome
if [ `echo $?` != 0 ]; then
rv=`$CD ok-msgbox --icon x --text "There was a problem discovering who you are." --informative-text "You don't appear to be logged in with a domain account. Please contact the administrator if you need further assistance." --no-newline --float`
if [ "$rv" == "1" ]; then
echo "User said OK"
elif [ "$rv" == "2" ]; then
echo "Canceling"
exit
fi
exit 0
fi

#Check for the path before trying to mount
ls /Volumes/dfs_dir
if [ `echo $?` == 0 ]; then
rv=`$CD ok-msgbox --icon x --text "There was a problem mounting the requested share." --informative-text "You may already have your Network Home mounted. Please contact the administrator if you need further assistance." --no-newline --float`
if [ "$rv" == "1" ]; then
echo "User said OK"
elif [ "$rv" == "2" ]; then
echo "Canceling"
exit
fi
else

echo $SMBPATH

#Make the users home folder and mount it
rmdir /Volumes/dfs_dir

su $3 -c "mkdir /Volumes/dfs_dir; mount -t smbfs -o nodev,nosuid $SMBPATH /Volumes/dfs_dir"

#Place it in the Dock for the user
su $3 -c 'defaults write com.apple.dock persistent-others -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Volumes/dfs_dir</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"'
#restart the Dock to make sure it's there
killall Dock
fi

russeller
Contributor III

@Andrina
I dig CocoaDialog, I saw it in another one of your posts. I'm re-writing some of my scripts to incorporate it.

I also am using a script to mount the users network home at login, but I used a launch agent loaded in /Library/LaunchAgents so it'll run for every user at login. The nice thing about using a launch agent that calls a script is that you don't have do "su" the mount command. I included a check in the script to exit if the uid is below 1000 (non ad-users). The other nice thing is that it doesn't require the JSS to run the login policy so it doesn't create ten of thousands of policy logs. You can specify a log file in the launch agent plist so you can troubleshoot issues if needed. Here is a sample of my launch agent plist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>org.companyname.mountserverhome</string>
    <key>LimitLoadToSessionType</key>
    <string>Aqua</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Path/to/Script/script_to_mount_server_home.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/Path/to/errorLog/mount_home_err.log</string>
    <key>StandardOutPath</key>
    <string>/Path/to/standardOut/mount_home.log</string>
</dict>
</plist>

Be sure to set the permissions correctly on the plist or it won't launch.

chown root:wheel /Library/LaunchAgents/org.companyname.mountserverhome.plist
chmod 644 /Library/LaunchAgents/org.companyname.mountserverhome.plist

aburrow
Contributor

Thanks guys. I like the idea of the launchagent. The solution by Andrina also works.