0
0
Fork 0
mirror of https://github.com/atmoz/sftp.git synced 2024-11-24 12:59:24 -05:00
atmoz-sftp/files/entrypoint

96 lines
2.6 KiB
Text
Raw Normal View History

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
reArgsMaybe="^[^:[:space:]]+:.*$" # Smallest indication of attempt to use argument
reArgSkip='^([[:blank:]]*#.*|[[:blank:]]*)$' # comment or empty line
# Paths
userConfPath="/etc/sftp/users.conf"
userConfPathLegacy="/etc/sftp-users.conf"
userConfFinalPath="/var/run/sftp/users.conf"
2015-12-07 04:10:24 -05:00
function log() {
echo "[$0] $*" >&2
2015-07-25 08:00:24 -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
# Backward compatibility with legacy config path
2018-11-07 03:49:47 -05:00
if [ ! -f "$userConfPath" ] && [ -f "$userConfPathLegacy" ]; then
mkdir -p "$(dirname $userConfPath)"
ln -s "$userConfPathLegacy" "$userConfPath"
fi
2015-12-20 19:37:42 -05:00
# Create users only on first run
if [ ! -f "$userConfFinalPath" ]; then
mkdir -p "$(dirname $userConfFinalPath)"
2015-12-07 04:10:24 -05:00
if [ -f "$userConfPath" ]; then
# Append mounted config to final config
2018-11-07 03:49:47 -05:00
grep -v -E "$reArgSkip" < "$userConfPath" > "$userConfFinalPath"
fi
2015-12-07 04:10:24 -05:00
if $startSshd; then
# Append users from arguments to final config
for user in "$@"; do
echo "$user" >> "$userConfFinalPath"
done
fi
2015-12-07 04:10:24 -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"
for user in "${usersFromEnv[@]}"; do
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
# Import users from final conf file
while IFS= read -r user || [[ -n "$user" ]]; do
create-sftp-user "$user"
done < "$userConfFinalPath"
elif $startSshd; then
log "FATAL: No users provided!"
2015-12-20 19:37:42 -05:00
exit 3
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 ''
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 ''
2016-06-02 08:56:30 -04:00
fi
fi
# Source custom scripts, if any
if [ -d /etc/sftp.d ]; then
for f in /etc/sftp.d/*; do
if [ -x "$f" ]; then
log "Running $f ..."
$f
else
log "Could not run $f, because it's missing execute permission (+x)."
fi
done
unset f
fi
if $startSshd; then
log "Executing sshd"
exec /usr/sbin/sshd -D -e
else
2018-11-07 03:49:47 -05:00
log "Executing $*"
exec "$@"
fi