Advertisement
Guest User

Untitled

a guest
Jan 26th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.77 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Some bash script optimization for robustness (More information: www.davidpashley.com/articles/writing-robust-shell-scripts/)
  4. #Break if the script uses unset variables
  5. set -o nounset
  6. #Break if a command has a non-true return value
  7. set -o errexit
  8.  
  9. echo_info () {
  10. echo "--------------------------------------"
  11. echo "| INFO: $1"
  12. echo "--------------------------------------"
  13. }
  14.  
  15. echo_error () {
  16. echo "--------------------------------------"
  17. echo "| ERROR: $1"
  18. echo "--------------------------------------"
  19. }
  20.  
  21. is_installed () {
  22. #Check if a package is installed (More information: https://askubuntu.com/questions/319307/reliably-check-if-a-package-is-installed-or-not)
  23. if dpkg --get-selections | grep -q "^$1[[:space:]]*install$" >/dev/null; then
  24. echo_error "$1 is already installed"
  25. exit 1
  26. fi
  27. }
  28.  
  29. check_not_installed () {
  30.  
  31. echo_info "Check if some package is already installed. If this is the case the script stops because it could not be performed without risc."
  32.  
  33. is_installed apache2
  34. is_installed mysql-server-5.5
  35. is_installed owncloud
  36. is_installed fail2ban
  37.  
  38. }
  39.  
  40. check_root () {
  41. if [ "$(id -u)" != "0" ]; then
  42. echo_error "The script must be called as root user!"
  43. exit 1
  44. fi
  45. }
  46.  
  47. install_owncloud () {
  48.  
  49. cd /tmp
  50.  
  51. #Automatic installation of owncloud (More information: https://software.opensuse.org/download/package?project=isv:ownCloud:community&package=owncloud)
  52.  
  53. echo_info "Add package repository"
  54.  
  55. echo 'deb http://download.owncloud.org/download/repositories/8.2/Debian_8.0/ /' >> /etc/apt/sources.list.d/owncloud.list
  56.  
  57. wget -nv https://download.owncloud.org/download/repositories/8.2/Debian_8.0/Release.key -O Release.key
  58. apt-key add - < Release.key
  59. rm Release.key
  60.  
  61. echo_info "Install owncloud"
  62.  
  63. #Because a dependency the package mysql-server-5.5 is installed too. During the installation a prompt ask for the root password. The next lines set it with the value of the variable. (More information: http://www.microhowto.info/howto/perform_an_unattended_installation_of_a_debian_package.html)
  64. echo "mysql-server-5.5 mysql-server/root_password password $mysqlRootPw" | debconf-set-selections
  65. echo "mysql-server-5.5 mysql-server/root_password_again password $mysqlRootPw" | debconf-set-selections
  66.  
  67. apt-get update
  68. apt-get --assume-yes install owncloud
  69.  
  70. }
  71.  
  72. create_mysql_db () {
  73.  
  74. echo_info "Create mysql database for owncloud"
  75.  
  76. #Create a new database and user for owncloud (More information: http://www.bluepiccadilly.com/2011/12/creating-mysql-database-and-user-command-line-and-bash-script-automate-process)
  77. mysql=`which mysql`
  78.  
  79. Q1="CREATE DATABASE IF NOT EXISTS $ocDb;"
  80. Q2="GRANT USAGE ON *.* TO $ocDbUser@localhost IDENTIFIED BY '$ocDbUserPw';"
  81. Q3="GRANT ALL PRIVILEGES ON $ocDb.* TO $ocDbUser@localhost;"
  82. Q4="FLUSH PRIVILEGES;"
  83. SQL="${Q1}${Q2}${Q3}${Q4}"
  84.  
  85. $mysql -uroot -p$mysqlRootPw -e "$SQL"
  86.  
  87. }
  88.  
  89. patch_apache_filesize () {
  90.  
  91. echo_info "Patch apache configuration"
  92.  
  93. #Patch filesize in the htaccess (More Information: http://blog.webernetz.net/2015/07/15/yet-another-owncloud-installation-guide/)
  94. sed -i "s/php_value upload_max_filesize .*/php_value upload_max_filesize $maxFileSize/" /var/www/owncloud/.htaccess
  95. sed -i "s/php_value post_max_size .*/php_value post_max_size $maxFileSize/" /var/www/owncloud/.htaccess
  96. sed -i "s/php_value memory_limit .*/php_value memory_limit $maxFileSize/" /var/www/owncloud/.htaccess
  97.  
  98. /etc/init.d/apache2 restart
  99. }
  100.  
  101. configure_owncloud () {
  102.  
  103. echo_info "Configure owncloud"
  104.  
  105. #The data dir should not be under /var/www for security reasons, so the dir must be created (More information: https://doc.owncloud.org/server/8.1/admin_manual/configuration_server/harden_server.html)
  106. mkdir $ocDataDir
  107. chown -R ${htuser}:${htgroup} ${ocDataDir}/
  108.  
  109. #Use the cli for first configuration (More information: https://doc.owncloud.org/server/8.1/admin_manual/installation/command_line_installation.html)
  110. cd /var/www/owncloud
  111. sudo -u $htuser php occ maintenance:install --database "mysql" --database-name "$ocDb" --database-user "$ocDbUser" --database-pass "$ocDbUserPw" --admin-user "$ocAdminUser" --admin-pass "$ocAdminUserPw" --data-dir "$ocDataDir"
  112.  
  113. #Add the hostname and ip to the trusted domains, so that it could be reached from outside (More Information: https://doc.owncloud.org/server/8.1/admin_manual/installation/installation_wizard.html?highlight=trusted_domains#label-trusted-domains)
  114. sed -i "/.*0 => 'localhost',/a \ 1 => '$hostname',n 2 => '$ip'," /var/www/owncloud/config/config.php
  115.  
  116. /etc/init.d/apache2 restart
  117.  
  118. }
  119.  
  120. install_fail2ban () {
  121.  
  122. echo_info "Install fail2ban"
  123.  
  124. apt-get --assume-yes install fail2ban
  125.  
  126. #First configure the owncloud logfile
  127. logFileMasked=$(echo $logFile | sed 's///\//g')
  128. logTimezoneMasked=$(echo $logTimeZone | sed 's///\//g')
  129. sed -i "s/ 'logtimezone' => 'UTC',/ 'logtimezone' => '$logTimezoneMasked',n 'logfile' => '$logFileMasked',n 'loglevel' => '2',/" /var/www/owncloud/config/config.php
  130.  
  131. touch $logFile
  132. chown ${htuser}:${htgroup} $logFile
  133.  
  134. #Now configure fail2ban (More Information: http://www.rojtberg.net/711/secure-owncloud-server/, https://got-tty.org/archives/owncloud-6-sicherheit-durch-fail2ban.html)
  135. echo -e "[Definition]nfailregex={"app":"core","message":"Login failed: user '.*' , wrong password, IP:<HOST>","level":2,"time":".*"}n {"app":"core","message":"Login failed: '.*' (Remote IP: '<HOST>', X-Forwarded-For: '.*')","level":2,"time":".*"}n {"reqId":".*","remoteAddr":"<HOST>","app":"core","message":"Login failed: .*","level":2,"time":".*"}" > /etc/fail2ban/filter.d/owncloud.conf
  136.  
  137. echo -e "[owncloud]nenabled = truenfilter = owncloudnport = http,httpsnmaxretry = $maxRetrynlogpath = $logFile" >> /etc/fail2ban/jail.local
  138.  
  139. /etc/init.d/fail2ban restart
  140. }
  141.  
  142. enable_apache_ssl () {
  143.  
  144. echo_info "Enable and compel apache ssl with default self-signed certifiacte of debian"
  145.  
  146. #Uses the default self-signed certificate of debian (More information: https://doc.owncloud.org/server/8.1/admin_manual/installation/source_installation.html#enabling-ssl)
  147. a2enmod ssl
  148. a2ensite default-ssl
  149.  
  150. #Force https for every connection (More information: https://doc.owncloud.org/server/8.1/admin_manual/configuration_server/harden_server.html)
  151. a2enmod headers
  152. sed -i "/.*<VirtualHost.*/a \tServerName $hostnamentRedirect permanent / https://$hostname/" /etc/apache2/sites-available/000-default.conf
  153. sed -i "/.*<VirtualHost.*/a \ttServerName $hostnamenttHeader always add Strict-Transport-Security "max-age=15768000"" /etc/apache2/sites-available/default-ssl.conf
  154.  
  155. /etc/init.d/apache2 restart
  156. }
  157.  
  158. enable_apc_cache () {
  159.  
  160. echo_info "Enable apc cache"
  161. #Install and configure apcu (More information: https://owncloud.org/blog/making-owncloud-faster-through-caching/)
  162. apt-get --assume-yes install php-apc
  163. sed -i "s/);/ 'memcache.local' => '\OC\Memcache\APCu',n);/" /var/www/owncloud/config/config.php
  164.  
  165. /etc/init.d/apache2 restart
  166. }
  167.  
  168. #Read in the variables in an interacive mode. Too make it a little more comfortable, the following functions need to be defined.
  169.  
  170. #Read a value and set the default value as input (More Information: http://stackoverflow.com/questions/2642585/read-a-variable-in-bash-with-a-default-value)
  171. read_value () {
  172. unset value
  173. read -e -i $1 value
  174. }
  175.  
  176. #Hide the input and mask it with stars (More Information: http://stackoverflow.com/questions/1923435/how-do-i-echo-stars-when-reading-password-with-read) (Part of the read_pw function)
  177. read_pw_loop_masked () {
  178. unset password
  179. while IFS= read -p "$prompt" -r -s -n 1 char
  180. do
  181. if [[ $char == $'' ]]
  182. then
  183. break
  184. fi
  185. prompt='*'
  186. password+="$char"
  187. done
  188. echo
  189. }
  190.  
  191. #Ask for the password two times (Part of the read_pw function)
  192. read_pw_loop_compare () {
  193. prompt="Enter Password:"
  194. read_pw_loop_masked
  195. password1=$password
  196. prompt="Reenter Password:"
  197. read_pw_loop_masked
  198. password2=$password
  199. }
  200.  
  201. #Loop the password question until the two values match (Part of the read_pw function)
  202. read_pw_loop () {
  203. read_pw_loop_compare
  204. while [ "$password1" != "$password2" ] ; do
  205. echo "Please retype, because the passwords did not match."
  206. read_pw_loop_compare
  207. done
  208. password=$password1
  209. }
  210.  
  211. #Main function for reading a password
  212. read_pw () {
  213. echo $*
  214. read_pw_loop
  215.  
  216. }
  217.  
  218. ask_for_values () {
  219. echo_info "The script now ask for some values that are necessary for the installation."
  220. #You can customize the default values of the variables here or set a static value
  221.  
  222. #Hostname and IP (the command retrieve this information automatically in the case that only the standard ethernet interface is installed)
  223. hostname=`hostname`
  224. echo "Please enter the hostname (The automatically indentified value is filled in, but you can easily change it if it is wrong.):"
  225. read_value $hostname
  226. hostname=$value
  227. ip=`hostname -I`
  228. echo "Please enter the ip of the outside interface (The automatically indentified value is filled in, but you can easily change it if it is wrong.):"
  229. read_value $ip
  230. ip=$value
  231.  
  232. #Mysql configuration
  233. read_pw "Please enter a password for the root user of mysql:"
  234. mysqlRootPw=$password
  235.  
  236. echo "Please enter the name of the mysql database for owncloud (The default value is filled in, but you can easily change it.):"
  237. read_value owncloud
  238. ocDb=$value
  239.  
  240. echo "Please enter the name of the mysql user for owncloud (The default value is filled in, but you can easily change it.):"
  241. read_value owncloud
  242. ocDbUser=$value
  243.  
  244. read_pw "Please enter a password for the owncloud user of mysql:"
  245. ocDbUserPw=$password
  246.  
  247. #Apache2 configuration
  248. echo "Please enter the maximal size of files that could be uploaded to owncloud (The default value is filled in, but you can easily change it.):"
  249. read_value 1024M
  250. maxFileSize=$value
  251. #Typically user and group filled static
  252. htuser='www-data'
  253. htgroup='www-data'
  254.  
  255. #Owncloud configuration
  256.  
  257. echo "Please enter the name of the owncloud administrator(The default value is filled in, but you can easily change it.):"
  258. read_value admin
  259. ocAdminUser=$value
  260.  
  261. read_pw "Please enter a password for the owncloud administrator:"
  262. ocAdminUserPw=$password
  263.  
  264. echo "Please enter the path to the folder for files of owncloud (The default value is filled in, but you can easily change it.):"
  265. read_value /home/owncloud
  266. ocDataDir=$value
  267.  
  268. #Fail2Ban
  269. logTimeZone=`cat /etc/timezone`
  270. echo "Please enter the time zone for the owncloud log (The default value is filled in, but you can easily change it.):"
  271. read_value $logTimeZone
  272. logTimeZone=$value
  273.  
  274. echo "Please enter the path where owncloud log should be saved (The default value is filled in, but you can easily change it.):"
  275. read_value /var/log/owncloud.log
  276. logFile=$value
  277.  
  278. echo "Please enter the max fails until fail2ban ban an ip (The default value is filled in, but you can easily change it.):"
  279. read_value 3
  280. maxRetry=$value
  281.  
  282. }
  283.  
  284. generate_self_signed_certificate () {
  285.  
  286. echo_info "The script now generate a self signed certificate with a self created ca. For it the script ask for many values. The most necessary you can fill with a dot if you do not have a valid value. The 'extra' attributes you could skip with enter. Only the pass phrase for own-ca.key and the common name are important. The first value is the password for your ca, please type in a strong password and take a note of it. You will need it at least some seconds later. The common name must befit the hostname and you must type it in two times."
  287.  
  288. #Generate a self signed certificate with a self created ca (More Information: https://thomas-leister.de/internet/eine-eigene-openssl-ca-erstellen-und-self-signed-certe-ausstellen/)
  289. # This was necessary because otherwise Apps like DAVdroid didn't work with the owncloud (More Information: https://davdroid.bitfire.at/faq/entry/importing-a-certificate)
  290.  
  291. #Create a new ca
  292. mkdir /etc/ssl/ownca/
  293. cd /etc/ssl/ownca/
  294. openssl genrsa -aes256 -out own-ca.key 2048
  295. #Create the root certificate that is valid for 10 years
  296. openssl req -x509 -new -nodes -extensions v3_ca -key own-ca.key -days 3650 -out own-ca-root.pem -sha512
  297. #Create a client certificate that is valid for 10 years and sign it
  298. openssl genrsa -out self-signed-cert.key 4096
  299. openssl req -new -key self-signed-cert.key -out self-signed-cert.csr -sha512
  300. openssl x509 -req -in self-signed-cert.csr -CA own-ca-root.pem -CAkey own-ca.key -CAcreateserial -out self-signed-cert.pem -days 3650 -sha512
  301. rm self-signed-cert.csr
  302. mv ./self-signed-cert.pem ../certs
  303. mv ./self-signed-cert.key ../private
  304. mv ./own-ca-root.pem ../certs
  305.  
  306. }
  307.  
  308. install_self_signed_certificate () {
  309. sed -i "s/ SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem/ SSLCertificateFile /etc/ssl/certs/self-signed-cert.pem/" /etc/apache2/sites-available/default-ssl.conf
  310. sed -i "s/ SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key/ SSLCertificateKeyFile /etc/ssl/private/self-signed-cert.key/" /etc/apache2/sites-available/default-ssl.conf
  311. sed -i "s/ #SSLCertificateChainFile /etc/apache2/ssl.crt/server-ca.crt/ SSLCertificateChainFile /etc/ssl/certs/own-ca-root.pem/" /etc/apache2/sites-available/default-ssl.conf
  312. /etc/init.d/apache2 restart
  313. }
  314.  
  315. #You can customize the called functions here (you are responsible for looking for dependencies between them)
  316. check_root
  317. check_not_installed
  318. ask_for_values
  319. generate_self_signed_certificate
  320. echo_info "Now the unattended part of the setup is started."
  321. install_owncloud
  322. create_mysql_db
  323. patch_apache_filesize
  324. configure_owncloud
  325. install_fail2ban
  326. enable_apache_ssl
  327. enable_apc_cache
  328. install_self_signed_certificate
  329.  
  330. echo_info "FINISH"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement