mirror of
https://github.com/atmoz/sftp.git
synced 2024-11-17 12:51:33 -05:00
73 lines
1.5 KiB
Bash
Executable file
73 lines
1.5 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
function printHelp() {
|
|
echo "Syntax: user:pass[:e][:[uid][:gid]]..."
|
|
echo "Use --readme for information and examples."
|
|
}
|
|
|
|
function printReadme() {
|
|
cat /README.md
|
|
}
|
|
|
|
function createUser() {
|
|
IFS=':' read -a param <<< $@
|
|
user="${param[0]}"
|
|
pass="${param[1]}"
|
|
|
|
if [ -z "$user" -o -z "$pass" ]; then
|
|
echo "You must at least provide a username and a password."
|
|
printHelp
|
|
exit 1
|
|
fi
|
|
|
|
if [ "${param[2]}" == "e" ]; then
|
|
chpasswdOptions="-e"
|
|
uid="${param[3]}"
|
|
gid="${param[4]}"
|
|
else
|
|
uid="${param[2]}"
|
|
gid="${param[3]}"
|
|
fi
|
|
|
|
useraddOptions="--create-home --no-user-group"
|
|
|
|
if [ -n "$uid" ]; then
|
|
useraddOptions="$useraddOptions --non-unique --uid $uid"
|
|
fi
|
|
|
|
if [ -n "$gid" ]; then
|
|
useraddOptions="$useraddOptions --gid $gid"
|
|
groupadd --gid $gid $gid
|
|
fi
|
|
|
|
useradd $useraddOptions $user
|
|
chown root:root /home/$user
|
|
chmod 755 /home/$user
|
|
|
|
if [ -z "$pass" ]; then
|
|
pass="$(echo `</dev/urandom tr -dc A-Za-z0-9 | head -c256`)"
|
|
chpasswdOptions=""
|
|
fi
|
|
|
|
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
|
|
}
|
|
|
|
if [[ -z $1 || $1 =~ ^--help$|^-h$ ]]; then
|
|
printHelp
|
|
exit 0
|
|
fi
|
|
|
|
if [ "$1" == "--readme" ]; then
|
|
printReadme
|
|
exit 0
|
|
fi
|
|
|
|
for user in "$@"; do
|
|
createUser $user
|
|
done
|
|
|
|
exec /usr/sbin/sshd -D
|