#!/bin/sh
# Mounting SMB shares as specified user.
# Required: .mssmbrc file in users home directory (see man mssmbrc)
# with descriped shares and mount points (slightly mirrors /etc/fstab)
# and .nsmbrc file (see man mount_smbfs)
# Usage: mountsmb [-a] [-u user] [-m mountpoint]
# Written by CityCat at 05.02.2003 Version 0.90.1
# Translated to sh at 22.09.2005 by CityCat
# $Id: mountsmb2,v 1.3 2006/05/12 08:14:13 shelton Exp $
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
# Usage printing function
usage()
{
cat << !EOM
MountSMB, a SMB/CIFS shared resources mounter. Version 0.91.1
Usage: mountsmb {-h | -a | -m mountpoint [-u username]}
-h - this (very useful) help screen
-a - mount all resources, marked as "auto"
-m mountpoint - mount only specified mountpoint
-u username - mount as specified username
!EOM
}
# Mounting SMB share function
do_smbmount()
{
set $smbshare
$util_mount_smbfs $mfmode $dfmode //$username@$1/$2 $3
rc=$?
# Check on succesful mounting
if [ $rc -ne 0 ]; then
echo "Mounting $1/$2 to $3 under user rights $username unsuccesful, rc = $rc"
else
echo "Mounting $1/$2 to $3 under user rights $username succesful"
fi
}
# Initial values
allmount=0
effuser=""
mpoint=""
# Parse a command line
args=`getopt ham:u: $*`;
if [ $# -eq 0 ]; then
usage
exit 20
fi
set -- $args
for i in $args
do
case "$i"
in
-h) usage
exit;;
-a) allmount=1;
shift;;
-u) effuser="$2"; shift;
shift;;
-m) mpoint="$2"; shift;
shift;;
--) shift; break;;
esac
done
# Check on presence attendant utilites
wtools="mount mount_smbfs"
for tool in $wtools
do
locator=`whereis -b $tool`
set $locator
if [ -z $2 ]; then
echo "Your system does not include $tool utility, install it firstly"
exit 15
else
eval "util_$tool=$tool"
fi
done
# Checking permissions to mount/umount shares
euid=`id -u`
if [ ! $euid -eq 0 ]; then
echo "Only root can mount or umount SMB share resouces, please use su/sudo"
exit 17
fi
# Checking on presence any parameters. User should specify -a or -m mountpoint
if [ $allmount -eq 0 ] && [ ${#mpoint} -eq 0 ]; then
echo "You should specify -a or -m mountpoint"
exit 18
fi
# Checking on presence .mssmbrc in home directory
if [ ! -e $HOME/.mssmbrc ]; then
echo "File .mssmbrc missed in home directory"
exit 19
fi
# Set username, when it was missed
if [ ${#effuser} -eq 0 ]; then
username=$USER
else
username=$effuser
fi
# Parse .mssmbrc file for newline
strip="
"
saveifs=$IFS
IFS=$strip
smbshares=`cat $HOME/.mssmbrc`
# Check each described share and try to mount it, when -a specified and it still
# didn't mounted. Search specified share and try to mount it, when it still didn't
# mounted.
for smbshare in $smbshares
do
IFS=$saveifs
# Auto-parse line from .mssmbrc file.
# Here:
# $1 - server, $2 - share, $3 - mountpoint, $4 - files mode, $5 - dirs mode
# $6 - sign for auto/manually mounting
set $smbshare
# Check on comment line
comment=`echo $1 | awk '{print substr($0,1,1)}'`
if [ $comment = "#" ]; then
continue
fi
# Ensure, that this share is already mounted
domount=`$util_mount | grep -i $username@$1/$2`
if [ ${#domount} -eq 0 ]; then
# Set file mode as specified. When specified "none" do not apply this parameter
if [ $4 = "none" ]; then
mfmode=""
else
mfmode="-f $4"
fi
# Set directory mode as specified. When specified "none" do not apply this parameter
if [ $5 = "none" ]; then
dfmode=""
else
dfmode="-d $5"
fi
# Do mount this share, when automount is requested
if [ $allmount -eq 1 ] && [ $6 = "auto" ]; then
do_smbmount
continue
fi
# Do mount this share, when mount point was specified exactly and exit
if [ $mpoint = $3 ]; then
do_smbmount
exit
fi
fi
IFS=$strip
done
syntax highlighted by Code2HTML, v. 0.9.1