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
|
|
|
|
|
2016-11-29 21:22:04 +03:00
|
|
|
### charmap: setup character mapping for file/directory names
|
|
|
|
# Arguments:
|
|
|
|
# chars) from:to character mappings separated by ','
|
|
|
|
# Return: configured character mapings
|
|
|
|
charmap() { local chars="$1" file=/etc/samba/smb.conf
|
|
|
|
grep -q catia $file || sed -i '/TCP_NODELAY/a \
|
|
|
|
\
|
|
|
|
vfs objects = catia\
|
|
|
|
catia:mappings =\
|
|
|
|
|
|
|
|
' $file
|
|
|
|
|
2019-09-18 13:51:09 +03:00
|
|
|
sed -i '/catia:mappings/s| =.*| = '"$chars"'|' $file
|
2016-11-29 21:22:04 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 07:03:18 +03:00
|
|
|
### generic: set a generic config option in a section
|
|
|
|
# Arguments:
|
|
|
|
# section) section of config file
|
|
|
|
# option) raw option
|
|
|
|
# Return: line added to smb.conf (replaces existing line with same key)
|
|
|
|
generic() { local section="$1" key="$(sed 's| *=.*||' <<< $2)" \
|
2020-07-03 15:55:38 +03:00
|
|
|
value="$(sed 's|[^=]*= *||' <<< $2)" file=/etc/samba/smb.conf
|
2020-05-26 07:03:18 +03:00
|
|
|
if sed -n '/^\['"$section"'\]/,/^\[/p' $file | grep -qE '^;*\s*'"$key"; then
|
|
|
|
sed -i '/^\['"$1"'\]/,/^\[/s|^;*\s*\('"$key"' = \).*| \1'"$value"'|' \
|
|
|
|
"$file"
|
|
|
|
else
|
|
|
|
sed -i '/\['"$section"'\]/a \ '"$key = $value" "$file"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:12:08 +03:00
|
|
|
### global: set a global config option
|
|
|
|
# Arguments:
|
|
|
|
# option) raw option
|
|
|
|
# Return: line added to smb.conf (replaces existing line with same key)
|
2020-07-03 15:55:38 +03:00
|
|
|
global() { local key="$(sed 's| *=.*||' <<< $1)" \
|
|
|
|
value="$(sed 's|[^=]*= *||' <<< $1)" file=/etc/samba/smb.conf
|
2020-05-26 07:03:18 +03:00
|
|
|
if sed -n '/^\[global\]/,/^\[/p' $file | grep -qE '^;*\s*'"$key"; then
|
|
|
|
sed -i '/^\[global\]/,/^\[/s|^;*\s*\('"$key"' = \).*| \1'"$value"'|' \
|
|
|
|
"$file"
|
2017-10-08 22:12:08 +03:00
|
|
|
else
|
2020-05-26 07:03:18 +03:00
|
|
|
sed -i '/\[global\]/a \ '"$key = $value" "$file"
|
2017-10-08 22:12:08 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-09-23 19:57:54 +03:00
|
|
|
### include: add a samba config file include
|
|
|
|
# Arguments:
|
|
|
|
# file) file to import
|
|
|
|
include() { local includefile="$1" file=/etc/samba/smb.conf
|
|
|
|
sed -i "\\|include = $includefile|d" "$file"
|
|
|
|
echo "include = $includefile" >> "$file"
|
|
|
|
}
|
|
|
|
|
2015-06-17 20:37:00 +03:00
|
|
|
### import: import a smbpasswd file
|
|
|
|
# Arguments:
|
|
|
|
# file) file to import
|
|
|
|
# Return: user(s) added to container
|
2018-02-10 01:20:58 +03:00
|
|
|
import() { local file="$1" name id
|
2015-06-18 15:41:45 +03:00
|
|
|
while read name id; do
|
2017-12-21 18:32:29 +03:00
|
|
|
grep -q "^$name:" /etc/passwd || adduser -D -H -u "$id" "$name"
|
2017-10-24 18:28:26 +03:00
|
|
|
done < <(cut -d: -f1,2 $file | sed 's/:/ /')
|
2015-06-17 20:37:00 +03:00
|
|
|
pdbedit -i smbpasswd:$file
|
|
|
|
}
|
|
|
|
|
2016-06-08 14:36:20 +03:00
|
|
|
### 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
|
2016-06-08 14:36:20 +03:00
|
|
|
chown -Rh smbuser. $i
|
2018-01-16 04:25:12 +03:00
|
|
|
find $i -type d ! -perm 775 -exec chmod 775 {} \;
|
|
|
|
find $i -type f ! -perm 0664 -exec chmod 0664 {} \;
|
2016-06-08 14:36:20 +03:00
|
|
|
done
|
|
|
|
}
|
2020-03-25 16:06:26 +03:00
|
|
|
export -f perms
|
2016-06-08 14:36:20 +03:00
|
|
|
|
2017-05-25 17:45:14 +03:00
|
|
|
### recycle: disable recycle bin
|
|
|
|
# Arguments:
|
|
|
|
# none)
|
|
|
|
# Return: result
|
|
|
|
recycle() { local file=/etc/samba/smb.conf
|
2019-07-07 07:33:26 +03:00
|
|
|
sed -i '/recycle:/d; /vfs objects/s/ recycle / /' $file
|
2017-05-25 17:45:14 +03:00
|
|
|
}
|
|
|
|
|
2015-01-02 07:03:52 +03:00
|
|
|
### share: Add share
|
|
|
|
# Arguments:
|
|
|
|
# share) share name
|
|
|
|
# path) path to share
|
2016-01-12 13:15:47 +03:00
|
|
|
# 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
|
2016-12-12 01:12:11 +03:00
|
|
|
# writelist) list of users that can write to a RO share
|
2017-12-25 16:02:26 +03:00
|
|
|
# comment) description of share
|
2015-01-02 07:03:52 +03:00
|
|
|
# Return: result
|
2018-02-09 23:54:28 +03:00
|
|
|
share() { local share="$1" path="$2" browsable="${3:-yes}" ro="${4:-yes}" \
|
|
|
|
guest="${5:-yes}" users="${6:-""}" admins="${7:-""}" \
|
|
|
|
writelist="${8:-""}" comment="${9:-""}" 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
|
2016-01-12 13:15:47 +03:00
|
|
|
echo " browsable = $browsable" >>$file
|
2015-10-24 14:57:47 +03:00
|
|
|
echo " read only = $ro" >>$file
|
|
|
|
echo " guest ok = $guest" >>$file
|
2020-03-20 22:20:29 +03:00
|
|
|
[[ ${VETO:-yes} == no ]] || {
|
2020-04-12 06:19:10 +03:00
|
|
|
echo -n " veto files = /.apdisk/.DS_Store/.TemporaryItems/" >>$file
|
|
|
|
echo -n ".Trashes/desktop.ini/ehthumbs.db/Network Trash Folder/" >>$file
|
|
|
|
echo "Temporary Items/Thumbs.db/" >>$file
|
2020-03-20 18:30:02 +03:00
|
|
|
echo " delete veto files = yes" >>$file
|
|
|
|
}
|
2020-03-20 17:49:47 +03:00
|
|
|
[[ ${users:-""} && ! ${users:-""} == all ]] &&
|
2015-10-24 14:57:47 +03:00
|
|
|
echo " valid users = $(tr ',' ' ' <<< $users)" >>$file
|
2015-12-17 14:14:38 +03:00
|
|
|
[[ ${admins:-""} && ! ${admins:-""} =~ none ]] &&
|
|
|
|
echo " admin users = $(tr ',' ' ' <<< $admins)" >>$file
|
2016-12-12 01:12:11 +03:00
|
|
|
[[ ${writelist:-""} && ! ${writelist:-""} =~ none ]] &&
|
|
|
|
echo " write list = $(tr ',' ' ' <<< $writelist)" >>$file
|
2017-12-25 16:02:26 +03:00
|
|
|
[[ ${comment:-""} && ! ${comment:-""} =~ none ]] &&
|
|
|
|
echo " comment = $(tr ',' ' ' <<< $comment)" >>$file
|
2016-11-29 21:22:04 +03:00
|
|
|
echo "" >>$file
|
2018-01-26 14:52:44 +03:00
|
|
|
[[ -d $path ]] || mkdir -p $path
|
2015-01-02 07:03:52 +03:00
|
|
|
}
|
|
|
|
|
2017-10-08 21:47:27 +03:00
|
|
|
### smb: disable SMB2 minimum
|
2017-07-14 00:51:59 +03:00
|
|
|
# Arguments:
|
|
|
|
# none)
|
|
|
|
# Return: result
|
|
|
|
smb() { local file=/etc/samba/smb.conf
|
2020-03-23 19:42:09 +03:00
|
|
|
sed -i 's/\([^#]*min protocol *=\).*/\1 LANMAN1/' $file
|
2017-07-14 00:51:59 +03:00
|
|
|
}
|
|
|
|
|
2015-01-02 07:03:52 +03:00
|
|
|
### user: add a user
|
|
|
|
# Arguments:
|
|
|
|
# name) for user
|
|
|
|
# password) for user
|
2016-08-30 13:01:21 +03:00
|
|
|
# id) for user
|
2017-03-21 23:08:31 +03:00
|
|
|
# group) for user
|
2019-07-07 01:34:52 +03:00
|
|
|
# gid) for group
|
2015-01-02 07:03:52 +03:00
|
|
|
# Return: user added to container
|
2019-07-07 01:34:52 +03:00
|
|
|
user() { local name="$1" passwd="$2" id="${3:-""}" group="${4:-""}" \
|
|
|
|
gid="${5:-""}"
|
|
|
|
[[ "$group" ]] && { grep -q "^$group:" /etc/group ||
|
|
|
|
addgroup ${gid:+--gid $gid }"$group"; }
|
2017-12-20 14:17:44 +03:00
|
|
|
grep -q "^$name:" /etc/passwd ||
|
2017-12-19 18:30:02 +03:00
|
|
|
adduser -D -H ${group:+-G $group} ${id:+-u $id} "$name"
|
2016-10-19 22:33:00 +03:00
|
|
|
echo -e "$passwd\n$passwd" | smbpasswd -s -a "$name"
|
2015-01-02 07:03:52 +03:00
|
|
|
}
|
2015-06-17 20:37:00 +03:00
|
|
|
|
2016-02-13 02:13:20 +03:00
|
|
|
### workgroup: set the workgroup
|
|
|
|
# Arguments:
|
|
|
|
# workgroup) the name to set
|
|
|
|
# Return: configure the correct workgroup
|
2018-02-10 01:20:58 +03:00
|
|
|
workgroup() { local workgroup="$1" file=/etc/samba/smb.conf
|
2016-05-15 14:39:51 +03:00
|
|
|
sed -i 's|^\( *workgroup = \).*|\1'"$workgroup"'|' $file
|
2016-02-13 02:13:20 +03:00
|
|
|
}
|
|
|
|
|
2017-08-17 23:18:01 +03:00
|
|
|
### widelinks: allow access wide symbolic links
|
|
|
|
# Arguments:
|
|
|
|
# none)
|
|
|
|
# Return: result
|
|
|
|
widelinks() { local file=/etc/samba/smb.conf \
|
|
|
|
replace='\1\n wide links = yes\n unix extensions = no'
|
|
|
|
sed -i 's/\(follow symlinks = yes\)/'"$replace"'/' $file
|
|
|
|
}
|
|
|
|
|
2015-01-02 07:03:52 +03:00
|
|
|
### usage: Help
|
|
|
|
# Arguments:
|
|
|
|
# none)
|
|
|
|
# Return: Help text
|
2018-02-09 23:54:28 +03:00
|
|
|
usage() { local RC="${1:-0}"
|
2015-01-02 07:03:52 +03:00
|
|
|
echo "Usage: ${0##*/} [-opt] [command]
|
|
|
|
Options (fields in '[]' are optional, '<>' are required):
|
|
|
|
-h This help
|
2016-11-29 21:22:04 +03:00
|
|
|
-c \"<from:to>\" setup character mapping for file/directory names
|
|
|
|
required arg: \"<from:to>\" character mappings separated by ','
|
2020-05-26 07:03:18 +03:00
|
|
|
-G \"<section;parameter>\" Provide generic section option for smb.conf
|
|
|
|
required arg: \"<section>\" - IE: \"share\"
|
|
|
|
required arg: \"<parameter>\" - IE: \"log level = 2\"
|
2017-10-08 22:12:08 +03:00
|
|
|
-g \"<parameter>\" Provide global option for smb.conf
|
2020-05-26 07:03:18 +03:00
|
|
|
required arg: \"<parameter>\" - IE: \"log level = 2\"
|
2015-06-17 20:37:00 +03:00
|
|
|
-i \"<path>\" Import smbpassword
|
2016-01-29 03:05:20 +03:00
|
|
|
required arg: \"<path>\" - full file path in container
|
2016-02-07 02:37:31 +03:00
|
|
|
-n Start the 'nmbd' daemon to advertise the shares
|
2016-06-08 14:36:20 +03:00
|
|
|
-p Set ownership and permissions on the shares
|
2017-05-25 17:45:14 +03:00
|
|
|
-r Disable recycle bin for shares
|
2017-10-08 21:47:27 +03:00
|
|
|
-S Disable SMB2 minimum version
|
2017-12-25 16:02:26 +03:00
|
|
|
-s \"<name;/path>[;browse;readonly;guest;users;admins;writelist;comment]\"
|
|
|
|
Configure a share
|
2017-06-05 21:38:18 +03:00
|
|
|
required arg: \"<name>;</path>\"
|
2015-01-02 07:03:52 +03:00
|
|
|
<name> is how it's called for clients
|
|
|
|
<path> path to share
|
2015-08-02 15:23:04 +03:00
|
|
|
NOTE: for the default value, just leave blank
|
2016-01-12 13:15:47 +03:00
|
|
|
[browsable] default:'yes' or 'no'
|
2015-01-02 07:03:52 +03:00
|
|
|
[readonly] default:'yes' or 'no'
|
|
|
|
[guest] allowed default:'yes' or 'no'
|
2019-07-07 09:54:17 +03:00
|
|
|
NOTE: for user lists below, usernames are separated by ','
|
2015-01-02 07:03:52 +03:00
|
|
|
[users] allowed default:'all' or list of allowed users
|
2015-12-17 14:14:38 +03:00
|
|
|
[admins] allowed default:'none' or list of admin users
|
2016-12-12 01:12:11 +03:00
|
|
|
[writelist] list of users that can write to a RO share
|
2017-12-25 16:02:26 +03:00
|
|
|
[comment] description of share
|
2019-07-07 01:34:52 +03:00
|
|
|
-u \"<username;password>[;ID;group;GID]\" Add a user
|
2015-01-02 07:03:52 +03:00
|
|
|
required arg: \"<username>;<passwd>\"
|
|
|
|
<username> for user
|
|
|
|
<password> for user
|
2016-08-30 13:01:21 +03:00
|
|
|
[ID] for user
|
2016-12-12 01:12:11 +03:00
|
|
|
[group] for user
|
2019-07-07 01:34:52 +03:00
|
|
|
[GID] for group
|
2016-02-13 02:13:20 +03:00
|
|
|
-w \"<workgroup>\" Configure the workgroup (domain) samba should use
|
|
|
|
required arg: \"<workgroup>\"
|
|
|
|
<workgroup> for samba
|
2017-08-17 23:18:01 +03:00
|
|
|
-W Allow access wide symbolic links
|
2018-09-23 19:57:54 +03:00
|
|
|
-I Add an include option at the end of the smb.conf
|
|
|
|
required arg: \"<include file path>\"
|
|
|
|
<include file path> in the container, e.g. a bind mount
|
2015-01-02 07:03:52 +03:00
|
|
|
|
|
|
|
The 'command' (if provided and valid) will be run instead of samba
|
|
|
|
" >&2
|
|
|
|
exit $RC
|
|
|
|
}
|
|
|
|
|
2019-07-16 00:59:57 +03:00
|
|
|
[[ "${USERID:-""}" =~ ^[0-9]+$ ]] && usermod -u $USERID -o smbuser
|
2019-11-06 22:10:02 +03:00
|
|
|
[[ "${GROUPID:-""}" =~ ^[0-9]+$ ]] && groupmod -g $GROUPID -o smb
|
2017-01-04 23:54:52 +03:00
|
|
|
|
2020-05-26 07:03:18 +03:00
|
|
|
while getopts ":hc:G:g:i:nprs:Su:Ww:I:" opt; do
|
2015-01-02 07:03:52 +03:00
|
|
|
case "$opt" in
|
|
|
|
h) usage ;;
|
2016-11-29 21:22:04 +03:00
|
|
|
c) charmap "$OPTARG" ;;
|
2020-05-26 07:03:18 +03:00
|
|
|
G) eval generic $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $OPTARG) ;;
|
2017-10-08 22:12:08 +03:00
|
|
|
g) global "$OPTARG" ;;
|
2015-06-17 20:37:00 +03:00
|
|
|
i) import "$OPTARG" ;;
|
2016-02-07 02:37:31 +03:00
|
|
|
n) NMBD="true" ;;
|
2016-06-08 14:36:20 +03:00
|
|
|
p) PERMISSIONS="true" ;;
|
2017-05-25 17:45:14 +03:00
|
|
|
r) recycle ;;
|
2017-10-20 20:43:12 +03:00
|
|
|
s) eval share $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $OPTARG) ;;
|
2017-07-14 00:51:59 +03:00
|
|
|
S) smb ;;
|
2018-02-10 01:20:58 +03:00
|
|
|
u) eval user $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $OPTARG) ;;
|
2016-02-13 02:13:20 +03:00
|
|
|
w) workgroup "$OPTARG" ;;
|
2017-08-17 23:18:01 +03:00
|
|
|
W) widelinks ;;
|
2018-09-23 19:57:54 +03:00
|
|
|
I) include "$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 ))
|
|
|
|
|
2016-11-29 21:22:04 +03:00
|
|
|
[[ "${CHARMAP:-""}" ]] && charmap "$CHARMAP"
|
2020-05-26 07:03:18 +03:00
|
|
|
while read i; do
|
|
|
|
eval generic $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $i)
|
|
|
|
done < <(env | awk '/^GENERIC[0-9=_]/ {sub (/^[^=]*=/, "", $0); print}')
|
2019-07-06 23:27:43 +03:00
|
|
|
while read i; do
|
|
|
|
global "$i"
|
2019-07-15 00:21:12 +03:00
|
|
|
done < <(env | awk '/^GLOBAL[0-9=_]/ {sub (/^[^=]*=/, "", $0); print}')
|
2017-12-25 16:02:26 +03:00
|
|
|
[[ "${IMPORT:-""}" ]] && import "$IMPORT"
|
2017-05-25 17:45:14 +03:00
|
|
|
[[ "${RECYCLE:-""}" ]] && recycle
|
2019-07-06 23:27:43 +03:00
|
|
|
while read i; do
|
|
|
|
eval share $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $i)
|
2019-07-15 00:21:12 +03:00
|
|
|
done < <(env | awk '/^SHARE[0-9=_]/ {sub (/^[^=]*=/, "", $0); print}')
|
2017-07-14 00:51:59 +03:00
|
|
|
[[ "${SMB:-""}" ]] && smb
|
2019-07-06 23:27:43 +03:00
|
|
|
while read i; do
|
|
|
|
eval user $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $i)
|
2019-07-15 00:21:12 +03:00
|
|
|
done < <(env | awk '/^USER[0-9=_]/ {sub (/^[^=]*=/, "", $0); print}')
|
2016-02-13 02:13:20 +03:00
|
|
|
[[ "${WORKGROUP:-""}" ]] && workgroup "$WORKGROUP"
|
2017-08-17 23:18:01 +03:00
|
|
|
[[ "${WIDELINKS:-""}" ]] && widelinks
|
2018-09-23 19:57:54 +03:00
|
|
|
[[ "${INCLUDE:-""}" ]] && include "$INCLUDE"
|
2020-03-25 16:06:26 +03:00
|
|
|
[[ "${PERMISSIONS:-""}" ]] && perms &
|
2015-01-02 07:03:52 +03:00
|
|
|
|
2015-05-21 14:11:34 +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
|
2015-05-21 14:11:34 +03:00
|
|
|
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
|
2022-11-04 05:52:19 +03:00
|
|
|
exec ionice -c 3 smbd -F --debug-stdout --no-process-group </dev/null
|
2022-11-04 05:43:35 +03:00
|
|
|
fi
|