-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcreate_user.sh
230 lines (194 loc) · 6.99 KB
/
create_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env bash
################################################################################
# Source https://mailinabox.email/ https://github.com/mail-in-a-box/mailinabox #
# Updated by Dirty Harry for YiiMP use... #
# This script is intended to be ran from the Yiimp Server Installer #
################################################################################
source /etc/functions.sh
cd ~/yiimpserver/install
clear
# Welcome
message_box "Dirty Harry Yiimp Server Installer v1.0" \
"Hello and thanks for using the Dirty Harry Yiimp Server Installer v1.0!
\n\nInstallation for the most part is fully automated. In most cases any user responses that are needed are asked prior to the installation.
\n\nNOTE: You should only install this on a brand new Ubuntu 16.04 or Ubuntu 18.04 installation."
# Root warning message box
message_box "Dirty Harry Yiimp Server Installer v1.0" \
"Naughty, Naughty! You are trying to install this as the root user!
\n\nRunning any application as root is a serious security risk.
\n\nTherefore we make you create a user account :)"
# Ask if SSH key or password user
dialog --title "Create New User With SSH Key" \
--yesno "Do you want to create your new user with SSH key login?
Selecting no will create user with password login only." 7 60
response=$?
case $response in
0) UsingSSH=yes;;
1) UsingSSH=no;;
255) echo "[ESC] key pressed.";;
esac
# If Using SSH Key Login
if [[ ("$UsingSSH" == "yes") ]]; then
clear
if [ -z "${yiimpadmin:-}" ]; then
DEFAULT_yiimpadmin=yiimpadmin
input_box "New Account Name" \
"Please enter your desired user name.
\n\nUser Name:" \
${DEFAULT_yiimpadmin} \
yiimpadmin
if [ -z "${yiimpadmin}" ]; then
# user hit ESC/cancel
exit
fi
fi
if [ -z "${ssh_key:-}" ]; then
DEFAULT_ssh_key=PublicKey
input_box "Please open PuTTY Key Generator on your local machine and generate a new public key." \
"To paste your Public key use ctrl shift right click.
\n\nPublic Key:" \
${DEFAULT_ssh_key} \
ssh_key
if [ -z "${ssh_key}" ]; then
# user hit ESC/cancel
exit
fi
fi
# create random user password
RootPassword=$(openssl rand -base64 8 | tr -d "=+/")
clear
# Add user
echo -e "Adding new user and setting SSH key...$COL_RESET"
sudo adduser ${yiimpadmin} --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password
echo -e "${RootPassword}\n${RootPassword}" | passwd ${yiimpadmin}
sudo usermod -aG sudo ${yiimpadmin}
# Create SSH Key structure
mkdir -p /home/${yiimpadmin}/.ssh
touch /home/${yiimpadmin}/.ssh/authorized_keys
chown -R ${yiimpadmin}:${yiimpadmin} /home/${yiimpadmin}/.ssh
chmod 700 /home/${yiimpadmin}/.ssh
chmod 644 /home/${yiimpadmin}/.ssh/authorized_keys
authkeys=/home/${yiimpadmin}/.ssh/authorized_keys
echo "$ssh_key" > "$authkeys"
# enabling yiimpserver command
echo '# yiimp
# It needs passwordless sudo functionality.
'""''"${yiimpadmin}"''""' ALL=(ALL) NOPASSWD:ALL
' | sudo -E tee /etc/sudoers.d/${yiimpadmin} >/dev/null 2>&1
echo '
cd ~/yiimpserver/install
bash start.sh
' | sudo -E tee /usr/bin/yiimpserver >/dev/null 2>&1
sudo chmod +x /usr/bin/yiimpserver
# Check required files and set global variables
cd $HOME/yiimpserver/install
source pre_setup.sh
# Create the STORAGE_USER and STORAGE_ROOT directory if they don't already exist.
if ! id -u $STORAGE_USER >/dev/null 2>&1; then
sudo useradd -m $STORAGE_USER
fi
if [ ! -d $STORAGE_ROOT ]; then
sudo mkdir -p $STORAGE_ROOT
fi
# Save the global options in /etc/yiimpserver.conf so that standalone
# tools know where to look for data.
echo 'STORAGE_USER='"${STORAGE_USER}"'
STORAGE_ROOT='"${STORAGE_ROOT}"'
PUBLIC_IP='"${PUBLIC_IP}"'
PUBLIC_IPV6='"${PUBLIC_IPV6}"'
DISTRO='"${DISTRO}"'
PRIVATE_IP='"${PRIVATE_IP}"'' | sudo -E tee /etc/yiimpserver.conf >/dev/null 2>&1
sudo cp -r ~/yiimpserver /home/${yiimpadmin}/
cd ~
sudo setfacl -m u:${yiimpadmin}:rwx /home/${yiimpadmin}/yiimpserver
sudo rm -r $HOME/yiimpserver
clear
echo "New User is installed make sure you saved your private key..."
echo -e "$RED Please reboot system and log in as the new user and type$COL_RESET $GREEN yiimpserver$COL_RESET $RED to continue setup...$COL_RESET"
exit 0
fi
# New User Password Login Creation
if [ -z "${yiimpadmin:-}" ]; then
DEFAULT_yiimpadmin=yiimpadmin
input_box "New Account Name" \
"Please enter your desired user name.
\n\nUser Name:" \
${DEFAULT_yiimpadmin} \
yiimpadmin
if [ -z "${yiimpadmin}" ]; then
# user hit ESC/cancel
exit
fi
fi
if [ -z "${RootPassword:-}" ]; then
DEFAULT_RootPassword=$(openssl rand -base64 8 | tr -d "=+/")
input_box "User Password" \
"Enter your new user password or use this randomly system generated one.
\n\nUnfortunatley dialog doesnt let you copy. So you have to write it down.
\n\nUser password:" \
${DEFAULT_RootPassword} \
RootPassword
if [ -z "${RootPassword}" ]; then
# user hit ESC/cancel
exit
fi
fi
clear
dialog --title "Verify Your Responses" \
--yesno "Please verify your answers before you continue:
New User Name : ${yiimpadmin}
New User Pass : ${RootPassword}" 8 60
# Get exit status
# 0 means user hit [yes] button.
# 1 means user hit [no] button.
# 255 means user hit [Esc] key.
response=$?
case $response in
0)
clear
echo -e " Adding new user and password...$COL_RESET"
sudo adduser ${yiimpadmin} --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password
echo -e ""${RootPassword}"\n"${RootPassword}"" | passwd ${yiimpadmin}
sudo usermod -aG sudo ${yiimpadmin}
# enabling yiimpserver command
echo '# yiimp
# It needs passwordless sudo functionality.
'""''"${yiimpadmin}"''""' ALL=(ALL) NOPASSWD:ALL
' | sudo -E tee /etc/sudoers.d/${yiimpadmin} >/dev/null 2>&1
echo '
cd ~/yiimpserver/install
bash start.sh
' | sudo -E tee /usr/bin/yiimpserver >/dev/null 2>&1
sudo chmod +x /usr/bin/yiimpserver
# Check required files and set global variables
cd $HOME/yiimpserver/install
source pre_setup.sh
# Create the STORAGE_USER and STORAGE_ROOT directory if they don't already exist.
if ! id -u $STORAGE_USER >/dev/null 2>&1; then
sudo useradd -m $STORAGE_USER
fi
if [ ! -d $STORAGE_ROOT ]; then
sudo mkdir -p $STORAGE_ROOT
fi
# Save the global options in /etc/yiimpserver.conf so that standalone
# tools know where to look for data.
echo 'STORAGE_USER='"${STORAGE_USER}"'
STORAGE_ROOT='"${STORAGE_ROOT}"'
PUBLIC_IP='"${PUBLIC_IP}"'
PUBLIC_IPV6='"${PUBLIC_IPV6}"'
DISTRO='"${DISTRO}"'
PRIVATE_IP='"${PRIVATE_IP}"'' | sudo -E tee /etc/yiimpserver.conf >/dev/null 2>&1
sudo cp -r ~/yiimpserver /home/${yiimpadmin}/
cd ~
sudo setfacl -m u:${yiimpadmin}:rwx /home/${yiimpadmin}/yiimpserver
sudo rm -r $HOME/yiimpserver
clear
echo "New User is installed..."
echo -e "$RED Please reboot system and log in as the new user and type$COL_RESET $GREEN yiimpserver$COL_RESET $RED to continue setup...$COL_RESET"
exit 0;;
1)
clear
bash $(basename $0) && exit;;
255)
;;
esac