0
0
Fork 0
mirror of https://github.com/atmoz/sftp.git synced 2024-11-17 12:51:33 -05:00
atmoz-sftp/entrypoint

61 lines
1.4 KiB
Text
Raw Normal View History

2014-10-07 15:34:24 -04:00
#!/bin/bash
2015-07-25 07:55:33 -04:00
if [ $1 == "--readme" ]; then
cat /README.md
exit 0
fi
if [[ -z $1 || $1 =~ ^--help$|^-h$ ]]; then
echo "Syntax: user:pass[:e][:[uid][:gid]]..."
echo "Use --readme for information and examples."
exit 0
fi
2014-10-20 21:21:53 -04:00
for users in "$@"; do
IFS=':' read -a data <<< "$users"
2014-10-07 15:34:24 -04:00
user="${data[0]}"
pass="${data[1]}"
2015-07-25 07:55:33 -04:00
if [ -z "$user" -o -z "$pass" ]; then
echo "You must at least provide a username and a password."
exit 1
fi
if [ "${data[2]}" == "e" ]; then
2015-01-03 07:10:38 -05:00
chpasswdOptions="-e"
uid="${data[3]}"
gid="${data[4]}"
else
uid="${data[2]}"
gid="${data[3]}"
fi
2015-01-03 07:10:38 -05:00
useraddOptions="--create-home --no-user-group"
if [ -n "$uid" ]; then
2015-01-03 07:10:38 -05:00
useraddOptions="$useraddOptions --non-unique --uid $uid"
fi
if [ -n "$gid" ]; then
2015-01-03 07:10:38 -05:00
useraddOptions="$useraddOptions --gid $gid"
2014-12-31 10:42:09 -05:00
groupadd --gid $gid $gid
fi
2015-01-03 07:10:38 -05:00
useradd $useraddOptions $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
pass="$(echo `</dev/urandom tr -dc A-Za-z0-9 | 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
cat /home/$user/.ssh/keys/* >> /home/$user/.ssh/authorized_keys
chown $user /home/$user/.ssh/authorized_keys
chmod 600 /home/$user/.ssh/authorized_keys
2014-10-07 15:34:24 -04:00
done
2014-10-20 21:21:53 -04:00
exec /usr/sbin/sshd -D