-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathrtadduser
executable file
·434 lines (374 loc) · 12.4 KB
/
rtadduser
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/bin/bash
######################################################################
#
# Copyright (c) 2015 arakasi72 (https://github.com/arakasi72)
#
# --> Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#
######################################################################
###############################
### SET VARIABLES ###
###############################
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
passfile='/etc/nginx/.htpasswd'
scgi_port_start=5000
rpc_start=2
rtport_start=51001
sshport=$(grep 'Port ' /etc/ssh/sshd_config | sed 's/[^0-9]*//g')
ftpport=$(grep 'listen_port=' /etc/vsftpd.conf | sed 's/[^0-9]*//g')
cronline1="@reboot sleep 10; /usr/local/bin/rtcheck irssi rtorrent"
cronline2="*/10 * * * * /usr/local/bin/rtcheck irssi rtorrent"
serverip=$(ip route get 8.8.8.8 | awk 'NR==1 {print $7}')
check_ssh=1
rtdstatus=$(rtdload)
#get user list
cd /var/www/rutorrent/conf/users
user_list=*
###############################
### FUNCTIONS ###
###############################
#function to determin if unix user name is valid
valid_name(){
until [[ $user =~ ^[a-z][-a-z0-9_]{2,31}$ ]]
do
echo "Enter user name (lowercase, numbers, dash and underscore):"
read user
done
}
# function to ask user for y/n response
ask_user(){
local answer
while true
do
read answer
case $answer in [Yy]* ) return 0 ;;
[Nn]* ) return 1 ;;
* ) echo "Enter y or n";;
esac
done
}
genpasswd() {
local genln=$1
[ -z "$genln" ] && genln=8
tr -dc A-Za-z0-9 < /dev/urandom | head -c ${genln} | xargs
}
random()
{
local min=$1
local max=$2
local RAND=`od -t uI -N 4 /dev/urandom | awk '{print $2}'`
RAND=$((RAND%((($max-$min)+1))+$min))
echo $RAND
}
find_port() {
local port_count=$1
local port_type=$2
local no_match=1
while [[ $no_match -ne 0 ]]
do
if [[ $port_type = "adl" ]]; then
port_count=$(random 34000 39000)
else
port_count=$(( $port_count + 1 ))
fi
for i in $user_list
do
current_user=$i
current_port=$(grep $port_type /var/www/rutorrent/conf/users/$current_user/config.php | sed 's/[^0-9]*//g')
if [[ $current_port = $port_count ]]; then
no_match=1
break
else
no_match=0
fi
done
done
echo $port_count
}
find_range() {
local low_count=$1
local high_count=$(( $low_count +249 ))
local no_match=1
local higher=0
while [[ $no_match -eq 1 ]]
do
low_count=$(( $low_count + 250 ))
high_count=$(( $high_count + 250 ))
for i in $user_list
do
base_user=$i
base_home=$(eval echo "~$base_user")
base_low=$(grep "port_range" $base_home/.rtorrent.rc | sed 's/[^0-9-]*//g' | cut -d- -f1)
base_high=$(grep "port_range" $base_home/.rtorrent.rc | sed 's/[^0-9-]*//g' | cut -d- -f2)
if [[ $low_count -gt $base_high ]]; then
no_match=0
higher=0
elif [[ $high_count -lt $base_low ]]; then
no_match=0
higher=1
else
no_match=1
break
fi
for n in $user_list
do
compare_user=$n
compare_home=$(eval echo "~$compare_user")
if [[ $no_match -eq 1 ]]; then
break
fi
if [[ $base_user != $compare_user ]]; then
compare_low=$(grep "port_range" $compare_home/.rtorrent.rc | sed 's/[^0-9-]*//g' | cut -d- -f1)
compare_high=$(grep "port_range" $compare_home/.rtorrent.rc | sed 's/[^0-9-]*//g' | cut -d- -f2)
if [[ $higher -eq 0 ]]; then
if [[ $base_low -gt $compare_high ]]; then
no_match=0
elif [[ $high_count -lt $compare_low ]]; then
no_match=0
elif [[ $compare_low -gt $base_high && $compare_high -lt $low_count ]]; then
no_match=0
else
no_match=1
break
fi
fi
if [[ $higher -eq 1 ]]; then
if [[ $base_high -lt $compare_low ]]; then
no_match=0
elif [[ $low_count -gt $compare_high ]]; then
no_match=0
elif [[ $compare_high -lt $base_low && $compare_low -gt $high_count ]]; then
no_match=0
else
no_match=1
break
fi
fi
fi
done
done
done
echo "$low_count-$high_count"
}
###############################
### INITIAL SYSTEM CHECKS ###
###############################
# set and prepare user
if [ "$(id -u)" != "0" ]; then
echo "Must be run from root or using sudo" && exit 1
fi
# get options
OPTS=$(getopt -n "$0" -o snu:p:w: --long "ssh-access,no-ssh,user:,password:,webpass:" -- "$@")
eval set -- "$OPTS"
while true; do
case "$1" in
-s | --ssh-access ) check_ssh=0; sshaccess=0; shift;;
-n | --no-ssh ) check_ssh=0; sshaccess=1; shift;;
-u | --user ) user="$2"; shift; shift;;
-p | --password ) unixpass="$2"; shift; shift;;
-w | --webpass ) webpass="$2"; shift; shift;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ "$user" = "root" ]; then
echo "Cannot create root"
exit
fi
###############################
### ADD USER ###
###############################
if [ -z "$user" ]; then
echo "Enter the name of the user"
echo "It can be an existing user or a new user"
echo
confirm_name=1
while [ $confirm_name = 1 ]
do
valid_name
echo -n "Confirm that user name is $user y/n? "
if ask_user; then
confirm_name=0
else
user=''
fi
done
else
if ! [[ $user =~ ^[a-z][-a-z0-9_]{2,31}$ ]]; then
echo "$user is not a valid user name please enter again"
confirm_name=1
while [ $confirm_name = 1 ]
do
valid_name
echo -n "Confirm that user name is $user y/n? "
if ask_user; then
confirm_name=0
else
user=''
fi
done
fi
fi
echo "User name is $user"
if id -u $user >/dev/null 2>&1; then
echo "$user already exists"
else
if [ -z "$unixpass" ]; then
adduser --gecos "" $user
else
adduser --gecos "" $user --disabled-password
echo "$user:$unixpass" | sudo chpasswd
fi
if id -u $user >/dev/null 2>&1; then
echo "$user successfully created"
else
echo "create user failed - exiting process"
exit 1
fi
fi
home=$(eval echo "~$user")
if [ -d "/var/www/rutorrent/conf/users/$user" ]; then
echo "Will reset $user config, filters and torrents will not be deleted"
fi
if groups $user | grep -q -E ' sshuser(\s|$)' || groups $user | grep -q -E ' sudo(\s|$)'; then
echo "ssh already granted"
check_ssh=0
fi
while [ $check_ssh = 1 ]
do
read -p "Grant $user ssh access? " answer
case $answer in [Yy]* ) sshaccess=0 check_ssh=0 ;;
[Nn]* ) sshaccess=1 check_ssh=0 ;;
* ) echo "Enter y or n";;
esac
done
if [ "$sshaccess" = "0" ]; then
adduser $user sshuser
fi
if [ "$user" = "$SUDO_USER" ]; then
echo "Will reset $user config, autodl filters and torrents will not be deleted"
cp -f /etc/rtinst/conf/ru.config /var/www/rutorrent/conf/config.php
fi
###############################
### CONFIGURE APPLICATIONS ###
###############################
#remove existing directory if there
if [ -d "/var/www/rutorrent/conf/users/$user" ]; then
rm -r /var/www/rutorrent/conf/users/$user
fi
#assign ports
newscgi=$(find_port $scgi_port_start scgi)
newrpc=$(find_port $rpc_start RPC)
adlport=$(find_port $(random 34000 39000) adl)
adlpass=$(genpasswd $(random 12 16))
newrange=$(find_range $rtport_start)
#add directories and config files
mkdir /var/www/rutorrent/conf/users/$user
mkdir /var/www/rutorrent/conf/users/$user/plugins
if ! [ -d "$home/rtorrent" ]; then
mkdir $home/rtorrent
fi
if ! [ -d "$home/rtorrent/.session" ]; then
mkdir $home/rtorrent/.session
fi
if ! [ -d "$home/rtorrent/download" ]; then
mkdir $home/rtorrent/download
fi
if ! [ -d "$home/rtorrent/watch" ]; then
mkdir $home/rtorrent/watch
fi
if [ -z "$webpass" ] && [ -z $(grep -s $user $passfile) ]; then
webpass=$(genpasswd)
fi
if [ ! -z "$webpass" ]; then
htpasswd -b $passfile $user $webpass
fi
cp -f /etc/rtinst/conf/rtorrent.rc $home/.rtorrent.rc
sed -i "s|<user home>|${home}|g" $home/.rtorrent.rc
sed -i "s/<user name>/$user/g" $home/.rtorrent.rc
sed -i "s/port_range = 51001-51250/port_range = $newrange/g" $home/.rtorrent.rc
sed -i "s/network\.port_range\.set = 51001-51250/network\.port_range\.set = $newrange/g" $home/.rtorrent.rc
sed -i "s/network\.scgi\.open_port = 127\.0\.0\.1:5000/network\.scgi\.open_port = 127\.0\.0\.1:$newscgi/g" $home/.rtorrent.rc
echo "<?php" > /var/www/rutorrent/conf/users/$user/config.php
echo >> /var/www/rutorrent/conf/users/$user/config.php
echo "\$homeDirectory = \"$home\";" >> /var/www/rutorrent/conf/users/$user/config.php
echo "\$topDirectory = \"$home\";" >> /var/www/rutorrent/conf/users/$user/config.php
echo "\$scgi_port = $newscgi;" >> /var/www/rutorrent/conf/users/$user/config.php
echo "\$XMLRPCMountPoint = \"/RPC$newrpc\";" >> /var/www/rutorrent/conf/users/$user/config.php
echo >> /var/www/rutorrent/conf/users/$user/config.php
echo "?>" >> /var/www/rutorrent/conf/users/$user/config.php
###############################
### AUTODL-IRSSI ###
###############################
mkdir /var/www/rutorrent/conf/users/$user/plugins/autodl-irssi
mkdir -p $home/.irssi/scripts/autorun
cd $home/.irssi/scripts
curl -sL http://git.io/vlcND | sed -n 's/.*browser_download_url": \?"\(.*zip\)".*/\1/p' | xargs wget --quiet -O autodl-irssi.zip
unzip -o autodl-irssi.zip >> /dev/null 2>&1
rm autodl-irssi.zip
cp autodl-irssi.pl autorun/
mkdir -p $home/.autodl
echo "<?php" > /var/www/rutorrent/conf/users/$user/plugins/autodl-irssi/conf.php
echo >> /var/www/rutorrent/conf/users/$user/plugins/autodl-irssi/conf.php
echo "\$autodlPort = $adlport;" >> /var/www/rutorrent/conf/users/$user/plugins/autodl-irssi/conf.php
echo "\$autodlPassword = \"$adlpass\";" >> /var/www/rutorrent/conf/users/$user/plugins/autodl-irssi/conf.php
echo >> /var/www/rutorrent/conf/users/$user/plugins/autodl-irssi/conf.php
echo "?>" >> /var/www/rutorrent/conf/users/$user/plugins/autodl-irssi/conf.php
cd $home/.autodl
if [ -f "autodl.cfg" ]; then
if (grep -sq gui-server-port autodl.cfg) && (grep -sq gui-server-password autodl.cfg); then
sed -i "/gui-server-port/ c\gui-server-port = $adlport" autodl.cfg
sed -i "/gui-server-password/ c\gui-server-password = $adlpass" autodl.cfg
else
sed -i '/gui-server-port/ d' autodl.cfg
sed -i '/gui-server-password/ d' autodl.cfg
echo >> autodl.cfg
sed -i "1s/^/[options]\ngui-server-port = $adlport\ngui-server-password = $adlpass\n/" autodl.cfg
fi
else
echo "[options]" > autodl.cfg
echo "gui-server-port = $adlport" >> autodl.cfg
echo "gui-server-password = $adlpass" >> autodl.cfg
fi
chown -R $user:$user $home
chown -R www-data:www-data /var/www/rutorrent
if [ -z "$(crontab -u $user -l | grep "$cronline1")" ]; then
(crontab -u $user -l; echo "$cronline1" ) | crontab -u $user -
fi
if [ -z "$(crontab -u $user -l | grep "\*/10 \* \* \* \* /usr/local/bin/rtcheck irssi rtorrent")" ]; then
(crontab -u $user -l; echo "$cronline2" ) | crontab -u $user -
fi
su $user -c '/usr/local/bin/rt restart'
su $user -c '/usr/local/bin/rt -i restart'
echo "$user setup complete"
echo "SCGI Port: $newscgi"
echo "RPC Mount: $newrpc"
echo "rtorrent port range $newrange"
echo "autodl port: $adlport"
echo "autodl pass: $adlpass"
echo "crontab entries made, rtorrent and irssi will start on boot"
echo
###############################
### WRITE RTINST.INFO ###
###############################
echo > $home/rtinst.info
if groups $user | grep -q -E ' sshuser(\s|$)' || groups $user | grep -q -E ' sudo(\s|$)'; then
echo "$user has SSH access on port $sshport" | tee -a $home/rtinst.info
echo
fi
echo "ftp client should be set to explicit ftp over tls using port $ftpport" | tee -a $home/rtinst.info
echo
if [ "$rtdstatus" = "enabled" ]; then
echo "https downloads can be accessed at https://$serverip/download/$user" | tee -a $home/rtinst.info
echo
fi
echo "rutorrent can be accessed at https://$serverip/rutorrent" | tee -a $home/rtinst.info
if [ -z $webpass ]; then
echo "rutorrent password has not been changed" | tee -a $home/rtinst.info
else
echo "rutorrent password set to $webpass" | tee -a $home/rtinst.info
fi
echo "to change rutorrent password enter: rtpass" | tee -a $home/rtinst.info
echo
echo "The above info is stored at $home/rtinst.info"
chown $user:$user $home/rtinst.info