#!/bin/bash
SHARESDIR=/usr/local/samba
# Samba Control Script
# Version 2.1

# Commands Supported

# List All Users
# ./sambaCtl.sh listusers

# List All Groups
# ./sambaCtl.sh listgroups

# List All Samba Groups for a User
# ./sambaCtl.sh listgroups [username]

# Add User
# ./sambaCtl.sh useradd [username] [password]

# Delete User
# ./sambaCtl.sh userdel [username]

# Add Group
# ./sambaCtl.sh groupadd [groupName]

# Delete Group
# ./sambaCtl.sh groupdel [groupDel]

# Add User to Group
# ./sambaCtl.sh usermod groupadd [username] [groupName]

# Delete User from Group
# ./sambaCtl.sh usermod groupdel [username] [groupName]

# Change User's Password
# ./sambaCtl.sh usermod passwd [username] [password]

# Create a Samba Share (note: this does not add it to the config file)
# ./sambaCtl.sh shareadd [shareName]

# Delete a Samba Share (note: removes all files in it, does not affect config file)
# ./sambaCtl.sh sharedel [shareName]

# Move a Samba Share (note: does not affect config file)
# ./sambaCtl.sh sharemv [oldShareName] [newShareName]

my_useradd() {
	username=${1}
	password=${2}
	# Verify that we have a username and password to add
	if [ "${username}" = "" -o "${password}" = "" ]
	then
		echo "Must pass in username and password of the user to add."
		exit 1
	fi
	# Check to see if user already exists
	if [ ! -z "$(getent passwd ${username})" ]
	then
		echo "That username already exists."
		exit 1
	fi
	# Add the user in Linux
	/usr/sbin/useradd -r -g sambashare -s /bin/false -M -N ${username}
	# Add the user to Samba Database
	echo -en "${password}\n${password}\n" | smbpasswd -as ${username}
	# Done
	exit 0
}

my_userdel() {
	username=${1}
	# Check passed in parameters
	if [ "${username}" = "" ]
	then
		echo "Must pass in username."
		exit 1
	fi
	# Verify the user exists
	if [ -z "$(getent passwd ${username})" ]
	then
		echo "That username does not exist."
		exit 1
	fi
	# Drop them from the Samba database
	smbpasswd -d ${username}
	# End all of their Samba sessions
	for sambaPid in $(smbstatus -p | grep ${username} | awk '{print $1}')
	do
		kill ${sambaPid}
	done
	# Drop them as a Linux user
	/usr/sbin/userdel --force ${username}
	# Done
	exit 0
}

my_groupadd() {
	# Check passed in parameters
	if [ "${1}" = "" ]
	then
		echo "You must pass in a group name"
		exit 1
	fi
	groupname="smb-${1}"
	# Check to see if group already exists
	if [ ! -z "$(getent group ${groupname})" ]
	then
		echo "That group name already exists."
		exit 1
	fi
	# Create the group
	/usr/sbin/groupadd ${groupname}
	# Done
	exit 0
}

my_groupdel(){
	# Check passed in parameters
	if [ "${1}" = "" ]
	then
		echo "You must pass in a group name"
		exit 1
	fi
	groupname="smb-${1}"
	# Make sure the group exists
	if [ -z "$(getent group ${groupname})" ]
	then
		echo "That group does not exist."
		exit 1
	fi
	# Delete the group
	/usr/sbin/groupdel ${groupname}
	# Done
	exit 0
}

my_usermod(){
	action=${1}
	case "${action}" in
	
		"groupadd")
			# Check passed in parameters
			if [ "${2}" = "" -o "${3}" = "" ]
			then
				echo "You must pass in an username and a group name"
				exit 1
			fi
			username=${2}
			groupname="smb-${3}"
			# Make sure the group exists
			if [ -z "$(getent group ${groupname})" ]
			then
				echo "That group does not exist."
				exit 1
			fi
			# Verify the user exists
			if [ -z "$(getent passwd ${username})" ]
			then
				echo "That username does not exist."
				exit 1
			fi
			# Add the user to the group
			/usr/sbin/usermod --append --groups ${groupname} ${username}
			# Done
			exit 0
		;;

		"groupdel")
			# Check passed in parameters
			if [ "${2}" = "" -o "${3}" = "" ]
			then
				echo "You must pass in an username and a group name"
				exit 1
			fi
			username=${2}
			groupname="smb-${3}"
			# Make sure the group exists
			if [ -z "$(getent group ${groupname})" ]
			then
				echo "That group does not exist."
				exit 1
			fi
			# Verify the user exists
			if [ -z "$(getent passwd ${username})" ]
			then
				echo "That username does not exist."
				exit 1
			fi
			# Remove the user from the group
			/usr/sbin/usermod -G $(id -nG ${username} | sed -e "s/${groupname}//" -e "s/ /,/g" -e "s/^,//g" -e "s/,$//g" -e "s/,,/,/g") ${username}
			# Done
			exit 0
		;;

		"passwd")
			username=${2}
			password=${3}
			# Check passed in parameters
			if [ "${username}" = "" -o "${password}" = "" ]
			then
				echo "Must pass in username and password."
				exit 1
			fi
			# Verify the user exists
			if [ -z "$(getent passwd ${username})" ]
			then
				echo "That username does not exist."
				exit 1
			fi
			# Change their password
			echo -en "${password}\n${password}\n" | smbpasswd -s ${username}
			# Done			
			exit 0
		;;

		*)
			echo "Unrecognized action"
			exit 1
		;;
	esac
}

my_shareadd(){
	sharename=${1}
	# Check for valid parameters
	if [ "${sharename}" = "" ]
	then
		echo "Must pass in a share name"
		exit 1
	fi
	# Make sure it doesn't exist already
	lstmp=`ls --format=single-column ${SHARESDIR} | grep -i "${sharename}"`
	if [ "${lstmp}" != "" ]
	then
		echo "Share already exists."
		exit 1
	fi
	# Create the share with the correct permissions
	mkdir ${SHARESDIR}/${sharename}
	chown root:nogroup ${SHARESDIR}/${sharename}
	chmod 777 ${SHARESDIR}/${sharename}
	# Done
	exit 0	
}

my_sharedel(){
	sharename=${1}
	# Check for valid parameters
	if [ "${sharename}" = "" ]
	then
		echo "Must pass in a share name"
		exit 1
	fi
	# Check to see if it exists
	if [ ! -d ${SHARESDIR}/${sharename} ]
	then
		echo "Share does not exist."
		exit 1
	fi
	# Delete the share folder
	rm -rf ${SHARESDIR}/${sharename}
	# Done
	exit 0	
}

my_sharemv(){
	oldsharename=${1}
	newsharename=${2}
	# Check for valid parameters
	if [ "${oldsharename}" = "" -o "${newsharename}" = "" ]
	then
		echo "Must pass in old share name and new share name"
		exit 1
	fi
	# Check to see if it exists
	if [ ! -d ${SHARESDIR}/${oldsharename} ]
	then
		echo "Share does not exist."
		exit 1
	fi
	# Make sure new share name doesn't already exist
	lstmp=`ls --format=single-column ${SHARESDIR} | grep -i "${newsharename}"`
	if [ "${lstmp}" != "" ]
	then
		echo "New share name conflicts with already existing share."
		exit 1
	fi
	# Do the move
	mv ${SHARESDIR}/${oldsharename} ${SHARESDIR}/${newsharename}
	exit 0
}

my_listusers(){
	if [ "${1}" = "" ]
	then
		# Get the group id of the 'sambashare' group
		smbgid=`grep sambashare /etc/group | awk -F: '{print $3;}'`
		# Now list all the users who belong to that group
		awk -F: '{ if($4 == "'${smbgid}'") print $1; } ' /etc/passwd | grep -v "^$"	
	else	
		# Grab the group name
		groupname="smb-${1}"
		# Make sure the group exists
		if [ -z "$(getent group ${groupname})" ]
		then
			echo "That group does not exist."
			exit 1
		fi
		# Grab all the users of that group
		grep ${groupname} /etc/group | awk -F: '{ sub(/,/, "\n", $4); print $4; }' | grep -v "^$"
	fi
}

my_listgroups(){
	username=${1}
	# Check passed in parameters
	if [ "${username}" = "" ]
	then
		# Show all groups
		grep "smb-" /etc/group | awk -F: '{print substr($1,5); }' | grep -v "^$"
	else
		# Verify the user exists
		if [ -z "$(getent passwd ${username})" ]
		then
			echo "That username does not exist."
			exit 1
		fi
		# Echo out their groups
		id -nG ${username} | awk '{ for (i=1; i <= NF; i++){ if( $i ~ /^smb-.*$/ ) print substr($i,5); } }' | grep -v "^$"
	fi
}

if [ "$(id -u)" != "0" ]
then
	echo "You must run this script with superuser privileges."
	exit 1
fi

case "${1}" in

	"useradd")
		my_useradd ${2} ${3}
	;;

	"userdel")
		my_userdel ${2}
	;;

	"groupadd")
		my_groupadd ${2}
	;;

	"groupdel")
		my_groupdel ${2}
	;;

	"usermod")
		my_usermod ${2} ${3} ${4}
	;;

	"shareadd")
		my_shareadd ${2}
	;;

	"sharedel")
		my_sharedel ${2}
	;;

	"sharemv")
		my_sharemv ${2} ${3}
	;;

	"listusers")
		my_listusers ${2}
	;;

	"listgroups")
		my_listgroups ${2}
	;;

	*)
		echo "Unsupported command"
		exit 1
	;;
esac

exit 0
