· 10 years ago · May 12, 2016, 09:12 AM
1#!/bin/sh
2# ***** BEGIN LICENSE BLOCK *****
3# Version: MPL 1.1
4#
5# The contents of this file are subject to the Mozilla Public License Version
6# 1.1 (the "License"); you may not use this file except in compliance with
7# the License. You may obtain a copy of the License at
8# http://www.mozilla.org/MPL/
9#
10# Software distributed under the License is distributed on an "AS IS" basis,
11# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12# for the specific language governing rights and limitations under the
13# License.
14#
15# The Original Code is IRC Auto Downloader
16#
17# The Initial Developer of the Original Code is
18# David Nilsson.
19# Portions created by the Initial Developer are Copyright (C) 2010, 2011
20# the Initial Developer. All Rights Reserved.
21#
22# Contributor(s):
23#
24# ***** END LICENSE BLOCK *****
25
26AUTODL_IRSSI_ZIP_URL="http://update.autodl-community.com/autodl-irssi-community.zip"
27GIT_PATH_RUTORRENT_PLUGIN="https://github.com/autodl-community/autodl-rutorrent.git"
28
29WEBMIN_URL="http://www.webmin.com/download/webmin-current.tar.gz"
30
31# RUTORRENT_TRUNK_DIR="https://rutorrent.googlecode.com/svn/trunk"
32RUTORRENT_TRUNK_DIR="https://github.com/Novik/ruTorrent.git"
33
34# The official tarballs are tried if git fails.
35RUTORRENT_VERSION="3.6"
36RUTORRENT_CORE_NAME="rutorrent-$RUTORRENT_VERSION.tar.gz"
37RUTORRENT_CORE_URL="http://dl.bintray.com/novik65/generic/$RUTORRENT_CORE_NAME"
38#RUTORRENT_CORE_URL="http://rutorrent.googlecode.com/files/$RUTORRENT_CORE_NAME"
39RUTORRENT_PLUGINS_NAME="plugins-$RUTORRENT_VERSION.tar.gz"
40#RUTORRENT_PLUGINS_URL="http://rutorrent.googlecode.com/files/$RUTORRENT_PLUGINS_NAME"
41RUTORRENT_PLUGINS_URL="http://dl.bintray.com/novik65/generic/$RUTORRENT_PLUGINS_NAME"
42#RUTORRENT_CORE_URL2="http://sourceforge.net/projects/autodl-irssi/files/inst-files/$RUTORRENT_CORE_NAME/download"
43#RUTORRENT_PLUGINS_URL2="http://sourceforge.net/projects/autodl-irssi/files/inst-files/$RUTORRENT_PLUGINS_NAME/download"
44
45RUTORRENT_PLUGINS=
46
47# These are the ruTorrent plugins that will get installed. To remove one, either
48# remove the whole line or comment it (prepend a '#' to the start of the line).
49# If the line starts with '#', the plugin is not installed.
50RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS _getdir"
51RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS _noty2"
52RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS _task"
53RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS autotools"
54RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS check_port"
55RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS chunks"
56RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS cookies"
57RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS cpuload"
58RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS create"
59RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS data"
60RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS datadir"
61RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS diskspace"
62RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS edit"
63RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS erasedata"
64RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS extratio"
65RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS extsearch"
66RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS feeds"
67RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS filedrop"
68RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS geoip"
69RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS history"
70RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS httprpc"
71RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS ipad"
72RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS loginmgr"
73RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS lookat"
74RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS mediainfo"
75RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS ratio"
76RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS retrackers"
77#RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS rpc"
78RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS rss"
79RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS rssurlrewrite"
80RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS rutracker_check"
81RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS scheduler"
82RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS screenshots"
83RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS seedingtime"
84#RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS show_peers_like_wtorrent"
85RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS source"
86RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS theme"
87RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS throttle"
88RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS tracklabels"
89RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS trafic"
90RUTORRENT_PLUGINS="$RUTORRENT_PLUGINS unpack"
91
92
93# For building rtorrent
94LIBCURL_NAME="curl-7.38.0"
95LIBCURL_URL="http://curl.haxx.se/download/$LIBCURL_NAME.tar.gz"
96SIGCPP20_NAME="libsigc++-2.3.1"
97SIGCPP20_URL="http://ftp.gnome.org/pub/GNOME/sources/libsigc++/2.3/$SIGCPP20_NAME.tar.gz"
98XMLRPC_SVN_DIR="https://svn.code.sf.net/p/xmlrpc-c/code/stable"
99LIBTORRENT_VERSION="0.12.9"
100LIBTORRENT_NAME="libtorrent-$LIBTORRENT_VERSION"
101LIBTORRENT_URL="http://libtorrent.rakshasa.no/downloads/$LIBTORRENT_NAME.tar.gz"
102LIBTORRENT_URL2="http://sourceforge.net/projects/autodl-irssi/files/inst-files/$LIBTORRENT_NAME.tar.gz/download"
103RTORRENT_VERSION="0.8.9"
104RTORRENT_NAME="rtorrent-$RTORRENT_VERSION"
105RTORRENT_URL="http://libtorrent.rakshasa.no/downloads/$RTORRENT_NAME.tar.gz"
106RTORRENT_URL2="http://sourceforge.net/projects/autodl-irssi/files/inst-files/$RTORRENT_NAME.tar.gz/download"
107
108HTPASSWD_PY_SCRIPT_URL="http://trac.edgewall.org/export/10433/trunk/contrib/htpasswd.py"
109HTPASSWD_PY_SCRIPT_URL2="http://sourceforge.net/projects/autodl-irssi/files/inst-files/htpasswd.py/download"
110
111NGINX_NAME="nginx-1.7.0"
112NGINX_URL="http://nginx.org/download/$NGINX_NAME.tar.gz"
113
114LIGHTTPD_NAME="lighttpd-1.4.35"
115LIGHTTPD_URL="http://download.lighttpd.net/lighttpd/releases-1.4.x/$LIGHTTPD_NAME.tar.gz"
116
117MOD_SCGI_VERSION="1.14"
118MOD_SCGI_URL="http://python.ca/scgi/releases/scgi-$MOD_SCGI_VERSION.tar.gz"
119
120UNRAR_VERSION="4.0.7"
121UNRAR_URL="http://www.rarlab.com/rar/unrarsrc-$UNRAR_VERSION.tar.gz"
122
123# Dirs relative to user's home directory
124RTORRENT_REL_DOWNLOAD_DIR="downloads"
125RTORRENT_REL_WATCH_DIR="rtorrent/watch"
126RTORRENT_REL_SESSION_DIR="rtorrent/session"
127
128REQUIRED_PERL_MODULES="Time::HiRes XML::LibXML Archive::Zip Net::SSLeay HTML::Parser Digest::SHA JSON"
129REQUIRED_PHP_MODULES="json xml sockets"
130
131# If set to y, add 'load perl' to .irssi/startup
132IRSSI_LOAD_PERL=n
133IGNORE_IRSSI=n
134
135# Start port. When we need a new port number, this port is incremented by one.
136CURRENT_PORT=23875
137DEFAULT_PORT_FTP=21
138DEFAULT_PORT_FTPES=990
139DEFAULT_UMASK=022
140RPC_PREFIX=RPC
141
142LSB_DEFAULT_START="2 3 4 5"
143LSB_DEFAULT_STOP="0 1 6"
144
145# Setting these to anything other than 80 and 443 may not work with Apache (since the
146# code assumed it would always be 80 and 443).
147HTTP_PORT=80
148HTTPS_PORT=443
149
150SCGI_HOST="127.0.0.1"
151
152INTERACTIVE=n
153USE_RUTORRENT_PLUGIN=n
154REINSTALL_RUTORRENT_PLUGIN=n
155INSTALL_AUTODL_IRSSI=n
156RUTORRENT_PASSWORD_PROTECTED=n
157USERS=
158RUTORRENT_BASE_PATH=
159INSTALL_STARTUP_SCRIPT=n
160BUILD_RTORRENT=n
161INSTALL_WEB_SERVER=
162INSTALL_RUTORRENT=n
163INSTALL_VSFTPD=n
164FTP_PORT=
165USE_ENCRYPTED_FTP=n
166INSTALL_WEBMIN=n
167USER_TIMEZONE=
168
169RUTORRENT_SITE_REALM="My ruTorrent web site"
170AUTODL_STARTUP_DESC="Start autodl-irssi and rtorrent"
171PHPCGI_STARTUP_DESC="Start php-cgi"
172NGINX_STARTUP_DESC="Start nginx"
173LIGHTTPD_STARTUP_DESC="Start lighttpd"
174
175PATH="$PATH:/usr/local/bin"
176
177# Find an echo that supports -e
178echo=echo
179for cmd in echo /bin/echo; do
180 $cmd >/dev/null 2>&1 || continue
181 if ! $cmd -e "" | grep -qE '^-e'; then
182 echo=$cmd
183 break
184 fi
185done
186
187CSI=$($echo -e "\033[")
188CEND="${CSI}0m"
189CDGREEN="${CSI}32m"
190CRED="${CSI}1;31m"
191CGREEN="${CSI}1;32m"
192CYELLOW="${CSI}1;33m"
193CBLUE="${CSI}1;34m"
194CMAGENTA="${CSI}1;35m"
195CCYAN="${CSI}1;36m"
196CQUESTION="$CMAGENTA"
197CWARNING="$CRED"
198CMSG="$CCYAN"
199
200errorExit() {
201 cat << EOF
202$CWARNING
203***ERROR***
204$*
205***ERROR***
206$CEND
207Help channel: ${CGREEN}#autodl-community@irc.p2p-network.net$CEND
208EOF
209 exit 1
210}
211
212exitHelp() {
213 cat << EOF
214
215autodl-irssi and ruTorrent plugin installer
216
217 sh $0 [options]
218
219Options:
220
221--rtorrent
222 Will build and install rtorrent, libtorrent and xmlrpc-c from source code.
223
224--rtorrent-noexec-patch
225 Patch rtorrent to disallow remote users from executing code.
226
227--apache
228 Install Apache web server.
229
230--nginx
231 Install nginx web server.
232
233--lighttpd
234 Install lighttpd web server.
235
236--rutorrent
237 Install ruTorrent. Requires a web server, eg. --apache.
238
239--vsftpd
240 Install vsftpd.
241
242--ftpes
243 Use encrypted FTP (FTPES).
244
245--ftp-port <port>
246 FTP server port.
247
248--webmin
249 Install Webmin.
250
251-p
252--rutorrent-plugin
253 Use or install the autodl-irssi ruTorrent plugin. If it's already installed,
254 it will be updated (svn up), unless --reinstall-plugin option is used in
255 which case it will be re-installed.
256
257-i
258--reinstall-plugin
259 Re-install the autodl-irssi ruTorrent plugin if it's already installed.
260
261-a
262--install-autodl
263 Install autodl-irssi.
264
265-u user:autodlPass:webUser:webPass
266--user user:autodlPass:webUser:webPass
267 The $(uname -s) user, autodl-irssi password, ruTorrent user, ruTorrent password.
268 You can use more than one -u option. If autodlPassword is not set, a random
269 password will be used.
270
271-w
272--password-protected
273 Use this option if ruTorrent is password protected. It's required if you
274 want to use more than one ruTorrent user. Not needed if you install
275 ruTorrent.
276
277-r PATH
278--rutorrent-base-path PATH
279 Path to ruTorrent, eg. /var/www/rutorrent. Not needed if you install
280 ruTorrent.
281
282-s
283--install-startup-script
284 Install a startup script (service) which will start Irssi and rtorrent when
285 the computer boots.
286
287-h
288--help
289 Show this help text
290
291Examples (as root user):
292
293 sh $0 --rtorrent --apache --rutorrent --vsftpd --ftpes --webmin -p -a -s -u user1::user1:pass1 -u user2::user2:pass2
294
295Installs rtorrent, Apache, ruTorrent, vsftpd (encrypted), Webmin the
296autodl-irssi plugin, autodl-irssi, installs a service (starting Irssi and
297rtorrent) for both users.
298
299 sh $0 -p -a -u user::user:pass -r /var/www/rutorrent -s
300
301Installs autodl-irssi + ruTorrent plugin for user 'user'. ruTorrent has not been
302password protected. Also installs the startup script.
303
304 sh $0 -p -a -u user1::user1:pass1 -u user2::user2:pass2 -w -r /var/www/rutorrent -s
305
306Installs autodl-irssi + ruTorrent plugin for users 'user1' and 'user2'.
307ruTorrent has been password protected. Also installs the startup script.
308
309 sh $0 --rtorrent -a -u user1 -u user2 -u user3 -u user4 -s
310
311Installs rtorrent, autodl-irssi and the startup script.
312
313Help fchannel: #autodl-community@irc.p2p-network.net
314EOF
315 exit 1
316}
317
318parseCommandLine() {
319 while [ $# -gt 0 ]; do
320 local arg="$1"
321 shift
322
323 if [ "$arg" = "-p" ] || [ "$arg" = "--rutorrent-plugin" ]; then
324 USE_RUTORRENT_PLUGIN=y
325 elif [ "$arg" = "-i" ] || [ "$arg" = "--reinstall-plugin" ]; then
326 REINSTALL_RUTORRENT_PLUGIN=y
327 elif [ "$arg" = "-a" ] || [ "$arg" = "--install-autodl" ]; then
328 INSTALL_AUTODL_IRSSI=y
329 elif [ "$arg" = "-u" ] || [ "$arg" = "--user" ]; then
330 USERS="$USERS $1"
331 shift
332 elif [ "$arg" = "-w" ] || [ "$arg" = "--password-protected" ]; then
333 RUTORRENT_PASSWORD_PROTECTED=y
334 elif [ "$arg" = "-r" ] || [ "$arg" = "--rutorrent-base-path" ]; then
335 RUTORRENT_BASE_PATH="$1"
336 shift
337 elif [ "$arg" = "-s" ] || [ "$arg" = "--install-startup-script" ]; then
338 INSTALL_STARTUP_SCRIPT=y
339 elif [ "$arg" = "--rtorrent" ]; then
340 BUILD_RTORRENT=y
341 elif [ "$arg" = "--apache" ]; then
342 INSTALL_WEB_SERVER=apache
343 elif [ "$arg" = "--nginx" ]; then
344 INSTALL_WEB_SERVER=nginx
345 elif [ "$arg" = "--lighttpd" ]; then
346 INSTALL_WEB_SERVER=lighttpd
347 elif [ "$arg" = "--rutorrent" ]; then
348 INSTALL_RUTORRENT=y
349 elif [ "$arg" = "--vsftpd" ]; then
350 INSTALL_VSFTPD=y
351 elif [ "$arg" = "--ftpes" ]; then
352 USE_ENCRYPTED_FTP=y
353 elif [ "$arg" = "--ftp-port" ]; then
354 FTP_PORT="$1"
355 shift
356 elif [ "$arg" = "--webmin" ]; then
357 INSTALL_WEBMIN=y
358 else
359 exitHelp
360 fi
361 done
362}
363
364# Sets os and os_long to the OS type and OS name respectively
365detectOs() {
366 local DISTRIB_ID=
367 local DISTRIB_DESCRIPTION=
368 if [ -f /etc/lsb-release ]; then
369 . /etc/lsb-release
370 fi
371
372 if [ -f /etc/fedora-release ]; then
373 os=fedora
374 os_long="$(cat /etc/fedora-release)"
375 # Must be before a whole bunch of other OS tests
376 elif [ "$DISTRIB_ID" = "Ubuntu" ]; then
377 os=debian
378 os_long="$DISTRIB_DESCRIPTION"
379 elif [ "$DISTRIB_ID" = "LinuxMint" ]; then
380 os=debian
381 os_long="$DISTRIB_DESCRIPTION"
382 # Must be before Debian
383 elif [ "$DISTRIB_ID" = "Peppermint" ]; then
384 os=debian
385 os_long="$DISTRIB_DESCRIPTION"
386 elif [ "$DISTRIB_ID" = "MEPIS" ]; then
387 os=debian
388 os_long="$DISTRIB_DESCRIPTION"
389 elif [ -f /etc/clearos-release ]; then
390 os=fedora
391 os_long="$(cat /etc/clearos-release)"
392 elif [ -f /etc/debian_version ]; then
393 os=debian
394 local prefix=
395 if ! uname -s | grep -q GNU; then
396 prefix="GNU/"
397 fi
398 os_long="Debian $prefix$(uname -s) $(cat /etc/debian_version)"
399 elif [ -f /etc/redhat-release ]; then
400 os=fedora
401 os_long="$(cat /etc/redhat-release)"
402 else
403 echo -e "\e[1;31mYour OS is not detected as an OS supported by this script. This script only supports \e[5;32mDebian and Fedora\e[0m \e[1;31mbased OSes.\e[0m"
404 exit 1
405
406 fi
407
408 os_long="${os_long:-$(uname -s)}"
409}
410
411# In-place editing like sed -i but more portable...
412sed_i() {
413 local cmd="$1"
414 local file="$2"
415 isProgramInstalled ed || errorExit "ed is not installed!"
416 local addr=","
417 echo "$cmd" | grep -qE '^/' && addr=
418 ed -s "$file" > /dev/null 2>&1 << EOF
419$addr$cmd
420w
421q
422EOF
423}
424
425getFirst() {
426 echo $1
427}
428
429# Returns true if $1 is one of $2..$n
430arrayIsPresent() {
431 local val="$1"
432 shift
433 for v in "$@"; do
434 [ "$v" = "$val" ] && return 0
435 done
436 return 1
437}
438
439canGeneratePasswords() {
440 [ -c /dev/urandom ]
441}
442
443generatePassword() {
444 newPassword=$(cat /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c17)
445}
446
447isProgramInstalled() {
448 which "$1" > /dev/null 2>&1 && return 0
449 return 1
450}
451
452isPerlModuleInstalled() {
453 perl -M$1 -e '' > /dev/null 2>&1 && return 0
454 return 1
455}
456
457# Add $1 or $2 if it exists, to PACKAGES if program $1 isn't present
458addProgram() {
459 isProgramInstalled $1 || PACKAGES="$PACKAGES ${2:-$1}"
460}
461
462# Add $2 to PACKAGES if perl module $1 isn't present
463addPerlModule() {
464 isPerlModuleInstalled $1 || PACKAGES="$PACKAGES $2"
465}
466
467installPackages() {
468 [ -z "$INSTALL" ] && return
469 [ -z "$PACKAGES" ] && return
470 $INSTALL $PACKAGES
471 PACKAGES=
472}
473
474installPerlModule() {
475 isPerlModuleInstalled $1 || $INSTALL $2
476}
477
478installProgram() {
479 isProgramInstalled $1 || $INSTALL ${2:-$1}
480}
481
482installBuildTools() {
483 [ "$BUILD_TOOLS_INSTALLED" = y ] && return
484 PACKAGES="$BUILD_TOOLS"
485 installPackages
486 BUILD_TOOLS_INSTALLED=y
487}
488
489# Detects missing Perl modules and stores them in MISSING_PERL_MODULES
490detectMissingPerlModules() {
491 OLD_PERL_MODULES=
492 MISSING_PERL_MODULES=
493 for module in $REQUIRED_PERL_MODULES; do
494 isPerlModuleInstalled $module || MISSING_PERL_MODULES="$MISSING_PERL_MODULES $module"
495 done
496
497 # Make sure the JSON module is new
498 if isPerlModuleInstalled JSON && ! perl -MJSON -e '&decode_json("{}")' > /dev/null 2>&1; then
499 echo "${CWARNING}Old JSON module is installed. Need to install from CPAN.$CEND"
500 MISSING_PERL_MODULES="$MISSING_PERL_MODULES JSON"
501 OLD_PERL_MODULES="$OLD_PERL_MODULES JSON"
502 fi
503
504 if echo "$MISSING_PERL_MODULES" | grep -wq JSON; then
505 MISSING_PERL_MODULES="JSON::XS $MISSING_PERL_MODULES"
506 fi
507 if echo "$MISSING_PERL_MODULES" | grep -q 'XML::LibXML'; then
508 # Sometimes the cpan script doesn't install the required dependencies
509 MISSING_PERL_MODULES="XML::NamespaceSupport XML::SAX $MISSING_PERL_MODULES"
510 fi
511}
512
513# Detect where cpan is installed and place the path in the CPAN variable.
514# PCLinuxOS/Mandriva append the version number...
515detectCpanBin() {
516 for file in $(which cpan 2> /dev/null) $(ls /usr/bin/cpan-* 2> /dev/null); do
517 if [ -x "$file" ]; then
518 CPAN="$file"
519 return
520 fi
521 done
522 isPerlModuleInstalled CPAN && return
523 errorExit "Could not find the cpan script or the CPAN Perl module. Can't install missing Perl modules."
524}
525
526isValidTzName() {
527 echo "$1" | LC_ALL=C grep -qE '^[A-Z][^/ ]*(/[A-Z][^/ ]*)?$' && return 0
528 return 1
529}
530
531detectTimeZone() {
532 [ -n "$USER_TIMEZONE" ] && return
533
534 local hash=
535 local hasher=
536 local files=
537 local zoneinfoPath=/usr/share/zoneinfo
538 [ -d "$zoneinfoPath" ] || return
539 if hasher=md5sum; isProgramInstalled $hasher || hasher=sha1sum; isProgramInstalled $hasher; then
540 hash=$($hasher /etc/localtime | awk '{print $1}')
541 files="$(find $zoneinfoPath -type f -print | xargs $hasher | grep -E "^$hash\\>" | awk '{print $2}')"
542 elif hasher=md5; isProgramInstalled $hasher || hasher=sha1; isProgramInstalled $hasher; then
543 hash=$($hasher /etc/localtime | sed -e 's/^.* = \([a-zA-Z0-9]*\)$/\1/')
544 files="$(find $zoneinfoPath -type f -print | xargs $hasher | grep -E "$hash\$" | sed -e 's/^[^ ]* (\([^)]*\)).*/\1/')"
545 else
546 return
547 fi
548
549 # Detect all possible timezone names
550 local timezones=
551 for path in $files; do
552 local tz="${path#$zoneinfoPath/}"
553 while true; do
554 isValidTzName "$tz" && break
555 local newTz="${tz#*/}"
556 [ "$newTz" = "$tz" ] && tz= && break
557 tz="$newTz"
558 done
559 isValidTzName "$tz" && timezones="$timezones $tz"
560 done
561
562 # Now find the ones PHP likes...
563 local okRegions="(Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)"
564 for tz in $timezones; do
565 echo "$tz" | grep -qE "^$okRegions/" && USER_TIMEZONE="$tz" && return
566 done
567
568 # Return the first one we found
569 for tz in $timezones; do
570 USER_TIMEZONE="$tz"
571 return
572 done
573}
574
575getNumCpus() {
576 [ -n "$NUMCPUS" ] && return
577 [ -f /proc/cpuinfo ] && NUMCPUS=$(cat /proc/cpuinfo | grep '^processor' | wc -l) && return
578 isProgramInstalled sysctl && sysctl -n hw.ncpu | grep -qE '^[1-9][0-9]*$' && NUMCPUS=$(sysctl -n hw.ncpu) && return
579 NUMCPUS=1
580}
581
582# Appends $value to $varName in $file, creating the var if it doesn't exist. The
583# value is not appended if it already exists.
584appendFileStringVar() {
585 local file="$1"
586 local varName="$2"
587 local value="$3"
588
589 [ -f "$file" ] || errorExit "File '$file' does not exist"
590
591 local varLine=$(grep -E "^$varName=" "$file" | tail -n1)
592 if [ -z "$varLine" ]; then
593 echo "$varName=\"$value\"" >> "$file"
594 else
595 echo "$varLine" | grep -qE "[='\" ]$value(\"|'| |$)" && return
596
597 local q=
598 echo "$varLine" | grep -qE "^$varName='" && q="'"
599 echo "$varLine" | grep -qE "^$varName=\"" && q='"'
600 sed_i "s#^\\($varName=\\)$q\\(.*\\)$q#\\1\"\\2 $value\"#" "$file"
601 fi
602}
603
604# Set a variable in a script file to a new value. The variable is created if it does not exist
605setScriptVariable() {
606 local file="$1"
607 local name=$2
608 local value="$3"
609
610 [ -f "$file" ] || errorExit "File does not exist: $file"
611
612 local newLine="$name=\"$value\""
613 if grep -qE "^$name=" $file; then
614 sed_i "s!^$name=.*\$!$newLine!" "$file"
615 else
616 echo "$newLine" >> "$file"
617 fi
618}
619
620userExists() {
621 id -rg "$1" > /dev/null 2>&1 && return 0
622 return 1
623}
624
625setSvnOpts() {
626 SVN_OPTS=
627 isProgramInstalled svn || return
628 # svn sometimes gives an error message 'broken pipe'. Ignore it.
629 if svn help co 2>/dev/null | grep -q 'non-interactive' && svn help co 2>/dev/null | grep -q 'trust-server-cert'; then
630 SVN_OPTS="--non-interactive --trust-server-cert"
631 fi
632}
633
634downloadFile() {
635 local filename="$1"
636 shift
637 for url in "$@"; do
638 for i in 1 2 3 4 5; do
639 wget --no-check-certificate -O "$filename" "$url" && return 0
640 done
641 done
642 return 1
643}
644
645buildMakeProgram() {
646 local url="$1"
647 local name="$2"
648 local builddir="$3"
649 local srcdir="$4"
650 local makefile="${5:-Makefile}"
651
652 buildStart
653 echo "${CMSG}Downloading $name source code...$CEND"
654 cd "$BUILD_DIR"
655 local filename="${url##*/}"
656 downloadFile "$filename" "$url" || errorExit "Could not download $name source code."
657 tar xzf "$filename"
658 srcdir="${srcdir:-${filename%.tar.gz}}"
659 cd "$srcdir/$builddir"
660 local make=${MAKE:-make}
661 echo "${CMSG}Building $name...$CEND"
662 $make -f $makefile all || errorExit "Could not build $name."
663 $make -f $makefile install || errorExit "Could not install $name."
664 echo "${CMSG}$name is now installed$CEND"
665 buildEnd
666}
667
668buildProgram() {
669 local prog="$1"
670
671 echo "${CMSG}Building $prog...$CEND"
672
673 ./configure --prefix=$PREFIX $2 || errorExit "Could not configure $prog. Try again, or try logging out and in and try again."
674
675 getNumCpus
676 local make=${MAKE:-make}
677 local makeopts="-j$NUMCPUS"
678 $make $makeopts || $make || errorExit "Could not build $prog"
679 $make install || errorExit "Could not install $prog"
680
681 local ldsoconf=/etc/ld.so.conf
682 touch "$ldsoconf"
683 grep -qE "^$PREFIX/lib$" $ldsoconf || echo "$PREFIX/lib" >> $ldsoconf
684 ldconfig || errorExit "ldconfig failed"
685
686 echo "${CMSG}$prog is now installed.$CEND"
687}
688
689downloadAndBuild() {
690 local prog="$1"
691 local urls="$2"
692 local name="$3"
693 local configOptions="$4"
694 local patchUrl="$5"
695
696 cd "$BUILD_DIR"
697 echo "${CMSG}Downloading $prog source code...$CEND"
698 downloadFile "$name.tar.gz" $urls || errorExit "Could not download $prog source code."
699 tar xzf $name.tar.gz
700
701 if echo "$patchUrl" | grep '^[a-z]*://'; then
702 echo "${CMSG}Patching $prog...$CEND"
703 PACKAGES="patch"
704 installPackages
705 isProgramInstalled patch || errorExit "patch is not installed!"
706 downloadFile the.patch "$patchUrl" || errorExit "Could not download the patch file."
707 patch -p1 -d $name < the.patch || errorExit "Could not patch $prog."
708 elif [ -n "$patchUrl" ]; then
709 echo "${CMSG}Patching $prog...$CEND"
710 cd $name
711 eval $patchUrl
712 cd ..
713 fi
714
715 cd $name
716 buildProgram "$prog" "$configOptions"
717}
718
719checkoutAndBuild() {
720 local prog="$1"
721 local url="$2"
722 local configOptions="$3"
723 cd "$BUILD_DIR"
724 echo "${CMSG}Downloading $prog source code...$CEND"
725 svn export $SVN_OPTS "$url" "$prog" || errorExit "Could not download $prog source code."
726 cd "$prog"
727 buildProgram "$prog" "$configOptions"
728}
729
730buildStart() {
731 PREFIX=/usr/local
732 OLD_PATH="$PATH"
733 PATH="$PATH:$PREFIX/bin"
734 PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig
735 export PKG_CONFIG_PATH
736 BUILD_DIR="$(echo ~)/build-tmp"
737
738 rm -rf "$BUILD_DIR"
739 mkdir "$BUILD_DIR" || errorExit "Could not create build directory."
740}
741
742buildEnd() {
743 cd
744 rm -rf "$BUILD_DIR"
745
746 [ -n "$OLD_PATH" ] && PATH="$OLD_PATH"
747 unset OLD_PATH
748 unset PKG_CONFIG_PATH
749}
750
751buildRtorrent() {
752 installBuildTools
753 osHandler_$os installRtorrentBuildTools
754 isProgramInstalled pkg-config || errorExit "pkg-config is not installed."
755
756 # Minimum version is 7.15.4 but I had problems with 7.15.5 on CentOS. Set
757 # it to 7.18.0 (the version Ubuntu Server 8.04 is using).
758 if ! pkg-config --atleast-version=7.18.0 libcurl; then
759 downloadAndBuild "libcurl" "$LIBCURL_URL" "$LIBCURL_NAME" ""
760 # Prevent 'Unknown keyword 'URL' in ....' error
761 sed_i 's/^URL:/#URL:/' $PREFIX/lib/pkgconfig/libcurl.pc
762 fi
763 if ! pkg-config --exists sigc++-2.0; then
764 downloadAndBuild "sigc++-2.0" "$SIGCPP20_URL" "$SIGCPP20_NAME" ""
765 fi
766 checkoutAndBuild "xmlrpc-c" "$XMLRPC_SVN_DIR" "--disable-cplusplus"
767 downloadAndBuild "libtorrent" "$LIBTORRENT_URL $LIBTORRENT_URL2" "$LIBTORRENT_NAME" "" patchLibtorrent
768 downloadAndBuild "rtorrent" "$RTORRENT_URL $RTORRENT_URL2" "$RTORRENT_NAME" "--with-xmlrpc-c" ""
769}
770
771patchLibtorrent() {
772 # For gcc 4.6.0
773 ed -s "src/torrent/data/block_transfer.h" > /dev/null 2>&1 << EOF
774/#define LIBTORRENT_BLOCK_TRANSFER_H
775a
776#include <stddef.h>
777.
778w
779q
780EOF
781 ed -s "src/data/memory_chunk.cc" > /dev/null 2>&1 << EOF
782/#include "config.h"
783a
784#include <stddef.h>
785.
786w
787q
788EOF
789}
790
791installRtorrent() {
792 buildStart
793 buildRtorrent
794 buildEnd
795}
796
797installUnrar() {
798 isProgramInstalled unrar && return
799 installBuildTools
800 buildMakeProgram "$UNRAR_URL" "unrar" "" "unrar" "makefile.unix"
801}
802
803installModScgi() {
804 local modScgiFile="$1"
805 local modulesPath="$2"
806 [ -f "$modScgiFile" ] && errorExit "mod_scgi file already exists."
807 [ -d "$modulesPath" ] || errorExit "Apache modules path '$modulesPath' does not exist."
808
809 buildMakeProgram "$MOD_SCGI_URL" "mod_scgi" "apache2"
810 echo "LoadModule scgi_module $modulesPath/mod_scgi.so" > $modScgiFile
811}
812
813isPortUsed() {
814 local port="$1"
815 netstat -an | grep tcp | grep -w LISTEN | grep -qE "[.:]$port[ ]" && return 0
816 return 1
817}
818
819isValidIpAddress() {
820 # It's not 100% accurate ... ;)
821 echo $1 | grep -qE '^[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?$'
822}
823
824getIpAddress() {
825 isValidIpAddress "$OUR_IP_ADDRESS" && return
826 echo "${CMSG}Detecting your IP address...$CEND"
827 isValidIpAddress "$OUR_IP_ADDRESS" || OUR_IP_ADDRESS=$(wget --no-check-certificate http://www.whatismyip.com/automation/n09230945.asp -O - -o /dev/null)
828 isValidIpAddress "$OUR_IP_ADDRESS" || OUR_IP_ADDRESS=$(ifconfig -a | grep "inet addr" | head -n1 | awk -F: '{print $2}' | awk '{print $1}')
829 isValidIpAddress "$OUR_IP_ADDRESS" || OUR_IP_ADDRESS="1.2.3.4"
830}
831
832getNewPortNumber() {
833 while true; do
834 newPortNumber=$CURRENT_PORT
835 CURRENT_PORT=$(expr $CURRENT_PORT + 1)
836 isValidPortNumber $newPortNumber || errorExit "Invalid port number. Change CURRENT_PORT."
837 isPortUsed $newPortNumber || break
838 echo "${CWARNING}Port $newPortNumber is in use, trying next port...$CEND"
839 done
840}
841
842# Returns true if it's a valid ruTorrent base path
843isValidRutorrentBasePath() {
844 [ -n "$1" ] && [ -d "$1/conf" ]
845}
846
847isValidPassword() {
848 echo "$1" | grep -qE "^ " && return 1
849 echo "$1" | grep -qE " \$" && return 1
850 echo "$1" | grep -qE '^$' && return 1
851 echo "$1" | grep -q ':' && return 1
852 echo "'$1'" | grep -q ' ' && return 1
853 return 0
854}
855
856isValidPortNumber() {
857 echo "$1" | grep -qiE '^[0-9]+$' || return 1
858 echo "$1" | grep -qiE '[0-9][0-9][0-9][0-9][0-9][0-9]' && return 1
859 [ $1 -ge 1024 ] && [ $1 -le 65535 ]
860}
861
862isValidWebUser() {
863 [ -z "$1" ] && return 1
864 echo "$1" | LC_ALL=C grep -qE '[A-Z:]' && return 1
865 return 0
866}
867
868initUsers() {
869 local users=
870 for packedUser in $USERS; do
871 extractPackedUser $packedUser
872
873 if [ -z "$autodlPort" ]; then
874 getNewPortNumber
875 autodlPort=$newPortNumber
876 fi
877 if [ -z "$scgiPort" ]; then
878 getNewPortNumber
879 scgiPort=$newPortNumber
880 fi
881 if [ -z "$rtorrentPort" ]; then
882 getNewPortNumber
883 rtorrentPort=$newPortNumber
884 fi
885 if canGeneratePasswords; then
886 if [ -z "$autodlPassword" ]; then
887 generatePassword
888 autodlPassword="$newPassword"
889 fi
890 fi
891
892 users="$users $osUser:$autodlPassword:$webUser:$webPass:$autodlPort:$scgiPort:$rtorrentPort"
893 done
894 USERS="$users"
895}
896
897extractPackedUser() {
898 local packedUser="$1"
899
900 osUser="$(echo $packedUser | cut -d: -f1)"
901 autodlPassword="$(echo $packedUser | cut -d: -f2)"
902 webUser="$(echo $packedUser | cut -d: -f3)"
903 webPass="$(echo $packedUser | cut -d: -f4)"
904 autodlPort="$(echo $packedUser | cut -d: -f5)"
905 scgiPort="$(echo $packedUser | cut -d: -f6)"
906 rtorrentPort="$(echo $packedUser | cut -d: -f7)"
907}
908
909getUserGroup() {
910 local user="$1"
911
912 userExists "$user" || errorExit "The user '$user' does not exist."
913 group=$(grep -w $(id -rg $user) /etc/group | cut -d: -f1)
914 [ -z "$group" ] && group="$user"
915}
916
917getUserDir() {
918 local user="$1"
919
920 [ -z "$user" ] && errorExit "Invalid user (blank)."
921 userDir="$(eval echo ~$user)"
922 [ -d "$userDir" ] || errorExit "User $user's home directory does not exist."
923}
924
925resetOwner() {
926 local user="$1"
927 shift
928
929 if [ "$ISROOT" = y ]; then
930 getUserGroup "$user"
931 chown -R $user:$group "$@"
932 fi
933}
934
935# Updates MISSING_PHP_MODULES with all missing required PHP modules
936detectMissingPhpModules() {
937 MISSING_PHP_MODULES=
938 local php=${WWW_PHP_CGI:-php}
939 for module in $REQUIRED_PHP_MODULES; do
940 $php -m 2> /dev/null | grep -wq $module || MISSING_PHP_MODULES="$MISSING_PHP_MODULES $module"
941 done
942}
943
944installMissingPhpPackages() {
945 PACKAGES=
946 for module in $MISSING_PHP_MODULES; do
947 PACKAGES="$PACKAGES php-$module"
948 done
949 installPackages
950}
951
952installMissingPhp5Packages() {
953 PACKAGES=
954 for module in $MISSING_PHP_MODULES; do
955 PACKAGES="$PACKAGES php5-$module"
956 done
957 installPackages
958}
959
960enablePhpIniModules() {
961 local phpIni="$1"
962 shift
963 for module in "$@"; do
964 grep -qE "^extension=$module.so" "$phpIni" && continue
965 sed_i "s/^; *\\(extension=$module.so.*\\)/\\1/" "$phpIni"
966 grep -qE "^extension=$module.so" "$phpIni" && continue
967 echo "extension=$module.so" >> "$phpIni"
968 done
969}
970
971installMissingPhpModules() {
972 detectMissingPhpModules
973 [ -z "$MISSING_PHP_MODULES" ] && return
974
975 osHandler_$os installPhpModules
976
977 detectMissingPhpModules
978 [ -z "$MISSING_PHP_MODULES" ] && return
979 cat << EOF
980$CWARNING
981The following PHP modules appear to be missing:
982 $MISSING_PHP_MODULES
983If the autodl-irssi ruTorrent plugin isn't working, you now know why. Install
984them using your package manager or enable them in your php.ini file, and
985restart your web server.$CEND
986EOF
987}
988
989addLogrotateConfig() {
990 local serviceName="$1"
991 local logFiles="$2"
992 local postrotateScript="$3"
993
994 local logrotate_d=/etc/logrotate.d
995 [ -d "$logrotate_d" ] || errorExit "Missing logrotate dir: $logrotate_d"
996
997 cat > "$logrotate_d/$serviceName" << EOF
998$logFiles {
999 missingok
1000 daily
1001 rotate 14
1002 notifempty
1003 compress
1004 delaycompress
1005 sharedscripts
1006 postrotate
1007 $postrotateScript
1008 endscript
1009}
1010EOF
1011 [ $? -eq 0 ] || errorExit "Could not write logrotate file $logrotate_d/$serviceName"
1012}
1013
1014detectPhpCgi() {
1015 WWW_PHP_CGI=${WWW_PHP_CGI:-$(which php-cgi | head -n1)}
1016 [ -x "$WWW_PHP_CGI" ] || errorExit "Could not find php-cgi"
1017 "$WWW_PHP_CGI" -v | grep -q 'cgi-fcgi' || errorExit "$WWW_PHP_CGI does not support FastCGI"
1018}
1019
1020# $i is the i'th user number
1021getUserRpcMount() {
1022 local i=$1
1023 verifyWebServerVars
1024 # Make sure /RPC10 accesses aren't going to /RPC1 by padding with zeros. This is
1025 # easier than making sure every web server handles it correctly. Eg., lighttpd's
1026 # scgi.server table is read in order, so if you add /RPC1 before /RPC10, then
1027 # /RPC10 accesses will go to /RPC1. A fix would of course be to place /RPC10
1028 # before /RPC1 but it's easy to forget. Now we use /RPC00001 .. /RPC99999
1029 echo "/$RPC_PREFIX$(printf '%05d' $i)"
1030}
1031
1032createSelfSignedCertFile() {
1033 local pemfile="$1"
1034
1035 if [ ! -f "$OTHER_PEM_FILE" ]; then
1036 echo "${CMSG}Creating the self-signed certificate.$CEND"
1037 rm -f "$pemfile"
1038 openssl req -new -newkey rsa:1024 -days 1000 -nodes -x509 -keyout "$pemfile" -out "$pemfile" -batch \
1039 || errorExit "Failed to create self-signed certificate."
1040 OTHER_PEM_FILE="$pemfile"
1041 CREATED_CERT_FILE=y
1042 else
1043 rm -f "$pemfile"
1044 cp "$OTHER_PEM_FILE" "$pemfile" || errorExit "Failed to copy self-signed certificate."
1045 fi
1046
1047 chmod 0600 "$pemfile"
1048}
1049
1050detectHtpasswd() {
1051 [ -z "$htpasswd" ] && isProgramInstalled htpasswd && htpasswd=htpasswd
1052 [ -z "$htpasswd" ] && isProgramInstalled htpasswd2 && htpasswd=htpasswd2
1053 if [ -z "$htpasswd" ]; then
1054 installProgram python
1055 if isProgramInstalled python; then
1056 cd
1057 downloadFile "htpasswd.py" "$HTPASSWD_PY_SCRIPT_URL" "$HTPASSWD_PY_SCRIPT_URL2" \
1058 && htpasswd="python $(pwd)/htpasswd.py"
1059 fi
1060 fi
1061 [ -z "$htpasswd" ] && errorExit "Could not find htpasswd"
1062}
1063
1064verifyWebServerVars() {
1065 [ -z "$WWW_PEMFILE" ] && errorExit "You have not initialized WWW_PEMFILE"
1066 [ -z "$WWW_PASSWORD_FILE" ] && errorExit "You have not initialized WWW_PASSWORD_FILE"
1067 [ -z "$WWW_ROOT" ] && errorExit "You have not initialized WWW_ROOT"
1068 [ -z "$WWW_USER" ] && errorExit "You have not initialized WWW_USER"
1069 [ -z "$WWW_GROUP" ] && errorExit "You have not initialized WWW_GROUP"
1070}
1071
1072initializeWwwRootVar() {
1073 WWW_ROOT=${WWW_ROOT:-/var/rutorrent}
1074}
1075
1076createWebServerUserGroup() {
1077 verifyWebServerVars
1078 if ! grep -qE "^$WWW_GROUP:" /etc/group; then
1079 groupadd -r $WWW_GROUP 2>/dev/null || groupadd $WWW_GROUP || errorExit "Could not create web server group."
1080 fi
1081 if ! grep -qE "^$WWW_USER:" /etc/passwd; then
1082 local useradd="useradd -d $WWW_ROOT -g $WWW_GROUP -s /bin/sh"
1083 $useradd -M -r $WWW_USER 2>/dev/null || $useradd $WWW_USER || errorExit "Could not create web server user."
1084 fi
1085}
1086
1087resetAuthPasswordFilePermissions() {
1088 [ -n "$WWW_USER" ] || errorExit "WWW_USER not initialized"
1089 [ -n "$WWW_GROUP" ] || errorExit "WWW_GROUP not initialized"
1090 chmod 0400 "$WWW_PASSWORD_FILE"*
1091 chown $WWW_USER:$WWW_GROUP "$WWW_PASSWORD_FILE"*
1092}
1093
1094createAuthPasswordFile() {
1095 verifyWebServerVars
1096 : > "$WWW_PASSWORD_FILE"
1097 resetAuthPasswordFilePermissions
1098}
1099
1100# Some web servers (eg. nginx) can't allow just ONE user access to a certain
1101# location. To fix that, create one password file per user.
1102createOnePasswordFilePerUser() {
1103 for packedUser in $USERS; do
1104 extractPackedUser $packedUser
1105 grep "^$webUser:" "$WWW_PASSWORD_FILE" > "${WWW_PASSWORD_FILE}_$webUser"
1106 done
1107 resetAuthPasswordFilePermissions
1108}
1109
1110updatePhpTimezone() {
1111 if [ ! -f "$WWW_PHP_INI" ]; then
1112 echo "${CWARNING}Could not find php.ini file: $WWW_PHP_INI$CEND"
1113 return
1114 fi
1115 [ -z "$USER_TIMEZONE" ] && return
1116
1117 local zws="[ ]*"
1118 local newLine="date.timezone = $USER_TIMEZONE"
1119 if grep -qE "^${zws}date\\.timezone[ =]" "$WWW_PHP_INI"; then
1120 return
1121 elif grep -qE "^${zws}[;#]${zws}date\\.timezone[ =]" "$WWW_PHP_INI"; then
1122 sed_i "s!^${zws}[;#]${zws}date\\.timezone[ =].*\$!$newLine!" "$WWW_PHP_INI"
1123 else
1124 cat >> "$WWW_PHP_INI" << EOF
1125[Date]
1126$newLine
1127EOF
1128 fi
1129}
1130
1131doDetectTimeZone() {
1132 echo "${CMSG}Detecting timezone...$CEND"
1133 detectTimeZone
1134 if [ -n "$USER_TIMEZONE" ]; then
1135 echo "${CMSG}Timezone: $USER_TIMEZONE$CEND"
1136
1137 updatePhpTimezone
1138 else
1139 echo "${CWARNING}Could not detect timezone!$CEND"
1140 fi
1141}
1142
1143webServerCommonInitialization() {
1144 mkdir -p $WWW_ROOT
1145 doDetectTimeZone
1146 createSelfSignedCertFile "$WWW_PEMFILE"
1147 createAuthPasswordFile
1148}
1149
1150resetWebServerPermissions() {
1151 verifyWebServerVars
1152 chown -R $WWW_USER:$WWW_GROUP $WWW_ROOT || errorExit "Could not set web server as owner of $WWW_ROOT"
1153}
1154
1155createPhpcgiSocket() {
1156 PHPCGI_SOCKET_DIR=/etc/phpcgi
1157 PHPCGI_SOCKET_FILE="$PHPCGI_SOCKET_DIR/php-cgi.socket"
1158 mkdir -p "$PHPCGI_SOCKET_DIR"
1159 chown $WWW_USER:$WWW_GROUP "$PHPCGI_SOCKET_DIR"
1160 chmod 0770 "$PHPCGI_SOCKET_DIR"
1161}
1162
1163verifyApacheServerVars() {
1164 verifyWebServerVars
1165 [ -z "$APACHE_SITE_FILE" ] && errorExit "You have not initialized APACHE_SITE_FILE"
1166}
1167
1168apacheCreateApxs2Symlink() {
1169 # The makefile uses apxs2, so make a symlink
1170 if ! isProgramInstalled apxs2; then
1171 local apxs=$(which apxs 2>/dev/null) && ln -s "$apxs" "${apxs}2"
1172 fi
1173}
1174
1175initializeApacheVars() {
1176 local apacheUser="$1"
1177 local apacheGroup="$2"
1178 local apacheSitesDir="$3"
1179
1180 [ -n "$apacheUser" ] || errorExit "Invalid apache user: $apacheUser"
1181 [ -n "$apacheGroup" ] || errorExit "Invalid apache group: $apacheGroup"
1182 [ -d "$apacheSitesDir" ] || errorExit "Invalid apache sites dir: $apacheSitesDir"
1183 [ -d "$APACHE_DIR" ] || errorExit "Invalid apache dir: $APACHE_DIR"
1184
1185 APACHE_SITE_NAME=${APACHE_SITE_NAME:-rutorrent.conf}
1186 APACHE_SITE_FILE=$apacheSitesDir/$APACHE_SITE_NAME
1187 initializeWwwRootVar
1188 WWW_PASSWORD_FILE=$APACHE_DIR/rutorrent_passwd
1189 WWW_PEMFILE=$APACHE_DIR/rutorrent.pem
1190 WWW_USER="$apacheUser"
1191 WWW_GROUP="$apacheGroup"
1192}
1193
1194apacheGetRpcLocationDirectives() {
1195 if [ "$INSTALL_RUTORRENT" = y ]; then
1196 local i=1
1197 for packedUser in $USERS; do
1198 extractPackedUser $packedUser
1199 local rpcMount="$(getUserRpcMount $i)"
1200 cat << EOF
1201 <Location "$rpcMount">
1202 AuthType Basic
1203 AuthName "$RUTORRENT_SITE_REALM"
1204 AuthUserFile "$WWW_PASSWORD_FILE"
1205 Require user $webUser
1206 </Location>
1207EOF
1208 i=$(expr $i + 1)
1209 done
1210 fi
1211}
1212
1213apacheGetVirtualHost() {
1214 local port=$1
1215 local ssl=$2
1216 cat << EOF
1217<VirtualHost *:$port>
1218$(if [ "$ssl" = y ]; then
1219 echo " SSLEngine On"
1220 echo " SSLCertificateFile $WWW_PEMFILE"
1221fi)
1222 ServerAdmin admin@rutorrent
1223 ServerName localhost
1224 DocumentRoot $WWW_ROOT
1225
1226 <Directory />
1227 AllowOverride None
1228 Order deny,allow
1229 Deny from all
1230 </Directory>
1231 <Location />
1232 AuthType Basic
1233 AuthName "$RUTORRENT_SITE_REALM"
1234 AuthUserFile "$WWW_PASSWORD_FILE"
1235 Require valid-user
1236 Order allow,deny
1237 Allow from all
1238 </Location>
1239 <Location ~ "^/rutorrent/(conf|share)">
1240 Order deny,allow
1241 Deny from all
1242 </Location>
1243 <Location ~ "/\\\\.svn">
1244 Order deny,allow
1245 Deny from all
1246 </Location>
1247$(apacheGetRpcLocationDirectives)
1248</VirtualHost>
1249EOF
1250}
1251
1252apacheWriteSiteFile() {
1253 verifyApacheServerVars
1254 cat > "$APACHE_SITE_FILE" << EOF
1255ServerName localhost
1256
1257$(apacheGetVirtualHost $HTTP_PORT n)
1258$(apacheGetVirtualHost $HTTPS_PORT y)
1259EOF
1260
1261 if [ "$INSTALL_RUTORRENT" = y ]; then
1262 local i=1
1263 local lp=; isWebServerRpcModule || lp='#'
1264 for packedUser in $USERS; do
1265 extractPackedUser $packedUser
1266 local rpcMount="$(getUserRpcMount $i)"
1267 echo "${lp}SCGIMount $rpcMount $SCGI_HOST:$scgiPort" >> "$APACHE_SITE_FILE"
1268
1269 i=$(expr $i + 1)
1270 done
1271 fi
1272}
1273
1274apacheCommonInitialization() {
1275 local buildTools="$1"
1276 local modulesDir="$2"
1277
1278 webServerCommonInitialization
1279 apacheWriteSiteFile
1280
1281 if [ -n "$APACHE_SCGI_FILE" ] && [ ! -f "$APACHE_SCGI_FILE" ]; then
1282 installBuildTools
1283 PACKAGES="$buildTools"
1284 installPackages
1285 apacheCreateApxs2Symlink
1286
1287 # Compiling mod_scgi will fail unless we remove this warning-as-error flag
1288 local configVarsFile="$APACHE_DIR/modules/build/config_vars.mk"
1289 [ -f "$configVarsFile" ] && sed_i 's/-Werror=format-security//g' "$configVarsFile"
1290
1291 installModScgi "$APACHE_SCGI_FILE" "$modulesDir"
1292 fi
1293}
1294
1295installCommonWebServerStuff_mandriva() {
1296 # Also add some extra tools for ruTorrent and some of its plugins
1297 PACKAGES="php-cli gzip curl openssl logrotate"
1298 installPackages
1299 PACKAGE="unrar"
1300 installPackages
1301 PACKAGES="php-geoip"
1302 installPackages
1303}
1304
1305# Used by Mandriva, Unity Linux, and PCLinuxOS
1306installApache_mandriva() {
1307 installCommonWebServerStuff_mandriva
1308 PACKAGES="apache-mpm-prefork apache-mod_ssl apache-mod_php"
1309 installPackages
1310
1311 WWW_PHP_INI=/etc/php.ini
1312 APACHE_DIR=/etc/httpd
1313 local APACHE_CONF_D=$APACHE_DIR/conf.d
1314 local APACHE_SITES_DIR=$APACHE_DIR/conf/vhosts.d
1315 APACHE_SCGI_FILE=$APACHE_CONF_D/scgi.conf
1316 initializeApacheVars apache apache "$APACHE_SITES_DIR"
1317 apacheCommonInitialization "libopenssl-devel apache-devel" $APACHE_DIR/modules
1318
1319 for file in 00_default_vhosts.conf 01_default_ssl_vhost.conf; do
1320 [ -f "$APACHE_SITES_DIR/$file" ] && mv -f "$APACHE_SITES_DIR/$file" "$APACHE_SITES_DIR/$file-bak"
1321 done
1322
1323 resetWebServerPermissions
1324 chkconfig httpd on
1325 service httpd restart || errorExit "Could not start $INSTALL_WEB_SERVER"
1326}
1327
1328installCommonWebServerStuff_gentoo() {
1329 # Also add some extra tools for ruTorrent and some of its plugins
1330 PACKAGES="dev-lang/php logrotate"
1331 addProgram gzip app-arch/gzip
1332 addProgram curl net-misc/curl
1333 addProgram openssl dev-libs/openssl
1334 addProgram unrar app-arch/unrar
1335 installPackages
1336
1337 ACCEPT_LICENSE=MaxMind
1338 export ACCEPT_LICENSE
1339 PACKAGES="dev-php5/pecl-geoip"
1340 installPackages
1341 unset ACCEPT_LICENSE
1342}
1343
1344installApache_gentoo() {
1345 installCommonWebServerStuff_gentoo
1346 PACKAGES="www-servers/apache app-admin/apache-tools"
1347 installPackages
1348 PACKAGES="www-apache/mod_scgi"
1349 installPackages
1350
1351 WWW_PHP_INI=$(ls /etc/php/apache2-php5*/php.ini | tail -n1)
1352 [ -f "$WWW_PHP_INI" ] || errorExit "Could not find Apache php.ini file"
1353 APACHE_DIR=/etc/apache2
1354 local APACHE_CONF=$APACHE_DIR/httpd.conf
1355 local APACHE_SITES_DIR=$APACHE_DIR/vhosts.d
1356 local SERVER_ROOT=$(grep -E ^ServerRoot $APACHE_CONF | head -n1 | sed -e 's!^.*[ ][^/]*\(/[^"]*\).*!\1!')
1357 APACHE_SCGI_FILE="$APACHE_DIR/modules.d/scgi.conf"
1358 [ -d "$SERVER_ROOT" ] || errorExit "Apache server root dir does not exist: $SERVER_ROOT"
1359 initializeApacheVars apache apache "$APACHE_SITES_DIR"
1360 apacheCommonInitialization "" $SERVER_ROOT/modules
1361
1362 for file in 00_default_ssl_vhost.conf 00_default_vhost.conf; do
1363 [ -f "$APACHE_SITES_DIR/$file" ] && mv -f "$APACHE_SITES_DIR/$file" "$APACHE_SITES_DIR/$file-bak"
1364 done
1365
1366 appendFileStringVar /etc/conf.d/apache2 APACHE2_OPTS "-D SCGI"
1367 appendFileStringVar /etc/conf.d/apache2 APACHE2_OPTS "-D PHP5"
1368 appendFileStringVar /etc/conf.d/apache2 APACHE2_OPTS "-D SSL"
1369 cat >> $APACHE_SITE_FILE << EOF
1370Listen $HTTP_PORT
1371Listen $HTTPS_PORT
1372EOF
1373
1374 resetWebServerPermissions
1375 rc-update add apache2 default
1376 osHandler_$os restart_$INSTALL_WEB_SERVER
1377}
1378
1379nginxGetRpcMounts() {
1380 if [ "$INSTALL_RUTORRENT" = y ]; then
1381 local i=1
1382 local lp=; isWebServerRpcModule || lp='#'
1383 for packedUser in $USERS; do
1384 extractPackedUser $packedUser
1385 getUserScgiSocketPath "$osUser"
1386 local rpcMount="$(getUserRpcMount $i)"
1387
1388 cat << EOF
1389$lp location ~ ^$rpcMount\$ {
1390$lp include scgi_params;
1391$lp scgi_pass unix:$scgiSocketPath;
1392$lp auth_basic "$RUTORRENT_SITE_REALM";
1393$lp auth_basic_user_file "${WWW_PASSWORD_FILE}_$webUser";
1394$lp }
1395EOF
1396
1397 i=$(expr $i + 1)
1398 done
1399 fi
1400}
1401
1402nginxGetServerSection() {
1403 local port=$1
1404 local ssl=$2
1405
1406 cat << EOF
1407 server {
1408 listen $port;
1409 server_name localhost;
1410 auth_basic "$RUTORRENT_SITE_REALM";
1411 auth_basic_user_file "$WWW_PASSWORD_FILE";
1412
1413$(if [ "$ssl" = y ]; then
1414 cat << EOF2
1415 ssl on;
1416 ssl_certificate $WWW_PEMFILE;
1417 ssl_certificate_key $WWW_PEMFILE;
1418EOF2
1419fi)
1420
1421 location ~ ^/rutorrent/(?:share|conf) {
1422 deny all;
1423 }
1424
1425 location ~ /\.ht {
1426 deny all;
1427 }
1428
1429 location ~ /\.svn {
1430 deny all;
1431 }
1432
1433 location / {
1434 root $WWW_ROOT;
1435 index index.php index.html index.htm;
1436 }
1437
1438 location ~ \.php$ {
1439 root "$WWW_ROOT";
1440 fastcgi_pass unix:$PHPCGI_SOCKET_FILE;
1441 fastcgi_index index.php;
1442 fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
1443 include fastcgi_params;
1444 }
1445
1446$(nginxGetRpcMounts)
1447 }
1448EOF
1449}
1450
1451nginxWriteConfFile() {
1452 getNumCpus
1453 cat > $NGINX_CONF << EOF
1454worker_processes $NUMCPUS;
1455user $WWW_USER $WWW_GROUP;
1456events {
1457 worker_connections 1024;
1458}
1459
1460http {
1461 include mime.types;
1462 default_type application/octet-stream;
1463 sendfile on;
1464 keepalive_timeout 65;
1465
1466 # I have to set min length to 0 and http version to 1.0 or it won't compress
1467 # the XML-RPC (SCGI) responses. Those responses can be quite large if you're
1468 # using many torrent files.
1469 gzip on;
1470 gzip_min_length 0;
1471 gzip_http_version 1.0;
1472 gzip_types text/plain text/xml application/xml application/json text/css application/x-javascript text/javascript application/javascript;
1473
1474$(nginxGetServerSection $HTTP_PORT n)
1475$(nginxGetServerSection $HTTPS_PORT y)
1476}
1477EOF
1478 [ $? -eq 0 ] || errorExit "Could not write to file $NGINX_CONF"
1479}
1480
1481buildNginx() {
1482 buildStart
1483 PREFIX=$NGINX_DIR
1484 NGINX_BIN=$PREFIX/sbin/nginx
1485 NGINX_CONF=$PREFIX/conf/nginx.conf
1486 NGINX_PID_FILE=$PREFIX/nginx.pid
1487 NGINX_ACCESS_LOG=$PREFIX/logs/access.log
1488 NGINX_ERROR_LOG=$PREFIX/logs/error.log
1489 local configureFlags="\
1490 --prefix=$PREFIX \
1491 --sbin-path=$NGINX_BIN \
1492 --conf-path=$NGINX_CONF \
1493 --pid-path=$NGINX_PID_FILE \
1494 --lock-path=$PREFIX/logs/nginx.lock \
1495 --error-log-path=$NGINX_ERROR_LOG \
1496 --http-log-path=$NGINX_ACCESS_LOG \
1497 --user=$WWW_USER \
1498 --group=$WWW_GROUP \
1499 --with-http_ssl_module"
1500 export CFLAGS="$NGINX_CFLAGS"
1501 downloadAndBuild "nginx" "$NGINX_URL" "$NGINX_NAME" "$configureFlags" patchNginx
1502 unset CFLAGS
1503 buildEnd
1504}
1505
1506patchNginx() {
1507 # Get rid of the -Werror flag for gcc 4.6.0
1508 sed_i "s!-Werror[a-zA-Z0-9=-]*!!" "auto/cc/gcc"
1509}
1510
1511installNginx() {
1512 SCGI_USE_UNIX_DOMAIN_SOCKET=y
1513 CREATE_ONE_PASSWORD_FILE_PER_USER=y
1514 installBuildTools
1515 osHandler_$os preNginxInstall
1516 installUnrar
1517
1518 NGINX_DIR=/usr/local/nginx
1519 initializeWwwRootVar
1520 WWW_PASSWORD_FILE=$NGINX_DIR/rutorrent_passwd
1521 WWW_PEMFILE=$NGINX_DIR/rutorrent.pem
1522 WWW_USER=${WWW_USER:-nginx}
1523 WWW_GROUP=${WWW_GROUP:-nginx}
1524
1525 detectPhpCgi
1526 createWebServerUserGroup
1527 createPhpcgiSocket
1528
1529 buildNginx
1530 webServerCommonInitialization
1531 nginxWriteConfFile
1532
1533 addLogrotateConfig nginx \
1534 "$NGINX_ACCESS_LOG $NGINX_ERROR_LOG" \
1535 "[ -f \"$NGINX_PID_FILE\" ] && kill -USR1 \$(cat \"$NGINX_PID_FILE\") >/dev/null 2>&1; true"
1536
1537 resetWebServerPermissions
1538 osHandler_$os installPhpCgiService
1539 osHandler_$os installNginxService
1540}
1541
1542preNginxInstall_gentoo() {
1543 installCommonWebServerStuff_gentoo
1544 PACKAGES="libpcre openssl zlib php"
1545 installPackages
1546 WWW_PHP_INI=$(ls /etc/php/cgi-php5*/php.ini | tail -n1)
1547 [ -f "$WWW_PHP_INI" ] || errorExit "Could not find php-cgi php.ini file"
1548}
1549
1550preLighttpdInstall_gentoo() {
1551 preNginxInstall_gentoo
1552}
1553
1554lighttpdGetRpcRequire() {
1555 if [ "$INSTALL_RUTORRENT" = y ]; then
1556 local i=1
1557 for packedUser in $USERS; do
1558 extractPackedUser $packedUser
1559 local rpcMount="$(getUserRpcMount $i)"
1560
1561 cat << EOF
1562 "$rpcMount" => (
1563 "method" => "basic",
1564 "realm" => "$RUTORRENT_SITE_REALM",
1565 "require" => "user=$webUser",
1566 ),
1567EOF
1568 i=$(expr $i + 1)
1569 done
1570 fi
1571}
1572
1573lighttpdGetScgiServerTable() {
1574 echo 'scgi.server = ('
1575
1576 if [ "$INSTALL_RUTORRENT" = y ]; then
1577 local i=1
1578 local lp=; isWebServerRpcModule || lp='#'
1579 for packedUser in $USERS; do
1580 extractPackedUser $packedUser
1581 getUserScgiSocketPath "$osUser"
1582 local rpcMount="$(getUserRpcMount $i)"
1583
1584 cat << EOF
1585$lp "$rpcMount" => (
1586$lp (
1587$lp "socket" => "$scgiSocketPath",
1588$lp "check-local" => "disable",
1589$lp "disable-time" => 0,
1590$lp ),
1591$lp ),
1592EOF
1593 i=$(expr $i + 1)
1594 done
1595 fi
1596
1597 echo ')'
1598}
1599
1600lighttpdWriteConfFile() {
1601 cat > $LIGHTTPD_CONF << EOF
1602server.follow-symlink = "disable"
1603server.max-connections = 512
1604server.max-fds = 1024
1605server.max-keep-alive-idle = 15
1606dir-listing.activate = "disable"
1607server.pid-file = "$LIGHTTPD_PID_FILE"
1608server.errorlog = "$LIGHTTPD_ERROR_LOG"
1609accesslog.filename = "$LIGHTTPD_ACCESS_LOG"
1610
1611$(if [ $(uname -s) = Linux ]; then
1612 echo 'server.event-handler = "linux-sysepoll"'
1613 echo 'server.network-backend = "linux-sendfile"'
1614fi)
1615
1616server.username = "$WWW_USER"
1617server.groupname = "$WWW_GROUP"
1618
1619server.modules = (
1620# "mod_rewrite",
1621# "mod_redirect",
1622# "mod_alias",
1623 "mod_access",
1624 "mod_auth",
1625# "mod_status",
1626# "mod_simple_vhost",
1627# "mod_evhost",
1628# "mod_userdir",
1629# "mod_secdownload",
1630 "mod_fastcgi",
1631# "mod_proxy",
1632# "mod_cgi",
1633 "mod_scgi",
1634# "mod_ssi",
1635# "mod_compress",
1636# "mod_usertrack",
1637# "mod_expire",
1638# "mod_rrdtool",
1639 "mod_accesslog",
1640)
1641
1642fastcgi.server = (
1643 ".php" => (
1644 (
1645 "socket" => "$PHPCGI_SOCKET_FILE",
1646 ),
1647 ),
1648)
1649
1650index-file.names = ( "index.php", "index.html", "index.htm" )
1651static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" )
1652
1653mimetype.assign = (
1654 ".html" => "text/html",
1655 ".htm" => "text/html",
1656 ".txt" => "text/plain",
1657 ".csv" => "text/plain",
1658 ".ini" => "text/plain",
1659 ".jpg" => "image/jpeg",
1660 ".jpeg" => "image/jpeg",
1661 ".gif" => "image/gif",
1662 ".png" => "image/png",
1663 ".bmp" => "image/bmp",
1664 ".css" => "text/css",
1665 ".js" => "application/javascript",
1666 ".ico" => "image/x-icon",
1667 ".json" => "application/json",
1668 ".torrent" => "application/x-bittorrent",
1669 ".pdf" => "application/pdf",
1670 ".bz2" => "application/x-bzip2",
1671 ".gz" => "application/x-gzip",
1672 ".tar.gz" => "application/x-tgz",
1673 ".tar" => "application/x-tar",
1674 ".tar.bz2" => "application/x-bzip-compressed-tar",
1675 ".tbz" => "application/x-bzip-compressed-tar",
1676 ".tgz" => "application/x-tgz",
1677 ".zip" => "application/zip",
1678 ".mp3" => "audio/mpeg",
1679 ".flac" => "audio/x-flac",
1680 ".m3u" => "audio/x-mpegurl",
1681 ".wma" => "audio/x-ms-wma",
1682 ".wax" => "audio/x-ms-wax",
1683 ".ogg" => "audio/ogg",
1684 ".wav" => "audio/x-wav",
1685 ".dtd" => "text/xml",
1686 ".xml" => "text/xml",
1687 ".mpeg" => "video/mpeg",
1688 ".mpg" => "video/mpeg",
1689 ".mp4" => "video/mp4",
1690 ".wmv" => "video/x-ms-wmv",
1691 ".avi" => "video/x-msvideo",
1692)
1693
1694server.document-root = "$WWW_ROOT"
1695server.port = $HTTP_PORT
1696
1697\$SERVER["socket"] == ":$HTTPS_PORT" {
1698 ssl.engine = "enable"
1699 ssl.pemfile = "$WWW_PEMFILE"
1700}
1701
1702url.access-deny = (".htaccess")
1703\$HTTP["url"] =~ "^/rutorrent/(?:share|conf)" {
1704 url.access-deny = ("")
1705}
1706\$HTTP["url"] =~ "/\\.svn" {
1707 url.access-deny = ("")
1708}
1709
1710auth.backend = "htpasswd"
1711auth.backend.htpasswd.userfile = "$WWW_PASSWORD_FILE"
1712auth.require = (
1713$(lighttpdGetRpcRequire)
1714 "/" => (
1715 "method" => "basic",
1716 "realm" => "$RUTORRENT_SITE_REALM",
1717 "require" => "valid-user",
1718 ),
1719)
1720
1721$(lighttpdGetScgiServerTable)
1722EOF
1723 [ $? -eq 0 ] || errorExit "Could not write to file $LIGHTTPD_CONF"
1724}
1725
1726buildLighttpd() {
1727 buildStart
1728 PREFIX=$LIGHTTPD_DIR
1729 LIGHTTPD_BIN=$PREFIX/sbin/lighttpd
1730 LIGHTTPD_CONF=$PREFIX/lighttpd.conf
1731
1732 # The log dir will be set to owner lighttpd since it's started as non-root.
1733 # Put the pid file there so it can write to it.
1734 LIGHTTPD_LOG_DIR=$PREFIX/logs
1735 LIGHTTPD_PID_FILE=$LIGHTTPD_LOG_DIR/lighttpd.pid
1736 LIGHTTPD_ERROR_LOG=$LIGHTTPD_LOG_DIR/error.log
1737 LIGHTTPD_ACCESS_LOG=$LIGHTTPD_LOG_DIR/access.log
1738
1739 local configureFlags="\
1740 --prefix=$PREFIX \
1741 --with-openssl \
1742 --with-pcre \
1743 --with-zlib \
1744 --without-bzip2"
1745 downloadAndBuild "lighttpd" "$LIGHTTPD_URL" "$LIGHTTPD_NAME" "$configureFlags"
1746 buildEnd
1747}
1748
1749installLighttpd() {
1750 SCGI_USE_UNIX_DOMAIN_SOCKET=y
1751 installBuildTools
1752 osHandler_$os preLighttpdInstall
1753 installUnrar
1754
1755 LIGHTTPD_DIR=/usr/local/lighttpd
1756 initializeWwwRootVar
1757 WWW_PASSWORD_FILE=$LIGHTTPD_DIR/rutorrent_passwd
1758 WWW_PEMFILE=$LIGHTTPD_DIR/rutorrent.pem
1759 WWW_USER=${WWW_USER:-lighttpd}
1760 WWW_GROUP=${WWW_GROUP:-lighttpd}
1761
1762 detectPhpCgi
1763 createWebServerUserGroup
1764 createPhpcgiSocket
1765
1766 buildLighttpd
1767 webServerCommonInitialization
1768 lighttpdWriteConfFile
1769
1770 addLogrotateConfig lighttpd \
1771 "$LIGHTTPD_ACCESS_LOG $LIGHTTPD_ERROR_LOG" \
1772 "[ -f \"$LIGHTTPD_PID_FILE\" ] && kill -HUP \$(cat \"$LIGHTTPD_PID_FILE\") >/dev/null 2>&1; true"
1773
1774 mkdir -p "$LIGHTTPD_LOG_DIR"
1775 chown -R $WWW_USER:$WWW_GROUP "$LIGHTTPD_LOG_DIR"
1776
1777 resetWebServerPermissions
1778 osHandler_$os installPhpCgiService
1779 osHandler_$os installLighttpdService
1780}
1781
1782# Add $name=$value to the file, or modify an existing line if present in the file
1783setVsftpdValue() {
1784 local confFile="$1"
1785 local name="$2"
1786 local value="$3"
1787 local newLine="$name=$value"
1788
1789 sed_i "s!^[ ]*$name[ =:].*\$!$newLine!" "$confFile"
1790 grep -qE "^$newLine$" "$confFile" || echo "$newLine" >> "$confFile"
1791}
1792
1793# Same as setVsftpdValue but makes sure the option exists
1794setVsftpdValue2() {
1795 local confFile="$1"
1796 local name="$2"
1797 local value="$3"
1798
1799 [ -z "$VSFTPD_PATH" ] && return
1800 grep -q "$name" "$VSFTPD_PATH" && setVsftpdValue "$confFile" "$name" "$value"
1801}
1802
1803updateVsftpdConf() {
1804 local confFile="$1"
1805 touch "$confFile"
1806
1807 VSFTPD_PATH=$(which vsftpd 2>/dev/null)
1808
1809 setVsftpdValue "$confFile" anonymous_enable NO
1810 setVsftpdValue "$confFile" dirlist_enable YES
1811 setVsftpdValue "$confFile" download_enable YES
1812 setVsftpdValue "$confFile" guest_enable NO
1813 setVsftpdValue "$confFile" listen YES
1814 setVsftpdValue "$confFile" listen_ipv6 NO
1815 setVsftpdValue "$confFile" local_enable YES
1816 setVsftpdValue "$confFile" local_umask $DEFAULT_UMASK
1817 setVsftpdValue "$confFile" max_per_ip 0
1818 setVsftpdValue "$confFile" pasv_enable YES
1819 setVsftpdValue "$confFile" port_enable YES
1820 setVsftpdValue "$confFile" pasv_promiscuous NO
1821 setVsftpdValue "$confFile" port_promiscuous NO
1822 setVsftpdValue "$confFile" pasv_min_port 0
1823 setVsftpdValue "$confFile" pasv_max_port 0
1824 setVsftpdValue "$confFile" write_enable YES
1825
1826 if [ "$USE_ENCRYPTED_FTP" = y ]; then
1827 local pemfile=/etc/vsftpd.pem
1828 createSelfSignedCertFile "$pemfile"
1829
1830 FTP_SERVER_TYPE="FTPES - Encrypted (FTP over explicit TLS/SSL)"
1831 FTP_PORT=${FTP_PORT:-$DEFAULT_PORT_FTPES}
1832 setVsftpdValue "$confFile" listen_port $FTP_PORT
1833 setVsftpdValue "$confFile" ssl_enable YES
1834
1835 setVsftpdValue "$confFile" allow_anon_ssl NO
1836 setVsftpdValue "$confFile" force_local_data_ssl YES
1837 setVsftpdValue "$confFile" force_local_logins_ssl YES
1838 setVsftpdValue "$confFile" ssl_sslv2 NO
1839 setVsftpdValue "$confFile" ssl_sslv3 NO
1840 setVsftpdValue "$confFile" ssl_tlsv1 YES
1841 setVsftpdValue "$confFile" rsa_cert_file "$pemfile"
1842
1843 setVsftpdValue2 "$confFile" implicit_ssl NO
1844 setVsftpdValue2 "$confFile" require_cert NO
1845 setVsftpdValue2 "$confFile" require_ssl_reuse NO
1846 setVsftpdValue2 "$confFile" ssl_request_cert YES
1847 setVsftpdValue2 "$confFile" strict_ssl_read_eof NO
1848 setVsftpdValue2 "$confFile" strict_ssl_write_shutdown NO
1849 setVsftpdValue2 "$confFile" validate_cert NO
1850 else
1851 FTP_SERVER_TYPE="FTP - Non-encrypted"
1852 FTP_PORT=${FTP_PORT:-$DEFAULT_PORT_FTP}
1853 setVsftpdValue "$confFile" listen_port $FTP_PORT
1854 setVsftpdValue "$confFile" ssl_enable NO
1855 fi
1856
1857 # The private key is in rsa_cert_file
1858 sed_i 'g/rsa_private_key_file/d' "$confFile"
1859}
1860
1861installVsftpd_chkconfig() {
1862 local vsftpdConf="$1"
1863 installProgram vsftpd
1864 [ -f "$vsftpdConf" ] || errorExit "Invalid vsftpd.conf file: $vsftpdConf"
1865 updateVsftpdConf "$vsftpdConf"
1866 chkconfig vsftpd on
1867 if ! service vsftpd restart; then
1868 if [ "$SELINUX_ENABLED" = y ]; then
1869 errorExit "Could not restart vsftpd. SELinux could be the reason."
1870 else
1871 errorExit "Could not restart vsftpd"
1872 fi
1873 fi
1874}
1875
1876installVsftpd_gentoo() {
1877 installProgram net-ftp/vsftpd
1878 local vsftpdConf=/etc/vsftpd/vsftpd.conf
1879 if ! [ -f $vsftpdConf ]; then
1880 touch $vsftpdConf
1881 [ -f $vsftpdConf.example ] && cp $vsftpdConf.example $vsftpdConf
1882 fi
1883 updateVsftpdConf $vsftpdConf
1884 rc-update add vsftpd default
1885 /etc/init.d/vsftpd restart
1886}
1887
1888installService_chkconfig1() {
1889 CHKCONFIG_SERVICE_NAME=$1
1890 CHKCONFIG_SERVICE_FILE=/etc/init.d/$CHKCONFIG_SERVICE_NAME
1891 CHKCONFIG_LOCK_FILE=/var/lock/subsys/$CHKCONFIG_SERVICE_NAME
1892}
1893
1894installService_chkconfig2() {
1895 chmod +x "$CHKCONFIG_SERVICE_FILE" || errorExit "Could not set +x bit, file $CHKCONFIG_SERVICE_FILE."
1896 chkconfig --add $CHKCONFIG_SERVICE_NAME || errorExit "Could not install service $CHKCONFIG_SERVICE_FILE. Run this script as root."
1897 service $CHKCONFIG_SERVICE_NAME restart
1898}
1899
1900getChkconfigScriptContents() {
1901 local desc="$1"
1902 cat << EOF
1903# chkconfig: 2345 85 15
1904# description: $desc
1905EOF
1906}
1907
1908getLsbStartupScriptContents() {
1909 local startupName="$1"
1910 local desc="$2"
1911 cat << EOF
1912### BEGIN INIT INFO
1913# Provides: $startupName
1914# Required-Start: \$local_fs \$network \$syslog
1915# Required-Stop: \$local_fs \$syslog
1916# Default-Start: $LSB_DEFAULT_START
1917# Default-Stop: $LSB_DEFAULT_STOP
1918# Short-Description: $desc
1919# Description: $desc
1920### END INIT INFO
1921EOF
1922}
1923
1924# You must define v_{startIt,stopIt,restartIt,showStatus} and isStarted funcs
1925getLsbStartupFooter() {
1926 cat << EOF
1927resetPath # Some include files may have reset it
1928LOCKFILE=$1
1929updateLock() {
1930 [ -z "\$LOCKFILE" ] && return
1931 if isStarted; then
1932 touch \$LOCKFILE
1933 else
1934 rm -f \$LOCKFILE
1935 fi
1936}
1937
1938RETVAL=0
1939case \$1 in
1940 start)
1941 v_startIt
1942 ;;
1943 stop)
1944 v_stopIt
1945 ;;
1946 force-reload|restart)
1947 v_restartIt
1948 ;;
1949 try-restart)
1950 isStarted && v_restartIt
1951 ;;
1952 status)
1953 v_showStatus
1954 ;;
1955 *)
1956 echo "Usage: \$0 {start|stop|restart|try-restart|force-reload|status}"
1957 RETVAL=1
1958 ;;
1959esac
1960
1961updateLock
1962exit \$RETVAL
1963EOF
1964}
1965
1966getCommonStartupScriptContents_autodl() {
1967 local USER="$1"
1968 local STARTUPNAME="$2"
1969 cat << EOF
1970NAME=$STARTUPNAME
1971USER=$USER
1972SESSIONNAME=autodl
1973
1974resetPath() {
1975 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:\$PATH"
1976}
1977resetPath
1978
1979# Make sure the path is correct, and make sure we're in the home dir.
1980USER_INIT="umask $DEFAULT_UMASK; cd; PATH=\\\$PATH:\$PATH"
1981
1982# Run user command, ignoring any messages sent to stdout (eg. 'No mail.')
1983runUserCmd() {
1984 su - \$USER -c "\$USER_INIT; \$1" >/dev/null && return 0
1985 return 1
1986}
1987
1988isStarted() {
1989 su - \$USER -c "\$USER_INIT; screen -ls | grep -qE \\"[ ][0-9]+\\\\.\$SESSIONNAME[ ]\\"" >/dev/null && return 0
1990 return 1
1991}
1992
1993startIt() {
1994 isStarted && return 0
1995
1996 local START_IRSSI=n
1997 local START_RTORRENT=n
1998 which irssi > /dev/null 2>&1 && START_IRSSI=y
1999 which rtorrent > /dev/null 2>&1 && START_RTORRENT=y
2000
2001 case \$START_IRSSI\$START_RTORRENT in
2002 yy)
2003 runUserCmd "screen -S \$SESSIONNAME -d -t rtorrent -m rtorrent"
2004 runUserCmd "screen -d -r \$SESSIONNAME -X screen -t irssi irssi"
2005 ;;
2006 yn)
2007 runUserCmd "screen -S \$SESSIONNAME -d -t irssi -m irssi"
2008 ;;
2009 ny)
2010 runUserCmd "screen -S \$SESSIONNAME -d -t rtorrent -m rtorrent"
2011 ;;
2012 nn)
2013 ;;
2014 esac
2015 return 0
2016}
2017
2018stopIt() {
2019 isStarted || return 0
2020
2021 runUserCmd "screen -d -r \$SESSIONNAME -p irssi -X stuff \\"/quit
2022/quit
2023\\""
2024 runUserCmd "screen -d -r \$SESSIONNAME -p rtorrent -X xon"
2025
2026 for i in 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4; do
2027 isStarted || break
2028 sleep 1
2029 done
2030
2031 # If it's still not stopped, kill the whole screen session
2032 if isStarted; then
2033 echo -n "Couldn't stop it. Killing screen session..."
2034 runUserCmd "screen -d -r \$SESSIONNAME -p rtorrent -X xon"
2035 sleep 2
2036 runUserCmd "screen -d -r \$SESSIONNAME -X quit"
2037 echo "Done."
2038 fi
2039
2040 return 0
2041}
2042
2043v_restartIt() {
2044 v_stopIt
2045 v_startIt
2046}
2047
2048v_showStatus() {
2049 if isStarted; then
2050 echo "\$NAME is running."
2051 else
2052 echo "\$NAME is stopped."
2053 fi
2054}
2055EOF
2056}
2057
2058getChkconfigScriptContents_autodl() {
2059 getChkconfigScriptContents "$AUTODL_STARTUP_DESC"
2060}
2061
2062# LSB style script, also supports chkconfig
2063# $1 = user name
2064# $2 = service name
2065getLsbStartupHeader_autodl() {
2066 local USER="$1"
2067 local STARTUPNAME="$2"
2068 cat << EOF
2069$(getChkconfigScriptContents_autodl)
2070$(getLsbStartupScriptContents "$STARTUPNAME" "$AUTODL_STARTUP_DESC")
2071
2072$(getCommonStartupScriptContents_autodl "$USER" "$STARTUPNAME")
2073EOF
2074}
2075
2076installService_mandriva() {
2077 local serviceName="$1"
2078 local headerCommand="$2"
2079 installService_chkconfig1 $serviceName
2080 cat > "$CHKCONFIG_SERVICE_FILE" << EOF
2081#!/bin/bash
2082$(eval $headerCommand)
2083
2084. /etc/rc.d/init.d/functions
2085
2086v_startIt() {
2087 gprintf "Starting %s:" "\$NAME"
2088 startIt && success "startup" || failure "startup"
2089 echo
2090}
2091
2092v_stopIt() {
2093 gprintf "Stopping %s:" "\$NAME"
2094 stopIt && success "stop" || failure "stop"
2095 echo
2096}
2097
2098$(getLsbStartupFooter "$CHKCONFIG_LOCK_FILE")
2099EOF
2100 [ $? -eq 0 ] || errorExit "Could not write to file $CHKCONFIG_SERVICE_FILE. Run this script as root."
2101 installService_chkconfig2
2102}
2103
2104installService_mandriva_autodl() {
2105 local user=$1
2106 installService_mandriva autodl_$user "getLsbStartupHeader_autodl \"$user\" \"\$CHKCONFIG_SERVICE_NAME\""
2107}
2108
2109installService_mandriva_phpcgi() {
2110 installService_mandriva phpcgi getLsbStartupHeader_phpcgi
2111}
2112
2113installService_mandriva_nginx() {
2114 installService_mandriva nginx getLsbStartupHeader_nginx
2115}
2116
2117installService_mandriva_lighttpd() {
2118 installService_mandriva lighttpd getLsbStartupHeader_lighttpd
2119}
2120
2121installService_gentoo() {
2122 local scriptName="$1"
2123 local desc="$2"
2124 local headerCommand="$3"
2125 local serviceFile="/etc/init.d/$scriptName"
2126
2127 cat > "$serviceFile" << EOF
2128#!/sbin/runscript
2129
2130name=$scriptName
2131description="$desc"
2132
2133depend() {
2134 need localmount
2135 need net
2136}
2137
2138$(eval $headerCommand)
2139
2140start() {
2141 ebegin "Starting \$name"
2142 startIt
2143 eend \$?
2144}
2145
2146stop() {
2147 ebegin "Stopping \$name"
2148 stopIt
2149 eend \$?
2150}
2151
2152status() {
2153 if isStarted; then
2154 einfo "status: started"
2155 return 0
2156 else
2157 einfo "status: stopped"
2158 return 1
2159 fi
2160}
2161EOF
2162 [ $? -eq 0 ] || errorExit "Could not write to file $serviceFile. Run this script as root."
2163 chmod +x "$serviceFile"
2164 rc-update add $scriptName default || errorExit "Could not add service $scriptName"
2165 $serviceFile restart
2166}
2167
2168installService_gentoo_autodl() {
2169 local user="$1"
2170 installService_gentoo autodl_$user "$AUTODL_STARTUP_DESC" "getCommonStartupScriptContents_autodl \"$user\" \"\$scriptName\""
2171}
2172
2173installService_gentoo_phpcgi() {
2174 installService_gentoo phpcgi "$PHPCGI_STARTUP_DESC" "getCommonStartupScriptContents_phpcgi \"$WWW_USER\" \"$WWW_PHP_CGI\""
2175}
2176
2177installService_gentoo_nginx() {
2178 installService_gentoo nginx "$NGINX_STARTUP_DESC" "getCommonStartupScriptContents_nginx \"$NGINX_BIN\""
2179}
2180
2181installService_gentoo_lighttpd() {
2182 installService_gentoo lighttpd "$LIGHTTPD_STARTUP_DESC" "getCommonStartupScriptContents_lighttpd \"$LIGHTTPD_BIN\""
2183}
2184
2185installService_bsd_autodl() {
2186 local user="$1"
2187 local scriptName="$2"
2188 local file="$3"
2189
2190 cat > "$file" << EOF
2191#!/bin/sh
2192# PROVIDE: autodl_$user
2193# REQUIRE: FILESYSTEMS NETWORKING
2194# KEYWORD: shutdown
2195
2196. /etc/rc.subr
2197
2198name=$scriptName
2199desc="$AUTODL_STARTUP_DESC"
2200start_cmd=v_startIt
2201stop_cmd=v_stopIt
2202restart_cmd=v_restartIt
2203reload_cmd=v_restartIt
2204status_cmd=v_showStatus
2205extra_commands="status"
2206
2207v_startIt() {
2208 echo -n "Starting \$NAME"
2209 startIt && echo "." || echo ". ERROR."
2210}
2211
2212v_stopIt() {
2213 echo -n "Stopping \$NAME..."
2214 stopIt && echo "done." || echo "ERROR."
2215}
2216
2217$(getCommonStartupScriptContents_autodl "$user" "$scriptName")
2218
2219load_rc_config \$name
2220run_rc_command "\$1"
2221EOF
2222 [ $? -eq 0 ] || errorExit "Could not write to file $file. Run this script as root."
2223 chmod +x "$file"
2224}
2225
2226getCommonStartupScriptContents_simpleService() {
2227 local USER="$1"
2228 local BINPATH="$2"
2229 local START_CMD="$3"
2230 local PID_FILE="$4"
2231 cat << EOF
2232USER=$USER
2233BINPATH="$BINPATH"
2234NAME=\${BINPATH##*/}
2235PID_FILE="$PID_FILE"
2236
2237resetPath() {
2238 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:\$PATH"
2239}
2240resetPath
2241
2242getPids() {
2243 if [ -n "\$PID_FILE" ]; then
2244 pids=
2245 [ -f "\$PID_FILE" ] || return
2246 local the_pids="\$(cat "\$PID_FILE" 2>/dev/null)"
2247 for pid in \$the_pids; do
2248 PS_FORMAT= LINES= COLUMNS= ps ax | grep -qE "^[ ]*\$pid[ ]" && pids="\$pids \$pid"
2249 done
2250 [ -z "\$pids" ] && rm -f "\$PID_FILE"
2251 else
2252 # ps uses the COLUMNS variable... Make sure it's off so we get the full path.
2253 pids=\$(PS_FORMAT= LINES= COLUMNS= ps aux | grep -v grep | grep \$BINPATH | awk '{print \$2}')
2254 fi
2255}
2256
2257isStarted() {
2258 getPids
2259 [ -n "\$pids" ] && return 0
2260 return 1
2261}
2262
2263startIt() {
2264 isStarted && return 0
2265
2266 if [ \$USER = root ]; then
2267 $START_CMD
2268 else
2269 su - \$USER "-c $START_CMD"
2270 fi
2271 [ -n "\$PID_FILE" ] && sleep 2 # Allow it some time to create the pid file
2272 if ! isStarted; then
2273 for i in 1 2 3 4 5; do
2274 sleep 1
2275 isStarted && break
2276 done
2277 isStarted || return 1
2278 fi
2279
2280 return 0
2281}
2282
2283stopIt() {
2284 isStarted || return 0
2285
2286 kill \$pids 2>/dev/null
2287
2288 for i in 0 1 2 3 4 5 6 7 8 9; do
2289 isStarted || break
2290 sleep 1
2291 done
2292
2293 isStarted && kill -KILL \$pids 2>/dev/null
2294
2295 return 0
2296}
2297
2298v_startIt() {
2299 startIt
2300}
2301
2302v_stopIt() {
2303 stopIt
2304}
2305
2306v_restartIt() {
2307 v_stopIt
2308 v_startIt
2309}
2310
2311v_showStatus() {
2312 if isStarted; then
2313 echo "\$NAME is running."
2314 else
2315 echo "\$NAME is stopped."
2316 fi
2317}
2318EOF
2319}
2320
2321getCommonStartupScriptContents_phpcgi() {
2322 [ -n "$PHPCGI_SOCKET_FILE" ] || errorExit "PHPCGI_SOCKET_FILE is not initialized"
2323 getCommonStartupScriptContents_simpleService "$1" "$2" \
2324 "PHP_FCGI_CHILDREN=5 PHP_FCGI_MAX_REQUESTS=125 \$BINPATH -q -b \"$PHPCGI_SOCKET_FILE\" &"
2325}
2326
2327getLsbStartupHeader_phpcgi() {
2328 getChkconfigScriptContents "$PHPCGI_STARTUP_DESC"
2329 getLsbStartupScriptContents phpcgi "$PHPCGI_STARTUP_DESC"
2330
2331 getCommonStartupScriptContents_phpcgi "$WWW_USER" "$WWW_PHP_CGI"
2332}
2333
2334getCommonStartupScriptContents_nginx() {
2335 getCommonStartupScriptContents_simpleService "root" "$1" "\$BINPATH" "$NGINX_PID_FILE"
2336}
2337
2338getLsbStartupHeader_nginx() {
2339 getChkconfigScriptContents "$NGINX_STARTUP_DESC"
2340 getLsbStartupScriptContents nginx "$NGINX_STARTUP_DESC"
2341
2342 getCommonStartupScriptContents_nginx "$NGINX_BIN"
2343}
2344
2345getCommonStartupScriptContents_lighttpd() {
2346 getCommonStartupScriptContents_simpleService "root" "$1" "\$BINPATH -f $LIGHTTPD_CONF 2>/dev/null" "$LIGHTTPD_PID_FILE"
2347}
2348
2349getLsbStartupHeader_lighttpd() {
2350 getChkconfigScriptContents "$LIGHTTPD_STARTUP_DESC"
2351 getLsbStartupScriptContents lighttpd "$LIGHTTPD_STARTUP_DESC"
2352
2353 getCommonStartupScriptContents_lighttpd "$LIGHTTPD_BIN"
2354}
2355
2356addLsbHeaderToStartupScript() {
2357 local scriptPath="$1"
2358 local provides="${2:-$scriptPath}"
2359 local desc="${3:-$provides}"
2360 [ -x "$scriptPath" ] || return
2361 grep -qE '^### BEGIN INIT INFO' "$scriptPath" && return
2362 ed -s "$scriptPath" > /dev/null 2>&1 << EOF
23631a
2364$(getLsbStartupScriptContents "$provides" "$desc")
2365.
2366w
2367q
2368EOF
2369}
2370
2371installMissingPerlModules() {
2372 detectMissingPerlModules
2373 [ -z "$MISSING_PERL_MODULES" ] && return
2374 cat << EOF
2375$CWARNING
2376The following Perl modules are still missing:
2377 $MISSING_PERL_MODULES
2378I'll try to use the cpan script to install them.$CEND
2379EOF
2380
2381 # Some Perl modules will fail to build unless Test::More is installed
2382 MISSING_PERL_MODULES="Test::More $MISSING_PERL_MODULES"
2383
2384 echo "${CMSG}Installing cpan and required build tools...$CEND"
2385 installBuildTools
2386 osHandler_$os installCpanTools
2387 detectCpanBin
2388 if [ -n "$CPAN" ]; then
2389 $CPAN $MISSING_PERL_MODULES
2390 else
2391 local mods=
2392 for mod in $MISSING_PERL_MODULES; do
2393 [ -n "$mods" ] && mods="$mods,"
2394 mods="$mods \"$mod\""
2395 done
2396 perl -MCPAN -e "CPAN::Shell->install($mods)"
2397 fi
2398
2399 detectMissingPerlModules
2400 [ -z "$MISSING_PERL_MODULES" ] && return
2401 errorExit "CPAN somehow failed to install the missing Perl modules. Missing: $MISSING_PERL_MODULES"
2402}
2403
2404osHandler_debian() {
2405 case $1 in
2406 init)
2407 INSTALL="apt-get -y install"
2408 FEATURES="service rtorrent apache nginx lighttpd vsftpd webmin"
2409 BUILD_TOOLS="build-essential make file pkg-config libtool m4"
2410 ;;
2411 init2)
2412 apt-get update
2413 installProgram ed
2414
2415 # Make sure Debian 6 doesn't fail when adding new services. We need ed for this
2416 # so run it after installing ed...
2417 addLsbHeaderToStartupScript /etc/init.d/webmin "webmin" "webmin"
2418 ;;
2419 installTools)
2420 PACKAGES=
2421 addProgram irssi
2422 addProgram mediainfo
2423 addProgram git-core
2424 addProgram svn subversion
2425 addProgram wget
2426 addProgram unzip
2427 addProgram screen
2428 installPackages
2429 # Make sure subversion won't complain about invalid certs
2430 $INSTALL ca-certificates
2431 ;;
2432 installAutodlTools)
2433 PACKAGES=
2434 addProgram perl
2435 addPerlModule Archive::Zip libarchive-zip-perl
2436 addPerlModule HTML::Parser libhtml-parser-perl
2437 addPerlModule Digest::SHA libdigest-sha-perl
2438 installPackages
2439
2440 installPerlModule Net::SSLeay libnet-ssleay-perl
2441 installPerlModule XML::LibXML libxml-libxml-perl
2442 installPerlModule JSON::XS libjson-xs-perl
2443 installPerlModule JSON libjson-perl
2444 ;;
2445 installCpanTools)
2446 # Need to remove the old JSON or cpan may fail to build the modules
2447 if echo "$OLD_PERL_MODULES" | grep -qE '(^| )JSON( |$)'; then
2448 apt-get -y remove libjson-perl
2449 fi
2450
2451 PACKAGES=
2452 if echo "$MISSING_PERL_MODULES" | grep -q 'XML::LibXML'; then
2453 PACKAGES="$PACKAGES libxml2 libxml2-dev zlib1g zlib1g-dev"
2454 fi
2455 if echo "$MISSING_PERL_MODULES" | grep -q 'Net::SSLeay'; then
2456 # There's no libssl package
2457 PACKAGES="$PACKAGES openssl libssl-dev zlib1g zlib1g-dev"
2458 fi
2459 installPackages
2460 ;;
2461 installRtorrentBuildTools)
2462 PACKAGES="libsigc++-2.0-dev libssl-dev libncurses-dev"
2463 if apt-cache search libcurl4-openssl-dev 2> /dev/null | grep -q 'libcurl4-openssl-dev'; then
2464 PACKAGES="$PACKAGES libcurl4-openssl-dev"
2465 else
2466 PACKAGES="$PACKAGES libcurl3-openssl-dev"
2467 fi
2468 installPackages
2469 ;;
2470 installPhpModules)
2471 installMissingPhp5Packages
2472 ;;
2473 _installService)
2474 local serviceName="$2"
2475 local headerCommand="$3"
2476 local serviceFile=/etc/init.d/$serviceName
2477 local lockFile=
2478 cat > "$serviceFile" << EOF
2479#!/bin/sh
2480$(eval $headerCommand)
2481
2482. /lib/lsb/init-functions
2483
2484v_startIt() {
2485 log_begin_msg "Starting \$NAME..."
2486 startIt
2487 log_end_msg \$?
2488}
2489
2490v_stopIt() {
2491 log_begin_msg "Stopping \$NAME..."
2492 stopIt
2493 log_end_msg \$?
2494}
2495
2496$(getLsbStartupFooter "$lockFile")
2497EOF
2498 [ $? -eq 0 ] || errorExit "Could not write to file $serviceFile. Run this script as root."
2499 chmod +x "$serviceFile" || errorExit "Could not set +x bit, file $serviceFile."
2500 update-rc.d $serviceName defaults || errorExit "Could not install service $serviceFile. Run this script as root."
2501 invoke-rc.d $serviceName restart
2502 ;;
2503 installAutodlService)
2504 local user=$2
2505 osHandler_$os _installService autodl_$user "getLsbStartupHeader_autodl \"$user\" \"\$serviceName\""
2506 ;;
2507 _installCommonWebServerStuff)
2508 # Also add some extra tools for ruTorrent and some of its plugins
2509 PACKAGES="php5-cli gzip curl openssl logrotate"
2510 installPackages
2511 # Ubuntu 8 doesn't have php5-geoip
2512 PACKAGES="php5-geoip"
2513 installPackages
2514 PACKAGES="unrar"
2515 installPackages
2516 ;;
2517 installApache)
2518 osHandler_$os _installCommonWebServerStuff
2519 PACKAGES="apache2-mpm-prefork libapache2-mod-php5 php5 "
2520 installPackages
2521 PACKAGES="libapache2-mod-scgi"; installPackages
2522
2523 WWW_PHP_INI=/etc/php5/apache2/php.ini
2524 APACHE_DIR=/etc/apache2
2525 APACHE_SITE_NAME=rutorrent.conf
2526 APACHE_SCGI_FILE=$APACHE_DIR/mods-available/scgi.load
2527 initializeApacheVars www-data www-data "$APACHE_DIR/sites-available"
2528 apacheCommonInitialization "apache2-prefork-dev" /usr/lib/apache2/modules
2529
2530 a2enmod ssl || errorExit "Could not enable mod_ssl"
2531 a2enmod scgi || errorExit "Could not enable mod_scgi"
2532 # Ubuntu Server 6 doesn't come with this module
2533 a2enmod auth_basic
2534 a2dissite default
2535 a2dissite default-ssl
2536 a2ensite $APACHE_SITE_NAME || errorExit "Could not enable site"
2537 if ! grep -qE "^[ ]*Listen.*[: ]$HTTPS_PORT\\>" "$APACHE_DIR/ports.conf"; then
2538 echo "Listen $HTTPS_PORT" >> "$APACHE_DIR/ports.conf"
2539 fi
2540 resetWebServerPermissions
2541 invoke-rc.d apache2 restart || errorExit "Could not start $INSTALL_WEB_SERVER"
2542 ;;
2543 restart_apache)
2544 invoke-rc.d apache2 restart || errorExit "Could not start $INSTALL_WEB_SERVER"
2545 ;;
2546 installVsftpd)
2547 installProgram vsftpd
2548 updateVsftpdConf /etc/vsftpd.conf
2549 invoke-rc.d vsftpd restart || errorExit "Could not restart vsftpd"
2550 ;;
2551 preNginxInstall)
2552 WWW_PHP_INI=/etc/php5/cgi/php.ini
2553 osHandler_$os _installCommonWebServerStuff
2554 PACKAGES="libpcre3-dev libssl-dev zlib1g-dev php5-cgi"
2555 installPackages
2556
2557 # Make sure it builds on Debian GNU/kFreeBSD 6.0.
2558 if [ "$(uname -s)" != Linux ]; then
2559 NGINX_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64"
2560 fi
2561 ;;
2562 installPhpCgiService)
2563 osHandler_$os _installService phpcgi getLsbStartupHeader_phpcgi
2564 ;;
2565 installNginxService)
2566 osHandler_$os _installService nginx getLsbStartupHeader_nginx
2567 ;;
2568 restart_nginx)
2569 invoke-rc.d phpcgi restart || errorExit "Could not start php-cgi"
2570 invoke-rc.d nginx restart || errorExit "Could not start $INSTALL_WEB_SERVER"
2571 ;;
2572 preLighttpdInstall)
2573 WWW_PHP_INI=/etc/php5/cgi/php.ini
2574 osHandler_$os _installCommonWebServerStuff
2575 PACKAGES="libpcre3-dev libssl-dev zlib1g-dev php5-cgi"
2576 installPackages
2577 ;;
2578 installLighttpdService)
2579 osHandler_$os _installService lighttpd getLsbStartupHeader_lighttpd
2580 ;;
2581 restart_lighttpd)
2582 invoke-rc.d phpcgi restart || errorExit "Could not start php-cgi"
2583 invoke-rc.d lighttpd restart || errorExit "Could not start $INSTALL_WEB_SERVER"
2584 ;;
2585 postWebminInstall)
2586 addLsbHeaderToStartupScript /etc/init.d/webmin "webmin" "webmin"
2587 ;;
2588 *)
2589 ;;
2590 esac
2591}
2592
2593osHandler_fedora() {
2594 case $1 in
2595 init)
2596 INSTALL="yum -y install"
2597 FEATURES="service rtorrent apache nginx lighttpd vsftpd webmin"
2598 BUILD_TOOLS="gcc gcc-c++ make kernel-headers pkgconfig file libtool m4"
2599 ;;
2600 init2)
2601 installProgram which
2602 installProgram ed
2603 ;;
2604 installTools)
2605 PACKAGES=
2606 addProgram svn subversion
2607 addProgram wget
2608 addProgram unzip
2609 addProgram screen
2610 addProgram git
2611 installPackages
2612 ;;
2613 installAutodlTools)
2614 if grep -q "release 4[. ]" /etc/redhat-release; then
2615 IRSSI_LOAD_PERL=y
2616 fi
2617 PACKAGES=
2618 addProgram perl
2619 addProgram irssi
2620 addPerlModule Time::HiRes perl-Time-HiRes
2621 addPerlModule Archive::Zip perl-Archive-Zip
2622 addPerlModule Net::SSLeay perl-Net-SSLeay
2623 addPerlModule HTML::Parser perl-HTML-Parser
2624 addPerlModule XML::LibXML perl-XML-LibXML
2625 addPerlModule Digest::SHA1 perl-Digest-SHA1
2626 addPerlModule JSON::XS perl-JSON-XS
2627 addPerlModule JSON perl-JSON
2628 installPackages
2629 ;;
2630 installCpanTools)
2631 # Need to remove the old JSON or cpan may fail to build the modules
2632 if echo "$OLD_PERL_MODULES" | grep -qE '(^| )JSON( |$)'; then
2633 yum -y remove perl-JSON
2634 fi
2635
2636 # CentOS doesn't have 'perl-CPAN'. It's in the 'perl' package though.
2637 PACKAGES="perl-CPAN"
2638 if echo "$MISSING_PERL_MODULES" | grep -q 'XML::LibXML'; then
2639 PACKAGES="$PACKAGES libxml2 libxml2-devel zlib zlib-devel"
2640 fi
2641 if echo "$MISSING_PERL_MODULES" | grep -q 'Net::SSLeay'; then
2642 PACKAGES="$PACKAGES openssl openssl-devel zlib zlib-devel"
2643 fi
2644 installPackages
2645 ;;
2646 installRtorrentBuildTools)
2647 # CentOS 5: curl-devel, no libsigc++20
2648 # Fedora 14: libcurl-devel
2649 PACKAGES="libsigc++20-devel ncurses-devel openssl-devel libcurl-devel curl-devel"
2650 installPackages
2651 ;;
2652 installPhpModules)
2653 ;;
2654 _installService)
2655 local serviceName="$2"
2656 local headerCommand="$3"
2657 installService_chkconfig1 $serviceName
2658 cat > "$CHKCONFIG_SERVICE_FILE" << EOF
2659#!/bin/bash
2660$(eval $headerCommand)
2661
2662. /etc/rc.d/init.d/functions
2663
2664v_startIt() {
2665 echo -n "Starting \$NAME: "
2666 startIt && success || failure
2667 echo
2668}
2669
2670v_stopIt() {
2671 echo -n "Stopping \$NAME: "
2672 stopIt && success || failure
2673 echo
2674}
2675
2676$(getLsbStartupFooter "$CHKCONFIG_LOCK_FILE")
2677EOF
2678 [ $? -eq 0 ] || errorExit "Could not write to file $CHKCONFIG_SERVICE_FILE. Run this script as root."
2679 installService_chkconfig2
2680 ;;
2681 installAutodlService)
2682 local user=$2
2683 osHandler_$os _installService autodl_$user "getLsbStartupHeader_autodl \"$user\" \"\$CHKCONFIG_SERVICE_NAME\""
2684 ;;
2685 _installCommonWebServerStuff)
2686 PACKAGE="php-cli gzip curl openssl unrar logrotate"
2687 installPackages
2688 # CentOS 4: will get an error about a php v4 dependency, so can't put it in above PACKAGE var
2689 PACKAGES="php-pecl-geoip"
2690 installPackages
2691 ;;
2692 installApache)
2693 osHandler_$os _installCommonWebServerStuff
2694 # Also add some extra tools for ruTorrent and some of its plugins
2695 PACKAGES="httpd httpd-tools mod_ssl mod_scgi php"
2696 installPackages
2697
2698 WWW_PHP_INI=/etc/php.ini
2699 APACHE_DIR=/etc/httpd
2700 local APACHE_CONF_D=$APACHE_DIR/conf.d
2701 APACHE_SCGI_FILE=$APACHE_CONF_D/scgi.conf
2702 initializeApacheVars apache apache "$APACHE_CONF_D"
2703 apacheCommonInitialization "httpd-devel" $APACHE_DIR/modules
2704
2705 [ -f "$APACHE_CONF_D/welcome.conf" ] && mv -f "$APACHE_CONF_D/welcome.conf" "$APACHE_CONF_D/welcome.conf-bak"
2706 # Remove an Apache warning by commenting out the default SSL VirtualHost
2707 sed_i '/^<VirtualHost _default_:443>/,/^<\/VirtualHost>/s/^/#/' $APACHE_CONF_D/ssl.conf
2708
2709 resetWebServerPermissions
2710 chkconfig httpd on
2711 service httpd restart || errorExit "Could not start $INSTALL_WEB_SERVER"
2712 ;;
2713 restart_apache)
2714 service httpd restart || errorExit "Could not start $INSTALL_WEB_SERVER"
2715 ;;
2716 installVsftpd)
2717 installVsftpd_chkconfig /etc/vsftpd/vsftpd.conf
2718 ;;
2719 preNginxInstall)
2720 WWW_PHP_INI=/etc/php.ini
2721 osHandler_$os _installCommonWebServerStuff
2722 PACKAGES="pcre-devel openssl-devel zlib-devel php-cgi"
2723 installPackages
2724 ;;
2725 installPhpCgiService)
2726 osHandler_$os _installService phpcgi getLsbStartupHeader_phpcgi
2727 ;;
2728 installNginxService)
2729 osHandler_$os _installService nginx getLsbStartupHeader_nginx
2730 ;;
2731 restart_nginx)
2732 service phpcgi restart || errorExit "Could not start php-cgi"
2733 service nginx restart || errorExit "Could not start $INSTALL_WEB_SERVER"
2734 ;;
2735 preLighttpdInstall)
2736 WWW_PHP_INI=/etc/php.ini
2737 osHandler_$os _installCommonWebServerStuff
2738 PACKAGES="pcre-devel openssl-devel zlib-devel php-cgi"
2739 installPackages
2740 ;;
2741 installLighttpdService)
2742 osHandler_$os _installService lighttpd getLsbStartupHeader_lighttpd
2743 ;;
2744 restart_lighttpd)
2745 service phpcgi restart || errorExit "Could not start php-cgi"
2746 service lighttpd restart || errorExit "Could not start $INSTALL_WEB_SERVER"
2747 ;;
2748 *)
2749 ;;
2750 esac
2751}
2752
2753isWebServerRpcModule() {
2754 ! arrayIsPresent rpc $RUTORRENT_PLUGINS && ! arrayIsPresent httprpc $RUTORRENT_PLUGINS && return 0
2755 return 1
2756}
2757
2758canInstallService() {
2759 return $(arrayIsPresent service $FEATURES)
2760}
2761
2762canInstallRtorrent() {
2763 return $(arrayIsPresent rtorrent $FEATURES)
2764}
2765
2766canInstallApache() {
2767 return $(arrayIsPresent apache $FEATURES)
2768}
2769
2770canInstallNginx() {
2771 return $(arrayIsPresent nginx $FEATURES)
2772}
2773
2774canInstallLighttpd() {
2775 return $(arrayIsPresent lighttpd $FEATURES)
2776}
2777
2778canInstallVsftpd() {
2779 return $(arrayIsPresent vsftpd $FEATURES)
2780}
2781
2782canInstallWebmin() {
2783 return $(arrayIsPresent webmin $FEATURES)
2784}
2785
2786getRequiredPrograms() {
2787 REQUIRED_PROGRAMS="which svn wget unzip ed sed grep tar expr ps awk kill printf"
2788
2789 if [ "$INSTALL_AUTODL_IRSSI" = y ]; then
2790 REQUIRED_PROGRAMS="$REQUIRED_PROGRAMS perl"
2791
2792 # NetBSD and DragonFly BSD don't compile Irssi with Perl support by default
2793 [ "$IGNORE_IRSSI" != y ] && REQUIRED_PROGRAMS="$REQUIRED_PROGRAMS irssi"
2794 fi
2795}
2796
2797verifyInstalledPrograms() {
2798 getRequiredPrograms
2799
2800 local MISSING_PROGRAMS=
2801 for prog in $REQUIRED_PROGRAMS; do
2802 local name=$prog
2803 [ "$prog" = svn ] && name=subversion
2804 isProgramInstalled "$prog" || MISSING_PROGRAMS="$MISSING_PROGRAMS $name"
2805 done
2806 [ -n "$MISSING_PROGRAMS" ] && errorExit "Can't continue. The following programs are not installed: $MISSING_PROGRAMS"
2807}
2808
2809initPluginDirVar() {
2810 AUTODL_IRSSI_PLUGIN_DIR="$RUTORRENT_BASE_PATH/plugins/autodl-irssi"
2811}
2812
2813getRtorrentDirs() {
2814 local user="$1"
2815 getUserDir "$user"
2816 RTORRENT_DOWNLOAD_DIR="$userDir/$RTORRENT_REL_DOWNLOAD_DIR"
2817 RTORRENT_WATCH_DIR="$userDir/$RTORRENT_REL_WATCH_DIR"
2818 RTORRENT_SESSION_DIR="$userDir/$RTORRENT_REL_SESSION_DIR"
2819}
2820
2821getUserScgiSocketPath() {
2822 local user="$1"
2823 getRtorrentDirs "$user"
2824 scgiSocketPath="$RTORRENT_SESSION_DIR/rpc.socket"
2825}
2826
2827waitenter() {
2828 local msg="${1-Press ENTER to continue...}"
2829 echo -n "$CCYAN$msg$CEND"
2830 read dummy
2831}
2832
2833askQuestion() {
2834 local question="$1"
2835 local default="$2"
2836
2837 if [ -z "$default" ]; then
2838 echo -n "$CQUESTION$question$CEND "
2839 read answer
2840 else
2841 echo -n "$CQUESTION$question$CEND [$CGREEN$default$CEND] "
2842 read answer
2843 fi
2844 if [ -z "$answer" ]; then
2845 answer="$default"
2846 fi
2847}
2848
2849# Asks the user a question, and sets answer to y or n depending on the user's answer
2850askYesNo() {
2851 local question="$1"
2852 local default="$2"
2853
2854 while true; do
2855 askQuestion "$question" "$default"
2856 if echo "$answer" | grep -qiE '^y(es)?$'; then
2857 answer=y
2858 return 1
2859 elif echo "$answer" | grep -qiE '^no?$'; then
2860 answer=n
2861 return 0
2862 fi
2863 done
2864}
2865
2866askOsUser() {
2867 local stopIfEmpty=${1:-n}
2868 local user=
2869 while true; do
2870 askQuestion "Enter name of user:" ""
2871 user="$answer"
2872 [ -z "$user" ] && [ "$stopIfEmpty" = y ] && break
2873 userExists "$user" && break
2874
2875 echo "${CWARNING}User $user does not exist.$CEND"
2876 askYesNo "Do you want to create user $user?" "No"
2877 [ "$answer" = n ] && continue
2878
2879 echo "${CMSG}Creating user $user.$CEND"
2880 if ! useradd -m -s /bin/sh "$user"; then
2881 echo "${CWARNING}Could not create user $user.$CEND"
2882 continue
2883 fi
2884 echo "${CMSG}Enter the user's password:$CEND"
2885 if ! passwd "$user"; then
2886 echo "${CWARNING}Failed to set password.$CEND"
2887 continue
2888 fi
2889 userExists "$user" && break
2890 done
2891 answer="$user"
2892}
2893
2894# Adds another user to USERS
2895addUser() {
2896 local osUser="$1"
2897 local webUser="$2"
2898 local autodlPassword=
2899 local webpass=
2900
2901 if [ "$INSTALL_RUTORRENT" = y ] || [ "$USE_RUTORRENT_PLUGIN" = y ]; then
2902 if ! canGeneratePasswords; then
2903 cat << EOF
2904$CQUESTION
2905The password makes sure only you can change your autodl-irssi settings. This is
2906NOT your login password. Use any password, but each user should use a unique
2907password. The password is used by the PHP code to access autodl-irssi settings.
2908You don't need to remember this password. It can't contain any spaces or a
2909colon ':'.$CEND
2910EOF
2911 while true; do
2912 askQuestion "Enter the autodl-irssi password (this is not your login password):" ""
2913 autodlPassword="$answer"
2914 isValidPassword "$autodlPassword" && break
2915 echo "${CWARNING}Invalid password. Try again.$CEND"
2916 done
2917 fi
2918
2919 if [ "$RUTORRENT_PASSWORD_PROTECTED" = y ]; then
2920 while true; do
2921 askQuestion "Enter your ruTorrent password:" ""
2922 webpass="$answer"
2923 isValidPassword "$webpass" && break
2924 echo "${CWARNING}Invalid password. Try again.$CEND"
2925 done
2926 fi
2927
2928 USERS="$USERS $osUser:$autodlPassword:$webUser:$webpass"
2929 else
2930 USERS="$USERS $osUser"
2931 fi
2932}
2933
2934installUser() {
2935 local userPluginDir="$1"
2936 local osUser="$2"
2937 local webUser="${3:-$osUser}"
2938 local port="$4"
2939 local autodlPassword="$5"
2940
2941 getUserGroup "$osUser"
2942 getUserDir "$osUser"
2943
2944 cat << EOF
2945
2946========================= ${CGREEN}Installing autodl-irssi$CEND =========================
2947Use the ruTorrent plugin: $CGREEN$USE_RUTORRENT_PLUGIN$CEND
2948OS user: $CGREEN$osUser$CEND
2949EOF
2950 if [ "$USE_RUTORRENT_PLUGIN" = y ]; then
2951 [ "$RUTORRENT_PASSWORD_PROTECTED" = y ] && echo "ruTorrent user: $CGREEN$webUser$CEND"
2952 echo "port: $CGREEN$port$CEND"
2953 echo "password: $CGREEN$autodlPassword$CEND"
2954 fi
2955 cat << EOF
2956group: $CGREEN$group$CEND
2957home: $CGREEN$userDir$CEND
2958
2959EOF
2960 if [ "$USE_RUTORRENT_PLUGIN" = y ]; then
2961 isValidPortNumber "$port" || errorExit "Invalid port number: $port"
2962 isValidPassword "$autodlPassword" || errorExit "Invalid password: $autodlPassword"
2963 isValidWebUser "$webUser" || errorExit "Invalid web user: $webUser"
2964 fi
2965
2966 mkdir -p "$userDir/.irssi/scripts/autorun"
2967 cd "$userDir/.irssi/scripts" || errorExit "Could not CD into user dir. Run the script as root."
2968 echo "${CMSG}Downloading autodl-irssi.zip...$CEND"
2969 if ! downloadFile autodl-irssi.zip "$AUTODL_IRSSI_ZIP_URL"; then
2970 errorExit "Could not download autodl-irssi zip file"
2971 fi
2972 echo "${CMSG}Unpacking autodl-irssi...$CEND"
2973 unzip -o autodl-irssi.zip > /dev/null || errorExit "Could not unpack autodl-irssi zip file"
2974 rm -f autodl-irssi.zip
2975 cp autodl-irssi.pl autorun/ || errorExit "Could not copy autodl-irssi.pl to Irssi autorun dir."
2976 mkdir -p "$userDir/.autodl"
2977 touch "$userDir/.autodl/autodl.cfg"
2978 if ! [ -s "$userDir/.autodl/autodl.cfg" ]; then
2979 [ "$INSTALL_RUTORRENT" = y ] && cat > "$userDir/.autodl/autodl.cfg" << EOF
2980[options]
2981upload-type = rtorrent
2982EOF
2983 fi
2984
2985 if [ "$USE_RUTORRENT_PLUGIN" = y ]; then
2986 if [ ! -d "$AUTODL_IRSSI_PLUGIN_DIR" ]; then
2987 errorExit "The autodl-irssi ruTorrent plugin has not been installed. Install it."
2988 fi
2989
2990 cat > "$userDir/.autodl/autodl2.cfg" << EOF
2991[options]
2992gui-server-port = $port
2993gui-server-password = $autodlPassword
2994EOF
2995
2996 rm -f "$AUTODL_IRSSI_PLUGIN_DIR/conf.php"
2997 mkdir -p "$userPluginDir"
2998 cat > "$userPluginDir/conf.php" << EOF
2999<?php
3000\$autodlPort = $port;
3001\$autodlPassword = "$autodlPassword";
3002?>
3003EOF
3004 [ $? -eq 0 ] || errorExit "Could not write to $userPluginDir/conf.php. Run this script as root."
3005 # Only set perms to 0400 if the code knows the owner (web server owner) and will reset it
3006 [ "$ISROOT" = y ] && [ "$INSTALL_RUTORRENT" = y ] && chmod 0400 "$userPluginDir/conf.php"
3007 else
3008 # Don't use the autodl-irssi ruTorrent plugin
3009 : > "$userDir/.autodl/autodl2.cfg"
3010 fi
3011
3012 # The Perl module isn't loaded by default
3013 if [ "$IRSSI_LOAD_PERL" = y ]; then
3014 if [ ! -f "$userDir/.irssi/startup" ] || ! grep -q 'load perl' "$userDir/.irssi/startup"; then
3015 echo "load perl" >> "$userDir/.irssi/startup"
3016 fi
3017 fi
3018
3019 # Make sure we redownload the tracker files since the ones in the zip file are possibly
3020 # old versions.
3021 local autodlStateFile="$userDir/.autodl/AutodlState.xml"
3022 [ -f "$autodlStateFile" ] && sed_i 'g/<trackers-version>/d' "$autodlStateFile"
3023
3024 resetOwner "$osUser" "$userDir/.autodl" "$userDir/.irssi"
3025 chmod 0700 "$userDir/.autodl" "$userDir/.irssi"
3026}
3027
3028getRutorrentUserConfDir() {
3029 userConfDir="$RUTORRENT_BASE_PATH/conf/users/$webUser"
3030}
3031
3032getRutorrentUserShareDir() {
3033 userShareDir="$RUTORRENT_BASE_PATH/share/users/$webUser"
3034}
3035
3036resetRutorrentUserPermissions() {
3037 for packedUser in $USERS; do
3038 extractPackedUser $packedUser
3039 getRutorrentUserConfDir
3040 getRutorrentUserShareDir
3041 # Make sure only the web server and the user itself can access its share/conf dirs
3042 chown $osUser:$WWW_GROUP "$userShareDir" "$userConfDir"
3043 chmod 0770 "$userShareDir" "$userConfDir"
3044 done
3045}
3046
3047############################################################################
3048#
3049# This is where we start
3050#
3051############################################################################
3052
3053SELINUX_ENABLED=n
3054isProgramInstalled selinuxenabled && selinuxenabled && SELINUX_ENABLED=y
3055echo "SELinux enabled: $SELINUX_ENABLED"
3056
3057ISROOT=n
3058[ $(id -u) -eq 0 ] && ISROOT=y
3059echo "Is root user: $ISROOT"
3060
3061if [ $# -gt 0 ]; then
3062 parseCommandLine "$@"
3063 INTERACTIVE=n
3064else
3065 INTERACTIVE=y
3066fi
3067
3068detectOs
3069cat << EOF
3070Detected OS: $CGREEN$os_long$CEND
3071Type: $CGREEN$os$CEND
3072
3073Type sh $0 --help for all command line options.
3074EOF
3075
3076osHandler_$os init
3077
3078if [ "$INTERACTIVE" = y ]; then
3079 if [ "$ISROOT" = n ]; then
3080 cat << EOF
3081$CRED
3082You're not the root user! This install script may fail if you're not the root
3083user. To start it as the root user do one of the following:
3084
3085Ubuntu and Ubuntu clones:
3086 ${CGREEN}sudo sh $0$CRED
3087
3088Any other OS:
3089 ${CGREEN}su
3090 sh $0$CRED
3091$CEND
3092EOF
3093 waitenter "Press Ctrl+C to exit or ENTER to continue..."
3094 fi
3095
3096 cat << EOF
3097$CMSG
3098Press ENTER to use the default answer in [brackets].$CEND
3099EOF
3100
3101 webServers=
3102 canInstallLighttpd && webServers="$webServers lighttpd"
3103 canInstallNginx && webServers="$webServers nginx"
3104 canInstallApache && webServers="$webServers apache"
3105 if [ -n "$webServers" ]; then
3106 cat << EOF
3107$CQUESTION
3108If you want to install ruTorrent you must install a web server, eg. Apache.
3109If you've already installed another web server, you must first uninstall it or
3110disable it. List of supported web servers:$CEND
3111EOF
3112 canInstallLighttpd && echo " ${CGREEN}lighttpd$CEND ${CQUESTION}lighttpd web server (lightweight)$CEND"
3113 canInstallNginx && echo " ${CGREEN}nginx$CEND ${CQUESTION}nginx web server (lightweight)$CEND"
3114 canInstallApache && echo " ${CGREEN}apache$CEND ${CQUESTION}Apache web server$CEND"
3115 echo " ${CGREEN}none$CEND ${CQUESTION}Don't install a web server$CEND"
3116 while true; do
3117 INSTALL_WEB_SERVER=
3118 echo "${CQUESTION}Select one of:$CGREEN$webServers none$CEND"
3119 askQuestion "Enter name of web server" "$(getFirst $webServers)"
3120 [ "$answer" = none ] && break
3121 INSTALL_WEB_SERVER="$answer"
3122 arrayIsPresent $INSTALL_WEB_SERVER $webServers && break
3123 done
3124 if [ -n "$INSTALL_WEB_SERVER" ]; then
3125 askYesNo "Do you want to install ruTorrent?" "Yes"
3126 INSTALL_RUTORRENT="$answer"
3127 else
3128 INSTALL_RUTORRENT=n
3129 fi
3130 fi
3131
3132 if canInstallRtorrent; then
3133 cat << EOF
3134$CQUESTION
3135ruTorrent requires a working rtorrent built with XML-RPC support.$CEND
3136EOF
3137 askYesNo "Do you want to build rtorrent?" "Yes"
3138 BUILD_RTORRENT="$answer"
3139 fi
3140
3141 askYesNo "Do you want to install the autodl-irssi ruTorrent plugin?" "Yes"
3142 USE_RUTORRENT_PLUGIN="$answer"
3143
3144 if [ "$INSTALL_RUTORRENT" = y ]; then
3145 REINSTALL_RUTORRENT_PLUGIN=n
3146 RUTORRENT_PASSWORD_PROTECTED=y
3147 elif [ "$USE_RUTORRENT_PLUGIN" = y ]; then
3148 echo ""
3149 while true; do
3150 echo "${CQUESTION}The ruTorrent www base path is where you installed ruTorrent.$CEND"
3151 askQuestion "What is the ruTorrent www base path, eg. /var/www/rutorrent:" ""
3152 RUTORRENT_BASE_PATH="$answer"
3153 isValidRutorrentBasePath "$RUTORRENT_BASE_PATH" && break
3154
3155 echo "$CWARNING$RUTORRENT_BASE_PATH is not the ruTorrent base path.$CEND"
3156 done
3157
3158 initPluginDirVar
3159 if [ -d "$AUTODL_IRSSI_PLUGIN_DIR" ]; then
3160 echo ""
3161 askYesNo "The autodl-irssi ruTorrent plugin seems to be installed. Do you want to re-install it?" "No"
3162 REINSTALL_RUTORRENT_PLUGIN="$answer"
3163 else
3164 REINSTALL_RUTORRENT_PLUGIN=n
3165 fi
3166
3167 cat << EOF
3168$CQUESTION
3169To use more than one ruTorrent user, you must password protect ruTorrent.$CEND
3170EOF
3171 askYesNo "Is ruTorrent password protected?" "No"
3172 RUTORRENT_PASSWORD_PROTECTED="$answer"
3173 else
3174 RUTORRENT_PASSWORD_PROTECTED=n
3175 fi
3176
3177 if [ "$USE_RUTORRENT_PLUGIN" = y ]; then
3178 INSTALL_AUTODL_IRSSI=y
3179 else
3180 askYesNo "Do you want to install autodl-irssi?" "Yes"
3181 INSTALL_AUTODL_IRSSI="$answer"
3182 fi
3183
3184 if canInstallService && ([ "$INSTALL_AUTODL_IRSSI" = y ] || [ "$INSTALL_RUTORRENT" = y ]); then
3185 cat << EOF
3186$CQUESTION
3187If you want to automatically start Irssi and rtorrent when the computer boots,
3188install the startup script.$CEND
3189EOF
3190 askYesNo "Do you want to install the startup script?" "Yes"
3191 INSTALL_STARTUP_SCRIPT="$answer"
3192 fi
3193
3194 askUser=n
3195 if [ "$USE_RUTORRENT_PLUGIN" = y ] || [ "$INSTALL_AUTODL_IRSSI" = y ] || \
3196 [ "$INSTALL_STARTUP_SCRIPT" = y ] || [ "$INSTALL_RUTORRENT" = y ]; then
3197 askUser=y
3198 fi
3199 if [ "$askUser" = y ]; then
3200 if [ "$RUTORRENT_PASSWORD_PROTECTED" = n ]; then
3201 cat << EOF
3202$CQUESTION
3203This is the user running rtorrent and/or Irssi.$CEND
3204${CWARNING}Don't use the root user. Use a normal user!$CEND
3205EOF
3206 askOsUser
3207 addUser "$answer" ""
3208 else
3209 while true; do
3210 cat << EOF
3211$CQUESTION
3212===================== USER =====================
3213This is the user running rtorrent and/or Irssi.
3214${CWARNING}Don't use the root user. Use a normal user!$CEND
3215${CCYAN}Press ENTER to stop adding users.$CEND
3216EOF
3217 askOsUser y
3218 osUser="$answer"
3219 [ -z "$osUser" ] && break
3220
3221 while true; do
3222 cat << EOF
3223$CQUESTION
3224The ruTorrent user is the name you use to log in to ruTorrent.$CEND
3225EOF
3226 askQuestion "Enter ruTorrent user:" "$osUser"
3227 webUser="$answer"
3228 isValidWebUser "$webUser" && break
3229 echo "${CWARNING}Invalid web user name '$webUser'. Use only lower case letters."
3230 done
3231
3232 addUser "$osUser" "$webUser"
3233 done
3234 fi
3235 fi
3236
3237 if canInstallVsftpd; then
3238 cat << EOF
3239$CQUESTION
3240vsftpd is a very secure FTP daemon.$CEND
3241EOF
3242 askYesNo "Do you want to install vsftpd?" "Yes"
3243 INSTALL_VSFTPD="$answer"
3244
3245 if [ "$INSTALL_VSFTPD" = y ]; then
3246 askYesNo "Do you want to use encrypted FTP (FTPES)" "Yes"
3247 USE_ENCRYPTED_FTP="$answer"
3248
3249 port=$DEFAULT_PORT_FTP
3250 [ "$USE_ENCRYPTED_FTP" = y ] && port=$DEFAULT_PORT_FTPES
3251 askQuestion "Enter FTP port number" "$port"
3252 FTP_PORT="$answer"
3253 fi
3254 fi
3255
3256 if canInstallWebmin; then
3257 cat << EOF
3258$CQUESTION
3259Webmin is a web-based administration tool for your OS.$CEND
3260EOF
3261 askYesNo "Do you want to install Webmin?" "Yes"
3262 INSTALL_WEBMIN="$answer"
3263 fi
3264fi
3265
3266cmdline="sh $0"
3267[ "$USE_RUTORRENT_PLUGIN" = y ] && cmdline="$cmdline -p"
3268[ "$REINSTALL_RUTORRENT_PLUGIN" = y ] && cmdline="$cmdline -i"
3269[ "$INSTALL_AUTODL_IRSSI" = y ] && cmdline="$cmdline -a"
3270for user in $USERS; do cmdline="$cmdline -u $user"; done
3271[ "$RUTORRENT_PASSWORD_PROTECTED" = y ] && cmdline="$cmdline -w"
3272[ -n "$RUTORRENT_BASE_PATH" ] && cmdline="$cmdline -r '$RUTORRENT_BASE_PATH'"
3273[ "$INSTALL_STARTUP_SCRIPT" = y ] && cmdline="$cmdline -s"
3274if [ -z "$INSTALL_WEB_SERVER" ]; then
3275 :
3276elif [ "$INSTALL_WEB_SERVER" = apache ]; then
3277 cmdline="$cmdline --apache"
3278elif [ "$INSTALL_WEB_SERVER" = nginx ]; then
3279 cmdline="$cmdline --nginx"
3280elif [ "$INSTALL_WEB_SERVER" = lighttpd ]; then
3281 cmdline="$cmdline --lighttpd"
3282else
3283 errorExit "Invalid web server: $INSTALL_WEB_SERVER"
3284fi
3285[ "$BUILD_RTORRENT" = y ] && cmdline="$cmdline --rtorrent"
3286[ "$INSTALL_RUTORRENT" = y ] && cmdline="$cmdline --rutorrent"
3287[ "$INSTALL_VSFTPD" = y ] && cmdline="$cmdline --vsftpd"
3288[ -n "$FTP_PORT" ] && cmdline="$cmdline --ftp-port $FTP_PORT"
3289[ "$USE_ENCRYPTED_FTP" = y ] && cmdline="$cmdline --ftpes"
3290[ "$INSTALL_WEBMIN" = y ] && cmdline="$cmdline --webmin"
3291
3292cat << EOF
3293
3294You can execute this command as the root user (Ubuntu: use ${CRED}sudo$CEND):
3295 $CGREEN$cmdline$CEND
3296
3297Use the autodl-irssi ruTorrent plugin: $USE_RUTORRENT_PLUGIN
3298Re-install the autodl-irssi ruTorrent plugin: $REINSTALL_RUTORRENT_PLUGIN
3299Install autodl-irssi: $INSTALL_AUTODL_IRSSI
3300Users: $USERS
3301ruTorrent is password protected: $RUTORRENT_PASSWORD_PROTECTED
3302ruTorrent base path: $RUTORRENT_BASE_PATH
3303Install startup script: $INSTALL_STARTUP_SCRIPT
3304Install web server: $INSTALL_WEB_SERVER
3305Build rtorrent: $BUILD_RTORRENT
3306Install ruTorrent: $INSTALL_RUTORRENT
3307Install vsftpd: $INSTALL_VSFTPD
3308FTP port: $FTP_PORT
3309Use FTPES: $USE_ENCRYPTED_FTP
3310Install Webmin: $INSTALL_WEBMIN
3311EOF
3312
3313if [ -n "$INSTALL_WEB_SERVER" ]; then
3314 for port in $HTTP_PORT $HTTPS_PORT; do
3315 isPortUsed $port || continue
3316 cat << EOF
3317$CWARNING
3318Port $port is in use. If it's not used by $INSTALL_WEB_SERVER, you may need to disable or
3319uninstall that other web server before continuing.$CEND
3320EOF
3321 done
3322fi
3323
3324if [ "$INTERACTIVE" = y ]; then
3325 echo ""
3326 waitenter "Press Ctrl+C to cancel or ENTER to install."
3327fi
3328
3329osHandler_$os init2
3330initUsers
3331initPluginDirVar
3332
3333[ -n "$INSTALL_WEB_SERVER" ] || [ "$INSTALL_RUTORRENT" = y ] && RUTORRENT_PASSWORD_PROTECTED=y
3334[ "$INSTALL_RUTORRENT" = y ] && [ -z "$INSTALL_WEB_SERVER" ] && errorExit "You must install a web server if you want to install ruTorrent, eg. use --apache --rutorrent"
3335[ "$INSTALL_RUTORRENT" = y ] && REINSTALL_RUTORRENT_PLUGIN=n
3336
3337[ "$BUILD_RTORRENT" = y ] && ! canInstallRtorrent && errorExit "Can't build rtorrent."
3338[ "$INSTALL_STARTUP_SCRIPT" = y ] && ! canInstallService && errorExit "Can't install startup script."
3339[ "$INSTALL_VSFTPD" = y ] && ! canInstallVsftpd && errorExit "Can't install vsftpd."
3340[ "$INSTALL_WEBMIN" = y ] && ! canInstallWebmin && errorExit "Can't install webmin."
3341
3342if [ -z "$INSTALL_WEB_SERVER" ]; then
3343 :
3344elif [ "$INSTALL_WEB_SERVER" = apache ]; then
3345 canInstallApache || errorExit "Can't install Apache."
3346elif [ "$INSTALL_WEB_SERVER" = nginx ]; then
3347 canInstallNginx || errorExit "Can't install nginx."
3348elif [ "$INSTALL_WEB_SERVER" = lighttpd ]; then
3349 canInstallLighttpd || errorExit "Can't install lighttpd."
3350else
3351 errorExit "Invalid web server: $INSTALL_WEB_SERVER"
3352fi
3353
3354[ -n "$FTP_PORT" ] && [ "$FTP_PORT" -lt 1 -o "$FTP_PORT" -gt 65535 ] && errorExit "Invalid FTP port: $FTP_PORT"
3355
3356if [ "$USE_RUTORRENT_PLUGIN" = y ]; then
3357 if [ -z "$INSTALL_WEB_SERVER" ] && ! isValidRutorrentBasePath "$RUTORRENT_BASE_PATH"; then
3358 errorExit "$RUTORRENT_BASE_PATH is not a valid ruTorrent base path."
3359 fi
3360fi
3361
3362if [ "$INSTALL_AUTODL_IRSSI" = y ]; then
3363 cat << EOF
3364${CMSG}Installing required tools and Perl modules...
3365Some Perl modules may not be present, but will be installed from CPAN.$CEND
3366EOF
3367else
3368 echo "${CMSG}Installing required tools...$CEND"
3369fi
3370osHandler_$os installTools
3371setSvnOpts
3372[ "$INSTALL_AUTODL_IRSSI" = y ] && osHandler_$os installAutodlTools
3373
3374verifyInstalledPrograms
3375
3376[ "$INSTALL_AUTODL_IRSSI" = y ] && installMissingPerlModules
3377
3378cat << EOF
3379$CMSG
3380All required programs and Perl modules are now installed. Ignore any errors you
3381saw.$CEND
3382EOF
3383
3384INSTALLED_RTORRENT=n
3385if [ "$BUILD_RTORRENT" = y ]; then
3386 cat << EOF
3387$CMSG
3388Building rtorrent and dependencies...$CEND
3389EOF
3390 installRtorrent
3391 INSTALLED_RTORRENT=y
3392fi
3393
3394if [ -z "$INSTALL_WEB_SERVER" ]; then
3395 :
3396elif [ "$INSTALL_WEB_SERVER" = apache ]; then
3397 echo ""
3398 echo "${CMSG}Installing Apache$CEND"
3399 osHandler_$os installApache
3400 installUnrar
3401elif [ "$INSTALL_WEB_SERVER" = nginx ]; then
3402 echo ""
3403 echo "${CMSG}Installing nginx$CEND"
3404 installNginx
3405elif [ "$INSTALL_WEB_SERVER" = lighttpd ]; then
3406 echo ""
3407 echo "${CMSG}Installing lighttpd$CEND"
3408 installLighttpd
3409else
3410 errorExit "Invalid web server: $INSTALL_WEB_SERVER"
3411fi
3412
3413if [ "$USE_RUTORRENT_PLUGIN" = y ]; then
3414 if isProgramInstalled php; then
3415 installMissingPhpModules
3416 else
3417 cat << EOF
3418$CWARNING
3419Could not find the php executable. PHP is not installed or the PHP CLI version
3420is not installed. The autodl-irssi ruTorrent plugin requires the following PHP
3421modules:
3422 $CGREEN$REQUIRED_PHP_MODULES$CWARNING
3423They're normally installed and enabled by default. If not you will need to
3424install them (if needed) and then enable each one in php.ini, eg.
3425extension=MODULE.so and then restart your web server.$CEND
3426EOF
3427 fi
3428fi
3429
3430if [ "$INSTALL_RUTORRENT" = y ]; then
3431 isProgramInstalled php || errorExit "php is not installed!"
3432 verifyWebServerVars
3433 echo ""
3434 echo "${CMSG}Installing ruTorrent$CEND"
3435 [ -d "$WWW_ROOT" ] || errorExit "Invalid web root: '$WWW_ROOT' (does not exist)"
3436 RUTORRENT_DIRNAME=rutorrent
3437
3438 [ -f "$WWW_ROOT/index.html" ] || cat > "$WWW_ROOT/index.html" << EOF
3439<html><head><title>Root page</title></head><body>
3440<a href="$RUTORRENT_DIRNAME/">ruTorrent</a>
3441</body></html>
3442EOF
3443
3444 cd "$WWW_ROOT"
3445 rm -rf "$RUTORRENT_DIRNAME"
3446 tmpName=rutorrent-tmp
3447 rm -rf $tmpName
3448 if ! git clone "$RUTORRENT_TRUNK_DIR" $tmpName/rutorrent >/dev/null; then
3449 # Subversion failed. Try the official tar balls
3450 mkdir -p $tmpName
3451 cd $tmpName
3452 downloadFile "$RUTORRENT_CORE_NAME" "$RUTORRENT_CORE_URL" "$RUTORRENT_CORE_URL2" || errorExit "Could not download ruTorrent."
3453 downloadFile "$RUTORRENT_PLUGINS_NAME" "$RUTORRENT_PLUGINS_URL" "$RUTORRENT_PLUGINS_URL2" || errorExit "Could not download ruTorrent plugins."
3454 for name in "$RUTORRENT_CORE_NAME" "$RUTORRENT_PLUGINS_NAME"; do
3455 tar xzf "$name" || errorExit "Could not unpack $name"
3456 rm -f "$name"
3457 done
3458 cd ..
3459 fi
3460 cd $tmpName
3461 RUTORRENT_BASE_PATH="$WWW_ROOT/$RUTORRENT_DIRNAME"
3462 initPluginDirVar
3463 mv rutorrent/plugins/ .
3464 mv rutorrent/ "$RUTORRENT_BASE_PATH"
3465 mkdir -p "$RUTORRENT_BASE_PATH/plugins"
3466 for plugin in $RUTORRENT_PLUGINS; do
3467 if [ -d "plugins/$plugin/" ]; then
3468 echo "${CMSG}Installing ruTorrent plugin: $plugin$CEND..."
3469 mv plugins/$plugin/ "$RUTORRENT_BASE_PATH/plugins/" || errorExit "Could not install plugin '$plugin'."
3470 else
3471 echo "${CWARNING}Can't install missing plugin $plugin!$CEND"
3472 fi
3473 done
3474 cd ..
3475 rm -rf $tmpName
3476 cp "$RUTORRENT_BASE_PATH/favicon.ico" "$WWW_ROOT"
3477
3478 i=1
3479 touch "$WWW_PASSWORD_FILE"
3480 resetAuthPasswordFilePermissions
3481 detectHtpasswd
3482 for packedUser in $USERS; do
3483 extractPackedUser $packedUser
3484 getRutorrentUserConfDir
3485 getRutorrentUserShareDir
3486 getUserScgiSocketPath "$osUser"
3487 getRtorrentDirs "$osUser"
3488
3489 # Create user's ruTorrent config.php
3490 rpcMount="$(getUserRpcMount $i)"
3491 mkdir -p "$userConfDir"
3492
3493 rutConfigFile="$userConfDir/config.php"
3494 if [ "$SCGI_USE_UNIX_DOMAIN_SOCKET" = y ]; then
3495 cat > "$rutConfigFile" << EOF
3496<?php
3497\$scgi_port = 0;
3498\$scgi_host = "unix://$scgiSocketPath";
3499\$XMLRPCMountPoint = "$rpcMount";
3500?>
3501EOF
3502 exitCode=$?
3503 else
3504 cat > "$rutConfigFile" << EOF
3505<?php
3506\$scgi_port = $scgiPort;
3507\$scgi_host = "$SCGI_HOST";
3508\$XMLRPCMountPoint = "$rpcMount";
3509?>
3510EOF
3511 exitCode=$?
3512 fi
3513 [ $exitCode -eq 0 ] || errorExit "Could not write to file $rutConfigFile"
3514
3515 # Create user's directory to prevent certain errors the first time ruTorrent is started
3516 mkdir -p "$userShareDir/settings"
3517 mkdir -p "$userShareDir/torrents"
3518 chmod 0777 "$userShareDir/settings" "$userShareDir/torrents"
3519
3520 # Setup rtorrent
3521 mkdir -p "$RTORRENT_DOWNLOAD_DIR"
3522 mkdir -p "$RTORRENT_WATCH_DIR"
3523 mkdir -p "$RTORRENT_SESSION_DIR"
3524 rtorrentRc="$userDir/.rtorrent.rc"
3525 [ -f "$rtorrentRc" ] && mv -f "$rtorrentRc" "$rtorrentRc-backup"
3526 cat > "$rtorrentRc" << EOF
3527$(if [ "$SCGI_USE_UNIX_DOMAIN_SOCKET" = y ]; then
3528 cat << EOF2
3529execute = {sh,-c,rm -f $scgiSocketPath}
3530scgi_local = $scgiSocketPath
3531execute = {sh,-c,chmod 0666 $scgiSocketPath}
3532EOF2
3533else
3534 echo "scgi_port = $SCGI_HOST:$scgiPort"
3535fi)
3536encoding_list = UTF-8
3537system.umask.set = $DEFAULT_UMASK
3538port_range = $rtorrentPort-$rtorrentPort
3539port_random = no
3540check_hash = no
3541directory = $RTORRENT_DOWNLOAD_DIR
3542session = $RTORRENT_SESSION_DIR
3543encryption = allow_incoming, try_outgoing, enable_retry
3544schedule = watch_directory,1,1,"load_start=$RTORRENT_WATCH_DIR/*.torrent"
3545#schedule = untied_directory,5,5,"stop_untied=$RTORRENT_WATCH_DIR/*.torrent"
3546trackers.enable = 1
3547#min_peers = 40
3548#max_peers = 100
3549#min_peers_seed = 10
3550#max_peers_seed = 50
3551#max_uploads = 15
3552#download_rate = 0
3553#upload_rate = 0
3554use_udp_trackers = yes
3555dht = auto
3556dht_port = 6881
3557peer_exchange = yes
3558#hash_read_ahead = 10
3559#hash_interval = 100
3560#hash_max_tries = 10
3561EOF
3562 PHP_BIN_PATH=$(which php 2> /dev/null)
3563 COMMENT=
3564 [ -x "$PHP_BIN_PATH" ] || COMMENT="#" PHP_BIN_PATH=/path/to/php
3565 cat >> "$rtorrentRc" << EOF
3566${COMMENT}execute = {sh,-c,$PHP_BIN_PATH $RUTORRENT_BASE_PATH/php/initplugins.php $webUser &}
3567EOF
3568
3569 # Add user to web server's password file
3570 sed_i "g/^$webUser:/d" "$WWW_PASSWORD_FILE"
3571 $htpasswd -b "$WWW_PASSWORD_FILE" "$webUser" "$webPass" || errorExit "Could not add user to password file"
3572 resetAuthPasswordFilePermissions
3573
3574 resetOwner "$osUser" "$rtorrentRc" "$userDir" "$RTORRENT_DOWNLOAD_DIR" "$RTORRENT_WATCH_DIR" "$RTORRENT_SESSION_DIR"
3575
3576 # Required so some ruTorrent plugins work, eg. _getdir
3577 chmod 0755 "$userDir"
3578
3579 # Protect some dirs and files, giving only the user and the web server
3580 # access. We need to do this since we set perms to 0755 above, or if
3581 # the perms already were 0755 to begin with.
3582 if [ -n "$WWW_GROUP" ]; then
3583 chmod 0600 "$rtorrentRc"
3584 for dir in $RTORRENT_REL_DOWNLOAD_DIR $RTORRENT_REL_WATCH_DIR $RTORRENT_REL_SESSION_DIR; do
3585 chown $osUser:$WWW_GROUP "$userDir/$dir"
3586 chmod 0770 "$userDir/$dir"
3587 done
3588 fi
3589
3590 i=$(expr $i + 1)
3591 done
3592
3593 [ "$CREATE_ONE_PASSWORD_FILE_PER_USER" = y ] && createOnePasswordFilePerUser
3594fi
3595
3596if [ "$USE_RUTORRENT_PLUGIN" = y ]; then
3597 [ -z "$RUTORRENT_BASE_PATH" ] && errorExit "Invalid ruTorrent base path."
3598
3599 [ "$REINSTALL_RUTORRENT_PLUGIN" = y ] && rm -rf "$AUTODL_IRSSI_PLUGIN_DIR"
3600 mkdir -p "$RUTORRENT_BASE_PATH/plugins"
3601 cd "$RUTORRENT_BASE_PATH/plugins"
3602
3603 if [ -d "$AUTODL_IRSSI_PLUGIN_DIR" ]; then
3604 echo "${CMSG}The autodl-irssi ruTorrent plugin dir already exists. Updating it...$CEND"
3605 cd "$AUTODL_IRSSI_PLUGIN_DIR"
3606 if ! svn up $SVN_OPTS > /dev/null; then
3607 errorExit "Could not update the autodl-irssi ruTorrent plugin. Run the script as root."
3608 fi
3609 else
3610 echo "${CMSG}Downloading the autodl-irssi ruTorrent plugin...$CEND"
3611 if ! git clone "$GIT_PATH_RUTORRENT_PLUGIN" autodl-irssi > /dev/null; then
3612 errorExit "Could not check out the autodl-irssi ruTorrent plugin"
3613 fi
3614 fi
3615fi
3616
3617if [ "$INSTALL_AUTODL_IRSSI" = y ]; then
3618 if [ "$RUTORRENT_PASSWORD_PROTECTED" = y ]; then
3619 for packedUser in $USERS; do
3620 extractPackedUser $packedUser
3621 installUser "$RUTORRENT_BASE_PATH/conf/users/$webUser/plugins/autodl-irssi" "$osUser" "$webUser" "$autodlPort" "$autodlPassword"
3622 done
3623 else
3624 for packedUser in $USERS; do
3625 extractPackedUser $packedUser
3626 installUser "$AUTODL_IRSSI_PLUGIN_DIR" "$osUser" "$webUser" "$autodlPort" "$autodlPassword"
3627 [ "$USE_RUTORRENT_PLUGIN" = y ] && break
3628 done
3629 fi
3630fi
3631
3632if [ "$INSTALL_RUTORRENT" = y ]; then
3633 verifyWebServerVars
3634 resetWebServerPermissions
3635 resetRutorrentUserPermissions
3636 # Restart it just in case we enabled/installed PHP modules.
3637 osHandler_$os restart_$INSTALL_WEB_SERVER
3638fi
3639
3640if [ "$INSTALL_VSFTPD" = y ]; then
3641 echo "${CMSG}Installing vsftpd...$CEND"
3642 osHandler_$os installVsftpd
3643fi
3644
3645if [ "$INSTALL_WEBMIN" = y ]; then
3646 cd
3647 rm -rf webmin-*/ webmin*.tar.gz
3648 if ! downloadFile webmin.tar.gz "$WEBMIN_URL"; then
3649 errorExit "Could not download Webmin."
3650 fi
3651 tar xzf webmin.tar.gz
3652 rm -f webmin.tar.gz
3653 cd webmin-*/ || errorExit "Could not CD to webmin dir"
3654 osHandler_$os preWebminInstall
3655 [ -x "setup.sh" ] || errorExit "Missing Webmin setup.sh file or not executable."
3656 cat << EOF
3657$CMSG
3658Starting Webmin installer. Use another port than 10000, enable SSL, start webmin
3659at boot, and use a strong admin password.$CEND
3660$CWARNING
3661When it asks you if it should use SSL and whether it should start at boot,
3662type y.$CEND
3663EOF
3664 ./setup.sh /usr/local/webmin
3665 osHandler_$os postWebminInstall
3666 cd
3667 rm -rf webmin-*/
3668fi
3669
3670if [ "$INSTALL_RUTORRENT" = y ]; then
3671 # Some plugins will fail unless they can write to the /tmp directory
3672 # We MUST do this AFTER installing Webmin since it will reset the perms!
3673 chmod 1777 /tmp
3674fi
3675
3676if [ "$INSTALL_STARTUP_SCRIPT" = y ]; then
3677 for packedUser in $USERS; do
3678 extractPackedUser $packedUser
3679 echo "${CMSG}Installing service for user $osUser.$CEND"
3680 osHandler_$os installAutodlService $osUser
3681 done
3682fi
3683
3684echo ""
3685echo "${CGREEN}================================= DONE =================================$CEND"
3686[ "$INSTALLED_RTORRENT" = y ] && echo "${CDGREEN}[+]$CEND ${CGREEN}Built and installed rtorrent with XML-RPC support$CEND"
3687[ -n "$INSTALL_WEB_SERVER" ] && echo "${CDGREEN}[+]$CEND ${CGREEN}Installed and configured web server ($INSTALL_WEB_SERVER)$CEND"
3688[ "$INSTALL_VSFTPD" = y ] && echo "${CDGREEN}[+]$CEND ${CGREEN}Installed and configured FTP server (vsftpd)$CEND"
3689[ "$INSTALL_RUTORRENT" = y ] && echo "${CDGREEN}[+]$CEND ${CGREEN}Installed ruTorrent$CEND"
3690[ "$USE_RUTORRENT_PLUGIN" = y ] && echo "${CDGREEN}[+]$CEND ${CGREEN}Installed/updated the autodl-irssi ruTorrent plugin$CEND"
3691[ "$INSTALL_AUTODL_IRSSI" = y ] && echo "${CDGREEN}[+]$CEND ${CGREEN}Installed autodl-irssi$CEND"
3692[ "$INSTALL_STARTUP_SCRIPT" = y ] && echo "${CDGREEN}[+]$CEND ${CGREEN}Installed and started Irssi and rtorrent service$CEND"
3693[ "$INSTALL_WEBMIN" = y ] && echo "${CDGREEN}[+]$CEND ${CGREEN}Installed Webmin$CEND"
3694
3695if [ "$INSTALL_RUTORRENT" = y ]; then
3696 getIpAddress
3697 cat << EOF
3698
3699${CMSG}ruTorrent URLs.$CEND ${CWARNING}Verify that the IP address below is correct!$CEND
3700 ${CMAGENTA}http://$OUR_IP_ADDRESS/$RUTORRENT_DIRNAME/$CEND
3701 ${CMAGENTA}https://$OUR_IP_ADDRESS/$RUTORRENT_DIRNAME/$CEND
3702EOF
3703fi
3704
3705if [ "$INSTALL_RUTORRENT" = y ]; then
3706 cat << EOF
3707
3708${CMSG}rtorrent directories$CEND:
3709EOF
3710 for packedUser in $USERS; do
3711 extractPackedUser $packedUser
3712 getRtorrentDirs "$osUser"
3713 cat << EOF
3714${CMSG}User $CGREEN$osUser$CEND:
3715 ${CGREEN}Downloads$CEND : $CMAGENTA$RTORRENT_DOWNLOAD_DIR$CEND
3716 ${CGREEN}Watch dir$CEND : $CMAGENTA$RTORRENT_WATCH_DIR$CEND
3717 ${CGREEN}Session dir$CEND : $CMAGENTA$RTORRENT_SESSION_DIR$CEND
3718 ${CGREEN}rtorrent port$CEND: $CMAGENTA$rtorrentPort$CEND
3719EOF
3720 done
3721fi
3722
3723if [ -n "$INSTALL_WEB_SERVER" ]; then
3724 cat << EOF
3725
3726${CMSG}Web server info$CEND:
3727 ${CGREEN}Web server root$CEND: ${CMAGENTA}$WWW_ROOT$CEND
3728 ${CGREEN}ruTorrent dir$CEND : ${CMAGENTA}$RUTORRENT_BASE_PATH$CEND
3729EOF
3730fi
3731
3732if [ "$INSTALL_VSFTPD" = y ]; then
3733 getIpAddress
3734 cat << EOF
3735$CMSG
3736To log in to the FTP server, use your $(uname -s) login name and password. I
3737guessed the IP address below. Make sure it's correct.$CEND
3738 ${CGREEN}FTP IP address$CEND: ${CMAGENTA}$OUR_IP_ADDRESS$CEND
3739 ${CGREEN}FTP port$CEND: ${CMAGENTA}$FTP_PORT$CEND
3740 ${CGREEN}FTP server type$CEND: $CMAGENTA$FTP_SERVER_TYPE$CEND
3741EOF
3742fi
3743
3744if [ "$INSTALL_WEBMIN" = y ]; then
3745 getIpAddress
3746
3747 WEBMIN_PORT=
3748 WEBMIN_SSL=
3749 WEBMIN_NAME=
3750 WEBMIN_CONFIG=/etc/webmin/miniserv.conf
3751 WEBMIN_USERS=/etc/webmin/miniserv.users
3752 if [ -f $WEBMIN_CONFIG ]; then
3753 WEBMIN_PORT=$(grep '^port=' $WEBMIN_CONFIG | tail -n1 | sed -e 's/^port=\([0-9]*\).*/\1/')
3754 WEBMIN_SSL=$(grep '^ssl=' $WEBMIN_CONFIG | tail -n1 | sed -e 's/^ssl=\([0-9]*\).*/\1/')
3755 WEBMIN_NAME=$(head -n1 $WEBMIN_USERS | cut -d: -f1)
3756 fi
3757 WEBMIN_PORT=${WEBMIN_PORT:-UNKNOWN}
3758 WEBMIN_NAME=${WEBMIN_NAME:-UNKNOWN}
3759
3760 cat << EOF
3761$CMSG
3762Webmin was installed. Port is $WEBMIN_PORT and the user is $WEBMIN_NAME.
3763${CRED}Verify the IP address below.$CEND
3764EOF
3765 if [ -z "$WEBMIN_SSL" ]; then
3766 echo " ${CMAGENTA}http://$OUR_IP_ADDRESS:$WEBMIN_PORT/$CEND"
3767 echo "${CMSG}or:$CEND"
3768 echo " ${CMAGENTA}https://$OUR_IP_ADDRESS:$WEBMIN_PORT/$CEND"
3769 elif [ "$WEBMIN_SSL" = 0 ]; then
3770 echo " ${CMAGENTA}http://$OUR_IP_ADDRESS:$WEBMIN_PORT/$CEND"
3771 else
3772 echo " ${CMAGENTA}https://$OUR_IP_ADDRESS:$WEBMIN_PORT/$CEND"
3773 fi
3774fi
3775
3776if [ -n "$INSTALL_WEB_SERVER" ] || [ "$INSTALL_VSFTPD" = y ] || \
3777 [ "$INSTALL_RUTORRENT" = y ] || [ "$USE_RUTORRENT_PLUGIN" = y ]; then
3778 if [ "$SELINUX_ENABLED" = y ]; then
3779 cat << EOF
3780$CWARNING
3781SELinux is enabled. It could cause some problems with the web server, vsftpd or
3782ruTorrent.
3783EOF
3784 fi
3785fi
3786
3787if [ "$CREATED_CERT_FILE" = y ]; then
3788 cat << EOF
3789$CWARNING
3790A self-signed certificate was created. Your browser or FTP client will most
3791likely warn you about a non-trusted certificate.$CEND
3792EOF
3793fi
3794
3795if [ "$USE_RUTORRENT_PLUGIN" = y ]; then
3796 if isProgramInstalled php; then
3797 detectMissingPhpModules
3798 if [ -n "$MISSING_PHP_MODULES" ]; then
3799 cat << EOF
3800$CWARNING
3801The following PHP modules seem to be missing. It's possible that the
3802autodl-irssi ruTorrent plugin won't work. Missing PHP modules:
3803 $MISSING_PHP_MODULES$CEND
3804EOF
3805 fi
3806 else
3807 cat << EOF
3808$CWARNING
3809Could not find the php executable. Make sure the following PHP modules are
3810enabled or the autodl-irssi ruTorrent plugin won't work:
3811 $REQUIRED_PHP_MODULES$CEND
3812EOF
3813 fi
3814fi