-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-user.sh
74 lines (61 loc) · 1.51 KB
/
add-user.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# Get the main directory of the script.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. "$DIR/config.sh"
showHelp() {
cat << EOF
Usage: ${0##*/} -u USERNAME
Create a website and resource pool for DOMAIN for USERNAME
-u USERNAME Usernames are limited to 0-9A-Za-z_-
EOF
}
# Require options to be setup.
if [[ ! $@ =~ ^\-.+ ]]; then
error "You must specify the -u parameter."
showHelp
exit
fi
while [[ $# -gt 1 ]]; do
key="$1"
case $key in
-u|--username)
username="$2"
shift # past argument
;;
*)
# Skip this option, nothing special
;;
esac
shift # past argument or value
done
# Creates all user directories.
setupUser() {
success "Creating directories for ${username}"
useradd -m "${username}"
base="/home/${username}"
sshPass=$(randomPW)
# Set the users password.
echo -e "${sshPass}\n${sshPass}" | passwd "${username}"
# The main dirs
mkdir "${base}/log"
mkdir "${base}/html"
mkdir "${base}/tmp"
mkdir "${base}/run"
chown -fR "$username:$username" "$base"
success "==========================================================="
success "Make sure you save the username/password combo below."
success "==========================================================="
success "Login: $username"
success "Password: ${sshPass}"
}
if isValidUsername "$username"; then
if [ -d "/home/$username" ]; then
error "The user $username already exists, please choose another."
getUser
else
setupUser
fi
else
error "$username is not a valid username"
getUser
fi