2014-10-07 15:34:24 -04:00
|
|
|
#!/bin/bash
|
2018-11-07 03:49:47 -05:00
|
|
|
set -Eeo pipefail
|
|
|
|
|
|
|
|
# shellcheck disable=2154
|
|
|
|
trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
|
2014-10-07 15:34:24 -04:00
|
|
|
|
2018-11-15 18:57:33 -05:00
|
|
|
reArgsMaybe="^[^:[:space:]]+:.*$" # Smallest indication of attempt to use argument
|
|
|
|
reArgSkip='^([[:blank:]]*#.*|[[:blank:]]*)$' # comment or empty line
|
|
|
|
|
2017-10-08 12:36:51 -04:00
|
|
|
# Paths
|
2017-06-25 08:05:22 -04:00
|
|
|
userConfPath="/etc/sftp/users.conf"
|
|
|
|
userConfPathLegacy="/etc/sftp-users.conf"
|
|
|
|
userConfFinalPath="/var/run/sftp/users.conf"
|
2015-12-07 04:10:24 -05:00
|
|
|
|
2017-10-08 12:36:51 -04:00
|
|
|
function log() {
|
2018-11-15 18:57:33 -05:00
|
|
|
echo "[$0] $*" >&2
|
2015-07-25 08:00:24 -04:00
|
|
|
}
|
|
|
|
|
2017-10-08 12:36:51 -04:00
|
|
|
# Allow running other programs, e.g. bash
|
|
|
|
if [[ -z "$1" || "$1" =~ $reArgsMaybe ]]; then
|
|
|
|
startSshd=true
|
|
|
|
else
|
|
|
|
startSshd=false
|
2015-07-25 08:00:24 -04:00
|
|
|
fi
|
|
|
|
|
2017-06-25 08:05:22 -04:00
|
|
|
# Backward compatibility with legacy config path
|
2018-11-07 03:49:47 -05:00
|
|
|
if [ ! -f "$userConfPath" ] && [ -f "$userConfPathLegacy" ]; then
|
2017-06-25 08:05:22 -04:00
|
|
|
mkdir -p "$(dirname $userConfPath)"
|
|
|
|
ln -s "$userConfPathLegacy" "$userConfPath"
|
|
|
|
fi
|
|
|
|
|
2015-12-20 19:37:42 -05:00
|
|
|
# Create users only on first run
|
2015-12-16 13:41:49 -05:00
|
|
|
if [ ! -f "$userConfFinalPath" ]; then
|
2017-06-25 08:05:22 -04:00
|
|
|
mkdir -p "$(dirname $userConfFinalPath)"
|
2015-12-07 04:10:24 -05:00
|
|
|
|
2015-12-16 13:41:49 -05:00
|
|
|
if [ -f "$userConfPath" ]; then
|
2018-11-15 18:57:33 -05:00
|
|
|
# Append mounted config to final config
|
2018-11-07 03:49:47 -05:00
|
|
|
grep -v -E "$reArgSkip" < "$userConfPath" > "$userConfFinalPath"
|
2015-12-16 13:41:49 -05:00
|
|
|
fi
|
2015-12-07 04:10:24 -05:00
|
|
|
|
2018-01-04 13:32:05 -05:00
|
|
|
if $startSshd; then
|
|
|
|
# Append users from arguments to final config
|
|
|
|
for user in "$@"; do
|
2015-12-16 13:41:49 -05:00
|
|
|
echo "$user" >> "$userConfFinalPath"
|
|
|
|
done
|
|
|
|
fi
|
2015-12-07 04:10:24 -05:00
|
|
|
|
2018-01-04 13:32:05 -05:00
|
|
|
if [ -n "$SFTP_USERS" ]; then
|
|
|
|
# Append users from environment variable to final config
|
2018-11-07 03:49:47 -05:00
|
|
|
IFS=" " read -r -a usersFromEnv <<< "$SFTP_USERS"
|
2018-01-04 13:32:05 -05:00
|
|
|
for user in "${usersFromEnv[@]}"; do
|
2017-10-08 12:36:51 -04:00
|
|
|
echo "$user" >> "$userConfFinalPath"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2015-12-20 19:37:42 -05:00
|
|
|
# Check that we have users in config
|
2018-11-07 03:49:47 -05:00
|
|
|
if [ -f "$userConfFinalPath" ] && [ "$(wc -l < "$userConfFinalPath")" -gt 0 ]; then
|
2017-10-08 12:36:51 -04:00
|
|
|
# Import users from final conf file
|
|
|
|
while IFS= read -r user || [[ -n "$user" ]]; do
|
2018-11-15 18:57:33 -05:00
|
|
|
create-sftp-user "$user"
|
2017-10-08 12:36:51 -04:00
|
|
|
done < "$userConfFinalPath"
|
|
|
|
elif $startSshd; then
|
|
|
|
log "FATAL: No users provided!"
|
2015-12-20 19:37:42 -05:00
|
|
|
exit 3
|
2015-12-16 13:41:49 -05:00
|
|
|
fi
|
|
|
|
|
2016-06-02 08:56:30 -04:00
|
|
|
# Generate unique ssh keys for this container, if needed
|
|
|
|
if [ ! -f /etc/ssh/ssh_host_ed25519_key ]; then
|
2016-11-15 09:32:03 -05:00
|
|
|
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
|
2019-10-03 06:14:12 -04:00
|
|
|
else
|
|
|
|
chmod 700 /etc/ssh/ssh_host_ed25519_key
|
2016-06-02 08:56:30 -04:00
|
|
|
fi
|
|
|
|
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
|
2016-11-15 09:32:03 -05:00
|
|
|
ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ''
|
2019-10-03 06:14:12 -04:00
|
|
|
else
|
|
|
|
chmod 700 /etc/ssh/ssh_host_rsa_key
|
2016-06-02 08:56:30 -04:00
|
|
|
fi
|
2016-03-23 17:32:26 -04:00
|
|
|
fi
|
2016-03-28 12:06:53 -04:00
|
|
|
|
2016-03-23 17:32:26 -04:00
|
|
|
# Source custom scripts, if any
|
|
|
|
if [ -d /etc/sftp.d ]; then
|
|
|
|
for f in /etc/sftp.d/*; do
|
2016-08-12 10:21:07 -04:00
|
|
|
if [ -x "$f" ]; then
|
2017-10-08 12:36:51 -04:00
|
|
|
log "Running $f ..."
|
2016-08-12 10:21:07 -04:00
|
|
|
$f
|
2018-01-05 07:14:29 -05:00
|
|
|
else
|
|
|
|
log "Could not run $f, because it's missing execute permission (+x)."
|
2016-08-12 10:21:07 -04:00
|
|
|
fi
|
2016-03-23 17:32:26 -04:00
|
|
|
done
|
|
|
|
unset f
|
2015-12-16 13:41:49 -05:00
|
|
|
fi
|
2016-03-28 12:06:53 -04:00
|
|
|
|
2017-10-08 12:36:51 -04:00
|
|
|
if $startSshd; then
|
|
|
|
log "Executing sshd"
|
|
|
|
exec /usr/sbin/sshd -D -e
|
|
|
|
else
|
2018-11-07 03:49:47 -05:00
|
|
|
log "Executing $*"
|
2017-10-08 12:36:51 -04:00
|
|
|
exec "$@"
|
|
|
|
fi
|