2014-10-07 15:34:24 -04:00
|
|
|
#!/bin/bash
|
2015-12-16 05:54:57 -05:00
|
|
|
set -e
|
2014-10-07 15:34:24 -04:00
|
|
|
|
2015-12-07 04:10:24 -05:00
|
|
|
userConfPath="/etc/sftp-users.conf"
|
|
|
|
userConfFinalPath="/var/run/sftp-users.conf"
|
|
|
|
|
2015-07-25 08:00:24 -04:00
|
|
|
function printHelp() {
|
2015-12-07 04:10:24 -05:00
|
|
|
echo "Add users as command arguments, STDIN or mounted in $userConfPath"
|
|
|
|
echo "Syntax: user:pass[:e][:uid[:gid]]..."
|
|
|
|
echo "Use --readme for more information and examples."
|
2015-07-25 08:00:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function printReadme() {
|
|
|
|
cat /README.md
|
2015-12-07 04:10:24 -05:00
|
|
|
echo "TIP: Read this in HTML format here: https://github.com/atmoz/sftp"
|
2015-07-25 08:00:24 -04:00
|
|
|
}
|
2015-07-25 07:55:33 -04:00
|
|
|
|
2015-07-25 08:00:24 -04:00
|
|
|
function createUser() {
|
|
|
|
IFS=':' read -a param <<< $@
|
|
|
|
user="${param[0]}"
|
|
|
|
pass="${param[1]}"
|
2014-10-07 15:34:24 -04:00
|
|
|
|
2015-07-25 08:00:24 -04:00
|
|
|
if [ "${param[2]}" == "e" ]; then
|
2015-01-03 07:10:38 -05:00
|
|
|
chpasswdOptions="-e"
|
2015-07-25 08:00:24 -04:00
|
|
|
uid="${param[3]}"
|
|
|
|
gid="${param[4]}"
|
2014-10-20 13:43:07 -04:00
|
|
|
else
|
2015-07-25 08:00:24 -04:00
|
|
|
uid="${param[2]}"
|
|
|
|
gid="${param[3]}"
|
2014-10-20 13:43:07 -04:00
|
|
|
fi
|
|
|
|
|
2015-12-20 19:37:42 -05:00
|
|
|
if [ -z "$user" ]; then
|
|
|
|
echo "FATAL: You must at least provide a username."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if $(cat /etc/passwd | cut -d: -f1 | grep -q "$user"); then
|
|
|
|
echo "FATAL: User \"$user\" already exists."
|
|
|
|
exit 2
|
2015-12-16 13:41:49 -05:00
|
|
|
fi
|
|
|
|
|
2015-12-07 04:10:24 -05:00
|
|
|
useraddOptions="--no-user-group"
|
2014-10-20 13:43:07 -04:00
|
|
|
|
|
|
|
if [ -n "$uid" ]; then
|
2015-01-03 07:10:38 -05:00
|
|
|
useraddOptions="$useraddOptions --non-unique --uid $uid"
|
2014-10-20 13:43:07 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$gid" ]; then
|
2015-12-16 09:33:12 -05:00
|
|
|
if ! $(cat /etc/group | cut -d: -f3 | grep -q "$gid"); then
|
2015-12-07 04:10:24 -05:00
|
|
|
groupadd --gid $gid $gid
|
|
|
|
fi
|
2015-12-20 19:37:42 -05:00
|
|
|
|
|
|
|
useraddOptions="$useraddOptions --gid $gid"
|
2014-10-20 13:43:07 -04:00
|
|
|
fi
|
|
|
|
|
2015-01-03 07:10:38 -05:00
|
|
|
useradd $useraddOptions $user
|
2015-12-20 18:55:32 -05:00
|
|
|
mkdir -p /home/$user
|
2014-10-07 15:34:24 -04:00
|
|
|
chown root:root /home/$user
|
|
|
|
chmod 755 /home/$user
|
2014-10-20 21:21:53 -04:00
|
|
|
|
|
|
|
if [ -z "$pass" ]; then
|
2016-03-17 16:07:49 -04:00
|
|
|
pass="$(tr -dc A-Za-z0-9 </dev/urandom | head -c256)"
|
2015-01-03 07:10:38 -05:00
|
|
|
chpasswdOptions=""
|
2014-10-20 21:21:53 -04:00
|
|
|
fi
|
|
|
|
|
2015-01-03 07:10:38 -05:00
|
|
|
echo "$user:$pass" | chpasswd $chpasswdOptions
|
2015-06-03 06:03:55 -04:00
|
|
|
|
2015-12-20 18:55:32 -05:00
|
|
|
# Add SSH keys to authorized_keys with valid permissions
|
|
|
|
if [ -d /home/$user/.ssh/keys ]; then
|
|
|
|
cat /home/$user/.ssh/keys/* >> /home/$user/.ssh/authorized_keys
|
|
|
|
chown $user /home/$user/.ssh/authorized_keys
|
|
|
|
chmod 600 /home/$user/.ssh/authorized_keys
|
|
|
|
fi
|
2015-07-25 08:00:24 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 04:10:24 -05:00
|
|
|
if [[ $1 =~ ^--help$|^-h$ ]]; then
|
2015-07-25 08:00:24 -04:00
|
|
|
printHelp
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$1" == "--readme" ]; then
|
|
|
|
printReadme
|
|
|
|
exit 0
|
|
|
|
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
|
2015-12-07 04:10:24 -05:00
|
|
|
|
2015-12-16 13:41:49 -05:00
|
|
|
# Append mounted config to final config
|
|
|
|
if [ -f "$userConfPath" ]; then
|
2015-12-20 19:37:42 -05:00
|
|
|
cat "$userConfPath" > "$userConfFinalPath"
|
2015-12-16 13:41:49 -05:00
|
|
|
fi
|
2015-12-07 04:10:24 -05:00
|
|
|
|
2015-12-16 13:41:49 -05:00
|
|
|
# Append users from arguments to final config
|
|
|
|
for user in "$@"; do
|
2015-12-16 05:54:57 -05:00
|
|
|
echo "$user" >> "$userConfFinalPath"
|
|
|
|
done
|
2014-10-07 15:34:24 -04:00
|
|
|
|
2015-12-16 13:41:49 -05:00
|
|
|
# Append users from STDIN to final config
|
|
|
|
if [ ! -t 0 ]; then
|
|
|
|
while IFS= read -r user || [[ -n "$user" ]]; do
|
|
|
|
echo "$user" >> "$userConfFinalPath"
|
|
|
|
done
|
|
|
|
fi
|
2015-12-07 04:10:24 -05:00
|
|
|
|
2015-12-20 19:37:42 -05:00
|
|
|
# Check that we have users in config
|
|
|
|
if [ "$(cat "$userConfFinalPath" | wc -l)" == 0 ]; then
|
|
|
|
echo "FATAL: No users provided!"
|
2015-12-16 13:41:49 -05:00
|
|
|
printHelp
|
2015-12-20 19:37:42 -05:00
|
|
|
exit 3
|
2015-12-16 13:41:49 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Import users from final conf file
|
|
|
|
while IFS= read -r user || [[ -n "$user" ]]; do
|
|
|
|
createUser "$user"
|
|
|
|
done < "$userConfFinalPath"
|
|
|
|
|
2016-03-23 17:32:26 -04:00
|
|
|
|
|
|
|
fi
|
|
|
|
# Source custom scripts, if any
|
|
|
|
if [ -d /etc/sftp.d ]; then
|
|
|
|
for f in /etc/sftp.d/*; do
|
|
|
|
[ -x "$f" ] && . "$f"
|
|
|
|
done
|
|
|
|
unset f
|
2015-12-16 13:41:49 -05:00
|
|
|
fi
|
2014-10-20 21:21:53 -04:00
|
|
|
exec /usr/sbin/sshd -D
|