forked from arakasi72/rtinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtadduser
355 lines (304 loc) · 10.5 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
#!/bin/bash
######################################################################
#
# Copyright (c) 2015 arakasi72 (https://github.com/arakasi72)
#
# --> Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#
######################################################################
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=$(cat '/etc/pure-ftpd/conf/Bind')
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 $NF}')
#get user list
cd /var/www/rutorrent/conf/users
user_list=*
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 = 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 = 0 ]
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=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 = 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 $count_low ]; then
no_match=0
else
no_match=1
break
fi
fi
if [ $higher = 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 $count_high ]; then
no_match=0
else
no_match=1
break
fi
fi
fi
done
done
done
echo "$low_count-$high_count"
}
# set and prepare user
if ! [ "$LOGNAME" = "root" ]; then
echo "Must be run from root or using sudo" && exit 1
fi
echo "Enter the name of the user to add"
echo "It can be an existing user or a new user"
echo
confirm_name=1
while [ $confirm_name = 1 ]
do
read -p "Enter user name: " answer
addname=$answer
check_name=1
if [ "$addname" = "root" ]; then
echo "Cannot create root"
exit
fi
while [ $check_name = 1 ]
do
read -p "Is $addname correct? " answer
case $answer in [Yy]* ) confirm_name=0 && check_name=0 ;;
[Nn]* ) confirm_name=1 && check_name=0 ;;
* ) echo "Enter y or n";;
esac
done
done
user=$addname
if id -u $user >/dev/null 2>&1; then
echo "$user already exists"
else
echo "adding $user"
adduser --gecos "" $user
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
check_ssh=1
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
groupadd sshuser
allowlist=$(grep AllowUsers /etc/ssh/sshd_config)
if ! [ -z "$allowlist" ]; then
for ssh_user in $allowlist
do
if ! [ "$ssh_user" = "AllowUsers" ]; then
adduser $ssh_user sshuser
fi
done
perl -pi -e "s/$allowlist//g" /etc/ssh/sshd_config
service ssh restart
fi
groupslist=$(grep AllowGroups /etc/ssh/sshd_config)
echo $groupslist
if [ -z "$groupslist" ]; then
echo "AllowGroups sudo sshuser" | tee -a /etc/ssh/sshd_config > /dev/null
service ssh restart
fi
if [ -z "$(groups $user | grep "sshuser")" ]; then
adduser $user sshuser
fi
fi
rtgetscripts
if [ "$user" = "$SUDO_USER" ]; then
echo "Will reset $user config, autodl filters and torrents will not be deleted"
rtgetscripts /var/www/rutorrent/conf/config.php ru.config
fi
#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/downloads" ]; then
mkdir $home/rtorrent/downloads
fi
if ! [ -d "$home/rtorrent/watch" ]; then
mkdir $home/rtorrent/watch
fi
WEBPASS=$(genpasswd)
htpasswd -b $passfile $user $WEBPASS
rtgetscripts $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\.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
#add auto-dl
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 | grep -Po '(?<="browser_download_url": ")(.*-v[\d.]+.zip)' | 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
touch $home/.autodl/autodl.cfg && touch $home/.autodl/autodl2.cfg
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
echo "[options]" > autodl2.cfg
echo "gui-server-port = $adlport" >> autodl2.cfg
echo "gui-server-password = $adlpass" >> autodl2.cfg
chown -R $user:$user $home
chown -R www-data:www-data /var/www/rutorrent
chmod -R 755 /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'
dload_line=$(grep "include /etc/nginx/conf.d/rtdload;" /etc/nginx/sites-available/default)
dload_hash=$(echo $dload_line | sed 's/[^#]*//g')
if [ ! -z "$dload_line" -a -z "$dload_hash" ]; then
dload_flag=0
else
dload_flag=1
fi
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
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 [ $dload_flag = 0 ]; 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
echo "rutorrent password set to $WEBPASS" | tee -a $home/rtinst.info
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