-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.sh
executable file
·78 lines (65 loc) · 1.94 KB
/
run.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
75
76
77
78
#!/usr/bin/env bash
prefix="BASTION_USER_"
# Check environment variables
_checkEnv() {
if ! (env | grep -q ${prefix}); then
echo "Error: not found environment variables \"${prefix}*\", exiting..."
exit 1
fi
}
# Preparing system parameters
_sysPrep() {
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
fi
if [ ! -f /etc/ssh/ssh_host_dsa_key ]; then
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
fi
if [ ! -f /etc/ssh/ssh_host_ecdsa_key ]; then
ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa
fi
if [ ! -f /etc/ssh/ssh_host_ed25519_key ]; then
ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -t ed25519
fi
}
# Create users and add 'authorized_keys' file
_createUsers() {
userVars=$(compgen -v ${prefix})
for userVar in ${userVars}; do
userName=$(echo ${userVar} | cut -d _ -f 3 | awk '{print tolower($0)}')
userKey=$(eval echo \$${userVar})
if ! (id -u ${userName} >/dev/null 2>&1); then
adduser -D ${userName} -s /bin/false
passwd -u ${userName}
mkdir -p /home/${userName}/.ssh
echo ${userKey} > /home/${userName}/.ssh/authorized_keys
chown -R ${userName}:${userName} /home/${userName}/.ssh
chmod 700 /home/${userName}/.ssh
chmod 600 /home/${userName}/.ssh/*
fi
done
}
# Starting sshd
_startSSHD() {
if (pgrep -fl sshd >/dev/null 2>&1); then
echo "Info: sshd process already running, killing..."
pkill -9 sshd
fi
/usr/sbin/sshd -D -p 22 -f /etc/sshd_config &
sleep 1
echo "Info: sshd process started!"
}
# Checking process is running
_healthCheck() {
while (pgrep -fl sshd >/dev/null 2>&1)
do
sleep 5
done
echo "Error: sshd is not running, exiting..."
exit 1
}
_checkEnv
_sysPrep
_createUsers
_startSSHD
_healthCheck