samba/samba.sh

172 lines
5.6 KiB
Bash
Raw Normal View History

2015-01-02 07:03:52 +03:00
#!/usr/bin/env bash
#===============================================================================
# FILE: samba.sh
#
# USAGE: ./samba.sh
#
# DESCRIPTION: Entrypoint for samba docker container
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: David Personette (dperson@gmail.com),
# ORGANIZATION:
# CREATED: 09/28/2014 12:11
# REVISION: 1.0
#===============================================================================
set -o nounset # Treat unset variables as an error
### import: import a smbpasswd file
# Arguments:
# file) file to import
# Return: user(s) added to container
import() { local name id file="${1}"
while read name id; do
useradd "$name" -M -u "$id"
done < <(cut -d: -f1,2 --output-delimiter=' ' $file)
pdbedit -i smbpasswd:$file
}
### perms: fix ownership and permissions of share paths
# Arguments:
# none)
# Return: result
perms() { local i file=/etc/samba/smb.conf
2016-06-21 20:46:12 +03:00
for i in $(awk -F ' = ' '/ path = / {print $2}' $file); do
chown -Rh smbuser. $i
find $i -type d -exec chmod 775 {} \;
find $i -type f -exec chmod 664 {} \;
done
}
2015-01-02 07:03:52 +03:00
### share: Add share
# Arguments:
# share) share name
# path) path to share
# browsable) 'yes' or 'no'
2015-01-02 07:03:52 +03:00
# readonly) 'yes' or 'no'
# guest) 'yes' or 'no'
# users) list of allowed users
2015-12-17 14:17:34 +03:00
# admins) list of admin users
2015-01-02 07:03:52 +03:00
# Return: result
share() { local share="$1" path="$2" browsable=${3:-yes} ro=${4:-yes} \
2016-01-05 14:48:01 +03:00
guest=${5:-yes} users=${6:-""} admins=${7:-""} \
file=/etc/samba/smb.conf
2015-04-12 18:59:23 +03:00
sed -i "/\\[$share\\]/,/^\$/d" $file
2015-10-24 14:57:47 +03:00
echo "[$share]" >>$file
echo " path = $path" >>$file
echo " browsable = $browsable" >>$file
2015-10-24 14:57:47 +03:00
echo " read only = $ro" >>$file
echo " guest ok = $guest" >>$file
[[ ${users:-""} && ! ${users:-""} =~ all ]] &&
2015-10-24 14:57:47 +03:00
echo " valid users = $(tr ',' ' ' <<< $users)" >>$file
[[ ${admins:-""} && ! ${admins:-""} =~ none ]] &&
echo " admin users = $(tr ',' ' ' <<< $admins)" >>$file
2015-10-24 14:57:47 +03:00
echo -e "" >>$file
2015-01-02 07:03:52 +03:00
}
### timezone: Set the timezone for the container
# Arguments:
# timezone) for example EST5EDT
# Return: the correct zoneinfo file will be symlinked into place
timezone() { local timezone="${1:-EST5EDT}"
[[ -e /usr/share/zoneinfo/$timezone ]] || {
echo "ERROR: invalid timezone specified: $timezone" >&2
2015-01-02 07:03:52 +03:00
return
}
if [[ -w /etc/timezone && $(cat /etc/timezone) != $timezone ]]; then
2015-10-24 14:57:47 +03:00
echo "$timezone" >/etc/timezone
ln -sf /usr/share/zoneinfo/$timezone /etc/localtime
2015-08-19 13:28:21 +03:00
dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
2015-01-02 07:03:52 +03:00
}
### user: add a user
# Arguments:
# name) for user
# password) for user
# Return: user added to container
2015-04-12 18:59:23 +03:00
user() { local name="${1}" passwd="${2}"
2015-01-02 07:03:52 +03:00
useradd "$name" -M
echo "$passwd" | tee - | smbpasswd -s -a "$name"
}
### workgroup: set the workgroup
# Arguments:
# workgroup) the name to set
# Return: configure the correct workgroup
workgroup() { local workgroup="${1}" file=/etc/samba/smb.conf
2016-05-15 14:39:51 +03:00
sed -i 's|^\( *workgroup = \).*|\1'"$workgroup"'|' $file
}
2015-01-02 07:03:52 +03:00
### usage: Help
# Arguments:
# none)
# Return: Help text
usage() { local RC=${1:-0}
echo "Usage: ${0##*/} [-opt] [command]
Options (fields in '[]' are optional, '<>' are required):
-h This help
-i \"<path>\" Import smbpassword
required arg: \"<path>\" - full file path in container
-n Start the 'nmbd' daemon to advertise the shares
-p Set ownership and permissions on the shares
-s \"<name;/path>[;browsable;readonly;guest;users]\" Configure a share
2015-01-02 07:03:52 +03:00
required arg: \"<name>;<comment>;</path>\"
<name> is how it's called for clients
<path> path to share
NOTE: for the default value, just leave blank
[browsable] default:'yes' or 'no'
2015-01-02 07:03:52 +03:00
[readonly] default:'yes' or 'no'
[guest] allowed default:'yes' or 'no'
[users] allowed default:'all' or list of allowed users
[admins] allowed default:'none' or list of admin users
2015-01-02 07:03:52 +03:00
-t \"\" Configure timezone
possible arg: \"[timezone]\" - zoneinfo timezone for container
-u \"<username;password>\" Add a user
required arg: \"<username>;<passwd>\"
<username> for user
<password> for user
-w \"<workgroup>\" Configure the workgroup (domain) samba should use
required arg: \"<workgroup>\"
<workgroup> for samba
2015-01-02 07:03:52 +03:00
The 'command' (if provided and valid) will be run instead of samba
" >&2
exit $RC
}
while getopts ":hi:nps:t:u:w:" opt; do
2015-01-02 07:03:52 +03:00
case "$opt" in
h) usage ;;
i) import "$OPTARG" ;;
n) NMBD="true" ;;
p) PERMISSIONS="true" ;;
2015-01-02 07:03:52 +03:00
s) eval share $(sed 's/^\|$/"/g; s/;/" "/g' <<< $OPTARG) ;;
t) timezone "$OPTARG" ;;
2016-05-15 14:39:51 +03:00
u) eval user $(sed 's|;| |g' <<< $OPTARG) ;;
w) workgroup "$OPTARG" ;;
2015-01-02 07:03:52 +03:00
"?") echo "Unknown option: -$OPTARG"; usage 1 ;;
":") echo "No argument value for option: -$OPTARG"; usage 2 ;;
esac
done
shift $(( OPTIND - 1 ))
[[ "${TZ:-""}" ]] && timezone "$TZ"
[[ "${WORKGROUP:-""}" ]] && workgroup "$WORKGROUP"
[[ "${PERMISSIONS:-""}" ]] && perms
2015-01-02 07:03:52 +03:00
if [[ $# -ge 1 && -x $(which $1 2>&-) ]]; then
2015-01-02 07:03:52 +03:00
exec "$@"
elif [[ $# -ge 1 ]]; then
echo "ERROR: command not found: $1"
exit 13
elif ps -ef | egrep -v grep | grep -q smbd; then
echo "Service already running, please restart container to apply changes"
2015-01-02 07:03:52 +03:00
else
2016-02-10 17:25:12 +03:00
[[ ${NMBD:-""} ]] && ionice -c 3 nmbd -D
exec ionice -c 3 smbd -FS </dev/null
2016-02-02 07:55:16 +03:00
fi