freedombone-app-webmail 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Webmail application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS=''
  31. WEBMAIL_REPO="https://github.com/roundcube/roundcubemail"
  32. WEBMAIL_COMMIT='78cf9c2e5cff2d78cd8beb647d60570f195a2a9c'
  33. WEBMAIL_ONION_HOSTNAME=
  34. WEBMAIL_ONION_PORT=8097
  35. WEBMAIL_ADMIN_PASSWORD=
  36. WEB_PATH=/var/www
  37. WEBMAIL_PATH=$WEB_PATH/webmail
  38. function reconfigure_webmail {
  39. echo -n ''
  40. }
  41. function upgrade_webmail {
  42. if ! grep -Fxq "install_webmail" $COMPLETION_FILE; then
  43. return
  44. fi
  45. function_check set_repo_commit
  46. set_repo_commit $WEBMAIL_PATH "Webmail commit" "$WEBMAIL_COMMIT" $WEBMAIL_REPO
  47. }
  48. function backup_local_webmail {
  49. if ! grep -q "install_webmail" $COMPLETION_FILE; then
  50. return
  51. fi
  52. echo $"Backing up webmail"
  53. function_check backup_database_to_usb
  54. backup_database_to_usb webmail
  55. echo $"Backing up webmail complete"
  56. }
  57. function restore_local_webmail {
  58. if ! grep -q "install_webmail" $COMPLETION_FILE; then
  59. return
  60. fi
  61. echo $"Restoring webmail"
  62. function_check restore_database
  63. restore_database webmail
  64. echo $"Restore of webmail complete"
  65. }
  66. function backup_remote_webmail {
  67. echo -n ''
  68. }
  69. function restore_remote_webmail {
  70. echo -n ''
  71. }
  72. function remove_webmail {
  73. if ! grep -Fxq "install_webmail" $COMPLETION_FILE; then
  74. return
  75. fi
  76. nginx_dissite webmail
  77. if [ -f /etc/nginx/sites-available/webmail ]; then
  78. rm /etc/nginx/sites-available/webmail
  79. fi
  80. function_check remove_onion_service
  81. remove_onion_service webmail ${WEBMAIL_ONION_PORT}
  82. function_check drop_database
  83. drop_database webmail
  84. if [ -d $WEB_PATH/webmail ]; then
  85. rm -rf $WEB_PATH/webmail
  86. fi
  87. sed -i '/Webmail /d' $COMPLETION_FILE
  88. sed -i '/install_webmail/d' $COMPLETION_FILE
  89. }
  90. function install_webmail {
  91. if grep -Fxq "install_webmail" $COMPLETION_FILE; then
  92. return
  93. fi
  94. if [ -d /etc/apache2 ]; then
  95. rm -rf /etc/apache2
  96. echo $'Removed Apache installation after Dokuwiki install'
  97. fi
  98. function_check install_mariadb
  99. install_mariadb
  100. function_check get_mariadb_password
  101. get_mariadb_password
  102. function_check repair_databases_script
  103. repair_databases_script
  104. apt-get -y install php5-common php5-cli php5-curl php5-gd php5-mysql php5-mcrypt git
  105. apt-get -y install php5-dev imagemagick php5-imagick php5-sqlite php-auth-sasl php-net-smtp php-mime-type
  106. apt-get -y install php-mail-mime php-mail-mimedecode php-net-ldap3 php5-pspell
  107. pear install Net_IDNA2
  108. if [ ! -f $WEBMAIL_PATH/index.php ]; then
  109. cd $WEB_PATH
  110. git_clone $WEBMAIL_REPO webmail
  111. cd $WEBMAIL_PATH
  112. git checkout $WEBMAIL_COMMIT -b $WEBMAIL_COMMIT
  113. if ! grep -q "Webmail commit" $COMPLETION_FILE; then
  114. echo "Webmail commit:$WEBMAIL_COMMIT" >> $COMPLETION_FILE
  115. else
  116. sed -i "s/Webmail commit.*/Webmail commit:$WEBMAIL_COMMIT/g" $COMPLETION_FILE
  117. fi
  118. fi
  119. if [ ! -f $WEBMAIL_PATH/index.php ]; then
  120. echo $'Did not clone webmail repo'
  121. exit 52825
  122. fi
  123. WEBMAIL_ONION_HOSTNAME=$(add_onion_service webmail 80 ${WEBMAIL_ONION_PORT})
  124. echo "Webmail onion domain:${WEBMAIL_ONION_HOSTNAME}" >> $COMPLETION_FILE
  125. function_check get_mariadb_webmail_admin_password
  126. get_mariadb_webmail_admin_password
  127. if [ ! $WEBMAIL_ADMIN_PASSWORD ]; then
  128. if [ -f $IMAGE_PASSWORD_FILE ]; then
  129. WEBMAIL_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  130. else
  131. WEBMAIL_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  132. fi
  133. fi
  134. function_check create_database
  135. create_database webmail "$WEBMAIL_ADMIN_PASSWORD"
  136. mysql -u root --password="$MARIADB_PASSWORD" -D webmail < $WEBMAIL_PATH/SQL/mysql.initial.sql
  137. if [ ! -d /var/www/$DEFAULT_DOMAIN_NAME/htdocs ]; then
  138. mkdir -p /var/www/$DEFAULT_DOMAIN_NAME/htdocs
  139. fi
  140. ln -s $WEBMAIL_PATH /var/www/$DEFAULT_DOMAIN_NAME/htdocs/webmail
  141. if [ ! -f /var/www/webmail/config/config.inc.php ]; then
  142. # generate the configuration
  143. echo '<?php' > /var/www/webmail/config/config.inc.php
  144. echo "\$config['db_dsnw'] = 'mysql://root:${MARIADB_PASSWORD}@localhost/webmail';" >> /var/www/webmail/config/config.inc.php
  145. echo "\$config['default_host'] = 'localhost';" >> /var/www/webmail/config/config.inc.php
  146. echo "\$config['support_url'] = '';" >> /var/www/webmail/config/config.inc.php
  147. WEBMAIL_DES_KEY="$(create_password 25)"
  148. echo "\$config['des_key'] = '${WEBMAIL_DES_KEY}';" >> /var/www/webmail/config/config.inc.php
  149. echo "\$config['product_name'] = '${PROJECT_NAME}';" >> /var/www/webmail/config/config.inc.php
  150. echo "\$config['plugins'] = array('enigma');" >> /var/www/webmail/config/config.inc.php
  151. echo "\$config['mime_param_folding'] = 0;" >> /var/www/webmail/config/config.inc.php
  152. echo "\$config['enable_installer'] = false;" >> /var/www/webmail/config/config.inc.php
  153. fi
  154. chown -R www-data:www-data $WEBMAIL_PATH
  155. echo 'server {' > /etc/nginx/sites-available/webmail
  156. echo " listen 127.0.0.1:$WEBMAIL_ONION_PORT default_server;" >> /etc/nginx/sites-available/webmail
  157. echo " server_name $WEBMAIL_ONION_HOSTNAME;" >> /etc/nginx/sites-available/webmail
  158. echo " root ${WEBMAIL_PATH};" >> /etc/nginx/sites-available/webmail
  159. echo ' index index.php index.html index.htm;' >> /etc/nginx/sites-available/webmail
  160. echo ' access_log off;' >> /etc/nginx/sites-available/webmail
  161. echo ' error_log off;' >> /etc/nginx/sites-available/webmail
  162. echo ' location / {' >> /etc/nginx/sites-available/webmail
  163. echo ' try_files $uri $uri/ /index.php?q=$uri&$args;' >> /etc/nginx/sites-available/webmail
  164. echo ' }' >> /etc/nginx/sites-available/webmail
  165. echo ' error_page 404 /404.html;' >> /etc/nginx/sites-available/webmail
  166. echo ' error_page 500 502 503 504 /50x.html;' >> /etc/nginx/sites-available/webmail
  167. echo ' location = /50x.html {' >> /etc/nginx/sites-available/webmail
  168. echo " root ${WEBMAIL_PATH};" >> /etc/nginx/sites-available/webmail
  169. echo ' }' >> /etc/nginx/sites-available/webmail
  170. echo ' location ~ ^/(README.md|INSTALL|LICENSE|CHANGELOG|UPGRADING)$ {' >> /etc/nginx/sites-available/webmail
  171. echo ' deny all;' >> /etc/nginx/sites-available/webmail
  172. echo ' }' >> /etc/nginx/sites-available/webmail
  173. echo ' location ~ ^/(config|temp|logs)/ {' >> /etc/nginx/sites-available/webmail
  174. echo ' deny all;' >> /etc/nginx/sites-available/webmail
  175. echo ' }' >> /etc/nginx/sites-available/webmail
  176. echo ' location ~ /\. {' >> /etc/nginx/sites-available/webmail
  177. echo ' deny all;' >> /etc/nginx/sites-available/webmail
  178. echo ' access_log off;' >> /etc/nginx/sites-available/webmail
  179. echo ' log_not_found off;' >> /etc/nginx/sites-available/webmail
  180. echo ' }' >> /etc/nginx/sites-available/webmail
  181. echo ' location ~ \.php$ {' >> /etc/nginx/sites-available/webmail
  182. echo ' try_files $uri =404;' >> /etc/nginx/sites-available/webmail
  183. echo ' fastcgi_pass unix:/var/run/php5-fpm.sock;' >> /etc/nginx/sites-available/webmail
  184. echo ' fastcgi_index index.php;' >> /etc/nginx/sites-available/webmail
  185. echo ' fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' >> /etc/nginx/sites-available/webmail
  186. echo ' include fastcgi_params;' >> /etc/nginx/sites-available/webmail
  187. echo ' }' >> /etc/nginx/sites-available/webmail
  188. echo ' add_header X-Frame-Options DENY;' >> /etc/nginx/sites-available/webmail
  189. echo ' add_header X-Content-Type-Options nosniff;' >> /etc/nginx/sites-available/webmail
  190. echo ' client_max_body_size 15m;' >> /etc/nginx/sites-available/webmail
  191. echo '}' >> /etc/nginx/sites-available/webmail
  192. nginx_ensite webmail
  193. systemctl restart php5-fpm
  194. systemctl restart nginx
  195. if ! grep -q "Webmail" /home/$MY_USERNAME/README; then
  196. echo '' >> /home/$MY_USERNAME/README
  197. echo '' >> /home/$MY_USERNAME/README
  198. echo 'Webmail' >> /home/$MY_USERNAME/README
  199. echo '=======' >> /home/$MY_USERNAME/README
  200. if [[ $ONION_ONLY == 'no' ]]; then
  201. echo $"Open https://$DEFAULT_DOMAIN_NAME/webmail/index.php" >> /home/$MY_USERNAME/README
  202. else
  203. echo $"Open http://$WEBMAIL_ONION_HOSTNAME" >> /home/$MY_USERNAME/README
  204. fi
  205. echo $"MariaDB webmail admin password: $WEBMAIL_ADMIN_PASSWORD" >> /home/$MY_USERNAME/README
  206. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  207. chmod 600 /home/$MY_USERNAME/README
  208. fi
  209. echo 'install_webmail' >> $COMPLETION_FILE
  210. }
  211. # NOTE: deliberately no exit 0